blob: cccc06ea0fb2265f987cac9275eb50dd90463793 [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;
Chiao Cheng94b10b52012-08-17 16:59:12 -070032import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070033import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070034import android.speech.RecognizerIntent;
Yorke Leecc4660d2014-04-24 11:22:57 -070035import android.support.v4.view.ViewPager;
Tyler Gunn9dc924c2014-09-12 09:33:50 -070036import android.telecom.PhoneAccount;
37import android.telecom.TelecomManager;
Yorke Leec3766332013-07-31 11:13:16 -070038import android.telephony.TelephonyManager;
Yorke Lee53a22302014-04-29 18:13:46 -070039import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070040import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070041import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070042import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070043import android.view.DragEvent;
Yorke Lee74edcdc2014-07-11 16:15:08 -070044import android.view.Gravity;
Andrew Leeb1903842014-05-15 16:11:24 -070045import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070048import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070050import android.view.View.OnDragListener;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070051import android.view.View.OnTouchListener;
Ihab Awad9134e252014-07-09 12:32:52 -070052import android.view.ViewTreeObserver;
Andrew Lee2a58ab82014-05-15 02:16:04 -070053import android.view.animation.Animation;
Andrew Lee2a58ab82014-05-15 02:16:04 -070054import android.view.animation.AnimationUtils;
Yorke Leec3766332013-07-31 11:13:16 -070055import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070056import android.widget.EditText;
Yorke Lee28aab272014-06-11 10:24:19 -070057import android.widget.FrameLayout;
Andrew Leea73e1892014-05-13 13:21:16 -070058import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070059import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080060import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070061
Brian Attwellc311af12014-10-22 18:28:58 -070062import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070063import com.android.contacts.common.dialog.ClearFrequentsDialog;
64import com.android.contacts.common.interactions.ImportExportDialogFragment;
Nancy Chen0d3a7392014-08-12 14:44:53 -070065import com.android.contacts.common.interactions.TouchPointManager;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080066import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -070067import com.android.contacts.common.widget.FloatingActionButtonController;
Brian Attwellbeab3bb2014-10-27 12:24:49 -070068import com.android.contacts.commonbind.analytics.AnalyticsUtil;
Yorke Leec3766332013-07-31 11:13:16 -070069import com.android.dialer.calllog.CallLogActivity;
Yorke 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;
Yorke Leeb82e8da2015-04-18 14:37:50 -070085import com.android.dialer.util.CallIntentUtil;
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;
Yorke Lee11ca39e2014-05-19 20:31:37 -070089import com.android.dialer.widget.SearchEditTextLayout.OnBackButtonClickedListener;
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 Lee6a1461a2014-04-21 16:27:14 -0700105 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700106 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700107 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700108 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700109 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700110 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700111 ViewPager.OnPageChangeListener,
112 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 private static final String TAG = "DialtactsActivity";
114
Ihab Awad526c0b82014-02-27 12:55:36 -0800115 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700116
Yorke Leef74011e2013-08-02 11:30:30 -0700117 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
118
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119 /** @see #getCallOrigin() */
120 private static final String CALL_ORIGIN_DIALTACTS =
121 "com.android.dialer.DialtactsActivity";
122
Yorke Leeb207f8a2013-08-29 18:51:06 -0700123 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
124 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700125 private static final String KEY_SEARCH_QUERY = "search_query";
126 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700127 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700128
Yorke Leec3766332013-07-31 11:13:16 -0700129 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
130 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
131 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
132 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700133
Chiao Cheng94b10b52012-08-17 16:59:12 -0700134 /**
135 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
136 */
137 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
138
Yorke Leec3766332013-07-31 11:13:16 -0700139 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700140
Yorke Leef614f6f2014-10-03 10:44:19 -0700141 private FrameLayout mParentLayout;
Andrew Lee0c667702014-05-12 14:31:45 -0700142
Yorke Leec3766332013-07-31 11:13:16 -0700143 /**
Yorke Leec3766332013-07-31 11:13:16 -0700144 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700145 */
Evan Charlton72d66252014-11-07 16:23:47 -0800146 protected DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700147
148 /**
149 * Fragment for searching phone numbers using the alphanumeric keyboard.
150 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700151 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700152
153 /**
154 * Fragment for searching phone numbers using the dialpad.
155 */
156 private SmartDialSearchFragment mSmartDialSearchFragment;
157
Yorke Leee00c9fe2014-04-12 12:42:06 -0700158 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700159 * Animation that slides in.
160 */
161 private Animation mSlideIn;
162
163 /**
164 * Animation that slides out.
165 */
166 private Animation mSlideOut;
167
Yorke Lee6c450eb2015-05-08 09:45:47 -0700168 AnimationListenerAdapter mSlideInListener = new AnimationListenerAdapter() {
169 @Override
170 public void onAnimationEnd(Animation animation) {
171 if (!isInSearchUi()) {
172 enterSearchUi(true /* isSmartDial */, mSearchQuery, false);
173 }
174 }
175 };
176
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700177 /**
178 * Listener for after slide out animation completes on dialer fragment.
179 */
180 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
181 @Override
182 public void onAnimationEnd(Animation animation) {
183 commitDialpadFragmentHide();
184 }
185 };
186
187 /**
Yorke Leee00c9fe2014-04-12 12:42:06 -0700188 * Fragment containing the speed dial list, recents list, and all contacts list.
189 */
190 private ListsFragment mListsFragment;
191
Yorke Lee19e68ca2014-10-28 11:51:40 -0700192 /**
193 * Tracks whether onSaveInstanceState has been called. If true, no fragment transactions can
194 * be commited.
195 */
196 private boolean mStateSaved;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800197 private boolean mIsRestarting;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700198 private boolean mInDialpadSearch;
199 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700200 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700201 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700202 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700203
Yorke Leeef2b7382013-08-09 17:39:25 -0700204 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700205 * Whether or not the device is in landscape orientation.
206 */
207 private boolean mIsLandscape;
208
209 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700210 * True if the dialpad is only temporarily showing due to being in call
211 */
212 private boolean mInCallDialpadUp;
213
214 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700215 * True when this activity has been launched for the first time.
216 */
Yorke Lee98702de2013-08-06 12:03:32 -0700217 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700218
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700219 /**
220 * Search query to be applied to the SearchView in the ActionBar once
221 * onCreateOptionsMenu has been called.
222 */
223 private String mPendingSearchViewQuery;
224
Yorke Lee74edcdc2014-07-11 16:15:08 -0700225 private PopupMenu mOverflowMenu;
Yorke Lee53a22302014-04-29 18:13:46 -0700226 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700227 private View mVoiceSearchButton;
Andrew Leee74a10e2014-05-16 14:31:40 -0700228
Yorke Leeef2b7382013-08-09 17:39:25 -0700229 private String mSearchQuery;
230
Yorke Leefce269a2013-08-12 11:56:04 -0700231 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700232 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700233 private ActionBarController mActionBarController;
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700234
Sai Cheemalapati41460652014-06-02 21:05:39 -0700235 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700236
Andrew Lee2423a2f2014-05-29 14:14:45 -0700237 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700238
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800239 /**
240 * The text returned from a voice search query. Set in {@link #onActivityResult} and used in
241 * {@link #onResume()} to populate the search box.
242 */
243 private String mVoiceSearchQuery;
244
Andrew Lee467de3d2015-03-17 15:47:24 -0700245 protected class OptionsPopupMenu extends PopupMenu {
Andrew Leee74a10e2014-05-16 14:31:40 -0700246 public OptionsPopupMenu(Context context, View anchor) {
Yorke Lee74edcdc2014-07-11 16:15:08 -0700247 super(context, anchor, Gravity.END);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700248 }
249
250 @Override
251 public void show() {
252 final Menu menu = getMenu();
253 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700254 clearFrequents.setVisible(mListsFragment != null &&
255 mListsFragment.getSpeedDialFragment() != null &&
256 mListsFragment.getSpeedDialFragment().hasFrequents());
Yorke Leee00c9fe2014-04-12 12:42:06 -0700257 super.show();
258 }
259 }
260
Chiao Cheng94b10b52012-08-17 16:59:12 -0700261 /**
Yorke Lee28266192014-05-01 10:05:52 -0700262 * Listener that listens to drag events and sends their x and y coordinates to a
263 * {@link DragDropController}.
264 */
265 private class LayoutOnDragListener implements OnDragListener {
266 @Override
267 public boolean onDrag(View v, DragEvent event) {
268 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Andrew Lee18963442014-06-06 17:20:13 -0700269 mDragDropController.handleDragHovered(v, (int) event.getX(), (int) event.getY());
Yorke Lee28266192014-05-01 10:05:52 -0700270 }
271 return true;
272 }
273 }
274
275 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700276 * Listener used to send search queries to the phone search fragment.
277 */
Yorke Lee53a22302014-04-29 18:13:46 -0700278 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700279 @Override
280 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
281 }
Yorke Leec3766332013-07-31 11:13:16 -0700282
Andrew Lee99a570c2014-05-15 14:18:50 -0700283 @Override
284 public void onTextChanged(CharSequence s, int start, int before, int count) {
285 final String newText = s.toString();
286 if (newText.equals(mSearchQuery)) {
287 // If the query hasn't changed (perhaps due to activity being destroyed
288 // and restored, or user launching the same DIAL intent twice), then there is
289 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700290 return;
291 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700292 if (DEBUG) {
293 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700294 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700295 }
Yorke Lee710709d2014-05-29 07:18:42 -0700296 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700297
Andrew Lee90374a72014-05-16 15:01:09 -0700298 // Show search fragment only when the query string is changed to non-empty text.
299 if (!TextUtils.isEmpty(newText)) {
300 // Call enterSearchUi only if we are switching search modes, or showing a search
301 // fragment for the first time.
302 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
303 (!mIsDialpadShown && mInRegularSearch);
304 if (!sameSearchMode) {
Yorke Lee6c450eb2015-05-08 09:45:47 -0700305 enterSearchUi(mIsDialpadShown, mSearchQuery, true /* animate */);
Andrew Lee90374a72014-05-16 15:01:09 -0700306 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700307 }
308
Andrew Leea8515422014-06-13 16:53:54 -0700309 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700310 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Leea8515422014-06-13 16:53:54 -0700311 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Leeb1903842014-05-15 16:11:24 -0700312 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700313 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700314 }
315
316 @Override
317 public void afterTextChanged(Editable s) {
318 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700319 };
320
Andrew Leeb1903842014-05-15 16:11:24 -0700321
322 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700323 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700324 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700325 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700326 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700327 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700328 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700329 mActionBarController.onSearchBoxTapped();
Yorke Lee6c450eb2015-05-08 09:45:47 -0700330 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString(),
331 true /* animate */);
Andrew Leeb1903842014-05-15 16:11:24 -0700332 }
Andrew Leeb1903842014-05-15 16:11:24 -0700333 }
334 };
335
336 /**
337 * If the search term is empty and the user closes the soft keyboard, close the search UI.
338 */
339 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
340 @Override
341 public boolean onKey(View v, int keyCode, KeyEvent event) {
342 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
343 TextUtils.isEmpty(mSearchView.getText().toString())) {
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700344 maybeExitSearchUi();
Andrew Leeb1903842014-05-15 16:11:24 -0700345 }
346 return false;
347 }
348 };
349
Chiao Cheng94b10b52012-08-17 16:59:12 -0700350 @Override
Nancy Chen49db1ad2014-08-18 20:10:17 -0700351 public boolean dispatchTouchEvent(MotionEvent ev) {
352 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
353 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
354 }
355 return super.dispatchTouchEvent(ev);
356
357 }
358
359 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700360 protected void onCreate(Bundle savedInstanceState) {
Yorke Leef5554ce2015-01-27 14:46:02 -0800361 Trace.beginSection(TAG + " onCreate");
Yorke Leec3766332013-07-31 11:13:16 -0700362 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700363 mFirstLaunch = true;
364
Andrew Lee2423a2f2014-05-29 14:14:45 -0700365 final Resources resources = getResources();
Nancy Chenb8170de2014-07-28 10:41:08 -0700366 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height_large);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700367
Yorke Leef5554ce2015-01-27 14:46:02 -0800368 Trace.beginSection(TAG + " setContentView");
Yorke Lee8898cd02013-08-08 10:24:27 -0700369 setContentView(R.layout.dialtacts_activity);
Yorke Leef5554ce2015-01-27 14:46:02 -0800370 Trace.endSection();
Yorke Lee9e91bb02014-03-05 17:50:50 -0800371 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700372
Yorke Leef5554ce2015-01-27 14:46:02 -0800373 Trace.beginSection(TAG + " setup Views");
Yorke Lee53a22302014-04-29 18:13:46 -0700374 final ActionBar actionBar = getActionBar();
375 actionBar.setCustomView(R.layout.search_edittext);
376 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700377 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700378
Andrew Lee04675a52014-06-05 18:36:20 -0700379 SearchEditTextLayout searchEditTextLayout =
Andrew Leee3f59a82015-02-26 12:06:35 -0800380 (SearchEditTextLayout) actionBar.getCustomView().findViewById(R.id.search_view_container);
Andrew Lee04675a52014-06-05 18:36:20 -0700381 searchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
382
Andrew Leee3f59a82015-02-26 12:06:35 -0800383 mActionBarController = new ActionBarController(this, searchEditTextLayout);
384
Andrew Lee04675a52014-06-05 18:36:20 -0700385 mSearchView = (EditText) searchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700386 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Lee04675a52014-06-05 18:36:20 -0700387 mVoiceSearchButton = searchEditTextLayout.findViewById(R.id.voice_search_button);
388 searchEditTextLayout.findViewById(R.id.search_magnifying_glass)
389 .setOnClickListener(mSearchViewOnClickListener);
390 searchEditTextLayout.findViewById(R.id.search_box_start_search)
391 .setOnClickListener(mSearchViewOnClickListener);
392 searchEditTextLayout.setOnBackButtonClickedListener(new OnBackButtonClickedListener() {
Yorke Lee11ca39e2014-05-19 20:31:37 -0700393 @Override
394 public void onBackButtonClicked() {
395 onBackPressed();
396 }
397 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700398
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700399 mIsLandscape = getResources().getConfiguration().orientation
Sai Cheemalapati41460652014-06-02 21:05:39 -0700400 == Configuration.ORIENTATION_LANDSCAPE;
Nancy Chen8e066292014-06-18 17:16:10 -0700401
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700402 final View floatingActionButtonContainer = findViewById(
403 R.id.floating_action_button_container);
Andrew Lee405a6e62014-08-20 15:02:16 -0700404 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
405 floatingActionButton.setOnClickListener(this);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700406 mFloatingActionButtonController = new FloatingActionButtonController(this,
Andrew Lee405a6e62014-08-20 15:02:16 -0700407 floatingActionButtonContainer, floatingActionButton);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700408
Andrew Lee04675a52014-06-05 18:36:20 -0700409 ImageButton optionsMenuButton =
410 (ImageButton) searchEditTextLayout.findViewById(R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700411 optionsMenuButton.setOnClickListener(this);
Yorke Lee74edcdc2014-07-11 16:15:08 -0700412 mOverflowMenu = buildOptionsMenu(searchEditTextLayout);
413 optionsMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Andrew Leee74a10e2014-05-16 14:31:40 -0700414
Yorke Leed5c512a2015-01-30 14:42:53 -0800415 // Add the favorites fragment but only if savedInstanceState is null. Otherwise the
416 // fragment manager is responsible for recreating it.
Yorke Leec3766332013-07-31 11:13:16 -0700417 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800418 getFragmentManager().beginTransaction()
Andrew Lee6a866242015-05-05 18:21:45 +0000419 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800420 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700421 } else {
422 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700423 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
424 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700425 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700426 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700427 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700428 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700429
Tyler Gunnbf7a95e2014-08-12 12:16:28 -0700430 final boolean isLayoutRtl = DialerUtils.isRtl();
431 if (mIsLandscape) {
432 mSlideIn = AnimationUtils.loadAnimation(this,
433 isLayoutRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
434 mSlideOut = AnimationUtils.loadAnimation(this,
435 isLayoutRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
436 } else {
437 mSlideIn = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
438 mSlideOut = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
439 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700440
Yorke Lee2fec47b2014-08-05 18:16:27 -0700441 mSlideIn.setInterpolator(AnimUtils.EASE_IN);
442 mSlideOut.setInterpolator(AnimUtils.EASE_OUT);
443
Yorke Lee6c450eb2015-05-08 09:45:47 -0700444 mSlideIn.setAnimationListener(mSlideInListener);
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700445 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700446
Yorke Leef614f6f2014-10-03 10:44:19 -0700447 mParentLayout = (FrameLayout) findViewById(R.id.dialtacts_mainlayout);
448 mParentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700449 floatingActionButtonContainer.getViewTreeObserver().addOnGlobalLayoutListener(
Sai Cheemalapati41460652014-06-02 21:05:39 -0700450 new ViewTreeObserver.OnGlobalLayoutListener() {
451 @Override
452 public void onGlobalLayout() {
Andrew Lee405a6e62014-08-20 15:02:16 -0700453 final ViewTreeObserver observer =
454 floatingActionButtonContainer.getViewTreeObserver();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700455 if (!observer.isAlive()) {
456 return;
457 }
458 observer.removeOnGlobalLayoutListener(this);
Yorke Leef614f6f2014-10-03 10:44:19 -0700459 int screenWidth = mParentLayout.getWidth();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700460 mFloatingActionButtonController.setScreenWidth(screenWidth);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700461 updateFloatingActionButtonControllerAlignment(false /* animate */);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700462 }
463 });
Andrew Lee0c667702014-05-12 14:31:45 -0700464
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700465 setupActivityOverlay();
466
Yorke Leef5554ce2015-01-27 14:46:02 -0800467 Trace.endSection();
468
469 Trace.beginSection(TAG + " initialize smart dialing");
Yorke Lee0baa98b2013-08-22 10:55:16 -0700470 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700471 SmartDialPrefix.initializeNanpSettings(this);
Yorke Leef5554ce2015-01-27 14:46:02 -0800472 Trace.endSection();
473 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700474 }
475
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700476 private void setupActivityOverlay() {
477 final View activityOverlay = findViewById(R.id.activity_overlay);
478 activityOverlay.setOnTouchListener(new OnTouchListener() {
479 @Override
480 public boolean onTouch(View v, MotionEvent event) {
481 if (!mIsDialpadShown) {
482 maybeExitSearchUi();
483 }
484 return false;
485 }
486 });
487 }
488
Chiao Cheng94b10b52012-08-17 16:59:12 -0700489 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700490 protected void onResume() {
Yorke Leef5554ce2015-01-27 14:46:02 -0800491 Trace.beginSection(TAG + " onResume");
Yorke Leec3766332013-07-31 11:13:16 -0700492 super.onResume();
Yorke Lee19e68ca2014-10-28 11:51:40 -0700493 mStateSaved = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700494 if (mFirstLaunch) {
495 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700496 } else if (!phoneIsInUse() && mInCallDialpadUp) {
497 hideDialpadFragment(false, true);
498 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700499 } else if (mShowDialpadOnResume) {
500 showDialpadFragment(false);
501 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700502 }
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800503
504 // If there was a voice query result returned in the {@link #onActivityResult} callback, it
505 // will have been stashed in mVoiceSearchQuery since the search results fragment cannot be
506 // shown until onResume has completed. Active the search UI and set the search term now.
507 if (!TextUtils.isEmpty(mVoiceSearchQuery)) {
508 mActionBarController.onSearchBoxTapped();
509 mSearchView.setText(mVoiceSearchQuery);
510 mVoiceSearchQuery = null;
511 }
512
Yorke Lee98702de2013-08-06 12:03:32 -0700513 mFirstLaunch = false;
Yorke Lee7ee5c422014-11-04 10:29:32 -0800514
515 if (mIsRestarting) {
516 // This is only called when the activity goes from resumed -> paused -> resumed, so it
517 // will not cause an extra view to be sent out on rotation
518 if (mIsDialpadShown) {
519 AnalyticsUtil.sendScreenView(mDialpadFragment, this);
520 }
521 mIsRestarting = false;
522 }
Yorke Lee53a22302014-04-29 18:13:46 -0700523 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700524 mDialerDatabaseHelper.startSmartDialUpdateThread();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700525 updateFloatingActionButtonControllerAlignment(false /* animate */);
Yorke Leef5554ce2015-01-27 14:46:02 -0800526 Trace.endSection();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700527 }
528
529 @Override
Yorke Lee7ee5c422014-11-04 10:29:32 -0800530 protected void onRestart() {
531 super.onRestart();
532 mIsRestarting = true;
533 }
534
535 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700536 protected void onPause() {
537 if (mClearSearchOnPause) {
538 hideDialpadAndSearchUi();
539 mClearSearchOnPause = false;
540 }
Yorke Lee19e68ca2014-10-28 11:51:40 -0700541 if (mSlideOut.hasStarted() && !mSlideOut.hasEnded()) {
542 commitDialpadFragmentHide();
543 }
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700544 super.onPause();
545 }
546
547 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700548 protected void onSaveInstanceState(Bundle outState) {
549 super.onSaveInstanceState(outState);
550 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700551 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
552 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700553 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700554 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700555 mActionBarController.saveInstanceState(outState);
Yorke Lee19e68ca2014-10-28 11:51:40 -0700556 mStateSaved = true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700557 }
558
559 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700560 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700561 if (fragment instanceof DialpadFragment) {
562 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leed5c512a2015-01-30 14:42:53 -0800563 if (!mIsDialpadShown && !mShowDialpadOnResume) {
Yorke Lee50aba882014-05-28 20:04:31 -0700564 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
565 transaction.hide(mDialpadFragment);
566 transaction.commit();
567 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700568 } else if (fragment instanceof SmartDialSearchFragment) {
569 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700570 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700571 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700572 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700573 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700574 } else if (fragment instanceof ListsFragment) {
575 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700576 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700577 }
Yorke Leec3766332013-07-31 11:13:16 -0700578 }
579
Alon Albertb453e5b2013-09-10 15:54:23 -0700580 protected void handleMenuSettings() {
Yorke Leeaf6f1952014-07-14 19:13:16 -0700581 final Intent intent = new Intent(this, DialerSettingsActivity.class);
582 startActivity(intent);
Alon Albertb453e5b2013-09-10 15:54:23 -0700583 }
584
Chiao Cheng94b10b52012-08-17 16:59:12 -0700585 @Override
586 public void onClick(View view) {
587 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700588 case R.id.floating_action_button:
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700589 if (mListsFragment.getTabPosition() == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
590 sendAddNewContactIntent();
591 } else if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700592 mInCallDialpadUp = false;
593 showDialpadFragment(true);
Andrew Leea73e1892014-05-13 13:21:16 -0700594 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700595 break;
Yorke Leec3766332013-07-31 11:13:16 -0700596 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800597 try {
598 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
599 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
600 } catch (ActivityNotFoundException e) {
601 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
602 Toast.LENGTH_SHORT).show();
603 }
Yorke Leec3766332013-07-31 11:13:16 -0700604 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700605 case R.id.dialtacts_options_menu_button:
Yorke Lee74edcdc2014-07-11 16:15:08 -0700606 mOverflowMenu.show();
Andrew Leee74a10e2014-05-16 14:31:40 -0700607 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700608 default: {
609 Log.wtf(TAG, "Unexpected onClick event from " + view);
610 break;
611 }
612 }
613 }
614
Yorke Leec3766332013-07-31 11:13:16 -0700615 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700616 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700617 switch (item.getItemId()) {
618 case R.id.menu_history:
Andrew Lee69705be2015-05-04 17:24:01 -0700619 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
620 // CONTENT_TYPE, so that we always open our call log from our dialer
621 final Intent intent = new Intent(this, CallLogActivity.class);
622 startActivity(intent);
Yorke Lee86e21f72014-04-02 16:49:38 -0700623 break;
624 case R.id.menu_add_contact:
Andrew Leefdfeaaf2015-05-05 14:10:35 -0700625 sendAddNewContactIntent();
Yorke Lee86e21f72014-04-02 16:49:38 -0700626 break;
627 case R.id.menu_import_export:
628 // We hard-code the "contactsAreAvailable" argument because doing it properly would
629 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
630 // now in Dialtacts for (potential) performance reasons. Compare with how it is
631 // done in {@link PeopleActivity}.
632 ImportExportDialogFragment.show(getFragmentManager(), true,
633 DialtactsActivity.class);
634 return true;
635 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700636 ClearFrequentsDialog.show(getFragmentManager());
637 return true;
638 case R.id.menu_call_settings:
639 handleMenuSettings();
640 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700641 }
642 return false;
643 }
644
645 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700646 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
647 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
648 if (resultCode == RESULT_OK) {
649 final ArrayList<String> matches = data.getStringArrayListExtra(
650 RecognizerIntent.EXTRA_RESULTS);
651 if (matches.size() > 0) {
652 final String match = matches.get(0);
Tyler Gunnc1cfae52014-11-20 13:22:54 -0800653 mVoiceSearchQuery = match;
Yorke Leec3766332013-07-31 11:13:16 -0700654 } else {
655 Log.e(TAG, "Voice search - nothing heard");
656 }
657 } else {
658 Log.e(TAG, "Voice search failed");
659 }
660 }
661 super.onActivityResult(requestCode, resultCode, data);
662 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700663
Andrew Lee2a58ab82014-05-15 02:16:04 -0700664 /**
665 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
666 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
667 * @see #onDialpadShown
668 */
Yorke Leec3766332013-07-31 11:13:16 -0700669 private void showDialpadFragment(boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700670 if (mIsDialpadShown || mStateSaved) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700671 return;
672 }
673 mIsDialpadShown = true;
Yorke Leed5c512a2015-01-30 14:42:53 -0800674
Yorke Lee7ee5c422014-11-04 10:29:32 -0800675 mListsFragment.setUserVisibleHint(false);
Andrew Leecdfa6c62014-05-06 15:33:35 -0700676
Chiao Cheng94b10b52012-08-17 16:59:12 -0700677 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leed5c512a2015-01-30 14:42:53 -0800678 if (mDialpadFragment == null) {
679 mDialpadFragment = new DialpadFragment();
680 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
681 } else {
682 ft.show(mDialpadFragment);
683 }
684
685 mDialpadFragment.setAnimate(animate);
686 AnalyticsUtil.sendScreenView(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700687 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700688
Yorke Lee29ae4ae2014-09-09 16:30:34 -0700689 if (animate) {
690 mFloatingActionButtonController.scaleOut();
691 } else {
692 mFloatingActionButtonController.setVisible(false);
693 }
Yorke Lee5253be02014-05-21 18:50:03 -0700694 mActionBarController.onDialpadUp();
695
Yorke Lee6c450eb2015-05-08 09:45:47 -0700696 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leec3766332013-07-31 11:13:16 -0700697 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700698
Andrew Lee2a58ab82014-05-15 02:16:04 -0700699 /**
700 * Callback from child DialpadFragment when the dialpad is shown.
701 */
702 public void onDialpadShown() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800703 Assert.assertNotNull(mDialpadFragment);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700704 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700705 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700706 } else {
707 mDialpadFragment.setYFraction(0);
708 }
709
Andrew Lee2a58ab82014-05-15 02:16:04 -0700710 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700711 }
712
713 /**
714 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
715 * a callback after the hide animation ends.
716 * @see #commitDialpadFragmentHide
717 */
Yorke Leeca195042013-09-25 16:29:38 -0700718 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700719 if (mDialpadFragment == null) {
720 return;
721 }
Yorke Leef6673d32013-08-23 15:34:17 -0700722 if (clearDialpad) {
723 mDialpadFragment.clearDialpad();
724 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700725 if (!mIsDialpadShown) {
726 return;
Yorke Leec3766332013-07-31 11:13:16 -0700727 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700728 mIsDialpadShown = false;
729 mDialpadFragment.setAnimate(animate);
Yorke Lee7ee5c422014-11-04 10:29:32 -0800730 mListsFragment.setUserVisibleHint(true);
731 mListsFragment.sendScreenViewForCurrentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700732
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700733 updateSearchFragmentPosition();
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -0700734
735 updateFloatingActionButtonControllerAlignment(animate);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700736 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700737 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700738 } else {
739 commitDialpadFragmentHide();
740 }
741
Yorke Lee5253be02014-05-21 18:50:03 -0700742 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700743
Andrew Leed4cde832014-05-16 15:56:48 -0700744 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700745 if (TextUtils.isEmpty(mSearchQuery)) {
746 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700747 }
Andrew Leed4cde832014-05-16 15:56:48 -0700748 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700749 }
750
751 /**
752 * Finishes hiding the dialpad fragment after any animations are completed.
753 */
754 private void commitDialpadFragmentHide() {
Yorke Leed5c512a2015-01-30 14:42:53 -0800755 if (!mStateSaved && mDialpadFragment != null && !mDialpadFragment.isHidden()) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700756 final FragmentTransaction ft = getFragmentManager().beginTransaction();
757 ft.hide(mDialpadFragment);
758 ft.commit();
759 }
Andrew Lee405a6e62014-08-20 15:02:16 -0700760 mFloatingActionButtonController.scaleIn(AnimUtils.NO_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700761 }
762
Andrew Lee2a58ab82014-05-15 02:16:04 -0700763 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700764 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700765 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700766 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700767 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700768 fragment = mRegularSearchFragment;
769 }
770 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700771 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700772 }
773 }
774
Yorke Lee5253be02014-05-21 18:50:03 -0700775 @Override
776 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700777 return mInDialpadSearch || mInRegularSearch;
778 }
779
Yorke Lee5253be02014-05-21 18:50:03 -0700780 @Override
781 public boolean hasSearchQuery() {
782 return !TextUtils.isEmpty(mSearchQuery);
783 }
784
785 @Override
786 public boolean shouldShowActionBar() {
787 return mListsFragment.shouldShowActionBar();
788 }
789
Yorke Leeb207f8a2013-08-29 18:51:06 -0700790 private void setNotInSearchUi() {
791 mInDialpadSearch = false;
792 mInRegularSearch = false;
793 }
794
Yorke Lee311969c2013-08-26 06:31:11 -0700795 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700796 if (mIsDialpadShown) {
797 hideDialpadFragment(false, true);
798 } else {
799 exitSearchUi();
800 }
Yorke Lee311969c2013-08-26 06:31:11 -0700801 }
802
Yorke Lee53a22302014-04-29 18:13:46 -0700803 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700804 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
805 if (canIntentBeHandled(voiceIntent)) {
806 mVoiceSearchButton.setVisibility(View.VISIBLE);
807 mVoiceSearchButton.setOnClickListener(this);
808 } else {
809 mVoiceSearchButton.setVisibility(View.GONE);
810 }
811 }
812
Andrew Lee467de3d2015-03-17 15:47:24 -0700813 protected OptionsPopupMenu buildOptionsMenu(View invoker) {
Andrew Leee74a10e2014-05-16 14:31:40 -0700814 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
815 popupMenu.inflate(R.menu.dialtacts_options);
Nancy Chenbeededb2014-06-10 18:30:28 -0700816 final Menu menu = popupMenu.getMenu();
Andrew Leee74a10e2014-05-16 14:31:40 -0700817 popupMenu.setOnMenuItemClickListener(this);
818 return popupMenu;
819 }
820
Yorke Lee86e21f72014-04-02 16:49:38 -0700821 @Override
822 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700823 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700824 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700825 mPendingSearchViewQuery = null;
826 }
Yorke Leea524ae52014-08-05 18:01:05 -0700827 mActionBarController.restoreActionBarOffset();
Andrew Leee74a10e2014-05-16 14:31:40 -0700828 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700829 }
830
Chiao Cheng94b10b52012-08-17 16:59:12 -0700831 /**
832 * Returns true if the intent is due to hitting the green send key (hardware call button:
833 * KEYCODE_CALL) while in a call.
834 *
835 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700836 * @return true if the intent is due to hitting the green send key while in a call
837 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700838 private boolean isSendKeyWhileInCall(Intent intent) {
839 // If there is a call in progress and the user launched the dialer by hitting the call
840 // button, go straight to the in-call screen.
841 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700842
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700843 if (callKey) {
Tyler Gunn9dc924c2014-09-12 09:33:50 -0700844 getTelecomManager().showInCallScreen(false);
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700845 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700846 }
847
848 return false;
849 }
850
851 /**
852 * Sets the current tab based on the intent's request type
853 *
854 * @param intent Intent that contains information about which tab should be selected
855 */
Yorke Leec3766332013-07-31 11:13:16 -0700856 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700857 // 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 -0700858 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700859 finish();
860 return;
861 }
862
Yorke Leed5c512a2015-01-30 14:42:53 -0800863 final boolean phoneIsInUse = phoneIsInUse();
864 if (phoneIsInUse || (intent.getData() != null && isDialIntent(intent))) {
865 showDialpadFragment(false);
866 mDialpadFragment.setStartedFromNewIntent(true);
867 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
868 mInCallDialpadUp = true;
Yorke Lee35127cd2013-09-10 14:09:29 -0700869 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700870 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700871 }
872
873 @Override
874 public void onNewIntent(Intent newIntent) {
875 setIntent(newIntent);
Nancy Chen199b7f02014-11-05 15:03:00 -0800876 mStateSaved = false;
Yorke Leec3766332013-07-31 11:13:16 -0700877 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700878
Chiao Cheng94b10b52012-08-17 16:59:12 -0700879 invalidateOptionsMenu();
880 }
881
882 /** Returns true if the given intent contains a phone number to populate the dialer with */
883 private boolean isDialIntent(Intent intent) {
884 final String action = intent.getAction();
885 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
886 return true;
887 }
888 if (Intent.ACTION_VIEW.equals(action)) {
889 final Uri data = intent.getData();
Jay Shraunerae274c02014-09-06 10:09:24 -0700890 if (data != null && PhoneAccount.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700891 return true;
892 }
893 }
894 return false;
895 }
896
897 /**
898 * Returns an appropriate call origin for this Activity. May return null when no call origin
899 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
900 * for remembering the tab in which the user made a phone call, so the external app's DIAL
901 * request should not be counted.)
902 */
903 public String getCallOrigin() {
904 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
905 }
906
Chiao Cheng94b10b52012-08-17 16:59:12 -0700907 /**
Yorke Leec3766332013-07-31 11:13:16 -0700908 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700909 */
Yorke Lee6c450eb2015-05-08 09:45:47 -0700910 private void enterSearchUi(boolean smartDialSearch, String query, boolean animate) {
Yorke Lee19e68ca2014-10-28 11:51:40 -0700911 if (mStateSaved || getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700912 // Weird race condition where fragment is doing work after the activity is destroyed
913 // due to talkback being on (b/10209937). Just return since we can't do any
914 // constructive here.
915 return;
916 }
917
Yorke Leeef2b7382013-08-09 17:39:25 -0700918 if (DEBUG) {
919 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
920 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700921
Yorke Leec3766332013-07-31 11:13:16 -0700922 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700923 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700924 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700925 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700926 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700927 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700928
Yorke Leeb207f8a2013-08-29 18:51:06 -0700929 final String tag;
930 if (smartDialSearch) {
931 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
932 } else {
933 tag = TAG_REGULAR_SEARCH_FRAGMENT;
934 }
935 mInDialpadSearch = smartDialSearch;
936 mInRegularSearch = !smartDialSearch;
937
Andrew Lee752956e2014-05-15 11:43:38 -0700938 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700939 if (animate) {
940 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
941 } else {
942 transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
943 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700944 if (fragment == null) {
945 if (smartDialSearch) {
946 fragment = new SmartDialSearchFragment();
947 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700948 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700949 }
Andrew Leeb1903842014-05-15 16:11:24 -0700950 transaction.add(R.id.dialtacts_frame, fragment, tag);
951 } else {
952 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700953 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700954 // DialtactsActivity will provide the options menu
955 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700956 fragment.setShowEmptyListForNullQuery(true);
Yorke Lee6c450eb2015-05-08 09:45:47 -0700957 if (!smartDialSearch) {
958 fragment.setQueryString(query, false /* delaySelection */);
959 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700960 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700961
Yorke Lee6c450eb2015-05-08 09:45:47 -0700962 if (animate) {
963 mListsFragment.getView().animate().alpha(0).withLayer();
964 }
Yorke Lee7ee5c422014-11-04 10:29:32 -0800965 mListsFragment.setUserVisibleHint(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700966 }
967
968 /**
Yorke Leec3766332013-07-31 11:13:16 -0700969 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700970 */
Yorke Leec3766332013-07-31 11:13:16 -0700971 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700972 // See related bug in enterSearchUI();
Yorke Lee19e68ca2014-10-28 11:51:40 -0700973 if (getFragmentManager().isDestroyed() || mStateSaved) {
Yorke Lee3487db72013-09-26 17:12:06 -0700974 return;
975 }
Andrew Leeb1903842014-05-15 16:11:24 -0700976
Andrew Leeb1903842014-05-15 16:11:24 -0700977 mSearchView.setText(null);
Yorke Leed5c512a2015-01-30 14:42:53 -0800978
979 if (mDialpadFragment != null) {
980 mDialpadFragment.clearDialpad();
981 }
982
Yorke Leeb207f8a2013-08-29 18:51:06 -0700983 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700984
Andrew Leeb1903842014-05-15 16:11:24 -0700985 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -0700986 if (mSmartDialSearchFragment != null) {
987 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700988 }
Andrew Leeb1903842014-05-15 16:11:24 -0700989 if (mRegularSearchFragment != null) {
990 transaction.remove(mRegularSearchFragment);
991 }
992 transaction.commit();
993
994 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leed5c512a2015-01-30 14:42:53 -0800995
996 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
Yorke Lee7ee5c422014-11-04 10:29:32 -0800997 // If the dialpad fragment wasn't previously visible, then send a screen view because
998 // we are exiting regular search. Otherwise, the screen view will be sent by
999 // {@link #hideDialpadFragment}.
1000 mListsFragment.sendScreenViewForCurrentPosition();
1001 mListsFragment.setUserVisibleHint(true);
1002 }
1003
Yorke Lee5253be02014-05-21 18:50:03 -07001004 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001005 }
1006
Chiao Cheng94b10b52012-08-17 16:59:12 -07001007 @Override
Yorke Leec3766332013-07-31 11:13:16 -07001008 public void onBackPressed() {
Yorke Lee19e68ca2014-10-28 11:51:40 -07001009 if (mStateSaved) {
1010 return;
1011 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001012 if (mIsDialpadShown) {
Andrew Lee9fd8f252014-06-13 17:09:26 -07001013 if (TextUtils.isEmpty(mSearchQuery) ||
1014 (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()
1015 && mSmartDialSearchFragment.getAdapter().getCount() == 0)) {
Andrew Leeb1903842014-05-15 16:11:24 -07001016 exitSearchUi();
1017 }
Yorke Leef6673d32013-08-23 15:34:17 -07001018 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -07001019 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -07001020 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001021 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001022 } else {
1023 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -07001024 }
Yorke Leec3766332013-07-31 11:13:16 -07001025 }
1026
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001027 /**
1028 * @return True if the search UI was exited, false otherwise
1029 */
1030 private boolean maybeExitSearchUi() {
1031 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
1032 exitSearchUi();
Yorke Leef614f6f2014-10-03 10:44:19 -07001033 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec7b2a0e2014-05-20 17:21:25 -07001034 return true;
1035 }
1036 return false;
1037 }
1038
Yorke Leec3766332013-07-31 11:13:16 -07001039 @Override
1040 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -07001041 if (mSmartDialSearchFragment != null) {
1042 mSmartDialSearchFragment.setAddToContactNumber(query);
1043 }
Yorke Leec3766332013-07-31 11:13:16 -07001044 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
1045 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -07001046
1047 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -07001048 if (DEBUG) {
1049 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
1050 }
1051 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
1052 // This callback can happen if the dialpad fragment is recreated because of
1053 // activity destruction. In that case, don't update the search view because
1054 // that would bring the user back to the search fragment regardless of the
1055 // previous state of the application. Instead, just return here and let the
1056 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -07001057 if (!TextUtils.isEmpty(normalizedQuery)) {
1058 mPendingSearchViewQuery = normalizedQuery;
1059 }
Yorke Leeef2b7382013-08-09 17:39:25 -07001060 return;
1061 }
Yorke Lee53a22302014-04-29 18:13:46 -07001062 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -07001063 }
1064 }
Yorke Leec3766332013-07-31 11:13:16 -07001065
1066 @Override
1067 public void onListFragmentScrollStateChange(int scrollState) {
1068 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -07001069 hideDialpadFragment(true, false);
Yorke Leef614f6f2014-10-03 10:44:19 -07001070 DialerUtils.hideInputMethod(mParentLayout);
Yorke Leec3766332013-07-31 11:13:16 -07001071 }
1072 }
1073
1074 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -07001075 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -07001076 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -07001077 // TODO: No-op for now. This should eventually show/hide the actionBar based on
1078 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -07001079 }
1080
Yorke Leec3766332013-07-31 11:13:16 -07001081 private boolean phoneIsInUse() {
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001082 return getTelecomManager().isInCall();
Yorke Leec3766332013-07-31 11:13:16 -07001083 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001084
Yorke Leee1424812013-09-04 18:24:48 -07001085 public static Intent getAddNumberToContactIntent(CharSequence text) {
Tyler Gunn1513fec2014-12-05 14:19:00 -08001086 return getAddToContactIntent(null /* name */, text /* phoneNumber */,
1087 -1 /* phoneNumberType */);
1088 }
1089
1090 public static Intent getAddToContactIntent(CharSequence name, CharSequence phoneNumber,
1091 int phoneNumberType) {
1092 Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1093 intent.putExtra(Intents.Insert.PHONE, phoneNumber);
1094 // Only include the name and phone type extras if they are specified (the method
1095 // getAddNumberToContactIntent does not use them).
1096 if (name != null) {
1097 intent.putExtra(Intents.Insert.NAME, name);
1098 }
1099 if (phoneNumberType != -1) {
1100 intent.putExtra(Intents.Insert.PHONE_TYPE, phoneNumberType);
1101 }
Yorke Leee1424812013-09-04 18:24:48 -07001102 intent.setType(Contacts.CONTENT_ITEM_TYPE);
1103 return intent;
1104 }
1105
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001106 private void sendAddNewContactIntent() {
1107 try {
1108 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
1109 } catch (ActivityNotFoundException e) {
1110 Toast toast = Toast.makeText(this,
1111 R.string.add_contact_not_available,
1112 Toast.LENGTH_SHORT);
1113 toast.show();
1114 }
1115 }
1116
Yorke Leeda0f9042013-12-05 14:25:51 -08001117 private boolean canIntentBeHandled(Intent intent) {
1118 final PackageManager packageManager = getPackageManager();
1119 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1120 PackageManager.MATCH_DEFAULT_ONLY);
1121 return resolveInfo != null && resolveInfo.size() > 0;
1122 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001123
Yorke Lee86e21f72014-04-02 16:49:38 -07001124 /**
1125 * Called when the user has long-pressed a contact tile to start a drag operation.
1126 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001127 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001128 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Lee18963442014-06-06 17:20:13 -07001129 mListsFragment.showRemoveView(true);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001130 }
1131
1132 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001133 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1134 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001135
Yorke Lee86e21f72014-04-02 16:49:38 -07001136 /**
1137 * Called when the user has released a contact tile after long-pressing it.
1138 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001139 @Override
1140 public void onDragFinished(int x, int y) {
Andrew Lee18963442014-06-06 17:20:13 -07001141 mListsFragment.showRemoveView(false);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001142 }
1143
1144 @Override
1145 public void onDroppedOnRemove() {}
1146
1147 /**
Yorke Leed999b712014-04-23 15:10:58 -07001148 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001149 * once it has been attached to the activity.
1150 */
1151 @Override
1152 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001153 mDragDropController = dragController;
Andrew Lee18963442014-06-06 17:20:13 -07001154 mListsFragment.getRemoveView().setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001155 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001156
1157 @Override
1158 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001159 // Specify call-origin so that users will see the previous tab instead of
1160 // CallLog screen (search UI will be automatically exited).
1161 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001162 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001163 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001164 }
1165
1166 @Override
1167 public void onCallNumberDirectly(String phoneNumber) {
Andrew Leefc1fed72014-07-02 11:05:53 -07001168 onCallNumberDirectly(phoneNumber, false /* isVideoCall */);
1169 }
1170
1171 @Override
1172 public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall) {
Andrew Leed2a683f2014-07-08 15:54:50 -07001173 Intent intent = isVideoCall ?
Yorke Leeb82e8da2015-04-18 14:37:50 -07001174 CallIntentUtil.getVideoCallIntent(phoneNumber, getCallOrigin()) :
1175 CallIntentUtil.getCallIntent(phoneNumber, getCallOrigin());
Yorke Lee7d20f822014-06-19 17:09:33 -07001176 DialerUtils.startActivityWithErrorToast(this, intent);
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001177 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001178 }
1179
1180 @Override
1181 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001182 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001183 }
1184
1185 @Override
1186 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001187 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001188 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001189
Yorke Leecc4660d2014-04-24 11:22:57 -07001190 @Override
1191 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Yorke Lee69c247f2014-07-22 11:49:49 -07001192 position = mListsFragment.getRtlPosition(position);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001193 // Only scroll the button when the first tab is selected. The button should scroll from
1194 // the middle to right position only on the transition from the first tab to the second
1195 // tab.
Yorke Lee69c247f2014-07-22 11:49:49 -07001196 // If the app is in RTL mode, we need to check against the second tab, rather than the
1197 // first. This is because if we are scrolling between the first and second tabs, the
1198 // viewpager will report that the starting tab position is 1 rather than 0, due to the
1199 // reversal of the order of the tabs.
1200 final boolean isLayoutRtl = DialerUtils.isRtl();
1201 final boolean shouldScrollButton = position == (isLayoutRtl
1202 ? ListsFragment.TAB_INDEX_RECENTS : ListsFragment.TAB_INDEX_SPEED_DIAL);
1203 if (shouldScrollButton && !mIsLandscape) {
1204 mFloatingActionButtonController.onPageScrolled(
1205 isLayoutRtl ? 1 - positionOffset : positionOffset);
Sai Cheemalapati00a3d5d2014-06-13 10:22:26 -07001206 } else if (position != ListsFragment.TAB_INDEX_SPEED_DIAL) {
1207 mFloatingActionButtonController.onPageScrolled(1);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001208 }
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001209
1210 if (position == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
1211 mFloatingActionButtonController.changeIcon(
1212 getResources().getDrawable(R.drawable.ic_person_add_24dp),
1213 getResources().getString(R.string.search_shortcut_create_new_contact));
1214 } else {
1215 mFloatingActionButtonController.changeIcon(
1216 getResources().getDrawable(R.drawable.fab_ic_dial),
1217 getResources().getString(R.string.action_menu_dialpad_button));
1218 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001219 }
1220
1221 @Override
1222 public void onPageSelected(int position) {
Yorke Leecc4660d2014-04-24 11:22:57 -07001223 }
1224
1225 @Override
1226 public void onPageScrollStateChanged(int state) {
1227 }
1228
Tyler Gunn9dc924c2014-09-12 09:33:50 -07001229 private TelecomManager getTelecomManager() {
1230 return (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
Santos Cordond15629d2014-07-07 15:15:29 -07001231 }
1232
Yorke Lee5253be02014-05-21 18:50:03 -07001233 @Override
1234 public boolean isActionBarShowing() {
1235 return mActionBarController.isActionBarShowing();
1236 }
1237
Yorke Leec98a5bb2014-10-28 16:12:05 -07001238 @Override
1239 public ActionBarController getActionBarController() {
1240 return mActionBarController;
1241 }
1242
Andrew Lee9da8fb42014-06-03 14:03:09 -07001243 public boolean isDialpadShown() {
1244 return mIsDialpadShown;
1245 }
1246
Yorke Lee5253be02014-05-21 18:50:03 -07001247 @Override
1248 public int getActionBarHideOffset() {
1249 return getActionBar().getHideOffset();
1250 }
1251
1252 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001253 public void setActionBarHideOffset(int offset) {
1254 getActionBar().setHideOffset(offset);
Yorke Lee5253be02014-05-21 18:50:03 -07001255 }
1256
1257 @Override
Yorke Leec98a5bb2014-10-28 16:12:05 -07001258 public int getActionBarHeight() {
1259 return mActionBarHeight;
Yorke Lee5253be02014-05-21 18:50:03 -07001260 }
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001261
1262 /**
1263 * Updates controller based on currently known information.
1264 *
1265 * @param animate Whether or not to animate the transition.
1266 */
1267 private void updateFloatingActionButtonControllerAlignment(boolean animate) {
Andrew Leefdfeaaf2015-05-05 14:10:35 -07001268 int align = (!mIsLandscape &&
1269 mListsFragment.getTabPosition() == ListsFragment.TAB_INDEX_SPEED_DIAL) ?
Andrew Lee405a6e62014-08-20 15:02:16 -07001270 FloatingActionButtonController.ALIGN_MIDDLE :
1271 FloatingActionButtonController.ALIGN_END;
Andrew Leef43b32f2014-08-14 17:43:04 -07001272 mFloatingActionButtonController.align(align, 0 /* offsetX */, 0 /* offsetY */, animate);
Sai Cheemalapatic4969ea2014-06-04 16:35:16 -07001273 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001274}