blob: e513f92a58d771ec2542131f628817e8dd4803f2 [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 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700164 * True if the dialpad is only temporarily showing due to being in call
165 */
166 private boolean mInCallDialpadUp;
167
168 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700169 * True when this activity has been launched for the first time.
170 */
Yorke Lee98702de2013-08-06 12:03:32 -0700171 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700172 private View mSearchViewContainer;
173 private View mSearchViewCloseButton;
174 private View mVoiceSearchButton;
175 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700176
Yorke Leeef2b7382013-08-09 17:39:25 -0700177 private String mSearchQuery;
178
Yorke Leefce269a2013-08-12 11:56:04 -0700179 private DialerDatabaseHelper mDialerDatabaseHelper;
180
Chiao Cheng94b10b52012-08-17 16:59:12 -0700181 /**
182 * Listener used when one of phone numbers in search UI is selected. This will initiate a
183 * phone call using the phone number.
184 */
185 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
186 new OnPhoneNumberPickerActionListener() {
187 @Override
188 public void onPickPhoneNumberAction(Uri dataUri) {
189 // Specify call-origin so that users will see the previous tab instead of
190 // CallLog screen (search UI will be automatically exited).
191 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700192 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Leef6673d32013-08-23 15:34:17 -0700193 hideDialpadAndSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700194 }
195
196 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700197 public void onCallNumberDirectly(String phoneNumber) {
198 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
199 startActivity(intent);
Yorke Leef6673d32013-08-23 15:34:17 -0700200 hideDialpadAndSearchUi();
Jay Shrauner31a760b2013-07-26 09:34:49 -0700201 }
202
203 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700204 public void onShortcutIntentCreated(Intent intent) {
205 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
206 }
207
208 @Override
209 public void onHomeInActionBarSelected() {
210 exitSearchUi();
211 }
212 };
213
214 /**
215 * Listener used to send search queries to the phone search fragment.
216 */
Yorke Leec3766332013-07-31 11:13:16 -0700217 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
218 @Override
219 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
220 }
221
222 @Override
223 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700224 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700225 if (newText.equals(mSearchQuery)) {
226 // If the query hasn't changed (perhaps due to activity being destroyed
227 // and restored, or user launching the same DIAL intent twice), then there is
228 // no need to do anything here.
229 return;
230 }
231 mSearchQuery = newText;
232 if (DEBUG) {
233 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
234 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700235 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700236
Yorke Leec3766332013-07-31 11:13:16 -0700237 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700238 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700239 exitSearchUi();
240 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700241 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700242 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700243 } else if (!TextUtils.isEmpty(newText)) {
244 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
245 (!dialpadSearch && mInRegularSearch);
246 if (!sameSearchMode) {
247 // call enterSearchUi only if we are switching search modes, or entering
248 // search ui for the first time
249 enterSearchUi(dialpadSearch, newText);
250 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700251
Yorke Leeb207f8a2013-08-29 18:51:06 -0700252 if (dialpadSearch && mSmartDialSearchFragment != null) {
253 mSmartDialSearchFragment.setQueryString(newText, false);
254 } else if (mRegularSearchFragment != null) {
255 mRegularSearchFragment.setQueryString(newText, false);
256 }
257 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700258 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700259 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700260 }
Yorke Leec3766332013-07-31 11:13:16 -0700261 }
262
263 @Override
264 public void afterTextChanged(Editable s) {
265 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700266 };
267
Yorke Leec3766332013-07-31 11:13:16 -0700268 private boolean isDialpadShowing() {
269 return mDialpadFragment != null && mDialpadFragment.isVisible();
270 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700271
272 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700273 protected void onCreate(Bundle savedInstanceState) {
274 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700275 mFirstLaunch = true;
276
Chiao Cheng94b10b52012-08-17 16:59:12 -0700277 final Intent intent = getIntent();
278 fixIntent(intent);
279
Yorke Lee8898cd02013-08-08 10:24:27 -0700280 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700281
Yorke Leec3766332013-07-31 11:13:16 -0700282 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700283
Yorke Leeef2b7382013-08-09 17:39:25 -0700284 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
285 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700286 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700287 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700288
Yorke Leec3766332013-07-31 11:13:16 -0700289 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700290 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
291 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700292 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700293 } else {
294 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700295 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
296 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700297 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700298 }
299
Yorke Leec3766332013-07-31 11:13:16 -0700300 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700301 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leec3766332013-07-31 11:13:16 -0700302 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700303
Chiao Cheng94b10b52012-08-17 16:59:12 -0700304 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700305 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700306 setupFilterText(intent);
307 }
Yorke Leefce269a2013-08-12 11:56:04 -0700308
Yorke Leeb207f8a2013-08-29 18:51:06 -0700309 setupFakeActionBarItems();
310
Yorke Lee0baa98b2013-08-22 10:55:16 -0700311 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700312 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700313 }
314
315 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700316 protected void onResume() {
317 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700318 if (mFirstLaunch) {
319 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700320 } else if (!phoneIsInUse() && mInCallDialpadUp) {
321 hideDialpadFragment(false, true);
322 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700323 }
Yorke Lee35127cd2013-09-10 14:09:29 -0700324
Yorke Lee98702de2013-08-06 12:03:32 -0700325 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700326 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700327 }
328
329 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700330 protected void onSaveInstanceState(Bundle outState) {
331 super.onSaveInstanceState(outState);
332 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700333 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
334 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700335 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
336 }
337
338 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700339 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700340 if (fragment instanceof DialpadFragment) {
341 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700342 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700343 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700344 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700345 } else if (fragment instanceof SmartDialSearchFragment) {
346 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700347 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
348 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700349 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700350 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700351 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
352 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700353 } else if (fragment instanceof PhoneFavoriteFragment) {
354 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700355 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700356 }
Yorke Leec3766332013-07-31 11:13:16 -0700357 }
358
359 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700360 public boolean onMenuItemClick(MenuItem item) {
361 switch (item.getItemId()) {
362 case R.id.menu_import_export:
363 // We hard-code the "contactsAreAvailable" argument because doing it properly would
364 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
365 // now in Dialtacts for (potential) performance reasons. Compare with how it is
366 // done in {@link PeopleActivity}.
367 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700368 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700369 return true;
370 case R.id.menu_clear_frequents:
371 ClearFrequentsDialog.show(getFragmentManager());
372 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700373 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700374 try {
375 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
376 } catch (ActivityNotFoundException e) {
377 Toast toast = Toast.makeText(this,
378 R.string.add_contact_not_available,
379 Toast.LENGTH_SHORT);
380 toast.show();
381 }
382 return true;
383 case R.id.menu_call_settings:
Yorke Leef74011e2013-08-02 11:30:30 -0700384 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
Yorke Leec3766332013-07-31 11:13:16 -0700385 startActivity(settingsIntent);
Yorke Lee5e931972013-08-30 11:37:53 -0700386 return true;
387 case R.id.menu_all_contacts:
388 onShowAllContacts();
389 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700390 }
391 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700392 }
393
394 @Override
395 public void onClick(View view) {
396 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700397 case R.id.overflow_menu: {
398 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
399 final Menu menu = popupMenu.getMenu();
Yorke Lee8898cd02013-08-08 10:24:27 -0700400 popupMenu.inflate(R.menu.dialtacts_options);
Yorke Leef420a532013-08-09 15:16:10 -0700401 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Lee0d391b42013-08-09 17:27:00 -0700402 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
Yorke Leec3766332013-07-31 11:13:16 -0700403 popupMenu.setOnMenuItemClickListener(this);
404 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700405 break;
406 }
Yorke Leec3766332013-07-31 11:13:16 -0700407 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700408 // Reset the boolean flag that tracks whether the dialpad was up because
409 // we were in call. Regardless of whether it was true before, we want to
410 // show the dialpad because the user has explicitly clicked the dialpad
411 // button.
412 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700413 showDialpadFragment(true);
414 break;
415 case R.id.call_history_on_dialpad_button:
416 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700417 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
418 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700419 final Intent intent = new Intent(this, CallLogActivity.class);
420 startActivity(intent);
421 break;
422 case R.id.search_close_button:
423 // Clear the search field
424 if (!TextUtils.isEmpty(mSearchView.getText())) {
425 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700426 }
427 break;
Yorke Leec3766332013-07-31 11:13:16 -0700428 case R.id.voice_search_button:
429 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
430 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
431 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700432 default: {
433 Log.wtf(TAG, "Unexpected onClick event from " + view);
434 break;
435 }
436 }
437 }
438
Yorke Leec3766332013-07-31 11:13:16 -0700439 @Override
440 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
441 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
442 if (resultCode == RESULT_OK) {
443 final ArrayList<String> matches = data.getStringArrayListExtra(
444 RecognizerIntent.EXTRA_RESULTS);
445 if (matches.size() > 0) {
446 final String match = matches.get(0);
447 mSearchView.setText(match);
448 } else {
449 Log.e(TAG, "Voice search - nothing heard");
450 }
451 } else {
452 Log.e(TAG, "Voice search failed");
453 }
454 }
455 super.onActivityResult(requestCode, resultCode, data);
456 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700457
Yorke Leec3766332013-07-31 11:13:16 -0700458 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700459 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700460 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700461 if (animate) {
462 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700463 } else {
464 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700465 }
466 ft.show(mDialpadFragment);
467 ft.commit();
468 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700469
Yorke Leef6673d32013-08-23 15:34:17 -0700470 private void hideDialpadFragment(boolean animate, boolean clearDialpad) {
471 if (mDialpadFragment == null) return;
472 if (clearDialpad) {
473 mDialpadFragment.clearDialpad();
474 }
475 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700476 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700477 final FragmentTransaction ft = getFragmentManager().beginTransaction();
478 if (animate) {
479 ft.setCustomAnimations(0, R.anim.slide_out);
480 }
481 ft.hide(mDialpadFragment);
482 ft.commit();
483 }
484
Chiao Cheng94b10b52012-08-17 16:59:12 -0700485 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700486 mSearchViewContainer = findViewById(R.id.search_view_container);
487 mSearchViewCloseButton = findViewById(R.id.search_close_button);
488 mSearchViewCloseButton.setOnClickListener(this);
489 mVoiceSearchButton = findViewById(R.id.voice_search_button);
490 mVoiceSearchButton.setOnClickListener(this);
491 mSearchView = (EditText) findViewById(R.id.search_view);
492 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
493 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
494 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700495 @Override
496 public void onFocusChange(View view, boolean hasFocus) {
497 if (hasFocus) {
498 showInputMethod(view.findFocus());
499 }
500 }
501 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700502 }
503
Yorke Leec3766332013-07-31 11:13:16 -0700504 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
505 @Override
506 public void onAnimationEnd(Animator animation) {
507 mSearchViewContainer.setVisibility(View.GONE);
508 }
509 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700510
Yorke Leeb207f8a2013-08-29 18:51:06 -0700511 private boolean getInSearchUi() {
512 return mInDialpadSearch || mInRegularSearch;
513 }
514
515 private void setNotInSearchUi() {
516 mInDialpadSearch = false;
517 mInRegularSearch = false;
518 }
519
Yorke Lee311969c2013-08-26 06:31:11 -0700520 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700521 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700522 hideDialpadFragment(false, true);
523 }
524
Yorke Leec3766332013-07-31 11:13:16 -0700525 public void hideSearchBar() {
526 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700527 }
528
Yorke Leec3766332013-07-31 11:13:16 -0700529 public void hideSearchBar(boolean shiftView) {
530 if (shiftView) {
531 mSearchViewContainer.animate().cancel();
532 mSearchViewContainer.setAlpha(1);
533 mSearchViewContainer.setTranslationY(0);
534 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
535 .setDuration(200).setListener(mHideListener);
536
Yorke Leeb207f8a2013-08-29 18:51:06 -0700537 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700538 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
539 new AnimatorListenerAdapter() {
540 @Override
541 public void onAnimationEnd(Animator animation) {
542 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700543 mFragmentsFrame.setTranslationY(0);
Yorke Leec3766332013-07-31 11:13:16 -0700544 }
545 });
546 } else {
547 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
548 }
549 }
550
551 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700552 mSearchViewContainer.animate().cancel();
553 mSearchViewContainer.setAlpha(0);
554 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
555 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
556 .setListener(new AnimatorListenerAdapter() {
557 @Override
558 public void onAnimationStart(Animator animation) {
559 mSearchViewContainer.setVisibility(View.VISIBLE);
560 }
561 });
562
Yorke Leeb207f8a2013-08-29 18:51:06 -0700563 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
564 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700565 .setListener(
566 new AnimatorListenerAdapter() {
567 @Override
568 public void onAnimationStart(Animator animation) {
569 mBottomPaddingView.setVisibility(View.GONE);
570 }
571 });
572 }
573
574
Yorke Leeb207f8a2013-08-29 18:51:06 -0700575 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700576 mMenuButton = findViewById(R.id.overflow_menu);
577 if (mMenuButton != null) {
578 mMenuButton.setOnClickListener(this);
579 }
580
581 mCallHistoryButton = findViewById(R.id.call_history_button);
582 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
583 mCallHistoryButton.setOnClickListener(this);
584
585 mDialpadButton = findViewById(R.id.dialpad_button);
586 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
587 mDialpadButton.setOnClickListener(this);
588 }
589
590 public void setupFakeActionBarItemsForDialpadFragment() {
591 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
592 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700593 }
594
595 private void fixIntent(Intent intent) {
596 // This should be cleaned up: the call key used to send an Intent
597 // that just said to go to the recent calls list. It now sends this
598 // abstract action, but this class hasn't been rewritten to deal with it.
599 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
600 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
601 intent.putExtra("call_key", true);
602 setIntent(intent);
603 }
604 }
605
Chiao Cheng94b10b52012-08-17 16:59:12 -0700606 /**
607 * Returns true if the intent is due to hitting the green send key (hardware call button:
608 * KEYCODE_CALL) while in a call.
609 *
610 * @param intent the intent that launched this activity
611 * @param recentCallsRequest true if the intent is requesting to view recent calls
612 * @return true if the intent is due to hitting the green send key while in a call
613 */
Yorke Leec3766332013-07-31 11:13:16 -0700614 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700615 // If there is a call in progress go to the call screen
616 if (recentCallsRequest) {
617 final boolean callKey = intent.getBooleanExtra("call_key", false);
618
619 try {
620 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
621 if (callKey && phone != null && phone.showCallScreen()) {
622 return true;
623 }
624 } catch (RemoteException e) {
625 Log.e(TAG, "Failed to handle send while in call", e);
626 }
627 }
628
629 return false;
630 }
631
632 /**
633 * Sets the current tab based on the intent's request type
634 *
635 * @param intent Intent that contains information about which tab should be selected
636 */
Yorke Leec3766332013-07-31 11:13:16 -0700637 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700638 // If we got here by hitting send and we're in call forward along to the in-call activity
639 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
640 getContentResolver()));
641 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
642 finish();
643 return;
644 }
645
Yorke Lee98702de2013-08-06 12:03:32 -0700646 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700647 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Lee35127cd2013-09-10 14:09:29 -0700648 if (!mDialpadFragment.isVisible()) {
649 mInCallDialpadUp = true;
650 }
Yorke Leeea695cb2013-08-09 11:02:26 -0700651 showDialpadFragment(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700652 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700653 }
654
655 @Override
656 public void onNewIntent(Intent newIntent) {
657 setIntent(newIntent);
658 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700659 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700660 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700661
Chiao Cheng94b10b52012-08-17 16:59:12 -0700662 invalidateOptionsMenu();
663 }
664
665 /** Returns true if the given intent contains a phone number to populate the dialer with */
666 private boolean isDialIntent(Intent intent) {
667 final String action = intent.getAction();
668 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
669 return true;
670 }
671 if (Intent.ACTION_VIEW.equals(action)) {
672 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700673 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700674 return true;
675 }
676 }
677 return false;
678 }
679
680 /**
681 * Returns an appropriate call origin for this Activity. May return null when no call origin
682 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
683 * for remembering the tab in which the user made a phone call, so the external app's DIAL
684 * request should not be counted.)
685 */
686 public String getCallOrigin() {
687 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
688 }
689
690 /**
691 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
692 * This text originally came from a FILTER_CONTACTS_ACTION intent received
693 * by this activity. The stored text will then be cleared after after this
694 * method returns.
695 *
696 * @return The stored filter text
697 */
698 public String getAndClearFilterText() {
699 String filterText = mFilterText;
700 mFilterText = null;
701 return filterText;
702 }
703
704 /**
705 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
706 * This is so child activities can check if they are supposed to display a filter.
707 *
708 * @param intent The intent received in {@link #onNewIntent(Intent)}
709 */
710 private void setupFilterText(Intent intent) {
711 // If the intent was relaunched from history, don't apply the filter text.
712 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
713 return;
714 }
715 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
716 if (filter != null && filter.length() > 0) {
717 mFilterText = filter;
718 }
719 }
720
Yorke Lee8898cd02013-08-08 10:24:27 -0700721 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
722 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700723 @Override
724 public void onContactSelected(Uri contactUri) {
725 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700726 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700727 }
728
729 @Override
730 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700731 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700732 startActivity(intent);
733 }
734 };
735
Yorke Leec3766332013-07-31 11:13:16 -0700736 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
737 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
738 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700739
740 @Override
741 public void startSearch(String initialQuery, boolean selectInitialQuery,
742 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700743 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 if (mInSearchUi) {
745 if (mSearchView.hasFocus()) {
746 showInputMethod(mSearchView.findFocus());
747 } else {
748 mSearchView.requestFocus();
749 }
750 } else {
751 enterSearchUi();
752 }
753 } else {
754 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
755 }
Yorke Leec3766332013-07-31 11:13:16 -0700756 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700757
758 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700759 final InputMethodManager imm = (InputMethodManager) getSystemService(
760 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700761 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700762 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700763 }
764 }
765
766 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700767 final InputMethodManager imm = (InputMethodManager) getSystemService(
768 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700769 if (imm != null && view != null) {
770 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
771 }
772 }
773
774 /**
Yorke Leec3766332013-07-31 11:13:16 -0700775 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700776 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700777 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700778 if (getFragmentManager().isDestroyed()) {
779 // Weird race condition where fragment is doing work after the activity is destroyed
780 // due to talkback being on (b/10209937). Just return since we can't do any
781 // constructive here.
782 return;
783 }
784
Yorke Leeef2b7382013-08-09 17:39:25 -0700785 if (DEBUG) {
786 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
787 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700788
Yorke Leec3766332013-07-31 11:13:16 -0700789 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
790 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700791
Yorke Leeef2b7382013-08-09 17:39:25 -0700792 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700793 if (mInDialpadSearch) {
794 transaction.remove(mSmartDialSearchFragment);
795 } else if (mInRegularSearch) {
796 transaction.remove(mRegularSearchFragment);
797 } else {
798 transaction.remove(mPhoneFavoriteFragment);
799 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700800
Yorke Leeb207f8a2013-08-29 18:51:06 -0700801 final String tag;
802 if (smartDialSearch) {
803 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
804 } else {
805 tag = TAG_REGULAR_SEARCH_FRAGMENT;
806 }
807 mInDialpadSearch = smartDialSearch;
808 mInRegularSearch = !smartDialSearch;
809
Yorke Leeef2b7382013-08-09 17:39:25 -0700810 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
811 if (fragment == null) {
812 if (smartDialSearch) {
813 fragment = new SmartDialSearchFragment();
814 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700815 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700816 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700817 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700818 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700819 transaction.addToBackStack(null);
820 fragment.setQueryString(query, false);
821 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700822 }
823
824 /**
Yorke Leec3766332013-07-31 11:13:16 -0700825 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700826 */
Yorke Leec3766332013-07-31 11:13:16 -0700827 private void exitSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700828 // Go all the way back to the favorites fragment, regardless of how many times we
829 // transitioned between search fragments
830 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
831 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
832 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700833 }
834
835 /** Returns an Intent to launch Call Settings screen */
836 public static Intent getCallSettingsIntent() {
837 final Intent intent = new Intent(Intent.ACTION_MAIN);
838 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
839 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
840 return intent;
841 }
842
843 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700844 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700845 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700846 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700847 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700848 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700849 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700850 } else if (isTaskRoot()) {
851 // Instead of stopping, simply push this to the back of the stack.
852 // This is only done when running at the top of the stack;
853 // otherwise, we have been launched by someone else so need to
854 // allow the user to go back to the caller.
855 moveTaskToBack(false);
856 } else {
857 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700858 }
Yorke Leec3766332013-07-31 11:13:16 -0700859 }
860
861 @Override
862 public void onDialpadQueryChanged(String query) {
863 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
864 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
865 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700866 if (DEBUG) {
867 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
868 }
869 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
870 // This callback can happen if the dialpad fragment is recreated because of
871 // activity destruction. In that case, don't update the search view because
872 // that would bring the user back to the search fragment regardless of the
873 // previous state of the application. Instead, just return here and let the
874 // fragment manager correctly figure out whatever fragment was last displayed.
875 return;
876 }
Yorke Leec3766332013-07-31 11:13:16 -0700877 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700878 }
879 }
Yorke Leec3766332013-07-31 11:13:16 -0700880
881 @Override
882 public void onListFragmentScrollStateChange(int scrollState) {
883 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700884 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700885 hideInputMethod(getCurrentFocus());
886 }
887 }
888
889 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700890 public void onDialpadFragmentStarted() {
891 setupFakeActionBarItemsForDialpadFragment();
892 }
893
894 private boolean phoneIsInUse() {
895 final TelephonyManager tm = (TelephonyManager) getSystemService(
896 Context.TELEPHONY_SERVICE);
897 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
898 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700899
900 @Override
901 public void onShowAllContacts() {
902 final Intent intent = new Intent(this, AllContactsActivity.class);
903 startActivity(intent);
904 }
Yorke Leee1424812013-09-04 18:24:48 -0700905
906 public static Intent getAddNumberToContactIntent(CharSequence text) {
907 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
908 intent.putExtra(Intents.Insert.PHONE, text);
909 intent.setType(Contacts.CONTENT_ITEM_TYPE);
910 return intent;
911 }
912
913 public static Intent getInsertContactWithNameIntent(CharSequence text) {
914 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
915 intent.putExtra(Intents.Insert.NAME, text);
916 return intent;
917 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700918}