blob: bfb908f04f1a3e311925de5dd87438696fa1ac37 [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
Chiao Cheng94b10b52012-08-17 16:59:12 -070019import android.app.Fragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -070020import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080021import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.content.Context;
23import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
Sai Cheemalapati82d6da52014-06-03 13:54:23 -070026import android.content.res.Configuration;
Andrew Lee2423a2f2014-05-29 14:14:45 -070027import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.net.Uri;
29import android.os.Bundle;
Yorke Leef5554ce2015-01-27 14:46:02 -080030import android.os.Trace;
Nancy Chen019713e2015-07-06 18:25:24 -070031import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070032import android.speech.RecognizerIntent;
Nancy Chenf3d9f822015-09-11 00:47:31 -040033import android.support.design.widget.CoordinatorLayout;
Yorke Leecc4660d2014-04-24 11:22:57 -070034import android.support.v4.view.ViewPager;
Nancy Chen44898ad2015-08-13 12:03:47 -070035import android.support.v7.app.ActionBar;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070036import android.telecom.PhoneAccount;
Yorke Lee53a22302014-04-29 18:13:46 -070037import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070039import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070040import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070041import android.view.DragEvent;
Yorke Lee74edcdc2014-07-11 16:15:08 -070042import android.view.Gravity;
Andrew Leeb1903842014-05-15 16:11:24 -070043import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070046import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070048import android.view.View.OnDragListener;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070049import android.view.View.OnTouchListener;
Ihab Awad9134e252014-07-09 12:32:52 -070050import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070051import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070052import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070053import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070054import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070055import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070056import android.widget.PopupMenu;
Anne Rong35208002015-07-22 16:27:37 -070057import android.widget.TextView;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080058import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070059
Yorke Leec3766332013-07-31 11:13:16 -070060import com.android.contacts.common.dialog.ClearFrequentsDialog;
61import com.android.contacts.common.interactions.ImportExportDialogFragment;
Nancy Chen0d3a7392014-08-12 14:44:53 -070062import com.android.contacts.common.interactions.TouchPointManager;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080063import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec16ea5a2015-05-19 15:51:01 -070064import com.android.contacts.common.util.PermissionsUtil;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -070065import com.android.contacts.common.widget.FloatingActionButtonController;
Brian Attwellbeab3bb2014-10-27 12:24:49 -070066import com.android.contacts.commonbind.analytics.AnalyticsUtil;
Yorke Leec3766332013-07-31 11:13:16 -070067import com.android.dialer.calllog.CallLogActivity;
Yorke Lee38019af2015-07-14 17:05:38 -070068import com.android.dialer.calllog.CallLogFragment;
Yorke Leefce269a2013-08-12 11:56:04 -070069import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070070import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070072import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070073import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080074import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070075import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080076import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070077import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070078import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070079import com.android.dialer.list.RegularSearchFragment;
80import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070081import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070082import com.android.dialer.list.SpeedDialFragment;
Yorke Leef44e5172015-10-01 17:16:43 -070083import com.android.dialer.onboard.OnboardingActivity;
Yorke Leeaf6f1952014-07-14 19:13:16 -070084import com.android.dialer.settings.DialerSettingsActivity;
Andrew Lee247df6e2015-05-12 19:09:00 -070085import com.android.dialer.util.IntentUtil;
Yorke Lee827a90f2015-09-17 18:10:58 -070086import com.android.dialer.util.TelecomUtil;
Yorke Leee3a2d132015-09-14 16:52:08 -070087import com.android.dialer.util.IntentUtil.CallIntentBuilder;
Yorke Lee7d20f822014-06-19 17:09:33 -070088import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070089import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070090import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070091import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070092import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee807afd82015-10-12 10:03:07 -070093import com.android.dialerbind.ObjectFactory;
Yorke Leec8d6e162015-09-14 18:43:27 -070094import com.android.incallui.Call.LogState;
Yorke Lee2fec47b2014-08-05 18:16:27 -070095import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070096import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070097
Yorke Leed5c512a2015-01-30 14:42:53 -080098import junit.framework.Assert;
99
Yorke Leec3766332013-07-31 11:13:16 -0700100import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -0800101import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -0700102
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700104 * The dialer tab's title is 'phone', a more common name (see strings.xml).
105 */
Yorke Leec3766332013-07-31 11:13:16 -0700106public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700107 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700108 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700109 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700110 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700111 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700112 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700113 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700114 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700115 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700116 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700117 ViewPager.OnPageChangeListener,
118 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119 private static final String TAG = "DialtactsActivity";
120
Yorke Lee7cc61492015-06-01 14:59:33 -0700121 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700122
Yorke Leef74011e2013-08-02 11:30:30 -0700123 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
124
Yorke Leeb207f8a2013-08-29 18:51:06 -0700125 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
126 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700127 private static final String KEY_SEARCH_QUERY = "search_query";
128 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700129 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700130
Yorke Leec3766332013-07-31 11:13:16 -0700131 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
132 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
133 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
134 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700135
Chiao Cheng94b10b52012-08-17 16:59:12 -0700136 /**
137 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
138 */
139 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700140 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700141
Yorke Leec3766332013-07-31 11:13:16 -0700142 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700143
Andrew Lee6324f702015-06-16 14:45:58 -0700144 private static final int FAB_SCALE_IN_DELAY_MS = 300;
145
Nancy Chenf3d9f822015-09-11 00:47:31 -0400146 private CoordinatorLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700147
Yorke Leec3766332013-07-31 11:13:16 -0700148 /**
Yorke Leec3766332013-07-31 11:13:16 -0700149 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700150 */
Evan Charlton72d66252014-11-07 16:23:47 -0800151 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700152
153 /**
154 * Fragment for searching phone numbers using the alphanumeric keyboard.
155 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700156 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700157
158 /**
159 * Fragment for searching phone numbers using the dialpad.
160 */
161 private SmartDialSearchFragment mSmartDialSearchFragment;
162
Yorke Leee00c9fe2014-04-12 12:42:06 -0700163 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700164 * Animation that slides in.
165 */
166 private Animation mSlideIn;
167
168 /**
169 * Animation that slides out.
170 */
171 private Animation mSlideOut;
172
Yorke Lee6c450eb2015-05-08 09:45:47 -0700173 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
174 @Override
175 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700176 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700177 }
178 };
179
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700180 /**
181 * Listener for after slide out animation completes on dialer fragment.
182 */
183 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
184 @Override
185 public void onAnimationEnd(Animation animation) {
186 commitDialpadFragmentHide();
187 }
188 };
189
190 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700191 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700192 */
193 private ListsFragment mListsFragment;
194
Yorke Lee19e68ca2014-10-28 11:51:40 -0700195 /**
196 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
197 * be commited.
198 */
199 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800200 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700201 private boolean mInDialpadSearch;
202 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700203 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700204 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700205 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700206
Yorke Leeef2b7382013-08-09 17:39:25 -0700207 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700208 * Whether or not the device is in landscape orientation.
209 */
210 private boolean mIsLandscape;
211
212 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700213 * True if the dialpad is only temporarily showing due to being in call
214 */
215 private boolean mInCallDialpadUp;
216
217 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700218 * True when this activity has been launched for the first time.
219 */
Yorke Lee98702de2013-08-06 12:03:32 -0700220 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700221
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700222 /**
223 * Search query to be applied to the SearchView in the ActionBar once
224 * onCreateOptionsMenu has been called.
225 */
226 private String mPendingSearchViewQuery;
227
Yorke Lee74edcdc2014-07-11 16:15:08 -0700228 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700229 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700230 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700231
Yorke Leeef2b7382013-08-09 17:39:25 -0700232 private String mSearchQuery;
233
Yorke Leefce269a2013-08-12 11:56:04 -0700234 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700235 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700236 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700237
Sai Cheemalapati41460652014-06-02 21:05:39 -0700238 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700239
Andrew Lee2423a2f2014-05-29 14:14:45 -0700240 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700241
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800242 /**
243 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
244 * {@link #onResume()} to populate the search box.
245 */
246 private String mVoiceSearchQuery;
247
Andrew Lee467de3d2015-03-17 15:47:24 -0700248 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700249 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700250 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700251 }
252
253 @Override
254 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700255 final boolean hasContactsPermission =
256 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700257 final Menu menu = getMenu();
258 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700259 clearFrequents.setVisible(mListsFragment != null &&
260 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700261 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
262
263 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
264 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700265
266 menu.findItem(R.id.menu_history).setVisible(
267 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700268 super.show();
269 }
270 }
271
Chiao Cheng94b10b52012-08-17 16:59:12 -0700272 /**
Yorke Lee28266192014-05-01 10:05:52 -0700273 * Listener that listens to drag events and sends their x and y coordinates to a
274 * {@link DragDropController}.
275 */
276 private class LayoutOnDragListener implements OnDragListener {
277 @Override
278 public boolean onDrag(View v, DragEvent event) {
279 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700280 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700281 }
282 return true;
283 }
284 }
285
286 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700287 * Listener used to send search queries to the phone search fragment.
288 */
Yorke Lee53a22302014-04-29 18:13:46 -0700289 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700290 @Override
291 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
292 }
Yorke Leec3766332013-07-31 11:13:16 -0700293
Andrew Lee99a570c2014-05-15 14:18:50 -0700294 @Override
295 public void onTextChanged(CharSequence s, int start, int before, int count) {
296 final String newText = s.toString();
297 if (newText.equals(mSearchQuery)) {
298 // If the query hasn't changed (perhaps due to activity being destroyed
299 // and restored, or user launching the same DIAL intent twice), then there is
300 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700301 return;
302 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700303 if (DEBUG) {
304 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700305 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700306 }
Yorke Lee710709d2014-05-29 07:18:42 -0700307 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700308
Andrew Lee90374a72014-05-16 15:01:09 -0700309 // Show search fragment only when the query string is changed to non-empty text.
310 if (!TextUtils.isEmpty(newText)) {
311 // Call enterSearchUi only if we are switching search modes, or showing a search
312 // fragment for the first time.
313 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
314 (!mIsDialpadShown && mInRegularSearch);
315 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700316 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700317 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700318 }
319
Andrew Leea8515422014-06-13 16:53:54 -0700320 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700321 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700322 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700323 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700324 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700325 }
326
327 @Override
328 public void afterTextChanged(Editable s) {
329 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700330 };
331
Andrew Leeb1903842014-05-15 16:11:24 -0700332
333 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700334 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700335 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700336 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700337 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700338 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700339 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700340 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700341 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
342 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700343 }
Andrew Leeb1903842014-05-15 16:11:24 -0700344 }
345 };
346
347 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700348 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700349 */
350 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
351 @Override
352 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700353 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
354 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
355 // If the search term is empty, close the search UI.
356 maybeExitSearchUi();
357 } else {
358 // If the search term is not empty, show the dialpad fab.
359 showFabInSearchUi();
360 }
Andrew Leeb1903842014-05-15 16:11:24 -0700361 }
362 return false;
363 }
364 };
365
Chiao Cheng94b10b52012-08-17 16:59:12 -0700366 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700367 public boolean dispatchTouchEvent(MotionEvent ev) {
368 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
369 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
370 }
371 return super.dispatchTouchEvent(ev);
Nancy Chen49db1ad2014-08-18 20:10:17 -0700372 }
373
374 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700375 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800376 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700377 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700378
Yorke Leef44e5172015-10-01 17:16:43 -0700379 if (OnboardingActivity.shouldStartOnboardingActivity(this)) {
380 OnboardingActivity.startOnboardingActivity(this);
381 finish();
382 }
383
Yorke Lee98702de2013-08-06 12:03:32 -0700384 mFirstLaunch = true;
385
Andrew Lee2423a2f2014-05-29 14:14:45 -0700386 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700387 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700388
Yorke Leef5554ce2015-01-27 14:46:02 -0800389 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700390 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800391 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800392 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700393
Yorke Leef5554ce2015-01-27 14:46:02 -0800394 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700395 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700396 actionBar.setCustomView(R.layout.search_edittext);
397 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700398 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700399
Anne Rong35208002015-07-22 16:27:37 -0700400 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
401 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700402 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
403
Andrew Leee3f59a82015-02-26 12:06:35 -0800404 mActionBarController = new ActionBarController(this, searchEditTextLayout);
405
Andrew Lee04675a52014-06-05 18:36:20 -0700406 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700407 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700408 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
409 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
410 .setOnClickListener(mSearchViewOnClickListener);
411 searchEditTextLayout.findViewById(R.id.search_box_start_search)
412 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700413 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700414 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700415 @Override
416 public void onBackButtonClicked() {
417 onBackPressed();
418 }
Andrew Lee6324f702015-06-16 14:45:58 -0700419
420 @Override
421 public void onSearchViewClicked() {
422 // Hide FAB, as the keyboard is shown.
423 mFloatingActionButtonController.scaleOut();
424 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700425 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700426
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700427 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700428 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700429
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700430 final View floatingActionButtonContainer = findViewById(
431 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700432 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
433 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700434 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700435 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700436
Andrew Lee04675a52014-06-05 18:36:20 -0700437 ImageButton optionsMenuButton =
438 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700439 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700440 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
441 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700442
Yorke Leed5c512a2015-01-30 14:42:53 -0800443 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
444 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700445 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800446 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000447 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800448 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700449 } else {
450 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700451 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
452 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700453 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700454 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700455 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700456 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700457
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700458 final boolean isLayoutRtl = DialerUtils.isRtl();
459 if (mIsLandscape) {
460 mSlideIn = AnimationUtils.loadAnimation(this,
461 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
462 mSlideOut = AnimationUtils.loadAnimation(this,
463 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
464 } else {
465 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
466 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
467 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700468
Yorke Lee2fec47b2014-08-05 18:16:27 -0700469 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
470 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
471
Yorke Lee6c450eb2015-05-08 09:45:47 -0700472 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700473 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700474
Nancy Chenf3d9f822015-09-11 00:47:31 -0400475 mParentLayout = (CoordinatorLayout) findViewById(R.id.dialtacts_mainlayout);
Yorke Leef614f6f2014-10-03 10:44:19 -0700476 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700477 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700478 new ViewTreeObserver.OnGlobalLayoutListener() {
479 @Override
480 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700481 final ViewTreeObserver observer =
482 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700483 if (!observer.isAlive()) {
484 return;
485 }
486 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700487 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700488 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700489 mFloatingActionButtonController.align(
490 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700491 }
492 });
Andrew Lee0c667702014-05-12 14:31:45 -0700493
Yorke Leef5554ce2015-01-27 14:46:02 -0800494 Trace.endSection();
495
496 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700497 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700498 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800499 Trace.endSection();
500 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700501 }
502
503 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700504 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800505 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700506 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700507
Yorke Lee19e68ca2014-10-28 11:51:40 -0700508 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700509 if (mFirstLaunch) {
510 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700511 } else if (!phoneIsInUse() && mInCallDialpadUp) {
512 hideDialpadFragment(false, true);
513 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700514 } else if (mShowDialpadOnResume) {
515 showDialpadFragment(false);
516 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700517 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800518
519 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
520 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
521 // shown until onResume has completed. Active the search UI and set the search term now.
522 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
523 mActionBarController.onSearchBoxTapped();
524 mSearchView.setText(mVoiceSearchQuery);
525 mVoiceSearchQuery = null;
526 }
527
Yorke Lee98702de2013-08-06 12:03:32 -0700528 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800529
530 if (mIsRestarting) {
531 // This is only called when the activity goes from resumed -> paused -> resumed, so it
532 // will not cause an extra view to be sent out on rotation
533 if (mIsDialpadShown) {
534 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
535 }
536 mIsRestarting = false;
537 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700538
Yorke Lee53a22302014-04-29 18:13:46 -0700539 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700540 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700541 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700542
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700543 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
544 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
545 // used internally.
546 final Bundle extras = getIntent().getExtras();
547 if (extras != null
548 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
549 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
550 } else {
551 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
552 }
553 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700554 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
555 if (index < mListsFragment.getTabCount()) {
556 mListsFragment.showTab(index);
557 }
558 }
559
Anne Rong35208002015-07-22 16:27:37 -0700560 setSearchBoxHint();
561
Yorke Leef5554ce2015-01-27 14:46:02 -0800562 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700563 }
564
565 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800566 protected void onRestart() {
567 super.onRestart();
568 mIsRestarting = true;
569 }
570
571 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700572 protected void onPause() {
573 if (mClearSearchOnPause) {
574 hideDialpadAndSearchUi();
575 mClearSearchOnPause = false;
576 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700577 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
578 commitDialpadFragmentHide();
579 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700580 super.onPause();
581 }
582
583 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700584 protected void onSaveInstanceState(Bundle outState) {
585 super.onSaveInstanceState(outState);
586 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700587 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
588 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700589 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700590 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700591 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700592 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700593 }
594
595 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700596 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700597 if (fragment instanceof DialpadFragment) {
598 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800599 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700600 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
601 transaction.hide(mDialpadFragment);
602 transaction.commit();
603 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700604 } else if (fragment instanceof SmartDialSearchFragment) {
605 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700606 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700607 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700608 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700609 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700610 } else if (fragment instanceof ListsFragment) {
611 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700612 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700613 }
Yorke Leec3766332013-07-31 11:13:16 -0700614 }
615
Alon Albertb453e5b2013-09-10 15:54:23 -0700616 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700617 final Intent intent = new Intent(this, DialerSettingsActivity.class);
618 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700619 }
620
Chiao Cheng94b10b52012-08-17 16:59:12 -0700621 @Override
622 public void onClick(View view) {
623 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700624 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700625 if (mListsFragment.getCurrentTabIndex()
626 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700627 DialerUtils.startActivityWithErrorToast(
628 this,
629 IntentUtil.getNewContactIntent(),
630 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700631 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700632 mInCallDialpadUp = false;
633 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700634 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700635 break;
Yorke Leec3766332013-07-31 11:13:16 -0700636 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800637 try {
638 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
639 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
640 } catch (ActivityNotFoundException e) {
641 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
642 Toast.LENGTH_SHORT).show();
643 }
Yorke Leec3766332013-07-31 11:13:16 -0700644 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700645 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700646 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700647 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700648 default: {
649 Log.wtf(TAG, "Unexpected onClick event from " + view);
650 break;
651 }
652 }
653 }
654
Yorke Leec3766332013-07-31 11:13:16 -0700655 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700656 public boolean onMenuItemClick(MenuItem item) {
Jay Shrauner193cfc52015-09-25 14:13:16 -0700657 if (!isSafeToCommitTransactions()) {
Jay Shrauner119b0652015-09-17 12:00:18 -0700658 return true;
659 }
660
Yorke Lee86e21f72014-04-02 16:49:38 -0700661 switch (item.getItemId()) {
662 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700663 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
664 // CONTENT_TYPE, so that we always open our call log from our dialer
665 final Intent intent = new Intent(this, CallLogActivity.class);
666 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700667 break;
668 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700669 DialerUtils.startActivityWithErrorToast(
670 this,
671 IntentUtil.getNewContactIntent(),
672 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700673 break;
674 case R.id.menu_import_export:
675 // We hard-code the "contactsAreAvailable" argument because doing it properly would
676 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
677 // now in Dialtacts for (potential) performance reasons. Compare with how it is
678 // done in {@link PeopleActivity}.
Wenyi Wang5953e8c2015-09-24 15:38:14 -0700679 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
680 ImportExportDialogFragment.show(getFragmentManager(), true,
681 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_FAVORITES);
682 } else {
683 ImportExportDialogFragment.show(getFragmentManager(), true,
684 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_DEFAULT);
685 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700686 return true;
687 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700688 ClearFrequentsDialog.show(getFragmentManager());
689 return true;
690 case R.id.menu_call_settings:
691 handleMenuSettings();
692 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700693 }
694 return false;
695 }
696
697 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700698 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
699 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
700 if (resultCode == RESULT_OK) {
701 final ArrayList<String> matches = data.getStringArrayListExtra(
702 RecognizerIntent.EXTRA_RESULTS);
703 if (matches.size() > 0) {
704 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800705 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700706 } else {
707 Log.e(TAG, "Voice search - nothing heard");
708 }
709 } else {
710 Log.e(TAG, "Voice search failed");
711 }
712 }
713 super.onActivityResult(requestCode, resultCode, data);
714 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700715
Andrew Lee2a58ab82014-05-15 02:16:04 -0700716 /**
Nancy Chenea7c0b82015-08-31 17:18:41 -0700717 * Update the number of unread voicemails (potentially other tabs) displayed next to the tab
718 * icon.
719 */
720 public void updateTabUnreadCounts() {
721 mListsFragment.updateTabUnreadCounts();
722 }
723
724 /**
Andrew Lee2a58ab82014-05-15 02:16:04 -0700725 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
726 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
727 * @see #onDialpadShown
728 */
Yorke Leec3766332013-07-31 11:13:16 -0700729 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700730 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700731 return;
732 }
733 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800734
Yorke Lee7ee5c422014-11-04 10:29:32 -0800735 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700736
Chiao Cheng94b10b52012-08-17 16:59:12 -0700737 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800738 if (mDialpadFragment == null) {
739 mDialpadFragment = new DialpadFragment();
740 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
741 } else {
742 ft.show(mDialpadFragment);
743 }
744
745 mDialpadFragment.setAnimate(animate);
746 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700747 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700748
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700749 if (animate) {
750 mFloatingActionButtonController.scaleOut();
751 } else {
752 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700753 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700754 }
Yorke Lee5253be02014-05-21 18:50:03 -0700755 mActionBarController.onDialpadUp();
756
Yorke Lee6c450eb2015-05-08 09:45:47 -0700757 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700758 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700759
Andrew Lee2a58ab82014-05-15 02:16:04 -0700760 /**
761 * Callback from child DialpadFragment when the dialpad is shown.
762 */
763 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800764 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700765 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700766 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700767 } else {
768 mDialpadFragment.setYFraction(0);
769 }
770
Andrew Lee2a58ab82014-05-15 02:16:04 -0700771 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700772 }
773
774 /**
775 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
776 * a callback after the hide animation ends.
777 * @see #commitDialpadFragmentHide
778 */
Yorke Leeca195042013-09-25 16:29:38 -0700779 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700780 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700781 return;
782 }
Yorke Leef6673d32013-08-23 15:34:17 -0700783 if (clearDialpad) {
784 mDialpadFragment.clearDialpad();
785 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700786 if (!mIsDialpadShown) {
787 return;
Yorke Leec3766332013-07-31 11:13:16 -0700788 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700789 mIsDialpadShown = false;
790 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800791 mListsFragment.setUserVisibleHint(true);
792 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700793
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700794 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700795
Andrew Lee6324f702015-06-16 14:45:58 -0700796 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700797 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700798 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700799 } else {
800 commitDialpadFragmentHide();
801 }
802
Yorke Lee5253be02014-05-21 18:50:03 -0700803 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700804
Andrew Leed4cde832014-05-16 15:56:48 -0700805 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700806 if (TextUtils.isEmpty(mSearchQuery)) {
807 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700808 }
Andrew Leed4cde832014-05-16 15:56:48 -0700809 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700810 }
811
812 /**
813 * Finishes hiding the dialpad fragment after any animations are completed.
814 */
815 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800816 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700817 final FragmentTransaction ft = getFragmentManager().beginTransaction();
818 ft.hide(mDialpadFragment);
819 ft.commit();
820 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700821 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700822 }
823
Andrew Lee2a58ab82014-05-15 02:16:04 -0700824 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700825 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700826 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700827 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700828 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700829 fragment = mRegularSearchFragment;
830 }
831 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700832 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700833 }
834 }
835
Yorke Lee5253be02014-05-21 18:50:03 -0700836 @Override
837 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700838 return mInDialpadSearch || mInRegularSearch;
839 }
840
Yorke Lee5253be02014-05-21 18:50:03 -0700841 @Override
842 public boolean hasSearchQuery() {
843 return !TextUtils.isEmpty(mSearchQuery);
844 }
845
846 @Override
847 public boolean shouldShowActionBar() {
848 return mListsFragment.shouldShowActionBar();
849 }
850
Yorke Leeb207f8a2013-08-29 18:51:06 -0700851 private void setNotInSearchUi() {
852 mInDialpadSearch = false;
853 mInRegularSearch = false;
854 }
855
Yorke Lee311969c2013-08-26 06:31:11 -0700856 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700857 if (mIsDialpadShown) {
858 hideDialpadFragment(false, true);
859 } else {
860 exitSearchUi();
861 }
Yorke Lee311969c2013-08-26 06:31:11 -0700862 }
863
Yorke Lee53a22302014-04-29 18:13:46 -0700864 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700865 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
866 if (canIntentBeHandled(voiceIntent)) {
867 mVoiceSearchButton.setVisibility(View.VISIBLE);
868 mVoiceSearchButton.setOnClickListener(this);
869 } else {
870 mVoiceSearchButton.setVisibility(View.GONE);
871 }
872 }
873
Yorke Leee9c82332015-10-06 14:21:53 -0700874 public boolean isNearbyPlacesSearchEnabled() {
875 return false;
876 }
877
Anne Rong35208002015-07-22 16:27:37 -0700878 protected int getSearchBoxHint () {
879 return R.string.dialer_hint_find_contact;
880 }
881
882 /**
883 * Sets the hint text for the contacts search box
884 */
885 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700886 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700887 .getCustomView().findViewById(R.id.search_view_container);
888 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
889 .setHint(getSearchBoxHint());
890 }
891
Andrew Lee467de3d2015-03-17 15:47:24 -0700892 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700893 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
894 popupMenu.inflate(R.menu.dialtacts_options);
895 popupMenu.setOnMenuItemClickListener(this);
896 return popupMenu;
897 }
898
Yorke Lee86e21f72014-04-02 16:49:38 -0700899 @Override
900 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700901 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700902 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700903 mPendingSearchViewQuery = null;
904 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700905 if (mActionBarController != null) {
906 mActionBarController.restoreActionBarOffset();
907 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700908 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700909 }
910
Chiao Cheng94b10b52012-08-17 16:59:12 -0700911 /**
912 * Returns true if the intent is due to hitting the green send key (hardware call button:
913 * KEYCODE_CALL) while in a call.
914 *
915 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700916 * @return true if the intent is due to hitting the green send key while in a call
917 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700918 private boolean isSendKeyWhileInCall(Intent intent) {
919 // If there is a call in progress and the user launched the dialer by hitting the call
920 // button, go straight to the in-call screen.
921 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700922
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700923 if (callKey) {
Yorke Lee762b3572015-09-18 12:54:59 -0700924 TelecomUtil.showInCallScreen(this, false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700925 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700926 }
927
928 return false;
929 }
930
931 /**
932 * Sets the current tab based on the intent's request type
933 *
934 * @param intent Intent that contains information about which tab should be selected
935 */
Yorke Leec3766332013-07-31 11:13:16 -0700936 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700937 // If we got here by hitting send and we're in call forward along to the in-call activity
Yorke Lee62e995c2014-03-28 10:02:56 -0700938 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700939 finish();
940 return;
941 }
942
Yorke Leed77017d2015-08-31 16:19:43 -0700943 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
944 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800945 showDialpadFragment(false);
946 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700947 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800948 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700949 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700950 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700951 }
952
953 @Override
954 public void onNewIntent(Intent newIntent) {
955 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700956
Nancy Chen199b7f02014-11-05 15:03:00 -0800957 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700958 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700959
Chiao Cheng94b10b52012-08-17 16:59:12 -0700960 invalidateOptionsMenu();
961 }
962
963 /** Returns true if the given intent contains a phone number to populate the dialer with */
964 private boolean isDialIntent(Intent intent) {
965 final String action = intent.getAction();
966 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
967 return true;
968 }
969 if (Intent.ACTION_VIEW.equals(action)) {
970 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700971 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700972 return true;
973 }
974 }
975 return false;
976 }
977
978 /**
Yorke Leec3766332013-07-31 11:13:16 -0700979 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700980 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700981 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700982 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700983 // Weird race condition where fragment is doing work after the activity is destroyed
984 // due to talkback being on (b/10209937). Just return since we can't do any
985 // constructive here.
986 return;
987 }
988
Yorke Leeef2b7382013-08-09 17:39:25 -0700989 if (DEBUG) {
990 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
991 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700992
Yorke Leec3766332013-07-31 11:13:16 -0700993 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700994 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700995 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700996 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700997 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700998 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700999
Yorke Leeb207f8a2013-08-29 18:51:06 -07001000 final String tag;
1001 if (smartDialSearch) {
1002 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
1003 } else {
1004 tag = TAG_REGULAR_SEARCH_FRAGMENT;
1005 }
1006 mInDialpadSearch = smartDialSearch;
1007 mInRegularSearch = !smartDialSearch;
1008
Andrew Lee6324f702015-06-16 14:45:58 -07001009 mFloatingActionButtonController.scaleOut();
1010
Andrew Lee752956e2014-05-15 11:43:38 -07001011 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001012 if (animate) {
1013 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
1014 } else {
1015 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
1016 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001017 if (fragment == null) {
1018 if (smartDialSearch) {
1019 fragment = new SmartDialSearchFragment();
1020 } else {
Yorke Lee807afd82015-10-12 10:03:07 -07001021 fragment = ObjectFactory.newRegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001022 fragment.setOnTouchListener(new View.OnTouchListener() {
1023 @Override
1024 public boolean onTouch(View v, MotionEvent event) {
1025 // Show the FAB when the user touches the lists fragment and the soft
1026 // keyboard is hidden.
1027 showFabInSearchUi();
1028 return false;
1029 }
1030 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001031 }
Andrew Leeb1903842014-05-15 16:11:24 -07001032 transaction.add(R.id.dialtacts_frame, fragment, tag);
1033 } else {
1034 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001035 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001036 // DialtactsActivity will provide the options menu
1037 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001038 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001039 if (!smartDialSearch) {
1040 fragment.setQueryString(query, false /* delaySelection */);
1041 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001042 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001043
Yorke Lee6c450eb2015-05-08 09:45:47 -07001044 if (animate) {
1045 mListsFragment.getView().animate().alpha(0).withLayer();
1046 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001047 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001048 }
1049
1050 /**
Yorke Leec3766332013-07-31 11:13:16 -07001051 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001052 */
Yorke Leec3766332013-07-31 11:13:16 -07001053 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001054 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001055 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001056 return;
1057 }
Andrew Leeb1903842014-05-15 16:11:24 -07001058
Andrew Leeb1903842014-05-15 16:11:24 -07001059 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001060
1061 if (mDialpadFragment != null) {
1062 mDialpadFragment.clearDialpad();
1063 }
1064
Yorke Leeb207f8a2013-08-29 18:51:06 -07001065 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001066
Andrew Lee6324f702015-06-16 14:45:58 -07001067 // Restore the FAB for the lists fragment.
1068 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1069 mFloatingActionButtonController.setVisible(false);
1070 }
1071 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1072 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1073 onPageSelected(mListsFragment.getCurrentTabIndex());
1074
Andrew Leeb1903842014-05-15 16:11:24 -07001075 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001076 if (mSmartDialSearchFragment != null) {
1077 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001078 }
Andrew Leeb1903842014-05-15 16:11:24 -07001079 if (mRegularSearchFragment != null) {
1080 transaction.remove(mRegularSearchFragment);
1081 }
1082 transaction.commit();
1083
1084 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001085
1086 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001087 // If the dialpad fragment wasn't previously visible, then send a screen view because
1088 // we are exiting regular search. Otherwise, the screen view will be sent by
1089 // {@link #hideDialpadFragment}.
1090 mListsFragment.sendScreenViewForCurrentPosition();
1091 mListsFragment.setUserVisibleHint(true);
1092 }
1093
Yorke Lee5253be02014-05-21 18:50:03 -07001094 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001095 }
1096
Chiao Cheng94b10b52012-08-17 16:59:12 -07001097 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001098 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001099 if (mStateSaved) {
1100 return;
1101 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001102 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001103 if (TextUtils.isEmpty(mSearchQuery) ||
1104 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1105 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001106 exitSearchUi();
1107 }
Yorke Leef6673d32013-08-23 15:34:17 -07001108 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001109 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001110 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001111 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001112 } else {
1113 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001114 }
Yorke Leec3766332013-07-31 11:13:16 -07001115 }
1116
Yorke Lee81a313d2015-06-01 14:53:59 -07001117 private void maybeEnterSearchUi() {
1118 if (!isInSearchUi()) {
1119 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1120 }
1121 }
1122
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001123 /**
1124 * @return True if the search UI was exited, false otherwise
1125 */
1126 private boolean maybeExitSearchUi() {
1127 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1128 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001129 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001130 return true;
1131 }
1132 return false;
1133 }
1134
Andrew Lee6324f702015-06-16 14:45:58 -07001135 private void showFabInSearchUi() {
1136 mFloatingActionButtonController.changeIcon(
1137 getResources().getDrawable(R.drawable.fab_ic_dial),
1138 getResources().getString(R.string.action_menu_dialpad_button));
1139 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1140 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1141 }
1142
Yorke Leec3766332013-07-31 11:13:16 -07001143 @Override
1144 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001145 if (mSmartDialSearchFragment != null) {
1146 mSmartDialSearchFragment.setAddToContactNumber(query);
1147 }
Yorke Leec3766332013-07-31 11:13:16 -07001148 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1149 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001150
1151 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001152 if (DEBUG) {
1153 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1154 }
1155 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1156 // This callback can happen if the dialpad fragment is recreated because of
1157 // activity destruction. In that case, don't update the search view because
1158 // that would bring the user back to the search fragment regardless of the
1159 // previous state of the application. Instead, just return here and let the
1160 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001161 if (!TextUtils.isEmpty(normalizedQuery)) {
1162 mPendingSearchViewQuery = normalizedQuery;
1163 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001164 return;
1165 }
Yorke Lee53a22302014-04-29 18:13:46 -07001166 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001167 }
Santos Cordon83131712015-04-16 21:25:35 -07001168
1169 try {
1170 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1171 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1172 }
1173 } catch (Exception ignored) {
1174 // Skip any exceptions for this piece of code
1175 }
Yorke Lee575ae382015-07-16 11:36:07 -07001176 }
Santos Cordon83131712015-04-16 21:25:35 -07001177
Yorke Lee575ae382015-07-16 11:36:07 -07001178 @Override
1179 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1180 if (mInDialpadSearch && mSmartDialSearchFragment != null
1181 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1182 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1183 return true;
1184 }
1185 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001186 }
Yorke Leec3766332013-07-31 11:13:16 -07001187
1188 @Override
1189 public void onListFragmentScrollStateChange(int scrollState) {
1190 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001191 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001192 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001193 }
1194 }
1195
1196 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001197 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001198 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001199 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1200 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001201 }
1202
Yorke Leec3766332013-07-31 11:13:16 -07001203 private boolean phoneIsInUse() {
Yorke Lee827a90f2015-09-17 18:10:58 -07001204 return TelecomUtil.isInCall(this);
Yorke Leec3766332013-07-31 11:13:16 -07001205 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001206
Yorke Leeda0f9042013-12-05 14:25:51 -08001207 private boolean canIntentBeHandled(Intent intent) {
1208 final PackageManager packageManager = getPackageManager();
1209 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1210 PackageManager.MATCH_DEFAULT_ONLY);
1211 return resolveInfo != null && resolveInfo.size() > 0;
1212 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001213
Yorke Lee86e21f72014-04-02 16:49:38 -07001214 /**
1215 * Called when the user has long-pressed a contact tile to start a drag operation.
1216 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001217 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001218 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001219 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001220 }
1221
1222 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001223 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1224 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001225
Yorke Lee86e21f72014-04-02 16:49:38 -07001226 /**
1227 * Called when the user has released a contact tile after long-pressing it.
1228 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001229 @Override
1230 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001231 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001232 }
1233
1234 @Override
1235 public void onDroppedOnRemove() {}
1236
1237 /**
Yorke Leed999b712014-04-23 15:10:58 -07001238 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001239 * once it has been attached to the activity.
1240 */
1241 @Override
1242 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001243 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001244 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001245 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001246
Yorke Lee38019af2015-07-14 17:05:38 -07001247 /**
1248 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1249 */
1250 @Override
1251 public void showAllContactsTab() {
1252 if (mListsFragment != null) {
1253 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1254 }
1255 }
1256
1257 /**
1258 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1259 */
1260 @Override
1261 public void showDialpad() {
1262 showDialpadFragment(true);
1263 }
1264
Yorke Leee00c9fe2014-04-12 12:42:06 -07001265 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001266 public void onPickDataUri(Uri dataUri, int callInitiationType) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001267 mClearSearchOnPause = true;
Yorke Leec8d6e162015-09-14 18:43:27 -07001268 PhoneNumberInteraction.startInteractionForPhoneCall(
1269 DialtactsActivity.this, dataUri, callInitiationType);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001270 }
1271
1272 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001273 public void onPickPhoneNumber(String phoneNumber, boolean isVideoCall, int callInitiationType) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001274 if (phoneNumber == null) {
1275 // Invalid phone number, but let the call go through so that InCallUI can show
1276 // an error message.
1277 phoneNumber = "";
1278 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001279
1280 final Intent intent = new CallIntentBuilder(phoneNumber)
1281 .setIsVideoCall(isVideoCall)
Yorke Leec8d6e162015-09-14 18:43:27 -07001282 .setCallInitiationType(callInitiationType)
Yorke Leee3a2d132015-09-14 16:52:08 -07001283 .build();
1284
Yorke Lee7d20f822014-06-19 17:09:33 -07001285 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001286 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001287 }
1288
1289 @Override
1290 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001291 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001292 }
1293
1294 @Override
1295 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001296 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001297 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001298
Yorke Leecc4660d2014-04-24 11:22:57 -07001299 @Override
1300 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001301 int tabIndex = mListsFragment.getCurrentTabIndex();
1302
Andrew Lee4de59fb2015-08-13 15:20:08 -07001303 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1304 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1305 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001306 boolean isRtl = DialerUtils.isRtl();
1307 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1308 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001309 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001310 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1311 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001312 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001313 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001314 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001315
Andrew Lee432d4b52015-05-20 16:52:00 -07001316 @Override
1317 public void onPageSelected(int position) {
1318 int tabIndex = mListsFragment.getCurrentTabIndex();
1319 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001320 mFloatingActionButtonController.changeIcon(
1321 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1322 getResources().getString(R.string.search_shortcut_create_new_contact));
1323 } else {
1324 mFloatingActionButtonController.changeIcon(
1325 getResources().getDrawable(R.drawable.fab_ic_dial),
1326 getResources().getString(R.string.action_menu_dialpad_button));
1327 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001328 }
1329
1330 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001331 public void onPageScrollStateChanged(int state) {
1332 }
1333
Yorke Lee5253be02014-05-21 18:50:03 -07001334 @Override
1335 public boolean isActionBarShowing() {
1336 return mActionBarController.isActionBarShowing();
1337 }
1338
Yorke Leec98a5bb2014-10-28 16:12:05 -07001339 @Override
1340 public ActionBarController getActionBarController() {
1341 return mActionBarController;
1342 }
1343
Yorke Lee6bc67162015-06-27 14:03:25 -07001344 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001345 public boolean isDialpadShown() {
1346 return mIsDialpadShown;
1347 }
1348
Yorke Lee5253be02014-05-21 18:50:03 -07001349 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001350 public int getDialpadHeight() {
1351 if (mDialpadFragment != null) {
1352 return mDialpadFragment.getDialpadHeight();
1353 }
1354 return 0;
1355 }
1356
1357 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001358 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001359 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001360 }
1361
1362 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001363 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001364 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001365 }
1366
1367 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001368 public int getActionBarHeight() {
1369 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001370 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001371
Andrew Lee6324f702015-06-16 14:45:58 -07001372 private int getFabAlignment() {
1373 if (!mIsLandscape && !isInSearchUi() &&
1374 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1375 return FloatingActionButtonController.ALIGN_MIDDLE;
1376 }
1377 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001378 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001379}