DialtactsActivity.java revision ca19504ff58c91497885d9705905f7ab25e97ce0
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;
3094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.net.Uri;
3194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.Bundle;
3202403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Leeimport android.os.Handler;
3302403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Leeimport android.os.Message;
3494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.RemoteException;
3594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.ServiceManager;
3694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.CallLog.Calls;
3794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.ContactsContract.Contacts;
38e142481570d7fbda5d035555fe217314e396ae90Yorke Leeimport android.provider.ContactsContract.Intents;
3994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.ContactsContract.Intents.UI;
40c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.speech.RecognizerIntent;
41c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.telephony.TelephonyManager;
42c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.text.Editable;
4394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.text.TextUtils;
44c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.text.TextWatcher;
4594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.util.Log;
4694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.Menu;
4794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.MenuItem;
4894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.View;
4994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.View.OnFocusChangeListener;
5094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.inputmethod.InputMethodManager;
51c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.widget.AbsListView.OnScrollListener;
52c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.widget.EditText;
5394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.PopupMenu;
54073b5cf341964796ccf082d30aff63225d3adb28Chiao Chengimport android.widget.Toast;
5594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
569d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Chengimport com.android.contacts.common.CallUtil;
57603ff6858d321e8265b3af6363a63df2827e0334Chiao Chengimport com.android.contacts.common.activity.TransactionSafeActivity;
58c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.contacts.common.dialog.ClearFrequentsDialog;
59c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.contacts.common.interactions.ImportExportDialogFragment;
608efbcf920eeebc826c18b3c474e9c62b77dcd232Chiao Chengimport com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
61c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.calllog.CallLogActivity;
62fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Leeimport com.android.dialer.database.DialerDatabaseHelper;
63f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Leeimport com.android.dialer.dialpad.DialpadFragment;
64c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.dialpad.SmartDialNameMatcher;
65fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Leeimport com.android.dialer.dialpad.SmartDialPrefix;
661429f1a5c2b993a5013bfda7cfd75a2c43ead2bfChiao Chengimport com.android.dialer.interactions.PhoneNumberInteraction;
678dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport com.android.dialer.list.AllContactsActivity;
68c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.list.OnListFragmentScrolledListener;
69b453e5b0ce106987c75901321ceba13ed111222cAlon Albertimport com.android.dialer.list.PhoneFavoriteFragment;
702e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shraunerimport com.android.dialer.list.RegularSearchFragment;
712e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shraunerimport com.android.dialer.list.SearchFragment;
72c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.list.SmartDialSearchFragment;
730baa98b5163f83a715b37e3cffe1488ac88ab049Yorke Leeimport com.android.dialerbind.DatabaseHelperManager;
7494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.internal.telephony.ITelephony;
7594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
76c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport java.util.ArrayList;
77c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
7894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/**
7994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * The dialer tab's title is 'phone', a more common name (see strings.xml).
8094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
81c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leepublic class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
82f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee        DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
83c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        OnListFragmentScrolledListener,
848dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        DialpadFragment.OnDialpadFragmentStartedListener,
858dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        PhoneFavoriteFragment.OnShowAllContactsListener {
8694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String TAG = "DialtactsActivity";
8794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
8894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static final boolean DEBUG = false;
8994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
90f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee    public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
91f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee
9294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Used to open Call Setting */
9394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String PHONE_PACKAGE = "com.android.phone";
9494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String CALL_SETTINGS_CLASS_NAME =
9594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            "com.android.phone.CallFeaturesSetting";
9694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** @see #getCallOrigin() */
9794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String CALL_ORIGIN_DIALTACTS =
9894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            "com.android.dialer.DialtactsActivity";
9994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
100b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
101b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
102ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private static final String KEY_SEARCH_QUERY = "search_query";
103ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private static final String KEY_FIRST_LAUNCH = "first_launch";
104ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
105c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
106c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
107c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
108c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_FAVORITES_FRAGMENT = "favorites";
109c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
11094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
11194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
11294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
11394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
11494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
11594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
11694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
117c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
11894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
119c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private String mFilterText;
12094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
12194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
122c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * The main fragment displaying the user's favorites and frequent contacts
12394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
1248898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee    private PhoneFavoriteFragment mPhoneFavoriteFragment;
125c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
12694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
127c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment containing the dialpad that slides into view
12894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
129f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee    private DialpadFragment mDialpadFragment;
13094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
131c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /**
132c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment for searching phone numbers using the alphanumeric keyboard.
133c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     */
1342e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner    private RegularSearchFragment mRegularSearchFragment;
13594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
136c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /**
137c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment for searching phone numbers using the dialpad.
138c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     */
139c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private SmartDialSearchFragment mSmartDialSearchFragment;
14094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
14194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private View mMenuButton;
142c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mCallHistoryButton;
143c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mDialpadButton;
14494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
145c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    // Padding view used to shift the fragments up when the dialpad is shown.
146c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mBottomPaddingView;
147b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private View mFragmentsFrame;
148b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
149b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean mInDialpadSearch;
150b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean mInRegularSearch;
15194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
152ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    /**
15335127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee     * True if the dialpad is only temporarily showing due to being in call
15435127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee     */
15535127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee    private boolean mInCallDialpadUp;
15635127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee
15735127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee    /**
158ef2b7388569a94432278a617b528f628385fbb8cYorke Lee     * True when this activity has been launched for the first time.
159ef2b7388569a94432278a617b528f628385fbb8cYorke Lee     */
16098702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee    private boolean mFirstLaunch;
161c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mSearchViewContainer;
162c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mSearchViewCloseButton;
163c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mVoiceSearchButton;
164c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private EditText mSearchView;
16594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
166ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private String mSearchQuery;
167ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
168fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee    private DialerDatabaseHelper mDialerDatabaseHelper;
169fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee
17002403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    private static final int EVENT_DELAYED_HIDE_SEARCH = 1;
17102403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee
17202403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    // Wait this much time (in ms) before hiding the search fragment after clicking on
17302403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    // a search result to dial, to avoid jank
17402403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    private static final int CLEAR_SEARCH_DELAY = 500;
17502403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee
17602403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    final Handler mHandler = new Handler() {
17702403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee        @Override
17802403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee        public void handleMessage(Message msg) {
17902403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee            if (msg.what == EVENT_DELAYED_HIDE_SEARCH) {
18002403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee                hideDialpadAndSearchUi();
18102403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee            }
18202403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee        }
18302403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    };
18402403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee
18594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
18694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Listener used when one of phone numbers in search UI is selected. This will initiate a
18794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * phone call using the phone number.
18894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
18994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
19094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            new OnPhoneNumberPickerActionListener() {
19194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
19294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onPickPhoneNumberAction(Uri dataUri) {
19394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    // Specify call-origin so that users will see the previous tab instead of
19494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    // CallLog screen (search UI will be automatically exited).
19594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    PhoneNumberInteraction.startInteractionForPhoneCall(
196c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        DialtactsActivity.this, dataUri, getCallOrigin());
19702403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee                    mHandler.sendEmptyMessageDelayed(EVENT_DELAYED_HIDE_SEARCH,
19802403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee                            CLEAR_SEARCH_DELAY);
19994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
20094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
20194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
20231a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                public void onCallNumberDirectly(String phoneNumber) {
20331a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                    Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
20431a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                    startActivity(intent);
20502403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee                    mHandler.sendEmptyMessageDelayed(EVENT_DELAYED_HIDE_SEARCH,
20602403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee                            CLEAR_SEARCH_DELAY);
20731a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                }
20831a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner
20931a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                @Override
21094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onShortcutIntentCreated(Intent intent) {
21194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
21294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
21394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
21494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
21594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onHomeInActionBarSelected() {
21694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    exitSearchUi();
21794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
21894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
21994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
22094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
22194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Listener used to send search queries to the phone search fragment.
22294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
223c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
224c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
225c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
226c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
22794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
228c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
229c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void onTextChanged(CharSequence s, int start, int before, int count) {
230c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final String newText = s.toString();
231ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                if (newText.equals(mSearchQuery)) {
232ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // If the query hasn't changed (perhaps due to activity being destroyed
233ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // and restored, or user launching the same DIAL intent twice), then there is
234ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // no need to do anything here.
235ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    return;
236ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                }
237ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                mSearchQuery = newText;
238ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                if (DEBUG) {
239ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
240ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                }
241b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                final boolean dialpadSearch = isDialpadShowing();
242ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
243c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // Show search result with non-empty text. Show a bare list otherwise.
244b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                if (TextUtils.isEmpty(newText) && getInSearchUi()) {
245c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    exitSearchUi();
246c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchViewCloseButton.setVisibility(View.GONE);
2473b82fc14c1d05a85e07e457c4d6eeb0e405b7eb3Yorke Lee                    mVoiceSearchButton.setVisibility(View.VISIBLE);
248c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    return;
249b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                } else if (!TextUtils.isEmpty(newText)) {
250b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
251b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                            (!dialpadSearch && mInRegularSearch);
252b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    if (!sameSearchMode) {
253b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        // call enterSearchUi only if we are switching search modes, or entering
254b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        // search ui for the first time
255b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        enterSearchUi(dialpadSearch, newText);
256b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    }
25794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
258b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    if (dialpadSearch && mSmartDialSearchFragment != null) {
259b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                            mSmartDialSearchFragment.setQueryString(newText, false);
260b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    } else if (mRegularSearchFragment != null) {
261b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        mRegularSearchFragment.setQueryString(newText, false);
262b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    }
263b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    mSearchViewCloseButton.setVisibility(View.VISIBLE);
2643b82fc14c1d05a85e07e457c4d6eeb0e405b7eb3Yorke Lee                    mVoiceSearchButton.setVisibility(View.GONE);
265b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    return;
26694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
267c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
26894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
269c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
270c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void afterTextChanged(Editable s) {
271c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
27294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
27394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
274c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean isDialpadShowing() {
275c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return mDialpadFragment != null && mDialpadFragment.isVisible();
276c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
277c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
27894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
279c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onCreate(Bundle savedInstanceState) {
280c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onCreate(savedInstanceState);
28198702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        mFirstLaunch = true;
28298702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee
28394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final Intent intent = getIntent();
28494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        fixIntent(intent);
28594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
2868898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee        setContentView(R.layout.dialtacts_activity);
28794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
288c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        getActionBar().hide();
28994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
290ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
291ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // is null. Otherwise the fragment manager takes care of recreating these fragments.
292c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (savedInstanceState == null) {
293ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
29494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
295c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            final FragmentTransaction ft = getFragmentManager().beginTransaction();
296ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
297ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
298c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.commit();
299ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else {
300ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
301b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
302b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
303ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
30494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
30594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
306c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
307b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame = findViewById(R.id.dialtacts_frame);
308c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        prepareSearchView();
30994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
31094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
311c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                && savedInstanceState == null) {
31294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            setupFilterText(intent);
31394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
314fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee
315b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        setupFakeActionBarItems();
316b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
3170baa98b5163f83a715b37e3cffe1488ac88ab049Yorke Lee        mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
318fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee        SmartDialPrefix.initializeNanpSettings(this);
31994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
32094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
32194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
322c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onResume() {
323c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onResume();
32498702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        if (mFirstLaunch) {
32598702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee            displayFragment(getIntent());
32635127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee        } else if (!phoneIsInUse() && mInCallDialpadUp) {
32735127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            hideDialpadFragment(false, true);
32835127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            mInCallDialpadUp = false;
32998702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        }
33035127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee
33198702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        mFirstLaunch = false;
332fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee        mDialerDatabaseHelper.startSmartDialUpdateThread();
333c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
33494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
335c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
336ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    protected void onSaveInstanceState(Bundle outState) {
337ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        super.onSaveInstanceState(outState);
338ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
339b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
340b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
341ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
342ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    }
343ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
344ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    @Override
345c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onAttachFragment(Fragment fragment) {
346ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (fragment instanceof DialpadFragment) {
347ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mDialpadFragment = (DialpadFragment) fragment;
348c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            final FragmentTransaction transaction = getFragmentManager().beginTransaction();
349ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            transaction.hide(mDialpadFragment);
350c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            transaction.commit();
351ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof SmartDialSearchFragment) {
352ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
35363ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee            mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
35463ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee                    mPhoneNumberPickerActionListener);
355ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof SearchFragment) {
3562e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner            mRegularSearchFragment = (RegularSearchFragment) fragment;
35763ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee            mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
35863ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee                    mPhoneNumberPickerActionListener);
359ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof PhoneFavoriteFragment) {
360ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
3615781afe916100c4ed709e8edacac4a71db090108Yorke Lee            mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
36294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
363c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
364c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
365c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
366c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public boolean onMenuItemClick(MenuItem item) {
367c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        switch (item.getItemId()) {
368c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_import_export:
369c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // We hard-code the "contactsAreAvailable" argument because doing it properly would
370c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
371c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // now in Dialtacts for (potential) performance reasons. Compare with how it is
372c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // done in {@link PeopleActivity}.
373c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                ImportExportDialogFragment.show(getFragmentManager(), true,
374f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee                        DialtactsActivity.class);
375c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
376c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_clear_frequents:
377c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                ClearFrequentsDialog.show(getFragmentManager());
378c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
3795e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee            case R.id.menu_add_contact:
380c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                try {
381c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
382c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                } catch (ActivityNotFoundException e) {
383c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    Toast toast = Toast.makeText(this,
384c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            R.string.add_contact_not_available,
385c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            Toast.LENGTH_SHORT);
386c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    toast.show();
387c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                }
388c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
389c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_call_settings:
390b453e5b0ce106987c75901321ceba13ed111222cAlon Albert                handleMenuSettings();
3915e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                return true;
3925e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee            case R.id.menu_all_contacts:
3935e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                onShowAllContacts();
3945e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                return true;
395c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
396c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return false;
39794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
39894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
399b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    protected void handleMenuSettings() {
400b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        openTelephonySetting(this);
401b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    }
402b453e5b0ce106987c75901321ceba13ed111222cAlon Albert
403b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    public static void openTelephonySetting(Activity activity) {
404b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        final Intent settingsIntent = getCallSettingsIntent();
405b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        activity.startActivity(settingsIntent);
406b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    }
407b453e5b0ce106987c75901321ceba13ed111222cAlon Albert
40894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
40994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void onClick(View view) {
41094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        switch (view.getId()) {
411c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.overflow_menu: {
412c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
413c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Menu menu = popupMenu.getMenu();
4148898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee                popupMenu.inflate(R.menu.dialtacts_options);
415f420a5344995b2c450379159f679809690bc9880Yorke Lee                final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
4160d391b481d691fb26fb114a82d98100a5804ca62Yorke Lee                clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
417c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                popupMenu.setOnMenuItemClickListener(this);
418c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                popupMenu.show();
41994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
42094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
421c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.dialpad_button:
42235127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // Reset the boolean flag that tracks whether the dialpad was up because
42335127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // we were in call. Regardless of whether it was true before, we want to
42435127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // show the dialpad because the user has explicitly clicked the dialpad
42535127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // button.
42635127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                mInCallDialpadUp = false;
427c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                showDialpadFragment(true);
428c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
429c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.call_history_on_dialpad_button:
430c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.call_history_button:
431f420a5344995b2c450379159f679809690bc9880Yorke Lee                // Use explicit CallLogActivity intent instead of ACTION_VIEW +
432f420a5344995b2c450379159f679809690bc9880Yorke Lee                // CONTENT_TYPE, so that we always open our call log from our dialer
433c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Intent intent = new Intent(this, CallLogActivity.class);
434c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                startActivity(intent);
435c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
436c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.search_close_button:
437c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // Clear the search field
438c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                if (!TextUtils.isEmpty(mSearchView.getText())) {
4392eafb7afc46135bbbf5a79b437df33cd0fb66c0bYorke Lee                    mDialpadFragment.clearDialpad();
440c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchView.setText("");
44194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
44294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
443c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.voice_search_button:
444c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
445c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
446c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
44794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            default: {
44894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                Log.wtf(TAG, "Unexpected onClick event from " + view);
44994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
45094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
45194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
45294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
45394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
454c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
455c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
456c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
457c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            if (resultCode == RESULT_OK) {
458c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final ArrayList<String> matches = data.getStringArrayListExtra(
459c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        RecognizerIntent.EXTRA_RESULTS);
460c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                if (matches.size() > 0) {
461c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    final String match = matches.get(0);
462c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchView.setText(match);
463c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                } else {
464c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    Log.e(TAG, "Voice search - nothing heard");
465c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                }
466c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            } else {
467c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Log.e(TAG, "Voice search failed");
468c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
469c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
470c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onActivityResult(requestCode, resultCode, data);
471c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
47294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
473c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void showDialpadFragment(boolean animate) {
474ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        mDialpadFragment.setAdjustTranslationForAnimation(animate);
475c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction ft = getFragmentManager().beginTransaction();
476c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (animate) {
477c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.setCustomAnimations(R.anim.slide_in, 0);
478ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        } else {
479ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee            mDialpadFragment.setYFraction(0);
480c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
481c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.show(mDialpadFragment);
482c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.commit();
483c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
484c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
485ca19504ff58c91497885d9705905f7ab25e97ce0Yorke Lee    public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
486f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (mDialpadFragment == null) return;
487f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (clearDialpad) {
488f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            mDialpadFragment.clearDialpad();
489f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        }
490f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (!mDialpadFragment.isVisible()) return;
491ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        mDialpadFragment.setAdjustTranslationForAnimation(animate);
492c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction ft = getFragmentManager().beginTransaction();
493c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (animate) {
494c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.setCustomAnimations(0, R.anim.slide_out);
495c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
496c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.hide(mDialpadFragment);
497c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.commit();
498c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
49994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
50094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void prepareSearchView() {
501c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer = findViewById(R.id.search_view_container);
502c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewCloseButton = findViewById(R.id.search_close_button);
503c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewCloseButton.setOnClickListener(this);
504c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mVoiceSearchButton = findViewById(R.id.voice_search_button);
505c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mVoiceSearchButton.setOnClickListener(this);
506c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView = (EditText) findViewById(R.id.search_view);
507c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
508c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
509c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
51094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            @Override
51194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            public void onFocusChange(View view, boolean hasFocus) {
51294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (hasFocus) {
51394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    showInputMethod(view.findFocus());
51494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
51594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
51694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        });
517c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
518c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
519c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
520c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        @Override
521c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        public void onAnimationEnd(Animator animation) {
522c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setVisibility(View.GONE);
52394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
524c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    };
52594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
526b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean getInSearchUi() {
527b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        return mInDialpadSearch || mInRegularSearch;
528b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    }
529b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
530b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private void setNotInSearchUi() {
531b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInDialpadSearch = false;
532b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInRegularSearch = false;
533b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    }
534b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
535311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee    private void hideDialpadAndSearchUi() {
5367a06c4462c9f0fabeff15be92854a40f286058f8Yorke Lee        mSearchView.setText(null);
537311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee        hideDialpadFragment(false, true);
538311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee    }
539311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee
540c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void hideSearchBar() {
541c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee       hideSearchBar(true);
542c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
543c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
544c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void hideSearchBar(boolean shiftView) {
545c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (shiftView) {
546c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.animate().cancel();
547c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setAlpha(1);
548c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setTranslationY(0);
549c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
550c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    .setDuration(200).setListener(mHideListener);
551c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
552b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mFragmentsFrame.animate().withLayer()
553c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
554c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    new AnimatorListenerAdapter() {
555c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        @Override
556c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        public void onAnimationEnd(Animator animation) {
557c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            mBottomPaddingView.setVisibility(View.VISIBLE);
558b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                                mFragmentsFrame.setTranslationY(0);
559c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        }
560c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    });
561c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else {
562c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
563c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
56494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
56594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
566c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void showSearchBar() {
567c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.animate().cancel();
568c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.setAlpha(0);
569c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
570c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
571c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                .setListener(new AnimatorListenerAdapter() {
572c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    @Override
573c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    public void onAnimationStart(Animator animation) {
574c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        mSearchViewContainer.setVisibility(View.VISIBLE);
575c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    }
576c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                });
577c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
578b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
579b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
580c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                .setListener(
581c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        new AnimatorListenerAdapter() {
582c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            @Override
583c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            public void onAnimationStart(Animator animation) {
584c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                                mBottomPaddingView.setVisibility(View.GONE);
585c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            }
586c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        });
58794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
58894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
58994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
590b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    public void setupFakeActionBarItems() {
591c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mMenuButton = findViewById(R.id.overflow_menu);
592c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (mMenuButton != null) {
593c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mMenuButton.setOnClickListener(this);
594c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
595c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
596c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mCallHistoryButton = findViewById(R.id.call_history_button);
597c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
598c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mCallHistoryButton.setOnClickListener(this);
599c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
600c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mDialpadButton = findViewById(R.id.dialpad_button);
601c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
602c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mDialpadButton.setOnClickListener(this);
6039c1e0653addc1fff03601289a9f9a57b1daeaf57Christine Chen    }
6049c1e0653addc1fff03601289a9f9a57b1daeaf57Christine Chen
605c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void setupFakeActionBarItemsForDialpadFragment() {
606c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
607c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        callhistoryButton.setOnClickListener(this);
60894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
60994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
61094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void fixIntent(Intent intent) {
61194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // This should be cleaned up: the call key used to send an Intent
61294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // that just said to go to the recent calls list.  It now sends this
61394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // abstract action, but this class hasn't been rewritten to deal with it.
61494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
61594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
61694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            intent.putExtra("call_key", true);
61794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            setIntent(intent);
61894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
61994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
62094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
62194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
62294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Returns true if the intent is due to hitting the green send key (hardware call button:
62394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * KEYCODE_CALL) while in a call.
62494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
62594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent the intent that launched this activity
62694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param recentCallsRequest true if the intent is requesting to view recent calls
62794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @return true if the intent is due to hitting the green send key while in a call
62894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
629c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
63094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If there is a call in progress go to the call screen
63194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (recentCallsRequest) {
63294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            final boolean callKey = intent.getBooleanExtra("call_key", false);
63394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
63494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            try {
63594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
63694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (callKey && phone != null && phone.showCallScreen()) {
63794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    return true;
63894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
63994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            } catch (RemoteException e) {
64094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                Log.e(TAG, "Failed to handle send while in call", e);
64194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
64294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
64394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
64494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return false;
64594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
64694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
64794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
64894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Sets the current tab based on the intent's request type
64994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
65094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent Intent that contains information about which tab should be selected
65194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
652c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void displayFragment(Intent intent) {
65394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If we got here by hitting send and we're in call forward along to the in-call activity
65494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
65594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            getContentResolver()));
65694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
65794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            finish();
65894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return;
65994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
66094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
661669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee        if (mDialpadFragment != null) {
662669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee            final boolean phoneIsInUse = phoneIsInUse();
663669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee            if (phoneIsInUse || isDialIntent(intent)) {
664669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                mDialpadFragment.setStartedFromNewIntent(true);
665669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                if (phoneIsInUse && !mDialpadFragment.isVisible()) {
666669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                    mInCallDialpadUp = true;
667669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                }
668669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                showDialpadFragment(false);
66935127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            }
67094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
67194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
67294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
67394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
67494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void onNewIntent(Intent newIntent) {
67594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setIntent(newIntent);
67694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        fixIntent(newIntent);
677c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        displayFragment(newIntent);
67894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final String action = newIntent.getAction();
679c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
68094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        invalidateOptionsMenu();
68194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
68294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
68394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Returns true if the given intent contains a phone number to populate the dialer with */
68494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private boolean isDialIntent(Intent intent) {
68594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final String action = intent.getAction();
68694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
68794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return true;
68894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
68994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_VIEW.equals(action)) {
69094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            final Uri data = intent.getData();
6919d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Cheng            if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
69294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                return true;
69394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
69494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
69594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return false;
69694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
69794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
69894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
69994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Returns an appropriate call origin for this Activity. May return null when no call origin
70094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * should be used (e.g. when some 3rd party application launched the screen. Call origin is
70194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * for remembering the tab in which the user made a phone call, so the external app's DIAL
70294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * request should not be counted.)
70394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
70494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public String getCallOrigin() {
70594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
70694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
70794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
70894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
70994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
71094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * This text originally came from a FILTER_CONTACTS_ACTION intent received
71194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * by this activity. The stored text will then be cleared after after this
71294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * method returns.
71394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
71494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @return The stored filter text
71594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
71694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public String getAndClearFilterText() {
71794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        String filterText = mFilterText;
71894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFilterText = null;
71994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return filterText;
72094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
72194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
72294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
72394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
72494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * This is so child activities can check if they are supposed to display a filter.
72594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
72694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent The intent received in {@link #onNewIntent(Intent)}
72794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
72894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void setupFilterText(Intent intent) {
72994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If the intent was relaunched from history, don't apply the filter text.
73094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
73194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return;
73294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
73394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
73494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (filter != null && filter.length() > 0) {
73594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            mFilterText = filter;
73694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
73794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
73894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
7398898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee    private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
7408898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee            new PhoneFavoriteFragment.Listener() {
74194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        @Override
74294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        public void onContactSelected(Uri contactUri) {
74394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            PhoneNumberInteraction.startInteractionForPhoneCall(
744c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        DialtactsActivity.this, contactUri, getCallOrigin());
74594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
74694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
74794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        @Override
74894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        public void onCallNumberDirectly(String phoneNumber) {
7499d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Cheng            Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
75094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            startActivity(intent);
75194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
75294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
75394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
754c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
755c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
756c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * be showing when the search key is pressed so there is more state management involved.
75794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
75894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
75994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void startSearch(String initialQuery, boolean selectInitialQuery,
76094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            Bundle appSearchData, boolean globalSearch) {
761c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
76294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            if (mInSearchUi) {
76394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (mSearchView.hasFocus()) {
76494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    showInputMethod(mSearchView.findFocus());
76594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                } else {
76694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    mSearchView.requestFocus();
76794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
76894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            } else {
76994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                enterSearchUi();
77094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
77194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        } else {
77294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
77394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
774c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }*/
77594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
77694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void showInputMethod(View view) {
777c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final InputMethodManager imm = (InputMethodManager) getSystemService(
778c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.INPUT_METHOD_SERVICE);
77994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (imm != null) {
780c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            imm.showSoftInput(view, 0);
78194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
78294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
78394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
78494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void hideInputMethod(View view) {
785c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final InputMethodManager imm = (InputMethodManager) getSystemService(
786c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.INPUT_METHOD_SERVICE);
78794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (imm != null && view != null) {
78894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
78994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
79094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
79194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
79294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
793c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Shows the search fragment
79494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
795ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private void enterSearchUi(boolean smartDialSearch, String query) {
79634bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee        if (getFragmentManager().isDestroyed()) {
79734bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // Weird race condition where fragment is doing work after the activity is destroyed
79834bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // due to talkback being on (b/10209937). Just return since we can't do any
79934bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // constructive here.
80034bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            return;
80134bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee        }
80234bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee
803ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (DEBUG) {
804ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
805ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        }
806ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
807c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction transaction = getFragmentManager().beginTransaction();
808c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
809ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
810ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        SearchFragment fragment;
811b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        if (mInDialpadSearch) {
812b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mSmartDialSearchFragment);
813b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else if (mInRegularSearch) {
814b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mRegularSearchFragment);
815b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else {
816b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mPhoneFavoriteFragment);
817b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        }
818b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
819b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        final String tag;
820b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        if (smartDialSearch) {
821b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
822b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else {
823b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            tag = TAG_REGULAR_SEARCH_FRAGMENT;
824b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        }
825b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInDialpadSearch = smartDialSearch;
826b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInRegularSearch = !smartDialSearch;
827ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
828ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
829ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (fragment == null) {
830ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (smartDialSearch) {
831ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                fragment = new SmartDialSearchFragment();
832ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            } else {
8332e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner                fragment = new RegularSearchFragment();
834ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
83594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
836b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        transaction.replace(R.id.dialtacts_frame, fragment, tag);
837ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        transaction.addToBackStack(null);
838ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        fragment.setQueryString(query, false);
839ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        transaction.commit();
84094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
84194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
84294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
843c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Hides the search fragment
84494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
845c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void exitSearchUi() {
846b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        // Go all the way back to the favorites fragment, regardless of how many times we
847b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        // transitioned between search fragments
848b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
849b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
850b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        setNotInSearchUi();
85194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
85294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
85394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Returns an Intent to launch Call Settings screen */
85494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static Intent getCallSettingsIntent() {
85594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final Intent intent = new Intent(Intent.ACTION_MAIN);
85694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
85794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
85894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return intent;
85994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
86094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
86194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
862c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onBackPressed() {
863ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
864f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            hideDialpadFragment(true, false);
865b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else if (getInSearchUi()) {
866c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchView.setText(null);
867f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            mDialpadFragment.clearDialpad();
868c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else if (isTaskRoot()) {
869c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // Instead of stopping, simply push this to the back of the stack.
870c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // This is only done when running at the top of the stack;
871c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // otherwise, we have been launched by someone else so need to
872c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // allow the user to go back to the caller.
873c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            moveTaskToBack(false);
874c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else {
875c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            super.onBackPressed();
87694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
877c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
878c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
879c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
880c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onDialpadQueryChanged(String query) {
881c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
882c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
883c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
884ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (DEBUG) {
885ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
886ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
887ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
888ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // This callback can happen if the dialpad fragment is recreated because of
889ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // activity destruction. In that case, don't update the search view because
890ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // that would bring the user back to the search fragment regardless of the
891ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // previous state of the application. Instead, just return here and let the
892ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // fragment manager correctly figure out whatever fragment was last displayed.
893ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                return;
894ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
895c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchView.setText(normalizedQuery);
89694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
89794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
898c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
899c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
900c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onListFragmentScrollStateChange(int scrollState) {
901c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
902f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            hideDialpadFragment(true, false);
903c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            hideInputMethod(getCurrentFocus());
904c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
905c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
906c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
907c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
908c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onDialpadFragmentStarted() {
909c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        setupFakeActionBarItemsForDialpadFragment();
910c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
911c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
912c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean phoneIsInUse() {
913c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final TelephonyManager tm = (TelephonyManager) getSystemService(
914c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.TELEPHONY_SERVICE);
915c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
916c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
9178dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
9188dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    @Override
9198dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    public void onShowAllContacts() {
9208dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        final Intent intent = new Intent(this, AllContactsActivity.class);
9218dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        startActivity(intent);
9228dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    }
923e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
924e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    public static Intent getAddNumberToContactIntent(CharSequence text) {
925e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
926e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.putExtra(Intents.Insert.PHONE, text);
927e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.setType(Contacts.CONTENT_ITEM_TYPE);
928e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        return intent;
929e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    }
930e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
931e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    public static Intent getInsertContactWithNameIntent(CharSequence text) {
932e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
933e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.putExtra(Intents.Insert.NAME, text);
934e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        return intent;
935e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    }
93694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng}
937