blob: d1a6872409a3b2c885707cc3fa23ded3bcdf9351 [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 Lee53a22302014-04-29 18:13:46 -070019import android.app.ActionBar;
Chiao Cheng94b10b52012-08-17 16:59:12 -070020import android.app.Fragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -070021import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080022import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070023import android.content.Context;
24import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080025import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
Sai Cheemalapati82d6da52014-06-03 13:54:23 -070027import android.content.res.Configuration;
Andrew Lee2423a2f2014-05-29 14:14:45 -070028import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070029import android.net.Uri;
30import android.os.Bundle;
Yorke Leef5554ce2015-01-27 14:46:02 -080031import android.os.Trace;
Yorke Leec3766332013-07-31 11:13:16 -070032import android.speech.RecognizerIntent;
Yorke Leecc4660d2014-04-24 11:22:57 -070033import android.support.v4.view.ViewPager;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070034import android.telecom.PhoneAccount;
35import android.telecom.TelecomManager;
Yorke Lee53a22302014-04-29 18:13:46 -070036import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070038import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070040import android.view.DragEvent;
Yorke Lee74edcdc2014-07-11 16:15:08 -070041import android.view.Gravity;
Andrew Leeb1903842014-05-15 16:11:24 -070042import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070045import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070047import android.view.View.OnDragListener;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070048import android.view.View.OnTouchListener;
Ihab Awad9134e252014-07-09 12:32:52 -070049import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070050import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070051import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070052import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070053import android.widget.EditText;
Yorke Lee28aab272014-06-11 10:24:19 -070054import android.widget.FrameLayout;
Andrew Leea73e1892014-05-13 13:21:16 -070055import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070056import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080057import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070058
Brian Attwellc311af12014-10-22 18:28:58 -070059import com.android.contacts.common.activity.TransactionSafeActivity;
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 Leefce269a2013-08-12 11:56:04 -070068import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070069import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070071import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070072import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080073import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070074import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080075import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070076import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070077import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070078import com.android.dialer.list.RegularSearchFragment;
79import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070080import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070081import com.android.dialer.list.SpeedDialFragment;
Yorke Leeaf6f1952014-07-14 19:13:16 -070082import com.android.dialer.settings.DialerSettingsActivity;
Andrew Lee247df6e2015-05-12 19:09:00 -070083import com.android.dialer.util.IntentUtil;
Yorke Lee7d20f822014-06-19 17:09:33 -070084import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070085import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070086import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070087import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070088import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee2fec47b2014-08-05 18:16:27 -070089import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070090import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070091
Yorke Leed5c512a2015-01-30 14:42:53 -080092import junit.framework.Assert;
93
Yorke Leec3766332013-07-31 11:13:16 -070094import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080095import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070096
Chiao Cheng94b10b52012-08-17 16:59:12 -070097/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070098 * The dialer tab's title is 'phone', a more common name (see strings.xml).
99 */
Yorke Leec3766332013-07-31 11:13:16 -0700100public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700101 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700102 OnListFragmentScrolledListener,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700103 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700104 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700105 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700106 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700107 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700108 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700109 ViewPager.OnPageChangeListener,
110 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700111 private static final String TAG = "DialtactsActivity";
112
Yorke Lee7cc61492015-06-01 14:59:33 -0700113 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700114
Yorke Leef74011e2013-08-02 11:30:30 -0700115 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
116
Chiao Cheng94b10b52012-08-17 16:59:12 -0700117 /** @see #getCallOrigin() */
118 private static final String CALL_ORIGIN_DIALTACTS =
119 "com.android.dialer.DialtactsActivity";
120
Yorke Leeb207f8a2013-08-29 18:51:06 -0700121 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
122 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700123 private static final String KEY_SEARCH_QUERY = "search_query";
124 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700125 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700126
Yorke Leec3766332013-07-31 11:13:16 -0700127 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
128 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
129 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
130 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700131
Chiao Cheng94b10b52012-08-17 16:59:12 -0700132 /**
133 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
134 */
135 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700136 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700137
Yorke Leec3766332013-07-31 11:13:16 -0700138 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700139
Andrew Lee6324f702015-06-16 14:45:58 -0700140 private static final int FAB_SCALE_IN_DELAY_MS = 300;
141
Yorke Leef614f6f2014-10-03 10:44:19 -0700142 private FrameLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700143
Yorke Leec3766332013-07-31 11:13:16 -0700144 /**
Yorke Leec3766332013-07-31 11:13:16 -0700145 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700146 */
Evan Charlton72d66252014-11-07 16:23:47 -0800147 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700148
149 /**
150 * Fragment for searching phone numbers using the alphanumeric keyboard.
151 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700152 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700153
154 /**
155 * Fragment for searching phone numbers using the dialpad.
156 */
157 private SmartDialSearchFragment mSmartDialSearchFragment;
158
Yorke Leee00c9fe2014-04-12 12:42:06 -0700159 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700160 * Animation that slides in.
161 */
162 private Animation mSlideIn;
163
164 /**
165 * Animation that slides out.
166 */
167 private Animation mSlideOut;
168
Yorke Lee6c450eb2015-05-08 09:45:47 -0700169 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
170 @Override
171 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700172 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700173 }
174 };
175
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700176 /**
177 * Listener for after slide out animation completes on dialer fragment.
178 */
179 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
180 @Override
181 public void onAnimationEnd(Animation animation) {
182 commitDialpadFragmentHide();
183 }
184 };
185
186 /**
Yorke Leee00c9fe2014-04-12 12:42:06 -0700187 * Fragment containing the speed dial list, recents list, and all contacts list.
188 */
189 private ListsFragment mListsFragment;
190
Yorke Lee19e68ca2014-10-28 11:51:40 -0700191 /**
192 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
193 * be commited.
194 */
195 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800196 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700197 private boolean mInDialpadSearch;
198 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700199 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700200 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700201 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700202
Yorke Leeef2b7382013-08-09 17:39:25 -0700203 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700204 * Whether or not the device is in landscape orientation.
205 */
206 private boolean mIsLandscape;
207
208 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700209 * True if the dialpad is only temporarily showing due to being in call
210 */
211 private boolean mInCallDialpadUp;
212
213 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700214 * True when this activity has been launched for the first time.
215 */
Yorke Lee98702de2013-08-06 12:03:32 -0700216 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700217
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700218 /**
219 * Search query to be applied to the SearchView in the ActionBar once
220 * onCreateOptionsMenu has been called.
221 */
222 private String mPendingSearchViewQuery;
223
Yorke Lee74edcdc2014-07-11 16:15:08 -0700224 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700225 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700226 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700227
Yorke Leeef2b7382013-08-09 17:39:25 -0700228 private String mSearchQuery;
229
Yorke Leefce269a2013-08-12 11:56:04 -0700230 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700231 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700232 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700233
Sai Cheemalapati41460652014-06-02 21:05:39 -0700234 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700235
Andrew Lee2423a2f2014-05-29 14:14:45 -0700236 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700237
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800238 /**
239 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
240 * {@link #onResume()} to populate the search box.
241 */
242 private String mVoiceSearchQuery;
243
Andrew Lee467de3d2015-03-17 15:47:24 -0700244 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700245 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700246 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700247 }
248
249 @Override
250 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700251 final boolean hasContactsPermission =
252 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700253 final Menu menu = getMenu();
254 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700255 clearFrequents.setVisible(mListsFragment != null &&
256 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700257 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
258
259 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
260 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700261
262 menu.findItem(R.id.menu_history).setVisible(
263 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700264 super.show();
265 }
266 }
267
Chiao Cheng94b10b52012-08-17 16:59:12 -0700268 /**
Yorke Lee28266192014-05-01 10:05:52 -0700269 * Listener that listens to drag events and sends their x and y coordinates to a
270 * {@link DragDropController}.
271 */
272 private class LayoutOnDragListener implements OnDragListener {
273 @Override
274 public boolean onDrag(View v, DragEvent event) {
275 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700276 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700277 }
278 return true;
279 }
280 }
281
282 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700283 * Listener used to send search queries to the phone search fragment.
284 */
Yorke Lee53a22302014-04-29 18:13:46 -0700285 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700286 @Override
287 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
288 }
Yorke Leec3766332013-07-31 11:13:16 -0700289
Andrew Lee99a570c2014-05-15 14:18:50 -0700290 @Override
291 public void onTextChanged(CharSequence s, int start, int before, int count) {
292 final String newText = s.toString();
293 if (newText.equals(mSearchQuery)) {
294 // If the query hasn't changed (perhaps due to activity being destroyed
295 // and restored, or user launching the same DIAL intent twice), then there is
296 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700297 return;
298 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700299 if (DEBUG) {
300 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700301 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700302 }
Yorke Lee710709d2014-05-29 07:18:42 -0700303 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700304
Andrew Lee90374a72014-05-16 15:01:09 -0700305 // Show search fragment only when the query string is changed to non-empty text.
306 if (!TextUtils.isEmpty(newText)) {
307 // Call enterSearchUi only if we are switching search modes, or showing a search
308 // fragment for the first time.
309 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
310 (!mIsDialpadShown && mInRegularSearch);
311 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700312 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700313 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700314 }
315
Andrew Leea8515422014-06-13 16:53:54 -0700316 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700317 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700318 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700319 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700320 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700321 }
322
323 @Override
324 public void afterTextChanged(Editable s) {
325 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700326 };
327
Andrew Leeb1903842014-05-15 16:11:24 -0700328
329 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700330 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700331 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700332 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700333 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700334 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700335 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700336 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700337 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
338 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700339 }
Andrew Leeb1903842014-05-15 16:11:24 -0700340 }
341 };
342
343 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700344 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700345 */
346 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
347 @Override
348 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700349 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
350 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
351 // If the search term is empty, close the search UI.
352 maybeExitSearchUi();
353 } else {
354 // If the search term is not empty, show the dialpad fab.
355 showFabInSearchUi();
356 }
Andrew Leeb1903842014-05-15 16:11:24 -0700357 }
358 return false;
359 }
360 };
361
Chiao Cheng94b10b52012-08-17 16:59:12 -0700362 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700363 public boolean dispatchTouchEvent(MotionEvent ev) {
364 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
365 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
366 }
367 return super.dispatchTouchEvent(ev);
368
369 }
370
371 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700372 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800373 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700374 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700375
Yorke Lee98702de2013-08-06 12:03:32 -0700376 mFirstLaunch = true;
377
Andrew Lee2423a2f2014-05-29 14:14:45 -0700378 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700379 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700380
Yorke Leef5554ce2015-01-27 14:46:02 -0800381 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700382 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800383 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800384 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700385
Yorke Leef5554ce2015-01-27 14:46:02 -0800386 Trace.beginSection(TAG + " setup Views");
Yorke Lee53a22302014-04-29 18:13:46 -0700387 final ActionBar actionBar = getActionBar();
388 actionBar.setCustomView(R.layout.search_edittext);
389 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700390 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700391
Andrew Lee04675a52014-06-05 18:36:20 -0700392 SearchEditTextLayout searchEditTextLayout =
Andrew Leee3f59a82015-02-26 12:06:35 -0800393 (SearchEditTextLayout) actionBar.getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700394 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
395
Andrew Leee3f59a82015-02-26 12:06:35 -0800396 mActionBarController = new ActionBarController(this, searchEditTextLayout);
397
Andrew Lee04675a52014-06-05 18:36:20 -0700398 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700399 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700400 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
401 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
402 .setOnClickListener(mSearchViewOnClickListener);
403 searchEditTextLayout.findViewById(R.id.search_box_start_search)
404 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700405 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700406 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700407 @Override
408 public void onBackButtonClicked() {
409 onBackPressed();
410 }
Andrew Lee6324f702015-06-16 14:45:58 -0700411
412 @Override
413 public void onSearchViewClicked() {
414 // Hide FAB, as the keyboard is shown.
415 mFloatingActionButtonController.scaleOut();
416 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700417 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700418
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700419 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700420 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700421
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700422 final View floatingActionButtonContainer = findViewById(
423 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700424 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
425 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700426 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700427 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700428
Andrew Lee04675a52014-06-05 18:36:20 -0700429 ImageButton optionsMenuButton =
430 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700431 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700432 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
433 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700434
Yorke Leed5c512a2015-01-30 14:42:53 -0800435 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
436 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700437 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800438 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000439 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800440 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700441 } else {
442 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700443 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
444 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700445 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700446 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700447 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700448 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700449
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700450 final boolean isLayoutRtl = DialerUtils.isRtl();
451 if (mIsLandscape) {
452 mSlideIn = AnimationUtils.loadAnimation(this,
453 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
454 mSlideOut = AnimationUtils.loadAnimation(this,
455 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
456 } else {
457 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
458 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
459 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700460
Yorke Lee2fec47b2014-08-05 18:16:27 -0700461 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
462 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
463
Yorke Lee6c450eb2015-05-08 09:45:47 -0700464 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700465 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700466
Yorke Leef614f6f2014-10-03 10:44:19 -0700467 mParentLayout = (FrameLayout) findViewById(R.id.dialtacts_mainlayout);
468 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700469 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700470 new ViewTreeObserver.OnGlobalLayoutListener() {
471 @Override
472 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700473 final ViewTreeObserver observer =
474 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700475 if (!observer.isAlive()) {
476 return;
477 }
478 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700479 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700480 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700481 mFloatingActionButtonController.align(
482 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700483 }
484 });
Andrew Lee0c667702014-05-12 14:31:45 -0700485
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700486 setupActivityOverlay();
487
Yorke Leef5554ce2015-01-27 14:46:02 -0800488 Trace.endSection();
489
490 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700491 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700492 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800493 Trace.endSection();
494 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700495 }
496
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700497 private void setupActivityOverlay() {
498 final View activityOverlay = findViewById(R.id.activity_overlay);
499 activityOverlay.setOnTouchListener(new OnTouchListener() {
500 @Override
501 public boolean onTouch(View v, MotionEvent event) {
502 if (!mIsDialpadShown) {
503 maybeExitSearchUi();
504 }
505 return false;
506 }
507 });
508 }
509
Chiao Cheng94b10b52012-08-17 16:59:12 -0700510 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700511 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800512 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700513 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700514
515 PermissionsUtil.updateCachedPermissions(this);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700516 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700517 if (mFirstLaunch) {
518 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700519 } else if (!phoneIsInUse() && mInCallDialpadUp) {
520 hideDialpadFragment(false, true);
521 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700522 } else if (mShowDialpadOnResume) {
523 showDialpadFragment(false);
524 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700525 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800526
527 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
528 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
529 // shown until onResume has completed. Active the search UI and set the search term now.
530 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
531 mActionBarController.onSearchBoxTapped();
532 mSearchView.setText(mVoiceSearchQuery);
533 mVoiceSearchQuery = null;
534 }
535
Yorke Lee98702de2013-08-06 12:03:32 -0700536 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800537
538 if (mIsRestarting) {
539 // This is only called when the activity goes from resumed -> paused -> resumed, so it
540 // will not cause an extra view to be sent out on rotation
541 if (mIsDialpadShown) {
542 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
543 }
544 mIsRestarting = false;
545 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700546
Yorke Lee53a22302014-04-29 18:13:46 -0700547 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700548 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700549 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Yorke Leef5554ce2015-01-27 14:46:02 -0800550 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700551 }
552
553 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800554 protected void onRestart() {
555 super.onRestart();
556 mIsRestarting = true;
557 }
558
559 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700560 protected void onPause() {
561 if (mClearSearchOnPause) {
562 hideDialpadAndSearchUi();
563 mClearSearchOnPause = false;
564 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700565 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
566 commitDialpadFragmentHide();
567 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700568 super.onPause();
569 }
570
571 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700572 protected void onSaveInstanceState(Bundle outState) {
573 super.onSaveInstanceState(outState);
574 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700575 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
576 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700577 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700578 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700579 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700580 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700581 }
582
583 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700584 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700585 if (fragment instanceof DialpadFragment) {
586 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800587 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700588 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
589 transaction.hide(mDialpadFragment);
590 transaction.commit();
591 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700592 } else if (fragment instanceof SmartDialSearchFragment) {
593 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700594 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700595 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700596 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700597 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700598 } else if (fragment instanceof ListsFragment) {
599 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700600 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700601 }
Yorke Leec3766332013-07-31 11:13:16 -0700602 }
603
Alon Albertb453e5b2013-09-10 15:54:23 -0700604 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700605 final Intent intent = new Intent(this, DialerSettingsActivity.class);
606 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700607 }
608
Chiao Cheng94b10b52012-08-17 16:59:12 -0700609 @Override
610 public void onClick(View view) {
611 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700612 case R.id.floating_action_button:
Andrew Lee432d4b52015-05-20 16:52:00 -0700613 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700614 DialerUtils.startActivityWithErrorToast(
615 this,
616 IntentUtil.getNewContactIntent(),
617 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700618 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700619 mInCallDialpadUp = false;
620 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700621 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700622 break;
Yorke Leec3766332013-07-31 11:13:16 -0700623 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800624 try {
625 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
626 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
627 } catch (ActivityNotFoundException e) {
628 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
629 Toast.LENGTH_SHORT).show();
630 }
Yorke Leec3766332013-07-31 11:13:16 -0700631 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700632 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700633 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700634 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700635 default: {
636 Log.wtf(TAG, "Unexpected onClick event from " + view);
637 break;
638 }
639 }
640 }
641
Yorke Leec3766332013-07-31 11:13:16 -0700642 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700643 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700644 switch (item.getItemId()) {
645 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700646 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
647 // CONTENT_TYPE, so that we always open our call log from our dialer
648 final Intent intent = new Intent(this, CallLogActivity.class);
649 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700650 break;
651 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700652 DialerUtils.startActivityWithErrorToast(
653 this,
654 IntentUtil.getNewContactIntent(),
655 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700656 break;
657 case R.id.menu_import_export:
658 // We hard-code the "contactsAreAvailable" argument because doing it properly would
659 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
660 // now in Dialtacts for (potential) performance reasons. Compare with how it is
661 // done in {@link PeopleActivity}.
662 ImportExportDialogFragment.show(getFragmentManager(), true,
663 DialtactsActivity.class);
664 return true;
665 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700666 ClearFrequentsDialog.show(getFragmentManager());
667 return true;
668 case R.id.menu_call_settings:
669 handleMenuSettings();
670 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700671 }
672 return false;
673 }
674
675 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700676 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
677 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
678 if (resultCode == RESULT_OK) {
679 final ArrayList<String> matches = data.getStringArrayListExtra(
680 RecognizerIntent.EXTRA_RESULTS);
681 if (matches.size() > 0) {
682 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800683 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700684 } else {
685 Log.e(TAG, "Voice search - nothing heard");
686 }
687 } else {
688 Log.e(TAG, "Voice search failed");
689 }
690 }
691 super.onActivityResult(requestCode, resultCode, data);
692 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700693
Andrew Lee2a58ab82014-05-15 02:16:04 -0700694 /**
695 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
696 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
697 * @see #onDialpadShown
698 */
Yorke Leec3766332013-07-31 11:13:16 -0700699 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700700 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700701 return;
702 }
703 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800704
Yorke Lee7ee5c422014-11-04 10:29:32 -0800705 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700706
Chiao Cheng94b10b52012-08-17 16:59:12 -0700707 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800708 if (mDialpadFragment == null) {
709 mDialpadFragment = new DialpadFragment();
710 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
711 } else {
712 ft.show(mDialpadFragment);
713 }
714
715 mDialpadFragment.setAnimate(animate);
716 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700717 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700718
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700719 if (animate) {
720 mFloatingActionButtonController.scaleOut();
721 } else {
722 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700723 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700724 }
Yorke Lee5253be02014-05-21 18:50:03 -0700725 mActionBarController.onDialpadUp();
726
Yorke Lee6c450eb2015-05-08 09:45:47 -0700727 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700728 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729
Andrew Lee2a58ab82014-05-15 02:16:04 -0700730 /**
731 * Callback from child DialpadFragment when the dialpad is shown.
732 */
733 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800734 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700735 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700736 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700737 } else {
738 mDialpadFragment.setYFraction(0);
739 }
740
Andrew Lee2a58ab82014-05-15 02:16:04 -0700741 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700742 }
743
744 /**
745 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
746 * a callback after the hide animation ends.
747 * @see #commitDialpadFragmentHide
748 */
Yorke Leeca195042013-09-25 16:29:38 -0700749 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700750 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700751 return;
752 }
Yorke Leef6673d32013-08-23 15:34:17 -0700753 if (clearDialpad) {
754 mDialpadFragment.clearDialpad();
755 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700756 if (!mIsDialpadShown) {
757 return;
Yorke Leec3766332013-07-31 11:13:16 -0700758 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700759 mIsDialpadShown = false;
760 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800761 mListsFragment.setUserVisibleHint(true);
762 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700763
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700764 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700765
Andrew Lee6324f702015-06-16 14:45:58 -0700766 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700767 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700768 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700769 } else {
770 commitDialpadFragmentHide();
771 }
772
Yorke Lee5253be02014-05-21 18:50:03 -0700773 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700774
Andrew Leed4cde832014-05-16 15:56:48 -0700775 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700776 if (TextUtils.isEmpty(mSearchQuery)) {
777 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700778 }
Andrew Leed4cde832014-05-16 15:56:48 -0700779 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700780 }
781
782 /**
783 * Finishes hiding the dialpad fragment after any animations are completed.
784 */
785 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800786 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700787 final FragmentTransaction ft = getFragmentManager().beginTransaction();
788 ft.hide(mDialpadFragment);
789 ft.commit();
790 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700791 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700792 }
793
Andrew Lee2a58ab82014-05-15 02:16:04 -0700794 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700795 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700796 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700797 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700798 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700799 fragment = mRegularSearchFragment;
800 }
801 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700802 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700803 }
804 }
805
Yorke Lee5253be02014-05-21 18:50:03 -0700806 @Override
807 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700808 return mInDialpadSearch || mInRegularSearch;
809 }
810
Yorke Lee5253be02014-05-21 18:50:03 -0700811 @Override
812 public boolean hasSearchQuery() {
813 return !TextUtils.isEmpty(mSearchQuery);
814 }
815
816 @Override
817 public boolean shouldShowActionBar() {
818 return mListsFragment.shouldShowActionBar();
819 }
820
Yorke Leeb207f8a2013-08-29 18:51:06 -0700821 private void setNotInSearchUi() {
822 mInDialpadSearch = false;
823 mInRegularSearch = false;
824 }
825
Yorke Lee311969c2013-08-26 06:31:11 -0700826 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700827 if (mIsDialpadShown) {
828 hideDialpadFragment(false, true);
829 } else {
830 exitSearchUi();
831 }
Yorke Lee311969c2013-08-26 06:31:11 -0700832 }
833
Yorke Lee53a22302014-04-29 18:13:46 -0700834 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700835 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
836 if (canIntentBeHandled(voiceIntent)) {
837 mVoiceSearchButton.setVisibility(View.VISIBLE);
838 mVoiceSearchButton.setOnClickListener(this);
839 } else {
840 mVoiceSearchButton.setVisibility(View.GONE);
841 }
842 }
843
Andrew Lee467de3d2015-03-17 15:47:24 -0700844 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700845 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
846 popupMenu.inflate(R.menu.dialtacts_options);
847 popupMenu.setOnMenuItemClickListener(this);
848 return popupMenu;
849 }
850
Yorke Lee86e21f72014-04-02 16:49:38 -0700851 @Override
852 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700853 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700854 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700855 mPendingSearchViewQuery = null;
856 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700857 if (mActionBarController != null) {
858 mActionBarController.restoreActionBarOffset();
859 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700860 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700861 }
862
Chiao Cheng94b10b52012-08-17 16:59:12 -0700863 /**
864 * Returns true if the intent is due to hitting the green send key (hardware call button:
865 * KEYCODE_CALL) while in a call.
866 *
867 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700868 * @return true if the intent is due to hitting the green send key while in a call
869 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700870 private boolean isSendKeyWhileInCall(Intent intent) {
871 // If there is a call in progress and the user launched the dialer by hitting the call
872 // button, go straight to the in-call screen.
873 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700874
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700875 if (callKey) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700876 getTelecomManager().showInCallScreen(false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700877 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700878 }
879
880 return false;
881 }
882
883 /**
884 * Sets the current tab based on the intent's request type
885 *
886 * @param intent Intent that contains information about which tab should be selected
887 */
Yorke Leec3766332013-07-31 11:13:16 -0700888 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700889 // 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 -0700890 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700891 finish();
892 return;
893 }
894
Yorke Leed5c512a2015-01-30 14:42:53 -0800895 final boolean phoneIsInUse = phoneIsInUse();
896 if (phoneIsInUse || (intent.getData() != null && isDialIntent(intent))) {
897 showDialpadFragment(false);
898 mDialpadFragment.setStartedFromNewIntent(true);
899 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
900 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700901 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700902 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700903 }
904
905 @Override
906 public void onNewIntent(Intent newIntent) {
907 setIntent(newIntent);
Nancy Chen199b7f02014-11-05 15:03:00 -0800908 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700909 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700910
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700911 if (newIntent.hasExtra(EXTRA_SHOW_TAB)) {
912 mListsFragment.showTab(
913 getIntent().getIntExtra(EXTRA_SHOW_TAB, mListsFragment.TAB_INDEX_SPEED_DIAL));
914 }
915
Chiao Cheng94b10b52012-08-17 16:59:12 -0700916 invalidateOptionsMenu();
917 }
918
919 /** Returns true if the given intent contains a phone number to populate the dialer with */
920 private boolean isDialIntent(Intent intent) {
921 final String action = intent.getAction();
922 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
923 return true;
924 }
925 if (Intent.ACTION_VIEW.equals(action)) {
926 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700927 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700928 return true;
929 }
930 }
931 return false;
932 }
933
934 /**
935 * Returns an appropriate call origin for this Activity. May return null when no call origin
936 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
937 * for remembering the tab in which the user made a phone call, so the external app's DIAL
938 * request should not be counted.)
939 */
940 public String getCallOrigin() {
941 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
942 }
943
Chiao Cheng94b10b52012-08-17 16:59:12 -0700944 /**
Yorke Leec3766332013-07-31 11:13:16 -0700945 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700946 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700947 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700948 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700949 // Weird race condition where fragment is doing work after the activity is destroyed
950 // due to talkback being on (b/10209937). Just return since we can't do any
951 // constructive here.
952 return;
953 }
954
Yorke Leeef2b7382013-08-09 17:39:25 -0700955 if (DEBUG) {
956 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
957 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700958
Yorke Leec3766332013-07-31 11:13:16 -0700959 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700960 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700961 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700962 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700963 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700964 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700965
Yorke Leeb207f8a2013-08-29 18:51:06 -0700966 final String tag;
967 if (smartDialSearch) {
968 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
969 } else {
970 tag = TAG_REGULAR_SEARCH_FRAGMENT;
971 }
972 mInDialpadSearch = smartDialSearch;
973 mInRegularSearch = !smartDialSearch;
974
Andrew Lee6324f702015-06-16 14:45:58 -0700975 mFloatingActionButtonController.scaleOut();
976
Andrew Lee752956e2014-05-15 11:43:38 -0700977 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700978 if (animate) {
979 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
980 } else {
981 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
982 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700983 if (fragment == null) {
984 if (smartDialSearch) {
985 fragment = new SmartDialSearchFragment();
986 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700987 fragment = new RegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -0700988 fragment.setOnTouchListener(new View.OnTouchListener() {
989 @Override
990 public boolean onTouch(View v, MotionEvent event) {
991 // Show the FAB when the user touches the lists fragment and the soft
992 // keyboard is hidden.
993 showFabInSearchUi();
994 return false;
995 }
996 });
Yorke Leeef2b7382013-08-09 17:39:25 -0700997 }
Andrew Leeb1903842014-05-15 16:11:24 -0700998 transaction.add(R.id.dialtacts_frame, fragment, tag);
999 } else {
1000 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001001 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001002 // DialtactsActivity will provide the options menu
1003 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001004 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001005 if (!smartDialSearch) {
1006 fragment.setQueryString(query, false /* delaySelection */);
1007 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001008 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001009
Yorke Lee6c450eb2015-05-08 09:45:47 -07001010 if (animate) {
1011 mListsFragment.getView().animate().alpha(0).withLayer();
1012 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001013 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001014 }
1015
1016 /**
Yorke Leec3766332013-07-31 11:13:16 -07001017 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001018 */
Yorke Leec3766332013-07-31 11:13:16 -07001019 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001020 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001021 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001022 return;
1023 }
Andrew Leeb1903842014-05-15 16:11:24 -07001024
Andrew Leeb1903842014-05-15 16:11:24 -07001025 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001026
1027 if (mDialpadFragment != null) {
1028 mDialpadFragment.clearDialpad();
1029 }
1030
Yorke Leeb207f8a2013-08-29 18:51:06 -07001031 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001032
Andrew Lee6324f702015-06-16 14:45:58 -07001033 // Restore the FAB for the lists fragment.
1034 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1035 mFloatingActionButtonController.setVisible(false);
1036 }
1037 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1038 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1039 onPageSelected(mListsFragment.getCurrentTabIndex());
1040
Andrew Leeb1903842014-05-15 16:11:24 -07001041 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001042 if (mSmartDialSearchFragment != null) {
1043 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001044 }
Andrew Leeb1903842014-05-15 16:11:24 -07001045 if (mRegularSearchFragment != null) {
1046 transaction.remove(mRegularSearchFragment);
1047 }
1048 transaction.commit();
1049
1050 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001051
1052 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001053 // If the dialpad fragment wasn't previously visible, then send a screen view because
1054 // we are exiting regular search. Otherwise, the screen view will be sent by
1055 // {@link #hideDialpadFragment}.
1056 mListsFragment.sendScreenViewForCurrentPosition();
1057 mListsFragment.setUserVisibleHint(true);
1058 }
1059
Yorke Lee5253be02014-05-21 18:50:03 -07001060 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001061 }
1062
Chiao Cheng94b10b52012-08-17 16:59:12 -07001063 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001064 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001065 if (mStateSaved) {
1066 return;
1067 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001068 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001069 if (TextUtils.isEmpty(mSearchQuery) ||
1070 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1071 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001072 exitSearchUi();
1073 }
Yorke Leef6673d32013-08-23 15:34:17 -07001074 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001075 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001076 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001077 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001078 } else {
1079 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001080 }
Yorke Leec3766332013-07-31 11:13:16 -07001081 }
1082
Yorke Lee81a313d2015-06-01 14:53:59 -07001083 private void maybeEnterSearchUi() {
1084 if (!isInSearchUi()) {
1085 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1086 }
1087 }
1088
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001089 /**
1090 * @return True if the search UI was exited, false otherwise
1091 */
1092 private boolean maybeExitSearchUi() {
1093 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1094 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001095 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001096 return true;
1097 }
1098 return false;
1099 }
1100
Andrew Lee6324f702015-06-16 14:45:58 -07001101 private void showFabInSearchUi() {
1102 mFloatingActionButtonController.changeIcon(
1103 getResources().getDrawable(R.drawable.fab_ic_dial),
1104 getResources().getString(R.string.action_menu_dialpad_button));
1105 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1106 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1107 }
1108
Yorke Leec3766332013-07-31 11:13:16 -07001109 @Override
1110 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001111 if (mSmartDialSearchFragment != null) {
1112 mSmartDialSearchFragment.setAddToContactNumber(query);
1113 }
Yorke Leec3766332013-07-31 11:13:16 -07001114 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1115 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001116
1117 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001118 if (DEBUG) {
1119 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1120 }
1121 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1122 // This callback can happen if the dialpad fragment is recreated because of
1123 // activity destruction. In that case, don't update the search view because
1124 // that would bring the user back to the search fragment regardless of the
1125 // previous state of the application. Instead, just return here and let the
1126 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001127 if (!TextUtils.isEmpty(normalizedQuery)) {
1128 mPendingSearchViewQuery = normalizedQuery;
1129 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001130 return;
1131 }
Yorke Lee53a22302014-04-29 18:13:46 -07001132 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001133 }
Santos Cordon83131712015-04-16 21:25:35 -07001134
1135 try {
1136 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1137 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1138 }
1139 } catch (Exception ignored) {
1140 // Skip any exceptions for this piece of code
1141 }
1142
Chiao Cheng94b10b52012-08-17 16:59:12 -07001143 }
Yorke Leec3766332013-07-31 11:13:16 -07001144
1145 @Override
1146 public void onListFragmentScrollStateChange(int scrollState) {
1147 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001148 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001149 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001150 }
1151 }
1152
1153 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001154 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001155 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001156 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1157 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001158 }
1159
Yorke Leec3766332013-07-31 11:13:16 -07001160 private boolean phoneIsInUse() {
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001161 return getTelecomManager().isInCall();
Yorke Leec3766332013-07-31 11:13:16 -07001162 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001163
Yorke Leeda0f9042013-12-05 14:25:51 -08001164 private boolean canIntentBeHandled(Intent intent) {
1165 final PackageManager packageManager = getPackageManager();
1166 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1167 PackageManager.MATCH_DEFAULT_ONLY);
1168 return resolveInfo != null && resolveInfo.size() > 0;
1169 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001170
Yorke Lee86e21f72014-04-02 16:49:38 -07001171 /**
1172 * Called when the user has long-pressed a contact tile to start a drag operation.
1173 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001174 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001175 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001176 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001177 }
1178
1179 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001180 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1181 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001182
Yorke Lee86e21f72014-04-02 16:49:38 -07001183 /**
1184 * Called when the user has released a contact tile after long-pressing it.
1185 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001186 @Override
1187 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001188 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001189 }
1190
1191 @Override
1192 public void onDroppedOnRemove() {}
1193
1194 /**
Yorke Leed999b712014-04-23 15:10:58 -07001195 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001196 * once it has been attached to the activity.
1197 */
1198 @Override
1199 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001200 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001201 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001202 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001203
1204 @Override
1205 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001206 // Specify call-origin so that users will see the previous tab instead of
1207 // CallLog screen (search UI will be automatically exited).
1208 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001209 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001210 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001211 }
1212
1213 @Override
1214 public void onCallNumberDirectly(String phoneNumber) {
Andrew Leefc1fed72014-07-02 11:05:53 -07001215 onCallNumberDirectly(phoneNumber, false /* isVideoCall */);
1216 }
1217
1218 @Override
1219 public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall) {
Andrew Leed2a683f2014-07-08 15:54:50 -07001220 Intent intent = isVideoCall ?
Andrew Lee247df6e2015-05-12 19:09:00 -07001221 IntentUtil.getVideoCallIntent(phoneNumber, getCallOrigin()) :
1222 IntentUtil.getCallIntent(phoneNumber, getCallOrigin());
Yorke Lee7d20f822014-06-19 17:09:33 -07001223 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001224 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001225 }
1226
1227 @Override
1228 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001229 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001230 }
1231
1232 @Override
1233 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001234 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001235 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001236
Yorke Leecc4660d2014-04-24 11:22:57 -07001237 @Override
1238 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001239 int tabIndex = mListsFragment.getCurrentTabIndex();
1240
1241 // Scroll the button from center to end when moving from the Speed Dial to Recents tab.
1242 // In RTL, scroll when the current tab is Recents instead of Speed Dial, because the order
1243 // of the tabs is reversed and the ViewPager returns the left tab position during scroll.
1244 boolean isRtl = DialerUtils.isRtl();
1245 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1246 mFloatingActionButtonController.onPageScrolled(positionOffset);
1247 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_RECENTS && !mIsLandscape) {
1248 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1249 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001250 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001251 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001252 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001253
Andrew Lee432d4b52015-05-20 16:52:00 -07001254 @Override
1255 public void onPageSelected(int position) {
1256 int tabIndex = mListsFragment.getCurrentTabIndex();
1257 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001258 mFloatingActionButtonController.changeIcon(
1259 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1260 getResources().getString(R.string.search_shortcut_create_new_contact));
1261 } else {
1262 mFloatingActionButtonController.changeIcon(
1263 getResources().getDrawable(R.drawable.fab_ic_dial),
1264 getResources().getString(R.string.action_menu_dialpad_button));
1265 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001266 }
1267
1268 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001269 public void onPageScrollStateChanged(int state) {
1270 }
1271
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001272 private TelecomManager getTelecomManager() {
1273 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordond15629d2014-07-07 15:15:29 -07001274 }
1275
Yorke Lee5253be02014-05-21 18:50:03 -07001276 @Override
1277 public boolean isActionBarShowing() {
1278 return mActionBarController.isActionBarShowing();
1279 }
1280
Yorke Leec98a5bb2014-10-28 16:12:05 -07001281 @Override
1282 public ActionBarController getActionBarController() {
1283 return mActionBarController;
1284 }
1285
Yorke Lee6bc67162015-06-27 14:03:25 -07001286 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001287 public boolean isDialpadShown() {
1288 return mIsDialpadShown;
1289 }
1290
Yorke Lee5253be02014-05-21 18:50:03 -07001291 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001292 public int getDialpadHeight() {
1293 if (mDialpadFragment != null) {
1294 return mDialpadFragment.getDialpadHeight();
1295 }
1296 return 0;
1297 }
1298
1299 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001300 public int getActionBarHideOffset() {
1301 return getActionBar().getHideOffset();
1302 }
1303
1304 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001305 public void setActionBarHideOffset(int offset) {
1306 getActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001307 }
1308
1309 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001310 public int getActionBarHeight() {
1311 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001312 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001313
Andrew Lee6324f702015-06-16 14:45:58 -07001314
1315 private int getFabAlignment() {
1316 if (!mIsLandscape && !isInSearchUi() &&
1317 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1318 return FloatingActionButtonController.ALIGN_MIDDLE;
1319 }
1320 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001321 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001322}