blob: 31d1b94bf5d175b85895077a572bfcabab32629d [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;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080058import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070059
Brian Attwellc311af12014-10-22 18:28:58 -070060import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070061import com.android.contacts.common.dialog.ClearFrequentsDialog;
62import com.android.contacts.common.interactions.ImportExportDialogFragment;
Nancy Chen0d3a7392014-08-12 14:44:53 -070063import com.android.contacts.common.interactions.TouchPointManager;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080064import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec16ea5a2015-05-19 15:51:01 -070065import com.android.contacts.common.util.PermissionsUtil;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -070066import com.android.contacts.common.widget.FloatingActionButtonController;
Brian Attwellbeab3bb2014-10-27 12:24:49 -070067import com.android.contacts.commonbind.analytics.AnalyticsUtil;
Yorke Leec3766332013-07-31 11:13:16 -070068import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070069import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070070import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070072import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070073import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080074import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070075import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080076import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070077import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070078import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070079import com.android.dialer.list.RegularSearchFragment;
80import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070081import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070082import com.android.dialer.list.SpeedDialFragment;
Yorke Leeaf6f1952014-07-14 19:13:16 -070083import com.android.dialer.settings.DialerSettingsActivity;
Andrew Lee247df6e2015-05-12 19:09:00 -070084import com.android.dialer.util.IntentUtil;
Yorke Lee7d20f822014-06-19 17:09:33 -070085import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070086import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070087import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070088import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070089import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee2fec47b2014-08-05 18:16:27 -070090import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070091import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070092
Yorke Leed5c512a2015-01-30 14:42:53 -080093import junit.framework.Assert;
94
Yorke Leec3766332013-07-31 11:13:16 -070095import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080096import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070097
Chiao Cheng94b10b52012-08-17 16:59:12 -070098/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070099 * The dialer tab's title is 'phone', a more common name (see strings.xml).
100 */
Yorke Leec3766332013-07-31 11:13:16 -0700101public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700102 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700103 OnListFragmentScrolledListener,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700104 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700105 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700106 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700107 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700108 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700109 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700110 ViewPager.OnPageChangeListener,
111 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700112 private static final String TAG = "DialtactsActivity";
113
Yorke Lee7cc61492015-06-01 14:59:33 -0700114 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115
Yorke Leef74011e2013-08-02 11:30:30 -0700116 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
117
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118 /** @see #getCallOrigin() */
119 private static final String CALL_ORIGIN_DIALTACTS =
120 "com.android.dialer.DialtactsActivity";
121
Yorke Leeb207f8a2013-08-29 18:51:06 -0700122 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
123 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700124 private static final String KEY_SEARCH_QUERY = "search_query";
125 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700126 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700127
Yorke Leec3766332013-07-31 11:13:16 -0700128 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
129 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
130 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
131 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700132
Chiao Cheng94b10b52012-08-17 16:59:12 -0700133 /**
134 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
135 */
136 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700137 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700138
Yorke Leec3766332013-07-31 11:13:16 -0700139 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700140
Andrew Lee6324f702015-06-16 14:45:58 -0700141 private static final int FAB_SCALE_IN_DELAY_MS = 300;
142
Yorke Leef614f6f2014-10-03 10:44:19 -0700143 private FrameLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700144
Yorke Leec3766332013-07-31 11:13:16 -0700145 /**
Yorke Leec3766332013-07-31 11:13:16 -0700146 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700147 */
Evan Charlton72d66252014-11-07 16:23:47 -0800148 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700149
150 /**
151 * Fragment for searching phone numbers using the alphanumeric keyboard.
152 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700153 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700154
155 /**
156 * Fragment for searching phone numbers using the dialpad.
157 */
158 private SmartDialSearchFragment mSmartDialSearchFragment;
159
Yorke Leee00c9fe2014-04-12 12:42:06 -0700160 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700161 * Animation that slides in.
162 */
163 private Animation mSlideIn;
164
165 /**
166 * Animation that slides out.
167 */
168 private Animation mSlideOut;
169
Yorke Lee6c450eb2015-05-08 09:45:47 -0700170 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
171 @Override
172 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700173 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700174 }
175 };
176
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700177 /**
178 * Listener for after slide out animation completes on dialer fragment.
179 */
180 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
181 @Override
182 public void onAnimationEnd(Animation animation) {
183 commitDialpadFragmentHide();
184 }
185 };
186
187 /**
Yorke Leee00c9fe2014-04-12 12:42:06 -0700188 * Fragment containing the speed dial list, recents list, and all contacts list.
189 */
190 private ListsFragment mListsFragment;
191
Yorke Lee19e68ca2014-10-28 11:51:40 -0700192 /**
193 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
194 * be commited.
195 */
196 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800197 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700198 private boolean mInDialpadSearch;
199 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700200 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700201 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700202 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700203
Yorke Leeef2b7382013-08-09 17:39:25 -0700204 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700205 * Whether or not the device is in landscape orientation.
206 */
207 private boolean mIsLandscape;
208
209 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700210 * True if the dialpad is only temporarily showing due to being in call
211 */
212 private boolean mInCallDialpadUp;
213
214 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700215 * True when this activity has been launched for the first time.
216 */
Yorke Lee98702de2013-08-06 12:03:32 -0700217 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700218
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700219 /**
220 * Search query to be applied to the SearchView in the ActionBar once
221 * onCreateOptionsMenu has been called.
222 */
223 private String mPendingSearchViewQuery;
224
Yorke Lee74edcdc2014-07-11 16:15:08 -0700225 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700226 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700227 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700228
Yorke Leeef2b7382013-08-09 17:39:25 -0700229 private String mSearchQuery;
230
Yorke Leefce269a2013-08-12 11:56:04 -0700231 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700232 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700233 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700234
Sai Cheemalapati41460652014-06-02 21:05:39 -0700235 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700236
Andrew Lee2423a2f2014-05-29 14:14:45 -0700237 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700238
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800239 /**
240 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
241 * {@link #onResume()} to populate the search box.
242 */
243 private String mVoiceSearchQuery;
244
Andrew Lee467de3d2015-03-17 15:47:24 -0700245 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700246 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700247 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700248 }
249
250 @Override
251 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700252 final boolean hasContactsPermission =
253 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700254 final Menu menu = getMenu();
255 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700256 clearFrequents.setVisible(mListsFragment != null &&
257 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700258 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
259
260 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
261 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700262
263 menu.findItem(R.id.menu_history).setVisible(
264 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700265 super.show();
266 }
267 }
268
Chiao Cheng94b10b52012-08-17 16:59:12 -0700269 /**
Yorke Lee28266192014-05-01 10:05:52 -0700270 * Listener that listens to drag events and sends their x and y coordinates to a
271 * {@link DragDropController}.
272 */
273 private class LayoutOnDragListener implements OnDragListener {
274 @Override
275 public boolean onDrag(View v, DragEvent event) {
276 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700277 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700278 }
279 return true;
280 }
281 }
282
283 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700284 * Listener used to send search queries to the phone search fragment.
285 */
Yorke Lee53a22302014-04-29 18:13:46 -0700286 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700287 @Override
288 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
289 }
Yorke Leec3766332013-07-31 11:13:16 -0700290
Andrew Lee99a570c2014-05-15 14:18:50 -0700291 @Override
292 public void onTextChanged(CharSequence s, int start, int before, int count) {
293 final String newText = s.toString();
294 if (newText.equals(mSearchQuery)) {
295 // If the query hasn't changed (perhaps due to activity being destroyed
296 // and restored, or user launching the same DIAL intent twice), then there is
297 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700298 return;
299 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700300 if (DEBUG) {
301 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700302 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700303 }
Yorke Lee710709d2014-05-29 07:18:42 -0700304 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700305
Andrew Lee90374a72014-05-16 15:01:09 -0700306 // Show search fragment only when the query string is changed to non-empty text.
307 if (!TextUtils.isEmpty(newText)) {
308 // Call enterSearchUi only if we are switching search modes, or showing a search
309 // fragment for the first time.
310 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
311 (!mIsDialpadShown && mInRegularSearch);
312 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700313 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700314 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700315 }
316
Andrew Leea8515422014-06-13 16:53:54 -0700317 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700318 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700319 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700320 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700321 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700322 }
323
324 @Override
325 public void afterTextChanged(Editable s) {
326 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700327 };
328
Andrew Leeb1903842014-05-15 16:11:24 -0700329
330 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700331 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700332 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700333 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700334 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700335 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700336 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700337 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700338 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
339 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700340 }
Andrew Leeb1903842014-05-15 16:11:24 -0700341 }
342 };
343
344 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700345 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700346 */
347 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
348 @Override
349 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700350 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
351 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
352 // If the search term is empty, close the search UI.
353 maybeExitSearchUi();
354 } else {
355 // If the search term is not empty, show the dialpad fab.
356 showFabInSearchUi();
357 }
Andrew Leeb1903842014-05-15 16:11:24 -0700358 }
359 return false;
360 }
361 };
362
Chiao Cheng94b10b52012-08-17 16:59:12 -0700363 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700364 public boolean dispatchTouchEvent(MotionEvent ev) {
365 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
366 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
367 }
368 return super.dispatchTouchEvent(ev);
369
370 }
371
372 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700373 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800374 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700375 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700376
Yorke Lee98702de2013-08-06 12:03:32 -0700377 mFirstLaunch = true;
378
Andrew Lee2423a2f2014-05-29 14:14:45 -0700379 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700380 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700381
Yorke Leef5554ce2015-01-27 14:46:02 -0800382 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700383 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800384 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800385 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700386
Yorke Leef5554ce2015-01-27 14:46:02 -0800387 Trace.beginSection(TAG + " setup Views");
Yorke Lee53a22302014-04-29 18:13:46 -0700388 final ActionBar actionBar = getActionBar();
389 actionBar.setCustomView(R.layout.search_edittext);
390 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700391 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700392
Andrew Lee04675a52014-06-05 18:36:20 -0700393 SearchEditTextLayout searchEditTextLayout =
Andrew Leee3f59a82015-02-26 12:06:35 -0800394 (SearchEditTextLayout) actionBar.getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700395 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
396
Andrew Leee3f59a82015-02-26 12:06:35 -0800397 mActionBarController = new ActionBarController(this, searchEditTextLayout);
398
Andrew Lee04675a52014-06-05 18:36:20 -0700399 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700400 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700401 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
402 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
403 .setOnClickListener(mSearchViewOnClickListener);
404 searchEditTextLayout.findViewById(R.id.search_box_start_search)
405 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700406 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700407 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700408 @Override
409 public void onBackButtonClicked() {
410 onBackPressed();
411 }
Andrew Lee6324f702015-06-16 14:45:58 -0700412
413 @Override
414 public void onSearchViewClicked() {
415 // Hide FAB, as the keyboard is shown.
416 mFloatingActionButtonController.scaleOut();
417 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700418 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700419
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700420 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700421 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700422
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700423 final View floatingActionButtonContainer = findViewById(
424 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700425 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
426 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700427 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700428 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700429
Andrew Lee04675a52014-06-05 18:36:20 -0700430 ImageButton optionsMenuButton =
431 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700432 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700433 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
434 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700435
Yorke Leed5c512a2015-01-30 14:42:53 -0800436 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
437 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700438 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800439 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000440 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800441 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700442 } else {
443 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700444 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
445 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700446 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700447 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700448 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700449 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700450
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700451 final boolean isLayoutRtl = DialerUtils.isRtl();
452 if (mIsLandscape) {
453 mSlideIn = AnimationUtils.loadAnimation(this,
454 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
455 mSlideOut = AnimationUtils.loadAnimation(this,
456 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
457 } else {
458 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
459 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
460 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700461
Yorke Lee2fec47b2014-08-05 18:16:27 -0700462 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
463 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
464
Yorke Lee6c450eb2015-05-08 09:45:47 -0700465 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700466 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700467
Yorke Leef614f6f2014-10-03 10:44:19 -0700468 mParentLayout = (FrameLayout) findViewById(R.id.dialtacts_mainlayout);
469 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700470 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700471 new ViewTreeObserver.OnGlobalLayoutListener() {
472 @Override
473 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700474 final ViewTreeObserver observer =
475 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700476 if (!observer.isAlive()) {
477 return;
478 }
479 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700480 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700481 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700482 mFloatingActionButtonController.align(
483 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700484 }
485 });
Andrew Lee0c667702014-05-12 14:31:45 -0700486
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700487 setupActivityOverlay();
488
Yorke Leef5554ce2015-01-27 14:46:02 -0800489 Trace.endSection();
490
491 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700492 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700493 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800494 Trace.endSection();
495 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700496 }
497
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700498 private void setupActivityOverlay() {
499 final View activityOverlay = findViewById(R.id.activity_overlay);
500 activityOverlay.setOnTouchListener(new OnTouchListener() {
501 @Override
502 public boolean onTouch(View v, MotionEvent event) {
503 if (!mIsDialpadShown) {
504 maybeExitSearchUi();
505 }
506 return false;
507 }
508 });
509 }
510
Chiao Cheng94b10b52012-08-17 16:59:12 -0700511 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700512 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800513 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700514 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700515
Yorke Lee19e68ca2014-10-28 11:51:40 -0700516 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700517 if (mFirstLaunch) {
518 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700519 } else if (!phoneIsInUse() && mInCallDialpadUp) {
520 hideDialpadFragment(false, true);
521 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700522 } else if (mShowDialpadOnResume) {
523 showDialpadFragment(false);
524 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700525 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800526
527 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
528 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
529 // shown until onResume has completed. Active the search UI and set the search term now.
530 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
531 mActionBarController.onSearchBoxTapped();
532 mSearchView.setText(mVoiceSearchQuery);
533 mVoiceSearchQuery = null;
534 }
535
Yorke Lee98702de2013-08-06 12:03:32 -0700536 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800537
538 if (mIsRestarting) {
539 // This is only called when the activity goes from resumed -> paused -> resumed, so it
540 // will not cause an extra view to be sent out on rotation
541 if (mIsDialpadShown) {
542 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
543 }
544 mIsRestarting = false;
545 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700546
Yorke Lee53a22302014-04-29 18:13:46 -0700547 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700548 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700549 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700550
551 if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
552 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
553 if (index < mListsFragment.getTabCount()) {
554 mListsFragment.showTab(index);
555 }
Nancy Chen019713e2015-07-06 18:25:24 -0700556 } else if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
557 mListsFragment.showTab(ListsFragment.TAB_INDEX_RECENTS);
Nancy Chend5de03b2015-07-01 09:20:45 -0700558 }
559
Yorke Leef5554ce2015-01-27 14:46:02 -0800560 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561 }
562
563 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800564 protected void onRestart() {
565 super.onRestart();
566 mIsRestarting = true;
567 }
568
569 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700570 protected void onPause() {
571 if (mClearSearchOnPause) {
572 hideDialpadAndSearchUi();
573 mClearSearchOnPause = false;
574 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700575 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
576 commitDialpadFragmentHide();
577 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700578 super.onPause();
579 }
580
581 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700582 protected void onSaveInstanceState(Bundle outState) {
583 super.onSaveInstanceState(outState);
584 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700585 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
586 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700587 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700588 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700589 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700590 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700591 }
592
593 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700594 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700595 if (fragment instanceof DialpadFragment) {
596 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800597 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700598 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
599 transaction.hide(mDialpadFragment);
600 transaction.commit();
601 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700602 } else if (fragment instanceof SmartDialSearchFragment) {
603 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700604 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700605 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700606 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700607 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700608 } else if (fragment instanceof ListsFragment) {
609 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700610 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700611 }
Yorke Leec3766332013-07-31 11:13:16 -0700612 }
613
Alon Albertb453e5b2013-09-10 15:54:23 -0700614 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700615 final Intent intent = new Intent(this, DialerSettingsActivity.class);
616 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700617 }
618
Chiao Cheng94b10b52012-08-17 16:59:12 -0700619 @Override
620 public void onClick(View view) {
621 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700622 case R.id.floating_action_button:
Andrew Lee432d4b52015-05-20 16:52:00 -0700623 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700624 DialerUtils.startActivityWithErrorToast(
625 this,
626 IntentUtil.getNewContactIntent(),
627 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700628 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700629 mInCallDialpadUp = false;
630 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700631 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700632 break;
Yorke Leec3766332013-07-31 11:13:16 -0700633 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800634 try {
635 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
636 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
637 } catch (ActivityNotFoundException e) {
638 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
639 Toast.LENGTH_SHORT).show();
640 }
Yorke Leec3766332013-07-31 11:13:16 -0700641 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700642 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700643 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700644 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700645 default: {
646 Log.wtf(TAG, "Unexpected onClick event from " + view);
647 break;
648 }
649 }
650 }
651
Yorke Leec3766332013-07-31 11:13:16 -0700652 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700653 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700654 switch (item.getItemId()) {
655 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700656 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
657 // CONTENT_TYPE, so that we always open our call log from our dialer
658 final Intent intent = new Intent(this, CallLogActivity.class);
659 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700660 break;
661 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700662 DialerUtils.startActivityWithErrorToast(
663 this,
664 IntentUtil.getNewContactIntent(),
665 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700666 break;
667 case R.id.menu_import_export:
668 // We hard-code the "contactsAreAvailable" argument because doing it properly would
669 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
670 // now in Dialtacts for (potential) performance reasons. Compare with how it is
671 // done in {@link PeopleActivity}.
672 ImportExportDialogFragment.show(getFragmentManager(), true,
673 DialtactsActivity.class);
674 return true;
675 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700676 ClearFrequentsDialog.show(getFragmentManager());
677 return true;
678 case R.id.menu_call_settings:
679 handleMenuSettings();
680 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700681 }
682 return false;
683 }
684
685 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700686 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
687 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
688 if (resultCode == RESULT_OK) {
689 final ArrayList<String> matches = data.getStringArrayListExtra(
690 RecognizerIntent.EXTRA_RESULTS);
691 if (matches.size() > 0) {
692 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800693 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700694 } else {
695 Log.e(TAG, "Voice search - nothing heard");
696 }
697 } else {
698 Log.e(TAG, "Voice search failed");
699 }
700 }
701 super.onActivityResult(requestCode, resultCode, data);
702 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700703
Andrew Lee2a58ab82014-05-15 02:16:04 -0700704 /**
705 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
706 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
707 * @see #onDialpadShown
708 */
Yorke Leec3766332013-07-31 11:13:16 -0700709 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700710 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700711 return;
712 }
713 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800714
Yorke Lee7ee5c422014-11-04 10:29:32 -0800715 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700716
Chiao Cheng94b10b52012-08-17 16:59:12 -0700717 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800718 if (mDialpadFragment == null) {
719 mDialpadFragment = new DialpadFragment();
720 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
721 } else {
722 ft.show(mDialpadFragment);
723 }
724
725 mDialpadFragment.setAnimate(animate);
726 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700727 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700728
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700729 if (animate) {
730 mFloatingActionButtonController.scaleOut();
731 } else {
732 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700733 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700734 }
Yorke Lee5253be02014-05-21 18:50:03 -0700735 mActionBarController.onDialpadUp();
736
Yorke Lee6c450eb2015-05-08 09:45:47 -0700737 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700738 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700739
Andrew Lee2a58ab82014-05-15 02:16:04 -0700740 /**
741 * Callback from child DialpadFragment when the dialpad is shown.
742 */
743 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800744 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700745 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700746 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700747 } else {
748 mDialpadFragment.setYFraction(0);
749 }
750
Andrew Lee2a58ab82014-05-15 02:16:04 -0700751 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700752 }
753
754 /**
755 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
756 * a callback after the hide animation ends.
757 * @see #commitDialpadFragmentHide
758 */
Yorke Leeca195042013-09-25 16:29:38 -0700759 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700760 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700761 return;
762 }
Yorke Leef6673d32013-08-23 15:34:17 -0700763 if (clearDialpad) {
764 mDialpadFragment.clearDialpad();
765 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700766 if (!mIsDialpadShown) {
767 return;
Yorke Leec3766332013-07-31 11:13:16 -0700768 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700769 mIsDialpadShown = false;
770 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800771 mListsFragment.setUserVisibleHint(true);
772 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700773
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700774 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700775
Andrew Lee6324f702015-06-16 14:45:58 -0700776 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700777 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700778 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700779 } else {
780 commitDialpadFragmentHide();
781 }
782
Yorke Lee5253be02014-05-21 18:50:03 -0700783 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700784
Andrew Leed4cde832014-05-16 15:56:48 -0700785 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700786 if (TextUtils.isEmpty(mSearchQuery)) {
787 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700788 }
Andrew Leed4cde832014-05-16 15:56:48 -0700789 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700790 }
791
792 /**
793 * Finishes hiding the dialpad fragment after any animations are completed.
794 */
795 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800796 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700797 final FragmentTransaction ft = getFragmentManager().beginTransaction();
798 ft.hide(mDialpadFragment);
799 ft.commit();
800 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700801 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700802 }
803
Andrew Lee2a58ab82014-05-15 02:16:04 -0700804 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700805 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700806 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700807 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700808 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700809 fragment = mRegularSearchFragment;
810 }
811 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700812 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700813 }
814 }
815
Yorke Lee5253be02014-05-21 18:50:03 -0700816 @Override
817 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700818 return mInDialpadSearch || mInRegularSearch;
819 }
820
Yorke Lee5253be02014-05-21 18:50:03 -0700821 @Override
822 public boolean hasSearchQuery() {
823 return !TextUtils.isEmpty(mSearchQuery);
824 }
825
826 @Override
827 public boolean shouldShowActionBar() {
828 return mListsFragment.shouldShowActionBar();
829 }
830
Yorke Leeb207f8a2013-08-29 18:51:06 -0700831 private void setNotInSearchUi() {
832 mInDialpadSearch = false;
833 mInRegularSearch = false;
834 }
835
Yorke Lee311969c2013-08-26 06:31:11 -0700836 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700837 if (mIsDialpadShown) {
838 hideDialpadFragment(false, true);
839 } else {
840 exitSearchUi();
841 }
Yorke Lee311969c2013-08-26 06:31:11 -0700842 }
843
Yorke Lee53a22302014-04-29 18:13:46 -0700844 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700845 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
846 if (canIntentBeHandled(voiceIntent)) {
847 mVoiceSearchButton.setVisibility(View.VISIBLE);
848 mVoiceSearchButton.setOnClickListener(this);
849 } else {
850 mVoiceSearchButton.setVisibility(View.GONE);
851 }
852 }
853
Andrew Lee467de3d2015-03-17 15:47:24 -0700854 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700855 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
856 popupMenu.inflate(R.menu.dialtacts_options);
857 popupMenu.setOnMenuItemClickListener(this);
858 return popupMenu;
859 }
860
Yorke Lee86e21f72014-04-02 16:49:38 -0700861 @Override
862 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700863 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700864 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700865 mPendingSearchViewQuery = null;
866 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700867 if (mActionBarController != null) {
868 mActionBarController.restoreActionBarOffset();
869 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700870 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700871 }
872
Chiao Cheng94b10b52012-08-17 16:59:12 -0700873 /**
874 * Returns true if the intent is due to hitting the green send key (hardware call button:
875 * KEYCODE_CALL) while in a call.
876 *
877 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700878 * @return true if the intent is due to hitting the green send key while in a call
879 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700880 private boolean isSendKeyWhileInCall(Intent intent) {
881 // If there is a call in progress and the user launched the dialer by hitting the call
882 // button, go straight to the in-call screen.
883 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700884
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700885 if (callKey) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700886 getTelecomManager().showInCallScreen(false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700887 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700888 }
889
890 return false;
891 }
892
893 /**
894 * Sets the current tab based on the intent's request type
895 *
896 * @param intent Intent that contains information about which tab should be selected
897 */
Yorke Leec3766332013-07-31 11:13:16 -0700898 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700899 // 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 -0700900 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700901 finish();
902 return;
903 }
904
Yorke Leed5c512a2015-01-30 14:42:53 -0800905 final boolean phoneIsInUse = phoneIsInUse();
906 if (phoneIsInUse || (intent.getData() != null && isDialIntent(intent))) {
907 showDialpadFragment(false);
908 mDialpadFragment.setStartedFromNewIntent(true);
909 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
910 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700911 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700912 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700913 }
914
915 @Override
916 public void onNewIntent(Intent newIntent) {
917 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700918
Nancy Chen199b7f02014-11-05 15:03:00 -0800919 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700920 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700921
Chiao Cheng94b10b52012-08-17 16:59:12 -0700922 invalidateOptionsMenu();
923 }
924
925 /** Returns true if the given intent contains a phone number to populate the dialer with */
926 private boolean isDialIntent(Intent intent) {
927 final String action = intent.getAction();
928 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
929 return true;
930 }
931 if (Intent.ACTION_VIEW.equals(action)) {
932 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700933 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700934 return true;
935 }
936 }
937 return false;
938 }
939
940 /**
941 * Returns an appropriate call origin for this Activity. May return null when no call origin
942 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
943 * for remembering the tab in which the user made a phone call, so the external app's DIAL
944 * request should not be counted.)
945 */
946 public String getCallOrigin() {
947 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
948 }
949
Chiao Cheng94b10b52012-08-17 16:59:12 -0700950 /**
Yorke Leec3766332013-07-31 11:13:16 -0700951 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700952 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700953 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700954 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700955 // Weird race condition where fragment is doing work after the activity is destroyed
956 // due to talkback being on (b/10209937). Just return since we can't do any
957 // constructive here.
958 return;
959 }
960
Yorke Leeef2b7382013-08-09 17:39:25 -0700961 if (DEBUG) {
962 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
963 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700964
Yorke Leec3766332013-07-31 11:13:16 -0700965 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700966 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700967 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700968 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700969 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700970 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700971
Yorke Leeb207f8a2013-08-29 18:51:06 -0700972 final String tag;
973 if (smartDialSearch) {
974 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
975 } else {
976 tag = TAG_REGULAR_SEARCH_FRAGMENT;
977 }
978 mInDialpadSearch = smartDialSearch;
979 mInRegularSearch = !smartDialSearch;
980
Andrew Lee6324f702015-06-16 14:45:58 -0700981 mFloatingActionButtonController.scaleOut();
982
Andrew Lee752956e2014-05-15 11:43:38 -0700983 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700984 if (animate) {
985 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
986 } else {
987 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
988 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700989 if (fragment == null) {
990 if (smartDialSearch) {
991 fragment = new SmartDialSearchFragment();
992 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700993 fragment = new RegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -0700994 fragment.setOnTouchListener(new View.OnTouchListener() {
995 @Override
996 public boolean onTouch(View v, MotionEvent event) {
997 // Show the FAB when the user touches the lists fragment and the soft
998 // keyboard is hidden.
999 showFabInSearchUi();
1000 return false;
1001 }
1002 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001003 }
Andrew Leeb1903842014-05-15 16:11:24 -07001004 transaction.add(R.id.dialtacts_frame, fragment, tag);
1005 } else {
1006 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001007 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001008 // DialtactsActivity will provide the options menu
1009 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001010 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001011 if (!smartDialSearch) {
1012 fragment.setQueryString(query, false /* delaySelection */);
1013 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001014 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001015
Yorke Lee6c450eb2015-05-08 09:45:47 -07001016 if (animate) {
1017 mListsFragment.getView().animate().alpha(0).withLayer();
1018 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001019 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001020 }
1021
1022 /**
Yorke Leec3766332013-07-31 11:13:16 -07001023 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001024 */
Yorke Leec3766332013-07-31 11:13:16 -07001025 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001026 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001027 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001028 return;
1029 }
Andrew Leeb1903842014-05-15 16:11:24 -07001030
Andrew Leeb1903842014-05-15 16:11:24 -07001031 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001032
1033 if (mDialpadFragment != null) {
1034 mDialpadFragment.clearDialpad();
1035 }
1036
Yorke Leeb207f8a2013-08-29 18:51:06 -07001037 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001038
Andrew Lee6324f702015-06-16 14:45:58 -07001039 // Restore the FAB for the lists fragment.
1040 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1041 mFloatingActionButtonController.setVisible(false);
1042 }
1043 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1044 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1045 onPageSelected(mListsFragment.getCurrentTabIndex());
1046
Andrew Leeb1903842014-05-15 16:11:24 -07001047 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001048 if (mSmartDialSearchFragment != null) {
1049 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001050 }
Andrew Leeb1903842014-05-15 16:11:24 -07001051 if (mRegularSearchFragment != null) {
1052 transaction.remove(mRegularSearchFragment);
1053 }
1054 transaction.commit();
1055
1056 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001057
1058 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001059 // If the dialpad fragment wasn't previously visible, then send a screen view because
1060 // we are exiting regular search. Otherwise, the screen view will be sent by
1061 // {@link #hideDialpadFragment}.
1062 mListsFragment.sendScreenViewForCurrentPosition();
1063 mListsFragment.setUserVisibleHint(true);
1064 }
1065
Yorke Lee5253be02014-05-21 18:50:03 -07001066 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001067 }
1068
Chiao Cheng94b10b52012-08-17 16:59:12 -07001069 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001070 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001071 if (mStateSaved) {
1072 return;
1073 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001074 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001075 if (TextUtils.isEmpty(mSearchQuery) ||
1076 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1077 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001078 exitSearchUi();
1079 }
Yorke Leef6673d32013-08-23 15:34:17 -07001080 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001081 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001082 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001083 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001084 } else {
1085 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001086 }
Yorke Leec3766332013-07-31 11:13:16 -07001087 }
1088
Yorke Lee81a313d2015-06-01 14:53:59 -07001089 private void maybeEnterSearchUi() {
1090 if (!isInSearchUi()) {
1091 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1092 }
1093 }
1094
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001095 /**
1096 * @return True if the search UI was exited, false otherwise
1097 */
1098 private boolean maybeExitSearchUi() {
1099 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1100 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001101 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001102 return true;
1103 }
1104 return false;
1105 }
1106
Andrew Lee6324f702015-06-16 14:45:58 -07001107 private void showFabInSearchUi() {
1108 mFloatingActionButtonController.changeIcon(
1109 getResources().getDrawable(R.drawable.fab_ic_dial),
1110 getResources().getString(R.string.action_menu_dialpad_button));
1111 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1112 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1113 }
1114
Yorke Leec3766332013-07-31 11:13:16 -07001115 @Override
1116 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001117 if (mSmartDialSearchFragment != null) {
1118 mSmartDialSearchFragment.setAddToContactNumber(query);
1119 }
Yorke Leec3766332013-07-31 11:13:16 -07001120 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1121 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001122
1123 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001124 if (DEBUG) {
1125 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1126 }
1127 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1128 // This callback can happen if the dialpad fragment is recreated because of
1129 // activity destruction. In that case, don't update the search view because
1130 // that would bring the user back to the search fragment regardless of the
1131 // previous state of the application. Instead, just return here and let the
1132 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001133 if (!TextUtils.isEmpty(normalizedQuery)) {
1134 mPendingSearchViewQuery = normalizedQuery;
1135 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001136 return;
1137 }
Yorke Lee53a22302014-04-29 18:13:46 -07001138 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001139 }
Santos Cordon83131712015-04-16 21:25:35 -07001140
1141 try {
1142 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1143 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1144 }
1145 } catch (Exception ignored) {
1146 // Skip any exceptions for this piece of code
1147 }
1148
Chiao Cheng94b10b52012-08-17 16:59:12 -07001149 }
Yorke Leec3766332013-07-31 11:13:16 -07001150
1151 @Override
1152 public void onListFragmentScrollStateChange(int scrollState) {
1153 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001154 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001155 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001156 }
1157 }
1158
1159 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001160 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001161 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001162 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1163 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001164 }
1165
Yorke Leec3766332013-07-31 11:13:16 -07001166 private boolean phoneIsInUse() {
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001167 return getTelecomManager().isInCall();
Yorke Leec3766332013-07-31 11:13:16 -07001168 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001169
Yorke Leeda0f9042013-12-05 14:25:51 -08001170 private boolean canIntentBeHandled(Intent intent) {
1171 final PackageManager packageManager = getPackageManager();
1172 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1173 PackageManager.MATCH_DEFAULT_ONLY);
1174 return resolveInfo != null && resolveInfo.size() > 0;
1175 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001176
Yorke Lee86e21f72014-04-02 16:49:38 -07001177 /**
1178 * Called when the user has long-pressed a contact tile to start a drag operation.
1179 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001180 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001181 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001182 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001183 }
1184
1185 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001186 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1187 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001188
Yorke Lee86e21f72014-04-02 16:49:38 -07001189 /**
1190 * Called when the user has released a contact tile after long-pressing it.
1191 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001192 @Override
1193 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001194 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001195 }
1196
1197 @Override
1198 public void onDroppedOnRemove() {}
1199
1200 /**
Yorke Leed999b712014-04-23 15:10:58 -07001201 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001202 * once it has been attached to the activity.
1203 */
1204 @Override
1205 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001206 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001207 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001208 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001209
1210 @Override
1211 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001212 // Specify call-origin so that users will see the previous tab instead of
1213 // CallLog screen (search UI will be automatically exited).
1214 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001215 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001216 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001217 }
1218
1219 @Override
1220 public void onCallNumberDirectly(String phoneNumber) {
Andrew Leefc1fed72014-07-02 11:05:53 -07001221 onCallNumberDirectly(phoneNumber, false /* isVideoCall */);
1222 }
1223
1224 @Override
1225 public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001226 if (phoneNumber == null) {
1227 // Invalid phone number, but let the call go through so that InCallUI can show
1228 // an error message.
1229 phoneNumber = "";
1230 }
Andrew Leed2a683f2014-07-08 15:54:50 -07001231 Intent intent = isVideoCall ?
Andrew Lee247df6e2015-05-12 19:09:00 -07001232 IntentUtil.getVideoCallIntent(phoneNumber, getCallOrigin()) :
1233 IntentUtil.getCallIntent(phoneNumber, getCallOrigin());
Yorke Lee7d20f822014-06-19 17:09:33 -07001234 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001235 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001236 }
1237
1238 @Override
1239 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001240 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001241 }
1242
1243 @Override
1244 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001245 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001246 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001247
Yorke Leecc4660d2014-04-24 11:22:57 -07001248 @Override
1249 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001250 int tabIndex = mListsFragment.getCurrentTabIndex();
1251
1252 // Scroll the button from center to end when moving from the Speed Dial to Recents tab.
1253 // In RTL, scroll when the current tab is Recents instead of Speed Dial, because the order
1254 // of the tabs is reversed and the ViewPager returns the left tab position during scroll.
1255 boolean isRtl = DialerUtils.isRtl();
1256 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1257 mFloatingActionButtonController.onPageScrolled(positionOffset);
1258 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_RECENTS && !mIsLandscape) {
1259 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1260 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001261 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001262 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001263 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001264
Andrew Lee432d4b52015-05-20 16:52:00 -07001265 @Override
1266 public void onPageSelected(int position) {
1267 int tabIndex = mListsFragment.getCurrentTabIndex();
1268 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001269 mFloatingActionButtonController.changeIcon(
1270 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1271 getResources().getString(R.string.search_shortcut_create_new_contact));
1272 } else {
1273 mFloatingActionButtonController.changeIcon(
1274 getResources().getDrawable(R.drawable.fab_ic_dial),
1275 getResources().getString(R.string.action_menu_dialpad_button));
1276 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001277 }
1278
1279 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001280 public void onPageScrollStateChanged(int state) {
1281 }
1282
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001283 private TelecomManager getTelecomManager() {
1284 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordond15629d2014-07-07 15:15:29 -07001285 }
1286
Yorke Lee5253be02014-05-21 18:50:03 -07001287 @Override
1288 public boolean isActionBarShowing() {
1289 return mActionBarController.isActionBarShowing();
1290 }
1291
Yorke Leec98a5bb2014-10-28 16:12:05 -07001292 @Override
1293 public ActionBarController getActionBarController() {
1294 return mActionBarController;
1295 }
1296
Yorke Lee6bc67162015-06-27 14:03:25 -07001297 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001298 public boolean isDialpadShown() {
1299 return mIsDialpadShown;
1300 }
1301
Yorke Lee5253be02014-05-21 18:50:03 -07001302 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001303 public int getDialpadHeight() {
1304 if (mDialpadFragment != null) {
1305 return mDialpadFragment.getDialpadHeight();
1306 }
1307 return 0;
1308 }
1309
1310 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001311 public int getActionBarHideOffset() {
1312 return getActionBar().getHideOffset();
1313 }
1314
1315 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001316 public void setActionBarHideOffset(int offset) {
1317 getActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001318 }
1319
1320 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001321 public int getActionBarHeight() {
1322 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001323 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001324
Andrew Lee6324f702015-06-16 14:45:58 -07001325
1326 private int getFabAlignment() {
1327 if (!mIsLandscape && !isInSearchUi() &&
1328 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1329 return FloatingActionButtonController.ALIGN_MIDDLE;
1330 }
1331 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001332 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001333}