DialtactsActivity.java revision 0baa98b5163f83a715b37e3cffe1488ac88ab049
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;
239c1e0653addc1fff03601289a9f9a57b1daeaf57Christine Chenimport android.app.backup.BackupManager;
2494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.app.Fragment;
2594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.app.FragmentManager;
2694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.app.FragmentTransaction;
27073b5cf341964796ccf082d30aff63225d3adb28Chiao Chengimport android.content.ActivityNotFoundException;
2894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.Context;
2994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.Intent;
3094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.SharedPreferences;
31c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.content.res.Resources;
3294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.net.Uri;
3394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.Bundle;
3494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.RemoteException;
3594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.ServiceManager;
3694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.CallLog.Calls;
37c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.provider.ContactsContract;
3894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.ContactsContract.Contacts;
3994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.ContactsContract.Intents.UI;
40c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.provider.Settings;
41c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.speech.RecognizerIntent;
42c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.support.v4.app.NavUtils;
43c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.telephony.TelephonyManager;
44c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.text.Editable;
4594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.text.TextUtils;
46c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.text.TextWatcher;
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.ViewConfiguration;
5394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.inputmethod.InputMethodManager;
54c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.widget.AbsListView.OnScrollListener;
55c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.widget.EditText;
56c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.widget.ImageView;
5794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.PopupMenu;
5894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.SearchView;
5994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.SearchView.OnCloseListener;
6094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.SearchView.OnQueryTextListener;
61073b5cf341964796ccf082d30aff63225d3adb28Chiao Chengimport android.widget.Toast;
6294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
639d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Chengimport com.android.contacts.common.CallUtil;
64603ff6858d321e8265b3af6363a63df2827e0334Chiao Chengimport com.android.contacts.common.activity.TransactionSafeActivity;
65c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.contacts.common.dialog.ClearFrequentsDialog;
66c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.contacts.common.interactions.ImportExportDialogFragment;
671429f1a5c2b993a5013bfda7cfd75a2c43ead2bfChiao Chengimport com.android.contacts.common.list.ContactListItemView;
688efbcf920eeebc826c18b3c474e9c62b77dcd232Chiao Chengimport com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
698efbcf920eeebc826c18b3c474e9c62b77dcd232Chiao Chengimport com.android.contacts.common.list.PhoneNumberPickerFragment;
70c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.calllog.CallLogActivity;
71fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Leeimport com.android.dialer.database.DialerDatabaseHelper;
72f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Leeimport com.android.dialer.dialpad.DialpadFragment;
73c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.dialpad.SmartDialNameMatcher;
74fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Leeimport com.android.dialer.dialpad.SmartDialPrefix;
751429f1a5c2b993a5013bfda7cfd75a2c43ead2bfChiao Chengimport com.android.dialer.interactions.PhoneNumberInteraction;
768dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport com.android.dialer.list.AllContactsActivity;
778898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Leeimport com.android.dialer.list.PhoneFavoriteFragment;
78c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.list.OnListFragmentScrolledListener;
79c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.list.SmartDialSearchFragment;
800baa98b5163f83a715b37e3cffe1488ac88ab049Yorke Leeimport com.android.dialerbind.DatabaseHelperManager;
8194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.internal.telephony.ITelephony;
8294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
83c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport java.util.ArrayList;
84c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
8594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/**
8694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * The dialer tab's title is 'phone', a more common name (see strings.xml).
8794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
88c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leepublic class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
89f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee        DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
90c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        OnListFragmentScrolledListener,
918898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee        PhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener,
928dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        DialpadFragment.OnDialpadFragmentStartedListener,
938dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        PhoneFavoriteFragment.OnShowAllContactsListener {
9494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String TAG = "DialtactsActivity";
9594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
9694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static final boolean DEBUG = false;
9794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
98f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee    public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
99f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee
10094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Used to open Call Setting */
10194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String PHONE_PACKAGE = "com.android.phone";
10294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String CALL_SETTINGS_CLASS_NAME =
10394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            "com.android.phone.CallFeaturesSetting";
10494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** @see #getCallOrigin() */
10594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String CALL_ORIGIN_DIALTACTS =
10694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            "com.android.dialer.DialtactsActivity";
10794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
108ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private static final String KEY_IN_SEARCH_UI = "in_search_ui";
109ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private static final String KEY_SEARCH_QUERY = "search_query";
110ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private static final String KEY_FIRST_LAUNCH = "first_launch";
111ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
112c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
113c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
114c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
115c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_FAVORITES_FRAGMENT = "favorites";
116c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
11794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
11894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
11994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
12094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
12194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
12294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
12394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
124c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
12594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
126c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private String mFilterText;
12794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
12894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
129c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * The main fragment displaying the user's favorites and frequent contacts
13094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
1318898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee    private PhoneFavoriteFragment mPhoneFavoriteFragment;
132c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
13394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
134c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment containing the dialpad that slides into view
13594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
136f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee    private DialpadFragment mDialpadFragment;
13794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
138c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /**
139c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment for searching phone numbers using the alphanumeric keyboard.
140c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     */
1418898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee    private SearchFragment mRegularSearchFragment;
14294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
143c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /**
144c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment for searching phone numbers using the dialpad.
145c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     */
146c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private SmartDialSearchFragment mSmartDialSearchFragment;
14794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
14894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private View mMenuButton;
149c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mCallHistoryButton;
150c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mDialpadButton;
15194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
152c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    // Padding view used to shift the fragments up when the dialpad is shown.
153c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mBottomPaddingView;
15494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
15594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
15694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * True when this Activity is in its search UI (with a {@link SearchView} and
15794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * {@link PhoneNumberPickerFragment}).
15894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
15994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private boolean mInSearchUi;
160ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    /**
161ef2b7388569a94432278a617b528f628385fbb8cYorke Lee     * True when this activity has been launched for the first time.
162ef2b7388569a94432278a617b528f628385fbb8cYorke Lee     */
16398702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee    private boolean mFirstLaunch;
164c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mSearchViewContainer;
165c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mSearchViewCloseButton;
166c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mVoiceSearchButton;
167c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private EditText mSearchView;
16894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
169ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private String mSearchQuery;
170ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
171fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee    private DialerDatabaseHelper mDialerDatabaseHelper;
172fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee
17394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
17494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Listener used when one of phone numbers in search UI is selected. This will initiate a
17594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * phone call using the phone number.
17694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
17794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
17894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            new OnPhoneNumberPickerActionListener() {
17994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
18094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onPickPhoneNumberAction(Uri dataUri) {
18194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    // Specify call-origin so that users will see the previous tab instead of
18294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    // CallLog screen (search UI will be automatically exited).
18394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    PhoneNumberInteraction.startInteractionForPhoneCall(
184c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        DialtactsActivity.this, dataUri, getCallOrigin());
185f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee                    hideDialpadAndSearchUi();
18694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
18794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
18894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
18931a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                public void onCallNumberDirectly(String phoneNumber) {
19031a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                    Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
19131a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                    startActivity(intent);
192f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee                    hideDialpadAndSearchUi();
19331a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                }
19431a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner
19531a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                @Override
19694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onShortcutIntentCreated(Intent intent) {
19794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
19894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
19994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
20094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
20194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onHomeInActionBarSelected() {
20294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    exitSearchUi();
20394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
20494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
20594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
20694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
20794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Listener used to send search queries to the phone search fragment.
20894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
209c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
210c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
211c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
212c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
21394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
214c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
215c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void onTextChanged(CharSequence s, int start, int before, int count) {
216c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final String newText = s.toString();
217ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                if (newText.equals(mSearchQuery)) {
218ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // If the query hasn't changed (perhaps due to activity being destroyed
219ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // and restored, or user launching the same DIAL intent twice), then there is
220ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // no need to do anything here.
221ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    return;
222ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                }
223ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                mSearchQuery = newText;
224ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                if (DEBUG) {
225ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
226ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                }
227ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                final boolean smartDialSearch = isDialpadShowing();
228ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
229c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // Show search result with non-empty text. Show a bare list otherwise.
230c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                if (TextUtils.isEmpty(newText) && mInSearchUi) {
231c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    exitSearchUi();
232c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchViewCloseButton.setVisibility(View.GONE);
233c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    return;
234c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
235ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    enterSearchUi(smartDialSearch, newText);
23694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
23794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
238ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                if (smartDialSearch && mSmartDialSearchFragment != null) {
239ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                        mSmartDialSearchFragment.setQueryString(newText, false);
240ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                } else if (mRegularSearchFragment != null) {
241c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mRegularSearchFragment.setQueryString(newText, false);
24294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
243c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                mSearchViewCloseButton.setVisibility(View.VISIBLE);
244c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return;
245c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
24694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
247c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
248c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void afterTextChanged(Editable s) {
249c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
25094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
25194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
252c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean isDialpadShowing() {
253c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return mDialpadFragment != null && mDialpadFragment.isVisible();
254c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
255c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
25694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
257c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onCreate(Bundle savedInstanceState) {
258c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onCreate(savedInstanceState);
25998702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        mFirstLaunch = true;
26098702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee
26194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final Intent intent = getIntent();
26294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        fixIntent(intent);
26394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
2648898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee        setContentView(R.layout.dialtacts_activity);
26594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
266c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        getActionBar().hide();
26794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
268ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
269ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // is null. Otherwise the fragment manager takes care of recreating these fragments.
270c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (savedInstanceState == null) {
271ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
27294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
273c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            final FragmentTransaction ft = getFragmentManager().beginTransaction();
274ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
275ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
276c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.commit();
277ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else {
278ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
279ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mInSearchUi = savedInstanceState.getBoolean(KEY_IN_SEARCH_UI);
280ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
28194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
28294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
283c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
284c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        prepareSearchView();
28594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
28694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
287c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                && savedInstanceState == null) {
28894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            setupFilterText(intent);
28994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
290fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee
2910baa98b5163f83a715b37e3cffe1488ac88ab049Yorke Lee        mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
292fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee        SmartDialPrefix.initializeNanpSettings(this);
29394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
29494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
29594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
296c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onResume() {
297c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onResume();
29898702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        if (mFirstLaunch) {
29998702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee            displayFragment(getIntent());
30098702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        }
30198702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        mFirstLaunch = false;
302fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee        mDialerDatabaseHelper.startSmartDialUpdateThread();
303c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
30494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
305c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
306ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    protected void onSaveInstanceState(Bundle outState) {
307ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        super.onSaveInstanceState(outState);
308ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
309ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        outState.putBoolean(KEY_IN_SEARCH_UI, mInSearchUi);
310ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
311ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    }
312ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
313ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    @Override
314c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onAttachFragment(Fragment fragment) {
315ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (fragment instanceof DialpadFragment) {
316ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mDialpadFragment = (DialpadFragment) fragment;
317c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            final FragmentTransaction transaction = getFragmentManager().beginTransaction();
318ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            transaction.hide(mDialpadFragment);
319c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            transaction.commit();
320ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof SmartDialSearchFragment) {
321ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
32263ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee            mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
32363ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee                    mPhoneNumberPickerActionListener);
324ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof SearchFragment) {
325ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mRegularSearchFragment = (SearchFragment) fragment;
32663ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee            mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
32763ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee                    mPhoneNumberPickerActionListener);
328ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof PhoneFavoriteFragment) {
329ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
3305781afe916100c4ed709e8edacac4a71db090108Yorke Lee            mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
33194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
332c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
333c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
334c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
335c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public boolean onMenuItemClick(MenuItem item) {
336c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        switch (item.getItemId()) {
337c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_import_export:
338c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // We hard-code the "contactsAreAvailable" argument because doing it properly would
339c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
340c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // now in Dialtacts for (potential) performance reasons. Compare with how it is
341c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // done in {@link PeopleActivity}.
342c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                ImportExportDialogFragment.show(getFragmentManager(), true,
343f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee                        DialtactsActivity.class);
344c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
345c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_clear_frequents:
346c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                ClearFrequentsDialog.show(getFragmentManager());
347c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
348c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.add_contact:
349c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                try {
350c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
351c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                } catch (ActivityNotFoundException e) {
352c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    Toast toast = Toast.makeText(this,
353c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            R.string.add_contact_not_available,
354c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            Toast.LENGTH_SHORT);
355c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    toast.show();
356c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                }
357c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
358c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_call_settings:
359f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee                final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
360c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                startActivity(settingsIntent);
361c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
362c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return false;
36394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
36494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
36594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
36694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void onClick(View view) {
36794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        switch (view.getId()) {
368c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.overflow_menu: {
369c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
370c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Menu menu = popupMenu.getMenu();
3718898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee                popupMenu.inflate(R.menu.dialtacts_options);
372f420a5344995b2c450379159f679809690bc9880Yorke Lee                final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
3730d391b481d691fb26fb114a82d98100a5804ca62Yorke Lee                clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
374c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                popupMenu.setOnMenuItemClickListener(this);
375c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                popupMenu.show();
37694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
37794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
378c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.dialpad_button:
379c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                showDialpadFragment(true);
380c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
381c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.call_history_on_dialpad_button:
382c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.call_history_button:
383f420a5344995b2c450379159f679809690bc9880Yorke Lee                // Use explicit CallLogActivity intent instead of ACTION_VIEW +
384f420a5344995b2c450379159f679809690bc9880Yorke Lee                // CONTENT_TYPE, so that we always open our call log from our dialer
385c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Intent intent = new Intent(this, CallLogActivity.class);
386c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                startActivity(intent);
387c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
388c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.search_close_button:
389c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // Clear the search field
390c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                if (!TextUtils.isEmpty(mSearchView.getText())) {
391c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchView.setText("");
39294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
39394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
394c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.voice_search_button:
395c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
396c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
397c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
39894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            default: {
39994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                Log.wtf(TAG, "Unexpected onClick event from " + view);
40094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
40194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
40294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
40394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
40494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
405c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
406c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
407c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
408c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            if (resultCode == RESULT_OK) {
409c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final ArrayList<String> matches = data.getStringArrayListExtra(
410c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        RecognizerIntent.EXTRA_RESULTS);
411c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                if (matches.size() > 0) {
412c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    final String match = matches.get(0);
413c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchView.setText(match);
414c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                } else {
415c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    Log.e(TAG, "Voice search - nothing heard");
416c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                }
417c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            } else {
418c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Log.e(TAG, "Voice search failed");
419c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
420c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
421c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onActivityResult(requestCode, resultCode, data);
422c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
42394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
424c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void showDialpadFragment(boolean animate) {
425ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        mDialpadFragment.setAdjustTranslationForAnimation(animate);
426c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction ft = getFragmentManager().beginTransaction();
427c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (animate) {
428c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.setCustomAnimations(R.anim.slide_in, 0);
429ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        } else {
430ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee            mDialpadFragment.setYFraction(0);
431c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
432c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.show(mDialpadFragment);
433c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.commit();
434c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
435c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
436f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee    private void hideDialpadFragment(boolean animate, boolean clearDialpad) {
437f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (mDialpadFragment == null) return;
438f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (clearDialpad) {
439f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            mDialpadFragment.clearDialpad();
440f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        }
441f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (!mDialpadFragment.isVisible()) return;
442ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        mDialpadFragment.setAdjustTranslationForAnimation(animate);
443c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction ft = getFragmentManager().beginTransaction();
444c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (animate) {
445c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.setCustomAnimations(0, R.anim.slide_out);
446c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
447c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.hide(mDialpadFragment);
448c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.commit();
449c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
45094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
45194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void prepareSearchView() {
452c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer = findViewById(R.id.search_view_container);
453c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewCloseButton = findViewById(R.id.search_close_button);
454c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewCloseButton.setOnClickListener(this);
455c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mVoiceSearchButton = findViewById(R.id.voice_search_button);
456c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mVoiceSearchButton.setOnClickListener(this);
457c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView = (EditText) findViewById(R.id.search_view);
458c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
459c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
460c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
46194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            @Override
46294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            public void onFocusChange(View view, boolean hasFocus) {
46394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (hasFocus) {
46494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    showInputMethod(view.findFocus());
46594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
46694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
46794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        });
468c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
469c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
470c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
471c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        @Override
472c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        public void onAnimationEnd(Animator animation) {
473c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setVisibility(View.GONE);
47494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
475c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    };
47694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
477311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee    private void hideDialpadAndSearchUi() {
478311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee        if (mInSearchUi) {
479311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee            exitSearchUi();
480311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee        }
481311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee        hideDialpadFragment(false, true);
482311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee    }
483311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee
484c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void hideSearchBar() {
485c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee       hideSearchBar(true);
486c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
487c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
488c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void hideSearchBar(boolean shiftView) {
489c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (shiftView) {
490c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.animate().cancel();
491c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setAlpha(1);
492c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setTranslationY(0);
493c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
494c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    .setDuration(200).setListener(mHideListener);
495c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
496ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
497ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                mBottomPaddingView.setVisibility(View.VISIBLE);
498ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                return;
499ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
500ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
501c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mPhoneFavoriteFragment.getView().animate().withLayer()
502c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
503c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    new AnimatorListenerAdapter() {
504c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        @Override
505c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        public void onAnimationEnd(Animator animation) {
506c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            mBottomPaddingView.setVisibility(View.VISIBLE);
507ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                            if (mPhoneFavoriteFragment.getView() != null) {
508ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                                mPhoneFavoriteFragment.getView().setTranslationY(0);
509ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                            }
510c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        }
511c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    });
512c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else {
513c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
514c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
51594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
51694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
517c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void showSearchBar() {
518ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
519ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
520c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.animate().cancel();
521c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.setAlpha(0);
522c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
523c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
524c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                .setListener(new AnimatorListenerAdapter() {
525c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    @Override
526c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    public void onAnimationStart(Animator animation) {
527c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        mSearchViewContainer.setVisibility(View.VISIBLE);
528c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    }
529c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                });
530c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
531ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // If the favorites fragment hasn't been fully created before the dialpad fragment
532ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // is hidden (i.e. onResume), don't bother animating
533ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
534ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mBottomPaddingView.setVisibility(View.GONE);
535ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            return;
536ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        }
537c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
538c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
539c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                .setListener(
540c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        new AnimatorListenerAdapter() {
541c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            @Override
542c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            public void onAnimationStart(Animator animation) {
543c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                                mBottomPaddingView.setVisibility(View.GONE);
544c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            }
545c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        });
54694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
54794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
54894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
549c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void setupFakeActionBarItemsForFavoritesFragment() {
550c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mMenuButton = findViewById(R.id.overflow_menu);
551c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (mMenuButton != null) {
552c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mMenuButton.setOnClickListener(this);
553c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
554c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
555c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mCallHistoryButton = findViewById(R.id.call_history_button);
556c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
557c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mCallHistoryButton.setOnClickListener(this);
558c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
559c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mDialpadButton = findViewById(R.id.dialpad_button);
560c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
561c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mDialpadButton.setOnClickListener(this);
5629c1e0653addc1fff03601289a9f9a57b1daeaf57Christine Chen    }
5639c1e0653addc1fff03601289a9f9a57b1daeaf57Christine Chen
564c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void setupFakeActionBarItemsForDialpadFragment() {
565c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
566c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        callhistoryButton.setOnClickListener(this);
56794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
56894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
56994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void fixIntent(Intent intent) {
57094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // This should be cleaned up: the call key used to send an Intent
57194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // that just said to go to the recent calls list.  It now sends this
57294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // abstract action, but this class hasn't been rewritten to deal with it.
57394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
57494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
57594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            intent.putExtra("call_key", true);
57694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            setIntent(intent);
57794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
57894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
57994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
58094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
58194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Returns true if the intent is due to hitting the green send key (hardware call button:
58294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * KEYCODE_CALL) while in a call.
58394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
58494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent the intent that launched this activity
58594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param recentCallsRequest true if the intent is requesting to view recent calls
58694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @return true if the intent is due to hitting the green send key while in a call
58794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
588c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
58994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If there is a call in progress go to the call screen
59094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (recentCallsRequest) {
59194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            final boolean callKey = intent.getBooleanExtra("call_key", false);
59294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
59394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            try {
59494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
59594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (callKey && phone != null && phone.showCallScreen()) {
59694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    return true;
59794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
59894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            } catch (RemoteException e) {
59994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                Log.e(TAG, "Failed to handle send while in call", e);
60094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
60194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
60294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
60394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return false;
60494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
60594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
60694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
60794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Sets the current tab based on the intent's request type
60894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
60994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent Intent that contains information about which tab should be selected
61094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
611c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void displayFragment(Intent intent) {
61294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If we got here by hitting send and we're in call forward along to the in-call activity
61394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
61494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            getContentResolver()));
61594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
61694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            finish();
61794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return;
61894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
61994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
62098702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
621c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mDialpadFragment.setStartedFromNewIntent(true);
622ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee            showDialpadFragment(false);
62394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
62494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
62594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
62694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
62794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void onNewIntent(Intent newIntent) {
62894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setIntent(newIntent);
62994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        fixIntent(newIntent);
630c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        displayFragment(newIntent);
63194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final String action = newIntent.getAction();
632c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
63394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        invalidateOptionsMenu();
63494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
63594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
63694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Returns true if the given intent contains a phone number to populate the dialer with */
63794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private boolean isDialIntent(Intent intent) {
63894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final String action = intent.getAction();
63994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
64094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return true;
64194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
64294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_VIEW.equals(action)) {
64394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            final Uri data = intent.getData();
6449d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Cheng            if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
64594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                return true;
64694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
64794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
64894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return false;
64994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
65094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
65194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
65294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Returns an appropriate call origin for this Activity. May return null when no call origin
65394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * should be used (e.g. when some 3rd party application launched the screen. Call origin is
65494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * for remembering the tab in which the user made a phone call, so the external app's DIAL
65594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * request should not be counted.)
65694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
65794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public String getCallOrigin() {
65894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
65994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
66094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
66194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
66294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
66394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * This text originally came from a FILTER_CONTACTS_ACTION intent received
66494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * by this activity. The stored text will then be cleared after after this
66594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * method returns.
66694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
66794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @return The stored filter text
66894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
66994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public String getAndClearFilterText() {
67094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        String filterText = mFilterText;
67194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFilterText = null;
67294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return filterText;
67394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
67494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
67594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
67694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
67794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * This is so child activities can check if they are supposed to display a filter.
67894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
67994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent The intent received in {@link #onNewIntent(Intent)}
68094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
68194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void setupFilterText(Intent intent) {
68294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If the intent was relaunched from history, don't apply the filter text.
68394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
68494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return;
68594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
68694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
68794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (filter != null && filter.length() > 0) {
68894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            mFilterText = filter;
68994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
69094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
69194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
6928898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee    private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
6938898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee            new PhoneFavoriteFragment.Listener() {
69494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        @Override
69594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        public void onContactSelected(Uri contactUri) {
69694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            PhoneNumberInteraction.startInteractionForPhoneCall(
697c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        DialtactsActivity.this, contactUri, getCallOrigin());
69894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
69994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
70094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        @Override
70194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        public void onCallNumberDirectly(String phoneNumber) {
7029d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Cheng            Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
70394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            startActivity(intent);
70494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
70594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
70694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
707c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
708c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
709c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * be showing when the search key is pressed so there is more state management involved.
71094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
71194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
71294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void startSearch(String initialQuery, boolean selectInitialQuery,
71394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            Bundle appSearchData, boolean globalSearch) {
714c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
71594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            if (mInSearchUi) {
71694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (mSearchView.hasFocus()) {
71794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    showInputMethod(mSearchView.findFocus());
71894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                } else {
71994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    mSearchView.requestFocus();
72094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
72194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            } else {
72294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                enterSearchUi();
72394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
72494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        } else {
72594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
72694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
727c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }*/
72894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
72994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void showInputMethod(View view) {
730c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final InputMethodManager imm = (InputMethodManager) getSystemService(
731c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.INPUT_METHOD_SERVICE);
73294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (imm != null) {
733c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            imm.showSoftInput(view, 0);
73494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
73594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
73694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
73794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void hideInputMethod(View view) {
738c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final InputMethodManager imm = (InputMethodManager) getSystemService(
739c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.INPUT_METHOD_SERVICE);
74094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (imm != null && view != null) {
74194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
74294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
74394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
74494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
74594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
746c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Shows the search fragment
74794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
748ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private void enterSearchUi(boolean smartDialSearch, String query) {
74934bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee        if (getFragmentManager().isDestroyed()) {
75034bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // Weird race condition where fragment is doing work after the activity is destroyed
75134bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // due to talkback being on (b/10209937). Just return since we can't do any
75234bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // constructive here.
75334bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            return;
75434bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee        }
75534bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee
756ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (DEBUG) {
757ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
758ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        }
759ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        final String tag = smartDialSearch ? TAG_SMARTDIAL_SEARCH_FRAGMENT :
760ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                TAG_REGULAR_SEARCH_FRAGMENT;
761ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
762c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction transaction = getFragmentManager().beginTransaction();
763c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
764ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
765ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        SearchFragment fragment;
766ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
767ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        transaction.remove(mPhoneFavoriteFragment);
768ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
769ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (fragment == null) {
770ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (smartDialSearch) {
771ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                fragment = new SmartDialSearchFragment();
772ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            } else {
773ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                fragment = new SearchFragment();
774ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
775ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            transaction.replace(R.id.dialtacts_frame, fragment, tag);
776c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else {
777ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            transaction.attach(fragment);
77894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
77994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
780ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        transaction.addToBackStack(null);
781ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        fragment.setQueryString(query, false);
782ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        transaction.commit();
783c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mInSearchUi = true;
78494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
78594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
78694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
787c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Hides the search fragment
78894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
789c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void exitSearchUi() {
790ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        getFragmentManager().popBackStack();
791c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mInSearchUi = false;
79294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
79394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
79494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Returns an Intent to launch Call Settings screen */
79594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static Intent getCallSettingsIntent() {
79694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final Intent intent = new Intent(Intent.ACTION_MAIN);
79794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
79894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
79994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return intent;
80094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
80194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
80294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
803c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onBackPressed() {
804ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
805f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            hideDialpadFragment(true, false);
806c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else if (mInSearchUi) {
807c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchView.setText(null);
808f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            mDialpadFragment.clearDialpad();
809c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else if (isTaskRoot()) {
810c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // Instead of stopping, simply push this to the back of the stack.
811c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // This is only done when running at the top of the stack;
812c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // otherwise, we have been launched by someone else so need to
813c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // allow the user to go back to the caller.
814c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            moveTaskToBack(false);
815c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else {
816c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            super.onBackPressed();
81794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
818c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
819c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
820c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
821c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onDialpadQueryChanged(String query) {
822c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
823c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
824c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
825ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (DEBUG) {
826ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
827ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
828ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
829ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // This callback can happen if the dialpad fragment is recreated because of
830ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // activity destruction. In that case, don't update the search view because
831ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // that would bring the user back to the search fragment regardless of the
832ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // previous state of the application. Instead, just return here and let the
833ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // fragment manager correctly figure out whatever fragment was last displayed.
834ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                return;
835ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
836c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchView.setText(normalizedQuery);
83794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
83894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
839c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
840c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
841c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onListFragmentScrollStateChange(int scrollState) {
842c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
843f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            hideDialpadFragment(true, false);
844c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            hideInputMethod(getCurrentFocus());
845c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
846c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
847c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
848c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
849c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onPhoneFavoriteFragmentStarted() {
850c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        setupFakeActionBarItemsForFavoritesFragment();
851c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
852c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
853c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
854c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onDialpadFragmentStarted() {
855c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        setupFakeActionBarItemsForDialpadFragment();
856c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
857c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
858c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean phoneIsInUse() {
859c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final TelephonyManager tm = (TelephonyManager) getSystemService(
860c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.TELEPHONY_SERVICE);
861c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
862c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
8638dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
8648dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    @Override
8658dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    public void onShowAllContacts() {
8668dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        final Intent intent = new Intent(this, AllContactsActivity.class);
8678dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        startActivity(intent);
8688dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    }
86994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng}
870