blob: 9105e6f5aa1de6f0a8db0f5b951d96652217ac42 [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;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070051import android.view.View.OnTouchListener;
Ihab Awad9134e252014-07-09 12:32:52 -070052import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070053import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070054import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070055import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070056import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070057import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070058import android.widget.PopupMenu;
Anne Rong35208002015-07-22 16:27:37 -070059import android.widget.TextView;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080060import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070061
Yorke Leec3766332013-07-31 11:13:16 -070062import com.android.contacts.common.dialog.ClearFrequentsDialog;
63import com.android.contacts.common.interactions.ImportExportDialogFragment;
Nancy Chen0d3a7392014-08-12 14:44:53 -070064import com.android.contacts.common.interactions.TouchPointManager;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080065import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec16ea5a2015-05-19 15:51:01 -070066import com.android.contacts.common.util.PermissionsUtil;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -070067import com.android.contacts.common.widget.FloatingActionButtonController;
Brian Attwellbeab3bb2014-10-27 12:24:49 -070068import com.android.contacts.commonbind.analytics.AnalyticsUtil;
Yorke Leec3766332013-07-31 11:13:16 -070069import com.android.dialer.calllog.CallLogActivity;
Yorke Lee38019af2015-07-14 17:05:38 -070070import com.android.dialer.calllog.CallLogFragment;
Yorke Leefce269a2013-08-12 11:56:04 -070071import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070072import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070073import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070074import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070075import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080076import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070077import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080078import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070079import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070080import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070081import com.android.dialer.list.RegularSearchFragment;
82import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070083import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070084import com.android.dialer.list.SpeedDialFragment;
Yorke Leeaf6f1952014-07-14 19:13:16 -070085import com.android.dialer.settings.DialerSettingsActivity;
Andrew Lee247df6e2015-05-12 19:09:00 -070086import com.android.dialer.util.IntentUtil;
Yorke Lee827a90f2015-09-17 18:10:58 -070087import com.android.dialer.util.TelecomUtil;
Yorke Leee3a2d132015-09-14 16:52:08 -070088import com.android.dialer.util.IntentUtil.CallIntentBuilder;
Yorke Lee7d20f822014-06-19 17:09:33 -070089import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070090import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070091import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070092import com.android.dialer.widget.SearchEditTextLayout.Callback;
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 Leec8d6e162015-09-14 18:43:27 -070095import com.android.incallui.Call.LogState;
Yorke Lee2fec47b2014-08-05 18:16:27 -070096import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070097import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070098
Yorke Leed5c512a2015-01-30 14:42:53 -080099import junit.framework.Assert;
100
Yorke Leec3766332013-07-31 11:13:16 -0700101import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -0800102import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -0700103
Chiao Cheng94b10b52012-08-17 16:59:12 -0700104/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700105 * The dialer tab's title is 'phone', a more common name (see strings.xml).
106 */
Yorke Leec3766332013-07-31 11:13:16 -0700107public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700108 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700109 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700110 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700111 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700112 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700113 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700114 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700115 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700116 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700117 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700118 ViewPager.OnPageChangeListener,
119 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700120 private static final String TAG = "DialtactsActivity";
121
Yorke Lee7cc61492015-06-01 14:59:33 -0700122 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700123
Yorke Leef74011e2013-08-02 11:30:30 -0700124 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
125
Yorke Leeb207f8a2013-08-29 18:51:06 -0700126 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
127 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700128 private static final String KEY_SEARCH_QUERY = "search_query";
129 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700130 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700131
Yorke Lee135e4652015-10-23 18:12:51 -0700132 @VisibleForTesting
133 public static final String TAG_DIALPAD_FRAGMENT = "dialpad";
Yorke Leec3766332013-07-31 11:13:16 -0700134 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
135 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
136 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700137
Chiao Cheng94b10b52012-08-17 16:59:12 -0700138 /**
139 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
140 */
141 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700142 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700143
Yorke Leec3766332013-07-31 11:13:16 -0700144 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700145
Andrew Lee6324f702015-06-16 14:45:58 -0700146 private static final int FAB_SCALE_IN_DELAY_MS = 300;
147
Nancy Chenf3d9f822015-09-11 00:47:31 -0400148 private CoordinatorLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700149
Yorke Leec3766332013-07-31 11:13:16 -0700150 /**
Yorke Leec3766332013-07-31 11:13:16 -0700151 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700152 */
Evan Charlton72d66252014-11-07 16:23:47 -0800153 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700154
155 /**
156 * Fragment for searching phone numbers using the alphanumeric keyboard.
157 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700158 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700159
160 /**
161 * Fragment for searching phone numbers using the dialpad.
162 */
163 private SmartDialSearchFragment mSmartDialSearchFragment;
164
Yorke Leee00c9fe2014-04-12 12:42:06 -0700165 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700166 * Animation that slides in.
167 */
168 private Animation mSlideIn;
169
170 /**
171 * Animation that slides out.
172 */
173 private Animation mSlideOut;
174
Yorke Lee6c450eb2015-05-08 09:45:47 -0700175 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
176 @Override
177 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700178 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700179 }
180 };
181
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700182 /**
183 * Listener for after slide out animation completes on dialer fragment.
184 */
185 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
186 @Override
187 public void onAnimationEnd(Animation animation) {
188 commitDialpadFragmentHide();
189 }
190 };
191
192 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700193 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700194 */
195 private ListsFragment mListsFragment;
196
Yorke Lee19e68ca2014-10-28 11:51:40 -0700197 /**
198 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
199 * be commited.
200 */
201 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800202 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700203 private boolean mInDialpadSearch;
204 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700205 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700206 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700207 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700208
Yorke Leeef2b7382013-08-09 17:39:25 -0700209 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700210 * Whether or not the device is in landscape orientation.
211 */
212 private boolean mIsLandscape;
213
214 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700215 * True if the dialpad is only temporarily showing due to being in call
216 */
217 private boolean mInCallDialpadUp;
218
219 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700220 * True when this activity has been launched for the first time.
221 */
Yorke Lee98702de2013-08-06 12:03:32 -0700222 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700223
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700224 /**
225 * Search query to be applied to the SearchView in the ActionBar once
226 * onCreateOptionsMenu has been called.
227 */
228 private String mPendingSearchViewQuery;
229
Yorke Lee74edcdc2014-07-11 16:15:08 -0700230 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700231 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700232 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700233
Yorke Leeef2b7382013-08-09 17:39:25 -0700234 private String mSearchQuery;
Yorke Leed9a93162015-10-14 09:39:05 -0700235 private String mDialpadQuery;
Yorke Leeef2b7382013-08-09 17:39:25 -0700236
Yorke Leefce269a2013-08-12 11:56:04 -0700237 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700238 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700239 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700240
Sai Cheemalapati41460652014-06-02 21:05:39 -0700241 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700242
Andrew Lee2423a2f2014-05-29 14:14:45 -0700243 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700244
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800245 /**
246 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
247 * {@link #onResume()} to populate the search box.
248 */
249 private String mVoiceSearchQuery;
250
Andrew Lee467de3d2015-03-17 15:47:24 -0700251 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700252 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700253 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700254 }
255
256 @Override
257 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700258 final boolean hasContactsPermission =
259 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700260 final Menu menu = getMenu();
261 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700262 clearFrequents.setVisible(mListsFragment != null &&
263 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700264 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
265
266 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
267 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700268
269 menu.findItem(R.id.menu_history).setVisible(
270 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700271 super.show();
272 }
273 }
274
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275 /**
Yorke Lee28266192014-05-01 10:05:52 -0700276 * Listener that listens to drag events and sends their x and y coordinates to a
277 * {@link DragDropController}.
278 */
279 private class LayoutOnDragListener implements OnDragListener {
280 @Override
281 public boolean onDrag(View v, DragEvent event) {
282 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700283 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700284 }
285 return true;
286 }
287 }
288
289 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700290 * Listener used to send search queries to the phone search fragment.
291 */
Yorke Lee53a22302014-04-29 18:13:46 -0700292 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700293 @Override
294 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
295 }
Yorke Leec3766332013-07-31 11:13:16 -0700296
Andrew Lee99a570c2014-05-15 14:18:50 -0700297 @Override
298 public void onTextChanged(CharSequence s, int start, int before, int count) {
299 final String newText = s.toString();
300 if (newText.equals(mSearchQuery)) {
301 // If the query hasn't changed (perhaps due to activity being destroyed
302 // and restored, or user launching the same DIAL intent twice), then there is
303 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700304 return;
305 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700306 if (DEBUG) {
307 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700308 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700309 }
Yorke Lee710709d2014-05-29 07:18:42 -0700310 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700311
Andrew Lee90374a72014-05-16 15:01:09 -0700312 // Show search fragment only when the query string is changed to non-empty text.
313 if (!TextUtils.isEmpty(newText)) {
314 // Call enterSearchUi only if we are switching search modes, or showing a search
315 // fragment for the first time.
316 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
317 (!mIsDialpadShown && mInRegularSearch);
318 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700319 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700320 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700321 }
322
Andrew Leea8515422014-06-13 16:53:54 -0700323 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700324 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700325 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700326 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700327 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700328 }
329
330 @Override
331 public void afterTextChanged(Editable s) {
332 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700333 };
334
Andrew Leeb1903842014-05-15 16:11:24 -0700335
336 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700337 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700338 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700339 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700340 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700341 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700342 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700343 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700344 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
345 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700346 }
Andrew Leeb1903842014-05-15 16:11:24 -0700347 }
348 };
349
350 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700351 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700352 */
353 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
354 @Override
355 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700356 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
357 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
358 // If the search term is empty, close the search UI.
359 maybeExitSearchUi();
360 } else {
361 // If the search term is not empty, show the dialpad fab.
362 showFabInSearchUi();
363 }
Andrew Leeb1903842014-05-15 16:11:24 -0700364 }
365 return false;
366 }
367 };
368
Chiao Cheng94b10b52012-08-17 16:59:12 -0700369 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700370 public boolean dispatchTouchEvent(MotionEvent ev) {
371 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
372 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
373 }
374 return super.dispatchTouchEvent(ev);
Nancy Chen49db1ad2014-08-18 20:10:17 -0700375 }
376
377 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700378 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800379 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700380 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700381
Yorke Lee98702de2013-08-06 12:03:32 -0700382 mFirstLaunch = true;
383
Andrew Lee2423a2f2014-05-29 14:14:45 -0700384 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700385 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700386
Yorke Leef5554ce2015-01-27 14:46:02 -0800387 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700388 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800389 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800390 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700391
Yorke Leef5554ce2015-01-27 14:46:02 -0800392 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700393 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700394 actionBar.setCustomView(R.layout.search_edittext);
395 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700396 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700397
Anne Rong35208002015-07-22 16:27:37 -0700398 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
399 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700400 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
401
Andrew Leee3f59a82015-02-26 12:06:35 -0800402 mActionBarController = new ActionBarController(this, searchEditTextLayout);
403
Andrew Lee04675a52014-06-05 18:36:20 -0700404 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700405 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700406 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
407 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
408 .setOnClickListener(mSearchViewOnClickListener);
409 searchEditTextLayout.findViewById(R.id.search_box_start_search)
410 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700411 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700412 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700413 @Override
414 public void onBackButtonClicked() {
415 onBackPressed();
416 }
Andrew Lee6324f702015-06-16 14:45:58 -0700417
418 @Override
419 public void onSearchViewClicked() {
420 // Hide FAB, as the keyboard is shown.
421 mFloatingActionButtonController.scaleOut();
422 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700423 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700424
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700425 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700426 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700427
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700428 final View floatingActionButtonContainer = findViewById(
429 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700430 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
431 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700432 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700433 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700434
Andrew Lee04675a52014-06-05 18:36:20 -0700435 ImageButton optionsMenuButton =
436 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700437 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700438 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
439 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700440
Yorke Leed5c512a2015-01-30 14:42:53 -0800441 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
442 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700443 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800444 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000445 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800446 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700447 } else {
448 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700449 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
450 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700451 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700452 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700453 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700454 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700455
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700456 final boolean isLayoutRtl = DialerUtils.isRtl();
457 if (mIsLandscape) {
458 mSlideIn = AnimationUtils.loadAnimation(this,
459 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
460 mSlideOut = AnimationUtils.loadAnimation(this,
461 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
462 } else {
463 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
464 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
465 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700466
Yorke Lee2fec47b2014-08-05 18:16:27 -0700467 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
468 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
469
Yorke Lee6c450eb2015-05-08 09:45:47 -0700470 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700471 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700472
Nancy Chenf3d9f822015-09-11 00:47:31 -0400473 mParentLayout = (CoordinatorLayout) findViewById(R.id.dialtacts_mainlayout);
Yorke Leef614f6f2014-10-03 10:44:19 -0700474 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700475 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700476 new ViewTreeObserver.OnGlobalLayoutListener() {
477 @Override
478 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700479 final ViewTreeObserver observer =
480 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700481 if (!observer.isAlive()) {
482 return;
483 }
484 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700485 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700486 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700487 mFloatingActionButtonController.align(
488 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700489 }
490 });
Andrew Lee0c667702014-05-12 14:31:45 -0700491
Yorke Leef5554ce2015-01-27 14:46:02 -0800492 Trace.endSection();
493
494 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700495 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700496 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800497 Trace.endSection();
498 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700499 }
500
501 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700502 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800503 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700504 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700505
Yorke Lee19e68ca2014-10-28 11:51:40 -0700506 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700507 if (mFirstLaunch) {
508 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700509 } else if (!phoneIsInUse() && mInCallDialpadUp) {
510 hideDialpadFragment(false, true);
511 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700512 } else if (mShowDialpadOnResume) {
513 showDialpadFragment(false);
514 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700515 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800516
517 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
518 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
519 // shown until onResume has completed. Active the search UI and set the search term now.
520 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
521 mActionBarController.onSearchBoxTapped();
522 mSearchView.setText(mVoiceSearchQuery);
523 mVoiceSearchQuery = null;
524 }
525
Yorke Lee98702de2013-08-06 12:03:32 -0700526 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800527
528 if (mIsRestarting) {
529 // This is only called when the activity goes from resumed -> paused -> resumed, so it
530 // will not cause an extra view to be sent out on rotation
531 if (mIsDialpadShown) {
532 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
533 }
534 mIsRestarting = false;
535 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700536
Yorke Lee53a22302014-04-29 18:13:46 -0700537 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700538 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700539 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700540
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700541 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
542 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
543 // used internally.
544 final Bundle extras = getIntent().getExtras();
545 if (extras != null
546 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
547 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
548 } else {
549 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
550 }
551 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700552 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
553 if (index < mListsFragment.getTabCount()) {
554 mListsFragment.showTab(index);
555 }
556 }
557
Anne Rong35208002015-07-22 16:27:37 -0700558 setSearchBoxHint();
559
Yorke Leef5554ce2015-01-27 14:46:02 -0800560 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561 }
562
563 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800564 protected void onRestart() {
565 super.onRestart();
566 mIsRestarting = true;
567 }
568
569 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700570 protected void onPause() {
571 if (mClearSearchOnPause) {
572 hideDialpadAndSearchUi();
573 mClearSearchOnPause = false;
574 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700575 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
576 commitDialpadFragmentHide();
577 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700578 super.onPause();
579 }
580
581 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700582 protected void onSaveInstanceState(Bundle outState) {
583 super.onSaveInstanceState(outState);
584 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700585 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
586 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700587 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700588 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700589 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700590 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700591 }
592
593 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700594 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700595 if (fragment instanceof DialpadFragment) {
596 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800597 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700598 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
599 transaction.hide(mDialpadFragment);
600 transaction.commit();
601 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700602 } else if (fragment instanceof SmartDialSearchFragment) {
603 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700604 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leed9a93162015-10-14 09:39:05 -0700605 if (!TextUtils.isEmpty(mDialpadQuery)) {
606 mSmartDialSearchFragment.setAddToContactNumber(mDialpadQuery);
607 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700608 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700609 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700610 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700611 } else if (fragment instanceof ListsFragment) {
612 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700613 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700614 }
Yorke Leec3766332013-07-31 11:13:16 -0700615 }
616
Alon Albertb453e5b2013-09-10 15:54:23 -0700617 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700618 final Intent intent = new Intent(this, DialerSettingsActivity.class);
619 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700620 }
621
Chiao Cheng94b10b52012-08-17 16:59:12 -0700622 @Override
623 public void onClick(View view) {
624 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700625 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700626 if (mListsFragment.getCurrentTabIndex()
627 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700628 DialerUtils.startActivityWithErrorToast(
629 this,
630 IntentUtil.getNewContactIntent(),
631 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700632 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700633 mInCallDialpadUp = false;
634 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700635 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700636 break;
Yorke Leec3766332013-07-31 11:13:16 -0700637 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800638 try {
639 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
640 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
641 } catch (ActivityNotFoundException e) {
642 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
643 Toast.LENGTH_SHORT).show();
644 }
Yorke Leec3766332013-07-31 11:13:16 -0700645 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700646 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700647 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700648 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700649 default: {
650 Log.wtf(TAG, "Unexpected onClick event from " + view);
651 break;
652 }
653 }
654 }
655
Yorke Leec3766332013-07-31 11:13:16 -0700656 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700657 public boolean onMenuItemClick(MenuItem item) {
Jay Shrauner193cfc52015-09-25 14:13:16 -0700658 if (!isSafeToCommitTransactions()) {
Jay Shrauner119b0652015-09-17 12:00:18 -0700659 return true;
660 }
661
Yorke Lee86e21f72014-04-02 16:49:38 -0700662 switch (item.getItemId()) {
663 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700664 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
665 // CONTENT_TYPE, so that we always open our call log from our dialer
666 final Intent intent = new Intent(this, CallLogActivity.class);
667 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700668 break;
669 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700670 DialerUtils.startActivityWithErrorToast(
671 this,
672 IntentUtil.getNewContactIntent(),
673 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700674 break;
675 case R.id.menu_import_export:
676 // We hard-code the "contactsAreAvailable" argument because doing it properly would
677 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
678 // now in Dialtacts for (potential) performance reasons. Compare with how it is
679 // done in {@link PeopleActivity}.
Wenyi Wang5953e8c2015-09-24 15:38:14 -0700680 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
681 ImportExportDialogFragment.show(getFragmentManager(), true,
682 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_FAVORITES);
683 } else {
684 ImportExportDialogFragment.show(getFragmentManager(), true,
685 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_DEFAULT);
686 }
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());
690 return true;
691 case R.id.menu_call_settings:
692 handleMenuSettings();
693 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700694 }
695 return false;
696 }
697
698 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700699 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
700 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
701 if (resultCode == RESULT_OK) {
702 final ArrayList<String> matches = data.getStringArrayListExtra(
703 RecognizerIntent.EXTRA_RESULTS);
704 if (matches.size() > 0) {
705 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800706 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700707 } else {
708 Log.e(TAG, "Voice search - nothing heard");
709 }
710 } else {
711 Log.e(TAG, "Voice search failed");
712 }
713 }
714 super.onActivityResult(requestCode, resultCode, data);
715 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700716
Andrew Lee2a58ab82014-05-15 02:16:04 -0700717 /**
Nancy Chenea7c0b82015-08-31 17:18:41 -0700718 * Update the number of unread voicemails (potentially other tabs) displayed next to the tab
719 * icon.
720 */
721 public void updateTabUnreadCounts() {
722 mListsFragment.updateTabUnreadCounts();
723 }
724
725 /**
Andrew Lee2a58ab82014-05-15 02:16:04 -0700726 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
727 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
728 * @see #onDialpadShown
729 */
Yorke Leec3766332013-07-31 11:13:16 -0700730 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700731 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700732 return;
733 }
734 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800735
Yorke Lee7ee5c422014-11-04 10:29:32 -0800736 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700737
Chiao Cheng94b10b52012-08-17 16:59:12 -0700738 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800739 if (mDialpadFragment == null) {
740 mDialpadFragment = new DialpadFragment();
741 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
742 } else {
743 ft.show(mDialpadFragment);
744 }
745
746 mDialpadFragment.setAnimate(animate);
747 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700748 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700749
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700750 if (animate) {
751 mFloatingActionButtonController.scaleOut();
752 } else {
753 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700754 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700755 }
Yorke Lee5253be02014-05-21 18:50:03 -0700756 mActionBarController.onDialpadUp();
757
Yorke Lee6c450eb2015-05-08 09:45:47 -0700758 mListsFragment.getView().animate().alpha(0).withLayer();
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 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700811 }
812
813 /**
814 * Finishes hiding the dialpad fragment after any animations are completed.
815 */
816 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800817 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700818 final FragmentTransaction ft = getFragmentManager().beginTransaction();
819 ft.hide(mDialpadFragment);
820 ft.commit();
821 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700822 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700823 }
824
Andrew Lee2a58ab82014-05-15 02:16:04 -0700825 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700826 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700827 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700828 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700829 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700830 fragment = mRegularSearchFragment;
831 }
832 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700833 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700834 }
835 }
836
Yorke Lee5253be02014-05-21 18:50:03 -0700837 @Override
838 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700839 return mInDialpadSearch || mInRegularSearch;
840 }
841
Yorke Lee5253be02014-05-21 18:50:03 -0700842 @Override
843 public boolean hasSearchQuery() {
844 return !TextUtils.isEmpty(mSearchQuery);
845 }
846
847 @Override
848 public boolean shouldShowActionBar() {
849 return mListsFragment.shouldShowActionBar();
850 }
851
Yorke Leeb207f8a2013-08-29 18:51:06 -0700852 private void setNotInSearchUi() {
853 mInDialpadSearch = false;
854 mInRegularSearch = false;
855 }
856
Yorke Lee311969c2013-08-26 06:31:11 -0700857 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700858 if (mIsDialpadShown) {
859 hideDialpadFragment(false, true);
860 } else {
861 exitSearchUi();
862 }
Yorke Lee311969c2013-08-26 06:31:11 -0700863 }
864
Yorke Lee53a22302014-04-29 18:13:46 -0700865 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700866 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
867 if (canIntentBeHandled(voiceIntent)) {
868 mVoiceSearchButton.setVisibility(View.VISIBLE);
869 mVoiceSearchButton.setOnClickListener(this);
870 } else {
871 mVoiceSearchButton.setVisibility(View.GONE);
872 }
873 }
874
Yorke Leee9c82332015-10-06 14:21:53 -0700875 public boolean isNearbyPlacesSearchEnabled() {
876 return false;
877 }
878
Anne Rong35208002015-07-22 16:27:37 -0700879 protected int getSearchBoxHint () {
880 return R.string.dialer_hint_find_contact;
881 }
882
883 /**
884 * Sets the hint text for the contacts search box
885 */
886 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700887 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700888 .getCustomView().findViewById(R.id.search_view_container);
889 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
890 .setHint(getSearchBoxHint());
891 }
892
Andrew Lee467de3d2015-03-17 15:47:24 -0700893 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700894 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
895 popupMenu.inflate(R.menu.dialtacts_options);
896 popupMenu.setOnMenuItemClickListener(this);
897 return popupMenu;
898 }
899
Yorke Lee86e21f72014-04-02 16:49:38 -0700900 @Override
901 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700902 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700903 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700904 mPendingSearchViewQuery = null;
905 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700906 if (mActionBarController != null) {
907 mActionBarController.restoreActionBarOffset();
908 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700909 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700910 }
911
Chiao Cheng94b10b52012-08-17 16:59:12 -0700912 /**
913 * Returns true if the intent is due to hitting the green send key (hardware call button:
914 * KEYCODE_CALL) while in a call.
915 *
916 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700917 * @return true if the intent is due to hitting the green send key while in a call
918 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700919 private boolean isSendKeyWhileInCall(Intent intent) {
920 // If there is a call in progress and the user launched the dialer by hitting the call
921 // button, go straight to the in-call screen.
922 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700923
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700924 if (callKey) {
Yorke Lee762b3572015-09-18 12:54:59 -0700925 TelecomUtil.showInCallScreen(this, false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700926 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700927 }
928
929 return false;
930 }
931
932 /**
933 * Sets the current tab based on the intent's request type
934 *
935 * @param intent Intent that contains information about which tab should be selected
936 */
Yorke Leec3766332013-07-31 11:13:16 -0700937 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700938 // 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 -0700939 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700940 finish();
941 return;
942 }
943
Yorke Leed77017d2015-08-31 16:19:43 -0700944 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
945 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800946 showDialpadFragment(false);
947 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700948 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800949 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700950 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700951 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700952 }
953
954 @Override
955 public void onNewIntent(Intent newIntent) {
956 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700957
Nancy Chen199b7f02014-11-05 15:03:00 -0800958 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700959 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700960
Chiao Cheng94b10b52012-08-17 16:59:12 -0700961 invalidateOptionsMenu();
962 }
963
964 /** Returns true if the given intent contains a phone number to populate the dialer with */
965 private boolean isDialIntent(Intent intent) {
966 final String action = intent.getAction();
967 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
968 return true;
969 }
970 if (Intent.ACTION_VIEW.equals(action)) {
971 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700972 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700973 return true;
974 }
975 }
976 return false;
977 }
978
979 /**
Yorke Leec3766332013-07-31 11:13:16 -0700980 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700981 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700982 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700983 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700984 // Weird race condition where fragment is doing work after the activity is destroyed
985 // due to talkback being on (b/10209937). Just return since we can't do any
986 // constructive here.
987 return;
988 }
989
Yorke Leeef2b7382013-08-09 17:39:25 -0700990 if (DEBUG) {
991 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
992 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700993
Yorke Leec3766332013-07-31 11:13:16 -0700994 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700995 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700996 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700997 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700998 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700999 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001000
Yorke Leeb207f8a2013-08-29 18:51:06 -07001001 final String tag;
1002 if (smartDialSearch) {
1003 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
1004 } else {
1005 tag = TAG_REGULAR_SEARCH_FRAGMENT;
1006 }
1007 mInDialpadSearch = smartDialSearch;
1008 mInRegularSearch = !smartDialSearch;
1009
Andrew Lee6324f702015-06-16 14:45:58 -07001010 mFloatingActionButtonController.scaleOut();
1011
Andrew Lee752956e2014-05-15 11:43:38 -07001012 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001013 if (animate) {
1014 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
1015 } else {
1016 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
1017 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001018 if (fragment == null) {
1019 if (smartDialSearch) {
1020 fragment = new SmartDialSearchFragment();
1021 } else {
Yorke Lee807afd82015-10-12 10:03:07 -07001022 fragment = ObjectFactory.newRegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001023 fragment.setOnTouchListener(new View.OnTouchListener() {
1024 @Override
1025 public boolean onTouch(View v, MotionEvent event) {
1026 // Show the FAB when the user touches the lists fragment and the soft
1027 // keyboard is hidden.
1028 showFabInSearchUi();
1029 return false;
1030 }
1031 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001032 }
Andrew Leeb1903842014-05-15 16:11:24 -07001033 transaction.add(R.id.dialtacts_frame, fragment, tag);
1034 } else {
1035 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001036 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001037 // DialtactsActivity will provide the options menu
1038 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001039 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001040 if (!smartDialSearch) {
1041 fragment.setQueryString(query, false /* delaySelection */);
1042 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001043 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001044
Yorke Lee6c450eb2015-05-08 09:45:47 -07001045 if (animate) {
1046 mListsFragment.getView().animate().alpha(0).withLayer();
1047 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001048 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001049 }
1050
1051 /**
Yorke Leec3766332013-07-31 11:13:16 -07001052 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001053 */
Yorke Leec3766332013-07-31 11:13:16 -07001054 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001055 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001056 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001057 return;
1058 }
Andrew Leeb1903842014-05-15 16:11:24 -07001059
Andrew Leeb1903842014-05-15 16:11:24 -07001060 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001061
1062 if (mDialpadFragment != null) {
1063 mDialpadFragment.clearDialpad();
1064 }
1065
Yorke Leeb207f8a2013-08-29 18:51:06 -07001066 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001067
Andrew Lee6324f702015-06-16 14:45:58 -07001068 // Restore the FAB for the lists fragment.
1069 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1070 mFloatingActionButtonController.setVisible(false);
1071 }
1072 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1073 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1074 onPageSelected(mListsFragment.getCurrentTabIndex());
1075
Andrew Leeb1903842014-05-15 16:11:24 -07001076 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001077 if (mSmartDialSearchFragment != null) {
1078 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001079 }
Andrew Leeb1903842014-05-15 16:11:24 -07001080 if (mRegularSearchFragment != null) {
1081 transaction.remove(mRegularSearchFragment);
1082 }
1083 transaction.commit();
1084
1085 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001086
1087 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001088 // If the dialpad fragment wasn't previously visible, then send a screen view because
1089 // we are exiting regular search. Otherwise, the screen view will be sent by
1090 // {@link #hideDialpadFragment}.
1091 mListsFragment.sendScreenViewForCurrentPosition();
1092 mListsFragment.setUserVisibleHint(true);
1093 }
1094
Yorke Lee5253be02014-05-21 18:50:03 -07001095 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001096 }
1097
Chiao Cheng94b10b52012-08-17 16:59:12 -07001098 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001099 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001100 if (mStateSaved) {
1101 return;
1102 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001103 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001104 if (TextUtils.isEmpty(mSearchQuery) ||
1105 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1106 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001107 exitSearchUi();
1108 }
Yorke Leef6673d32013-08-23 15:34:17 -07001109 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001110 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001111 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001112 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001113 } else {
1114 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001115 }
Yorke Leec3766332013-07-31 11:13:16 -07001116 }
1117
Yorke Lee81a313d2015-06-01 14:53:59 -07001118 private void maybeEnterSearchUi() {
1119 if (!isInSearchUi()) {
1120 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1121 }
1122 }
1123
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001124 /**
1125 * @return True if the search UI was exited, false otherwise
1126 */
1127 private boolean maybeExitSearchUi() {
1128 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1129 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001130 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001131 return true;
1132 }
1133 return false;
1134 }
1135
Andrew Lee6324f702015-06-16 14:45:58 -07001136 private void showFabInSearchUi() {
1137 mFloatingActionButtonController.changeIcon(
1138 getResources().getDrawable(R.drawable.fab_ic_dial),
1139 getResources().getString(R.string.action_menu_dialpad_button));
1140 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1141 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1142 }
1143
Yorke Leec3766332013-07-31 11:13:16 -07001144 @Override
1145 public void onDialpadQueryChanged(String query) {
Yorke Leed9a93162015-10-14 09:39:05 -07001146 mDialpadQuery = query;
Yorke Lee46635872014-04-11 11:20:15 -07001147 if (mSmartDialSearchFragment != null) {
1148 mSmartDialSearchFragment.setAddToContactNumber(query);
1149 }
Yorke Leec3766332013-07-31 11:13:16 -07001150 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1151 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001152
1153 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001154 if (DEBUG) {
1155 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1156 }
1157 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1158 // This callback can happen if the dialpad fragment is recreated because of
1159 // activity destruction. In that case, don't update the search view because
1160 // that would bring the user back to the search fragment regardless of the
1161 // previous state of the application. Instead, just return here and let the
1162 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001163 if (!TextUtils.isEmpty(normalizedQuery)) {
1164 mPendingSearchViewQuery = normalizedQuery;
1165 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001166 return;
1167 }
Yorke Lee53a22302014-04-29 18:13:46 -07001168 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001169 }
Santos Cordon83131712015-04-16 21:25:35 -07001170
1171 try {
1172 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1173 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1174 }
1175 } catch (Exception ignored) {
1176 // Skip any exceptions for this piece of code
1177 }
Yorke Lee575ae382015-07-16 11:36:07 -07001178 }
Santos Cordon83131712015-04-16 21:25:35 -07001179
Yorke Lee575ae382015-07-16 11:36:07 -07001180 @Override
1181 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1182 if (mInDialpadSearch && mSmartDialSearchFragment != null
1183 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1184 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1185 return true;
1186 }
1187 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001188 }
Yorke Leec3766332013-07-31 11:13:16 -07001189
1190 @Override
1191 public void onListFragmentScrollStateChange(int scrollState) {
1192 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001193 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001194 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001195 }
1196 }
1197
1198 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001199 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001200 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001201 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1202 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001203 }
1204
Yorke Leec3766332013-07-31 11:13:16 -07001205 private boolean phoneIsInUse() {
Yorke Lee827a90f2015-09-17 18:10:58 -07001206 return TelecomUtil.isInCall(this);
Yorke Leec3766332013-07-31 11:13:16 -07001207 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001208
Yorke Leeda0f9042013-12-05 14:25:51 -08001209 private boolean canIntentBeHandled(Intent intent) {
1210 final PackageManager packageManager = getPackageManager();
1211 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1212 PackageManager.MATCH_DEFAULT_ONLY);
1213 return resolveInfo != null && resolveInfo.size() > 0;
1214 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001215
Yorke Lee86e21f72014-04-02 16:49:38 -07001216 /**
1217 * Called when the user has long-pressed a contact tile to start a drag operation.
1218 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001219 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001220 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001221 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001222 }
1223
1224 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001225 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1226 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001227
Yorke Lee86e21f72014-04-02 16:49:38 -07001228 /**
1229 * Called when the user has released a contact tile after long-pressing it.
1230 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001231 @Override
1232 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001233 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001234 }
1235
1236 @Override
1237 public void onDroppedOnRemove() {}
1238
1239 /**
Yorke Leed999b712014-04-23 15:10:58 -07001240 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001241 * once it has been attached to the activity.
1242 */
1243 @Override
1244 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001245 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001246 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001247 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001248
Yorke Lee38019af2015-07-14 17:05:38 -07001249 /**
1250 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1251 */
1252 @Override
1253 public void showAllContactsTab() {
1254 if (mListsFragment != null) {
1255 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1256 }
1257 }
1258
1259 /**
1260 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1261 */
1262 @Override
1263 public void showDialpad() {
1264 showDialpadFragment(true);
1265 }
1266
Yorke Leee00c9fe2014-04-12 12:42:06 -07001267 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001268 public void onPickDataUri(Uri dataUri, int callInitiationType) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001269 mClearSearchOnPause = true;
Yorke Leec8d6e162015-09-14 18:43:27 -07001270 PhoneNumberInteraction.startInteractionForPhoneCall(
1271 DialtactsActivity.this, dataUri, callInitiationType);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001272 }
1273
1274 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001275 public void onPickPhoneNumber(String phoneNumber, boolean isVideoCall, int callInitiationType) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001276 if (phoneNumber == null) {
1277 // Invalid phone number, but let the call go through so that InCallUI can show
1278 // an error message.
1279 phoneNumber = "";
1280 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001281
1282 final Intent intent = new CallIntentBuilder(phoneNumber)
1283 .setIsVideoCall(isVideoCall)
Yorke Leec8d6e162015-09-14 18:43:27 -07001284 .setCallInitiationType(callInitiationType)
Yorke Leee3a2d132015-09-14 16:52:08 -07001285 .build();
1286
Yorke Lee7d20f822014-06-19 17:09:33 -07001287 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001288 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001289 }
1290
1291 @Override
1292 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001293 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001294 }
1295
1296 @Override
1297 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001298 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001299 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001300
Yorke Leecc4660d2014-04-24 11:22:57 -07001301 @Override
1302 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001303 int tabIndex = mListsFragment.getCurrentTabIndex();
1304
Andrew Lee4de59fb2015-08-13 15:20:08 -07001305 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1306 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1307 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001308 boolean isRtl = DialerUtils.isRtl();
1309 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1310 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001311 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001312 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1313 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001314 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001315 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001316 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001317
Andrew Lee432d4b52015-05-20 16:52:00 -07001318 @Override
1319 public void onPageSelected(int position) {
1320 int tabIndex = mListsFragment.getCurrentTabIndex();
1321 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001322 mFloatingActionButtonController.changeIcon(
1323 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1324 getResources().getString(R.string.search_shortcut_create_new_contact));
1325 } else {
1326 mFloatingActionButtonController.changeIcon(
1327 getResources().getDrawable(R.drawable.fab_ic_dial),
1328 getResources().getString(R.string.action_menu_dialpad_button));
1329 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001330 }
1331
1332 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001333 public void onPageScrollStateChanged(int state) {
1334 }
1335
Yorke Lee5253be02014-05-21 18:50:03 -07001336 @Override
1337 public boolean isActionBarShowing() {
1338 return mActionBarController.isActionBarShowing();
1339 }
1340
Yorke Leec98a5bb2014-10-28 16:12:05 -07001341 @Override
1342 public ActionBarController getActionBarController() {
1343 return mActionBarController;
1344 }
1345
Yorke Lee6bc67162015-06-27 14:03:25 -07001346 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001347 public boolean isDialpadShown() {
1348 return mIsDialpadShown;
1349 }
1350
Yorke Lee5253be02014-05-21 18:50:03 -07001351 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001352 public int getDialpadHeight() {
1353 if (mDialpadFragment != null) {
1354 return mDialpadFragment.getDialpadHeight();
1355 }
1356 return 0;
1357 }
1358
1359 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001360 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001361 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001362 }
1363
1364 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001365 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001366 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001367 }
1368
1369 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001370 public int getActionBarHeight() {
1371 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001372 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001373
Andrew Lee6324f702015-06-16 14:45:58 -07001374 private int getFabAlignment() {
1375 if (!mIsLandscape && !isInSearchUi() &&
1376 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1377 return FloatingActionButtonController.ALIGN_MIDDLE;
1378 }
1379 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001380 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001381}