blob: f8fb17f6bfe3d116e076f9d22cf35e4427f684d8 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
Yorke Leec3766332013-07-31 11:13:16 -07002 * Copyright (C) 2013 The Android Open Source Project
Chiao Cheng94b10b52012-08-17 16:59:12 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer;
18
Chiao Cheng94b10b52012-08-17 16:59:12 -070019import android.app.Fragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -070020import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080021import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.content.Context;
23import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
Sai Cheemalapati82d6da52014-06-03 13:54:23 -070026import android.content.res.Configuration;
Andrew Lee2423a2f2014-05-29 14:14:45 -070027import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.net.Uri;
29import android.os.Bundle;
Yorke Leef5554ce2015-01-27 14:46:02 -080030import android.os.Trace;
Nancy Chen019713e2015-07-06 18:25:24 -070031import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070032import android.speech.RecognizerIntent;
Nancy Chenf3d9f822015-09-11 00:47:31 -040033import android.support.design.widget.CoordinatorLayout;
Yorke Leecc4660d2014-04-24 11:22:57 -070034import android.support.v4.view.ViewPager;
Nancy Chen44898ad2015-08-13 12:03:47 -070035import android.support.v7.app.ActionBar;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070036import android.telecom.PhoneAccount;
37import android.telecom.TelecomManager;
Yorke Lee53a22302014-04-29 18:13:46 -070038import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070040import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070042import android.view.DragEvent;
Yorke Lee74edcdc2014-07-11 16:15:08 -070043import android.view.Gravity;
Andrew Leeb1903842014-05-15 16:11:24 -070044import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070047import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070049import android.view.View.OnDragListener;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070050import android.view.View.OnTouchListener;
Ihab Awad9134e252014-07-09 12:32:52 -070051import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070052import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070053import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070054import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070055import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070056import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.widget.PopupMenu;
Anne Rong35208002015-07-22 16:27:37 -070058import android.widget.TextView;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080059import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070060
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 Lee827a90f2015-09-17 18:10:58 -070086import com.android.dialer.util.TelecomUtil;
Yorke Leee3a2d132015-09-14 16:52:08 -070087import com.android.dialer.util.IntentUtil.CallIntentBuilder;
Yorke Lee7d20f822014-06-19 17:09:33 -070088import com.android.dialer.util.DialerUtils;
Yorke Lee5253be02014-05-21 18:50:03 -070089import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070090import com.android.dialer.widget.SearchEditTextLayout;
Andrew Lee6324f702015-06-16 14:45:58 -070091import com.android.dialer.widget.SearchEditTextLayout.Callback;
Yorke Lee0baa98b2013-08-22 10:55:16 -070092import com.android.dialerbind.DatabaseHelperManager;
Yorke Leec8d6e162015-09-14 18:43:27 -070093import com.android.incallui.Call.LogState;
Yorke Lee2fec47b2014-08-05 18:16:27 -070094import com.android.phone.common.animation.AnimUtils;
Sai Cheemalapatifd723ca2014-06-19 12:30:07 -070095import com.android.phone.common.animation.AnimationListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070096
Yorke Leed5c512a2015-01-30 14:42:53 -080097import junit.framework.Assert;
98
Yorke Leec3766332013-07-31 11:13:16 -070099import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -0800100import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -0700101
Chiao Cheng94b10b52012-08-17 16:59:12 -0700102/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103 * The dialer tab's title is 'phone', a more common name (see strings.xml).
104 */
Yorke Leec3766332013-07-31 11:13:16 -0700105public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700106 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700107 OnListFragmentScrolledListener,
Yorke Lee38019af2015-07-14 17:05:38 -0700108 CallLogFragment.HostInterface,
Yorke Lee575ae382015-07-16 11:36:07 -0700109 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700110 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700111 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700112 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700113 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700114 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700115 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700116 ViewPager.OnPageChangeListener,
117 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118 private static final String TAG = "DialtactsActivity";
119
Yorke Lee7cc61492015-06-01 14:59:33 -0700120 public static final boolean DEBUG = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121
Yorke Leef74011e2013-08-02 11:30:30 -0700122 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
123
Yorke Leeb207f8a2013-08-29 18:51:06 -0700124 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
125 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700126 private static final String KEY_SEARCH_QUERY = "search_query";
127 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700128 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700129
Yorke Leec3766332013-07-31 11:13:16 -0700130 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
131 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
132 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
133 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700134
Chiao Cheng94b10b52012-08-17 16:59:12 -0700135 /**
136 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
137 */
138 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700139 public static final String EXTRA_SHOW_TAB = "EXTRA_SHOW_TAB";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700140
Yorke Leec3766332013-07-31 11:13:16 -0700141 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700142
Andrew Lee6324f702015-06-16 14:45:58 -0700143 private static final int FAB_SCALE_IN_DELAY_MS = 300;
144
Nancy Chenf3d9f822015-09-11 00:47:31 -0400145 private CoordinatorLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700146
Yorke Leec3766332013-07-31 11:13:16 -0700147 /**
Yorke Leec3766332013-07-31 11:13:16 -0700148 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700149 */
Evan Charlton72d66252014-11-07 16:23:47 -0800150 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700151
152 /**
153 * Fragment for searching phone numbers using the alphanumeric keyboard.
154 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700155 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700156
157 /**
158 * Fragment for searching phone numbers using the dialpad.
159 */
160 private SmartDialSearchFragment mSmartDialSearchFragment;
161
Jay Shrauner119b0652015-09-17 12:00:18 -0700162 private boolean mIsVisible;
163
Yorke Leee00c9fe2014-04-12 12:42:06 -0700164 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700165 * Animation that slides in.
166 */
167 private Animation mSlideIn;
168
169 /**
170 * Animation that slides out.
171 */
172 private Animation mSlideOut;
173
Yorke Lee6c450eb2015-05-08 09:45:47 -0700174 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
175 @Override
176 public void onAnimationEnd(Animation animation) {
Yorke Lee81a313d2015-06-01 14:53:59 -0700177 maybeEnterSearchUi();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700178 }
179 };
180
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700181 /**
182 * Listener for after slide out animation completes on dialer fragment.
183 */
184 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
185 @Override
186 public void onAnimationEnd(Animation animation) {
187 commitDialpadFragmentHide();
188 }
189 };
190
191 /**
Andrew Lee4de59fb2015-08-13 15:20:08 -0700192 * Fragment containing the speed dial list, call history list, and all contacts list.
Yorke Leee00c9fe2014-04-12 12:42:06 -0700193 */
194 private ListsFragment mListsFragment;
195
Yorke Lee19e68ca2014-10-28 11:51:40 -0700196 /**
197 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
198 * be commited.
199 */
200 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800201 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700202 private boolean mInDialpadSearch;
203 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700204 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700205 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700206 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700207
Yorke Leeef2b7382013-08-09 17:39:25 -0700208 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700209 * Whether or not the device is in landscape orientation.
210 */
211 private boolean mIsLandscape;
212
213 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700214 * True if the dialpad is only temporarily showing due to being in call
215 */
216 private boolean mInCallDialpadUp;
217
218 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700219 * True when this activity has been launched for the first time.
220 */
Yorke Lee98702de2013-08-06 12:03:32 -0700221 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700222
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700223 /**
224 * Search query to be applied to the SearchView in the ActionBar once
225 * onCreateOptionsMenu has been called.
226 */
227 private String mPendingSearchViewQuery;
228
Yorke Lee74edcdc2014-07-11 16:15:08 -0700229 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700230 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700231 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700232
Yorke Leeef2b7382013-08-09 17:39:25 -0700233 private String mSearchQuery;
234
Yorke Leefce269a2013-08-12 11:56:04 -0700235 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700236 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700237 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700238
Sai Cheemalapati41460652014-06-02 21:05:39 -0700239 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700240
Andrew Lee2423a2f2014-05-29 14:14:45 -0700241 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700242
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800243 /**
244 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
245 * {@link #onResume()} to populate the search box.
246 */
247 private String mVoiceSearchQuery;
248
Andrew Lee467de3d2015-03-17 15:47:24 -0700249 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700250 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700251 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700252 }
253
254 @Override
255 public void show() {
Yorke Leec16ea5a2015-05-19 15:51:01 -0700256 final boolean hasContactsPermission =
257 PermissionsUtil.hasContactsPermissions(DialtactsActivity.this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700258 final Menu menu = getMenu();
259 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700260 clearFrequents.setVisible(mListsFragment != null &&
261 mListsFragment.getSpeedDialFragment() != null &&
Yorke Leec16ea5a2015-05-19 15:51:01 -0700262 mListsFragment.getSpeedDialFragment().hasFrequents() && hasContactsPermission);
263
264 menu.findItem(R.id.menu_import_export).setVisible(hasContactsPermission);
265 menu.findItem(R.id.menu_add_contact).setVisible(hasContactsPermission);
Yorke Leeb7330e32015-05-22 15:55:22 -0700266
267 menu.findItem(R.id.menu_history).setVisible(
268 PermissionsUtil.hasPhonePermissions(DialtactsActivity.this));
Yorke Leee00c9fe2014-04-12 12:42:06 -0700269 super.show();
270 }
271 }
272
Chiao Cheng94b10b52012-08-17 16:59:12 -0700273 /**
Yorke Lee28266192014-05-01 10:05:52 -0700274 * Listener that listens to drag events and sends their x and y coordinates to a
275 * {@link DragDropController}.
276 */
277 private class LayoutOnDragListener implements OnDragListener {
278 @Override
279 public boolean onDrag(View v, DragEvent event) {
280 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700281 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700282 }
283 return true;
284 }
285 }
286
287 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700288 * Listener used to send search queries to the phone search fragment.
289 */
Yorke Lee53a22302014-04-29 18:13:46 -0700290 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700291 @Override
292 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
293 }
Yorke Leec3766332013-07-31 11:13:16 -0700294
Andrew Lee99a570c2014-05-15 14:18:50 -0700295 @Override
296 public void onTextChanged(CharSequence s, int start, int before, int count) {
297 final String newText = s.toString();
298 if (newText.equals(mSearchQuery)) {
299 // If the query hasn't changed (perhaps due to activity being destroyed
300 // and restored, or user launching the same DIAL intent twice), then there is
301 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700302 return;
303 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700304 if (DEBUG) {
305 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700306 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700307 }
Yorke Lee710709d2014-05-29 07:18:42 -0700308 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700309
Andrew Lee90374a72014-05-16 15:01:09 -0700310 // Show search fragment only when the query string is changed to non-empty text.
311 if (!TextUtils.isEmpty(newText)) {
312 // Call enterSearchUi only if we are switching search modes, or showing a search
313 // fragment for the first time.
314 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
315 (!mIsDialpadShown && mInRegularSearch);
316 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700317 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700318 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700319 }
320
Andrew Leea8515422014-06-13 16:53:54 -0700321 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700322 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700323 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700324 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700325 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700326 }
327
328 @Override
329 public void afterTextChanged(Editable s) {
330 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700331 };
332
Andrew Leeb1903842014-05-15 16:11:24 -0700333
334 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700335 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700336 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700337 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700338 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700339 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700340 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700341 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700342 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
343 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700344 }
Andrew Leeb1903842014-05-15 16:11:24 -0700345 }
346 };
347
348 /**
Andrew Lee6324f702015-06-16 14:45:58 -0700349 * Handles the user closing the soft keyboard.
Andrew Leeb1903842014-05-15 16:11:24 -0700350 */
351 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
352 @Override
353 public boolean onKey(View v, int keyCode, KeyEvent event) {
Andrew Lee6324f702015-06-16 14:45:58 -0700354 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
355 if (TextUtils.isEmpty(mSearchView.getText().toString())) {
356 // If the search term is empty, close the search UI.
357 maybeExitSearchUi();
358 } else {
359 // If the search term is not empty, show the dialpad fab.
360 showFabInSearchUi();
361 }
Andrew Leeb1903842014-05-15 16:11:24 -0700362 }
363 return false;
364 }
365 };
366
Chiao Cheng94b10b52012-08-17 16:59:12 -0700367 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700368 public boolean dispatchTouchEvent(MotionEvent ev) {
369 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
370 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
371 }
372 return super.dispatchTouchEvent(ev);
373
374 }
375
376 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700377 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800378 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700379 super.onCreate(savedInstanceState);
Yorke Leec16ea5a2015-05-19 15:51:01 -0700380
Yorke Lee98702de2013-08-06 12:03:32 -0700381 mFirstLaunch = true;
382
Andrew Lee2423a2f2014-05-29 14:14:45 -0700383 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700384 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700385
Yorke Leef5554ce2015-01-27 14:46:02 -0800386 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700387 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800388 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800389 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700390
Yorke Leef5554ce2015-01-27 14:46:02 -0800391 Trace.beginSection(TAG + " setup Views");
Nancy Chen44898ad2015-08-13 12:03:47 -0700392 final ActionBar actionBar = getSupportActionBar();
Yorke Lee53a22302014-04-29 18:13:46 -0700393 actionBar.setCustomView(R.layout.search_edittext);
394 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700395 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700396
Anne Rong35208002015-07-22 16:27:37 -0700397 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) actionBar
398 .getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700399 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
400
Andrew Leee3f59a82015-02-26 12:06:35 -0800401 mActionBarController = new ActionBarController(this, searchEditTextLayout);
402
Andrew Lee04675a52014-06-05 18:36:20 -0700403 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700404 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700405 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
406 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
407 .setOnClickListener(mSearchViewOnClickListener);
408 searchEditTextLayout.findViewById(R.id.search_box_start_search)
409 .setOnClickListener(mSearchViewOnClickListener);
Yorke Lee7a6f1892015-06-27 13:21:48 -0700410 searchEditTextLayout.setOnClickListener(mSearchViewOnClickListener);
Andrew Lee6324f702015-06-16 14:45:58 -0700411 searchEditTextLayout.setCallback(new SearchEditTextLayout.Callback() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700412 @Override
413 public void onBackButtonClicked() {
414 onBackPressed();
415 }
Andrew Lee6324f702015-06-16 14:45:58 -0700416
417 @Override
418 public void onSearchViewClicked() {
419 // Hide FAB, as the keyboard is shown.
420 mFloatingActionButtonController.scaleOut();
421 }
Yorke Lee11ca39e2014-05-19 20:31:37 -0700422 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700423
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700424 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700425 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700426
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700427 final View floatingActionButtonContainer = findViewById(
428 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700429 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
430 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700431 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700432 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700433
Andrew Lee04675a52014-06-05 18:36:20 -0700434 ImageButton optionsMenuButton =
435 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700436 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700437 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
438 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700439
Yorke Leed5c512a2015-01-30 14:42:53 -0800440 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
441 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700442 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800443 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000444 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800445 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700446 } else {
447 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700448 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
449 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700450 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700451 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700452 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700453 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700454
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700455 final boolean isLayoutRtl = DialerUtils.isRtl();
456 if (mIsLandscape) {
457 mSlideIn = AnimationUtils.loadAnimation(this,
458 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
459 mSlideOut = AnimationUtils.loadAnimation(this,
460 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
461 } else {
462 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
463 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
464 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700465
Yorke Lee2fec47b2014-08-05 18:16:27 -0700466 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
467 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
468
Yorke Lee6c450eb2015-05-08 09:45:47 -0700469 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700470 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700471
Nancy Chenf3d9f822015-09-11 00:47:31 -0400472 mParentLayout = (CoordinatorLayout) findViewById(R.id.dialtacts_mainlayout);
Yorke Leef614f6f2014-10-03 10:44:19 -0700473 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700474 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700475 new ViewTreeObserver.OnGlobalLayoutListener() {
476 @Override
477 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700478 final ViewTreeObserver observer =
479 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700480 if (!observer.isAlive()) {
481 return;
482 }
483 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700484 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700485 mFloatingActionButtonController.setScreenWidth(screenWidth);
Andrew Lee6324f702015-06-16 14:45:58 -0700486 mFloatingActionButtonController.align(
487 getFabAlignment(), false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700488 }
489 });
Andrew Lee0c667702014-05-12 14:31:45 -0700490
Yorke Leef5554ce2015-01-27 14:46:02 -0800491 Trace.endSection();
492
493 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700494 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700495 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800496 Trace.endSection();
497 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700498 }
499
500 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700501 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800502 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700503 super.onResume();
Yorke Lee2a613022015-07-01 14:28:13 -0700504
Yorke Lee19e68ca2014-10-28 11:51:40 -0700505 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700506 if (mFirstLaunch) {
507 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700508 } else if (!phoneIsInUse() && mInCallDialpadUp) {
509 hideDialpadFragment(false, true);
510 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700511 } else if (mShowDialpadOnResume) {
512 showDialpadFragment(false);
513 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700514 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800515
516 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
517 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
518 // shown until onResume has completed. Active the search UI and set the search term now.
519 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
520 mActionBarController.onSearchBoxTapped();
521 mSearchView.setText(mVoiceSearchQuery);
522 mVoiceSearchQuery = null;
523 }
524
Yorke Lee98702de2013-08-06 12:03:32 -0700525 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800526
527 if (mIsRestarting) {
528 // This is only called when the activity goes from resumed -> paused -> resumed, so it
529 // will not cause an extra view to be sent out on rotation
530 if (mIsDialpadShown) {
531 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
532 }
533 mIsRestarting = false;
534 }
Andrew Lee75d5b2d2015-06-19 16:53:54 -0700535
Yorke Lee53a22302014-04-29 18:13:46 -0700536 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700537 mDialerDatabaseHelper.startSmartDialUpdateThread();
Andrew Lee6324f702015-06-16 14:45:58 -0700538 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
Nancy Chend5de03b2015-07-01 09:20:45 -0700539
Yorke Lee49aa5bf2015-09-10 15:53:43 -0700540 if (Calls.CONTENT_TYPE.equals(getIntent().getType())) {
541 // Externally specified extras take precedence to EXTRA_SHOW_TAB, which is only
542 // used internally.
543 final Bundle extras = getIntent().getExtras();
544 if (extras != null
545 && extras.getInt(Calls.EXTRA_CALL_TYPE_FILTER) == Calls.VOICEMAIL_TYPE) {
546 mListsFragment.showTab(ListsFragment.TAB_INDEX_VOICEMAIL);
547 } else {
548 mListsFragment.showTab(ListsFragment.TAB_INDEX_HISTORY);
549 }
550 } else if (getIntent().hasExtra(EXTRA_SHOW_TAB)) {
Nancy Chend5de03b2015-07-01 09:20:45 -0700551 int index = getIntent().getIntExtra(EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_SPEED_DIAL);
552 if (index < mListsFragment.getTabCount()) {
553 mListsFragment.showTab(index);
554 }
555 }
556
Anne Rong35208002015-07-22 16:27:37 -0700557 setSearchBoxHint();
558
Yorke Leef5554ce2015-01-27 14:46:02 -0800559 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700560 }
561
562 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800563 protected void onRestart() {
564 super.onRestart();
565 mIsRestarting = true;
566 }
567
568 @Override
Jay Shrauner119b0652015-09-17 12:00:18 -0700569 protected void onStart() {
570 super.onStart();
571 mIsVisible = true;
572 }
573
574 @Override
575 protected void onStop() {
576 mIsVisible = false;
577 super.onStop();
578 }
579
580 /**
581 * Returns true when the Activity is currently visible (between onStart and onStop).
582 */
583 /* package */ boolean isVisible() {
584 return mIsVisible;
585 }
586
587 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700588 protected void onPause() {
589 if (mClearSearchOnPause) {
590 hideDialpadAndSearchUi();
591 mClearSearchOnPause = false;
592 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700593 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
594 commitDialpadFragmentHide();
595 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700596 super.onPause();
597 }
598
599 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700600 protected void onSaveInstanceState(Bundle outState) {
Jay Shrauner119b0652015-09-17 12:00:18 -0700601 mIsVisible = false;
Yorke Leeef2b7382013-08-09 17:39:25 -0700602 super.onSaveInstanceState(outState);
603 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700604 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
605 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700606 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700607 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700608 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700609 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700610 }
611
612 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700613 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700614 if (fragment instanceof DialpadFragment) {
615 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800616 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700617 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
618 transaction.hide(mDialpadFragment);
619 transaction.commit();
620 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700621 } else if (fragment instanceof SmartDialSearchFragment) {
622 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700623 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700624 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700625 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700626 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700627 } else if (fragment instanceof ListsFragment) {
628 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700629 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700630 }
Yorke Leec3766332013-07-31 11:13:16 -0700631 }
632
Alon Albertb453e5b2013-09-10 15:54:23 -0700633 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700634 final Intent intent = new Intent(this, DialerSettingsActivity.class);
635 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700636 }
637
Chiao Cheng94b10b52012-08-17 16:59:12 -0700638 @Override
639 public void onClick(View view) {
640 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700641 case R.id.floating_action_button:
Yorke Leedeab5c52015-07-27 17:03:11 -0700642 if (mListsFragment.getCurrentTabIndex()
643 == ListsFragment.TAB_INDEX_ALL_CONTACTS && !mInRegularSearch) {
Andrew Lee247df6e2015-05-12 19:09:00 -0700644 DialerUtils.startActivityWithErrorToast(
645 this,
646 IntentUtil.getNewContactIntent(),
647 R.string.add_contact_not_available);
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700648 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700649 mInCallDialpadUp = false;
650 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700651 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700652 break;
Yorke Leec3766332013-07-31 11:13:16 -0700653 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800654 try {
655 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
656 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
657 } catch (ActivityNotFoundException e) {
658 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
659 Toast.LENGTH_SHORT).show();
660 }
Yorke Leec3766332013-07-31 11:13:16 -0700661 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700662 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700663 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700664 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700665 default: {
666 Log.wtf(TAG, "Unexpected onClick event from " + view);
667 break;
668 }
669 }
670 }
671
Yorke Leec3766332013-07-31 11:13:16 -0700672 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700673 public boolean onMenuItemClick(MenuItem item) {
Jay Shrauner119b0652015-09-17 12:00:18 -0700674 if (!isVisible()) {
675 return true;
676 }
677
Yorke Lee86e21f72014-04-02 16:49:38 -0700678 switch (item.getItemId()) {
679 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700680 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
681 // CONTENT_TYPE, so that we always open our call log from our dialer
682 final Intent intent = new Intent(this, CallLogActivity.class);
683 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700684 break;
685 case R.id.menu_add_contact:
Andrew Lee247df6e2015-05-12 19:09:00 -0700686 DialerUtils.startActivityWithErrorToast(
687 this,
688 IntentUtil.getNewContactIntent(),
689 R.string.add_contact_not_available);
Yorke Lee86e21f72014-04-02 16:49:38 -0700690 break;
691 case R.id.menu_import_export:
692 // We hard-code the "contactsAreAvailable" argument because doing it properly would
693 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
694 // now in Dialtacts for (potential) performance reasons. Compare with how it is
695 // done in {@link PeopleActivity}.
696 ImportExportDialogFragment.show(getFragmentManager(), true,
697 DialtactsActivity.class);
698 return true;
699 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700700 ClearFrequentsDialog.show(getFragmentManager());
701 return true;
702 case R.id.menu_call_settings:
703 handleMenuSettings();
704 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700705 }
706 return false;
707 }
708
709 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700710 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
711 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
712 if (resultCode == RESULT_OK) {
713 final ArrayList<String> matches = data.getStringArrayListExtra(
714 RecognizerIntent.EXTRA_RESULTS);
715 if (matches.size() > 0) {
716 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800717 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700718 } else {
719 Log.e(TAG, "Voice search - nothing heard");
720 }
721 } else {
722 Log.e(TAG, "Voice search failed");
723 }
724 }
725 super.onActivityResult(requestCode, resultCode, data);
726 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700727
Andrew Lee2a58ab82014-05-15 02:16:04 -0700728 /**
Nancy Chenea7c0b82015-08-31 17:18:41 -0700729 * Update the number of unread voicemails (potentially other tabs) displayed next to the tab
730 * icon.
731 */
732 public void updateTabUnreadCounts() {
733 mListsFragment.updateTabUnreadCounts();
734 }
735
736 /**
Andrew Lee2a58ab82014-05-15 02:16:04 -0700737 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
738 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
739 * @see #onDialpadShown
740 */
Yorke Leec3766332013-07-31 11:13:16 -0700741 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700742 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700743 return;
744 }
745 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800746
Yorke Lee7ee5c422014-11-04 10:29:32 -0800747 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700748
Chiao Cheng94b10b52012-08-17 16:59:12 -0700749 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800750 if (mDialpadFragment == null) {
751 mDialpadFragment = new DialpadFragment();
752 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
753 } else {
754 ft.show(mDialpadFragment);
755 }
756
757 mDialpadFragment.setAnimate(animate);
758 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700759 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700760
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700761 if (animate) {
762 mFloatingActionButtonController.scaleOut();
763 } else {
764 mFloatingActionButtonController.setVisible(false);
Yorke Lee81a313d2015-06-01 14:53:59 -0700765 maybeEnterSearchUi();
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700766 }
Yorke Lee5253be02014-05-21 18:50:03 -0700767 mActionBarController.onDialpadUp();
768
Yorke Lee6c450eb2015-05-08 09:45:47 -0700769 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700770 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700771
Andrew Lee2a58ab82014-05-15 02:16:04 -0700772 /**
773 * Callback from child DialpadFragment when the dialpad is shown.
774 */
775 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800776 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700777 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700778 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700779 } else {
780 mDialpadFragment.setYFraction(0);
781 }
782
Andrew Lee2a58ab82014-05-15 02:16:04 -0700783 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700784 }
785
786 /**
787 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
788 * a callback after the hide animation ends.
789 * @see #commitDialpadFragmentHide
790 */
Yorke Leeca195042013-09-25 16:29:38 -0700791 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Lee791b4882015-05-15 10:52:27 -0700792 if (mDialpadFragment == null || mDialpadFragment.getView() == null) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700793 return;
794 }
Yorke Leef6673d32013-08-23 15:34:17 -0700795 if (clearDialpad) {
796 mDialpadFragment.clearDialpad();
797 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700798 if (!mIsDialpadShown) {
799 return;
Yorke Leec3766332013-07-31 11:13:16 -0700800 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700801 mIsDialpadShown = false;
802 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800803 mListsFragment.setUserVisibleHint(true);
804 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700805
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700806 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700807
Andrew Lee6324f702015-06-16 14:45:58 -0700808 mFloatingActionButtonController.align(getFabAlignment(), animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700809 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700810 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700811 } else {
812 commitDialpadFragmentHide();
813 }
814
Yorke Lee5253be02014-05-21 18:50:03 -0700815 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700816
Andrew Leed4cde832014-05-16 15:56:48 -0700817 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700818 if (TextUtils.isEmpty(mSearchQuery)) {
819 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700820 }
Andrew Leed4cde832014-05-16 15:56:48 -0700821 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700822 }
823
824 /**
825 * Finishes hiding the dialpad fragment after any animations are completed.
826 */
827 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800828 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700829 final FragmentTransaction ft = getFragmentManager().beginTransaction();
830 ft.hide(mDialpadFragment);
831 ft.commit();
832 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700833 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700834 }
835
Andrew Lee2a58ab82014-05-15 02:16:04 -0700836 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700837 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700838 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700839 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700840 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700841 fragment = mRegularSearchFragment;
842 }
843 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700844 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700845 }
846 }
847
Yorke Lee5253be02014-05-21 18:50:03 -0700848 @Override
849 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700850 return mInDialpadSearch || mInRegularSearch;
851 }
852
Yorke Lee5253be02014-05-21 18:50:03 -0700853 @Override
854 public boolean hasSearchQuery() {
855 return !TextUtils.isEmpty(mSearchQuery);
856 }
857
858 @Override
859 public boolean shouldShowActionBar() {
860 return mListsFragment.shouldShowActionBar();
861 }
862
Yorke Leeb207f8a2013-08-29 18:51:06 -0700863 private void setNotInSearchUi() {
864 mInDialpadSearch = false;
865 mInRegularSearch = false;
866 }
867
Yorke Lee311969c2013-08-26 06:31:11 -0700868 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700869 if (mIsDialpadShown) {
870 hideDialpadFragment(false, true);
871 } else {
872 exitSearchUi();
873 }
Yorke Lee311969c2013-08-26 06:31:11 -0700874 }
875
Yorke Lee53a22302014-04-29 18:13:46 -0700876 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700877 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
878 if (canIntentBeHandled(voiceIntent)) {
879 mVoiceSearchButton.setVisibility(View.VISIBLE);
880 mVoiceSearchButton.setOnClickListener(this);
881 } else {
882 mVoiceSearchButton.setVisibility(View.GONE);
883 }
884 }
885
Anne Rong35208002015-07-22 16:27:37 -0700886 protected int getSearchBoxHint () {
887 return R.string.dialer_hint_find_contact;
888 }
889
890 /**
891 * Sets the hint text for the contacts search box
892 */
893 private void setSearchBoxHint() {
Nancy Chen44898ad2015-08-13 12:03:47 -0700894 SearchEditTextLayout searchEditTextLayout = (SearchEditTextLayout) getSupportActionBar()
Anne Rong35208002015-07-22 16:27:37 -0700895 .getCustomView().findViewById(R.id.search_view_container);
896 ((TextView) searchEditTextLayout.findViewById(R.id.search_box_start_search))
897 .setHint(getSearchBoxHint());
898 }
899
Andrew Lee467de3d2015-03-17 15:47:24 -0700900 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700901 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
902 popupMenu.inflate(R.menu.dialtacts_options);
903 popupMenu.setOnMenuItemClickListener(this);
904 return popupMenu;
905 }
906
Yorke Lee86e21f72014-04-02 16:49:38 -0700907 @Override
908 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700909 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700910 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700911 mPendingSearchViewQuery = null;
912 }
Yorke Leec16ea5a2015-05-19 15:51:01 -0700913 if (mActionBarController != null) {
914 mActionBarController.restoreActionBarOffset();
915 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700916 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700917 }
918
Chiao Cheng94b10b52012-08-17 16:59:12 -0700919 /**
920 * Returns true if the intent is due to hitting the green send key (hardware call button:
921 * KEYCODE_CALL) while in a call.
922 *
923 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700924 * @return true if the intent is due to hitting the green send key while in a call
925 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700926 private boolean isSendKeyWhileInCall(Intent intent) {
927 // If there is a call in progress and the user launched the dialer by hitting the call
928 // button, go straight to the in-call screen.
929 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700930
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700931 if (callKey) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700932 getTelecomManager().showInCallScreen(false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700933 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700934 }
935
936 return false;
937 }
938
939 /**
940 * Sets the current tab based on the intent's request type
941 *
942 * @param intent Intent that contains information about which tab should be selected
943 */
Yorke Leec3766332013-07-31 11:13:16 -0700944 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700945 // 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 -0700946 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700947 finish();
948 return;
949 }
950
Yorke Leed77017d2015-08-31 16:19:43 -0700951 final boolean showDialpadChooser = phoneIsInUse() && !DialpadFragment.isAddCallMode(intent);
952 if (showDialpadChooser || (intent.getData() != null && isDialIntent(intent))) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800953 showDialpadFragment(false);
954 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leed77017d2015-08-31 16:19:43 -0700955 if (showDialpadChooser && !mDialpadFragment.isVisible()) {
Yorke Leed5c512a2015-01-30 14:42:53 -0800956 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700957 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700958 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700959 }
960
961 @Override
962 public void onNewIntent(Intent newIntent) {
963 setIntent(newIntent);
Yorke Lee24d034f2015-07-01 12:34:50 -0700964
Nancy Chen199b7f02014-11-05 15:03:00 -0800965 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700966 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700967
Chiao Cheng94b10b52012-08-17 16:59:12 -0700968 invalidateOptionsMenu();
969 }
970
971 /** Returns true if the given intent contains a phone number to populate the dialer with */
972 private boolean isDialIntent(Intent intent) {
973 final String action = intent.getAction();
974 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
975 return true;
976 }
977 if (Intent.ACTION_VIEW.equals(action)) {
978 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700979 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700980 return true;
981 }
982 }
983 return false;
984 }
985
986 /**
Yorke Leec3766332013-07-31 11:13:16 -0700987 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700988 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700989 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700990 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700991 // Weird race condition where fragment is doing work after the activity is destroyed
992 // due to talkback being on (b/10209937). Just return since we can't do any
993 // constructive here.
994 return;
995 }
996
Yorke Leeef2b7382013-08-09 17:39:25 -0700997 if (DEBUG) {
998 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
999 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001000
Yorke Leec3766332013-07-31 11:13:16 -07001001 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001002 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001003 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001004 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -07001005 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -07001006 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001007
Yorke Leeb207f8a2013-08-29 18:51:06 -07001008 final String tag;
1009 if (smartDialSearch) {
1010 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
1011 } else {
1012 tag = TAG_REGULAR_SEARCH_FRAGMENT;
1013 }
1014 mInDialpadSearch = smartDialSearch;
1015 mInRegularSearch = !smartDialSearch;
1016
Andrew Lee6324f702015-06-16 14:45:58 -07001017 mFloatingActionButtonController.scaleOut();
1018
Andrew Lee752956e2014-05-15 11:43:38 -07001019 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001020 if (animate) {
1021 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
1022 } else {
1023 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
1024 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001025 if (fragment == null) {
1026 if (smartDialSearch) {
1027 fragment = new SmartDialSearchFragment();
1028 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -07001029 fragment = new RegularSearchFragment();
Andrew Lee6324f702015-06-16 14:45:58 -07001030 fragment.setOnTouchListener(new View.OnTouchListener() {
1031 @Override
1032 public boolean onTouch(View v, MotionEvent event) {
1033 // Show the FAB when the user touches the lists fragment and the soft
1034 // keyboard is hidden.
1035 showFabInSearchUi();
1036 return false;
1037 }
1038 });
Yorke Leeef2b7382013-08-09 17:39:25 -07001039 }
Andrew Leeb1903842014-05-15 16:11:24 -07001040 transaction.add(R.id.dialtacts_frame, fragment, tag);
1041 } else {
1042 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -07001043 }
Yorke Lee86e21f72014-04-02 16:49:38 -07001044 // DialtactsActivity will provide the options menu
1045 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -07001046 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -07001047 if (!smartDialSearch) {
1048 fragment.setQueryString(query, false /* delaySelection */);
1049 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001050 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -07001051
Yorke Lee6c450eb2015-05-08 09:45:47 -07001052 if (animate) {
1053 mListsFragment.getView().animate().alpha(0).withLayer();
1054 }
Yorke Lee7ee5c422014-11-04 10:29:32 -08001055 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001056 }
1057
1058 /**
Yorke Leec3766332013-07-31 11:13:16 -07001059 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -07001060 */
Yorke Leec3766332013-07-31 11:13:16 -07001061 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -07001062 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -07001063 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -07001064 return;
1065 }
Andrew Leeb1903842014-05-15 16:11:24 -07001066
Andrew Leeb1903842014-05-15 16:11:24 -07001067 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -08001068
1069 if (mDialpadFragment != null) {
1070 mDialpadFragment.clearDialpad();
1071 }
1072
Yorke Leeb207f8a2013-08-29 18:51:06 -07001073 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -07001074
Andrew Lee6324f702015-06-16 14:45:58 -07001075 // Restore the FAB for the lists fragment.
1076 if (getFabAlignment() != FloatingActionButtonController.ALIGN_END) {
1077 mFloatingActionButtonController.setVisible(false);
1078 }
1079 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1080 onPageScrolled(mListsFragment.getCurrentTabIndex(), 0 /* offset */, 0 /* pixelOffset */);
1081 onPageSelected(mListsFragment.getCurrentTabIndex());
1082
Andrew Leeb1903842014-05-15 16:11:24 -07001083 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -07001084 if (mSmartDialSearchFragment != null) {
1085 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -07001086 }
Andrew Leeb1903842014-05-15 16:11:24 -07001087 if (mRegularSearchFragment != null) {
1088 transaction.remove(mRegularSearchFragment);
1089 }
1090 transaction.commit();
1091
1092 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -08001093
1094 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -08001095 // If the dialpad fragment wasn't previously visible, then send a screen view because
1096 // we are exiting regular search. Otherwise, the screen view will be sent by
1097 // {@link #hideDialpadFragment}.
1098 mListsFragment.sendScreenViewForCurrentPosition();
1099 mListsFragment.setUserVisibleHint(true);
1100 }
1101
Yorke Lee5253be02014-05-21 18:50:03 -07001102 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001103 }
1104
Chiao Cheng94b10b52012-08-17 16:59:12 -07001105 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001106 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001107 if (mStateSaved) {
1108 return;
1109 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001110 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001111 if (TextUtils.isEmpty(mSearchQuery) ||
1112 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1113 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001114 exitSearchUi();
1115 }
Yorke Leef6673d32013-08-23 15:34:17 -07001116 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001117 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001118 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001119 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001120 } else {
1121 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001122 }
Yorke Leec3766332013-07-31 11:13:16 -07001123 }
1124
Yorke Lee81a313d2015-06-01 14:53:59 -07001125 private void maybeEnterSearchUi() {
1126 if (!isInSearchUi()) {
1127 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
1128 }
1129 }
1130
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001131 /**
1132 * @return True if the search UI was exited, false otherwise
1133 */
1134 private boolean maybeExitSearchUi() {
1135 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1136 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001137 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001138 return true;
1139 }
1140 return false;
1141 }
1142
Andrew Lee6324f702015-06-16 14:45:58 -07001143 private void showFabInSearchUi() {
1144 mFloatingActionButtonController.changeIcon(
1145 getResources().getDrawable(R.drawable.fab_ic_dial),
1146 getResources().getString(R.string.action_menu_dialpad_button));
1147 mFloatingActionButtonController.align(getFabAlignment(), false /* animate */);
1148 mFloatingActionButtonController.scaleIn(FAB_SCALE_IN_DELAY_MS);
1149 }
1150
Yorke Leec3766332013-07-31 11:13:16 -07001151 @Override
1152 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001153 if (mSmartDialSearchFragment != null) {
1154 mSmartDialSearchFragment.setAddToContactNumber(query);
1155 }
Yorke Leec3766332013-07-31 11:13:16 -07001156 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1157 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001158
1159 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001160 if (DEBUG) {
1161 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1162 }
1163 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1164 // This callback can happen if the dialpad fragment is recreated because of
1165 // activity destruction. In that case, don't update the search view because
1166 // that would bring the user back to the search fragment regardless of the
1167 // previous state of the application. Instead, just return here and let the
1168 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001169 if (!TextUtils.isEmpty(normalizedQuery)) {
1170 mPendingSearchViewQuery = normalizedQuery;
1171 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001172 return;
1173 }
Yorke Lee53a22302014-04-29 18:13:46 -07001174 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001175 }
Santos Cordon83131712015-04-16 21:25:35 -07001176
1177 try {
1178 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
1179 mDialpadFragment.process_quote_emergency_unquote(normalizedQuery);
1180 }
1181 } catch (Exception ignored) {
1182 // Skip any exceptions for this piece of code
1183 }
Yorke Lee575ae382015-07-16 11:36:07 -07001184 }
Santos Cordon83131712015-04-16 21:25:35 -07001185
Yorke Lee575ae382015-07-16 11:36:07 -07001186 @Override
1187 public boolean onDialpadSpacerTouchWithEmptyQuery() {
1188 if (mInDialpadSearch && mSmartDialSearchFragment != null
1189 && !mSmartDialSearchFragment.isShowingPermissionRequest()) {
1190 hideDialpadFragment(true /* animate */, true /* clearDialpad */);
1191 return true;
1192 }
1193 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -07001194 }
Yorke Leec3766332013-07-31 11:13:16 -07001195
1196 @Override
1197 public void onListFragmentScrollStateChange(int scrollState) {
1198 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001199 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001200 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001201 }
1202 }
1203
1204 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001205 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001206 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001207 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1208 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001209 }
1210
Yorke Leec3766332013-07-31 11:13:16 -07001211 private boolean phoneIsInUse() {
Yorke Lee827a90f2015-09-17 18:10:58 -07001212 return TelecomUtil.isInCall(this);
Yorke Leec3766332013-07-31 11:13:16 -07001213 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001214
Yorke Leeda0f9042013-12-05 14:25:51 -08001215 private boolean canIntentBeHandled(Intent intent) {
1216 final PackageManager packageManager = getPackageManager();
1217 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1218 PackageManager.MATCH_DEFAULT_ONLY);
1219 return resolveInfo != null && resolveInfo.size() > 0;
1220 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001221
Yorke Lee86e21f72014-04-02 16:49:38 -07001222 /**
1223 * Called when the user has long-pressed a contact tile to start a drag operation.
1224 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001225 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001226 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001227 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001228 }
1229
1230 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001231 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1232 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001233
Yorke Lee86e21f72014-04-02 16:49:38 -07001234 /**
1235 * Called when the user has released a contact tile after long-pressing it.
1236 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001237 @Override
1238 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001239 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001240 }
1241
1242 @Override
1243 public void onDroppedOnRemove() {}
1244
1245 /**
Yorke Leed999b712014-04-23 15:10:58 -07001246 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001247 * once it has been attached to the activity.
1248 */
1249 @Override
1250 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001251 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001252 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001253 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001254
Yorke Lee38019af2015-07-14 17:05:38 -07001255 /**
1256 * Implemented to satisfy {@link SpeedDialFragment.HostInterface}
1257 */
1258 @Override
1259 public void showAllContactsTab() {
1260 if (mListsFragment != null) {
1261 mListsFragment.showTab(ListsFragment.TAB_INDEX_ALL_CONTACTS);
1262 }
1263 }
1264
1265 /**
1266 * Implemented to satisfy {@link CallLogFragment.HostInterface}
1267 */
1268 @Override
1269 public void showDialpad() {
1270 showDialpadFragment(true);
1271 }
1272
Yorke Leee00c9fe2014-04-12 12:42:06 -07001273 @Override
Yorke Leec8d6e162015-09-14 18:43:27 -07001274 public void onPickPhoneNumberAction(Uri dataUri, int callInitiationType) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001275 mClearSearchOnPause = true;
Yorke Leec8d6e162015-09-14 18:43:27 -07001276 PhoneNumberInteraction.startInteractionForPhoneCall(
1277 DialtactsActivity.this, dataUri, callInitiationType);
Yorke Leee00c9fe2014-04-12 12:42:06 -07001278 }
1279
1280 @Override
Yorke Leec8d6e162015-09-14 18:43:27 -07001281 public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall,
1282 int callInitiationType) {
Yorke Lee24d034f2015-07-01 12:34:50 -07001283 if (phoneNumber == null) {
1284 // Invalid phone number, but let the call go through so that InCallUI can show
1285 // an error message.
1286 phoneNumber = "";
1287 }
Yorke Leee3a2d132015-09-14 16:52:08 -07001288
1289 final Intent intent = new CallIntentBuilder(phoneNumber)
1290 .setIsVideoCall(isVideoCall)
Yorke Leec8d6e162015-09-14 18:43:27 -07001291 .setCallInitiationType(callInitiationType)
Yorke Leee3a2d132015-09-14 16:52:08 -07001292 .build();
1293
Yorke Lee7d20f822014-06-19 17:09:33 -07001294 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001295 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001296 }
1297
1298 @Override
1299 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001300 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001301 }
1302
1303 @Override
1304 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001305 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001306 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001307
Yorke Leecc4660d2014-04-24 11:22:57 -07001308 @Override
1309 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001310 int tabIndex = mListsFragment.getCurrentTabIndex();
1311
Andrew Lee4de59fb2015-08-13 15:20:08 -07001312 // Scroll the button from center to end when moving from the Speed Dial to Call History tab.
1313 // In RTL, scroll when the current tab is Call History instead, since the order of the tabs
1314 // is reversed and the ViewPager returns the left tab position during scroll.
Andrew Lee432d4b52015-05-20 16:52:00 -07001315 boolean isRtl = DialerUtils.isRtl();
1316 if (!isRtl && tabIndex == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
1317 mFloatingActionButtonController.onPageScrolled(positionOffset);
Andrew Lee4de59fb2015-08-13 15:20:08 -07001318 } else if (isRtl && tabIndex == ListsFragment.TAB_INDEX_HISTORY && !mIsLandscape) {
Andrew Lee432d4b52015-05-20 16:52:00 -07001319 mFloatingActionButtonController.onPageScrolled(1 - positionOffset);
1320 } else if (tabIndex != ListsFragment.TAB_INDEX_SPEED_DIAL) {
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001321 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001322 }
Andrew Lee432d4b52015-05-20 16:52:00 -07001323 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001324
Andrew Lee432d4b52015-05-20 16:52:00 -07001325 @Override
1326 public void onPageSelected(int position) {
1327 int tabIndex = mListsFragment.getCurrentTabIndex();
1328 if (tabIndex == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001329 mFloatingActionButtonController.changeIcon(
1330 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1331 getResources().getString(R.string.search_shortcut_create_new_contact));
1332 } else {
1333 mFloatingActionButtonController.changeIcon(
1334 getResources().getDrawable(R.drawable.fab_ic_dial),
1335 getResources().getString(R.string.action_menu_dialpad_button));
1336 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001337 }
1338
1339 @Override
Yorke Leecc4660d2014-04-24 11:22:57 -07001340 public void onPageScrollStateChanged(int state) {
1341 }
1342
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001343 private TelecomManager getTelecomManager() {
1344 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordond15629d2014-07-07 15:15:29 -07001345 }
1346
Yorke Lee5253be02014-05-21 18:50:03 -07001347 @Override
1348 public boolean isActionBarShowing() {
1349 return mActionBarController.isActionBarShowing();
1350 }
1351
Yorke Leec98a5bb2014-10-28 16:12:05 -07001352 @Override
1353 public ActionBarController getActionBarController() {
1354 return mActionBarController;
1355 }
1356
Yorke Lee6bc67162015-06-27 14:03:25 -07001357 @Override
Andrew Lee9da8fb42014-06-03 14:03:09 -07001358 public boolean isDialpadShown() {
1359 return mIsDialpadShown;
1360 }
1361
Yorke Lee5253be02014-05-21 18:50:03 -07001362 @Override
Yorke Lee6bc67162015-06-27 14:03:25 -07001363 public int getDialpadHeight() {
1364 if (mDialpadFragment != null) {
1365 return mDialpadFragment.getDialpadHeight();
1366 }
1367 return 0;
1368 }
1369
1370 @Override
Yorke Lee5253be02014-05-21 18:50:03 -07001371 public int getActionBarHideOffset() {
Nancy Chen44898ad2015-08-13 12:03:47 -07001372 return getSupportActionBar().getHideOffset();
Yorke Lee5253be02014-05-21 18:50:03 -07001373 }
1374
1375 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001376 public void setActionBarHideOffset(int offset) {
Nancy Chen44898ad2015-08-13 12:03:47 -07001377 getSupportActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001378 }
1379
1380 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001381 public int getActionBarHeight() {
1382 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001383 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001384
Andrew Lee6324f702015-06-16 14:45:58 -07001385 private int getFabAlignment() {
1386 if (!mIsLandscape && !isInSearchUi() &&
1387 mListsFragment.getCurrentTabIndex() == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1388 return FloatingActionButtonController.ALIGN_MIDDLE;
1389 }
1390 return FloatingActionButtonController.ALIGN_END;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001391 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001392}