blob: f62dea161866b0b01f14b989716f660f32d71a18 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
Yorke Leec3766332013-07-31 11:13:16 -07002 * Copyright (C) 2013 The Android Open Source Project
Chiao Cheng94b10b52012-08-17 16:59:12 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer;
18
Chiao Cheng94b10b52012-08-17 16:59:12 -070019import android.app.Fragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -070020import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080021import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.content.Context;
23import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
Sai Cheemalapati82d6da52014-06-03 13:54:23 -070026import android.content.res.Configuration;
Andrew Lee2423a2f2014-05-29 14:14:45 -070027import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.net.Uri;
29import android.os.Bundle;
Yorke Leef5554ce2015-01-27 14:46:02 -080030import android.os.Trace;
Nancy Chen019713e2015-07-06 18:25:24 -070031import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070032import android.speech.RecognizerIntent;
Nancy Chenf3d9f822015-09-11 00:47:31 -040033import android.support.design.widget.CoordinatorLayout;
Yorke Leecc4660d2014-04-24 11:22:57 -070034import android.support.v4.view.ViewPager;
Nancy Chen44898ad2015-08-13 12:03:47 -070035import android.support.v7.app.ActionBar;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070036import android.telecom.PhoneAccount;
37import android.telecom.TelecomManager;
Yorke Lee53a22302014-04-29 18:13:46 -070038import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070040import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070042import android.view.DragEvent;
Yorke Lee74edcdc2014-07-11 16:15:08 -070043import android.view.Gravity;
Andrew Leeb1903842014-05-15 16:11:24 -070044import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070047import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070049import android.view.View.OnDragListener;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070050import android.view.View.OnTouchListener;
Ihab Awad9134e252014-07-09 12:32:52 -070051import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070052import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070053import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070054import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070055import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070056import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.widget.PopupMenu;
Anne Rong35208002015-07-22 16:27:37 -070058import android.widget.TextView;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080059import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070060
Yorke Leec3766332013-07-31 11:13:16 -070061import com.android.contacts.common.dialog.ClearFrequentsDialog;
62import com.android.contacts.common.interactions.ImportExportDialogFragment;
Nancy Chen0d3a7392014-08-12 14:44:53 -070063import com.android.contacts.common.interactions.TouchPointManager;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080064import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec16ea5a2015-05-19 15:51:01 -070065import com.android.contacts.common.util.PermissionsUtil;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -070066import com.android.contacts.common.widget.FloatingActionButtonController;
Brian Attwellbeab3bb2014-10-27 12:24:49 -070067import com.android.contacts.commonbind.analytics.AnalyticsUtil;
Yorke Leec3766332013-07-31 11:13:16 -070068import com.android.dialer.calllog.CallLogActivity;
Yorke Lee38019af2015-07-14 17:05:38 -070069import com.android.dialer.calllog.CallLogFragment;
Yorke Leefce269a2013-08-12 11:56:04 -070070import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070071import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070073import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070074import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080075import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070076import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080077import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070078import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070079import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070080import com.android.dialer.list.RegularSearchFragment;
81import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070082import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070083import com.android.dialer.list.SpeedDialFragment;
Yorke Leeaf6f1952014-07-14 19:13:16 -070084import com.android.dialer.settings.DialerSettingsActivity;
Andrew Lee247df6e2015-05-12 19:09:00 -070085import com.android.dialer.util.IntentUtil;
Yorke Leee3a2d132015-09-14 16:52:08 -070086import com.android.dialer.util.IntentUtil.CallIntentBuilder;
Yorke Lee7d20f822014-06-19 17:09:33 -070087import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070088import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070089import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070090import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070091import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee2fec47b2014-08-05 18:16:27 -070092import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070093import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070094
Yorke Leed5c512a2015-01-30 14:42:53 -080095import junit.framework.Assert;
96
Yorke Leec3766332013-07-31 11:13:16 -070097import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080098import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070099
Chiao Cheng94b10b52012-08-17 16:59:12 -0700100/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101 * The dialer tab's title is 'phone', a more common name (see strings.xml).
102 */
Yorke Leec3766332013-07-31 11:13:16 -0700103public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700104 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700105 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700106 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700107 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700108 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700109 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700110 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700111 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700112 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700113 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700114 ViewPager.OnPageChangeListener,
115 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700116 private static final String TAG = "DialtactsActivity";
117
Yorke Lee7cc61492015-06-01 14:59:33 -0700118 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119
Yorke Leef74011e2013-08-02 11:30:30 -0700120 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
121
Yorke Leeb207f8a2013-08-29 18:51:06 -0700122 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
123 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700124 private static final String KEY_SEARCH_QUERY = "search_query";
125 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700126 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700127
Yorke Leec3766332013-07-31 11:13:16 -0700128 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
129 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
130 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
131 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700132
Chiao Cheng94b10b52012-08-17 16:59:12 -0700133 /**
134 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
135 */
136 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700137 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700138
Yorke Leec3766332013-07-31 11:13:16 -0700139 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700140
Andrew Lee6324f702015-06-16 14:45:58 -0700141 private static final int FAB_SCALE_IN_DELAY_MS = 300;
142
Nancy Chenf3d9f822015-09-11 00:47:31 -0400143 private CoordinatorLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700144
Yorke Leec3766332013-07-31 11:13:16 -0700145 /**
Yorke Leec3766332013-07-31 11:13:16 -0700146 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700147 */
Evan Charlton72d66252014-11-07 16:23:47 -0800148 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700149
150 /**
151 * Fragment for searching phone numbers using the alphanumeric keyboard.
152 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700153 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700154
155 /**
156 * Fragment for searching phone numbers using the dialpad.
157 */
158 private SmartDialSearchFragment mSmartDialSearchFragment;
159
Yorke Leee00c9fe2014-04-12 12:42:06 -0700160 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700161 * Animation that slides in.
162 */
163 private Animation mSlideIn;
164
165 /**
166 * Animation that slides out.
167 */
168 private Animation mSlideOut;
169
Yorke Lee6c450eb2015-05-08 09:45:47 -0700170 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
171 @Override
172 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700173 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700174 }
175 };
176
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700177 /**
178 * Listener for after slide out animation completes on dialer fragment.
179 */
180 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
181 @Override
182 public void onAnimationEnd(Animation animation) {
183 commitDialpadFragmentHide();
184 }
185 };
186
187 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700188 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700189 */
190 private ListsFragment mListsFragment;
191
Yorke Lee19e68ca2014-10-28 11:51:40 -0700192 /**
193 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
194 * be commited.
195 */
196 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800197 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700198 private boolean mInDialpadSearch;
199 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700200 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700201 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700202 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700203
Yorke Leeef2b7382013-08-09 17:39:25 -0700204 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700205 * Whether or not the device is in landscape orientation.
206 */
207 private boolean mIsLandscape;
208
209 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700210 * True if the dialpad is only temporarily showing due to being in call
211 */
212 private boolean mInCallDialpadUp;
213
214 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700215 * True when this activity has been launched for the first time.
216 */
Yorke Lee98702de2013-08-06 12:03:32 -0700217 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700218
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700219 /**
220 * Search query to be applied to the SearchView in the ActionBar once
221 * onCreateOptionsMenu has been called.
222 */
223 private String mPendingSearchViewQuery;
224
Yorke Lee74edcdc2014-07-11 16:15:08 -0700225 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700226 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700227 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700228
Yorke Leeef2b7382013-08-09 17:39:25 -0700229 private String mSearchQuery;
230
Yorke Leefce269a2013-08-12 11:56:04 -0700231 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700232 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700233 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700234
Sai Cheemalapati41460652014-06-02 21:05:39 -0700235 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700236
Andrew Lee2423a2f2014-05-29 14:14:45 -0700237 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700238
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800239 /**
240 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
241 * {@link #onResume()} to populate the search box.
242 */
243 private String mVoiceSearchQuery;
244
Andrew Lee467de3d2015-03-17 15:47:24 -0700245 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700246 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700247 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700248 }
249
250 @Override
251 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700252 final boolean hasContactsPermission =
253 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700254 final Menu menu = getMenu();
255 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700256 clearFrequents.setVisible(mListsFragment != null &&
257 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700258 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
259
260 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
261 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700262
263 menu.findItem(R.id.menu_history).setVisible(
264 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700265 super.show();
266 }
267 }
268
Chiao Cheng94b10b52012-08-17 16:59:12 -0700269 /**
Yorke Lee28266192014-05-01 10:05:52 -0700270 * Listener that listens to drag events and sends their x and y coordinates to a
271 * {@link DragDropController}.
272 */
273 private class LayoutOnDragListener implements OnDragListener {
274 @Override
275 public boolean onDrag(View v, DragEvent event) {
276 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700277 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700278 }
279 return true;
280 }
281 }
282
283 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700284 * Listener used to send search queries to the phone search fragment.
285 */
Yorke Lee53a22302014-04-29 18:13:46 -0700286 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700287 @Override
288 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
289 }
Yorke Leec3766332013-07-31 11:13:16 -0700290
Andrew Lee99a570c2014-05-15 14:18:50 -0700291 @Override
292 public void onTextChanged(CharSequence s, int start, int before, int count) {
293 final String newText = s.toString();
294 if (newText.equals(mSearchQuery)) {
295 // If the query hasn't changed (perhaps due to activity being destroyed
296 // and restored, or user launching the same DIAL intent twice), then there is
297 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700298 return;
299 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700300 if (DEBUG) {
301 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700302 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700303 }
Yorke Lee710709d2014-05-29 07:18:42 -0700304 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700305
Andrew Lee90374a72014-05-16 15:01:09 -0700306 // Show search fragment only when the query string is changed to non-empty text.
307 if (!TextUtils.isEmpty(newText)) {
308 // Call enterSearchUi only if we are switching search modes, or showing a search
309 // fragment for the first time.
310 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
311 (!mIsDialpadShown && mInRegularSearch);
312 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700313 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700314 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700315 }
316
Andrew Leea8515422014-06-13 16:53:54 -0700317 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700318 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700319 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700320 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700321 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700322 }
323
324 @Override
325 public void afterTextChanged(Editable s) {
326 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700327 };
328
Andrew Leeb1903842014-05-15 16:11:24 -0700329
330 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700331 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700332 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700333 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700334 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700335 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700336 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700337 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700338 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
339 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700340 }
Andrew Leeb1903842014-05-15 16:11:24 -0700341 }
342 };
343
344 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700345 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700346 */
347 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
348 @Override
349 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700350 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
351 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
352 // If the search term is empty, close the search UI.
353 maybeExitSearchUi();
354 } else {
355 // If the search term is not empty, show the dialpad fab.
356 showFabInSearchUi();
357 }
Andrew Leeb1903842014-05-15 16:11:24 -0700358 }
359 return false;
360 }
361 };
362
Chiao Cheng94b10b52012-08-17 16:59:12 -0700363 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700364 public boolean dispatchTouchEvent(MotionEvent ev) {
365 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
366 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
367 }
368 return super.dispatchTouchEvent(ev);
369
370 }
371
372 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700373 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800374 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700375 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700376
Yorke Lee98702de2013-08-06 12:03:32 -0700377 mFirstLaunch = true;
378
Andrew Lee2423a2f2014-05-29 14:14:45 -0700379 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700380 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700381
Yorke Leef5554ce2015-01-27 14:46:02 -0800382 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700383 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800384 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800385 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700386
Yorke Leef5554ce2015-01-27 14:46:02 -0800387 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700388 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700389 actionBar.setCustomView(R.layout.search_edittext);
390 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700391 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700392
Anne Rong35208002015-07-22 16:27:37 -0700393 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
394 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700395 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
396
Andrew Leee3f59a82015-02-26 12:06:35 -0800397 mActionBarController = new ActionBarController(this, searchEditTextLayout);
398
Andrew Lee04675a52014-06-05 18:36:20 -0700399 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700400 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700401 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
402 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
403 .setOnClickListener(mSearchViewOnClickListener);
404 searchEditTextLayout.findViewById(R.id.search_box_start_search)
405 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700406 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700407 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700408 @Override
409 public void onBackButtonClicked() {
410 onBackPressed();
411 }
Andrew Lee6324f702015-06-16 14:45:58 -0700412
413 @Override
414 public void onSearchViewClicked() {
415 // Hide FAB, as the keyboard is shown.
416 mFloatingActionButtonController.scaleOut();
417 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700418 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700419
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700420 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700421 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700422
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700423 final View floatingActionButtonContainer = findViewById(
424 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700425 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
426 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700427 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700428 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700429
Andrew Lee04675a52014-06-05 18:36:20 -0700430 ImageButton optionsMenuButton =
431 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700432 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700433 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
434 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700435
Yorke Leed5c512a2015-01-30 14:42:53 -0800436 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
437 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700438 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800439 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000440 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800441 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700442 } else {
443 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700444 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
445 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700446 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700447 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700448 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700449 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700450
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700451 final boolean isLayoutRtl = DialerUtils.isRtl();
452 if (mIsLandscape) {
453 mSlideIn = AnimationUtils.loadAnimation(this,
454 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
455 mSlideOut = AnimationUtils.loadAnimation(this,
456 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
457 } else {
458 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
459 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
460 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700461
Yorke Lee2fec47b2014-08-05 18:16:27 -0700462 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
463 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
464
Yorke Lee6c450eb2015-05-08 09:45:47 -0700465 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700466 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700467
Nancy Chenf3d9f822015-09-11 00:47:31 -0400468 mParentLayout = (CoordinatorLayout) findViewById(R.id.dialtacts_mainlayout);
Yorke Leef614f6f2014-10-03 10:44:19 -0700469 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700470 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700471 new ViewTreeObserver.OnGlobalLayoutListener() {
472 @Override
473 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700474 final ViewTreeObserver observer =
475 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700476 if (!observer.isAlive()) {
477 return;
478 }
479 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700480 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700481 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700482 mFloatingActionButtonController.align(
483 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700484 }
485 });
Andrew Lee0c667702014-05-12 14:31:45 -0700486
Yorke Leef5554ce2015-01-27 14:46:02 -0800487 Trace.endSection();
488
489 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700490 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700491 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800492 Trace.endSection();
493 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700494 }
495
496 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700497 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800498 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700499 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700500
Yorke Lee19e68ca2014-10-28 11:51:40 -0700501 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700502 if (mFirstLaunch) {
503 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700504 } else if (!phoneIsInUse() && mInCallDialpadUp) {
505 hideDialpadFragment(false, true);
506 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700507 } else if (mShowDialpadOnResume) {
508 showDialpadFragment(false);
509 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700510 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800511
512 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
513 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
514 // shown until onResume has completed. Active the search UI and set the search term now.
515 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
516 mActionBarController.onSearchBoxTapped();
517 mSearchView.setText(mVoiceSearchQuery);
518 mVoiceSearchQuery = null;
519 }
520
Yorke Lee98702de2013-08-06 12:03:32 -0700521 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800522
523 if (mIsRestarting) {
524 // This is only called when the activity goes from resumed -> paused -> resumed, so it
525 // will not cause an extra view to be sent out on rotation
526 if (mIsDialpadShown) {
527 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
528 }
529 mIsRestarting = false;
530 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700531
Yorke Lee53a22302014-04-29 18:13:46 -0700532 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700533 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700534 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700535
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700536 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
537 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
538 // used internally.
539 final Bundle extras = getIntent().getExtras();
540 if (extras != null
541 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
542 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
543 } else {
544 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
545 }
546 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700547 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
548 if (index < mListsFragment.getTabCount()) {
549 mListsFragment.showTab(index);
550 }
551 }
552
Anne Rong35208002015-07-22 16:27:37 -0700553 setSearchBoxHint();
554
Yorke Leef5554ce2015-01-27 14:46:02 -0800555 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700556 }
557
558 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800559 protected void onRestart() {
560 super.onRestart();
561 mIsRestarting = true;
562 }
563
564 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700565 protected void onPause() {
566 if (mClearSearchOnPause) {
567 hideDialpadAndSearchUi();
568 mClearSearchOnPause = false;
569 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700570 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
571 commitDialpadFragmentHide();
572 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700573 super.onPause();
574 }
575
576 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700577 protected void onSaveInstanceState(Bundle outState) {
578 super.onSaveInstanceState(outState);
579 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700580 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
581 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700582 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700583 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700584 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700585 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700586 }
587
588 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700589 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700590 if (fragment instanceof DialpadFragment) {
591 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800592 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700593 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
594 transaction.hide(mDialpadFragment);
595 transaction.commit();
596 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700597 } else if (fragment instanceof SmartDialSearchFragment) {
598 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700599 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700600 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700601 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700602 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700603 } else if (fragment instanceof ListsFragment) {
604 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700605 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700606 }
Yorke Leec3766332013-07-31 11:13:16 -0700607 }
608
Alon Albertb453e5b2013-09-10 15:54:23 -0700609 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700610 final Intent intent = new Intent(this, DialerSettingsActivity.class);
611 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700612 }
613
Chiao Cheng94b10b52012-08-17 16:59:12 -0700614 @Override
615 public void onClick(View view) {
616 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700617 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700618 if (mListsFragment.getCurrentTabIndex()
619 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700620 DialerUtils.startActivityWithErrorToast(
621 this,
622 IntentUtil.getNewContactIntent(),
623 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700624 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700625 mInCallDialpadUp = false;
626 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700627 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700628 break;
Yorke Leec3766332013-07-31 11:13:16 -0700629 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800630 try {
631 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
632 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
633 } catch (ActivityNotFoundException e) {
634 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
635 Toast.LENGTH_SHORT).show();
636 }
Yorke Leec3766332013-07-31 11:13:16 -0700637 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700638 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700639 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700640 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700641 default: {
642 Log.wtf(TAG, "Unexpected onClick event from " + view);
643 break;
644 }
645 }
646 }
647
Yorke Leec3766332013-07-31 11:13:16 -0700648 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700649 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700650 switch (item.getItemId()) {
651 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700652 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
653 // CONTENT_TYPE, so that we always open our call log from our dialer
654 final Intent intent = new Intent(this, CallLogActivity.class);
655 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700656 break;
657 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700658 DialerUtils.startActivityWithErrorToast(
659 this,
660 IntentUtil.getNewContactIntent(),
661 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700662 break;
663 case R.id.menu_import_export:
664 // We hard-code the "contactsAreAvailable" argument because doing it properly would
665 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
666 // now in Dialtacts for (potential) performance reasons. Compare with how it is
667 // done in {@link PeopleActivity}.
668 ImportExportDialogFragment.show(getFragmentManager(), true,
669 DialtactsActivity.class);
670 return true;
671 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700672 ClearFrequentsDialog.show(getFragmentManager());
673 return true;
674 case R.id.menu_call_settings:
675 handleMenuSettings();
676 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700677 }
678 return false;
679 }
680
681 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700682 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
683 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
684 if (resultCode == RESULT_OK) {
685 final ArrayList<String> matches = data.getStringArrayListExtra(
686 RecognizerIntent.EXTRA_RESULTS);
687 if (matches.size() > 0) {
688 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800689 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700690 } else {
691 Log.e(TAG, "Voice search - nothing heard");
692 }
693 } else {
694 Log.e(TAG, "Voice search failed");
695 }
696 }
697 super.onActivityResult(requestCode, resultCode, data);
698 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700699
Andrew Lee2a58ab82014-05-15 02:16:04 -0700700 /**
Nancy Chenea7c0b82015-08-31 17:18:41 -0700701 * Update the number of unread voicemails (potentially other tabs) displayed next to the tab
702 * icon.
703 */
704 public void updateTabUnreadCounts() {
705 mListsFragment.updateTabUnreadCounts();
706 }
707
708 /**
Andrew Lee2a58ab82014-05-15 02:16:04 -0700709 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
710 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
711 * @see #onDialpadShown
712 */
Yorke Leec3766332013-07-31 11:13:16 -0700713 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700714 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700715 return;
716 }
717 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800718
Yorke Lee7ee5c422014-11-04 10:29:32 -0800719 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700720
Chiao Cheng94b10b52012-08-17 16:59:12 -0700721 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800722 if (mDialpadFragment == null) {
723 mDialpadFragment = new DialpadFragment();
724 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
725 } else {
726 ft.show(mDialpadFragment);
727 }
728
729 mDialpadFragment.setAnimate(animate);
730 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700731 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700732
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700733 if (animate) {
734 mFloatingActionButtonController.scaleOut();
735 } else {
736 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700737 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700738 }
Yorke Lee5253be02014-05-21 18:50:03 -0700739 mActionBarController.onDialpadUp();
740
Yorke Lee6c450eb2015-05-08 09:45:47 -0700741 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700742 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700743
Andrew Lee2a58ab82014-05-15 02:16:04 -0700744 /**
745 * Callback from child DialpadFragment when the dialpad is shown.
746 */
747 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800748 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700749 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700750 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700751 } else {
752 mDialpadFragment.setYFraction(0);
753 }
754
Andrew Lee2a58ab82014-05-15 02:16:04 -0700755 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700756 }
757
758 /**
759 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
760 * a callback after the hide animation ends.
761 * @see #commitDialpadFragmentHide
762 */
Yorke Leeca195042013-09-25 16:29:38 -0700763 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700764 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700765 return;
766 }
Yorke Leef6673d32013-08-23 15:34:17 -0700767 if (clearDialpad) {
768 mDialpadFragment.clearDialpad();
769 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700770 if (!mIsDialpadShown) {
771 return;
Yorke Leec3766332013-07-31 11:13:16 -0700772 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700773 mIsDialpadShown = false;
774 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800775 mListsFragment.setUserVisibleHint(true);
776 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700777
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700778 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700779
Andrew Lee6324f702015-06-16 14:45:58 -0700780 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700781 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700782 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700783 } else {
784 commitDialpadFragmentHide();
785 }
786
Yorke Lee5253be02014-05-21 18:50:03 -0700787 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700788
Andrew Leed4cde832014-05-16 15:56:48 -0700789 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700790 if (TextUtils.isEmpty(mSearchQuery)) {
791 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700792 }
Andrew Leed4cde832014-05-16 15:56:48 -0700793 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700794 }
795
796 /**
797 * Finishes hiding the dialpad fragment after any animations are completed.
798 */
799 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800800 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700801 final FragmentTransaction ft = getFragmentManager().beginTransaction();
802 ft.hide(mDialpadFragment);
803 ft.commit();
804 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700805 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700806 }
807
Andrew Lee2a58ab82014-05-15 02:16:04 -0700808 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700809 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700810 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700811 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700812 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700813 fragment = mRegularSearchFragment;
814 }
815 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700816 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700817 }
818 }
819
Yorke Lee5253be02014-05-21 18:50:03 -0700820 @Override
821 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700822 return mInDialpadSearch || mInRegularSearch;
823 }
824
Yorke Lee5253be02014-05-21 18:50:03 -0700825 @Override
826 public boolean hasSearchQuery() {
827 return !TextUtils.isEmpty(mSearchQuery);
828 }
829
830 @Override
831 public boolean shouldShowActionBar() {
832 return mListsFragment.shouldShowActionBar();
833 }
834
Yorke Leeb207f8a2013-08-29 18:51:06 -0700835 private void setNotInSearchUi() {
836 mInDialpadSearch = false;
837 mInRegularSearch = false;
838 }
839
Yorke Lee311969c2013-08-26 06:31:11 -0700840 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700841 if (mIsDialpadShown) {
842 hideDialpadFragment(false, true);
843 } else {
844 exitSearchUi();
845 }
Yorke Lee311969c2013-08-26 06:31:11 -0700846 }
847
Yorke Lee53a22302014-04-29 18:13:46 -0700848 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700849 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
850 if (canIntentBeHandled(voiceIntent)) {
851 mVoiceSearchButton.setVisibility(View.VISIBLE);
852 mVoiceSearchButton.setOnClickListener(this);
853 } else {
854 mVoiceSearchButton.setVisibility(View.GONE);
855 }
856 }
857
Anne Rong35208002015-07-22 16:27:37 -0700858 protected int getSearchBoxHint () {
859 return R.string.dialer_hint_find_contact;
860 }
861
862 /**
863 * Sets the hint text for the contacts search box
864 */
865 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700866 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700867 .getCustomView().findViewById(R.id.search_view_container);
868 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
869 .setHint(getSearchBoxHint());
870 }
871
Andrew Lee467de3d2015-03-17 15:47:24 -0700872 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700873 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
874 popupMenu.inflate(R.menu.dialtacts_options);
875 popupMenu.setOnMenuItemClickListener(this);
876 return popupMenu;
877 }
878
Yorke Lee86e21f72014-04-02 16:49:38 -0700879 @Override
880 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700881 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700882 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700883 mPendingSearchViewQuery = null;
884 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700885 if (mActionBarController != null) {
886 mActionBarController.restoreActionBarOffset();
887 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700888 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700889 }
890
Chiao Cheng94b10b52012-08-17 16:59:12 -0700891 /**
892 * Returns true if the intent is due to hitting the green send key (hardware call button:
893 * KEYCODE_CALL) while in a call.
894 *
895 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700896 * @return true if the intent is due to hitting the green send key while in a call
897 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700898 private boolean isSendKeyWhileInCall(Intent intent) {
899 // If there is a call in progress and the user launched the dialer by hitting the call
900 // button, go straight to the in-call screen.
901 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700902
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700903 if (callKey) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700904 getTelecomManager().showInCallScreen(false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700905 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700906 }
907
908 return false;
909 }
910
911 /**
912 * Sets the current tab based on the intent's request type
913 *
914 * @param intent Intent that contains information about which tab should be selected
915 */
Yorke Leec3766332013-07-31 11:13:16 -0700916 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700917 // 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 -0700918 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700919 finish();
920 return;
921 }
922
Yorke Leed77017d2015-08-31 16:19:43 -0700923 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
924 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800925 showDialpadFragment(false);
926 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700927 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800928 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700929 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700930 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700931 }
932
933 @Override
934 public void onNewIntent(Intent newIntent) {
935 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700936
Nancy Chen199b7f02014-11-05 15:03:00 -0800937 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700938 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700939
Chiao Cheng94b10b52012-08-17 16:59:12 -0700940 invalidateOptionsMenu();
941 }
942
943 /** Returns true if the given intent contains a phone number to populate the dialer with */
944 private boolean isDialIntent(Intent intent) {
945 final String action = intent.getAction();
946 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
947 return true;
948 }
949 if (Intent.ACTION_VIEW.equals(action)) {
950 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700951 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700952 return true;
953 }
954 }
955 return false;
956 }
957
958 /**
Yorke Leec3766332013-07-31 11:13:16 -0700959 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700960 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700961 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700962 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700963 // Weird race condition where fragment is doing work after the activity is destroyed
964 // due to talkback being on (b/10209937). Just return since we can't do any
965 // constructive here.
966 return;
967 }
968
Yorke Leeef2b7382013-08-09 17:39:25 -0700969 if (DEBUG) {
970 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
971 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700972
Yorke Leec3766332013-07-31 11:13:16 -0700973 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700974 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700975 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700976 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700977 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700978 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700979
Yorke Leeb207f8a2013-08-29 18:51:06 -0700980 final String tag;
981 if (smartDialSearch) {
982 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
983 } else {
984 tag = TAG_REGULAR_SEARCH_FRAGMENT;
985 }
986 mInDialpadSearch = smartDialSearch;
987 mInRegularSearch = !smartDialSearch;
988
Andrew Lee6324f702015-06-16 14:45:58 -0700989 mFloatingActionButtonController.scaleOut();
990
Andrew Lee752956e2014-05-15 11:43:38 -0700991 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700992 if (animate) {
993 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
994 } else {
995 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
996 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700997 if (fragment == null) {
998 if (smartDialSearch) {
999 fragment = new SmartDialSearchFragment();
1000 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -07001001 fragment = new RegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001002 fragment.setOnTouchListener(new View.OnTouchListener() {
1003 @Override
1004 public boolean onTouch(View v, MotionEvent event) {
1005 // Show the FAB when the user touches the lists fragment and the soft
1006 // keyboard is hidden.
1007 showFabInSearchUi();
1008 return false;
1009 }
1010 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001011 }
Andrew Leeb1903842014-05-15 16:11:24 -07001012 transaction.add(R.id.dialtacts_frame, fragment, tag);
1013 } else {
1014 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001015 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001016 // DialtactsActivity will provide the options menu
1017 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001018 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001019 if (!smartDialSearch) {
1020 fragment.setQueryString(query, false /* delaySelection */);
1021 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001022 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001023
Yorke Lee6c450eb2015-05-08 09:45:47 -07001024 if (animate) {
1025 mListsFragment.getView().animate().alpha(0).withLayer();
1026 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001027 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001028 }
1029
1030 /**
Yorke Leec3766332013-07-31 11:13:16 -07001031 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001032 */
Yorke Leec3766332013-07-31 11:13:16 -07001033 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001034 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001035 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001036 return;
1037 }
Andrew Leeb1903842014-05-15 16:11:24 -07001038
Andrew Leeb1903842014-05-15 16:11:24 -07001039 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001040
1041 if (mDialpadFragment != null) {
1042 mDialpadFragment.clearDialpad();
1043 }
1044
Yorke Leeb207f8a2013-08-29 18:51:06 -07001045 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001046
Andrew Lee6324f702015-06-16 14:45:58 -07001047 // Restore the FAB for the lists fragment.
1048 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1049 mFloatingActionButtonController.setVisible(false);
1050 }
1051 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1052 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1053 onPageSelected(mListsFragment.getCurrentTabIndex());
1054
Andrew Leeb1903842014-05-15 16:11:24 -07001055 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001056 if (mSmartDialSearchFragment != null) {
1057 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001058 }
Andrew Leeb1903842014-05-15 16:11:24 -07001059 if (mRegularSearchFragment != null) {
1060 transaction.remove(mRegularSearchFragment);
1061 }
1062 transaction.commit();
1063
1064 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001065
1066 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001067 // If the dialpad fragment wasn't previously visible, then send a screen view because
1068 // we are exiting regular search. Otherwise, the screen view will be sent by
1069 // {@link #hideDialpadFragment}.
1070 mListsFragment.sendScreenViewForCurrentPosition();
1071 mListsFragment.setUserVisibleHint(true);
1072 }
1073
Yorke Lee5253be02014-05-21 18:50:03 -07001074 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001075 }
1076
Chiao Cheng94b10b52012-08-17 16:59:12 -07001077 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001078 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001079 if (mStateSaved) {
1080 return;
1081 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001082 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001083 if (TextUtils.isEmpty(mSearchQuery) ||
1084 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1085 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001086 exitSearchUi();
1087 }
Yorke Leef6673d32013-08-23 15:34:17 -07001088 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001089 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001090 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001091 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001092 } else {
1093 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001094 }
Yorke Leec3766332013-07-31 11:13:16 -07001095 }
1096
Yorke Lee81a313d2015-06-01 14:53:59 -07001097 private void maybeEnterSearchUi() {
1098 if (!isInSearchUi()) {
1099 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1100 }
1101 }
1102
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001103 /**
1104 * @return True if the search UI was exited, false otherwise
1105 */
1106 private boolean maybeExitSearchUi() {
1107 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1108 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001109 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001110 return true;
1111 }
1112 return false;
1113 }
1114
Andrew Lee6324f702015-06-16 14:45:58 -07001115 private void showFabInSearchUi() {
1116 mFloatingActionButtonController.changeIcon(
1117 getResources().getDrawable(R.drawable.fab_ic_dial),
1118 getResources().getString(R.string.action_menu_dialpad_button));
1119 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1120 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1121 }
1122
Yorke Leec3766332013-07-31 11:13:16 -07001123 @Override
1124 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001125 if (mSmartDialSearchFragment != null) {
1126 mSmartDialSearchFragment.setAddToContactNumber(query);
1127 }
Yorke Leec3766332013-07-31 11:13:16 -07001128 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1129 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001130
1131 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001132 if (DEBUG) {
1133 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1134 }
1135 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1136 // This callback can happen if the dialpad fragment is recreated because of
1137 // activity destruction. In that case, don't update the search view because
1138 // that would bring the user back to the search fragment regardless of the
1139 // previous state of the application. Instead, just return here and let the
1140 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001141 if (!TextUtils.isEmpty(normalizedQuery)) {
1142 mPendingSearchViewQuery = normalizedQuery;
1143 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001144 return;
1145 }
Yorke Lee53a22302014-04-29 18:13:46 -07001146 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001147 }
Santos Cordon83131712015-04-16 21:25:35 -07001148
1149 try {
1150 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1151 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1152 }
1153 } catch (Exception ignored) {
1154 // Skip any exceptions for this piece of code
1155 }
Yorke Lee575ae382015-07-16 11:36:07 -07001156 }
Santos Cordon83131712015-04-16 21:25:35 -07001157
Yorke Lee575ae382015-07-16 11:36:07 -07001158 @Override
1159 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1160 if (mInDialpadSearch && mSmartDialSearchFragment != null
1161 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1162 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1163 return true;
1164 }
1165 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001166 }
Yorke Leec3766332013-07-31 11:13:16 -07001167
1168 @Override
1169 public void onListFragmentScrollStateChange(int scrollState) {
1170 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001171 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001172 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001173 }
1174 }
1175
1176 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001177 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001178 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001179 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1180 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001181 }
1182
Yorke Leec3766332013-07-31 11:13:16 -07001183 private boolean phoneIsInUse() {
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001184 return getTelecomManager().isInCall();
Yorke Leec3766332013-07-31 11:13:16 -07001185 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001186
Yorke Leeda0f9042013-12-05 14:25:51 -08001187 private boolean canIntentBeHandled(Intent intent) {
1188 final PackageManager packageManager = getPackageManager();
1189 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1190 PackageManager.MATCH_DEFAULT_ONLY);
1191 return resolveInfo != null && resolveInfo.size() > 0;
1192 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001193
Yorke Lee86e21f72014-04-02 16:49:38 -07001194 /**
1195 * Called when the user has long-pressed a contact tile to start a drag operation.
1196 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001197 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001198 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001199 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001200 }
1201
1202 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001203 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1204 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001205
Yorke Lee86e21f72014-04-02 16:49:38 -07001206 /**
1207 * Called when the user has released a contact tile after long-pressing it.
1208 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001209 @Override
1210 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001211 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001212 }
1213
1214 @Override
1215 public void onDroppedOnRemove() {}
1216
1217 /**
Yorke Leed999b712014-04-23 15:10:58 -07001218 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001219 * once it has been attached to the activity.
1220 */
1221 @Override
1222 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001223 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001224 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001225 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001226
Yorke Lee38019af2015-07-14 17:05:38 -07001227 /**
1228 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1229 */
1230 @Override
1231 public void showAllContactsTab() {
1232 if (mListsFragment != null) {
1233 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1234 }
1235 }
1236
1237 /**
1238 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1239 */
1240 @Override
1241 public void showDialpad() {
1242 showDialpadFragment(true);
1243 }
1244
Yorke Leee00c9fe2014-04-12 12:42:06 -07001245 @Override
1246 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001247 // Specify call-origin so that users will see the previous tab instead of
1248 // CallLog screen (search UI will be automatically exited).
1249 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Lee306324b2015-09-11 12:14:12 -07001250 DialtactsActivity.this, dataUri, null);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001251 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001252 }
1253
1254 @Override
1255 public void onCallNumberDirectly(String phoneNumber) {
Andrew Leefc1fed72014-07-02 11:05:53 -07001256 onCallNumberDirectly(phoneNumber, false /* isVideoCall */);
1257 }
1258
1259 @Override
1260 public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001261 if (phoneNumber == null) {
1262 // Invalid phone number, but let the call go through so that InCallUI can show
1263 // an error message.
1264 phoneNumber = "";
1265 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001266
1267 final Intent intent = new CallIntentBuilder(phoneNumber)
1268 .setIsVideoCall(isVideoCall)
1269 .build();
1270
Yorke Lee7d20f822014-06-19 17:09:33 -07001271 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001272 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001273 }
1274
1275 @Override
1276 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001277 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001278 }
1279
1280 @Override
1281 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001282 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001283 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001284
Yorke Leecc4660d2014-04-24 11:22:57 -07001285 @Override
1286 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001287 int tabIndex = mListsFragment.getCurrentTabIndex();
1288
Andrew Lee4de59fb2015-08-13 15:20:08 -07001289 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1290 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1291 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001292 boolean isRtl = DialerUtils.isRtl();
1293 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1294 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001295 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001296 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1297 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001298 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001299 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001300 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001301
Andrew Lee432d4b52015-05-20 16:52:00 -07001302 @Override
1303 public void onPageSelected(int position) {
1304 int tabIndex = mListsFragment.getCurrentTabIndex();
1305 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001306 mFloatingActionButtonController.changeIcon(
1307 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1308 getResources().getString(R.string.search_shortcut_create_new_contact));
1309 } else {
1310 mFloatingActionButtonController.changeIcon(
1311 getResources().getDrawable(R.drawable.fab_ic_dial),
1312 getResources().getString(R.string.action_menu_dialpad_button));
1313 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001314 }
1315
1316 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001317 public void onPageScrollStateChanged(int state) {
1318 }
1319
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001320 private TelecomManager getTelecomManager() {
1321 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordond15629d2014-07-07 15:15:29 -07001322 }
1323
Yorke Lee5253be02014-05-21 18:50:03 -07001324 @Override
1325 public boolean isActionBarShowing() {
1326 return mActionBarController.isActionBarShowing();
1327 }
1328
Yorke Leec98a5bb2014-10-28 16:12:05 -07001329 @Override
1330 public ActionBarController getActionBarController() {
1331 return mActionBarController;
1332 }
1333
Yorke Lee6bc67162015-06-27 14:03:25 -07001334 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001335 public boolean isDialpadShown() {
1336 return mIsDialpadShown;
1337 }
1338
Yorke Lee5253be02014-05-21 18:50:03 -07001339 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001340 public int getDialpadHeight() {
1341 if (mDialpadFragment != null) {
1342 return mDialpadFragment.getDialpadHeight();
1343 }
1344 return 0;
1345 }
1346
1347 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001348 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001349 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001350 }
1351
1352 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001353 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001354 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001355 }
1356
1357 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001358 public int getActionBarHeight() {
1359 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001360 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001361
Andrew Lee6324f702015-06-16 14:45:58 -07001362 private int getFabAlignment() {
1363 if (!mIsLandscape && !isInSearchUi() &&
1364 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1365 return FloatingActionButtonController.ALIGN_MIDDLE;
1366 }
1367 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001368 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001369}