DialtactsActivity.java revision 3487db78fd47ccecef4e6b86e5cacfc0a8e6ba83
194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/*
2c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee * Copyright (C) 2013 The Android Open Source Project
394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * you may not use this file except in compliance with the License.
694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * You may obtain a copy of the License at
794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
1094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Unless required by applicable law or agreed to in writing, software
1194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
1294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * See the License for the specific language governing permissions and
1494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * limitations under the License.
1594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
1694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
1794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengpackage com.android.dialer;
1894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
19c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.animation.Animator;
20c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.animation.Animator.AnimatorListener;
21c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.animation.AnimatorListenerAdapter;
2294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.app.Activity;
2394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.app.Fragment;
2494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.app.FragmentManager;
25b207f8a073e8b3c001986b31c951f2c951cc631bYorke Leeimport android.app.FragmentManager.BackStackEntry;
2694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.app.FragmentTransaction;
27073b5cf341964796ccf082d30aff63225d3adb28Chiao Chengimport android.content.ActivityNotFoundException;
2894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.Context;
2994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.Intent;
3094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.net.Uri;
3194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.Bundle;
3202403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Leeimport android.os.Handler;
3302403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Leeimport android.os.Message;
3494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.RemoteException;
3594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.os.ServiceManager;
3694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.CallLog.Calls;
3794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.ContactsContract.Contacts;
38e142481570d7fbda5d035555fe217314e396ae90Yorke Leeimport android.provider.ContactsContract.Intents;
3994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.ContactsContract.Intents.UI;
40c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.speech.RecognizerIntent;
41c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.telephony.TelephonyManager;
42c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.text.Editable;
4394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.text.TextUtils;
44c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.text.TextWatcher;
4594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.util.Log;
4694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.Menu;
4794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.MenuItem;
4894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.View;
4994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.View.OnFocusChangeListener;
5094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.inputmethod.InputMethodManager;
51c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.widget.AbsListView.OnScrollListener;
52c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport android.widget.EditText;
5394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.PopupMenu;
54073b5cf341964796ccf082d30aff63225d3adb28Chiao Chengimport android.widget.Toast;
5594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
569d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Chengimport com.android.contacts.common.CallUtil;
57603ff6858d321e8265b3af6363a63df2827e0334Chiao Chengimport com.android.contacts.common.activity.TransactionSafeActivity;
58c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.contacts.common.dialog.ClearFrequentsDialog;
59c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.contacts.common.interactions.ImportExportDialogFragment;
608efbcf920eeebc826c18b3c474e9c62b77dcd232Chiao Chengimport com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
61c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.calllog.CallLogActivity;
62fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Leeimport com.android.dialer.database.DialerDatabaseHelper;
63f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Leeimport com.android.dialer.dialpad.DialpadFragment;
64c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.dialpad.SmartDialNameMatcher;
65fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Leeimport com.android.dialer.dialpad.SmartDialPrefix;
661429f1a5c2b993a5013bfda7cfd75a2c43ead2bfChiao Chengimport com.android.dialer.interactions.PhoneNumberInteraction;
678dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport com.android.dialer.list.AllContactsActivity;
68c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.list.OnListFragmentScrolledListener;
69b453e5b0ce106987c75901321ceba13ed111222cAlon Albertimport com.android.dialer.list.PhoneFavoriteFragment;
702e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shraunerimport com.android.dialer.list.RegularSearchFragment;
712e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shraunerimport com.android.dialer.list.SearchFragment;
72c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport com.android.dialer.list.SmartDialSearchFragment;
730baa98b5163f83a715b37e3cffe1488ac88ab049Yorke Leeimport com.android.dialerbind.DatabaseHelperManager;
7494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.internal.telephony.ITelephony;
7594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
76c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leeimport java.util.ArrayList;
77c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
7894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/**
7994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * The dialer tab's title is 'phone', a more common name (see strings.xml).
8094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
81c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Leepublic class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
82f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee        DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
83c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        OnListFragmentScrolledListener,
848dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        DialpadFragment.OnDialpadFragmentStartedListener,
858dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        PhoneFavoriteFragment.OnShowAllContactsListener {
8694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String TAG = "DialtactsActivity";
8794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
8894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static final boolean DEBUG = false;
8994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
90f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee    public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
91f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee
9294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Used to open Call Setting */
9394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String PHONE_PACKAGE = "com.android.phone";
9494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String CALL_SETTINGS_CLASS_NAME =
9594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            "com.android.phone.CallFeaturesSetting";
9694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** @see #getCallOrigin() */
9794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String CALL_ORIGIN_DIALTACTS =
9894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            "com.android.dialer.DialtactsActivity";
9994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
100b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
101b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
102ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private static final String KEY_SEARCH_QUERY = "search_query";
103ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private static final String KEY_FIRST_LAUNCH = "first_launch";
104ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
105c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
106c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
107c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
108c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final String TAG_FAVORITES_FRAGMENT = "favorites";
109c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
11094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
11194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
11294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
11394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
11494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
11594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
11694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
117c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
11894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
119c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private String mFilterText;
12094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
12194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
122c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * The main fragment displaying the user's favorites and frequent contacts
12394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
1248898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee    private PhoneFavoriteFragment mPhoneFavoriteFragment;
125c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
12694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
127c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment containing the dialpad that slides into view
12894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
129f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee    private DialpadFragment mDialpadFragment;
13094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
131c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /**
132c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment for searching phone numbers using the alphanumeric keyboard.
133c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     */
1342e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner    private RegularSearchFragment mRegularSearchFragment;
13594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
136c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /**
137c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Fragment for searching phone numbers using the dialpad.
138c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     */
139c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private SmartDialSearchFragment mSmartDialSearchFragment;
14094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
14194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private View mMenuButton;
142c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mCallHistoryButton;
143c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mDialpadButton;
14436c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee    private PopupMenu mOverflowMenu;
14594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
146c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    // Padding view used to shift the fragments up when the dialpad is shown.
147c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private View mBottomPaddingView;
148b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private View mFragmentsFrame;
149b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
150b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean mInDialpadSearch;
151b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean mInRegularSearch;
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
17102403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    private static final int EVENT_DELAYED_HIDE_SEARCH = 1;
17202403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee
17302403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    // Wait this much time (in ms) before hiding the search fragment after clicking on
17402403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    // a search result to dial, to avoid jank
17502403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    private static final int CLEAR_SEARCH_DELAY = 500;
17602403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee
17702403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    final Handler mHandler = new Handler() {
17802403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee        @Override
17902403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee        public void handleMessage(Message msg) {
18002403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee            if (msg.what == EVENT_DELAYED_HIDE_SEARCH) {
18102403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee                hideDialpadAndSearchUi();
18202403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee            }
18302403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee        }
18402403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee    };
18502403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee
18636c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee    private class OverflowPopupMenu extends PopupMenu {
18736c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        public OverflowPopupMenu(Context context, View anchor) {
18836c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            super(context, anchor);
18936c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        }
19036c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee
19136c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        @Override
19236c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        public void show() {
19336c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            final Menu menu = getMenu();
19436c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
19536c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
19636c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            super.show();
19736c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee        }
19836c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee    }
19936c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee
20094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
20194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Listener used when one of phone numbers in search UI is selected. This will initiate a
20294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * phone call using the phone number.
20394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
20494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
20594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            new OnPhoneNumberPickerActionListener() {
20694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
20794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onPickPhoneNumberAction(Uri dataUri) {
20894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    // Specify call-origin so that users will see the previous tab instead of
20994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    // CallLog screen (search UI will be automatically exited).
21094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    PhoneNumberInteraction.startInteractionForPhoneCall(
211c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        DialtactsActivity.this, dataUri, getCallOrigin());
21202403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee                    mHandler.sendEmptyMessageDelayed(EVENT_DELAYED_HIDE_SEARCH,
21302403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee                            CLEAR_SEARCH_DELAY);
21494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
21594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
21694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
21731a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                public void onCallNumberDirectly(String phoneNumber) {
21831a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                    Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
21931a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                    startActivity(intent);
22002403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee                    mHandler.sendEmptyMessageDelayed(EVENT_DELAYED_HIDE_SEARCH,
22102403e5cd7eb0f1cff85146013e33691e1dc2e8bYorke Lee                            CLEAR_SEARCH_DELAY);
22231a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                }
22331a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner
22431a760be9135b5ef05e504dbd1594e1617af0326Jay Shrauner                @Override
22594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onShortcutIntentCreated(Intent intent) {
22694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
22794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
22894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
22994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                @Override
23094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                public void onHomeInActionBarSelected() {
23194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    exitSearchUi();
23294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
23394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
23494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
23594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
23694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Listener used to send search queries to the phone search fragment.
23794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
238c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
239c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
240c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
241c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
24294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
243c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
244c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void onTextChanged(CharSequence s, int start, int before, int count) {
245c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final String newText = s.toString();
246ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                if (newText.equals(mSearchQuery)) {
247ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // If the query hasn't changed (perhaps due to activity being destroyed
248ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // and restored, or user launching the same DIAL intent twice), then there is
249ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    // no need to do anything here.
250ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    return;
251ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                }
252ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                mSearchQuery = newText;
253ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                if (DEBUG) {
254ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                    Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
255ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                }
256b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                final boolean dialpadSearch = isDialpadShowing();
257ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
258c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // Show search result with non-empty text. Show a bare list otherwise.
259b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                if (TextUtils.isEmpty(newText) && getInSearchUi()) {
260c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    exitSearchUi();
261c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchViewCloseButton.setVisibility(View.GONE);
2623b82fc14c1d05a85e07e457c4d6eeb0e405b7eb3Yorke Lee                    mVoiceSearchButton.setVisibility(View.VISIBLE);
263c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    return;
264b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                } else if (!TextUtils.isEmpty(newText)) {
265b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
266b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                            (!dialpadSearch && mInRegularSearch);
267b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    if (!sameSearchMode) {
268b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        // call enterSearchUi only if we are switching search modes, or entering
269b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        // search ui for the first time
270b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        enterSearchUi(dialpadSearch, newText);
271b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    }
27294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
273b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    if (dialpadSearch && mSmartDialSearchFragment != null) {
274b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                            mSmartDialSearchFragment.setQueryString(newText, false);
275b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    } else if (mRegularSearchFragment != null) {
276b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                        mRegularSearchFragment.setQueryString(newText, false);
277b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    }
278b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    mSearchViewCloseButton.setVisibility(View.VISIBLE);
2793b82fc14c1d05a85e07e457c4d6eeb0e405b7eb3Yorke Lee                    mVoiceSearchButton.setVisibility(View.GONE);
280b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                    return;
28194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
282c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
28394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
284c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            @Override
285c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            public void afterTextChanged(Editable s) {
286c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
28794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
28894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
289c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean isDialpadShowing() {
290c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return mDialpadFragment != null && mDialpadFragment.isVisible();
291c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
292c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
29394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
294c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onCreate(Bundle savedInstanceState) {
295c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onCreate(savedInstanceState);
29698702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        mFirstLaunch = true;
29798702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee
29894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final Intent intent = getIntent();
29994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        fixIntent(intent);
30094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
3018898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee        setContentView(R.layout.dialtacts_activity);
30294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
303c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        getActionBar().hide();
30494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
305ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
306ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        // is null. Otherwise the fragment manager takes care of recreating these fragments.
307c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (savedInstanceState == null) {
308ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
30994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
310c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            final FragmentTransaction ft = getFragmentManager().beginTransaction();
311ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
312ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
313c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.commit();
314ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else {
315ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
316b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
317b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
318ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
31994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
32094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
321c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
322b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame = findViewById(R.id.dialtacts_frame);
323c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        prepareSearchView();
32494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
32594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
326c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                && savedInstanceState == null) {
32794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            setupFilterText(intent);
32894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
329fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee
330b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        setupFakeActionBarItems();
331b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
3320baa98b5163f83a715b37e3cffe1488ac88ab049Yorke Lee        mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
333fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee        SmartDialPrefix.initializeNanpSettings(this);
33494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
33594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
33694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
337c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onResume() {
338c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onResume();
33998702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        if (mFirstLaunch) {
34098702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee            displayFragment(getIntent());
34135127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee        } else if (!phoneIsInUse() && mInCallDialpadUp) {
34235127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            hideDialpadFragment(false, true);
34335127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            mInCallDialpadUp = false;
34498702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        }
34535127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee
34698702de246d42c844fabdb8a1f3407bf1747d379Yorke Lee        mFirstLaunch = false;
347fce269a30e2ec112ea4c287d97e08ef7b3b31b89Yorke Lee        mDialerDatabaseHelper.startSmartDialUpdateThread();
348c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
34994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
350c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
351ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    protected void onSaveInstanceState(Bundle outState) {
352ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        super.onSaveInstanceState(outState);
353ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
354b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
355b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
356ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
357ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    }
358ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
359ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    @Override
360c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onAttachFragment(Fragment fragment) {
361ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (fragment instanceof DialpadFragment) {
362ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mDialpadFragment = (DialpadFragment) fragment;
363c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            final FragmentTransaction transaction = getFragmentManager().beginTransaction();
364ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            transaction.hide(mDialpadFragment);
365c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            transaction.commit();
366ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof SmartDialSearchFragment) {
367ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
36863ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee            mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
36963ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee                    mPhoneNumberPickerActionListener);
370ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof SearchFragment) {
3712e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner            mRegularSearchFragment = (RegularSearchFragment) fragment;
37263ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee            mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
37363ea4cd19569c8d5ee6f9a7ce2c933d09359fc0bYorke Lee                    mPhoneNumberPickerActionListener);
374ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        } else if (fragment instanceof PhoneFavoriteFragment) {
375ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
3765781afe916100c4ed709e8edacac4a71db090108Yorke Lee            mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
37794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
378c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
379c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
380c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
381c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public boolean onMenuItemClick(MenuItem item) {
382c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        switch (item.getItemId()) {
383c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_import_export:
384c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // We hard-code the "contactsAreAvailable" argument because doing it properly would
385c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
386c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // now in Dialtacts for (potential) performance reasons. Compare with how it is
387c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // done in {@link PeopleActivity}.
388c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                ImportExportDialogFragment.show(getFragmentManager(), true,
389f74011e7a9b4007f6795ecee5adc2739d5fad4e6Yorke Lee                        DialtactsActivity.class);
390c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
391c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_clear_frequents:
392c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                ClearFrequentsDialog.show(getFragmentManager());
393c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
3945e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee            case R.id.menu_add_contact:
395c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                try {
396c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
397c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                } catch (ActivityNotFoundException e) {
398c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    Toast toast = Toast.makeText(this,
399c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            R.string.add_contact_not_available,
400c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            Toast.LENGTH_SHORT);
401c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    toast.show();
402c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                }
403c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                return true;
404c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.menu_call_settings:
405b453e5b0ce106987c75901321ceba13ed111222cAlon Albert                handleMenuSettings();
4065e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                return true;
4075e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee            case R.id.menu_all_contacts:
4085e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                onShowAllContacts();
4095e931977ba56a38db2e72785a4067b5a1fdb79c7Yorke Lee                return true;
410c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
411c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return false;
41294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
41394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
414b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    protected void handleMenuSettings() {
415b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        openTelephonySetting(this);
416b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    }
417b453e5b0ce106987c75901321ceba13ed111222cAlon Albert
418b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    public static void openTelephonySetting(Activity activity) {
419b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        final Intent settingsIntent = getCallSettingsIntent();
420b453e5b0ce106987c75901321ceba13ed111222cAlon Albert        activity.startActivity(settingsIntent);
421b453e5b0ce106987c75901321ceba13ed111222cAlon Albert    }
422b453e5b0ce106987c75901321ceba13ed111222cAlon Albert
42394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
42494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void onClick(View view) {
42594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        switch (view.getId()) {
426c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.overflow_menu: {
42736c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee                mOverflowMenu.show();
42894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
42994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
430c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.dialpad_button:
43135127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // Reset the boolean flag that tracks whether the dialpad was up because
43235127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // we were in call. Regardless of whether it was true before, we want to
43335127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // show the dialpad because the user has explicitly clicked the dialpad
43435127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                // button.
43535127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee                mInCallDialpadUp = false;
436c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                showDialpadFragment(true);
437c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
438c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.call_history_on_dialpad_button:
439c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.call_history_button:
440f420a5344995b2c450379159f679809690bc9880Yorke Lee                // Use explicit CallLogActivity intent instead of ACTION_VIEW +
441f420a5344995b2c450379159f679809690bc9880Yorke Lee                // CONTENT_TYPE, so that we always open our call log from our dialer
442c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Intent intent = new Intent(this, CallLogActivity.class);
443c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                startActivity(intent);
444c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
445c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.search_close_button:
446c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                // Clear the search field
447c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                if (!TextUtils.isEmpty(mSearchView.getText())) {
4482eafb7afc46135bbbf5a79b437df33cd0fb66c0bYorke Lee                    mDialpadFragment.clearDialpad();
449c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchView.setText("");
45094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
45194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
452c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            case R.id.voice_search_button:
453c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
454c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
455c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                break;
45694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            default: {
45794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                Log.wtf(TAG, "Unexpected onClick event from " + view);
45894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                break;
45994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
46094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
46194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
46294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
463c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
464c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
465c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
466c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            if (resultCode == RESULT_OK) {
467c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                final ArrayList<String> matches = data.getStringArrayListExtra(
468c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        RecognizerIntent.EXTRA_RESULTS);
469c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                if (matches.size() > 0) {
470c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    final String match = matches.get(0);
471c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    mSearchView.setText(match);
472c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                } else {
473c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    Log.e(TAG, "Voice search - nothing heard");
474c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                }
475c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            } else {
476c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Log.e(TAG, "Voice search failed");
477c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            }
478c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
479c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        super.onActivityResult(requestCode, resultCode, data);
480c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
48194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
482c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void showDialpadFragment(boolean animate) {
483ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        mDialpadFragment.setAdjustTranslationForAnimation(animate);
484c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction ft = getFragmentManager().beginTransaction();
485c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (animate) {
486c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.setCustomAnimations(R.anim.slide_in, 0);
487ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        } else {
488ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee            mDialpadFragment.setYFraction(0);
489c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
490c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.show(mDialpadFragment);
491c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.commit();
492c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
493c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
494ca19504ff58c91497885d9705905f7ab25e97ce0Yorke Lee    public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
495f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (mDialpadFragment == null) return;
496f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (clearDialpad) {
497f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            mDialpadFragment.clearDialpad();
498f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        }
499f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee        if (!mDialpadFragment.isVisible()) return;
500ea695cb0f877ad49a98df184efe982de83ed09d7Yorke Lee        mDialpadFragment.setAdjustTranslationForAnimation(animate);
501c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction ft = getFragmentManager().beginTransaction();
502c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (animate) {
503c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            ft.setCustomAnimations(0, R.anim.slide_out);
504c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
505c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.hide(mDialpadFragment);
506c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        ft.commit();
507c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
50894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
50994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void prepareSearchView() {
510c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer = findViewById(R.id.search_view_container);
511c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewCloseButton = findViewById(R.id.search_close_button);
512c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewCloseButton.setOnClickListener(this);
513c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mVoiceSearchButton = findViewById(R.id.voice_search_button);
514c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mVoiceSearchButton.setOnClickListener(this);
515c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView = (EditText) findViewById(R.id.search_view);
516c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
517c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
518c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
51994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            @Override
52094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            public void onFocusChange(View view, boolean hasFocus) {
52194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (hasFocus) {
52294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    showInputMethod(view.findFocus());
52394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
52494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
52594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        });
526c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
527c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
528c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
529c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        @Override
530c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        public void onAnimationEnd(Animator animation) {
531c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setVisibility(View.GONE);
53294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
533c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    };
53494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
535b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private boolean getInSearchUi() {
536b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        return mInDialpadSearch || mInRegularSearch;
537b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    }
538b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
539b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    private void setNotInSearchUi() {
540b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInDialpadSearch = false;
541b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInRegularSearch = false;
542b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    }
543b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
544311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee    private void hideDialpadAndSearchUi() {
5457a06c4462c9f0fabeff15be92854a40f286058f8Yorke Lee        mSearchView.setText(null);
546311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee        hideDialpadFragment(false, true);
547311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee    }
548311969c96e36da85118d4aabdddbd98e5ab6a3c8Yorke Lee
549c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void hideSearchBar() {
550c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee       hideSearchBar(true);
551c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
552c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
553c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void hideSearchBar(boolean shiftView) {
554c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (shiftView) {
555c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.animate().cancel();
556c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setAlpha(1);
557c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setTranslationY(0);
558c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
559c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    .setDuration(200).setListener(mHideListener);
560c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
561b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            mFragmentsFrame.animate().withLayer()
562c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
563c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    new AnimatorListenerAdapter() {
564c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        @Override
565c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        public void onAnimationEnd(Animator animation) {
566c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            mBottomPaddingView.setVisibility(View.VISIBLE);
567b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee                                mFragmentsFrame.setTranslationY(0);
568c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        }
569c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    });
570c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else {
571c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
572c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
57394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
57494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
575c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void showSearchBar() {
576c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.animate().cancel();
577c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.setAlpha(0);
578c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
579c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
580c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                .setListener(new AnimatorListenerAdapter() {
581c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    @Override
582c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    public void onAnimationStart(Animator animation) {
583c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        mSearchViewContainer.setVisibility(View.VISIBLE);
584c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                    }
585c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                });
586c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
587b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
588b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
589c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                .setListener(
590c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        new AnimatorListenerAdapter() {
591c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            @Override
592c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            public void onAnimationStart(Animator animation) {
593c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                                mBottomPaddingView.setVisibility(View.GONE);
594c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                            }
595c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        });
59694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
59794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
59894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
599b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee    public void setupFakeActionBarItems() {
600c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mMenuButton = findViewById(R.id.overflow_menu);
601c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (mMenuButton != null) {
602c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mMenuButton.setOnClickListener(this);
60336c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee
60436c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
60536c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            final Menu menu = mOverflowMenu.getMenu();
60636c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mOverflowMenu.inflate(R.menu.dialtacts_options);
60736c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mOverflowMenu.setOnMenuItemClickListener(this);
60836c5413800ecd06d4d93a9e5333078fc7fbcf1d6Yorke Lee            mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
609c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
610c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
611c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mCallHistoryButton = findViewById(R.id.call_history_button);
612c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
613c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mCallHistoryButton.setOnClickListener(this);
614c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
615c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mDialpadButton = findViewById(R.id.dialpad_button);
616c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
617c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        mDialpadButton.setOnClickListener(this);
6189c1e0653addc1fff03601289a9f9a57b1daeaf57Christine Chen    }
6199c1e0653addc1fff03601289a9f9a57b1daeaf57Christine Chen
620c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void setupFakeActionBarItemsForDialpadFragment() {
621c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
622c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        callhistoryButton.setOnClickListener(this);
62394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
62494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
62594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void fixIntent(Intent intent) {
62694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // This should be cleaned up: the call key used to send an Intent
62794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // that just said to go to the recent calls list.  It now sends this
62894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // abstract action, but this class hasn't been rewritten to deal with it.
62994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
63094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
63194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            intent.putExtra("call_key", true);
63294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            setIntent(intent);
63394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
63494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
63594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
63694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
63794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Returns true if the intent is due to hitting the green send key (hardware call button:
63894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * KEYCODE_CALL) while in a call.
63994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
64094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent the intent that launched this activity
64194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param recentCallsRequest true if the intent is requesting to view recent calls
64294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @return true if the intent is due to hitting the green send key while in a call
64394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
644c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
64594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If there is a call in progress go to the call screen
64694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (recentCallsRequest) {
64794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            final boolean callKey = intent.getBooleanExtra("call_key", false);
64894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
64994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            try {
65094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
65194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (callKey && phone != null && phone.showCallScreen()) {
65294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    return true;
65394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
65494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            } catch (RemoteException e) {
65594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                Log.e(TAG, "Failed to handle send while in call", e);
65694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
65794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
65894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
65994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return false;
66094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
66194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
66294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
66394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Sets the current tab based on the intent's request type
66494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
66594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent Intent that contains information about which tab should be selected
66694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
667c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void displayFragment(Intent intent) {
66894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If we got here by hitting send and we're in call forward along to the in-call activity
66994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
67094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            getContentResolver()));
67194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
67294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            finish();
67394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return;
67494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
67594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
676669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee        if (mDialpadFragment != null) {
677669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee            final boolean phoneIsInUse = phoneIsInUse();
678669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee            if (phoneIsInUse || isDialIntent(intent)) {
679669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                mDialpadFragment.setStartedFromNewIntent(true);
680669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                if (phoneIsInUse && !mDialpadFragment.isVisible()) {
681669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                    mInCallDialpadUp = true;
682669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                }
683669b608b31d468084bbae81ba8c51fec44814b18Yorke Lee                showDialpadFragment(false);
68435127cdea7f9043626ae9d0c6becb31730d0b236Yorke Lee            }
68594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
68694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
68794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
68894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
68994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void onNewIntent(Intent newIntent) {
69094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setIntent(newIntent);
69194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        fixIntent(newIntent);
692c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        displayFragment(newIntent);
69394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final String action = newIntent.getAction();
694c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
69594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        invalidateOptionsMenu();
69694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
69794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
69894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Returns true if the given intent contains a phone number to populate the dialer with */
69994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private boolean isDialIntent(Intent intent) {
70094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final String action = intent.getAction();
70194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
70294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return true;
70394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
70494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (Intent.ACTION_VIEW.equals(action)) {
70594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            final Uri data = intent.getData();
7069d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Cheng            if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
70794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                return true;
70894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
70994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
71094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return false;
71194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
71294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
71394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
71494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Returns an appropriate call origin for this Activity. May return null when no call origin
71594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * should be used (e.g. when some 3rd party application launched the screen. Call origin is
71694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * for remembering the tab in which the user made a phone call, so the external app's DIAL
71794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * request should not be counted.)
71894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
71994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public String getCallOrigin() {
72094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
72194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
72294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
72394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
72494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
72594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * This text originally came from a FILTER_CONTACTS_ACTION intent received
72694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * by this activity. The stored text will then be cleared after after this
72794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * method returns.
72894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
72994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @return The stored filter text
73094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
73194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public String getAndClearFilterText() {
73294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        String filterText = mFilterText;
73394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFilterText = null;
73494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return filterText;
73594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
73694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
73794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
73894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
73994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * This is so child activities can check if they are supposed to display a filter.
74094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
74194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param intent The intent received in {@link #onNewIntent(Intent)}
74294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
74394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void setupFilterText(Intent intent) {
74494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // If the intent was relaunched from history, don't apply the filter text.
74594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
74694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            return;
74794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
74894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
74994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (filter != null && filter.length() > 0) {
75094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            mFilterText = filter;
75194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
75294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
75394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
7548898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee    private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
7558898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Lee            new PhoneFavoriteFragment.Listener() {
75694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        @Override
75794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        public void onContactSelected(Uri contactUri) {
75894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            PhoneNumberInteraction.startInteractionForPhoneCall(
759c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                        DialtactsActivity.this, contactUri, getCallOrigin());
76094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
76194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
76294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        @Override
76394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        public void onCallNumberDirectly(String phoneNumber) {
7649d4f3b20793a007b4489547aa40fc1049d0d8fefChiao Cheng            Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
76594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            startActivity(intent);
76694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
76794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    };
76894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
769c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
770c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
771c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * be showing when the search key is pressed so there is more state management involved.
77294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
77394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
77494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void startSearch(String initialQuery, boolean selectInitialQuery,
77594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            Bundle appSearchData, boolean globalSearch) {
776c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
77794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            if (mInSearchUi) {
77894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                if (mSearchView.hasFocus()) {
77994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    showInputMethod(mSearchView.findFocus());
78094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                } else {
78194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    mSearchView.requestFocus();
78294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                }
78394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            } else {
78494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                enterSearchUi();
78594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
78694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        } else {
78794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
78894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
789c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }*/
79094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
79194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void showInputMethod(View view) {
792c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final InputMethodManager imm = (InputMethodManager) getSystemService(
793c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.INPUT_METHOD_SERVICE);
79494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (imm != null) {
795c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            imm.showSoftInput(view, 0);
79694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
79794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
79894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
79994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void hideInputMethod(View view) {
800c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final InputMethodManager imm = (InputMethodManager) getSystemService(
801c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.INPUT_METHOD_SERVICE);
80294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (imm != null && view != null) {
80394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
80494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
80594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
80694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
80794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
808c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Shows the search fragment
80994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
810ef2b7388569a94432278a617b528f628385fbb8cYorke Lee    private void enterSearchUi(boolean smartDialSearch, String query) {
81134bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee        if (getFragmentManager().isDestroyed()) {
81234bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // Weird race condition where fragment is doing work after the activity is destroyed
81334bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // due to talkback being on (b/10209937). Just return since we can't do any
81434bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            // constructive here.
81534bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee            return;
81634bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee        }
81734bdf87700194c35ab8f43c001391bb2ac8bc993Yorke Lee
818ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (DEBUG) {
819ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
820ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        }
821ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
822c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final FragmentTransaction transaction = getFragmentManager().beginTransaction();
823c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
824ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
825ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        SearchFragment fragment;
826b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        if (mInDialpadSearch) {
827b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mSmartDialSearchFragment);
828b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else if (mInRegularSearch) {
829b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mRegularSearchFragment);
830b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else {
831b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            transaction.remove(mPhoneFavoriteFragment);
832b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        }
833b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee
834b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        final String tag;
835b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        if (smartDialSearch) {
836b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
837b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else {
838b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee            tag = TAG_REGULAR_SEARCH_FRAGMENT;
839b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        }
840b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInDialpadSearch = smartDialSearch;
841b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        mInRegularSearch = !smartDialSearch;
842ef2b7388569a94432278a617b528f628385fbb8cYorke Lee
843ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
844ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (fragment == null) {
845ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (smartDialSearch) {
846ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                fragment = new SmartDialSearchFragment();
847ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            } else {
8482e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shrauner                fragment = new RegularSearchFragment();
849ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
85094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
851b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        transaction.replace(R.id.dialtacts_frame, fragment, tag);
852ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        transaction.addToBackStack(null);
853ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        fragment.setQueryString(query, false);
854ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        transaction.commit();
85594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
85694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
85794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
858c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee     * Hides the search fragment
85994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
860c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private void exitSearchUi() {
8613487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee        // See related bug in enterSearchUI();
8623487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee        if (getFragmentManager().isDestroyed()) {
8633487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee            return;
8643487db78fd47ccecef4e6b86e5cacfc0a8e6ba83Yorke Lee        }
865b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        // Go all the way back to the favorites fragment, regardless of how many times we
866b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        // transitioned between search fragments
867b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
868b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
869b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        setNotInSearchUi();
87094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
87194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
87294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Returns an Intent to launch Call Settings screen */
87394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public static Intent getCallSettingsIntent() {
87494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        final Intent intent = new Intent(Intent.ACTION_MAIN);
87594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
87694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
87794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return intent;
87894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
87994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
88094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
881c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onBackPressed() {
882ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
883f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            hideDialpadFragment(true, false);
884b207f8a073e8b3c001986b31c951f2c951cc631bYorke Lee        } else if (getInSearchUi()) {
885c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchView.setText(null);
886f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            mDialpadFragment.clearDialpad();
887c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else if (isTaskRoot()) {
888c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // Instead of stopping, simply push this to the back of the stack.
889c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // This is only done when running at the top of the stack;
890c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // otherwise, we have been launched by someone else so need to
891c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            // allow the user to go back to the caller.
892c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            moveTaskToBack(false);
893c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        } else {
894c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            super.onBackPressed();
89594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
896c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
897c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
898c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
899c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onDialpadQueryChanged(String query) {
900c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
901c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
902c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
903ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (DEBUG) {
904ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
905ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
906ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
907ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // This callback can happen if the dialpad fragment is recreated because of
908ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // activity destruction. In that case, don't update the search view because
909ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // that would bring the user back to the search fragment regardless of the
910ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // previous state of the application. Instead, just return here and let the
911ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                // fragment manager correctly figure out whatever fragment was last displayed.
912ef2b7388569a94432278a617b528f628385fbb8cYorke Lee                return;
913ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            }
914c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            mSearchView.setText(normalizedQuery);
91594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
91694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
917c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
918c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
919c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onListFragmentScrollStateChange(int scrollState) {
920c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
921f6673d3e5cbd10febea04f02035591ab15c5c7c0Yorke Lee            hideDialpadFragment(true, false);
922c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee            hideInputMethod(getCurrentFocus());
923c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        }
924c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
925c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
926c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    @Override
927c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    public void onDialpadFragmentStarted() {
928c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        setupFakeActionBarItemsForDialpadFragment();
929c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
930c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee
931c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    private boolean phoneIsInUse() {
932c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        final TelephonyManager tm = (TelephonyManager) getSystemService(
933c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee                Context.TELEPHONY_SERVICE);
934c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee        return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
935c3766338b312f9c8a6bdb2facaf4b0a4bd199de4Yorke Lee    }
9368dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
9378dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    @Override
9388dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    public void onShowAllContacts() {
9398dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        final Intent intent = new Intent(this, AllContactsActivity.class);
9408dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        startActivity(intent);
9418dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    }
942e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
943e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    public static Intent getAddNumberToContactIntent(CharSequence text) {
944e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
945e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.putExtra(Intents.Insert.PHONE, text);
946e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.setType(Contacts.CONTENT_ITEM_TYPE);
947e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        return intent;
948e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    }
949e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
950e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    public static Intent getInsertContactWithNameIntent(CharSequence text) {
951e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
952e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        intent.putExtra(Intents.Insert.NAME, text);
953e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        return intent;
954e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    }
95594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng}
956