blob: 398c90e42d043181312f319530764f8ba0472465 [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 Lee7a06c442013-09-04 15:49:51 -0700504 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700505 hideDialpadFragment(false, true);
506 }
507
Yorke Leec3766332013-07-31 11:13:16 -0700508 public void hideSearchBar() {
509 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700510 }
511
Yorke Leec3766332013-07-31 11:13:16 -0700512 public void hideSearchBar(boolean shiftView) {
513 if (shiftView) {
514 mSearchViewContainer.animate().cancel();
515 mSearchViewContainer.setAlpha(1);
516 mSearchViewContainer.setTranslationY(0);
517 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
518 .setDuration(200).setListener(mHideListener);
519
Yorke Leeb207f8a2013-08-29 18:51:06 -0700520 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700521 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
522 new AnimatorListenerAdapter() {
523 @Override
524 public void onAnimationEnd(Animator animation) {
525 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700526 mFragmentsFrame.setTranslationY(0);
Yorke Leec3766332013-07-31 11:13:16 -0700527 }
528 });
529 } else {
530 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
531 }
532 }
533
534 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700535 mSearchViewContainer.animate().cancel();
536 mSearchViewContainer.setAlpha(0);
537 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
538 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
539 .setListener(new AnimatorListenerAdapter() {
540 @Override
541 public void onAnimationStart(Animator animation) {
542 mSearchViewContainer.setVisibility(View.VISIBLE);
543 }
544 });
545
Yorke Leeb207f8a2013-08-29 18:51:06 -0700546 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
547 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700548 .setListener(
549 new AnimatorListenerAdapter() {
550 @Override
551 public void onAnimationStart(Animator animation) {
552 mBottomPaddingView.setVisibility(View.GONE);
553 }
554 });
555 }
556
557
Yorke Leeb207f8a2013-08-29 18:51:06 -0700558 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700559 mMenuButton = findViewById(R.id.overflow_menu);
560 if (mMenuButton != null) {
561 mMenuButton.setOnClickListener(this);
562 }
563
564 mCallHistoryButton = findViewById(R.id.call_history_button);
565 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
566 mCallHistoryButton.setOnClickListener(this);
567
568 mDialpadButton = findViewById(R.id.dialpad_button);
569 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
570 mDialpadButton.setOnClickListener(this);
571 }
572
573 public void setupFakeActionBarItemsForDialpadFragment() {
574 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
575 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700576 }
577
578 private void fixIntent(Intent intent) {
579 // This should be cleaned up: the call key used to send an Intent
580 // that just said to go to the recent calls list. It now sends this
581 // abstract action, but this class hasn't been rewritten to deal with it.
582 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
583 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
584 intent.putExtra("call_key", true);
585 setIntent(intent);
586 }
587 }
588
Chiao Cheng94b10b52012-08-17 16:59:12 -0700589 /**
590 * Returns true if the intent is due to hitting the green send key (hardware call button:
591 * KEYCODE_CALL) while in a call.
592 *
593 * @param intent the intent that launched this activity
594 * @param recentCallsRequest true if the intent is requesting to view recent calls
595 * @return true if the intent is due to hitting the green send key while in a call
596 */
Yorke Leec3766332013-07-31 11:13:16 -0700597 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700598 // If there is a call in progress go to the call screen
599 if (recentCallsRequest) {
600 final boolean callKey = intent.getBooleanExtra("call_key", false);
601
602 try {
603 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
604 if (callKey && phone != null && phone.showCallScreen()) {
605 return true;
606 }
607 } catch (RemoteException e) {
608 Log.e(TAG, "Failed to handle send while in call", e);
609 }
610 }
611
612 return false;
613 }
614
615 /**
616 * Sets the current tab based on the intent's request type
617 *
618 * @param intent Intent that contains information about which tab should be selected
619 */
Yorke Leec3766332013-07-31 11:13:16 -0700620 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700621 // If we got here by hitting send and we're in call forward along to the in-call activity
622 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
623 getContentResolver()));
624 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
625 finish();
626 return;
627 }
628
Yorke Lee98702de2013-08-06 12:03:32 -0700629 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700630 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leeea695cb2013-08-09 11:02:26 -0700631 showDialpadFragment(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700632 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700633 }
634
635 @Override
636 public void onNewIntent(Intent newIntent) {
637 setIntent(newIntent);
638 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700639 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700640 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700641
Chiao Cheng94b10b52012-08-17 16:59:12 -0700642 invalidateOptionsMenu();
643 }
644
645 /** Returns true if the given intent contains a phone number to populate the dialer with */
646 private boolean isDialIntent(Intent intent) {
647 final String action = intent.getAction();
648 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
649 return true;
650 }
651 if (Intent.ACTION_VIEW.equals(action)) {
652 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700653 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700654 return true;
655 }
656 }
657 return false;
658 }
659
660 /**
661 * Returns an appropriate call origin for this Activity. May return null when no call origin
662 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
663 * for remembering the tab in which the user made a phone call, so the external app's DIAL
664 * request should not be counted.)
665 */
666 public String getCallOrigin() {
667 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
668 }
669
670 /**
671 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
672 * This text originally came from a FILTER_CONTACTS_ACTION intent received
673 * by this activity. The stored text will then be cleared after after this
674 * method returns.
675 *
676 * @return The stored filter text
677 */
678 public String getAndClearFilterText() {
679 String filterText = mFilterText;
680 mFilterText = null;
681 return filterText;
682 }
683
684 /**
685 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
686 * This is so child activities can check if they are supposed to display a filter.
687 *
688 * @param intent The intent received in {@link #onNewIntent(Intent)}
689 */
690 private void setupFilterText(Intent intent) {
691 // If the intent was relaunched from history, don't apply the filter text.
692 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
693 return;
694 }
695 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
696 if (filter != null && filter.length() > 0) {
697 mFilterText = filter;
698 }
699 }
700
Yorke Lee8898cd02013-08-08 10:24:27 -0700701 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
702 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700703 @Override
704 public void onContactSelected(Uri contactUri) {
705 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700706 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700707 }
708
709 @Override
710 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700711 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700712 startActivity(intent);
713 }
714 };
715
Yorke Leec3766332013-07-31 11:13:16 -0700716 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
717 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
718 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719
720 @Override
721 public void startSearch(String initialQuery, boolean selectInitialQuery,
722 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700723 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700724 if (mInSearchUi) {
725 if (mSearchView.hasFocus()) {
726 showInputMethod(mSearchView.findFocus());
727 } else {
728 mSearchView.requestFocus();
729 }
730 } else {
731 enterSearchUi();
732 }
733 } else {
734 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
735 }
Yorke Leec3766332013-07-31 11:13:16 -0700736 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700737
738 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700739 final InputMethodManager imm = (InputMethodManager) getSystemService(
740 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700741 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700742 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700743 }
744 }
745
746 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700747 final InputMethodManager imm = (InputMethodManager) getSystemService(
748 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700749 if (imm != null && view != null) {
750 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
751 }
752 }
753
754 /**
Yorke Leec3766332013-07-31 11:13:16 -0700755 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700756 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700757 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700758 if (getFragmentManager().isDestroyed()) {
759 // Weird race condition where fragment is doing work after the activity is destroyed
760 // due to talkback being on (b/10209937). Just return since we can't do any
761 // constructive here.
762 return;
763 }
764
Yorke Leeef2b7382013-08-09 17:39:25 -0700765 if (DEBUG) {
766 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
767 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700768
Yorke Leec3766332013-07-31 11:13:16 -0700769 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
770 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700771
Yorke Leeef2b7382013-08-09 17:39:25 -0700772 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700773 if (mInDialpadSearch) {
774 transaction.remove(mSmartDialSearchFragment);
775 } else if (mInRegularSearch) {
776 transaction.remove(mRegularSearchFragment);
777 } else {
778 transaction.remove(mPhoneFavoriteFragment);
779 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700780
Yorke Leeb207f8a2013-08-29 18:51:06 -0700781 final String tag;
782 if (smartDialSearch) {
783 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
784 } else {
785 tag = TAG_REGULAR_SEARCH_FRAGMENT;
786 }
787 mInDialpadSearch = smartDialSearch;
788 mInRegularSearch = !smartDialSearch;
789
Yorke Leeef2b7382013-08-09 17:39:25 -0700790 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
791 if (fragment == null) {
792 if (smartDialSearch) {
793 fragment = new SmartDialSearchFragment();
794 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700795 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700796 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700797 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700798 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700799 transaction.addToBackStack(null);
800 fragment.setQueryString(query, false);
801 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700802 }
803
804 /**
Yorke Leec3766332013-07-31 11:13:16 -0700805 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700806 */
Yorke Leec3766332013-07-31 11:13:16 -0700807 private void exitSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700808 // Go all the way back to the favorites fragment, regardless of how many times we
809 // transitioned between search fragments
810 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
811 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
812 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700813 }
814
815 /** Returns an Intent to launch Call Settings screen */
816 public static Intent getCallSettingsIntent() {
817 final Intent intent = new Intent(Intent.ACTION_MAIN);
818 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
819 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
820 return intent;
821 }
822
823 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700824 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700825 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700826 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700827 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700828 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700829 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700830 } else if (isTaskRoot()) {
831 // Instead of stopping, simply push this to the back of the stack.
832 // This is only done when running at the top of the stack;
833 // otherwise, we have been launched by someone else so need to
834 // allow the user to go back to the caller.
835 moveTaskToBack(false);
836 } else {
837 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700838 }
Yorke Leec3766332013-07-31 11:13:16 -0700839 }
840
841 @Override
842 public void onDialpadQueryChanged(String query) {
843 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
844 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
845 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700846 if (DEBUG) {
847 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
848 }
849 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
850 // This callback can happen if the dialpad fragment is recreated because of
851 // activity destruction. In that case, don't update the search view because
852 // that would bring the user back to the search fragment regardless of the
853 // previous state of the application. Instead, just return here and let the
854 // fragment manager correctly figure out whatever fragment was last displayed.
855 return;
856 }
Yorke Leec3766332013-07-31 11:13:16 -0700857 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700858 }
859 }
Yorke Leec3766332013-07-31 11:13:16 -0700860
861 @Override
862 public void onListFragmentScrollStateChange(int scrollState) {
863 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700864 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700865 hideInputMethod(getCurrentFocus());
866 }
867 }
868
869 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700870 public void onDialpadFragmentStarted() {
871 setupFakeActionBarItemsForDialpadFragment();
872 }
873
874 private boolean phoneIsInUse() {
875 final TelephonyManager tm = (TelephonyManager) getSystemService(
876 Context.TELEPHONY_SERVICE);
877 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
878 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700879
880 @Override
881 public void onShowAllContacts() {
882 final Intent intent = new Intent(this, AllContactsActivity.class);
883 startActivity(intent);
884 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700885}