blob: 4c747c95ba68e22408ee5995610dff19ca5929dd [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 Lee669b6082013-09-17 15:43:38 -0700642 if (mDialpadFragment != null) {
643 final boolean phoneIsInUse = phoneIsInUse();
644 if (phoneIsInUse || isDialIntent(intent)) {
645 mDialpadFragment.setStartedFromNewIntent(true);
646 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
647 mInCallDialpadUp = true;
648 }
649 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700650 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700651 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700652 }
653
654 @Override
655 public void onNewIntent(Intent newIntent) {
656 setIntent(newIntent);
657 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700658 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700659 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700660
Chiao Cheng94b10b52012-08-17 16:59:12 -0700661 invalidateOptionsMenu();
662 }
663
664 /** Returns true if the given intent contains a phone number to populate the dialer with */
665 private boolean isDialIntent(Intent intent) {
666 final String action = intent.getAction();
667 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
668 return true;
669 }
670 if (Intent.ACTION_VIEW.equals(action)) {
671 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700672 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700673 return true;
674 }
675 }
676 return false;
677 }
678
679 /**
680 * Returns an appropriate call origin for this Activity. May return null when no call origin
681 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
682 * for remembering the tab in which the user made a phone call, so the external app's DIAL
683 * request should not be counted.)
684 */
685 public String getCallOrigin() {
686 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
687 }
688
689 /**
690 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
691 * This text originally came from a FILTER_CONTACTS_ACTION intent received
692 * by this activity. The stored text will then be cleared after after this
693 * method returns.
694 *
695 * @return The stored filter text
696 */
697 public String getAndClearFilterText() {
698 String filterText = mFilterText;
699 mFilterText = null;
700 return filterText;
701 }
702
703 /**
704 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
705 * This is so child activities can check if they are supposed to display a filter.
706 *
707 * @param intent The intent received in {@link #onNewIntent(Intent)}
708 */
709 private void setupFilterText(Intent intent) {
710 // If the intent was relaunched from history, don't apply the filter text.
711 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
712 return;
713 }
714 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
715 if (filter != null && filter.length() > 0) {
716 mFilterText = filter;
717 }
718 }
719
Yorke Lee8898cd02013-08-08 10:24:27 -0700720 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
721 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700722 @Override
723 public void onContactSelected(Uri contactUri) {
724 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700725 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700726 }
727
728 @Override
729 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700730 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700731 startActivity(intent);
732 }
733 };
734
Yorke Leec3766332013-07-31 11:13:16 -0700735 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
736 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
737 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700738
739 @Override
740 public void startSearch(String initialQuery, boolean selectInitialQuery,
741 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700742 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700743 if (mInSearchUi) {
744 if (mSearchView.hasFocus()) {
745 showInputMethod(mSearchView.findFocus());
746 } else {
747 mSearchView.requestFocus();
748 }
749 } else {
750 enterSearchUi();
751 }
752 } else {
753 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
754 }
Yorke Leec3766332013-07-31 11:13:16 -0700755 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700756
757 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700758 final InputMethodManager imm = (InputMethodManager) getSystemService(
759 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700760 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700761 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700762 }
763 }
764
765 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700766 final InputMethodManager imm = (InputMethodManager) getSystemService(
767 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700768 if (imm != null && view != null) {
769 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
770 }
771 }
772
773 /**
Yorke Leec3766332013-07-31 11:13:16 -0700774 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700775 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700776 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700777 if (getFragmentManager().isDestroyed()) {
778 // Weird race condition where fragment is doing work after the activity is destroyed
779 // due to talkback being on (b/10209937). Just return since we can't do any
780 // constructive here.
781 return;
782 }
783
Yorke Leeef2b7382013-08-09 17:39:25 -0700784 if (DEBUG) {
785 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
786 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700787
Yorke Leec3766332013-07-31 11:13:16 -0700788 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
789 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700790
Yorke Leeef2b7382013-08-09 17:39:25 -0700791 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700792 if (mInDialpadSearch) {
793 transaction.remove(mSmartDialSearchFragment);
794 } else if (mInRegularSearch) {
795 transaction.remove(mRegularSearchFragment);
796 } else {
797 transaction.remove(mPhoneFavoriteFragment);
798 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700799
Yorke Leeb207f8a2013-08-29 18:51:06 -0700800 final String tag;
801 if (smartDialSearch) {
802 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
803 } else {
804 tag = TAG_REGULAR_SEARCH_FRAGMENT;
805 }
806 mInDialpadSearch = smartDialSearch;
807 mInRegularSearch = !smartDialSearch;
808
Yorke Leeef2b7382013-08-09 17:39:25 -0700809 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
810 if (fragment == null) {
811 if (smartDialSearch) {
812 fragment = new SmartDialSearchFragment();
813 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700814 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700815 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700816 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700817 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700818 transaction.addToBackStack(null);
819 fragment.setQueryString(query, false);
820 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700821 }
822
823 /**
Yorke Leec3766332013-07-31 11:13:16 -0700824 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700825 */
Yorke Leec3766332013-07-31 11:13:16 -0700826 private void exitSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700827 // Go all the way back to the favorites fragment, regardless of how many times we
828 // transitioned between search fragments
829 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
830 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
831 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700832 }
833
834 /** Returns an Intent to launch Call Settings screen */
835 public static Intent getCallSettingsIntent() {
836 final Intent intent = new Intent(Intent.ACTION_MAIN);
837 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
838 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
839 return intent;
840 }
841
842 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700843 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700844 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700845 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700846 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700847 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700848 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700849 } else if (isTaskRoot()) {
850 // Instead of stopping, simply push this to the back of the stack.
851 // This is only done when running at the top of the stack;
852 // otherwise, we have been launched by someone else so need to
853 // allow the user to go back to the caller.
854 moveTaskToBack(false);
855 } else {
856 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700857 }
Yorke Leec3766332013-07-31 11:13:16 -0700858 }
859
860 @Override
861 public void onDialpadQueryChanged(String query) {
862 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
863 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
864 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700865 if (DEBUG) {
866 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
867 }
868 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
869 // This callback can happen if the dialpad fragment is recreated because of
870 // activity destruction. In that case, don't update the search view because
871 // that would bring the user back to the search fragment regardless of the
872 // previous state of the application. Instead, just return here and let the
873 // fragment manager correctly figure out whatever fragment was last displayed.
874 return;
875 }
Yorke Leec3766332013-07-31 11:13:16 -0700876 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700877 }
878 }
Yorke Leec3766332013-07-31 11:13:16 -0700879
880 @Override
881 public void onListFragmentScrollStateChange(int scrollState) {
882 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700883 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700884 hideInputMethod(getCurrentFocus());
885 }
886 }
887
888 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700889 public void onDialpadFragmentStarted() {
890 setupFakeActionBarItemsForDialpadFragment();
891 }
892
893 private boolean phoneIsInUse() {
894 final TelephonyManager tm = (TelephonyManager) getSystemService(
895 Context.TELEPHONY_SERVICE);
896 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
897 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700898
899 @Override
900 public void onShowAllContacts() {
901 final Intent intent = new Intent(this, AllContactsActivity.class);
902 startActivity(intent);
903 }
Yorke Leee1424812013-09-04 18:24:48 -0700904
905 public static Intent getAddNumberToContactIntent(CharSequence text) {
906 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
907 intent.putExtra(Intents.Insert.PHONE, text);
908 intent.setType(Contacts.CONTENT_ITEM_TYPE);
909 return intent;
910 }
911
912 public static Intent getInsertContactWithNameIntent(CharSequence text) {
913 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
914 intent.putExtra(Intents.Insert.NAME, text);
915 return intent;
916 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700917}