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;
3294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.RemoteException;
3394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.ServiceManager;
3494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.CallLog.Calls;
3594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.ContactsContract.Contacts;
36e142481570d7fbda5d035555fe217314e396ae90Yorke Leeimport android.provider.ContactsContract.Intents;
3794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.ContactsContract.Intents.UI;
38c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.speech.RecognizerIntent;
39c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.telephony.TelephonyManager;
40c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.text.Editable;
4194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.text.TextUtils;
42c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.text.TextWatcher;
4394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.util.Log;
4494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.Menu;
4594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.MenuItem;
4694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.View;
4794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.View.OnFocusChangeListener;
4894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.inputmethod.InputMethodManager;
49c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.widget.AbsListView.OnScrollListener;
50c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.widget.EditText;
5194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.PopupMenu;
52073b5cf341964796ccf082d30aff63225d3adb28Chiao Chengimport android.widget.Toast;
5394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
549d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Chengimport com.android.contacts.common.CallUtil;
55603ff6858d321e8265b3af6363a63df2827e0334Chiao Chengimport com.android.contacts.common.activity.TransactionSafeActivity;
56c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.contacts.common.dialog.ClearFrequentsDialog;
57c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.contacts.common.interactions.ImportExportDialogFragment;
588efbcf920eeebc826c18b3c474e9c62b77dcd232Chiao Chengimport com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
59c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.calllog.CallLogActivity;
60fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Leeimport com.android.dialer.database.DialerDatabaseHelper;
61f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Leeimport com.android.dialer.dialpad.DialpadFragment;
62c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.dialpad.SmartDialNameMatcher;
63fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Leeimport com.android.dialer.dialpad.SmartDialPrefix;
641429f1a5c2b993a5013bfda7cfd75a2c43ead2bfChiao Chengimport com.android.dialer.interactions.PhoneNumberInteraction;
658dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport com.android.dialer.list.AllContactsActivity;
66c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.list.OnListFragmentScrolledListener;
67b453e5b0ce106987c75901321ceba13ed111222cAlon Albertimport com.android.dialer.list.PhoneFavoriteFragment;
682e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shraunerimport com.android.dialer.list.RegularSearchFragment;
692e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shraunerimport com.android.dialer.list.SearchFragment;
70c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.list.SmartDialSearchFragment;
710baa98b5163f83a715b37e3cffe1488ac88ab049Yorke Leeimport com.android.dialerbind.DatabaseHelperManager;
7294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.internal.telephony.ITelephony;
7394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
74c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport java.util.ArrayList;
75c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
7694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/**
7794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * The dialer tab's title is 'phone', a more common name (see strings.xml).
7894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
79c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leepublic class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
80f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee        DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
81c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        OnListFragmentScrolledListener,
828dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        DialpadFragment.OnDialpadFragmentStartedListener,
838dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        PhoneFavoriteFragment.OnShowAllContactsListener {
8494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String TAG = "DialtactsActivity";
8594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
8694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static final boolean DEBUG = false;
8794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
88f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee    public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
89f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee
9094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Used to open Call Setting */
9194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String PHONE_PACKAGE = "com.android.phone";
9294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String CALL_SETTINGS_CLASS_NAME =
9394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            "com.android.phone.CallFeaturesSetting";
9494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** @see #getCallOrigin() */
9594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String CALL_ORIGIN_DIALTACTS =
9694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            "com.android.dialer.DialtactsActivity";
9794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
98b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
99b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
100ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private static final String KEY_SEARCH_QUERY = "search_query";
101ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private static final String KEY_FIRST_LAUNCH = "first_launch";
102ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
103c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
104c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
105c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
106c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_FAVORITES_FRAGMENT = "favorites";
107c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
10894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
10994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
11094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
11194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
11294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
11394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
11494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
115c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
11694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
117c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private String mFilterText;
11894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
11994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
120c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * The main fragment displaying the user's favorites and frequent contacts
12194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
1228898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee    private PhoneFavoriteFragment mPhoneFavoriteFragment;
123c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
12494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
125c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment containing the dialpad that slides into view
12694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
127f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee    private DialpadFragment mDialpadFragment;
12894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
129c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /**
130c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment for searching phone numbers using the alphanumeric keyboard.
131c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     */
1322e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner    private RegularSearchFragment mRegularSearchFragment;
13394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
134c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /**
135c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment for searching phone numbers using the dialpad.
136c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     */
137c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private SmartDialSearchFragment mSmartDialSearchFragment;
13894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
13994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private View mMenuButton;
140c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mCallHistoryButton;
141c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mDialpadButton;
14236c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee    private PopupMenu mOverflowMenu;
14394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
144c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    // Padding view used to shift the fragments up when the dialpad is shown.
145c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mBottomPaddingView;
146b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private View mFragmentsFrame;
1475f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee    private View mActionBar;
148b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
149b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean mInDialpadSearch;
150b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean mInRegularSearch;
1518e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee    private boolean mClearSearchOnPause;
15294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
153ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    /**
15435127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee     * True if the dialpad is only temporarily showing due to being in call
15535127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee     */
15635127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee    private boolean mInCallDialpadUp;
15735127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee
15835127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee    /**
159ef2b7388569a94432278a617b528f628385fbb8cYorke Lee     * True when this activity has been launched for the first time.
160ef2b7388569a94432278a617b528f628385fbb8cYorke Lee     */
16198702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee    private boolean mFirstLaunch;
162c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mSearchViewContainer;
163c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mSearchViewCloseButton;
164c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mVoiceSearchButton;
165c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private EditText mSearchView;
16694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
167ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private String mSearchQuery;
168ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
169fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee    private DialerDatabaseHelper mDialerDatabaseHelper;
170fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee
17136c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee    private class OverflowPopupMenu extends PopupMenu {
17236c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        public OverflowPopupMenu(Context context, View anchor) {
17336c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            super(context, anchor);
17436c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        }
17536c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee
17636c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        @Override
17736c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        public void show() {
17836c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            final Menu menu = getMenu();
17936c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
18036c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
18136c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            super.show();
18236c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        }
18336c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee    }
18436c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke 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());
1978e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee                    mClearSearchOnPause = true;
19894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
19994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
20094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
20131a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                public void onCallNumberDirectly(String phoneNumber) {
20231a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                    Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
20331a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                    startActivity(intent);
2048e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee                    mClearSearchOnPause = true;
20531a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                }
20631a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner
20731a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                @Override
20894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onShortcutIntentCreated(Intent intent) {
20994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
21094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
21194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
21294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
21394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onHomeInActionBarSelected() {
21494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    exitSearchUi();
21594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
21694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
21794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
21894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
21994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Listener used to send search queries to the phone search fragment.
22094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
221c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
222c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
223c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
224c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
22594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
226c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
227c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void onTextChanged(CharSequence s, int start, int before, int count) {
228c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final String newText = s.toString();
229ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                if (newText.equals(mSearchQuery)) {
230ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // If the query hasn't changed (perhaps due to activity being destroyed
231ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // and restored, or user launching the same DIAL intent twice), then there is
232ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // no need to do anything here.
233ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    return;
234ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                }
235ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                mSearchQuery = newText;
236ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                if (DEBUG) {
237ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
238ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                }
239b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                final boolean dialpadSearch = isDialpadShowing();
240ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
241c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // Show search result with non-empty text. Show a bare list otherwise.
242b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                if (TextUtils.isEmpty(newText) && getInSearchUi()) {
243c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    exitSearchUi();
244c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchViewCloseButton.setVisibility(View.GONE);
2453b82fc14c1d05a85e07e457c4d6eeb0e405b7eb3Yorke Lee                    mVoiceSearchButton.setVisibility(View.VISIBLE);
246c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    return;
247b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                } else if (!TextUtils.isEmpty(newText)) {
248b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
249b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                            (!dialpadSearch && mInRegularSearch);
250b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    if (!sameSearchMode) {
251b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        // call enterSearchUi only if we are switching search modes, or entering
252b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        // search ui for the first time
253b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        enterSearchUi(dialpadSearch, newText);
254b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    }
25594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
256b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    if (dialpadSearch && mSmartDialSearchFragment != null) {
257b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                            mSmartDialSearchFragment.setQueryString(newText, false);
258b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    } else if (mRegularSearchFragment != null) {
259b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        mRegularSearchFragment.setQueryString(newText, false);
260b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    }
261b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    mSearchViewCloseButton.setVisibility(View.VISIBLE);
2623b82fc14c1d05a85e07e457c4d6eeb0e405b7eb3Yorke Lee                    mVoiceSearchButton.setVisibility(View.GONE);
263b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    return;
26494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
265c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
26694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
267c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
268c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void afterTextChanged(Editable s) {
269c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
27094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
27194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
272c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean isDialpadShowing() {
273c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return mDialpadFragment != null && mDialpadFragment.isVisible();
274c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
275c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
27694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
277c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onCreate(Bundle savedInstanceState) {
278c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onCreate(savedInstanceState);
27998702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        mFirstLaunch = true;
28098702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee
28194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final Intent intent = getIntent();
28294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        fixIntent(intent);
28394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
2848898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee        setContentView(R.layout.dialtacts_activity);
28594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
286c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        getActionBar().hide();
28794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
288ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
289ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // is null. Otherwise the fragment manager takes care of recreating these fragments.
290c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (savedInstanceState == null) {
291ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
29294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
293c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            final FragmentTransaction ft = getFragmentManager().beginTransaction();
294ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
295ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
296c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.commit();
297ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else {
298ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
299b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
300b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
301ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
30294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
30394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
304c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
305b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame = findViewById(R.id.dialtacts_frame);
3065f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee        mActionBar = findViewById(R.id.fake_action_bar);
307c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        prepareSearchView();
30894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
30994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
310c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                && savedInstanceState == null) {
31194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            setupFilterText(intent);
31294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
313fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee
314b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        setupFakeActionBarItems();
315b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
3160baa98b5163f83a715b37e3cffe1488ac88ab049Yorke Lee        mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
317fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee        SmartDialPrefix.initializeNanpSettings(this);
31894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
31994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
32094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
321c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onResume() {
322c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onResume();
32398702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        if (mFirstLaunch) {
32498702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee            displayFragment(getIntent());
32535127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee        } else if (!phoneIsInUse() && mInCallDialpadUp) {
32635127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            hideDialpadFragment(false, true);
32735127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            mInCallDialpadUp = false;
32898702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        }
32935127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee
33098702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        mFirstLaunch = false;
331fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee        mDialerDatabaseHelper.startSmartDialUpdateThread();
332c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
33394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
334c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
3358e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee    protected void onPause() {
3368e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee        if (mClearSearchOnPause) {
3378e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee            hideDialpadAndSearchUi();
3388e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee            mClearSearchOnPause = false;
3398e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee        }
3408e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee        super.onPause();
3418e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee    }
3428e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee
3438e5c8b11eb9516fa9b8407e0aa16c0405b5e717eYorke Lee    @Override
344ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    protected void onSaveInstanceState(Bundle outState) {
345ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        super.onSaveInstanceState(outState);
346ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
347b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
348b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
349ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
350ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    }
351ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
352ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    @Override
353c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onAttachFragment(Fragment fragment) {
354ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (fragment instanceof DialpadFragment) {
355ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mDialpadFragment = (DialpadFragment) fragment;
356c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            final FragmentTransaction transaction = getFragmentManager().beginTransaction();
357ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            transaction.hide(mDialpadFragment);
358c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            transaction.commit();
359ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof SmartDialSearchFragment) {
360ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
36163ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee            mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
36263ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee                    mPhoneNumberPickerActionListener);
363ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof SearchFragment) {
3642e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner            mRegularSearchFragment = (RegularSearchFragment) fragment;
36563ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee            mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
36663ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee                    mPhoneNumberPickerActionListener);
367ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof PhoneFavoriteFragment) {
368ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
3695781afe916100c4ed709e8edacac4a71db090108Yorke Lee            mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
37094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
371c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
372c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
373c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
374c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public boolean onMenuItemClick(MenuItem item) {
375c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        switch (item.getItemId()) {
376c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_import_export:
377c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // We hard-code the "contactsAreAvailable" argument because doing it properly would
378c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
379c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // now in Dialtacts for (potential) performance reasons. Compare with how it is
380c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // done in {@link PeopleActivity}.
381c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                ImportExportDialogFragment.show(getFragmentManager(), true,
382f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee                        DialtactsActivity.class);
383c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
384c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_clear_frequents:
385c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                ClearFrequentsDialog.show(getFragmentManager());
386c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
3875e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee            case R.id.menu_add_contact:
388c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                try {
389c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
390c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                } catch (ActivityNotFoundException e) {
391c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    Toast toast = Toast.makeText(this,
392c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            R.string.add_contact_not_available,
393c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            Toast.LENGTH_SHORT);
394c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    toast.show();
395c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                }
396c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
397c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_call_settings:
398b453e5b0ce106987c75901321ceba13ed111222cAlon Albert                handleMenuSettings();
3995e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                return true;
4005e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee            case R.id.menu_all_contacts:
4015e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                onShowAllContacts();
4025e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                return true;
403c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
404c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return false;
40594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
40694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
407b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    protected void handleMenuSettings() {
408b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        openTelephonySetting(this);
409b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    }
410b453e5b0ce106987c75901321ceba13ed111222cAlon Albert
411b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    public static void openTelephonySetting(Activity activity) {
412b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        final Intent settingsIntent = getCallSettingsIntent();
413b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        activity.startActivity(settingsIntent);
414b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    }
415b453e5b0ce106987c75901321ceba13ed111222cAlon Albert
41694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
41794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void onClick(View view) {
41894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        switch (view.getId()) {
419c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.overflow_menu: {
42036c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee                mOverflowMenu.show();
42194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
42294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
423c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.dialpad_button:
42435127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // Reset the boolean flag that tracks whether the dialpad was up because
42535127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // we were in call. Regardless of whether it was true before, we want to
42635127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // show the dialpad because the user has explicitly clicked the dialpad
42735127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // button.
42835127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                mInCallDialpadUp = false;
429c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                showDialpadFragment(true);
430c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
431c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.call_history_on_dialpad_button:
432c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.call_history_button:
433f420a5344995b2c450379159f679809690bc9880Yorke Lee                // Use explicit CallLogActivity intent instead of ACTION_VIEW +
434f420a5344995b2c450379159f679809690bc9880Yorke Lee                // CONTENT_TYPE, so that we always open our call log from our dialer
435c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Intent intent = new Intent(this, CallLogActivity.class);
436c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                startActivity(intent);
437c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
438c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.search_close_button:
439c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // Clear the search field
440c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                if (!TextUtils.isEmpty(mSearchView.getText())) {
4412eafb7afc46135bbbf5a79b437df33cd0fb66c0bYorke Lee                    mDialpadFragment.clearDialpad();
442c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchView.setText("");
44394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
44494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
445c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.voice_search_button:
446c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
447c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
448c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
44994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            default: {
45094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                Log.wtf(TAG, "Unexpected onClick event from " + view);
45194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
45294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
45394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
45494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
45594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
456c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
457c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
458c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
459c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            if (resultCode == RESULT_OK) {
460c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final ArrayList<String> matches = data.getStringArrayListExtra(
461c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        RecognizerIntent.EXTRA_RESULTS);
462c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                if (matches.size() > 0) {
463c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    final String match = matches.get(0);
464c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchView.setText(match);
465c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                } else {
466c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    Log.e(TAG, "Voice search - nothing heard");
467c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                }
468c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            } else {
469c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Log.e(TAG, "Voice search failed");
470c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
471c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
472c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onActivityResult(requestCode, resultCode, data);
473c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
47494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
475c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void showDialpadFragment(boolean animate) {
476ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        mDialpadFragment.setAdjustTranslationForAnimation(animate);
477c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction ft = getFragmentManager().beginTransaction();
478c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (animate) {
479c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.setCustomAnimations(R.anim.slide_in, 0);
480ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        } else {
481ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee            mDialpadFragment.setYFraction(0);
482c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
483c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.show(mDialpadFragment);
484c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.commit();
485c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
486c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
487ca19504ff58c91497885d9705905f7ab25e97ce0Yorke Lee    public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
488f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (mDialpadFragment == null) return;
489f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (clearDialpad) {
490f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            mDialpadFragment.clearDialpad();
491f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        }
492f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (!mDialpadFragment.isVisible()) return;
493ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        mDialpadFragment.setAdjustTranslationForAnimation(animate);
494c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction ft = getFragmentManager().beginTransaction();
495c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (animate) {
496c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.setCustomAnimations(0, R.anim.slide_out);
497c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
498c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.hide(mDialpadFragment);
499c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.commit();
500c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
50194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
50294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void prepareSearchView() {
503c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer = findViewById(R.id.search_view_container);
504c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewCloseButton = findViewById(R.id.search_close_button);
505c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewCloseButton.setOnClickListener(this);
506c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mVoiceSearchButton = findViewById(R.id.voice_search_button);
507c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mVoiceSearchButton.setOnClickListener(this);
508c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView = (EditText) findViewById(R.id.search_view);
509c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
510c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
511c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
512c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
513c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
514c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        @Override
515c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        public void onAnimationEnd(Animator animation) {
516c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setVisibility(View.GONE);
51794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
518c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    };
51994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
520b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean getInSearchUi() {
521b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        return mInDialpadSearch || mInRegularSearch;
522b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    }
523b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
524b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private void setNotInSearchUi() {
525b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInDialpadSearch = false;
526b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInRegularSearch = false;
527b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    }
528b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
529311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee    private void hideDialpadAndSearchUi() {
5307a06c4462c9f0fabeff15be92854a40f286058f8Yorke Lee        mSearchView.setText(null);
531311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee        hideDialpadFragment(false, true);
532311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee    }
533311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee
534c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void hideSearchBar() {
535c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee       hideSearchBar(true);
536c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
537c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
538c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void hideSearchBar(boolean shiftView) {
539c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (shiftView) {
540c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.animate().cancel();
541c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setAlpha(1);
542c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setTranslationY(0);
543c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
544c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    .setDuration(200).setListener(mHideListener);
545c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
546b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mFragmentsFrame.animate().withLayer()
547c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
548c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    new AnimatorListenerAdapter() {
549c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        @Override
550c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        public void onAnimationEnd(Animator animation) {
551c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            mBottomPaddingView.setVisibility(View.VISIBLE);
5525f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee                            mFragmentsFrame.setTranslationY(0);
5535f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee                            mActionBar.setVisibility(View.INVISIBLE);
554c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        }
555c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    });
556c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else {
557c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
5585f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee            mActionBar.setVisibility(View.INVISIBLE);
559c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
56094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
56194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
562c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void showSearchBar() {
563c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.animate().cancel();
564c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.setAlpha(0);
565c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
566c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
567c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                .setListener(new AnimatorListenerAdapter() {
568c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    @Override
569c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    public void onAnimationStart(Animator animation) {
570c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        mSearchViewContainer.setVisibility(View.VISIBLE);
5715f3ad54d27ab463e92ea557e1102bc1d34732adbYorke Lee                        mActionBar.setVisibility(View.VISIBLE);
572c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    }
573c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                });
574c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
575b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
576b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
577c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                .setListener(
578c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        new AnimatorListenerAdapter() {
579c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            @Override
580c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            public void onAnimationStart(Animator animation) {
581c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                                mBottomPaddingView.setVisibility(View.GONE);
582c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            }
583c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        });
58494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
58594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
58694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
587b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    public void setupFakeActionBarItems() {
588c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mMenuButton = findViewById(R.id.overflow_menu);
589c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (mMenuButton != null) {
590c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mMenuButton.setOnClickListener(this);
59136c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee
59236c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
59336c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            final Menu menu = mOverflowMenu.getMenu();
59436c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mOverflowMenu.inflate(R.menu.dialtacts_options);
59536c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mOverflowMenu.setOnMenuItemClickListener(this);
59636c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
597c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
598c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
599c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mCallHistoryButton = findViewById(R.id.call_history_button);
600c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
601c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mCallHistoryButton.setOnClickListener(this);
602c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
603c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mDialpadButton = findViewById(R.id.dialpad_button);
604c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
605c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mDialpadButton.setOnClickListener(this);
6069c1e0653addc1fff03601289a9f9a57b1daeaf57Christine Chen    }
6079c1e0653addc1fff03601289a9f9a57b1daeaf57Christine Chen
608c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void setupFakeActionBarItemsForDialpadFragment() {
609c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
610c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        callhistoryButton.setOnClickListener(this);
61194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
61294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
61394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void fixIntent(Intent intent) {
61494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // This should be cleaned up: the call key used to send an Intent
61594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // that just said to go to the recent calls list.  It now sends this
61694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // abstract action, but this class hasn't been rewritten to deal with it.
61794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
61894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
61994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            intent.putExtra("call_key", true);
62094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            setIntent(intent);
62194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
62294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
62394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
62494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
62594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Returns true if the intent is due to hitting the green send key (hardware call button:
62694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * KEYCODE_CALL) while in a call.
62794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
62894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent the intent that launched this activity
62994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param recentCallsRequest true if the intent is requesting to view recent calls
63094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @return true if the intent is due to hitting the green send key while in a call
63194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
632c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
63394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If there is a call in progress go to the call screen
63494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (recentCallsRequest) {
63594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            final boolean callKey = intent.getBooleanExtra("call_key", false);
63694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
63794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            try {
63894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
63994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (callKey && phone != null && phone.showCallScreen()) {
64094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    return true;
64194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
64294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            } catch (RemoteException e) {
64394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                Log.e(TAG, "Failed to handle send while in call", e);
64494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
64594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
64694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
64794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return false;
64894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
64994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
65094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
65194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Sets the current tab based on the intent's request type
65294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
65394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent Intent that contains information about which tab should be selected
65494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
655c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void displayFragment(Intent intent) {
65694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If we got here by hitting send and we're in call forward along to the in-call activity
65794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
65894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            getContentResolver()));
65994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
66094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            finish();
66194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return;
66294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
66394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
664669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee        if (mDialpadFragment != null) {
665669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee            final boolean phoneIsInUse = phoneIsInUse();
666669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee            if (phoneIsInUse || isDialIntent(intent)) {
667669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                mDialpadFragment.setStartedFromNewIntent(true);
668669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                if (phoneIsInUse && !mDialpadFragment.isVisible()) {
669669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                    mInCallDialpadUp = true;
670669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                }
671669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                showDialpadFragment(false);
67235127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            }
67394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
67494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
67594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
67694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
67794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void onNewIntent(Intent newIntent) {
67894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setIntent(newIntent);
67994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        fixIntent(newIntent);
680c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        displayFragment(newIntent);
68194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final String action = newIntent.getAction();
682c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
68394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        invalidateOptionsMenu();
68494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
68594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
68694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Returns true if the given intent contains a phone number to populate the dialer with */
68794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private boolean isDialIntent(Intent intent) {
68894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final String action = intent.getAction();
68994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
69094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return true;
69194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
69294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_VIEW.equals(action)) {
69394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            final Uri data = intent.getData();
6949d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Cheng            if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
69594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                return true;
69694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
69794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
69894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return false;
69994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
70094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
70194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
70294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Returns an appropriate call origin for this Activity. May return null when no call origin
70394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * should be used (e.g. when some 3rd party application launched the screen. Call origin is
70494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * for remembering the tab in which the user made a phone call, so the external app's DIAL
70594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * request should not be counted.)
70694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
70794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public String getCallOrigin() {
70894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
70994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
71094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
71194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
71294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
71394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * This text originally came from a FILTER_CONTACTS_ACTION intent received
71494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * by this activity. The stored text will then be cleared after after this
71594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * method returns.
71694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
71794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @return The stored filter text
71894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
71994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public String getAndClearFilterText() {
72094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        String filterText = mFilterText;
72194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFilterText = null;
72294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return filterText;
72394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
72494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
72594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
72694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
72794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * This is so child activities can check if they are supposed to display a filter.
72894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
72994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent The intent received in {@link #onNewIntent(Intent)}
73094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
73194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void setupFilterText(Intent intent) {
73294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If the intent was relaunched from history, don't apply the filter text.
73394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
73494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return;
73594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
73694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
73794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (filter != null && filter.length() > 0) {
73894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            mFilterText = filter;
73994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
74094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
74194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
7428898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee    private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
7438898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee            new PhoneFavoriteFragment.Listener() {
74494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        @Override
74594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        public void onContactSelected(Uri contactUri) {
74694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            PhoneNumberInteraction.startInteractionForPhoneCall(
747c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        DialtactsActivity.this, contactUri, getCallOrigin());
74894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
74994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
75094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        @Override
75194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        public void onCallNumberDirectly(String phoneNumber) {
7529d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Cheng            Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
75394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            startActivity(intent);
75494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
75594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
75694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
757c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
758c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
759c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * be showing when the search key is pressed so there is more state management involved.
76094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
76194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
76294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void startSearch(String initialQuery, boolean selectInitialQuery,
76394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            Bundle appSearchData, boolean globalSearch) {
764c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
76594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            if (mInSearchUi) {
76694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (mSearchView.hasFocus()) {
76794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    showInputMethod(mSearchView.findFocus());
76894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                } else {
76994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    mSearchView.requestFocus();
77094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
77194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            } else {
77294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                enterSearchUi();
77394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
77494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        } else {
77594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
77694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
777c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }*/
77894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
77994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void showInputMethod(View view) {
780c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final InputMethodManager imm = (InputMethodManager) getSystemService(
781c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.INPUT_METHOD_SERVICE);
78294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (imm != null) {
783c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            imm.showSoftInput(view, 0);
78494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
78594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
78694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
78794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void hideInputMethod(View view) {
788c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final InputMethodManager imm = (InputMethodManager) getSystemService(
789c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.INPUT_METHOD_SERVICE);
79094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (imm != null && view != null) {
79194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
79294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
79394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
79494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
79594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
796c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Shows the search fragment
79794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
798ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private void enterSearchUi(boolean smartDialSearch, String query) {
79934bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee        if (getFragmentManager().isDestroyed()) {
80034bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // Weird race condition where fragment is doing work after the activity is destroyed
80134bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // due to talkback being on (b/10209937). Just return since we can't do any
80234bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // constructive here.
80334bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            return;
80434bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee        }
80534bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee
806ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (DEBUG) {
807ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
808ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        }
809ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
810c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction transaction = getFragmentManager().beginTransaction();
811c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
812ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
813ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        SearchFragment fragment;
814b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        if (mInDialpadSearch) {
815b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mSmartDialSearchFragment);
816b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else if (mInRegularSearch) {
817b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mRegularSearchFragment);
818b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else {
819b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mPhoneFavoriteFragment);
820b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        }
821b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
822b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        final String tag;
823b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        if (smartDialSearch) {
824b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
825b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else {
826b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            tag = TAG_REGULAR_SEARCH_FRAGMENT;
827b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        }
828b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInDialpadSearch = smartDialSearch;
829b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInRegularSearch = !smartDialSearch;
830ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
831ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
832ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (fragment == null) {
833ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (smartDialSearch) {
834ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                fragment = new SmartDialSearchFragment();
835ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            } else {
8362e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner                fragment = new RegularSearchFragment();
837ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
83894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
839b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        transaction.replace(R.id.dialtacts_frame, fragment, tag);
840ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        transaction.addToBackStack(null);
841ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        fragment.setQueryString(query, false);
842ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        transaction.commit();
84394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
84494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
84594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
846c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Hides the search fragment
84794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
848c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void exitSearchUi() {
8493487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee        // See related bug in enterSearchUI();
8503487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee        if (getFragmentManager().isDestroyed()) {
8513487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee            return;
8523487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee        }
853b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        // Go all the way back to the favorites fragment, regardless of how many times we
854b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        // transitioned between search fragments
855b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
856b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
857b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        setNotInSearchUi();
85894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
85994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
86094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Returns an Intent to launch Call Settings screen */
86194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static Intent getCallSettingsIntent() {
86294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final Intent intent = new Intent(Intent.ACTION_MAIN);
86394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
86494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
86594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return intent;
86694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
86794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
86894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
869c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onBackPressed() {
870ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
871f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            hideDialpadFragment(true, false);
872b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else if (getInSearchUi()) {
873c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchView.setText(null);
874f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            mDialpadFragment.clearDialpad();
875c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else if (isTaskRoot()) {
876c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // Instead of stopping, simply push this to the back of the stack.
877c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // This is only done when running at the top of the stack;
878c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // otherwise, we have been launched by someone else so need to
879c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // allow the user to go back to the caller.
880c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            moveTaskToBack(false);
881c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else {
882c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            super.onBackPressed();
88394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
884c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
885c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
886c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
887c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onDialpadQueryChanged(String query) {
888c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
889c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
890c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
891ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (DEBUG) {
892ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
893ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
894ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
895ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // This callback can happen if the dialpad fragment is recreated because of
896ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // activity destruction. In that case, don't update the search view because
897ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // that would bring the user back to the search fragment regardless of the
898ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // previous state of the application. Instead, just return here and let the
899ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // fragment manager correctly figure out whatever fragment was last displayed.
900ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                return;
901ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
902c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchView.setText(normalizedQuery);
90394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
90494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
905c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
906c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
907c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onListFragmentScrollStateChange(int scrollState) {
908c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
909f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            hideDialpadFragment(true, false);
910c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            hideInputMethod(getCurrentFocus());
911c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
912c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
913c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
914c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
915c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onDialpadFragmentStarted() {
916c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        setupFakeActionBarItemsForDialpadFragment();
917c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
918c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
919c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean phoneIsInUse() {
920c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final TelephonyManager tm = (TelephonyManager) getSystemService(
921c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.TELEPHONY_SERVICE);
922c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
923c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
9248dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
9258dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    @Override
9268dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    public void onShowAllContacts() {
9278dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        final Intent intent = new Intent(this, AllContactsActivity.class);
9288dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        startActivity(intent);
9298dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    }
930e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
931e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    public static Intent getAddNumberToContactIntent(CharSequence text) {
932e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
933e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.putExtra(Intents.Insert.PHONE, text);
934e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.setType(Contacts.CONTENT_ITEM_TYPE);
935e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        return intent;
936e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    }
937e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
938e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    public static Intent getInsertContactWithNameIntent(CharSequence text) {
939e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
940e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.putExtra(Intents.Insert.NAME, text);
941e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        return intent;
942e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    }
94394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng}
944