194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/*
2c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee * Copyright (C) 2013 The Android Open Source Project
394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * you may not use this file except in compliance with the License.
694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * You may obtain a copy of the License at
794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
1094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Unless required by applicable law or agreed to in writing, software
1194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
1294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * See the License for the specific language governing permissions and
1494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * limitations under the License.
1594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
1694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
1794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengpackage com.android.dialer;
1894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
19c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.animation.Animator;
20c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.animation.Animator.AnimatorListener;
21c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.animation.AnimatorListenerAdapter;
2294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.app.Activity;
2394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.app.Fragment;
2494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.app.FragmentManager;
25b207f8a073e8b3c001986b31c951f2c951cc631bYorke Leeimport android.app.FragmentManager.BackStackEntry;
2694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.app.FragmentTransaction;
27073b5cf341964796ccf082d30aff63225d3adb28Chiao Chengimport android.content.ActivityNotFoundException;
2894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.Context;
2994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.Intent;
3041a24b477dd0c550eda011a1b591603a20b980beYorke Leeimport android.graphics.drawable.Drawable;
3194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.net.Uri;
3294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.Bundle;
3394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.RemoteException;
3494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.ServiceManager;
3594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.CallLog.Calls;
3694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.ContactsContract.Contacts;
37e142481570d7fbda5d035555fe217314e396ae90Yorke Leeimport android.provider.ContactsContract.Intents;
3894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.ContactsContract.Intents.UI;
39c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.speech.RecognizerIntent;
40c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.telephony.TelephonyManager;
41c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.text.Editable;
4241a24b477dd0c550eda011a1b591603a20b980beYorke Leeimport android.text.Spannable;
4341a24b477dd0c550eda011a1b591603a20b980beYorke Leeimport android.text.SpannableStringBuilder;
4494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.text.TextUtils;
45c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.text.TextWatcher;
4641a24b477dd0c550eda011a1b591603a20b980beYorke Leeimport android.text.style.ImageSpan;
4794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.util.Log;
4894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.Menu;
4994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.MenuItem;
5094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.View;
5194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.View.OnFocusChangeListener;
5294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.inputmethod.InputMethodManager;
53c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.widget.AbsListView.OnScrollListener;
54c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.widget.EditText;
5594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.PopupMenu;
56073b5cf341964796ccf082d30aff63225d3adb28Chiao Chengimport android.widget.Toast;
5794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
589d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Chengimport com.android.contacts.common.CallUtil;
59603ff6858d321e8265b3af6363a63df2827e0334Chiao Chengimport com.android.contacts.common.activity.TransactionSafeActivity;
60c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.contacts.common.dialog.ClearFrequentsDialog;
61c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.contacts.common.interactions.ImportExportDialogFragment;
628efbcf920eeebc826c18b3c474e9c62b77dcd232Chiao Chengimport com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
63c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.calllog.CallLogActivity;
64fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Leeimport com.android.dialer.database.DialerDatabaseHelper;
65f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Leeimport com.android.dialer.dialpad.DialpadFragment;
66c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.dialpad.SmartDialNameMatcher;
67fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Leeimport com.android.dialer.dialpad.SmartDialPrefix;
681429f1a5c2b993a5013bfda7cfd75a2c43ead2bfChiao Chengimport com.android.dialer.interactions.PhoneNumberInteraction;
698dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport com.android.dialer.list.AllContactsActivity;
70c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.list.OnListFragmentScrolledListener;
71b453e5b0ce106987c75901321ceba13ed111222cAlon Albertimport com.android.dialer.list.PhoneFavoriteFragment;
722e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shraunerimport com.android.dialer.list.RegularSearchFragment;
732e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shraunerimport com.android.dialer.list.SearchFragment;
74c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.list.SmartDialSearchFragment;
750baa98b5163f83a715b37e3cffe1488ac88ab049Yorke Leeimport com.android.dialerbind.DatabaseHelperManager;
7694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.internal.telephony.ITelephony;
7794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
78c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport java.util.ArrayList;
79c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
8094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/**
8194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * The dialer tab's title is 'phone', a more common name (see strings.xml).
8294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
83c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leepublic class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
84f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee        DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
85c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        OnListFragmentScrolledListener,
868dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        DialpadFragment.OnDialpadFragmentStartedListener,
878dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        PhoneFavoriteFragment.OnShowAllContactsListener {
8894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String TAG = "DialtactsActivity";
8994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
9094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static final boolean DEBUG = false;
9194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
92f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee    public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
93f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee
9494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Used to open Call Setting */
9594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String PHONE_PACKAGE = "com.android.phone";
9694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String CALL_SETTINGS_CLASS_NAME =
9794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            "com.android.phone.CallFeaturesSetting";
9894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** @see #getCallOrigin() */
9994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String CALL_ORIGIN_DIALTACTS =
10094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            "com.android.dialer.DialtactsActivity";
10194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
102b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
103b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
104ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private static final String KEY_SEARCH_QUERY = "search_query";
105ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private static final String KEY_FIRST_LAUNCH = "first_launch";
106ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
107c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
108c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
109c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
110c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_FAVORITES_FRAGMENT = "favorites";
111c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
11294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
11394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
11494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
11594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
11694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
11794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
11894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
119c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
12094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
121c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private String mFilterText;
12294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
12394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
124c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * The main fragment displaying the user's favorites and frequent contacts
12594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
1268898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee    private PhoneFavoriteFragment mPhoneFavoriteFragment;
127c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
12894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
129c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment containing the dialpad that slides into view
13094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
131f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee    private DialpadFragment mDialpadFragment;
13294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
133c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /**
134c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment for searching phone numbers using the alphanumeric keyboard.
135c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     */
1362e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner    private RegularSearchFragment mRegularSearchFragment;
13794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
138c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /**
139c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment for searching phone numbers using the dialpad.
140c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     */
141c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private SmartDialSearchFragment mSmartDialSearchFragment;
14294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
14394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private View mMenuButton;
144c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mCallHistoryButton;
145c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mDialpadButton;
14636c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee    private PopupMenu mOverflowMenu;
14794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
148c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    // Padding view used to shift the fragments up when the dialpad is shown.
149c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mBottomPaddingView;
150b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private View mFragmentsFrame;
1515f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee    private View mActionBar;
152b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
153b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean mInDialpadSearch;
154b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean mInRegularSearch;
1558e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee    private boolean mClearSearchOnPause;
15694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
157ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    /**
15835127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee     * True if the dialpad is only temporarily showing due to being in call
15935127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee     */
16035127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee    private boolean mInCallDialpadUp;
16135127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee
16235127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee    /**
163ef2b7388569a94432278a617b528f628385fbb8cYorke Lee     * True when this activity has been launched for the first time.
164ef2b7388569a94432278a617b528f628385fbb8cYorke Lee     */
16598702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee    private boolean mFirstLaunch;
166c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mSearchViewContainer;
167c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mSearchViewCloseButton;
168c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mVoiceSearchButton;
169c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private EditText mSearchView;
17094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
171ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private String mSearchQuery;
172ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
173fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee    private DialerDatabaseHelper mDialerDatabaseHelper;
174fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee
17536c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee    private class OverflowPopupMenu extends PopupMenu {
17636c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        public OverflowPopupMenu(Context context, View anchor) {
17736c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            super(context, anchor);
17836c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        }
17936c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee
18036c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        @Override
18136c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        public void show() {
18236c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            final Menu menu = getMenu();
18336c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
18436c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
18536c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            super.show();
18636c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        }
18736c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee    }
18836c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee
18994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
19094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Listener used when one of phone numbers in search UI is selected. This will initiate a
19194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * phone call using the phone number.
19294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
19394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
19494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            new OnPhoneNumberPickerActionListener() {
19594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
19694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onPickPhoneNumberAction(Uri dataUri) {
19794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    // Specify call-origin so that users will see the previous tab instead of
19894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    // CallLog screen (search UI will be automatically exited).
19994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    PhoneNumberInteraction.startInteractionForPhoneCall(
200c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        DialtactsActivity.this, dataUri, getCallOrigin());
2018e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee                    mClearSearchOnPause = true;
20294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
20394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
20494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
20531a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                public void onCallNumberDirectly(String phoneNumber) {
20631a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                    Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
20731a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                    startActivity(intent);
2088e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee                    mClearSearchOnPause = true;
20931a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                }
21031a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner
21131a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                @Override
21294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onShortcutIntentCreated(Intent intent) {
21394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
21494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
21594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
21694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
21794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onHomeInActionBarSelected() {
21894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    exitSearchUi();
21994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
22094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
22194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
22294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
22394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Listener used to send search queries to the phone search fragment.
22494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
225c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
226c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
227c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
228c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
22994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
230c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
231c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void onTextChanged(CharSequence s, int start, int before, int count) {
232c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final String newText = s.toString();
233ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                if (newText.equals(mSearchQuery)) {
234ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // If the query hasn't changed (perhaps due to activity being destroyed
235ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // and restored, or user launching the same DIAL intent twice), then there is
236ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // no need to do anything here.
237ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    return;
238ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                }
239ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                mSearchQuery = newText;
240ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                if (DEBUG) {
241ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
242ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                }
243b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                final boolean dialpadSearch = isDialpadShowing();
244ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
245c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // Show search result with non-empty text. Show a bare list otherwise.
246b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                if (TextUtils.isEmpty(newText) && getInSearchUi()) {
247c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    exitSearchUi();
248c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchViewCloseButton.setVisibility(View.GONE);
2493b82fc14c1d05a85e07e457c4d6eeb0e405b7eb3Yorke Lee                    mVoiceSearchButton.setVisibility(View.VISIBLE);
250c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    return;
251b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                } else if (!TextUtils.isEmpty(newText)) {
252b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
253b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                            (!dialpadSearch && mInRegularSearch);
254b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    if (!sameSearchMode) {
255b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        // call enterSearchUi only if we are switching search modes, or entering
256b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        // search ui for the first time
257b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        enterSearchUi(dialpadSearch, newText);
258b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    }
25994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
260b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    if (dialpadSearch && mSmartDialSearchFragment != null) {
261b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                            mSmartDialSearchFragment.setQueryString(newText, false);
262b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    } else if (mRegularSearchFragment != null) {
263b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        mRegularSearchFragment.setQueryString(newText, false);
264b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    }
265b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    mSearchViewCloseButton.setVisibility(View.VISIBLE);
2663b82fc14c1d05a85e07e457c4d6eeb0e405b7eb3Yorke Lee                    mVoiceSearchButton.setVisibility(View.GONE);
267b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    return;
26894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
269c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
27094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
271c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
272c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void afterTextChanged(Editable s) {
273c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
27494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
27594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
276c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean isDialpadShowing() {
277c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return mDialpadFragment != null && mDialpadFragment.isVisible();
278c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
279c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
28094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
281c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onCreate(Bundle savedInstanceState) {
282c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onCreate(savedInstanceState);
28398702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        mFirstLaunch = true;
28498702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee
28594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final Intent intent = getIntent();
28694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        fixIntent(intent);
28794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
2888898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee        setContentView(R.layout.dialtacts_activity);
28994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
290c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        getActionBar().hide();
29194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
292ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
293ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // is null. Otherwise the fragment manager takes care of recreating these fragments.
294c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (savedInstanceState == null) {
295ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
29694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
297c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            final FragmentTransaction ft = getFragmentManager().beginTransaction();
298ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
299ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
300c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.commit();
301ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else {
302ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
303b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
304b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
305ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
30694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
30794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
308c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
309b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame = findViewById(R.id.dialtacts_frame);
3105f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee        mActionBar = findViewById(R.id.fake_action_bar);
311c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        prepareSearchView();
31294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
31394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
314c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                && savedInstanceState == null) {
31594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            setupFilterText(intent);
31694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
317fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee
318b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        setupFakeActionBarItems();
319b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
3200baa98b5163f83a715b37e3cffe1488ac88ab049Yorke Lee        mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
321fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee        SmartDialPrefix.initializeNanpSettings(this);
32294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
32394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
32494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
325c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onResume() {
326c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onResume();
32798702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        if (mFirstLaunch) {
32898702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee            displayFragment(getIntent());
32935127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee        } else if (!phoneIsInUse() && mInCallDialpadUp) {
33035127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            hideDialpadFragment(false, true);
33135127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            mInCallDialpadUp = false;
33298702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        }
33335127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee
33498702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        mFirstLaunch = false;
335fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee        mDialerDatabaseHelper.startSmartDialUpdateThread();
336c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
33794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
338c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
3398e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee    protected void onPause() {
3408e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee        if (mClearSearchOnPause) {
3418e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee            hideDialpadAndSearchUi();
3428e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee            mClearSearchOnPause = false;
3438e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee        }
3448e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee        super.onPause();
3458e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee    }
3468e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee
3478e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee    @Override
348ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    protected void onSaveInstanceState(Bundle outState) {
349ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        super.onSaveInstanceState(outState);
350ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
351b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
352b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
353ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
354ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    }
355ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
356ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    @Override
357c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onAttachFragment(Fragment fragment) {
358ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (fragment instanceof DialpadFragment) {
359ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mDialpadFragment = (DialpadFragment) fragment;
360c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            final FragmentTransaction transaction = getFragmentManager().beginTransaction();
361ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            transaction.hide(mDialpadFragment);
362c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            transaction.commit();
363ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof SmartDialSearchFragment) {
364ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
36563ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee            mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
36663ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee                    mPhoneNumberPickerActionListener);
367ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof SearchFragment) {
3682e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner            mRegularSearchFragment = (RegularSearchFragment) fragment;
36963ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee            mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
37063ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee                    mPhoneNumberPickerActionListener);
371ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof PhoneFavoriteFragment) {
372ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
3735781afe916100c4ed709e8edacac4a71db090108Yorke Lee            mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
37494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
375c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
376c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
377c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
378c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public boolean onMenuItemClick(MenuItem item) {
379c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        switch (item.getItemId()) {
380c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_import_export:
381c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // We hard-code the "contactsAreAvailable" argument because doing it properly would
382c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
383c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // now in Dialtacts for (potential) performance reasons. Compare with how it is
384c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // done in {@link PeopleActivity}.
385c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                ImportExportDialogFragment.show(getFragmentManager(), true,
386f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee                        DialtactsActivity.class);
387c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
388c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_clear_frequents:
389c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                ClearFrequentsDialog.show(getFragmentManager());
390c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
3915e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee            case R.id.menu_add_contact:
392c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                try {
393c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
394c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                } catch (ActivityNotFoundException e) {
395c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    Toast toast = Toast.makeText(this,
396c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            R.string.add_contact_not_available,
397c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            Toast.LENGTH_SHORT);
398c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    toast.show();
399c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                }
400c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
401c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_call_settings:
402b453e5b0ce106987c75901321ceba13ed111222cAlon Albert                handleMenuSettings();
4035e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                return true;
4045e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee            case R.id.menu_all_contacts:
4055e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                onShowAllContacts();
4065e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                return true;
407c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
408c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return false;
40994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
41094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
411b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    protected void handleMenuSettings() {
412b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        openTelephonySetting(this);
413b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    }
414b453e5b0ce106987c75901321ceba13ed111222cAlon Albert
415b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    public static void openTelephonySetting(Activity activity) {
416b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        final Intent settingsIntent = getCallSettingsIntent();
417b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        activity.startActivity(settingsIntent);
418b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    }
419b453e5b0ce106987c75901321ceba13ed111222cAlon Albert
42094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
42194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void onClick(View view) {
42294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        switch (view.getId()) {
423c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.overflow_menu: {
42436c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee                mOverflowMenu.show();
42594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
42694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
427c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.dialpad_button:
42835127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // Reset the boolean flag that tracks whether the dialpad was up because
42935127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // we were in call. Regardless of whether it was true before, we want to
43035127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // show the dialpad because the user has explicitly clicked the dialpad
43135127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // button.
43235127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                mInCallDialpadUp = false;
433c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                showDialpadFragment(true);
434c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
435c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.call_history_on_dialpad_button:
436c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.call_history_button:
437f420a5344995b2c450379159f679809690bc9880Yorke Lee                // Use explicit CallLogActivity intent instead of ACTION_VIEW +
438f420a5344995b2c450379159f679809690bc9880Yorke Lee                // CONTENT_TYPE, so that we always open our call log from our dialer
439c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Intent intent = new Intent(this, CallLogActivity.class);
440c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                startActivity(intent);
441c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
442c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.search_close_button:
443c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // Clear the search field
444c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                if (!TextUtils.isEmpty(mSearchView.getText())) {
4452eafb7afc46135bbbf5a79b437df33cd0fb66c0bYorke Lee                    mDialpadFragment.clearDialpad();
446c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchView.setText("");
44794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
44894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
449c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.voice_search_button:
450c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
451c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
452c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
45394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            default: {
45494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                Log.wtf(TAG, "Unexpected onClick event from " + view);
45594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
45694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
45794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
45894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
45994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
460c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
461c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
462c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
463c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            if (resultCode == RESULT_OK) {
464c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final ArrayList<String> matches = data.getStringArrayListExtra(
465c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        RecognizerIntent.EXTRA_RESULTS);
466c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                if (matches.size() > 0) {
467c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    final String match = matches.get(0);
468c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchView.setText(match);
469c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                } else {
470c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    Log.e(TAG, "Voice search - nothing heard");
471c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                }
472c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            } else {
473c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Log.e(TAG, "Voice search failed");
474c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
475c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
476c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onActivityResult(requestCode, resultCode, data);
477c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
47894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
479c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void showDialpadFragment(boolean animate) {
480ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        mDialpadFragment.setAdjustTranslationForAnimation(animate);
481c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction ft = getFragmentManager().beginTransaction();
482c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (animate) {
483c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.setCustomAnimations(R.anim.slide_in, 0);
484ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        } else {
485ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee            mDialpadFragment.setYFraction(0);
486c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
487c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.show(mDialpadFragment);
488c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.commit();
489c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
490c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
491ca19504ff58c91497885d9705905f7ab25e97ce0Yorke Lee    public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
492f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (mDialpadFragment == null) return;
493f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (clearDialpad) {
494f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            mDialpadFragment.clearDialpad();
495f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        }
496f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (!mDialpadFragment.isVisible()) return;
497ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        mDialpadFragment.setAdjustTranslationForAnimation(animate);
498c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction ft = getFragmentManager().beginTransaction();
499c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (animate) {
500c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.setCustomAnimations(0, R.anim.slide_out);
501c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
502c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.hide(mDialpadFragment);
503c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.commit();
504c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
50594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
50694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void prepareSearchView() {
507c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer = findViewById(R.id.search_view_container);
508c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewCloseButton = findViewById(R.id.search_close_button);
509c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewCloseButton.setOnClickListener(this);
510c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mVoiceSearchButton = findViewById(R.id.voice_search_button);
511c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mVoiceSearchButton.setOnClickListener(this);
512c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView = (EditText) findViewById(R.id.search_view);
513c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
51441a24b477dd0c550eda011a1b591603a20b980beYorke Lee
51541a24b477dd0c550eda011a1b591603a20b980beYorke Lee        final String hintText = getString(R.string.dialer_hint_find_contact);
51641a24b477dd0c550eda011a1b591603a20b980beYorke Lee
51741a24b477dd0c550eda011a1b591603a20b980beYorke Lee        // The following code is used to insert an icon into a CharSequence (copied from
51841a24b477dd0c550eda011a1b591603a20b980beYorke Lee        // SearchView)
51941a24b477dd0c550eda011a1b591603a20b980beYorke Lee        final SpannableStringBuilder ssb = new SpannableStringBuilder("   "); // for the icon
52041a24b477dd0c550eda011a1b591603a20b980beYorke Lee        ssb.append(hintText);
52141a24b477dd0c550eda011a1b591603a20b980beYorke Lee        final Drawable searchIcon = getResources().getDrawable(R.drawable.ic_ab_search);
52241a24b477dd0c550eda011a1b591603a20b980beYorke Lee        final int textSize = (int) (mSearchView.getTextSize() * 1.20);
52341a24b477dd0c550eda011a1b591603a20b980beYorke Lee        searchIcon.setBounds(0, 0, textSize, textSize);
52441a24b477dd0c550eda011a1b591603a20b980beYorke Lee        ssb.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
52541a24b477dd0c550eda011a1b591603a20b980beYorke Lee
52641a24b477dd0c550eda011a1b591603a20b980beYorke Lee        mSearchView.setHint(ssb);
527c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
528c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
529c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
530c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        @Override
531c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        public void onAnimationEnd(Animator animation) {
532c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setVisibility(View.GONE);
53394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
534c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    };
53594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
536b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean getInSearchUi() {
537b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        return mInDialpadSearch || mInRegularSearch;
538b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    }
539b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
540b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private void setNotInSearchUi() {
541b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInDialpadSearch = false;
542b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInRegularSearch = false;
543b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    }
544b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
545311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee    private void hideDialpadAndSearchUi() {
5467a06c4462c9f0fabeff15be92854a40f286058f8Yorke Lee        mSearchView.setText(null);
547311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee        hideDialpadFragment(false, true);
548311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee    }
549311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee
550c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void hideSearchBar() {
551c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee       hideSearchBar(true);
552c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
553c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
554c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void hideSearchBar(boolean shiftView) {
555c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (shiftView) {
556c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.animate().cancel();
557c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setAlpha(1);
558c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setTranslationY(0);
559c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
560c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    .setDuration(200).setListener(mHideListener);
561c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
562b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mFragmentsFrame.animate().withLayer()
563c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
564c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    new AnimatorListenerAdapter() {
565c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        @Override
566c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        public void onAnimationEnd(Animator animation) {
567c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            mBottomPaddingView.setVisibility(View.VISIBLE);
5685f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee                            mFragmentsFrame.setTranslationY(0);
5695f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee                            mActionBar.setVisibility(View.INVISIBLE);
570c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        }
571c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    });
572c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else {
573c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
5745f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee            mActionBar.setVisibility(View.INVISIBLE);
575c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
57694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
57794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
578c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void showSearchBar() {
579c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.animate().cancel();
580c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.setAlpha(0);
581c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
582c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
583c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                .setListener(new AnimatorListenerAdapter() {
584c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    @Override
585c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    public void onAnimationStart(Animator animation) {
586c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        mSearchViewContainer.setVisibility(View.VISIBLE);
5875f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee                        mActionBar.setVisibility(View.VISIBLE);
588c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    }
589c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                });
590c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
591b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
592b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
593c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                .setListener(
594c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        new AnimatorListenerAdapter() {
595c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            @Override
596c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            public void onAnimationStart(Animator animation) {
597c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                                mBottomPaddingView.setVisibility(View.GONE);
598c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            }
599c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        });
60094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
60194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
60294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
603b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    public void setupFakeActionBarItems() {
604c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mMenuButton = findViewById(R.id.overflow_menu);
605c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (mMenuButton != null) {
606c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mMenuButton.setOnClickListener(this);
60736c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee
60836c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
60936c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            final Menu menu = mOverflowMenu.getMenu();
61036c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mOverflowMenu.inflate(R.menu.dialtacts_options);
61136c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mOverflowMenu.setOnMenuItemClickListener(this);
61236c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
613c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
614c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
615c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mCallHistoryButton = findViewById(R.id.call_history_button);
616c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
617c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mCallHistoryButton.setOnClickListener(this);
618c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
619c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mDialpadButton = findViewById(R.id.dialpad_button);
620c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
621c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mDialpadButton.setOnClickListener(this);
6229c1e0653addc1fff03601289a9f9a57b1daeaf57Christine Chen    }
6239c1e0653addc1fff03601289a9f9a57b1daeaf57Christine Chen
624c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void setupFakeActionBarItemsForDialpadFragment() {
625c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
626c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        callhistoryButton.setOnClickListener(this);
62794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
62894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
62994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void fixIntent(Intent intent) {
63094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // This should be cleaned up: the call key used to send an Intent
63194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // that just said to go to the recent calls list.  It now sends this
63294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // abstract action, but this class hasn't been rewritten to deal with it.
63394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
63494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
63594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            intent.putExtra("call_key", true);
63694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            setIntent(intent);
63794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
63894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
63994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
64094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
64194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Returns true if the intent is due to hitting the green send key (hardware call button:
64294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * KEYCODE_CALL) while in a call.
64394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
64494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent the intent that launched this activity
64594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param recentCallsRequest true if the intent is requesting to view recent calls
64694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @return true if the intent is due to hitting the green send key while in a call
64794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
648c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
64994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If there is a call in progress go to the call screen
65094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (recentCallsRequest) {
65194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            final boolean callKey = intent.getBooleanExtra("call_key", false);
65294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
65394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            try {
65494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
65594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (callKey && phone != null && phone.showCallScreen()) {
65694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    return true;
65794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
65894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            } catch (RemoteException e) {
65994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                Log.e(TAG, "Failed to handle send while in call", e);
66094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
66194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
66294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
66394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return false;
66494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
66594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
66694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
66794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Sets the current tab based on the intent's request type
66894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
66994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent Intent that contains information about which tab should be selected
67094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
671c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void displayFragment(Intent intent) {
67294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If we got here by hitting send and we're in call forward along to the in-call activity
67394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
67494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            getContentResolver()));
67594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
67694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            finish();
67794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return;
67894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
67994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
680669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee        if (mDialpadFragment != null) {
681669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee            final boolean phoneIsInUse = phoneIsInUse();
682669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee            if (phoneIsInUse || isDialIntent(intent)) {
683669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                mDialpadFragment.setStartedFromNewIntent(true);
684669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                if (phoneIsInUse && !mDialpadFragment.isVisible()) {
685669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                    mInCallDialpadUp = true;
686669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                }
687669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                showDialpadFragment(false);
68835127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            }
68994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
69094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
69194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
69294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
69394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void onNewIntent(Intent newIntent) {
69494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setIntent(newIntent);
69594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        fixIntent(newIntent);
696c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        displayFragment(newIntent);
69794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final String action = newIntent.getAction();
698c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
69994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        invalidateOptionsMenu();
70094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
70194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
70294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Returns true if the given intent contains a phone number to populate the dialer with */
70394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private boolean isDialIntent(Intent intent) {
70494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final String action = intent.getAction();
70594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
70694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return true;
70794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
70894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_VIEW.equals(action)) {
70994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            final Uri data = intent.getData();
7109d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Cheng            if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
71194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                return true;
71294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
71394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
71494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return false;
71594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
71694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
71794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
71894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Returns an appropriate call origin for this Activity. May return null when no call origin
71994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * should be used (e.g. when some 3rd party application launched the screen. Call origin is
72094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * for remembering the tab in which the user made a phone call, so the external app's DIAL
72194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * request should not be counted.)
72294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
72394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public String getCallOrigin() {
72494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
72594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
72694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
72794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
72894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
72994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * This text originally came from a FILTER_CONTACTS_ACTION intent received
73094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * by this activity. The stored text will then be cleared after after this
73194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * method returns.
73294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
73394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @return The stored filter text
73494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
73594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public String getAndClearFilterText() {
73694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        String filterText = mFilterText;
73794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFilterText = null;
73894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return filterText;
73994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
74094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
74194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
74294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
74394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * This is so child activities can check if they are supposed to display a filter.
74494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
74594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent The intent received in {@link #onNewIntent(Intent)}
74694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
74794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void setupFilterText(Intent intent) {
74894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If the intent was relaunched from history, don't apply the filter text.
74994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
75094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return;
75194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
75294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
75394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (filter != null && filter.length() > 0) {
75494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            mFilterText = filter;
75594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
75694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
75794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
7588898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee    private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
7598898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee            new PhoneFavoriteFragment.Listener() {
76094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        @Override
76194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        public void onContactSelected(Uri contactUri) {
76294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            PhoneNumberInteraction.startInteractionForPhoneCall(
763c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        DialtactsActivity.this, contactUri, getCallOrigin());
76494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
76594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
76694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        @Override
76794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        public void onCallNumberDirectly(String phoneNumber) {
7689d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Cheng            Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
76994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            startActivity(intent);
77094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
77194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
77294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
773c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
774c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
775c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * be showing when the search key is pressed so there is more state management involved.
77694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
77794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
77894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void startSearch(String initialQuery, boolean selectInitialQuery,
77994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            Bundle appSearchData, boolean globalSearch) {
780c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
78194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            if (mInSearchUi) {
78294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (mSearchView.hasFocus()) {
78394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    showInputMethod(mSearchView.findFocus());
78494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                } else {
78594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    mSearchView.requestFocus();
78694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
78794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            } else {
78894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                enterSearchUi();
78994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
79094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        } else {
79194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
79294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
793c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }*/
79494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
79594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void showInputMethod(View view) {
796c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final InputMethodManager imm = (InputMethodManager) getSystemService(
797c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.INPUT_METHOD_SERVICE);
79894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (imm != null) {
799c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            imm.showSoftInput(view, 0);
80094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
80194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
80294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
80394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void hideInputMethod(View view) {
804c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final InputMethodManager imm = (InputMethodManager) getSystemService(
805c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.INPUT_METHOD_SERVICE);
80694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (imm != null && view != null) {
80794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
80894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
80994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
81094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
81194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
812c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Shows the search fragment
81394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
814ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private void enterSearchUi(boolean smartDialSearch, String query) {
81534bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee        if (getFragmentManager().isDestroyed()) {
81634bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // Weird race condition where fragment is doing work after the activity is destroyed
81734bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // due to talkback being on (b/10209937). Just return since we can't do any
81834bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // constructive here.
81934bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            return;
82034bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee        }
82134bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee
822ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (DEBUG) {
823ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
824ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        }
825ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
826c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction transaction = getFragmentManager().beginTransaction();
827c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
828ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
829ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        SearchFragment fragment;
830b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        if (mInDialpadSearch) {
831b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mSmartDialSearchFragment);
832b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else if (mInRegularSearch) {
833b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mRegularSearchFragment);
834b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else {
835b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mPhoneFavoriteFragment);
836b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        }
837b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
838b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        final String tag;
839b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        if (smartDialSearch) {
840b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
841b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else {
842b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            tag = TAG_REGULAR_SEARCH_FRAGMENT;
843b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        }
844b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInDialpadSearch = smartDialSearch;
845b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInRegularSearch = !smartDialSearch;
846ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
847ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
848ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (fragment == null) {
849ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (smartDialSearch) {
850ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                fragment = new SmartDialSearchFragment();
851ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            } else {
8522e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner                fragment = new RegularSearchFragment();
853ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
85494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
855b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        transaction.replace(R.id.dialtacts_frame, fragment, tag);
856ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        transaction.addToBackStack(null);
857ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        fragment.setQueryString(query, false);
858ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        transaction.commit();
85994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
86094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
86194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
862c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Hides the search fragment
86394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
864c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void exitSearchUi() {
8653487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee        // See related bug in enterSearchUI();
8663487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee        if (getFragmentManager().isDestroyed()) {
8673487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee            return;
8683487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee        }
869b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        // Go all the way back to the favorites fragment, regardless of how many times we
870b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        // transitioned between search fragments
871b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
872b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        setNotInSearchUi();
87394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
87494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
87594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Returns an Intent to launch Call Settings screen */
87694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static Intent getCallSettingsIntent() {
87794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final Intent intent = new Intent(Intent.ACTION_MAIN);
87894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
87994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
88094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return intent;
88194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
88294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
88394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
884c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onBackPressed() {
885ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
886f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            hideDialpadFragment(true, false);
887b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else if (getInSearchUi()) {
888c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchView.setText(null);
889f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            mDialpadFragment.clearDialpad();
890c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else if (isTaskRoot()) {
891c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // Instead of stopping, simply push this to the back of the stack.
892c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // This is only done when running at the top of the stack;
893c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // otherwise, we have been launched by someone else so need to
894c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // allow the user to go back to the caller.
895c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            moveTaskToBack(false);
896c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else {
897c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            super.onBackPressed();
89894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
899c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
900c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
901c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
902c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onDialpadQueryChanged(String query) {
903c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
904c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
905c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
906ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (DEBUG) {
907ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
908ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
909ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
910ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // This callback can happen if the dialpad fragment is recreated because of
911ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // activity destruction. In that case, don't update the search view because
912ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // that would bring the user back to the search fragment regardless of the
913ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // previous state of the application. Instead, just return here and let the
914ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // fragment manager correctly figure out whatever fragment was last displayed.
915ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                return;
916ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
917c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchView.setText(normalizedQuery);
91894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
91994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
920c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
921c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
922c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onListFragmentScrollStateChange(int scrollState) {
923c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
924f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            hideDialpadFragment(true, false);
925c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            hideInputMethod(getCurrentFocus());
926c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
927c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
928c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
929c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
930c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onDialpadFragmentStarted() {
931c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        setupFakeActionBarItemsForDialpadFragment();
932c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
933c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
934c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean phoneIsInUse() {
935c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final TelephonyManager tm = (TelephonyManager) getSystemService(
936c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.TELEPHONY_SERVICE);
937c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
938c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
9398dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
9408dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    @Override
9418dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    public void onShowAllContacts() {
9428dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        final Intent intent = new Intent(this, AllContactsActivity.class);
9438dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        startActivity(intent);
9448dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    }
945e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
946e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    public static Intent getAddNumberToContactIntent(CharSequence text) {
947e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
948e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.putExtra(Intents.Insert.PHONE, text);
949e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.setType(Contacts.CONTENT_ITEM_TYPE);
950e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        return intent;
951e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    }
952e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
953e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    public static Intent getInsertContactWithNameIntent(CharSequence text) {
954e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
955e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.putExtra(Intents.Insert.NAME, text);
956e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        return intent;
957e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    }
95894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng}
959