blob: 69cc14673c80f324721c98c44981ae592262a297 [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;
Nancy Chen019713e2015-07-06 18:25:24 -070032import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070033import android.speech.RecognizerIntent;
Yorke Leecc4660d2014-04-24 11:22:57 -070034import android.support.v4.view.ViewPager;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070035import android.telecom.PhoneAccount;
36import android.telecom.TelecomManager;
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;
Yorke Lee28aab272014-06-11 10:24:19 -070055import android.widget.FrameLayout;
Andrew Leea73e1892014-05-13 13:21:16 -070056import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080058import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070059
Brian Attwellc311af12014-10-22 18:28:58 -070060import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070061import com.android.contacts.common.dialog.ClearFrequentsDialog;
62import com.android.contacts.common.interactions.ImportExportDialogFragment;
Nancy Chen0d3a7392014-08-12 14:44:53 -070063import com.android.contacts.common.interactions.TouchPointManager;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080064import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec16ea5a2015-05-19 15:51:01 -070065import com.android.contacts.common.util.PermissionsUtil;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -070066import com.android.contacts.common.widget.FloatingActionButtonController;
Brian Attwellbeab3bb2014-10-27 12:24:49 -070067import com.android.contacts.commonbind.analytics.AnalyticsUtil;
Yorke Leec3766332013-07-31 11:13:16 -070068import com.android.dialer.calllog.CallLogActivity;
Yorke Lee38019af2015-07-14 17:05:38 -070069import com.android.dialer.calllog.CallLogFragment;
Yorke Leefce269a2013-08-12 11:56:04 -070070import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070071import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070073import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070074import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080075import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070076import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080077import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070078import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070079import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070080import com.android.dialer.list.RegularSearchFragment;
81import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070082import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070083import com.android.dialer.list.SpeedDialFragment;
Yorke Leeaf6f1952014-07-14 19:13:16 -070084import com.android.dialer.settings.DialerSettingsActivity;
Andrew Lee247df6e2015-05-12 19:09:00 -070085import com.android.dialer.util.IntentUtil;
Yorke Lee7d20f822014-06-19 17:09:33 -070086import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070087import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070088import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070089import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070090import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee2fec47b2014-08-05 18:16:27 -070091import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070092import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070093
Yorke Leed5c512a2015-01-30 14:42:53 -080094import junit.framework.Assert;
95
Yorke Leec3766332013-07-31 11:13:16 -070096import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080097import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070098
Chiao Cheng94b10b52012-08-17 16:59:12 -070099/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700100 * The dialer tab's title is 'phone', a more common name (see strings.xml).
101 */
Yorke Leec3766332013-07-31 11:13:16 -0700102public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700103 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700104 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700105 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700106 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700107 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700108 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700109 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700110 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700111 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700112 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700113 ViewPager.OnPageChangeListener,
114 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 private static final String TAG = "DialtactsActivity";
116
Yorke Lee7cc61492015-06-01 14:59:33 -0700117 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118
Yorke Leef74011e2013-08-02 11:30:30 -0700119 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
120
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121 /** @see #getCallOrigin() */
122 private static final String CALL_ORIGIN_DIALTACTS =
123 "com.android.dialer.DialtactsActivity";
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
Yorke Leef614f6f2014-10-03 10:44:19 -0700146 private FrameLayout 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 /**
Yorke Leee00c9fe2014-04-12 12:42:06 -0700191 * Fragment containing the speed dial list, recents list, and all contacts list.
192 */
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;
233
Yorke Leefce269a2013-08-12 11:56:04 -0700234 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700235 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700236 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700237
Sai Cheemalapati41460652014-06-02 21:05:39 -0700238 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700239
Andrew Lee2423a2f2014-05-29 14:14:45 -0700240 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700241
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800242 /**
243 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
244 * {@link #onResume()} to populate the search box.
245 */
246 private String mVoiceSearchQuery;
247
Andrew Lee467de3d2015-03-17 15:47:24 -0700248 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700249 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700250 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700251 }
252
253 @Override
254 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700255 final boolean hasContactsPermission =
256 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700257 final Menu menu = getMenu();
258 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700259 clearFrequents.setVisible(mListsFragment != null &&
260 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700261 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
262
263 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
264 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700265
266 menu.findItem(R.id.menu_history).setVisible(
267 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700268 super.show();
269 }
270 }
271
Chiao Cheng94b10b52012-08-17 16:59:12 -0700272 /**
Yorke Lee28266192014-05-01 10:05:52 -0700273 * Listener that listens to drag events and sends their x and y coordinates to a
274 * {@link DragDropController}.
275 */
276 private class LayoutOnDragListener implements OnDragListener {
277 @Override
278 public boolean onDrag(View v, DragEvent event) {
279 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700280 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700281 }
282 return true;
283 }
284 }
285
286 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700287 * Listener used to send search queries to the phone search fragment.
288 */
Yorke Lee53a22302014-04-29 18:13:46 -0700289 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700290 @Override
291 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
292 }
Yorke Leec3766332013-07-31 11:13:16 -0700293
Andrew Lee99a570c2014-05-15 14:18:50 -0700294 @Override
295 public void onTextChanged(CharSequence s, int start, int before, int count) {
296 final String newText = s.toString();
297 if (newText.equals(mSearchQuery)) {
298 // If the query hasn't changed (perhaps due to activity being destroyed
299 // and restored, or user launching the same DIAL intent twice), then there is
300 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700301 return;
302 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700303 if (DEBUG) {
304 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700305 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700306 }
Yorke Lee710709d2014-05-29 07:18:42 -0700307 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700308
Andrew Lee90374a72014-05-16 15:01:09 -0700309 // Show search fragment only when the query string is changed to non-empty text.
310 if (!TextUtils.isEmpty(newText)) {
311 // Call enterSearchUi only if we are switching search modes, or showing a search
312 // fragment for the first time.
313 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
314 (!mIsDialpadShown && mInRegularSearch);
315 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700316 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700317 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700318 }
319
Andrew Leea8515422014-06-13 16:53:54 -0700320 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700321 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700322 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700323 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700324 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700325 }
326
327 @Override
328 public void afterTextChanged(Editable s) {
329 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700330 };
331
Andrew Leeb1903842014-05-15 16:11:24 -0700332
333 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700334 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700335 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700336 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700337 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700338 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700339 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700340 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700341 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
342 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700343 }
Andrew Leeb1903842014-05-15 16:11:24 -0700344 }
345 };
346
347 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700348 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700349 */
350 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
351 @Override
352 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700353 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
354 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
355 // If the search term is empty, close the search UI.
356 maybeExitSearchUi();
357 } else {
358 // If the search term is not empty, show the dialpad fab.
359 showFabInSearchUi();
360 }
Andrew Leeb1903842014-05-15 16:11:24 -0700361 }
362 return false;
363 }
364 };
365
Chiao Cheng94b10b52012-08-17 16:59:12 -0700366 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700367 public boolean dispatchTouchEvent(MotionEvent ev) {
368 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
369 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
370 }
371 return super.dispatchTouchEvent(ev);
372
373 }
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 Lee98702de2013-08-06 12:03:32 -0700380 mFirstLaunch = true;
381
Andrew Lee2423a2f2014-05-29 14:14:45 -0700382 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700383 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700384
Yorke Leef5554ce2015-01-27 14:46:02 -0800385 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700386 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800387 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800388 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700389
Yorke Leef5554ce2015-01-27 14:46:02 -0800390 Trace.beginSection(TAG + " setup Views");
Yorke Lee53a22302014-04-29 18:13:46 -0700391 final ActionBar actionBar = getActionBar();
392 actionBar.setCustomView(R.layout.search_edittext);
393 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700394 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700395
Andrew Lee04675a52014-06-05 18:36:20 -0700396 SearchEditTextLayout searchEditTextLayout =
Andrew Leee3f59a82015-02-26 12:06:35 -0800397 (SearchEditTextLayout) actionBar.getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700398 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
399
Andrew Leee3f59a82015-02-26 12:06:35 -0800400 mActionBarController = new ActionBarController(this, searchEditTextLayout);
401
Andrew Lee04675a52014-06-05 18:36:20 -0700402 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700403 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700404 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
405 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
406 .setOnClickListener(mSearchViewOnClickListener);
407 searchEditTextLayout.findViewById(R.id.search_box_start_search)
408 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700409 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700410 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700411 @Override
412 public void onBackButtonClicked() {
413 onBackPressed();
414 }
Andrew Lee6324f702015-06-16 14:45:58 -0700415
416 @Override
417 public void onSearchViewClicked() {
418 // Hide FAB, as the keyboard is shown.
419 mFloatingActionButtonController.scaleOut();
420 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700421 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700422
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700423 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700424 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700425
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700426 final View floatingActionButtonContainer = findViewById(
427 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700428 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
429 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700430 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700431 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700432
Andrew Lee04675a52014-06-05 18:36:20 -0700433 ImageButton optionsMenuButton =
434 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700435 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700436 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
437 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700438
Yorke Leed5c512a2015-01-30 14:42:53 -0800439 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
440 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700441 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800442 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000443 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800444 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700445 } else {
446 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700447 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
448 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700449 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700450 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700451 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700452 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700453
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700454 final boolean isLayoutRtl = DialerUtils.isRtl();
455 if (mIsLandscape) {
456 mSlideIn = AnimationUtils.loadAnimation(this,
457 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
458 mSlideOut = AnimationUtils.loadAnimation(this,
459 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
460 } else {
461 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
462 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
463 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700464
Yorke Lee2fec47b2014-08-05 18:16:27 -0700465 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
466 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
467
Yorke Lee6c450eb2015-05-08 09:45:47 -0700468 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700469 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700470
Yorke Leef614f6f2014-10-03 10:44:19 -0700471 mParentLayout = (FrameLayout) findViewById(R.id.dialtacts_mainlayout);
472 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700473 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700474 new ViewTreeObserver.OnGlobalLayoutListener() {
475 @Override
476 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700477 final ViewTreeObserver observer =
478 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700479 if (!observer.isAlive()) {
480 return;
481 }
482 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700483 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700484 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700485 mFloatingActionButtonController.align(
486 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700487 }
488 });
Andrew Lee0c667702014-05-12 14:31:45 -0700489
Yorke Leef5554ce2015-01-27 14:46:02 -0800490 Trace.endSection();
491
492 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700493 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700494 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800495 Trace.endSection();
496 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700497 }
498
499 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700500 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800501 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700502 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700503
Yorke Lee19e68ca2014-10-28 11:51:40 -0700504 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700505 if (mFirstLaunch) {
506 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700507 } else if (!phoneIsInUse() && mInCallDialpadUp) {
508 hideDialpadFragment(false, true);
509 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700510 } else if (mShowDialpadOnResume) {
511 showDialpadFragment(false);
512 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700513 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800514
515 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
516 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
517 // shown until onResume has completed. Active the search UI and set the search term now.
518 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
519 mActionBarController.onSearchBoxTapped();
520 mSearchView.setText(mVoiceSearchQuery);
521 mVoiceSearchQuery = null;
522 }
523
Yorke Lee98702de2013-08-06 12:03:32 -0700524 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800525
526 if (mIsRestarting) {
527 // This is only called when the activity goes from resumed -> paused -> resumed, so it
528 // will not cause an extra view to be sent out on rotation
529 if (mIsDialpadShown) {
530 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
531 }
532 mIsRestarting = false;
533 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700534
Yorke Lee53a22302014-04-29 18:13:46 -0700535 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700536 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700537 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700538
539 if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
540 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
541 if (index < mListsFragment.getTabCount()) {
542 mListsFragment.showTab(index);
543 }
Nancy Chen019713e2015-07-06 18:25:24 -0700544 } else if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
545 mListsFragment.showTab(ListsFragment.TAB_INDEX_RECENTS);
Nancy Chend5de03b2015-07-01 09:20:45 -0700546 }
547
Yorke Leef5554ce2015-01-27 14:46:02 -0800548 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700549 }
550
551 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800552 protected void onRestart() {
553 super.onRestart();
554 mIsRestarting = true;
555 }
556
557 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700558 protected void onPause() {
559 if (mClearSearchOnPause) {
560 hideDialpadAndSearchUi();
561 mClearSearchOnPause = false;
562 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700563 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
564 commitDialpadFragmentHide();
565 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700566 super.onPause();
567 }
568
569 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700570 protected void onSaveInstanceState(Bundle outState) {
571 super.onSaveInstanceState(outState);
572 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700573 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
574 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700575 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700576 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700577 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700578 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700579 }
580
581 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700582 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700583 if (fragment instanceof DialpadFragment) {
584 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800585 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700586 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
587 transaction.hide(mDialpadFragment);
588 transaction.commit();
589 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700590 } else if (fragment instanceof SmartDialSearchFragment) {
591 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700592 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700593 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700594 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700595 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700596 } else if (fragment instanceof ListsFragment) {
597 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700598 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700599 }
Yorke Leec3766332013-07-31 11:13:16 -0700600 }
601
Alon Albertb453e5b2013-09-10 15:54:23 -0700602 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700603 final Intent intent = new Intent(this, DialerSettingsActivity.class);
604 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700605 }
606
Chiao Cheng94b10b52012-08-17 16:59:12 -0700607 @Override
608 public void onClick(View view) {
609 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700610 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700611 if (mListsFragment.getCurrentTabIndex()
612 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700613 DialerUtils.startActivityWithErrorToast(
614 this,
615 IntentUtil.getNewContactIntent(),
616 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700617 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700618 mInCallDialpadUp = false;
619 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700620 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700621 break;
Yorke Leec3766332013-07-31 11:13:16 -0700622 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800623 try {
624 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
625 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
626 } catch (ActivityNotFoundException e) {
627 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
628 Toast.LENGTH_SHORT).show();
629 }
Yorke Leec3766332013-07-31 11:13:16 -0700630 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700631 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700632 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700633 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700634 default: {
635 Log.wtf(TAG, "Unexpected onClick event from " + view);
636 break;
637 }
638 }
639 }
640
Yorke Leec3766332013-07-31 11:13:16 -0700641 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700642 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700643 switch (item.getItemId()) {
644 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700645 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
646 // CONTENT_TYPE, so that we always open our call log from our dialer
647 final Intent intent = new Intent(this, CallLogActivity.class);
648 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700649 break;
650 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700651 DialerUtils.startActivityWithErrorToast(
652 this,
653 IntentUtil.getNewContactIntent(),
654 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700655 break;
656 case R.id.menu_import_export:
657 // We hard-code the "contactsAreAvailable" argument because doing it properly would
658 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
659 // now in Dialtacts for (potential) performance reasons. Compare with how it is
660 // done in {@link PeopleActivity}.
661 ImportExportDialogFragment.show(getFragmentManager(), true,
662 DialtactsActivity.class);
663 return true;
664 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700665 ClearFrequentsDialog.show(getFragmentManager());
666 return true;
667 case R.id.menu_call_settings:
668 handleMenuSettings();
669 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700670 }
671 return false;
672 }
673
674 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700675 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
676 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
677 if (resultCode == RESULT_OK) {
678 final ArrayList<String> matches = data.getStringArrayListExtra(
679 RecognizerIntent.EXTRA_RESULTS);
680 if (matches.size() > 0) {
681 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800682 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700683 } else {
684 Log.e(TAG, "Voice search - nothing heard");
685 }
686 } else {
687 Log.e(TAG, "Voice search failed");
688 }
689 }
690 super.onActivityResult(requestCode, resultCode, data);
691 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700692
Andrew Lee2a58ab82014-05-15 02:16:04 -0700693 /**
694 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
695 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
696 * @see #onDialpadShown
697 */
Yorke Leec3766332013-07-31 11:13:16 -0700698 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700699 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700700 return;
701 }
702 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800703
Yorke Lee7ee5c422014-11-04 10:29:32 -0800704 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700705
Chiao Cheng94b10b52012-08-17 16:59:12 -0700706 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800707 if (mDialpadFragment == null) {
708 mDialpadFragment = new DialpadFragment();
709 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
710 } else {
711 ft.show(mDialpadFragment);
712 }
713
714 mDialpadFragment.setAnimate(animate);
715 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700716 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700717
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700718 if (animate) {
719 mFloatingActionButtonController.scaleOut();
720 } else {
721 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700722 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700723 }
Yorke Lee5253be02014-05-21 18:50:03 -0700724 mActionBarController.onDialpadUp();
725
Yorke Lee6c450eb2015-05-08 09:45:47 -0700726 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700727 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728
Andrew Lee2a58ab82014-05-15 02:16:04 -0700729 /**
730 * Callback from child DialpadFragment when the dialpad is shown.
731 */
732 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800733 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700734 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700735 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700736 } else {
737 mDialpadFragment.setYFraction(0);
738 }
739
Andrew Lee2a58ab82014-05-15 02:16:04 -0700740 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700741 }
742
743 /**
744 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
745 * a callback after the hide animation ends.
746 * @see #commitDialpadFragmentHide
747 */
Yorke Leeca195042013-09-25 16:29:38 -0700748 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700749 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700750 return;
751 }
Yorke Leef6673d32013-08-23 15:34:17 -0700752 if (clearDialpad) {
753 mDialpadFragment.clearDialpad();
754 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700755 if (!mIsDialpadShown) {
756 return;
Yorke Leec3766332013-07-31 11:13:16 -0700757 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700758 mIsDialpadShown = false;
759 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800760 mListsFragment.setUserVisibleHint(true);
761 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700762
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700763 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700764
Andrew Lee6324f702015-06-16 14:45:58 -0700765 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700766 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700767 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700768 } else {
769 commitDialpadFragmentHide();
770 }
771
Yorke Lee5253be02014-05-21 18:50:03 -0700772 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700773
Andrew Leed4cde832014-05-16 15:56:48 -0700774 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700775 if (TextUtils.isEmpty(mSearchQuery)) {
776 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700777 }
Andrew Leed4cde832014-05-16 15:56:48 -0700778 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700779 }
780
781 /**
782 * Finishes hiding the dialpad fragment after any animations are completed.
783 */
784 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800785 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700786 final FragmentTransaction ft = getFragmentManager().beginTransaction();
787 ft.hide(mDialpadFragment);
788 ft.commit();
789 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700790 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700791 }
792
Andrew Lee2a58ab82014-05-15 02:16:04 -0700793 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700794 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700795 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700796 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700797 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700798 fragment = mRegularSearchFragment;
799 }
800 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700801 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700802 }
803 }
804
Yorke Lee5253be02014-05-21 18:50:03 -0700805 @Override
806 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700807 return mInDialpadSearch || mInRegularSearch;
808 }
809
Yorke Lee5253be02014-05-21 18:50:03 -0700810 @Override
811 public boolean hasSearchQuery() {
812 return !TextUtils.isEmpty(mSearchQuery);
813 }
814
815 @Override
816 public boolean shouldShowActionBar() {
817 return mListsFragment.shouldShowActionBar();
818 }
819
Yorke Leeb207f8a2013-08-29 18:51:06 -0700820 private void setNotInSearchUi() {
821 mInDialpadSearch = false;
822 mInRegularSearch = false;
823 }
824
Yorke Lee311969c2013-08-26 06:31:11 -0700825 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700826 if (mIsDialpadShown) {
827 hideDialpadFragment(false, true);
828 } else {
829 exitSearchUi();
830 }
Yorke Lee311969c2013-08-26 06:31:11 -0700831 }
832
Yorke Lee53a22302014-04-29 18:13:46 -0700833 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700834 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
835 if (canIntentBeHandled(voiceIntent)) {
836 mVoiceSearchButton.setVisibility(View.VISIBLE);
837 mVoiceSearchButton.setOnClickListener(this);
838 } else {
839 mVoiceSearchButton.setVisibility(View.GONE);
840 }
841 }
842
Andrew Lee467de3d2015-03-17 15:47:24 -0700843 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700844 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
845 popupMenu.inflate(R.menu.dialtacts_options);
846 popupMenu.setOnMenuItemClickListener(this);
847 return popupMenu;
848 }
849
Yorke Lee86e21f72014-04-02 16:49:38 -0700850 @Override
851 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700852 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700853 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700854 mPendingSearchViewQuery = null;
855 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700856 if (mActionBarController != null) {
857 mActionBarController.restoreActionBarOffset();
858 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700859 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700860 }
861
Chiao Cheng94b10b52012-08-17 16:59:12 -0700862 /**
863 * Returns true if the intent is due to hitting the green send key (hardware call button:
864 * KEYCODE_CALL) while in a call.
865 *
866 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700867 * @return true if the intent is due to hitting the green send key while in a call
868 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700869 private boolean isSendKeyWhileInCall(Intent intent) {
870 // If there is a call in progress and the user launched the dialer by hitting the call
871 // button, go straight to the in-call screen.
872 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700873
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700874 if (callKey) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700875 getTelecomManager().showInCallScreen(false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700876 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700877 }
878
879 return false;
880 }
881
882 /**
883 * Sets the current tab based on the intent's request type
884 *
885 * @param intent Intent that contains information about which tab should be selected
886 */
Yorke Leec3766332013-07-31 11:13:16 -0700887 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700888 // 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 -0700889 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700890 finish();
891 return;
892 }
893
Yorke Leed5c512a2015-01-30 14:42:53 -0800894 final boolean phoneIsInUse = phoneIsInUse();
895 if (phoneIsInUse || (intent.getData() != null && isDialIntent(intent))) {
896 showDialpadFragment(false);
897 mDialpadFragment.setStartedFromNewIntent(true);
898 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
899 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700900 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700901 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700902 }
903
904 @Override
905 public void onNewIntent(Intent newIntent) {
906 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700907
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
Chiao Cheng94b10b52012-08-17 16:59:12 -0700911 invalidateOptionsMenu();
912 }
913
914 /** Returns true if the given intent contains a phone number to populate the dialer with */
915 private boolean isDialIntent(Intent intent) {
916 final String action = intent.getAction();
917 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
918 return true;
919 }
920 if (Intent.ACTION_VIEW.equals(action)) {
921 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700922 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700923 return true;
924 }
925 }
926 return false;
927 }
928
929 /**
930 * Returns an appropriate call origin for this Activity. May return null when no call origin
931 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
932 * for remembering the tab in which the user made a phone call, so the external app's DIAL
933 * request should not be counted.)
934 */
935 public String getCallOrigin() {
936 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
937 }
938
Chiao Cheng94b10b52012-08-17 16:59:12 -0700939 /**
Yorke Leec3766332013-07-31 11:13:16 -0700940 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700941 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700942 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700943 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700944 // Weird race condition where fragment is doing work after the activity is destroyed
945 // due to talkback being on (b/10209937). Just return since we can't do any
946 // constructive here.
947 return;
948 }
949
Yorke Leeef2b7382013-08-09 17:39:25 -0700950 if (DEBUG) {
951 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
952 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700953
Yorke Leec3766332013-07-31 11:13:16 -0700954 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700955 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700956 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700957 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700958 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700959 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700960
Yorke Leeb207f8a2013-08-29 18:51:06 -0700961 final String tag;
962 if (smartDialSearch) {
963 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
964 } else {
965 tag = TAG_REGULAR_SEARCH_FRAGMENT;
966 }
967 mInDialpadSearch = smartDialSearch;
968 mInRegularSearch = !smartDialSearch;
969
Andrew Lee6324f702015-06-16 14:45:58 -0700970 mFloatingActionButtonController.scaleOut();
971
Andrew Lee752956e2014-05-15 11:43:38 -0700972 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700973 if (animate) {
974 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
975 } else {
976 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
977 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700978 if (fragment == null) {
979 if (smartDialSearch) {
980 fragment = new SmartDialSearchFragment();
981 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700982 fragment = new RegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -0700983 fragment.setOnTouchListener(new View.OnTouchListener() {
984 @Override
985 public boolean onTouch(View v, MotionEvent event) {
986 // Show the FAB when the user touches the lists fragment and the soft
987 // keyboard is hidden.
988 showFabInSearchUi();
989 return false;
990 }
991 });
Yorke Leeef2b7382013-08-09 17:39:25 -0700992 }
Andrew Leeb1903842014-05-15 16:11:24 -0700993 transaction.add(R.id.dialtacts_frame, fragment, tag);
994 } else {
995 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700996 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700997 // DialtactsActivity will provide the options menu
998 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700999 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001000 if (!smartDialSearch) {
1001 fragment.setQueryString(query, false /* delaySelection */);
1002 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001003 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001004
Yorke Lee6c450eb2015-05-08 09:45:47 -07001005 if (animate) {
1006 mListsFragment.getView().animate().alpha(0).withLayer();
1007 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001008 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001009 }
1010
1011 /**
Yorke Leec3766332013-07-31 11:13:16 -07001012 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001013 */
Yorke Leec3766332013-07-31 11:13:16 -07001014 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001015 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001016 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001017 return;
1018 }
Andrew Leeb1903842014-05-15 16:11:24 -07001019
Andrew Leeb1903842014-05-15 16:11:24 -07001020 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001021
1022 if (mDialpadFragment != null) {
1023 mDialpadFragment.clearDialpad();
1024 }
1025
Yorke Leeb207f8a2013-08-29 18:51:06 -07001026 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001027
Andrew Lee6324f702015-06-16 14:45:58 -07001028 // Restore the FAB for the lists fragment.
1029 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1030 mFloatingActionButtonController.setVisible(false);
1031 }
1032 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1033 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1034 onPageSelected(mListsFragment.getCurrentTabIndex());
1035
Andrew Leeb1903842014-05-15 16:11:24 -07001036 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001037 if (mSmartDialSearchFragment != null) {
1038 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001039 }
Andrew Leeb1903842014-05-15 16:11:24 -07001040 if (mRegularSearchFragment != null) {
1041 transaction.remove(mRegularSearchFragment);
1042 }
1043 transaction.commit();
1044
1045 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001046
1047 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001048 // If the dialpad fragment wasn't previously visible, then send a screen view because
1049 // we are exiting regular search. Otherwise, the screen view will be sent by
1050 // {@link #hideDialpadFragment}.
1051 mListsFragment.sendScreenViewForCurrentPosition();
1052 mListsFragment.setUserVisibleHint(true);
1053 }
1054
Yorke Lee5253be02014-05-21 18:50:03 -07001055 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001056 }
1057
Chiao Cheng94b10b52012-08-17 16:59:12 -07001058 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001059 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001060 if (mStateSaved) {
1061 return;
1062 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001063 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001064 if (TextUtils.isEmpty(mSearchQuery) ||
1065 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1066 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001067 exitSearchUi();
1068 }
Yorke Leef6673d32013-08-23 15:34:17 -07001069 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001070 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001071 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001072 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001073 } else {
1074 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001075 }
Yorke Leec3766332013-07-31 11:13:16 -07001076 }
1077
Yorke Lee81a313d2015-06-01 14:53:59 -07001078 private void maybeEnterSearchUi() {
1079 if (!isInSearchUi()) {
1080 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1081 }
1082 }
1083
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001084 /**
1085 * @return True if the search UI was exited, false otherwise
1086 */
1087 private boolean maybeExitSearchUi() {
1088 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1089 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001090 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001091 return true;
1092 }
1093 return false;
1094 }
1095
Andrew Lee6324f702015-06-16 14:45:58 -07001096 private void showFabInSearchUi() {
1097 mFloatingActionButtonController.changeIcon(
1098 getResources().getDrawable(R.drawable.fab_ic_dial),
1099 getResources().getString(R.string.action_menu_dialpad_button));
1100 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1101 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1102 }
1103
Yorke Leec3766332013-07-31 11:13:16 -07001104 @Override
1105 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001106 if (mSmartDialSearchFragment != null) {
1107 mSmartDialSearchFragment.setAddToContactNumber(query);
1108 }
Yorke Leec3766332013-07-31 11:13:16 -07001109 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1110 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001111
1112 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001113 if (DEBUG) {
1114 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1115 }
1116 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1117 // This callback can happen if the dialpad fragment is recreated because of
1118 // activity destruction. In that case, don't update the search view because
1119 // that would bring the user back to the search fragment regardless of the
1120 // previous state of the application. Instead, just return here and let the
1121 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001122 if (!TextUtils.isEmpty(normalizedQuery)) {
1123 mPendingSearchViewQuery = normalizedQuery;
1124 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001125 return;
1126 }
Yorke Lee53a22302014-04-29 18:13:46 -07001127 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001128 }
Santos Cordon83131712015-04-16 21:25:35 -07001129
1130 try {
1131 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1132 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1133 }
1134 } catch (Exception ignored) {
1135 // Skip any exceptions for this piece of code
1136 }
Yorke Lee575ae382015-07-16 11:36:07 -07001137 }
Santos Cordon83131712015-04-16 21:25:35 -07001138
Yorke Lee575ae382015-07-16 11:36:07 -07001139 @Override
1140 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1141 if (mInDialpadSearch && mSmartDialSearchFragment != null
1142 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1143 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1144 return true;
1145 }
1146 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001147 }
Yorke Leec3766332013-07-31 11:13:16 -07001148
1149 @Override
1150 public void onListFragmentScrollStateChange(int scrollState) {
1151 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001152 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001153 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001154 }
1155 }
1156
1157 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001158 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001159 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001160 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1161 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001162 }
1163
Yorke Leec3766332013-07-31 11:13:16 -07001164 private boolean phoneIsInUse() {
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001165 return getTelecomManager().isInCall();
Yorke Leec3766332013-07-31 11:13:16 -07001166 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001167
Yorke Leeda0f9042013-12-05 14:25:51 -08001168 private boolean canIntentBeHandled(Intent intent) {
1169 final PackageManager packageManager = getPackageManager();
1170 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1171 PackageManager.MATCH_DEFAULT_ONLY);
1172 return resolveInfo != null && resolveInfo.size() > 0;
1173 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001174
Yorke Lee86e21f72014-04-02 16:49:38 -07001175 /**
1176 * Called when the user has long-pressed a contact tile to start a drag operation.
1177 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001178 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001179 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001180 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001181 }
1182
1183 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001184 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1185 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001186
Yorke Lee86e21f72014-04-02 16:49:38 -07001187 /**
1188 * Called when the user has released a contact tile after long-pressing it.
1189 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001190 @Override
1191 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001192 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001193 }
1194
1195 @Override
1196 public void onDroppedOnRemove() {}
1197
1198 /**
Yorke Leed999b712014-04-23 15:10:58 -07001199 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001200 * once it has been attached to the activity.
1201 */
1202 @Override
1203 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001204 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001205 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001206 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001207
Yorke Lee38019af2015-07-14 17:05:38 -07001208 /**
1209 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1210 */
1211 @Override
1212 public void showAllContactsTab() {
1213 if (mListsFragment != null) {
1214 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1215 }
1216 }
1217
1218 /**
1219 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1220 */
1221 @Override
1222 public void showDialpad() {
1223 showDialpadFragment(true);
1224 }
1225
Yorke Leee00c9fe2014-04-12 12:42:06 -07001226 @Override
1227 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001228 // Specify call-origin so that users will see the previous tab instead of
1229 // CallLog screen (search UI will be automatically exited).
1230 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001231 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001232 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001233 }
1234
1235 @Override
1236 public void onCallNumberDirectly(String phoneNumber) {
Andrew Leefc1fed72014-07-02 11:05:53 -07001237 onCallNumberDirectly(phoneNumber, false /* isVideoCall */);
1238 }
1239
1240 @Override
1241 public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001242 if (phoneNumber == null) {
1243 // Invalid phone number, but let the call go through so that InCallUI can show
1244 // an error message.
1245 phoneNumber = "";
1246 }
Andrew Leed2a683f2014-07-08 15:54:50 -07001247 Intent intent = isVideoCall ?
Andrew Lee247df6e2015-05-12 19:09:00 -07001248 IntentUtil.getVideoCallIntent(phoneNumber, getCallOrigin()) :
1249 IntentUtil.getCallIntent(phoneNumber, getCallOrigin());
Yorke Lee7d20f822014-06-19 17:09:33 -07001250 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001251 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001252 }
1253
1254 @Override
1255 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001256 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001257 }
1258
1259 @Override
1260 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001261 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001262 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001263
Yorke Leecc4660d2014-04-24 11:22:57 -07001264 @Override
1265 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001266 int tabIndex = mListsFragment.getCurrentTabIndex();
1267
1268 // Scroll the button from center to end when moving from the Speed Dial to Recents tab.
1269 // In RTL, scroll when the current tab is Recents instead of Speed Dial, because the order
1270 // of the tabs is reversed and the ViewPager returns the left tab position during scroll.
1271 boolean isRtl = DialerUtils.isRtl();
1272 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1273 mFloatingActionButtonController.onPageScrolled(positionOffset);
1274 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_RECENTS && !mIsLandscape) {
1275 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1276 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001277 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001278 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001279 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001280
Andrew Lee432d4b52015-05-20 16:52:00 -07001281 @Override
1282 public void onPageSelected(int position) {
1283 int tabIndex = mListsFragment.getCurrentTabIndex();
1284 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001285 mFloatingActionButtonController.changeIcon(
1286 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1287 getResources().getString(R.string.search_shortcut_create_new_contact));
1288 } else {
1289 mFloatingActionButtonController.changeIcon(
1290 getResources().getDrawable(R.drawable.fab_ic_dial),
1291 getResources().getString(R.string.action_menu_dialpad_button));
1292 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001293 }
1294
1295 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001296 public void onPageScrollStateChanged(int state) {
1297 }
1298
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001299 private TelecomManager getTelecomManager() {
1300 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordond15629d2014-07-07 15:15:29 -07001301 }
1302
Yorke Lee5253be02014-05-21 18:50:03 -07001303 @Override
1304 public boolean isActionBarShowing() {
1305 return mActionBarController.isActionBarShowing();
1306 }
1307
Yorke Leec98a5bb2014-10-28 16:12:05 -07001308 @Override
1309 public ActionBarController getActionBarController() {
1310 return mActionBarController;
1311 }
1312
Yorke Lee6bc67162015-06-27 14:03:25 -07001313 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001314 public boolean isDialpadShown() {
1315 return mIsDialpadShown;
1316 }
1317
Yorke Lee5253be02014-05-21 18:50:03 -07001318 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001319 public int getDialpadHeight() {
1320 if (mDialpadFragment != null) {
1321 return mDialpadFragment.getDialpadHeight();
1322 }
1323 return 0;
1324 }
1325
1326 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001327 public int getActionBarHideOffset() {
1328 return getActionBar().getHideOffset();
1329 }
1330
1331 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001332 public void setActionBarHideOffset(int offset) {
1333 getActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001334 }
1335
1336 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001337 public int getActionBarHeight() {
1338 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001339 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001340
Andrew Lee6324f702015-06-16 14:45:58 -07001341 private int getFabAlignment() {
1342 if (!mIsLandscape && !isInSearchUi() &&
1343 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1344 return FloatingActionButtonController.ALIGN_MIDDLE;
1345 }
1346 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001347 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001348}