blob: a593c14a578f8d3db0c313eccdacd9f5c450210a [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 Leecc4660d2014-04-24 11:22:57 -070019import android.animation.LayoutTransition;
Yorke Lee53a22302014-04-29 18:13:46 -070020import android.app.ActionBar;
Chiao Cheng94b10b52012-08-17 16:59:12 -070021import android.app.Activity;
22import android.app.Fragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -070023import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080024import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070025import android.content.Context;
26import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080027import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Yorke Lee33932ff2014-04-16 13:34:32 -070029import android.content.res.TypedArray;
Chiao Cheng94b10b52012-08-17 16:59:12 -070030import android.net.Uri;
31import android.os.Bundle;
32import android.os.RemoteException;
33import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070035import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070036import android.speech.RecognizerIntent;
Yorke Leecc4660d2014-04-24 11:22:57 -070037import android.support.v4.view.ViewPager;
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;
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;
Yorke Lee86e21f72014-04-02 16:49:38 -070051import android.view.animation.AccelerateInterpolator;
Andrew Lee2a58ab82014-05-15 02:16:04 -070052import android.view.animation.Animation;
53import android.view.animation.Animation.AnimationListener;
54import android.view.animation.AnimationUtils;
Yorke Lee86e21f72014-04-02 16:49:38 -070055import android.view.animation.DecelerateInterpolator;
56import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070058import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070059import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070060import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070061import android.widget.PopupMenu;
Yorke Leecc4660d2014-04-24 11:22:57 -070062import android.widget.RelativeLayout;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080063import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070064
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070065import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070066import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070067import com.android.contacts.common.dialog.ClearFrequentsDialog;
68import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080069import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070070import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070072import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070073import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070074import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070075import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070076import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080077import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070078import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080079import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070080import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070081import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070082import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070083import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080084import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070085import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070086import com.android.dialer.list.SmartDialSearchFragment;
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;
Chiao Cheng94b10b52012-08-17 16:59:12 -070091import com.android.internal.telephony.ITelephony;
92
Yorke Leec3766332013-07-31 11:13:16 -070093import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080094import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070095
Chiao Cheng94b10b52012-08-17 16:59:12 -070096/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070097 * The dialer tab's title is 'phone', a more common name (see strings.xml).
98 */
Yorke Leec3766332013-07-31 11:13:16 -070099public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700100 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700101 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -0800102 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700103 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700104 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700105 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700106 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700107 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700108 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700109 ViewPager.OnPageChangeListener,
110 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700111 private static final String TAG = "DialtactsActivity";
112
Ihab Awad526c0b82014-02-27 12:55:36 -0800113 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700114
Yorke Leef74011e2013-08-02 11:30:30 -0700115 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
116
Chiao Cheng94b10b52012-08-17 16:59:12 -0700117 /** Used to open Call Setting */
118 private static final String PHONE_PACKAGE = "com.android.phone";
119 private static final String CALL_SETTINGS_CLASS_NAME =
120 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121 /** @see #getCallOrigin() */
122 private static final String CALL_ORIGIN_DIALTACTS =
123 "com.android.dialer.DialtactsActivity";
124
Yorke Leeb207f8a2013-08-29 18:51:06 -0700125 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
126 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700127 private static final String KEY_SEARCH_QUERY = "search_query";
128 private static final String KEY_FIRST_LAUNCH = "first_launch";
129
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";
139
Yorke Leec3766332013-07-31 11:13:16 -0700140 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700141
Yorke Lee86e21f72014-04-02 16:49:38 -0700142 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800143
Andrew Lee0c667702014-05-12 14:31:45 -0700144 private RelativeLayout parentLayout;
145
Yorke Leec3766332013-07-31 11:13:16 -0700146 /**
Yorke Leec3766332013-07-31 11:13:16 -0700147 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700148 */
Yorke Leef74011e2013-08-02 11:30:30 -0700149 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700150
151 /**
152 * Fragment for searching phone numbers using the alphanumeric keyboard.
153 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700154 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700155
156 /**
157 * Fragment for searching phone numbers using the dialpad.
158 */
159 private SmartDialSearchFragment mSmartDialSearchFragment;
160
Yorke Leee00c9fe2014-04-12 12:42:06 -0700161 /**
162 * Fragment containing the speed dial list, recents list, and all contacts list.
163 */
164 private ListsFragment mListsFragment;
165
Andrew Leea73e1892014-05-13 13:21:16 -0700166 private View mFloatingActionButtonContainer;
167 private ImageButton mFloatingActionButton;
Yorke Leec3766332013-07-31 11:13:16 -0700168
Yorke Lee33932ff2014-04-16 13:34:32 -0700169 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700170 private boolean mInDialpadSearch;
171 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700172 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700173 private boolean mIsDialpadShown;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700174
Yorke Leeef2b7382013-08-09 17:39:25 -0700175 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700176 * The position of the currently selected tab in the attached {@link ListsFragment}.
177 */
178 private int mCurrentTabPosition = 0;
179
180 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700181 * True if the dialpad is only temporarily showing due to being in call
182 */
183 private boolean mInCallDialpadUp;
184
185 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700186 * True when this activity has been launched for the first time.
187 */
Yorke Lee98702de2013-08-06 12:03:32 -0700188 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700189
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700190 /**
191 * Search query to be applied to the SearchView in the ActionBar once
192 * onCreateOptionsMenu has been called.
193 */
194 private String mPendingSearchViewQuery;
195
Yorke Lee53a22302014-04-29 18:13:46 -0700196 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700197 private View mVoiceSearchButton;
Yorke Leeec489fb2014-05-19 15:39:27 -0700198 private SearchEditTextLayout mSearchEditTextLayout;
Andrew Leee74a10e2014-05-16 14:31:40 -0700199
Yorke Lee86e21f72014-04-02 16:49:38 -0700200 /**
201 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
202 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
203 * removed from the speed dial list.
204 */
Yorke Lee28266192014-05-01 10:05:52 -0700205 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700206
Yorke Lee86e21f72014-04-02 16:49:38 -0700207 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
208 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700209 private String mSearchQuery;
210
Yorke Leefce269a2013-08-12 11:56:04 -0700211 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700212 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700213 private ActionBarController mActionBarController;
Yorke Leefce269a2013-08-12 11:56:04 -0700214
Andrew Leee74a10e2014-05-16 14:31:40 -0700215 private class OptionsPopupMenu extends PopupMenu {
216 public OptionsPopupMenu(Context context, View anchor) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700217 super(context, anchor);
218 }
219
220 @Override
221 public void show() {
222 final Menu menu = getMenu();
223 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700224 clearFrequents.setVisible(mListsFragment != null &&
225 mListsFragment.getSpeedDialFragment() != null &&
226 mListsFragment.getSpeedDialFragment().hasFrequents());
Yorke Leee00c9fe2014-04-12 12:42:06 -0700227 super.show();
228 }
229 }
230
Chiao Cheng94b10b52012-08-17 16:59:12 -0700231 /**
Yorke Lee28266192014-05-01 10:05:52 -0700232 * Listener that listens to drag events and sends their x and y coordinates to a
233 * {@link DragDropController}.
234 */
235 private class LayoutOnDragListener implements OnDragListener {
236 @Override
237 public boolean onDrag(View v, DragEvent event) {
238 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
239 mDragDropController.handleDragHovered(v, (int) event.getX(),
240 (int) event.getY());
241 }
242 return true;
243 }
244 }
245
246 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700247 * Listener used to send search queries to the phone search fragment.
248 */
Yorke Lee53a22302014-04-29 18:13:46 -0700249 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700250 @Override
251 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
252 }
Yorke Leec3766332013-07-31 11:13:16 -0700253
Andrew Lee99a570c2014-05-15 14:18:50 -0700254 @Override
255 public void onTextChanged(CharSequence s, int start, int before, int count) {
256 final String newText = s.toString();
257 if (newText.equals(mSearchQuery)) {
258 // If the query hasn't changed (perhaps due to activity being destroyed
259 // and restored, or user launching the same DIAL intent twice), then there is
260 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700261 return;
262 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700263 mSearchQuery = newText;
264 if (DEBUG) {
265 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leec3766332013-07-31 11:13:16 -0700266 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700267
Andrew Lee90374a72014-05-16 15:01:09 -0700268 // Show search fragment only when the query string is changed to non-empty text.
269 if (!TextUtils.isEmpty(newText)) {
270 // Call enterSearchUi only if we are switching search modes, or showing a search
271 // fragment for the first time.
272 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
273 (!mIsDialpadShown && mInRegularSearch);
274 if (!sameSearchMode) {
275 enterSearchUi(mIsDialpadShown, mSearchQuery);
276 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700277 }
278
279 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700280 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700281 } else if (mRegularSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700282 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700283 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700284 }
285
286 @Override
287 public void afterTextChanged(Editable s) {
288 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700289 };
290
Andrew Leeb1903842014-05-15 16:11:24 -0700291
292 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700293 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700294 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700295 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700296 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700297 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700298 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700299 mActionBarController.onSearchBoxTapped();
Andrew Leeb1903842014-05-15 16:11:24 -0700300 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
301 }
Andrew Leeb1903842014-05-15 16:11:24 -0700302 }
303 };
304
305 /**
306 * If the search term is empty and the user closes the soft keyboard, close the search UI.
307 */
308 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
309 @Override
310 public boolean onKey(View v, int keyCode, KeyEvent event) {
311 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
312 TextUtils.isEmpty(mSearchView.getText().toString())) {
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700313 maybeExitSearchUi();
Andrew Leeb1903842014-05-15 16:11:24 -0700314 }
315 return false;
316 }
317 };
318
Chiao Cheng94b10b52012-08-17 16:59:12 -0700319 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700320 protected void onCreate(Bundle savedInstanceState) {
321 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700322 mFirstLaunch = true;
323
Yorke Lee8898cd02013-08-08 10:24:27 -0700324 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800325 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700326
Yorke Lee53a22302014-04-29 18:13:46 -0700327 final ActionBar actionBar = getActionBar();
328 actionBar.setCustomView(R.layout.search_edittext);
329 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700330 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700331
Yorke Lee5253be02014-05-21 18:50:03 -0700332 mActionBarController = new ActionBarController(this,
333 (SearchEditTextLayout) actionBar.getCustomView());
334
Yorke Leeec489fb2014-05-19 15:39:27 -0700335 mSearchEditTextLayout = (SearchEditTextLayout) actionBar.getCustomView();
336 mSearchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700337
Yorke Leeec489fb2014-05-19 15:39:27 -0700338 mSearchView = (EditText) mSearchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700339 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Yorke Lee11ca39e2014-05-19 20:31:37 -0700340 mVoiceSearchButton = mSearchEditTextLayout.findViewById(R.id.voice_search_button);
341 mSearchEditTextLayout.findViewById(R.id.search_box_start_search).setOnClickListener(
342 mSearchViewOnClickListener);
343 mSearchEditTextLayout.setOnBackButtonClickedListener(new OnBackButtonClickedListener() {
344 @Override
345 public void onBackButtonClicked() {
346 onBackPressed();
347 }
348 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700349
Yorke Leeec489fb2014-05-19 15:39:27 -0700350 ImageButton optionsMenuButton = (ImageButton) mSearchEditTextLayout.findViewById(
351 R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700352 optionsMenuButton.setOnClickListener(this);
353 final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
354 optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
355
Yorke Lee3443d4a2014-05-23 19:30:35 -0700356 mActionBarHeight = getResources().getDimensionPixelSize(R.dimen.action_bar_height);
Yorke Lee33932ff2014-04-16 13:34:32 -0700357
Yorke Leeef2b7382013-08-09 17:39:25 -0700358 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
359 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700360 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800361 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700362 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800363 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
364 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700365 } else {
366 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700367 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
368 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700369 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee5253be02014-05-21 18:50:03 -0700370 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700371 }
372
Andrew Lee0c667702014-05-12 14:31:45 -0700373 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
374 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
375 parentLayout.setOnDragListener(new LayoutOnDragListener());
376
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700377 setupActivityOverlay();
378
Andrew Leea73e1892014-05-13 13:21:16 -0700379 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
380 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700381
Andrew Leea73e1892014-05-13 13:21:16 -0700382 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
383 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800384
Yorke Lee28266192014-05-01 10:05:52 -0700385 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800386
Yorke Lee0baa98b2013-08-22 10:55:16 -0700387 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700388 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700389 }
390
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700391 private void setupActivityOverlay() {
392 final View activityOverlay = findViewById(R.id.activity_overlay);
393 activityOverlay.setOnTouchListener(new OnTouchListener() {
394 @Override
395 public boolean onTouch(View v, MotionEvent event) {
396 if (!mIsDialpadShown) {
397 maybeExitSearchUi();
398 }
399 return false;
400 }
401 });
402 }
403
Chiao Cheng94b10b52012-08-17 16:59:12 -0700404 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700405 protected void onResume() {
406 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700407 if (mFirstLaunch) {
408 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700409 } else if (!phoneIsInUse() && mInCallDialpadUp) {
410 hideDialpadFragment(false, true);
411 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700412 }
413 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700414 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700415 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700416 }
417
418 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700419 protected void onPause() {
420 if (mClearSearchOnPause) {
421 hideDialpadAndSearchUi();
422 mClearSearchOnPause = false;
423 }
424 super.onPause();
425 }
426
427 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700428 protected void onSaveInstanceState(Bundle outState) {
429 super.onSaveInstanceState(outState);
430 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700431 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
432 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700433 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee5253be02014-05-21 18:50:03 -0700434 mActionBarController.saveInstanceState(outState);
Yorke Leeef2b7382013-08-09 17:39:25 -0700435 }
436
437 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700438 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700439 if (fragment instanceof DialpadFragment) {
440 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700441 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700442 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700443 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700444 } else if (fragment instanceof SmartDialSearchFragment) {
445 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700446 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700447 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700448 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700449 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700450 } else if (fragment instanceof ListsFragment) {
451 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700452 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700453 }
Yorke Leec3766332013-07-31 11:13:16 -0700454 }
455
Alon Albertb453e5b2013-09-10 15:54:23 -0700456 protected void handleMenuSettings() {
457 openTelephonySetting(this);
458 }
459
460 public static void openTelephonySetting(Activity activity) {
461 final Intent settingsIntent = getCallSettingsIntent();
462 activity.startActivity(settingsIntent);
463 }
464
Chiao Cheng94b10b52012-08-17 16:59:12 -0700465 @Override
466 public void onClick(View view) {
467 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700468 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700469 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700470 mInCallDialpadUp = false;
471 showDialpadFragment(true);
472 } else {
473 // Dial button was pressed; tell the Dialpad fragment
474 mDialpadFragment.dialButtonPressed();
475 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700476 break;
Yorke Leec3766332013-07-31 11:13:16 -0700477 case R.id.search_close_button:
478 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700479 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700480 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700481 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700482 }
483 break;
Yorke Leec3766332013-07-31 11:13:16 -0700484 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800485 try {
486 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
487 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
488 } catch (ActivityNotFoundException e) {
489 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
490 Toast.LENGTH_SHORT).show();
491 }
Yorke Leec3766332013-07-31 11:13:16 -0700492 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700493 case R.id.dialtacts_options_menu_button:
494 buildOptionsMenu(view).show();
495 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700496 default: {
497 Log.wtf(TAG, "Unexpected onClick event from " + view);
498 break;
499 }
500 }
501 }
502
Yorke Leec3766332013-07-31 11:13:16 -0700503 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700504 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700505 switch (item.getItemId()) {
506 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700507 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700508 break;
509 case R.id.menu_add_contact:
510 try {
511 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
512 } catch (ActivityNotFoundException e) {
513 Toast toast = Toast.makeText(this,
514 R.string.add_contact_not_available,
515 Toast.LENGTH_SHORT);
516 toast.show();
517 }
518 break;
519 case R.id.menu_import_export:
520 // We hard-code the "contactsAreAvailable" argument because doing it properly would
521 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
522 // now in Dialtacts for (potential) performance reasons. Compare with how it is
523 // done in {@link PeopleActivity}.
524 ImportExportDialogFragment.show(getFragmentManager(), true,
525 DialtactsActivity.class);
526 return true;
527 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700528 ClearFrequentsDialog.show(getFragmentManager());
529 return true;
530 case R.id.menu_call_settings:
531 handleMenuSettings();
532 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700533 }
534 return false;
535 }
536
537 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700538 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
539 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
540 if (resultCode == RESULT_OK) {
541 final ArrayList<String> matches = data.getStringArrayListExtra(
542 RecognizerIntent.EXTRA_RESULTS);
543 if (matches.size() > 0) {
544 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700545 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700546 } else {
547 Log.e(TAG, "Voice search - nothing heard");
548 }
549 } else {
550 Log.e(TAG, "Voice search failed");
551 }
552 }
553 super.onActivityResult(requestCode, resultCode, data);
554 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700555
Andrew Lee2a58ab82014-05-15 02:16:04 -0700556 /**
557 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
558 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
559 * @see #onDialpadShown
560 */
Yorke Leec3766332013-07-31 11:13:16 -0700561 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700562 if (mIsDialpadShown) {
563 return;
564 }
565 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700566 mDialpadFragment.setAnimate(animate);
567
Chiao Cheng94b10b52012-08-17 16:59:12 -0700568 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700569 ft.show(mDialpadFragment);
570 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700571
Yorke Lee5253be02014-05-21 18:50:03 -0700572 mActionBarController.onDialpadUp();
573
Andrew Leeb1903842014-05-15 16:11:24 -0700574 if (!isInSearchUi()) {
575 enterSearchUi(true /* isSmartDial */, mSearchQuery);
576 }
Yorke Leec3766332013-07-31 11:13:16 -0700577 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700578
Andrew Lee2a58ab82014-05-15 02:16:04 -0700579 /**
580 * Callback from child DialpadFragment when the dialpad is shown.
581 */
582 public void onDialpadShown() {
583 updateFloatingActionButton();
584 if (mDialpadFragment.getAnimate()) {
585 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
586 mDialpadFragment.getView().startAnimation(slideIn);
587 } else {
588 mDialpadFragment.setYFraction(0);
589 }
590
Andrew Lee2a58ab82014-05-15 02:16:04 -0700591 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700592 }
593
594 /**
595 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
596 * a callback after the hide animation ends.
597 * @see #commitDialpadFragmentHide
598 */
Yorke Leeca195042013-09-25 16:29:38 -0700599 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700600 if (mDialpadFragment == null) {
601 return;
602 }
Yorke Leef6673d32013-08-23 15:34:17 -0700603 if (clearDialpad) {
604 mDialpadFragment.clearDialpad();
605 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700606 if (!mIsDialpadShown) {
607 return;
Yorke Leec3766332013-07-31 11:13:16 -0700608 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700609 mIsDialpadShown = false;
610 mDialpadFragment.setAnimate(animate);
611
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700612 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700613 updateFloatingActionButton();
614 if (animate) {
615 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
616 slideOut.setAnimationListener(new ActivityAnimationListener() {
617 @Override
618 public void onAnimationEnd(Animation animation) {
619 commitDialpadFragmentHide();
620 }
621 });
622 mDialpadFragment.getView().startAnimation(slideOut);
623 } else {
624 commitDialpadFragmentHide();
625 }
626
Yorke Lee5253be02014-05-21 18:50:03 -0700627 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700628
Andrew Leed4cde832014-05-16 15:56:48 -0700629 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700630 if (TextUtils.isEmpty(mSearchQuery)) {
631 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700632 }
Andrew Leed4cde832014-05-16 15:56:48 -0700633 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700634 }
635
636 /**
637 * Finishes hiding the dialpad fragment after any animations are completed.
638 */
639 private void commitDialpadFragmentHide() {
640 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700641 ft.hide(mDialpadFragment);
642 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700643 }
644
Andrew Lee2a58ab82014-05-15 02:16:04 -0700645 private void updateSearchFragmentPosition() {
646 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
647 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700648 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700649 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700650 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700651 fragment = mRegularSearchFragment;
652 }
653 if (fragment != null && fragment.isVisible()) {
654 fragment.getView().animate().translationY(translationValue)
655 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
656 }
657 }
658
Yorke Lee5253be02014-05-21 18:50:03 -0700659 @Override
660 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700661 return mInDialpadSearch || mInRegularSearch;
662 }
663
Yorke Lee5253be02014-05-21 18:50:03 -0700664 @Override
665 public boolean hasSearchQuery() {
666 return !TextUtils.isEmpty(mSearchQuery);
667 }
668
669 @Override
670 public boolean shouldShowActionBar() {
671 return mListsFragment.shouldShowActionBar();
672 }
673
Yorke Leeb207f8a2013-08-29 18:51:06 -0700674 private void setNotInSearchUi() {
675 mInDialpadSearch = false;
676 mInRegularSearch = false;
677 }
678
Yorke Lee311969c2013-08-26 06:31:11 -0700679 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700680 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700681 hideDialpadFragment(false, true);
682 }
683
Yorke Leee00c9fe2014-04-12 12:42:06 -0700684 private void hideInputMethod(View view) {
685 final InputMethodManager imm = (InputMethodManager) getSystemService(
686 Context.INPUT_METHOD_SERVICE);
687 if (imm != null && view != null) {
688 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
689 }
690 }
691
Yorke Lee53a22302014-04-29 18:13:46 -0700692 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700693 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
694 if (canIntentBeHandled(voiceIntent)) {
695 mVoiceSearchButton.setVisibility(View.VISIBLE);
696 mVoiceSearchButton.setOnClickListener(this);
697 } else {
698 mVoiceSearchButton.setVisibility(View.GONE);
699 }
700 }
701
Andrew Leee74a10e2014-05-16 14:31:40 -0700702 private OptionsPopupMenu buildOptionsMenu(View invoker) {
703 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
704 popupMenu.inflate(R.menu.dialtacts_options);
705 popupMenu.setOnMenuItemClickListener(this);
706 return popupMenu;
707 }
708
Yorke Lee86e21f72014-04-02 16:49:38 -0700709 @Override
710 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700711 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700712 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700713 mPendingSearchViewQuery = null;
714 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700715 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700716 }
717
Chiao Cheng94b10b52012-08-17 16:59:12 -0700718 /**
719 * Returns true if the intent is due to hitting the green send key (hardware call button:
720 * KEYCODE_CALL) while in a call.
721 *
722 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700723 * @return true if the intent is due to hitting the green send key while in a call
724 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700725 private boolean isSendKeyWhileInCall(Intent intent) {
726 // If there is a call in progress and the user launched the dialer by hitting the call
727 // button, go straight to the in-call screen.
728 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729
Yorke Lee62e995c2014-03-28 10:02:56 -0700730 try {
731 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
732 if (callKey && phone != null && phone.showCallScreen()) {
733 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700734 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700735 } catch (RemoteException e) {
736 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700737 }
738
739 return false;
740 }
741
742 /**
743 * Sets the current tab based on the intent's request type
744 *
745 * @param intent Intent that contains information about which tab should be selected
746 */
Yorke Leec3766332013-07-31 11:13:16 -0700747 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700748 // 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 -0700749 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700750 finish();
751 return;
752 }
753
Yorke Lee669b6082013-09-17 15:43:38 -0700754 if (mDialpadFragment != null) {
755 final boolean phoneIsInUse = phoneIsInUse();
756 if (phoneIsInUse || isDialIntent(intent)) {
757 mDialpadFragment.setStartedFromNewIntent(true);
758 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
759 mInCallDialpadUp = true;
760 }
761 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700762 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700763 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 }
765
766 @Override
767 public void onNewIntent(Intent newIntent) {
768 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700769 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700770
Chiao Cheng94b10b52012-08-17 16:59:12 -0700771 invalidateOptionsMenu();
772 }
773
774 /** Returns true if the given intent contains a phone number to populate the dialer with */
775 private boolean isDialIntent(Intent intent) {
776 final String action = intent.getAction();
777 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
778 return true;
779 }
780 if (Intent.ACTION_VIEW.equals(action)) {
781 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700782 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700783 return true;
784 }
785 }
786 return false;
787 }
788
789 /**
790 * Returns an appropriate call origin for this Activity. May return null when no call origin
791 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
792 * for remembering the tab in which the user made a phone call, so the external app's DIAL
793 * request should not be counted.)
794 */
795 public String getCallOrigin() {
796 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
797 }
798
Chiao Cheng94b10b52012-08-17 16:59:12 -0700799 /**
Yorke Leec3766332013-07-31 11:13:16 -0700800 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700801 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700802 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700803 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700804 // Weird race condition where fragment is doing work after the activity is destroyed
805 // due to talkback being on (b/10209937). Just return since we can't do any
806 // constructive here.
807 return;
808 }
809
Yorke Leeef2b7382013-08-09 17:39:25 -0700810 if (DEBUG) {
811 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
812 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700813
Yorke Leec3766332013-07-31 11:13:16 -0700814 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700815 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700816 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700817 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700818 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700819 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700820
Yorke Leeb207f8a2013-08-29 18:51:06 -0700821 final String tag;
822 if (smartDialSearch) {
823 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
824 } else {
825 tag = TAG_REGULAR_SEARCH_FRAGMENT;
826 }
827 mInDialpadSearch = smartDialSearch;
828 mInRegularSearch = !smartDialSearch;
829
Andrew Lee752956e2014-05-15 11:43:38 -0700830 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700831 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700832 if (fragment == null) {
833 if (smartDialSearch) {
834 fragment = new SmartDialSearchFragment();
835 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700836 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700837 }
Andrew Leeb1903842014-05-15 16:11:24 -0700838 transaction.add(R.id.dialtacts_frame, fragment, tag);
839 } else {
840 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700841 }
Andrew Leeb1903842014-05-15 16:11:24 -0700842
Yorke Lee86e21f72014-04-02 16:49:38 -0700843 // DialtactsActivity will provide the options menu
844 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700845 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700846 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700847 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700848
849 mListsFragment.getView().animate().alpha(0).withLayer();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700850 }
851
852 /**
Yorke Leec3766332013-07-31 11:13:16 -0700853 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700854 */
Yorke Leec3766332013-07-31 11:13:16 -0700855 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700856 // See related bug in enterSearchUI();
857 if (getFragmentManager().isDestroyed()) {
858 return;
859 }
Andrew Leeb1903842014-05-15 16:11:24 -0700860
Andrew Leeb1903842014-05-15 16:11:24 -0700861 mSearchView.setText(null);
862 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700863 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700864
Andrew Leeb1903842014-05-15 16:11:24 -0700865 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
866 transaction.setCustomAnimations(0, android.R.animator.fade_out);
Andrew Leeb1903842014-05-15 16:11:24 -0700867 if (mSmartDialSearchFragment != null) {
868 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700869 }
Andrew Leeb1903842014-05-15 16:11:24 -0700870 if (mRegularSearchFragment != null) {
871 transaction.remove(mRegularSearchFragment);
872 }
873 transaction.commit();
874
875 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Lee5253be02014-05-21 18:50:03 -0700876 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700877 }
878
879 /** Returns an Intent to launch Call Settings screen */
880 public static Intent getCallSettingsIntent() {
881 final Intent intent = new Intent(Intent.ACTION_MAIN);
882 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
883 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
884 return intent;
885 }
886
887 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700888 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700889 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700890 if (TextUtils.isEmpty(mSearchQuery)) {
891 exitSearchUi();
892 }
Yorke Leef6673d32013-08-23 15:34:17 -0700893 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700894 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700895 exitSearchUi();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700896 hideInputMethod(parentLayout);
Yorke Leec3766332013-07-31 11:13:16 -0700897 } else {
898 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700899 }
Yorke Leec3766332013-07-31 11:13:16 -0700900 }
901
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700902 /**
903 * @return True if the search UI was exited, false otherwise
904 */
905 private boolean maybeExitSearchUi() {
906 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
907 exitSearchUi();
908 hideInputMethod(parentLayout);
909 return true;
910 }
911 return false;
912 }
913
Yorke Leec3766332013-07-31 11:13:16 -0700914 @Override
915 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700916 if (mSmartDialSearchFragment != null) {
917 mSmartDialSearchFragment.setAddToContactNumber(query);
918 }
Yorke Leec3766332013-07-31 11:13:16 -0700919 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
920 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700921
922 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700923 if (DEBUG) {
924 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
925 }
926 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
927 // This callback can happen if the dialpad fragment is recreated because of
928 // activity destruction. In that case, don't update the search view because
929 // that would bring the user back to the search fragment regardless of the
930 // previous state of the application. Instead, just return here and let the
931 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700932 if (!TextUtils.isEmpty(normalizedQuery)) {
933 mPendingSearchViewQuery = normalizedQuery;
934 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700935 return;
936 }
Yorke Lee53a22302014-04-29 18:13:46 -0700937 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700938 }
939 }
Yorke Leec3766332013-07-31 11:13:16 -0700940
941 @Override
942 public void onListFragmentScrollStateChange(int scrollState) {
943 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700944 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700945 hideInputMethod(getCurrentFocus());
946 }
947 }
948
949 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700950 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700951 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700952 // TODO: No-op for now. This should eventually show/hide the actionBar based on
953 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700954 }
955
Yorke Lee86e21f72014-04-02 16:49:38 -0700956 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700957 public void setFloatingActionButtonVisible(boolean visible) {
958 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700959 }
960
961 private boolean phoneIsInUse() {
962 final TelephonyManager tm = (TelephonyManager) getSystemService(
963 Context.TELEPHONY_SERVICE);
964 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
965 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700966
Yorke Leee1424812013-09-04 18:24:48 -0700967 public static Intent getAddNumberToContactIntent(CharSequence text) {
968 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
969 intent.putExtra(Intents.Insert.PHONE, text);
970 intent.setType(Contacts.CONTENT_ITEM_TYPE);
971 return intent;
972 }
973
Yorke Leeda0f9042013-12-05 14:25:51 -0800974 private boolean canIntentBeHandled(Intent intent) {
975 final PackageManager packageManager = getPackageManager();
976 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
977 PackageManager.MATCH_DEFAULT_ONLY);
978 return resolveInfo != null && resolveInfo.size() > 0;
979 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800980
Yorke Lee6a1461a2014-04-21 16:27:14 -0700981 @Override
982 public void showCallHistory() {
983 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
984 // CONTENT_TYPE, so that we always open our call log from our dialer
985 final Intent intent = new Intent(this, CallLogActivity.class);
986 startActivity(intent);
987 }
988
Yorke Lee86e21f72014-04-02 16:49:38 -0700989 /**
990 * Called when the user has long-pressed a contact tile to start a drag operation.
991 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800992 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700993 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee5253be02014-05-21 18:50:03 -0700994 mActionBarController.slideActionBarUp(true);
Yorke Lee28266192014-05-01 10:05:52 -0700995 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800996 }
997
998 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700999 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1000 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001001
Yorke Lee86e21f72014-04-02 16:49:38 -07001002 /**
1003 * Called when the user has released a contact tile after long-pressing it.
1004 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001005 @Override
1006 public void onDragFinished(int x, int y) {
Yorke Lee5253be02014-05-21 18:50:03 -07001007 mActionBarController.slideActionBarDown(true);
Yorke Lee28266192014-05-01 10:05:52 -07001008 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001009 }
1010
1011 @Override
1012 public void onDroppedOnRemove() {}
1013
1014 /**
Yorke Leed999b712014-04-23 15:10:58 -07001015 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001016 * once it has been attached to the activity.
1017 */
1018 @Override
1019 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001020 mDragDropController = dragController;
1021 ((RemoveView) findViewById(R.id.remove_view))
1022 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001023 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001024
1025 @Override
1026 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001027 // Specify call-origin so that users will see the previous tab instead of
1028 // CallLog screen (search UI will be automatically exited).
1029 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001030 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001031 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001032 }
1033
1034 @Override
1035 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001036 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
1037 startActivity(intent);
1038 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001039 }
1040
1041 @Override
1042 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001043 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001044 }
1045
1046 @Override
1047 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001048 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001049 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001050
Yorke Leecc4660d2014-04-24 11:22:57 -07001051 @Override
1052 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1053
1054 }
1055
1056 @Override
1057 public void onPageSelected(int position) {
1058 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001059 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001060 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001061 alignFloatingActionButtonByTab(mCurrentTabPosition);
1062 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001063 }
1064
1065 @Override
1066 public void onPageScrollStateChanged(int state) {
1067 }
1068
Andrew Lee2a58ab82014-05-15 02:16:04 -07001069 private void updateFloatingActionButton() {
1070 if (mIsDialpadShown) {
1071 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1072 mFloatingActionButton.setContentDescription(
1073 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001074 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001075 } else {
1076 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1077 mFloatingActionButton.setContentDescription(
1078 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001079 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001080 }
1081 }
1082
Yorke Leecc4660d2014-04-24 11:22:57 -07001083 private void alignFloatingActionButtonByTab(int position) {
1084 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1085 alignFloatingActionButtonMiddle();
1086 } else {
1087 alignFloatingActionButtonRight();
1088 }
1089 }
1090
1091 private void alignFloatingActionButtonRight() {
1092 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001093 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001094 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1095 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001096 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001097 }
1098
1099 private void alignFloatingActionButtonMiddle() {
1100 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001101 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001102 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1103 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001104 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001105 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001106
1107 /**
1108 * Convenience class which implements AnimationListener interface as null-op methods.
1109 */
1110 private class ActivityAnimationListener implements AnimationListener {
1111 @Override
1112 public void onAnimationStart(Animation animation) {
1113 }
1114
1115 @Override
1116 public void onAnimationEnd(Animation animation) {
1117 }
1118
1119 @Override
1120 public void onAnimationRepeat(Animation animation) {
1121 }
1122 }
Yorke Lee5253be02014-05-21 18:50:03 -07001123
1124 @Override
1125 public boolean isActionBarShowing() {
1126 return mActionBarController.isActionBarShowing();
1127 }
1128
1129 @Override
1130 public int getActionBarHideOffset() {
1131 return getActionBar().getHideOffset();
1132 }
1133
1134 @Override
1135 public int getActionBarHeight() {
1136 return mActionBarHeight;
1137 }
1138
1139 @Override
1140 public void setActionBarHideOffset(int hideOffset) {
1141 getActionBar().setHideOffset(hideOffset);
1142 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001143}