blob: e1bf3ed6adf2b05158c3e43daf983501ba3cf495 [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;
23import android.app.Fragment;
24import android.app.FragmentManager;
Yorke Leeb207f8a2013-08-29 18:51:06 -070025import android.app.FragmentManager.BackStackEntry;
Chiao Cheng94b10b52012-08-17 16:59:12 -070026import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080027import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.content.Context;
29import android.content.Intent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070030import android.net.Uri;
31import android.os.Bundle;
32import android.os.RemoteException;
33import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.provider.CallLog.Calls;
35import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070036import android.provider.ContactsContract.Intents;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070038import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070039import android.telephony.TelephonyManager;
40import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070042import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.util.Log;
44import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.View.OnFocusChangeListener;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070049import android.widget.AbsListView.OnScrollListener;
50import android.widget.EditText;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080052import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070054import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070055import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070056import com.android.contacts.common.dialog.ClearFrequentsDialog;
57import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080058import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070059import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070060import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070061import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070062import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070063import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070064import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070065import com.android.dialer.list.AllContactsActivity;
Yorke Leec3766332013-07-31 11:13:16 -070066import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070067import com.android.dialer.list.PhoneFavoriteFragment;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070068import com.android.dialer.list.RegularSearchFragment;
69import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070071import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070072import com.android.internal.telephony.ITelephony;
73
Yorke Leec3766332013-07-31 11:13:16 -070074import java.util.ArrayList;
75
Chiao Cheng94b10b52012-08-17 16:59:12 -070076/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070077 * The dialer tab's title is 'phone', a more common name (see strings.xml).
78 */
Yorke Leec3766332013-07-31 11:13:16 -070079public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070080 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070081 OnListFragmentScrolledListener,
Yorke Lee8dd62002013-08-08 15:57:20 -070082 DialpadFragment.OnDialpadFragmentStartedListener,
83 PhoneFavoriteFragment.OnShowAllContactsListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070084 private static final String TAG = "DialtactsActivity";
85
86 public static final boolean DEBUG = false;
87
Yorke Leef74011e2013-08-02 11:30:30 -070088 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
89
Chiao Cheng94b10b52012-08-17 16:59:12 -070090 /** Used to open Call Setting */
91 private static final String PHONE_PACKAGE = "com.android.phone";
92 private static final String CALL_SETTINGS_CLASS_NAME =
93 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -070094 /** @see #getCallOrigin() */
95 private static final String CALL_ORIGIN_DIALTACTS =
96 "com.android.dialer.DialtactsActivity";
97
Yorke Leeb207f8a2013-08-29 18:51:06 -070098 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
99 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700100 private static final String KEY_SEARCH_QUERY = "search_query";
101 private static final String KEY_FIRST_LAUNCH = "first_launch";
102
Yorke Leec3766332013-07-31 11:13:16 -0700103 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
104 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
105 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
106 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700107
Chiao Cheng94b10b52012-08-17 16:59:12 -0700108 /**
109 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
110 */
111 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
112
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
114
Yorke Leec3766332013-07-31 11:13:16 -0700115 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700116
117 private String mFilterText;
118
Yorke Leec3766332013-07-31 11:13:16 -0700119 /**
120 * The main fragment displaying the user's favorites and frequent contacts
121 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700122 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700123
124 /**
Yorke Leec3766332013-07-31 11:13:16 -0700125 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700126 */
Yorke Leef74011e2013-08-02 11:30:30 -0700127 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700128
129 /**
130 * Fragment for searching phone numbers using the alphanumeric keyboard.
131 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700132 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700133
134 /**
135 * Fragment for searching phone numbers using the dialpad.
136 */
137 private SmartDialSearchFragment mSmartDialSearchFragment;
138
Yorke Leec3766332013-07-31 11:13:16 -0700139 private View mMenuButton;
140 private View mCallHistoryButton;
141 private View mDialpadButton;
142
143 // Padding view used to shift the fragments up when the dialpad is shown.
144 private View mBottomPaddingView;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700145 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700146
Yorke Leeb207f8a2013-08-29 18:51:06 -0700147 private boolean mInDialpadSearch;
148 private boolean mInRegularSearch;
149
Yorke Leeef2b7382013-08-09 17:39:25 -0700150 /**
151 * True when this activity has been launched for the first time.
152 */
Yorke Lee98702de2013-08-06 12:03:32 -0700153 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700154 private View mSearchViewContainer;
155 private View mSearchViewCloseButton;
156 private View mVoiceSearchButton;
157 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700158
Yorke Leeef2b7382013-08-09 17:39:25 -0700159 private String mSearchQuery;
160
Yorke Leefce269a2013-08-12 11:56:04 -0700161 private DialerDatabaseHelper mDialerDatabaseHelper;
162
Chiao Cheng94b10b52012-08-17 16:59:12 -0700163 /**
164 * Listener used when one of phone numbers in search UI is selected. This will initiate a
165 * phone call using the phone number.
166 */
167 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
168 new OnPhoneNumberPickerActionListener() {
169 @Override
170 public void onPickPhoneNumberAction(Uri dataUri) {
171 // Specify call-origin so that users will see the previous tab instead of
172 // CallLog screen (search UI will be automatically exited).
173 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700174 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Leef6673d32013-08-23 15:34:17 -0700175 hideDialpadAndSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700176 }
177
178 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700179 public void onCallNumberDirectly(String phoneNumber) {
180 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
181 startActivity(intent);
Yorke Leef6673d32013-08-23 15:34:17 -0700182 hideDialpadAndSearchUi();
Jay Shrauner31a760b2013-07-26 09:34:49 -0700183 }
184
185 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700186 public void onShortcutIntentCreated(Intent intent) {
187 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
188 }
189
190 @Override
191 public void onHomeInActionBarSelected() {
192 exitSearchUi();
193 }
194 };
195
196 /**
197 * Listener used to send search queries to the phone search fragment.
198 */
Yorke Leec3766332013-07-31 11:13:16 -0700199 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
200 @Override
201 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
202 }
203
204 @Override
205 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700206 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700207 if (newText.equals(mSearchQuery)) {
208 // If the query hasn't changed (perhaps due to activity being destroyed
209 // and restored, or user launching the same DIAL intent twice), then there is
210 // no need to do anything here.
211 return;
212 }
213 mSearchQuery = newText;
214 if (DEBUG) {
215 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
216 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700217 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700218
Yorke Leec3766332013-07-31 11:13:16 -0700219 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700220 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700221 exitSearchUi();
222 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700223 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700224 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700225 } else if (!TextUtils.isEmpty(newText)) {
226 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
227 (!dialpadSearch && mInRegularSearch);
228 if (!sameSearchMode) {
229 // call enterSearchUi only if we are switching search modes, or entering
230 // search ui for the first time
231 enterSearchUi(dialpadSearch, newText);
232 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700233
Yorke Leeb207f8a2013-08-29 18:51:06 -0700234 if (dialpadSearch && mSmartDialSearchFragment != null) {
235 mSmartDialSearchFragment.setQueryString(newText, false);
236 } else if (mRegularSearchFragment != null) {
237 mRegularSearchFragment.setQueryString(newText, false);
238 }
239 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700240 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700241 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700242 }
Yorke Leec3766332013-07-31 11:13:16 -0700243 }
244
245 @Override
246 public void afterTextChanged(Editable s) {
247 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700248 };
249
Yorke Leec3766332013-07-31 11:13:16 -0700250 private boolean isDialpadShowing() {
251 return mDialpadFragment != null && mDialpadFragment.isVisible();
252 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700253
254 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700255 protected void onCreate(Bundle savedInstanceState) {
256 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700257 mFirstLaunch = true;
258
Chiao Cheng94b10b52012-08-17 16:59:12 -0700259 final Intent intent = getIntent();
260 fixIntent(intent);
261
Yorke Lee8898cd02013-08-08 10:24:27 -0700262 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700263
Yorke Leec3766332013-07-31 11:13:16 -0700264 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700265
Yorke Leeef2b7382013-08-09 17:39:25 -0700266 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
267 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700268 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700269 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700270
Yorke Leec3766332013-07-31 11:13:16 -0700271 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700272 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
273 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700274 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700275 } else {
276 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700277 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
278 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700279 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700280 }
281
Yorke Leec3766332013-07-31 11:13:16 -0700282 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700283 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leec3766332013-07-31 11:13:16 -0700284 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700285
Chiao Cheng94b10b52012-08-17 16:59:12 -0700286 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700287 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700288 setupFilterText(intent);
289 }
Yorke Leefce269a2013-08-12 11:56:04 -0700290
Yorke Leeb207f8a2013-08-29 18:51:06 -0700291 setupFakeActionBarItems();
292
Yorke Lee0baa98b2013-08-22 10:55:16 -0700293 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700294 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700295 }
296
297 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700298 protected void onResume() {
299 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700300 if (mFirstLaunch) {
301 displayFragment(getIntent());
302 }
303 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700304 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700305 }
306
307 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700308 protected void onSaveInstanceState(Bundle outState) {
309 super.onSaveInstanceState(outState);
310 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700311 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
312 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700313 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
314 }
315
316 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700317 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700318 if (fragment instanceof DialpadFragment) {
319 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700320 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700321 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700322 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700323 } else if (fragment instanceof SmartDialSearchFragment) {
324 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700325 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
326 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700327 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700328 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700329 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
330 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700331 } else if (fragment instanceof PhoneFavoriteFragment) {
332 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700333 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700334 }
Yorke Leec3766332013-07-31 11:13:16 -0700335 }
336
337 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700338 public boolean onMenuItemClick(MenuItem item) {
339 switch (item.getItemId()) {
340 case R.id.menu_import_export:
341 // We hard-code the "contactsAreAvailable" argument because doing it properly would
342 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
343 // now in Dialtacts for (potential) performance reasons. Compare with how it is
344 // done in {@link PeopleActivity}.
345 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700346 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700347 return true;
348 case R.id.menu_clear_frequents:
349 ClearFrequentsDialog.show(getFragmentManager());
350 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700351 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700352 try {
353 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
354 } catch (ActivityNotFoundException e) {
355 Toast toast = Toast.makeText(this,
356 R.string.add_contact_not_available,
357 Toast.LENGTH_SHORT);
358 toast.show();
359 }
360 return true;
361 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700362 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700363 return true;
364 case R.id.menu_all_contacts:
365 onShowAllContacts();
366 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700367 }
368 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700369 }
370
Alon Albertb453e5b2013-09-10 15:54:23 -0700371 protected void handleMenuSettings() {
372 openTelephonySetting(this);
373 }
374
375 public static void openTelephonySetting(Activity activity) {
376 final Intent settingsIntent = getCallSettingsIntent();
377 activity.startActivity(settingsIntent);
378 }
379
Chiao Cheng94b10b52012-08-17 16:59:12 -0700380 @Override
381 public void onClick(View view) {
382 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700383 case R.id.overflow_menu: {
384 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
385 final Menu menu = popupMenu.getMenu();
Yorke Lee8898cd02013-08-08 10:24:27 -0700386 popupMenu.inflate(R.menu.dialtacts_options);
Yorke Leef420a532013-08-09 15:16:10 -0700387 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Lee0d391b42013-08-09 17:27:00 -0700388 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
Yorke Leec3766332013-07-31 11:13:16 -0700389 popupMenu.setOnMenuItemClickListener(this);
390 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700391 break;
392 }
Yorke Leec3766332013-07-31 11:13:16 -0700393 case R.id.dialpad_button:
394 showDialpadFragment(true);
395 break;
396 case R.id.call_history_on_dialpad_button:
397 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700398 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
399 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700400 final Intent intent = new Intent(this, CallLogActivity.class);
401 startActivity(intent);
402 break;
403 case R.id.search_close_button:
404 // Clear the search field
405 if (!TextUtils.isEmpty(mSearchView.getText())) {
406 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700407 }
408 break;
Yorke Leec3766332013-07-31 11:13:16 -0700409 case R.id.voice_search_button:
410 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
411 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
412 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700413 default: {
414 Log.wtf(TAG, "Unexpected onClick event from " + view);
415 break;
416 }
417 }
418 }
419
Yorke Leec3766332013-07-31 11:13:16 -0700420 @Override
421 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
422 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
423 if (resultCode == RESULT_OK) {
424 final ArrayList<String> matches = data.getStringArrayListExtra(
425 RecognizerIntent.EXTRA_RESULTS);
426 if (matches.size() > 0) {
427 final String match = matches.get(0);
428 mSearchView.setText(match);
429 } else {
430 Log.e(TAG, "Voice search - nothing heard");
431 }
432 } else {
433 Log.e(TAG, "Voice search failed");
434 }
435 }
436 super.onActivityResult(requestCode, resultCode, data);
437 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700438
Yorke Leec3766332013-07-31 11:13:16 -0700439 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700440 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700441 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700442 if (animate) {
443 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700444 } else {
445 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700446 }
447 ft.show(mDialpadFragment);
448 ft.commit();
449 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700450
Yorke Leef6673d32013-08-23 15:34:17 -0700451 private void hideDialpadFragment(boolean animate, boolean clearDialpad) {
452 if (mDialpadFragment == null) return;
453 if (clearDialpad) {
454 mDialpadFragment.clearDialpad();
455 }
456 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700457 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700458 final FragmentTransaction ft = getFragmentManager().beginTransaction();
459 if (animate) {
460 ft.setCustomAnimations(0, R.anim.slide_out);
461 }
462 ft.hide(mDialpadFragment);
463 ft.commit();
464 }
465
Chiao Cheng94b10b52012-08-17 16:59:12 -0700466 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700467 mSearchViewContainer = findViewById(R.id.search_view_container);
468 mSearchViewCloseButton = findViewById(R.id.search_close_button);
469 mSearchViewCloseButton.setOnClickListener(this);
470 mVoiceSearchButton = findViewById(R.id.voice_search_button);
471 mVoiceSearchButton.setOnClickListener(this);
472 mSearchView = (EditText) findViewById(R.id.search_view);
473 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
474 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
475 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700476 @Override
477 public void onFocusChange(View view, boolean hasFocus) {
478 if (hasFocus) {
479 showInputMethod(view.findFocus());
480 }
481 }
482 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700483 }
484
Yorke Leec3766332013-07-31 11:13:16 -0700485 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
486 @Override
487 public void onAnimationEnd(Animator animation) {
488 mSearchViewContainer.setVisibility(View.GONE);
489 }
490 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700491
Yorke Leeb207f8a2013-08-29 18:51:06 -0700492 private boolean getInSearchUi() {
493 return mInDialpadSearch || mInRegularSearch;
494 }
495
496 private void setNotInSearchUi() {
497 mInDialpadSearch = false;
498 mInRegularSearch = false;
499 }
500
Yorke Lee311969c2013-08-26 06:31:11 -0700501 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700502 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700503 hideDialpadFragment(false, true);
504 }
505
Yorke Leec3766332013-07-31 11:13:16 -0700506 public void hideSearchBar() {
507 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700508 }
509
Yorke Leec3766332013-07-31 11:13:16 -0700510 public void hideSearchBar(boolean shiftView) {
511 if (shiftView) {
512 mSearchViewContainer.animate().cancel();
513 mSearchViewContainer.setAlpha(1);
514 mSearchViewContainer.setTranslationY(0);
515 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
516 .setDuration(200).setListener(mHideListener);
517
Yorke Leeb207f8a2013-08-29 18:51:06 -0700518 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700519 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
520 new AnimatorListenerAdapter() {
521 @Override
522 public void onAnimationEnd(Animator animation) {
523 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700524 mFragmentsFrame.setTranslationY(0);
Yorke Leec3766332013-07-31 11:13:16 -0700525 }
526 });
527 } else {
528 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
529 }
530 }
531
532 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700533 mSearchViewContainer.animate().cancel();
534 mSearchViewContainer.setAlpha(0);
535 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
536 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
537 .setListener(new AnimatorListenerAdapter() {
538 @Override
539 public void onAnimationStart(Animator animation) {
540 mSearchViewContainer.setVisibility(View.VISIBLE);
541 }
542 });
543
Yorke Leeb207f8a2013-08-29 18:51:06 -0700544 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
545 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700546 .setListener(
547 new AnimatorListenerAdapter() {
548 @Override
549 public void onAnimationStart(Animator animation) {
550 mBottomPaddingView.setVisibility(View.GONE);
551 }
552 });
553 }
554
555
Yorke Leeb207f8a2013-08-29 18:51:06 -0700556 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700557 mMenuButton = findViewById(R.id.overflow_menu);
558 if (mMenuButton != null) {
559 mMenuButton.setOnClickListener(this);
560 }
561
562 mCallHistoryButton = findViewById(R.id.call_history_button);
563 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
564 mCallHistoryButton.setOnClickListener(this);
565
566 mDialpadButton = findViewById(R.id.dialpad_button);
567 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
568 mDialpadButton.setOnClickListener(this);
569 }
570
571 public void setupFakeActionBarItemsForDialpadFragment() {
572 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
573 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700574 }
575
576 private void fixIntent(Intent intent) {
577 // This should be cleaned up: the call key used to send an Intent
578 // that just said to go to the recent calls list. It now sends this
579 // abstract action, but this class hasn't been rewritten to deal with it.
580 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
581 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
582 intent.putExtra("call_key", true);
583 setIntent(intent);
584 }
585 }
586
Chiao Cheng94b10b52012-08-17 16:59:12 -0700587 /**
588 * Returns true if the intent is due to hitting the green send key (hardware call button:
589 * KEYCODE_CALL) while in a call.
590 *
591 * @param intent the intent that launched this activity
592 * @param recentCallsRequest true if the intent is requesting to view recent calls
593 * @return true if the intent is due to hitting the green send key while in a call
594 */
Yorke Leec3766332013-07-31 11:13:16 -0700595 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700596 // If there is a call in progress go to the call screen
597 if (recentCallsRequest) {
598 final boolean callKey = intent.getBooleanExtra("call_key", false);
599
600 try {
601 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
602 if (callKey && phone != null && phone.showCallScreen()) {
603 return true;
604 }
605 } catch (RemoteException e) {
606 Log.e(TAG, "Failed to handle send while in call", e);
607 }
608 }
609
610 return false;
611 }
612
613 /**
614 * Sets the current tab based on the intent's request type
615 *
616 * @param intent Intent that contains information about which tab should be selected
617 */
Yorke Leec3766332013-07-31 11:13:16 -0700618 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700619 // If we got here by hitting send and we're in call forward along to the in-call activity
620 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
621 getContentResolver()));
622 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
623 finish();
624 return;
625 }
626
Yorke Lee98702de2013-08-06 12:03:32 -0700627 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700628 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leeea695cb2013-08-09 11:02:26 -0700629 showDialpadFragment(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700630 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700631 }
632
633 @Override
634 public void onNewIntent(Intent newIntent) {
635 setIntent(newIntent);
636 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700637 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700638 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700639
Chiao Cheng94b10b52012-08-17 16:59:12 -0700640 invalidateOptionsMenu();
641 }
642
643 /** Returns true if the given intent contains a phone number to populate the dialer with */
644 private boolean isDialIntent(Intent intent) {
645 final String action = intent.getAction();
646 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
647 return true;
648 }
649 if (Intent.ACTION_VIEW.equals(action)) {
650 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700651 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700652 return true;
653 }
654 }
655 return false;
656 }
657
658 /**
659 * Returns an appropriate call origin for this Activity. May return null when no call origin
660 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
661 * for remembering the tab in which the user made a phone call, so the external app's DIAL
662 * request should not be counted.)
663 */
664 public String getCallOrigin() {
665 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
666 }
667
668 /**
669 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
670 * This text originally came from a FILTER_CONTACTS_ACTION intent received
671 * by this activity. The stored text will then be cleared after after this
672 * method returns.
673 *
674 * @return The stored filter text
675 */
676 public String getAndClearFilterText() {
677 String filterText = mFilterText;
678 mFilterText = null;
679 return filterText;
680 }
681
682 /**
683 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
684 * This is so child activities can check if they are supposed to display a filter.
685 *
686 * @param intent The intent received in {@link #onNewIntent(Intent)}
687 */
688 private void setupFilterText(Intent intent) {
689 // If the intent was relaunched from history, don't apply the filter text.
690 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
691 return;
692 }
693 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
694 if (filter != null && filter.length() > 0) {
695 mFilterText = filter;
696 }
697 }
698
Yorke Lee8898cd02013-08-08 10:24:27 -0700699 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
700 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700701 @Override
702 public void onContactSelected(Uri contactUri) {
703 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700704 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700705 }
706
707 @Override
708 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700709 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700710 startActivity(intent);
711 }
712 };
713
Yorke Leec3766332013-07-31 11:13:16 -0700714 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
715 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
716 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700717
718 @Override
719 public void startSearch(String initialQuery, boolean selectInitialQuery,
720 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700721 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700722 if (mInSearchUi) {
723 if (mSearchView.hasFocus()) {
724 showInputMethod(mSearchView.findFocus());
725 } else {
726 mSearchView.requestFocus();
727 }
728 } else {
729 enterSearchUi();
730 }
731 } else {
732 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
733 }
Yorke Leec3766332013-07-31 11:13:16 -0700734 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700735
736 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700737 final InputMethodManager imm = (InputMethodManager) getSystemService(
738 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700739 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700740 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700741 }
742 }
743
744 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700745 final InputMethodManager imm = (InputMethodManager) getSystemService(
746 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700747 if (imm != null && view != null) {
748 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
749 }
750 }
751
752 /**
Yorke Leec3766332013-07-31 11:13:16 -0700753 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700754 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700755 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700756 if (getFragmentManager().isDestroyed()) {
757 // Weird race condition where fragment is doing work after the activity is destroyed
758 // due to talkback being on (b/10209937). Just return since we can't do any
759 // constructive here.
760 return;
761 }
762
Yorke Leeef2b7382013-08-09 17:39:25 -0700763 if (DEBUG) {
764 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
765 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700766
Yorke Leec3766332013-07-31 11:13:16 -0700767 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
768 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700769
Yorke Leeef2b7382013-08-09 17:39:25 -0700770 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700771 if (mInDialpadSearch) {
772 transaction.remove(mSmartDialSearchFragment);
773 } else if (mInRegularSearch) {
774 transaction.remove(mRegularSearchFragment);
775 } else {
776 transaction.remove(mPhoneFavoriteFragment);
777 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700778
Yorke Leeb207f8a2013-08-29 18:51:06 -0700779 final String tag;
780 if (smartDialSearch) {
781 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
782 } else {
783 tag = TAG_REGULAR_SEARCH_FRAGMENT;
784 }
785 mInDialpadSearch = smartDialSearch;
786 mInRegularSearch = !smartDialSearch;
787
Yorke Leeef2b7382013-08-09 17:39:25 -0700788 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
789 if (fragment == null) {
790 if (smartDialSearch) {
791 fragment = new SmartDialSearchFragment();
792 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700793 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700794 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700795 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700796 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700797 transaction.addToBackStack(null);
798 fragment.setQueryString(query, false);
799 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700800 }
801
802 /**
Yorke Leec3766332013-07-31 11:13:16 -0700803 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700804 */
Yorke Leec3766332013-07-31 11:13:16 -0700805 private void exitSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700806 // Go all the way back to the favorites fragment, regardless of how many times we
807 // transitioned between search fragments
808 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
809 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
810 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700811 }
812
813 /** Returns an Intent to launch Call Settings screen */
814 public static Intent getCallSettingsIntent() {
815 final Intent intent = new Intent(Intent.ACTION_MAIN);
816 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
817 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
818 return intent;
819 }
820
821 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700822 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700823 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700824 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700825 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700826 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700827 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700828 } else if (isTaskRoot()) {
829 // Instead of stopping, simply push this to the back of the stack.
830 // This is only done when running at the top of the stack;
831 // otherwise, we have been launched by someone else so need to
832 // allow the user to go back to the caller.
833 moveTaskToBack(false);
834 } else {
835 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700836 }
Yorke Leec3766332013-07-31 11:13:16 -0700837 }
838
839 @Override
840 public void onDialpadQueryChanged(String query) {
841 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
842 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
843 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700844 if (DEBUG) {
845 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
846 }
847 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
848 // This callback can happen if the dialpad fragment is recreated because of
849 // activity destruction. In that case, don't update the search view because
850 // that would bring the user back to the search fragment regardless of the
851 // previous state of the application. Instead, just return here and let the
852 // fragment manager correctly figure out whatever fragment was last displayed.
853 return;
854 }
Yorke Leec3766332013-07-31 11:13:16 -0700855 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700856 }
857 }
Yorke Leec3766332013-07-31 11:13:16 -0700858
859 @Override
860 public void onListFragmentScrollStateChange(int scrollState) {
861 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700862 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700863 hideInputMethod(getCurrentFocus());
864 }
865 }
866
867 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700868 public void onDialpadFragmentStarted() {
869 setupFakeActionBarItemsForDialpadFragment();
870 }
871
872 private boolean phoneIsInUse() {
873 final TelephonyManager tm = (TelephonyManager) getSystemService(
874 Context.TELEPHONY_SERVICE);
875 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
876 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700877
878 @Override
879 public void onShowAllContacts() {
880 final Intent intent = new Intent(this, AllContactsActivity.class);
881 startActivity(intent);
882 }
Yorke Leee1424812013-09-04 18:24:48 -0700883
884 public static Intent getAddNumberToContactIntent(CharSequence text) {
885 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
886 intent.putExtra(Intents.Insert.PHONE, text);
887 intent.setType(Contacts.CONTENT_ITEM_TYPE);
888 return intent;
889 }
890
891 public static Intent getInsertContactWithNameIntent(CharSequence text) {
892 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
893 intent.putExtra(Intents.Insert.NAME, text);
894 return intent;
895 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700896}