blob: 441501cfd33eaa7e9acaa64a13ca10b1a106b07a [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;
Ihab Awad9134e252014-07-09 12:32:52 -070049import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070050import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070051import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070052import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070053import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070054import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070055import android.widget.PopupMenu;
Anne Rong35208002015-07-22 16:27:37 -070056import android.widget.TextView;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080057import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070058
Yorke Leec3766332013-07-31 11:13:16 -070059import com.android.contacts.common.dialog.ClearFrequentsDialog;
60import com.android.contacts.common.interactions.ImportExportDialogFragment;
Nancy Chen0d3a7392014-08-12 14:44:53 -070061import com.android.contacts.common.interactions.TouchPointManager;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080062import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec16ea5a2015-05-19 15:51:01 -070063import com.android.contacts.common.util.PermissionsUtil;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -070064import com.android.contacts.common.widget.FloatingActionButtonController;
Yorke Leec3766332013-07-31 11:13:16 -070065import com.android.dialer.calllog.CallLogActivity;
Yorke Lee38019af2015-07-14 17:05:38 -070066import com.android.dialer.calllog.CallLogFragment;
Yorke Leefce269a2013-08-12 11:56:04 -070067import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070068import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070069import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070070import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070071import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080072import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070073import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080074import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070075import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070076import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070077import com.android.dialer.list.RegularSearchFragment;
78import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070079import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070080import com.android.dialer.list.SpeedDialFragment;
Yorke Lee6e0fd6b2015-11-04 20:44:25 -080081import com.android.dialer.logging.Logger;
Yorke Lee58e68a92015-11-06 10:35:30 -080082import com.android.dialer.logging.ScreenEvent;
Yorke Leeaf6f1952014-07-14 19:13:16 -070083import com.android.dialer.settings.DialerSettingsActivity;
Ta-wei Yenbcb15f92016-03-07 15:29:13 -080084import com.android.dialer.util.Assert;
Yorke Lee7d20f822014-06-19 17:09:33 -070085import com.android.dialer.util.DialerUtils;
Ta-wei Yene3cae632015-10-27 16:09:09 -070086import com.android.dialer.util.IntentUtil;
87import com.android.dialer.util.IntentUtil.CallIntentBuilder;
88import com.android.dialer.util.TelecomUtil;
Ta-wei Yenbcb15f92016-03-07 15:29:13 -080089import com.android.dialer.voicemail.VoicemailArchiveActivity;
Yorke Lee5253be02014-05-21 18:50:03 -070090import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070091import com.android.dialer.widget.SearchEditTextLayout;
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 Lee2fec47b2014-08-05 18:16:27 -070094import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070095import com.android.phone.common.animation.AnimationListenerAdapter;
Ta-wei Yenbcb15f92016-03-07 15:29:13 -080096import com.google.common.annotations.VisibleForTesting;
Yorke Leed5c512a2015-01-30 14:42:53 -080097
Yorke Leec3766332013-07-31 11:13:16 -070098import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080099import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -0700100
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700102 * The dialer tab's title is 'phone', a more common name (see strings.xml).
103 */
Yorke Leec3766332013-07-31 11:13:16 -0700104public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700105 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700106 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700107 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700108 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700109 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700110 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700111 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700112 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700113 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700114 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700115 ViewPager.OnPageChangeListener,
116 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700117 private static final String TAG = "DialtactsActivity";
118
Yorke Lee7cc61492015-06-01 14:59:33 -0700119 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700120
Yorke Leef74011e2013-08-02 11:30:30 -0700121 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
122
Yorke Leeb207f8a2013-08-29 18:51:06 -0700123 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
124 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700125 private static final String KEY_SEARCH_QUERY = "search_query";
126 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700127 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700128
Yorke Lee135e4652015-10-23 18:12:51 -0700129 @VisibleForTesting
130 public static final String TAG_DIALPAD_FRAGMENT = "dialpad";
Yorke Leec3766332013-07-31 11:13:16 -0700131 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
132 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
133 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700134
Chiao Cheng94b10b52012-08-17 16:59:12 -0700135 /**
136 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
137 */
138 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700139 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700140
Yorke Leec3766332013-07-31 11:13:16 -0700141 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700142
Andrew Lee6324f702015-06-16 14:45:58 -0700143 private static final int FAB_SCALE_IN_DELAY_MS = 300;
144
Nancy Chenf3d9f822015-09-11 00:47:31 -0400145 private CoordinatorLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700146
Yorke Leec3766332013-07-31 11:13:16 -0700147 /**
Yorke Leec3766332013-07-31 11:13:16 -0700148 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700149 */
Evan Charlton72d66252014-11-07 16:23:47 -0800150 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700151
152 /**
153 * Fragment for searching phone numbers using the alphanumeric keyboard.
154 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700155 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700156
157 /**
158 * Fragment for searching phone numbers using the dialpad.
159 */
160 private SmartDialSearchFragment mSmartDialSearchFragment;
161
Yorke Leee00c9fe2014-04-12 12:42:06 -0700162 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700163 * Animation that slides in.
164 */
165 private Animation mSlideIn;
166
167 /**
168 * Animation that slides out.
169 */
170 private Animation mSlideOut;
171
Yorke Lee6c450eb2015-05-08 09:45:47 -0700172 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
173 @Override
174 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700175 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700176 }
177 };
178
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700179 /**
180 * Listener for after slide out animation completes on dialer fragment.
181 */
182 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
183 @Override
184 public void onAnimationEnd(Animation animation) {
185 commitDialpadFragmentHide();
186 }
187 };
188
189 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700190 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700191 */
192 private ListsFragment mListsFragment;
193
Yorke Lee19e68ca2014-10-28 11:51:40 -0700194 /**
195 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
196 * be commited.
197 */
198 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800199 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700200 private boolean mInDialpadSearch;
201 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700202 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700203 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700204 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700205
Yorke Leeef2b7382013-08-09 17:39:25 -0700206 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700207 * Whether or not the device is in landscape orientation.
208 */
209 private boolean mIsLandscape;
210
211 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700212 * True if the dialpad is only temporarily showing due to being in call
213 */
214 private boolean mInCallDialpadUp;
215
216 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700217 * True when this activity has been launched for the first time.
218 */
Yorke Lee98702de2013-08-06 12:03:32 -0700219 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700220
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700221 /**
222 * Search query to be applied to the SearchView in the ActionBar once
223 * onCreateOptionsMenu has been called.
224 */
225 private String mPendingSearchViewQuery;
226
Yorke Lee74edcdc2014-07-11 16:15:08 -0700227 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700228 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700229 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700230
Yorke Leeef2b7382013-08-09 17:39:25 -0700231 private String mSearchQuery;
Yorke Leed9a93162015-10-14 09:39:05 -0700232 private String mDialpadQuery;
Yorke Leeef2b7382013-08-09 17:39:25 -0700233
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;
Sarmad Hashmicb559812016-02-10 18:39:59 -0800241 private int mPreviouslySelectedTabIndex;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700242
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800243 /**
244 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
245 * {@link #onResume()} to populate the search box.
246 */
247 private String mVoiceSearchQuery;
248
Andrew Lee467de3d2015-03-17 15:47:24 -0700249 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700250 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700251 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700252 }
253
254 @Override
255 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700256 final boolean hasContactsPermission =
257 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700258 final Menu menu = getMenu();
259 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700260 clearFrequents.setVisible(mListsFragment != null &&
261 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700262 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
263
264 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
265 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700266
267 menu.findItem(R.id.menu_history).setVisible(
268 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700269 super.show();
270 }
271 }
272
Chiao Cheng94b10b52012-08-17 16:59:12 -0700273 /**
Yorke Lee28266192014-05-01 10:05:52 -0700274 * Listener that listens to drag events and sends their x and y coordinates to a
275 * {@link DragDropController}.
276 */
277 private class LayoutOnDragListener implements OnDragListener {
278 @Override
279 public boolean onDrag(View v, DragEvent event) {
280 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700281 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700282 }
283 return true;
284 }
285 }
286
287 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700288 * Listener used to send search queries to the phone search fragment.
289 */
Yorke Lee53a22302014-04-29 18:13:46 -0700290 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700291 @Override
292 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
293 }
Yorke Leec3766332013-07-31 11:13:16 -0700294
Andrew Lee99a570c2014-05-15 14:18:50 -0700295 @Override
296 public void onTextChanged(CharSequence s, int start, int before, int count) {
297 final String newText = s.toString();
298 if (newText.equals(mSearchQuery)) {
299 // If the query hasn't changed (perhaps due to activity being destroyed
300 // and restored, or user launching the same DIAL intent twice), then there is
301 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700302 return;
303 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700304 if (DEBUG) {
305 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700306 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700307 }
Yorke Lee710709d2014-05-29 07:18:42 -0700308 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700309
Andrew Lee90374a72014-05-16 15:01:09 -0700310 // Show search fragment only when the query string is changed to non-empty text.
311 if (!TextUtils.isEmpty(newText)) {
312 // Call enterSearchUi only if we are switching search modes, or showing a search
313 // fragment for the first time.
314 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
315 (!mIsDialpadShown && mInRegularSearch);
316 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700317 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700318 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700319 }
320
Andrew Leea8515422014-06-13 16:53:54 -0700321 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700322 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700323 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700324 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700325 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700326 }
327
328 @Override
329 public void afterTextChanged(Editable s) {
330 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700331 };
332
Andrew Leeb1903842014-05-15 16:11:24 -0700333
334 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700335 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700336 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700337 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700338 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700339 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700340 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700341 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700342 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
343 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700344 }
Andrew Leeb1903842014-05-15 16:11:24 -0700345 }
346 };
347
348 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700349 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700350 */
351 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
352 @Override
353 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700354 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
355 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
356 // If the search term is empty, close the search UI.
357 maybeExitSearchUi();
358 } else {
359 // If the search term is not empty, show the dialpad fab.
360 showFabInSearchUi();
361 }
Andrew Leeb1903842014-05-15 16:11:24 -0700362 }
363 return false;
364 }
365 };
366
Chiao Cheng94b10b52012-08-17 16:59:12 -0700367 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700368 public boolean dispatchTouchEvent(MotionEvent ev) {
369 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
370 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
371 }
372 return super.dispatchTouchEvent(ev);
Nancy Chen49db1ad2014-08-18 20:10:17 -0700373 }
374
375 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700376 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800377 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700378 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700379
Yorke Lee98702de2013-08-06 12:03:32 -0700380 mFirstLaunch = true;
381
Andrew Lee2423a2f2014-05-29 14:14:45 -0700382 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700383 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700384
Yorke Leef5554ce2015-01-27 14:46:02 -0800385 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700386 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800387 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800388 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700389
Yorke Leef5554ce2015-01-27 14:46:02 -0800390 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700391 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700392 actionBar.setCustomView(R.layout.search_edittext);
393 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700394 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700395
Anne Rong35208002015-07-22 16:27:37 -0700396 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
397 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700398 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
399
Andrew Leee3f59a82015-02-26 12:06:35 -0800400 mActionBarController = new ActionBarController(this, searchEditTextLayout);
401
Andrew Lee04675a52014-06-05 18:36:20 -0700402 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700403 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700404 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
405 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
406 .setOnClickListener(mSearchViewOnClickListener);
407 searchEditTextLayout.findViewById(R.id.search_box_start_search)
408 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700409 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700410 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700411 @Override
412 public void onBackButtonClicked() {
413 onBackPressed();
414 }
Andrew Lee6324f702015-06-16 14:45:58 -0700415
416 @Override
417 public void onSearchViewClicked() {
418 // Hide FAB, as the keyboard is shown.
419 mFloatingActionButtonController.scaleOut();
420 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700421 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700422
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700423 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700424 == Configuration.ORIENTATION_LANDSCAPE;
Sarmad Hashmicb559812016-02-10 18:39:59 -0800425 mPreviouslySelectedTabIndex = ListsFragment.TAB_INDEX_SPEED_DIAL;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700426 final View floatingActionButtonContainer = findViewById(
427 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700428 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
429 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700430 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700431 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700432
Andrew Lee04675a52014-06-05 18:36:20 -0700433 ImageButton optionsMenuButton =
434 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700435 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700436 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
437 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700438
Yorke Leed5c512a2015-01-30 14:42:53 -0800439 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
440 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700441 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800442 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000443 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800444 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700445 } else {
446 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700447 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
448 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700449 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700450 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700451 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700452 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700453
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700454 final boolean isLayoutRtl = DialerUtils.isRtl();
455 if (mIsLandscape) {
456 mSlideIn = AnimationUtils.loadAnimation(this,
457 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
458 mSlideOut = AnimationUtils.loadAnimation(this,
459 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
460 } else {
461 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
462 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
463 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700464
Yorke Lee2fec47b2014-08-05 18:16:27 -0700465 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
466 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
467
Yorke Lee6c450eb2015-05-08 09:45:47 -0700468 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700469 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700470
Nancy Chenf3d9f822015-09-11 00:47:31 -0400471 mParentLayout = (CoordinatorLayout) findViewById(R.id.dialtacts_mainlayout);
Yorke Leef614f6f2014-10-03 10:44:19 -0700472 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700473 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700474 new ViewTreeObserver.OnGlobalLayoutListener() {
475 @Override
476 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700477 final ViewTreeObserver observer =
478 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700479 if (!observer.isAlive()) {
480 return;
481 }
482 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700483 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700484 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700485 mFloatingActionButtonController.align(
486 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700487 }
488 });
Andrew Lee0c667702014-05-12 14:31:45 -0700489
Yorke Leef5554ce2015-01-27 14:46:02 -0800490 Trace.endSection();
491
492 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700493 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700494 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800495 Trace.endSection();
496 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700497 }
498
499 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700500 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800501 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700502 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700503
Yorke Lee19e68ca2014-10-28 11:51:40 -0700504 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700505 if (mFirstLaunch) {
506 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700507 } else if (!phoneIsInUse() && mInCallDialpadUp) {
508 hideDialpadFragment(false, true);
509 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700510 } else if (mShowDialpadOnResume) {
511 showDialpadFragment(false);
512 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700513 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800514
515 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
516 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
517 // shown until onResume has completed. Active the search UI and set the search term now.
518 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
519 mActionBarController.onSearchBoxTapped();
520 mSearchView.setText(mVoiceSearchQuery);
521 mVoiceSearchQuery = null;
522 }
523
Yorke Lee98702de2013-08-06 12:03:32 -0700524 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800525
526 if (mIsRestarting) {
527 // This is only called when the activity goes from resumed -> paused -> resumed, so it
528 // will not cause an extra view to be sent out on rotation
529 if (mIsDialpadShown) {
Yorke Lee58e68a92015-11-06 10:35:30 -0800530 Logger.logScreenView(ScreenEvent.DIALPAD, this);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800531 }
532 mIsRestarting = false;
533 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700534
Yorke Lee53a22302014-04-29 18:13:46 -0700535 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700536 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700537 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700538
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700539 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
540 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
541 // used internally.
542 final Bundle extras = getIntent().getExtras();
543 if (extras != null
544 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
545 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
546 } else {
547 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
548 }
549 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700550 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
551 if (index < mListsFragment.getTabCount()) {
552 mListsFragment.showTab(index);
553 }
554 }
555
Anne Rong35208002015-07-22 16:27:37 -0700556 setSearchBoxHint();
557
Yorke Leef5554ce2015-01-27 14:46:02 -0800558 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700559 }
560
561 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800562 protected void onRestart() {
563 super.onRestart();
564 mIsRestarting = true;
565 }
566
567 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700568 protected void onPause() {
Sarmad Hashmicb559812016-02-10 18:39:59 -0800569 // Only clear missed calls if the pause was not triggered by an orientation change
570 // (or any other confirguration change)
571 if (!isChangingConfigurations()) {
572 updateMissedCalls();
573 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700574 if (mClearSearchOnPause) {
575 hideDialpadAndSearchUi();
576 mClearSearchOnPause = false;
577 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700578 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
579 commitDialpadFragmentHide();
580 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700581 super.onPause();
582 }
583
584 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700585 protected void onSaveInstanceState(Bundle outState) {
586 super.onSaveInstanceState(outState);
587 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700588 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
589 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700590 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700591 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700592 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700593 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700594 }
595
596 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700597 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700598 if (fragment instanceof DialpadFragment) {
599 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800600 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700601 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
602 transaction.hide(mDialpadFragment);
603 transaction.commit();
604 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700605 } else if (fragment instanceof SmartDialSearchFragment) {
606 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700607 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leed9a93162015-10-14 09:39:05 -0700608 if (!TextUtils.isEmpty(mDialpadQuery)) {
609 mSmartDialSearchFragment.setAddToContactNumber(mDialpadQuery);
610 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700611 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700612 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700613 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700614 } else if (fragment instanceof ListsFragment) {
615 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700616 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700617 }
Yorke Leec3766332013-07-31 11:13:16 -0700618 }
619
Alon Albertb453e5b2013-09-10 15:54:23 -0700620 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700621 final Intent intent = new Intent(this, DialerSettingsActivity.class);
622 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700623 }
624
Chiao Cheng94b10b52012-08-17 16:59:12 -0700625 @Override
626 public void onClick(View view) {
Sailesh Nepal68d86c62016-02-21 11:41:28 -0800627 int resId = view.getId();
628 if (resId == R.id.floating_action_button) {
629 if (mListsFragment.getCurrentTabIndex()
Naoki Amano10c93822016-09-13 18:56:57 +0900630 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch &&
631 !mInDialpadSearch) {
Sailesh Nepal68d86c62016-02-21 11:41:28 -0800632 DialerUtils.startActivityWithErrorToast(
633 this,
634 IntentUtil.getNewContactIntent(),
635 R.string.add_contact_not_available);
636 } else if (!mIsDialpadShown) {
637 mInCallDialpadUp = false;
638 showDialpadFragment(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700639 }
Sailesh Nepal68d86c62016-02-21 11:41:28 -0800640 } else if (resId == R.id.voice_search_button) {
641 try {
642 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
643 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
644 } catch (ActivityNotFoundException e) {
645 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
646 Toast.LENGTH_SHORT).show();
647 }
648 } else if (resId == R.id.dialtacts_options_menu_button) {
649 mOverflowMenu.show();
650 } else {
651 Log.wtf(TAG, "Unexpected onClick event from " + view);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700652 }
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
Sailesh Nepal68d86c62016-02-21 11:41:28 -0800661 int resId = item.getItemId();
662 if (resId == R.id.menu_history) {// Use explicit CallLogActivity intent instead of ACTION_VIEW +
663 // CONTENT_TYPE, so that we always open our call log from our dialer
664 final Intent intent = new Intent(this, CallLogActivity.class);
665 startActivity(intent);
666 } else if (resId == R.id.menu_add_contact) {
667 DialerUtils.startActivityWithErrorToast(
668 this,
669 IntentUtil.getNewContactIntent(),
670 R.string.add_contact_not_available);
671 } else if (resId == R.id.menu_import_export) {// We hard-code the "contactsAreAvailable" argument because doing it properly would
672 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
673 // now in Dialtacts for (potential) performance reasons. Compare with how it is
674 // done in {@link PeopleActivity}.
675 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
676 ImportExportDialogFragment.show(getFragmentManager(), true,
677 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_FAVORITES);
678 } else {
679 ImportExportDialogFragment.show(getFragmentManager(), true,
680 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_DEFAULT);
681 }
682 Logger.logScreenView(ScreenEvent.IMPORT_EXPORT_CONTACTS, this);
683 return true;
684 } else if (resId == R.id.menu_clear_frequents) {
685 ClearFrequentsDialog.show(getFragmentManager());
686 Logger.logScreenView(ScreenEvent.CLEAR_FREQUENTS, this);
687 return true;
688 } else if (resId == R.id.menu_call_settings) {
689 handleMenuSettings();
690 Logger.logScreenView(ScreenEvent.SETTINGS, this);
691 return true;
Sarmad Hashmi3694b112016-02-17 17:39:36 -0800692 } else if (resId == R.id.menu_archive) {
693 final Intent intent = new Intent(this, VoicemailArchiveActivity.class);
694 startActivity(intent);
695 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700696 }
697 return false;
698 }
699
700 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700701 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
702 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
703 if (resultCode == RESULT_OK) {
704 final ArrayList<String> matches = data.getStringArrayListExtra(
705 RecognizerIntent.EXTRA_RESULTS);
706 if (matches.size() > 0) {
707 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800708 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700709 } else {
710 Log.e(TAG, "Voice search - nothing heard");
711 }
712 } else {
713 Log.e(TAG, "Voice search failed");
714 }
715 }
716 super.onActivityResult(requestCode, resultCode, data);
717 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700718
Andrew Lee2a58ab82014-05-15 02:16:04 -0700719 /**
Nancy Chenea7c0b82015-08-31 17:18:41 -0700720 * Update the number of unread voicemails (potentially other tabs) displayed next to the tab
721 * icon.
722 */
723 public void updateTabUnreadCounts() {
724 mListsFragment.updateTabUnreadCounts();
725 }
726
727 /**
Andrew Lee2a58ab82014-05-15 02:16:04 -0700728 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
729 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
730 * @see #onDialpadShown
731 */
Yorke Leec3766332013-07-31 11:13:16 -0700732 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700733 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700734 return;
735 }
736 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800737
Yorke Lee7ee5c422014-11-04 10:29:32 -0800738 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700739
Chiao Cheng94b10b52012-08-17 16:59:12 -0700740 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800741 if (mDialpadFragment == null) {
742 mDialpadFragment = new DialpadFragment();
743 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
744 } else {
745 ft.show(mDialpadFragment);
746 }
747
748 mDialpadFragment.setAnimate(animate);
Yorke Lee58e68a92015-11-06 10:35:30 -0800749 Logger.logScreenView(ScreenEvent.DIALPAD, this);
Yorke Leec3766332013-07-31 11:13:16 -0700750 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700751
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700752 if (animate) {
753 mFloatingActionButtonController.scaleOut();
754 } else {
755 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700756 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700757 }
Yorke Lee5253be02014-05-21 18:50:03 -0700758 mActionBarController.onDialpadUp();
759
Yorke Lee6c450eb2015-05-08 09:45:47 -0700760 mListsFragment.getView().animate().alpha(0).withLayer();
Ta-wei Yene3cae632015-10-27 16:09:09 -0700761
762 //adjust the title, so the user will know where we're at when the activity start/resumes.
763 setTitle(R.string.launcherDialpadActivityLabel);
Yorke Leec3766332013-07-31 11:13:16 -0700764 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700765
Andrew Lee2a58ab82014-05-15 02:16:04 -0700766 /**
767 * Callback from child DialpadFragment when the dialpad is shown.
768 */
769 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800770 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700771 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700772 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700773 } else {
774 mDialpadFragment.setYFraction(0);
775 }
776
Andrew Lee2a58ab82014-05-15 02:16:04 -0700777 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700778 }
779
780 /**
781 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
782 * a callback after the hide animation ends.
783 * @see #commitDialpadFragmentHide
784 */
Yorke Leeca195042013-09-25 16:29:38 -0700785 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700786 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700787 return;
788 }
Yorke Leef6673d32013-08-23 15:34:17 -0700789 if (clearDialpad) {
Ta-wei Yenaf875dd2015-11-03 15:57:03 -0800790 // Temporarily disable accessibility when we clear the dialpad, since it should be
791 // invisible and should not announce anything.
792 mDialpadFragment.getDigitsWidget().setImportantForAccessibility(
793 View.IMPORTANT_FOR_ACCESSIBILITY_NO);
Yorke Leef6673d32013-08-23 15:34:17 -0700794 mDialpadFragment.clearDialpad();
Ta-wei Yenaf875dd2015-11-03 15:57:03 -0800795 mDialpadFragment.getDigitsWidget().setImportantForAccessibility(
796 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Yorke Leef6673d32013-08-23 15:34:17 -0700797 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700798 if (!mIsDialpadShown) {
799 return;
Yorke Leec3766332013-07-31 11:13:16 -0700800 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700801 mIsDialpadShown = false;
802 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800803 mListsFragment.setUserVisibleHint(true);
804 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700805
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700806 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700807
Andrew Lee6324f702015-06-16 14:45:58 -0700808 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700809 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700810 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700811 } else {
812 commitDialpadFragmentHide();
813 }
814
Yorke Lee5253be02014-05-21 18:50:03 -0700815 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700816
Andrew Leed4cde832014-05-16 15:56:48 -0700817 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700818 if (TextUtils.isEmpty(mSearchQuery)) {
819 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700820 }
Andrew Leed4cde832014-05-16 15:56:48 -0700821 }
Ta-wei Yene3cae632015-10-27 16:09:09 -0700822 //reset the title to normal.
823 setTitle(R.string.launcherActivityLabel);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700824 }
825
826 /**
827 * Finishes hiding the dialpad fragment after any animations are completed.
828 */
829 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800830 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700831 final FragmentTransaction ft = getFragmentManager().beginTransaction();
832 ft.hide(mDialpadFragment);
833 ft.commit();
834 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700835 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700836 }
837
Andrew Lee2a58ab82014-05-15 02:16:04 -0700838 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700839 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700840 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700841 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700842 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700843 fragment = mRegularSearchFragment;
844 }
845 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700846 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700847 }
848 }
849
Yorke Lee5253be02014-05-21 18:50:03 -0700850 @Override
851 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700852 return mInDialpadSearch || mInRegularSearch;
853 }
854
Yorke Lee5253be02014-05-21 18:50:03 -0700855 @Override
856 public boolean hasSearchQuery() {
857 return !TextUtils.isEmpty(mSearchQuery);
858 }
859
860 @Override
861 public boolean shouldShowActionBar() {
862 return mListsFragment.shouldShowActionBar();
863 }
864
Yorke Leeb207f8a2013-08-29 18:51:06 -0700865 private void setNotInSearchUi() {
866 mInDialpadSearch = false;
867 mInRegularSearch = false;
868 }
869
Yorke Lee311969c2013-08-26 06:31:11 -0700870 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700871 if (mIsDialpadShown) {
872 hideDialpadFragment(false, true);
873 } else {
874 exitSearchUi();
875 }
Yorke Lee311969c2013-08-26 06:31:11 -0700876 }
877
Yorke Lee53a22302014-04-29 18:13:46 -0700878 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700879 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
880 if (canIntentBeHandled(voiceIntent)) {
881 mVoiceSearchButton.setVisibility(View.VISIBLE);
882 mVoiceSearchButton.setOnClickListener(this);
883 } else {
884 mVoiceSearchButton.setVisibility(View.GONE);
885 }
886 }
887
Yorke Leee9c82332015-10-06 14:21:53 -0700888 public boolean isNearbyPlacesSearchEnabled() {
889 return false;
890 }
891
Anne Rong35208002015-07-22 16:27:37 -0700892 protected int getSearchBoxHint () {
893 return R.string.dialer_hint_find_contact;
894 }
895
896 /**
897 * Sets the hint text for the contacts search box
898 */
899 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700900 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700901 .getCustomView().findViewById(R.id.search_view_container);
902 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
903 .setHint(getSearchBoxHint());
904 }
905
Andrew Lee467de3d2015-03-17 15:47:24 -0700906 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700907 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
908 popupMenu.inflate(R.menu.dialtacts_options);
Sarmad Hashmi78f55ff2016-03-04 12:32:17 -0800909 if (ObjectFactory.isVoicemailArchiveEnabled(this)) {
910 popupMenu.getMenu().findItem(R.id.menu_archive).setVisible(true);
911 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700912 popupMenu.setOnMenuItemClickListener(this);
913 return popupMenu;
914 }
915
Yorke Lee86e21f72014-04-02 16:49:38 -0700916 @Override
917 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700918 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700919 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700920 mPendingSearchViewQuery = null;
921 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700922 if (mActionBarController != null) {
923 mActionBarController.restoreActionBarOffset();
924 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700925 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700926 }
927
Chiao Cheng94b10b52012-08-17 16:59:12 -0700928 /**
929 * Returns true if the intent is due to hitting the green send key (hardware call button:
930 * KEYCODE_CALL) while in a call.
931 *
932 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700933 * @return true if the intent is due to hitting the green send key while in a call
934 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700935 private boolean isSendKeyWhileInCall(Intent intent) {
936 // If there is a call in progress and the user launched the dialer by hitting the call
937 // button, go straight to the in-call screen.
938 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700939
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700940 if (callKey) {
Yorke Lee762b3572015-09-18 12:54:59 -0700941 TelecomUtil.showInCallScreen(this, false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700942 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700943 }
944
945 return false;
946 }
947
948 /**
949 * Sets the current tab based on the intent's request type
950 *
951 * @param intent Intent that contains information about which tab should be selected
952 */
Yorke Leec3766332013-07-31 11:13:16 -0700953 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700954 // 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 -0700955 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700956 finish();
957 return;
958 }
959
Yorke Leed77017d2015-08-31 16:19:43 -0700960 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
961 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800962 showDialpadFragment(false);
963 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700964 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800965 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700966 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700967 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700968 }
969
970 @Override
971 public void onNewIntent(Intent newIntent) {
972 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700973
Nancy Chen199b7f02014-11-05 15:03:00 -0800974 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700975 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700976
Chiao Cheng94b10b52012-08-17 16:59:12 -0700977 invalidateOptionsMenu();
978 }
979
980 /** Returns true if the given intent contains a phone number to populate the dialer with */
981 private boolean isDialIntent(Intent intent) {
982 final String action = intent.getAction();
983 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
984 return true;
985 }
986 if (Intent.ACTION_VIEW.equals(action)) {
987 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700988 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700989 return true;
990 }
991 }
992 return false;
993 }
994
995 /**
Yorke Leec3766332013-07-31 11:13:16 -0700996 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700997 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700998 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700999 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -07001000 // Weird race condition where fragment is doing work after the activity is destroyed
1001 // due to talkback being on (b/10209937). Just return since we can't do any
1002 // constructive here.
1003 return;
1004 }
1005
Yorke Leeef2b7382013-08-09 17:39:25 -07001006 if (DEBUG) {
1007 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
1008 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001009
Yorke Leec3766332013-07-31 11:13:16 -07001010 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001011 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001012 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001013 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001014 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -07001015 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001016
Yorke Leeb207f8a2013-08-29 18:51:06 -07001017 final String tag;
1018 if (smartDialSearch) {
1019 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
1020 } else {
1021 tag = TAG_REGULAR_SEARCH_FRAGMENT;
1022 }
1023 mInDialpadSearch = smartDialSearch;
1024 mInRegularSearch = !smartDialSearch;
1025
Andrew Lee6324f702015-06-16 14:45:58 -07001026 mFloatingActionButtonController.scaleOut();
1027
Andrew Lee752956e2014-05-15 11:43:38 -07001028 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001029 if (animate) {
1030 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
1031 } else {
1032 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
1033 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001034 if (fragment == null) {
1035 if (smartDialSearch) {
1036 fragment = new SmartDialSearchFragment();
1037 } else {
Yorke Lee807afd82015-10-12 10:03:07 -07001038 fragment = ObjectFactory.newRegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001039 fragment.setOnTouchListener(new View.OnTouchListener() {
1040 @Override
1041 public boolean onTouch(View v, MotionEvent event) {
1042 // Show the FAB when the user touches the lists fragment and the soft
1043 // keyboard is hidden.
Nancy Chen0bb96ac2016-01-15 16:21:27 -08001044 hideDialpadFragment(true, false);
Andrew Lee6324f702015-06-16 14:45:58 -07001045 showFabInSearchUi();
1046 return false;
1047 }
1048 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001049 }
Andrew Leeb1903842014-05-15 16:11:24 -07001050 transaction.add(R.id.dialtacts_frame, fragment, tag);
1051 } else {
1052 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001053 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001054 // DialtactsActivity will provide the options menu
1055 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001056 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001057 if (!smartDialSearch) {
1058 fragment.setQueryString(query, false /* delaySelection */);
1059 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001060 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001061
Yorke Lee6c450eb2015-05-08 09:45:47 -07001062 if (animate) {
1063 mListsFragment.getView().animate().alpha(0).withLayer();
1064 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001065 mListsFragment.setUserVisibleHint(false);
Yorke Leef96bb182015-11-10 10:06:21 -08001066
1067 if (smartDialSearch) {
1068 Logger.logScreenView(ScreenEvent.SMART_DIAL_SEARCH, this);
1069 } else {
1070 Logger.logScreenView(ScreenEvent.REGULAR_SEARCH, this);
1071 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001072 }
1073
1074 /**
Yorke Leec3766332013-07-31 11:13:16 -07001075 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001076 */
Yorke Leec3766332013-07-31 11:13:16 -07001077 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001078 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001079 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001080 return;
1081 }
Andrew Leeb1903842014-05-15 16:11:24 -07001082
Andrew Leeb1903842014-05-15 16:11:24 -07001083 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001084
1085 if (mDialpadFragment != null) {
1086 mDialpadFragment.clearDialpad();
1087 }
1088
Yorke Leeb207f8a2013-08-29 18:51:06 -07001089 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001090
Andrew Lee6324f702015-06-16 14:45:58 -07001091 // Restore the FAB for the lists fragment.
1092 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1093 mFloatingActionButtonController.setVisible(false);
1094 }
1095 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1096 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1097 onPageSelected(mListsFragment.getCurrentTabIndex());
1098
Andrew Leeb1903842014-05-15 16:11:24 -07001099 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001100 if (mSmartDialSearchFragment != null) {
1101 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001102 }
Andrew Leeb1903842014-05-15 16:11:24 -07001103 if (mRegularSearchFragment != null) {
1104 transaction.remove(mRegularSearchFragment);
1105 }
1106 transaction.commit();
1107
1108 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001109
1110 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001111 // If the dialpad fragment wasn't previously visible, then send a screen view because
1112 // we are exiting regular search. Otherwise, the screen view will be sent by
1113 // {@link #hideDialpadFragment}.
1114 mListsFragment.sendScreenViewForCurrentPosition();
1115 mListsFragment.setUserVisibleHint(true);
1116 }
1117
Yorke Lee5253be02014-05-21 18:50:03 -07001118 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001119 }
1120
Chiao Cheng94b10b52012-08-17 16:59:12 -07001121 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001122 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001123 if (mStateSaved) {
1124 return;
1125 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001126 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001127 if (TextUtils.isEmpty(mSearchQuery) ||
1128 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1129 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001130 exitSearchUi();
1131 }
Yorke Leef6673d32013-08-23 15:34:17 -07001132 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001133 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001134 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001135 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001136 } else {
1137 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001138 }
Yorke Leec3766332013-07-31 11:13:16 -07001139 }
1140
Yorke Lee81a313d2015-06-01 14:53:59 -07001141 private void maybeEnterSearchUi() {
1142 if (!isInSearchUi()) {
1143 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1144 }
1145 }
1146
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001147 /**
1148 * @return True if the search UI was exited, false otherwise
1149 */
1150 private boolean maybeExitSearchUi() {
1151 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1152 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001153 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001154 return true;
1155 }
1156 return false;
1157 }
1158
Andrew Lee6324f702015-06-16 14:45:58 -07001159 private void showFabInSearchUi() {
1160 mFloatingActionButtonController.changeIcon(
1161 getResources().getDrawable(R.drawable.fab_ic_dial),
1162 getResources().getString(R.string.action_menu_dialpad_button));
1163 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1164 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1165 }
1166
Yorke Leec3766332013-07-31 11:13:16 -07001167 @Override
1168 public void onDialpadQueryChanged(String query) {
Yorke Leed9a93162015-10-14 09:39:05 -07001169 mDialpadQuery = query;
Yorke Lee46635872014-04-11 11:20:15 -07001170 if (mSmartDialSearchFragment != null) {
1171 mSmartDialSearchFragment.setAddToContactNumber(query);
1172 }
Yorke Leec3766332013-07-31 11:13:16 -07001173 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1174 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001175
1176 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001177 if (DEBUG) {
1178 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1179 }
1180 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1181 // This callback can happen if the dialpad fragment is recreated because of
1182 // activity destruction. In that case, don't update the search view because
1183 // that would bring the user back to the search fragment regardless of the
1184 // previous state of the application. Instead, just return here and let the
1185 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001186 if (!TextUtils.isEmpty(normalizedQuery)) {
1187 mPendingSearchViewQuery = normalizedQuery;
1188 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001189 return;
1190 }
Yorke Lee53a22302014-04-29 18:13:46 -07001191 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001192 }
Santos Cordon83131712015-04-16 21:25:35 -07001193
1194 try {
1195 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1196 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1197 }
1198 } catch (Exception ignored) {
1199 // Skip any exceptions for this piece of code
1200 }
Yorke Lee575ae382015-07-16 11:36:07 -07001201 }
Santos Cordon83131712015-04-16 21:25:35 -07001202
Yorke Lee575ae382015-07-16 11:36:07 -07001203 @Override
1204 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1205 if (mInDialpadSearch && mSmartDialSearchFragment != null
1206 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1207 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1208 return true;
1209 }
1210 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001211 }
Yorke Leec3766332013-07-31 11:13:16 -07001212
1213 @Override
1214 public void onListFragmentScrollStateChange(int scrollState) {
1215 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001216 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001217 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001218 }
1219 }
1220
1221 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001222 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001223 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001224 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1225 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001226 }
1227
Yorke Leec3766332013-07-31 11:13:16 -07001228 private boolean phoneIsInUse() {
Yorke Lee827a90f2015-09-17 18:10:58 -07001229 return TelecomUtil.isInCall(this);
Yorke Leec3766332013-07-31 11:13:16 -07001230 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001231
Yorke Leeda0f9042013-12-05 14:25:51 -08001232 private boolean canIntentBeHandled(Intent intent) {
1233 final PackageManager packageManager = getPackageManager();
1234 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1235 PackageManager.MATCH_DEFAULT_ONLY);
1236 return resolveInfo != null && resolveInfo.size() > 0;
1237 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001238
Yorke Lee86e21f72014-04-02 16:49:38 -07001239 /**
1240 * Called when the user has long-pressed a contact tile to start a drag operation.
1241 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001242 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001243 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001244 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001245 }
1246
1247 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001248 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1249 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001250
Yorke Lee86e21f72014-04-02 16:49:38 -07001251 /**
1252 * Called when the user has released a contact tile after long-pressing it.
1253 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001254 @Override
1255 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001256 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001257 }
1258
1259 @Override
1260 public void onDroppedOnRemove() {}
1261
1262 /**
Yorke Leed999b712014-04-23 15:10:58 -07001263 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001264 * once it has been attached to the activity.
1265 */
1266 @Override
1267 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001268 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001269 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001270 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001271
Yorke Lee38019af2015-07-14 17:05:38 -07001272 /**
1273 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1274 */
1275 @Override
1276 public void showAllContactsTab() {
1277 if (mListsFragment != null) {
1278 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1279 }
1280 }
1281
1282 /**
1283 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1284 */
1285 @Override
1286 public void showDialpad() {
1287 showDialpadFragment(true);
1288 }
1289
Yorke Leee00c9fe2014-04-12 12:42:06 -07001290 @Override
Tyler Gunnb31f5d12015-12-18 13:57:37 -08001291 public void onPickDataUri(Uri dataUri, boolean isVideoCall, int callInitiationType) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001292 mClearSearchOnPause = true;
Yorke Leec8d6e162015-09-14 18:43:27 -07001293 PhoneNumberInteraction.startInteractionForPhoneCall(
Tyler Gunnb31f5d12015-12-18 13:57:37 -08001294 DialtactsActivity.this, dataUri, isVideoCall, callInitiationType);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001295 }
1296
1297 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001298 public void onPickPhoneNumber(String phoneNumber, boolean isVideoCall, int callInitiationType) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001299 if (phoneNumber == null) {
1300 // Invalid phone number, but let the call go through so that InCallUI can show
1301 // an error message.
1302 phoneNumber = "";
1303 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001304
1305 final Intent intent = new CallIntentBuilder(phoneNumber)
1306 .setIsVideoCall(isVideoCall)
Yorke Leec8d6e162015-09-14 18:43:27 -07001307 .setCallInitiationType(callInitiationType)
Yorke Leee3a2d132015-09-14 16:52:08 -07001308 .build();
1309
Yorke Lee7d20f822014-06-19 17:09:33 -07001310 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001311 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001312 }
1313
1314 @Override
1315 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001316 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001317 }
1318
1319 @Override
1320 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001321 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001322 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001323
Yorke Leecc4660d2014-04-24 11:22:57 -07001324 @Override
1325 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001326 int tabIndex = mListsFragment.getCurrentTabIndex();
1327
Andrew Lee4de59fb2015-08-13 15:20:08 -07001328 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1329 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1330 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001331 boolean isRtl = DialerUtils.isRtl();
1332 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1333 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001334 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001335 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1336 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001337 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001338 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001339 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001340
Andrew Lee432d4b52015-05-20 16:52:00 -07001341 @Override
1342 public void onPageSelected(int position) {
Sarmad Hashmicb559812016-02-10 18:39:59 -08001343 updateMissedCalls();
Andrew Lee432d4b52015-05-20 16:52:00 -07001344 int tabIndex = mListsFragment.getCurrentTabIndex();
Sarmad Hashmicb559812016-02-10 18:39:59 -08001345 mPreviouslySelectedTabIndex = tabIndex;
Naoki Amano10c93822016-09-13 18:56:57 +09001346 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS &&
1347 !mInRegularSearch && !mInDialpadSearch) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001348 mFloatingActionButtonController.changeIcon(
1349 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1350 getResources().getString(R.string.search_shortcut_create_new_contact));
1351 } else {
1352 mFloatingActionButtonController.changeIcon(
1353 getResources().getDrawable(R.drawable.fab_ic_dial),
1354 getResources().getString(R.string.action_menu_dialpad_button));
1355 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001356 }
1357
1358 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001359 public void onPageScrollStateChanged(int state) {
1360 }
1361
Yorke Lee5253be02014-05-21 18:50:03 -07001362 @Override
1363 public boolean isActionBarShowing() {
1364 return mActionBarController.isActionBarShowing();
1365 }
1366
Yorke Leec98a5bb2014-10-28 16:12:05 -07001367 @Override
1368 public ActionBarController getActionBarController() {
1369 return mActionBarController;
1370 }
1371
Yorke Lee6bc67162015-06-27 14:03:25 -07001372 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001373 public boolean isDialpadShown() {
1374 return mIsDialpadShown;
1375 }
1376
Yorke Lee5253be02014-05-21 18:50:03 -07001377 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001378 public int getDialpadHeight() {
1379 if (mDialpadFragment != null) {
1380 return mDialpadFragment.getDialpadHeight();
1381 }
1382 return 0;
1383 }
1384
1385 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001386 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001387 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001388 }
1389
1390 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001391 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001392 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001393 }
1394
1395 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001396 public int getActionBarHeight() {
1397 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001398 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001399
Andrew Lee6324f702015-06-16 14:45:58 -07001400 private int getFabAlignment() {
1401 if (!mIsLandscape && !isInSearchUi() &&
1402 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1403 return FloatingActionButtonController.ALIGN_MIDDLE;
1404 }
1405 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001406 }
Sarmad Hashmicb559812016-02-10 18:39:59 -08001407
1408 private void updateMissedCalls() {
1409 if (mPreviouslySelectedTabIndex == ListsFragment.TAB_INDEX_HISTORY) {
1410 mListsFragment.markMissedCallsAsReadAndRemoveNotifications();
1411 }
1412 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001413}