blob: 227cd8ecad5d3230fd7c429d0a8809048f2cc974 [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);
Yorke Lee41a24b42013-11-13 16:14:41 -0800529
530 final String hintText = getString(R.string.dialer_hint_find_contact);
531
532 // The following code is used to insert an icon into a CharSequence (copied from
533 // SearchView)
534 final SpannableStringBuilder ssb = new SpannableStringBuilder(" "); // for the icon
535 ssb.append(hintText);
536 final Drawable searchIcon = getResources().getDrawable(R.drawable.ic_ab_search);
537 final int textSize = (int) (mSearchView.getTextSize() * 1.20);
538 searchIcon.setBounds(0, 0, textSize, textSize);
539 ssb.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
540
541 mSearchView.setHint(ssb);
Yorke Leeda0f9042013-12-05 14:25:51 -0800542
543 prepareVoiceSearchButton();
544 }
545
546 private void prepareVoiceSearchButton() {
547 mVoiceSearchButton = findViewById(R.id.voice_search_button);
548 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
549 if (canIntentBeHandled(voiceIntent)) {
550 mVoiceSearchButton.setVisibility(View.VISIBLE);
551 mVoiceSearchButton.setOnClickListener(this);
552 } else {
553 mVoiceSearchButton.setVisibility(View.GONE);
554 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700555 }
556
Yorke Leec3766332013-07-31 11:13:16 -0700557 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
558 @Override
559 public void onAnimationEnd(Animator animation) {
560 mSearchViewContainer.setVisibility(View.GONE);
561 }
562 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700563
Yorke Leeb207f8a2013-08-29 18:51:06 -0700564 private boolean getInSearchUi() {
565 return mInDialpadSearch || mInRegularSearch;
566 }
567
568 private void setNotInSearchUi() {
569 mInDialpadSearch = false;
570 mInRegularSearch = false;
571 }
572
Yorke Lee311969c2013-08-26 06:31:11 -0700573 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700574 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700575 hideDialpadFragment(false, true);
576 }
577
Yorke Leec3766332013-07-31 11:13:16 -0700578 public void hideSearchBar() {
579 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700580 }
581
Yorke Leec3766332013-07-31 11:13:16 -0700582 public void hideSearchBar(boolean shiftView) {
583 if (shiftView) {
584 mSearchViewContainer.animate().cancel();
585 mSearchViewContainer.setAlpha(1);
586 mSearchViewContainer.setTranslationY(0);
587 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
588 .setDuration(200).setListener(mHideListener);
589
Yorke Leeb207f8a2013-08-29 18:51:06 -0700590 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700591 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
592 new AnimatorListenerAdapter() {
593 @Override
594 public void onAnimationEnd(Animator animation) {
595 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700596 mFragmentsFrame.setTranslationY(0);
597 mActionBar.setVisibility(View.INVISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700598 }
599 });
600 } else {
601 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
Yorke Lee5f3ad542013-10-08 08:14:36 -0700602 mActionBar.setVisibility(View.INVISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700603 }
604 }
605
606 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700607 mSearchViewContainer.animate().cancel();
608 mSearchViewContainer.setAlpha(0);
609 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
610 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
611 .setListener(new AnimatorListenerAdapter() {
612 @Override
613 public void onAnimationStart(Animator animation) {
614 mSearchViewContainer.setVisibility(View.VISIBLE);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700615 mActionBar.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700616 }
617 });
618
Yorke Leeb207f8a2013-08-29 18:51:06 -0700619 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
620 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700621 .setListener(
622 new AnimatorListenerAdapter() {
623 @Override
624 public void onAnimationStart(Animator animation) {
625 mBottomPaddingView.setVisibility(View.GONE);
626 }
627 });
628 }
629
630
Yorke Leeb207f8a2013-08-29 18:51:06 -0700631 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700632 mMenuButton = findViewById(R.id.overflow_menu);
633 if (mMenuButton != null) {
634 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700635
636 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
637 final Menu menu = mOverflowMenu.getMenu();
638 mOverflowMenu.inflate(R.menu.dialtacts_options);
639 mOverflowMenu.setOnMenuItemClickListener(this);
640 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700641 }
642
643 mCallHistoryButton = findViewById(R.id.call_history_button);
644 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
645 mCallHistoryButton.setOnClickListener(this);
646
647 mDialpadButton = findViewById(R.id.dialpad_button);
648 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
649 mDialpadButton.setOnClickListener(this);
650 }
651
652 public void setupFakeActionBarItemsForDialpadFragment() {
653 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
654 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700655 }
656
657 private void fixIntent(Intent intent) {
658 // This should be cleaned up: the call key used to send an Intent
659 // that just said to go to the recent calls list. It now sends this
660 // abstract action, but this class hasn't been rewritten to deal with it.
661 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
662 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
663 intent.putExtra("call_key", true);
664 setIntent(intent);
665 }
666 }
667
Chiao Cheng94b10b52012-08-17 16:59:12 -0700668 /**
669 * Returns true if the intent is due to hitting the green send key (hardware call button:
670 * KEYCODE_CALL) while in a call.
671 *
672 * @param intent the intent that launched this activity
673 * @param recentCallsRequest true if the intent is requesting to view recent calls
674 * @return true if the intent is due to hitting the green send key while in a call
675 */
Yorke Leec3766332013-07-31 11:13:16 -0700676 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700677 // If there is a call in progress go to the call screen
678 if (recentCallsRequest) {
679 final boolean callKey = intent.getBooleanExtra("call_key", false);
680
681 try {
682 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
683 if (callKey && phone != null && phone.showCallScreen()) {
684 return true;
685 }
686 } catch (RemoteException e) {
687 Log.e(TAG, "Failed to handle send while in call", e);
688 }
689 }
690
691 return false;
692 }
693
694 /**
695 * Sets the current tab based on the intent's request type
696 *
697 * @param intent Intent that contains information about which tab should be selected
698 */
Yorke Leec3766332013-07-31 11:13:16 -0700699 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700700 // If we got here by hitting send and we're in call forward along to the in-call activity
701 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
702 getContentResolver()));
703 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
704 finish();
705 return;
706 }
707
Yorke Lee669b6082013-09-17 15:43:38 -0700708 if (mDialpadFragment != null) {
709 final boolean phoneIsInUse = phoneIsInUse();
710 if (phoneIsInUse || isDialIntent(intent)) {
711 mDialpadFragment.setStartedFromNewIntent(true);
712 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
713 mInCallDialpadUp = true;
714 }
715 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700716 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700717 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700718 }
719
720 @Override
721 public void onNewIntent(Intent newIntent) {
722 setIntent(newIntent);
723 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700724 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700725 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700726
Chiao Cheng94b10b52012-08-17 16:59:12 -0700727 invalidateOptionsMenu();
728 }
729
730 /** Returns true if the given intent contains a phone number to populate the dialer with */
731 private boolean isDialIntent(Intent intent) {
732 final String action = intent.getAction();
733 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
734 return true;
735 }
736 if (Intent.ACTION_VIEW.equals(action)) {
737 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700738 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700739 return true;
740 }
741 }
742 return false;
743 }
744
745 /**
746 * Returns an appropriate call origin for this Activity. May return null when no call origin
747 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
748 * for remembering the tab in which the user made a phone call, so the external app's DIAL
749 * request should not be counted.)
750 */
751 public String getCallOrigin() {
752 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
753 }
754
755 /**
756 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
757 * This text originally came from a FILTER_CONTACTS_ACTION intent received
758 * by this activity. The stored text will then be cleared after after this
759 * method returns.
760 *
761 * @return The stored filter text
762 */
763 public String getAndClearFilterText() {
764 String filterText = mFilterText;
765 mFilterText = null;
766 return filterText;
767 }
768
769 /**
770 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
771 * This is so child activities can check if they are supposed to display a filter.
772 *
773 * @param intent The intent received in {@link #onNewIntent(Intent)}
774 */
775 private void setupFilterText(Intent intent) {
776 // If the intent was relaunched from history, don't apply the filter text.
777 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
778 return;
779 }
780 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
781 if (filter != null && filter.length() > 0) {
782 mFilterText = filter;
783 }
784 }
785
Yorke Lee8898cd02013-08-08 10:24:27 -0700786 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
787 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700788 @Override
789 public void onContactSelected(Uri contactUri) {
790 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700791 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700792 }
793
794 @Override
795 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700796 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700797 startActivity(intent);
798 }
799 };
800
Yorke Leec3766332013-07-31 11:13:16 -0700801 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
802 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
803 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700804
805 @Override
806 public void startSearch(String initialQuery, boolean selectInitialQuery,
807 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700808 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700809 if (mInSearchUi) {
810 if (mSearchView.hasFocus()) {
811 showInputMethod(mSearchView.findFocus());
812 } else {
813 mSearchView.requestFocus();
814 }
815 } else {
816 enterSearchUi();
817 }
818 } else {
819 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
820 }
Yorke Leec3766332013-07-31 11:13:16 -0700821 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700822
823 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700824 final InputMethodManager imm = (InputMethodManager) getSystemService(
825 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700826 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700827 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700828 }
829 }
830
831 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700832 final InputMethodManager imm = (InputMethodManager) getSystemService(
833 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700834 if (imm != null && view != null) {
835 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
836 }
837 }
838
839 /**
Yorke Leec3766332013-07-31 11:13:16 -0700840 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700841 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700842 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700843 if (getFragmentManager().isDestroyed()) {
844 // Weird race condition where fragment is doing work after the activity is destroyed
845 // due to talkback being on (b/10209937). Just return since we can't do any
846 // constructive here.
847 return;
848 }
849
Yorke Leeef2b7382013-08-09 17:39:25 -0700850 if (DEBUG) {
851 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
852 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700853
Yorke Leec3766332013-07-31 11:13:16 -0700854 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
855 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700856
Yorke Leeef2b7382013-08-09 17:39:25 -0700857 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700858 if (mInDialpadSearch) {
859 transaction.remove(mSmartDialSearchFragment);
860 } else if (mInRegularSearch) {
861 transaction.remove(mRegularSearchFragment);
862 } else {
863 transaction.remove(mPhoneFavoriteFragment);
864 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700865
Yorke Leeb207f8a2013-08-29 18:51:06 -0700866 final String tag;
867 if (smartDialSearch) {
868 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
869 } else {
870 tag = TAG_REGULAR_SEARCH_FRAGMENT;
871 }
872 mInDialpadSearch = smartDialSearch;
873 mInRegularSearch = !smartDialSearch;
874
Yorke Leeef2b7382013-08-09 17:39:25 -0700875 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
876 if (fragment == null) {
877 if (smartDialSearch) {
878 fragment = new SmartDialSearchFragment();
879 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700880 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700881 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700882 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700883 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700884 transaction.addToBackStack(null);
885 fragment.setQueryString(query, false);
886 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700887 }
888
889 /**
Yorke Leec3766332013-07-31 11:13:16 -0700890 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700891 */
Yorke Leec3766332013-07-31 11:13:16 -0700892 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700893 // See related bug in enterSearchUI();
894 if (getFragmentManager().isDestroyed()) {
895 return;
896 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700897 // Go all the way back to the favorites fragment, regardless of how many times we
898 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700899 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
900 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700901 }
902
903 /** Returns an Intent to launch Call Settings screen */
904 public static Intent getCallSettingsIntent() {
905 final Intent intent = new Intent(Intent.ACTION_MAIN);
906 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
907 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
908 return intent;
909 }
910
911 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700912 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700913 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700914 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700915 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700916 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700917 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700918 } else {
919 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700920 }
Yorke Leec3766332013-07-31 11:13:16 -0700921 }
922
923 @Override
924 public void onDialpadQueryChanged(String query) {
925 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
926 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
927 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700928 if (DEBUG) {
929 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
930 }
931 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
932 // This callback can happen if the dialpad fragment is recreated because of
933 // activity destruction. In that case, don't update the search view because
934 // that would bring the user back to the search fragment regardless of the
935 // previous state of the application. Instead, just return here and let the
936 // fragment manager correctly figure out whatever fragment was last displayed.
937 return;
938 }
Yorke Leec3766332013-07-31 11:13:16 -0700939 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700940 }
941 }
Yorke Leec3766332013-07-31 11:13:16 -0700942
943 @Override
944 public void onListFragmentScrollStateChange(int scrollState) {
945 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700946 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700947 hideInputMethod(getCurrentFocus());
948 }
949 }
950
951 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700952 public void onDialpadFragmentStarted() {
953 setupFakeActionBarItemsForDialpadFragment();
954 }
955
956 private boolean phoneIsInUse() {
957 final TelephonyManager tm = (TelephonyManager) getSystemService(
958 Context.TELEPHONY_SERVICE);
959 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
960 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700961
962 @Override
963 public void onShowAllContacts() {
964 final Intent intent = new Intent(this, AllContactsActivity.class);
965 startActivity(intent);
966 }
Yorke Leee1424812013-09-04 18:24:48 -0700967
968 public static Intent getAddNumberToContactIntent(CharSequence text) {
969 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
970 intent.putExtra(Intents.Insert.PHONE, text);
971 intent.setType(Contacts.CONTENT_ITEM_TYPE);
972 return intent;
973 }
974
975 public static Intent getInsertContactWithNameIntent(CharSequence text) {
976 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
977 intent.putExtra(Intents.Insert.NAME, text);
978 return intent;
979 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800980
981 private boolean canIntentBeHandled(Intent intent) {
982 final PackageManager packageManager = getPackageManager();
983 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
984 PackageManager.MATCH_DEFAULT_ONLY);
985 return resolveInfo != null && resolveInfo.size() > 0;
986 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800987
988 @Override
989 public void onDragStarted(int itemIndex, int x, int y, PhoneFavoriteTileView view) {
990 crossfadeViews(mRemoveViewContainer, mSearchViewContainer, FADE_ANIMATION_DURATION);
991 }
992
993 @Override
994 public void onDragHovered(int itemIndex, int x, int y) {}
995
996 @Override
997 public void onDragFinished(int x, int y) {
998 crossfadeViews(mSearchViewContainer, mRemoveViewContainer, FADE_ANIMATION_DURATION);
999 }
1000
1001 @Override
1002 public void onDroppedOnRemove() {}
1003
1004 /**
1005 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
1006 * once it has been attached to the activity.
1007 */
1008 @Override
1009 public void setDragDropController(DragDropController dragController) {
1010 mRemoveViewContainer.setDragDropController(dragController);
1011 }
1012
1013 /**
1014 * Crossfades two views so that the first one appears while the other one is fading
1015 * out of view.
1016 */
1017 private void crossfadeViews(final View fadeIn, final View fadeOut, int duration) {
1018 fadeOut.animate().alpha(0).setDuration(duration)
1019 .setListener(new AnimatorListenerAdapter() {
1020 @Override
1021 public void onAnimationEnd(Animator animation) {
1022 fadeOut.setVisibility(View.GONE);
1023 }
1024 });
1025
1026 fadeIn.setVisibility(View.VISIBLE);
1027 fadeIn.setAlpha(0);
1028 fadeIn.animate().alpha(1).setDuration(FADE_ANIMATION_DURATION)
1029 .setListener(null);
1030 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001031}