blob: d12cf24df6e580788ec5a7be39ee0029ef661434 [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 Lee135e4652015-10-23 18:12:51 -070019import com.google.common.annotations.VisibleForTesting;
20
Chiao Cheng94b10b52012-08-17 16:59:12 -070021import android.app.Fragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080023import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070024import android.content.Context;
25import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
Sai Cheemalapati82d6da52014-06-03 13:54:23 -070028import android.content.res.Configuration;
Andrew Lee2423a2f2014-05-29 14:14:45 -070029import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070030import android.net.Uri;
31import android.os.Bundle;
Yorke Leef5554ce2015-01-27 14:46:02 -080032import android.os.Trace;
Nancy Chen019713e2015-07-06 18:25:24 -070033import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070034import android.speech.RecognizerIntent;
Nancy Chenf3d9f822015-09-11 00:47:31 -040035import android.support.design.widget.CoordinatorLayout;
Yorke Leecc4660d2014-04-24 11:22:57 -070036import android.support.v4.view.ViewPager;
Nancy Chen44898ad2015-08-13 12:03:47 -070037import android.support.v7.app.ActionBar;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070038import android.telecom.PhoneAccount;
Yorke Lee53a22302014-04-29 18:13:46 -070039import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070040import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070041import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070042import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070043import android.view.DragEvent;
Yorke Lee74edcdc2014-07-11 16:15:08 -070044import android.view.Gravity;
Andrew Leeb1903842014-05-15 16:11:24 -070045import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070048import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070050import android.view.View.OnDragListener;
Ihab Awad9134e252014-07-09 12:32:52 -070051import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070052import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070053import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070054import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070055import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070056import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.widget.PopupMenu;
Anne Rong35208002015-07-22 16:27:37 -070058import android.widget.TextView;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080059import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070060
Yorke Leec3766332013-07-31 11:13:16 -070061import com.android.contacts.common.dialog.ClearFrequentsDialog;
62import com.android.contacts.common.interactions.ImportExportDialogFragment;
Nancy Chen0d3a7392014-08-12 14:44:53 -070063import com.android.contacts.common.interactions.TouchPointManager;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080064import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec16ea5a2015-05-19 15:51:01 -070065import com.android.contacts.common.util.PermissionsUtil;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -070066import com.android.contacts.common.widget.FloatingActionButtonController;
Brian Attwellbeab3bb2014-10-27 12:24:49 -070067import com.android.contacts.commonbind.analytics.AnalyticsUtil;
Yorke Leec3766332013-07-31 11:13:16 -070068import com.android.dialer.calllog.CallLogActivity;
Yorke Lee38019af2015-07-14 17:05:38 -070069import com.android.dialer.calllog.CallLogFragment;
Yorke Leefce269a2013-08-12 11:56:04 -070070import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070071import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070073import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070074import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080075import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070076import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080077import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070078import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070079import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070080import com.android.dialer.list.RegularSearchFragment;
81import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070082import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070083import com.android.dialer.list.SpeedDialFragment;
Yorke Lee6e0fd6b2015-11-04 20:44:25 -080084import com.android.dialer.logging.Logger;
Yorke Lee58e68a92015-11-06 10:35:30 -080085import com.android.dialer.logging.ScreenEvent;
Yorke Leeaf6f1952014-07-14 19:13:16 -070086import com.android.dialer.settings.DialerSettingsActivity;
Yorke Lee7d20f822014-06-19 17:09:33 -070087import com.android.dialer.util.DialerUtils;
Ta-wei Yene3cae632015-10-27 16:09:09 -070088import com.android.dialer.util.IntentUtil;
89import com.android.dialer.util.IntentUtil.CallIntentBuilder;
90import com.android.dialer.util.TelecomUtil;
Yorke Lee5253be02014-05-21 18:50:03 -070091import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070092import com.android.dialer.widget.SearchEditTextLayout;
Yorke Lee0baa98b2013-08-22 10:55:16 -070093import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee807afd82015-10-12 10:03:07 -070094import com.android.dialerbind.ObjectFactory;
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 Lee135e4652015-10-23 18:12:51 -0700131 @VisibleForTesting
132 public static final String TAG_DIALPAD_FRAGMENT = "dialpad";
Yorke Leec3766332013-07-31 11:13:16 -0700133 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
134 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
135 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700136
Chiao Cheng94b10b52012-08-17 16:59:12 -0700137 /**
138 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
139 */
140 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700141 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700142
Yorke Leec3766332013-07-31 11:13:16 -0700143 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700144
Andrew Lee6324f702015-06-16 14:45:58 -0700145 private static final int FAB_SCALE_IN_DELAY_MS = 300;
146
Nancy Chenf3d9f822015-09-11 00:47:31 -0400147 private CoordinatorLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700148
Yorke Leec3766332013-07-31 11:13:16 -0700149 /**
Yorke Leec3766332013-07-31 11:13:16 -0700150 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700151 */
Evan Charlton72d66252014-11-07 16:23:47 -0800152 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700153
154 /**
155 * Fragment for searching phone numbers using the alphanumeric keyboard.
156 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700157 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700158
159 /**
160 * Fragment for searching phone numbers using the dialpad.
161 */
162 private SmartDialSearchFragment mSmartDialSearchFragment;
163
Yorke Leee00c9fe2014-04-12 12:42:06 -0700164 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700165 * Animation that slides in.
166 */
167 private Animation mSlideIn;
168
169 /**
170 * Animation that slides out.
171 */
172 private Animation mSlideOut;
173
Yorke Lee6c450eb2015-05-08 09:45:47 -0700174 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
175 @Override
176 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700177 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700178 }
179 };
180
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700181 /**
182 * Listener for after slide out animation completes on dialer fragment.
183 */
184 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
185 @Override
186 public void onAnimationEnd(Animation animation) {
187 commitDialpadFragmentHide();
188 }
189 };
190
191 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700192 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700193 */
194 private ListsFragment mListsFragment;
195
Yorke Lee19e68ca2014-10-28 11:51:40 -0700196 /**
197 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
198 * be commited.
199 */
200 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800201 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700202 private boolean mInDialpadSearch;
203 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700204 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700205 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700206 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700207
Yorke Leeef2b7382013-08-09 17:39:25 -0700208 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700209 * Whether or not the device is in landscape orientation.
210 */
211 private boolean mIsLandscape;
212
213 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700214 * True if the dialpad is only temporarily showing due to being in call
215 */
216 private boolean mInCallDialpadUp;
217
218 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700219 * True when this activity has been launched for the first time.
220 */
Yorke Lee98702de2013-08-06 12:03:32 -0700221 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700222
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700223 /**
224 * Search query to be applied to the SearchView in the ActionBar once
225 * onCreateOptionsMenu has been called.
226 */
227 private String mPendingSearchViewQuery;
228
Yorke Lee74edcdc2014-07-11 16:15:08 -0700229 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700230 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700231 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700232
Yorke Leeef2b7382013-08-09 17:39:25 -0700233 private String mSearchQuery;
Yorke Leed9a93162015-10-14 09:39:05 -0700234 private String mDialpadQuery;
Yorke Leeef2b7382013-08-09 17:39:25 -0700235
Yorke Leefce269a2013-08-12 11:56:04 -0700236 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700237 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700238 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700239
Sai Cheemalapati41460652014-06-02 21:05:39 -0700240 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700241
Andrew Lee2423a2f2014-05-29 14:14:45 -0700242 private int mActionBarHeight;
Sarmad Hashmicb559812016-02-10 18:39:59 -0800243 private int mPreviouslySelectedTabIndex;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700244
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800245 /**
246 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
247 * {@link #onResume()} to populate the search box.
248 */
249 private String mVoiceSearchQuery;
250
Andrew Lee467de3d2015-03-17 15:47:24 -0700251 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700252 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700253 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700254 }
255
256 @Override
257 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700258 final boolean hasContactsPermission =
259 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700260 final Menu menu = getMenu();
261 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700262 clearFrequents.setVisible(mListsFragment != null &&
263 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700264 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
265
266 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
267 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700268
269 menu.findItem(R.id.menu_history).setVisible(
270 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700271 super.show();
272 }
273 }
274
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275 /**
Yorke Lee28266192014-05-01 10:05:52 -0700276 * Listener that listens to drag events and sends their x and y coordinates to a
277 * {@link DragDropController}.
278 */
279 private class LayoutOnDragListener implements OnDragListener {
280 @Override
281 public boolean onDrag(View v, DragEvent event) {
282 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700283 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700284 }
285 return true;
286 }
287 }
288
289 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700290 * Listener used to send search queries to the phone search fragment.
291 */
Yorke Lee53a22302014-04-29 18:13:46 -0700292 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700293 @Override
294 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
295 }
Yorke Leec3766332013-07-31 11:13:16 -0700296
Andrew Lee99a570c2014-05-15 14:18:50 -0700297 @Override
298 public void onTextChanged(CharSequence s, int start, int before, int count) {
299 final String newText = s.toString();
300 if (newText.equals(mSearchQuery)) {
301 // If the query hasn't changed (perhaps due to activity being destroyed
302 // and restored, or user launching the same DIAL intent twice), then there is
303 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700304 return;
305 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700306 if (DEBUG) {
307 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700308 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700309 }
Yorke Lee710709d2014-05-29 07:18:42 -0700310 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700311
Andrew Lee90374a72014-05-16 15:01:09 -0700312 // Show search fragment only when the query string is changed to non-empty text.
313 if (!TextUtils.isEmpty(newText)) {
314 // Call enterSearchUi only if we are switching search modes, or showing a search
315 // fragment for the first time.
316 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
317 (!mIsDialpadShown && mInRegularSearch);
318 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700319 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700320 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700321 }
322
Andrew Leea8515422014-06-13 16:53:54 -0700323 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700324 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700325 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700326 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700327 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700328 }
329
330 @Override
331 public void afterTextChanged(Editable s) {
332 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700333 };
334
Andrew Leeb1903842014-05-15 16:11:24 -0700335
336 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700337 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700338 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700339 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700340 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700341 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700342 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700343 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700344 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
345 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700346 }
Andrew Leeb1903842014-05-15 16:11:24 -0700347 }
348 };
349
350 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700351 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700352 */
353 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
354 @Override
355 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700356 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
357 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
358 // If the search term is empty, close the search UI.
359 maybeExitSearchUi();
360 } else {
361 // If the search term is not empty, show the dialpad fab.
362 showFabInSearchUi();
363 }
Andrew Leeb1903842014-05-15 16:11:24 -0700364 }
365 return false;
366 }
367 };
368
Chiao Cheng94b10b52012-08-17 16:59:12 -0700369 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700370 public boolean dispatchTouchEvent(MotionEvent ev) {
371 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
372 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
373 }
374 return super.dispatchTouchEvent(ev);
Nancy Chen49db1ad2014-08-18 20:10:17 -0700375 }
376
377 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700378 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800379 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700380 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700381
Yorke Lee98702de2013-08-06 12:03:32 -0700382 mFirstLaunch = true;
383
Andrew Lee2423a2f2014-05-29 14:14:45 -0700384 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700385 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700386
Yorke Leef5554ce2015-01-27 14:46:02 -0800387 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700388 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800389 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800390 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700391
Yorke Leef5554ce2015-01-27 14:46:02 -0800392 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700393 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700394 actionBar.setCustomView(R.layout.search_edittext);
395 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700396 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700397
Anne Rong35208002015-07-22 16:27:37 -0700398 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
399 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700400 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
401
Andrew Leee3f59a82015-02-26 12:06:35 -0800402 mActionBarController = new ActionBarController(this, searchEditTextLayout);
403
Andrew Lee04675a52014-06-05 18:36:20 -0700404 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700405 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700406 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
407 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
408 .setOnClickListener(mSearchViewOnClickListener);
409 searchEditTextLayout.findViewById(R.id.search_box_start_search)
410 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700411 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700412 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700413 @Override
414 public void onBackButtonClicked() {
415 onBackPressed();
416 }
Andrew Lee6324f702015-06-16 14:45:58 -0700417
418 @Override
419 public void onSearchViewClicked() {
420 // Hide FAB, as the keyboard is shown.
421 mFloatingActionButtonController.scaleOut();
422 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700423 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700424
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700425 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700426 == Configuration.ORIENTATION_LANDSCAPE;
Sarmad Hashmicb559812016-02-10 18:39:59 -0800427 mPreviouslySelectedTabIndex = ListsFragment.TAB_INDEX_SPEED_DIAL;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700428 final View floatingActionButtonContainer = findViewById(
429 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700430 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
431 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700432 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700433 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700434
Andrew Lee04675a52014-06-05 18:36:20 -0700435 ImageButton optionsMenuButton =
436 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700437 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700438 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
439 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700440
Yorke Leed5c512a2015-01-30 14:42:53 -0800441 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
442 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700443 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800444 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000445 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800446 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700447 } else {
448 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700449 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
450 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700451 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700452 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700453 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700454 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700455
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700456 final boolean isLayoutRtl = DialerUtils.isRtl();
457 if (mIsLandscape) {
458 mSlideIn = AnimationUtils.loadAnimation(this,
459 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
460 mSlideOut = AnimationUtils.loadAnimation(this,
461 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
462 } else {
463 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
464 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
465 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700466
Yorke Lee2fec47b2014-08-05 18:16:27 -0700467 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
468 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
469
Yorke Lee6c450eb2015-05-08 09:45:47 -0700470 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700471 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700472
Nancy Chenf3d9f822015-09-11 00:47:31 -0400473 mParentLayout = (CoordinatorLayout) findViewById(R.id.dialtacts_mainlayout);
Yorke Leef614f6f2014-10-03 10:44:19 -0700474 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700475 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700476 new ViewTreeObserver.OnGlobalLayoutListener() {
477 @Override
478 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700479 final ViewTreeObserver observer =
480 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700481 if (!observer.isAlive()) {
482 return;
483 }
484 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700485 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700486 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700487 mFloatingActionButtonController.align(
488 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700489 }
490 });
Andrew Lee0c667702014-05-12 14:31:45 -0700491
Yorke Leef5554ce2015-01-27 14:46:02 -0800492 Trace.endSection();
493
494 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700495 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700496 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800497 Trace.endSection();
498 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700499 }
500
501 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700502 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800503 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700504 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700505
Yorke Lee19e68ca2014-10-28 11:51:40 -0700506 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700507 if (mFirstLaunch) {
508 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700509 } else if (!phoneIsInUse() && mInCallDialpadUp) {
510 hideDialpadFragment(false, true);
511 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700512 } else if (mShowDialpadOnResume) {
513 showDialpadFragment(false);
514 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700515 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800516
517 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
518 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
519 // shown until onResume has completed. Active the search UI and set the search term now.
520 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
521 mActionBarController.onSearchBoxTapped();
522 mSearchView.setText(mVoiceSearchQuery);
523 mVoiceSearchQuery = null;
524 }
525
Yorke Lee98702de2013-08-06 12:03:32 -0700526 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800527
528 if (mIsRestarting) {
529 // This is only called when the activity goes from resumed -> paused -> resumed, so it
530 // will not cause an extra view to be sent out on rotation
531 if (mIsDialpadShown) {
Yorke Lee58e68a92015-11-06 10:35:30 -0800532 Logger.logScreenView(ScreenEvent.DIALPAD, this);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800533 }
534 mIsRestarting = false;
535 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700536
Yorke Lee53a22302014-04-29 18:13:46 -0700537 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700538 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700539 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700540
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700541 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
542 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
543 // used internally.
544 final Bundle extras = getIntent().getExtras();
545 if (extras != null
546 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
547 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
548 } else {
549 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
550 }
551 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700552 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
553 if (index < mListsFragment.getTabCount()) {
554 mListsFragment.showTab(index);
555 }
556 }
557
Anne Rong35208002015-07-22 16:27:37 -0700558 setSearchBoxHint();
559
Yorke Leef5554ce2015-01-27 14:46:02 -0800560 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561 }
562
563 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800564 protected void onRestart() {
565 super.onRestart();
566 mIsRestarting = true;
567 }
568
569 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700570 protected void onPause() {
Sarmad Hashmicb559812016-02-10 18:39:59 -0800571 // Only clear missed calls if the pause was not triggered by an orientation change
572 // (or any other confirguration change)
573 if (!isChangingConfigurations()) {
574 updateMissedCalls();
575 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700576 if (mClearSearchOnPause) {
577 hideDialpadAndSearchUi();
578 mClearSearchOnPause = false;
579 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700580 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
581 commitDialpadFragmentHide();
582 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700583 super.onPause();
584 }
585
586 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700587 protected void onSaveInstanceState(Bundle outState) {
588 super.onSaveInstanceState(outState);
589 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700590 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
591 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700592 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700593 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700594 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700595 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700596 }
597
598 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700599 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700600 if (fragment instanceof DialpadFragment) {
601 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800602 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700603 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
604 transaction.hide(mDialpadFragment);
605 transaction.commit();
606 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700607 } else if (fragment instanceof SmartDialSearchFragment) {
608 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700609 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leed9a93162015-10-14 09:39:05 -0700610 if (!TextUtils.isEmpty(mDialpadQuery)) {
611 mSmartDialSearchFragment.setAddToContactNumber(mDialpadQuery);
612 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700613 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700614 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700615 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700616 } else if (fragment instanceof ListsFragment) {
617 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700618 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700619 }
Yorke Leec3766332013-07-31 11:13:16 -0700620 }
621
Alon Albertb453e5b2013-09-10 15:54:23 -0700622 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700623 final Intent intent = new Intent(this, DialerSettingsActivity.class);
624 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700625 }
626
Chiao Cheng94b10b52012-08-17 16:59:12 -0700627 @Override
628 public void onClick(View view) {
Sailesh Nepal68d86c62016-02-21 11:41:28 -0800629 int resId = view.getId();
630 if (resId == R.id.floating_action_button) {
631 if (mListsFragment.getCurrentTabIndex()
632 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
633 DialerUtils.startActivityWithErrorToast(
634 this,
635 IntentUtil.getNewContactIntent(),
636 R.string.add_contact_not_available);
637 } else if (!mIsDialpadShown) {
638 mInCallDialpadUp = false;
639 showDialpadFragment(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700640 }
Sailesh Nepal68d86c62016-02-21 11:41:28 -0800641 } else if (resId == R.id.voice_search_button) {
642 try {
643 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
644 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
645 } catch (ActivityNotFoundException e) {
646 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
647 Toast.LENGTH_SHORT).show();
648 }
649 } else if (resId == R.id.dialtacts_options_menu_button) {
650 mOverflowMenu.show();
651 } else {
652 Log.wtf(TAG, "Unexpected onClick event from " + view);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700653 }
654 }
655
Yorke Leec3766332013-07-31 11:13:16 -0700656 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700657 public boolean onMenuItemClick(MenuItem item) {
Jay Shrauner193cfc52015-09-25 14:13:16 -0700658 if (!isSafeToCommitTransactions()) {
Jay Shrauner119b0652015-09-17 12:00:18 -0700659 return true;
660 }
661
Sailesh Nepal68d86c62016-02-21 11:41:28 -0800662 int resId = item.getItemId();
663 if (resId == R.id.menu_history) {// 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);
667 } else if (resId == R.id.menu_add_contact) {
668 DialerUtils.startActivityWithErrorToast(
669 this,
670 IntentUtil.getNewContactIntent(),
671 R.string.add_contact_not_available);
672 } else if (resId == R.id.menu_import_export) {// We hard-code the "contactsAreAvailable" argument because doing it properly would
673 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
674 // now in Dialtacts for (potential) performance reasons. Compare with how it is
675 // done in {@link PeopleActivity}.
676 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
677 ImportExportDialogFragment.show(getFragmentManager(), true,
678 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_FAVORITES);
679 } else {
680 ImportExportDialogFragment.show(getFragmentManager(), true,
681 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_DEFAULT);
682 }
683 Logger.logScreenView(ScreenEvent.IMPORT_EXPORT_CONTACTS, this);
684 return true;
685 } else if (resId == R.id.menu_clear_frequents) {
686 ClearFrequentsDialog.show(getFragmentManager());
687 Logger.logScreenView(ScreenEvent.CLEAR_FREQUENTS, this);
688 return true;
689 } else if (resId == R.id.menu_call_settings) {
690 handleMenuSettings();
691 Logger.logScreenView(ScreenEvent.SETTINGS, this);
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);
Yorke Lee58e68a92015-11-06 10:35:30 -0800746 Logger.logScreenView(ScreenEvent.DIALPAD, this);
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();
Ta-wei Yene3cae632015-10-27 16:09:09 -0700758
759 //adjust the title, so the user will know where we're at when the activity start/resumes.
760 setTitle(R.string.launcherDialpadActivityLabel);
Yorke Leec3766332013-07-31 11:13:16 -0700761 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700762
Andrew Lee2a58ab82014-05-15 02:16:04 -0700763 /**
764 * Callback from child DialpadFragment when the dialpad is shown.
765 */
766 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800767 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700768 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700769 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700770 } else {
771 mDialpadFragment.setYFraction(0);
772 }
773
Andrew Lee2a58ab82014-05-15 02:16:04 -0700774 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700775 }
776
777 /**
778 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
779 * a callback after the hide animation ends.
780 * @see #commitDialpadFragmentHide
781 */
Yorke Leeca195042013-09-25 16:29:38 -0700782 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700783 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700784 return;
785 }
Yorke Leef6673d32013-08-23 15:34:17 -0700786 if (clearDialpad) {
Ta-wei Yenaf875dd2015-11-03 15:57:03 -0800787 // Temporarily disable accessibility when we clear the dialpad, since it should be
788 // invisible and should not announce anything.
789 mDialpadFragment.getDigitsWidget().setImportantForAccessibility(
790 View.IMPORTANT_FOR_ACCESSIBILITY_NO);
Yorke Leef6673d32013-08-23 15:34:17 -0700791 mDialpadFragment.clearDialpad();
Ta-wei Yenaf875dd2015-11-03 15:57:03 -0800792 mDialpadFragment.getDigitsWidget().setImportantForAccessibility(
793 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Yorke Leef6673d32013-08-23 15:34:17 -0700794 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700795 if (!mIsDialpadShown) {
796 return;
Yorke Leec3766332013-07-31 11:13:16 -0700797 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700798 mIsDialpadShown = false;
799 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800800 mListsFragment.setUserVisibleHint(true);
801 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700802
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700803 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700804
Andrew Lee6324f702015-06-16 14:45:58 -0700805 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700806 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700807 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700808 } else {
809 commitDialpadFragmentHide();
810 }
811
Yorke Lee5253be02014-05-21 18:50:03 -0700812 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700813
Andrew Leed4cde832014-05-16 15:56:48 -0700814 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700815 if (TextUtils.isEmpty(mSearchQuery)) {
816 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700817 }
Andrew Leed4cde832014-05-16 15:56:48 -0700818 }
Ta-wei Yene3cae632015-10-27 16:09:09 -0700819 //reset the title to normal.
820 setTitle(R.string.launcherActivityLabel);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700821 }
822
823 /**
824 * Finishes hiding the dialpad fragment after any animations are completed.
825 */
826 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800827 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700828 final FragmentTransaction ft = getFragmentManager().beginTransaction();
829 ft.hide(mDialpadFragment);
830 ft.commit();
831 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700832 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700833 }
834
Andrew Lee2a58ab82014-05-15 02:16:04 -0700835 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700836 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700837 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700838 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700839 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700840 fragment = mRegularSearchFragment;
841 }
842 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700843 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700844 }
845 }
846
Yorke Lee5253be02014-05-21 18:50:03 -0700847 @Override
848 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700849 return mInDialpadSearch || mInRegularSearch;
850 }
851
Yorke Lee5253be02014-05-21 18:50:03 -0700852 @Override
853 public boolean hasSearchQuery() {
854 return !TextUtils.isEmpty(mSearchQuery);
855 }
856
857 @Override
858 public boolean shouldShowActionBar() {
859 return mListsFragment.shouldShowActionBar();
860 }
861
Yorke Leeb207f8a2013-08-29 18:51:06 -0700862 private void setNotInSearchUi() {
863 mInDialpadSearch = false;
864 mInRegularSearch = false;
865 }
866
Yorke Lee311969c2013-08-26 06:31:11 -0700867 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700868 if (mIsDialpadShown) {
869 hideDialpadFragment(false, true);
870 } else {
871 exitSearchUi();
872 }
Yorke Lee311969c2013-08-26 06:31:11 -0700873 }
874
Yorke Lee53a22302014-04-29 18:13:46 -0700875 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700876 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
877 if (canIntentBeHandled(voiceIntent)) {
878 mVoiceSearchButton.setVisibility(View.VISIBLE);
879 mVoiceSearchButton.setOnClickListener(this);
880 } else {
881 mVoiceSearchButton.setVisibility(View.GONE);
882 }
883 }
884
Yorke Leee9c82332015-10-06 14:21:53 -0700885 public boolean isNearbyPlacesSearchEnabled() {
886 return false;
887 }
888
Anne Rong35208002015-07-22 16:27:37 -0700889 protected int getSearchBoxHint () {
890 return R.string.dialer_hint_find_contact;
891 }
892
893 /**
894 * Sets the hint text for the contacts search box
895 */
896 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700897 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700898 .getCustomView().findViewById(R.id.search_view_container);
899 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
900 .setHint(getSearchBoxHint());
901 }
902
Andrew Lee467de3d2015-03-17 15:47:24 -0700903 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700904 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
905 popupMenu.inflate(R.menu.dialtacts_options);
906 popupMenu.setOnMenuItemClickListener(this);
907 return popupMenu;
908 }
909
Yorke Lee86e21f72014-04-02 16:49:38 -0700910 @Override
911 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700912 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700913 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700914 mPendingSearchViewQuery = null;
915 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700916 if (mActionBarController != null) {
917 mActionBarController.restoreActionBarOffset();
918 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700919 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700920 }
921
Chiao Cheng94b10b52012-08-17 16:59:12 -0700922 /**
923 * Returns true if the intent is due to hitting the green send key (hardware call button:
924 * KEYCODE_CALL) while in a call.
925 *
926 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700927 * @return true if the intent is due to hitting the green send key while in a call
928 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700929 private boolean isSendKeyWhileInCall(Intent intent) {
930 // If there is a call in progress and the user launched the dialer by hitting the call
931 // button, go straight to the in-call screen.
932 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700933
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700934 if (callKey) {
Yorke Lee762b3572015-09-18 12:54:59 -0700935 TelecomUtil.showInCallScreen(this, false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700936 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700937 }
938
939 return false;
940 }
941
942 /**
943 * Sets the current tab based on the intent's request type
944 *
945 * @param intent Intent that contains information about which tab should be selected
946 */
Yorke Leec3766332013-07-31 11:13:16 -0700947 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700948 // 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 -0700949 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700950 finish();
951 return;
952 }
953
Yorke Leed77017d2015-08-31 16:19:43 -0700954 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
955 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800956 showDialpadFragment(false);
957 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700958 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800959 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700960 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700961 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700962 }
963
964 @Override
965 public void onNewIntent(Intent newIntent) {
966 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700967
Nancy Chen199b7f02014-11-05 15:03:00 -0800968 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700969 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700970
Chiao Cheng94b10b52012-08-17 16:59:12 -0700971 invalidateOptionsMenu();
972 }
973
974 /** Returns true if the given intent contains a phone number to populate the dialer with */
975 private boolean isDialIntent(Intent intent) {
976 final String action = intent.getAction();
977 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
978 return true;
979 }
980 if (Intent.ACTION_VIEW.equals(action)) {
981 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700982 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700983 return true;
984 }
985 }
986 return false;
987 }
988
989 /**
Yorke Leec3766332013-07-31 11:13:16 -0700990 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700991 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700992 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700993 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700994 // Weird race condition where fragment is doing work after the activity is destroyed
995 // due to talkback being on (b/10209937). Just return since we can't do any
996 // constructive here.
997 return;
998 }
999
Yorke Leeef2b7382013-08-09 17:39:25 -07001000 if (DEBUG) {
1001 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
1002 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001003
Yorke Leec3766332013-07-31 11:13:16 -07001004 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001005 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001006 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001007 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001008 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -07001009 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001010
Yorke Leeb207f8a2013-08-29 18:51:06 -07001011 final String tag;
1012 if (smartDialSearch) {
1013 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
1014 } else {
1015 tag = TAG_REGULAR_SEARCH_FRAGMENT;
1016 }
1017 mInDialpadSearch = smartDialSearch;
1018 mInRegularSearch = !smartDialSearch;
1019
Andrew Lee6324f702015-06-16 14:45:58 -07001020 mFloatingActionButtonController.scaleOut();
1021
Andrew Lee752956e2014-05-15 11:43:38 -07001022 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001023 if (animate) {
1024 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
1025 } else {
1026 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
1027 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001028 if (fragment == null) {
1029 if (smartDialSearch) {
1030 fragment = new SmartDialSearchFragment();
1031 } else {
Yorke Lee807afd82015-10-12 10:03:07 -07001032 fragment = ObjectFactory.newRegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001033 fragment.setOnTouchListener(new View.OnTouchListener() {
1034 @Override
1035 public boolean onTouch(View v, MotionEvent event) {
1036 // Show the FAB when the user touches the lists fragment and the soft
1037 // keyboard is hidden.
Nancy Chen0bb96ac2016-01-15 16:21:27 -08001038 hideDialpadFragment(true, false);
Andrew Lee6324f702015-06-16 14:45:58 -07001039 showFabInSearchUi();
1040 return false;
1041 }
1042 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001043 }
Andrew Leeb1903842014-05-15 16:11:24 -07001044 transaction.add(R.id.dialtacts_frame, fragment, tag);
1045 } else {
1046 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001047 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001048 // DialtactsActivity will provide the options menu
1049 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001050 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001051 if (!smartDialSearch) {
1052 fragment.setQueryString(query, false /* delaySelection */);
1053 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001054 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001055
Yorke Lee6c450eb2015-05-08 09:45:47 -07001056 if (animate) {
1057 mListsFragment.getView().animate().alpha(0).withLayer();
1058 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001059 mListsFragment.setUserVisibleHint(false);
Yorke Leef96bb182015-11-10 10:06:21 -08001060
1061 if (smartDialSearch) {
1062 Logger.logScreenView(ScreenEvent.SMART_DIAL_SEARCH, this);
1063 } else {
1064 Logger.logScreenView(ScreenEvent.REGULAR_SEARCH, this);
1065 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001066 }
1067
1068 /**
Yorke Leec3766332013-07-31 11:13:16 -07001069 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001070 */
Yorke Leec3766332013-07-31 11:13:16 -07001071 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001072 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001073 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001074 return;
1075 }
Andrew Leeb1903842014-05-15 16:11:24 -07001076
Andrew Leeb1903842014-05-15 16:11:24 -07001077 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001078
1079 if (mDialpadFragment != null) {
1080 mDialpadFragment.clearDialpad();
1081 }
1082
Yorke Leeb207f8a2013-08-29 18:51:06 -07001083 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001084
Andrew Lee6324f702015-06-16 14:45:58 -07001085 // Restore the FAB for the lists fragment.
1086 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1087 mFloatingActionButtonController.setVisible(false);
1088 }
1089 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1090 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1091 onPageSelected(mListsFragment.getCurrentTabIndex());
1092
Andrew Leeb1903842014-05-15 16:11:24 -07001093 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001094 if (mSmartDialSearchFragment != null) {
1095 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001096 }
Andrew Leeb1903842014-05-15 16:11:24 -07001097 if (mRegularSearchFragment != null) {
1098 transaction.remove(mRegularSearchFragment);
1099 }
1100 transaction.commit();
1101
1102 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001103
1104 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001105 // If the dialpad fragment wasn't previously visible, then send a screen view because
1106 // we are exiting regular search. Otherwise, the screen view will be sent by
1107 // {@link #hideDialpadFragment}.
1108 mListsFragment.sendScreenViewForCurrentPosition();
1109 mListsFragment.setUserVisibleHint(true);
1110 }
1111
Yorke Lee5253be02014-05-21 18:50:03 -07001112 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001113 }
1114
Chiao Cheng94b10b52012-08-17 16:59:12 -07001115 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001116 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001117 if (mStateSaved) {
1118 return;
1119 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001120 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001121 if (TextUtils.isEmpty(mSearchQuery) ||
1122 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1123 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001124 exitSearchUi();
1125 }
Yorke Leef6673d32013-08-23 15:34:17 -07001126 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001127 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001128 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001129 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001130 } else {
1131 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001132 }
Yorke Leec3766332013-07-31 11:13:16 -07001133 }
1134
Yorke Lee81a313d2015-06-01 14:53:59 -07001135 private void maybeEnterSearchUi() {
1136 if (!isInSearchUi()) {
1137 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1138 }
1139 }
1140
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001141 /**
1142 * @return True if the search UI was exited, false otherwise
1143 */
1144 private boolean maybeExitSearchUi() {
1145 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1146 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001147 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001148 return true;
1149 }
1150 return false;
1151 }
1152
Andrew Lee6324f702015-06-16 14:45:58 -07001153 private void showFabInSearchUi() {
1154 mFloatingActionButtonController.changeIcon(
1155 getResources().getDrawable(R.drawable.fab_ic_dial),
1156 getResources().getString(R.string.action_menu_dialpad_button));
1157 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1158 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1159 }
1160
Yorke Leec3766332013-07-31 11:13:16 -07001161 @Override
1162 public void onDialpadQueryChanged(String query) {
Yorke Leed9a93162015-10-14 09:39:05 -07001163 mDialpadQuery = query;
Yorke Lee46635872014-04-11 11:20:15 -07001164 if (mSmartDialSearchFragment != null) {
1165 mSmartDialSearchFragment.setAddToContactNumber(query);
1166 }
Yorke Leec3766332013-07-31 11:13:16 -07001167 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1168 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001169
1170 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001171 if (DEBUG) {
1172 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1173 }
1174 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1175 // This callback can happen if the dialpad fragment is recreated because of
1176 // activity destruction. In that case, don't update the search view because
1177 // that would bring the user back to the search fragment regardless of the
1178 // previous state of the application. Instead, just return here and let the
1179 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001180 if (!TextUtils.isEmpty(normalizedQuery)) {
1181 mPendingSearchViewQuery = normalizedQuery;
1182 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001183 return;
1184 }
Yorke Lee53a22302014-04-29 18:13:46 -07001185 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001186 }
Santos Cordon83131712015-04-16 21:25:35 -07001187
1188 try {
1189 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1190 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1191 }
1192 } catch (Exception ignored) {
1193 // Skip any exceptions for this piece of code
1194 }
Yorke Lee575ae382015-07-16 11:36:07 -07001195 }
Santos Cordon83131712015-04-16 21:25:35 -07001196
Yorke Lee575ae382015-07-16 11:36:07 -07001197 @Override
1198 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1199 if (mInDialpadSearch && mSmartDialSearchFragment != null
1200 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1201 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1202 return true;
1203 }
1204 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001205 }
Yorke Leec3766332013-07-31 11:13:16 -07001206
1207 @Override
1208 public void onListFragmentScrollStateChange(int scrollState) {
1209 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001210 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001211 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001212 }
1213 }
1214
1215 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001216 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001217 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001218 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1219 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001220 }
1221
Yorke Leec3766332013-07-31 11:13:16 -07001222 private boolean phoneIsInUse() {
Yorke Lee827a90f2015-09-17 18:10:58 -07001223 return TelecomUtil.isInCall(this);
Yorke Leec3766332013-07-31 11:13:16 -07001224 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001225
Yorke Leeda0f9042013-12-05 14:25:51 -08001226 private boolean canIntentBeHandled(Intent intent) {
1227 final PackageManager packageManager = getPackageManager();
1228 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1229 PackageManager.MATCH_DEFAULT_ONLY);
1230 return resolveInfo != null && resolveInfo.size() > 0;
1231 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001232
Yorke Lee86e21f72014-04-02 16:49:38 -07001233 /**
1234 * Called when the user has long-pressed a contact tile to start a drag operation.
1235 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001236 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001237 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001238 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001239 }
1240
1241 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001242 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1243 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001244
Yorke Lee86e21f72014-04-02 16:49:38 -07001245 /**
1246 * Called when the user has released a contact tile after long-pressing it.
1247 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001248 @Override
1249 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001250 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001251 }
1252
1253 @Override
1254 public void onDroppedOnRemove() {}
1255
1256 /**
Yorke Leed999b712014-04-23 15:10:58 -07001257 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001258 * once it has been attached to the activity.
1259 */
1260 @Override
1261 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001262 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001263 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001264 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001265
Yorke Lee38019af2015-07-14 17:05:38 -07001266 /**
1267 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1268 */
1269 @Override
1270 public void showAllContactsTab() {
1271 if (mListsFragment != null) {
1272 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1273 }
1274 }
1275
1276 /**
1277 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1278 */
1279 @Override
1280 public void showDialpad() {
1281 showDialpadFragment(true);
1282 }
1283
Yorke Leee00c9fe2014-04-12 12:42:06 -07001284 @Override
Tyler Gunnb31f5d12015-12-18 13:57:37 -08001285 public void onPickDataUri(Uri dataUri, boolean isVideoCall, int callInitiationType) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001286 mClearSearchOnPause = true;
Yorke Leec8d6e162015-09-14 18:43:27 -07001287 PhoneNumberInteraction.startInteractionForPhoneCall(
Tyler Gunnb31f5d12015-12-18 13:57:37 -08001288 DialtactsActivity.this, dataUri, isVideoCall, callInitiationType);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001289 }
1290
1291 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001292 public void onPickPhoneNumber(String phoneNumber, boolean isVideoCall, int callInitiationType) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001293 if (phoneNumber == null) {
1294 // Invalid phone number, but let the call go through so that InCallUI can show
1295 // an error message.
1296 phoneNumber = "";
1297 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001298
1299 final Intent intent = new CallIntentBuilder(phoneNumber)
1300 .setIsVideoCall(isVideoCall)
Yorke Leec8d6e162015-09-14 18:43:27 -07001301 .setCallInitiationType(callInitiationType)
Yorke Leee3a2d132015-09-14 16:52:08 -07001302 .build();
1303
Yorke Lee7d20f822014-06-19 17:09:33 -07001304 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001305 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001306 }
1307
1308 @Override
1309 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001310 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001311 }
1312
1313 @Override
1314 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001315 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001316 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001317
Yorke Leecc4660d2014-04-24 11:22:57 -07001318 @Override
1319 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001320 int tabIndex = mListsFragment.getCurrentTabIndex();
1321
Andrew Lee4de59fb2015-08-13 15:20:08 -07001322 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1323 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1324 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001325 boolean isRtl = DialerUtils.isRtl();
1326 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1327 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001328 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001329 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1330 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001331 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001332 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001333 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001334
Andrew Lee432d4b52015-05-20 16:52:00 -07001335 @Override
1336 public void onPageSelected(int position) {
Sarmad Hashmicb559812016-02-10 18:39:59 -08001337 updateMissedCalls();
Andrew Lee432d4b52015-05-20 16:52:00 -07001338 int tabIndex = mListsFragment.getCurrentTabIndex();
Sarmad Hashmicb559812016-02-10 18:39:59 -08001339 mPreviouslySelectedTabIndex = tabIndex;
Andrew Lee432d4b52015-05-20 16:52:00 -07001340 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001341 mFloatingActionButtonController.changeIcon(
1342 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1343 getResources().getString(R.string.search_shortcut_create_new_contact));
1344 } else {
1345 mFloatingActionButtonController.changeIcon(
1346 getResources().getDrawable(R.drawable.fab_ic_dial),
1347 getResources().getString(R.string.action_menu_dialpad_button));
1348 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001349 }
1350
1351 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001352 public void onPageScrollStateChanged(int state) {
1353 }
1354
Yorke Lee5253be02014-05-21 18:50:03 -07001355 @Override
1356 public boolean isActionBarShowing() {
1357 return mActionBarController.isActionBarShowing();
1358 }
1359
Yorke Leec98a5bb2014-10-28 16:12:05 -07001360 @Override
1361 public ActionBarController getActionBarController() {
1362 return mActionBarController;
1363 }
1364
Yorke Lee6bc67162015-06-27 14:03:25 -07001365 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001366 public boolean isDialpadShown() {
1367 return mIsDialpadShown;
1368 }
1369
Yorke Lee5253be02014-05-21 18:50:03 -07001370 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001371 public int getDialpadHeight() {
1372 if (mDialpadFragment != null) {
1373 return mDialpadFragment.getDialpadHeight();
1374 }
1375 return 0;
1376 }
1377
1378 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001379 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001380 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001381 }
1382
1383 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001384 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001385 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001386 }
1387
1388 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001389 public int getActionBarHeight() {
1390 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001391 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001392
Andrew Lee6324f702015-06-16 14:45:58 -07001393 private int getFabAlignment() {
1394 if (!mIsLandscape && !isInSearchUi() &&
1395 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1396 return FloatingActionButtonController.ALIGN_MIDDLE;
1397 }
1398 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001399 }
Sarmad Hashmicb559812016-02-10 18:39:59 -08001400
1401 private void updateMissedCalls() {
1402 if (mPreviouslySelectedTabIndex == ListsFragment.TAB_INDEX_HISTORY) {
1403 mListsFragment.markMissedCallsAsReadAndRemoveNotifications();
1404 }
1405 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001406}