blob: 04934c7769b3c99e6c28b42c42d44c82cecb6326 [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;
Yorke Lee41a24b42013-11-13 16:14:41 -080030import android.graphics.drawable.Drawable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070031import android.net.Uri;
32import android.os.Bundle;
33import android.os.RemoteException;
34import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.provider.CallLog.Calls;
36import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070037import android.provider.ContactsContract.Intents;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070039import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070040import android.telephony.TelephonyManager;
41import android.text.Editable;
Yorke Lee41a24b42013-11-13 16:14:41 -080042import android.text.Spannable;
43import android.text.SpannableStringBuilder;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070045import android.text.TextWatcher;
Yorke Lee41a24b42013-11-13 16:14:41 -080046import android.text.style.ImageSpan;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.util.Log;
48import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.view.View.OnFocusChangeListener;
Chiao Cheng94b10b52012-08-17 16:59:12 -070052import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070053import android.widget.AbsListView.OnScrollListener;
54import android.widget.EditText;
Chiao Cheng94b10b52012-08-17 16:59:12 -070055import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080056import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070058import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070059import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070060import com.android.contacts.common.dialog.ClearFrequentsDialog;
61import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080062import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070063import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070064import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070065import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070066import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070067import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070068import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070069import com.android.dialer.list.AllContactsActivity;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070071import com.android.dialer.list.PhoneFavoriteFragment;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070072import com.android.dialer.list.RegularSearchFragment;
73import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070074import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070075import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070076import com.android.internal.telephony.ITelephony;
77
Yorke Leec3766332013-07-31 11:13:16 -070078import java.util.ArrayList;
79
Chiao Cheng94b10b52012-08-17 16:59:12 -070080/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070081 * The dialer tab's title is 'phone', a more common name (see strings.xml).
82 */
Yorke Leec3766332013-07-31 11:13:16 -070083public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070084 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070085 OnListFragmentScrolledListener,
Yorke Lee8dd62002013-08-08 15:57:20 -070086 DialpadFragment.OnDialpadFragmentStartedListener,
87 PhoneFavoriteFragment.OnShowAllContactsListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070088 private static final String TAG = "DialtactsActivity";
89
90 public static final boolean DEBUG = false;
91
Yorke Leef74011e2013-08-02 11:30:30 -070092 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
93
Chiao Cheng94b10b52012-08-17 16:59:12 -070094 /** Used to open Call Setting */
95 private static final String PHONE_PACKAGE = "com.android.phone";
96 private static final String CALL_SETTINGS_CLASS_NAME =
97 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -070098 /** @see #getCallOrigin() */
99 private static final String CALL_ORIGIN_DIALTACTS =
100 "com.android.dialer.DialtactsActivity";
101
Yorke Leeb207f8a2013-08-29 18:51:06 -0700102 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
103 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700104 private static final String KEY_SEARCH_QUERY = "search_query";
105 private static final String KEY_FIRST_LAUNCH = "first_launch";
106
Yorke Leec3766332013-07-31 11:13:16 -0700107 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
108 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
109 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
110 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700111
Chiao Cheng94b10b52012-08-17 16:59:12 -0700112 /**
113 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
114 */
115 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
116
Chiao Cheng94b10b52012-08-17 16:59:12 -0700117 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
118
Yorke Leec3766332013-07-31 11:13:16 -0700119 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700120
121 private String mFilterText;
122
Yorke Leec3766332013-07-31 11:13:16 -0700123 /**
124 * The main fragment displaying the user's favorites and frequent contacts
125 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700126 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700127
128 /**
Yorke Leec3766332013-07-31 11:13:16 -0700129 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700130 */
Yorke Leef74011e2013-08-02 11:30:30 -0700131 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700132
133 /**
134 * Fragment for searching phone numbers using the alphanumeric keyboard.
135 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700136 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700137
138 /**
139 * Fragment for searching phone numbers using the dialpad.
140 */
141 private SmartDialSearchFragment mSmartDialSearchFragment;
142
Yorke Leec3766332013-07-31 11:13:16 -0700143 private View mMenuButton;
144 private View mCallHistoryButton;
145 private View mDialpadButton;
Yorke Lee36c54132013-09-25 16:17:06 -0700146 private PopupMenu mOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700147
148 // Padding view used to shift the fragments up when the dialpad is shown.
149 private View mBottomPaddingView;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700150 private View mFragmentsFrame;
Yorke Lee5f3ad542013-10-08 08:14:36 -0700151 private View mActionBar;
Yorke Leec3766332013-07-31 11:13:16 -0700152
Yorke Leeb207f8a2013-08-29 18:51:06 -0700153 private boolean mInDialpadSearch;
154 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700155 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700156
Yorke Leeef2b7382013-08-09 17:39:25 -0700157 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700158 * True if the dialpad is only temporarily showing due to being in call
159 */
160 private boolean mInCallDialpadUp;
161
162 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700163 * True when this activity has been launched for the first time.
164 */
Yorke Lee98702de2013-08-06 12:03:32 -0700165 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700166 private View mSearchViewContainer;
167 private View mSearchViewCloseButton;
168 private View mVoiceSearchButton;
169 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700170
Yorke Leeef2b7382013-08-09 17:39:25 -0700171 private String mSearchQuery;
172
Yorke Leefce269a2013-08-12 11:56:04 -0700173 private DialerDatabaseHelper mDialerDatabaseHelper;
174
Yorke Lee36c54132013-09-25 16:17:06 -0700175 private class OverflowPopupMenu extends PopupMenu {
176 public OverflowPopupMenu(Context context, View anchor) {
177 super(context, anchor);
178 }
179
180 @Override
181 public void show() {
182 final Menu menu = getMenu();
183 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
184 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
185 super.show();
186 }
187 }
188
Chiao Cheng94b10b52012-08-17 16:59:12 -0700189 /**
190 * Listener used when one of phone numbers in search UI is selected. This will initiate a
191 * phone call using the phone number.
192 */
193 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
194 new OnPhoneNumberPickerActionListener() {
195 @Override
196 public void onPickPhoneNumberAction(Uri dataUri) {
197 // Specify call-origin so that users will see the previous tab instead of
198 // CallLog screen (search UI will be automatically exited).
199 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700200 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700201 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700202 }
203
204 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700205 public void onCallNumberDirectly(String phoneNumber) {
206 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
207 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700208 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700209 }
210
211 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700212 public void onShortcutIntentCreated(Intent intent) {
213 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
214 }
215
216 @Override
217 public void onHomeInActionBarSelected() {
218 exitSearchUi();
219 }
220 };
221
222 /**
223 * Listener used to send search queries to the phone search fragment.
224 */
Yorke Leec3766332013-07-31 11:13:16 -0700225 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
226 @Override
227 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
228 }
229
230 @Override
231 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700232 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700233 if (newText.equals(mSearchQuery)) {
234 // If the query hasn't changed (perhaps due to activity being destroyed
235 // and restored, or user launching the same DIAL intent twice), then there is
236 // no need to do anything here.
237 return;
238 }
239 mSearchQuery = newText;
240 if (DEBUG) {
241 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
242 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700243 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700244
Yorke Leec3766332013-07-31 11:13:16 -0700245 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700246 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700247 exitSearchUi();
248 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700249 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700250 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700251 } else if (!TextUtils.isEmpty(newText)) {
252 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
253 (!dialpadSearch && mInRegularSearch);
254 if (!sameSearchMode) {
255 // call enterSearchUi only if we are switching search modes, or entering
256 // search ui for the first time
257 enterSearchUi(dialpadSearch, newText);
258 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700259
Yorke Leeb207f8a2013-08-29 18:51:06 -0700260 if (dialpadSearch && mSmartDialSearchFragment != null) {
261 mSmartDialSearchFragment.setQueryString(newText, false);
262 } else if (mRegularSearchFragment != null) {
263 mRegularSearchFragment.setQueryString(newText, false);
264 }
265 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700266 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700267 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700268 }
Yorke Leec3766332013-07-31 11:13:16 -0700269 }
270
271 @Override
272 public void afterTextChanged(Editable s) {
273 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700274 };
275
Yorke Leec3766332013-07-31 11:13:16 -0700276 private boolean isDialpadShowing() {
277 return mDialpadFragment != null && mDialpadFragment.isVisible();
278 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700279
280 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700281 protected void onCreate(Bundle savedInstanceState) {
282 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700283 mFirstLaunch = true;
284
Chiao Cheng94b10b52012-08-17 16:59:12 -0700285 final Intent intent = getIntent();
286 fixIntent(intent);
287
Yorke Lee8898cd02013-08-08 10:24:27 -0700288 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700289
Yorke Leec3766332013-07-31 11:13:16 -0700290 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291
Yorke Leeef2b7382013-08-09 17:39:25 -0700292 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
293 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700294 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700295 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700296
Yorke Leec3766332013-07-31 11:13:16 -0700297 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700298 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
299 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700300 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700301 } else {
302 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700303 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
304 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700305 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700306 }
307
Yorke Leec3766332013-07-31 11:13:16 -0700308 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700309 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700310 mActionBar = findViewById(R.id.fake_action_bar);
Yorke Leec3766332013-07-31 11:13:16 -0700311 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700312
Chiao Cheng94b10b52012-08-17 16:59:12 -0700313 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700314 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700315 setupFilterText(intent);
316 }
Yorke Leefce269a2013-08-12 11:56:04 -0700317
Yorke Leeb207f8a2013-08-29 18:51:06 -0700318 setupFakeActionBarItems();
319
Yorke Lee0baa98b2013-08-22 10:55:16 -0700320 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700321 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700322 }
323
324 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700325 protected void onResume() {
326 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700327 if (mFirstLaunch) {
328 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700329 } else if (!phoneIsInUse() && mInCallDialpadUp) {
330 hideDialpadFragment(false, true);
331 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700332 }
Yorke Lee35127cd2013-09-10 14:09:29 -0700333
Yorke Lee98702de2013-08-06 12:03:32 -0700334 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700335 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700336 }
337
338 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700339 protected void onPause() {
340 if (mClearSearchOnPause) {
341 hideDialpadAndSearchUi();
342 mClearSearchOnPause = false;
343 }
344 super.onPause();
345 }
346
347 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700348 protected void onSaveInstanceState(Bundle outState) {
349 super.onSaveInstanceState(outState);
350 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700351 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
352 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700353 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
354 }
355
356 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700357 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700358 if (fragment instanceof DialpadFragment) {
359 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700360 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700361 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700362 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700363 } else if (fragment instanceof SmartDialSearchFragment) {
364 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700365 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
366 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700367 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700368 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700369 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
370 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700371 } else if (fragment instanceof PhoneFavoriteFragment) {
372 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700373 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700374 }
Yorke Leec3766332013-07-31 11:13:16 -0700375 }
376
377 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700378 public boolean onMenuItemClick(MenuItem item) {
379 switch (item.getItemId()) {
380 case R.id.menu_import_export:
381 // We hard-code the "contactsAreAvailable" argument because doing it properly would
382 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
383 // now in Dialtacts for (potential) performance reasons. Compare with how it is
384 // done in {@link PeopleActivity}.
385 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700386 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700387 return true;
388 case R.id.menu_clear_frequents:
389 ClearFrequentsDialog.show(getFragmentManager());
390 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700391 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700392 try {
393 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
394 } catch (ActivityNotFoundException e) {
395 Toast toast = Toast.makeText(this,
396 R.string.add_contact_not_available,
397 Toast.LENGTH_SHORT);
398 toast.show();
399 }
400 return true;
401 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700402 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700403 return true;
404 case R.id.menu_all_contacts:
405 onShowAllContacts();
406 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700407 }
408 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700409 }
410
Alon Albertb453e5b2013-09-10 15:54:23 -0700411 protected void handleMenuSettings() {
412 openTelephonySetting(this);
413 }
414
415 public static void openTelephonySetting(Activity activity) {
416 final Intent settingsIntent = getCallSettingsIntent();
417 activity.startActivity(settingsIntent);
418 }
419
Chiao Cheng94b10b52012-08-17 16:59:12 -0700420 @Override
421 public void onClick(View view) {
422 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700423 case R.id.overflow_menu: {
Yorke Lee36c54132013-09-25 16:17:06 -0700424 mOverflowMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700425 break;
426 }
Yorke Leec3766332013-07-31 11:13:16 -0700427 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700428 // Reset the boolean flag that tracks whether the dialpad was up because
429 // we were in call. Regardless of whether it was true before, we want to
430 // show the dialpad because the user has explicitly clicked the dialpad
431 // button.
432 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700433 showDialpadFragment(true);
434 break;
435 case R.id.call_history_on_dialpad_button:
436 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700437 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
438 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700439 final Intent intent = new Intent(this, CallLogActivity.class);
440 startActivity(intent);
441 break;
442 case R.id.search_close_button:
443 // Clear the search field
444 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700445 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700446 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700447 }
448 break;
Yorke Leec3766332013-07-31 11:13:16 -0700449 case R.id.voice_search_button:
450 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
451 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
452 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700453 default: {
454 Log.wtf(TAG, "Unexpected onClick event from " + view);
455 break;
456 }
457 }
458 }
459
Yorke Leec3766332013-07-31 11:13:16 -0700460 @Override
461 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
462 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
463 if (resultCode == RESULT_OK) {
464 final ArrayList<String> matches = data.getStringArrayListExtra(
465 RecognizerIntent.EXTRA_RESULTS);
466 if (matches.size() > 0) {
467 final String match = matches.get(0);
468 mSearchView.setText(match);
469 } else {
470 Log.e(TAG, "Voice search - nothing heard");
471 }
472 } else {
473 Log.e(TAG, "Voice search failed");
474 }
475 }
476 super.onActivityResult(requestCode, resultCode, data);
477 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700478
Yorke Leec3766332013-07-31 11:13:16 -0700479 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700480 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700481 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700482 if (animate) {
483 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700484 } else {
485 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700486 }
487 ft.show(mDialpadFragment);
488 ft.commit();
489 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700490
Yorke Leeca195042013-09-25 16:29:38 -0700491 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700492 if (mDialpadFragment == null) return;
493 if (clearDialpad) {
494 mDialpadFragment.clearDialpad();
495 }
496 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700497 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700498 final FragmentTransaction ft = getFragmentManager().beginTransaction();
499 if (animate) {
500 ft.setCustomAnimations(0, R.anim.slide_out);
501 }
502 ft.hide(mDialpadFragment);
503 ft.commit();
504 }
505
Chiao Cheng94b10b52012-08-17 16:59:12 -0700506 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700507 mSearchViewContainer = findViewById(R.id.search_view_container);
508 mSearchViewCloseButton = findViewById(R.id.search_close_button);
509 mSearchViewCloseButton.setOnClickListener(this);
510 mVoiceSearchButton = findViewById(R.id.voice_search_button);
511 mVoiceSearchButton.setOnClickListener(this);
512 mSearchView = (EditText) findViewById(R.id.search_view);
513 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Yorke Lee41a24b42013-11-13 16:14:41 -0800514
515 final String hintText = getString(R.string.dialer_hint_find_contact);
516
517 // The following code is used to insert an icon into a CharSequence (copied from
518 // SearchView)
519 final SpannableStringBuilder ssb = new SpannableStringBuilder(" "); // for the icon
520 ssb.append(hintText);
521 final Drawable searchIcon = getResources().getDrawable(R.drawable.ic_ab_search);
522 final int textSize = (int) (mSearchView.getTextSize() * 1.20);
523 searchIcon.setBounds(0, 0, textSize, textSize);
524 ssb.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
525
526 mSearchView.setHint(ssb);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700527 }
528
Yorke Leec3766332013-07-31 11:13:16 -0700529 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
530 @Override
531 public void onAnimationEnd(Animator animation) {
532 mSearchViewContainer.setVisibility(View.GONE);
533 }
534 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700535
Yorke Leeb207f8a2013-08-29 18:51:06 -0700536 private boolean getInSearchUi() {
537 return mInDialpadSearch || mInRegularSearch;
538 }
539
540 private void setNotInSearchUi() {
541 mInDialpadSearch = false;
542 mInRegularSearch = false;
543 }
544
Yorke Lee311969c2013-08-26 06:31:11 -0700545 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700546 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700547 hideDialpadFragment(false, true);
548 }
549
Yorke Leec3766332013-07-31 11:13:16 -0700550 public void hideSearchBar() {
551 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700552 }
553
Yorke Leec3766332013-07-31 11:13:16 -0700554 public void hideSearchBar(boolean shiftView) {
555 if (shiftView) {
556 mSearchViewContainer.animate().cancel();
557 mSearchViewContainer.setAlpha(1);
558 mSearchViewContainer.setTranslationY(0);
559 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
560 .setDuration(200).setListener(mHideListener);
561
Yorke Leeb207f8a2013-08-29 18:51:06 -0700562 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700563 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
564 new AnimatorListenerAdapter() {
565 @Override
566 public void onAnimationEnd(Animator animation) {
567 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700568 mFragmentsFrame.setTranslationY(0);
569 mActionBar.setVisibility(View.INVISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700570 }
571 });
572 } else {
573 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
Yorke Lee5f3ad542013-10-08 08:14:36 -0700574 mActionBar.setVisibility(View.INVISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700575 }
576 }
577
578 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700579 mSearchViewContainer.animate().cancel();
580 mSearchViewContainer.setAlpha(0);
581 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
582 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
583 .setListener(new AnimatorListenerAdapter() {
584 @Override
585 public void onAnimationStart(Animator animation) {
586 mSearchViewContainer.setVisibility(View.VISIBLE);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700587 mActionBar.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700588 }
589 });
590
Yorke Leeb207f8a2013-08-29 18:51:06 -0700591 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
592 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700593 .setListener(
594 new AnimatorListenerAdapter() {
595 @Override
596 public void onAnimationStart(Animator animation) {
597 mBottomPaddingView.setVisibility(View.GONE);
598 }
599 });
600 }
601
602
Yorke Leeb207f8a2013-08-29 18:51:06 -0700603 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700604 mMenuButton = findViewById(R.id.overflow_menu);
605 if (mMenuButton != null) {
606 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700607
608 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
609 final Menu menu = mOverflowMenu.getMenu();
610 mOverflowMenu.inflate(R.menu.dialtacts_options);
611 mOverflowMenu.setOnMenuItemClickListener(this);
612 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700613 }
614
615 mCallHistoryButton = findViewById(R.id.call_history_button);
616 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
617 mCallHistoryButton.setOnClickListener(this);
618
619 mDialpadButton = findViewById(R.id.dialpad_button);
620 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
621 mDialpadButton.setOnClickListener(this);
622 }
623
624 public void setupFakeActionBarItemsForDialpadFragment() {
625 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
626 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700627 }
628
629 private void fixIntent(Intent intent) {
630 // This should be cleaned up: the call key used to send an Intent
631 // that just said to go to the recent calls list. It now sends this
632 // abstract action, but this class hasn't been rewritten to deal with it.
633 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
634 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
635 intent.putExtra("call_key", true);
636 setIntent(intent);
637 }
638 }
639
Chiao Cheng94b10b52012-08-17 16:59:12 -0700640 /**
641 * Returns true if the intent is due to hitting the green send key (hardware call button:
642 * KEYCODE_CALL) while in a call.
643 *
644 * @param intent the intent that launched this activity
645 * @param recentCallsRequest true if the intent is requesting to view recent calls
646 * @return true if the intent is due to hitting the green send key while in a call
647 */
Yorke Leec3766332013-07-31 11:13:16 -0700648 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700649 // If there is a call in progress go to the call screen
650 if (recentCallsRequest) {
651 final boolean callKey = intent.getBooleanExtra("call_key", false);
652
653 try {
654 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
655 if (callKey && phone != null && phone.showCallScreen()) {
656 return true;
657 }
658 } catch (RemoteException e) {
659 Log.e(TAG, "Failed to handle send while in call", e);
660 }
661 }
662
663 return false;
664 }
665
666 /**
667 * Sets the current tab based on the intent's request type
668 *
669 * @param intent Intent that contains information about which tab should be selected
670 */
Yorke Leec3766332013-07-31 11:13:16 -0700671 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700672 // If we got here by hitting send and we're in call forward along to the in-call activity
673 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
674 getContentResolver()));
675 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
676 finish();
677 return;
678 }
679
Yorke Lee669b6082013-09-17 15:43:38 -0700680 if (mDialpadFragment != null) {
681 final boolean phoneIsInUse = phoneIsInUse();
682 if (phoneIsInUse || isDialIntent(intent)) {
683 mDialpadFragment.setStartedFromNewIntent(true);
684 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
685 mInCallDialpadUp = true;
686 }
687 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700688 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700689 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700690 }
691
692 @Override
693 public void onNewIntent(Intent newIntent) {
694 setIntent(newIntent);
695 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700696 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700697 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700698
Chiao Cheng94b10b52012-08-17 16:59:12 -0700699 invalidateOptionsMenu();
700 }
701
702 /** Returns true if the given intent contains a phone number to populate the dialer with */
703 private boolean isDialIntent(Intent intent) {
704 final String action = intent.getAction();
705 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
706 return true;
707 }
708 if (Intent.ACTION_VIEW.equals(action)) {
709 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700710 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700711 return true;
712 }
713 }
714 return false;
715 }
716
717 /**
718 * Returns an appropriate call origin for this Activity. May return null when no call origin
719 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
720 * for remembering the tab in which the user made a phone call, so the external app's DIAL
721 * request should not be counted.)
722 */
723 public String getCallOrigin() {
724 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
725 }
726
727 /**
728 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
729 * This text originally came from a FILTER_CONTACTS_ACTION intent received
730 * by this activity. The stored text will then be cleared after after this
731 * method returns.
732 *
733 * @return The stored filter text
734 */
735 public String getAndClearFilterText() {
736 String filterText = mFilterText;
737 mFilterText = null;
738 return filterText;
739 }
740
741 /**
742 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
743 * This is so child activities can check if they are supposed to display a filter.
744 *
745 * @param intent The intent received in {@link #onNewIntent(Intent)}
746 */
747 private void setupFilterText(Intent intent) {
748 // If the intent was relaunched from history, don't apply the filter text.
749 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
750 return;
751 }
752 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
753 if (filter != null && filter.length() > 0) {
754 mFilterText = filter;
755 }
756 }
757
Yorke Lee8898cd02013-08-08 10:24:27 -0700758 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
759 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700760 @Override
761 public void onContactSelected(Uri contactUri) {
762 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700763 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 }
765
766 @Override
767 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700768 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700769 startActivity(intent);
770 }
771 };
772
Yorke Leec3766332013-07-31 11:13:16 -0700773 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
774 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
775 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700776
777 @Override
778 public void startSearch(String initialQuery, boolean selectInitialQuery,
779 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700780 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700781 if (mInSearchUi) {
782 if (mSearchView.hasFocus()) {
783 showInputMethod(mSearchView.findFocus());
784 } else {
785 mSearchView.requestFocus();
786 }
787 } else {
788 enterSearchUi();
789 }
790 } else {
791 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
792 }
Yorke Leec3766332013-07-31 11:13:16 -0700793 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700794
795 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700796 final InputMethodManager imm = (InputMethodManager) getSystemService(
797 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700798 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700799 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700800 }
801 }
802
803 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700804 final InputMethodManager imm = (InputMethodManager) getSystemService(
805 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700806 if (imm != null && view != null) {
807 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
808 }
809 }
810
811 /**
Yorke Leec3766332013-07-31 11:13:16 -0700812 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700813 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700814 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700815 if (getFragmentManager().isDestroyed()) {
816 // Weird race condition where fragment is doing work after the activity is destroyed
817 // due to talkback being on (b/10209937). Just return since we can't do any
818 // constructive here.
819 return;
820 }
821
Yorke Leeef2b7382013-08-09 17:39:25 -0700822 if (DEBUG) {
823 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
824 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700825
Yorke Leec3766332013-07-31 11:13:16 -0700826 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
827 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700828
Yorke Leeef2b7382013-08-09 17:39:25 -0700829 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700830 if (mInDialpadSearch) {
831 transaction.remove(mSmartDialSearchFragment);
832 } else if (mInRegularSearch) {
833 transaction.remove(mRegularSearchFragment);
834 } else {
835 transaction.remove(mPhoneFavoriteFragment);
836 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700837
Yorke Leeb207f8a2013-08-29 18:51:06 -0700838 final String tag;
839 if (smartDialSearch) {
840 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
841 } else {
842 tag = TAG_REGULAR_SEARCH_FRAGMENT;
843 }
844 mInDialpadSearch = smartDialSearch;
845 mInRegularSearch = !smartDialSearch;
846
Yorke Leeef2b7382013-08-09 17:39:25 -0700847 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
848 if (fragment == null) {
849 if (smartDialSearch) {
850 fragment = new SmartDialSearchFragment();
851 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700852 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700853 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700854 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700855 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700856 transaction.addToBackStack(null);
857 fragment.setQueryString(query, false);
858 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700859 }
860
861 /**
Yorke Leec3766332013-07-31 11:13:16 -0700862 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700863 */
Yorke Leec3766332013-07-31 11:13:16 -0700864 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700865 // See related bug in enterSearchUI();
866 if (getFragmentManager().isDestroyed()) {
867 return;
868 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700869 // Go all the way back to the favorites fragment, regardless of how many times we
870 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700871 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
872 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700873 }
874
875 /** Returns an Intent to launch Call Settings screen */
876 public static Intent getCallSettingsIntent() {
877 final Intent intent = new Intent(Intent.ACTION_MAIN);
878 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
879 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
880 return intent;
881 }
882
883 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700884 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700885 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700886 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700887 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700888 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700889 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700890 } else if (isTaskRoot()) {
891 // Instead of stopping, simply push this to the back of the stack.
892 // This is only done when running at the top of the stack;
893 // otherwise, we have been launched by someone else so need to
894 // allow the user to go back to the caller.
895 moveTaskToBack(false);
896 } else {
897 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700898 }
Yorke Leec3766332013-07-31 11:13:16 -0700899 }
900
901 @Override
902 public void onDialpadQueryChanged(String query) {
903 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
904 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
905 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700906 if (DEBUG) {
907 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
908 }
909 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
910 // This callback can happen if the dialpad fragment is recreated because of
911 // activity destruction. In that case, don't update the search view because
912 // that would bring the user back to the search fragment regardless of the
913 // previous state of the application. Instead, just return here and let the
914 // fragment manager correctly figure out whatever fragment was last displayed.
915 return;
916 }
Yorke Leec3766332013-07-31 11:13:16 -0700917 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700918 }
919 }
Yorke Leec3766332013-07-31 11:13:16 -0700920
921 @Override
922 public void onListFragmentScrollStateChange(int scrollState) {
923 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700924 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700925 hideInputMethod(getCurrentFocus());
926 }
927 }
928
929 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700930 public void onDialpadFragmentStarted() {
931 setupFakeActionBarItemsForDialpadFragment();
932 }
933
934 private boolean phoneIsInUse() {
935 final TelephonyManager tm = (TelephonyManager) getSystemService(
936 Context.TELEPHONY_SERVICE);
937 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
938 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700939
940 @Override
941 public void onShowAllContacts() {
942 final Intent intent = new Intent(this, AllContactsActivity.class);
943 startActivity(intent);
944 }
Yorke Leee1424812013-09-04 18:24:48 -0700945
946 public static Intent getAddNumberToContactIntent(CharSequence text) {
947 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
948 intent.putExtra(Intents.Insert.PHONE, text);
949 intent.setType(Contacts.CONTENT_ITEM_TYPE);
950 return intent;
951 }
952
953 public static Intent getInsertContactWithNameIntent(CharSequence text) {
954 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
955 intent.putExtra(Intents.Insert.NAME, text);
956 return intent;
957 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700958}