blob: dabe590adb685eee66d3d740bcbf45209f7aa4db [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 Leeaf6f1952014-07-14 19:13:16 -070084import com.android.dialer.settings.DialerSettingsActivity;
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;
Yorke Lee5253be02014-05-21 18:50:03 -070089import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070090import com.android.dialer.widget.SearchEditTextLayout;
Yorke Lee0baa98b2013-08-22 10:55:16 -070091import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee807afd82015-10-12 10:03:07 -070092import com.android.dialerbind.ObjectFactory;
Yorke Lee2fec47b2014-08-05 18:16:27 -070093import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070094import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070095
Yorke Leed5c512a2015-01-30 14:42:53 -080096import junit.framework.Assert;
97
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;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700241
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800242 /**
243 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
244 * {@link #onResume()} to populate the search box.
245 */
246 private String mVoiceSearchQuery;
247
Andrew Lee467de3d2015-03-17 15:47:24 -0700248 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700249 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700250 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700251 }
252
253 @Override
254 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700255 final boolean hasContactsPermission =
256 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700257 final Menu menu = getMenu();
258 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700259 clearFrequents.setVisible(mListsFragment != null &&
260 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700261 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
262
263 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
264 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700265
266 menu.findItem(R.id.menu_history).setVisible(
267 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700268 super.show();
269 }
270 }
271
Chiao Cheng94b10b52012-08-17 16:59:12 -0700272 /**
Yorke Lee28266192014-05-01 10:05:52 -0700273 * Listener that listens to drag events and sends their x and y coordinates to a
274 * {@link DragDropController}.
275 */
276 private class LayoutOnDragListener implements OnDragListener {
277 @Override
278 public boolean onDrag(View v, DragEvent event) {
279 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700280 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700281 }
282 return true;
283 }
284 }
285
286 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700287 * Listener used to send search queries to the phone search fragment.
288 */
Yorke Lee53a22302014-04-29 18:13:46 -0700289 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700290 @Override
291 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
292 }
Yorke Leec3766332013-07-31 11:13:16 -0700293
Andrew Lee99a570c2014-05-15 14:18:50 -0700294 @Override
295 public void onTextChanged(CharSequence s, int start, int before, int count) {
296 final String newText = s.toString();
297 if (newText.equals(mSearchQuery)) {
298 // If the query hasn't changed (perhaps due to activity being destroyed
299 // and restored, or user launching the same DIAL intent twice), then there is
300 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700301 return;
302 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700303 if (DEBUG) {
304 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700305 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700306 }
Yorke Lee710709d2014-05-29 07:18:42 -0700307 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700308
Andrew Lee90374a72014-05-16 15:01:09 -0700309 // Show search fragment only when the query string is changed to non-empty text.
310 if (!TextUtils.isEmpty(newText)) {
311 // Call enterSearchUi only if we are switching search modes, or showing a search
312 // fragment for the first time.
313 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
314 (!mIsDialpadShown && mInRegularSearch);
315 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700316 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700317 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700318 }
319
Andrew Leea8515422014-06-13 16:53:54 -0700320 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700321 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700322 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700323 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700324 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700325 }
326
327 @Override
328 public void afterTextChanged(Editable s) {
329 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700330 };
331
Andrew Leeb1903842014-05-15 16:11:24 -0700332
333 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700334 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700335 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700336 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700337 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700338 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700339 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700340 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700341 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
342 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700343 }
Andrew Leeb1903842014-05-15 16:11:24 -0700344 }
345 };
346
347 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700348 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700349 */
350 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
351 @Override
352 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700353 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
354 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
355 // If the search term is empty, close the search UI.
356 maybeExitSearchUi();
357 } else {
358 // If the search term is not empty, show the dialpad fab.
359 showFabInSearchUi();
360 }
Andrew Leeb1903842014-05-15 16:11:24 -0700361 }
362 return false;
363 }
364 };
365
Chiao Cheng94b10b52012-08-17 16:59:12 -0700366 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700367 public boolean dispatchTouchEvent(MotionEvent ev) {
368 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
369 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
370 }
371 return super.dispatchTouchEvent(ev);
Nancy Chen49db1ad2014-08-18 20:10:17 -0700372 }
373
374 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700375 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800376 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700377 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700378
Yorke Lee98702de2013-08-06 12:03:32 -0700379 mFirstLaunch = true;
380
Andrew Lee2423a2f2014-05-29 14:14:45 -0700381 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700382 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700383
Yorke Leef5554ce2015-01-27 14:46:02 -0800384 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700385 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800386 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800387 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700388
Yorke Leef5554ce2015-01-27 14:46:02 -0800389 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700390 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700391 actionBar.setCustomView(R.layout.search_edittext);
392 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700393 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700394
Anne Rong35208002015-07-22 16:27:37 -0700395 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
396 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700397 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
398
Andrew Leee3f59a82015-02-26 12:06:35 -0800399 mActionBarController = new ActionBarController(this, searchEditTextLayout);
400
Andrew Lee04675a52014-06-05 18:36:20 -0700401 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700402 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700403 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
404 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
405 .setOnClickListener(mSearchViewOnClickListener);
406 searchEditTextLayout.findViewById(R.id.search_box_start_search)
407 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700408 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700409 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700410 @Override
411 public void onBackButtonClicked() {
412 onBackPressed();
413 }
Andrew Lee6324f702015-06-16 14:45:58 -0700414
415 @Override
416 public void onSearchViewClicked() {
417 // Hide FAB, as the keyboard is shown.
418 mFloatingActionButtonController.scaleOut();
419 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700420 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700421
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700422 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700423 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700424
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700425 final View floatingActionButtonContainer = findViewById(
426 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700427 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
428 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700429 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700430 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700431
Andrew Lee04675a52014-06-05 18:36:20 -0700432 ImageButton optionsMenuButton =
433 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700434 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700435 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
436 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700437
Yorke Leed5c512a2015-01-30 14:42:53 -0800438 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
439 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700440 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800441 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000442 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800443 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700444 } else {
445 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700446 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
447 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700448 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700449 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700450 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700451 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700452
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700453 final boolean isLayoutRtl = DialerUtils.isRtl();
454 if (mIsLandscape) {
455 mSlideIn = AnimationUtils.loadAnimation(this,
456 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
457 mSlideOut = AnimationUtils.loadAnimation(this,
458 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
459 } else {
460 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
461 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
462 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700463
Yorke Lee2fec47b2014-08-05 18:16:27 -0700464 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
465 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
466
Yorke Lee6c450eb2015-05-08 09:45:47 -0700467 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700468 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700469
Nancy Chenf3d9f822015-09-11 00:47:31 -0400470 mParentLayout = (CoordinatorLayout) findViewById(R.id.dialtacts_mainlayout);
Yorke Leef614f6f2014-10-03 10:44:19 -0700471 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700472 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700473 new ViewTreeObserver.OnGlobalLayoutListener() {
474 @Override
475 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700476 final ViewTreeObserver observer =
477 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700478 if (!observer.isAlive()) {
479 return;
480 }
481 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700482 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700483 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700484 mFloatingActionButtonController.align(
485 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700486 }
487 });
Andrew Lee0c667702014-05-12 14:31:45 -0700488
Yorke Leef5554ce2015-01-27 14:46:02 -0800489 Trace.endSection();
490
491 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700492 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700493 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800494 Trace.endSection();
495 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700496 }
497
498 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700499 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800500 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700501 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700502
Yorke Lee19e68ca2014-10-28 11:51:40 -0700503 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700504 if (mFirstLaunch) {
505 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700506 } else if (!phoneIsInUse() && mInCallDialpadUp) {
507 hideDialpadFragment(false, true);
508 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700509 } else if (mShowDialpadOnResume) {
510 showDialpadFragment(false);
511 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700512 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800513
514 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
515 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
516 // shown until onResume has completed. Active the search UI and set the search term now.
517 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
518 mActionBarController.onSearchBoxTapped();
519 mSearchView.setText(mVoiceSearchQuery);
520 mVoiceSearchQuery = null;
521 }
522
Yorke Lee98702de2013-08-06 12:03:32 -0700523 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800524
525 if (mIsRestarting) {
526 // This is only called when the activity goes from resumed -> paused -> resumed, so it
527 // will not cause an extra view to be sent out on rotation
528 if (mIsDialpadShown) {
529 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
530 }
531 mIsRestarting = false;
532 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700533
Yorke Lee53a22302014-04-29 18:13:46 -0700534 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700535 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700536 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700537
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700538 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
539 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
540 // used internally.
541 final Bundle extras = getIntent().getExtras();
542 if (extras != null
543 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
544 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
545 } else {
546 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
547 }
548 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700549 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
550 if (index < mListsFragment.getTabCount()) {
551 mListsFragment.showTab(index);
552 }
553 }
554
Anne Rong35208002015-07-22 16:27:37 -0700555 setSearchBoxHint();
556
Yorke Leef5554ce2015-01-27 14:46:02 -0800557 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700558 }
559
560 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800561 protected void onRestart() {
562 super.onRestart();
563 mIsRestarting = true;
564 }
565
566 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700567 protected void onPause() {
568 if (mClearSearchOnPause) {
569 hideDialpadAndSearchUi();
570 mClearSearchOnPause = false;
571 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700572 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
573 commitDialpadFragmentHide();
574 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700575 super.onPause();
576 }
577
578 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700579 protected void onSaveInstanceState(Bundle outState) {
580 super.onSaveInstanceState(outState);
581 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700582 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
583 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700584 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700585 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700586 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700587 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700588 }
589
590 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700591 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700592 if (fragment instanceof DialpadFragment) {
593 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800594 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700595 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
596 transaction.hide(mDialpadFragment);
597 transaction.commit();
598 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700599 } else if (fragment instanceof SmartDialSearchFragment) {
600 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700601 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leed9a93162015-10-14 09:39:05 -0700602 if (!TextUtils.isEmpty(mDialpadQuery)) {
603 mSmartDialSearchFragment.setAddToContactNumber(mDialpadQuery);
604 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700605 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700606 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700607 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700608 } else if (fragment instanceof ListsFragment) {
609 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700610 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700611 }
Yorke Leec3766332013-07-31 11:13:16 -0700612 }
613
Alon Albertb453e5b2013-09-10 15:54:23 -0700614 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700615 final Intent intent = new Intent(this, DialerSettingsActivity.class);
616 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700617 }
618
Chiao Cheng94b10b52012-08-17 16:59:12 -0700619 @Override
620 public void onClick(View view) {
621 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700622 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700623 if (mListsFragment.getCurrentTabIndex()
624 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700625 DialerUtils.startActivityWithErrorToast(
626 this,
627 IntentUtil.getNewContactIntent(),
628 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700629 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700630 mInCallDialpadUp = false;
631 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700632 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700633 break;
Yorke Leec3766332013-07-31 11:13:16 -0700634 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800635 try {
636 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
637 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
638 } catch (ActivityNotFoundException e) {
639 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
640 Toast.LENGTH_SHORT).show();
641 }
Yorke Leec3766332013-07-31 11:13:16 -0700642 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700643 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700644 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700645 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700646 default: {
647 Log.wtf(TAG, "Unexpected onClick event from " + view);
648 break;
649 }
650 }
651 }
652
Yorke Leec3766332013-07-31 11:13:16 -0700653 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700654 public boolean onMenuItemClick(MenuItem item) {
Jay Shrauner193cfc52015-09-25 14:13:16 -0700655 if (!isSafeToCommitTransactions()) {
Jay Shrauner119b0652015-09-17 12:00:18 -0700656 return true;
657 }
658
Yorke Lee86e21f72014-04-02 16:49:38 -0700659 switch (item.getItemId()) {
660 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700661 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
662 // CONTENT_TYPE, so that we always open our call log from our dialer
663 final Intent intent = new Intent(this, CallLogActivity.class);
664 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700665 break;
666 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700667 DialerUtils.startActivityWithErrorToast(
668 this,
669 IntentUtil.getNewContactIntent(),
670 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700671 break;
672 case R.id.menu_import_export:
673 // We hard-code the "contactsAreAvailable" argument because doing it properly would
674 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
675 // now in Dialtacts for (potential) performance reasons. Compare with how it is
676 // done in {@link PeopleActivity}.
Wenyi Wang5953e8c2015-09-24 15:38:14 -0700677 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
678 ImportExportDialogFragment.show(getFragmentManager(), true,
679 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_FAVORITES);
680 } else {
681 ImportExportDialogFragment.show(getFragmentManager(), true,
682 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_DEFAULT);
683 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700684 return true;
685 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700686 ClearFrequentsDialog.show(getFragmentManager());
687 return true;
688 case R.id.menu_call_settings:
689 handleMenuSettings();
690 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700691 }
692 return false;
693 }
694
695 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700696 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
697 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
698 if (resultCode == RESULT_OK) {
699 final ArrayList<String> matches = data.getStringArrayListExtra(
700 RecognizerIntent.EXTRA_RESULTS);
701 if (matches.size() > 0) {
702 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800703 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700704 } else {
705 Log.e(TAG, "Voice search - nothing heard");
706 }
707 } else {
708 Log.e(TAG, "Voice search failed");
709 }
710 }
711 super.onActivityResult(requestCode, resultCode, data);
712 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700713
Andrew Lee2a58ab82014-05-15 02:16:04 -0700714 /**
Nancy Chenea7c0b82015-08-31 17:18:41 -0700715 * Update the number of unread voicemails (potentially other tabs) displayed next to the tab
716 * icon.
717 */
718 public void updateTabUnreadCounts() {
719 mListsFragment.updateTabUnreadCounts();
720 }
721
722 /**
Andrew Lee2a58ab82014-05-15 02:16:04 -0700723 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
724 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
725 * @see #onDialpadShown
726 */
Yorke Leec3766332013-07-31 11:13:16 -0700727 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700728 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700729 return;
730 }
731 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800732
Yorke Lee7ee5c422014-11-04 10:29:32 -0800733 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700734
Chiao Cheng94b10b52012-08-17 16:59:12 -0700735 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800736 if (mDialpadFragment == null) {
737 mDialpadFragment = new DialpadFragment();
738 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
739 } else {
740 ft.show(mDialpadFragment);
741 }
742
743 mDialpadFragment.setAnimate(animate);
744 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700745 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700746
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700747 if (animate) {
748 mFloatingActionButtonController.scaleOut();
749 } else {
750 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700751 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700752 }
Yorke Lee5253be02014-05-21 18:50:03 -0700753 mActionBarController.onDialpadUp();
754
Yorke Lee6c450eb2015-05-08 09:45:47 -0700755 mListsFragment.getView().animate().alpha(0).withLayer();
Ta-wei Yene3cae632015-10-27 16:09:09 -0700756
757 //adjust the title, so the user will know where we're at when the activity start/resumes.
758 setTitle(R.string.launcherDialpadActivityLabel);
Yorke Leec3766332013-07-31 11:13:16 -0700759 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700760
Andrew Lee2a58ab82014-05-15 02:16:04 -0700761 /**
762 * Callback from child DialpadFragment when the dialpad is shown.
763 */
764 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800765 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700766 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700767 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700768 } else {
769 mDialpadFragment.setYFraction(0);
770 }
771
Andrew Lee2a58ab82014-05-15 02:16:04 -0700772 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700773 }
774
775 /**
776 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
777 * a callback after the hide animation ends.
778 * @see #commitDialpadFragmentHide
779 */
Yorke Leeca195042013-09-25 16:29:38 -0700780 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700781 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700782 return;
783 }
Yorke Leef6673d32013-08-23 15:34:17 -0700784 if (clearDialpad) {
785 mDialpadFragment.clearDialpad();
786 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700787 if (!mIsDialpadShown) {
788 return;
Yorke Leec3766332013-07-31 11:13:16 -0700789 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700790 mIsDialpadShown = false;
791 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800792 mListsFragment.setUserVisibleHint(true);
793 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700794
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700795 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700796
Andrew Lee6324f702015-06-16 14:45:58 -0700797 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700798 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700799 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700800 } else {
801 commitDialpadFragmentHide();
802 }
803
Yorke Lee5253be02014-05-21 18:50:03 -0700804 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700805
Andrew Leed4cde832014-05-16 15:56:48 -0700806 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700807 if (TextUtils.isEmpty(mSearchQuery)) {
808 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700809 }
Andrew Leed4cde832014-05-16 15:56:48 -0700810 }
Ta-wei Yene3cae632015-10-27 16:09:09 -0700811 //reset the title to normal.
812 setTitle(R.string.launcherActivityLabel);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700813 }
814
815 /**
816 * Finishes hiding the dialpad fragment after any animations are completed.
817 */
818 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800819 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700820 final FragmentTransaction ft = getFragmentManager().beginTransaction();
821 ft.hide(mDialpadFragment);
822 ft.commit();
823 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700824 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700825 }
826
Andrew Lee2a58ab82014-05-15 02:16:04 -0700827 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700828 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700829 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700830 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700831 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700832 fragment = mRegularSearchFragment;
833 }
834 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700835 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700836 }
837 }
838
Yorke Lee5253be02014-05-21 18:50:03 -0700839 @Override
840 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700841 return mInDialpadSearch || mInRegularSearch;
842 }
843
Yorke Lee5253be02014-05-21 18:50:03 -0700844 @Override
845 public boolean hasSearchQuery() {
846 return !TextUtils.isEmpty(mSearchQuery);
847 }
848
849 @Override
850 public boolean shouldShowActionBar() {
851 return mListsFragment.shouldShowActionBar();
852 }
853
Yorke Leeb207f8a2013-08-29 18:51:06 -0700854 private void setNotInSearchUi() {
855 mInDialpadSearch = false;
856 mInRegularSearch = false;
857 }
858
Yorke Lee311969c2013-08-26 06:31:11 -0700859 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700860 if (mIsDialpadShown) {
861 hideDialpadFragment(false, true);
862 } else {
863 exitSearchUi();
864 }
Yorke Lee311969c2013-08-26 06:31:11 -0700865 }
866
Yorke Lee53a22302014-04-29 18:13:46 -0700867 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700868 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
869 if (canIntentBeHandled(voiceIntent)) {
870 mVoiceSearchButton.setVisibility(View.VISIBLE);
871 mVoiceSearchButton.setOnClickListener(this);
872 } else {
873 mVoiceSearchButton.setVisibility(View.GONE);
874 }
875 }
876
Yorke Leee9c82332015-10-06 14:21:53 -0700877 public boolean isNearbyPlacesSearchEnabled() {
878 return false;
879 }
880
Anne Rong35208002015-07-22 16:27:37 -0700881 protected int getSearchBoxHint () {
882 return R.string.dialer_hint_find_contact;
883 }
884
885 /**
886 * Sets the hint text for the contacts search box
887 */
888 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700889 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700890 .getCustomView().findViewById(R.id.search_view_container);
891 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
892 .setHint(getSearchBoxHint());
893 }
894
Andrew Lee467de3d2015-03-17 15:47:24 -0700895 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700896 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
897 popupMenu.inflate(R.menu.dialtacts_options);
898 popupMenu.setOnMenuItemClickListener(this);
899 return popupMenu;
900 }
901
Yorke Lee86e21f72014-04-02 16:49:38 -0700902 @Override
903 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700904 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700905 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700906 mPendingSearchViewQuery = null;
907 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700908 if (mActionBarController != null) {
909 mActionBarController.restoreActionBarOffset();
910 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700911 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700912 }
913
Chiao Cheng94b10b52012-08-17 16:59:12 -0700914 /**
915 * Returns true if the intent is due to hitting the green send key (hardware call button:
916 * KEYCODE_CALL) while in a call.
917 *
918 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700919 * @return true if the intent is due to hitting the green send key while in a call
920 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700921 private boolean isSendKeyWhileInCall(Intent intent) {
922 // If there is a call in progress and the user launched the dialer by hitting the call
923 // button, go straight to the in-call screen.
924 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700925
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700926 if (callKey) {
Yorke Lee762b3572015-09-18 12:54:59 -0700927 TelecomUtil.showInCallScreen(this, false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700928 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700929 }
930
931 return false;
932 }
933
934 /**
935 * Sets the current tab based on the intent's request type
936 *
937 * @param intent Intent that contains information about which tab should be selected
938 */
Yorke Leec3766332013-07-31 11:13:16 -0700939 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700940 // 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 -0700941 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700942 finish();
943 return;
944 }
945
Yorke Leed77017d2015-08-31 16:19:43 -0700946 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
947 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800948 showDialpadFragment(false);
949 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700950 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800951 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700952 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700953 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700954 }
955
956 @Override
957 public void onNewIntent(Intent newIntent) {
958 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700959
Nancy Chen199b7f02014-11-05 15:03:00 -0800960 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700961 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700962
Chiao Cheng94b10b52012-08-17 16:59:12 -0700963 invalidateOptionsMenu();
964 }
965
966 /** Returns true if the given intent contains a phone number to populate the dialer with */
967 private boolean isDialIntent(Intent intent) {
968 final String action = intent.getAction();
969 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
970 return true;
971 }
972 if (Intent.ACTION_VIEW.equals(action)) {
973 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700974 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700975 return true;
976 }
977 }
978 return false;
979 }
980
981 /**
Yorke Leec3766332013-07-31 11:13:16 -0700982 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700983 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700984 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700985 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700986 // Weird race condition where fragment is doing work after the activity is destroyed
987 // due to talkback being on (b/10209937). Just return since we can't do any
988 // constructive here.
989 return;
990 }
991
Yorke Leeef2b7382013-08-09 17:39:25 -0700992 if (DEBUG) {
993 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
994 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700995
Yorke Leec3766332013-07-31 11:13:16 -0700996 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700997 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700998 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700999 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001000 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -07001001 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001002
Yorke Leeb207f8a2013-08-29 18:51:06 -07001003 final String tag;
1004 if (smartDialSearch) {
1005 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
1006 } else {
1007 tag = TAG_REGULAR_SEARCH_FRAGMENT;
1008 }
1009 mInDialpadSearch = smartDialSearch;
1010 mInRegularSearch = !smartDialSearch;
1011
Andrew Lee6324f702015-06-16 14:45:58 -07001012 mFloatingActionButtonController.scaleOut();
1013
Andrew Lee752956e2014-05-15 11:43:38 -07001014 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001015 if (animate) {
1016 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
1017 } else {
1018 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
1019 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001020 if (fragment == null) {
1021 if (smartDialSearch) {
1022 fragment = new SmartDialSearchFragment();
1023 } else {
Yorke Lee807afd82015-10-12 10:03:07 -07001024 fragment = ObjectFactory.newRegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001025 fragment.setOnTouchListener(new View.OnTouchListener() {
1026 @Override
1027 public boolean onTouch(View v, MotionEvent event) {
1028 // Show the FAB when the user touches the lists fragment and the soft
1029 // keyboard is hidden.
1030 showFabInSearchUi();
1031 return false;
1032 }
1033 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001034 }
Andrew Leeb1903842014-05-15 16:11:24 -07001035 transaction.add(R.id.dialtacts_frame, fragment, tag);
1036 } else {
1037 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001038 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001039 // DialtactsActivity will provide the options menu
1040 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001041 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001042 if (!smartDialSearch) {
1043 fragment.setQueryString(query, false /* delaySelection */);
1044 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001045 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001046
Yorke Lee6c450eb2015-05-08 09:45:47 -07001047 if (animate) {
1048 mListsFragment.getView().animate().alpha(0).withLayer();
1049 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001050 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001051 }
1052
1053 /**
Yorke Leec3766332013-07-31 11:13:16 -07001054 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001055 */
Yorke Leec3766332013-07-31 11:13:16 -07001056 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001057 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001058 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001059 return;
1060 }
Andrew Leeb1903842014-05-15 16:11:24 -07001061
Andrew Leeb1903842014-05-15 16:11:24 -07001062 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001063
1064 if (mDialpadFragment != null) {
1065 mDialpadFragment.clearDialpad();
1066 }
1067
Yorke Leeb207f8a2013-08-29 18:51:06 -07001068 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001069
Andrew Lee6324f702015-06-16 14:45:58 -07001070 // Restore the FAB for the lists fragment.
1071 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1072 mFloatingActionButtonController.setVisible(false);
1073 }
1074 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1075 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1076 onPageSelected(mListsFragment.getCurrentTabIndex());
1077
Andrew Leeb1903842014-05-15 16:11:24 -07001078 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001079 if (mSmartDialSearchFragment != null) {
1080 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001081 }
Andrew Leeb1903842014-05-15 16:11:24 -07001082 if (mRegularSearchFragment != null) {
1083 transaction.remove(mRegularSearchFragment);
1084 }
1085 transaction.commit();
1086
1087 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001088
1089 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001090 // If the dialpad fragment wasn't previously visible, then send a screen view because
1091 // we are exiting regular search. Otherwise, the screen view will be sent by
1092 // {@link #hideDialpadFragment}.
1093 mListsFragment.sendScreenViewForCurrentPosition();
1094 mListsFragment.setUserVisibleHint(true);
1095 }
1096
Yorke Lee5253be02014-05-21 18:50:03 -07001097 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001098 }
1099
Chiao Cheng94b10b52012-08-17 16:59:12 -07001100 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001101 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001102 if (mStateSaved) {
1103 return;
1104 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001105 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001106 if (TextUtils.isEmpty(mSearchQuery) ||
1107 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1108 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001109 exitSearchUi();
1110 }
Yorke Leef6673d32013-08-23 15:34:17 -07001111 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001112 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001113 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001114 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001115 } else {
1116 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001117 }
Yorke Leec3766332013-07-31 11:13:16 -07001118 }
1119
Yorke Lee81a313d2015-06-01 14:53:59 -07001120 private void maybeEnterSearchUi() {
1121 if (!isInSearchUi()) {
1122 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1123 }
1124 }
1125
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001126 /**
1127 * @return True if the search UI was exited, false otherwise
1128 */
1129 private boolean maybeExitSearchUi() {
1130 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1131 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001132 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001133 return true;
1134 }
1135 return false;
1136 }
1137
Andrew Lee6324f702015-06-16 14:45:58 -07001138 private void showFabInSearchUi() {
1139 mFloatingActionButtonController.changeIcon(
1140 getResources().getDrawable(R.drawable.fab_ic_dial),
1141 getResources().getString(R.string.action_menu_dialpad_button));
1142 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1143 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1144 }
1145
Yorke Leec3766332013-07-31 11:13:16 -07001146 @Override
1147 public void onDialpadQueryChanged(String query) {
Yorke Leed9a93162015-10-14 09:39:05 -07001148 mDialpadQuery = query;
Yorke Lee46635872014-04-11 11:20:15 -07001149 if (mSmartDialSearchFragment != null) {
1150 mSmartDialSearchFragment.setAddToContactNumber(query);
1151 }
Yorke Leec3766332013-07-31 11:13:16 -07001152 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1153 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001154
1155 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001156 if (DEBUG) {
1157 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1158 }
1159 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1160 // This callback can happen if the dialpad fragment is recreated because of
1161 // activity destruction. In that case, don't update the search view because
1162 // that would bring the user back to the search fragment regardless of the
1163 // previous state of the application. Instead, just return here and let the
1164 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001165 if (!TextUtils.isEmpty(normalizedQuery)) {
1166 mPendingSearchViewQuery = normalizedQuery;
1167 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001168 return;
1169 }
Yorke Lee53a22302014-04-29 18:13:46 -07001170 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001171 }
Santos Cordon83131712015-04-16 21:25:35 -07001172
1173 try {
1174 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1175 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1176 }
1177 } catch (Exception ignored) {
1178 // Skip any exceptions for this piece of code
1179 }
Yorke Lee575ae382015-07-16 11:36:07 -07001180 }
Santos Cordon83131712015-04-16 21:25:35 -07001181
Yorke Lee575ae382015-07-16 11:36:07 -07001182 @Override
1183 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1184 if (mInDialpadSearch && mSmartDialSearchFragment != null
1185 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1186 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1187 return true;
1188 }
1189 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001190 }
Yorke Leec3766332013-07-31 11:13:16 -07001191
1192 @Override
1193 public void onListFragmentScrollStateChange(int scrollState) {
1194 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001195 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001196 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001197 }
1198 }
1199
1200 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001201 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001202 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001203 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1204 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001205 }
1206
Yorke Leec3766332013-07-31 11:13:16 -07001207 private boolean phoneIsInUse() {
Yorke Lee827a90f2015-09-17 18:10:58 -07001208 return TelecomUtil.isInCall(this);
Yorke Leec3766332013-07-31 11:13:16 -07001209 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001210
Yorke Leeda0f9042013-12-05 14:25:51 -08001211 private boolean canIntentBeHandled(Intent intent) {
1212 final PackageManager packageManager = getPackageManager();
1213 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1214 PackageManager.MATCH_DEFAULT_ONLY);
1215 return resolveInfo != null && resolveInfo.size() > 0;
1216 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001217
Yorke Lee86e21f72014-04-02 16:49:38 -07001218 /**
1219 * Called when the user has long-pressed a contact tile to start a drag operation.
1220 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001221 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001222 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001223 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001224 }
1225
1226 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001227 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1228 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001229
Yorke Lee86e21f72014-04-02 16:49:38 -07001230 /**
1231 * Called when the user has released a contact tile after long-pressing it.
1232 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001233 @Override
1234 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001235 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001236 }
1237
1238 @Override
1239 public void onDroppedOnRemove() {}
1240
1241 /**
Yorke Leed999b712014-04-23 15:10:58 -07001242 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001243 * once it has been attached to the activity.
1244 */
1245 @Override
1246 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001247 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001248 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001249 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001250
Yorke Lee38019af2015-07-14 17:05:38 -07001251 /**
1252 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1253 */
1254 @Override
1255 public void showAllContactsTab() {
1256 if (mListsFragment != null) {
1257 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1258 }
1259 }
1260
1261 /**
1262 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1263 */
1264 @Override
1265 public void showDialpad() {
1266 showDialpadFragment(true);
1267 }
1268
Yorke Leee00c9fe2014-04-12 12:42:06 -07001269 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001270 public void onPickDataUri(Uri dataUri, int callInitiationType) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001271 mClearSearchOnPause = true;
Yorke Leec8d6e162015-09-14 18:43:27 -07001272 PhoneNumberInteraction.startInteractionForPhoneCall(
1273 DialtactsActivity.this, dataUri, callInitiationType);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001274 }
1275
1276 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001277 public void onPickPhoneNumber(String phoneNumber, boolean isVideoCall, int callInitiationType) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001278 if (phoneNumber == null) {
1279 // Invalid phone number, but let the call go through so that InCallUI can show
1280 // an error message.
1281 phoneNumber = "";
1282 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001283
1284 final Intent intent = new CallIntentBuilder(phoneNumber)
1285 .setIsVideoCall(isVideoCall)
Yorke Leec8d6e162015-09-14 18:43:27 -07001286 .setCallInitiationType(callInitiationType)
Yorke Leee3a2d132015-09-14 16:52:08 -07001287 .build();
1288
Yorke Lee7d20f822014-06-19 17:09:33 -07001289 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001290 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001291 }
1292
1293 @Override
1294 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001295 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001296 }
1297
1298 @Override
1299 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001300 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001301 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001302
Yorke Leecc4660d2014-04-24 11:22:57 -07001303 @Override
1304 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001305 int tabIndex = mListsFragment.getCurrentTabIndex();
1306
Andrew Lee4de59fb2015-08-13 15:20:08 -07001307 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1308 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1309 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001310 boolean isRtl = DialerUtils.isRtl();
1311 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1312 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001313 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001314 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1315 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001316 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001317 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001318 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001319
Andrew Lee432d4b52015-05-20 16:52:00 -07001320 @Override
1321 public void onPageSelected(int position) {
1322 int tabIndex = mListsFragment.getCurrentTabIndex();
1323 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001324 mFloatingActionButtonController.changeIcon(
1325 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1326 getResources().getString(R.string.search_shortcut_create_new_contact));
1327 } else {
1328 mFloatingActionButtonController.changeIcon(
1329 getResources().getDrawable(R.drawable.fab_ic_dial),
1330 getResources().getString(R.string.action_menu_dialpad_button));
1331 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001332 }
1333
1334 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001335 public void onPageScrollStateChanged(int state) {
1336 }
1337
Yorke Lee5253be02014-05-21 18:50:03 -07001338 @Override
1339 public boolean isActionBarShowing() {
1340 return mActionBarController.isActionBarShowing();
1341 }
1342
Yorke Leec98a5bb2014-10-28 16:12:05 -07001343 @Override
1344 public ActionBarController getActionBarController() {
1345 return mActionBarController;
1346 }
1347
Yorke Lee6bc67162015-06-27 14:03:25 -07001348 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001349 public boolean isDialpadShown() {
1350 return mIsDialpadShown;
1351 }
1352
Yorke Lee5253be02014-05-21 18:50:03 -07001353 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001354 public int getDialpadHeight() {
1355 if (mDialpadFragment != null) {
1356 return mDialpadFragment.getDialpadHeight();
1357 }
1358 return 0;
1359 }
1360
1361 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001362 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001363 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001364 }
1365
1366 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001367 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001368 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001369 }
1370
1371 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001372 public int getActionBarHeight() {
1373 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001374 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001375
Andrew Lee6324f702015-06-16 14:45:58 -07001376 private int getFabAlignment() {
1377 if (!mIsLandscape && !isInSearchUi() &&
1378 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1379 return FloatingActionButtonController.ALIGN_MIDDLE;
1380 }
1381 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001382 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001383}