blob: 4680038064c32477af542d0a07a2dd878d4514eb [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 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700151 * True if the dialpad is only temporarily showing due to being in call
152 */
153 private boolean mInCallDialpadUp;
154
155 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700156 * True when this activity has been launched for the first time.
157 */
Yorke Lee98702de2013-08-06 12:03:32 -0700158 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700159 private View mSearchViewContainer;
160 private View mSearchViewCloseButton;
161 private View mVoiceSearchButton;
162 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700163
Yorke Leeef2b7382013-08-09 17:39:25 -0700164 private String mSearchQuery;
165
Yorke Leefce269a2013-08-12 11:56:04 -0700166 private DialerDatabaseHelper mDialerDatabaseHelper;
167
Chiao Cheng94b10b52012-08-17 16:59:12 -0700168 /**
169 * Listener used when one of phone numbers in search UI is selected. This will initiate a
170 * phone call using the phone number.
171 */
172 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
173 new OnPhoneNumberPickerActionListener() {
174 @Override
175 public void onPickPhoneNumberAction(Uri dataUri) {
176 // Specify call-origin so that users will see the previous tab instead of
177 // CallLog screen (search UI will be automatically exited).
178 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700179 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Leef6673d32013-08-23 15:34:17 -0700180 hideDialpadAndSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700181 }
182
183 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700184 public void onCallNumberDirectly(String phoneNumber) {
185 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
186 startActivity(intent);
Yorke Leef6673d32013-08-23 15:34:17 -0700187 hideDialpadAndSearchUi();
Jay Shrauner31a760b2013-07-26 09:34:49 -0700188 }
189
190 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700191 public void onShortcutIntentCreated(Intent intent) {
192 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
193 }
194
195 @Override
196 public void onHomeInActionBarSelected() {
197 exitSearchUi();
198 }
199 };
200
201 /**
202 * Listener used to send search queries to the phone search fragment.
203 */
Yorke Leec3766332013-07-31 11:13:16 -0700204 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
205 @Override
206 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
207 }
208
209 @Override
210 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700211 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700212 if (newText.equals(mSearchQuery)) {
213 // If the query hasn't changed (perhaps due to activity being destroyed
214 // and restored, or user launching the same DIAL intent twice), then there is
215 // no need to do anything here.
216 return;
217 }
218 mSearchQuery = newText;
219 if (DEBUG) {
220 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
221 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700222 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700223
Yorke Leec3766332013-07-31 11:13:16 -0700224 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700225 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700226 exitSearchUi();
227 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700228 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700229 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700230 } else if (!TextUtils.isEmpty(newText)) {
231 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
232 (!dialpadSearch && mInRegularSearch);
233 if (!sameSearchMode) {
234 // call enterSearchUi only if we are switching search modes, or entering
235 // search ui for the first time
236 enterSearchUi(dialpadSearch, newText);
237 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700238
Yorke Leeb207f8a2013-08-29 18:51:06 -0700239 if (dialpadSearch && mSmartDialSearchFragment != null) {
240 mSmartDialSearchFragment.setQueryString(newText, false);
241 } else if (mRegularSearchFragment != null) {
242 mRegularSearchFragment.setQueryString(newText, false);
243 }
244 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700245 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700246 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700247 }
Yorke Leec3766332013-07-31 11:13:16 -0700248 }
249
250 @Override
251 public void afterTextChanged(Editable s) {
252 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700253 };
254
Yorke Leec3766332013-07-31 11:13:16 -0700255 private boolean isDialpadShowing() {
256 return mDialpadFragment != null && mDialpadFragment.isVisible();
257 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700258
259 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700260 protected void onCreate(Bundle savedInstanceState) {
261 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700262 mFirstLaunch = true;
263
Chiao Cheng94b10b52012-08-17 16:59:12 -0700264 final Intent intent = getIntent();
265 fixIntent(intent);
266
Yorke Lee8898cd02013-08-08 10:24:27 -0700267 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700268
Yorke Leec3766332013-07-31 11:13:16 -0700269 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700270
Yorke Leeef2b7382013-08-09 17:39:25 -0700271 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
272 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700273 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700274 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275
Yorke Leec3766332013-07-31 11:13:16 -0700276 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700277 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
278 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700279 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700280 } else {
281 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700282 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
283 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700284 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700285 }
286
Yorke Leec3766332013-07-31 11:13:16 -0700287 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700288 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leec3766332013-07-31 11:13:16 -0700289 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700290
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700292 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700293 setupFilterText(intent);
294 }
Yorke Leefce269a2013-08-12 11:56:04 -0700295
Yorke Leeb207f8a2013-08-29 18:51:06 -0700296 setupFakeActionBarItems();
297
Yorke Lee0baa98b2013-08-22 10:55:16 -0700298 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700299 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700300 }
301
302 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700303 protected void onResume() {
304 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700305 if (mFirstLaunch) {
306 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700307 } else if (!phoneIsInUse() && mInCallDialpadUp) {
308 hideDialpadFragment(false, true);
309 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700310 }
Yorke Lee35127cd2013-09-10 14:09:29 -0700311
Yorke Lee98702de2013-08-06 12:03:32 -0700312 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700313 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700314 }
315
316 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700317 protected void onSaveInstanceState(Bundle outState) {
318 super.onSaveInstanceState(outState);
319 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700320 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
321 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700322 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
323 }
324
325 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700326 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700327 if (fragment instanceof DialpadFragment) {
328 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700329 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700330 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700331 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700332 } else if (fragment instanceof SmartDialSearchFragment) {
333 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700334 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
335 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700336 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700337 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700338 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
339 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700340 } else if (fragment instanceof PhoneFavoriteFragment) {
341 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700342 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700343 }
Yorke Leec3766332013-07-31 11:13:16 -0700344 }
345
346 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700347 public boolean onMenuItemClick(MenuItem item) {
348 switch (item.getItemId()) {
349 case R.id.menu_import_export:
350 // We hard-code the "contactsAreAvailable" argument because doing it properly would
351 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
352 // now in Dialtacts for (potential) performance reasons. Compare with how it is
353 // done in {@link PeopleActivity}.
354 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700355 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700356 return true;
357 case R.id.menu_clear_frequents:
358 ClearFrequentsDialog.show(getFragmentManager());
359 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700360 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700361 try {
362 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
363 } catch (ActivityNotFoundException e) {
364 Toast toast = Toast.makeText(this,
365 R.string.add_contact_not_available,
366 Toast.LENGTH_SHORT);
367 toast.show();
368 }
369 return true;
370 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700371 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700372 return true;
373 case R.id.menu_all_contacts:
374 onShowAllContacts();
375 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700376 }
377 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700378 }
379
Alon Albertb453e5b2013-09-10 15:54:23 -0700380 protected void handleMenuSettings() {
381 openTelephonySetting(this);
382 }
383
384 public static void openTelephonySetting(Activity activity) {
385 final Intent settingsIntent = getCallSettingsIntent();
386 activity.startActivity(settingsIntent);
387 }
388
Chiao Cheng94b10b52012-08-17 16:59:12 -0700389 @Override
390 public void onClick(View view) {
391 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700392 case R.id.overflow_menu: {
393 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
394 final Menu menu = popupMenu.getMenu();
Yorke Lee8898cd02013-08-08 10:24:27 -0700395 popupMenu.inflate(R.menu.dialtacts_options);
Yorke Leef420a532013-08-09 15:16:10 -0700396 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Lee0d391b42013-08-09 17:27:00 -0700397 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
Yorke Leec3766332013-07-31 11:13:16 -0700398 popupMenu.setOnMenuItemClickListener(this);
399 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700400 break;
401 }
Yorke Leec3766332013-07-31 11:13:16 -0700402 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700403 // Reset the boolean flag that tracks whether the dialpad was up because
404 // we were in call. Regardless of whether it was true before, we want to
405 // show the dialpad because the user has explicitly clicked the dialpad
406 // button.
407 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700408 showDialpadFragment(true);
409 break;
410 case R.id.call_history_on_dialpad_button:
411 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700412 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
413 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700414 final Intent intent = new Intent(this, CallLogActivity.class);
415 startActivity(intent);
416 break;
417 case R.id.search_close_button:
418 // Clear the search field
419 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700420 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700421 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700422 }
423 break;
Yorke Leec3766332013-07-31 11:13:16 -0700424 case R.id.voice_search_button:
425 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
426 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
427 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700428 default: {
429 Log.wtf(TAG, "Unexpected onClick event from " + view);
430 break;
431 }
432 }
433 }
434
Yorke Leec3766332013-07-31 11:13:16 -0700435 @Override
436 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
437 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
438 if (resultCode == RESULT_OK) {
439 final ArrayList<String> matches = data.getStringArrayListExtra(
440 RecognizerIntent.EXTRA_RESULTS);
441 if (matches.size() > 0) {
442 final String match = matches.get(0);
443 mSearchView.setText(match);
444 } else {
445 Log.e(TAG, "Voice search - nothing heard");
446 }
447 } else {
448 Log.e(TAG, "Voice search failed");
449 }
450 }
451 super.onActivityResult(requestCode, resultCode, data);
452 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700453
Yorke Leec3766332013-07-31 11:13:16 -0700454 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700455 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700456 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700457 if (animate) {
458 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700459 } else {
460 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700461 }
462 ft.show(mDialpadFragment);
463 ft.commit();
464 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700465
Yorke Leef6673d32013-08-23 15:34:17 -0700466 private void hideDialpadFragment(boolean animate, boolean clearDialpad) {
467 if (mDialpadFragment == null) return;
468 if (clearDialpad) {
469 mDialpadFragment.clearDialpad();
470 }
471 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700472 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700473 final FragmentTransaction ft = getFragmentManager().beginTransaction();
474 if (animate) {
475 ft.setCustomAnimations(0, R.anim.slide_out);
476 }
477 ft.hide(mDialpadFragment);
478 ft.commit();
479 }
480
Chiao Cheng94b10b52012-08-17 16:59:12 -0700481 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700482 mSearchViewContainer = findViewById(R.id.search_view_container);
483 mSearchViewCloseButton = findViewById(R.id.search_close_button);
484 mSearchViewCloseButton.setOnClickListener(this);
485 mVoiceSearchButton = findViewById(R.id.voice_search_button);
486 mVoiceSearchButton.setOnClickListener(this);
487 mSearchView = (EditText) findViewById(R.id.search_view);
488 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
489 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
490 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700491 @Override
492 public void onFocusChange(View view, boolean hasFocus) {
493 if (hasFocus) {
494 showInputMethod(view.findFocus());
495 }
496 }
497 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700498 }
499
Yorke Leec3766332013-07-31 11:13:16 -0700500 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
501 @Override
502 public void onAnimationEnd(Animator animation) {
503 mSearchViewContainer.setVisibility(View.GONE);
504 }
505 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700506
Yorke Leeb207f8a2013-08-29 18:51:06 -0700507 private boolean getInSearchUi() {
508 return mInDialpadSearch || mInRegularSearch;
509 }
510
511 private void setNotInSearchUi() {
512 mInDialpadSearch = false;
513 mInRegularSearch = false;
514 }
515
Yorke Lee311969c2013-08-26 06:31:11 -0700516 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700517 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700518 hideDialpadFragment(false, true);
519 }
520
Yorke Leec3766332013-07-31 11:13:16 -0700521 public void hideSearchBar() {
522 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700523 }
524
Yorke Leec3766332013-07-31 11:13:16 -0700525 public void hideSearchBar(boolean shiftView) {
526 if (shiftView) {
527 mSearchViewContainer.animate().cancel();
528 mSearchViewContainer.setAlpha(1);
529 mSearchViewContainer.setTranslationY(0);
530 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
531 .setDuration(200).setListener(mHideListener);
532
Yorke Leeb207f8a2013-08-29 18:51:06 -0700533 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700534 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
535 new AnimatorListenerAdapter() {
536 @Override
537 public void onAnimationEnd(Animator animation) {
538 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700539 mFragmentsFrame.setTranslationY(0);
Yorke Leec3766332013-07-31 11:13:16 -0700540 }
541 });
542 } else {
543 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
544 }
545 }
546
547 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700548 mSearchViewContainer.animate().cancel();
549 mSearchViewContainer.setAlpha(0);
550 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
551 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
552 .setListener(new AnimatorListenerAdapter() {
553 @Override
554 public void onAnimationStart(Animator animation) {
555 mSearchViewContainer.setVisibility(View.VISIBLE);
556 }
557 });
558
Yorke Leeb207f8a2013-08-29 18:51:06 -0700559 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
560 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700561 .setListener(
562 new AnimatorListenerAdapter() {
563 @Override
564 public void onAnimationStart(Animator animation) {
565 mBottomPaddingView.setVisibility(View.GONE);
566 }
567 });
568 }
569
570
Yorke Leeb207f8a2013-08-29 18:51:06 -0700571 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700572 mMenuButton = findViewById(R.id.overflow_menu);
573 if (mMenuButton != null) {
574 mMenuButton.setOnClickListener(this);
575 }
576
577 mCallHistoryButton = findViewById(R.id.call_history_button);
578 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
579 mCallHistoryButton.setOnClickListener(this);
580
581 mDialpadButton = findViewById(R.id.dialpad_button);
582 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
583 mDialpadButton.setOnClickListener(this);
584 }
585
586 public void setupFakeActionBarItemsForDialpadFragment() {
587 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
588 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700589 }
590
591 private void fixIntent(Intent intent) {
592 // This should be cleaned up: the call key used to send an Intent
593 // that just said to go to the recent calls list. It now sends this
594 // abstract action, but this class hasn't been rewritten to deal with it.
595 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
596 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
597 intent.putExtra("call_key", true);
598 setIntent(intent);
599 }
600 }
601
Chiao Cheng94b10b52012-08-17 16:59:12 -0700602 /**
603 * Returns true if the intent is due to hitting the green send key (hardware call button:
604 * KEYCODE_CALL) while in a call.
605 *
606 * @param intent the intent that launched this activity
607 * @param recentCallsRequest true if the intent is requesting to view recent calls
608 * @return true if the intent is due to hitting the green send key while in a call
609 */
Yorke Leec3766332013-07-31 11:13:16 -0700610 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700611 // If there is a call in progress go to the call screen
612 if (recentCallsRequest) {
613 final boolean callKey = intent.getBooleanExtra("call_key", false);
614
615 try {
616 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
617 if (callKey && phone != null && phone.showCallScreen()) {
618 return true;
619 }
620 } catch (RemoteException e) {
621 Log.e(TAG, "Failed to handle send while in call", e);
622 }
623 }
624
625 return false;
626 }
627
628 /**
629 * Sets the current tab based on the intent's request type
630 *
631 * @param intent Intent that contains information about which tab should be selected
632 */
Yorke Leec3766332013-07-31 11:13:16 -0700633 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700634 // If we got here by hitting send and we're in call forward along to the in-call activity
635 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
636 getContentResolver()));
637 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
638 finish();
639 return;
640 }
641
Yorke Lee98702de2013-08-06 12:03:32 -0700642 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700643 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Lee35127cd2013-09-10 14:09:29 -0700644 if (!mDialpadFragment.isVisible()) {
645 mInCallDialpadUp = true;
646 }
Yorke Leeea695cb2013-08-09 11:02:26 -0700647 showDialpadFragment(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700648 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700649 }
650
651 @Override
652 public void onNewIntent(Intent newIntent) {
653 setIntent(newIntent);
654 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700655 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700656 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700657
Chiao Cheng94b10b52012-08-17 16:59:12 -0700658 invalidateOptionsMenu();
659 }
660
661 /** Returns true if the given intent contains a phone number to populate the dialer with */
662 private boolean isDialIntent(Intent intent) {
663 final String action = intent.getAction();
664 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
665 return true;
666 }
667 if (Intent.ACTION_VIEW.equals(action)) {
668 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700669 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700670 return true;
671 }
672 }
673 return false;
674 }
675
676 /**
677 * Returns an appropriate call origin for this Activity. May return null when no call origin
678 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
679 * for remembering the tab in which the user made a phone call, so the external app's DIAL
680 * request should not be counted.)
681 */
682 public String getCallOrigin() {
683 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
684 }
685
686 /**
687 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
688 * This text originally came from a FILTER_CONTACTS_ACTION intent received
689 * by this activity. The stored text will then be cleared after after this
690 * method returns.
691 *
692 * @return The stored filter text
693 */
694 public String getAndClearFilterText() {
695 String filterText = mFilterText;
696 mFilterText = null;
697 return filterText;
698 }
699
700 /**
701 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
702 * This is so child activities can check if they are supposed to display a filter.
703 *
704 * @param intent The intent received in {@link #onNewIntent(Intent)}
705 */
706 private void setupFilterText(Intent intent) {
707 // If the intent was relaunched from history, don't apply the filter text.
708 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
709 return;
710 }
711 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
712 if (filter != null && filter.length() > 0) {
713 mFilterText = filter;
714 }
715 }
716
Yorke Lee8898cd02013-08-08 10:24:27 -0700717 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
718 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719 @Override
720 public void onContactSelected(Uri contactUri) {
721 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700722 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700723 }
724
725 @Override
726 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700727 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728 startActivity(intent);
729 }
730 };
731
Yorke Leec3766332013-07-31 11:13:16 -0700732 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
733 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
734 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700735
736 @Override
737 public void startSearch(String initialQuery, boolean selectInitialQuery,
738 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700739 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700740 if (mInSearchUi) {
741 if (mSearchView.hasFocus()) {
742 showInputMethod(mSearchView.findFocus());
743 } else {
744 mSearchView.requestFocus();
745 }
746 } else {
747 enterSearchUi();
748 }
749 } else {
750 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
751 }
Yorke Leec3766332013-07-31 11:13:16 -0700752 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700753
754 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700755 final InputMethodManager imm = (InputMethodManager) getSystemService(
756 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700757 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700758 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700759 }
760 }
761
762 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700763 final InputMethodManager imm = (InputMethodManager) getSystemService(
764 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700765 if (imm != null && view != null) {
766 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
767 }
768 }
769
770 /**
Yorke Leec3766332013-07-31 11:13:16 -0700771 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700772 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700773 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700774 if (getFragmentManager().isDestroyed()) {
775 // Weird race condition where fragment is doing work after the activity is destroyed
776 // due to talkback being on (b/10209937). Just return since we can't do any
777 // constructive here.
778 return;
779 }
780
Yorke Leeef2b7382013-08-09 17:39:25 -0700781 if (DEBUG) {
782 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
783 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700784
Yorke Leec3766332013-07-31 11:13:16 -0700785 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
786 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700787
Yorke Leeef2b7382013-08-09 17:39:25 -0700788 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700789 if (mInDialpadSearch) {
790 transaction.remove(mSmartDialSearchFragment);
791 } else if (mInRegularSearch) {
792 transaction.remove(mRegularSearchFragment);
793 } else {
794 transaction.remove(mPhoneFavoriteFragment);
795 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700796
Yorke Leeb207f8a2013-08-29 18:51:06 -0700797 final String tag;
798 if (smartDialSearch) {
799 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
800 } else {
801 tag = TAG_REGULAR_SEARCH_FRAGMENT;
802 }
803 mInDialpadSearch = smartDialSearch;
804 mInRegularSearch = !smartDialSearch;
805
Yorke Leeef2b7382013-08-09 17:39:25 -0700806 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
807 if (fragment == null) {
808 if (smartDialSearch) {
809 fragment = new SmartDialSearchFragment();
810 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700811 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700812 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700813 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700814 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700815 transaction.addToBackStack(null);
816 fragment.setQueryString(query, false);
817 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700818 }
819
820 /**
Yorke Leec3766332013-07-31 11:13:16 -0700821 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700822 */
Yorke Leec3766332013-07-31 11:13:16 -0700823 private void exitSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700824 // Go all the way back to the favorites fragment, regardless of how many times we
825 // transitioned between search fragments
826 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
827 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
828 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700829 }
830
831 /** Returns an Intent to launch Call Settings screen */
832 public static Intent getCallSettingsIntent() {
833 final Intent intent = new Intent(Intent.ACTION_MAIN);
834 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
835 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
836 return intent;
837 }
838
839 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700840 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700841 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700842 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700843 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700844 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700845 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700846 } else if (isTaskRoot()) {
847 // Instead of stopping, simply push this to the back of the stack.
848 // This is only done when running at the top of the stack;
849 // otherwise, we have been launched by someone else so need to
850 // allow the user to go back to the caller.
851 moveTaskToBack(false);
852 } else {
853 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700854 }
Yorke Leec3766332013-07-31 11:13:16 -0700855 }
856
857 @Override
858 public void onDialpadQueryChanged(String query) {
859 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
860 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
861 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700862 if (DEBUG) {
863 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
864 }
865 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
866 // This callback can happen if the dialpad fragment is recreated because of
867 // activity destruction. In that case, don't update the search view because
868 // that would bring the user back to the search fragment regardless of the
869 // previous state of the application. Instead, just return here and let the
870 // fragment manager correctly figure out whatever fragment was last displayed.
871 return;
872 }
Yorke Leec3766332013-07-31 11:13:16 -0700873 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700874 }
875 }
Yorke Leec3766332013-07-31 11:13:16 -0700876
877 @Override
878 public void onListFragmentScrollStateChange(int scrollState) {
879 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700880 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700881 hideInputMethod(getCurrentFocus());
882 }
883 }
884
885 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700886 public void onDialpadFragmentStarted() {
887 setupFakeActionBarItemsForDialpadFragment();
888 }
889
890 private boolean phoneIsInUse() {
891 final TelephonyManager tm = (TelephonyManager) getSystemService(
892 Context.TELEPHONY_SERVICE);
893 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
894 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700895
896 @Override
897 public void onShowAllContacts() {
898 final Intent intent = new Intent(this, AllContactsActivity.class);
899 startActivity(intent);
900 }
Yorke Leee1424812013-09-04 18:24:48 -0700901
902 public static Intent getAddNumberToContactIntent(CharSequence text) {
903 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
904 intent.putExtra(Intents.Insert.PHONE, text);
905 intent.setType(Contacts.CONTENT_ITEM_TYPE);
906 return intent;
907 }
908
909 public static Intent getInsertContactWithNameIntent(CharSequence text) {
910 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
911 intent.putExtra(Intents.Insert.NAME, text);
912 return intent;
913 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700914}