blob: fc0f1fb242ceee18aadc7e86a22ea6227c3ef0ff [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";
136
Yorke Leec3766332013-07-31 11:13:16 -0700137 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700138
Andrew Lee6324f702015-06-16 14:45:58 -0700139 private static final int FAB_SCALE_IN_DELAY_MS = 300;
140
Yorke Leef614f6f2014-10-03 10:44:19 -0700141 private FrameLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700142
Yorke Leec3766332013-07-31 11:13:16 -0700143 /**
Yorke Leec3766332013-07-31 11:13:16 -0700144 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700145 */
Evan Charlton72d66252014-11-07 16:23:47 -0800146 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700147
148 /**
149 * Fragment for searching phone numbers using the alphanumeric keyboard.
150 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700151 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700152
153 /**
154 * Fragment for searching phone numbers using the dialpad.
155 */
156 private SmartDialSearchFragment mSmartDialSearchFragment;
157
Yorke Leee00c9fe2014-04-12 12:42:06 -0700158 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700159 * Animation that slides in.
160 */
161 private Animation mSlideIn;
162
163 /**
164 * Animation that slides out.
165 */
166 private Animation mSlideOut;
167
Yorke Lee6c450eb2015-05-08 09:45:47 -0700168 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
169 @Override
170 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700171 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700172 }
173 };
174
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700175 /**
176 * Listener for after slide out animation completes on dialer fragment.
177 */
178 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
179 @Override
180 public void onAnimationEnd(Animation animation) {
181 commitDialpadFragmentHide();
182 }
183 };
184
185 /**
Yorke Leee00c9fe2014-04-12 12:42:06 -0700186 * Fragment containing the speed dial list, recents list, and all contacts list.
187 */
188 private ListsFragment mListsFragment;
189
Yorke Lee19e68ca2014-10-28 11:51:40 -0700190 /**
191 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
192 * be commited.
193 */
194 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800195 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700196 private boolean mInDialpadSearch;
197 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700198 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700199 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700200 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700201
Yorke Leeef2b7382013-08-09 17:39:25 -0700202 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700203 * Whether or not the device is in landscape orientation.
204 */
205 private boolean mIsLandscape;
206
207 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700208 * True if the dialpad is only temporarily showing due to being in call
209 */
210 private boolean mInCallDialpadUp;
211
212 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700213 * True when this activity has been launched for the first time.
214 */
Yorke Lee98702de2013-08-06 12:03:32 -0700215 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700216
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700217 /**
218 * Search query to be applied to the SearchView in the ActionBar once
219 * onCreateOptionsMenu has been called.
220 */
221 private String mPendingSearchViewQuery;
222
Yorke Lee74edcdc2014-07-11 16:15:08 -0700223 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700224 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700225 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700226
Yorke Leeef2b7382013-08-09 17:39:25 -0700227 private String mSearchQuery;
228
Yorke Leefce269a2013-08-12 11:56:04 -0700229 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700230 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700231 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700232
Sai Cheemalapati41460652014-06-02 21:05:39 -0700233 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700234
Andrew Lee2423a2f2014-05-29 14:14:45 -0700235 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700236
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800237 /**
238 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
239 * {@link #onResume()} to populate the search box.
240 */
241 private String mVoiceSearchQuery;
242
Andrew Lee467de3d2015-03-17 15:47:24 -0700243 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700244 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700245 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700246 }
247
248 @Override
249 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700250 final boolean hasContactsPermission =
251 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700252 final Menu menu = getMenu();
253 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700254 clearFrequents.setVisible(mListsFragment != null &&
255 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700256 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
257
258 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
259 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700260
261 menu.findItem(R.id.menu_history).setVisible(
262 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700263 super.show();
264 }
265 }
266
Chiao Cheng94b10b52012-08-17 16:59:12 -0700267 /**
Yorke Lee28266192014-05-01 10:05:52 -0700268 * Listener that listens to drag events and sends their x and y coordinates to a
269 * {@link DragDropController}.
270 */
271 private class LayoutOnDragListener implements OnDragListener {
272 @Override
273 public boolean onDrag(View v, DragEvent event) {
274 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700275 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700276 }
277 return true;
278 }
279 }
280
281 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700282 * Listener used to send search queries to the phone search fragment.
283 */
Yorke Lee53a22302014-04-29 18:13:46 -0700284 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700285 @Override
286 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
287 }
Yorke Leec3766332013-07-31 11:13:16 -0700288
Andrew Lee99a570c2014-05-15 14:18:50 -0700289 @Override
290 public void onTextChanged(CharSequence s, int start, int before, int count) {
291 final String newText = s.toString();
292 if (newText.equals(mSearchQuery)) {
293 // If the query hasn't changed (perhaps due to activity being destroyed
294 // and restored, or user launching the same DIAL intent twice), then there is
295 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700296 return;
297 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700298 if (DEBUG) {
299 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700300 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700301 }
Yorke Lee710709d2014-05-29 07:18:42 -0700302 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700303
Andrew Lee90374a72014-05-16 15:01:09 -0700304 // Show search fragment only when the query string is changed to non-empty text.
305 if (!TextUtils.isEmpty(newText)) {
306 // Call enterSearchUi only if we are switching search modes, or showing a search
307 // fragment for the first time.
308 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
309 (!mIsDialpadShown && mInRegularSearch);
310 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700311 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700312 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700313 }
314
Andrew Leea8515422014-06-13 16:53:54 -0700315 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700316 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700317 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700318 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700319 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700320 }
321
322 @Override
323 public void afterTextChanged(Editable s) {
324 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700325 };
326
Andrew Leeb1903842014-05-15 16:11:24 -0700327
328 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700329 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700330 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700331 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700332 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700333 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700334 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700335 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700336 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
337 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700338 }
Andrew Leeb1903842014-05-15 16:11:24 -0700339 }
340 };
341
342 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700343 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700344 */
345 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
346 @Override
347 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700348 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
349 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
350 // If the search term is empty, close the search UI.
351 maybeExitSearchUi();
352 } else {
353 // If the search term is not empty, show the dialpad fab.
354 showFabInSearchUi();
355 }
Andrew Leeb1903842014-05-15 16:11:24 -0700356 }
357 return false;
358 }
359 };
360
Chiao Cheng94b10b52012-08-17 16:59:12 -0700361 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700362 public boolean dispatchTouchEvent(MotionEvent ev) {
363 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
364 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
365 }
366 return super.dispatchTouchEvent(ev);
367
368 }
369
370 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700371 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800372 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700373 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700374
Yorke Lee98702de2013-08-06 12:03:32 -0700375 mFirstLaunch = true;
376
Andrew Lee2423a2f2014-05-29 14:14:45 -0700377 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700378 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700379
Yorke Leef5554ce2015-01-27 14:46:02 -0800380 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700381 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800382 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800383 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700384
Yorke Leef5554ce2015-01-27 14:46:02 -0800385 Trace.beginSection(TAG + " setup Views");
Yorke Lee53a22302014-04-29 18:13:46 -0700386 final ActionBar actionBar = getActionBar();
387 actionBar.setCustomView(R.layout.search_edittext);
388 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700389 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700390
Andrew Lee04675a52014-06-05 18:36:20 -0700391 SearchEditTextLayout searchEditTextLayout =
Andrew Leee3f59a82015-02-26 12:06:35 -0800392 (SearchEditTextLayout) actionBar.getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700393 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
394
Andrew Leee3f59a82015-02-26 12:06:35 -0800395 mActionBarController = new ActionBarController(this, searchEditTextLayout);
396
Andrew Lee04675a52014-06-05 18:36:20 -0700397 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700398 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700399 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
400 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
401 .setOnClickListener(mSearchViewOnClickListener);
402 searchEditTextLayout.findViewById(R.id.search_box_start_search)
403 .setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700404 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700405 @Override
406 public void onBackButtonClicked() {
407 onBackPressed();
408 }
Andrew Lee6324f702015-06-16 14:45:58 -0700409
410 @Override
411 public void onSearchViewClicked() {
412 // Hide FAB, as the keyboard is shown.
413 mFloatingActionButtonController.scaleOut();
414 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700415 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700416
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700417 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700418 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700419
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700420 final View floatingActionButtonContainer = findViewById(
421 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700422 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
423 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700424 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700425 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700426
Andrew Lee04675a52014-06-05 18:36:20 -0700427 ImageButton optionsMenuButton =
428 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700429 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700430 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
431 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700432
Yorke Leed5c512a2015-01-30 14:42:53 -0800433 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
434 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700435 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800436 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000437 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800438 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700439 } else {
440 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700441 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
442 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700443 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700444 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700445 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700446 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700447
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700448 final boolean isLayoutRtl = DialerUtils.isRtl();
449 if (mIsLandscape) {
450 mSlideIn = AnimationUtils.loadAnimation(this,
451 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
452 mSlideOut = AnimationUtils.loadAnimation(this,
453 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
454 } else {
455 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
456 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
457 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700458
Yorke Lee2fec47b2014-08-05 18:16:27 -0700459 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
460 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
461
Yorke Lee6c450eb2015-05-08 09:45:47 -0700462 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700463 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700464
Yorke Leef614f6f2014-10-03 10:44:19 -0700465 mParentLayout = (FrameLayout) findViewById(R.id.dialtacts_mainlayout);
466 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700467 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700468 new ViewTreeObserver.OnGlobalLayoutListener() {
469 @Override
470 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700471 final ViewTreeObserver observer =
472 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700473 if (!observer.isAlive()) {
474 return;
475 }
476 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700477 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700478 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700479 mFloatingActionButtonController.align(
480 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700481 }
482 });
Andrew Lee0c667702014-05-12 14:31:45 -0700483
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700484 setupActivityOverlay();
485
Yorke Leef5554ce2015-01-27 14:46:02 -0800486 Trace.endSection();
487
488 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700489 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700490 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800491 Trace.endSection();
492 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700493 }
494
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700495 private void setupActivityOverlay() {
496 final View activityOverlay = findViewById(R.id.activity_overlay);
497 activityOverlay.setOnTouchListener(new OnTouchListener() {
498 @Override
499 public boolean onTouch(View v, MotionEvent event) {
500 if (!mIsDialpadShown) {
501 maybeExitSearchUi();
502 }
503 return false;
504 }
505 });
506 }
507
Chiao Cheng94b10b52012-08-17 16:59:12 -0700508 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700509 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800510 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700511 super.onResume();
Yorke Lee19e68ca2014-10-28 11:51:40 -0700512 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700513 if (mFirstLaunch) {
514 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700515 } else if (!phoneIsInUse() && mInCallDialpadUp) {
516 hideDialpadFragment(false, true);
517 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700518 } else if (mShowDialpadOnResume) {
519 showDialpadFragment(false);
520 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700521 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800522
523 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
524 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
525 // shown until onResume has completed. Active the search UI and set the search term now.
526 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
527 mActionBarController.onSearchBoxTapped();
528 mSearchView.setText(mVoiceSearchQuery);
529 mVoiceSearchQuery = null;
530 }
531
Yorke Lee98702de2013-08-06 12:03:32 -0700532 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800533
534 if (mIsRestarting) {
535 // This is only called when the activity goes from resumed -> paused -> resumed, so it
536 // will not cause an extra view to be sent out on rotation
537 if (mIsDialpadShown) {
538 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
539 }
540 mIsRestarting = false;
541 }
Yorke Lee53a22302014-04-29 18:13:46 -0700542 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700543 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700544 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Yorke Leef5554ce2015-01-27 14:46:02 -0800545 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700546 }
547
548 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800549 protected void onRestart() {
550 super.onRestart();
551 mIsRestarting = true;
552 }
553
554 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700555 protected void onPause() {
556 if (mClearSearchOnPause) {
557 hideDialpadAndSearchUi();
558 mClearSearchOnPause = false;
559 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700560 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
561 commitDialpadFragmentHide();
562 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700563 super.onPause();
564 }
565
566 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700567 protected void onSaveInstanceState(Bundle outState) {
568 super.onSaveInstanceState(outState);
569 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700570 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
571 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700572 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700573 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700574 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700575 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700576 }
577
578 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700579 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700580 if (fragment instanceof DialpadFragment) {
581 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800582 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700583 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
584 transaction.hide(mDialpadFragment);
585 transaction.commit();
586 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700587 } else if (fragment instanceof SmartDialSearchFragment) {
588 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700589 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700590 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700591 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700592 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700593 } else if (fragment instanceof ListsFragment) {
594 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700595 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700596 }
Yorke Leec3766332013-07-31 11:13:16 -0700597 }
598
Alon Albertb453e5b2013-09-10 15:54:23 -0700599 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700600 final Intent intent = new Intent(this, DialerSettingsActivity.class);
601 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700602 }
603
Chiao Cheng94b10b52012-08-17 16:59:12 -0700604 @Override
605 public void onClick(View view) {
606 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700607 case R.id.floating_action_button:
Andrew Lee432d4b52015-05-20 16:52:00 -0700608 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700609 DialerUtils.startActivityWithErrorToast(
610 this,
611 IntentUtil.getNewContactIntent(),
612 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700613 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700614 mInCallDialpadUp = false;
615 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700616 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700617 break;
Yorke Leec3766332013-07-31 11:13:16 -0700618 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800619 try {
620 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
621 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
622 } catch (ActivityNotFoundException e) {
623 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
624 Toast.LENGTH_SHORT).show();
625 }
Yorke Leec3766332013-07-31 11:13:16 -0700626 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700627 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700628 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700629 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700630 default: {
631 Log.wtf(TAG, "Unexpected onClick event from " + view);
632 break;
633 }
634 }
635 }
636
Yorke Leec3766332013-07-31 11:13:16 -0700637 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700638 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700639 switch (item.getItemId()) {
640 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700641 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
642 // CONTENT_TYPE, so that we always open our call log from our dialer
643 final Intent intent = new Intent(this, CallLogActivity.class);
644 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700645 break;
646 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700647 DialerUtils.startActivityWithErrorToast(
648 this,
649 IntentUtil.getNewContactIntent(),
650 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700651 break;
652 case R.id.menu_import_export:
653 // We hard-code the "contactsAreAvailable" argument because doing it properly would
654 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
655 // now in Dialtacts for (potential) performance reasons. Compare with how it is
656 // done in {@link PeopleActivity}.
657 ImportExportDialogFragment.show(getFragmentManager(), true,
658 DialtactsActivity.class);
659 return true;
660 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700661 ClearFrequentsDialog.show(getFragmentManager());
662 return true;
663 case R.id.menu_call_settings:
664 handleMenuSettings();
665 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700666 }
667 return false;
668 }
669
670 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700671 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
672 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
673 if (resultCode == RESULT_OK) {
674 final ArrayList<String> matches = data.getStringArrayListExtra(
675 RecognizerIntent.EXTRA_RESULTS);
676 if (matches.size() > 0) {
677 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800678 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700679 } else {
680 Log.e(TAG, "Voice search - nothing heard");
681 }
682 } else {
683 Log.e(TAG, "Voice search failed");
684 }
685 }
686 super.onActivityResult(requestCode, resultCode, data);
687 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700688
Andrew Lee2a58ab82014-05-15 02:16:04 -0700689 /**
690 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
691 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
692 * @see #onDialpadShown
693 */
Yorke Leec3766332013-07-31 11:13:16 -0700694 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700695 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700696 return;
697 }
698 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800699
Yorke Lee7ee5c422014-11-04 10:29:32 -0800700 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700701
Chiao Cheng94b10b52012-08-17 16:59:12 -0700702 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800703 if (mDialpadFragment == null) {
704 mDialpadFragment = new DialpadFragment();
705 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
706 } else {
707 ft.show(mDialpadFragment);
708 }
709
710 mDialpadFragment.setAnimate(animate);
711 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700712 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700713
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700714 if (animate) {
715 mFloatingActionButtonController.scaleOut();
716 } else {
717 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700718 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700719 }
Yorke Lee5253be02014-05-21 18:50:03 -0700720 mActionBarController.onDialpadUp();
721
Yorke Lee6c450eb2015-05-08 09:45:47 -0700722 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700723 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700724
Andrew Lee2a58ab82014-05-15 02:16:04 -0700725 /**
726 * Callback from child DialpadFragment when the dialpad is shown.
727 */
728 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800729 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700730 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700731 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700732 } else {
733 mDialpadFragment.setYFraction(0);
734 }
735
Andrew Lee2a58ab82014-05-15 02:16:04 -0700736 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700737 }
738
739 /**
740 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
741 * a callback after the hide animation ends.
742 * @see #commitDialpadFragmentHide
743 */
Yorke Leeca195042013-09-25 16:29:38 -0700744 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700745 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700746 return;
747 }
Yorke Leef6673d32013-08-23 15:34:17 -0700748 if (clearDialpad) {
749 mDialpadFragment.clearDialpad();
750 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700751 if (!mIsDialpadShown) {
752 return;
Yorke Leec3766332013-07-31 11:13:16 -0700753 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700754 mIsDialpadShown = false;
755 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800756 mListsFragment.setUserVisibleHint(true);
757 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700758
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700759 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700760
Andrew Lee6324f702015-06-16 14:45:58 -0700761 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700762 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700763 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700764 } else {
765 commitDialpadFragmentHide();
766 }
767
Yorke Lee5253be02014-05-21 18:50:03 -0700768 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700769
Andrew Leed4cde832014-05-16 15:56:48 -0700770 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700771 if (TextUtils.isEmpty(mSearchQuery)) {
772 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700773 }
Andrew Leed4cde832014-05-16 15:56:48 -0700774 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700775 }
776
777 /**
778 * Finishes hiding the dialpad fragment after any animations are completed.
779 */
780 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800781 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700782 final FragmentTransaction ft = getFragmentManager().beginTransaction();
783 ft.hide(mDialpadFragment);
784 ft.commit();
785 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700786 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700787 }
788
Andrew Lee2a58ab82014-05-15 02:16:04 -0700789 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700790 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700791 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700792 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700793 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700794 fragment = mRegularSearchFragment;
795 }
796 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700797 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700798 }
799 }
800
Yorke Lee5253be02014-05-21 18:50:03 -0700801 @Override
802 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700803 return mInDialpadSearch || mInRegularSearch;
804 }
805
Yorke Lee5253be02014-05-21 18:50:03 -0700806 @Override
807 public boolean hasSearchQuery() {
808 return !TextUtils.isEmpty(mSearchQuery);
809 }
810
811 @Override
812 public boolean shouldShowActionBar() {
813 return mListsFragment.shouldShowActionBar();
814 }
815
Yorke Leeb207f8a2013-08-29 18:51:06 -0700816 private void setNotInSearchUi() {
817 mInDialpadSearch = false;
818 mInRegularSearch = false;
819 }
820
Yorke Lee311969c2013-08-26 06:31:11 -0700821 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700822 if (mIsDialpadShown) {
823 hideDialpadFragment(false, true);
824 } else {
825 exitSearchUi();
826 }
Yorke Lee311969c2013-08-26 06:31:11 -0700827 }
828
Yorke Lee53a22302014-04-29 18:13:46 -0700829 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700830 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
831 if (canIntentBeHandled(voiceIntent)) {
832 mVoiceSearchButton.setVisibility(View.VISIBLE);
833 mVoiceSearchButton.setOnClickListener(this);
834 } else {
835 mVoiceSearchButton.setVisibility(View.GONE);
836 }
837 }
838
Andrew Lee467de3d2015-03-17 15:47:24 -0700839 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700840 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
841 popupMenu.inflate(R.menu.dialtacts_options);
842 popupMenu.setOnMenuItemClickListener(this);
843 return popupMenu;
844 }
845
Yorke Lee86e21f72014-04-02 16:49:38 -0700846 @Override
847 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700848 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700849 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700850 mPendingSearchViewQuery = null;
851 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700852 if (mActionBarController != null) {
853 mActionBarController.restoreActionBarOffset();
854 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700855 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700856 }
857
Chiao Cheng94b10b52012-08-17 16:59:12 -0700858 /**
859 * Returns true if the intent is due to hitting the green send key (hardware call button:
860 * KEYCODE_CALL) while in a call.
861 *
862 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700863 * @return true if the intent is due to hitting the green send key while in a call
864 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700865 private boolean isSendKeyWhileInCall(Intent intent) {
866 // If there is a call in progress and the user launched the dialer by hitting the call
867 // button, go straight to the in-call screen.
868 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700869
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700870 if (callKey) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700871 getTelecomManager().showInCallScreen(false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700872 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700873 }
874
875 return false;
876 }
877
878 /**
879 * Sets the current tab based on the intent's request type
880 *
881 * @param intent Intent that contains information about which tab should be selected
882 */
Yorke Leec3766332013-07-31 11:13:16 -0700883 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700884 // 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 -0700885 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700886 finish();
887 return;
888 }
889
Yorke Leed5c512a2015-01-30 14:42:53 -0800890 final boolean phoneIsInUse = phoneIsInUse();
891 if (phoneIsInUse || (intent.getData() != null && isDialIntent(intent))) {
892 showDialpadFragment(false);
893 mDialpadFragment.setStartedFromNewIntent(true);
894 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
895 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700896 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700897 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700898 }
899
900 @Override
901 public void onNewIntent(Intent newIntent) {
902 setIntent(newIntent);
Nancy Chen199b7f02014-11-05 15:03:00 -0800903 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700904 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700905
Chiao Cheng94b10b52012-08-17 16:59:12 -0700906 invalidateOptionsMenu();
907 }
908
909 /** Returns true if the given intent contains a phone number to populate the dialer with */
910 private boolean isDialIntent(Intent intent) {
911 final String action = intent.getAction();
912 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
913 return true;
914 }
915 if (Intent.ACTION_VIEW.equals(action)) {
916 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700917 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700918 return true;
919 }
920 }
921 return false;
922 }
923
924 /**
925 * Returns an appropriate call origin for this Activity. May return null when no call origin
926 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
927 * for remembering the tab in which the user made a phone call, so the external app's DIAL
928 * request should not be counted.)
929 */
930 public String getCallOrigin() {
931 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
932 }
933
Chiao Cheng94b10b52012-08-17 16:59:12 -0700934 /**
Yorke Leec3766332013-07-31 11:13:16 -0700935 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700936 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700937 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700938 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700939 // Weird race condition where fragment is doing work after the activity is destroyed
940 // due to talkback being on (b/10209937). Just return since we can't do any
941 // constructive here.
942 return;
943 }
944
Yorke Leeef2b7382013-08-09 17:39:25 -0700945 if (DEBUG) {
946 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
947 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700948
Yorke Leec3766332013-07-31 11:13:16 -0700949 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700950 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700951 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700952 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700953 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700954 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700955
Yorke Leeb207f8a2013-08-29 18:51:06 -0700956 final String tag;
957 if (smartDialSearch) {
958 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
959 } else {
960 tag = TAG_REGULAR_SEARCH_FRAGMENT;
961 }
962 mInDialpadSearch = smartDialSearch;
963 mInRegularSearch = !smartDialSearch;
964
Andrew Lee6324f702015-06-16 14:45:58 -0700965 mFloatingActionButtonController.scaleOut();
966
Andrew Lee752956e2014-05-15 11:43:38 -0700967 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700968 if (animate) {
969 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
970 } else {
971 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
972 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700973 if (fragment == null) {
974 if (smartDialSearch) {
975 fragment = new SmartDialSearchFragment();
976 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700977 fragment = new RegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -0700978 fragment.setOnTouchListener(new View.OnTouchListener() {
979 @Override
980 public boolean onTouch(View v, MotionEvent event) {
981 // Show the FAB when the user touches the lists fragment and the soft
982 // keyboard is hidden.
983 showFabInSearchUi();
984 return false;
985 }
986 });
Yorke Leeef2b7382013-08-09 17:39:25 -0700987 }
Andrew Leeb1903842014-05-15 16:11:24 -0700988 transaction.add(R.id.dialtacts_frame, fragment, tag);
989 } else {
990 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700991 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700992 // DialtactsActivity will provide the options menu
993 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700994 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700995 if (!smartDialSearch) {
996 fragment.setQueryString(query, false /* delaySelection */);
997 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700998 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700999
Yorke Lee6c450eb2015-05-08 09:45:47 -07001000 if (animate) {
1001 mListsFragment.getView().animate().alpha(0).withLayer();
1002 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001003 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001004 }
1005
1006 /**
Yorke Leec3766332013-07-31 11:13:16 -07001007 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001008 */
Yorke Leec3766332013-07-31 11:13:16 -07001009 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001010 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001011 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001012 return;
1013 }
Andrew Leeb1903842014-05-15 16:11:24 -07001014
Andrew Leeb1903842014-05-15 16:11:24 -07001015 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001016
1017 if (mDialpadFragment != null) {
1018 mDialpadFragment.clearDialpad();
1019 }
1020
Yorke Leeb207f8a2013-08-29 18:51:06 -07001021 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001022
Andrew Lee6324f702015-06-16 14:45:58 -07001023 // Restore the FAB for the lists fragment.
1024 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1025 mFloatingActionButtonController.setVisible(false);
1026 }
1027 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1028 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1029 onPageSelected(mListsFragment.getCurrentTabIndex());
1030
Andrew Leeb1903842014-05-15 16:11:24 -07001031 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001032 if (mSmartDialSearchFragment != null) {
1033 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001034 }
Andrew Leeb1903842014-05-15 16:11:24 -07001035 if (mRegularSearchFragment != null) {
1036 transaction.remove(mRegularSearchFragment);
1037 }
1038 transaction.commit();
1039
1040 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001041
1042 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001043 // If the dialpad fragment wasn't previously visible, then send a screen view because
1044 // we are exiting regular search. Otherwise, the screen view will be sent by
1045 // {@link #hideDialpadFragment}.
1046 mListsFragment.sendScreenViewForCurrentPosition();
1047 mListsFragment.setUserVisibleHint(true);
1048 }
1049
Yorke Lee5253be02014-05-21 18:50:03 -07001050 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001051 }
1052
Chiao Cheng94b10b52012-08-17 16:59:12 -07001053 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001054 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001055 if (mStateSaved) {
1056 return;
1057 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001058 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001059 if (TextUtils.isEmpty(mSearchQuery) ||
1060 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1061 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001062 exitSearchUi();
1063 }
Yorke Leef6673d32013-08-23 15:34:17 -07001064 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001065 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001066 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001067 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001068 } else {
1069 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001070 }
Yorke Leec3766332013-07-31 11:13:16 -07001071 }
1072
Yorke Lee81a313d2015-06-01 14:53:59 -07001073 private void maybeEnterSearchUi() {
1074 if (!isInSearchUi()) {
1075 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1076 }
1077 }
1078
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001079 /**
1080 * @return True if the search UI was exited, false otherwise
1081 */
1082 private boolean maybeExitSearchUi() {
1083 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1084 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001085 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001086 return true;
1087 }
1088 return false;
1089 }
1090
Andrew Lee6324f702015-06-16 14:45:58 -07001091 private void showFabInSearchUi() {
1092 mFloatingActionButtonController.changeIcon(
1093 getResources().getDrawable(R.drawable.fab_ic_dial),
1094 getResources().getString(R.string.action_menu_dialpad_button));
1095 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1096 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1097 }
1098
Yorke Leec3766332013-07-31 11:13:16 -07001099 @Override
1100 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001101 if (mSmartDialSearchFragment != null) {
1102 mSmartDialSearchFragment.setAddToContactNumber(query);
1103 }
Yorke Leec3766332013-07-31 11:13:16 -07001104 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1105 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001106
1107 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001108 if (DEBUG) {
1109 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1110 }
1111 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1112 // This callback can happen if the dialpad fragment is recreated because of
1113 // activity destruction. In that case, don't update the search view because
1114 // that would bring the user back to the search fragment regardless of the
1115 // previous state of the application. Instead, just return here and let the
1116 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001117 if (!TextUtils.isEmpty(normalizedQuery)) {
1118 mPendingSearchViewQuery = normalizedQuery;
1119 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001120 return;
1121 }
Yorke Lee53a22302014-04-29 18:13:46 -07001122 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001123 }
Santos Cordon83131712015-04-16 21:25:35 -07001124
1125 try {
1126 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1127 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1128 }
1129 } catch (Exception ignored) {
1130 // Skip any exceptions for this piece of code
1131 }
1132
Chiao Cheng94b10b52012-08-17 16:59:12 -07001133 }
Yorke Leec3766332013-07-31 11:13:16 -07001134
1135 @Override
1136 public void onListFragmentScrollStateChange(int scrollState) {
1137 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001138 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001139 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001140 }
1141 }
1142
1143 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001144 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001145 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001146 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1147 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001148 }
1149
Yorke Leec3766332013-07-31 11:13:16 -07001150 private boolean phoneIsInUse() {
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001151 return getTelecomManager().isInCall();
Yorke Leec3766332013-07-31 11:13:16 -07001152 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001153
Yorke Leeda0f9042013-12-05 14:25:51 -08001154 private boolean canIntentBeHandled(Intent intent) {
1155 final PackageManager packageManager = getPackageManager();
1156 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1157 PackageManager.MATCH_DEFAULT_ONLY);
1158 return resolveInfo != null && resolveInfo.size() > 0;
1159 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001160
Yorke Lee86e21f72014-04-02 16:49:38 -07001161 /**
1162 * Called when the user has long-pressed a contact tile to start a drag operation.
1163 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001164 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001165 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001166 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001167 }
1168
1169 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001170 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1171 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001172
Yorke Lee86e21f72014-04-02 16:49:38 -07001173 /**
1174 * Called when the user has released a contact tile after long-pressing it.
1175 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001176 @Override
1177 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001178 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001179 }
1180
1181 @Override
1182 public void onDroppedOnRemove() {}
1183
1184 /**
Yorke Leed999b712014-04-23 15:10:58 -07001185 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001186 * once it has been attached to the activity.
1187 */
1188 @Override
1189 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001190 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001191 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001192 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001193
1194 @Override
1195 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001196 // Specify call-origin so that users will see the previous tab instead of
1197 // CallLog screen (search UI will be automatically exited).
1198 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001199 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001200 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001201 }
1202
1203 @Override
1204 public void onCallNumberDirectly(String phoneNumber) {
Andrew Leefc1fed72014-07-02 11:05:53 -07001205 onCallNumberDirectly(phoneNumber, false /* isVideoCall */);
1206 }
1207
1208 @Override
1209 public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall) {
Andrew Leed2a683f2014-07-08 15:54:50 -07001210 Intent intent = isVideoCall ?
Andrew Lee247df6e2015-05-12 19:09:00 -07001211 IntentUtil.getVideoCallIntent(phoneNumber, getCallOrigin()) :
1212 IntentUtil.getCallIntent(phoneNumber, getCallOrigin());
Yorke Lee7d20f822014-06-19 17:09:33 -07001213 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001214 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001215 }
1216
1217 @Override
1218 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001219 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001220 }
1221
1222 @Override
1223 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001224 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001225 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001226
Yorke Leecc4660d2014-04-24 11:22:57 -07001227 @Override
1228 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001229 int tabIndex = mListsFragment.getCurrentTabIndex();
1230
1231 // Scroll the button from center to end when moving from the Speed Dial to Recents tab.
1232 // In RTL, scroll when the current tab is Recents instead of Speed Dial, because the order
1233 // of the tabs is reversed and the ViewPager returns the left tab position during scroll.
1234 boolean isRtl = DialerUtils.isRtl();
1235 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1236 mFloatingActionButtonController.onPageScrolled(positionOffset);
1237 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_RECENTS && !mIsLandscape) {
1238 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1239 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001240 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001241 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001242 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001243
Andrew Lee432d4b52015-05-20 16:52:00 -07001244 @Override
1245 public void onPageSelected(int position) {
1246 int tabIndex = mListsFragment.getCurrentTabIndex();
1247 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001248 mFloatingActionButtonController.changeIcon(
1249 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1250 getResources().getString(R.string.search_shortcut_create_new_contact));
1251 } else {
1252 mFloatingActionButtonController.changeIcon(
1253 getResources().getDrawable(R.drawable.fab_ic_dial),
1254 getResources().getString(R.string.action_menu_dialpad_button));
1255 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001256 }
1257
1258 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001259 public void onPageScrollStateChanged(int state) {
1260 }
1261
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001262 private TelecomManager getTelecomManager() {
1263 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordond15629d2014-07-07 15:15:29 -07001264 }
1265
Yorke Lee5253be02014-05-21 18:50:03 -07001266 @Override
1267 public boolean isActionBarShowing() {
1268 return mActionBarController.isActionBarShowing();
1269 }
1270
Yorke Leec98a5bb2014-10-28 16:12:05 -07001271 @Override
1272 public ActionBarController getActionBarController() {
1273 return mActionBarController;
1274 }
1275
Andrew Lee9da8fb42014-06-03 14:03:09 -07001276 public boolean isDialpadShown() {
1277 return mIsDialpadShown;
1278 }
1279
Yorke Lee5253be02014-05-21 18:50:03 -07001280 @Override
1281 public int getActionBarHideOffset() {
1282 return getActionBar().getHideOffset();
1283 }
1284
1285 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001286 public void setActionBarHideOffset(int offset) {
1287 getActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001288 }
1289
1290 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001291 public int getActionBarHeight() {
1292 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001293 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001294
Andrew Lee6324f702015-06-16 14:45:58 -07001295
1296 private int getFabAlignment() {
1297 if (!mIsLandscape && !isInSearchUi() &&
1298 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1299 return FloatingActionButtonController.ALIGN_MIDDLE;
1300 }
1301 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001302 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001303}