blob: 5989e52289b38521652f44b92a5019d979a55de2 [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;
25import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080026import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070027import android.content.Context;
28import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
Yorke Lee41a24b42013-11-13 16:14:41 -080031import android.graphics.drawable.Drawable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070032import android.net.Uri;
33import android.os.Bundle;
34import android.os.RemoteException;
35import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036import android.provider.CallLog.Calls;
37import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070038import android.provider.ContactsContract.Intents;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070040import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070041import android.telephony.TelephonyManager;
42import android.text.Editable;
Yorke Lee41a24b42013-11-13 16:14:41 -080043import android.text.Spannable;
44import android.text.SpannableStringBuilder;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070046import android.text.TextWatcher;
Yorke Lee41a24b42013-11-13 16:14:41 -080047import android.text.style.ImageSpan;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.util.Log;
49import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.view.View;
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 Lee3cefcc62014-01-16 11:26:46 -080070import com.android.dialer.list.DragDropController;
71import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070073import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080074import com.android.dialer.list.PhoneFavoriteTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070075import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080076import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070077import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070078import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070079import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070080import com.android.internal.telephony.ITelephony;
81
Yorke Leec3766332013-07-31 11:13:16 -070082import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080083import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070084
Chiao Cheng94b10b52012-08-17 16:59:12 -070085/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070086 * The dialer tab's title is 'phone', a more common name (see strings.xml).
87 */
Yorke Leec3766332013-07-31 11:13:16 -070088public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070089 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070090 OnListFragmentScrolledListener,
Yorke Lee8dd62002013-08-08 15:57:20 -070091 DialpadFragment.OnDialpadFragmentStartedListener,
Yorke Lee3cefcc62014-01-16 11:26:46 -080092 PhoneFavoriteFragment.OnShowAllContactsListener,
93 PhoneFavoriteFragment.HostInterface,
94 OnDragDropListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070095 private static final String TAG = "DialtactsActivity";
96
97 public static final boolean DEBUG = false;
98
Yorke Leef74011e2013-08-02 11:30:30 -070099 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
100
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101 /** Used to open Call Setting */
102 private static final String PHONE_PACKAGE = "com.android.phone";
103 private static final String CALL_SETTINGS_CLASS_NAME =
104 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700105 /** @see #getCallOrigin() */
106 private static final String CALL_ORIGIN_DIALTACTS =
107 "com.android.dialer.DialtactsActivity";
108
Yorke Leeb207f8a2013-08-29 18:51:06 -0700109 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
110 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700111 private static final String KEY_SEARCH_QUERY = "search_query";
112 private static final String KEY_FIRST_LAUNCH = "first_launch";
113
Yorke Leec3766332013-07-31 11:13:16 -0700114 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
115 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
116 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
117 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700118
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119 /**
120 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
121 */
122 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
123
Chiao Cheng94b10b52012-08-17 16:59:12 -0700124 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
125
Yorke Leec3766332013-07-31 11:13:16 -0700126 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700127
Yorke Lee3cefcc62014-01-16 11:26:46 -0800128 private static final int FADE_ANIMATION_DURATION = 200;
129
Chiao Cheng94b10b52012-08-17 16:59:12 -0700130 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;
Yorke Lee36c54132013-09-25 16:17:06 -0700155 private PopupMenu mOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700156
157 // Padding view used to shift the fragments up when the dialpad is shown.
158 private View mBottomPaddingView;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700159 private View mFragmentsFrame;
Yorke Lee5f3ad542013-10-08 08:14:36 -0700160 private View mActionBar;
Yorke Leec3766332013-07-31 11:13:16 -0700161
Yorke Leeb207f8a2013-08-29 18:51:06 -0700162 private boolean mInDialpadSearch;
163 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700164 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700165
Yorke Leeef2b7382013-08-09 17:39:25 -0700166 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700167 * True if the dialpad is only temporarily showing due to being in call
168 */
169 private boolean mInCallDialpadUp;
170
171 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700172 * True when this activity has been launched for the first time.
173 */
Yorke Lee98702de2013-08-06 12:03:32 -0700174 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700175 private View mSearchViewContainer;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800176 private RemoveView mRemoveViewContainer;
Yorke Leec3766332013-07-31 11:13:16 -0700177 private View mSearchViewCloseButton;
178 private View mVoiceSearchButton;
179 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700180
Yorke Leeef2b7382013-08-09 17:39:25 -0700181 private String mSearchQuery;
182
Yorke Leefce269a2013-08-12 11:56:04 -0700183 private DialerDatabaseHelper mDialerDatabaseHelper;
184
Yorke Lee36c54132013-09-25 16:17:06 -0700185 private class OverflowPopupMenu extends PopupMenu {
186 public OverflowPopupMenu(Context context, View anchor) {
187 super(context, anchor);
188 }
189
190 @Override
191 public void show() {
192 final Menu menu = getMenu();
193 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
194 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
195 super.show();
196 }
197 }
198
Chiao Cheng94b10b52012-08-17 16:59:12 -0700199 /**
200 * Listener used when one of phone numbers in search UI is selected. This will initiate a
201 * phone call using the phone number.
202 */
203 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
204 new OnPhoneNumberPickerActionListener() {
205 @Override
206 public void onPickPhoneNumberAction(Uri dataUri) {
207 // Specify call-origin so that users will see the previous tab instead of
208 // CallLog screen (search UI will be automatically exited).
209 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700210 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700211 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700212 }
213
214 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700215 public void onCallNumberDirectly(String phoneNumber) {
216 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
217 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700218 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700219 }
220
221 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700222 public void onShortcutIntentCreated(Intent intent) {
223 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
224 }
225
226 @Override
227 public void onHomeInActionBarSelected() {
228 exitSearchUi();
229 }
230 };
231
232 /**
233 * Listener used to send search queries to the phone search fragment.
234 */
Yorke Leec3766332013-07-31 11:13:16 -0700235 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
236 @Override
237 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
238 }
239
240 @Override
241 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700242 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700243 if (newText.equals(mSearchQuery)) {
244 // If the query hasn't changed (perhaps due to activity being destroyed
245 // and restored, or user launching the same DIAL intent twice), then there is
246 // no need to do anything here.
247 return;
248 }
249 mSearchQuery = newText;
250 if (DEBUG) {
251 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
252 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700253 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700254
Yorke Leec3766332013-07-31 11:13:16 -0700255 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700256 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700257 exitSearchUi();
258 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700259 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700260 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700261 } else if (!TextUtils.isEmpty(newText)) {
262 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
263 (!dialpadSearch && mInRegularSearch);
264 if (!sameSearchMode) {
265 // call enterSearchUi only if we are switching search modes, or entering
266 // search ui for the first time
267 enterSearchUi(dialpadSearch, newText);
268 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700269
Yorke Leeb207f8a2013-08-29 18:51:06 -0700270 if (dialpadSearch && mSmartDialSearchFragment != null) {
271 mSmartDialSearchFragment.setQueryString(newText, false);
272 } else if (mRegularSearchFragment != null) {
273 mRegularSearchFragment.setQueryString(newText, false);
274 }
275 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700276 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700277 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700278 }
Yorke Leec3766332013-07-31 11:13:16 -0700279 }
280
281 @Override
282 public void afterTextChanged(Editable s) {
283 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700284 };
285
Yorke Leec3766332013-07-31 11:13:16 -0700286 private boolean isDialpadShowing() {
287 return mDialpadFragment != null && mDialpadFragment.isVisible();
288 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700289
290 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700291 protected void onCreate(Bundle savedInstanceState) {
292 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700293 mFirstLaunch = true;
294
Chiao Cheng94b10b52012-08-17 16:59:12 -0700295 final Intent intent = getIntent();
296 fixIntent(intent);
297
Yorke Lee8898cd02013-08-08 10:24:27 -0700298 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700299
Yorke Leec3766332013-07-31 11:13:16 -0700300 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700301
Yorke Leeef2b7382013-08-09 17:39:25 -0700302 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
303 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700304 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700305 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700306
Yorke Leec3766332013-07-31 11:13:16 -0700307 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700308 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
309 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700310 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700311 } else {
312 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700313 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
314 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700315 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700316 }
317
Yorke Leec3766332013-07-31 11:13:16 -0700318 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700319 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700320 mActionBar = findViewById(R.id.fake_action_bar);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800321 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leec3766332013-07-31 11:13:16 -0700322 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700323
Chiao Cheng94b10b52012-08-17 16:59:12 -0700324 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700325 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700326 setupFilterText(intent);
327 }
Yorke Leefce269a2013-08-12 11:56:04 -0700328
Yorke Leeb207f8a2013-08-29 18:51:06 -0700329 setupFakeActionBarItems();
330
Yorke Lee0baa98b2013-08-22 10:55:16 -0700331 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700332 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700333 }
334
335 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700336 protected void onResume() {
337 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700338 if (mFirstLaunch) {
339 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700340 } else if (!phoneIsInUse() && mInCallDialpadUp) {
341 hideDialpadFragment(false, true);
342 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700343 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800344 prepareVoiceSearchButton();
Yorke Lee98702de2013-08-06 12:03:32 -0700345 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700346 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700347 }
348
349 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700350 protected void onPause() {
351 if (mClearSearchOnPause) {
352 hideDialpadAndSearchUi();
353 mClearSearchOnPause = false;
354 }
355 super.onPause();
356 }
357
358 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700359 protected void onSaveInstanceState(Bundle outState) {
360 super.onSaveInstanceState(outState);
361 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700362 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
363 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700364 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
365 }
366
367 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700368 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700369 if (fragment instanceof DialpadFragment) {
370 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700371 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700372 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700373 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700374 } else if (fragment instanceof SmartDialSearchFragment) {
375 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700376 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
377 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700378 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700379 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700380 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
381 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700382 } else if (fragment instanceof PhoneFavoriteFragment) {
383 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700384 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700385 }
Yorke Leec3766332013-07-31 11:13:16 -0700386 }
387
388 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700389 public boolean onMenuItemClick(MenuItem item) {
390 switch (item.getItemId()) {
391 case R.id.menu_import_export:
392 // We hard-code the "contactsAreAvailable" argument because doing it properly would
393 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
394 // now in Dialtacts for (potential) performance reasons. Compare with how it is
395 // done in {@link PeopleActivity}.
396 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700397 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700398 return true;
399 case R.id.menu_clear_frequents:
400 ClearFrequentsDialog.show(getFragmentManager());
401 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700402 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700403 try {
404 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
405 } catch (ActivityNotFoundException e) {
406 Toast toast = Toast.makeText(this,
407 R.string.add_contact_not_available,
408 Toast.LENGTH_SHORT);
409 toast.show();
410 }
411 return true;
412 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700413 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700414 return true;
415 case R.id.menu_all_contacts:
416 onShowAllContacts();
417 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700418 }
419 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700420 }
421
Alon Albertb453e5b2013-09-10 15:54:23 -0700422 protected void handleMenuSettings() {
423 openTelephonySetting(this);
424 }
425
426 public static void openTelephonySetting(Activity activity) {
427 final Intent settingsIntent = getCallSettingsIntent();
428 activity.startActivity(settingsIntent);
429 }
430
Chiao Cheng94b10b52012-08-17 16:59:12 -0700431 @Override
432 public void onClick(View view) {
433 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700434 case R.id.overflow_menu: {
Yorke Lee36c54132013-09-25 16:17:06 -0700435 mOverflowMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700436 break;
437 }
Yorke Leec3766332013-07-31 11:13:16 -0700438 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700439 // Reset the boolean flag that tracks whether the dialpad was up because
440 // we were in call. Regardless of whether it was true before, we want to
441 // show the dialpad because the user has explicitly clicked the dialpad
442 // button.
443 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700444 showDialpadFragment(true);
445 break;
446 case R.id.call_history_on_dialpad_button:
447 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700448 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
449 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700450 final Intent intent = new Intent(this, CallLogActivity.class);
451 startActivity(intent);
452 break;
453 case R.id.search_close_button:
454 // Clear the search field
455 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700456 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700457 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700458 }
459 break;
Yorke Leec3766332013-07-31 11:13:16 -0700460 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800461 try {
462 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
463 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
464 } catch (ActivityNotFoundException e) {
465 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
466 Toast.LENGTH_SHORT).show();
467 }
Yorke Leec3766332013-07-31 11:13:16 -0700468 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700469 default: {
470 Log.wtf(TAG, "Unexpected onClick event from " + view);
471 break;
472 }
473 }
474 }
475
Yorke Leec3766332013-07-31 11:13:16 -0700476 @Override
477 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
478 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
479 if (resultCode == RESULT_OK) {
480 final ArrayList<String> matches = data.getStringArrayListExtra(
481 RecognizerIntent.EXTRA_RESULTS);
482 if (matches.size() > 0) {
483 final String match = matches.get(0);
484 mSearchView.setText(match);
485 } else {
486 Log.e(TAG, "Voice search - nothing heard");
487 }
488 } else {
489 Log.e(TAG, "Voice search failed");
490 }
491 }
492 super.onActivityResult(requestCode, resultCode, data);
493 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700494
Yorke Leec3766332013-07-31 11:13:16 -0700495 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700496 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700497 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700498 if (animate) {
499 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700500 } else {
501 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700502 }
503 ft.show(mDialpadFragment);
504 ft.commit();
505 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700506
Yorke Leeca195042013-09-25 16:29:38 -0700507 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700508 if (mDialpadFragment == null) return;
509 if (clearDialpad) {
510 mDialpadFragment.clearDialpad();
511 }
512 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700513 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700514 final FragmentTransaction ft = getFragmentManager().beginTransaction();
515 if (animate) {
516 ft.setCustomAnimations(0, R.anim.slide_out);
517 }
518 ft.hide(mDialpadFragment);
519 ft.commit();
520 }
521
Chiao Cheng94b10b52012-08-17 16:59:12 -0700522 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700523 mSearchViewContainer = findViewById(R.id.search_view_container);
524 mSearchViewCloseButton = findViewById(R.id.search_close_button);
525 mSearchViewCloseButton.setOnClickListener(this);
Yorke Leeda0f9042013-12-05 14:25:51 -0800526
Yorke Leec3766332013-07-31 11:13:16 -0700527 mSearchView = (EditText) findViewById(R.id.search_view);
528 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Tyler Gunna8ff9752014-02-24 11:57:57 -0800529 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
Yorke Leeda0f9042013-12-05 14:25:51 -0800530
531 prepareVoiceSearchButton();
532 }
533
534 private void prepareVoiceSearchButton() {
535 mVoiceSearchButton = findViewById(R.id.voice_search_button);
536 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
537 if (canIntentBeHandled(voiceIntent)) {
538 mVoiceSearchButton.setVisibility(View.VISIBLE);
539 mVoiceSearchButton.setOnClickListener(this);
540 } else {
541 mVoiceSearchButton.setVisibility(View.GONE);
542 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700543 }
544
Yorke Leec3766332013-07-31 11:13:16 -0700545 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
546 @Override
547 public void onAnimationEnd(Animator animation) {
548 mSearchViewContainer.setVisibility(View.GONE);
549 }
550 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700551
Yorke Leeb207f8a2013-08-29 18:51:06 -0700552 private boolean getInSearchUi() {
553 return mInDialpadSearch || mInRegularSearch;
554 }
555
556 private void setNotInSearchUi() {
557 mInDialpadSearch = false;
558 mInRegularSearch = false;
559 }
560
Yorke Lee311969c2013-08-26 06:31:11 -0700561 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700562 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700563 hideDialpadFragment(false, true);
564 }
565
Yorke Leec3766332013-07-31 11:13:16 -0700566 public void hideSearchBar() {
567 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700568 }
569
Yorke Leec3766332013-07-31 11:13:16 -0700570 public void hideSearchBar(boolean shiftView) {
571 if (shiftView) {
572 mSearchViewContainer.animate().cancel();
573 mSearchViewContainer.setAlpha(1);
574 mSearchViewContainer.setTranslationY(0);
575 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
576 .setDuration(200).setListener(mHideListener);
577
Yorke Leeb207f8a2013-08-29 18:51:06 -0700578 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700579 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
580 new AnimatorListenerAdapter() {
581 @Override
582 public void onAnimationEnd(Animator animation) {
583 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700584 mFragmentsFrame.setTranslationY(0);
585 mActionBar.setVisibility(View.INVISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700586 }
587 });
588 } else {
589 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
Yorke Lee5f3ad542013-10-08 08:14:36 -0700590 mActionBar.setVisibility(View.INVISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700591 }
592 }
593
594 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700595 mSearchViewContainer.animate().cancel();
596 mSearchViewContainer.setAlpha(0);
597 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
598 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
599 .setListener(new AnimatorListenerAdapter() {
600 @Override
601 public void onAnimationStart(Animator animation) {
602 mSearchViewContainer.setVisibility(View.VISIBLE);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700603 mActionBar.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700604 }
605 });
606
Yorke Leeb207f8a2013-08-29 18:51:06 -0700607 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
608 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700609 .setListener(
610 new AnimatorListenerAdapter() {
611 @Override
612 public void onAnimationStart(Animator animation) {
613 mBottomPaddingView.setVisibility(View.GONE);
614 }
615 });
616 }
617
618
Yorke Leeb207f8a2013-08-29 18:51:06 -0700619 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700620 mMenuButton = findViewById(R.id.overflow_menu);
621 if (mMenuButton != null) {
622 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700623
624 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
625 final Menu menu = mOverflowMenu.getMenu();
626 mOverflowMenu.inflate(R.menu.dialtacts_options);
627 mOverflowMenu.setOnMenuItemClickListener(this);
628 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700629 }
630
631 mCallHistoryButton = findViewById(R.id.call_history_button);
632 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
633 mCallHistoryButton.setOnClickListener(this);
634
635 mDialpadButton = findViewById(R.id.dialpad_button);
636 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
637 mDialpadButton.setOnClickListener(this);
638 }
639
640 public void setupFakeActionBarItemsForDialpadFragment() {
641 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
642 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700643 }
644
645 private void fixIntent(Intent intent) {
646 // This should be cleaned up: the call key used to send an Intent
647 // that just said to go to the recent calls list. It now sends this
648 // abstract action, but this class hasn't been rewritten to deal with it.
649 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
650 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
651 intent.putExtra("call_key", true);
652 setIntent(intent);
653 }
654 }
655
Chiao Cheng94b10b52012-08-17 16:59:12 -0700656 /**
657 * Returns true if the intent is due to hitting the green send key (hardware call button:
658 * KEYCODE_CALL) while in a call.
659 *
660 * @param intent the intent that launched this activity
661 * @param recentCallsRequest true if the intent is requesting to view recent calls
662 * @return true if the intent is due to hitting the green send key while in a call
663 */
Yorke Leec3766332013-07-31 11:13:16 -0700664 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700665 // If there is a call in progress go to the call screen
666 if (recentCallsRequest) {
667 final boolean callKey = intent.getBooleanExtra("call_key", false);
668
669 try {
670 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
671 if (callKey && phone != null && phone.showCallScreen()) {
672 return true;
673 }
674 } catch (RemoteException e) {
675 Log.e(TAG, "Failed to handle send while in call", e);
676 }
677 }
678
679 return false;
680 }
681
682 /**
683 * Sets the current tab based on the intent's request type
684 *
685 * @param intent Intent that contains information about which tab should be selected
686 */
Yorke Leec3766332013-07-31 11:13:16 -0700687 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700688 // If we got here by hitting send and we're in call forward along to the in-call activity
689 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
690 getContentResolver()));
691 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
692 finish();
693 return;
694 }
695
Yorke Lee669b6082013-09-17 15:43:38 -0700696 if (mDialpadFragment != null) {
697 final boolean phoneIsInUse = phoneIsInUse();
698 if (phoneIsInUse || isDialIntent(intent)) {
699 mDialpadFragment.setStartedFromNewIntent(true);
700 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
701 mInCallDialpadUp = true;
702 }
703 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700704 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700705 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700706 }
707
708 @Override
709 public void onNewIntent(Intent newIntent) {
710 setIntent(newIntent);
711 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700712 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700713 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700714
Chiao Cheng94b10b52012-08-17 16:59:12 -0700715 invalidateOptionsMenu();
716 }
717
718 /** Returns true if the given intent contains a phone number to populate the dialer with */
719 private boolean isDialIntent(Intent intent) {
720 final String action = intent.getAction();
721 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
722 return true;
723 }
724 if (Intent.ACTION_VIEW.equals(action)) {
725 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700726 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700727 return true;
728 }
729 }
730 return false;
731 }
732
733 /**
734 * Returns an appropriate call origin for this Activity. May return null when no call origin
735 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
736 * for remembering the tab in which the user made a phone call, so the external app's DIAL
737 * request should not be counted.)
738 */
739 public String getCallOrigin() {
740 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
741 }
742
743 /**
744 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
745 * This text originally came from a FILTER_CONTACTS_ACTION intent received
746 * by this activity. The stored text will then be cleared after after this
747 * method returns.
748 *
749 * @return The stored filter text
750 */
751 public String getAndClearFilterText() {
752 String filterText = mFilterText;
753 mFilterText = null;
754 return filterText;
755 }
756
757 /**
758 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
759 * This is so child activities can check if they are supposed to display a filter.
760 *
761 * @param intent The intent received in {@link #onNewIntent(Intent)}
762 */
763 private void setupFilterText(Intent intent) {
764 // If the intent was relaunched from history, don't apply the filter text.
765 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
766 return;
767 }
768 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
769 if (filter != null && filter.length() > 0) {
770 mFilterText = filter;
771 }
772 }
773
Yorke Lee8898cd02013-08-08 10:24:27 -0700774 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
775 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700776 @Override
777 public void onContactSelected(Uri contactUri) {
778 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700779 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700780 }
781
782 @Override
783 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700784 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700785 startActivity(intent);
786 }
787 };
788
Yorke Leec3766332013-07-31 11:13:16 -0700789 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
790 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
791 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700792
793 @Override
794 public void startSearch(String initialQuery, boolean selectInitialQuery,
795 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700796 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700797 if (mInSearchUi) {
798 if (mSearchView.hasFocus()) {
799 showInputMethod(mSearchView.findFocus());
800 } else {
801 mSearchView.requestFocus();
802 }
803 } else {
804 enterSearchUi();
805 }
806 } else {
807 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
808 }
Yorke Leec3766332013-07-31 11:13:16 -0700809 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700810
811 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700812 final InputMethodManager imm = (InputMethodManager) getSystemService(
813 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700814 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700815 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700816 }
817 }
818
819 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700820 final InputMethodManager imm = (InputMethodManager) getSystemService(
821 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700822 if (imm != null && view != null) {
823 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
824 }
825 }
826
827 /**
Yorke Leec3766332013-07-31 11:13:16 -0700828 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700829 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700830 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700831 if (getFragmentManager().isDestroyed()) {
832 // Weird race condition where fragment is doing work after the activity is destroyed
833 // due to talkback being on (b/10209937). Just return since we can't do any
834 // constructive here.
835 return;
836 }
837
Yorke Leeef2b7382013-08-09 17:39:25 -0700838 if (DEBUG) {
839 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
840 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700841
Yorke Leec3766332013-07-31 11:13:16 -0700842 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
843 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700844
Yorke Leeef2b7382013-08-09 17:39:25 -0700845 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700846 if (mInDialpadSearch) {
847 transaction.remove(mSmartDialSearchFragment);
848 } else if (mInRegularSearch) {
849 transaction.remove(mRegularSearchFragment);
850 } else {
851 transaction.remove(mPhoneFavoriteFragment);
852 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700853
Yorke Leeb207f8a2013-08-29 18:51:06 -0700854 final String tag;
855 if (smartDialSearch) {
856 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
857 } else {
858 tag = TAG_REGULAR_SEARCH_FRAGMENT;
859 }
860 mInDialpadSearch = smartDialSearch;
861 mInRegularSearch = !smartDialSearch;
862
Yorke Leeef2b7382013-08-09 17:39:25 -0700863 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
864 if (fragment == null) {
865 if (smartDialSearch) {
866 fragment = new SmartDialSearchFragment();
867 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700868 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700869 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700870 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700871 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700872 transaction.addToBackStack(null);
873 fragment.setQueryString(query, false);
874 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700875 }
876
877 /**
Yorke Leec3766332013-07-31 11:13:16 -0700878 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700879 */
Yorke Leec3766332013-07-31 11:13:16 -0700880 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700881 // See related bug in enterSearchUI();
882 if (getFragmentManager().isDestroyed()) {
883 return;
884 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700885 // Go all the way back to the favorites fragment, regardless of how many times we
886 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700887 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
888 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700889 }
890
891 /** Returns an Intent to launch Call Settings screen */
892 public static Intent getCallSettingsIntent() {
893 final Intent intent = new Intent(Intent.ACTION_MAIN);
894 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
895 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
896 return intent;
897 }
898
899 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700900 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700901 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700902 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700903 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700904 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700905 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700906 } else {
907 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700908 }
Yorke Leec3766332013-07-31 11:13:16 -0700909 }
910
911 @Override
912 public void onDialpadQueryChanged(String query) {
913 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
914 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
915 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700916 if (DEBUG) {
917 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
918 }
919 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
920 // This callback can happen if the dialpad fragment is recreated because of
921 // activity destruction. In that case, don't update the search view because
922 // that would bring the user back to the search fragment regardless of the
923 // previous state of the application. Instead, just return here and let the
924 // fragment manager correctly figure out whatever fragment was last displayed.
925 return;
926 }
Yorke Leec3766332013-07-31 11:13:16 -0700927 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700928 }
929 }
Yorke Leec3766332013-07-31 11:13:16 -0700930
931 @Override
932 public void onListFragmentScrollStateChange(int scrollState) {
933 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700934 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700935 hideInputMethod(getCurrentFocus());
936 }
937 }
938
939 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700940 public void onDialpadFragmentStarted() {
941 setupFakeActionBarItemsForDialpadFragment();
942 }
943
944 private boolean phoneIsInUse() {
945 final TelephonyManager tm = (TelephonyManager) getSystemService(
946 Context.TELEPHONY_SERVICE);
947 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
948 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700949
950 @Override
951 public void onShowAllContacts() {
952 final Intent intent = new Intent(this, AllContactsActivity.class);
953 startActivity(intent);
954 }
Yorke Leee1424812013-09-04 18:24:48 -0700955
956 public static Intent getAddNumberToContactIntent(CharSequence text) {
957 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
958 intent.putExtra(Intents.Insert.PHONE, text);
959 intent.setType(Contacts.CONTENT_ITEM_TYPE);
960 return intent;
961 }
962
963 public static Intent getInsertContactWithNameIntent(CharSequence text) {
964 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
965 intent.putExtra(Intents.Insert.NAME, text);
966 return intent;
967 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800968
969 private boolean canIntentBeHandled(Intent intent) {
970 final PackageManager packageManager = getPackageManager();
971 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
972 PackageManager.MATCH_DEFAULT_ONLY);
973 return resolveInfo != null && resolveInfo.size() > 0;
974 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800975
976 @Override
977 public void onDragStarted(int itemIndex, int x, int y, PhoneFavoriteTileView view) {
978 crossfadeViews(mRemoveViewContainer, mSearchViewContainer, FADE_ANIMATION_DURATION);
979 }
980
981 @Override
982 public void onDragHovered(int itemIndex, int x, int y) {}
983
984 @Override
985 public void onDragFinished(int x, int y) {
986 crossfadeViews(mSearchViewContainer, mRemoveViewContainer, FADE_ANIMATION_DURATION);
987 }
988
989 @Override
990 public void onDroppedOnRemove() {}
991
992 /**
993 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
994 * once it has been attached to the activity.
995 */
996 @Override
997 public void setDragDropController(DragDropController dragController) {
998 mRemoveViewContainer.setDragDropController(dragController);
999 }
1000
1001 /**
1002 * Crossfades two views so that the first one appears while the other one is fading
1003 * out of view.
1004 */
1005 private void crossfadeViews(final View fadeIn, final View fadeOut, int duration) {
1006 fadeOut.animate().alpha(0).setDuration(duration)
1007 .setListener(new AnimatorListenerAdapter() {
1008 @Override
1009 public void onAnimationEnd(Animator animation) {
1010 fadeOut.setVisibility(View.GONE);
1011 }
1012 });
1013
1014 fadeIn.setVisibility(View.VISIBLE);
1015 fadeIn.setAlpha(0);
1016 fadeIn.animate().alpha(1).setDuration(FADE_ANIMATION_DURATION)
1017 .setListener(null);
1018 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001019}