blob: 7ba49979ec8cc4813bbaac178a20338db3da80bd [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
Yorke Leec3766332013-07-31 11:13:16 -07002 * Copyright (C) 2013 The Android Open Source Project
Chiao Cheng94b10b52012-08-17 16:59:12 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer;
18
Yorke Leec3766332013-07-31 11:13:16 -070019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.app.Activity;
Christine Chen9c1e0652013-05-23 15:40:19 -070023import android.app.backup.BackupManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070024import android.app.Fragment;
25import android.app.FragmentManager;
Yorke Leeb207f8a2013-08-29 18:51:06 -070026import android.app.FragmentManager.BackStackEntry;
Chiao Cheng94b10b52012-08-17 16:59:12 -070027import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080028import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070029import android.content.Context;
30import android.content.Intent;
31import android.content.SharedPreferences;
Yorke Leec3766332013-07-31 11:13:16 -070032import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070033import android.net.Uri;
34import android.os.Bundle;
35import android.os.RemoteException;
36import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070038import android.provider.ContactsContract;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.provider.ContactsContract.Contacts;
40import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070041import android.provider.Settings;
42import android.speech.RecognizerIntent;
43import android.support.v4.app.NavUtils;
44import android.telephony.TelephonyManager;
45import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070047import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.util.Log;
49import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070052import android.view.View.OnFocusChangeListener;
53import android.view.ViewConfiguration;
Chiao Cheng94b10b52012-08-17 16:59:12 -070054import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070055import android.widget.AbsListView.OnScrollListener;
56import android.widget.EditText;
57import android.widget.ImageView;
Chiao Cheng94b10b52012-08-17 16:59:12 -070058import android.widget.PopupMenu;
59import android.widget.SearchView;
60import android.widget.SearchView.OnCloseListener;
61import android.widget.SearchView.OnQueryTextListener;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080062import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070063
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070064import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070065import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070066import com.android.contacts.common.dialog.ClearFrequentsDialog;
67import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070068import com.android.contacts.common.list.ContactListItemView;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080069import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
70import com.android.contacts.common.list.PhoneNumberPickerFragment;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070072import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070073import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070074import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070075import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070076import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070077import com.android.dialer.list.AllContactsActivity;
Yorke Lee8898cd02013-08-08 10:24:27 -070078import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Leec3766332013-07-31 11:13:16 -070079import com.android.dialer.list.OnListFragmentScrolledListener;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070080import com.android.dialer.list.RegularSearchFragment;
81import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070082import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070083import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070084import com.android.internal.telephony.ITelephony;
85
Yorke Leec3766332013-07-31 11:13:16 -070086import java.util.ArrayList;
87
Chiao Cheng94b10b52012-08-17 16:59:12 -070088/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070089 * The dialer tab's title is 'phone', a more common name (see strings.xml).
90 */
Yorke Leec3766332013-07-31 11:13:16 -070091public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070092 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070093 OnListFragmentScrolledListener,
Yorke Lee8dd62002013-08-08 15:57:20 -070094 DialpadFragment.OnDialpadFragmentStartedListener,
95 PhoneFavoriteFragment.OnShowAllContactsListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070096 private static final String TAG = "DialtactsActivity";
97
98 public static final boolean DEBUG = false;
99
Yorke Leef74011e2013-08-02 11:30:30 -0700100 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
101
Chiao Cheng94b10b52012-08-17 16:59:12 -0700102 /** Used to open Call Setting */
103 private static final String PHONE_PACKAGE = "com.android.phone";
104 private static final String CALL_SETTINGS_CLASS_NAME =
105 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700106 /** @see #getCallOrigin() */
107 private static final String CALL_ORIGIN_DIALTACTS =
108 "com.android.dialer.DialtactsActivity";
109
Yorke Leeb207f8a2013-08-29 18:51:06 -0700110 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
111 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700112 private static final String KEY_SEARCH_QUERY = "search_query";
113 private static final String KEY_FIRST_LAUNCH = "first_launch";
114
Yorke Leec3766332013-07-31 11:13:16 -0700115 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
116 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
117 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
118 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700119
Chiao Cheng94b10b52012-08-17 16:59:12 -0700120 /**
121 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
122 */
123 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
124
Chiao Cheng94b10b52012-08-17 16:59:12 -0700125 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
126
Yorke Leec3766332013-07-31 11:13:16 -0700127 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700128
129 private String mFilterText;
130
Yorke Leec3766332013-07-31 11:13:16 -0700131 /**
132 * The main fragment displaying the user's favorites and frequent contacts
133 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700134 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700135
136 /**
Yorke Leec3766332013-07-31 11:13:16 -0700137 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700138 */
Yorke Leef74011e2013-08-02 11:30:30 -0700139 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700140
141 /**
142 * Fragment for searching phone numbers using the alphanumeric keyboard.
143 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700144 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700145
146 /**
147 * Fragment for searching phone numbers using the dialpad.
148 */
149 private SmartDialSearchFragment mSmartDialSearchFragment;
150
Yorke Leec3766332013-07-31 11:13:16 -0700151 private View mMenuButton;
152 private View mCallHistoryButton;
153 private View mDialpadButton;
154
155 // Padding view used to shift the fragments up when the dialpad is shown.
156 private View mBottomPaddingView;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700157 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700158
Yorke Leeb207f8a2013-08-29 18:51:06 -0700159 private boolean mInDialpadSearch;
160 private boolean mInRegularSearch;
161
Yorke Leeef2b7382013-08-09 17:39:25 -0700162 /**
163 * True when this activity has been launched for the first time.
164 */
Yorke Lee98702de2013-08-06 12:03:32 -0700165 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700166 private View mSearchViewContainer;
167 private View mSearchViewCloseButton;
168 private View mVoiceSearchButton;
169 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700170
Yorke Leeef2b7382013-08-09 17:39:25 -0700171 private String mSearchQuery;
172
Yorke Leefce269a2013-08-12 11:56:04 -0700173 private DialerDatabaseHelper mDialerDatabaseHelper;
174
Chiao Cheng94b10b52012-08-17 16:59:12 -0700175 /**
176 * Listener used when one of phone numbers in search UI is selected. This will initiate a
177 * phone call using the phone number.
178 */
179 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
180 new OnPhoneNumberPickerActionListener() {
181 @Override
182 public void onPickPhoneNumberAction(Uri dataUri) {
183 // Specify call-origin so that users will see the previous tab instead of
184 // CallLog screen (search UI will be automatically exited).
185 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700186 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Leef6673d32013-08-23 15:34:17 -0700187 hideDialpadAndSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700188 }
189
190 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700191 public void onCallNumberDirectly(String phoneNumber) {
192 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
193 startActivity(intent);
Yorke Leef6673d32013-08-23 15:34:17 -0700194 hideDialpadAndSearchUi();
Jay Shrauner31a760b2013-07-26 09:34:49 -0700195 }
196
197 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700198 public void onShortcutIntentCreated(Intent intent) {
199 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
200 }
201
202 @Override
203 public void onHomeInActionBarSelected() {
204 exitSearchUi();
205 }
206 };
207
208 /**
209 * Listener used to send search queries to the phone search fragment.
210 */
Yorke Leec3766332013-07-31 11:13:16 -0700211 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
212 @Override
213 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
214 }
215
216 @Override
217 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700218 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700219 if (newText.equals(mSearchQuery)) {
220 // If the query hasn't changed (perhaps due to activity being destroyed
221 // and restored, or user launching the same DIAL intent twice), then there is
222 // no need to do anything here.
223 return;
224 }
225 mSearchQuery = newText;
226 if (DEBUG) {
227 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
228 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700229 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700230
Yorke Leec3766332013-07-31 11:13:16 -0700231 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700232 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700233 exitSearchUi();
234 mSearchViewCloseButton.setVisibility(View.GONE);
235 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700236 } else if (!TextUtils.isEmpty(newText)) {
237 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
238 (!dialpadSearch && mInRegularSearch);
239 if (!sameSearchMode) {
240 // call enterSearchUi only if we are switching search modes, or entering
241 // search ui for the first time
242 enterSearchUi(dialpadSearch, newText);
243 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700244
Yorke Leeb207f8a2013-08-29 18:51:06 -0700245 if (dialpadSearch && mSmartDialSearchFragment != null) {
246 mSmartDialSearchFragment.setQueryString(newText, false);
247 } else if (mRegularSearchFragment != null) {
248 mRegularSearchFragment.setQueryString(newText, false);
249 }
250 mSearchViewCloseButton.setVisibility(View.VISIBLE);
251 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700252 }
Yorke Leec3766332013-07-31 11:13:16 -0700253 }
254
255 @Override
256 public void afterTextChanged(Editable s) {
257 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700258 };
259
Yorke Leec3766332013-07-31 11:13:16 -0700260 private boolean isDialpadShowing() {
261 return mDialpadFragment != null && mDialpadFragment.isVisible();
262 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700263
264 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700265 protected void onCreate(Bundle savedInstanceState) {
266 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700267 mFirstLaunch = true;
268
Chiao Cheng94b10b52012-08-17 16:59:12 -0700269 final Intent intent = getIntent();
270 fixIntent(intent);
271
Yorke Lee8898cd02013-08-08 10:24:27 -0700272 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700273
Yorke Leec3766332013-07-31 11:13:16 -0700274 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275
Yorke Leeef2b7382013-08-09 17:39:25 -0700276 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
277 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700278 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700279 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700280
Yorke Leec3766332013-07-31 11:13:16 -0700281 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700282 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
283 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700284 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700285 } else {
286 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700287 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
288 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700289 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700290 }
291
Yorke Leec3766332013-07-31 11:13:16 -0700292 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700293 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leec3766332013-07-31 11:13:16 -0700294 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700295
Chiao Cheng94b10b52012-08-17 16:59:12 -0700296 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700297 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700298 setupFilterText(intent);
299 }
Yorke Leefce269a2013-08-12 11:56:04 -0700300
Yorke Leeb207f8a2013-08-29 18:51:06 -0700301 setupFakeActionBarItems();
302
Yorke Lee0baa98b2013-08-22 10:55:16 -0700303 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700304 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700305 }
306
307 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700308 protected void onResume() {
309 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700310 if (mFirstLaunch) {
311 displayFragment(getIntent());
312 }
313 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700314 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700315 }
316
317 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700318 protected void onSaveInstanceState(Bundle outState) {
319 super.onSaveInstanceState(outState);
320 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700321 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
322 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700323 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
324 }
325
326 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700327 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700328 if (fragment instanceof DialpadFragment) {
329 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700330 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700331 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700332 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700333 } else if (fragment instanceof SmartDialSearchFragment) {
334 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700335 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
336 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700337 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700338 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700339 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
340 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700341 } else if (fragment instanceof PhoneFavoriteFragment) {
342 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700343 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700344 }
Yorke Leec3766332013-07-31 11:13:16 -0700345 }
346
347 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700348 public boolean onMenuItemClick(MenuItem item) {
349 switch (item.getItemId()) {
350 case R.id.menu_import_export:
351 // We hard-code the "contactsAreAvailable" argument because doing it properly would
352 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
353 // now in Dialtacts for (potential) performance reasons. Compare with how it is
354 // done in {@link PeopleActivity}.
355 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700356 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700357 return true;
358 case R.id.menu_clear_frequents:
359 ClearFrequentsDialog.show(getFragmentManager());
360 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700361 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700362 try {
363 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
364 } catch (ActivityNotFoundException e) {
365 Toast toast = Toast.makeText(this,
366 R.string.add_contact_not_available,
367 Toast.LENGTH_SHORT);
368 toast.show();
369 }
370 return true;
371 case R.id.menu_call_settings:
Yorke Leef74011e2013-08-02 11:30:30 -0700372 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
Yorke Leec3766332013-07-31 11:13:16 -0700373 startActivity(settingsIntent);
Yorke Lee5e931972013-08-30 11:37:53 -0700374 return true;
375 case R.id.menu_all_contacts:
376 onShowAllContacts();
377 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700378 }
379 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700380 }
381
382 @Override
383 public void onClick(View view) {
384 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700385 case R.id.overflow_menu: {
386 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
387 final Menu menu = popupMenu.getMenu();
Yorke Lee8898cd02013-08-08 10:24:27 -0700388 popupMenu.inflate(R.menu.dialtacts_options);
Yorke Leef420a532013-08-09 15:16:10 -0700389 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Lee0d391b42013-08-09 17:27:00 -0700390 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
Yorke Leec3766332013-07-31 11:13:16 -0700391 popupMenu.setOnMenuItemClickListener(this);
392 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700393 break;
394 }
Yorke Leec3766332013-07-31 11:13:16 -0700395 case R.id.dialpad_button:
396 showDialpadFragment(true);
397 break;
398 case R.id.call_history_on_dialpad_button:
399 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700400 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
401 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700402 final Intent intent = new Intent(this, CallLogActivity.class);
403 startActivity(intent);
404 break;
405 case R.id.search_close_button:
406 // Clear the search field
407 if (!TextUtils.isEmpty(mSearchView.getText())) {
408 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700409 }
410 break;
Yorke Leec3766332013-07-31 11:13:16 -0700411 case R.id.voice_search_button:
412 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
413 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
414 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700415 default: {
416 Log.wtf(TAG, "Unexpected onClick event from " + view);
417 break;
418 }
419 }
420 }
421
Yorke Leec3766332013-07-31 11:13:16 -0700422 @Override
423 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
424 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
425 if (resultCode == RESULT_OK) {
426 final ArrayList<String> matches = data.getStringArrayListExtra(
427 RecognizerIntent.EXTRA_RESULTS);
428 if (matches.size() > 0) {
429 final String match = matches.get(0);
430 mSearchView.setText(match);
431 } else {
432 Log.e(TAG, "Voice search - nothing heard");
433 }
434 } else {
435 Log.e(TAG, "Voice search failed");
436 }
437 }
438 super.onActivityResult(requestCode, resultCode, data);
439 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700440
Yorke Leec3766332013-07-31 11:13:16 -0700441 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700442 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700443 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700444 if (animate) {
445 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700446 } else {
447 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700448 }
449 ft.show(mDialpadFragment);
450 ft.commit();
451 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700452
Yorke Leef6673d32013-08-23 15:34:17 -0700453 private void hideDialpadFragment(boolean animate, boolean clearDialpad) {
454 if (mDialpadFragment == null) return;
455 if (clearDialpad) {
456 mDialpadFragment.clearDialpad();
457 }
458 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700459 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700460 final FragmentTransaction ft = getFragmentManager().beginTransaction();
461 if (animate) {
462 ft.setCustomAnimations(0, R.anim.slide_out);
463 }
464 ft.hide(mDialpadFragment);
465 ft.commit();
466 }
467
Chiao Cheng94b10b52012-08-17 16:59:12 -0700468 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700469 mSearchViewContainer = findViewById(R.id.search_view_container);
470 mSearchViewCloseButton = findViewById(R.id.search_close_button);
471 mSearchViewCloseButton.setOnClickListener(this);
472 mVoiceSearchButton = findViewById(R.id.voice_search_button);
473 mVoiceSearchButton.setOnClickListener(this);
474 mSearchView = (EditText) findViewById(R.id.search_view);
475 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
476 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
477 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700478 @Override
479 public void onFocusChange(View view, boolean hasFocus) {
480 if (hasFocus) {
481 showInputMethod(view.findFocus());
482 }
483 }
484 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700485 }
486
Yorke Leec3766332013-07-31 11:13:16 -0700487 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
488 @Override
489 public void onAnimationEnd(Animator animation) {
490 mSearchViewContainer.setVisibility(View.GONE);
491 }
492 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700493
Yorke Leeb207f8a2013-08-29 18:51:06 -0700494 private boolean getInSearchUi() {
495 return mInDialpadSearch || mInRegularSearch;
496 }
497
498 private void setNotInSearchUi() {
499 mInDialpadSearch = false;
500 mInRegularSearch = false;
501 }
502
Yorke Lee311969c2013-08-26 06:31:11 -0700503 private void hideDialpadAndSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700504 if (getInSearchUi()) {
Yorke Lee311969c2013-08-26 06:31:11 -0700505 exitSearchUi();
506 }
507 hideDialpadFragment(false, true);
508 }
509
Yorke Leec3766332013-07-31 11:13:16 -0700510 public void hideSearchBar() {
511 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700512 }
513
Yorke Leec3766332013-07-31 11:13:16 -0700514 public void hideSearchBar(boolean shiftView) {
515 if (shiftView) {
516 mSearchViewContainer.animate().cancel();
517 mSearchViewContainer.setAlpha(1);
518 mSearchViewContainer.setTranslationY(0);
519 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
520 .setDuration(200).setListener(mHideListener);
521
Yorke Leeb207f8a2013-08-29 18:51:06 -0700522 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700523 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
524 new AnimatorListenerAdapter() {
525 @Override
526 public void onAnimationEnd(Animator animation) {
527 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700528 mFragmentsFrame.setTranslationY(0);
Yorke Leec3766332013-07-31 11:13:16 -0700529 }
530 });
531 } else {
532 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
533 }
534 }
535
536 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700537 mSearchViewContainer.animate().cancel();
538 mSearchViewContainer.setAlpha(0);
539 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
540 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
541 .setListener(new AnimatorListenerAdapter() {
542 @Override
543 public void onAnimationStart(Animator animation) {
544 mSearchViewContainer.setVisibility(View.VISIBLE);
545 }
546 });
547
Yorke Leeb207f8a2013-08-29 18:51:06 -0700548 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
549 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700550 .setListener(
551 new AnimatorListenerAdapter() {
552 @Override
553 public void onAnimationStart(Animator animation) {
554 mBottomPaddingView.setVisibility(View.GONE);
555 }
556 });
557 }
558
559
Yorke Leeb207f8a2013-08-29 18:51:06 -0700560 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700561 mMenuButton = findViewById(R.id.overflow_menu);
562 if (mMenuButton != null) {
563 mMenuButton.setOnClickListener(this);
564 }
565
566 mCallHistoryButton = findViewById(R.id.call_history_button);
567 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
568 mCallHistoryButton.setOnClickListener(this);
569
570 mDialpadButton = findViewById(R.id.dialpad_button);
571 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
572 mDialpadButton.setOnClickListener(this);
573 }
574
575 public void setupFakeActionBarItemsForDialpadFragment() {
576 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
577 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700578 }
579
580 private void fixIntent(Intent intent) {
581 // This should be cleaned up: the call key used to send an Intent
582 // that just said to go to the recent calls list. It now sends this
583 // abstract action, but this class hasn't been rewritten to deal with it.
584 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
585 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
586 intent.putExtra("call_key", true);
587 setIntent(intent);
588 }
589 }
590
Chiao Cheng94b10b52012-08-17 16:59:12 -0700591 /**
592 * Returns true if the intent is due to hitting the green send key (hardware call button:
593 * KEYCODE_CALL) while in a call.
594 *
595 * @param intent the intent that launched this activity
596 * @param recentCallsRequest true if the intent is requesting to view recent calls
597 * @return true if the intent is due to hitting the green send key while in a call
598 */
Yorke Leec3766332013-07-31 11:13:16 -0700599 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700600 // If there is a call in progress go to the call screen
601 if (recentCallsRequest) {
602 final boolean callKey = intent.getBooleanExtra("call_key", false);
603
604 try {
605 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
606 if (callKey && phone != null && phone.showCallScreen()) {
607 return true;
608 }
609 } catch (RemoteException e) {
610 Log.e(TAG, "Failed to handle send while in call", e);
611 }
612 }
613
614 return false;
615 }
616
617 /**
618 * Sets the current tab based on the intent's request type
619 *
620 * @param intent Intent that contains information about which tab should be selected
621 */
Yorke Leec3766332013-07-31 11:13:16 -0700622 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700623 // If we got here by hitting send and we're in call forward along to the in-call activity
624 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
625 getContentResolver()));
626 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
627 finish();
628 return;
629 }
630
Yorke Lee98702de2013-08-06 12:03:32 -0700631 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700632 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leeea695cb2013-08-09 11:02:26 -0700633 showDialpadFragment(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700634 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700635 }
636
637 @Override
638 public void onNewIntent(Intent newIntent) {
639 setIntent(newIntent);
640 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700641 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700642 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700643
Chiao Cheng94b10b52012-08-17 16:59:12 -0700644 invalidateOptionsMenu();
645 }
646
647 /** Returns true if the given intent contains a phone number to populate the dialer with */
648 private boolean isDialIntent(Intent intent) {
649 final String action = intent.getAction();
650 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
651 return true;
652 }
653 if (Intent.ACTION_VIEW.equals(action)) {
654 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700655 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700656 return true;
657 }
658 }
659 return false;
660 }
661
662 /**
663 * Returns an appropriate call origin for this Activity. May return null when no call origin
664 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
665 * for remembering the tab in which the user made a phone call, so the external app's DIAL
666 * request should not be counted.)
667 */
668 public String getCallOrigin() {
669 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
670 }
671
672 /**
673 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
674 * This text originally came from a FILTER_CONTACTS_ACTION intent received
675 * by this activity. The stored text will then be cleared after after this
676 * method returns.
677 *
678 * @return The stored filter text
679 */
680 public String getAndClearFilterText() {
681 String filterText = mFilterText;
682 mFilterText = null;
683 return filterText;
684 }
685
686 /**
687 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
688 * This is so child activities can check if they are supposed to display a filter.
689 *
690 * @param intent The intent received in {@link #onNewIntent(Intent)}
691 */
692 private void setupFilterText(Intent intent) {
693 // If the intent was relaunched from history, don't apply the filter text.
694 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
695 return;
696 }
697 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
698 if (filter != null && filter.length() > 0) {
699 mFilterText = filter;
700 }
701 }
702
Yorke Lee8898cd02013-08-08 10:24:27 -0700703 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
704 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700705 @Override
706 public void onContactSelected(Uri contactUri) {
707 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700708 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700709 }
710
711 @Override
712 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700713 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700714 startActivity(intent);
715 }
716 };
717
Yorke Leec3766332013-07-31 11:13:16 -0700718 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
719 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
720 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700721
722 @Override
723 public void startSearch(String initialQuery, boolean selectInitialQuery,
724 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700725 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700726 if (mInSearchUi) {
727 if (mSearchView.hasFocus()) {
728 showInputMethod(mSearchView.findFocus());
729 } else {
730 mSearchView.requestFocus();
731 }
732 } else {
733 enterSearchUi();
734 }
735 } else {
736 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
737 }
Yorke Leec3766332013-07-31 11:13:16 -0700738 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700739
740 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700741 final InputMethodManager imm = (InputMethodManager) getSystemService(
742 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700743 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700744 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700745 }
746 }
747
748 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700749 final InputMethodManager imm = (InputMethodManager) getSystemService(
750 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700751 if (imm != null && view != null) {
752 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
753 }
754 }
755
756 /**
Yorke Leec3766332013-07-31 11:13:16 -0700757 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700758 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700759 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700760 if (getFragmentManager().isDestroyed()) {
761 // Weird race condition where fragment is doing work after the activity is destroyed
762 // due to talkback being on (b/10209937). Just return since we can't do any
763 // constructive here.
764 return;
765 }
766
Yorke Leeef2b7382013-08-09 17:39:25 -0700767 if (DEBUG) {
768 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
769 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700770
Yorke Leec3766332013-07-31 11:13:16 -0700771 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
772 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700773
Yorke Leeef2b7382013-08-09 17:39:25 -0700774 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700775 if (mInDialpadSearch) {
776 transaction.remove(mSmartDialSearchFragment);
777 } else if (mInRegularSearch) {
778 transaction.remove(mRegularSearchFragment);
779 } else {
780 transaction.remove(mPhoneFavoriteFragment);
781 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700782
Yorke Leeb207f8a2013-08-29 18:51:06 -0700783 final String tag;
784 if (smartDialSearch) {
785 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
786 } else {
787 tag = TAG_REGULAR_SEARCH_FRAGMENT;
788 }
789 mInDialpadSearch = smartDialSearch;
790 mInRegularSearch = !smartDialSearch;
791
Yorke Leeef2b7382013-08-09 17:39:25 -0700792 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
793 if (fragment == null) {
794 if (smartDialSearch) {
795 fragment = new SmartDialSearchFragment();
796 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700797 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700798 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700799 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700800 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700801 transaction.addToBackStack(null);
802 fragment.setQueryString(query, false);
803 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700804 }
805
806 /**
Yorke Leec3766332013-07-31 11:13:16 -0700807 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700808 */
Yorke Leec3766332013-07-31 11:13:16 -0700809 private void exitSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700810 // Go all the way back to the favorites fragment, regardless of how many times we
811 // transitioned between search fragments
812 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
813 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
814 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700815 }
816
817 /** Returns an Intent to launch Call Settings screen */
818 public static Intent getCallSettingsIntent() {
819 final Intent intent = new Intent(Intent.ACTION_MAIN);
820 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
821 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
822 return intent;
823 }
824
825 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700826 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700827 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700828 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700829 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700830 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700831 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700832 } else if (isTaskRoot()) {
833 // Instead of stopping, simply push this to the back of the stack.
834 // This is only done when running at the top of the stack;
835 // otherwise, we have been launched by someone else so need to
836 // allow the user to go back to the caller.
837 moveTaskToBack(false);
838 } else {
839 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700840 }
Yorke Leec3766332013-07-31 11:13:16 -0700841 }
842
843 @Override
844 public void onDialpadQueryChanged(String query) {
845 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
846 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
847 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700848 if (DEBUG) {
849 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
850 }
851 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
852 // This callback can happen if the dialpad fragment is recreated because of
853 // activity destruction. In that case, don't update the search view because
854 // that would bring the user back to the search fragment regardless of the
855 // previous state of the application. Instead, just return here and let the
856 // fragment manager correctly figure out whatever fragment was last displayed.
857 return;
858 }
Yorke Leec3766332013-07-31 11:13:16 -0700859 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700860 }
861 }
Yorke Leec3766332013-07-31 11:13:16 -0700862
863 @Override
864 public void onListFragmentScrollStateChange(int scrollState) {
865 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700866 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700867 hideInputMethod(getCurrentFocus());
868 }
869 }
870
871 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700872 public void onDialpadFragmentStarted() {
873 setupFakeActionBarItemsForDialpadFragment();
874 }
875
876 private boolean phoneIsInUse() {
877 final TelephonyManager tm = (TelephonyManager) getSystemService(
878 Context.TELEPHONY_SERVICE);
879 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
880 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700881
882 @Override
883 public void onShowAllContacts() {
884 final Intent intent = new Intent(this, AllContactsActivity.class);
885 startActivity(intent);
886 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700887}