blob: a4702c637eb2841b22f40ca8f62d14b3a0a263ed [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;
Yorke Lee36c54132013-09-25 16:17:06 -0700142 private PopupMenu mOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700143
144 // Padding view used to shift the fragments up when the dialpad is shown.
145 private View mBottomPaddingView;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700146 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700147
Yorke Leeb207f8a2013-08-29 18:51:06 -0700148 private boolean mInDialpadSearch;
149 private boolean mInRegularSearch;
150
Yorke Leeef2b7382013-08-09 17:39:25 -0700151 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700152 * True if the dialpad is only temporarily showing due to being in call
153 */
154 private boolean mInCallDialpadUp;
155
156 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700157 * True when this activity has been launched for the first time.
158 */
Yorke Lee98702de2013-08-06 12:03:32 -0700159 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700160 private View mSearchViewContainer;
161 private View mSearchViewCloseButton;
162 private View mVoiceSearchButton;
163 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700164
Yorke Leeef2b7382013-08-09 17:39:25 -0700165 private String mSearchQuery;
166
Yorke Leefce269a2013-08-12 11:56:04 -0700167 private DialerDatabaseHelper mDialerDatabaseHelper;
168
Yorke Lee36c54132013-09-25 16:17:06 -0700169 private class OverflowPopupMenu extends PopupMenu {
170 public OverflowPopupMenu(Context context, View anchor) {
171 super(context, anchor);
172 }
173
174 @Override
175 public void show() {
176 final Menu menu = getMenu();
177 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
178 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
179 super.show();
180 }
181 }
182
Chiao Cheng94b10b52012-08-17 16:59:12 -0700183 /**
184 * Listener used when one of phone numbers in search UI is selected. This will initiate a
185 * phone call using the phone number.
186 */
187 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
188 new OnPhoneNumberPickerActionListener() {
189 @Override
190 public void onPickPhoneNumberAction(Uri dataUri) {
191 // Specify call-origin so that users will see the previous tab instead of
192 // CallLog screen (search UI will be automatically exited).
193 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700194 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Leeb4680c52013-09-27 02:50:27 +0000195 hideDialpadAndSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700196 }
197
198 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700199 public void onCallNumberDirectly(String phoneNumber) {
200 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
201 startActivity(intent);
Yorke Leeb4680c52013-09-27 02:50:27 +0000202 hideDialpadAndSearchUi();
Jay Shrauner31a760b2013-07-26 09:34:49 -0700203 }
204
205 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700206 public void onShortcutIntentCreated(Intent intent) {
207 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
208 }
209
210 @Override
211 public void onHomeInActionBarSelected() {
212 exitSearchUi();
213 }
214 };
215
216 /**
217 * Listener used to send search queries to the phone search fragment.
218 */
Yorke Leec3766332013-07-31 11:13:16 -0700219 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
220 @Override
221 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
222 }
223
224 @Override
225 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700226 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700227 if (newText.equals(mSearchQuery)) {
228 // If the query hasn't changed (perhaps due to activity being destroyed
229 // and restored, or user launching the same DIAL intent twice), then there is
230 // no need to do anything here.
231 return;
232 }
233 mSearchQuery = newText;
234 if (DEBUG) {
235 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
236 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700237 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700238
Yorke Leec3766332013-07-31 11:13:16 -0700239 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700240 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700241 exitSearchUi();
242 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700243 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700244 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700245 } else if (!TextUtils.isEmpty(newText)) {
246 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
247 (!dialpadSearch && mInRegularSearch);
248 if (!sameSearchMode) {
249 // call enterSearchUi only if we are switching search modes, or entering
250 // search ui for the first time
251 enterSearchUi(dialpadSearch, newText);
252 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700253
Yorke Leeb207f8a2013-08-29 18:51:06 -0700254 if (dialpadSearch && mSmartDialSearchFragment != null) {
255 mSmartDialSearchFragment.setQueryString(newText, false);
256 } else if (mRegularSearchFragment != null) {
257 mRegularSearchFragment.setQueryString(newText, false);
258 }
259 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700260 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700261 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700262 }
Yorke Leec3766332013-07-31 11:13:16 -0700263 }
264
265 @Override
266 public void afterTextChanged(Editable s) {
267 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700268 };
269
Yorke Leec3766332013-07-31 11:13:16 -0700270 private boolean isDialpadShowing() {
271 return mDialpadFragment != null && mDialpadFragment.isVisible();
272 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700273
274 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700275 protected void onCreate(Bundle savedInstanceState) {
276 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700277 mFirstLaunch = true;
278
Chiao Cheng94b10b52012-08-17 16:59:12 -0700279 final Intent intent = getIntent();
280 fixIntent(intent);
281
Yorke Lee8898cd02013-08-08 10:24:27 -0700282 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700283
Yorke Leec3766332013-07-31 11:13:16 -0700284 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700285
Yorke Leeef2b7382013-08-09 17:39:25 -0700286 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
287 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700288 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700289 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700290
Yorke Leec3766332013-07-31 11:13:16 -0700291 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700292 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
293 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700294 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700295 } else {
296 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700297 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
298 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700299 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700300 }
301
Yorke Leec3766332013-07-31 11:13:16 -0700302 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700303 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leec3766332013-07-31 11:13:16 -0700304 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700305
Chiao Cheng94b10b52012-08-17 16:59:12 -0700306 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700307 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700308 setupFilterText(intent);
309 }
Yorke Leefce269a2013-08-12 11:56:04 -0700310
Yorke Leeb207f8a2013-08-29 18:51:06 -0700311 setupFakeActionBarItems();
312
Yorke Lee0baa98b2013-08-22 10:55:16 -0700313 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700314 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700315 }
316
317 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700318 protected void onResume() {
319 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700320 if (mFirstLaunch) {
321 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700322 } else if (!phoneIsInUse() && mInCallDialpadUp) {
323 hideDialpadFragment(false, true);
324 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700325 }
Yorke Lee35127cd2013-09-10 14:09:29 -0700326
Yorke Lee98702de2013-08-06 12:03:32 -0700327 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700328 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700329 }
330
331 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700332 protected void onSaveInstanceState(Bundle outState) {
333 super.onSaveInstanceState(outState);
334 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700335 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
336 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700337 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
338 }
339
340 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700341 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700342 if (fragment instanceof DialpadFragment) {
343 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700344 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700345 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700346 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700347 } else if (fragment instanceof SmartDialSearchFragment) {
348 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700349 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
350 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700351 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700352 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700353 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
354 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700355 } else if (fragment instanceof PhoneFavoriteFragment) {
356 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700357 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700358 }
Yorke Leec3766332013-07-31 11:13:16 -0700359 }
360
361 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700362 public boolean onMenuItemClick(MenuItem item) {
363 switch (item.getItemId()) {
364 case R.id.menu_import_export:
365 // We hard-code the "contactsAreAvailable" argument because doing it properly would
366 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
367 // now in Dialtacts for (potential) performance reasons. Compare with how it is
368 // done in {@link PeopleActivity}.
369 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700370 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700371 return true;
372 case R.id.menu_clear_frequents:
373 ClearFrequentsDialog.show(getFragmentManager());
374 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700375 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700376 try {
377 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
378 } catch (ActivityNotFoundException e) {
379 Toast toast = Toast.makeText(this,
380 R.string.add_contact_not_available,
381 Toast.LENGTH_SHORT);
382 toast.show();
383 }
384 return true;
385 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700386 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700387 return true;
388 case R.id.menu_all_contacts:
389 onShowAllContacts();
390 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700391 }
392 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700393 }
394
Alon Albertb453e5b2013-09-10 15:54:23 -0700395 protected void handleMenuSettings() {
396 openTelephonySetting(this);
397 }
398
399 public static void openTelephonySetting(Activity activity) {
400 final Intent settingsIntent = getCallSettingsIntent();
401 activity.startActivity(settingsIntent);
402 }
403
Chiao Cheng94b10b52012-08-17 16:59:12 -0700404 @Override
405 public void onClick(View view) {
406 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700407 case R.id.overflow_menu: {
Yorke Lee36c54132013-09-25 16:17:06 -0700408 mOverflowMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700409 break;
410 }
Yorke Leec3766332013-07-31 11:13:16 -0700411 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700412 // Reset the boolean flag that tracks whether the dialpad was up because
413 // we were in call. Regardless of whether it was true before, we want to
414 // show the dialpad because the user has explicitly clicked the dialpad
415 // button.
416 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700417 showDialpadFragment(true);
418 break;
419 case R.id.call_history_on_dialpad_button:
420 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700421 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
422 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700423 final Intent intent = new Intent(this, CallLogActivity.class);
424 startActivity(intent);
425 break;
426 case R.id.search_close_button:
427 // Clear the search field
428 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700429 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700430 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700431 }
432 break;
Yorke Leec3766332013-07-31 11:13:16 -0700433 case R.id.voice_search_button:
434 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
435 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
436 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700437 default: {
438 Log.wtf(TAG, "Unexpected onClick event from " + view);
439 break;
440 }
441 }
442 }
443
Yorke Leec3766332013-07-31 11:13:16 -0700444 @Override
445 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
446 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
447 if (resultCode == RESULT_OK) {
448 final ArrayList<String> matches = data.getStringArrayListExtra(
449 RecognizerIntent.EXTRA_RESULTS);
450 if (matches.size() > 0) {
451 final String match = matches.get(0);
452 mSearchView.setText(match);
453 } else {
454 Log.e(TAG, "Voice search - nothing heard");
455 }
456 } else {
457 Log.e(TAG, "Voice search failed");
458 }
459 }
460 super.onActivityResult(requestCode, resultCode, data);
461 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700462
Yorke Leec3766332013-07-31 11:13:16 -0700463 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700464 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700465 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700466 if (animate) {
467 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700468 } else {
469 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700470 }
471 ft.show(mDialpadFragment);
472 ft.commit();
473 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700474
Yorke Leeca195042013-09-25 16:29:38 -0700475 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700476 if (mDialpadFragment == null) return;
477 if (clearDialpad) {
478 mDialpadFragment.clearDialpad();
479 }
480 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700481 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700482 final FragmentTransaction ft = getFragmentManager().beginTransaction();
483 if (animate) {
484 ft.setCustomAnimations(0, R.anim.slide_out);
485 }
486 ft.hide(mDialpadFragment);
487 ft.commit();
488 }
489
Chiao Cheng94b10b52012-08-17 16:59:12 -0700490 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700491 mSearchViewContainer = findViewById(R.id.search_view_container);
492 mSearchViewCloseButton = findViewById(R.id.search_close_button);
493 mSearchViewCloseButton.setOnClickListener(this);
494 mVoiceSearchButton = findViewById(R.id.voice_search_button);
495 mVoiceSearchButton.setOnClickListener(this);
496 mSearchView = (EditText) findViewById(R.id.search_view);
497 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
498 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
499 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700500 @Override
501 public void onFocusChange(View view, boolean hasFocus) {
502 if (hasFocus) {
503 showInputMethod(view.findFocus());
504 }
505 }
506 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700507 }
508
Yorke Leec3766332013-07-31 11:13:16 -0700509 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
510 @Override
511 public void onAnimationEnd(Animator animation) {
512 mSearchViewContainer.setVisibility(View.GONE);
513 }
514 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700515
Yorke Leeb207f8a2013-08-29 18:51:06 -0700516 private boolean getInSearchUi() {
517 return mInDialpadSearch || mInRegularSearch;
518 }
519
520 private void setNotInSearchUi() {
521 mInDialpadSearch = false;
522 mInRegularSearch = false;
523 }
524
Yorke Lee311969c2013-08-26 06:31:11 -0700525 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700526 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700527 hideDialpadFragment(false, true);
528 }
529
Yorke Leec3766332013-07-31 11:13:16 -0700530 public void hideSearchBar() {
531 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700532 }
533
Yorke Leec3766332013-07-31 11:13:16 -0700534 public void hideSearchBar(boolean shiftView) {
535 if (shiftView) {
536 mSearchViewContainer.animate().cancel();
537 mSearchViewContainer.setAlpha(1);
538 mSearchViewContainer.setTranslationY(0);
539 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
540 .setDuration(200).setListener(mHideListener);
541
Yorke Leeb207f8a2013-08-29 18:51:06 -0700542 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700543 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
544 new AnimatorListenerAdapter() {
545 @Override
546 public void onAnimationEnd(Animator animation) {
547 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700548 mFragmentsFrame.setTranslationY(0);
Yorke Leec3766332013-07-31 11:13:16 -0700549 }
550 });
551 } else {
552 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
553 }
554 }
555
556 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700557 mSearchViewContainer.animate().cancel();
558 mSearchViewContainer.setAlpha(0);
559 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
560 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
561 .setListener(new AnimatorListenerAdapter() {
562 @Override
563 public void onAnimationStart(Animator animation) {
564 mSearchViewContainer.setVisibility(View.VISIBLE);
565 }
566 });
567
Yorke Leeb207f8a2013-08-29 18:51:06 -0700568 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
569 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700570 .setListener(
571 new AnimatorListenerAdapter() {
572 @Override
573 public void onAnimationStart(Animator animation) {
574 mBottomPaddingView.setVisibility(View.GONE);
575 }
576 });
577 }
578
579
Yorke Leeb207f8a2013-08-29 18:51:06 -0700580 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700581 mMenuButton = findViewById(R.id.overflow_menu);
582 if (mMenuButton != null) {
583 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700584
585 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
586 final Menu menu = mOverflowMenu.getMenu();
587 mOverflowMenu.inflate(R.menu.dialtacts_options);
588 mOverflowMenu.setOnMenuItemClickListener(this);
589 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700590 }
591
592 mCallHistoryButton = findViewById(R.id.call_history_button);
593 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
594 mCallHistoryButton.setOnClickListener(this);
595
596 mDialpadButton = findViewById(R.id.dialpad_button);
597 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
598 mDialpadButton.setOnClickListener(this);
599 }
600
601 public void setupFakeActionBarItemsForDialpadFragment() {
602 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
603 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700604 }
605
606 private void fixIntent(Intent intent) {
607 // This should be cleaned up: the call key used to send an Intent
608 // that just said to go to the recent calls list. It now sends this
609 // abstract action, but this class hasn't been rewritten to deal with it.
610 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
611 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
612 intent.putExtra("call_key", true);
613 setIntent(intent);
614 }
615 }
616
Chiao Cheng94b10b52012-08-17 16:59:12 -0700617 /**
618 * Returns true if the intent is due to hitting the green send key (hardware call button:
619 * KEYCODE_CALL) while in a call.
620 *
621 * @param intent the intent that launched this activity
622 * @param recentCallsRequest true if the intent is requesting to view recent calls
623 * @return true if the intent is due to hitting the green send key while in a call
624 */
Yorke Leec3766332013-07-31 11:13:16 -0700625 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700626 // If there is a call in progress go to the call screen
627 if (recentCallsRequest) {
628 final boolean callKey = intent.getBooleanExtra("call_key", false);
629
630 try {
631 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
632 if (callKey && phone != null && phone.showCallScreen()) {
633 return true;
634 }
635 } catch (RemoteException e) {
636 Log.e(TAG, "Failed to handle send while in call", e);
637 }
638 }
639
640 return false;
641 }
642
643 /**
644 * Sets the current tab based on the intent's request type
645 *
646 * @param intent Intent that contains information about which tab should be selected
647 */
Yorke Leec3766332013-07-31 11:13:16 -0700648 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700649 // If we got here by hitting send and we're in call forward along to the in-call activity
650 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
651 getContentResolver()));
652 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
653 finish();
654 return;
655 }
656
Yorke Lee669b6082013-09-17 15:43:38 -0700657 if (mDialpadFragment != null) {
658 final boolean phoneIsInUse = phoneIsInUse();
659 if (phoneIsInUse || isDialIntent(intent)) {
660 mDialpadFragment.setStartedFromNewIntent(true);
661 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
662 mInCallDialpadUp = true;
663 }
664 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700665 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700666 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700667 }
668
669 @Override
670 public void onNewIntent(Intent newIntent) {
671 setIntent(newIntent);
672 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700673 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700674 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700675
Chiao Cheng94b10b52012-08-17 16:59:12 -0700676 invalidateOptionsMenu();
677 }
678
679 /** Returns true if the given intent contains a phone number to populate the dialer with */
680 private boolean isDialIntent(Intent intent) {
681 final String action = intent.getAction();
682 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
683 return true;
684 }
685 if (Intent.ACTION_VIEW.equals(action)) {
686 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700687 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700688 return true;
689 }
690 }
691 return false;
692 }
693
694 /**
695 * Returns an appropriate call origin for this Activity. May return null when no call origin
696 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
697 * for remembering the tab in which the user made a phone call, so the external app's DIAL
698 * request should not be counted.)
699 */
700 public String getCallOrigin() {
701 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
702 }
703
704 /**
705 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
706 * This text originally came from a FILTER_CONTACTS_ACTION intent received
707 * by this activity. The stored text will then be cleared after after this
708 * method returns.
709 *
710 * @return The stored filter text
711 */
712 public String getAndClearFilterText() {
713 String filterText = mFilterText;
714 mFilterText = null;
715 return filterText;
716 }
717
718 /**
719 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
720 * This is so child activities can check if they are supposed to display a filter.
721 *
722 * @param intent The intent received in {@link #onNewIntent(Intent)}
723 */
724 private void setupFilterText(Intent intent) {
725 // If the intent was relaunched from history, don't apply the filter text.
726 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
727 return;
728 }
729 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
730 if (filter != null && filter.length() > 0) {
731 mFilterText = filter;
732 }
733 }
734
Yorke Lee8898cd02013-08-08 10:24:27 -0700735 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
736 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700737 @Override
738 public void onContactSelected(Uri contactUri) {
739 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700740 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700741 }
742
743 @Override
744 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700745 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700746 startActivity(intent);
747 }
748 };
749
Yorke Leec3766332013-07-31 11:13:16 -0700750 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
751 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
752 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700753
754 @Override
755 public void startSearch(String initialQuery, boolean selectInitialQuery,
756 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700757 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700758 if (mInSearchUi) {
759 if (mSearchView.hasFocus()) {
760 showInputMethod(mSearchView.findFocus());
761 } else {
762 mSearchView.requestFocus();
763 }
764 } else {
765 enterSearchUi();
766 }
767 } else {
768 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
769 }
Yorke Leec3766332013-07-31 11:13:16 -0700770 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700771
772 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700773 final InputMethodManager imm = (InputMethodManager) getSystemService(
774 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700775 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700776 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700777 }
778 }
779
780 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700781 final InputMethodManager imm = (InputMethodManager) getSystemService(
782 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700783 if (imm != null && view != null) {
784 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
785 }
786 }
787
788 /**
Yorke Leec3766332013-07-31 11:13:16 -0700789 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700790 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700791 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700792 if (getFragmentManager().isDestroyed()) {
793 // Weird race condition where fragment is doing work after the activity is destroyed
794 // due to talkback being on (b/10209937). Just return since we can't do any
795 // constructive here.
796 return;
797 }
798
Yorke Leeef2b7382013-08-09 17:39:25 -0700799 if (DEBUG) {
800 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
801 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700802
Yorke Leec3766332013-07-31 11:13:16 -0700803 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
804 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700805
Yorke Leeef2b7382013-08-09 17:39:25 -0700806 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700807 if (mInDialpadSearch) {
808 transaction.remove(mSmartDialSearchFragment);
809 } else if (mInRegularSearch) {
810 transaction.remove(mRegularSearchFragment);
811 } else {
812 transaction.remove(mPhoneFavoriteFragment);
813 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700814
Yorke Leeb207f8a2013-08-29 18:51:06 -0700815 final String tag;
816 if (smartDialSearch) {
817 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
818 } else {
819 tag = TAG_REGULAR_SEARCH_FRAGMENT;
820 }
821 mInDialpadSearch = smartDialSearch;
822 mInRegularSearch = !smartDialSearch;
823
Yorke Leeef2b7382013-08-09 17:39:25 -0700824 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
825 if (fragment == null) {
826 if (smartDialSearch) {
827 fragment = new SmartDialSearchFragment();
828 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700829 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700830 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700831 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700832 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700833 transaction.addToBackStack(null);
834 fragment.setQueryString(query, false);
835 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700836 }
837
838 /**
Yorke Leec3766332013-07-31 11:13:16 -0700839 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700840 */
Yorke Leec3766332013-07-31 11:13:16 -0700841 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700842 // See related bug in enterSearchUI();
843 if (getFragmentManager().isDestroyed()) {
844 return;
845 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700846 // Go all the way back to the favorites fragment, regardless of how many times we
847 // transitioned between search fragments
848 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
849 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
850 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700851 }
852
853 /** Returns an Intent to launch Call Settings screen */
854 public static Intent getCallSettingsIntent() {
855 final Intent intent = new Intent(Intent.ACTION_MAIN);
856 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
857 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
858 return intent;
859 }
860
861 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700862 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700863 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700864 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700865 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700866 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700867 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700868 } else if (isTaskRoot()) {
869 // Instead of stopping, simply push this to the back of the stack.
870 // This is only done when running at the top of the stack;
871 // otherwise, we have been launched by someone else so need to
872 // allow the user to go back to the caller.
873 moveTaskToBack(false);
874 } else {
875 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700876 }
Yorke Leec3766332013-07-31 11:13:16 -0700877 }
878
879 @Override
880 public void onDialpadQueryChanged(String query) {
881 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
882 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
883 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700884 if (DEBUG) {
885 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
886 }
887 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
888 // This callback can happen if the dialpad fragment is recreated because of
889 // activity destruction. In that case, don't update the search view because
890 // that would bring the user back to the search fragment regardless of the
891 // previous state of the application. Instead, just return here and let the
892 // fragment manager correctly figure out whatever fragment was last displayed.
893 return;
894 }
Yorke Leec3766332013-07-31 11:13:16 -0700895 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700896 }
897 }
Yorke Leec3766332013-07-31 11:13:16 -0700898
899 @Override
900 public void onListFragmentScrollStateChange(int scrollState) {
901 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700902 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700903 hideInputMethod(getCurrentFocus());
904 }
905 }
906
907 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700908 public void onDialpadFragmentStarted() {
909 setupFakeActionBarItemsForDialpadFragment();
910 }
911
912 private boolean phoneIsInUse() {
913 final TelephonyManager tm = (TelephonyManager) getSystemService(
914 Context.TELEPHONY_SERVICE);
915 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
916 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700917
918 @Override
919 public void onShowAllContacts() {
920 final Intent intent = new Intent(this, AllContactsActivity.class);
921 startActivity(intent);
922 }
Yorke Leee1424812013-09-04 18:24:48 -0700923
924 public static Intent getAddNumberToContactIntent(CharSequence text) {
925 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
926 intent.putExtra(Intents.Insert.PHONE, text);
927 intent.setType(Contacts.CONTENT_ITEM_TYPE);
928 return intent;
929 }
930
931 public static Intent getInsertContactWithNameIntent(CharSequence text) {
932 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
933 intent.putExtra(Intents.Insert.NAME, text);
934 return intent;
935 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700936}