blob: 58a0474d1adb269fabf7718c63aef9d7b6cae34a [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
Sarmad Hashmi3694b112016-02-17 17:39:36 -080019import com.android.dialer.voicemail.VoicemailArchiveActivity;
Yorke Lee135e4652015-10-23 18:12:51 -070020import com.google.common.annotations.VisibleForTesting;
21
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.app.Fragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -070023import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080024import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070025import android.content.Context;
26import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080027import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Sai Cheemalapati82d6da52014-06-03 13:54:23 -070029import android.content.res.Configuration;
Andrew Lee2423a2f2014-05-29 14:14:45 -070030import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070031import android.net.Uri;
32import android.os.Bundle;
Yorke Leef5554ce2015-01-27 14:46:02 -080033import android.os.Trace;
Nancy Chen019713e2015-07-06 18:25:24 -070034import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070035import android.speech.RecognizerIntent;
Nancy Chenf3d9f822015-09-11 00:47:31 -040036import android.support.design.widget.CoordinatorLayout;
Yorke Leecc4660d2014-04-24 11:22:57 -070037import android.support.v4.view.ViewPager;
Nancy Chen44898ad2015-08-13 12:03:47 -070038import android.support.v7.app.ActionBar;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070039import android.telecom.PhoneAccount;
Yorke Lee53a22302014-04-29 18:13:46 -070040import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070042import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070044import android.view.DragEvent;
Yorke Lee74edcdc2014-07-11 16:15:08 -070045import android.view.Gravity;
Andrew Leeb1903842014-05-15 16:11:24 -070046import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070049import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070051import android.view.View.OnDragListener;
Ihab Awad9134e252014-07-09 12:32:52 -070052import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070053import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070054import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070055import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070056import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070057import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070058import android.widget.PopupMenu;
Anne Rong35208002015-07-22 16:27:37 -070059import android.widget.TextView;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080060import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070061
Yorke Leec3766332013-07-31 11:13:16 -070062import com.android.contacts.common.dialog.ClearFrequentsDialog;
63import com.android.contacts.common.interactions.ImportExportDialogFragment;
Nancy Chen0d3a7392014-08-12 14:44:53 -070064import com.android.contacts.common.interactions.TouchPointManager;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080065import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec16ea5a2015-05-19 15:51:01 -070066import com.android.contacts.common.util.PermissionsUtil;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -070067import com.android.contacts.common.widget.FloatingActionButtonController;
Brian Attwellbeab3bb2014-10-27 12:24:49 -070068import com.android.contacts.commonbind.analytics.AnalyticsUtil;
Yorke Leec3766332013-07-31 11:13:16 -070069import com.android.dialer.calllog.CallLogActivity;
Yorke Lee38019af2015-07-14 17:05:38 -070070import com.android.dialer.calllog.CallLogFragment;
Yorke Leefce269a2013-08-12 11:56:04 -070071import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070072import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070073import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070074import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070075import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080076import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070077import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080078import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070079import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070080import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070081import com.android.dialer.list.RegularSearchFragment;
82import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070083import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070084import com.android.dialer.list.SpeedDialFragment;
Yorke Lee6e0fd6b2015-11-04 20:44:25 -080085import com.android.dialer.logging.Logger;
Yorke Lee58e68a92015-11-06 10:35:30 -080086import com.android.dialer.logging.ScreenEvent;
Yorke Leeaf6f1952014-07-14 19:13:16 -070087import com.android.dialer.settings.DialerSettingsActivity;
Yorke Lee7d20f822014-06-19 17:09:33 -070088import com.android.dialer.util.DialerUtils;
Ta-wei Yene3cae632015-10-27 16:09:09 -070089import com.android.dialer.util.IntentUtil;
90import com.android.dialer.util.IntentUtil.CallIntentBuilder;
91import com.android.dialer.util.TelecomUtil;
Yorke Lee5253be02014-05-21 18:50:03 -070092import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070093import com.android.dialer.widget.SearchEditTextLayout;
Yorke Lee0baa98b2013-08-22 10:55:16 -070094import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee807afd82015-10-12 10:03:07 -070095import com.android.dialerbind.ObjectFactory;
Yorke Lee2fec47b2014-08-05 18:16:27 -070096import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070097import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070098
Yorke Leed5c512a2015-01-30 14:42:53 -080099import junit.framework.Assert;
100
Yorke Leec3766332013-07-31 11:13:16 -0700101import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -0800102import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -0700103
Chiao Cheng94b10b52012-08-17 16:59:12 -0700104/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700105 * The dialer tab's title is 'phone', a more common name (see strings.xml).
106 */
Yorke Leec3766332013-07-31 11:13:16 -0700107public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700108 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700109 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700110 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700111 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700112 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700113 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700114 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700115 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700116 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700117 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700118 ViewPager.OnPageChangeListener,
119 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700120 private static final String TAG = "DialtactsActivity";
121
Yorke Lee7cc61492015-06-01 14:59:33 -0700122 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700123
Yorke Leef74011e2013-08-02 11:30:30 -0700124 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
125
Yorke Leeb207f8a2013-08-29 18:51:06 -0700126 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
127 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700128 private static final String KEY_SEARCH_QUERY = "search_query";
129 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700130 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700131
Yorke Lee135e4652015-10-23 18:12:51 -0700132 @VisibleForTesting
133 public static final String TAG_DIALPAD_FRAGMENT = "dialpad";
Yorke Leec3766332013-07-31 11:13:16 -0700134 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
135 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
136 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700137
Chiao Cheng94b10b52012-08-17 16:59:12 -0700138 /**
139 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
140 */
141 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700142 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700143
Yorke Leec3766332013-07-31 11:13:16 -0700144 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700145
Andrew Lee6324f702015-06-16 14:45:58 -0700146 private static final int FAB_SCALE_IN_DELAY_MS = 300;
147
Nancy Chenf3d9f822015-09-11 00:47:31 -0400148 private CoordinatorLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700149
Yorke Leec3766332013-07-31 11:13:16 -0700150 /**
Yorke Leec3766332013-07-31 11:13:16 -0700151 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700152 */
Evan Charlton72d66252014-11-07 16:23:47 -0800153 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700154
155 /**
156 * Fragment for searching phone numbers using the alphanumeric keyboard.
157 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700158 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700159
160 /**
161 * Fragment for searching phone numbers using the dialpad.
162 */
163 private SmartDialSearchFragment mSmartDialSearchFragment;
164
Yorke Leee00c9fe2014-04-12 12:42:06 -0700165 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700166 * Animation that slides in.
167 */
168 private Animation mSlideIn;
169
170 /**
171 * Animation that slides out.
172 */
173 private Animation mSlideOut;
174
Yorke Lee6c450eb2015-05-08 09:45:47 -0700175 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
176 @Override
177 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700178 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700179 }
180 };
181
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700182 /**
183 * Listener for after slide out animation completes on dialer fragment.
184 */
185 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
186 @Override
187 public void onAnimationEnd(Animation animation) {
188 commitDialpadFragmentHide();
189 }
190 };
191
192 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700193 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700194 */
195 private ListsFragment mListsFragment;
196
Yorke Lee19e68ca2014-10-28 11:51:40 -0700197 /**
198 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
199 * be commited.
200 */
201 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800202 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700203 private boolean mInDialpadSearch;
204 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700205 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700206 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700207 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700208
Yorke Leeef2b7382013-08-09 17:39:25 -0700209 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700210 * Whether or not the device is in landscape orientation.
211 */
212 private boolean mIsLandscape;
213
214 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700215 * True if the dialpad is only temporarily showing due to being in call
216 */
217 private boolean mInCallDialpadUp;
218
219 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700220 * True when this activity has been launched for the first time.
221 */
Yorke Lee98702de2013-08-06 12:03:32 -0700222 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700223
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700224 /**
225 * Search query to be applied to the SearchView in the ActionBar once
226 * onCreateOptionsMenu has been called.
227 */
228 private String mPendingSearchViewQuery;
229
Yorke Lee74edcdc2014-07-11 16:15:08 -0700230 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700231 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700232 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700233
Yorke Leeef2b7382013-08-09 17:39:25 -0700234 private String mSearchQuery;
Yorke Leed9a93162015-10-14 09:39:05 -0700235 private String mDialpadQuery;
Yorke Leeef2b7382013-08-09 17:39:25 -0700236
Yorke Leefce269a2013-08-12 11:56:04 -0700237 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700238 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700239 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700240
Sai Cheemalapati41460652014-06-02 21:05:39 -0700241 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700242
Andrew Lee2423a2f2014-05-29 14:14:45 -0700243 private int mActionBarHeight;
Sarmad Hashmicb559812016-02-10 18:39:59 -0800244 private int mPreviouslySelectedTabIndex;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700245
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800246 /**
247 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
248 * {@link #onResume()} to populate the search box.
249 */
250 private String mVoiceSearchQuery;
251
Andrew Lee467de3d2015-03-17 15:47:24 -0700252 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700253 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700254 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700255 }
256
257 @Override
258 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700259 final boolean hasContactsPermission =
260 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700261 final Menu menu = getMenu();
262 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700263 clearFrequents.setVisible(mListsFragment != null &&
264 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700265 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
266
267 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
268 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700269
270 menu.findItem(R.id.menu_history).setVisible(
271 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700272 super.show();
273 }
274 }
275
Chiao Cheng94b10b52012-08-17 16:59:12 -0700276 /**
Yorke Lee28266192014-05-01 10:05:52 -0700277 * Listener that listens to drag events and sends their x and y coordinates to a
278 * {@link DragDropController}.
279 */
280 private class LayoutOnDragListener implements OnDragListener {
281 @Override
282 public boolean onDrag(View v, DragEvent event) {
283 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700284 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700285 }
286 return true;
287 }
288 }
289
290 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291 * Listener used to send search queries to the phone search fragment.
292 */
Yorke Lee53a22302014-04-29 18:13:46 -0700293 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700294 @Override
295 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
296 }
Yorke Leec3766332013-07-31 11:13:16 -0700297
Andrew Lee99a570c2014-05-15 14:18:50 -0700298 @Override
299 public void onTextChanged(CharSequence s, int start, int before, int count) {
300 final String newText = s.toString();
301 if (newText.equals(mSearchQuery)) {
302 // If the query hasn't changed (perhaps due to activity being destroyed
303 // and restored, or user launching the same DIAL intent twice), then there is
304 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700305 return;
306 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700307 if (DEBUG) {
308 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700309 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700310 }
Yorke Lee710709d2014-05-29 07:18:42 -0700311 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700312
Andrew Lee90374a72014-05-16 15:01:09 -0700313 // Show search fragment only when the query string is changed to non-empty text.
314 if (!TextUtils.isEmpty(newText)) {
315 // Call enterSearchUi only if we are switching search modes, or showing a search
316 // fragment for the first time.
317 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
318 (!mIsDialpadShown && mInRegularSearch);
319 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700320 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700321 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700322 }
323
Andrew Leea8515422014-06-13 16:53:54 -0700324 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700325 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700326 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700327 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700328 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700329 }
330
331 @Override
332 public void afterTextChanged(Editable s) {
333 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700334 };
335
Andrew Leeb1903842014-05-15 16:11:24 -0700336
337 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700338 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700339 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700340 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700341 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700342 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700343 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700344 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700345 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
346 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700347 }
Andrew Leeb1903842014-05-15 16:11:24 -0700348 }
349 };
350
351 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700352 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700353 */
354 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
355 @Override
356 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700357 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
358 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
359 // If the search term is empty, close the search UI.
360 maybeExitSearchUi();
361 } else {
362 // If the search term is not empty, show the dialpad fab.
363 showFabInSearchUi();
364 }
Andrew Leeb1903842014-05-15 16:11:24 -0700365 }
366 return false;
367 }
368 };
369
Chiao Cheng94b10b52012-08-17 16:59:12 -0700370 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700371 public boolean dispatchTouchEvent(MotionEvent ev) {
372 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
373 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
374 }
375 return super.dispatchTouchEvent(ev);
Nancy Chen49db1ad2014-08-18 20:10:17 -0700376 }
377
378 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700379 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800380 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700381 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700382
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;
Sarmad Hashmicb559812016-02-10 18:39:59 -0800428 mPreviouslySelectedTabIndex = ListsFragment.TAB_INDEX_SPEED_DIAL;
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) {
Yorke Lee58e68a92015-11-06 10:35:30 -0800533 Logger.logScreenView(ScreenEvent.DIALPAD, this);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800534 }
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() {
Sarmad Hashmicb559812016-02-10 18:39:59 -0800572 // Only clear missed calls if the pause was not triggered by an orientation change
573 // (or any other confirguration change)
574 if (!isChangingConfigurations()) {
575 updateMissedCalls();
576 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700577 if (mClearSearchOnPause) {
578 hideDialpadAndSearchUi();
579 mClearSearchOnPause = false;
580 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700581 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
582 commitDialpadFragmentHide();
583 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700584 super.onPause();
585 }
586
587 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700588 protected void onSaveInstanceState(Bundle outState) {
589 super.onSaveInstanceState(outState);
590 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700591 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
592 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700593 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700594 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700595 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700596 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700597 }
598
599 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700600 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700601 if (fragment instanceof DialpadFragment) {
602 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800603 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700604 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
605 transaction.hide(mDialpadFragment);
606 transaction.commit();
607 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700608 } else if (fragment instanceof SmartDialSearchFragment) {
609 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700610 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leed9a93162015-10-14 09:39:05 -0700611 if (!TextUtils.isEmpty(mDialpadQuery)) {
612 mSmartDialSearchFragment.setAddToContactNumber(mDialpadQuery);
613 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700614 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700615 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700616 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700617 } else if (fragment instanceof ListsFragment) {
618 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700619 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700620 }
Yorke Leec3766332013-07-31 11:13:16 -0700621 }
622
Alon Albertb453e5b2013-09-10 15:54:23 -0700623 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700624 final Intent intent = new Intent(this, DialerSettingsActivity.class);
625 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700626 }
627
Chiao Cheng94b10b52012-08-17 16:59:12 -0700628 @Override
629 public void onClick(View view) {
Sailesh Nepal68d86c62016-02-21 11:41:28 -0800630 int resId = view.getId();
631 if (resId == R.id.floating_action_button) {
632 if (mListsFragment.getCurrentTabIndex()
633 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
634 DialerUtils.startActivityWithErrorToast(
635 this,
636 IntentUtil.getNewContactIntent(),
637 R.string.add_contact_not_available);
638 } else if (!mIsDialpadShown) {
639 mInCallDialpadUp = false;
640 showDialpadFragment(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700641 }
Sailesh Nepal68d86c62016-02-21 11:41:28 -0800642 } else if (resId == R.id.voice_search_button) {
643 try {
644 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
645 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
646 } catch (ActivityNotFoundException e) {
647 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
648 Toast.LENGTH_SHORT).show();
649 }
650 } else if (resId == R.id.dialtacts_options_menu_button) {
651 mOverflowMenu.show();
652 } else {
653 Log.wtf(TAG, "Unexpected onClick event from " + view);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700654 }
655 }
656
Yorke Leec3766332013-07-31 11:13:16 -0700657 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700658 public boolean onMenuItemClick(MenuItem item) {
Jay Shrauner193cfc52015-09-25 14:13:16 -0700659 if (!isSafeToCommitTransactions()) {
Jay Shrauner119b0652015-09-17 12:00:18 -0700660 return true;
661 }
662
Sailesh Nepal68d86c62016-02-21 11:41:28 -0800663 int resId = item.getItemId();
664 if (resId == R.id.menu_history) {// Use explicit CallLogActivity intent instead of ACTION_VIEW +
665 // CONTENT_TYPE, so that we always open our call log from our dialer
666 final Intent intent = new Intent(this, CallLogActivity.class);
667 startActivity(intent);
668 } else if (resId == R.id.menu_add_contact) {
669 DialerUtils.startActivityWithErrorToast(
670 this,
671 IntentUtil.getNewContactIntent(),
672 R.string.add_contact_not_available);
673 } else if (resId == R.id.menu_import_export) {// We hard-code the "contactsAreAvailable" argument because doing it properly would
674 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
675 // now in Dialtacts for (potential) performance reasons. Compare with how it is
676 // done in {@link PeopleActivity}.
677 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
678 ImportExportDialogFragment.show(getFragmentManager(), true,
679 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_FAVORITES);
680 } else {
681 ImportExportDialogFragment.show(getFragmentManager(), true,
682 DialtactsActivity.class, ImportExportDialogFragment.EXPORT_MODE_DEFAULT);
683 }
684 Logger.logScreenView(ScreenEvent.IMPORT_EXPORT_CONTACTS, this);
685 return true;
686 } else if (resId == R.id.menu_clear_frequents) {
687 ClearFrequentsDialog.show(getFragmentManager());
688 Logger.logScreenView(ScreenEvent.CLEAR_FREQUENTS, this);
689 return true;
690 } else if (resId == R.id.menu_call_settings) {
691 handleMenuSettings();
692 Logger.logScreenView(ScreenEvent.SETTINGS, this);
693 return true;
Sarmad Hashmi3694b112016-02-17 17:39:36 -0800694 } else if (resId == R.id.menu_archive) {
695 final Intent intent = new Intent(this, VoicemailArchiveActivity.class);
696 startActivity(intent);
697 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700698 }
699 return false;
700 }
701
702 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700703 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
704 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
705 if (resultCode == RESULT_OK) {
706 final ArrayList<String> matches = data.getStringArrayListExtra(
707 RecognizerIntent.EXTRA_RESULTS);
708 if (matches.size() > 0) {
709 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800710 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700711 } else {
712 Log.e(TAG, "Voice search - nothing heard");
713 }
714 } else {
715 Log.e(TAG, "Voice search failed");
716 }
717 }
718 super.onActivityResult(requestCode, resultCode, data);
719 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700720
Andrew Lee2a58ab82014-05-15 02:16:04 -0700721 /**
Nancy Chenea7c0b82015-08-31 17:18:41 -0700722 * Update the number of unread voicemails (potentially other tabs) displayed next to the tab
723 * icon.
724 */
725 public void updateTabUnreadCounts() {
726 mListsFragment.updateTabUnreadCounts();
727 }
728
729 /**
Andrew Lee2a58ab82014-05-15 02:16:04 -0700730 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
731 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
732 * @see #onDialpadShown
733 */
Yorke Leec3766332013-07-31 11:13:16 -0700734 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700735 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700736 return;
737 }
738 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800739
Yorke Lee7ee5c422014-11-04 10:29:32 -0800740 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700741
Chiao Cheng94b10b52012-08-17 16:59:12 -0700742 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800743 if (mDialpadFragment == null) {
744 mDialpadFragment = new DialpadFragment();
745 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
746 } else {
747 ft.show(mDialpadFragment);
748 }
749
750 mDialpadFragment.setAnimate(animate);
Yorke Lee58e68a92015-11-06 10:35:30 -0800751 Logger.logScreenView(ScreenEvent.DIALPAD, this);
Yorke Leec3766332013-07-31 11:13:16 -0700752 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700753
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700754 if (animate) {
755 mFloatingActionButtonController.scaleOut();
756 } else {
757 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700758 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700759 }
Yorke Lee5253be02014-05-21 18:50:03 -0700760 mActionBarController.onDialpadUp();
761
Yorke Lee6c450eb2015-05-08 09:45:47 -0700762 mListsFragment.getView().animate().alpha(0).withLayer();
Ta-wei Yene3cae632015-10-27 16:09:09 -0700763
764 //adjust the title, so the user will know where we're at when the activity start/resumes.
765 setTitle(R.string.launcherDialpadActivityLabel);
Yorke Leec3766332013-07-31 11:13:16 -0700766 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700767
Andrew Lee2a58ab82014-05-15 02:16:04 -0700768 /**
769 * Callback from child DialpadFragment when the dialpad is shown.
770 */
771 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800772 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700773 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700774 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700775 } else {
776 mDialpadFragment.setYFraction(0);
777 }
778
Andrew Lee2a58ab82014-05-15 02:16:04 -0700779 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700780 }
781
782 /**
783 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
784 * a callback after the hide animation ends.
785 * @see #commitDialpadFragmentHide
786 */
Yorke Leeca195042013-09-25 16:29:38 -0700787 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700788 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700789 return;
790 }
Yorke Leef6673d32013-08-23 15:34:17 -0700791 if (clearDialpad) {
Ta-wei Yenaf875dd2015-11-03 15:57:03 -0800792 // Temporarily disable accessibility when we clear the dialpad, since it should be
793 // invisible and should not announce anything.
794 mDialpadFragment.getDigitsWidget().setImportantForAccessibility(
795 View.IMPORTANT_FOR_ACCESSIBILITY_NO);
Yorke Leef6673d32013-08-23 15:34:17 -0700796 mDialpadFragment.clearDialpad();
Ta-wei Yenaf875dd2015-11-03 15:57:03 -0800797 mDialpadFragment.getDigitsWidget().setImportantForAccessibility(
798 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Yorke Leef6673d32013-08-23 15:34:17 -0700799 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700800 if (!mIsDialpadShown) {
801 return;
Yorke Leec3766332013-07-31 11:13:16 -0700802 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700803 mIsDialpadShown = false;
804 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800805 mListsFragment.setUserVisibleHint(true);
806 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700807
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700808 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700809
Andrew Lee6324f702015-06-16 14:45:58 -0700810 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700811 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700812 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700813 } else {
814 commitDialpadFragmentHide();
815 }
816
Yorke Lee5253be02014-05-21 18:50:03 -0700817 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700818
Andrew Leed4cde832014-05-16 15:56:48 -0700819 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700820 if (TextUtils.isEmpty(mSearchQuery)) {
821 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700822 }
Andrew Leed4cde832014-05-16 15:56:48 -0700823 }
Ta-wei Yene3cae632015-10-27 16:09:09 -0700824 //reset the title to normal.
825 setTitle(R.string.launcherActivityLabel);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700826 }
827
828 /**
829 * Finishes hiding the dialpad fragment after any animations are completed.
830 */
831 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800832 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700833 final FragmentTransaction ft = getFragmentManager().beginTransaction();
834 ft.hide(mDialpadFragment);
835 ft.commit();
836 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700837 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700838 }
839
Andrew Lee2a58ab82014-05-15 02:16:04 -0700840 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700841 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700842 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700843 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700844 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700845 fragment = mRegularSearchFragment;
846 }
847 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700848 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700849 }
850 }
851
Yorke Lee5253be02014-05-21 18:50:03 -0700852 @Override
853 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700854 return mInDialpadSearch || mInRegularSearch;
855 }
856
Yorke Lee5253be02014-05-21 18:50:03 -0700857 @Override
858 public boolean hasSearchQuery() {
859 return !TextUtils.isEmpty(mSearchQuery);
860 }
861
862 @Override
863 public boolean shouldShowActionBar() {
864 return mListsFragment.shouldShowActionBar();
865 }
866
Yorke Leeb207f8a2013-08-29 18:51:06 -0700867 private void setNotInSearchUi() {
868 mInDialpadSearch = false;
869 mInRegularSearch = false;
870 }
871
Yorke Lee311969c2013-08-26 06:31:11 -0700872 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700873 if (mIsDialpadShown) {
874 hideDialpadFragment(false, true);
875 } else {
876 exitSearchUi();
877 }
Yorke Lee311969c2013-08-26 06:31:11 -0700878 }
879
Yorke Lee53a22302014-04-29 18:13:46 -0700880 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700881 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
882 if (canIntentBeHandled(voiceIntent)) {
883 mVoiceSearchButton.setVisibility(View.VISIBLE);
884 mVoiceSearchButton.setOnClickListener(this);
885 } else {
886 mVoiceSearchButton.setVisibility(View.GONE);
887 }
888 }
889
Yorke Leee9c82332015-10-06 14:21:53 -0700890 public boolean isNearbyPlacesSearchEnabled() {
891 return false;
892 }
893
Anne Rong35208002015-07-22 16:27:37 -0700894 protected int getSearchBoxHint () {
895 return R.string.dialer_hint_find_contact;
896 }
897
898 /**
899 * Sets the hint text for the contacts search box
900 */
901 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700902 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700903 .getCustomView().findViewById(R.id.search_view_container);
904 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
905 .setHint(getSearchBoxHint());
906 }
907
Andrew Lee467de3d2015-03-17 15:47:24 -0700908 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700909 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
910 popupMenu.inflate(R.menu.dialtacts_options);
Sarmad Hashmi78f55ff2016-03-04 12:32:17 -0800911 if (ObjectFactory.isVoicemailArchiveEnabled(this)) {
912 popupMenu.getMenu().findItem(R.id.menu_archive).setVisible(true);
913 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700914 popupMenu.setOnMenuItemClickListener(this);
915 return popupMenu;
916 }
917
Yorke Lee86e21f72014-04-02 16:49:38 -0700918 @Override
919 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700920 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700921 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700922 mPendingSearchViewQuery = null;
923 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700924 if (mActionBarController != null) {
925 mActionBarController.restoreActionBarOffset();
926 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700927 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700928 }
929
Chiao Cheng94b10b52012-08-17 16:59:12 -0700930 /**
931 * Returns true if the intent is due to hitting the green send key (hardware call button:
932 * KEYCODE_CALL) while in a call.
933 *
934 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700935 * @return true if the intent is due to hitting the green send key while in a call
936 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700937 private boolean isSendKeyWhileInCall(Intent intent) {
938 // If there is a call in progress and the user launched the dialer by hitting the call
939 // button, go straight to the in-call screen.
940 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700941
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700942 if (callKey) {
Yorke Lee762b3572015-09-18 12:54:59 -0700943 TelecomUtil.showInCallScreen(this, false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700944 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700945 }
946
947 return false;
948 }
949
950 /**
951 * Sets the current tab based on the intent's request type
952 *
953 * @param intent Intent that contains information about which tab should be selected
954 */
Yorke Leec3766332013-07-31 11:13:16 -0700955 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700956 // 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 -0700957 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700958 finish();
959 return;
960 }
961
Yorke Leed77017d2015-08-31 16:19:43 -0700962 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
963 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800964 showDialpadFragment(false);
965 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700966 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800967 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700968 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700969 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700970 }
971
972 @Override
973 public void onNewIntent(Intent newIntent) {
974 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700975
Nancy Chen199b7f02014-11-05 15:03:00 -0800976 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700977 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700978
Chiao Cheng94b10b52012-08-17 16:59:12 -0700979 invalidateOptionsMenu();
980 }
981
982 /** Returns true if the given intent contains a phone number to populate the dialer with */
983 private boolean isDialIntent(Intent intent) {
984 final String action = intent.getAction();
985 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
986 return true;
987 }
988 if (Intent.ACTION_VIEW.equals(action)) {
989 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700990 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700991 return true;
992 }
993 }
994 return false;
995 }
996
997 /**
Yorke Leec3766332013-07-31 11:13:16 -0700998 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700999 */
Yorke Lee6c450eb2015-05-08 09:45:47 -07001000 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001001 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -07001002 // Weird race condition where fragment is doing work after the activity is destroyed
1003 // due to talkback being on (b/10209937). Just return since we can't do any
1004 // constructive here.
1005 return;
1006 }
1007
Yorke Leeef2b7382013-08-09 17:39:25 -07001008 if (DEBUG) {
1009 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
1010 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001011
Yorke Leec3766332013-07-31 11:13:16 -07001012 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001013 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001014 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001015 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001016 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -07001017 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001018
Yorke Leeb207f8a2013-08-29 18:51:06 -07001019 final String tag;
1020 if (smartDialSearch) {
1021 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
1022 } else {
1023 tag = TAG_REGULAR_SEARCH_FRAGMENT;
1024 }
1025 mInDialpadSearch = smartDialSearch;
1026 mInRegularSearch = !smartDialSearch;
1027
Andrew Lee6324f702015-06-16 14:45:58 -07001028 mFloatingActionButtonController.scaleOut();
1029
Andrew Lee752956e2014-05-15 11:43:38 -07001030 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001031 if (animate) {
1032 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
1033 } else {
1034 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
1035 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001036 if (fragment == null) {
1037 if (smartDialSearch) {
1038 fragment = new SmartDialSearchFragment();
1039 } else {
Yorke Lee807afd82015-10-12 10:03:07 -07001040 fragment = ObjectFactory.newRegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001041 fragment.setOnTouchListener(new View.OnTouchListener() {
1042 @Override
1043 public boolean onTouch(View v, MotionEvent event) {
1044 // Show the FAB when the user touches the lists fragment and the soft
1045 // keyboard is hidden.
Nancy Chen0bb96ac2016-01-15 16:21:27 -08001046 hideDialpadFragment(true, false);
Andrew Lee6324f702015-06-16 14:45:58 -07001047 showFabInSearchUi();
1048 return false;
1049 }
1050 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001051 }
Andrew Leeb1903842014-05-15 16:11:24 -07001052 transaction.add(R.id.dialtacts_frame, fragment, tag);
1053 } else {
1054 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001055 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001056 // DialtactsActivity will provide the options menu
1057 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001058 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001059 if (!smartDialSearch) {
1060 fragment.setQueryString(query, false /* delaySelection */);
1061 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001062 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001063
Yorke Lee6c450eb2015-05-08 09:45:47 -07001064 if (animate) {
1065 mListsFragment.getView().animate().alpha(0).withLayer();
1066 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001067 mListsFragment.setUserVisibleHint(false);
Yorke Leef96bb182015-11-10 10:06:21 -08001068
1069 if (smartDialSearch) {
1070 Logger.logScreenView(ScreenEvent.SMART_DIAL_SEARCH, this);
1071 } else {
1072 Logger.logScreenView(ScreenEvent.REGULAR_SEARCH, this);
1073 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001074 }
1075
1076 /**
Yorke Leec3766332013-07-31 11:13:16 -07001077 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001078 */
Yorke Leec3766332013-07-31 11:13:16 -07001079 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001080 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001081 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001082 return;
1083 }
Andrew Leeb1903842014-05-15 16:11:24 -07001084
Andrew Leeb1903842014-05-15 16:11:24 -07001085 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001086
1087 if (mDialpadFragment != null) {
1088 mDialpadFragment.clearDialpad();
1089 }
1090
Yorke Leeb207f8a2013-08-29 18:51:06 -07001091 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001092
Andrew Lee6324f702015-06-16 14:45:58 -07001093 // Restore the FAB for the lists fragment.
1094 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1095 mFloatingActionButtonController.setVisible(false);
1096 }
1097 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1098 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1099 onPageSelected(mListsFragment.getCurrentTabIndex());
1100
Andrew Leeb1903842014-05-15 16:11:24 -07001101 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001102 if (mSmartDialSearchFragment != null) {
1103 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001104 }
Andrew Leeb1903842014-05-15 16:11:24 -07001105 if (mRegularSearchFragment != null) {
1106 transaction.remove(mRegularSearchFragment);
1107 }
1108 transaction.commit();
1109
1110 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001111
1112 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001113 // If the dialpad fragment wasn't previously visible, then send a screen view because
1114 // we are exiting regular search. Otherwise, the screen view will be sent by
1115 // {@link #hideDialpadFragment}.
1116 mListsFragment.sendScreenViewForCurrentPosition();
1117 mListsFragment.setUserVisibleHint(true);
1118 }
1119
Yorke Lee5253be02014-05-21 18:50:03 -07001120 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001121 }
1122
Chiao Cheng94b10b52012-08-17 16:59:12 -07001123 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001124 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001125 if (mStateSaved) {
1126 return;
1127 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001128 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001129 if (TextUtils.isEmpty(mSearchQuery) ||
1130 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1131 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001132 exitSearchUi();
1133 }
Yorke Leef6673d32013-08-23 15:34:17 -07001134 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001135 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001136 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001137 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001138 } else {
1139 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001140 }
Yorke Leec3766332013-07-31 11:13:16 -07001141 }
1142
Yorke Lee81a313d2015-06-01 14:53:59 -07001143 private void maybeEnterSearchUi() {
1144 if (!isInSearchUi()) {
1145 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1146 }
1147 }
1148
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001149 /**
1150 * @return True if the search UI was exited, false otherwise
1151 */
1152 private boolean maybeExitSearchUi() {
1153 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1154 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001155 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001156 return true;
1157 }
1158 return false;
1159 }
1160
Andrew Lee6324f702015-06-16 14:45:58 -07001161 private void showFabInSearchUi() {
1162 mFloatingActionButtonController.changeIcon(
1163 getResources().getDrawable(R.drawable.fab_ic_dial),
1164 getResources().getString(R.string.action_menu_dialpad_button));
1165 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1166 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1167 }
1168
Yorke Leec3766332013-07-31 11:13:16 -07001169 @Override
1170 public void onDialpadQueryChanged(String query) {
Yorke Leed9a93162015-10-14 09:39:05 -07001171 mDialpadQuery = query;
Yorke Lee46635872014-04-11 11:20:15 -07001172 if (mSmartDialSearchFragment != null) {
1173 mSmartDialSearchFragment.setAddToContactNumber(query);
1174 }
Yorke Leec3766332013-07-31 11:13:16 -07001175 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1176 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001177
1178 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001179 if (DEBUG) {
1180 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1181 }
1182 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1183 // This callback can happen if the dialpad fragment is recreated because of
1184 // activity destruction. In that case, don't update the search view because
1185 // that would bring the user back to the search fragment regardless of the
1186 // previous state of the application. Instead, just return here and let the
1187 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001188 if (!TextUtils.isEmpty(normalizedQuery)) {
1189 mPendingSearchViewQuery = normalizedQuery;
1190 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001191 return;
1192 }
Yorke Lee53a22302014-04-29 18:13:46 -07001193 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001194 }
Santos Cordon83131712015-04-16 21:25:35 -07001195
1196 try {
1197 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1198 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1199 }
1200 } catch (Exception ignored) {
1201 // Skip any exceptions for this piece of code
1202 }
Yorke Lee575ae382015-07-16 11:36:07 -07001203 }
Santos Cordon83131712015-04-16 21:25:35 -07001204
Yorke Lee575ae382015-07-16 11:36:07 -07001205 @Override
1206 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1207 if (mInDialpadSearch && mSmartDialSearchFragment != null
1208 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1209 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1210 return true;
1211 }
1212 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001213 }
Yorke Leec3766332013-07-31 11:13:16 -07001214
1215 @Override
1216 public void onListFragmentScrollStateChange(int scrollState) {
1217 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001218 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001219 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001220 }
1221 }
1222
1223 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001224 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001225 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001226 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1227 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001228 }
1229
Yorke Leec3766332013-07-31 11:13:16 -07001230 private boolean phoneIsInUse() {
Yorke Lee827a90f2015-09-17 18:10:58 -07001231 return TelecomUtil.isInCall(this);
Yorke Leec3766332013-07-31 11:13:16 -07001232 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001233
Yorke Leeda0f9042013-12-05 14:25:51 -08001234 private boolean canIntentBeHandled(Intent intent) {
1235 final PackageManager packageManager = getPackageManager();
1236 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1237 PackageManager.MATCH_DEFAULT_ONLY);
1238 return resolveInfo != null && resolveInfo.size() > 0;
1239 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001240
Yorke Lee86e21f72014-04-02 16:49:38 -07001241 /**
1242 * Called when the user has long-pressed a contact tile to start a drag operation.
1243 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001244 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001245 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001246 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001247 }
1248
1249 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001250 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1251 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001252
Yorke Lee86e21f72014-04-02 16:49:38 -07001253 /**
1254 * Called when the user has released a contact tile after long-pressing it.
1255 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001256 @Override
1257 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001258 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001259 }
1260
1261 @Override
1262 public void onDroppedOnRemove() {}
1263
1264 /**
Yorke Leed999b712014-04-23 15:10:58 -07001265 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001266 * once it has been attached to the activity.
1267 */
1268 @Override
1269 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001270 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001271 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001272 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001273
Yorke Lee38019af2015-07-14 17:05:38 -07001274 /**
1275 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1276 */
1277 @Override
1278 public void showAllContactsTab() {
1279 if (mListsFragment != null) {
1280 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1281 }
1282 }
1283
1284 /**
1285 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1286 */
1287 @Override
1288 public void showDialpad() {
1289 showDialpadFragment(true);
1290 }
1291
Yorke Leee00c9fe2014-04-12 12:42:06 -07001292 @Override
Tyler Gunnb31f5d12015-12-18 13:57:37 -08001293 public void onPickDataUri(Uri dataUri, boolean isVideoCall, int callInitiationType) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001294 mClearSearchOnPause = true;
Yorke Leec8d6e162015-09-14 18:43:27 -07001295 PhoneNumberInteraction.startInteractionForPhoneCall(
Tyler Gunnb31f5d12015-12-18 13:57:37 -08001296 DialtactsActivity.this, dataUri, isVideoCall, callInitiationType);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001297 }
1298
1299 @Override
Anne Rong071d0a42015-09-29 15:15:26 -07001300 public void onPickPhoneNumber(String phoneNumber, boolean isVideoCall, int callInitiationType) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001301 if (phoneNumber == null) {
1302 // Invalid phone number, but let the call go through so that InCallUI can show
1303 // an error message.
1304 phoneNumber = "";
1305 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001306
1307 final Intent intent = new CallIntentBuilder(phoneNumber)
1308 .setIsVideoCall(isVideoCall)
Yorke Leec8d6e162015-09-14 18:43:27 -07001309 .setCallInitiationType(callInitiationType)
Yorke Leee3a2d132015-09-14 16:52:08 -07001310 .build();
1311
Yorke Lee7d20f822014-06-19 17:09:33 -07001312 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001313 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001314 }
1315
1316 @Override
1317 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001318 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001319 }
1320
1321 @Override
1322 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001323 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001324 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001325
Yorke Leecc4660d2014-04-24 11:22:57 -07001326 @Override
1327 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001328 int tabIndex = mListsFragment.getCurrentTabIndex();
1329
Andrew Lee4de59fb2015-08-13 15:20:08 -07001330 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1331 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1332 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001333 boolean isRtl = DialerUtils.isRtl();
1334 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1335 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001336 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001337 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1338 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001339 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001340 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001341 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001342
Andrew Lee432d4b52015-05-20 16:52:00 -07001343 @Override
1344 public void onPageSelected(int position) {
Sarmad Hashmicb559812016-02-10 18:39:59 -08001345 updateMissedCalls();
Andrew Lee432d4b52015-05-20 16:52:00 -07001346 int tabIndex = mListsFragment.getCurrentTabIndex();
Sarmad Hashmicb559812016-02-10 18:39:59 -08001347 mPreviouslySelectedTabIndex = tabIndex;
Andrew Lee432d4b52015-05-20 16:52:00 -07001348 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001349 mFloatingActionButtonController.changeIcon(
1350 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1351 getResources().getString(R.string.search_shortcut_create_new_contact));
1352 } else {
1353 mFloatingActionButtonController.changeIcon(
1354 getResources().getDrawable(R.drawable.fab_ic_dial),
1355 getResources().getString(R.string.action_menu_dialpad_button));
1356 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001357 }
1358
1359 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001360 public void onPageScrollStateChanged(int state) {
1361 }
1362
Yorke Lee5253be02014-05-21 18:50:03 -07001363 @Override
1364 public boolean isActionBarShowing() {
1365 return mActionBarController.isActionBarShowing();
1366 }
1367
Yorke Leec98a5bb2014-10-28 16:12:05 -07001368 @Override
1369 public ActionBarController getActionBarController() {
1370 return mActionBarController;
1371 }
1372
Yorke Lee6bc67162015-06-27 14:03:25 -07001373 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001374 public boolean isDialpadShown() {
1375 return mIsDialpadShown;
1376 }
1377
Yorke Lee5253be02014-05-21 18:50:03 -07001378 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001379 public int getDialpadHeight() {
1380 if (mDialpadFragment != null) {
1381 return mDialpadFragment.getDialpadHeight();
1382 }
1383 return 0;
1384 }
1385
1386 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001387 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001388 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001389 }
1390
1391 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001392 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001393 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001394 }
1395
1396 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001397 public int getActionBarHeight() {
1398 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001399 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001400
Andrew Lee6324f702015-06-16 14:45:58 -07001401 private int getFabAlignment() {
1402 if (!mIsLandscape && !isInSearchUi() &&
1403 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1404 return FloatingActionButtonController.ALIGN_MIDDLE;
1405 }
1406 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001407 }
Sarmad Hashmicb559812016-02-10 18:39:59 -08001408
1409 private void updateMissedCalls() {
1410 if (mPreviouslySelectedTabIndex == ListsFragment.TAB_INDEX_HISTORY) {
1411 mListsFragment.markMissedCallsAsReadAndRemoveNotifications();
1412 }
1413 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001414}