blob: 63b0cb8398e6a819db9161d1c1e1b7f734fbdeb2 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
Yorke Leec3766332013-07-31 11:13:16 -07002 * Copyright (C) 2013 The Android Open Source Project
Chiao Cheng94b10b52012-08-17 16:59:12 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer;
18
Yorke Leec3766332013-07-31 11:13:16 -070019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.app.Activity;
Christine Chen9c1e0652013-05-23 15:40:19 -070023import android.app.backup.BackupManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070024import android.app.Fragment;
25import android.app.FragmentManager;
Yorke Leeb207f8a2013-08-29 18:51:06 -070026import android.app.FragmentManager.BackStackEntry;
Chiao Cheng94b10b52012-08-17 16:59:12 -070027import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080028import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070029import android.content.Context;
30import android.content.Intent;
31import android.content.SharedPreferences;
Yorke Leec3766332013-07-31 11:13:16 -070032import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070033import android.net.Uri;
34import android.os.Bundle;
35import android.os.RemoteException;
36import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070038import android.provider.ContactsContract;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070040import android.provider.ContactsContract.Intents;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070042import android.provider.Settings;
43import android.speech.RecognizerIntent;
44import android.support.v4.app.NavUtils;
45import android.telephony.TelephonyManager;
46import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070048import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.util.Log;
50import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070052import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053import android.view.View.OnFocusChangeListener;
54import android.view.ViewConfiguration;
Chiao Cheng94b10b52012-08-17 16:59:12 -070055import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070056import android.widget.AbsListView.OnScrollListener;
57import android.widget.EditText;
58import android.widget.ImageView;
Chiao Cheng94b10b52012-08-17 16:59:12 -070059import android.widget.PopupMenu;
60import android.widget.SearchView;
61import android.widget.SearchView.OnCloseListener;
62import android.widget.SearchView.OnQueryTextListener;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080063import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070064
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070065import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070066import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070067import com.android.contacts.common.dialog.ClearFrequentsDialog;
68import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070069import com.android.contacts.common.list.ContactListItemView;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080070import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
71import com.android.contacts.common.list.PhoneNumberPickerFragment;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070073import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070074import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070075import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070076import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070077import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070078import com.android.dialer.list.AllContactsActivity;
Yorke Lee8898cd02013-08-08 10:24:27 -070079import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Leec3766332013-07-31 11:13:16 -070080import com.android.dialer.list.OnListFragmentScrolledListener;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070081import com.android.dialer.list.RegularSearchFragment;
82import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070083import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070084import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070085import com.android.internal.telephony.ITelephony;
86
Yorke Leec3766332013-07-31 11:13:16 -070087import java.util.ArrayList;
88
Chiao Cheng94b10b52012-08-17 16:59:12 -070089/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070090 * The dialer tab's title is 'phone', a more common name (see strings.xml).
91 */
Yorke Leec3766332013-07-31 11:13:16 -070092public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070093 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070094 OnListFragmentScrolledListener,
Yorke Lee8dd62002013-08-08 15:57:20 -070095 DialpadFragment.OnDialpadFragmentStartedListener,
96 PhoneFavoriteFragment.OnShowAllContactsListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070097 private static final String TAG = "DialtactsActivity";
98
99 public static final boolean DEBUG = false;
100
Yorke Leef74011e2013-08-02 11:30:30 -0700101 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
102
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103 /** Used to open Call Setting */
104 private static final String PHONE_PACKAGE = "com.android.phone";
105 private static final String CALL_SETTINGS_CLASS_NAME =
106 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700107 /** @see #getCallOrigin() */
108 private static final String CALL_ORIGIN_DIALTACTS =
109 "com.android.dialer.DialtactsActivity";
110
Yorke Leeb207f8a2013-08-29 18:51:06 -0700111 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
112 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700113 private static final String KEY_SEARCH_QUERY = "search_query";
114 private static final String KEY_FIRST_LAUNCH = "first_launch";
115
Yorke Leec3766332013-07-31 11:13:16 -0700116 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
117 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
118 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
119 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700120
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121 /**
122 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
123 */
124 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
125
Chiao Cheng94b10b52012-08-17 16:59:12 -0700126 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
127
Yorke Leec3766332013-07-31 11:13:16 -0700128 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700129
130 private String mFilterText;
131
Yorke Leec3766332013-07-31 11:13:16 -0700132 /**
133 * The main fragment displaying the user's favorites and frequent contacts
134 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700135 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700136
137 /**
Yorke Leec3766332013-07-31 11:13:16 -0700138 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700139 */
Yorke Leef74011e2013-08-02 11:30:30 -0700140 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700141
142 /**
143 * Fragment for searching phone numbers using the alphanumeric keyboard.
144 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700145 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700146
147 /**
148 * Fragment for searching phone numbers using the dialpad.
149 */
150 private SmartDialSearchFragment mSmartDialSearchFragment;
151
Yorke Leec3766332013-07-31 11:13:16 -0700152 private View mMenuButton;
153 private View mCallHistoryButton;
154 private View mDialpadButton;
155
156 // Padding view used to shift the fragments up when the dialpad is shown.
157 private View mBottomPaddingView;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700158 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700159
Yorke Leeb207f8a2013-08-29 18:51:06 -0700160 private boolean mInDialpadSearch;
161 private boolean mInRegularSearch;
162
Yorke Leeef2b7382013-08-09 17:39:25 -0700163 /**
164 * True when this activity has been launched for the first time.
165 */
Yorke Lee98702de2013-08-06 12:03:32 -0700166 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700167 private View mSearchViewContainer;
168 private View mSearchViewCloseButton;
169 private View mVoiceSearchButton;
170 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700171
Yorke Leeef2b7382013-08-09 17:39:25 -0700172 private String mSearchQuery;
173
Yorke Leefce269a2013-08-12 11:56:04 -0700174 private DialerDatabaseHelper mDialerDatabaseHelper;
175
Chiao Cheng94b10b52012-08-17 16:59:12 -0700176 /**
177 * Listener used when one of phone numbers in search UI is selected. This will initiate a
178 * phone call using the phone number.
179 */
180 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
181 new OnPhoneNumberPickerActionListener() {
182 @Override
183 public void onPickPhoneNumberAction(Uri dataUri) {
184 // Specify call-origin so that users will see the previous tab instead of
185 // CallLog screen (search UI will be automatically exited).
186 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700187 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Leef6673d32013-08-23 15:34:17 -0700188 hideDialpadAndSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700189 }
190
191 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700192 public void onCallNumberDirectly(String phoneNumber) {
193 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
194 startActivity(intent);
Yorke Leef6673d32013-08-23 15:34:17 -0700195 hideDialpadAndSearchUi();
Jay Shrauner31a760b2013-07-26 09:34:49 -0700196 }
197
198 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700199 public void onShortcutIntentCreated(Intent intent) {
200 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
201 }
202
203 @Override
204 public void onHomeInActionBarSelected() {
205 exitSearchUi();
206 }
207 };
208
209 /**
210 * Listener used to send search queries to the phone search fragment.
211 */
Yorke Leec3766332013-07-31 11:13:16 -0700212 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
213 @Override
214 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
215 }
216
217 @Override
218 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700219 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700220 if (newText.equals(mSearchQuery)) {
221 // If the query hasn't changed (perhaps due to activity being destroyed
222 // and restored, or user launching the same DIAL intent twice), then there is
223 // no need to do anything here.
224 return;
225 }
226 mSearchQuery = newText;
227 if (DEBUG) {
228 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
229 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700230 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700231
Yorke Leec3766332013-07-31 11:13:16 -0700232 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700233 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700234 exitSearchUi();
235 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700236 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700237 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700238 } else if (!TextUtils.isEmpty(newText)) {
239 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
240 (!dialpadSearch && mInRegularSearch);
241 if (!sameSearchMode) {
242 // call enterSearchUi only if we are switching search modes, or entering
243 // search ui for the first time
244 enterSearchUi(dialpadSearch, newText);
245 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700246
Yorke Leeb207f8a2013-08-29 18:51:06 -0700247 if (dialpadSearch && mSmartDialSearchFragment != null) {
248 mSmartDialSearchFragment.setQueryString(newText, false);
249 } else if (mRegularSearchFragment != null) {
250 mRegularSearchFragment.setQueryString(newText, false);
251 }
252 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700253 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700254 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700255 }
Yorke Leec3766332013-07-31 11:13:16 -0700256 }
257
258 @Override
259 public void afterTextChanged(Editable s) {
260 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700261 };
262
Yorke Leec3766332013-07-31 11:13:16 -0700263 private boolean isDialpadShowing() {
264 return mDialpadFragment != null && mDialpadFragment.isVisible();
265 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700266
267 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700268 protected void onCreate(Bundle savedInstanceState) {
269 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700270 mFirstLaunch = true;
271
Chiao Cheng94b10b52012-08-17 16:59:12 -0700272 final Intent intent = getIntent();
273 fixIntent(intent);
274
Yorke Lee8898cd02013-08-08 10:24:27 -0700275 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700276
Yorke Leec3766332013-07-31 11:13:16 -0700277 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700278
Yorke Leeef2b7382013-08-09 17:39:25 -0700279 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
280 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700281 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700282 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700283
Yorke Leec3766332013-07-31 11:13:16 -0700284 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700285 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
286 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700287 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700288 } else {
289 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700290 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
291 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700292 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700293 }
294
Yorke Leec3766332013-07-31 11:13:16 -0700295 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700296 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leec3766332013-07-31 11:13:16 -0700297 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700298
Chiao Cheng94b10b52012-08-17 16:59:12 -0700299 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700300 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700301 setupFilterText(intent);
302 }
Yorke Leefce269a2013-08-12 11:56:04 -0700303
Yorke Leeb207f8a2013-08-29 18:51:06 -0700304 setupFakeActionBarItems();
305
Yorke Lee0baa98b2013-08-22 10:55:16 -0700306 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700307 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700308 }
309
310 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700311 protected void onResume() {
312 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700313 if (mFirstLaunch) {
314 displayFragment(getIntent());
315 }
316 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700317 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700318 }
319
320 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700321 protected void onSaveInstanceState(Bundle outState) {
322 super.onSaveInstanceState(outState);
323 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700324 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
325 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700326 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
327 }
328
329 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700330 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700331 if (fragment instanceof DialpadFragment) {
332 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700333 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700334 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700335 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700336 } else if (fragment instanceof SmartDialSearchFragment) {
337 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700338 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
339 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700340 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700341 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700342 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
343 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700344 } else if (fragment instanceof PhoneFavoriteFragment) {
345 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700346 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700347 }
Yorke Leec3766332013-07-31 11:13:16 -0700348 }
349
350 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700351 public boolean onMenuItemClick(MenuItem item) {
352 switch (item.getItemId()) {
353 case R.id.menu_import_export:
354 // We hard-code the "contactsAreAvailable" argument because doing it properly would
355 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
356 // now in Dialtacts for (potential) performance reasons. Compare with how it is
357 // done in {@link PeopleActivity}.
358 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700359 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700360 return true;
361 case R.id.menu_clear_frequents:
362 ClearFrequentsDialog.show(getFragmentManager());
363 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700364 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700365 try {
366 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
367 } catch (ActivityNotFoundException e) {
368 Toast toast = Toast.makeText(this,
369 R.string.add_contact_not_available,
370 Toast.LENGTH_SHORT);
371 toast.show();
372 }
373 return true;
374 case R.id.menu_call_settings:
Yorke Leef74011e2013-08-02 11:30:30 -0700375 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
Yorke Leec3766332013-07-31 11:13:16 -0700376 startActivity(settingsIntent);
Yorke Lee5e931972013-08-30 11:37:53 -0700377 return true;
378 case R.id.menu_all_contacts:
379 onShowAllContacts();
380 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700381 }
382 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700383 }
384
385 @Override
386 public void onClick(View view) {
387 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700388 case R.id.overflow_menu: {
389 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
390 final Menu menu = popupMenu.getMenu();
Yorke Lee8898cd02013-08-08 10:24:27 -0700391 popupMenu.inflate(R.menu.dialtacts_options);
Yorke Leef420a532013-08-09 15:16:10 -0700392 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Lee0d391b42013-08-09 17:27:00 -0700393 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
Yorke Leec3766332013-07-31 11:13:16 -0700394 popupMenu.setOnMenuItemClickListener(this);
395 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700396 break;
397 }
Yorke Leec3766332013-07-31 11:13:16 -0700398 case R.id.dialpad_button:
399 showDialpadFragment(true);
400 break;
401 case R.id.call_history_on_dialpad_button:
402 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700403 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
404 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700405 final Intent intent = new Intent(this, CallLogActivity.class);
406 startActivity(intent);
407 break;
408 case R.id.search_close_button:
409 // Clear the search field
410 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700411 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700412 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700413 }
414 break;
Yorke Leec3766332013-07-31 11:13:16 -0700415 case R.id.voice_search_button:
416 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
417 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
418 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700419 default: {
420 Log.wtf(TAG, "Unexpected onClick event from " + view);
421 break;
422 }
423 }
424 }
425
Yorke Leec3766332013-07-31 11:13:16 -0700426 @Override
427 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
428 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
429 if (resultCode == RESULT_OK) {
430 final ArrayList<String> matches = data.getStringArrayListExtra(
431 RecognizerIntent.EXTRA_RESULTS);
432 if (matches.size() > 0) {
433 final String match = matches.get(0);
434 mSearchView.setText(match);
435 } else {
436 Log.e(TAG, "Voice search - nothing heard");
437 }
438 } else {
439 Log.e(TAG, "Voice search failed");
440 }
441 }
442 super.onActivityResult(requestCode, resultCode, data);
443 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700444
Yorke Leec3766332013-07-31 11:13:16 -0700445 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700446 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700447 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700448 if (animate) {
449 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700450 } else {
451 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700452 }
453 ft.show(mDialpadFragment);
454 ft.commit();
455 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700456
Yorke Leef6673d32013-08-23 15:34:17 -0700457 private void hideDialpadFragment(boolean animate, boolean clearDialpad) {
458 if (mDialpadFragment == null) return;
459 if (clearDialpad) {
460 mDialpadFragment.clearDialpad();
461 }
462 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700463 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700464 final FragmentTransaction ft = getFragmentManager().beginTransaction();
465 if (animate) {
466 ft.setCustomAnimations(0, R.anim.slide_out);
467 }
468 ft.hide(mDialpadFragment);
469 ft.commit();
470 }
471
Chiao Cheng94b10b52012-08-17 16:59:12 -0700472 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700473 mSearchViewContainer = findViewById(R.id.search_view_container);
474 mSearchViewCloseButton = findViewById(R.id.search_close_button);
475 mSearchViewCloseButton.setOnClickListener(this);
476 mVoiceSearchButton = findViewById(R.id.voice_search_button);
477 mVoiceSearchButton.setOnClickListener(this);
478 mSearchView = (EditText) findViewById(R.id.search_view);
479 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
480 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
481 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700482 @Override
483 public void onFocusChange(View view, boolean hasFocus) {
484 if (hasFocus) {
485 showInputMethod(view.findFocus());
486 }
487 }
488 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700489 }
490
Yorke Leec3766332013-07-31 11:13:16 -0700491 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
492 @Override
493 public void onAnimationEnd(Animator animation) {
494 mSearchViewContainer.setVisibility(View.GONE);
495 }
496 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700497
Yorke Leeb207f8a2013-08-29 18:51:06 -0700498 private boolean getInSearchUi() {
499 return mInDialpadSearch || mInRegularSearch;
500 }
501
502 private void setNotInSearchUi() {
503 mInDialpadSearch = false;
504 mInRegularSearch = false;
505 }
506
Yorke Lee311969c2013-08-26 06:31:11 -0700507 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700508 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700509 hideDialpadFragment(false, true);
510 }
511
Yorke Leec3766332013-07-31 11:13:16 -0700512 public void hideSearchBar() {
513 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700514 }
515
Yorke Leec3766332013-07-31 11:13:16 -0700516 public void hideSearchBar(boolean shiftView) {
517 if (shiftView) {
518 mSearchViewContainer.animate().cancel();
519 mSearchViewContainer.setAlpha(1);
520 mSearchViewContainer.setTranslationY(0);
521 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
522 .setDuration(200).setListener(mHideListener);
523
Yorke Leeb207f8a2013-08-29 18:51:06 -0700524 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700525 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
526 new AnimatorListenerAdapter() {
527 @Override
528 public void onAnimationEnd(Animator animation) {
529 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700530 mFragmentsFrame.setTranslationY(0);
Yorke Leec3766332013-07-31 11:13:16 -0700531 }
532 });
533 } else {
534 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
535 }
536 }
537
538 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700539 mSearchViewContainer.animate().cancel();
540 mSearchViewContainer.setAlpha(0);
541 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
542 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
543 .setListener(new AnimatorListenerAdapter() {
544 @Override
545 public void onAnimationStart(Animator animation) {
546 mSearchViewContainer.setVisibility(View.VISIBLE);
547 }
548 });
549
Yorke Leeb207f8a2013-08-29 18:51:06 -0700550 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
551 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700552 .setListener(
553 new AnimatorListenerAdapter() {
554 @Override
555 public void onAnimationStart(Animator animation) {
556 mBottomPaddingView.setVisibility(View.GONE);
557 }
558 });
559 }
560
561
Yorke Leeb207f8a2013-08-29 18:51:06 -0700562 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700563 mMenuButton = findViewById(R.id.overflow_menu);
564 if (mMenuButton != null) {
565 mMenuButton.setOnClickListener(this);
566 }
567
568 mCallHistoryButton = findViewById(R.id.call_history_button);
569 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
570 mCallHistoryButton.setOnClickListener(this);
571
572 mDialpadButton = findViewById(R.id.dialpad_button);
573 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
574 mDialpadButton.setOnClickListener(this);
575 }
576
577 public void setupFakeActionBarItemsForDialpadFragment() {
578 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
579 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700580 }
581
582 private void fixIntent(Intent intent) {
583 // This should be cleaned up: the call key used to send an Intent
584 // that just said to go to the recent calls list. It now sends this
585 // abstract action, but this class hasn't been rewritten to deal with it.
586 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
587 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
588 intent.putExtra("call_key", true);
589 setIntent(intent);
590 }
591 }
592
Chiao Cheng94b10b52012-08-17 16:59:12 -0700593 /**
594 * Returns true if the intent is due to hitting the green send key (hardware call button:
595 * KEYCODE_CALL) while in a call.
596 *
597 * @param intent the intent that launched this activity
598 * @param recentCallsRequest true if the intent is requesting to view recent calls
599 * @return true if the intent is due to hitting the green send key while in a call
600 */
Yorke Leec3766332013-07-31 11:13:16 -0700601 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700602 // If there is a call in progress go to the call screen
603 if (recentCallsRequest) {
604 final boolean callKey = intent.getBooleanExtra("call_key", false);
605
606 try {
607 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
608 if (callKey && phone != null && phone.showCallScreen()) {
609 return true;
610 }
611 } catch (RemoteException e) {
612 Log.e(TAG, "Failed to handle send while in call", e);
613 }
614 }
615
616 return false;
617 }
618
619 /**
620 * Sets the current tab based on the intent's request type
621 *
622 * @param intent Intent that contains information about which tab should be selected
623 */
Yorke Leec3766332013-07-31 11:13:16 -0700624 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700625 // If we got here by hitting send and we're in call forward along to the in-call activity
626 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
627 getContentResolver()));
628 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
629 finish();
630 return;
631 }
632
Yorke Lee98702de2013-08-06 12:03:32 -0700633 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700634 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leeea695cb2013-08-09 11:02:26 -0700635 showDialpadFragment(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700636 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700637 }
638
639 @Override
640 public void onNewIntent(Intent newIntent) {
641 setIntent(newIntent);
642 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700643 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700644 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700645
Chiao Cheng94b10b52012-08-17 16:59:12 -0700646 invalidateOptionsMenu();
647 }
648
649 /** Returns true if the given intent contains a phone number to populate the dialer with */
650 private boolean isDialIntent(Intent intent) {
651 final String action = intent.getAction();
652 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
653 return true;
654 }
655 if (Intent.ACTION_VIEW.equals(action)) {
656 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700657 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700658 return true;
659 }
660 }
661 return false;
662 }
663
664 /**
665 * Returns an appropriate call origin for this Activity. May return null when no call origin
666 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
667 * for remembering the tab in which the user made a phone call, so the external app's DIAL
668 * request should not be counted.)
669 */
670 public String getCallOrigin() {
671 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
672 }
673
674 /**
675 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
676 * This text originally came from a FILTER_CONTACTS_ACTION intent received
677 * by this activity. The stored text will then be cleared after after this
678 * method returns.
679 *
680 * @return The stored filter text
681 */
682 public String getAndClearFilterText() {
683 String filterText = mFilterText;
684 mFilterText = null;
685 return filterText;
686 }
687
688 /**
689 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
690 * This is so child activities can check if they are supposed to display a filter.
691 *
692 * @param intent The intent received in {@link #onNewIntent(Intent)}
693 */
694 private void setupFilterText(Intent intent) {
695 // If the intent was relaunched from history, don't apply the filter text.
696 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
697 return;
698 }
699 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
700 if (filter != null && filter.length() > 0) {
701 mFilterText = filter;
702 }
703 }
704
Yorke Lee8898cd02013-08-08 10:24:27 -0700705 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
706 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700707 @Override
708 public void onContactSelected(Uri contactUri) {
709 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700710 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700711 }
712
713 @Override
714 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700715 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700716 startActivity(intent);
717 }
718 };
719
Yorke Leec3766332013-07-31 11:13:16 -0700720 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
721 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
722 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700723
724 @Override
725 public void startSearch(String initialQuery, boolean selectInitialQuery,
726 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700727 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728 if (mInSearchUi) {
729 if (mSearchView.hasFocus()) {
730 showInputMethod(mSearchView.findFocus());
731 } else {
732 mSearchView.requestFocus();
733 }
734 } else {
735 enterSearchUi();
736 }
737 } else {
738 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
739 }
Yorke Leec3766332013-07-31 11:13:16 -0700740 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700741
742 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700743 final InputMethodManager imm = (InputMethodManager) getSystemService(
744 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700745 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700746 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700747 }
748 }
749
750 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700751 final InputMethodManager imm = (InputMethodManager) getSystemService(
752 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700753 if (imm != null && view != null) {
754 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
755 }
756 }
757
758 /**
Yorke Leec3766332013-07-31 11:13:16 -0700759 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700760 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700761 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700762 if (getFragmentManager().isDestroyed()) {
763 // Weird race condition where fragment is doing work after the activity is destroyed
764 // due to talkback being on (b/10209937). Just return since we can't do any
765 // constructive here.
766 return;
767 }
768
Yorke Leeef2b7382013-08-09 17:39:25 -0700769 if (DEBUG) {
770 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
771 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700772
Yorke Leec3766332013-07-31 11:13:16 -0700773 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
774 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700775
Yorke Leeef2b7382013-08-09 17:39:25 -0700776 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700777 if (mInDialpadSearch) {
778 transaction.remove(mSmartDialSearchFragment);
779 } else if (mInRegularSearch) {
780 transaction.remove(mRegularSearchFragment);
781 } else {
782 transaction.remove(mPhoneFavoriteFragment);
783 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700784
Yorke Leeb207f8a2013-08-29 18:51:06 -0700785 final String tag;
786 if (smartDialSearch) {
787 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
788 } else {
789 tag = TAG_REGULAR_SEARCH_FRAGMENT;
790 }
791 mInDialpadSearch = smartDialSearch;
792 mInRegularSearch = !smartDialSearch;
793
Yorke Leeef2b7382013-08-09 17:39:25 -0700794 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
795 if (fragment == null) {
796 if (smartDialSearch) {
797 fragment = new SmartDialSearchFragment();
798 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700799 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700800 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700801 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700802 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700803 transaction.addToBackStack(null);
804 fragment.setQueryString(query, false);
805 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700806 }
807
808 /**
Yorke Leec3766332013-07-31 11:13:16 -0700809 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700810 */
Yorke Leec3766332013-07-31 11:13:16 -0700811 private void exitSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700812 // Go all the way back to the favorites fragment, regardless of how many times we
813 // transitioned between search fragments
814 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
815 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
816 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700817 }
818
819 /** Returns an Intent to launch Call Settings screen */
820 public static Intent getCallSettingsIntent() {
821 final Intent intent = new Intent(Intent.ACTION_MAIN);
822 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
823 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
824 return intent;
825 }
826
827 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700828 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700829 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700830 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700831 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700832 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700833 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700834 } else if (isTaskRoot()) {
835 // Instead of stopping, simply push this to the back of the stack.
836 // This is only done when running at the top of the stack;
837 // otherwise, we have been launched by someone else so need to
838 // allow the user to go back to the caller.
839 moveTaskToBack(false);
840 } else {
841 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700842 }
Yorke Leec3766332013-07-31 11:13:16 -0700843 }
844
845 @Override
846 public void onDialpadQueryChanged(String query) {
847 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
848 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
849 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700850 if (DEBUG) {
851 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
852 }
853 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
854 // This callback can happen if the dialpad fragment is recreated because of
855 // activity destruction. In that case, don't update the search view because
856 // that would bring the user back to the search fragment regardless of the
857 // previous state of the application. Instead, just return here and let the
858 // fragment manager correctly figure out whatever fragment was last displayed.
859 return;
860 }
Yorke Leec3766332013-07-31 11:13:16 -0700861 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700862 }
863 }
Yorke Leec3766332013-07-31 11:13:16 -0700864
865 @Override
866 public void onListFragmentScrollStateChange(int scrollState) {
867 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700868 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700869 hideInputMethod(getCurrentFocus());
870 }
871 }
872
873 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700874 public void onDialpadFragmentStarted() {
875 setupFakeActionBarItemsForDialpadFragment();
876 }
877
878 private boolean phoneIsInUse() {
879 final TelephonyManager tm = (TelephonyManager) getSystemService(
880 Context.TELEPHONY_SERVICE);
881 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
882 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700883
884 @Override
885 public void onShowAllContacts() {
886 final Intent intent = new Intent(this, AllContactsActivity.class);
887 startActivity(intent);
888 }
Yorke Leee1424812013-09-04 18:24:48 -0700889
890 public static Intent getAddNumberToContactIntent(CharSequence text) {
891 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
892 intent.putExtra(Intents.Insert.PHONE, text);
893 intent.setType(Contacts.CONTENT_ITEM_TYPE);
894 return intent;
895 }
896
897 public static Intent getInsertContactWithNameIntent(CharSequence text) {
898 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
899 intent.putExtra(Intents.Insert.NAME, text);
900 return intent;
901 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700902}