blob: 4542137fc167000ae4765970b7e736000763a8a4 [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;
Chiao Cheng94b10b52012-08-17 16:59:12 -070031import android.net.Uri;
32import android.os.Bundle;
33import android.os.RemoteException;
34import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.provider.CallLog.Calls;
36import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070037import android.provider.ContactsContract.Intents;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070039import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070040import android.telephony.TelephonyManager;
41import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070042import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070043import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.util.Log;
45import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070049import android.widget.AbsListView.OnScrollListener;
50import android.widget.EditText;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080052import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070054import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070055import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070056import com.android.contacts.common.dialog.ClearFrequentsDialog;
57import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080058import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070059import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070060import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070061import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070062import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070063import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070064import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070065import com.android.dialer.list.AllContactsActivity;
Yorke Lee3cefcc62014-01-16 11:26:46 -080066import com.android.dialer.list.DragDropController;
67import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070068import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070069import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080070import com.android.dialer.list.PhoneFavoriteTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070071import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080072import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070073import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070074import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070075import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070076import com.android.internal.telephony.ITelephony;
77
Yorke Leec3766332013-07-31 11:13:16 -070078import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080079import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070080
Chiao Cheng94b10b52012-08-17 16:59:12 -070081/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070082 * The dialer tab's title is 'phone', a more common name (see strings.xml).
83 */
Yorke Leec3766332013-07-31 11:13:16 -070084public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070085 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070086 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080087 DialpadFragment.HostInterface,
Yorke Lee3cefcc62014-01-16 11:26:46 -080088 PhoneFavoriteFragment.OnShowAllContactsListener,
89 PhoneFavoriteFragment.HostInterface,
Ihab Awad526c0b82014-02-27 12:55:36 -080090 OnDragDropListener, View.OnLongClickListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070091 private static final String TAG = "DialtactsActivity";
92
Ihab Awad526c0b82014-02-27 12:55:36 -080093 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -070094
Yorke Leef74011e2013-08-02 11:30:30 -070095 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
96
Chiao Cheng94b10b52012-08-17 16:59:12 -070097 /** Used to open Call Setting */
98 private static final String PHONE_PACKAGE = "com.android.phone";
99 private static final String CALL_SETTINGS_CLASS_NAME =
100 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101 /** @see #getCallOrigin() */
102 private static final String CALL_ORIGIN_DIALTACTS =
103 "com.android.dialer.DialtactsActivity";
104
Yorke Leeb207f8a2013-08-29 18:51:06 -0700105 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
106 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700107 private static final String KEY_SEARCH_QUERY = "search_query";
108 private static final String KEY_FIRST_LAUNCH = "first_launch";
109
Yorke Leec3766332013-07-31 11:13:16 -0700110 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
111 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
112 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
113 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700114
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 /**
116 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
117 */
118 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
119
Yorke Leec3766332013-07-31 11:13:16 -0700120 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121
Yorke Lee3cefcc62014-01-16 11:26:46 -0800122 private static final int FADE_ANIMATION_DURATION = 200;
123
Chiao Cheng94b10b52012-08-17 16:59:12 -0700124 private String mFilterText;
125
Yorke Leec3766332013-07-31 11:13:16 -0700126 /**
127 * The main fragment displaying the user's favorites and frequent contacts
128 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700129 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700130
131 /**
Yorke Leec3766332013-07-31 11:13:16 -0700132 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700133 */
Yorke Leef74011e2013-08-02 11:30:30 -0700134 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700135
136 /**
137 * Fragment for searching phone numbers using the alphanumeric keyboard.
138 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700139 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700140
141 /**
142 * Fragment for searching phone numbers using the dialpad.
143 */
144 private SmartDialSearchFragment mSmartDialSearchFragment;
145
Yorke Leec3766332013-07-31 11:13:16 -0700146 private View mMenuButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800147 private View mFakeActionBar;
Yorke Leec3766332013-07-31 11:13:16 -0700148 private View mCallHistoryButton;
149 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800150 private View mDialButton;
Yorke Lee36c54132013-09-25 16:17:06 -0700151 private PopupMenu mOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700152
153 // Padding view used to shift the fragments up when the dialpad is shown.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700154 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700155
Yorke Leeb207f8a2013-08-29 18:51:06 -0700156 private boolean mInDialpadSearch;
157 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700158 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700159
Yorke Leeef2b7382013-08-09 17:39:25 -0700160 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700161 * True if the dialpad is only temporarily showing due to being in call
162 */
163 private boolean mInCallDialpadUp;
164
165 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700166 * True when this activity has been launched for the first time.
167 */
Yorke Lee98702de2013-08-06 12:03:32 -0700168 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700169 private View mSearchViewContainer;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800170 private RemoveView mRemoveViewContainer;
Yorke Leec3766332013-07-31 11:13:16 -0700171 private View mSearchViewCloseButton;
172 private View mVoiceSearchButton;
173 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700174
Yorke Leeef2b7382013-08-09 17:39:25 -0700175 private String mSearchQuery;
176
Yorke Leefce269a2013-08-12 11:56:04 -0700177 private DialerDatabaseHelper mDialerDatabaseHelper;
178
Yorke Lee36c54132013-09-25 16:17:06 -0700179 private class OverflowPopupMenu extends PopupMenu {
180 public OverflowPopupMenu(Context context, View anchor) {
181 super(context, anchor);
182 }
183
184 @Override
185 public void show() {
186 final Menu menu = getMenu();
187 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
188 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
189 super.show();
190 }
191 }
192
Chiao Cheng94b10b52012-08-17 16:59:12 -0700193 /**
194 * Listener used when one of phone numbers in search UI is selected. This will initiate a
195 * phone call using the phone number.
196 */
197 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
198 new OnPhoneNumberPickerActionListener() {
199 @Override
200 public void onPickPhoneNumberAction(Uri dataUri) {
201 // Specify call-origin so that users will see the previous tab instead of
202 // CallLog screen (search UI will be automatically exited).
203 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700204 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700205 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700206 }
207
208 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700209 public void onCallNumberDirectly(String phoneNumber) {
210 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
211 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700212 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700213 }
214
215 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700216 public void onShortcutIntentCreated(Intent intent) {
217 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
218 }
219
220 @Override
221 public void onHomeInActionBarSelected() {
222 exitSearchUi();
223 }
224 };
225
226 /**
227 * Listener used to send search queries to the phone search fragment.
228 */
Yorke Leec3766332013-07-31 11:13:16 -0700229 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
230 @Override
231 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
232 }
233
234 @Override
235 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700236 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700237 if (newText.equals(mSearchQuery)) {
238 // If the query hasn't changed (perhaps due to activity being destroyed
239 // and restored, or user launching the same DIAL intent twice), then there is
240 // no need to do anything here.
241 return;
242 }
243 mSearchQuery = newText;
244 if (DEBUG) {
245 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
246 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700247 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700248
Yorke Leec3766332013-07-31 11:13:16 -0700249 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700250 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700251 exitSearchUi();
252 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700253 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700254 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700255 } else if (!TextUtils.isEmpty(newText)) {
256 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
257 (!dialpadSearch && mInRegularSearch);
258 if (!sameSearchMode) {
259 // call enterSearchUi only if we are switching search modes, or entering
260 // search ui for the first time
261 enterSearchUi(dialpadSearch, newText);
262 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700263
Yorke Leeb207f8a2013-08-29 18:51:06 -0700264 if (dialpadSearch && mSmartDialSearchFragment != null) {
265 mSmartDialSearchFragment.setQueryString(newText, false);
266 } else if (mRegularSearchFragment != null) {
267 mRegularSearchFragment.setQueryString(newText, false);
268 }
269 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700270 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700271 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700272 }
Yorke Leec3766332013-07-31 11:13:16 -0700273 }
274
275 @Override
276 public void afterTextChanged(Editable s) {
277 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700278 };
279
Yorke Leec3766332013-07-31 11:13:16 -0700280 private boolean isDialpadShowing() {
281 return mDialpadFragment != null && mDialpadFragment.isVisible();
282 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700283
284 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700285 protected void onCreate(Bundle savedInstanceState) {
286 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700287 mFirstLaunch = true;
288
Chiao Cheng94b10b52012-08-17 16:59:12 -0700289 final Intent intent = getIntent();
290 fixIntent(intent);
291
Yorke Lee8898cd02013-08-08 10:24:27 -0700292 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700293
Yorke Leec3766332013-07-31 11:13:16 -0700294 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700295
Yorke Leeef2b7382013-08-09 17:39:25 -0700296 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
297 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700298 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800299 getFragmentManager().beginTransaction()
300 .add(R.id.dialtacts_frame, new PhoneFavoriteFragment(), TAG_FAVORITES_FRAGMENT)
301 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
302 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700303 } else {
304 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700305 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
306 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700307 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700308 }
309
Yorke Leeb207f8a2013-08-29 18:51:06 -0700310 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Ihab Awad526c0b82014-02-27 12:55:36 -0800311
Yorke Lee3cefcc62014-01-16 11:26:46 -0800312 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leec3766332013-07-31 11:13:16 -0700313 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700314
Chiao Cheng94b10b52012-08-17 16:59:12 -0700315 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700316 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700317 setupFilterText(intent);
318 }
Yorke Leefce269a2013-08-12 11:56:04 -0700319
Ihab Awad526c0b82014-02-27 12:55:36 -0800320 hideDialpadFragment(false, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700321 setupFakeActionBarItems();
322
Yorke Lee0baa98b2013-08-22 10:55:16 -0700323 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700324 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700325 }
326
327 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700328 protected void onResume() {
329 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700330 if (mFirstLaunch) {
331 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700332 } else if (!phoneIsInUse() && mInCallDialpadUp) {
333 hideDialpadFragment(false, true);
334 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700335 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800336 prepareVoiceSearchButton();
Yorke Lee98702de2013-08-06 12:03:32 -0700337 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700338 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700339 }
340
341 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700342 protected void onPause() {
343 if (mClearSearchOnPause) {
344 hideDialpadAndSearchUi();
345 mClearSearchOnPause = false;
346 }
347 super.onPause();
348 }
349
350 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700351 protected void onSaveInstanceState(Bundle outState) {
352 super.onSaveInstanceState(outState);
353 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700354 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
355 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700356 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
357 }
358
359 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700360 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700361 if (fragment instanceof DialpadFragment) {
362 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700363 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700364 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700365 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700366 } else if (fragment instanceof SmartDialSearchFragment) {
367 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700368 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
369 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700370 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700371 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700372 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
373 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700374 } else if (fragment instanceof PhoneFavoriteFragment) {
375 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700376 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700377 }
Yorke Leec3766332013-07-31 11:13:16 -0700378 }
379
380 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700381 public boolean onMenuItemClick(MenuItem item) {
382 switch (item.getItemId()) {
383 case R.id.menu_import_export:
384 // We hard-code the "contactsAreAvailable" argument because doing it properly would
385 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
386 // now in Dialtacts for (potential) performance reasons. Compare with how it is
387 // done in {@link PeopleActivity}.
388 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700389 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700390 return true;
391 case R.id.menu_clear_frequents:
392 ClearFrequentsDialog.show(getFragmentManager());
393 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700394 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700395 try {
396 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
397 } catch (ActivityNotFoundException e) {
398 Toast toast = Toast.makeText(this,
399 R.string.add_contact_not_available,
400 Toast.LENGTH_SHORT);
401 toast.show();
402 }
403 return true;
404 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700405 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700406 return true;
407 case R.id.menu_all_contacts:
408 onShowAllContacts();
409 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700410 }
411 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700412 }
413
Alon Albertb453e5b2013-09-10 15:54:23 -0700414 protected void handleMenuSettings() {
415 openTelephonySetting(this);
416 }
417
418 public static void openTelephonySetting(Activity activity) {
419 final Intent settingsIntent = getCallSettingsIntent();
420 activity.startActivity(settingsIntent);
421 }
422
Chiao Cheng94b10b52012-08-17 16:59:12 -0700423 @Override
424 public void onClick(View view) {
425 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700426 case R.id.overflow_menu: {
Ihab Awad526c0b82014-02-27 12:55:36 -0800427 if (isDialpadShowing()) {
428 mDialpadFragment.optionsMenuInvoked(view);
429 } else {
430 mOverflowMenu.show();
431 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700432 break;
433 }
Yorke Leec3766332013-07-31 11:13:16 -0700434 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700435 // Reset the boolean flag that tracks whether the dialpad was up because
436 // we were in call. Regardless of whether it was true before, we want to
437 // show the dialpad because the user has explicitly clicked the dialpad
438 // button.
439 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700440 showDialpadFragment(true);
441 break;
Ihab Awad526c0b82014-02-27 12:55:36 -0800442 case R.id.dial_button:
443 // Dial button was pressed; tell the Dialpad fragment
444 mDialpadFragment.dialButtonPressed();
445 break;
Yorke Leec3766332013-07-31 11:13:16 -0700446 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700447 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
448 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700449 final Intent intent = new Intent(this, CallLogActivity.class);
450 startActivity(intent);
451 break;
452 case R.id.search_close_button:
453 // Clear the search field
454 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700455 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700456 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700457 }
458 break;
Yorke Leec3766332013-07-31 11:13:16 -0700459 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800460 try {
461 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
462 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
463 } catch (ActivityNotFoundException e) {
464 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
465 Toast.LENGTH_SHORT).show();
466 }
Yorke Leec3766332013-07-31 11:13:16 -0700467 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700468 default: {
469 Log.wtf(TAG, "Unexpected onClick event from " + view);
470 break;
471 }
472 }
473 }
474
Yorke Leec3766332013-07-31 11:13:16 -0700475 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800476 public boolean onLongClick(View view) {
477 switch (view.getId()) {
478 case R.id.dial_button: {
479 // Dial button was pressed; tell the Dialpad fragment
480 mDialpadFragment.dialButtonPressed();
481 return true; // Consume the event
482 }
483 default: {
484 Log.wtf(TAG, "Unexpected onClick event from " + view);
485 break;
486 }
487 }
488 return false;
489 }
490
491 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700492 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
493 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
494 if (resultCode == RESULT_OK) {
495 final ArrayList<String> matches = data.getStringArrayListExtra(
496 RecognizerIntent.EXTRA_RESULTS);
497 if (matches.size() > 0) {
498 final String match = matches.get(0);
499 mSearchView.setText(match);
500 } else {
501 Log.e(TAG, "Voice search - nothing heard");
502 }
503 } else {
504 Log.e(TAG, "Voice search failed");
505 }
506 }
507 super.onActivityResult(requestCode, resultCode, data);
508 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700509
Yorke Leec3766332013-07-31 11:13:16 -0700510 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700511 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700512 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700513 if (animate) {
514 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700515 } else {
516 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700517 }
518 ft.show(mDialpadFragment);
519 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800520 mDialButton.setVisibility(shouldShowOnscreenDialButton() ? View.VISIBLE : View.GONE);
521 mDialpadButton.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700522 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700523
Yorke Leeca195042013-09-25 16:29:38 -0700524 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700525 if (mDialpadFragment == null) return;
526 if (clearDialpad) {
527 mDialpadFragment.clearDialpad();
528 }
529 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700530 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700531 final FragmentTransaction ft = getFragmentManager().beginTransaction();
532 if (animate) {
533 ft.setCustomAnimations(0, R.anim.slide_out);
534 }
535 ft.hide(mDialpadFragment);
536 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800537 mDialButton.setVisibility(View.GONE);
538 mDialpadButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700539 }
540
Chiao Cheng94b10b52012-08-17 16:59:12 -0700541 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700542 mSearchViewContainer = findViewById(R.id.search_view_container);
543 mSearchViewCloseButton = findViewById(R.id.search_close_button);
544 mSearchViewCloseButton.setOnClickListener(this);
Yorke Leeda0f9042013-12-05 14:25:51 -0800545
Yorke Leec3766332013-07-31 11:13:16 -0700546 mSearchView = (EditText) findViewById(R.id.search_view);
547 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Tyler Gunna8ff9752014-02-24 11:57:57 -0800548 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
Yorke Leeda0f9042013-12-05 14:25:51 -0800549
550 prepareVoiceSearchButton();
551 }
552
553 private void prepareVoiceSearchButton() {
554 mVoiceSearchButton = findViewById(R.id.voice_search_button);
555 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
556 if (canIntentBeHandled(voiceIntent)) {
557 mVoiceSearchButton.setVisibility(View.VISIBLE);
558 mVoiceSearchButton.setOnClickListener(this);
559 } else {
560 mVoiceSearchButton.setVisibility(View.GONE);
561 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700562 }
563
Yorke Leec3766332013-07-31 11:13:16 -0700564 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
565 @Override
566 public void onAnimationEnd(Animator animation) {
567 mSearchViewContainer.setVisibility(View.GONE);
568 }
569 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700570
Yorke Leeb207f8a2013-08-29 18:51:06 -0700571 private boolean getInSearchUi() {
572 return mInDialpadSearch || mInRegularSearch;
573 }
574
575 private void setNotInSearchUi() {
576 mInDialpadSearch = false;
577 mInRegularSearch = false;
578 }
579
Yorke Lee311969c2013-08-26 06:31:11 -0700580 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700581 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700582 hideDialpadFragment(false, true);
583 }
584
Yorke Leec3766332013-07-31 11:13:16 -0700585 public void hideSearchBar() {
586 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700587 }
588
Yorke Leec3766332013-07-31 11:13:16 -0700589 public void hideSearchBar(boolean shiftView) {
590 if (shiftView) {
591 mSearchViewContainer.animate().cancel();
592 mSearchViewContainer.setAlpha(1);
593 mSearchViewContainer.setTranslationY(0);
594 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
595 .setDuration(200).setListener(mHideListener);
596
Yorke Leeb207f8a2013-08-29 18:51:06 -0700597 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700598 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
599 new AnimatorListenerAdapter() {
600 @Override
601 public void onAnimationEnd(Animator animation) {
Yorke Lee5f3ad542013-10-08 08:14:36 -0700602 mFragmentsFrame.setTranslationY(0);
Yorke Leec3766332013-07-31 11:13:16 -0700603 }
604 });
605 } else {
606 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
607 }
608 }
609
610 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700611 mSearchViewContainer.animate().cancel();
612 mSearchViewContainer.setAlpha(0);
613 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
614 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
615 .setListener(new AnimatorListenerAdapter() {
616 @Override
617 public void onAnimationStart(Animator animation) {
618 mSearchViewContainer.setVisibility(View.VISIBLE);
619 }
620 });
621
Yorke Leeb207f8a2013-08-29 18:51:06 -0700622 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
623 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700624 .setListener(
625 new AnimatorListenerAdapter() {
626 @Override
627 public void onAnimationStart(Animator animation) {
Yorke Leec3766332013-07-31 11:13:16 -0700628 }
629 });
630 }
631
632
Yorke Leeb207f8a2013-08-29 18:51:06 -0700633 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700634 mMenuButton = findViewById(R.id.overflow_menu);
635 if (mMenuButton != null) {
636 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700637
638 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
639 final Menu menu = mOverflowMenu.getMenu();
640 mOverflowMenu.inflate(R.menu.dialtacts_options);
641 mOverflowMenu.setOnMenuItemClickListener(this);
642 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700643 }
644
Ihab Awad526c0b82014-02-27 12:55:36 -0800645 mFakeActionBar = findViewById(R.id.fake_action_bar);
646
Yorke Leec3766332013-07-31 11:13:16 -0700647 mCallHistoryButton = findViewById(R.id.call_history_button);
648 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
649 mCallHistoryButton.setOnClickListener(this);
650
Ihab Awad526c0b82014-02-27 12:55:36 -0800651 mDialButton = findViewById(R.id.dial_button);
652 mDialButton.setOnClickListener(this);
653 mDialButton.setOnLongClickListener(this);
654
Yorke Leec3766332013-07-31 11:13:16 -0700655 mDialpadButton = findViewById(R.id.dialpad_button);
656 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
657 mDialpadButton.setOnClickListener(this);
658 }
659
Chiao Cheng94b10b52012-08-17 16:59:12 -0700660 private void fixIntent(Intent intent) {
661 // This should be cleaned up: the call key used to send an Intent
662 // that just said to go to the recent calls list. It now sends this
663 // abstract action, but this class hasn't been rewritten to deal with it.
664 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
665 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
666 intent.putExtra("call_key", true);
667 setIntent(intent);
668 }
669 }
670
Chiao Cheng94b10b52012-08-17 16:59:12 -0700671 /**
672 * Returns true if the intent is due to hitting the green send key (hardware call button:
673 * KEYCODE_CALL) while in a call.
674 *
675 * @param intent the intent that launched this activity
676 * @param recentCallsRequest true if the intent is requesting to view recent calls
677 * @return true if the intent is due to hitting the green send key while in a call
678 */
Yorke Leec3766332013-07-31 11:13:16 -0700679 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700680 // If there is a call in progress go to the call screen
681 if (recentCallsRequest) {
682 final boolean callKey = intent.getBooleanExtra("call_key", false);
683
684 try {
685 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
686 if (callKey && phone != null && phone.showCallScreen()) {
687 return true;
688 }
689 } catch (RemoteException e) {
690 Log.e(TAG, "Failed to handle send while in call", e);
691 }
692 }
693
694 return false;
695 }
696
697 /**
698 * Sets the current tab based on the intent's request type
699 *
700 * @param intent Intent that contains information about which tab should be selected
701 */
Yorke Leec3766332013-07-31 11:13:16 -0700702 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700703 // If we got here by hitting send and we're in call forward along to the in-call activity
704 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
705 getContentResolver()));
706 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
707 finish();
708 return;
709 }
710
Yorke Lee669b6082013-09-17 15:43:38 -0700711 if (mDialpadFragment != null) {
712 final boolean phoneIsInUse = phoneIsInUse();
713 if (phoneIsInUse || isDialIntent(intent)) {
714 mDialpadFragment.setStartedFromNewIntent(true);
715 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
716 mInCallDialpadUp = true;
717 }
718 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700719 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700720 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700721 }
722
723 @Override
724 public void onNewIntent(Intent newIntent) {
725 setIntent(newIntent);
726 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700727 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700729
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730 invalidateOptionsMenu();
731 }
732
733 /** Returns true if the given intent contains a phone number to populate the dialer with */
734 private boolean isDialIntent(Intent intent) {
735 final String action = intent.getAction();
736 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
737 return true;
738 }
739 if (Intent.ACTION_VIEW.equals(action)) {
740 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700741 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700742 return true;
743 }
744 }
745 return false;
746 }
747
748 /**
749 * Returns an appropriate call origin for this Activity. May return null when no call origin
750 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
751 * for remembering the tab in which the user made a phone call, so the external app's DIAL
752 * request should not be counted.)
753 */
754 public String getCallOrigin() {
755 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
756 }
757
758 /**
759 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
760 * This text originally came from a FILTER_CONTACTS_ACTION intent received
761 * by this activity. The stored text will then be cleared after after this
762 * method returns.
763 *
764 * @return The stored filter text
765 */
766 public String getAndClearFilterText() {
767 String filterText = mFilterText;
768 mFilterText = null;
769 return filterText;
770 }
771
772 /**
773 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
774 * This is so child activities can check if they are supposed to display a filter.
775 *
776 * @param intent The intent received in {@link #onNewIntent(Intent)}
777 */
778 private void setupFilterText(Intent intent) {
779 // If the intent was relaunched from history, don't apply the filter text.
780 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
781 return;
782 }
783 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
784 if (filter != null && filter.length() > 0) {
785 mFilterText = filter;
786 }
787 }
788
Yorke Lee8898cd02013-08-08 10:24:27 -0700789 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
790 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700791 @Override
792 public void onContactSelected(Uri contactUri) {
793 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700794 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700795 }
796
797 @Override
798 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700799 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700800 startActivity(intent);
801 }
802 };
803
Yorke Leec3766332013-07-31 11:13:16 -0700804 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
805 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
806 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700807
808 @Override
809 public void startSearch(String initialQuery, boolean selectInitialQuery,
810 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700811 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700812 if (mInSearchUi) {
813 if (mSearchView.hasFocus()) {
814 showInputMethod(mSearchView.findFocus());
815 } else {
816 mSearchView.requestFocus();
817 }
818 } else {
819 enterSearchUi();
820 }
821 } else {
822 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
823 }
Yorke Leec3766332013-07-31 11:13:16 -0700824 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700825
826 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700827 final InputMethodManager imm = (InputMethodManager) getSystemService(
828 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700829 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700830 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700831 }
832 }
833
834 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700835 final InputMethodManager imm = (InputMethodManager) getSystemService(
836 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700837 if (imm != null && view != null) {
838 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
839 }
840 }
841
842 /**
Yorke Leec3766332013-07-31 11:13:16 -0700843 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700844 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700845 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700846 if (getFragmentManager().isDestroyed()) {
847 // Weird race condition where fragment is doing work after the activity is destroyed
848 // due to talkback being on (b/10209937). Just return since we can't do any
849 // constructive here.
850 return;
851 }
852
Yorke Leeef2b7382013-08-09 17:39:25 -0700853 if (DEBUG) {
854 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
855 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700856
Yorke Leec3766332013-07-31 11:13:16 -0700857 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
858 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700859
Yorke Leeef2b7382013-08-09 17:39:25 -0700860 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700861 if (mInDialpadSearch) {
862 transaction.remove(mSmartDialSearchFragment);
863 } else if (mInRegularSearch) {
864 transaction.remove(mRegularSearchFragment);
865 } else {
866 transaction.remove(mPhoneFavoriteFragment);
867 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700868
Yorke Leeb207f8a2013-08-29 18:51:06 -0700869 final String tag;
870 if (smartDialSearch) {
871 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
872 } else {
873 tag = TAG_REGULAR_SEARCH_FRAGMENT;
874 }
875 mInDialpadSearch = smartDialSearch;
876 mInRegularSearch = !smartDialSearch;
877
Yorke Leeef2b7382013-08-09 17:39:25 -0700878 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
879 if (fragment == null) {
880 if (smartDialSearch) {
881 fragment = new SmartDialSearchFragment();
882 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700883 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700884 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700885 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700886 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700887 transaction.addToBackStack(null);
888 fragment.setQueryString(query, false);
889 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700890 }
891
892 /**
Yorke Leec3766332013-07-31 11:13:16 -0700893 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700894 */
Yorke Leec3766332013-07-31 11:13:16 -0700895 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700896 // See related bug in enterSearchUI();
897 if (getFragmentManager().isDestroyed()) {
898 return;
899 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700900 // Go all the way back to the favorites fragment, regardless of how many times we
901 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700902 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
903 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700904 }
905
906 /** Returns an Intent to launch Call Settings screen */
907 public static Intent getCallSettingsIntent() {
908 final Intent intent = new Intent(Intent.ACTION_MAIN);
909 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
910 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
911 return intent;
912 }
913
914 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700915 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700916 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700917 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700918 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700919 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700920 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700921 } else {
922 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700923 }
Yorke Leec3766332013-07-31 11:13:16 -0700924 }
925
926 @Override
927 public void onDialpadQueryChanged(String query) {
928 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
929 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
930 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700931 if (DEBUG) {
932 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
933 }
934 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
935 // This callback can happen if the dialpad fragment is recreated because of
936 // activity destruction. In that case, don't update the search view because
937 // that would bring the user back to the search fragment regardless of the
938 // previous state of the application. Instead, just return here and let the
939 // fragment manager correctly figure out whatever fragment was last displayed.
940 return;
941 }
Yorke Leec3766332013-07-31 11:13:16 -0700942 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700943 }
944 }
Yorke Leec3766332013-07-31 11:13:16 -0700945
946 @Override
947 public void onListFragmentScrollStateChange(int scrollState) {
948 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700949 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700950 hideInputMethod(getCurrentFocus());
951 }
952 }
953
954 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800955 public void setDialButtonEnabled(boolean enabled) {
956 mDialButton.setEnabled(enabled);
957 }
958
959 @Override
960 public void setDialButtonContainerVisible(boolean visible) {
961 mFakeActionBar.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700962 }
963
964 private boolean phoneIsInUse() {
965 final TelephonyManager tm = (TelephonyManager) getSystemService(
966 Context.TELEPHONY_SERVICE);
967 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
968 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700969
970 @Override
971 public void onShowAllContacts() {
972 final Intent intent = new Intent(this, AllContactsActivity.class);
973 startActivity(intent);
974 }
Yorke Leee1424812013-09-04 18:24:48 -0700975
976 public static Intent getAddNumberToContactIntent(CharSequence text) {
977 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
978 intent.putExtra(Intents.Insert.PHONE, text);
979 intent.setType(Contacts.CONTENT_ITEM_TYPE);
980 return intent;
981 }
982
Yorke Leeda0f9042013-12-05 14:25:51 -0800983 private boolean canIntentBeHandled(Intent intent) {
984 final PackageManager packageManager = getPackageManager();
985 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
986 PackageManager.MATCH_DEFAULT_ONLY);
987 return resolveInfo != null && resolveInfo.size() > 0;
988 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800989
990 @Override
991 public void onDragStarted(int itemIndex, int x, int y, PhoneFavoriteTileView view) {
992 crossfadeViews(mRemoveViewContainer, mSearchViewContainer, FADE_ANIMATION_DURATION);
993 }
994
995 @Override
996 public void onDragHovered(int itemIndex, int x, int y) {}
997
998 @Override
999 public void onDragFinished(int x, int y) {
1000 crossfadeViews(mSearchViewContainer, mRemoveViewContainer, FADE_ANIMATION_DURATION);
1001 }
1002
1003 @Override
1004 public void onDroppedOnRemove() {}
1005
1006 /**
1007 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
1008 * once it has been attached to the activity.
1009 */
1010 @Override
1011 public void setDragDropController(DragDropController dragController) {
1012 mRemoveViewContainer.setDragDropController(dragController);
1013 }
1014
1015 /**
1016 * Crossfades two views so that the first one appears while the other one is fading
1017 * out of view.
1018 */
1019 private void crossfadeViews(final View fadeIn, final View fadeOut, int duration) {
1020 fadeOut.animate().alpha(0).setDuration(duration)
1021 .setListener(new AnimatorListenerAdapter() {
1022 @Override
1023 public void onAnimationEnd(Animator animation) {
1024 fadeOut.setVisibility(View.GONE);
1025 }
1026 });
1027
1028 fadeIn.setVisibility(View.VISIBLE);
1029 fadeIn.setAlpha(0);
1030 fadeIn.animate().alpha(1).setDuration(FADE_ANIMATION_DURATION)
1031 .setListener(null);
1032 }
Ihab Awad526c0b82014-02-27 12:55:36 -08001033
1034 private boolean shouldShowOnscreenDialButton() {
1035 return getResources().getBoolean(R.bool.config_show_onscreen_dial_button);
1036 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001037}