blob: 0893b1c56891b165545e9531fb4007bcb2a78bc6 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
Yorke Leec3766332013-07-31 11:13:16 -07002 * Copyright (C) 2013 The Android Open Source Project
Chiao Cheng94b10b52012-08-17 16:59:12 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer;
18
Chiao Cheng94b10b52012-08-17 16:59:12 -070019import android.app.Fragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -070020import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080021import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.content.Context;
23import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
Sai Cheemalapati82d6da52014-06-03 13:54:23 -070026import android.content.res.Configuration;
Andrew Lee2423a2f2014-05-29 14:14:45 -070027import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.net.Uri;
29import android.os.Bundle;
Yorke Leef5554ce2015-01-27 14:46:02 -080030import android.os.Trace;
Nancy Chen019713e2015-07-06 18:25:24 -070031import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070032import android.speech.RecognizerIntent;
Nancy Chenf3d9f822015-09-11 00:47:31 -040033import android.support.design.widget.CoordinatorLayout;
Yorke Leecc4660d2014-04-24 11:22:57 -070034import android.support.v4.view.ViewPager;
Nancy Chen44898ad2015-08-13 12:03:47 -070035import android.support.v7.app.ActionBar;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070036import android.telecom.PhoneAccount;
Yorke Lee53a22302014-04-29 18:13:46 -070037import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070039import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070040import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070041import android.view.DragEvent;
Yorke Lee74edcdc2014-07-11 16:15:08 -070042import android.view.Gravity;
Andrew Leeb1903842014-05-15 16:11:24 -070043import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070046import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070048import android.view.View.OnDragListener;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070049import android.view.View.OnTouchListener;
Ihab Awad9134e252014-07-09 12:32:52 -070050import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070051import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070052import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070053import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070054import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070055import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070056import android.widget.PopupMenu;
Anne Rong35208002015-07-22 16:27:37 -070057import android.widget.TextView;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080058import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070059
Yorke Leec3766332013-07-31 11:13:16 -070060import com.android.contacts.common.dialog.ClearFrequentsDialog;
61import com.android.contacts.common.interactions.ImportExportDialogFragment;
Nancy Chen0d3a7392014-08-12 14:44:53 -070062import com.android.contacts.common.interactions.TouchPointManager;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080063import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec16ea5a2015-05-19 15:51:01 -070064import com.android.contacts.common.util.PermissionsUtil;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -070065import com.android.contacts.common.widget.FloatingActionButtonController;
Brian Attwellbeab3bb2014-10-27 12:24:49 -070066import com.android.contacts.commonbind.analytics.AnalyticsUtil;
Yorke Leec3766332013-07-31 11:13:16 -070067import com.android.dialer.calllog.CallLogActivity;
Yorke Lee38019af2015-07-14 17:05:38 -070068import com.android.dialer.calllog.CallLogFragment;
Yorke Leefce269a2013-08-12 11:56:04 -070069import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070070import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070072import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070073import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080074import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070075import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080076import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070077import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070078import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070079import com.android.dialer.list.RegularSearchFragment;
80import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070081import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070082import com.android.dialer.list.SpeedDialFragment;
Yorke Leef44e5172015-10-01 17:16:43 -070083import com.android.dialer.onboard.OnboardingActivity;
Yorke Leeaf6f1952014-07-14 19:13:16 -070084import com.android.dialer.settings.DialerSettingsActivity;
Andrew Lee247df6e2015-05-12 19:09:00 -070085import com.android.dialer.util.IntentUtil;
Yorke Lee827a90f2015-09-17 18:10:58 -070086import com.android.dialer.util.TelecomUtil;
Yorke Leee3a2d132015-09-14 16:52:08 -070087import com.android.dialer.util.IntentUtil.CallIntentBuilder;
Yorke Lee7d20f822014-06-19 17:09:33 -070088import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070089import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070090import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070091import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070092import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee807afd82015-10-12 10:03:07 -070093import com.android.dialerbind.ObjectFactory;
Yorke Leec8d6e162015-09-14 18:43:27 -070094import com.android.incallui.Call.LogState;
Yorke Lee2fec47b2014-08-05 18:16:27 -070095import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070096import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070097
Yorke Leed5c512a2015-01-30 14:42:53 -080098import junit.framework.Assert;
99
Yorke Leec3766332013-07-31 11:13:16 -0700100import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -0800101import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -0700102
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700104 * The dialer tab's title is 'phone', a more common name (see strings.xml).
105 */
Yorke Leec3766332013-07-31 11:13:16 -0700106public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700107 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700108 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700109 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700110 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700111 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700112 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700113 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700114 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700115 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700116 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700117 ViewPager.OnPageChangeListener,
118 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119 private static final String TAG = "DialtactsActivity";
120
Yorke Lee7cc61492015-06-01 14:59:33 -0700121 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700122
Yorke Leef74011e2013-08-02 11:30:30 -0700123 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
124
Yorke Leeb207f8a2013-08-29 18:51:06 -0700125 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
126 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700127 private static final String KEY_SEARCH_QUERY = "search_query";
128 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700129 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700130
Yorke Leec3766332013-07-31 11:13:16 -0700131 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
132 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
133 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
134 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700135
Chiao Cheng94b10b52012-08-17 16:59:12 -0700136 /**
137 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
138 */
139 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700140 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700141
Yorke Leec3766332013-07-31 11:13:16 -0700142 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700143
Andrew Lee6324f702015-06-16 14:45:58 -0700144 private static final int FAB_SCALE_IN_DELAY_MS = 300;
145
Nancy Chenf3d9f822015-09-11 00:47:31 -0400146 private CoordinatorLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700147
Yorke Leec3766332013-07-31 11:13:16 -0700148 /**
Yorke Leec3766332013-07-31 11:13:16 -0700149 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700150 */
Evan Charlton72d66252014-11-07 16:23:47 -0800151 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700152
153 /**
154 * Fragment for searching phone numbers using the alphanumeric keyboard.
155 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700156 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700157
158 /**
159 * Fragment for searching phone numbers using the dialpad.
160 */
161 private SmartDialSearchFragment mSmartDialSearchFragment;
162
Yorke Leee00c9fe2014-04-12 12:42:06 -0700163 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700164 * Animation that slides in.
165 */
166 private Animation mSlideIn;
167
168 /**
169 * Animation that slides out.
170 */
171 private Animation mSlideOut;
172
Yorke Lee6c450eb2015-05-08 09:45:47 -0700173 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
174 @Override
175 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700176 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700177 }
178 };
179
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700180 /**
181 * Listener for after slide out animation completes on dialer fragment.
182 */
183 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
184 @Override
185 public void onAnimationEnd(Animation animation) {
186 commitDialpadFragmentHide();
187 }
188 };
189
190 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700191 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700192 */
193 private ListsFragment mListsFragment;
194
Yorke Lee19e68ca2014-10-28 11:51:40 -0700195 /**
196 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
197 * be commited.
198 */
199 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800200 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700201 private boolean mInDialpadSearch;
202 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700203 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700204 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700205 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700206
Yorke Leeef2b7382013-08-09 17:39:25 -0700207 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700208 * Whether or not the device is in landscape orientation.
209 */
210 private boolean mIsLandscape;
211
212 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700213 * True if the dialpad is only temporarily showing due to being in call
214 */
215 private boolean mInCallDialpadUp;
216
217 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700218 * True when this activity has been launched for the first time.
219 */
Yorke Lee98702de2013-08-06 12:03:32 -0700220 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700221
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700222 /**
223 * Search query to be applied to the SearchView in the ActionBar once
224 * onCreateOptionsMenu has been called.
225 */
226 private String mPendingSearchViewQuery;
227
Yorke Lee74edcdc2014-07-11 16:15:08 -0700228 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700229 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700230 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700231
Yorke Leeef2b7382013-08-09 17:39:25 -0700232 private String mSearchQuery;
Yorke Leed9a93162015-10-14 09:39:05 -0700233 private String mDialpadQuery;
Yorke Leeef2b7382013-08-09 17:39:25 -0700234
Yorke Leefce269a2013-08-12 11:56:04 -0700235 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700236 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700237 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700238
Sai Cheemalapati41460652014-06-02 21:05:39 -0700239 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700240
Andrew Lee2423a2f2014-05-29 14:14:45 -0700241 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700242
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800243 /**
244 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
245 * {@link #onResume()} to populate the search box.
246 */
247 private String mVoiceSearchQuery;
248
Andrew Lee467de3d2015-03-17 15:47:24 -0700249 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700250 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700251 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700252 }
253
254 @Override
255 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700256 final boolean hasContactsPermission =
257 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700258 final Menu menu = getMenu();
259 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700260 clearFrequents.setVisible(mListsFragment != null &&
261 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700262 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
263
264 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
265 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700266
267 menu.findItem(R.id.menu_history).setVisible(
268 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700269 super.show();
270 }
271 }
272
Chiao Cheng94b10b52012-08-17 16:59:12 -0700273 /**
Yorke Lee28266192014-05-01 10:05:52 -0700274 * Listener that listens to drag events and sends their x and y coordinates to a
275 * {@link DragDropController}.
276 */
277 private class LayoutOnDragListener implements OnDragListener {
278 @Override
279 public boolean onDrag(View v, DragEvent event) {
280 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700281 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700282 }
283 return true;
284 }
285 }
286
287 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700288 * Listener used to send search queries to the phone search fragment.
289 */
Yorke Lee53a22302014-04-29 18:13:46 -0700290 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700291 @Override
292 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
293 }
Yorke Leec3766332013-07-31 11:13:16 -0700294
Andrew Lee99a570c2014-05-15 14:18:50 -0700295 @Override
296 public void onTextChanged(CharSequence s, int start, int before, int count) {
297 final String newText = s.toString();
298 if (newText.equals(mSearchQuery)) {
299 // If the query hasn't changed (perhaps due to activity being destroyed
300 // and restored, or user launching the same DIAL intent twice), then there is
301 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700302 return;
303 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700304 if (DEBUG) {
305 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700306 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700307 }
Yorke Lee710709d2014-05-29 07:18:42 -0700308 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700309
Andrew Lee90374a72014-05-16 15:01:09 -0700310 // Show search fragment only when the query string is changed to non-empty text.
311 if (!TextUtils.isEmpty(newText)) {
312 // Call enterSearchUi only if we are switching search modes, or showing a search
313 // fragment for the first time.
314 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
315 (!mIsDialpadShown && mInRegularSearch);
316 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700317 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700318 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700319 }
320
Andrew Leea8515422014-06-13 16:53:54 -0700321 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700322 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700323 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700324 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700325 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700326 }
327
328 @Override
329 public void afterTextChanged(Editable s) {
330 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700331 };
332
Andrew Leeb1903842014-05-15 16:11:24 -0700333
334 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700335 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700336 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700337 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700338 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700339 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700340 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700341 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700342 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
343 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700344 }
Andrew Leeb1903842014-05-15 16:11:24 -0700345 }
346 };
347
348 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700349 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700350 */
351 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
352 @Override
353 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700354 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
355 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
356 // If the search term is empty, close the search UI.
357 maybeExitSearchUi();
358 } else {
359 // If the search term is not empty, show the dialpad fab.
360 showFabInSearchUi();
361 }
Andrew Leeb1903842014-05-15 16:11:24 -0700362 }
363 return false;
364 }
365 };
366
Chiao Cheng94b10b52012-08-17 16:59:12 -0700367 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700368 public boolean dispatchTouchEvent(MotionEvent ev) {
369 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
370 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
371 }
372 return super.dispatchTouchEvent(ev);
Nancy Chen49db1ad2014-08-18 20:10:17 -0700373 }
374
375 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700376 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800377 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700378 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700379
Yorke Leef44e5172015-10-01 17:16:43 -0700380 if (OnboardingActivity.shouldStartOnboardingActivity(this)) {
381 OnboardingActivity.startOnboardingActivity(this);
382 finish();
383 }
384
Yorke Lee98702de2013-08-06 12:03:32 -0700385 mFirstLaunch = true;
386
Andrew Lee2423a2f2014-05-29 14:14:45 -0700387 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700388 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700389
Yorke Leef5554ce2015-01-27 14:46:02 -0800390 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700391 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800392 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800393 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700394
Yorke Leef5554ce2015-01-27 14:46:02 -0800395 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700396 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700397 actionBar.setCustomView(R.layout.search_edittext);
398 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700399 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700400
Anne Rong35208002015-07-22 16:27:37 -0700401 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
402 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700403 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
404
Andrew Leee3f59a82015-02-26 12:06:35 -0800405 mActionBarController = new ActionBarController(this, searchEditTextLayout);
406
Andrew Lee04675a52014-06-05 18:36:20 -0700407 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700408 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700409 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
410 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
411 .setOnClickListener(mSearchViewOnClickListener);
412 searchEditTextLayout.findViewById(R.id.search_box_start_search)
413 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700414 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700415 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700416 @Override
417 public void onBackButtonClicked() {
418 onBackPressed();
419 }
Andrew Lee6324f702015-06-16 14:45:58 -0700420
421 @Override
422 public void onSearchViewClicked() {
423 // Hide FAB, as the keyboard is shown.
424 mFloatingActionButtonController.scaleOut();
425 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700426 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700427
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700428 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700429 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700430
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700431 final View floatingActionButtonContainer = findViewById(
432 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700433 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
434 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700435 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700436 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700437
Andrew Lee04675a52014-06-05 18:36:20 -0700438 ImageButton optionsMenuButton =
439 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700440 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700441 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
442 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700443
Yorke Leed5c512a2015-01-30 14:42:53 -0800444 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
445 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700446 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800447 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000448 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800449 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700450 } else {
451 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700452 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
453 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700454 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700455 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700456 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700457 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700458
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700459 final boolean isLayoutRtl = DialerUtils.isRtl();
460 if (mIsLandscape) {
461 mSlideIn = AnimationUtils.loadAnimation(this,
462 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
463 mSlideOut = AnimationUtils.loadAnimation(this,
464 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
465 } else {
466 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
467 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
468 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700469
Yorke Lee2fec47b2014-08-05 18:16:27 -0700470 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
471 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
472
Yorke Lee6c450eb2015-05-08 09:45:47 -0700473 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700474 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700475
Nancy Chenf3d9f822015-09-11 00:47:31 -0400476 mParentLayout = (CoordinatorLayout) findViewById(R.id.dialtacts_mainlayout);
Yorke Leef614f6f2014-10-03 10:44:19 -0700477 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700478 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700479 new ViewTreeObserver.OnGlobalLayoutListener() {
480 @Override
481 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700482 final ViewTreeObserver observer =
483 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700484 if (!observer.isAlive()) {
485 return;
486 }
487 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700488 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700489 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700490 mFloatingActionButtonController.align(
491 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700492 }
493 });
Andrew Lee0c667702014-05-12 14:31:45 -0700494
Yorke Leef5554ce2015-01-27 14:46:02 -0800495 Trace.endSection();
496
497 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700498 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700499 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800500 Trace.endSection();
501 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700502 }
503
504 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700505 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800506 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700507 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700508
Yorke Lee19e68ca2014-10-28 11:51:40 -0700509 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700510 if (mFirstLaunch) {
511 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700512 } else if (!phoneIsInUse() && mInCallDialpadUp) {
513 hideDialpadFragment(false, true);
514 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700515 } else if (mShowDialpadOnResume) {
516 showDialpadFragment(false);
517 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700518 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800519
520 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
521 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
522 // shown until onResume has completed. Active the search UI and set the search term now.
523 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
524 mActionBarController.onSearchBoxTapped();
525 mSearchView.setText(mVoiceSearchQuery);
526 mVoiceSearchQuery = null;
527 }
528
Yorke Lee98702de2013-08-06 12:03:32 -0700529 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800530
531 if (mIsRestarting) {
532 // This is only called when the activity goes from resumed -> paused -> resumed, so it
533 // will not cause an extra view to be sent out on rotation
534 if (mIsDialpadShown) {
535 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
536 }
537 mIsRestarting = false;
538 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700539
Yorke Lee53a22302014-04-29 18:13:46 -0700540 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700541 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700542 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700543
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700544 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
545 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
546 // used internally.
547 final Bundle extras = getIntent().getExtras();
548 if (extras != null
549 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
550 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
551 } else {
552 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
553 }
554 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700555 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
556 if (index < mListsFragment.getTabCount()) {
557 mListsFragment.showTab(index);
558 }
559 }
560
Anne Rong35208002015-07-22 16:27:37 -0700561 setSearchBoxHint();
562
Yorke Leef5554ce2015-01-27 14:46:02 -0800563 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700564 }
565
566 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800567 protected void onRestart() {
568 super.onRestart();
569 mIsRestarting = true;
570 }
571
572 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700573 protected void onPause() {
574 if (mClearSearchOnPause) {
575 hideDialpadAndSearchUi();
576 mClearSearchOnPause = false;
577 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700578 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
579 commitDialpadFragmentHide();
580 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700581 super.onPause();
582 }
583
584 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700585 protected void onSaveInstanceState(Bundle outState) {
586 super.onSaveInstanceState(outState);
587 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700588 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
589 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700590 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700591 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700592 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700593 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700594 }
595
596 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700597 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700598 if (fragment instanceof DialpadFragment) {
599 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800600 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700601 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
602 transaction.hide(mDialpadFragment);
603 transaction.commit();
604 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700605 } else if (fragment instanceof SmartDialSearchFragment) {
606 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700607 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leed9a93162015-10-14 09:39:05 -0700608 if (!TextUtils.isEmpty(mDialpadQuery)) {
609 mSmartDialSearchFragment.setAddToContactNumber(mDialpadQuery);
610 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700611 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700612 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700613 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700614 } else if (fragment instanceof ListsFragment) {
615 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700616 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700617 }
Yorke Leec3766332013-07-31 11:13:16 -0700618 }
619
Alon Albertb453e5b2013-09-10 15:54:23 -0700620 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700621 final Intent intent = new Intent(this, DialerSettingsActivity.class);
622 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700623 }
624
Chiao Cheng94b10b52012-08-17 16:59:12 -0700625 @Override
626 public void onClick(View view) {
627 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700628 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700629 if (mListsFragment.getCurrentTabIndex()
630 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700631 DialerUtils.startActivityWithErrorToast(
632 this,
633 IntentUtil.getNewContactIntent(),
634 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700635 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700636 mInCallDialpadUp = false;
637 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700638 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700639 break;
Yorke Leec3766332013-07-31 11:13:16 -0700640 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800641 try {
642 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
643 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
644 } catch (ActivityNotFoundException e) {
645 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
646 Toast.LENGTH_SHORT).show();
647 }
Yorke Leec3766332013-07-31 11:13:16 -0700648 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700649 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700650 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700651 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700652 default: {
653 Log.wtf(TAG, "Unexpected onClick event from " + view);
654 break;
655 }
656 }
657 }
658
Yorke Leec3766332013-07-31 11:13:16 -0700659 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700660 public boolean onMenuItemClick(MenuItem item) {
Jay Shrauner193cfc52015-09-25 14:13:16 -0700661 if (!isSafeToCommitTransactions()) {
Jay Shrauner119b0652015-09-17 12:00:18 -0700662 return true;
663 }
664
Yorke Lee86e21f72014-04-02 16:49:38 -0700665 switch (item.getItemId()) {
666 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700667 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
668 // CONTENT_TYPE, so that we always open our call log from our dialer
669 final Intent intent = new Intent(this, CallLogActivity.class);
670 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700671 break;
672 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700673 DialerUtils.startActivityWithErrorToast(
674 this,
675 IntentUtil.getNewContactIntent(),
676 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700677 break;
678 case R.id.menu_import_export:
679 // We hard-code the "contactsAreAvailable" argument because doing it properly would
680 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
681 // now in Dialtacts for (potential) performance reasons. Compare with how it is
682 // done in {@link PeopleActivity}.
Wenyi Wang5953e8c2015-09-24 15:38:14 -0700683 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
684 ImportExportDialogFragment.show(getFragmentManager(), true,
685 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_FAVORITES);
686 } else {
687 ImportExportDialogFragment.show(getFragmentManager(), true,
688 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_DEFAULT);
689 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700690 return true;
691 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700692 ClearFrequentsDialog.show(getFragmentManager());
693 return true;
694 case R.id.menu_call_settings:
695 handleMenuSettings();
696 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700697 }
698 return false;
699 }
700
701 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700702 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
703 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
704 if (resultCode == RESULT_OK) {
705 final ArrayList<String> matches = data.getStringArrayListExtra(
706 RecognizerIntent.EXTRA_RESULTS);
707 if (matches.size() > 0) {
708 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800709 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700710 } else {
711 Log.e(TAG, "Voice search - nothing heard");
712 }
713 } else {
714 Log.e(TAG, "Voice search failed");
715 }
716 }
717 super.onActivityResult(requestCode, resultCode, data);
718 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719
Andrew Lee2a58ab82014-05-15 02:16:04 -0700720 /**
Nancy Chenea7c0b82015-08-31 17:18:41 -0700721 * Update the number of unread voicemails (potentially other tabs) displayed next to the tab
722 * icon.
723 */
724 public void updateTabUnreadCounts() {
725 mListsFragment.updateTabUnreadCounts();
726 }
727
728 /**
Andrew Lee2a58ab82014-05-15 02:16:04 -0700729 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
730 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
731 * @see #onDialpadShown
732 */
Yorke Leec3766332013-07-31 11:13:16 -0700733 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700734 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700735 return;
736 }
737 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800738
Yorke Lee7ee5c422014-11-04 10:29:32 -0800739 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700740
Chiao Cheng94b10b52012-08-17 16:59:12 -0700741 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800742 if (mDialpadFragment == null) {
743 mDialpadFragment = new DialpadFragment();
744 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
745 } else {
746 ft.show(mDialpadFragment);
747 }
748
749 mDialpadFragment.setAnimate(animate);
750 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700751 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700752
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700753 if (animate) {
754 mFloatingActionButtonController.scaleOut();
755 } else {
756 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700757 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700758 }
Yorke Lee5253be02014-05-21 18:50:03 -0700759 mActionBarController.onDialpadUp();
760
Yorke Lee6c450eb2015-05-08 09:45:47 -0700761 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700762 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700763
Andrew Lee2a58ab82014-05-15 02:16:04 -0700764 /**
765 * Callback from child DialpadFragment when the dialpad is shown.
766 */
767 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800768 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700769 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700770 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700771 } else {
772 mDialpadFragment.setYFraction(0);
773 }
774
Andrew Lee2a58ab82014-05-15 02:16:04 -0700775 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700776 }
777
778 /**
779 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
780 * a callback after the hide animation ends.
781 * @see #commitDialpadFragmentHide
782 */
Yorke Leeca195042013-09-25 16:29:38 -0700783 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700784 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700785 return;
786 }
Yorke Leef6673d32013-08-23 15:34:17 -0700787 if (clearDialpad) {
788 mDialpadFragment.clearDialpad();
789 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700790 if (!mIsDialpadShown) {
791 return;
Yorke Leec3766332013-07-31 11:13:16 -0700792 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700793 mIsDialpadShown = false;
794 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800795 mListsFragment.setUserVisibleHint(true);
796 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700797
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700798 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700799
Andrew Lee6324f702015-06-16 14:45:58 -0700800 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700801 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700802 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700803 } else {
804 commitDialpadFragmentHide();
805 }
806
Yorke Lee5253be02014-05-21 18:50:03 -0700807 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700808
Andrew Leed4cde832014-05-16 15:56:48 -0700809 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700810 if (TextUtils.isEmpty(mSearchQuery)) {
811 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700812 }
Andrew Leed4cde832014-05-16 15:56:48 -0700813 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700814 }
815
816 /**
817 * Finishes hiding the dialpad fragment after any animations are completed.
818 */
819 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800820 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700821 final FragmentTransaction ft = getFragmentManager().beginTransaction();
822 ft.hide(mDialpadFragment);
823 ft.commit();
824 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700825 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700826 }
827
Andrew Lee2a58ab82014-05-15 02:16:04 -0700828 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700829 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700830 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700831 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700832 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700833 fragment = mRegularSearchFragment;
834 }
835 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700836 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700837 }
838 }
839
Yorke Lee5253be02014-05-21 18:50:03 -0700840 @Override
841 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700842 return mInDialpadSearch || mInRegularSearch;
843 }
844
Yorke Lee5253be02014-05-21 18:50:03 -0700845 @Override
846 public boolean hasSearchQuery() {
847 return !TextUtils.isEmpty(mSearchQuery);
848 }
849
850 @Override
851 public boolean shouldShowActionBar() {
852 return mListsFragment.shouldShowActionBar();
853 }
854
Yorke Leeb207f8a2013-08-29 18:51:06 -0700855 private void setNotInSearchUi() {
856 mInDialpadSearch = false;
857 mInRegularSearch = false;
858 }
859
Yorke Lee311969c2013-08-26 06:31:11 -0700860 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700861 if (mIsDialpadShown) {
862 hideDialpadFragment(false, true);
863 } else {
864 exitSearchUi();
865 }
Yorke Lee311969c2013-08-26 06:31:11 -0700866 }
867
Yorke Lee53a22302014-04-29 18:13:46 -0700868 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700869 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
870 if (canIntentBeHandled(voiceIntent)) {
871 mVoiceSearchButton.setVisibility(View.VISIBLE);
872 mVoiceSearchButton.setOnClickListener(this);
873 } else {
874 mVoiceSearchButton.setVisibility(View.GONE);
875 }
876 }
877
Yorke Leee9c82332015-10-06 14:21:53 -0700878 public boolean isNearbyPlacesSearchEnabled() {
879 return false;
880 }
881
Anne Rong35208002015-07-22 16:27:37 -0700882 protected int getSearchBoxHint () {
883 return R.string.dialer_hint_find_contact;
884 }
885
886 /**
887 * Sets the hint text for the contacts search box
888 */
889 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700890 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700891 .getCustomView().findViewById(R.id.search_view_container);
892 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
893 .setHint(getSearchBoxHint());
894 }
895
Andrew Lee467de3d2015-03-17 15:47:24 -0700896 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700897 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
898 popupMenu.inflate(R.menu.dialtacts_options);
899 popupMenu.setOnMenuItemClickListener(this);
900 return popupMenu;
901 }
902
Yorke Lee86e21f72014-04-02 16:49:38 -0700903 @Override
904 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700905 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700906 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700907 mPendingSearchViewQuery = null;
908 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700909 if (mActionBarController != null) {
910 mActionBarController.restoreActionBarOffset();
911 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700912 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700913 }
914
Chiao Cheng94b10b52012-08-17 16:59:12 -0700915 /**
916 * Returns true if the intent is due to hitting the green send key (hardware call button:
917 * KEYCODE_CALL) while in a call.
918 *
919 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700920 * @return true if the intent is due to hitting the green send key while in a call
921 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700922 private boolean isSendKeyWhileInCall(Intent intent) {
923 // If there is a call in progress and the user launched the dialer by hitting the call
924 // button, go straight to the in-call screen.
925 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700926
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700927 if (callKey) {
Yorke Lee762b3572015-09-18 12:54:59 -0700928 TelecomUtil.showInCallScreen(this, false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700929 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700930 }
931
932 return false;
933 }
934
935 /**
936 * Sets the current tab based on the intent's request type
937 *
938 * @param intent Intent that contains information about which tab should be selected
939 */
Yorke Leec3766332013-07-31 11:13:16 -0700940 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700941 // 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 -0700942 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700943 finish();
944 return;
945 }
946
Yorke Leed77017d2015-08-31 16:19:43 -0700947 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
948 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800949 showDialpadFragment(false);
950 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700951 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800952 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700953 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700954 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700955 }
956
957 @Override
958 public void onNewIntent(Intent newIntent) {
959 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700960
Nancy Chen199b7f02014-11-05 15:03:00 -0800961 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700962 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700963
Chiao Cheng94b10b52012-08-17 16:59:12 -0700964 invalidateOptionsMenu();
965 }
966
967 /** Returns true if the given intent contains a phone number to populate the dialer with */
968 private boolean isDialIntent(Intent intent) {
969 final String action = intent.getAction();
970 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
971 return true;
972 }
973 if (Intent.ACTION_VIEW.equals(action)) {
974 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700975 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700976 return true;
977 }
978 }
979 return false;
980 }
981
982 /**
Yorke Leec3766332013-07-31 11:13:16 -0700983 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700984 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700985 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700986 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700987 // Weird race condition where fragment is doing work after the activity is destroyed
988 // due to talkback being on (b/10209937). Just return since we can't do any
989 // constructive here.
990 return;
991 }
992
Yorke Leeef2b7382013-08-09 17:39:25 -0700993 if (DEBUG) {
994 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
995 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700996
Yorke Leec3766332013-07-31 11:13:16 -0700997 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700998 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700999 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001000 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001001 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -07001002 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001003
Yorke Leeb207f8a2013-08-29 18:51:06 -07001004 final String tag;
1005 if (smartDialSearch) {
1006 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
1007 } else {
1008 tag = TAG_REGULAR_SEARCH_FRAGMENT;
1009 }
1010 mInDialpadSearch = smartDialSearch;
1011 mInRegularSearch = !smartDialSearch;
1012
Andrew Lee6324f702015-06-16 14:45:58 -07001013 mFloatingActionButtonController.scaleOut();
1014
Andrew Lee752956e2014-05-15 11:43:38 -07001015 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001016 if (animate) {
1017 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
1018 } else {
1019 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
1020 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001021 if (fragment == null) {
1022 if (smartDialSearch) {
1023 fragment = new SmartDialSearchFragment();
1024 } else {
Yorke Lee807afd82015-10-12 10:03:07 -07001025 fragment = ObjectFactory.newRegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001026 fragment.setOnTouchListener(new View.OnTouchListener() {
1027 @Override
1028 public boolean onTouch(View v, MotionEvent event) {
1029 // Show the FAB when the user touches the lists fragment and the soft
1030 // keyboard is hidden.
1031 showFabInSearchUi();
1032 return false;
1033 }
1034 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001035 }
Andrew Leeb1903842014-05-15 16:11:24 -07001036 transaction.add(R.id.dialtacts_frame, fragment, tag);
1037 } else {
1038 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001039 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001040 // DialtactsActivity will provide the options menu
1041 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001042 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001043 if (!smartDialSearch) {
1044 fragment.setQueryString(query, false /* delaySelection */);
1045 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001046 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001047
Yorke Lee6c450eb2015-05-08 09:45:47 -07001048 if (animate) {
1049 mListsFragment.getView().animate().alpha(0).withLayer();
1050 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001051 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001052 }
1053
1054 /**
Yorke Leec3766332013-07-31 11:13:16 -07001055 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001056 */
Yorke Leec3766332013-07-31 11:13:16 -07001057 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001058 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001059 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001060 return;
1061 }
Andrew Leeb1903842014-05-15 16:11:24 -07001062
Andrew Leeb1903842014-05-15 16:11:24 -07001063 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001064
1065 if (mDialpadFragment != null) {
1066 mDialpadFragment.clearDialpad();
1067 }
1068
Yorke Leeb207f8a2013-08-29 18:51:06 -07001069 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001070
Andrew Lee6324f702015-06-16 14:45:58 -07001071 // Restore the FAB for the lists fragment.
1072 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1073 mFloatingActionButtonController.setVisible(false);
1074 }
1075 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1076 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1077 onPageSelected(mListsFragment.getCurrentTabIndex());
1078
Andrew Leeb1903842014-05-15 16:11:24 -07001079 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001080 if (mSmartDialSearchFragment != null) {
1081 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001082 }
Andrew Leeb1903842014-05-15 16:11:24 -07001083 if (mRegularSearchFragment != null) {
1084 transaction.remove(mRegularSearchFragment);
1085 }
1086 transaction.commit();
1087
1088 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001089
1090 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001091 // If the dialpad fragment wasn't previously visible, then send a screen view because
1092 // we are exiting regular search. Otherwise, the screen view will be sent by
1093 // {@link #hideDialpadFragment}.
1094 mListsFragment.sendScreenViewForCurrentPosition();
1095 mListsFragment.setUserVisibleHint(true);
1096 }
1097
Yorke Lee5253be02014-05-21 18:50:03 -07001098 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001099 }
1100
Chiao Cheng94b10b52012-08-17 16:59:12 -07001101 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001102 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001103 if (mStateSaved) {
1104 return;
1105 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001106 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001107 if (TextUtils.isEmpty(mSearchQuery) ||
1108 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1109 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001110 exitSearchUi();
1111 }
Yorke Leef6673d32013-08-23 15:34:17 -07001112 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001113 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001114 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001115 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001116 } else {
1117 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001118 }
Yorke Leec3766332013-07-31 11:13:16 -07001119 }
1120
Yorke Lee81a313d2015-06-01 14:53:59 -07001121 private void maybeEnterSearchUi() {
1122 if (!isInSearchUi()) {
1123 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1124 }
1125 }
1126
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001127 /**
1128 * @return True if the search UI was exited, false otherwise
1129 */
1130 private boolean maybeExitSearchUi() {
1131 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1132 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001133 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001134 return true;
1135 }
1136 return false;
1137 }
1138
Andrew Lee6324f702015-06-16 14:45:58 -07001139 private void showFabInSearchUi() {
1140 mFloatingActionButtonController.changeIcon(
1141 getResources().getDrawable(R.drawable.fab_ic_dial),
1142 getResources().getString(R.string.action_menu_dialpad_button));
1143 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1144 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1145 }
1146
Yorke Leec3766332013-07-31 11:13:16 -07001147 @Override
1148 public void onDialpadQueryChanged(String query) {
Yorke Leed9a93162015-10-14 09:39:05 -07001149 mDialpadQuery = query;
Yorke Lee46635872014-04-11 11:20:15 -07001150 if (mSmartDialSearchFragment != null) {
1151 mSmartDialSearchFragment.setAddToContactNumber(query);
1152 }
Yorke Leec3766332013-07-31 11:13:16 -07001153 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1154 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001155
1156 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001157 if (DEBUG) {
1158 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1159 }
1160 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1161 // This callback can happen if the dialpad fragment is recreated because of
1162 // activity destruction. In that case, don't update the search view because
1163 // that would bring the user back to the search fragment regardless of the
1164 // previous state of the application. Instead, just return here and let the
1165 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001166 if (!TextUtils.isEmpty(normalizedQuery)) {
1167 mPendingSearchViewQuery = normalizedQuery;
1168 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001169 return;
1170 }
Yorke Lee53a22302014-04-29 18:13:46 -07001171 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001172 }
Santos Cordon83131712015-04-16 21:25:35 -07001173
1174 try {
1175 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1176 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1177 }
1178 } catch (Exception ignored) {
1179 // Skip any exceptions for this piece of code
1180 }
Yorke Lee575ae382015-07-16 11:36:07 -07001181 }
Santos Cordon83131712015-04-16 21:25:35 -07001182
Yorke Lee575ae382015-07-16 11:36:07 -07001183 @Override
1184 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1185 if (mInDialpadSearch && mSmartDialSearchFragment != null
1186 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1187 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1188 return true;
1189 }
1190 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001191 }
Yorke Leec3766332013-07-31 11:13:16 -07001192
1193 @Override
1194 public void onListFragmentScrollStateChange(int scrollState) {
1195 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001196 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001197 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001198 }
1199 }
1200
1201 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001202 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001203 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001204 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1205 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001206 }
1207
Yorke Leec3766332013-07-31 11:13:16 -07001208 private boolean phoneIsInUse() {
Yorke Lee827a90f2015-09-17 18:10:58 -07001209 return TelecomUtil.isInCall(this);
Yorke Leec3766332013-07-31 11:13:16 -07001210 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001211
Yorke Leeda0f9042013-12-05 14:25:51 -08001212 private boolean canIntentBeHandled(Intent intent) {
1213 final PackageManager packageManager = getPackageManager();
1214 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1215 PackageManager.MATCH_DEFAULT_ONLY);
1216 return resolveInfo != null && resolveInfo.size() > 0;
1217 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001218
Yorke Lee86e21f72014-04-02 16:49:38 -07001219 /**
1220 * Called when the user has long-pressed a contact tile to start a drag operation.
1221 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001222 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001223 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001224 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001225 }
1226
1227 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001228 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1229 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001230
Yorke Lee86e21f72014-04-02 16:49:38 -07001231 /**
1232 * Called when the user has released a contact tile after long-pressing it.
1233 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001234 @Override
1235 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001236 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001237 }
1238
1239 @Override
1240 public void onDroppedOnRemove() {}
1241
1242 /**
Yorke Leed999b712014-04-23 15:10:58 -07001243 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001244 * once it has been attached to the activity.
1245 */
1246 @Override
1247 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001248 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001249 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001250 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001251
Yorke Lee38019af2015-07-14 17:05:38 -07001252 /**
1253 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1254 */
1255 @Override
1256 public void showAllContactsTab() {
1257 if (mListsFragment != null) {
1258 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1259 }
1260 }
1261
1262 /**
1263 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1264 */
1265 @Override
1266 public void showDialpad() {
1267 showDialpadFragment(true);
1268 }
1269
Yorke Leee00c9fe2014-04-12 12:42:06 -07001270 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001271 public void onPickDataUri(Uri dataUri, int callInitiationType) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001272 mClearSearchOnPause = true;
Yorke Leec8d6e162015-09-14 18:43:27 -07001273 PhoneNumberInteraction.startInteractionForPhoneCall(
1274 DialtactsActivity.this, dataUri, callInitiationType);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001275 }
1276
1277 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001278 public void onPickPhoneNumber(String phoneNumber, boolean isVideoCall, int callInitiationType) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001279 if (phoneNumber == null) {
1280 // Invalid phone number, but let the call go through so that InCallUI can show
1281 // an error message.
1282 phoneNumber = "";
1283 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001284
1285 final Intent intent = new CallIntentBuilder(phoneNumber)
1286 .setIsVideoCall(isVideoCall)
Yorke Leec8d6e162015-09-14 18:43:27 -07001287 .setCallInitiationType(callInitiationType)
Yorke Leee3a2d132015-09-14 16:52:08 -07001288 .build();
1289
Yorke Lee7d20f822014-06-19 17:09:33 -07001290 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001291 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001292 }
1293
1294 @Override
1295 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001296 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001297 }
1298
1299 @Override
1300 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001301 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001302 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001303
Yorke Leecc4660d2014-04-24 11:22:57 -07001304 @Override
1305 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001306 int tabIndex = mListsFragment.getCurrentTabIndex();
1307
Andrew Lee4de59fb2015-08-13 15:20:08 -07001308 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1309 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1310 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001311 boolean isRtl = DialerUtils.isRtl();
1312 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1313 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001314 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001315 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1316 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001317 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001318 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001319 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001320
Andrew Lee432d4b52015-05-20 16:52:00 -07001321 @Override
1322 public void onPageSelected(int position) {
1323 int tabIndex = mListsFragment.getCurrentTabIndex();
1324 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001325 mFloatingActionButtonController.changeIcon(
1326 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1327 getResources().getString(R.string.search_shortcut_create_new_contact));
1328 } else {
1329 mFloatingActionButtonController.changeIcon(
1330 getResources().getDrawable(R.drawable.fab_ic_dial),
1331 getResources().getString(R.string.action_menu_dialpad_button));
1332 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001333 }
1334
1335 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001336 public void onPageScrollStateChanged(int state) {
1337 }
1338
Yorke Lee5253be02014-05-21 18:50:03 -07001339 @Override
1340 public boolean isActionBarShowing() {
1341 return mActionBarController.isActionBarShowing();
1342 }
1343
Yorke Leec98a5bb2014-10-28 16:12:05 -07001344 @Override
1345 public ActionBarController getActionBarController() {
1346 return mActionBarController;
1347 }
1348
Yorke Lee6bc67162015-06-27 14:03:25 -07001349 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001350 public boolean isDialpadShown() {
1351 return mIsDialpadShown;
1352 }
1353
Yorke Lee5253be02014-05-21 18:50:03 -07001354 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001355 public int getDialpadHeight() {
1356 if (mDialpadFragment != null) {
1357 return mDialpadFragment.getDialpadHeight();
1358 }
1359 return 0;
1360 }
1361
1362 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001363 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001364 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001365 }
1366
1367 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001368 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001369 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001370 }
1371
1372 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001373 public int getActionBarHeight() {
1374 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001375 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001376
Andrew Lee6324f702015-06-16 14:45:58 -07001377 private int getFabAlignment() {
1378 if (!mIsLandscape && !isInSearchUi() &&
1379 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1380 return FloatingActionButtonController.ALIGN_MIDDLE;
1381 }
1382 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001383 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001384}