blob: 85197a53099212e1723c386745987625b7d6a7f9 [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 Lee38019af2015-07-14 17:05:38 -070069import com.android.dialer.calllog.CallLogFragment;
Yorke Leefce269a2013-08-12 11:56:04 -070070import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070071import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070073import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070074import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080075import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070076import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080077import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070078import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070079import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070080import com.android.dialer.list.RegularSearchFragment;
81import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070082import com.android.dialer.list.SmartDialSearchFragment;
Ihab Awad9134e252014-07-09 12:32:52 -070083import com.android.dialer.list.SpeedDialFragment;
Yorke Leeaf6f1952014-07-14 19:13:16 -070084import com.android.dialer.settings.DialerSettingsActivity;
Andrew Lee247df6e2015-05-12 19:09:00 -070085import com.android.dialer.util.IntentUtil;
Yorke Lee7d20f822014-06-19 17:09:33 -070086import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070087import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070088import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070089import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070090import com.android.dialerbind.DatabaseHelperManager;
Yorke Lee2fec47b2014-08-05 18:16:27 -070091import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070092import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070093
Yorke Leed5c512a2015-01-30 14:42:53 -080094import junit.framework.Assert;
95
Yorke Leec3766332013-07-31 11:13:16 -070096import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080097import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070098
Chiao Cheng94b10b52012-08-17 16:59:12 -070099/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700100 * The dialer tab's title is 'phone', a more common name (see strings.xml).
101 */
Yorke Leec3766332013-07-31 11:13:16 -0700102public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700103 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700104 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700105 CallLogFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700106 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700107 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700108 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700109 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700110 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700111 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700112 ViewPager.OnPageChangeListener,
113 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700114 private static final String TAG = "DialtactsActivity";
115
Yorke Lee7cc61492015-06-01 14:59:33 -0700116 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700117
Yorke Leef74011e2013-08-02 11:30:30 -0700118 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
119
Chiao Cheng94b10b52012-08-17 16:59:12 -0700120 /** @see #getCallOrigin() */
121 private static final String CALL_ORIGIN_DIALTACTS =
122 "com.android.dialer.DialtactsActivity";
123
Yorke Leeb207f8a2013-08-29 18:51:06 -0700124 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
125 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700126 private static final String KEY_SEARCH_QUERY = "search_query";
127 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700128 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700129
Yorke Leec3766332013-07-31 11:13:16 -0700130 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
131 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
132 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
133 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700134
Chiao Cheng94b10b52012-08-17 16:59:12 -0700135 /**
136 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
137 */
138 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700139 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700140
Yorke Leec3766332013-07-31 11:13:16 -0700141 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700142
Andrew Lee6324f702015-06-16 14:45:58 -0700143 private static final int FAB_SCALE_IN_DELAY_MS = 300;
144
Yorke Leef614f6f2014-10-03 10:44:19 -0700145 private FrameLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700146
Yorke Leec3766332013-07-31 11:13:16 -0700147 /**
Yorke Leec3766332013-07-31 11:13:16 -0700148 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700149 */
Evan Charlton72d66252014-11-07 16:23:47 -0800150 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700151
152 /**
153 * Fragment for searching phone numbers using the alphanumeric keyboard.
154 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700155 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700156
157 /**
158 * Fragment for searching phone numbers using the dialpad.
159 */
160 private SmartDialSearchFragment mSmartDialSearchFragment;
161
Yorke Leee00c9fe2014-04-12 12:42:06 -0700162 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700163 * Animation that slides in.
164 */
165 private Animation mSlideIn;
166
167 /**
168 * Animation that slides out.
169 */
170 private Animation mSlideOut;
171
Yorke Lee6c450eb2015-05-08 09:45:47 -0700172 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
173 @Override
174 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700175 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700176 }
177 };
178
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700179 /**
180 * Listener for after slide out animation completes on dialer fragment.
181 */
182 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
183 @Override
184 public void onAnimationEnd(Animation animation) {
185 commitDialpadFragmentHide();
186 }
187 };
188
189 /**
Yorke Leee00c9fe2014-04-12 12:42:06 -0700190 * Fragment containing the speed dial list, recents list, and all contacts list.
191 */
192 private ListsFragment mListsFragment;
193
Yorke Lee19e68ca2014-10-28 11:51:40 -0700194 /**
195 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
196 * be commited.
197 */
198 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800199 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700200 private boolean mInDialpadSearch;
201 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700202 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700203 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700204 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700205
Yorke Leeef2b7382013-08-09 17:39:25 -0700206 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700207 * Whether or not the device is in landscape orientation.
208 */
209 private boolean mIsLandscape;
210
211 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700212 * True if the dialpad is only temporarily showing due to being in call
213 */
214 private boolean mInCallDialpadUp;
215
216 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700217 * True when this activity has been launched for the first time.
218 */
Yorke Lee98702de2013-08-06 12:03:32 -0700219 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700220
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700221 /**
222 * Search query to be applied to the SearchView in the ActionBar once
223 * onCreateOptionsMenu has been called.
224 */
225 private String mPendingSearchViewQuery;
226
Yorke Lee74edcdc2014-07-11 16:15:08 -0700227 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700228 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700229 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700230
Yorke Leeef2b7382013-08-09 17:39:25 -0700231 private String mSearchQuery;
232
Yorke Leefce269a2013-08-12 11:56:04 -0700233 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700234 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700235 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700236
Sai Cheemalapati41460652014-06-02 21:05:39 -0700237 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700238
Andrew Lee2423a2f2014-05-29 14:14:45 -0700239 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700240
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800241 /**
242 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
243 * {@link #onResume()} to populate the search box.
244 */
245 private String mVoiceSearchQuery;
246
Andrew Lee467de3d2015-03-17 15:47:24 -0700247 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700248 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700249 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700250 }
251
252 @Override
253 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700254 final boolean hasContactsPermission =
255 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700256 final Menu menu = getMenu();
257 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700258 clearFrequents.setVisible(mListsFragment != null &&
259 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700260 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
261
262 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
263 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700264
265 menu.findItem(R.id.menu_history).setVisible(
266 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700267 super.show();
268 }
269 }
270
Chiao Cheng94b10b52012-08-17 16:59:12 -0700271 /**
Yorke Lee28266192014-05-01 10:05:52 -0700272 * Listener that listens to drag events and sends their x and y coordinates to a
273 * {@link DragDropController}.
274 */
275 private class LayoutOnDragListener implements OnDragListener {
276 @Override
277 public boolean onDrag(View v, DragEvent event) {
278 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700279 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700280 }
281 return true;
282 }
283 }
284
285 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700286 * Listener used to send search queries to the phone search fragment.
287 */
Yorke Lee53a22302014-04-29 18:13:46 -0700288 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700289 @Override
290 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
291 }
Yorke Leec3766332013-07-31 11:13:16 -0700292
Andrew Lee99a570c2014-05-15 14:18:50 -0700293 @Override
294 public void onTextChanged(CharSequence s, int start, int before, int count) {
295 final String newText = s.toString();
296 if (newText.equals(mSearchQuery)) {
297 // If the query hasn't changed (perhaps due to activity being destroyed
298 // and restored, or user launching the same DIAL intent twice), then there is
299 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700300 return;
301 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700302 if (DEBUG) {
303 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700304 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700305 }
Yorke Lee710709d2014-05-29 07:18:42 -0700306 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700307
Andrew Lee90374a72014-05-16 15:01:09 -0700308 // Show search fragment only when the query string is changed to non-empty text.
309 if (!TextUtils.isEmpty(newText)) {
310 // Call enterSearchUi only if we are switching search modes, or showing a search
311 // fragment for the first time.
312 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
313 (!mIsDialpadShown && mInRegularSearch);
314 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700315 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700316 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700317 }
318
Andrew Leea8515422014-06-13 16:53:54 -0700319 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700320 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700321 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700322 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700323 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700324 }
325
326 @Override
327 public void afterTextChanged(Editable s) {
328 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700329 };
330
Andrew Leeb1903842014-05-15 16:11:24 -0700331
332 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700333 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700334 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700335 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700336 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700337 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700338 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700339 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700340 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
341 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700342 }
Andrew Leeb1903842014-05-15 16:11:24 -0700343 }
344 };
345
346 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700347 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700348 */
349 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
350 @Override
351 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700352 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
353 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
354 // If the search term is empty, close the search UI.
355 maybeExitSearchUi();
356 } else {
357 // If the search term is not empty, show the dialpad fab.
358 showFabInSearchUi();
359 }
Andrew Leeb1903842014-05-15 16:11:24 -0700360 }
361 return false;
362 }
363 };
364
Chiao Cheng94b10b52012-08-17 16:59:12 -0700365 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700366 public boolean dispatchTouchEvent(MotionEvent ev) {
367 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
368 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
369 }
370 return super.dispatchTouchEvent(ev);
371
372 }
373
374 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700375 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800376 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700377 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700378
Yorke Lee98702de2013-08-06 12:03:32 -0700379 mFirstLaunch = true;
380
Andrew Lee2423a2f2014-05-29 14:14:45 -0700381 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700382 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700383
Yorke Leef5554ce2015-01-27 14:46:02 -0800384 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700385 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800386 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800387 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700388
Yorke Leef5554ce2015-01-27 14:46:02 -0800389 Trace.beginSection(TAG + " setup Views");
Yorke Lee53a22302014-04-29 18:13:46 -0700390 final ActionBar actionBar = getActionBar();
391 actionBar.setCustomView(R.layout.search_edittext);
392 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700393 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700394
Andrew Lee04675a52014-06-05 18:36:20 -0700395 SearchEditTextLayout searchEditTextLayout =
Andrew Leee3f59a82015-02-26 12:06:35 -0800396 (SearchEditTextLayout) actionBar.getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700397 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
398
Andrew Leee3f59a82015-02-26 12:06:35 -0800399 mActionBarController = new ActionBarController(this, searchEditTextLayout);
400
Andrew Lee04675a52014-06-05 18:36:20 -0700401 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700402 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700403 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
404 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
405 .setOnClickListener(mSearchViewOnClickListener);
406 searchEditTextLayout.findViewById(R.id.search_box_start_search)
407 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700408 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700409 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700410 @Override
411 public void onBackButtonClicked() {
412 onBackPressed();
413 }
Andrew Lee6324f702015-06-16 14:45:58 -0700414
415 @Override
416 public void onSearchViewClicked() {
417 // Hide FAB, as the keyboard is shown.
418 mFloatingActionButtonController.scaleOut();
419 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700420 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700421
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700422 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700423 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700424
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700425 final View floatingActionButtonContainer = findViewById(
426 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700427 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
428 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700429 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700430 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700431
Andrew Lee04675a52014-06-05 18:36:20 -0700432 ImageButton optionsMenuButton =
433 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700434 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700435 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
436 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700437
Yorke Leed5c512a2015-01-30 14:42:53 -0800438 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
439 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700440 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800441 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000442 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800443 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700444 } else {
445 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700446 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
447 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700448 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700449 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700450 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700451 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700452
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700453 final boolean isLayoutRtl = DialerUtils.isRtl();
454 if (mIsLandscape) {
455 mSlideIn = AnimationUtils.loadAnimation(this,
456 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
457 mSlideOut = AnimationUtils.loadAnimation(this,
458 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
459 } else {
460 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
461 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
462 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700463
Yorke Lee2fec47b2014-08-05 18:16:27 -0700464 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
465 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
466
Yorke Lee6c450eb2015-05-08 09:45:47 -0700467 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700468 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700469
Yorke Leef614f6f2014-10-03 10:44:19 -0700470 mParentLayout = (FrameLayout) findViewById(R.id.dialtacts_mainlayout);
471 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700472 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700473 new ViewTreeObserver.OnGlobalLayoutListener() {
474 @Override
475 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700476 final ViewTreeObserver observer =
477 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700478 if (!observer.isAlive()) {
479 return;
480 }
481 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700482 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700483 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700484 mFloatingActionButtonController.align(
485 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700486 }
487 });
Andrew Lee0c667702014-05-12 14:31:45 -0700488
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700489 setupActivityOverlay();
490
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
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700500 private void setupActivityOverlay() {
501 final View activityOverlay = findViewById(R.id.activity_overlay);
502 activityOverlay.setOnTouchListener(new OnTouchListener() {
503 @Override
504 public boolean onTouch(View v, MotionEvent event) {
505 if (!mIsDialpadShown) {
506 maybeExitSearchUi();
507 }
508 return false;
509 }
510 });
511 }
512
Chiao Cheng94b10b52012-08-17 16:59:12 -0700513 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700514 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800515 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700516 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700517
Yorke Lee19e68ca2014-10-28 11:51:40 -0700518 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700519 if (mFirstLaunch) {
520 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700521 } else if (!phoneIsInUse() && mInCallDialpadUp) {
522 hideDialpadFragment(false, true);
523 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700524 } else if (mShowDialpadOnResume) {
525 showDialpadFragment(false);
526 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700527 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800528
529 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
530 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
531 // shown until onResume has completed. Active the search UI and set the search term now.
532 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
533 mActionBarController.onSearchBoxTapped();
534 mSearchView.setText(mVoiceSearchQuery);
535 mVoiceSearchQuery = null;
536 }
537
Yorke Lee98702de2013-08-06 12:03:32 -0700538 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800539
540 if (mIsRestarting) {
541 // This is only called when the activity goes from resumed -> paused -> resumed, so it
542 // will not cause an extra view to be sent out on rotation
543 if (mIsDialpadShown) {
544 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
545 }
546 mIsRestarting = false;
547 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700548
Yorke Lee53a22302014-04-29 18:13:46 -0700549 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700550 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700551 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700552
553 if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
554 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
555 if (index < mListsFragment.getTabCount()) {
556 mListsFragment.showTab(index);
557 }
Nancy Chen019713e2015-07-06 18:25:24 -0700558 } else if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
559 mListsFragment.showTab(ListsFragment.TAB_INDEX_RECENTS);
Nancy Chend5de03b2015-07-01 09:20:45 -0700560 }
561
Yorke Leef5554ce2015-01-27 14:46:02 -0800562 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700563 }
564
565 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800566 protected void onRestart() {
567 super.onRestart();
568 mIsRestarting = true;
569 }
570
571 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700572 protected void onPause() {
573 if (mClearSearchOnPause) {
574 hideDialpadAndSearchUi();
575 mClearSearchOnPause = false;
576 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700577 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
578 commitDialpadFragmentHide();
579 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700580 super.onPause();
581 }
582
583 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700584 protected void onSaveInstanceState(Bundle outState) {
585 super.onSaveInstanceState(outState);
586 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700587 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
588 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700589 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700590 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700591 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700592 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700593 }
594
595 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700596 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700597 if (fragment instanceof DialpadFragment) {
598 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800599 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700600 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
601 transaction.hide(mDialpadFragment);
602 transaction.commit();
603 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700604 } else if (fragment instanceof SmartDialSearchFragment) {
605 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700606 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700607 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700608 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700609 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700610 } else if (fragment instanceof ListsFragment) {
611 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700612 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700613 }
Yorke Leec3766332013-07-31 11:13:16 -0700614 }
615
Alon Albertb453e5b2013-09-10 15:54:23 -0700616 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700617 final Intent intent = new Intent(this, DialerSettingsActivity.class);
618 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700619 }
620
Chiao Cheng94b10b52012-08-17 16:59:12 -0700621 @Override
622 public void onClick(View view) {
623 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700624 case R.id.floating_action_button:
Andrew Lee432d4b52015-05-20 16:52:00 -0700625 if (mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700626 DialerUtils.startActivityWithErrorToast(
627 this,
628 IntentUtil.getNewContactIntent(),
629 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700630 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700631 mInCallDialpadUp = false;
632 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700633 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700634 break;
Yorke Leec3766332013-07-31 11:13:16 -0700635 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800636 try {
637 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
638 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
639 } catch (ActivityNotFoundException e) {
640 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
641 Toast.LENGTH_SHORT).show();
642 }
Yorke Leec3766332013-07-31 11:13:16 -0700643 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700644 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700645 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700646 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700647 default: {
648 Log.wtf(TAG, "Unexpected onClick event from " + view);
649 break;
650 }
651 }
652 }
653
Yorke Leec3766332013-07-31 11:13:16 -0700654 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700655 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700656 switch (item.getItemId()) {
657 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700658 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
659 // CONTENT_TYPE, so that we always open our call log from our dialer
660 final Intent intent = new Intent(this, CallLogActivity.class);
661 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700662 break;
663 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700664 DialerUtils.startActivityWithErrorToast(
665 this,
666 IntentUtil.getNewContactIntent(),
667 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700668 break;
669 case R.id.menu_import_export:
670 // We hard-code the "contactsAreAvailable" argument because doing it properly would
671 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
672 // now in Dialtacts for (potential) performance reasons. Compare with how it is
673 // done in {@link PeopleActivity}.
674 ImportExportDialogFragment.show(getFragmentManager(), true,
675 DialtactsActivity.class);
676 return true;
677 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700678 ClearFrequentsDialog.show(getFragmentManager());
679 return true;
680 case R.id.menu_call_settings:
681 handleMenuSettings();
682 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700683 }
684 return false;
685 }
686
687 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700688 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
689 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
690 if (resultCode == RESULT_OK) {
691 final ArrayList<String> matches = data.getStringArrayListExtra(
692 RecognizerIntent.EXTRA_RESULTS);
693 if (matches.size() > 0) {
694 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800695 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700696 } else {
697 Log.e(TAG, "Voice search - nothing heard");
698 }
699 } else {
700 Log.e(TAG, "Voice search failed");
701 }
702 }
703 super.onActivityResult(requestCode, resultCode, data);
704 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700705
Andrew Lee2a58ab82014-05-15 02:16:04 -0700706 /**
707 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
708 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
709 * @see #onDialpadShown
710 */
Yorke Leec3766332013-07-31 11:13:16 -0700711 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700712 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700713 return;
714 }
715 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800716
Yorke Lee7ee5c422014-11-04 10:29:32 -0800717 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700718
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800720 if (mDialpadFragment == null) {
721 mDialpadFragment = new DialpadFragment();
722 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
723 } else {
724 ft.show(mDialpadFragment);
725 }
726
727 mDialpadFragment.setAnimate(animate);
728 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700729 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700730
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700731 if (animate) {
732 mFloatingActionButtonController.scaleOut();
733 } else {
734 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700735 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700736 }
Yorke Lee5253be02014-05-21 18:50:03 -0700737 mActionBarController.onDialpadUp();
738
Yorke Lee6c450eb2015-05-08 09:45:47 -0700739 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700740 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700741
Andrew Lee2a58ab82014-05-15 02:16:04 -0700742 /**
743 * Callback from child DialpadFragment when the dialpad is shown.
744 */
745 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800746 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700747 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700748 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700749 } else {
750 mDialpadFragment.setYFraction(0);
751 }
752
Andrew Lee2a58ab82014-05-15 02:16:04 -0700753 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700754 }
755
756 /**
757 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
758 * a callback after the hide animation ends.
759 * @see #commitDialpadFragmentHide
760 */
Yorke Leeca195042013-09-25 16:29:38 -0700761 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700762 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700763 return;
764 }
Yorke Leef6673d32013-08-23 15:34:17 -0700765 if (clearDialpad) {
766 mDialpadFragment.clearDialpad();
767 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700768 if (!mIsDialpadShown) {
769 return;
Yorke Leec3766332013-07-31 11:13:16 -0700770 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700771 mIsDialpadShown = false;
772 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800773 mListsFragment.setUserVisibleHint(true);
774 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700775
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700776 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700777
Andrew Lee6324f702015-06-16 14:45:58 -0700778 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700779 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700780 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700781 } else {
782 commitDialpadFragmentHide();
783 }
784
Yorke Lee5253be02014-05-21 18:50:03 -0700785 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700786
Andrew Leed4cde832014-05-16 15:56:48 -0700787 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700788 if (TextUtils.isEmpty(mSearchQuery)) {
789 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700790 }
Andrew Leed4cde832014-05-16 15:56:48 -0700791 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700792 }
793
794 /**
795 * Finishes hiding the dialpad fragment after any animations are completed.
796 */
797 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800798 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700799 final FragmentTransaction ft = getFragmentManager().beginTransaction();
800 ft.hide(mDialpadFragment);
801 ft.commit();
802 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700803 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700804 }
805
Andrew Lee2a58ab82014-05-15 02:16:04 -0700806 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700807 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700808 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700809 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700810 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700811 fragment = mRegularSearchFragment;
812 }
813 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700814 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700815 }
816 }
817
Yorke Lee5253be02014-05-21 18:50:03 -0700818 @Override
819 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700820 return mInDialpadSearch || mInRegularSearch;
821 }
822
Yorke Lee5253be02014-05-21 18:50:03 -0700823 @Override
824 public boolean hasSearchQuery() {
825 return !TextUtils.isEmpty(mSearchQuery);
826 }
827
828 @Override
829 public boolean shouldShowActionBar() {
830 return mListsFragment.shouldShowActionBar();
831 }
832
Yorke Leeb207f8a2013-08-29 18:51:06 -0700833 private void setNotInSearchUi() {
834 mInDialpadSearch = false;
835 mInRegularSearch = false;
836 }
837
Yorke Lee311969c2013-08-26 06:31:11 -0700838 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700839 if (mIsDialpadShown) {
840 hideDialpadFragment(false, true);
841 } else {
842 exitSearchUi();
843 }
Yorke Lee311969c2013-08-26 06:31:11 -0700844 }
845
Yorke Lee53a22302014-04-29 18:13:46 -0700846 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700847 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
848 if (canIntentBeHandled(voiceIntent)) {
849 mVoiceSearchButton.setVisibility(View.VISIBLE);
850 mVoiceSearchButton.setOnClickListener(this);
851 } else {
852 mVoiceSearchButton.setVisibility(View.GONE);
853 }
854 }
855
Andrew Lee467de3d2015-03-17 15:47:24 -0700856 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700857 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
858 popupMenu.inflate(R.menu.dialtacts_options);
859 popupMenu.setOnMenuItemClickListener(this);
860 return popupMenu;
861 }
862
Yorke Lee86e21f72014-04-02 16:49:38 -0700863 @Override
864 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700865 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700866 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700867 mPendingSearchViewQuery = null;
868 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700869 if (mActionBarController != null) {
870 mActionBarController.restoreActionBarOffset();
871 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700872 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700873 }
874
Chiao Cheng94b10b52012-08-17 16:59:12 -0700875 /**
876 * Returns true if the intent is due to hitting the green send key (hardware call button:
877 * KEYCODE_CALL) while in a call.
878 *
879 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700880 * @return true if the intent is due to hitting the green send key while in a call
881 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700882 private boolean isSendKeyWhileInCall(Intent intent) {
883 // If there is a call in progress and the user launched the dialer by hitting the call
884 // button, go straight to the in-call screen.
885 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700886
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700887 if (callKey) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700888 getTelecomManager().showInCallScreen(false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700889 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700890 }
891
892 return false;
893 }
894
895 /**
896 * Sets the current tab based on the intent's request type
897 *
898 * @param intent Intent that contains information about which tab should be selected
899 */
Yorke Leec3766332013-07-31 11:13:16 -0700900 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700901 // 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 -0700902 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700903 finish();
904 return;
905 }
906
Yorke Leed5c512a2015-01-30 14:42:53 -0800907 final boolean phoneIsInUse = phoneIsInUse();
908 if (phoneIsInUse || (intent.getData() != null && isDialIntent(intent))) {
909 showDialpadFragment(false);
910 mDialpadFragment.setStartedFromNewIntent(true);
911 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
912 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700913 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700914 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700915 }
916
917 @Override
918 public void onNewIntent(Intent newIntent) {
919 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700920
Nancy Chen199b7f02014-11-05 15:03:00 -0800921 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700922 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700923
Chiao Cheng94b10b52012-08-17 16:59:12 -0700924 invalidateOptionsMenu();
925 }
926
927 /** Returns true if the given intent contains a phone number to populate the dialer with */
928 private boolean isDialIntent(Intent intent) {
929 final String action = intent.getAction();
930 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
931 return true;
932 }
933 if (Intent.ACTION_VIEW.equals(action)) {
934 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700935 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700936 return true;
937 }
938 }
939 return false;
940 }
941
942 /**
943 * Returns an appropriate call origin for this Activity. May return null when no call origin
944 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
945 * for remembering the tab in which the user made a phone call, so the external app's DIAL
946 * request should not be counted.)
947 */
948 public String getCallOrigin() {
949 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
950 }
951
Chiao Cheng94b10b52012-08-17 16:59:12 -0700952 /**
Yorke Leec3766332013-07-31 11:13:16 -0700953 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700954 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700955 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700956 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700957 // Weird race condition where fragment is doing work after the activity is destroyed
958 // due to talkback being on (b/10209937). Just return since we can't do any
959 // constructive here.
960 return;
961 }
962
Yorke Leeef2b7382013-08-09 17:39:25 -0700963 if (DEBUG) {
964 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
965 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700966
Yorke Leec3766332013-07-31 11:13:16 -0700967 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700968 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700969 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700970 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700971 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700972 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700973
Yorke Leeb207f8a2013-08-29 18:51:06 -0700974 final String tag;
975 if (smartDialSearch) {
976 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
977 } else {
978 tag = TAG_REGULAR_SEARCH_FRAGMENT;
979 }
980 mInDialpadSearch = smartDialSearch;
981 mInRegularSearch = !smartDialSearch;
982
Andrew Lee6324f702015-06-16 14:45:58 -0700983 mFloatingActionButtonController.scaleOut();
984
Andrew Lee752956e2014-05-15 11:43:38 -0700985 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700986 if (animate) {
987 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
988 } else {
989 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
990 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700991 if (fragment == null) {
992 if (smartDialSearch) {
993 fragment = new SmartDialSearchFragment();
994 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700995 fragment = new RegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -0700996 fragment.setOnTouchListener(new View.OnTouchListener() {
997 @Override
998 public boolean onTouch(View v, MotionEvent event) {
999 // Show the FAB when the user touches the lists fragment and the soft
1000 // keyboard is hidden.
1001 showFabInSearchUi();
1002 return false;
1003 }
1004 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001005 }
Andrew Leeb1903842014-05-15 16:11:24 -07001006 transaction.add(R.id.dialtacts_frame, fragment, tag);
1007 } else {
1008 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001009 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001010 // DialtactsActivity will provide the options menu
1011 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001012 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001013 if (!smartDialSearch) {
1014 fragment.setQueryString(query, false /* delaySelection */);
1015 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001016 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001017
Yorke Lee6c450eb2015-05-08 09:45:47 -07001018 if (animate) {
1019 mListsFragment.getView().animate().alpha(0).withLayer();
1020 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001021 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001022 }
1023
1024 /**
Yorke Leec3766332013-07-31 11:13:16 -07001025 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001026 */
Yorke Leec3766332013-07-31 11:13:16 -07001027 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001028 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001029 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001030 return;
1031 }
Andrew Leeb1903842014-05-15 16:11:24 -07001032
Andrew Leeb1903842014-05-15 16:11:24 -07001033 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001034
1035 if (mDialpadFragment != null) {
1036 mDialpadFragment.clearDialpad();
1037 }
1038
Yorke Leeb207f8a2013-08-29 18:51:06 -07001039 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001040
Andrew Lee6324f702015-06-16 14:45:58 -07001041 // Restore the FAB for the lists fragment.
1042 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1043 mFloatingActionButtonController.setVisible(false);
1044 }
1045 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1046 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1047 onPageSelected(mListsFragment.getCurrentTabIndex());
1048
Andrew Leeb1903842014-05-15 16:11:24 -07001049 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001050 if (mSmartDialSearchFragment != null) {
1051 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001052 }
Andrew Leeb1903842014-05-15 16:11:24 -07001053 if (mRegularSearchFragment != null) {
1054 transaction.remove(mRegularSearchFragment);
1055 }
1056 transaction.commit();
1057
1058 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001059
1060 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001061 // If the dialpad fragment wasn't previously visible, then send a screen view because
1062 // we are exiting regular search. Otherwise, the screen view will be sent by
1063 // {@link #hideDialpadFragment}.
1064 mListsFragment.sendScreenViewForCurrentPosition();
1065 mListsFragment.setUserVisibleHint(true);
1066 }
1067
Yorke Lee5253be02014-05-21 18:50:03 -07001068 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001069 }
1070
Chiao Cheng94b10b52012-08-17 16:59:12 -07001071 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001072 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001073 if (mStateSaved) {
1074 return;
1075 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001076 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001077 if (TextUtils.isEmpty(mSearchQuery) ||
1078 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1079 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001080 exitSearchUi();
1081 }
Yorke Leef6673d32013-08-23 15:34:17 -07001082 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001083 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001084 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001085 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001086 } else {
1087 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001088 }
Yorke Leec3766332013-07-31 11:13:16 -07001089 }
1090
Yorke Lee81a313d2015-06-01 14:53:59 -07001091 private void maybeEnterSearchUi() {
1092 if (!isInSearchUi()) {
1093 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1094 }
1095 }
1096
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001097 /**
1098 * @return True if the search UI was exited, false otherwise
1099 */
1100 private boolean maybeExitSearchUi() {
1101 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1102 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001103 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001104 return true;
1105 }
1106 return false;
1107 }
1108
Andrew Lee6324f702015-06-16 14:45:58 -07001109 private void showFabInSearchUi() {
1110 mFloatingActionButtonController.changeIcon(
1111 getResources().getDrawable(R.drawable.fab_ic_dial),
1112 getResources().getString(R.string.action_menu_dialpad_button));
1113 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1114 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1115 }
1116
Yorke Leec3766332013-07-31 11:13:16 -07001117 @Override
1118 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001119 if (mSmartDialSearchFragment != null) {
1120 mSmartDialSearchFragment.setAddToContactNumber(query);
1121 }
Yorke Leec3766332013-07-31 11:13:16 -07001122 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1123 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001124
1125 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001126 if (DEBUG) {
1127 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1128 }
1129 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1130 // This callback can happen if the dialpad fragment is recreated because of
1131 // activity destruction. In that case, don't update the search view because
1132 // that would bring the user back to the search fragment regardless of the
1133 // previous state of the application. Instead, just return here and let the
1134 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001135 if (!TextUtils.isEmpty(normalizedQuery)) {
1136 mPendingSearchViewQuery = normalizedQuery;
1137 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001138 return;
1139 }
Yorke Lee53a22302014-04-29 18:13:46 -07001140 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001141 }
Santos Cordon83131712015-04-16 21:25:35 -07001142
1143 try {
1144 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1145 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1146 }
1147 } catch (Exception ignored) {
1148 // Skip any exceptions for this piece of code
1149 }
1150
Chiao Cheng94b10b52012-08-17 16:59:12 -07001151 }
Yorke Leec3766332013-07-31 11:13:16 -07001152
1153 @Override
1154 public void onListFragmentScrollStateChange(int scrollState) {
1155 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001156 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001157 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001158 }
1159 }
1160
1161 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001162 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001163 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001164 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1165 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001166 }
1167
Yorke Leec3766332013-07-31 11:13:16 -07001168 private boolean phoneIsInUse() {
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001169 return getTelecomManager().isInCall();
Yorke Leec3766332013-07-31 11:13:16 -07001170 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001171
Yorke Leeda0f9042013-12-05 14:25:51 -08001172 private boolean canIntentBeHandled(Intent intent) {
1173 final PackageManager packageManager = getPackageManager();
1174 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1175 PackageManager.MATCH_DEFAULT_ONLY);
1176 return resolveInfo != null && resolveInfo.size() > 0;
1177 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001178
Yorke Lee86e21f72014-04-02 16:49:38 -07001179 /**
1180 * Called when the user has long-pressed a contact tile to start a drag operation.
1181 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001182 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001183 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001184 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001185 }
1186
1187 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001188 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1189 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001190
Yorke Lee86e21f72014-04-02 16:49:38 -07001191 /**
1192 * Called when the user has released a contact tile after long-pressing it.
1193 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001194 @Override
1195 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001196 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001197 }
1198
1199 @Override
1200 public void onDroppedOnRemove() {}
1201
1202 /**
Yorke Leed999b712014-04-23 15:10:58 -07001203 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001204 * once it has been attached to the activity.
1205 */
1206 @Override
1207 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001208 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001209 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001210 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001211
Yorke Lee38019af2015-07-14 17:05:38 -07001212 /**
1213 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1214 */
1215 @Override
1216 public void showAllContactsTab() {
1217 if (mListsFragment != null) {
1218 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1219 }
1220 }
1221
1222 /**
1223 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1224 */
1225 @Override
1226 public void showDialpad() {
1227 showDialpadFragment(true);
1228 }
1229
Yorke Leee00c9fe2014-04-12 12:42:06 -07001230 @Override
1231 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001232 // Specify call-origin so that users will see the previous tab instead of
1233 // CallLog screen (search UI will be automatically exited).
1234 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001235 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001236 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001237 }
1238
1239 @Override
1240 public void onCallNumberDirectly(String phoneNumber) {
Andrew Leefc1fed72014-07-02 11:05:53 -07001241 onCallNumberDirectly(phoneNumber, false /* isVideoCall */);
1242 }
1243
1244 @Override
1245 public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001246 if (phoneNumber == null) {
1247 // Invalid phone number, but let the call go through so that InCallUI can show
1248 // an error message.
1249 phoneNumber = "";
1250 }
Andrew Leed2a683f2014-07-08 15:54:50 -07001251 Intent intent = isVideoCall ?
Andrew Lee247df6e2015-05-12 19:09:00 -07001252 IntentUtil.getVideoCallIntent(phoneNumber, getCallOrigin()) :
1253 IntentUtil.getCallIntent(phoneNumber, getCallOrigin());
Yorke Lee7d20f822014-06-19 17:09:33 -07001254 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001255 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001256 }
1257
1258 @Override
1259 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001260 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001261 }
1262
1263 @Override
1264 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001265 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001266 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001267
Yorke Leecc4660d2014-04-24 11:22:57 -07001268 @Override
1269 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001270 int tabIndex = mListsFragment.getCurrentTabIndex();
1271
1272 // Scroll the button from center to end when moving from the Speed Dial to Recents tab.
1273 // In RTL, scroll when the current tab is Recents instead of Speed Dial, because the order
1274 // of the tabs is reversed and the ViewPager returns the left tab position during scroll.
1275 boolean isRtl = DialerUtils.isRtl();
1276 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1277 mFloatingActionButtonController.onPageScrolled(positionOffset);
1278 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_RECENTS && !mIsLandscape) {
1279 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1280 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001281 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001282 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001283 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001284
Andrew Lee432d4b52015-05-20 16:52:00 -07001285 @Override
1286 public void onPageSelected(int position) {
1287 int tabIndex = mListsFragment.getCurrentTabIndex();
1288 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001289 mFloatingActionButtonController.changeIcon(
1290 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1291 getResources().getString(R.string.search_shortcut_create_new_contact));
1292 } else {
1293 mFloatingActionButtonController.changeIcon(
1294 getResources().getDrawable(R.drawable.fab_ic_dial),
1295 getResources().getString(R.string.action_menu_dialpad_button));
1296 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001297 }
1298
1299 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001300 public void onPageScrollStateChanged(int state) {
1301 }
1302
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001303 private TelecomManager getTelecomManager() {
1304 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordond15629d2014-07-07 15:15:29 -07001305 }
1306
Yorke Lee5253be02014-05-21 18:50:03 -07001307 @Override
1308 public boolean isActionBarShowing() {
1309 return mActionBarController.isActionBarShowing();
1310 }
1311
Yorke Leec98a5bb2014-10-28 16:12:05 -07001312 @Override
1313 public ActionBarController getActionBarController() {
1314 return mActionBarController;
1315 }
1316
Yorke Lee6bc67162015-06-27 14:03:25 -07001317 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001318 public boolean isDialpadShown() {
1319 return mIsDialpadShown;
1320 }
1321
Yorke Lee5253be02014-05-21 18:50:03 -07001322 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001323 public int getDialpadHeight() {
1324 if (mDialpadFragment != null) {
1325 return mDialpadFragment.getDialpadHeight();
1326 }
1327 return 0;
1328 }
1329
1330 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001331 public int getActionBarHideOffset() {
1332 return getActionBar().getHideOffset();
1333 }
1334
1335 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001336 public void setActionBarHideOffset(int offset) {
1337 getActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001338 }
1339
1340 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001341 public int getActionBarHeight() {
1342 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001343 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001344
Andrew Lee6324f702015-06-16 14:45:58 -07001345 private int getFabAlignment() {
1346 if (!mIsLandscape && !isInSearchUi() &&
1347 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1348 return FloatingActionButtonController.ALIGN_MIDDLE;
1349 }
1350 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001351 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001352}