blob: 455b09758681eaca5beacbbbc6c3aee74f3ab7cf [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;
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;
Andrew Leea73e1892014-05-13 13:21:16 -070055import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070056import android.widget.PopupMenu;
Anne Rong35208002015-07-22 16:27:37 -070057import android.widget.TextView;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080058import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070059
Yorke Leec3766332013-07-31 11:13:16 -070060import com.android.contacts.common.dialog.ClearFrequentsDialog;
61import com.android.contacts.common.interactions.ImportExportDialogFragment;
Nancy Chen0d3a7392014-08-12 14:44:53 -070062import com.android.contacts.common.interactions.TouchPointManager;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080063import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec16ea5a2015-05-19 15:51:01 -070064import com.android.contacts.common.util.PermissionsUtil;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -070065import com.android.contacts.common.widget.FloatingActionButtonController;
Brian Attwellbeab3bb2014-10-27 12:24:49 -070066import com.android.contacts.commonbind.analytics.AnalyticsUtil;
Yorke Leec3766332013-07-31 11:13:16 -070067import com.android.dialer.calllog.CallLogActivity;
Yorke Lee38019af2015-07-14 17:05:38 -070068import com.android.dialer.calllog.CallLogFragment;
Yorke Leefce269a2013-08-12 11:56:04 -070069import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070070import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070072import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070073import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080074import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070075import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080076import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070077import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070078import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070079import com.android.dialer.list.RegularSearchFragment;
80import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070081import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070082import com.android.dialer.list.SpeedDialFragment;
Yorke Leef44e5172015-10-01 17:16:43 -070083import com.android.dialer.onboard.OnboardingActivity;
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 Lee827a90f2015-09-17 18:10:58 -070086import com.android.dialer.util.TelecomUtil;
Yorke Leee3a2d132015-09-14 16:52:08 -070087import com.android.dialer.util.IntentUtil.CallIntentBuilder;
Yorke Lee7d20f822014-06-19 17:09:33 -070088import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070089import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070090import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070091import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070092import com.android.dialerbind.DatabaseHelperManager;
Yorke Leec8d6e162015-09-14 18:43:27 -070093import com.android.incallui.Call.LogState;
Yorke Lee2fec47b2014-08-05 18:16:27 -070094import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070095import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070096
Yorke Leed5c512a2015-01-30 14:42:53 -080097import junit.framework.Assert;
98
Yorke Leec3766332013-07-31 11:13:16 -070099import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -0800100import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -0700101
Chiao Cheng94b10b52012-08-17 16:59:12 -0700102/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103 * The dialer tab's title is 'phone', a more common name (see strings.xml).
104 */
Yorke Leec3766332013-07-31 11:13:16 -0700105public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700106 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700107 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700108 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700109 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700110 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700111 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700112 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700113 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700114 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700115 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700116 ViewPager.OnPageChangeListener,
117 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118 private static final String TAG = "DialtactsActivity";
119
Yorke Lee7cc61492015-06-01 14:59:33 -0700120 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121
Yorke Leef74011e2013-08-02 11:30:30 -0700122 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
123
Yorke Leeb207f8a2013-08-29 18:51:06 -0700124 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
125 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700126 private static final String KEY_SEARCH_QUERY = "search_query";
127 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700128 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700129
Yorke Leec3766332013-07-31 11:13:16 -0700130 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
131 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
132 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
133 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700134
Chiao Cheng94b10b52012-08-17 16:59:12 -0700135 /**
136 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
137 */
138 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700139 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700140
Yorke Leec3766332013-07-31 11:13:16 -0700141 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700142
Andrew Lee6324f702015-06-16 14:45:58 -0700143 private static final int FAB_SCALE_IN_DELAY_MS = 300;
144
Nancy Chenf3d9f822015-09-11 00:47:31 -0400145 private CoordinatorLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700146
Yorke Leec3766332013-07-31 11:13:16 -0700147 /**
Yorke Leec3766332013-07-31 11:13:16 -0700148 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700149 */
Evan Charlton72d66252014-11-07 16:23:47 -0800150 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700151
152 /**
153 * Fragment for searching phone numbers using the alphanumeric keyboard.
154 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700155 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700156
157 /**
158 * Fragment for searching phone numbers using the dialpad.
159 */
160 private SmartDialSearchFragment mSmartDialSearchFragment;
161
Yorke Leee00c9fe2014-04-12 12:42:06 -0700162 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700163 * Animation that slides in.
164 */
165 private Animation mSlideIn;
166
167 /**
168 * Animation that slides out.
169 */
170 private Animation mSlideOut;
171
Yorke Lee6c450eb2015-05-08 09:45:47 -0700172 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
173 @Override
174 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700175 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700176 }
177 };
178
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700179 /**
180 * Listener for after slide out animation completes on dialer fragment.
181 */
182 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
183 @Override
184 public void onAnimationEnd(Animation animation) {
185 commitDialpadFragmentHide();
186 }
187 };
188
189 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700190 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700191 */
192 private ListsFragment mListsFragment;
193
Yorke Lee19e68ca2014-10-28 11:51:40 -0700194 /**
195 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
196 * be commited.
197 */
198 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800199 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700200 private boolean mInDialpadSearch;
201 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700202 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700203 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700204 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700205
Yorke Leeef2b7382013-08-09 17:39:25 -0700206 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700207 * Whether or not the device is in landscape orientation.
208 */
209 private boolean mIsLandscape;
210
211 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700212 * True if the dialpad is only temporarily showing due to being in call
213 */
214 private boolean mInCallDialpadUp;
215
216 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700217 * True when this activity has been launched for the first time.
218 */
Yorke Lee98702de2013-08-06 12:03:32 -0700219 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700220
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700221 /**
222 * Search query to be applied to the SearchView in the ActionBar once
223 * onCreateOptionsMenu has been called.
224 */
225 private String mPendingSearchViewQuery;
226
Yorke Lee74edcdc2014-07-11 16:15:08 -0700227 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700228 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700229 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700230
Yorke Leeef2b7382013-08-09 17:39:25 -0700231 private String mSearchQuery;
232
Yorke Leefce269a2013-08-12 11:56:04 -0700233 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700234 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700235 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700236
Sai Cheemalapati41460652014-06-02 21:05:39 -0700237 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700238
Andrew Lee2423a2f2014-05-29 14:14:45 -0700239 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700240
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800241 /**
242 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
243 * {@link #onResume()} to populate the search box.
244 */
245 private String mVoiceSearchQuery;
246
Andrew Lee467de3d2015-03-17 15:47:24 -0700247 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700248 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700249 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700250 }
251
252 @Override
253 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700254 final boolean hasContactsPermission =
255 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700256 final Menu menu = getMenu();
257 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700258 clearFrequents.setVisible(mListsFragment != null &&
259 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700260 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
261
262 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
263 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700264
265 menu.findItem(R.id.menu_history).setVisible(
266 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700267 super.show();
268 }
269 }
270
Chiao Cheng94b10b52012-08-17 16:59:12 -0700271 /**
Yorke Lee28266192014-05-01 10:05:52 -0700272 * Listener that listens to drag events and sends their x and y coordinates to a
273 * {@link DragDropController}.
274 */
275 private class LayoutOnDragListener implements OnDragListener {
276 @Override
277 public boolean onDrag(View v, DragEvent event) {
278 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700279 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700280 }
281 return true;
282 }
283 }
284
285 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700286 * Listener used to send search queries to the phone search fragment.
287 */
Yorke Lee53a22302014-04-29 18:13:46 -0700288 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700289 @Override
290 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
291 }
Yorke Leec3766332013-07-31 11:13:16 -0700292
Andrew Lee99a570c2014-05-15 14:18:50 -0700293 @Override
294 public void onTextChanged(CharSequence s, int start, int before, int count) {
295 final String newText = s.toString();
296 if (newText.equals(mSearchQuery)) {
297 // If the query hasn't changed (perhaps due to activity being destroyed
298 // and restored, or user launching the same DIAL intent twice), then there is
299 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700300 return;
301 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700302 if (DEBUG) {
303 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700304 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700305 }
Yorke Lee710709d2014-05-29 07:18:42 -0700306 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700307
Andrew Lee90374a72014-05-16 15:01:09 -0700308 // Show search fragment only when the query string is changed to non-empty text.
309 if (!TextUtils.isEmpty(newText)) {
310 // Call enterSearchUi only if we are switching search modes, or showing a search
311 // fragment for the first time.
312 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
313 (!mIsDialpadShown && mInRegularSearch);
314 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700315 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700316 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700317 }
318
Andrew Leea8515422014-06-13 16:53:54 -0700319 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700320 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700321 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700322 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700323 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700324 }
325
326 @Override
327 public void afterTextChanged(Editable s) {
328 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700329 };
330
Andrew Leeb1903842014-05-15 16:11:24 -0700331
332 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700333 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700334 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700335 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700336 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700337 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700338 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700339 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700340 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
341 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700342 }
Andrew Leeb1903842014-05-15 16:11:24 -0700343 }
344 };
345
346 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700347 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700348 */
349 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
350 @Override
351 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700352 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
353 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
354 // If the search term is empty, close the search UI.
355 maybeExitSearchUi();
356 } else {
357 // If the search term is not empty, show the dialpad fab.
358 showFabInSearchUi();
359 }
Andrew Leeb1903842014-05-15 16:11:24 -0700360 }
361 return false;
362 }
363 };
364
Chiao Cheng94b10b52012-08-17 16:59:12 -0700365 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700366 public boolean dispatchTouchEvent(MotionEvent ev) {
367 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
368 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
369 }
370 return super.dispatchTouchEvent(ev);
Nancy Chen49db1ad2014-08-18 20:10:17 -0700371 }
372
373 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700374 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800375 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700376 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700377
Yorke Leef44e5172015-10-01 17:16:43 -0700378 if (OnboardingActivity.shouldStartOnboardingActivity(this)) {
379 OnboardingActivity.startOnboardingActivity(this);
380 finish();
381 }
382
Yorke Lee98702de2013-08-06 12:03:32 -0700383 mFirstLaunch = true;
384
Andrew Lee2423a2f2014-05-29 14:14:45 -0700385 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700386 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700387
Yorke Leef5554ce2015-01-27 14:46:02 -0800388 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700389 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800390 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800391 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700392
Yorke Leef5554ce2015-01-27 14:46:02 -0800393 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700394 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700395 actionBar.setCustomView(R.layout.search_edittext);
396 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700397 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700398
Anne Rong35208002015-07-22 16:27:37 -0700399 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
400 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700401 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
402
Andrew Leee3f59a82015-02-26 12:06:35 -0800403 mActionBarController = new ActionBarController(this, searchEditTextLayout);
404
Andrew Lee04675a52014-06-05 18:36:20 -0700405 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700406 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700407 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
408 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
409 .setOnClickListener(mSearchViewOnClickListener);
410 searchEditTextLayout.findViewById(R.id.search_box_start_search)
411 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700412 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700413 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700414 @Override
415 public void onBackButtonClicked() {
416 onBackPressed();
417 }
Andrew Lee6324f702015-06-16 14:45:58 -0700418
419 @Override
420 public void onSearchViewClicked() {
421 // Hide FAB, as the keyboard is shown.
422 mFloatingActionButtonController.scaleOut();
423 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700424 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700425
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700426 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700427 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700428
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700429 final View floatingActionButtonContainer = findViewById(
430 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700431 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
432 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700433 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700434 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700435
Andrew Lee04675a52014-06-05 18:36:20 -0700436 ImageButton optionsMenuButton =
437 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700438 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700439 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
440 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700441
Yorke Leed5c512a2015-01-30 14:42:53 -0800442 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
443 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700444 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800445 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000446 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800447 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700448 } else {
449 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700450 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
451 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700452 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700453 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700454 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700455 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700456
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700457 final boolean isLayoutRtl = DialerUtils.isRtl();
458 if (mIsLandscape) {
459 mSlideIn = AnimationUtils.loadAnimation(this,
460 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
461 mSlideOut = AnimationUtils.loadAnimation(this,
462 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
463 } else {
464 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
465 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
466 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700467
Yorke Lee2fec47b2014-08-05 18:16:27 -0700468 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
469 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
470
Yorke Lee6c450eb2015-05-08 09:45:47 -0700471 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700472 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700473
Nancy Chenf3d9f822015-09-11 00:47:31 -0400474 mParentLayout = (CoordinatorLayout) findViewById(R.id.dialtacts_mainlayout);
Yorke Leef614f6f2014-10-03 10:44:19 -0700475 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700476 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700477 new ViewTreeObserver.OnGlobalLayoutListener() {
478 @Override
479 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700480 final ViewTreeObserver observer =
481 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700482 if (!observer.isAlive()) {
483 return;
484 }
485 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700486 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700487 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700488 mFloatingActionButtonController.align(
489 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700490 }
491 });
Andrew Lee0c667702014-05-12 14:31:45 -0700492
Yorke Leef5554ce2015-01-27 14:46:02 -0800493 Trace.endSection();
494
495 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700496 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700497 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800498 Trace.endSection();
499 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700500 }
501
502 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700503 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800504 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700505 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700506
Yorke Lee19e68ca2014-10-28 11:51:40 -0700507 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700508 if (mFirstLaunch) {
509 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700510 } else if (!phoneIsInUse() && mInCallDialpadUp) {
511 hideDialpadFragment(false, true);
512 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700513 } else if (mShowDialpadOnResume) {
514 showDialpadFragment(false);
515 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700516 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800517
518 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
519 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
520 // shown until onResume has completed. Active the search UI and set the search term now.
521 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
522 mActionBarController.onSearchBoxTapped();
523 mSearchView.setText(mVoiceSearchQuery);
524 mVoiceSearchQuery = null;
525 }
526
Yorke Lee98702de2013-08-06 12:03:32 -0700527 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800528
529 if (mIsRestarting) {
530 // This is only called when the activity goes from resumed -> paused -> resumed, so it
531 // will not cause an extra view to be sent out on rotation
532 if (mIsDialpadShown) {
533 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
534 }
535 mIsRestarting = false;
536 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700537
Yorke Lee53a22302014-04-29 18:13:46 -0700538 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700539 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700540 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700541
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700542 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
543 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
544 // used internally.
545 final Bundle extras = getIntent().getExtras();
546 if (extras != null
547 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
548 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
549 } else {
550 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
551 }
552 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700553 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
554 if (index < mListsFragment.getTabCount()) {
555 mListsFragment.showTab(index);
556 }
557 }
558
Anne Rong35208002015-07-22 16:27:37 -0700559 setSearchBoxHint();
560
Yorke Leef5554ce2015-01-27 14:46:02 -0800561 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700562 }
563
564 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800565 protected void onRestart() {
566 super.onRestart();
567 mIsRestarting = true;
568 }
569
570 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700571 protected void onPause() {
572 if (mClearSearchOnPause) {
573 hideDialpadAndSearchUi();
574 mClearSearchOnPause = false;
575 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700576 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
577 commitDialpadFragmentHide();
578 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700579 super.onPause();
580 }
581
582 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700583 protected void onSaveInstanceState(Bundle outState) {
584 super.onSaveInstanceState(outState);
585 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700586 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
587 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700588 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700589 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700590 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700591 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700592 }
593
594 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700595 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700596 if (fragment instanceof DialpadFragment) {
597 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800598 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700599 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
600 transaction.hide(mDialpadFragment);
601 transaction.commit();
602 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700603 } else if (fragment instanceof SmartDialSearchFragment) {
604 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700605 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700606 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700607 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700608 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700609 } else if (fragment instanceof ListsFragment) {
610 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700611 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700612 }
Yorke Leec3766332013-07-31 11:13:16 -0700613 }
614
Alon Albertb453e5b2013-09-10 15:54:23 -0700615 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700616 final Intent intent = new Intent(this, DialerSettingsActivity.class);
617 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700618 }
619
Chiao Cheng94b10b52012-08-17 16:59:12 -0700620 @Override
621 public void onClick(View view) {
622 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700623 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700624 if (mListsFragment.getCurrentTabIndex()
625 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700626 DialerUtils.startActivityWithErrorToast(
627 this,
628 IntentUtil.getNewContactIntent(),
629 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700630 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700631 mInCallDialpadUp = false;
632 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700633 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700634 break;
Yorke Leec3766332013-07-31 11:13:16 -0700635 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800636 try {
637 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
638 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
639 } catch (ActivityNotFoundException e) {
640 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
641 Toast.LENGTH_SHORT).show();
642 }
Yorke Leec3766332013-07-31 11:13:16 -0700643 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700644 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700645 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700646 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700647 default: {
648 Log.wtf(TAG, "Unexpected onClick event from " + view);
649 break;
650 }
651 }
652 }
653
Yorke Leec3766332013-07-31 11:13:16 -0700654 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700655 public boolean onMenuItemClick(MenuItem item) {
Jay Shrauner193cfc52015-09-25 14:13:16 -0700656 if (!isSafeToCommitTransactions()) {
Jay Shrauner119b0652015-09-17 12:00:18 -0700657 return true;
658 }
659
Yorke Lee86e21f72014-04-02 16:49:38 -0700660 switch (item.getItemId()) {
661 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700662 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
663 // CONTENT_TYPE, so that we always open our call log from our dialer
664 final Intent intent = new Intent(this, CallLogActivity.class);
665 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700666 break;
667 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700668 DialerUtils.startActivityWithErrorToast(
669 this,
670 IntentUtil.getNewContactIntent(),
671 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700672 break;
673 case R.id.menu_import_export:
674 // We hard-code the "contactsAreAvailable" argument because doing it properly would
675 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
676 // now in Dialtacts for (potential) performance reasons. Compare with how it is
677 // done in {@link PeopleActivity}.
Wenyi Wang5953e8c2015-09-24 15:38:14 -0700678 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
679 ImportExportDialogFragment.show(getFragmentManager(), true,
680 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_FAVORITES);
681 } else {
682 ImportExportDialogFragment.show(getFragmentManager(), true,
683 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_DEFAULT);
684 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700685 return true;
686 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700687 ClearFrequentsDialog.show(getFragmentManager());
688 return true;
689 case R.id.menu_call_settings:
690 handleMenuSettings();
691 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700692 }
693 return false;
694 }
695
696 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700697 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
698 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
699 if (resultCode == RESULT_OK) {
700 final ArrayList<String> matches = data.getStringArrayListExtra(
701 RecognizerIntent.EXTRA_RESULTS);
702 if (matches.size() > 0) {
703 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800704 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700705 } else {
706 Log.e(TAG, "Voice search - nothing heard");
707 }
708 } else {
709 Log.e(TAG, "Voice search failed");
710 }
711 }
712 super.onActivityResult(requestCode, resultCode, data);
713 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700714
Andrew Lee2a58ab82014-05-15 02:16:04 -0700715 /**
Nancy Chenea7c0b82015-08-31 17:18:41 -0700716 * Update the number of unread voicemails (potentially other tabs) displayed next to the tab
717 * icon.
718 */
719 public void updateTabUnreadCounts() {
720 mListsFragment.updateTabUnreadCounts();
721 }
722
723 /**
Andrew Lee2a58ab82014-05-15 02:16:04 -0700724 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
725 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
726 * @see #onDialpadShown
727 */
Yorke Leec3766332013-07-31 11:13:16 -0700728 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700729 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700730 return;
731 }
732 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800733
Yorke Lee7ee5c422014-11-04 10:29:32 -0800734 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700735
Chiao Cheng94b10b52012-08-17 16:59:12 -0700736 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800737 if (mDialpadFragment == null) {
738 mDialpadFragment = new DialpadFragment();
739 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
740 } else {
741 ft.show(mDialpadFragment);
742 }
743
744 mDialpadFragment.setAnimate(animate);
745 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700746 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700747
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700748 if (animate) {
749 mFloatingActionButtonController.scaleOut();
750 } else {
751 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700752 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700753 }
Yorke Lee5253be02014-05-21 18:50:03 -0700754 mActionBarController.onDialpadUp();
755
Yorke Lee6c450eb2015-05-08 09:45:47 -0700756 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700757 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700758
Andrew Lee2a58ab82014-05-15 02:16:04 -0700759 /**
760 * Callback from child DialpadFragment when the dialpad is shown.
761 */
762 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800763 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700764 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700765 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700766 } else {
767 mDialpadFragment.setYFraction(0);
768 }
769
Andrew Lee2a58ab82014-05-15 02:16:04 -0700770 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700771 }
772
773 /**
774 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
775 * a callback after the hide animation ends.
776 * @see #commitDialpadFragmentHide
777 */
Yorke Leeca195042013-09-25 16:29:38 -0700778 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700779 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700780 return;
781 }
Yorke Leef6673d32013-08-23 15:34:17 -0700782 if (clearDialpad) {
783 mDialpadFragment.clearDialpad();
784 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700785 if (!mIsDialpadShown) {
786 return;
Yorke Leec3766332013-07-31 11:13:16 -0700787 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700788 mIsDialpadShown = false;
789 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800790 mListsFragment.setUserVisibleHint(true);
791 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700792
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700793 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700794
Andrew Lee6324f702015-06-16 14:45:58 -0700795 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700796 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700797 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700798 } else {
799 commitDialpadFragmentHide();
800 }
801
Yorke Lee5253be02014-05-21 18:50:03 -0700802 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700803
Andrew Leed4cde832014-05-16 15:56:48 -0700804 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700805 if (TextUtils.isEmpty(mSearchQuery)) {
806 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700807 }
Andrew Leed4cde832014-05-16 15:56:48 -0700808 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700809 }
810
811 /**
812 * Finishes hiding the dialpad fragment after any animations are completed.
813 */
814 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800815 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700816 final FragmentTransaction ft = getFragmentManager().beginTransaction();
817 ft.hide(mDialpadFragment);
818 ft.commit();
819 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700820 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700821 }
822
Andrew Lee2a58ab82014-05-15 02:16:04 -0700823 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700824 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700825 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700826 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700827 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700828 fragment = mRegularSearchFragment;
829 }
830 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700831 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700832 }
833 }
834
Yorke Lee5253be02014-05-21 18:50:03 -0700835 @Override
836 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700837 return mInDialpadSearch || mInRegularSearch;
838 }
839
Yorke Lee5253be02014-05-21 18:50:03 -0700840 @Override
841 public boolean hasSearchQuery() {
842 return !TextUtils.isEmpty(mSearchQuery);
843 }
844
845 @Override
846 public boolean shouldShowActionBar() {
847 return mListsFragment.shouldShowActionBar();
848 }
849
Yorke Leeb207f8a2013-08-29 18:51:06 -0700850 private void setNotInSearchUi() {
851 mInDialpadSearch = false;
852 mInRegularSearch = false;
853 }
854
Yorke Lee311969c2013-08-26 06:31:11 -0700855 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700856 if (mIsDialpadShown) {
857 hideDialpadFragment(false, true);
858 } else {
859 exitSearchUi();
860 }
Yorke Lee311969c2013-08-26 06:31:11 -0700861 }
862
Yorke Lee53a22302014-04-29 18:13:46 -0700863 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700864 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
865 if (canIntentBeHandled(voiceIntent)) {
866 mVoiceSearchButton.setVisibility(View.VISIBLE);
867 mVoiceSearchButton.setOnClickListener(this);
868 } else {
869 mVoiceSearchButton.setVisibility(View.GONE);
870 }
871 }
872
Anne Rong35208002015-07-22 16:27:37 -0700873 protected int getSearchBoxHint () {
874 return R.string.dialer_hint_find_contact;
875 }
876
877 /**
878 * Sets the hint text for the contacts search box
879 */
880 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700881 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700882 .getCustomView().findViewById(R.id.search_view_container);
883 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
884 .setHint(getSearchBoxHint());
885 }
886
Andrew Lee467de3d2015-03-17 15:47:24 -0700887 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700888 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
889 popupMenu.inflate(R.menu.dialtacts_options);
890 popupMenu.setOnMenuItemClickListener(this);
891 return popupMenu;
892 }
893
Yorke Lee86e21f72014-04-02 16:49:38 -0700894 @Override
895 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700896 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700897 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700898 mPendingSearchViewQuery = null;
899 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700900 if (mActionBarController != null) {
901 mActionBarController.restoreActionBarOffset();
902 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700903 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700904 }
905
Chiao Cheng94b10b52012-08-17 16:59:12 -0700906 /**
907 * Returns true if the intent is due to hitting the green send key (hardware call button:
908 * KEYCODE_CALL) while in a call.
909 *
910 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700911 * @return true if the intent is due to hitting the green send key while in a call
912 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700913 private boolean isSendKeyWhileInCall(Intent intent) {
914 // If there is a call in progress and the user launched the dialer by hitting the call
915 // button, go straight to the in-call screen.
916 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700917
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700918 if (callKey) {
Yorke Lee762b3572015-09-18 12:54:59 -0700919 TelecomUtil.showInCallScreen(this, false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700920 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700921 }
922
923 return false;
924 }
925
926 /**
927 * Sets the current tab based on the intent's request type
928 *
929 * @param intent Intent that contains information about which tab should be selected
930 */
Yorke Leec3766332013-07-31 11:13:16 -0700931 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700932 // 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 -0700933 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700934 finish();
935 return;
936 }
937
Yorke Leed77017d2015-08-31 16:19:43 -0700938 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
939 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800940 showDialpadFragment(false);
941 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700942 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800943 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700944 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700945 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700946 }
947
948 @Override
949 public void onNewIntent(Intent newIntent) {
950 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700951
Nancy Chen199b7f02014-11-05 15:03:00 -0800952 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700953 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700954
Chiao Cheng94b10b52012-08-17 16:59:12 -0700955 invalidateOptionsMenu();
956 }
957
958 /** Returns true if the given intent contains a phone number to populate the dialer with */
959 private boolean isDialIntent(Intent intent) {
960 final String action = intent.getAction();
961 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
962 return true;
963 }
964 if (Intent.ACTION_VIEW.equals(action)) {
965 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700966 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700967 return true;
968 }
969 }
970 return false;
971 }
972
973 /**
Yorke Leec3766332013-07-31 11:13:16 -0700974 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700975 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700976 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700977 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700978 // Weird race condition where fragment is doing work after the activity is destroyed
979 // due to talkback being on (b/10209937). Just return since we can't do any
980 // constructive here.
981 return;
982 }
983
Yorke Leeef2b7382013-08-09 17:39:25 -0700984 if (DEBUG) {
985 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
986 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700987
Yorke Leec3766332013-07-31 11:13:16 -0700988 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700989 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700990 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700991 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700992 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700993 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700994
Yorke Leeb207f8a2013-08-29 18:51:06 -0700995 final String tag;
996 if (smartDialSearch) {
997 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
998 } else {
999 tag = TAG_REGULAR_SEARCH_FRAGMENT;
1000 }
1001 mInDialpadSearch = smartDialSearch;
1002 mInRegularSearch = !smartDialSearch;
1003
Andrew Lee6324f702015-06-16 14:45:58 -07001004 mFloatingActionButtonController.scaleOut();
1005
Andrew Lee752956e2014-05-15 11:43:38 -07001006 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001007 if (animate) {
1008 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
1009 } else {
1010 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
1011 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001012 if (fragment == null) {
1013 if (smartDialSearch) {
1014 fragment = new SmartDialSearchFragment();
1015 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -07001016 fragment = new RegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001017 fragment.setOnTouchListener(new View.OnTouchListener() {
1018 @Override
1019 public boolean onTouch(View v, MotionEvent event) {
1020 // Show the FAB when the user touches the lists fragment and the soft
1021 // keyboard is hidden.
1022 showFabInSearchUi();
1023 return false;
1024 }
1025 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001026 }
Andrew Leeb1903842014-05-15 16:11:24 -07001027 transaction.add(R.id.dialtacts_frame, fragment, tag);
1028 } else {
1029 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001030 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001031 // DialtactsActivity will provide the options menu
1032 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001033 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001034 if (!smartDialSearch) {
1035 fragment.setQueryString(query, false /* delaySelection */);
1036 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001037 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001038
Yorke Lee6c450eb2015-05-08 09:45:47 -07001039 if (animate) {
1040 mListsFragment.getView().animate().alpha(0).withLayer();
1041 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001042 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001043 }
1044
1045 /**
Yorke Leec3766332013-07-31 11:13:16 -07001046 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001047 */
Yorke Leec3766332013-07-31 11:13:16 -07001048 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001049 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001050 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001051 return;
1052 }
Andrew Leeb1903842014-05-15 16:11:24 -07001053
Andrew Leeb1903842014-05-15 16:11:24 -07001054 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001055
1056 if (mDialpadFragment != null) {
1057 mDialpadFragment.clearDialpad();
1058 }
1059
Yorke Leeb207f8a2013-08-29 18:51:06 -07001060 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001061
Andrew Lee6324f702015-06-16 14:45:58 -07001062 // Restore the FAB for the lists fragment.
1063 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1064 mFloatingActionButtonController.setVisible(false);
1065 }
1066 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1067 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1068 onPageSelected(mListsFragment.getCurrentTabIndex());
1069
Andrew Leeb1903842014-05-15 16:11:24 -07001070 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001071 if (mSmartDialSearchFragment != null) {
1072 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001073 }
Andrew Leeb1903842014-05-15 16:11:24 -07001074 if (mRegularSearchFragment != null) {
1075 transaction.remove(mRegularSearchFragment);
1076 }
1077 transaction.commit();
1078
1079 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001080
1081 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001082 // If the dialpad fragment wasn't previously visible, then send a screen view because
1083 // we are exiting regular search. Otherwise, the screen view will be sent by
1084 // {@link #hideDialpadFragment}.
1085 mListsFragment.sendScreenViewForCurrentPosition();
1086 mListsFragment.setUserVisibleHint(true);
1087 }
1088
Yorke Lee5253be02014-05-21 18:50:03 -07001089 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001090 }
1091
Chiao Cheng94b10b52012-08-17 16:59:12 -07001092 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001093 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001094 if (mStateSaved) {
1095 return;
1096 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001097 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001098 if (TextUtils.isEmpty(mSearchQuery) ||
1099 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1100 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001101 exitSearchUi();
1102 }
Yorke Leef6673d32013-08-23 15:34:17 -07001103 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001104 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001105 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001106 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001107 } else {
1108 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001109 }
Yorke Leec3766332013-07-31 11:13:16 -07001110 }
1111
Yorke Lee81a313d2015-06-01 14:53:59 -07001112 private void maybeEnterSearchUi() {
1113 if (!isInSearchUi()) {
1114 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1115 }
1116 }
1117
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001118 /**
1119 * @return True if the search UI was exited, false otherwise
1120 */
1121 private boolean maybeExitSearchUi() {
1122 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1123 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001124 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001125 return true;
1126 }
1127 return false;
1128 }
1129
Andrew Lee6324f702015-06-16 14:45:58 -07001130 private void showFabInSearchUi() {
1131 mFloatingActionButtonController.changeIcon(
1132 getResources().getDrawable(R.drawable.fab_ic_dial),
1133 getResources().getString(R.string.action_menu_dialpad_button));
1134 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1135 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1136 }
1137
Yorke Leec3766332013-07-31 11:13:16 -07001138 @Override
1139 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001140 if (mSmartDialSearchFragment != null) {
1141 mSmartDialSearchFragment.setAddToContactNumber(query);
1142 }
Yorke Leec3766332013-07-31 11:13:16 -07001143 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1144 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001145
1146 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001147 if (DEBUG) {
1148 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1149 }
1150 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1151 // This callback can happen if the dialpad fragment is recreated because of
1152 // activity destruction. In that case, don't update the search view because
1153 // that would bring the user back to the search fragment regardless of the
1154 // previous state of the application. Instead, just return here and let the
1155 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001156 if (!TextUtils.isEmpty(normalizedQuery)) {
1157 mPendingSearchViewQuery = normalizedQuery;
1158 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001159 return;
1160 }
Yorke Lee53a22302014-04-29 18:13:46 -07001161 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001162 }
Santos Cordon83131712015-04-16 21:25:35 -07001163
1164 try {
1165 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1166 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1167 }
1168 } catch (Exception ignored) {
1169 // Skip any exceptions for this piece of code
1170 }
Yorke Lee575ae382015-07-16 11:36:07 -07001171 }
Santos Cordon83131712015-04-16 21:25:35 -07001172
Yorke Lee575ae382015-07-16 11:36:07 -07001173 @Override
1174 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1175 if (mInDialpadSearch && mSmartDialSearchFragment != null
1176 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1177 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1178 return true;
1179 }
1180 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001181 }
Yorke Leec3766332013-07-31 11:13:16 -07001182
1183 @Override
1184 public void onListFragmentScrollStateChange(int scrollState) {
1185 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001186 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001187 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001188 }
1189 }
1190
1191 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001192 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001193 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001194 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1195 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001196 }
1197
Yorke Leec3766332013-07-31 11:13:16 -07001198 private boolean phoneIsInUse() {
Yorke Lee827a90f2015-09-17 18:10:58 -07001199 return TelecomUtil.isInCall(this);
Yorke Leec3766332013-07-31 11:13:16 -07001200 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001201
Yorke Leeda0f9042013-12-05 14:25:51 -08001202 private boolean canIntentBeHandled(Intent intent) {
1203 final PackageManager packageManager = getPackageManager();
1204 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1205 PackageManager.MATCH_DEFAULT_ONLY);
1206 return resolveInfo != null && resolveInfo.size() > 0;
1207 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001208
Yorke Lee86e21f72014-04-02 16:49:38 -07001209 /**
1210 * Called when the user has long-pressed a contact tile to start a drag operation.
1211 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001212 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001213 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001214 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001215 }
1216
1217 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001218 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1219 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001220
Yorke Lee86e21f72014-04-02 16:49:38 -07001221 /**
1222 * Called when the user has released a contact tile after long-pressing it.
1223 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001224 @Override
1225 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001226 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001227 }
1228
1229 @Override
1230 public void onDroppedOnRemove() {}
1231
1232 /**
Yorke Leed999b712014-04-23 15:10:58 -07001233 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001234 * once it has been attached to the activity.
1235 */
1236 @Override
1237 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001238 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001239 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001240 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001241
Yorke Lee38019af2015-07-14 17:05:38 -07001242 /**
1243 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1244 */
1245 @Override
1246 public void showAllContactsTab() {
1247 if (mListsFragment != null) {
1248 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1249 }
1250 }
1251
1252 /**
1253 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1254 */
1255 @Override
1256 public void showDialpad() {
1257 showDialpadFragment(true);
1258 }
1259
Yorke Leee00c9fe2014-04-12 12:42:06 -07001260 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001261 public void onPickDataUri(Uri dataUri, int callInitiationType) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001262 mClearSearchOnPause = true;
Yorke Leec8d6e162015-09-14 18:43:27 -07001263 PhoneNumberInteraction.startInteractionForPhoneCall(
1264 DialtactsActivity.this, dataUri, callInitiationType);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001265 }
1266
1267 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001268 public void onPickPhoneNumber(String phoneNumber, boolean isVideoCall, int callInitiationType) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001269 if (phoneNumber == null) {
1270 // Invalid phone number, but let the call go through so that InCallUI can show
1271 // an error message.
1272 phoneNumber = "";
1273 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001274
1275 final Intent intent = new CallIntentBuilder(phoneNumber)
1276 .setIsVideoCall(isVideoCall)
Yorke Leec8d6e162015-09-14 18:43:27 -07001277 .setCallInitiationType(callInitiationType)
Yorke Leee3a2d132015-09-14 16:52:08 -07001278 .build();
1279
Yorke Lee7d20f822014-06-19 17:09:33 -07001280 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001281 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001282 }
1283
1284 @Override
1285 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001286 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001287 }
1288
1289 @Override
1290 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001291 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001292 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001293
Yorke Leecc4660d2014-04-24 11:22:57 -07001294 @Override
1295 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001296 int tabIndex = mListsFragment.getCurrentTabIndex();
1297
Andrew Lee4de59fb2015-08-13 15:20:08 -07001298 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1299 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1300 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001301 boolean isRtl = DialerUtils.isRtl();
1302 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1303 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001304 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001305 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1306 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001307 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001308 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001309 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001310
Andrew Lee432d4b52015-05-20 16:52:00 -07001311 @Override
1312 public void onPageSelected(int position) {
1313 int tabIndex = mListsFragment.getCurrentTabIndex();
1314 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001315 mFloatingActionButtonController.changeIcon(
1316 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1317 getResources().getString(R.string.search_shortcut_create_new_contact));
1318 } else {
1319 mFloatingActionButtonController.changeIcon(
1320 getResources().getDrawable(R.drawable.fab_ic_dial),
1321 getResources().getString(R.string.action_menu_dialpad_button));
1322 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001323 }
1324
1325 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001326 public void onPageScrollStateChanged(int state) {
1327 }
1328
Yorke Lee5253be02014-05-21 18:50:03 -07001329 @Override
1330 public boolean isActionBarShowing() {
1331 return mActionBarController.isActionBarShowing();
1332 }
1333
Yorke Leec98a5bb2014-10-28 16:12:05 -07001334 @Override
1335 public ActionBarController getActionBarController() {
1336 return mActionBarController;
1337 }
1338
Yorke Lee6bc67162015-06-27 14:03:25 -07001339 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001340 public boolean isDialpadShown() {
1341 return mIsDialpadShown;
1342 }
1343
Yorke Lee5253be02014-05-21 18:50:03 -07001344 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001345 public int getDialpadHeight() {
1346 if (mDialpadFragment != null) {
1347 return mDialpadFragment.getDialpadHeight();
1348 }
1349 return 0;
1350 }
1351
1352 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001353 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001354 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001355 }
1356
1357 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001358 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001359 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001360 }
1361
1362 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001363 public int getActionBarHeight() {
1364 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001365 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001366
Andrew Lee6324f702015-06-16 14:45:58 -07001367 private int getFabAlignment() {
1368 if (!mIsLandscape && !isInSearchUi() &&
1369 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1370 return FloatingActionButtonController.ALIGN_MIDDLE;
1371 }
1372 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001373 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001374}