blob: 47e52342a663e1d8f3a026a9c794444b32d8d406 [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;
Yorke Lee11ca39e2014-05-19 20:31:37 -070087import com.android.dialer.widget.SearchEditTextLayout.OnBackButtonClickedListener;
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
Yorke Leef614f6f2014-10-03 10:44:19 -0700139 private FrameLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700140
Yorke Leec3766332013-07-31 11:13:16 -0700141 /**
Yorke Leec3766332013-07-31 11:13:16 -0700142 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700143 */
Evan Charlton72d66252014-11-07 16:23:47 -0800144 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700145
146 /**
147 * Fragment for searching phone numbers using the alphanumeric keyboard.
148 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700149 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700150
151 /**
152 * Fragment for searching phone numbers using the dialpad.
153 */
154 private SmartDialSearchFragment mSmartDialSearchFragment;
155
Yorke Leee00c9fe2014-04-12 12:42:06 -0700156 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700157 * Animation that slides in.
158 */
159 private Animation mSlideIn;
160
161 /**
162 * Animation that slides out.
163 */
164 private Animation mSlideOut;
165
Yorke Lee6c450eb2015-05-08 09:45:47 -0700166 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
167 @Override
168 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700169 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700170 }
171 };
172
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700173 /**
174 * Listener for after slide out animation completes on dialer fragment.
175 */
176 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
177 @Override
178 public void onAnimationEnd(Animation animation) {
179 commitDialpadFragmentHide();
180 }
181 };
182
183 /**
Yorke Leee00c9fe2014-04-12 12:42:06 -0700184 * Fragment containing the speed dial list, recents list, and all contacts list.
185 */
186 private ListsFragment mListsFragment;
187
Yorke Lee19e68ca2014-10-28 11:51:40 -0700188 /**
189 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
190 * be commited.
191 */
192 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800193 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700194 private boolean mInDialpadSearch;
195 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700196 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700197 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700198 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700199
Yorke Leeef2b7382013-08-09 17:39:25 -0700200 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700201 * Whether or not the device is in landscape orientation.
202 */
203 private boolean mIsLandscape;
204
205 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700206 * True if the dialpad is only temporarily showing due to being in call
207 */
208 private boolean mInCallDialpadUp;
209
210 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700211 * True when this activity has been launched for the first time.
212 */
Yorke Lee98702de2013-08-06 12:03:32 -0700213 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700214
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700215 /**
216 * Search query to be applied to the SearchView in the ActionBar once
217 * onCreateOptionsMenu has been called.
218 */
219 private String mPendingSearchViewQuery;
220
Yorke Lee74edcdc2014-07-11 16:15:08 -0700221 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700222 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700223 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700224
Yorke Leeef2b7382013-08-09 17:39:25 -0700225 private String mSearchQuery;
226
Yorke Leefce269a2013-08-12 11:56:04 -0700227 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700228 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700229 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700230
Sai Cheemalapati41460652014-06-02 21:05:39 -0700231 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700232
Andrew Lee2423a2f2014-05-29 14:14:45 -0700233 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700234
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800235 /**
236 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
237 * {@link #onResume()} to populate the search box.
238 */
239 private String mVoiceSearchQuery;
240
Andrew Lee467de3d2015-03-17 15:47:24 -0700241 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700242 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700243 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700244 }
245
246 @Override
247 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700248 final boolean hasContactsPermission =
249 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700250 final Menu menu = getMenu();
251 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700252 clearFrequents.setVisible(mListsFragment != null &&
253 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700254 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
255
256 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
257 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700258
259 menu.findItem(R.id.menu_history).setVisible(
260 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700261 super.show();
262 }
263 }
264
Chiao Cheng94b10b52012-08-17 16:59:12 -0700265 /**
Yorke Lee28266192014-05-01 10:05:52 -0700266 * Listener that listens to drag events and sends their x and y coordinates to a
267 * {@link DragDropController}.
268 */
269 private class LayoutOnDragListener implements OnDragListener {
270 @Override
271 public boolean onDrag(View v, DragEvent event) {
272 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700273 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700274 }
275 return true;
276 }
277 }
278
279 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700280 * Listener used to send search queries to the phone search fragment.
281 */
Yorke Lee53a22302014-04-29 18:13:46 -0700282 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700283 @Override
284 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
285 }
Yorke Leec3766332013-07-31 11:13:16 -0700286
Andrew Lee99a570c2014-05-15 14:18:50 -0700287 @Override
288 public void onTextChanged(CharSequence s, int start, int before, int count) {
289 final String newText = s.toString();
290 if (newText.equals(mSearchQuery)) {
291 // If the query hasn't changed (perhaps due to activity being destroyed
292 // and restored, or user launching the same DIAL intent twice), then there is
293 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700294 return;
295 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700296 if (DEBUG) {
297 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700298 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700299 }
Yorke Lee710709d2014-05-29 07:18:42 -0700300 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700301
Andrew Lee90374a72014-05-16 15:01:09 -0700302 // Show search fragment only when the query string is changed to non-empty text.
303 if (!TextUtils.isEmpty(newText)) {
304 // Call enterSearchUi only if we are switching search modes, or showing a search
305 // fragment for the first time.
306 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
307 (!mIsDialpadShown && mInRegularSearch);
308 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700309 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700310 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700311 }
312
Andrew Leea8515422014-06-13 16:53:54 -0700313 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700314 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700315 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700316 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700317 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700318 }
319
320 @Override
321 public void afterTextChanged(Editable s) {
322 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700323 };
324
Andrew Leeb1903842014-05-15 16:11:24 -0700325
326 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700327 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700328 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700329 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700330 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700331 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700332 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700333 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700334 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
335 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700336 }
Andrew Leeb1903842014-05-15 16:11:24 -0700337 }
338 };
339
340 /**
341 * If the search term is empty and the user closes the soft keyboard, close the search UI.
342 */
343 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
344 @Override
345 public boolean onKey(View v, int keyCode, KeyEvent event) {
346 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
347 TextUtils.isEmpty(mSearchView.getText().toString())) {
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700348 maybeExitSearchUi();
Andrew Leeb1903842014-05-15 16:11:24 -0700349 }
350 return false;
351 }
352 };
353
Chiao Cheng94b10b52012-08-17 16:59:12 -0700354 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700355 public boolean dispatchTouchEvent(MotionEvent ev) {
356 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
357 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
358 }
359 return super.dispatchTouchEvent(ev);
360
361 }
362
363 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700364 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800365 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700366 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700367
Yorke Lee98702de2013-08-06 12:03:32 -0700368 mFirstLaunch = true;
369
Andrew Lee2423a2f2014-05-29 14:14:45 -0700370 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700371 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700372
Yorke Leef5554ce2015-01-27 14:46:02 -0800373 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700374 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800375 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800376 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700377
Yorke Leef5554ce2015-01-27 14:46:02 -0800378 Trace.beginSection(TAG + " setup Views");
Yorke Lee53a22302014-04-29 18:13:46 -0700379 final ActionBar actionBar = getActionBar();
380 actionBar.setCustomView(R.layout.search_edittext);
381 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700382 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700383
Andrew Lee04675a52014-06-05 18:36:20 -0700384 SearchEditTextLayout searchEditTextLayout =
Andrew Leee3f59a82015-02-26 12:06:35 -0800385 (SearchEditTextLayout) actionBar.getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700386 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
387
Andrew Leee3f59a82015-02-26 12:06:35 -0800388 mActionBarController = new ActionBarController(this, searchEditTextLayout);
389
Andrew Lee04675a52014-06-05 18:36:20 -0700390 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700391 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700392 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
393 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
394 .setOnClickListener(mSearchViewOnClickListener);
395 searchEditTextLayout.findViewById(R.id.search_box_start_search)
396 .setOnClickListener(mSearchViewOnClickListener);
397 searchEditTextLayout.setOnBackButtonClickedListener(new OnBackButtonClickedListener() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700398 @Override
399 public void onBackButtonClicked() {
400 onBackPressed();
401 }
402 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700403
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700404 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700405 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700406
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700407 final View floatingActionButtonContainer = findViewById(
408 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700409 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
410 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700411 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700412 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700413
Andrew Lee04675a52014-06-05 18:36:20 -0700414 ImageButton optionsMenuButton =
415 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700416 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700417 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
418 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700419
Yorke Leed5c512a2015-01-30 14:42:53 -0800420 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
421 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700422 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800423 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000424 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800425 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700426 } else {
427 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700428 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
429 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700430 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700431 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700432 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700433 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700434
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700435 final boolean isLayoutRtl = DialerUtils.isRtl();
436 if (mIsLandscape) {
437 mSlideIn = AnimationUtils.loadAnimation(this,
438 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
439 mSlideOut = AnimationUtils.loadAnimation(this,
440 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
441 } else {
442 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
443 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
444 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700445
Yorke Lee2fec47b2014-08-05 18:16:27 -0700446 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
447 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
448
Yorke Lee6c450eb2015-05-08 09:45:47 -0700449 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700450 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700451
Yorke Leef614f6f2014-10-03 10:44:19 -0700452 mParentLayout = (FrameLayout) findViewById(R.id.dialtacts_mainlayout);
453 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700454 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700455 new ViewTreeObserver.OnGlobalLayoutListener() {
456 @Override
457 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700458 final ViewTreeObserver observer =
459 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700460 if (!observer.isAlive()) {
461 return;
462 }
463 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700464 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700465 mFloatingActionButtonController.setScreenWidth(screenWidth);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700466 updateFloatingActionButtonControllerAlignment(false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700467 }
468 });
Andrew Lee0c667702014-05-12 14:31:45 -0700469
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700470 setupActivityOverlay();
471
Yorke Leef5554ce2015-01-27 14:46:02 -0800472 Trace.endSection();
473
474 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700475 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700476 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800477 Trace.endSection();
478 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700479 }
480
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700481 private void setupActivityOverlay() {
482 final View activityOverlay = findViewById(R.id.activity_overlay);
483 activityOverlay.setOnTouchListener(new OnTouchListener() {
484 @Override
485 public boolean onTouch(View v, MotionEvent event) {
486 if (!mIsDialpadShown) {
487 maybeExitSearchUi();
488 }
489 return false;
490 }
491 });
492 }
493
Chiao Cheng94b10b52012-08-17 16:59:12 -0700494 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700495 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800496 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700497 super.onResume();
Yorke Lee19e68ca2014-10-28 11:51:40 -0700498 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700499 if (mFirstLaunch) {
500 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700501 } else if (!phoneIsInUse() && mInCallDialpadUp) {
502 hideDialpadFragment(false, true);
503 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700504 } else if (mShowDialpadOnResume) {
505 showDialpadFragment(false);
506 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700507 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800508
509 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
510 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
511 // shown until onResume has completed. Active the search UI and set the search term now.
512 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
513 mActionBarController.onSearchBoxTapped();
514 mSearchView.setText(mVoiceSearchQuery);
515 mVoiceSearchQuery = null;
516 }
517
Yorke Lee98702de2013-08-06 12:03:32 -0700518 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800519
520 if (mIsRestarting) {
521 // This is only called when the activity goes from resumed -> paused -> resumed, so it
522 // will not cause an extra view to be sent out on rotation
523 if (mIsDialpadShown) {
524 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
525 }
526 mIsRestarting = false;
527 }
Yorke Lee53a22302014-04-29 18:13:46 -0700528 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700529 mDialerDatabaseHelper.startSmartDialUpdateThread();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700530 updateFloatingActionButtonControllerAlignment(false /* animate */);
Yorke Leef5554ce2015-01-27 14:46:02 -0800531 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700532 }
533
534 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800535 protected void onRestart() {
536 super.onRestart();
537 mIsRestarting = true;
538 }
539
540 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700541 protected void onPause() {
542 if (mClearSearchOnPause) {
543 hideDialpadAndSearchUi();
544 mClearSearchOnPause = false;
545 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700546 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
547 commitDialpadFragmentHide();
548 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700549 super.onPause();
550 }
551
552 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700553 protected void onSaveInstanceState(Bundle outState) {
554 super.onSaveInstanceState(outState);
555 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700556 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
557 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700558 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700559 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700560 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700561 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700562 }
563
564 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700565 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700566 if (fragment instanceof DialpadFragment) {
567 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800568 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700569 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
570 transaction.hide(mDialpadFragment);
571 transaction.commit();
572 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700573 } else if (fragment instanceof SmartDialSearchFragment) {
574 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700575 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700576 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700577 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700578 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700579 } else if (fragment instanceof ListsFragment) {
580 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700581 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700582 }
Yorke Leec3766332013-07-31 11:13:16 -0700583 }
584
Alon Albertb453e5b2013-09-10 15:54:23 -0700585 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700586 final Intent intent = new Intent(this, DialerSettingsActivity.class);
587 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700588 }
589
Chiao Cheng94b10b52012-08-17 16:59:12 -0700590 @Override
591 public void onClick(View view) {
592 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700593 case R.id.floating_action_button:
Andrew Lee432d4b52015-05-20 16:52:00 -0700594 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700595 DialerUtils.startActivityWithErrorToast(
596 this,
597 IntentUtil.getNewContactIntent(),
598 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700599 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700600 mInCallDialpadUp = false;
601 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700602 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700603 break;
Yorke Leec3766332013-07-31 11:13:16 -0700604 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800605 try {
606 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
607 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
608 } catch (ActivityNotFoundException e) {
609 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
610 Toast.LENGTH_SHORT).show();
611 }
Yorke Leec3766332013-07-31 11:13:16 -0700612 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700613 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700614 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700615 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700616 default: {
617 Log.wtf(TAG, "Unexpected onClick event from " + view);
618 break;
619 }
620 }
621 }
622
Yorke Leec3766332013-07-31 11:13:16 -0700623 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700624 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700625 switch (item.getItemId()) {
626 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700627 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
628 // CONTENT_TYPE, so that we always open our call log from our dialer
629 final Intent intent = new Intent(this, CallLogActivity.class);
630 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700631 break;
632 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700633 DialerUtils.startActivityWithErrorToast(
634 this,
635 IntentUtil.getNewContactIntent(),
636 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700637 break;
638 case R.id.menu_import_export:
639 // We hard-code the "contactsAreAvailable" argument because doing it properly would
640 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
641 // now in Dialtacts for (potential) performance reasons. Compare with how it is
642 // done in {@link PeopleActivity}.
643 ImportExportDialogFragment.show(getFragmentManager(), true,
644 DialtactsActivity.class);
645 return true;
646 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700647 ClearFrequentsDialog.show(getFragmentManager());
648 return true;
649 case R.id.menu_call_settings:
650 handleMenuSettings();
651 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700652 }
653 return false;
654 }
655
656 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700657 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
658 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
659 if (resultCode == RESULT_OK) {
660 final ArrayList<String> matches = data.getStringArrayListExtra(
661 RecognizerIntent.EXTRA_RESULTS);
662 if (matches.size() > 0) {
663 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800664 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700665 } else {
666 Log.e(TAG, "Voice search - nothing heard");
667 }
668 } else {
669 Log.e(TAG, "Voice search failed");
670 }
671 }
672 super.onActivityResult(requestCode, resultCode, data);
673 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700674
Andrew Lee2a58ab82014-05-15 02:16:04 -0700675 /**
676 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
677 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
678 * @see #onDialpadShown
679 */
Yorke Leec3766332013-07-31 11:13:16 -0700680 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700681 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700682 return;
683 }
684 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800685
Yorke Lee7ee5c422014-11-04 10:29:32 -0800686 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700687
Chiao Cheng94b10b52012-08-17 16:59:12 -0700688 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800689 if (mDialpadFragment == null) {
690 mDialpadFragment = new DialpadFragment();
691 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
692 } else {
693 ft.show(mDialpadFragment);
694 }
695
696 mDialpadFragment.setAnimate(animate);
697 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700698 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700699
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700700 if (animate) {
701 mFloatingActionButtonController.scaleOut();
702 } else {
703 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700704 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700705 }
Yorke Lee5253be02014-05-21 18:50:03 -0700706 mActionBarController.onDialpadUp();
707
Yorke Lee6c450eb2015-05-08 09:45:47 -0700708 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700709 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700710
Andrew Lee2a58ab82014-05-15 02:16:04 -0700711 /**
712 * Callback from child DialpadFragment when the dialpad is shown.
713 */
714 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800715 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700716 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700717 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700718 } else {
719 mDialpadFragment.setYFraction(0);
720 }
721
Andrew Lee2a58ab82014-05-15 02:16:04 -0700722 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700723 }
724
725 /**
726 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
727 * a callback after the hide animation ends.
728 * @see #commitDialpadFragmentHide
729 */
Yorke Leeca195042013-09-25 16:29:38 -0700730 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700731 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700732 return;
733 }
Yorke Leef6673d32013-08-23 15:34:17 -0700734 if (clearDialpad) {
735 mDialpadFragment.clearDialpad();
736 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700737 if (!mIsDialpadShown) {
738 return;
Yorke Leec3766332013-07-31 11:13:16 -0700739 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700740 mIsDialpadShown = false;
741 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800742 mListsFragment.setUserVisibleHint(true);
743 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700744
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700745 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700746
747 updateFloatingActionButtonControllerAlignment(animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700748 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700749 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700750 } else {
751 commitDialpadFragmentHide();
752 }
753
Yorke Lee5253be02014-05-21 18:50:03 -0700754 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700755
Andrew Leed4cde832014-05-16 15:56:48 -0700756 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700757 if (TextUtils.isEmpty(mSearchQuery)) {
758 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700759 }
Andrew Leed4cde832014-05-16 15:56:48 -0700760 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700761 }
762
763 /**
764 * Finishes hiding the dialpad fragment after any animations are completed.
765 */
766 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800767 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700768 final FragmentTransaction ft = getFragmentManager().beginTransaction();
769 ft.hide(mDialpadFragment);
770 ft.commit();
771 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700772 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700773 }
774
Andrew Lee2a58ab82014-05-15 02:16:04 -0700775 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700776 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700777 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700778 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700779 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700780 fragment = mRegularSearchFragment;
781 }
782 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700783 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700784 }
785 }
786
Yorke Lee5253be02014-05-21 18:50:03 -0700787 @Override
788 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700789 return mInDialpadSearch || mInRegularSearch;
790 }
791
Yorke Lee5253be02014-05-21 18:50:03 -0700792 @Override
793 public boolean hasSearchQuery() {
794 return !TextUtils.isEmpty(mSearchQuery);
795 }
796
797 @Override
798 public boolean shouldShowActionBar() {
799 return mListsFragment.shouldShowActionBar();
800 }
801
Yorke Leeb207f8a2013-08-29 18:51:06 -0700802 private void setNotInSearchUi() {
803 mInDialpadSearch = false;
804 mInRegularSearch = false;
805 }
806
Yorke Lee311969c2013-08-26 06:31:11 -0700807 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700808 if (mIsDialpadShown) {
809 hideDialpadFragment(false, true);
810 } else {
811 exitSearchUi();
812 }
Yorke Lee311969c2013-08-26 06:31:11 -0700813 }
814
Yorke Lee53a22302014-04-29 18:13:46 -0700815 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700816 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
817 if (canIntentBeHandled(voiceIntent)) {
818 mVoiceSearchButton.setVisibility(View.VISIBLE);
819 mVoiceSearchButton.setOnClickListener(this);
820 } else {
821 mVoiceSearchButton.setVisibility(View.GONE);
822 }
823 }
824
Andrew Lee467de3d2015-03-17 15:47:24 -0700825 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700826 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
827 popupMenu.inflate(R.menu.dialtacts_options);
828 popupMenu.setOnMenuItemClickListener(this);
829 return popupMenu;
830 }
831
Yorke Lee86e21f72014-04-02 16:49:38 -0700832 @Override
833 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700834 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700835 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700836 mPendingSearchViewQuery = null;
837 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700838 if (mActionBarController != null) {
839 mActionBarController.restoreActionBarOffset();
840 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700841 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700842 }
843
Chiao Cheng94b10b52012-08-17 16:59:12 -0700844 /**
845 * Returns true if the intent is due to hitting the green send key (hardware call button:
846 * KEYCODE_CALL) while in a call.
847 *
848 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700849 * @return true if the intent is due to hitting the green send key while in a call
850 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700851 private boolean isSendKeyWhileInCall(Intent intent) {
852 // If there is a call in progress and the user launched the dialer by hitting the call
853 // button, go straight to the in-call screen.
854 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700855
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700856 if (callKey) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700857 getTelecomManager().showInCallScreen(false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700858 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700859 }
860
861 return false;
862 }
863
864 /**
865 * Sets the current tab based on the intent's request type
866 *
867 * @param intent Intent that contains information about which tab should be selected
868 */
Yorke Leec3766332013-07-31 11:13:16 -0700869 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700870 // 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 -0700871 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700872 finish();
873 return;
874 }
875
Yorke Leed5c512a2015-01-30 14:42:53 -0800876 final boolean phoneIsInUse = phoneIsInUse();
877 if (phoneIsInUse || (intent.getData() != null && isDialIntent(intent))) {
878 showDialpadFragment(false);
879 mDialpadFragment.setStartedFromNewIntent(true);
880 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
881 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700882 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700883 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700884 }
885
886 @Override
887 public void onNewIntent(Intent newIntent) {
888 setIntent(newIntent);
Nancy Chen199b7f02014-11-05 15:03:00 -0800889 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700890 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700891
Chiao Cheng94b10b52012-08-17 16:59:12 -0700892 invalidateOptionsMenu();
893 }
894
895 /** Returns true if the given intent contains a phone number to populate the dialer with */
896 private boolean isDialIntent(Intent intent) {
897 final String action = intent.getAction();
898 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
899 return true;
900 }
901 if (Intent.ACTION_VIEW.equals(action)) {
902 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700903 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700904 return true;
905 }
906 }
907 return false;
908 }
909
910 /**
911 * Returns an appropriate call origin for this Activity. May return null when no call origin
912 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
913 * for remembering the tab in which the user made a phone call, so the external app's DIAL
914 * request should not be counted.)
915 */
916 public String getCallOrigin() {
917 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
918 }
919
Chiao Cheng94b10b52012-08-17 16:59:12 -0700920 /**
Yorke Leec3766332013-07-31 11:13:16 -0700921 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700922 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700923 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700924 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700925 // Weird race condition where fragment is doing work after the activity is destroyed
926 // due to talkback being on (b/10209937). Just return since we can't do any
927 // constructive here.
928 return;
929 }
930
Yorke Leeef2b7382013-08-09 17:39:25 -0700931 if (DEBUG) {
932 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
933 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700934
Yorke Leec3766332013-07-31 11:13:16 -0700935 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700936 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700937 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700938 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700939 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700940 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700941
Yorke Leeb207f8a2013-08-29 18:51:06 -0700942 final String tag;
943 if (smartDialSearch) {
944 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
945 } else {
946 tag = TAG_REGULAR_SEARCH_FRAGMENT;
947 }
948 mInDialpadSearch = smartDialSearch;
949 mInRegularSearch = !smartDialSearch;
950
Andrew Lee752956e2014-05-15 11:43:38 -0700951 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700952 if (animate) {
953 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
954 } else {
955 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
956 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700957 if (fragment == null) {
958 if (smartDialSearch) {
959 fragment = new SmartDialSearchFragment();
960 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700961 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700962 }
Andrew Leeb1903842014-05-15 16:11:24 -0700963 transaction.add(R.id.dialtacts_frame, fragment, tag);
964 } else {
965 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700966 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700967 // DialtactsActivity will provide the options menu
968 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700969 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700970 if (!smartDialSearch) {
971 fragment.setQueryString(query, false /* delaySelection */);
972 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700973 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700974
Yorke Lee6c450eb2015-05-08 09:45:47 -0700975 if (animate) {
976 mListsFragment.getView().animate().alpha(0).withLayer();
977 }
Yorke Lee7ee5c422014-11-04 10:29:32 -0800978 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700979 }
980
981 /**
Yorke Leec3766332013-07-31 11:13:16 -0700982 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700983 */
Yorke Leec3766332013-07-31 11:13:16 -0700984 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700985 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -0700986 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -0700987 return;
988 }
Andrew Leeb1903842014-05-15 16:11:24 -0700989
Andrew Leeb1903842014-05-15 16:11:24 -0700990 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -0800991
992 if (mDialpadFragment != null) {
993 mDialpadFragment.clearDialpad();
994 }
995
Yorke Leeb207f8a2013-08-29 18:51:06 -0700996 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700997
Andrew Leeb1903842014-05-15 16:11:24 -0700998 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -0700999 if (mSmartDialSearchFragment != null) {
1000 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001001 }
Andrew Leeb1903842014-05-15 16:11:24 -07001002 if (mRegularSearchFragment != null) {
1003 transaction.remove(mRegularSearchFragment);
1004 }
1005 transaction.commit();
1006
1007 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001008
1009 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001010 // If the dialpad fragment wasn't previously visible, then send a screen view because
1011 // we are exiting regular search. Otherwise, the screen view will be sent by
1012 // {@link #hideDialpadFragment}.
1013 mListsFragment.sendScreenViewForCurrentPosition();
1014 mListsFragment.setUserVisibleHint(true);
1015 }
1016
Yorke Lee5253be02014-05-21 18:50:03 -07001017 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001018 }
1019
Chiao Cheng94b10b52012-08-17 16:59:12 -07001020 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001021 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001022 if (mStateSaved) {
1023 return;
1024 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001025 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001026 if (TextUtils.isEmpty(mSearchQuery) ||
1027 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1028 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001029 exitSearchUi();
1030 }
Yorke Leef6673d32013-08-23 15:34:17 -07001031 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001032 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001033 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001034 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001035 } else {
1036 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001037 }
Yorke Leec3766332013-07-31 11:13:16 -07001038 }
1039
Yorke Lee81a313d2015-06-01 14:53:59 -07001040 private void maybeEnterSearchUi() {
1041 if (!isInSearchUi()) {
1042 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1043 }
1044 }
1045
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001046 /**
1047 * @return True if the search UI was exited, false otherwise
1048 */
1049 private boolean maybeExitSearchUi() {
1050 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1051 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001052 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001053 return true;
1054 }
1055 return false;
1056 }
1057
Yorke Leec3766332013-07-31 11:13:16 -07001058 @Override
1059 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001060 if (mSmartDialSearchFragment != null) {
1061 mSmartDialSearchFragment.setAddToContactNumber(query);
1062 }
Yorke Leec3766332013-07-31 11:13:16 -07001063 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1064 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001065
1066 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001067 if (DEBUG) {
1068 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1069 }
1070 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1071 // This callback can happen if the dialpad fragment is recreated because of
1072 // activity destruction. In that case, don't update the search view because
1073 // that would bring the user back to the search fragment regardless of the
1074 // previous state of the application. Instead, just return here and let the
1075 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001076 if (!TextUtils.isEmpty(normalizedQuery)) {
1077 mPendingSearchViewQuery = normalizedQuery;
1078 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001079 return;
1080 }
Yorke Lee53a22302014-04-29 18:13:46 -07001081 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001082 }
Santos Cordon83131712015-04-16 21:25:35 -07001083
1084 try {
1085 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1086 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1087 }
1088 } catch (Exception ignored) {
1089 // Skip any exceptions for this piece of code
1090 }
1091
Chiao Cheng94b10b52012-08-17 16:59:12 -07001092 }
Yorke Leec3766332013-07-31 11:13:16 -07001093
1094 @Override
1095 public void onListFragmentScrollStateChange(int scrollState) {
1096 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001097 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001098 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001099 }
1100 }
1101
1102 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001103 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001104 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001105 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1106 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001107 }
1108
Yorke Leec3766332013-07-31 11:13:16 -07001109 private boolean phoneIsInUse() {
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001110 return getTelecomManager().isInCall();
Yorke Leec3766332013-07-31 11:13:16 -07001111 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001112
Yorke Leeda0f9042013-12-05 14:25:51 -08001113 private boolean canIntentBeHandled(Intent intent) {
1114 final PackageManager packageManager = getPackageManager();
1115 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1116 PackageManager.MATCH_DEFAULT_ONLY);
1117 return resolveInfo != null && resolveInfo.size() > 0;
1118 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001119
Yorke Lee86e21f72014-04-02 16:49:38 -07001120 /**
1121 * Called when the user has long-pressed a contact tile to start a drag operation.
1122 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001123 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001124 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001125 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001126 }
1127
1128 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001129 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1130 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001131
Yorke Lee86e21f72014-04-02 16:49:38 -07001132 /**
1133 * Called when the user has released a contact tile after long-pressing it.
1134 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001135 @Override
1136 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001137 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001138 }
1139
1140 @Override
1141 public void onDroppedOnRemove() {}
1142
1143 /**
Yorke Leed999b712014-04-23 15:10:58 -07001144 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001145 * once it has been attached to the activity.
1146 */
1147 @Override
1148 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001149 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001150 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001151 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001152
1153 @Override
1154 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001155 // Specify call-origin so that users will see the previous tab instead of
1156 // CallLog screen (search UI will be automatically exited).
1157 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001158 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001159 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001160 }
1161
1162 @Override
1163 public void onCallNumberDirectly(String phoneNumber) {
Andrew Leefc1fed72014-07-02 11:05:53 -07001164 onCallNumberDirectly(phoneNumber, false /* isVideoCall */);
1165 }
1166
1167 @Override
1168 public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall) {
Andrew Leed2a683f2014-07-08 15:54:50 -07001169 Intent intent = isVideoCall ?
Andrew Lee247df6e2015-05-12 19:09:00 -07001170 IntentUtil.getVideoCallIntent(phoneNumber, getCallOrigin()) :
1171 IntentUtil.getCallIntent(phoneNumber, getCallOrigin());
Yorke Lee7d20f822014-06-19 17:09:33 -07001172 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001173 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001174 }
1175
1176 @Override
1177 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001178 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001179 }
1180
1181 @Override
1182 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001183 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001184 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001185
Yorke Leecc4660d2014-04-24 11:22:57 -07001186 @Override
1187 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001188 int tabIndex = mListsFragment.getCurrentTabIndex();
1189
1190 // Scroll the button from center to end when moving from the Speed Dial to Recents tab.
1191 // In RTL, scroll when the current tab is Recents instead of Speed Dial, because the order
1192 // of the tabs is reversed and the ViewPager returns the left tab position during scroll.
1193 boolean isRtl = DialerUtils.isRtl();
1194 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1195 mFloatingActionButtonController.onPageScrolled(positionOffset);
1196 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_RECENTS && !mIsLandscape) {
1197 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1198 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001199 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001200 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001201 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001202
Andrew Lee432d4b52015-05-20 16:52:00 -07001203 @Override
1204 public void onPageSelected(int position) {
1205 int tabIndex = mListsFragment.getCurrentTabIndex();
1206 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001207 mFloatingActionButtonController.changeIcon(
1208 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1209 getResources().getString(R.string.search_shortcut_create_new_contact));
1210 } else {
1211 mFloatingActionButtonController.changeIcon(
1212 getResources().getDrawable(R.drawable.fab_ic_dial),
1213 getResources().getString(R.string.action_menu_dialpad_button));
1214 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001215 }
1216
1217 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001218 public void onPageScrollStateChanged(int state) {
1219 }
1220
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001221 private TelecomManager getTelecomManager() {
1222 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordond15629d2014-07-07 15:15:29 -07001223 }
1224
Yorke Lee5253be02014-05-21 18:50:03 -07001225 @Override
1226 public boolean isActionBarShowing() {
1227 return mActionBarController.isActionBarShowing();
1228 }
1229
Yorke Leec98a5bb2014-10-28 16:12:05 -07001230 @Override
1231 public ActionBarController getActionBarController() {
1232 return mActionBarController;
1233 }
1234
Andrew Lee9da8fb42014-06-03 14:03:09 -07001235 public boolean isDialpadShown() {
1236 return mIsDialpadShown;
1237 }
1238
Yorke Lee5253be02014-05-21 18:50:03 -07001239 @Override
1240 public int getActionBarHideOffset() {
1241 return getActionBar().getHideOffset();
1242 }
1243
1244 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001245 public void setActionBarHideOffset(int offset) {
1246 getActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001247 }
1248
1249 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001250 public int getActionBarHeight() {
1251 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001252 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001253
1254 /**
1255 * Updates controller based on currently known information.
1256 *
1257 * @param animate Whether or not to animate the transition.
1258 */
1259 private void updateFloatingActionButtonControllerAlignment(boolean animate) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001260 int align = (!mIsLandscape &&
Andrew Lee432d4b52015-05-20 16:52:00 -07001261 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) ?
Andrew Lee405a6e62014-08-20 15:02:16 -07001262 FloatingActionButtonController.ALIGN_MIDDLE :
1263 FloatingActionButtonController.ALIGN_END;
Andrew Leef43b32f2014-08-14 17:43:04 -07001264 mFloatingActionButtonController.align(align, 0 /* offsetX */, 0 /* offsetY */, animate);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001265 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001266}