blob: 8fa56f02ffec8ca8bda4fd14704e376ebbf752bc [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
Yorke Leec3766332013-07-31 11:13:16 -07002 * Copyright (C) 2013 The Android Open Source Project
Chiao Cheng94b10b52012-08-17 16:59:12 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer;
18
Yorke Lee53a22302014-04-29 18:13:46 -070019import android.app.ActionBar;
Chiao Cheng94b10b52012-08-17 16:59:12 -070020import android.app.Fragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -070021import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080022import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070023import android.content.Context;
24import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080025import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
Sai Cheemalapati82d6da52014-06-03 13:54:23 -070027import android.content.res.Configuration;
Andrew Lee2423a2f2014-05-29 14:14:45 -070028import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070029import android.net.Uri;
30import android.os.Bundle;
Yorke Leef5554ce2015-01-27 14:46:02 -080031import android.os.Trace;
Nancy Chen019713e2015-07-06 18:25:24 -070032import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070033import android.speech.RecognizerIntent;
Yorke Leecc4660d2014-04-24 11:22:57 -070034import android.support.v4.view.ViewPager;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070035import android.telecom.PhoneAccount;
36import android.telecom.TelecomManager;
Yorke Lee53a22302014-04-29 18:13:46 -070037import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070039import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070040import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070041import android.view.DragEvent;
Yorke Lee74edcdc2014-07-11 16:15:08 -070042import android.view.Gravity;
Andrew Leeb1903842014-05-15 16:11:24 -070043import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070046import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070048import android.view.View.OnDragListener;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070049import android.view.View.OnTouchListener;
Ihab Awad9134e252014-07-09 12:32:52 -070050import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070051import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070052import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070053import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070054import android.widget.EditText;
Yorke Lee28aab272014-06-11 10:24:19 -070055import android.widget.FrameLayout;
Andrew Leea73e1892014-05-13 13:21:16 -070056import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.widget.PopupMenu;
Anne Rong35208002015-07-22 16:27:37 -070058import android.widget.TextView;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080059import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070060
Brian Attwellc311af12014-10-22 18:28:58 -070061import com.android.contacts.common.activity.TransactionSafeActivity;
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 Leeaf6f1952014-07-14 19:13:16 -070085import com.android.dialer.settings.DialerSettingsActivity;
Andrew Lee247df6e2015-05-12 19:09:00 -070086import com.android.dialer.util.IntentUtil;
Yorke Lee7d20f822014-06-19 17:09:33 -070087import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070088import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070089import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070090import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070091import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee2fec47b2014-08-05 18:16:27 -070092import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070093import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070094
Yorke Leed5c512a2015-01-30 14:42:53 -080095import junit.framework.Assert;
96
Yorke Leec3766332013-07-31 11:13:16 -070097import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080098import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070099
Chiao Cheng94b10b52012-08-17 16:59:12 -0700100/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101 * The dialer tab's title is 'phone', a more common name (see strings.xml).
102 */
Yorke Leec3766332013-07-31 11:13:16 -0700103public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700104 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700105 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700106 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700107 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700108 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700109 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700110 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700111 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700112 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700113 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700114 ViewPager.OnPageChangeListener,
115 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700116 private static final String TAG = "DialtactsActivity";
117
Yorke Lee7cc61492015-06-01 14:59:33 -0700118 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119
Yorke Leef74011e2013-08-02 11:30:30 -0700120 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
121
Chiao Cheng94b10b52012-08-17 16:59:12 -0700122 /** @see #getCallOrigin() */
123 private static final String CALL_ORIGIN_DIALTACTS =
124 "com.android.dialer.DialtactsActivity";
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 Leec3766332013-07-31 11:13:16 -0700132 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
133 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
134 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
135 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700136
Chiao Cheng94b10b52012-08-17 16:59:12 -0700137 /**
138 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
139 */
140 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700141 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700142
Yorke Leec3766332013-07-31 11:13:16 -0700143 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700144
Andrew Lee6324f702015-06-16 14:45:58 -0700145 private static final int FAB_SCALE_IN_DELAY_MS = 300;
146
Yorke Leef614f6f2014-10-03 10:44:19 -0700147 private FrameLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700148
Yorke Leec3766332013-07-31 11:13:16 -0700149 /**
Yorke Leec3766332013-07-31 11:13:16 -0700150 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700151 */
Evan Charlton72d66252014-11-07 16:23:47 -0800152 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700153
154 /**
155 * Fragment for searching phone numbers using the alphanumeric keyboard.
156 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700157 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700158
159 /**
160 * Fragment for searching phone numbers using the dialpad.
161 */
162 private SmartDialSearchFragment mSmartDialSearchFragment;
163
Yorke Leee00c9fe2014-04-12 12:42:06 -0700164 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700165 * Animation that slides in.
166 */
167 private Animation mSlideIn;
168
169 /**
170 * Animation that slides out.
171 */
172 private Animation mSlideOut;
173
Yorke Lee6c450eb2015-05-08 09:45:47 -0700174 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
175 @Override
176 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700177 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700178 }
179 };
180
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700181 /**
182 * Listener for after slide out animation completes on dialer fragment.
183 */
184 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
185 @Override
186 public void onAnimationEnd(Animation animation) {
187 commitDialpadFragmentHide();
188 }
189 };
190
191 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700192 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700193 */
194 private ListsFragment mListsFragment;
195
Yorke Lee19e68ca2014-10-28 11:51:40 -0700196 /**
197 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
198 * be commited.
199 */
200 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800201 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700202 private boolean mInDialpadSearch;
203 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700204 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700205 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700206 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700207
Yorke Leeef2b7382013-08-09 17:39:25 -0700208 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700209 * Whether or not the device is in landscape orientation.
210 */
211 private boolean mIsLandscape;
212
213 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700214 * True if the dialpad is only temporarily showing due to being in call
215 */
216 private boolean mInCallDialpadUp;
217
218 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700219 * True when this activity has been launched for the first time.
220 */
Yorke Lee98702de2013-08-06 12:03:32 -0700221 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700222
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700223 /**
224 * Search query to be applied to the SearchView in the ActionBar once
225 * onCreateOptionsMenu has been called.
226 */
227 private String mPendingSearchViewQuery;
228
Yorke Lee74edcdc2014-07-11 16:15:08 -0700229 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700230 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700231 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700232
Yorke Leeef2b7382013-08-09 17:39:25 -0700233 private String mSearchQuery;
234
Yorke Leefce269a2013-08-12 11:56:04 -0700235 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700236 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700237 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700238
Sai Cheemalapati41460652014-06-02 21:05:39 -0700239 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700240
Andrew Lee2423a2f2014-05-29 14:14:45 -0700241 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700242
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800243 /**
244 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
245 * {@link #onResume()} to populate the search box.
246 */
247 private String mVoiceSearchQuery;
248
Andrew Lee467de3d2015-03-17 15:47:24 -0700249 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700250 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700251 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700252 }
253
254 @Override
255 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700256 final boolean hasContactsPermission =
257 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700258 final Menu menu = getMenu();
259 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700260 clearFrequents.setVisible(mListsFragment != null &&
261 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700262 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
263
264 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
265 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700266
267 menu.findItem(R.id.menu_history).setVisible(
268 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700269 super.show();
270 }
271 }
272
Chiao Cheng94b10b52012-08-17 16:59:12 -0700273 /**
Yorke Lee28266192014-05-01 10:05:52 -0700274 * Listener that listens to drag events and sends their x and y coordinates to a
275 * {@link DragDropController}.
276 */
277 private class LayoutOnDragListener implements OnDragListener {
278 @Override
279 public boolean onDrag(View v, DragEvent event) {
280 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700281 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700282 }
283 return true;
284 }
285 }
286
287 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700288 * Listener used to send search queries to the phone search fragment.
289 */
Yorke Lee53a22302014-04-29 18:13:46 -0700290 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700291 @Override
292 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
293 }
Yorke Leec3766332013-07-31 11:13:16 -0700294
Andrew Lee99a570c2014-05-15 14:18:50 -0700295 @Override
296 public void onTextChanged(CharSequence s, int start, int before, int count) {
297 final String newText = s.toString();
298 if (newText.equals(mSearchQuery)) {
299 // If the query hasn't changed (perhaps due to activity being destroyed
300 // and restored, or user launching the same DIAL intent twice), then there is
301 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700302 return;
303 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700304 if (DEBUG) {
305 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700306 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700307 }
Yorke Lee710709d2014-05-29 07:18:42 -0700308 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700309
Andrew Lee90374a72014-05-16 15:01:09 -0700310 // Show search fragment only when the query string is changed to non-empty text.
311 if (!TextUtils.isEmpty(newText)) {
312 // Call enterSearchUi only if we are switching search modes, or showing a search
313 // fragment for the first time.
314 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
315 (!mIsDialpadShown && mInRegularSearch);
316 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700317 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700318 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700319 }
320
Andrew Leea8515422014-06-13 16:53:54 -0700321 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700322 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700323 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700324 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700325 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700326 }
327
328 @Override
329 public void afterTextChanged(Editable s) {
330 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700331 };
332
Andrew Leeb1903842014-05-15 16:11:24 -0700333
334 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700335 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700336 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700337 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700338 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700339 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700340 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700341 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700342 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
343 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700344 }
Andrew Leeb1903842014-05-15 16:11:24 -0700345 }
346 };
347
348 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700349 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700350 */
351 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
352 @Override
353 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700354 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
355 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
356 // If the search term is empty, close the search UI.
357 maybeExitSearchUi();
358 } else {
359 // If the search term is not empty, show the dialpad fab.
360 showFabInSearchUi();
361 }
Andrew Leeb1903842014-05-15 16:11:24 -0700362 }
363 return false;
364 }
365 };
366
Chiao Cheng94b10b52012-08-17 16:59:12 -0700367 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700368 public boolean dispatchTouchEvent(MotionEvent ev) {
369 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
370 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
371 }
372 return super.dispatchTouchEvent(ev);
373
374 }
375
376 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700377 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800378 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700379 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700380
Yorke Lee98702de2013-08-06 12:03:32 -0700381 mFirstLaunch = true;
382
Andrew Lee2423a2f2014-05-29 14:14:45 -0700383 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700384 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700385
Yorke Leef5554ce2015-01-27 14:46:02 -0800386 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700387 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800388 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800389 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700390
Yorke Leef5554ce2015-01-27 14:46:02 -0800391 Trace.beginSection(TAG + " setup Views");
Yorke Lee53a22302014-04-29 18:13:46 -0700392 final ActionBar actionBar = getActionBar();
393 actionBar.setCustomView(R.layout.search_edittext);
394 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700395 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700396
Anne Rong35208002015-07-22 16:27:37 -0700397 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
398 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700399 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
400
Andrew Leee3f59a82015-02-26 12:06:35 -0800401 mActionBarController = new ActionBarController(this, searchEditTextLayout);
402
Andrew Lee04675a52014-06-05 18:36:20 -0700403 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700404 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700405 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
406 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
407 .setOnClickListener(mSearchViewOnClickListener);
408 searchEditTextLayout.findViewById(R.id.search_box_start_search)
409 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700410 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700411 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700412 @Override
413 public void onBackButtonClicked() {
414 onBackPressed();
415 }
Andrew Lee6324f702015-06-16 14:45:58 -0700416
417 @Override
418 public void onSearchViewClicked() {
419 // Hide FAB, as the keyboard is shown.
420 mFloatingActionButtonController.scaleOut();
421 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700422 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700423
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700424 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700425 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700426
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700427 final View floatingActionButtonContainer = findViewById(
428 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700429 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
430 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700431 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700432 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700433
Andrew Lee04675a52014-06-05 18:36:20 -0700434 ImageButton optionsMenuButton =
435 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700436 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700437 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
438 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700439
Yorke Leed5c512a2015-01-30 14:42:53 -0800440 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
441 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700442 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800443 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000444 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800445 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700446 } else {
447 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700448 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
449 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700450 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700451 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700452 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700453 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700454
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700455 final boolean isLayoutRtl = DialerUtils.isRtl();
456 if (mIsLandscape) {
457 mSlideIn = AnimationUtils.loadAnimation(this,
458 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
459 mSlideOut = AnimationUtils.loadAnimation(this,
460 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
461 } else {
462 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
463 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
464 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700465
Yorke Lee2fec47b2014-08-05 18:16:27 -0700466 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
467 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
468
Yorke Lee6c450eb2015-05-08 09:45:47 -0700469 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700470 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700471
Yorke Leef614f6f2014-10-03 10:44:19 -0700472 mParentLayout = (FrameLayout) findViewById(R.id.dialtacts_mainlayout);
473 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700474 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700475 new ViewTreeObserver.OnGlobalLayoutListener() {
476 @Override
477 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700478 final ViewTreeObserver observer =
479 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700480 if (!observer.isAlive()) {
481 return;
482 }
483 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700484 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700485 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700486 mFloatingActionButtonController.align(
487 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700488 }
489 });
Andrew Lee0c667702014-05-12 14:31:45 -0700490
Yorke Leef5554ce2015-01-27 14:46:02 -0800491 Trace.endSection();
492
493 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700494 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700495 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800496 Trace.endSection();
497 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700498 }
499
500 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700501 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800502 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700503 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700504
Yorke Lee19e68ca2014-10-28 11:51:40 -0700505 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700506 if (mFirstLaunch) {
507 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700508 } else if (!phoneIsInUse() && mInCallDialpadUp) {
509 hideDialpadFragment(false, true);
510 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700511 } else if (mShowDialpadOnResume) {
512 showDialpadFragment(false);
513 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700514 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800515
516 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
517 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
518 // shown until onResume has completed. Active the search UI and set the search term now.
519 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
520 mActionBarController.onSearchBoxTapped();
521 mSearchView.setText(mVoiceSearchQuery);
522 mVoiceSearchQuery = null;
523 }
524
Yorke Lee98702de2013-08-06 12:03:32 -0700525 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800526
527 if (mIsRestarting) {
528 // This is only called when the activity goes from resumed -> paused -> resumed, so it
529 // will not cause an extra view to be sent out on rotation
530 if (mIsDialpadShown) {
531 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
532 }
533 mIsRestarting = false;
534 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700535
Yorke Lee53a22302014-04-29 18:13:46 -0700536 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700537 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700538 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700539
540 if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
541 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
542 if (index < mListsFragment.getTabCount()) {
543 mListsFragment.showTab(index);
544 }
Nancy Chen019713e2015-07-06 18:25:24 -0700545 } else if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
Andrew Lee4de59fb2015-08-13 15:20:08 -0700546 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
Nancy Chend5de03b2015-07-01 09:20:45 -0700547 }
548
Anne Rong35208002015-07-22 16:27:37 -0700549 setSearchBoxHint();
550
Yorke Leef5554ce2015-01-27 14:46:02 -0800551 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700552 }
553
554 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800555 protected void onRestart() {
556 super.onRestart();
557 mIsRestarting = true;
558 }
559
560 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700561 protected void onPause() {
562 if (mClearSearchOnPause) {
563 hideDialpadAndSearchUi();
564 mClearSearchOnPause = false;
565 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700566 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
567 commitDialpadFragmentHide();
568 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700569 super.onPause();
570 }
571
572 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700573 protected void onSaveInstanceState(Bundle outState) {
574 super.onSaveInstanceState(outState);
575 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700576 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
577 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700578 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700579 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700580 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700581 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700582 }
583
584 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700585 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700586 if (fragment instanceof DialpadFragment) {
587 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800588 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700589 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
590 transaction.hide(mDialpadFragment);
591 transaction.commit();
592 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700593 } else if (fragment instanceof SmartDialSearchFragment) {
594 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700595 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700596 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700597 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700598 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700599 } else if (fragment instanceof ListsFragment) {
600 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700601 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700602 }
Yorke Leec3766332013-07-31 11:13:16 -0700603 }
604
Alon Albertb453e5b2013-09-10 15:54:23 -0700605 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700606 final Intent intent = new Intent(this, DialerSettingsActivity.class);
607 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700608 }
609
Chiao Cheng94b10b52012-08-17 16:59:12 -0700610 @Override
611 public void onClick(View view) {
612 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700613 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700614 if (mListsFragment.getCurrentTabIndex()
615 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700616 DialerUtils.startActivityWithErrorToast(
617 this,
618 IntentUtil.getNewContactIntent(),
619 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700620 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700621 mInCallDialpadUp = false;
622 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700623 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700624 break;
Yorke Leec3766332013-07-31 11:13:16 -0700625 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800626 try {
627 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
628 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
629 } catch (ActivityNotFoundException e) {
630 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
631 Toast.LENGTH_SHORT).show();
632 }
Yorke Leec3766332013-07-31 11:13:16 -0700633 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700634 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700635 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700636 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700637 default: {
638 Log.wtf(TAG, "Unexpected onClick event from " + view);
639 break;
640 }
641 }
642 }
643
Yorke Leec3766332013-07-31 11:13:16 -0700644 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700645 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700646 switch (item.getItemId()) {
647 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700648 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
649 // CONTENT_TYPE, so that we always open our call log from our dialer
650 final Intent intent = new Intent(this, CallLogActivity.class);
651 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700652 break;
653 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700654 DialerUtils.startActivityWithErrorToast(
655 this,
656 IntentUtil.getNewContactIntent(),
657 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700658 break;
659 case R.id.menu_import_export:
660 // We hard-code the "contactsAreAvailable" argument because doing it properly would
661 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
662 // now in Dialtacts for (potential) performance reasons. Compare with how it is
663 // done in {@link PeopleActivity}.
664 ImportExportDialogFragment.show(getFragmentManager(), true,
665 DialtactsActivity.class);
666 return true;
667 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700668 ClearFrequentsDialog.show(getFragmentManager());
669 return true;
670 case R.id.menu_call_settings:
671 handleMenuSettings();
672 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700673 }
674 return false;
675 }
676
677 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700678 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
679 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
680 if (resultCode == RESULT_OK) {
681 final ArrayList<String> matches = data.getStringArrayListExtra(
682 RecognizerIntent.EXTRA_RESULTS);
683 if (matches.size() > 0) {
684 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800685 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700686 } else {
687 Log.e(TAG, "Voice search - nothing heard");
688 }
689 } else {
690 Log.e(TAG, "Voice search failed");
691 }
692 }
693 super.onActivityResult(requestCode, resultCode, data);
694 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700695
Andrew Lee2a58ab82014-05-15 02:16:04 -0700696 /**
697 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
698 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
699 * @see #onDialpadShown
700 */
Yorke Leec3766332013-07-31 11:13:16 -0700701 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700702 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700703 return;
704 }
705 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800706
Yorke Lee7ee5c422014-11-04 10:29:32 -0800707 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700708
Chiao Cheng94b10b52012-08-17 16:59:12 -0700709 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800710 if (mDialpadFragment == null) {
711 mDialpadFragment = new DialpadFragment();
712 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
713 } else {
714 ft.show(mDialpadFragment);
715 }
716
717 mDialpadFragment.setAnimate(animate);
718 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700719 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700720
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700721 if (animate) {
722 mFloatingActionButtonController.scaleOut();
723 } else {
724 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700725 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700726 }
Yorke Lee5253be02014-05-21 18:50:03 -0700727 mActionBarController.onDialpadUp();
728
Yorke Lee6c450eb2015-05-08 09:45:47 -0700729 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700730 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700731
Andrew Lee2a58ab82014-05-15 02:16:04 -0700732 /**
733 * Callback from child DialpadFragment when the dialpad is shown.
734 */
735 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800736 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700737 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700738 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700739 } else {
740 mDialpadFragment.setYFraction(0);
741 }
742
Andrew Lee2a58ab82014-05-15 02:16:04 -0700743 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700744 }
745
746 /**
747 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
748 * a callback after the hide animation ends.
749 * @see #commitDialpadFragmentHide
750 */
Yorke Leeca195042013-09-25 16:29:38 -0700751 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700752 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700753 return;
754 }
Yorke Leef6673d32013-08-23 15:34:17 -0700755 if (clearDialpad) {
756 mDialpadFragment.clearDialpad();
757 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700758 if (!mIsDialpadShown) {
759 return;
Yorke Leec3766332013-07-31 11:13:16 -0700760 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700761 mIsDialpadShown = false;
762 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800763 mListsFragment.setUserVisibleHint(true);
764 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700765
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700766 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700767
Andrew Lee6324f702015-06-16 14:45:58 -0700768 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700769 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700770 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700771 } else {
772 commitDialpadFragmentHide();
773 }
774
Yorke Lee5253be02014-05-21 18:50:03 -0700775 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700776
Andrew Leed4cde832014-05-16 15:56:48 -0700777 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700778 if (TextUtils.isEmpty(mSearchQuery)) {
779 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700780 }
Andrew Leed4cde832014-05-16 15:56:48 -0700781 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700782 }
783
784 /**
785 * Finishes hiding the dialpad fragment after any animations are completed.
786 */
787 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800788 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700789 final FragmentTransaction ft = getFragmentManager().beginTransaction();
790 ft.hide(mDialpadFragment);
791 ft.commit();
792 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700793 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700794 }
795
Andrew Lee2a58ab82014-05-15 02:16:04 -0700796 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700797 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700798 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700799 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700800 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700801 fragment = mRegularSearchFragment;
802 }
803 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700804 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700805 }
806 }
807
Yorke Lee5253be02014-05-21 18:50:03 -0700808 @Override
809 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700810 return mInDialpadSearch || mInRegularSearch;
811 }
812
Yorke Lee5253be02014-05-21 18:50:03 -0700813 @Override
814 public boolean hasSearchQuery() {
815 return !TextUtils.isEmpty(mSearchQuery);
816 }
817
818 @Override
819 public boolean shouldShowActionBar() {
820 return mListsFragment.shouldShowActionBar();
821 }
822
Yorke Leeb207f8a2013-08-29 18:51:06 -0700823 private void setNotInSearchUi() {
824 mInDialpadSearch = false;
825 mInRegularSearch = false;
826 }
827
Yorke Lee311969c2013-08-26 06:31:11 -0700828 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700829 if (mIsDialpadShown) {
830 hideDialpadFragment(false, true);
831 } else {
832 exitSearchUi();
833 }
Yorke Lee311969c2013-08-26 06:31:11 -0700834 }
835
Yorke Lee53a22302014-04-29 18:13:46 -0700836 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700837 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
838 if (canIntentBeHandled(voiceIntent)) {
839 mVoiceSearchButton.setVisibility(View.VISIBLE);
840 mVoiceSearchButton.setOnClickListener(this);
841 } else {
842 mVoiceSearchButton.setVisibility(View.GONE);
843 }
844 }
845
Anne Rong35208002015-07-22 16:27:37 -0700846 protected int getSearchBoxHint () {
847 return R.string.dialer_hint_find_contact;
848 }
849
850 /**
851 * Sets the hint text for the contacts search box
852 */
853 private void setSearchBoxHint() {
854 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getActionBar()
855 .getCustomView().findViewById(R.id.search_view_container);
856 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
857 .setHint(getSearchBoxHint());
858 }
859
Andrew Lee467de3d2015-03-17 15:47:24 -0700860 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700861 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
862 popupMenu.inflate(R.menu.dialtacts_options);
863 popupMenu.setOnMenuItemClickListener(this);
864 return popupMenu;
865 }
866
Yorke Lee86e21f72014-04-02 16:49:38 -0700867 @Override
868 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700869 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700870 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700871 mPendingSearchViewQuery = null;
872 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700873 if (mActionBarController != null) {
874 mActionBarController.restoreActionBarOffset();
875 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700876 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700877 }
878
Chiao Cheng94b10b52012-08-17 16:59:12 -0700879 /**
880 * Returns true if the intent is due to hitting the green send key (hardware call button:
881 * KEYCODE_CALL) while in a call.
882 *
883 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700884 * @return true if the intent is due to hitting the green send key while in a call
885 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700886 private boolean isSendKeyWhileInCall(Intent intent) {
887 // If there is a call in progress and the user launched the dialer by hitting the call
888 // button, go straight to the in-call screen.
889 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700890
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700891 if (callKey) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700892 getTelecomManager().showInCallScreen(false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700893 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700894 }
895
896 return false;
897 }
898
899 /**
900 * Sets the current tab based on the intent's request type
901 *
902 * @param intent Intent that contains information about which tab should be selected
903 */
Yorke Leec3766332013-07-31 11:13:16 -0700904 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700905 // 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 -0700906 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700907 finish();
908 return;
909 }
910
Yorke Leed5c512a2015-01-30 14:42:53 -0800911 final boolean phoneIsInUse = phoneIsInUse();
912 if (phoneIsInUse || (intent.getData() != null && isDialIntent(intent))) {
913 showDialpadFragment(false);
914 mDialpadFragment.setStartedFromNewIntent(true);
915 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
916 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700917 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700918 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700919 }
920
921 @Override
922 public void onNewIntent(Intent newIntent) {
923 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700924
Nancy Chen199b7f02014-11-05 15:03:00 -0800925 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700926 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700927
Chiao Cheng94b10b52012-08-17 16:59:12 -0700928 invalidateOptionsMenu();
929 }
930
931 /** Returns true if the given intent contains a phone number to populate the dialer with */
932 private boolean isDialIntent(Intent intent) {
933 final String action = intent.getAction();
934 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
935 return true;
936 }
937 if (Intent.ACTION_VIEW.equals(action)) {
938 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700939 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700940 return true;
941 }
942 }
943 return false;
944 }
945
946 /**
947 * Returns an appropriate call origin for this Activity. May return null when no call origin
948 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
949 * for remembering the tab in which the user made a phone call, so the external app's DIAL
950 * request should not be counted.)
951 */
952 public String getCallOrigin() {
953 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
954 }
955
Chiao Cheng94b10b52012-08-17 16:59:12 -0700956 /**
Yorke Leec3766332013-07-31 11:13:16 -0700957 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700958 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700959 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700960 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700961 // Weird race condition where fragment is doing work after the activity is destroyed
962 // due to talkback being on (b/10209937). Just return since we can't do any
963 // constructive here.
964 return;
965 }
966
Yorke Leeef2b7382013-08-09 17:39:25 -0700967 if (DEBUG) {
968 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
969 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700970
Yorke Leec3766332013-07-31 11:13:16 -0700971 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700972 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700973 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700974 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700975 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700976 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700977
Yorke Leeb207f8a2013-08-29 18:51:06 -0700978 final String tag;
979 if (smartDialSearch) {
980 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
981 } else {
982 tag = TAG_REGULAR_SEARCH_FRAGMENT;
983 }
984 mInDialpadSearch = smartDialSearch;
985 mInRegularSearch = !smartDialSearch;
986
Andrew Lee6324f702015-06-16 14:45:58 -0700987 mFloatingActionButtonController.scaleOut();
988
Andrew Lee752956e2014-05-15 11:43:38 -0700989 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700990 if (animate) {
991 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
992 } else {
993 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
994 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700995 if (fragment == null) {
996 if (smartDialSearch) {
997 fragment = new SmartDialSearchFragment();
998 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700999 fragment = new RegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001000 fragment.setOnTouchListener(new View.OnTouchListener() {
1001 @Override
1002 public boolean onTouch(View v, MotionEvent event) {
1003 // Show the FAB when the user touches the lists fragment and the soft
1004 // keyboard is hidden.
1005 showFabInSearchUi();
1006 return false;
1007 }
1008 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001009 }
Andrew Leeb1903842014-05-15 16:11:24 -07001010 transaction.add(R.id.dialtacts_frame, fragment, tag);
1011 } else {
1012 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001013 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001014 // DialtactsActivity will provide the options menu
1015 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001016 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001017 if (!smartDialSearch) {
1018 fragment.setQueryString(query, false /* delaySelection */);
1019 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001020 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001021
Yorke Lee6c450eb2015-05-08 09:45:47 -07001022 if (animate) {
1023 mListsFragment.getView().animate().alpha(0).withLayer();
1024 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001025 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001026 }
1027
1028 /**
Yorke Leec3766332013-07-31 11:13:16 -07001029 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001030 */
Yorke Leec3766332013-07-31 11:13:16 -07001031 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001032 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001033 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001034 return;
1035 }
Andrew Leeb1903842014-05-15 16:11:24 -07001036
Andrew Leeb1903842014-05-15 16:11:24 -07001037 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001038
1039 if (mDialpadFragment != null) {
1040 mDialpadFragment.clearDialpad();
1041 }
1042
Yorke Leeb207f8a2013-08-29 18:51:06 -07001043 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001044
Andrew Lee6324f702015-06-16 14:45:58 -07001045 // Restore the FAB for the lists fragment.
1046 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1047 mFloatingActionButtonController.setVisible(false);
1048 }
1049 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1050 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1051 onPageSelected(mListsFragment.getCurrentTabIndex());
1052
Andrew Leeb1903842014-05-15 16:11:24 -07001053 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001054 if (mSmartDialSearchFragment != null) {
1055 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001056 }
Andrew Leeb1903842014-05-15 16:11:24 -07001057 if (mRegularSearchFragment != null) {
1058 transaction.remove(mRegularSearchFragment);
1059 }
1060 transaction.commit();
1061
1062 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001063
1064 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001065 // If the dialpad fragment wasn't previously visible, then send a screen view because
1066 // we are exiting regular search. Otherwise, the screen view will be sent by
1067 // {@link #hideDialpadFragment}.
1068 mListsFragment.sendScreenViewForCurrentPosition();
1069 mListsFragment.setUserVisibleHint(true);
1070 }
1071
Yorke Lee5253be02014-05-21 18:50:03 -07001072 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001073 }
1074
Chiao Cheng94b10b52012-08-17 16:59:12 -07001075 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001076 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001077 if (mStateSaved) {
1078 return;
1079 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001080 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001081 if (TextUtils.isEmpty(mSearchQuery) ||
1082 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1083 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001084 exitSearchUi();
1085 }
Yorke Leef6673d32013-08-23 15:34:17 -07001086 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001087 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001088 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001089 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001090 } else {
1091 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001092 }
Yorke Leec3766332013-07-31 11:13:16 -07001093 }
1094
Yorke Lee81a313d2015-06-01 14:53:59 -07001095 private void maybeEnterSearchUi() {
1096 if (!isInSearchUi()) {
1097 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1098 }
1099 }
1100
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001101 /**
1102 * @return True if the search UI was exited, false otherwise
1103 */
1104 private boolean maybeExitSearchUi() {
1105 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1106 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001107 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001108 return true;
1109 }
1110 return false;
1111 }
1112
Andrew Lee6324f702015-06-16 14:45:58 -07001113 private void showFabInSearchUi() {
1114 mFloatingActionButtonController.changeIcon(
1115 getResources().getDrawable(R.drawable.fab_ic_dial),
1116 getResources().getString(R.string.action_menu_dialpad_button));
1117 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1118 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1119 }
1120
Yorke Leec3766332013-07-31 11:13:16 -07001121 @Override
1122 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001123 if (mSmartDialSearchFragment != null) {
1124 mSmartDialSearchFragment.setAddToContactNumber(query);
1125 }
Yorke Leec3766332013-07-31 11:13:16 -07001126 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1127 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001128
1129 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001130 if (DEBUG) {
1131 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1132 }
1133 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1134 // This callback can happen if the dialpad fragment is recreated because of
1135 // activity destruction. In that case, don't update the search view because
1136 // that would bring the user back to the search fragment regardless of the
1137 // previous state of the application. Instead, just return here and let the
1138 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001139 if (!TextUtils.isEmpty(normalizedQuery)) {
1140 mPendingSearchViewQuery = normalizedQuery;
1141 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001142 return;
1143 }
Yorke Lee53a22302014-04-29 18:13:46 -07001144 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001145 }
Santos Cordon83131712015-04-16 21:25:35 -07001146
1147 try {
1148 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1149 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1150 }
1151 } catch (Exception ignored) {
1152 // Skip any exceptions for this piece of code
1153 }
Yorke Lee575ae382015-07-16 11:36:07 -07001154 }
Santos Cordon83131712015-04-16 21:25:35 -07001155
Yorke Lee575ae382015-07-16 11:36:07 -07001156 @Override
1157 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1158 if (mInDialpadSearch && mSmartDialSearchFragment != null
1159 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1160 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1161 return true;
1162 }
1163 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001164 }
Yorke Leec3766332013-07-31 11:13:16 -07001165
1166 @Override
1167 public void onListFragmentScrollStateChange(int scrollState) {
1168 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001169 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001170 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001171 }
1172 }
1173
1174 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001175 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001176 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001177 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1178 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001179 }
1180
Yorke Leec3766332013-07-31 11:13:16 -07001181 private boolean phoneIsInUse() {
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001182 return getTelecomManager().isInCall();
Yorke Leec3766332013-07-31 11:13:16 -07001183 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001184
Yorke Leeda0f9042013-12-05 14:25:51 -08001185 private boolean canIntentBeHandled(Intent intent) {
1186 final PackageManager packageManager = getPackageManager();
1187 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1188 PackageManager.MATCH_DEFAULT_ONLY);
1189 return resolveInfo != null && resolveInfo.size() > 0;
1190 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001191
Yorke Lee86e21f72014-04-02 16:49:38 -07001192 /**
1193 * Called when the user has long-pressed a contact tile to start a drag operation.
1194 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001195 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001196 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001197 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001198 }
1199
1200 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001201 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1202 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001203
Yorke Lee86e21f72014-04-02 16:49:38 -07001204 /**
1205 * Called when the user has released a contact tile after long-pressing it.
1206 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001207 @Override
1208 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001209 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001210 }
1211
1212 @Override
1213 public void onDroppedOnRemove() {}
1214
1215 /**
Yorke Leed999b712014-04-23 15:10:58 -07001216 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001217 * once it has been attached to the activity.
1218 */
1219 @Override
1220 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001221 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001222 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001223 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001224
Yorke Lee38019af2015-07-14 17:05:38 -07001225 /**
1226 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1227 */
1228 @Override
1229 public void showAllContactsTab() {
1230 if (mListsFragment != null) {
1231 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1232 }
1233 }
1234
1235 /**
1236 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1237 */
1238 @Override
1239 public void showDialpad() {
1240 showDialpadFragment(true);
1241 }
1242
Yorke Leee00c9fe2014-04-12 12:42:06 -07001243 @Override
1244 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001245 // Specify call-origin so that users will see the previous tab instead of
1246 // CallLog screen (search UI will be automatically exited).
1247 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001248 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001249 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001250 }
1251
1252 @Override
1253 public void onCallNumberDirectly(String phoneNumber) {
Andrew Leefc1fed72014-07-02 11:05:53 -07001254 onCallNumberDirectly(phoneNumber, false /* isVideoCall */);
1255 }
1256
1257 @Override
1258 public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001259 if (phoneNumber == null) {
1260 // Invalid phone number, but let the call go through so that InCallUI can show
1261 // an error message.
1262 phoneNumber = "";
1263 }
Andrew Leed2a683f2014-07-08 15:54:50 -07001264 Intent intent = isVideoCall ?
Andrew Lee247df6e2015-05-12 19:09:00 -07001265 IntentUtil.getVideoCallIntent(phoneNumber, getCallOrigin()) :
1266 IntentUtil.getCallIntent(phoneNumber, getCallOrigin());
Yorke Lee7d20f822014-06-19 17:09:33 -07001267 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001268 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001269 }
1270
1271 @Override
1272 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001273 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001274 }
1275
1276 @Override
1277 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001278 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001279 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001280
Yorke Leecc4660d2014-04-24 11:22:57 -07001281 @Override
1282 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001283 int tabIndex = mListsFragment.getCurrentTabIndex();
1284
Andrew Lee4de59fb2015-08-13 15:20:08 -07001285 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1286 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1287 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001288 boolean isRtl = DialerUtils.isRtl();
1289 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1290 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001291 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001292 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1293 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001294 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001295 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001296 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001297
Andrew Lee432d4b52015-05-20 16:52:00 -07001298 @Override
1299 public void onPageSelected(int position) {
1300 int tabIndex = mListsFragment.getCurrentTabIndex();
1301 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001302 mFloatingActionButtonController.changeIcon(
1303 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1304 getResources().getString(R.string.search_shortcut_create_new_contact));
1305 } else {
1306 mFloatingActionButtonController.changeIcon(
1307 getResources().getDrawable(R.drawable.fab_ic_dial),
1308 getResources().getString(R.string.action_menu_dialpad_button));
1309 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001310 }
1311
1312 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001313 public void onPageScrollStateChanged(int state) {
1314 }
1315
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001316 private TelecomManager getTelecomManager() {
1317 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordond15629d2014-07-07 15:15:29 -07001318 }
1319
Yorke Lee5253be02014-05-21 18:50:03 -07001320 @Override
1321 public boolean isActionBarShowing() {
1322 return mActionBarController.isActionBarShowing();
1323 }
1324
Yorke Leec98a5bb2014-10-28 16:12:05 -07001325 @Override
1326 public ActionBarController getActionBarController() {
1327 return mActionBarController;
1328 }
1329
Yorke Lee6bc67162015-06-27 14:03:25 -07001330 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001331 public boolean isDialpadShown() {
1332 return mIsDialpadShown;
1333 }
1334
Yorke Lee5253be02014-05-21 18:50:03 -07001335 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001336 public int getDialpadHeight() {
1337 if (mDialpadFragment != null) {
1338 return mDialpadFragment.getDialpadHeight();
1339 }
1340 return 0;
1341 }
1342
1343 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001344 public int getActionBarHideOffset() {
1345 return getActionBar().getHideOffset();
1346 }
1347
1348 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001349 public void setActionBarHideOffset(int offset) {
1350 getActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001351 }
1352
1353 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001354 public int getActionBarHeight() {
1355 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001356 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001357
Andrew Lee6324f702015-06-16 14:45:58 -07001358 private int getFabAlignment() {
1359 if (!mIsLandscape && !isInSearchUi() &&
1360 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1361 return FloatingActionButtonController.ALIGN_MIDDLE;
1362 }
1363 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001364 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001365}