blob: 140437e40fa3407d9b25a28f51596211fa9f0282 [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 Leef44e5172015-10-01 17:16:43 -070085import com.android.dialer.onboard.OnboardingActivity;
Yorke Leeaf6f1952014-07-14 19:13:16 -070086import com.android.dialer.settings.DialerSettingsActivity;
Andrew Lee247df6e2015-05-12 19:09:00 -070087import com.android.dialer.util.IntentUtil;
Yorke Lee827a90f2015-09-17 18:10:58 -070088import com.android.dialer.util.TelecomUtil;
Yorke Leee3a2d132015-09-14 16:52:08 -070089import com.android.dialer.util.IntentUtil.CallIntentBuilder;
Yorke Lee7d20f822014-06-19 17:09:33 -070090import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070091import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070092import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070093import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070094import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee807afd82015-10-12 10:03:07 -070095import com.android.dialerbind.ObjectFactory;
Yorke Leec8d6e162015-09-14 18:43:27 -070096import com.android.incallui.Call.LogState;
Yorke Lee2fec47b2014-08-05 18:16:27 -070097import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070098import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070099
Yorke Leed5c512a2015-01-30 14:42:53 -0800100import junit.framework.Assert;
101
Yorke Leec3766332013-07-31 11:13:16 -0700102import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -0800103import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -0700104
Chiao Cheng94b10b52012-08-17 16:59:12 -0700105/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700106 * The dialer tab's title is 'phone', a more common name (see strings.xml).
107 */
Yorke Leec3766332013-07-31 11:13:16 -0700108public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700109 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700110 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700111 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700112 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700113 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700114 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700115 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700116 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700117 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700118 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700119 ViewPager.OnPageChangeListener,
120 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121 private static final String TAG = "DialtactsActivity";
122
Yorke Lee7cc61492015-06-01 14:59:33 -0700123 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700124
Yorke Leef74011e2013-08-02 11:30:30 -0700125 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
126
Yorke Leeb207f8a2013-08-29 18:51:06 -0700127 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
128 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700129 private static final String KEY_SEARCH_QUERY = "search_query";
130 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700131 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700132
Yorke Lee135e4652015-10-23 18:12:51 -0700133 @VisibleForTesting
134 public static final String TAG_DIALPAD_FRAGMENT = "dialpad";
Yorke Leec3766332013-07-31 11:13:16 -0700135 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
136 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
137 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700138
Chiao Cheng94b10b52012-08-17 16:59:12 -0700139 /**
140 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
141 */
142 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700143 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700144
Yorke Leec3766332013-07-31 11:13:16 -0700145 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700146
Andrew Lee6324f702015-06-16 14:45:58 -0700147 private static final int FAB_SCALE_IN_DELAY_MS = 300;
148
Nancy Chenf3d9f822015-09-11 00:47:31 -0400149 private CoordinatorLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700150
Yorke Leec3766332013-07-31 11:13:16 -0700151 /**
Yorke Leec3766332013-07-31 11:13:16 -0700152 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700153 */
Evan Charlton72d66252014-11-07 16:23:47 -0800154 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700155
156 /**
157 * Fragment for searching phone numbers using the alphanumeric keyboard.
158 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700159 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700160
161 /**
162 * Fragment for searching phone numbers using the dialpad.
163 */
164 private SmartDialSearchFragment mSmartDialSearchFragment;
165
Yorke Leee00c9fe2014-04-12 12:42:06 -0700166 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700167 * Animation that slides in.
168 */
169 private Animation mSlideIn;
170
171 /**
172 * Animation that slides out.
173 */
174 private Animation mSlideOut;
175
Yorke Lee6c450eb2015-05-08 09:45:47 -0700176 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
177 @Override
178 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700179 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700180 }
181 };
182
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700183 /**
184 * Listener for after slide out animation completes on dialer fragment.
185 */
186 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
187 @Override
188 public void onAnimationEnd(Animation animation) {
189 commitDialpadFragmentHide();
190 }
191 };
192
193 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700194 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700195 */
196 private ListsFragment mListsFragment;
197
Yorke Lee19e68ca2014-10-28 11:51:40 -0700198 /**
199 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
200 * be commited.
201 */
202 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800203 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700204 private boolean mInDialpadSearch;
205 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700206 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700207 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700208 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700209
Yorke Leeef2b7382013-08-09 17:39:25 -0700210 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700211 * Whether or not the device is in landscape orientation.
212 */
213 private boolean mIsLandscape;
214
215 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700216 * True if the dialpad is only temporarily showing due to being in call
217 */
218 private boolean mInCallDialpadUp;
219
220 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700221 * True when this activity has been launched for the first time.
222 */
Yorke Lee98702de2013-08-06 12:03:32 -0700223 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700224
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700225 /**
226 * Search query to be applied to the SearchView in the ActionBar once
227 * onCreateOptionsMenu has been called.
228 */
229 private String mPendingSearchViewQuery;
230
Yorke Lee74edcdc2014-07-11 16:15:08 -0700231 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700232 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700233 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700234
Yorke Leeef2b7382013-08-09 17:39:25 -0700235 private String mSearchQuery;
Yorke Leed9a93162015-10-14 09:39:05 -0700236 private String mDialpadQuery;
Yorke Leeef2b7382013-08-09 17:39:25 -0700237
Yorke Leefce269a2013-08-12 11:56:04 -0700238 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700239 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700240 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700241
Sai Cheemalapati41460652014-06-02 21:05:39 -0700242 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700243
Andrew Lee2423a2f2014-05-29 14:14:45 -0700244 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700245
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800246 /**
247 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
248 * {@link #onResume()} to populate the search box.
249 */
250 private String mVoiceSearchQuery;
251
Andrew Lee467de3d2015-03-17 15:47:24 -0700252 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700253 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700254 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700255 }
256
257 @Override
258 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700259 final boolean hasContactsPermission =
260 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700261 final Menu menu = getMenu();
262 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700263 clearFrequents.setVisible(mListsFragment != null &&
264 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700265 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
266
267 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
268 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700269
270 menu.findItem(R.id.menu_history).setVisible(
271 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700272 super.show();
273 }
274 }
275
Chiao Cheng94b10b52012-08-17 16:59:12 -0700276 /**
Yorke Lee28266192014-05-01 10:05:52 -0700277 * Listener that listens to drag events and sends their x and y coordinates to a
278 * {@link DragDropController}.
279 */
280 private class LayoutOnDragListener implements OnDragListener {
281 @Override
282 public boolean onDrag(View v, DragEvent event) {
283 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700284 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700285 }
286 return true;
287 }
288 }
289
290 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291 * Listener used to send search queries to the phone search fragment.
292 */
Yorke Lee53a22302014-04-29 18:13:46 -0700293 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700294 @Override
295 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
296 }
Yorke Leec3766332013-07-31 11:13:16 -0700297
Andrew Lee99a570c2014-05-15 14:18:50 -0700298 @Override
299 public void onTextChanged(CharSequence s, int start, int before, int count) {
300 final String newText = s.toString();
301 if (newText.equals(mSearchQuery)) {
302 // If the query hasn't changed (perhaps due to activity being destroyed
303 // and restored, or user launching the same DIAL intent twice), then there is
304 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700305 return;
306 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700307 if (DEBUG) {
308 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700309 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700310 }
Yorke Lee710709d2014-05-29 07:18:42 -0700311 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700312
Andrew Lee90374a72014-05-16 15:01:09 -0700313 // Show search fragment only when the query string is changed to non-empty text.
314 if (!TextUtils.isEmpty(newText)) {
315 // Call enterSearchUi only if we are switching search modes, or showing a search
316 // fragment for the first time.
317 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
318 (!mIsDialpadShown && mInRegularSearch);
319 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700320 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700321 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700322 }
323
Andrew Leea8515422014-06-13 16:53:54 -0700324 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700325 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700326 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700327 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700328 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700329 }
330
331 @Override
332 public void afterTextChanged(Editable s) {
333 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700334 };
335
Andrew Leeb1903842014-05-15 16:11:24 -0700336
337 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700338 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700339 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700340 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700341 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700342 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700343 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700344 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700345 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
346 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700347 }
Andrew Leeb1903842014-05-15 16:11:24 -0700348 }
349 };
350
351 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700352 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700353 */
354 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
355 @Override
356 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700357 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
358 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
359 // If the search term is empty, close the search UI.
360 maybeExitSearchUi();
361 } else {
362 // If the search term is not empty, show the dialpad fab.
363 showFabInSearchUi();
364 }
Andrew Leeb1903842014-05-15 16:11:24 -0700365 }
366 return false;
367 }
368 };
369
Chiao Cheng94b10b52012-08-17 16:59:12 -0700370 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700371 public boolean dispatchTouchEvent(MotionEvent ev) {
372 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
373 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
374 }
375 return super.dispatchTouchEvent(ev);
Nancy Chen49db1ad2014-08-18 20:10:17 -0700376 }
377
378 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700379 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800380 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700381 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700382
Yorke Leef44e5172015-10-01 17:16:43 -0700383 if (OnboardingActivity.shouldStartOnboardingActivity(this)) {
384 OnboardingActivity.startOnboardingActivity(this);
385 finish();
386 }
387
Yorke Lee98702de2013-08-06 12:03:32 -0700388 mFirstLaunch = true;
389
Andrew Lee2423a2f2014-05-29 14:14:45 -0700390 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700391 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700392
Yorke Leef5554ce2015-01-27 14:46:02 -0800393 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700394 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800395 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800396 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700397
Yorke Leef5554ce2015-01-27 14:46:02 -0800398 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700399 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700400 actionBar.setCustomView(R.layout.search_edittext);
401 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700402 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700403
Anne Rong35208002015-07-22 16:27:37 -0700404 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
405 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700406 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
407
Andrew Leee3f59a82015-02-26 12:06:35 -0800408 mActionBarController = new ActionBarController(this, searchEditTextLayout);
409
Andrew Lee04675a52014-06-05 18:36:20 -0700410 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700411 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700412 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
413 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
414 .setOnClickListener(mSearchViewOnClickListener);
415 searchEditTextLayout.findViewById(R.id.search_box_start_search)
416 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700417 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700418 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700419 @Override
420 public void onBackButtonClicked() {
421 onBackPressed();
422 }
Andrew Lee6324f702015-06-16 14:45:58 -0700423
424 @Override
425 public void onSearchViewClicked() {
426 // Hide FAB, as the keyboard is shown.
427 mFloatingActionButtonController.scaleOut();
428 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700429 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700430
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700431 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700432 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700433
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700434 final View floatingActionButtonContainer = findViewById(
435 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700436 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
437 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700438 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700439 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700440
Andrew Lee04675a52014-06-05 18:36:20 -0700441 ImageButton optionsMenuButton =
442 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700443 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700444 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
445 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700446
Yorke Leed5c512a2015-01-30 14:42:53 -0800447 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
448 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700449 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800450 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000451 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800452 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700453 } else {
454 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700455 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
456 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700457 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700458 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700459 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700460 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700461
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700462 final boolean isLayoutRtl = DialerUtils.isRtl();
463 if (mIsLandscape) {
464 mSlideIn = AnimationUtils.loadAnimation(this,
465 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
466 mSlideOut = AnimationUtils.loadAnimation(this,
467 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
468 } else {
469 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
470 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
471 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700472
Yorke Lee2fec47b2014-08-05 18:16:27 -0700473 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
474 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
475
Yorke Lee6c450eb2015-05-08 09:45:47 -0700476 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700477 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700478
Nancy Chenf3d9f822015-09-11 00:47:31 -0400479 mParentLayout = (CoordinatorLayout) findViewById(R.id.dialtacts_mainlayout);
Yorke Leef614f6f2014-10-03 10:44:19 -0700480 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700481 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700482 new ViewTreeObserver.OnGlobalLayoutListener() {
483 @Override
484 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700485 final ViewTreeObserver observer =
486 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700487 if (!observer.isAlive()) {
488 return;
489 }
490 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700491 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700492 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700493 mFloatingActionButtonController.align(
494 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700495 }
496 });
Andrew Lee0c667702014-05-12 14:31:45 -0700497
Yorke Leef5554ce2015-01-27 14:46:02 -0800498 Trace.endSection();
499
500 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700501 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700502 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800503 Trace.endSection();
504 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700505 }
506
507 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700508 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800509 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700510 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700511
Yorke Lee19e68ca2014-10-28 11:51:40 -0700512 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700513 if (mFirstLaunch) {
514 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700515 } else if (!phoneIsInUse() && mInCallDialpadUp) {
516 hideDialpadFragment(false, true);
517 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700518 } else if (mShowDialpadOnResume) {
519 showDialpadFragment(false);
520 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700521 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800522
523 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
524 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
525 // shown until onResume has completed. Active the search UI and set the search term now.
526 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
527 mActionBarController.onSearchBoxTapped();
528 mSearchView.setText(mVoiceSearchQuery);
529 mVoiceSearchQuery = null;
530 }
531
Yorke Lee98702de2013-08-06 12:03:32 -0700532 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800533
534 if (mIsRestarting) {
535 // This is only called when the activity goes from resumed -> paused -> resumed, so it
536 // will not cause an extra view to be sent out on rotation
537 if (mIsDialpadShown) {
538 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
539 }
540 mIsRestarting = false;
541 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700542
Yorke Lee53a22302014-04-29 18:13:46 -0700543 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700544 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700545 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700546
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700547 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
548 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
549 // used internally.
550 final Bundle extras = getIntent().getExtras();
551 if (extras != null
552 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
553 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
554 } else {
555 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
556 }
557 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700558 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
559 if (index < mListsFragment.getTabCount()) {
560 mListsFragment.showTab(index);
561 }
562 }
563
Anne Rong35208002015-07-22 16:27:37 -0700564 setSearchBoxHint();
565
Yorke Leef5554ce2015-01-27 14:46:02 -0800566 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700567 }
568
569 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800570 protected void onRestart() {
571 super.onRestart();
572 mIsRestarting = true;
573 }
574
575 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700576 protected void onPause() {
577 if (mClearSearchOnPause) {
578 hideDialpadAndSearchUi();
579 mClearSearchOnPause = false;
580 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700581 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
582 commitDialpadFragmentHide();
583 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700584 super.onPause();
585 }
586
587 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700588 protected void onSaveInstanceState(Bundle outState) {
589 super.onSaveInstanceState(outState);
590 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700591 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
592 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700593 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700594 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700595 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700596 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700597 }
598
599 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700600 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700601 if (fragment instanceof DialpadFragment) {
602 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800603 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700604 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
605 transaction.hide(mDialpadFragment);
606 transaction.commit();
607 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700608 } else if (fragment instanceof SmartDialSearchFragment) {
609 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700610 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leed9a93162015-10-14 09:39:05 -0700611 if (!TextUtils.isEmpty(mDialpadQuery)) {
612 mSmartDialSearchFragment.setAddToContactNumber(mDialpadQuery);
613 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700614 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700615 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700616 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700617 } else if (fragment instanceof ListsFragment) {
618 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700619 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700620 }
Yorke Leec3766332013-07-31 11:13:16 -0700621 }
622
Alon Albertb453e5b2013-09-10 15:54:23 -0700623 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700624 final Intent intent = new Intent(this, DialerSettingsActivity.class);
625 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700626 }
627
Chiao Cheng94b10b52012-08-17 16:59:12 -0700628 @Override
629 public void onClick(View view) {
630 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700631 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700632 if (mListsFragment.getCurrentTabIndex()
633 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700634 DialerUtils.startActivityWithErrorToast(
635 this,
636 IntentUtil.getNewContactIntent(),
637 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700638 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700639 mInCallDialpadUp = false;
640 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700641 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700642 break;
Yorke Leec3766332013-07-31 11:13:16 -0700643 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800644 try {
645 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
646 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
647 } catch (ActivityNotFoundException e) {
648 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
649 Toast.LENGTH_SHORT).show();
650 }
Yorke Leec3766332013-07-31 11:13:16 -0700651 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700652 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700653 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700654 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700655 default: {
656 Log.wtf(TAG, "Unexpected onClick event from " + view);
657 break;
658 }
659 }
660 }
661
Yorke Leec3766332013-07-31 11:13:16 -0700662 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700663 public boolean onMenuItemClick(MenuItem item) {
Jay Shrauner193cfc52015-09-25 14:13:16 -0700664 if (!isSafeToCommitTransactions()) {
Jay Shrauner119b0652015-09-17 12:00:18 -0700665 return true;
666 }
667
Yorke Lee86e21f72014-04-02 16:49:38 -0700668 switch (item.getItemId()) {
669 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700670 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
671 // CONTENT_TYPE, so that we always open our call log from our dialer
672 final Intent intent = new Intent(this, CallLogActivity.class);
673 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700674 break;
675 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700676 DialerUtils.startActivityWithErrorToast(
677 this,
678 IntentUtil.getNewContactIntent(),
679 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700680 break;
681 case R.id.menu_import_export:
682 // We hard-code the "contactsAreAvailable" argument because doing it properly would
683 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
684 // now in Dialtacts for (potential) performance reasons. Compare with how it is
685 // done in {@link PeopleActivity}.
Wenyi Wang5953e8c2015-09-24 15:38:14 -0700686 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
687 ImportExportDialogFragment.show(getFragmentManager(), true,
688 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_FAVORITES);
689 } else {
690 ImportExportDialogFragment.show(getFragmentManager(), true,
691 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_DEFAULT);
692 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700693 return true;
694 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700695 ClearFrequentsDialog.show(getFragmentManager());
696 return true;
697 case R.id.menu_call_settings:
698 handleMenuSettings();
699 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700700 }
701 return false;
702 }
703
704 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700705 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
706 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
707 if (resultCode == RESULT_OK) {
708 final ArrayList<String> matches = data.getStringArrayListExtra(
709 RecognizerIntent.EXTRA_RESULTS);
710 if (matches.size() > 0) {
711 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800712 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700713 } else {
714 Log.e(TAG, "Voice search - nothing heard");
715 }
716 } else {
717 Log.e(TAG, "Voice search failed");
718 }
719 }
720 super.onActivityResult(requestCode, resultCode, data);
721 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700722
Andrew Lee2a58ab82014-05-15 02:16:04 -0700723 /**
Nancy Chenea7c0b82015-08-31 17:18:41 -0700724 * Update the number of unread voicemails (potentially other tabs) displayed next to the tab
725 * icon.
726 */
727 public void updateTabUnreadCounts() {
728 mListsFragment.updateTabUnreadCounts();
729 }
730
731 /**
Andrew Lee2a58ab82014-05-15 02:16:04 -0700732 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
733 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
734 * @see #onDialpadShown
735 */
Yorke Leec3766332013-07-31 11:13:16 -0700736 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700737 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700738 return;
739 }
740 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800741
Yorke Lee7ee5c422014-11-04 10:29:32 -0800742 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700743
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800745 if (mDialpadFragment == null) {
746 mDialpadFragment = new DialpadFragment();
747 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
748 } else {
749 ft.show(mDialpadFragment);
750 }
751
752 mDialpadFragment.setAnimate(animate);
753 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700754 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700755
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700756 if (animate) {
757 mFloatingActionButtonController.scaleOut();
758 } else {
759 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700760 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700761 }
Yorke Lee5253be02014-05-21 18:50:03 -0700762 mActionBarController.onDialpadUp();
763
Yorke Lee6c450eb2015-05-08 09:45:47 -0700764 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700765 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700766
Andrew Lee2a58ab82014-05-15 02:16:04 -0700767 /**
768 * Callback from child DialpadFragment when the dialpad is shown.
769 */
770 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800771 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700772 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700773 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700774 } else {
775 mDialpadFragment.setYFraction(0);
776 }
777
Andrew Lee2a58ab82014-05-15 02:16:04 -0700778 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700779 }
780
781 /**
782 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
783 * a callback after the hide animation ends.
784 * @see #commitDialpadFragmentHide
785 */
Yorke Leeca195042013-09-25 16:29:38 -0700786 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700787 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700788 return;
789 }
Yorke Leef6673d32013-08-23 15:34:17 -0700790 if (clearDialpad) {
791 mDialpadFragment.clearDialpad();
792 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700793 if (!mIsDialpadShown) {
794 return;
Yorke Leec3766332013-07-31 11:13:16 -0700795 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700796 mIsDialpadShown = false;
797 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800798 mListsFragment.setUserVisibleHint(true);
799 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700800
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700801 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700802
Andrew Lee6324f702015-06-16 14:45:58 -0700803 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700804 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700805 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700806 } else {
807 commitDialpadFragmentHide();
808 }
809
Yorke Lee5253be02014-05-21 18:50:03 -0700810 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700811
Andrew Leed4cde832014-05-16 15:56:48 -0700812 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700813 if (TextUtils.isEmpty(mSearchQuery)) {
814 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700815 }
Andrew Leed4cde832014-05-16 15:56:48 -0700816 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700817 }
818
819 /**
820 * Finishes hiding the dialpad fragment after any animations are completed.
821 */
822 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800823 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700824 final FragmentTransaction ft = getFragmentManager().beginTransaction();
825 ft.hide(mDialpadFragment);
826 ft.commit();
827 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700828 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700829 }
830
Andrew Lee2a58ab82014-05-15 02:16:04 -0700831 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700832 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700833 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700834 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700835 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700836 fragment = mRegularSearchFragment;
837 }
838 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700839 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700840 }
841 }
842
Yorke Lee5253be02014-05-21 18:50:03 -0700843 @Override
844 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700845 return mInDialpadSearch || mInRegularSearch;
846 }
847
Yorke Lee5253be02014-05-21 18:50:03 -0700848 @Override
849 public boolean hasSearchQuery() {
850 return !TextUtils.isEmpty(mSearchQuery);
851 }
852
853 @Override
854 public boolean shouldShowActionBar() {
855 return mListsFragment.shouldShowActionBar();
856 }
857
Yorke Leeb207f8a2013-08-29 18:51:06 -0700858 private void setNotInSearchUi() {
859 mInDialpadSearch = false;
860 mInRegularSearch = false;
861 }
862
Yorke Lee311969c2013-08-26 06:31:11 -0700863 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700864 if (mIsDialpadShown) {
865 hideDialpadFragment(false, true);
866 } else {
867 exitSearchUi();
868 }
Yorke Lee311969c2013-08-26 06:31:11 -0700869 }
870
Yorke Lee53a22302014-04-29 18:13:46 -0700871 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700872 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
873 if (canIntentBeHandled(voiceIntent)) {
874 mVoiceSearchButton.setVisibility(View.VISIBLE);
875 mVoiceSearchButton.setOnClickListener(this);
876 } else {
877 mVoiceSearchButton.setVisibility(View.GONE);
878 }
879 }
880
Yorke Leee9c82332015-10-06 14:21:53 -0700881 public boolean isNearbyPlacesSearchEnabled() {
882 return false;
883 }
884
Anne Rong35208002015-07-22 16:27:37 -0700885 protected int getSearchBoxHint () {
886 return R.string.dialer_hint_find_contact;
887 }
888
889 /**
890 * Sets the hint text for the contacts search box
891 */
892 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700893 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700894 .getCustomView().findViewById(R.id.search_view_container);
895 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
896 .setHint(getSearchBoxHint());
897 }
898
Andrew Lee467de3d2015-03-17 15:47:24 -0700899 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700900 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
901 popupMenu.inflate(R.menu.dialtacts_options);
902 popupMenu.setOnMenuItemClickListener(this);
903 return popupMenu;
904 }
905
Yorke Lee86e21f72014-04-02 16:49:38 -0700906 @Override
907 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700908 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700909 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700910 mPendingSearchViewQuery = null;
911 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700912 if (mActionBarController != null) {
913 mActionBarController.restoreActionBarOffset();
914 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700915 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700916 }
917
Chiao Cheng94b10b52012-08-17 16:59:12 -0700918 /**
919 * Returns true if the intent is due to hitting the green send key (hardware call button:
920 * KEYCODE_CALL) while in a call.
921 *
922 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700923 * @return true if the intent is due to hitting the green send key while in a call
924 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700925 private boolean isSendKeyWhileInCall(Intent intent) {
926 // If there is a call in progress and the user launched the dialer by hitting the call
927 // button, go straight to the in-call screen.
928 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700929
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700930 if (callKey) {
Yorke Lee762b3572015-09-18 12:54:59 -0700931 TelecomUtil.showInCallScreen(this, false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700932 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700933 }
934
935 return false;
936 }
937
938 /**
939 * Sets the current tab based on the intent's request type
940 *
941 * @param intent Intent that contains information about which tab should be selected
942 */
Yorke Leec3766332013-07-31 11:13:16 -0700943 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700944 // 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 -0700945 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700946 finish();
947 return;
948 }
949
Yorke Leed77017d2015-08-31 16:19:43 -0700950 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
951 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800952 showDialpadFragment(false);
953 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700954 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800955 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700956 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700957 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700958 }
959
960 @Override
961 public void onNewIntent(Intent newIntent) {
962 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700963
Nancy Chen199b7f02014-11-05 15:03:00 -0800964 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700965 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700966
Chiao Cheng94b10b52012-08-17 16:59:12 -0700967 invalidateOptionsMenu();
968 }
969
970 /** Returns true if the given intent contains a phone number to populate the dialer with */
971 private boolean isDialIntent(Intent intent) {
972 final String action = intent.getAction();
973 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
974 return true;
975 }
976 if (Intent.ACTION_VIEW.equals(action)) {
977 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700978 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700979 return true;
980 }
981 }
982 return false;
983 }
984
985 /**
Yorke Leec3766332013-07-31 11:13:16 -0700986 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700987 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700988 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700989 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700990 // Weird race condition where fragment is doing work after the activity is destroyed
991 // due to talkback being on (b/10209937). Just return since we can't do any
992 // constructive here.
993 return;
994 }
995
Yorke Leeef2b7382013-08-09 17:39:25 -0700996 if (DEBUG) {
997 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
998 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700999
Yorke Leec3766332013-07-31 11:13:16 -07001000 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001001 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001002 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001003 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001004 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -07001005 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001006
Yorke Leeb207f8a2013-08-29 18:51:06 -07001007 final String tag;
1008 if (smartDialSearch) {
1009 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
1010 } else {
1011 tag = TAG_REGULAR_SEARCH_FRAGMENT;
1012 }
1013 mInDialpadSearch = smartDialSearch;
1014 mInRegularSearch = !smartDialSearch;
1015
Andrew Lee6324f702015-06-16 14:45:58 -07001016 mFloatingActionButtonController.scaleOut();
1017
Andrew Lee752956e2014-05-15 11:43:38 -07001018 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001019 if (animate) {
1020 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
1021 } else {
1022 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
1023 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001024 if (fragment == null) {
1025 if (smartDialSearch) {
1026 fragment = new SmartDialSearchFragment();
1027 } else {
Yorke Lee807afd82015-10-12 10:03:07 -07001028 fragment = ObjectFactory.newRegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001029 fragment.setOnTouchListener(new View.OnTouchListener() {
1030 @Override
1031 public boolean onTouch(View v, MotionEvent event) {
1032 // Show the FAB when the user touches the lists fragment and the soft
1033 // keyboard is hidden.
1034 showFabInSearchUi();
1035 return false;
1036 }
1037 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001038 }
Andrew Leeb1903842014-05-15 16:11:24 -07001039 transaction.add(R.id.dialtacts_frame, fragment, tag);
1040 } else {
1041 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001042 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001043 // DialtactsActivity will provide the options menu
1044 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001045 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001046 if (!smartDialSearch) {
1047 fragment.setQueryString(query, false /* delaySelection */);
1048 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001049 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001050
Yorke Lee6c450eb2015-05-08 09:45:47 -07001051 if (animate) {
1052 mListsFragment.getView().animate().alpha(0).withLayer();
1053 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001054 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001055 }
1056
1057 /**
Yorke Leec3766332013-07-31 11:13:16 -07001058 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001059 */
Yorke Leec3766332013-07-31 11:13:16 -07001060 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001061 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001062 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001063 return;
1064 }
Andrew Leeb1903842014-05-15 16:11:24 -07001065
Andrew Leeb1903842014-05-15 16:11:24 -07001066 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001067
1068 if (mDialpadFragment != null) {
1069 mDialpadFragment.clearDialpad();
1070 }
1071
Yorke Leeb207f8a2013-08-29 18:51:06 -07001072 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001073
Andrew Lee6324f702015-06-16 14:45:58 -07001074 // Restore the FAB for the lists fragment.
1075 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1076 mFloatingActionButtonController.setVisible(false);
1077 }
1078 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1079 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1080 onPageSelected(mListsFragment.getCurrentTabIndex());
1081
Andrew Leeb1903842014-05-15 16:11:24 -07001082 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001083 if (mSmartDialSearchFragment != null) {
1084 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001085 }
Andrew Leeb1903842014-05-15 16:11:24 -07001086 if (mRegularSearchFragment != null) {
1087 transaction.remove(mRegularSearchFragment);
1088 }
1089 transaction.commit();
1090
1091 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001092
1093 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001094 // If the dialpad fragment wasn't previously visible, then send a screen view because
1095 // we are exiting regular search. Otherwise, the screen view will be sent by
1096 // {@link #hideDialpadFragment}.
1097 mListsFragment.sendScreenViewForCurrentPosition();
1098 mListsFragment.setUserVisibleHint(true);
1099 }
1100
Yorke Lee5253be02014-05-21 18:50:03 -07001101 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001102 }
1103
Chiao Cheng94b10b52012-08-17 16:59:12 -07001104 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001105 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001106 if (mStateSaved) {
1107 return;
1108 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001109 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001110 if (TextUtils.isEmpty(mSearchQuery) ||
1111 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1112 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001113 exitSearchUi();
1114 }
Yorke Leef6673d32013-08-23 15:34:17 -07001115 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001116 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001117 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001118 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001119 } else {
1120 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001121 }
Yorke Leec3766332013-07-31 11:13:16 -07001122 }
1123
Yorke Lee81a313d2015-06-01 14:53:59 -07001124 private void maybeEnterSearchUi() {
1125 if (!isInSearchUi()) {
1126 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1127 }
1128 }
1129
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001130 /**
1131 * @return True if the search UI was exited, false otherwise
1132 */
1133 private boolean maybeExitSearchUi() {
1134 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1135 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001136 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001137 return true;
1138 }
1139 return false;
1140 }
1141
Andrew Lee6324f702015-06-16 14:45:58 -07001142 private void showFabInSearchUi() {
1143 mFloatingActionButtonController.changeIcon(
1144 getResources().getDrawable(R.drawable.fab_ic_dial),
1145 getResources().getString(R.string.action_menu_dialpad_button));
1146 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1147 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1148 }
1149
Yorke Leec3766332013-07-31 11:13:16 -07001150 @Override
1151 public void onDialpadQueryChanged(String query) {
Yorke Leed9a93162015-10-14 09:39:05 -07001152 mDialpadQuery = query;
Yorke Lee46635872014-04-11 11:20:15 -07001153 if (mSmartDialSearchFragment != null) {
1154 mSmartDialSearchFragment.setAddToContactNumber(query);
1155 }
Yorke Leec3766332013-07-31 11:13:16 -07001156 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1157 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001158
1159 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001160 if (DEBUG) {
1161 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1162 }
1163 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1164 // This callback can happen if the dialpad fragment is recreated because of
1165 // activity destruction. In that case, don't update the search view because
1166 // that would bring the user back to the search fragment regardless of the
1167 // previous state of the application. Instead, just return here and let the
1168 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001169 if (!TextUtils.isEmpty(normalizedQuery)) {
1170 mPendingSearchViewQuery = normalizedQuery;
1171 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001172 return;
1173 }
Yorke Lee53a22302014-04-29 18:13:46 -07001174 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001175 }
Santos Cordon83131712015-04-16 21:25:35 -07001176
1177 try {
1178 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1179 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1180 }
1181 } catch (Exception ignored) {
1182 // Skip any exceptions for this piece of code
1183 }
Yorke Lee575ae382015-07-16 11:36:07 -07001184 }
Santos Cordon83131712015-04-16 21:25:35 -07001185
Yorke Lee575ae382015-07-16 11:36:07 -07001186 @Override
1187 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1188 if (mInDialpadSearch && mSmartDialSearchFragment != null
1189 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1190 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1191 return true;
1192 }
1193 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001194 }
Yorke Leec3766332013-07-31 11:13:16 -07001195
1196 @Override
1197 public void onListFragmentScrollStateChange(int scrollState) {
1198 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001199 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001200 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001201 }
1202 }
1203
1204 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001205 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001206 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001207 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1208 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001209 }
1210
Yorke Leec3766332013-07-31 11:13:16 -07001211 private boolean phoneIsInUse() {
Yorke Lee827a90f2015-09-17 18:10:58 -07001212 return TelecomUtil.isInCall(this);
Yorke Leec3766332013-07-31 11:13:16 -07001213 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001214
Yorke Leeda0f9042013-12-05 14:25:51 -08001215 private boolean canIntentBeHandled(Intent intent) {
1216 final PackageManager packageManager = getPackageManager();
1217 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1218 PackageManager.MATCH_DEFAULT_ONLY);
1219 return resolveInfo != null && resolveInfo.size() > 0;
1220 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001221
Yorke Lee86e21f72014-04-02 16:49:38 -07001222 /**
1223 * Called when the user has long-pressed a contact tile to start a drag operation.
1224 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001225 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001226 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001227 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001228 }
1229
1230 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001231 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1232 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001233
Yorke Lee86e21f72014-04-02 16:49:38 -07001234 /**
1235 * Called when the user has released a contact tile after long-pressing it.
1236 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001237 @Override
1238 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001239 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001240 }
1241
1242 @Override
1243 public void onDroppedOnRemove() {}
1244
1245 /**
Yorke Leed999b712014-04-23 15:10:58 -07001246 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001247 * once it has been attached to the activity.
1248 */
1249 @Override
1250 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001251 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001252 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001253 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001254
Yorke Lee38019af2015-07-14 17:05:38 -07001255 /**
1256 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1257 */
1258 @Override
1259 public void showAllContactsTab() {
1260 if (mListsFragment != null) {
1261 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1262 }
1263 }
1264
1265 /**
1266 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1267 */
1268 @Override
1269 public void showDialpad() {
1270 showDialpadFragment(true);
1271 }
1272
Yorke Leee00c9fe2014-04-12 12:42:06 -07001273 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001274 public void onPickDataUri(Uri dataUri, int callInitiationType) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001275 mClearSearchOnPause = true;
Yorke Leec8d6e162015-09-14 18:43:27 -07001276 PhoneNumberInteraction.startInteractionForPhoneCall(
1277 DialtactsActivity.this, dataUri, callInitiationType);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001278 }
1279
1280 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001281 public void onPickPhoneNumber(String phoneNumber, boolean isVideoCall, int callInitiationType) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001282 if (phoneNumber == null) {
1283 // Invalid phone number, but let the call go through so that InCallUI can show
1284 // an error message.
1285 phoneNumber = "";
1286 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001287
1288 final Intent intent = new CallIntentBuilder(phoneNumber)
1289 .setIsVideoCall(isVideoCall)
Yorke Leec8d6e162015-09-14 18:43:27 -07001290 .setCallInitiationType(callInitiationType)
Yorke Leee3a2d132015-09-14 16:52:08 -07001291 .build();
1292
Yorke Lee7d20f822014-06-19 17:09:33 -07001293 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001294 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001295 }
1296
1297 @Override
1298 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001299 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001300 }
1301
1302 @Override
1303 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001304 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001305 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001306
Yorke Leecc4660d2014-04-24 11:22:57 -07001307 @Override
1308 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001309 int tabIndex = mListsFragment.getCurrentTabIndex();
1310
Andrew Lee4de59fb2015-08-13 15:20:08 -07001311 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1312 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1313 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001314 boolean isRtl = DialerUtils.isRtl();
1315 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1316 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001317 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001318 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1319 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001320 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001321 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001322 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001323
Andrew Lee432d4b52015-05-20 16:52:00 -07001324 @Override
1325 public void onPageSelected(int position) {
1326 int tabIndex = mListsFragment.getCurrentTabIndex();
1327 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001328 mFloatingActionButtonController.changeIcon(
1329 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1330 getResources().getString(R.string.search_shortcut_create_new_contact));
1331 } else {
1332 mFloatingActionButtonController.changeIcon(
1333 getResources().getDrawable(R.drawable.fab_ic_dial),
1334 getResources().getString(R.string.action_menu_dialpad_button));
1335 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001336 }
1337
1338 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001339 public void onPageScrollStateChanged(int state) {
1340 }
1341
Yorke Lee5253be02014-05-21 18:50:03 -07001342 @Override
1343 public boolean isActionBarShowing() {
1344 return mActionBarController.isActionBarShowing();
1345 }
1346
Yorke Leec98a5bb2014-10-28 16:12:05 -07001347 @Override
1348 public ActionBarController getActionBarController() {
1349 return mActionBarController;
1350 }
1351
Yorke Lee6bc67162015-06-27 14:03:25 -07001352 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001353 public boolean isDialpadShown() {
1354 return mIsDialpadShown;
1355 }
1356
Yorke Lee5253be02014-05-21 18:50:03 -07001357 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001358 public int getDialpadHeight() {
1359 if (mDialpadFragment != null) {
1360 return mDialpadFragment.getDialpadHeight();
1361 }
1362 return 0;
1363 }
1364
1365 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001366 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001367 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001368 }
1369
1370 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001371 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001372 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001373 }
1374
1375 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001376 public int getActionBarHeight() {
1377 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001378 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001379
Andrew Lee6324f702015-06-16 14:45:58 -07001380 private int getFabAlignment() {
1381 if (!mIsLandscape && !isInSearchUi() &&
1382 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1383 return FloatingActionButtonController.ALIGN_MIDDLE;
1384 }
1385 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001386 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001387}