blob: a6fb98ca6ab9e939b9c8af7770ea8082aa5a94f0 [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;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700243
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800244 /**
245 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
246 * {@link #onResume()} to populate the search box.
247 */
248 private String mVoiceSearchQuery;
249
Andrew Lee467de3d2015-03-17 15:47:24 -0700250 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700251 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700252 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700253 }
254
255 @Override
256 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700257 final boolean hasContactsPermission =
258 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700259 final Menu menu = getMenu();
260 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700261 clearFrequents.setVisible(mListsFragment != null &&
262 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700263 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
264
265 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
266 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700267
268 menu.findItem(R.id.menu_history).setVisible(
269 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700270 super.show();
271 }
272 }
273
Chiao Cheng94b10b52012-08-17 16:59:12 -0700274 /**
Yorke Lee28266192014-05-01 10:05:52 -0700275 * Listener that listens to drag events and sends their x and y coordinates to a
276 * {@link DragDropController}.
277 */
278 private class LayoutOnDragListener implements OnDragListener {
279 @Override
280 public boolean onDrag(View v, DragEvent event) {
281 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700282 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700283 }
284 return true;
285 }
286 }
287
288 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700289 * Listener used to send search queries to the phone search fragment.
290 */
Yorke Lee53a22302014-04-29 18:13:46 -0700291 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700292 @Override
293 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
294 }
Yorke Leec3766332013-07-31 11:13:16 -0700295
Andrew Lee99a570c2014-05-15 14:18:50 -0700296 @Override
297 public void onTextChanged(CharSequence s, int start, int before, int count) {
298 final String newText = s.toString();
299 if (newText.equals(mSearchQuery)) {
300 // If the query hasn't changed (perhaps due to activity being destroyed
301 // and restored, or user launching the same DIAL intent twice), then there is
302 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700303 return;
304 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700305 if (DEBUG) {
306 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700307 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700308 }
Yorke Lee710709d2014-05-29 07:18:42 -0700309 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700310
Andrew Lee90374a72014-05-16 15:01:09 -0700311 // Show search fragment only when the query string is changed to non-empty text.
312 if (!TextUtils.isEmpty(newText)) {
313 // Call enterSearchUi only if we are switching search modes, or showing a search
314 // fragment for the first time.
315 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
316 (!mIsDialpadShown && mInRegularSearch);
317 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700318 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700319 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700320 }
321
Andrew Leea8515422014-06-13 16:53:54 -0700322 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700323 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700324 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700325 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700326 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700327 }
328
329 @Override
330 public void afterTextChanged(Editable s) {
331 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700332 };
333
Andrew Leeb1903842014-05-15 16:11:24 -0700334
335 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700336 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700337 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700338 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700339 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700340 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700341 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700342 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700343 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
344 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700345 }
Andrew Leeb1903842014-05-15 16:11:24 -0700346 }
347 };
348
349 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700350 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700351 */
352 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
353 @Override
354 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700355 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
356 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
357 // If the search term is empty, close the search UI.
358 maybeExitSearchUi();
359 } else {
360 // If the search term is not empty, show the dialpad fab.
361 showFabInSearchUi();
362 }
Andrew Leeb1903842014-05-15 16:11:24 -0700363 }
364 return false;
365 }
366 };
367
Chiao Cheng94b10b52012-08-17 16:59:12 -0700368 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700369 public boolean dispatchTouchEvent(MotionEvent ev) {
370 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
371 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
372 }
373 return super.dispatchTouchEvent(ev);
Nancy Chen49db1ad2014-08-18 20:10:17 -0700374 }
375
376 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700377 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800378 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700379 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700380
Yorke Lee98702de2013-08-06 12:03:32 -0700381 mFirstLaunch = true;
382
Andrew Lee2423a2f2014-05-29 14:14:45 -0700383 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700384 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700385
Yorke Leef5554ce2015-01-27 14:46:02 -0800386 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700387 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800388 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800389 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700390
Yorke Leef5554ce2015-01-27 14:46:02 -0800391 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700392 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700393 actionBar.setCustomView(R.layout.search_edittext);
394 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700395 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700396
Anne Rong35208002015-07-22 16:27:37 -0700397 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
398 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700399 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
400
Andrew Leee3f59a82015-02-26 12:06:35 -0800401 mActionBarController = new ActionBarController(this, searchEditTextLayout);
402
Andrew Lee04675a52014-06-05 18:36:20 -0700403 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700404 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700405 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
406 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
407 .setOnClickListener(mSearchViewOnClickListener);
408 searchEditTextLayout.findViewById(R.id.search_box_start_search)
409 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700410 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700411 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700412 @Override
413 public void onBackButtonClicked() {
414 onBackPressed();
415 }
Andrew Lee6324f702015-06-16 14:45:58 -0700416
417 @Override
418 public void onSearchViewClicked() {
419 // Hide FAB, as the keyboard is shown.
420 mFloatingActionButtonController.scaleOut();
421 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700422 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700423
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700424 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700425 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700426
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700427 final View floatingActionButtonContainer = findViewById(
428 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700429 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
430 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700431 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700432 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700433
Andrew Lee04675a52014-06-05 18:36:20 -0700434 ImageButton optionsMenuButton =
435 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700436 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700437 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
438 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700439
Yorke Leed5c512a2015-01-30 14:42:53 -0800440 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
441 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700442 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800443 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000444 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800445 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700446 } else {
447 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700448 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
449 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700450 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700451 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700452 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700453 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700454
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700455 final boolean isLayoutRtl = DialerUtils.isRtl();
456 if (mIsLandscape) {
457 mSlideIn = AnimationUtils.loadAnimation(this,
458 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
459 mSlideOut = AnimationUtils.loadAnimation(this,
460 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
461 } else {
462 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
463 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
464 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700465
Yorke Lee2fec47b2014-08-05 18:16:27 -0700466 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
467 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
468
Yorke Lee6c450eb2015-05-08 09:45:47 -0700469 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700470 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700471
Nancy Chenf3d9f822015-09-11 00:47:31 -0400472 mParentLayout = (CoordinatorLayout) findViewById(R.id.dialtacts_mainlayout);
Yorke Leef614f6f2014-10-03 10:44:19 -0700473 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700474 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700475 new ViewTreeObserver.OnGlobalLayoutListener() {
476 @Override
477 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700478 final ViewTreeObserver observer =
479 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700480 if (!observer.isAlive()) {
481 return;
482 }
483 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700484 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700485 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700486 mFloatingActionButtonController.align(
487 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700488 }
489 });
Andrew Lee0c667702014-05-12 14:31:45 -0700490
Yorke Leef5554ce2015-01-27 14:46:02 -0800491 Trace.endSection();
492
493 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700494 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700495 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800496 Trace.endSection();
497 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700498 }
499
500 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700501 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800502 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700503 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700504
Yorke Lee19e68ca2014-10-28 11:51:40 -0700505 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700506 if (mFirstLaunch) {
507 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700508 } else if (!phoneIsInUse() && mInCallDialpadUp) {
509 hideDialpadFragment(false, true);
510 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700511 } else if (mShowDialpadOnResume) {
512 showDialpadFragment(false);
513 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700514 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800515
516 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
517 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
518 // shown until onResume has completed. Active the search UI and set the search term now.
519 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
520 mActionBarController.onSearchBoxTapped();
521 mSearchView.setText(mVoiceSearchQuery);
522 mVoiceSearchQuery = null;
523 }
524
Yorke Lee98702de2013-08-06 12:03:32 -0700525 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800526
527 if (mIsRestarting) {
528 // This is only called when the activity goes from resumed -> paused -> resumed, so it
529 // will not cause an extra view to be sent out on rotation
530 if (mIsDialpadShown) {
Yorke Lee58e68a92015-11-06 10:35:30 -0800531 Logger.logScreenView(ScreenEvent.DIALPAD, this);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800532 }
533 mIsRestarting = false;
534 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700535
Yorke Lee53a22302014-04-29 18:13:46 -0700536 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700537 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700538 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700539
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700540 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
541 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
542 // used internally.
543 final Bundle extras = getIntent().getExtras();
544 if (extras != null
545 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
546 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
547 } else {
548 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
549 }
550 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700551 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
552 if (index < mListsFragment.getTabCount()) {
553 mListsFragment.showTab(index);
554 }
555 }
556
Anne Rong35208002015-07-22 16:27:37 -0700557 setSearchBoxHint();
558
Yorke Leef5554ce2015-01-27 14:46:02 -0800559 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700560 }
561
562 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800563 protected void onRestart() {
564 super.onRestart();
565 mIsRestarting = true;
566 }
567
568 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700569 protected void onPause() {
570 if (mClearSearchOnPause) {
571 hideDialpadAndSearchUi();
572 mClearSearchOnPause = false;
573 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700574 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
575 commitDialpadFragmentHide();
576 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700577 super.onPause();
578 }
579
580 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700581 protected void onSaveInstanceState(Bundle outState) {
582 super.onSaveInstanceState(outState);
583 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700584 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
585 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700586 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700587 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700588 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700589 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700590 }
591
592 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700593 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700594 if (fragment instanceof DialpadFragment) {
595 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800596 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700597 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
598 transaction.hide(mDialpadFragment);
599 transaction.commit();
600 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700601 } else if (fragment instanceof SmartDialSearchFragment) {
602 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700603 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leed9a93162015-10-14 09:39:05 -0700604 if (!TextUtils.isEmpty(mDialpadQuery)) {
605 mSmartDialSearchFragment.setAddToContactNumber(mDialpadQuery);
606 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700607 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700608 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700609 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700610 } else if (fragment instanceof ListsFragment) {
611 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700612 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700613 }
Yorke Leec3766332013-07-31 11:13:16 -0700614 }
615
Alon Albertb453e5b2013-09-10 15:54:23 -0700616 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700617 final Intent intent = new Intent(this, DialerSettingsActivity.class);
618 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700619 }
620
Chiao Cheng94b10b52012-08-17 16:59:12 -0700621 @Override
622 public void onClick(View view) {
623 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700624 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700625 if (mListsFragment.getCurrentTabIndex()
626 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700627 DialerUtils.startActivityWithErrorToast(
628 this,
629 IntentUtil.getNewContactIntent(),
630 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700631 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700632 mInCallDialpadUp = false;
633 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700634 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700635 break;
Yorke Leec3766332013-07-31 11:13:16 -0700636 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800637 try {
638 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
639 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
640 } catch (ActivityNotFoundException e) {
641 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
642 Toast.LENGTH_SHORT).show();
643 }
Yorke Leec3766332013-07-31 11:13:16 -0700644 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700645 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700646 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700647 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700648 default: {
649 Log.wtf(TAG, "Unexpected onClick event from " + view);
650 break;
651 }
652 }
653 }
654
Yorke Leec3766332013-07-31 11:13:16 -0700655 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700656 public boolean onMenuItemClick(MenuItem item) {
Jay Shrauner193cfc52015-09-25 14:13:16 -0700657 if (!isSafeToCommitTransactions()) {
Jay Shrauner119b0652015-09-17 12:00:18 -0700658 return true;
659 }
660
Yorke Lee86e21f72014-04-02 16:49:38 -0700661 switch (item.getItemId()) {
662 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700663 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
664 // CONTENT_TYPE, so that we always open our call log from our dialer
665 final Intent intent = new Intent(this, CallLogActivity.class);
666 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700667 break;
668 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700669 DialerUtils.startActivityWithErrorToast(
670 this,
671 IntentUtil.getNewContactIntent(),
672 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700673 break;
674 case R.id.menu_import_export:
675 // We hard-code the "contactsAreAvailable" argument because doing it properly would
676 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
677 // now in Dialtacts for (potential) performance reasons. Compare with how it is
678 // done in {@link PeopleActivity}.
Wenyi Wang5953e8c2015-09-24 15:38:14 -0700679 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
680 ImportExportDialogFragment.show(getFragmentManager(), true,
681 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_FAVORITES);
682 } else {
683 ImportExportDialogFragment.show(getFragmentManager(), true,
684 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_DEFAULT);
685 }
Yorke Lee58e68a92015-11-06 10:35:30 -0800686 Logger.logScreenView(ScreenEvent.IMPORT_EXPORT_CONTACTS, this);
Yorke Lee86e21f72014-04-02 16:49:38 -0700687 return true;
688 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700689 ClearFrequentsDialog.show(getFragmentManager());
Yorke Lee58e68a92015-11-06 10:35:30 -0800690 Logger.logScreenView(ScreenEvent.CLEAR_FREQUENTS, this);
Yorke Lee86e21f72014-04-02 16:49:38 -0700691 return true;
692 case R.id.menu_call_settings:
693 handleMenuSettings();
Yorke Lee58e68a92015-11-06 10:35:30 -0800694 Logger.logScreenView(ScreenEvent.SETTINGS, this);
Yorke Lee86e21f72014-04-02 16:49:38 -0700695 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);
909 popupMenu.setOnMenuItemClickListener(this);
910 return popupMenu;
911 }
912
Yorke Lee86e21f72014-04-02 16:49:38 -0700913 @Override
914 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700915 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700916 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700917 mPendingSearchViewQuery = null;
918 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700919 if (mActionBarController != null) {
920 mActionBarController.restoreActionBarOffset();
921 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700922 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700923 }
924
Chiao Cheng94b10b52012-08-17 16:59:12 -0700925 /**
926 * Returns true if the intent is due to hitting the green send key (hardware call button:
927 * KEYCODE_CALL) while in a call.
928 *
929 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700930 * @return true if the intent is due to hitting the green send key while in a call
931 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700932 private boolean isSendKeyWhileInCall(Intent intent) {
933 // If there is a call in progress and the user launched the dialer by hitting the call
934 // button, go straight to the in-call screen.
935 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700936
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700937 if (callKey) {
Yorke Lee762b3572015-09-18 12:54:59 -0700938 TelecomUtil.showInCallScreen(this, false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700939 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700940 }
941
942 return false;
943 }
944
945 /**
946 * Sets the current tab based on the intent's request type
947 *
948 * @param intent Intent that contains information about which tab should be selected
949 */
Yorke Leec3766332013-07-31 11:13:16 -0700950 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700951 // 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 -0700952 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700953 finish();
954 return;
955 }
956
Yorke Leed77017d2015-08-31 16:19:43 -0700957 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
958 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800959 showDialpadFragment(false);
960 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700961 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800962 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700963 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700964 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700965 }
966
967 @Override
968 public void onNewIntent(Intent newIntent) {
969 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700970
Nancy Chen199b7f02014-11-05 15:03:00 -0800971 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700972 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700973
Chiao Cheng94b10b52012-08-17 16:59:12 -0700974 invalidateOptionsMenu();
975 }
976
977 /** Returns true if the given intent contains a phone number to populate the dialer with */
978 private boolean isDialIntent(Intent intent) {
979 final String action = intent.getAction();
980 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
981 return true;
982 }
983 if (Intent.ACTION_VIEW.equals(action)) {
984 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700985 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700986 return true;
987 }
988 }
989 return false;
990 }
991
992 /**
Yorke Leec3766332013-07-31 11:13:16 -0700993 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700994 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700995 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700996 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700997 // Weird race condition where fragment is doing work after the activity is destroyed
998 // due to talkback being on (b/10209937). Just return since we can't do any
999 // constructive here.
1000 return;
1001 }
1002
Yorke Leeef2b7382013-08-09 17:39:25 -07001003 if (DEBUG) {
1004 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
1005 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001006
Yorke Leec3766332013-07-31 11:13:16 -07001007 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001008 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001009 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001010 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001011 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -07001012 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001013
Yorke Leeb207f8a2013-08-29 18:51:06 -07001014 final String tag;
1015 if (smartDialSearch) {
1016 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
1017 } else {
1018 tag = TAG_REGULAR_SEARCH_FRAGMENT;
1019 }
1020 mInDialpadSearch = smartDialSearch;
1021 mInRegularSearch = !smartDialSearch;
1022
Andrew Lee6324f702015-06-16 14:45:58 -07001023 mFloatingActionButtonController.scaleOut();
1024
Andrew Lee752956e2014-05-15 11:43:38 -07001025 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001026 if (animate) {
1027 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
1028 } else {
1029 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
1030 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001031 if (fragment == null) {
1032 if (smartDialSearch) {
1033 fragment = new SmartDialSearchFragment();
1034 } else {
Yorke Lee807afd82015-10-12 10:03:07 -07001035 fragment = ObjectFactory.newRegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001036 fragment.setOnTouchListener(new View.OnTouchListener() {
1037 @Override
1038 public boolean onTouch(View v, MotionEvent event) {
1039 // Show the FAB when the user touches the lists fragment and the soft
1040 // keyboard is hidden.
1041 showFabInSearchUi();
1042 return false;
1043 }
1044 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001045 }
Andrew Leeb1903842014-05-15 16:11:24 -07001046 transaction.add(R.id.dialtacts_frame, fragment, tag);
1047 } else {
1048 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001049 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001050 // DialtactsActivity will provide the options menu
1051 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001052 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001053 if (!smartDialSearch) {
1054 fragment.setQueryString(query, false /* delaySelection */);
1055 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001056 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001057
Yorke Lee6c450eb2015-05-08 09:45:47 -07001058 if (animate) {
1059 mListsFragment.getView().animate().alpha(0).withLayer();
1060 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001061 mListsFragment.setUserVisibleHint(false);
Yorke Leef96bb182015-11-10 10:06:21 -08001062
1063 if (smartDialSearch) {
1064 Logger.logScreenView(ScreenEvent.SMART_DIAL_SEARCH, this);
1065 } else {
1066 Logger.logScreenView(ScreenEvent.REGULAR_SEARCH, this);
1067 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001068 }
1069
1070 /**
Yorke Leec3766332013-07-31 11:13:16 -07001071 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001072 */
Yorke Leec3766332013-07-31 11:13:16 -07001073 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001074 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001075 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001076 return;
1077 }
Andrew Leeb1903842014-05-15 16:11:24 -07001078
Andrew Leeb1903842014-05-15 16:11:24 -07001079 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001080
1081 if (mDialpadFragment != null) {
1082 mDialpadFragment.clearDialpad();
1083 }
1084
Yorke Leeb207f8a2013-08-29 18:51:06 -07001085 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001086
Andrew Lee6324f702015-06-16 14:45:58 -07001087 // Restore the FAB for the lists fragment.
1088 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1089 mFloatingActionButtonController.setVisible(false);
1090 }
1091 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1092 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1093 onPageSelected(mListsFragment.getCurrentTabIndex());
1094
Andrew Leeb1903842014-05-15 16:11:24 -07001095 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001096 if (mSmartDialSearchFragment != null) {
1097 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001098 }
Andrew Leeb1903842014-05-15 16:11:24 -07001099 if (mRegularSearchFragment != null) {
1100 transaction.remove(mRegularSearchFragment);
1101 }
1102 transaction.commit();
1103
1104 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001105
1106 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001107 // If the dialpad fragment wasn't previously visible, then send a screen view because
1108 // we are exiting regular search. Otherwise, the screen view will be sent by
1109 // {@link #hideDialpadFragment}.
1110 mListsFragment.sendScreenViewForCurrentPosition();
1111 mListsFragment.setUserVisibleHint(true);
1112 }
1113
Yorke Lee5253be02014-05-21 18:50:03 -07001114 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001115 }
1116
Chiao Cheng94b10b52012-08-17 16:59:12 -07001117 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001118 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001119 if (mStateSaved) {
1120 return;
1121 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001122 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001123 if (TextUtils.isEmpty(mSearchQuery) ||
1124 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1125 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001126 exitSearchUi();
1127 }
Yorke Leef6673d32013-08-23 15:34:17 -07001128 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001129 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001130 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001131 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001132 } else {
1133 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001134 }
Yorke Leec3766332013-07-31 11:13:16 -07001135 }
1136
Yorke Lee81a313d2015-06-01 14:53:59 -07001137 private void maybeEnterSearchUi() {
1138 if (!isInSearchUi()) {
1139 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1140 }
1141 }
1142
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001143 /**
1144 * @return True if the search UI was exited, false otherwise
1145 */
1146 private boolean maybeExitSearchUi() {
1147 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1148 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001149 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001150 return true;
1151 }
1152 return false;
1153 }
1154
Andrew Lee6324f702015-06-16 14:45:58 -07001155 private void showFabInSearchUi() {
1156 mFloatingActionButtonController.changeIcon(
1157 getResources().getDrawable(R.drawable.fab_ic_dial),
1158 getResources().getString(R.string.action_menu_dialpad_button));
1159 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1160 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1161 }
1162
Yorke Leec3766332013-07-31 11:13:16 -07001163 @Override
1164 public void onDialpadQueryChanged(String query) {
Yorke Leed9a93162015-10-14 09:39:05 -07001165 mDialpadQuery = query;
Yorke Lee46635872014-04-11 11:20:15 -07001166 if (mSmartDialSearchFragment != null) {
1167 mSmartDialSearchFragment.setAddToContactNumber(query);
1168 }
Yorke Leec3766332013-07-31 11:13:16 -07001169 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1170 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001171
1172 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001173 if (DEBUG) {
1174 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1175 }
1176 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1177 // This callback can happen if the dialpad fragment is recreated because of
1178 // activity destruction. In that case, don't update the search view because
1179 // that would bring the user back to the search fragment regardless of the
1180 // previous state of the application. Instead, just return here and let the
1181 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001182 if (!TextUtils.isEmpty(normalizedQuery)) {
1183 mPendingSearchViewQuery = normalizedQuery;
1184 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001185 return;
1186 }
Yorke Lee53a22302014-04-29 18:13:46 -07001187 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001188 }
Santos Cordon83131712015-04-16 21:25:35 -07001189
1190 try {
1191 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1192 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1193 }
1194 } catch (Exception ignored) {
1195 // Skip any exceptions for this piece of code
1196 }
Yorke Lee575ae382015-07-16 11:36:07 -07001197 }
Santos Cordon83131712015-04-16 21:25:35 -07001198
Yorke Lee575ae382015-07-16 11:36:07 -07001199 @Override
1200 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1201 if (mInDialpadSearch && mSmartDialSearchFragment != null
1202 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1203 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1204 return true;
1205 }
1206 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001207 }
Yorke Leec3766332013-07-31 11:13:16 -07001208
1209 @Override
1210 public void onListFragmentScrollStateChange(int scrollState) {
1211 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001212 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001213 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001214 }
1215 }
1216
1217 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001218 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001219 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001220 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1221 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001222 }
1223
Yorke Leec3766332013-07-31 11:13:16 -07001224 private boolean phoneIsInUse() {
Yorke Lee827a90f2015-09-17 18:10:58 -07001225 return TelecomUtil.isInCall(this);
Yorke Leec3766332013-07-31 11:13:16 -07001226 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001227
Yorke Leeda0f9042013-12-05 14:25:51 -08001228 private boolean canIntentBeHandled(Intent intent) {
1229 final PackageManager packageManager = getPackageManager();
1230 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1231 PackageManager.MATCH_DEFAULT_ONLY);
1232 return resolveInfo != null && resolveInfo.size() > 0;
1233 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001234
Yorke Lee86e21f72014-04-02 16:49:38 -07001235 /**
1236 * Called when the user has long-pressed a contact tile to start a drag operation.
1237 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001238 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001239 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001240 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001241 }
1242
1243 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001244 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1245 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001246
Yorke Lee86e21f72014-04-02 16:49:38 -07001247 /**
1248 * Called when the user has released a contact tile after long-pressing it.
1249 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001250 @Override
1251 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001252 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001253 }
1254
1255 @Override
1256 public void onDroppedOnRemove() {}
1257
1258 /**
Yorke Leed999b712014-04-23 15:10:58 -07001259 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001260 * once it has been attached to the activity.
1261 */
1262 @Override
1263 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001264 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001265 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001266 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001267
Yorke Lee38019af2015-07-14 17:05:38 -07001268 /**
1269 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1270 */
1271 @Override
1272 public void showAllContactsTab() {
1273 if (mListsFragment != null) {
1274 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1275 }
1276 }
1277
1278 /**
1279 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1280 */
1281 @Override
1282 public void showDialpad() {
1283 showDialpadFragment(true);
1284 }
1285
Yorke Leee00c9fe2014-04-12 12:42:06 -07001286 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001287 public void onPickDataUri(Uri dataUri, int callInitiationType) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001288 mClearSearchOnPause = true;
Yorke Leec8d6e162015-09-14 18:43:27 -07001289 PhoneNumberInteraction.startInteractionForPhoneCall(
1290 DialtactsActivity.this, dataUri, callInitiationType);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001291 }
1292
1293 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001294 public void onPickPhoneNumber(String phoneNumber, boolean isVideoCall, int callInitiationType) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001295 if (phoneNumber == null) {
1296 // Invalid phone number, but let the call go through so that InCallUI can show
1297 // an error message.
1298 phoneNumber = "";
1299 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001300
1301 final Intent intent = new CallIntentBuilder(phoneNumber)
1302 .setIsVideoCall(isVideoCall)
Yorke Leec8d6e162015-09-14 18:43:27 -07001303 .setCallInitiationType(callInitiationType)
Yorke Leee3a2d132015-09-14 16:52:08 -07001304 .build();
1305
Yorke Lee7d20f822014-06-19 17:09:33 -07001306 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001307 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001308 }
1309
1310 @Override
1311 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001312 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001313 }
1314
1315 @Override
1316 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001317 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001318 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001319
Yorke Leecc4660d2014-04-24 11:22:57 -07001320 @Override
1321 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001322 int tabIndex = mListsFragment.getCurrentTabIndex();
1323
Andrew Lee4de59fb2015-08-13 15:20:08 -07001324 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1325 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1326 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001327 boolean isRtl = DialerUtils.isRtl();
1328 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1329 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001330 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001331 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1332 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001333 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001334 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001335 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001336
Andrew Lee432d4b52015-05-20 16:52:00 -07001337 @Override
1338 public void onPageSelected(int position) {
1339 int tabIndex = mListsFragment.getCurrentTabIndex();
1340 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 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001400}