blob: 786acab1a3670edfc73e3c335932426352898866 [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;
Yorke Leecc4660d2014-04-24 11:22:57 -070033import android.support.v4.view.ViewPager;
Nancy Chen44898ad2015-08-13 12:03:47 -070034import android.support.v7.app.ActionBar;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070035import android.telecom.PhoneAccount;
36import android.telecom.TelecomManager;
Yorke Lee53a22302014-04-29 18:13:46 -070037import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070039import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070040import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070041import android.view.DragEvent;
Yorke Lee74edcdc2014-07-11 16:15:08 -070042import android.view.Gravity;
Andrew Leeb1903842014-05-15 16:11:24 -070043import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070046import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070048import android.view.View.OnDragListener;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070049import android.view.View.OnTouchListener;
Ihab Awad9134e252014-07-09 12:32:52 -070050import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070051import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070052import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070053import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070054import android.widget.EditText;
Yorke Lee28aab272014-06-11 10:24:19 -070055import android.widget.FrameLayout;
Andrew Leea73e1892014-05-13 13:21:16 -070056import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.widget.PopupMenu;
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 Lee7d20f822014-06-19 17:09:33 -070086import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070087import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070088import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070089import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070090import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee2fec47b2014-08-05 18:16:27 -070091import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070092import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070093
Yorke Leed5c512a2015-01-30 14:42:53 -080094import junit.framework.Assert;
95
Yorke Leec3766332013-07-31 11:13:16 -070096import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080097import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070098
Chiao Cheng94b10b52012-08-17 16:59:12 -070099/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700100 * The dialer tab's title is 'phone', a more common name (see strings.xml).
101 */
Yorke Leec3766332013-07-31 11:13:16 -0700102public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700103 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700104 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700105 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700106 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700107 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700108 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700109 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700110 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700111 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700112 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700113 ViewPager.OnPageChangeListener,
114 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 private static final String TAG = "DialtactsActivity";
116
Yorke Lee7cc61492015-06-01 14:59:33 -0700117 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118
Yorke Leef74011e2013-08-02 11:30:30 -0700119 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
120
Yorke Leeb207f8a2013-08-29 18:51:06 -0700121 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
122 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700123 private static final String KEY_SEARCH_QUERY = "search_query";
124 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700125 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700126
Yorke Leec3766332013-07-31 11:13:16 -0700127 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
128 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
129 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
130 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700131
Chiao Cheng94b10b52012-08-17 16:59:12 -0700132 /**
133 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
134 */
135 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700136 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700137
Yorke Leec3766332013-07-31 11:13:16 -0700138 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700139
Andrew Lee6324f702015-06-16 14:45:58 -0700140 private static final int FAB_SCALE_IN_DELAY_MS = 300;
141
Yorke Leef614f6f2014-10-03 10:44:19 -0700142 private FrameLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700143
Yorke Leec3766332013-07-31 11:13:16 -0700144 /**
Yorke Leec3766332013-07-31 11:13:16 -0700145 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700146 */
Evan Charlton72d66252014-11-07 16:23:47 -0800147 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700148
149 /**
150 * Fragment for searching phone numbers using the alphanumeric keyboard.
151 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700152 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700153
154 /**
155 * Fragment for searching phone numbers using the dialpad.
156 */
157 private SmartDialSearchFragment mSmartDialSearchFragment;
158
Yorke Leee00c9fe2014-04-12 12:42:06 -0700159 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700160 * Animation that slides in.
161 */
162 private Animation mSlideIn;
163
164 /**
165 * Animation that slides out.
166 */
167 private Animation mSlideOut;
168
Yorke Lee6c450eb2015-05-08 09:45:47 -0700169 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
170 @Override
171 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700172 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700173 }
174 };
175
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700176 /**
177 * Listener for after slide out animation completes on dialer fragment.
178 */
179 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
180 @Override
181 public void onAnimationEnd(Animation animation) {
182 commitDialpadFragmentHide();
183 }
184 };
185
186 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700187 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700188 */
189 private ListsFragment mListsFragment;
190
Yorke Lee19e68ca2014-10-28 11:51:40 -0700191 /**
192 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
193 * be commited.
194 */
195 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800196 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700197 private boolean mInDialpadSearch;
198 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700199 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700200 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700201 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700202
Yorke Leeef2b7382013-08-09 17:39:25 -0700203 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700204 * Whether or not the device is in landscape orientation.
205 */
206 private boolean mIsLandscape;
207
208 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700209 * True if the dialpad is only temporarily showing due to being in call
210 */
211 private boolean mInCallDialpadUp;
212
213 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700214 * True when this activity has been launched for the first time.
215 */
Yorke Lee98702de2013-08-06 12:03:32 -0700216 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700217
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700218 /**
219 * Search query to be applied to the SearchView in the ActionBar once
220 * onCreateOptionsMenu has been called.
221 */
222 private String mPendingSearchViewQuery;
223
Yorke Lee74edcdc2014-07-11 16:15:08 -0700224 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700225 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700226 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700227
Yorke Leeef2b7382013-08-09 17:39:25 -0700228 private String mSearchQuery;
229
Yorke Leefce269a2013-08-12 11:56:04 -0700230 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700231 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700232 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700233
Sai Cheemalapati41460652014-06-02 21:05:39 -0700234 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700235
Andrew Lee2423a2f2014-05-29 14:14:45 -0700236 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700237
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800238 /**
239 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
240 * {@link #onResume()} to populate the search box.
241 */
242 private String mVoiceSearchQuery;
243
Andrew Lee467de3d2015-03-17 15:47:24 -0700244 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700245 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700246 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700247 }
248
249 @Override
250 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700251 final boolean hasContactsPermission =
252 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700253 final Menu menu = getMenu();
254 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700255 clearFrequents.setVisible(mListsFragment != null &&
256 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700257 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
258
259 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
260 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700261
262 menu.findItem(R.id.menu_history).setVisible(
263 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700264 super.show();
265 }
266 }
267
Chiao Cheng94b10b52012-08-17 16:59:12 -0700268 /**
Yorke Lee28266192014-05-01 10:05:52 -0700269 * Listener that listens to drag events and sends their x and y coordinates to a
270 * {@link DragDropController}.
271 */
272 private class LayoutOnDragListener implements OnDragListener {
273 @Override
274 public boolean onDrag(View v, DragEvent event) {
275 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700276 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700277 }
278 return true;
279 }
280 }
281
282 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700283 * Listener used to send search queries to the phone search fragment.
284 */
Yorke Lee53a22302014-04-29 18:13:46 -0700285 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700286 @Override
287 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
288 }
Yorke Leec3766332013-07-31 11:13:16 -0700289
Andrew Lee99a570c2014-05-15 14:18:50 -0700290 @Override
291 public void onTextChanged(CharSequence s, int start, int before, int count) {
292 final String newText = s.toString();
293 if (newText.equals(mSearchQuery)) {
294 // If the query hasn't changed (perhaps due to activity being destroyed
295 // and restored, or user launching the same DIAL intent twice), then there is
296 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700297 return;
298 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700299 if (DEBUG) {
300 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700301 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700302 }
Yorke Lee710709d2014-05-29 07:18:42 -0700303 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700304
Andrew Lee90374a72014-05-16 15:01:09 -0700305 // Show search fragment only when the query string is changed to non-empty text.
306 if (!TextUtils.isEmpty(newText)) {
307 // Call enterSearchUi only if we are switching search modes, or showing a search
308 // fragment for the first time.
309 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
310 (!mIsDialpadShown && mInRegularSearch);
311 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700312 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700313 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700314 }
315
Andrew Leea8515422014-06-13 16:53:54 -0700316 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700317 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700318 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700319 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700320 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700321 }
322
323 @Override
324 public void afterTextChanged(Editable s) {
325 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700326 };
327
Andrew Leeb1903842014-05-15 16:11:24 -0700328
329 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700330 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700331 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700332 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700333 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700334 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700335 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700336 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700337 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
338 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700339 }
Andrew Leeb1903842014-05-15 16:11:24 -0700340 }
341 };
342
343 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700344 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700345 */
346 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
347 @Override
348 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700349 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
350 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
351 // If the search term is empty, close the search UI.
352 maybeExitSearchUi();
353 } else {
354 // If the search term is not empty, show the dialpad fab.
355 showFabInSearchUi();
356 }
Andrew Leeb1903842014-05-15 16:11:24 -0700357 }
358 return false;
359 }
360 };
361
Chiao Cheng94b10b52012-08-17 16:59:12 -0700362 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700363 public boolean dispatchTouchEvent(MotionEvent ev) {
364 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
365 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
366 }
367 return super.dispatchTouchEvent(ev);
368
369 }
370
371 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700372 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800373 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700374 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700375
Yorke Lee98702de2013-08-06 12:03:32 -0700376 mFirstLaunch = true;
377
Andrew Lee2423a2f2014-05-29 14:14:45 -0700378 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700379 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700380
Yorke Leef5554ce2015-01-27 14:46:02 -0800381 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700382 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800383 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800384 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700385
Yorke Leef5554ce2015-01-27 14:46:02 -0800386 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700387 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700388 actionBar.setCustomView(R.layout.search_edittext);
389 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700390 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700391
Anne Rong35208002015-07-22 16:27:37 -0700392 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
393 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700394 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
395
Andrew Leee3f59a82015-02-26 12:06:35 -0800396 mActionBarController = new ActionBarController(this, searchEditTextLayout);
397
Andrew Lee04675a52014-06-05 18:36:20 -0700398 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700399 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700400 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
401 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
402 .setOnClickListener(mSearchViewOnClickListener);
403 searchEditTextLayout.findViewById(R.id.search_box_start_search)
404 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700405 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700406 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700407 @Override
408 public void onBackButtonClicked() {
409 onBackPressed();
410 }
Andrew Lee6324f702015-06-16 14:45:58 -0700411
412 @Override
413 public void onSearchViewClicked() {
414 // Hide FAB, as the keyboard is shown.
415 mFloatingActionButtonController.scaleOut();
416 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700417 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700418
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700419 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700420 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700421
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700422 final View floatingActionButtonContainer = findViewById(
423 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700424 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
425 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700426 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700427 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700428
Andrew Lee04675a52014-06-05 18:36:20 -0700429 ImageButton optionsMenuButton =
430 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700431 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700432 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
433 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700434
Yorke Leed5c512a2015-01-30 14:42:53 -0800435 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
436 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700437 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800438 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000439 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800440 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700441 } else {
442 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700443 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
444 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700445 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700446 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700447 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700448 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700449
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700450 final boolean isLayoutRtl = DialerUtils.isRtl();
451 if (mIsLandscape) {
452 mSlideIn = AnimationUtils.loadAnimation(this,
453 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
454 mSlideOut = AnimationUtils.loadAnimation(this,
455 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
456 } else {
457 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
458 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
459 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700460
Yorke Lee2fec47b2014-08-05 18:16:27 -0700461 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
462 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
463
Yorke Lee6c450eb2015-05-08 09:45:47 -0700464 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700465 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700466
Yorke Leef614f6f2014-10-03 10:44:19 -0700467 mParentLayout = (FrameLayout) findViewById(R.id.dialtacts_mainlayout);
468 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700469 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700470 new ViewTreeObserver.OnGlobalLayoutListener() {
471 @Override
472 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700473 final ViewTreeObserver observer =
474 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700475 if (!observer.isAlive()) {
476 return;
477 }
478 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700479 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700480 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700481 mFloatingActionButtonController.align(
482 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700483 }
484 });
Andrew Lee0c667702014-05-12 14:31:45 -0700485
Yorke Leef5554ce2015-01-27 14:46:02 -0800486 Trace.endSection();
487
488 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700489 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700490 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800491 Trace.endSection();
492 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700493 }
494
495 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700496 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800497 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700498 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700499
Yorke Lee19e68ca2014-10-28 11:51:40 -0700500 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700501 if (mFirstLaunch) {
502 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700503 } else if (!phoneIsInUse() && mInCallDialpadUp) {
504 hideDialpadFragment(false, true);
505 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700506 } else if (mShowDialpadOnResume) {
507 showDialpadFragment(false);
508 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700509 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800510
511 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
512 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
513 // shown until onResume has completed. Active the search UI and set the search term now.
514 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
515 mActionBarController.onSearchBoxTapped();
516 mSearchView.setText(mVoiceSearchQuery);
517 mVoiceSearchQuery = null;
518 }
519
Yorke Lee98702de2013-08-06 12:03:32 -0700520 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800521
522 if (mIsRestarting) {
523 // This is only called when the activity goes from resumed -> paused -> resumed, so it
524 // will not cause an extra view to be sent out on rotation
525 if (mIsDialpadShown) {
526 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
527 }
528 mIsRestarting = false;
529 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700530
Yorke Lee53a22302014-04-29 18:13:46 -0700531 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700532 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700533 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700534
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700535 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
536 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
537 // used internally.
538 final Bundle extras = getIntent().getExtras();
539 if (extras != null
540 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
541 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
542 } else {
543 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
544 }
545 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700546 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
547 if (index < mListsFragment.getTabCount()) {
548 mListsFragment.showTab(index);
549 }
550 }
551
Anne Rong35208002015-07-22 16:27:37 -0700552 setSearchBoxHint();
553
Yorke Leef5554ce2015-01-27 14:46:02 -0800554 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700555 }
556
557 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800558 protected void onRestart() {
559 super.onRestart();
560 mIsRestarting = true;
561 }
562
563 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700564 protected void onPause() {
565 if (mClearSearchOnPause) {
566 hideDialpadAndSearchUi();
567 mClearSearchOnPause = false;
568 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700569 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
570 commitDialpadFragmentHide();
571 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700572 super.onPause();
573 }
574
575 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700576 protected void onSaveInstanceState(Bundle outState) {
577 super.onSaveInstanceState(outState);
578 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700579 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
580 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700581 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700582 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700583 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700584 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700585 }
586
587 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700588 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700589 if (fragment instanceof DialpadFragment) {
590 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800591 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700592 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
593 transaction.hide(mDialpadFragment);
594 transaction.commit();
595 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700596 } else if (fragment instanceof SmartDialSearchFragment) {
597 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700598 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700599 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700600 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700601 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700602 } else if (fragment instanceof ListsFragment) {
603 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700604 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700605 }
Yorke Leec3766332013-07-31 11:13:16 -0700606 }
607
Alon Albertb453e5b2013-09-10 15:54:23 -0700608 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700609 final Intent intent = new Intent(this, DialerSettingsActivity.class);
610 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700611 }
612
Chiao Cheng94b10b52012-08-17 16:59:12 -0700613 @Override
614 public void onClick(View view) {
615 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700616 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700617 if (mListsFragment.getCurrentTabIndex()
618 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700619 DialerUtils.startActivityWithErrorToast(
620 this,
621 IntentUtil.getNewContactIntent(),
622 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700623 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700624 mInCallDialpadUp = false;
625 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700626 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700627 break;
Yorke Leec3766332013-07-31 11:13:16 -0700628 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800629 try {
630 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
631 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
632 } catch (ActivityNotFoundException e) {
633 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
634 Toast.LENGTH_SHORT).show();
635 }
Yorke Leec3766332013-07-31 11:13:16 -0700636 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700637 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700638 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700639 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700640 default: {
641 Log.wtf(TAG, "Unexpected onClick event from " + view);
642 break;
643 }
644 }
645 }
646
Yorke Leec3766332013-07-31 11:13:16 -0700647 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700648 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700649 switch (item.getItemId()) {
650 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700651 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
652 // CONTENT_TYPE, so that we always open our call log from our dialer
653 final Intent intent = new Intent(this, CallLogActivity.class);
654 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700655 break;
656 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700657 DialerUtils.startActivityWithErrorToast(
658 this,
659 IntentUtil.getNewContactIntent(),
660 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700661 break;
662 case R.id.menu_import_export:
663 // We hard-code the "contactsAreAvailable" argument because doing it properly would
664 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
665 // now in Dialtacts for (potential) performance reasons. Compare with how it is
666 // done in {@link PeopleActivity}.
667 ImportExportDialogFragment.show(getFragmentManager(), true,
668 DialtactsActivity.class);
669 return true;
670 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700671 ClearFrequentsDialog.show(getFragmentManager());
672 return true;
673 case R.id.menu_call_settings:
674 handleMenuSettings();
675 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700676 }
677 return false;
678 }
679
680 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700681 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
682 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
683 if (resultCode == RESULT_OK) {
684 final ArrayList<String> matches = data.getStringArrayListExtra(
685 RecognizerIntent.EXTRA_RESULTS);
686 if (matches.size() > 0) {
687 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800688 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700689 } else {
690 Log.e(TAG, "Voice search - nothing heard");
691 }
692 } else {
693 Log.e(TAG, "Voice search failed");
694 }
695 }
696 super.onActivityResult(requestCode, resultCode, data);
697 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700698
Andrew Lee2a58ab82014-05-15 02:16:04 -0700699 /**
Nancy Chenea7c0b82015-08-31 17:18:41 -0700700 * Update the number of unread voicemails (potentially other tabs) displayed next to the tab
701 * icon.
702 */
703 public void updateTabUnreadCounts() {
704 mListsFragment.updateTabUnreadCounts();
705 }
706
707 /**
Andrew Lee2a58ab82014-05-15 02:16:04 -0700708 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
709 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
710 * @see #onDialpadShown
711 */
Yorke Leec3766332013-07-31 11:13:16 -0700712 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700713 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700714 return;
715 }
716 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800717
Yorke Lee7ee5c422014-11-04 10:29:32 -0800718 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700719
Chiao Cheng94b10b52012-08-17 16:59:12 -0700720 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800721 if (mDialpadFragment == null) {
722 mDialpadFragment = new DialpadFragment();
723 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
724 } else {
725 ft.show(mDialpadFragment);
726 }
727
728 mDialpadFragment.setAnimate(animate);
729 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700730 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700731
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700732 if (animate) {
733 mFloatingActionButtonController.scaleOut();
734 } else {
735 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700736 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700737 }
Yorke Lee5253be02014-05-21 18:50:03 -0700738 mActionBarController.onDialpadUp();
739
Yorke Lee6c450eb2015-05-08 09:45:47 -0700740 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700741 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700742
Andrew Lee2a58ab82014-05-15 02:16:04 -0700743 /**
744 * Callback from child DialpadFragment when the dialpad is shown.
745 */
746 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800747 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700748 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700749 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700750 } else {
751 mDialpadFragment.setYFraction(0);
752 }
753
Andrew Lee2a58ab82014-05-15 02:16:04 -0700754 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700755 }
756
757 /**
758 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
759 * a callback after the hide animation ends.
760 * @see #commitDialpadFragmentHide
761 */
Yorke Leeca195042013-09-25 16:29:38 -0700762 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700763 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700764 return;
765 }
Yorke Leef6673d32013-08-23 15:34:17 -0700766 if (clearDialpad) {
767 mDialpadFragment.clearDialpad();
768 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700769 if (!mIsDialpadShown) {
770 return;
Yorke Leec3766332013-07-31 11:13:16 -0700771 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700772 mIsDialpadShown = false;
773 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800774 mListsFragment.setUserVisibleHint(true);
775 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700776
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700777 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700778
Andrew Lee6324f702015-06-16 14:45:58 -0700779 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700780 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700781 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700782 } else {
783 commitDialpadFragmentHide();
784 }
785
Yorke Lee5253be02014-05-21 18:50:03 -0700786 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700787
Andrew Leed4cde832014-05-16 15:56:48 -0700788 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700789 if (TextUtils.isEmpty(mSearchQuery)) {
790 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700791 }
Andrew Leed4cde832014-05-16 15:56:48 -0700792 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700793 }
794
795 /**
796 * Finishes hiding the dialpad fragment after any animations are completed.
797 */
798 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800799 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700800 final FragmentTransaction ft = getFragmentManager().beginTransaction();
801 ft.hide(mDialpadFragment);
802 ft.commit();
803 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700804 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700805 }
806
Andrew Lee2a58ab82014-05-15 02:16:04 -0700807 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700808 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700809 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700810 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700811 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700812 fragment = mRegularSearchFragment;
813 }
814 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700815 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700816 }
817 }
818
Yorke Lee5253be02014-05-21 18:50:03 -0700819 @Override
820 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700821 return mInDialpadSearch || mInRegularSearch;
822 }
823
Yorke Lee5253be02014-05-21 18:50:03 -0700824 @Override
825 public boolean hasSearchQuery() {
826 return !TextUtils.isEmpty(mSearchQuery);
827 }
828
829 @Override
830 public boolean shouldShowActionBar() {
831 return mListsFragment.shouldShowActionBar();
832 }
833
Yorke Leeb207f8a2013-08-29 18:51:06 -0700834 private void setNotInSearchUi() {
835 mInDialpadSearch = false;
836 mInRegularSearch = false;
837 }
838
Yorke Lee311969c2013-08-26 06:31:11 -0700839 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700840 if (mIsDialpadShown) {
841 hideDialpadFragment(false, true);
842 } else {
843 exitSearchUi();
844 }
Yorke Lee311969c2013-08-26 06:31:11 -0700845 }
846
Yorke Lee53a22302014-04-29 18:13:46 -0700847 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700848 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
849 if (canIntentBeHandled(voiceIntent)) {
850 mVoiceSearchButton.setVisibility(View.VISIBLE);
851 mVoiceSearchButton.setOnClickListener(this);
852 } else {
853 mVoiceSearchButton.setVisibility(View.GONE);
854 }
855 }
856
Anne Rong35208002015-07-22 16:27:37 -0700857 protected int getSearchBoxHint () {
858 return R.string.dialer_hint_find_contact;
859 }
860
861 /**
862 * Sets the hint text for the contacts search box
863 */
864 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700865 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700866 .getCustomView().findViewById(R.id.search_view_container);
867 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
868 .setHint(getSearchBoxHint());
869 }
870
Andrew Lee467de3d2015-03-17 15:47:24 -0700871 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700872 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
873 popupMenu.inflate(R.menu.dialtacts_options);
874 popupMenu.setOnMenuItemClickListener(this);
875 return popupMenu;
876 }
877
Yorke Lee86e21f72014-04-02 16:49:38 -0700878 @Override
879 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700880 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700881 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700882 mPendingSearchViewQuery = null;
883 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700884 if (mActionBarController != null) {
885 mActionBarController.restoreActionBarOffset();
886 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700887 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700888 }
889
Chiao Cheng94b10b52012-08-17 16:59:12 -0700890 /**
891 * Returns true if the intent is due to hitting the green send key (hardware call button:
892 * KEYCODE_CALL) while in a call.
893 *
894 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700895 * @return true if the intent is due to hitting the green send key while in a call
896 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700897 private boolean isSendKeyWhileInCall(Intent intent) {
898 // If there is a call in progress and the user launched the dialer by hitting the call
899 // button, go straight to the in-call screen.
900 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700901
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700902 if (callKey) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700903 getTelecomManager().showInCallScreen(false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700904 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700905 }
906
907 return false;
908 }
909
910 /**
911 * Sets the current tab based on the intent's request type
912 *
913 * @param intent Intent that contains information about which tab should be selected
914 */
Yorke Leec3766332013-07-31 11:13:16 -0700915 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700916 // 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 -0700917 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700918 finish();
919 return;
920 }
921
Yorke Leed77017d2015-08-31 16:19:43 -0700922 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
923 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800924 showDialpadFragment(false);
925 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700926 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800927 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700928 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700929 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700930 }
931
932 @Override
933 public void onNewIntent(Intent newIntent) {
934 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700935
Nancy Chen199b7f02014-11-05 15:03:00 -0800936 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700937 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700938
Chiao Cheng94b10b52012-08-17 16:59:12 -0700939 invalidateOptionsMenu();
940 }
941
942 /** Returns true if the given intent contains a phone number to populate the dialer with */
943 private boolean isDialIntent(Intent intent) {
944 final String action = intent.getAction();
945 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
946 return true;
947 }
948 if (Intent.ACTION_VIEW.equals(action)) {
949 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700950 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700951 return true;
952 }
953 }
954 return false;
955 }
956
957 /**
Yorke Leec3766332013-07-31 11:13:16 -0700958 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700959 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700960 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700961 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700962 // Weird race condition where fragment is doing work after the activity is destroyed
963 // due to talkback being on (b/10209937). Just return since we can't do any
964 // constructive here.
965 return;
966 }
967
Yorke Leeef2b7382013-08-09 17:39:25 -0700968 if (DEBUG) {
969 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
970 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700971
Yorke Leec3766332013-07-31 11:13:16 -0700972 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700973 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700974 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700975 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700976 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700977 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700978
Yorke Leeb207f8a2013-08-29 18:51:06 -0700979 final String tag;
980 if (smartDialSearch) {
981 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
982 } else {
983 tag = TAG_REGULAR_SEARCH_FRAGMENT;
984 }
985 mInDialpadSearch = smartDialSearch;
986 mInRegularSearch = !smartDialSearch;
987
Andrew Lee6324f702015-06-16 14:45:58 -0700988 mFloatingActionButtonController.scaleOut();
989
Andrew Lee752956e2014-05-15 11:43:38 -0700990 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700991 if (animate) {
992 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
993 } else {
994 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
995 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700996 if (fragment == null) {
997 if (smartDialSearch) {
998 fragment = new SmartDialSearchFragment();
999 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -07001000 fragment = new RegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001001 fragment.setOnTouchListener(new View.OnTouchListener() {
1002 @Override
1003 public boolean onTouch(View v, MotionEvent event) {
1004 // Show the FAB when the user touches the lists fragment and the soft
1005 // keyboard is hidden.
1006 showFabInSearchUi();
1007 return false;
1008 }
1009 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001010 }
Andrew Leeb1903842014-05-15 16:11:24 -07001011 transaction.add(R.id.dialtacts_frame, fragment, tag);
1012 } else {
1013 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001014 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001015 // DialtactsActivity will provide the options menu
1016 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001017 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001018 if (!smartDialSearch) {
1019 fragment.setQueryString(query, false /* delaySelection */);
1020 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001021 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001022
Yorke Lee6c450eb2015-05-08 09:45:47 -07001023 if (animate) {
1024 mListsFragment.getView().animate().alpha(0).withLayer();
1025 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001026 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001027 }
1028
1029 /**
Yorke Leec3766332013-07-31 11:13:16 -07001030 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001031 */
Yorke Leec3766332013-07-31 11:13:16 -07001032 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001033 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001034 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001035 return;
1036 }
Andrew Leeb1903842014-05-15 16:11:24 -07001037
Andrew Leeb1903842014-05-15 16:11:24 -07001038 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001039
1040 if (mDialpadFragment != null) {
1041 mDialpadFragment.clearDialpad();
1042 }
1043
Yorke Leeb207f8a2013-08-29 18:51:06 -07001044 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001045
Andrew Lee6324f702015-06-16 14:45:58 -07001046 // Restore the FAB for the lists fragment.
1047 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1048 mFloatingActionButtonController.setVisible(false);
1049 }
1050 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1051 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1052 onPageSelected(mListsFragment.getCurrentTabIndex());
1053
Andrew Leeb1903842014-05-15 16:11:24 -07001054 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001055 if (mSmartDialSearchFragment != null) {
1056 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001057 }
Andrew Leeb1903842014-05-15 16:11:24 -07001058 if (mRegularSearchFragment != null) {
1059 transaction.remove(mRegularSearchFragment);
1060 }
1061 transaction.commit();
1062
1063 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001064
1065 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001066 // If the dialpad fragment wasn't previously visible, then send a screen view because
1067 // we are exiting regular search. Otherwise, the screen view will be sent by
1068 // {@link #hideDialpadFragment}.
1069 mListsFragment.sendScreenViewForCurrentPosition();
1070 mListsFragment.setUserVisibleHint(true);
1071 }
1072
Yorke Lee5253be02014-05-21 18:50:03 -07001073 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001074 }
1075
Chiao Cheng94b10b52012-08-17 16:59:12 -07001076 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001077 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001078 if (mStateSaved) {
1079 return;
1080 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001081 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001082 if (TextUtils.isEmpty(mSearchQuery) ||
1083 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1084 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001085 exitSearchUi();
1086 }
Yorke Leef6673d32013-08-23 15:34:17 -07001087 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001088 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001089 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001090 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001091 } else {
1092 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001093 }
Yorke Leec3766332013-07-31 11:13:16 -07001094 }
1095
Yorke Lee81a313d2015-06-01 14:53:59 -07001096 private void maybeEnterSearchUi() {
1097 if (!isInSearchUi()) {
1098 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1099 }
1100 }
1101
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001102 /**
1103 * @return True if the search UI was exited, false otherwise
1104 */
1105 private boolean maybeExitSearchUi() {
1106 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1107 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001108 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001109 return true;
1110 }
1111 return false;
1112 }
1113
Andrew Lee6324f702015-06-16 14:45:58 -07001114 private void showFabInSearchUi() {
1115 mFloatingActionButtonController.changeIcon(
1116 getResources().getDrawable(R.drawable.fab_ic_dial),
1117 getResources().getString(R.string.action_menu_dialpad_button));
1118 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1119 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1120 }
1121
Yorke Leec3766332013-07-31 11:13:16 -07001122 @Override
1123 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001124 if (mSmartDialSearchFragment != null) {
1125 mSmartDialSearchFragment.setAddToContactNumber(query);
1126 }
Yorke Leec3766332013-07-31 11:13:16 -07001127 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1128 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001129
1130 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001131 if (DEBUG) {
1132 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1133 }
1134 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1135 // This callback can happen if the dialpad fragment is recreated because of
1136 // activity destruction. In that case, don't update the search view because
1137 // that would bring the user back to the search fragment regardless of the
1138 // previous state of the application. Instead, just return here and let the
1139 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001140 if (!TextUtils.isEmpty(normalizedQuery)) {
1141 mPendingSearchViewQuery = normalizedQuery;
1142 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001143 return;
1144 }
Yorke Lee53a22302014-04-29 18:13:46 -07001145 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001146 }
Santos Cordon83131712015-04-16 21:25:35 -07001147
1148 try {
1149 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1150 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1151 }
1152 } catch (Exception ignored) {
1153 // Skip any exceptions for this piece of code
1154 }
Yorke Lee575ae382015-07-16 11:36:07 -07001155 }
Santos Cordon83131712015-04-16 21:25:35 -07001156
Yorke Lee575ae382015-07-16 11:36:07 -07001157 @Override
1158 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1159 if (mInDialpadSearch && mSmartDialSearchFragment != null
1160 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1161 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1162 return true;
1163 }
1164 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001165 }
Yorke Leec3766332013-07-31 11:13:16 -07001166
1167 @Override
1168 public void onListFragmentScrollStateChange(int scrollState) {
1169 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001170 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001171 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001172 }
1173 }
1174
1175 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001176 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001177 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001178 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1179 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001180 }
1181
Yorke Leec3766332013-07-31 11:13:16 -07001182 private boolean phoneIsInUse() {
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001183 return getTelecomManager().isInCall();
Yorke Leec3766332013-07-31 11:13:16 -07001184 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001185
Yorke Leeda0f9042013-12-05 14:25:51 -08001186 private boolean canIntentBeHandled(Intent intent) {
1187 final PackageManager packageManager = getPackageManager();
1188 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1189 PackageManager.MATCH_DEFAULT_ONLY);
1190 return resolveInfo != null && resolveInfo.size() > 0;
1191 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001192
Yorke Lee86e21f72014-04-02 16:49:38 -07001193 /**
1194 * Called when the user has long-pressed a contact tile to start a drag operation.
1195 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001196 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001197 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001198 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001199 }
1200
1201 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001202 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1203 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001204
Yorke Lee86e21f72014-04-02 16:49:38 -07001205 /**
1206 * Called when the user has released a contact tile after long-pressing it.
1207 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001208 @Override
1209 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001210 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001211 }
1212
1213 @Override
1214 public void onDroppedOnRemove() {}
1215
1216 /**
Yorke Leed999b712014-04-23 15:10:58 -07001217 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001218 * once it has been attached to the activity.
1219 */
1220 @Override
1221 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001222 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001223 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001224 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001225
Yorke Lee38019af2015-07-14 17:05:38 -07001226 /**
1227 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1228 */
1229 @Override
1230 public void showAllContactsTab() {
1231 if (mListsFragment != null) {
1232 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1233 }
1234 }
1235
1236 /**
1237 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1238 */
1239 @Override
1240 public void showDialpad() {
1241 showDialpadFragment(true);
1242 }
1243
Yorke Leee00c9fe2014-04-12 12:42:06 -07001244 @Override
1245 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001246 // Specify call-origin so that users will see the previous tab instead of
1247 // CallLog screen (search UI will be automatically exited).
1248 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Lee306324b2015-09-11 12:14:12 -07001249 DialtactsActivity.this, dataUri, null);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001250 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001251 }
1252
1253 @Override
1254 public void onCallNumberDirectly(String phoneNumber) {
Andrew Leefc1fed72014-07-02 11:05:53 -07001255 onCallNumberDirectly(phoneNumber, false /* isVideoCall */);
1256 }
1257
1258 @Override
1259 public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001260 if (phoneNumber == null) {
1261 // Invalid phone number, but let the call go through so that InCallUI can show
1262 // an error message.
1263 phoneNumber = "";
1264 }
Andrew Leed2a683f2014-07-08 15:54:50 -07001265 Intent intent = isVideoCall ?
Yorke Lee306324b2015-09-11 12:14:12 -07001266 IntentUtil.getVideoCallIntent(phoneNumber, (String) null) :
1267 IntentUtil.getCallIntent(phoneNumber, (String) null);
Yorke Lee7d20f822014-06-19 17:09:33 -07001268 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001269 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001270 }
1271
1272 @Override
1273 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001274 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001275 }
1276
1277 @Override
1278 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001279 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001280 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001281
Yorke Leecc4660d2014-04-24 11:22:57 -07001282 @Override
1283 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001284 int tabIndex = mListsFragment.getCurrentTabIndex();
1285
Andrew Lee4de59fb2015-08-13 15:20:08 -07001286 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1287 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1288 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001289 boolean isRtl = DialerUtils.isRtl();
1290 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1291 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001292 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001293 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1294 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001295 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001296 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001297 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001298
Andrew Lee432d4b52015-05-20 16:52:00 -07001299 @Override
1300 public void onPageSelected(int position) {
1301 int tabIndex = mListsFragment.getCurrentTabIndex();
1302 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001303 mFloatingActionButtonController.changeIcon(
1304 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1305 getResources().getString(R.string.search_shortcut_create_new_contact));
1306 } else {
1307 mFloatingActionButtonController.changeIcon(
1308 getResources().getDrawable(R.drawable.fab_ic_dial),
1309 getResources().getString(R.string.action_menu_dialpad_button));
1310 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001311 }
1312
1313 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001314 public void onPageScrollStateChanged(int state) {
1315 }
1316
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001317 private TelecomManager getTelecomManager() {
1318 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordond15629d2014-07-07 15:15:29 -07001319 }
1320
Yorke Lee5253be02014-05-21 18:50:03 -07001321 @Override
1322 public boolean isActionBarShowing() {
1323 return mActionBarController.isActionBarShowing();
1324 }
1325
Yorke Leec98a5bb2014-10-28 16:12:05 -07001326 @Override
1327 public ActionBarController getActionBarController() {
1328 return mActionBarController;
1329 }
1330
Yorke Lee6bc67162015-06-27 14:03:25 -07001331 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001332 public boolean isDialpadShown() {
1333 return mIsDialpadShown;
1334 }
1335
Yorke Lee5253be02014-05-21 18:50:03 -07001336 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001337 public int getDialpadHeight() {
1338 if (mDialpadFragment != null) {
1339 return mDialpadFragment.getDialpadHeight();
1340 }
1341 return 0;
1342 }
1343
1344 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001345 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001346 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001347 }
1348
1349 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001350 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001351 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001352 }
1353
1354 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001355 public int getActionBarHeight() {
1356 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001357 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001358
Andrew Lee6324f702015-06-16 14:45:58 -07001359 private int getFabAlignment() {
1360 if (!mIsLandscape && !isInSearchUi() &&
1361 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1362 return FloatingActionButtonController.ALIGN_MIDDLE;
1363 }
1364 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001365 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001366}