blob: d8a02acbaea7138e8545fe7c1b41d6ef77f490b9 [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 Lee33932ff2014-04-16 13:34:32 -0700356 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
357 new int[] { android.R.attr.actionBarSize });
358 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
359 styledAttributes.recycle();
360
Yorke Leeef2b7382013-08-09 17:39:25 -0700361 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
362 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700363 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800364 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700365 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800366 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
367 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700368 } else {
369 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700370 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
371 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700372 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee5253be02014-05-21 18:50:03 -0700373 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700374 }
375
Andrew Lee0c667702014-05-12 14:31:45 -0700376 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
377 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
378 parentLayout.setOnDragListener(new LayoutOnDragListener());
379
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700380 setupActivityOverlay();
381
Andrew Leea73e1892014-05-13 13:21:16 -0700382 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
383 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700384
Andrew Leea73e1892014-05-13 13:21:16 -0700385 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
386 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800387
Yorke Lee28266192014-05-01 10:05:52 -0700388 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800389
Yorke Lee0baa98b2013-08-22 10:55:16 -0700390 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700391 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700392 }
393
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700394 private void setupActivityOverlay() {
395 final View activityOverlay = findViewById(R.id.activity_overlay);
396 activityOverlay.setOnTouchListener(new OnTouchListener() {
397 @Override
398 public boolean onTouch(View v, MotionEvent event) {
399 if (!mIsDialpadShown) {
400 maybeExitSearchUi();
401 }
402 return false;
403 }
404 });
405 }
406
Chiao Cheng94b10b52012-08-17 16:59:12 -0700407 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700408 protected void onResume() {
409 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700410 if (mFirstLaunch) {
411 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700412 } else if (!phoneIsInUse() && mInCallDialpadUp) {
413 hideDialpadFragment(false, true);
414 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700415 }
416 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700417 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700418 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700419 }
420
421 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700422 protected void onPause() {
423 if (mClearSearchOnPause) {
424 hideDialpadAndSearchUi();
425 mClearSearchOnPause = false;
426 }
427 super.onPause();
428 }
429
430 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700431 protected void onSaveInstanceState(Bundle outState) {
432 super.onSaveInstanceState(outState);
433 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700434 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
435 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700436 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee5253be02014-05-21 18:50:03 -0700437 mActionBarController.saveInstanceState(outState);
Yorke Leeef2b7382013-08-09 17:39:25 -0700438 }
439
440 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700441 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700442 if (fragment instanceof DialpadFragment) {
443 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700444 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700445 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700446 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700447 } else if (fragment instanceof SmartDialSearchFragment) {
448 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700449 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700450 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700451 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700452 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700453 } else if (fragment instanceof ListsFragment) {
454 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700455 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700456 }
Yorke Leec3766332013-07-31 11:13:16 -0700457 }
458
Alon Albertb453e5b2013-09-10 15:54:23 -0700459 protected void handleMenuSettings() {
460 openTelephonySetting(this);
461 }
462
463 public static void openTelephonySetting(Activity activity) {
464 final Intent settingsIntent = getCallSettingsIntent();
465 activity.startActivity(settingsIntent);
466 }
467
Chiao Cheng94b10b52012-08-17 16:59:12 -0700468 @Override
469 public void onClick(View view) {
470 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700471 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700472 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700473 mInCallDialpadUp = false;
474 showDialpadFragment(true);
475 } else {
476 // Dial button was pressed; tell the Dialpad fragment
477 mDialpadFragment.dialButtonPressed();
478 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700479 break;
Yorke Leec3766332013-07-31 11:13:16 -0700480 case R.id.search_close_button:
481 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700482 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700483 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700484 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700485 }
486 break;
Yorke Leec3766332013-07-31 11:13:16 -0700487 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800488 try {
489 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
490 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
491 } catch (ActivityNotFoundException e) {
492 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
493 Toast.LENGTH_SHORT).show();
494 }
Yorke Leec3766332013-07-31 11:13:16 -0700495 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700496 case R.id.dialtacts_options_menu_button:
497 buildOptionsMenu(view).show();
498 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700499 default: {
500 Log.wtf(TAG, "Unexpected onClick event from " + view);
501 break;
502 }
503 }
504 }
505
Yorke Leec3766332013-07-31 11:13:16 -0700506 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700507 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700508 switch (item.getItemId()) {
509 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700510 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700511 break;
512 case R.id.menu_add_contact:
513 try {
514 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
515 } catch (ActivityNotFoundException e) {
516 Toast toast = Toast.makeText(this,
517 R.string.add_contact_not_available,
518 Toast.LENGTH_SHORT);
519 toast.show();
520 }
521 break;
522 case R.id.menu_import_export:
523 // We hard-code the "contactsAreAvailable" argument because doing it properly would
524 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
525 // now in Dialtacts for (potential) performance reasons. Compare with how it is
526 // done in {@link PeopleActivity}.
527 ImportExportDialogFragment.show(getFragmentManager(), true,
528 DialtactsActivity.class);
529 return true;
530 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700531 ClearFrequentsDialog.show(getFragmentManager());
532 return true;
533 case R.id.menu_call_settings:
534 handleMenuSettings();
535 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700536 }
537 return false;
538 }
539
540 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700541 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
542 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
543 if (resultCode == RESULT_OK) {
544 final ArrayList<String> matches = data.getStringArrayListExtra(
545 RecognizerIntent.EXTRA_RESULTS);
546 if (matches.size() > 0) {
547 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700548 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700549 } else {
550 Log.e(TAG, "Voice search - nothing heard");
551 }
552 } else {
553 Log.e(TAG, "Voice search failed");
554 }
555 }
556 super.onActivityResult(requestCode, resultCode, data);
557 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700558
Andrew Lee2a58ab82014-05-15 02:16:04 -0700559 /**
560 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
561 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
562 * @see #onDialpadShown
563 */
Yorke Leec3766332013-07-31 11:13:16 -0700564 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700565 if (mIsDialpadShown) {
566 return;
567 }
568 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700569 mDialpadFragment.setAnimate(animate);
570
Chiao Cheng94b10b52012-08-17 16:59:12 -0700571 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700572 ft.show(mDialpadFragment);
573 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700574
Yorke Lee5253be02014-05-21 18:50:03 -0700575 mActionBarController.onDialpadUp();
576
Andrew Leeb1903842014-05-15 16:11:24 -0700577 if (!isInSearchUi()) {
578 enterSearchUi(true /* isSmartDial */, mSearchQuery);
579 }
Yorke Leec3766332013-07-31 11:13:16 -0700580 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700581
Andrew Lee2a58ab82014-05-15 02:16:04 -0700582 /**
583 * Callback from child DialpadFragment when the dialpad is shown.
584 */
585 public void onDialpadShown() {
586 updateFloatingActionButton();
587 if (mDialpadFragment.getAnimate()) {
588 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
589 mDialpadFragment.getView().startAnimation(slideIn);
590 } else {
591 mDialpadFragment.setYFraction(0);
592 }
593
Andrew Lee2a58ab82014-05-15 02:16:04 -0700594 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700595 }
596
597 /**
598 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
599 * a callback after the hide animation ends.
600 * @see #commitDialpadFragmentHide
601 */
Yorke Leeca195042013-09-25 16:29:38 -0700602 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700603 if (mDialpadFragment == null) {
604 return;
605 }
Yorke Leef6673d32013-08-23 15:34:17 -0700606 if (clearDialpad) {
607 mDialpadFragment.clearDialpad();
608 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700609 if (!mIsDialpadShown) {
610 return;
Yorke Leec3766332013-07-31 11:13:16 -0700611 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700612 mIsDialpadShown = false;
613 mDialpadFragment.setAnimate(animate);
614
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700615 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700616 updateFloatingActionButton();
617 if (animate) {
618 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
619 slideOut.setAnimationListener(new ActivityAnimationListener() {
620 @Override
621 public void onAnimationEnd(Animation animation) {
622 commitDialpadFragmentHide();
623 }
624 });
625 mDialpadFragment.getView().startAnimation(slideOut);
626 } else {
627 commitDialpadFragmentHide();
628 }
629
Yorke Lee5253be02014-05-21 18:50:03 -0700630 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700631
Andrew Leed4cde832014-05-16 15:56:48 -0700632 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700633 if (TextUtils.isEmpty(mSearchQuery)) {
634 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700635 }
Andrew Leed4cde832014-05-16 15:56:48 -0700636 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700637 }
638
639 /**
640 * Finishes hiding the dialpad fragment after any animations are completed.
641 */
642 private void commitDialpadFragmentHide() {
643 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700644 ft.hide(mDialpadFragment);
645 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700646 }
647
Andrew Lee2a58ab82014-05-15 02:16:04 -0700648 private void updateSearchFragmentPosition() {
649 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
650 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700651 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700652 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700653 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700654 fragment = mRegularSearchFragment;
655 }
656 if (fragment != null && fragment.isVisible()) {
657 fragment.getView().animate().translationY(translationValue)
658 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
659 }
660 }
661
Yorke Lee5253be02014-05-21 18:50:03 -0700662 @Override
663 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700664 return mInDialpadSearch || mInRegularSearch;
665 }
666
Yorke Lee5253be02014-05-21 18:50:03 -0700667 @Override
668 public boolean hasSearchQuery() {
669 return !TextUtils.isEmpty(mSearchQuery);
670 }
671
672 @Override
673 public boolean shouldShowActionBar() {
674 return mListsFragment.shouldShowActionBar();
675 }
676
Yorke Leeb207f8a2013-08-29 18:51:06 -0700677 private void setNotInSearchUi() {
678 mInDialpadSearch = false;
679 mInRegularSearch = false;
680 }
681
Yorke Lee311969c2013-08-26 06:31:11 -0700682 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700683 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700684 hideDialpadFragment(false, true);
685 }
686
Yorke Leee00c9fe2014-04-12 12:42:06 -0700687 private void hideInputMethod(View view) {
688 final InputMethodManager imm = (InputMethodManager) getSystemService(
689 Context.INPUT_METHOD_SERVICE);
690 if (imm != null && view != null) {
691 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
692 }
693 }
694
Yorke Lee53a22302014-04-29 18:13:46 -0700695 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700696 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
697 if (canIntentBeHandled(voiceIntent)) {
698 mVoiceSearchButton.setVisibility(View.VISIBLE);
699 mVoiceSearchButton.setOnClickListener(this);
700 } else {
701 mVoiceSearchButton.setVisibility(View.GONE);
702 }
703 }
704
Andrew Leee74a10e2014-05-16 14:31:40 -0700705 private OptionsPopupMenu buildOptionsMenu(View invoker) {
706 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
707 popupMenu.inflate(R.menu.dialtacts_options);
708 popupMenu.setOnMenuItemClickListener(this);
709 return popupMenu;
710 }
711
Yorke Lee86e21f72014-04-02 16:49:38 -0700712 @Override
713 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700714 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700715 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700716 mPendingSearchViewQuery = null;
717 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700718 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700719 }
720
Chiao Cheng94b10b52012-08-17 16:59:12 -0700721 /**
722 * Returns true if the intent is due to hitting the green send key (hardware call button:
723 * KEYCODE_CALL) while in a call.
724 *
725 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700726 * @return true if the intent is due to hitting the green send key while in a call
727 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700728 private boolean isSendKeyWhileInCall(Intent intent) {
729 // If there is a call in progress and the user launched the dialer by hitting the call
730 // button, go straight to the in-call screen.
731 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700732
Yorke Lee62e995c2014-03-28 10:02:56 -0700733 try {
734 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
735 if (callKey && phone != null && phone.showCallScreen()) {
736 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700737 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700738 } catch (RemoteException e) {
739 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700740 }
741
742 return false;
743 }
744
745 /**
746 * Sets the current tab based on the intent's request type
747 *
748 * @param intent Intent that contains information about which tab should be selected
749 */
Yorke Leec3766332013-07-31 11:13:16 -0700750 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700751 // 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 -0700752 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700753 finish();
754 return;
755 }
756
Yorke Lee669b6082013-09-17 15:43:38 -0700757 if (mDialpadFragment != null) {
758 final boolean phoneIsInUse = phoneIsInUse();
759 if (phoneIsInUse || isDialIntent(intent)) {
760 mDialpadFragment.setStartedFromNewIntent(true);
761 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
762 mInCallDialpadUp = true;
763 }
764 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700765 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700766 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700767 }
768
769 @Override
770 public void onNewIntent(Intent newIntent) {
771 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700772 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700773
Chiao Cheng94b10b52012-08-17 16:59:12 -0700774 invalidateOptionsMenu();
775 }
776
777 /** Returns true if the given intent contains a phone number to populate the dialer with */
778 private boolean isDialIntent(Intent intent) {
779 final String action = intent.getAction();
780 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
781 return true;
782 }
783 if (Intent.ACTION_VIEW.equals(action)) {
784 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700785 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700786 return true;
787 }
788 }
789 return false;
790 }
791
792 /**
793 * Returns an appropriate call origin for this Activity. May return null when no call origin
794 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
795 * for remembering the tab in which the user made a phone call, so the external app's DIAL
796 * request should not be counted.)
797 */
798 public String getCallOrigin() {
799 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
800 }
801
Chiao Cheng94b10b52012-08-17 16:59:12 -0700802 /**
Yorke Leec3766332013-07-31 11:13:16 -0700803 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700804 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700805 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700806 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700807 // Weird race condition where fragment is doing work after the activity is destroyed
808 // due to talkback being on (b/10209937). Just return since we can't do any
809 // constructive here.
810 return;
811 }
812
Yorke Leeef2b7382013-08-09 17:39:25 -0700813 if (DEBUG) {
814 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
815 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700816
Yorke Leec3766332013-07-31 11:13:16 -0700817 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700818 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700819 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700820 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700821 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700822 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700823
Yorke Leeb207f8a2013-08-29 18:51:06 -0700824 final String tag;
825 if (smartDialSearch) {
826 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
827 } else {
828 tag = TAG_REGULAR_SEARCH_FRAGMENT;
829 }
830 mInDialpadSearch = smartDialSearch;
831 mInRegularSearch = !smartDialSearch;
832
Andrew Lee752956e2014-05-15 11:43:38 -0700833 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700834 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700835 if (fragment == null) {
836 if (smartDialSearch) {
837 fragment = new SmartDialSearchFragment();
838 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700839 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700840 }
Andrew Leeb1903842014-05-15 16:11:24 -0700841 transaction.add(R.id.dialtacts_frame, fragment, tag);
842 } else {
843 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700844 }
Andrew Leeb1903842014-05-15 16:11:24 -0700845
Yorke Lee86e21f72014-04-02 16:49:38 -0700846 // DialtactsActivity will provide the options menu
847 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700848 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700849 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700850 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700851
852 mListsFragment.getView().animate().alpha(0).withLayer();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700853 }
854
855 /**
Yorke Leec3766332013-07-31 11:13:16 -0700856 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700857 */
Yorke Leec3766332013-07-31 11:13:16 -0700858 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700859 // See related bug in enterSearchUI();
860 if (getFragmentManager().isDestroyed()) {
861 return;
862 }
Andrew Leeb1903842014-05-15 16:11:24 -0700863
Andrew Leeb1903842014-05-15 16:11:24 -0700864 mSearchView.setText(null);
865 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700866 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700867
Andrew Leeb1903842014-05-15 16:11:24 -0700868 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
869 transaction.setCustomAnimations(0, android.R.animator.fade_out);
Andrew Leeb1903842014-05-15 16:11:24 -0700870 if (mSmartDialSearchFragment != null) {
871 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700872 }
Andrew Leeb1903842014-05-15 16:11:24 -0700873 if (mRegularSearchFragment != null) {
874 transaction.remove(mRegularSearchFragment);
875 }
876 transaction.commit();
877
878 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Lee5253be02014-05-21 18:50:03 -0700879 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700880 }
881
882 /** Returns an Intent to launch Call Settings screen */
883 public static Intent getCallSettingsIntent() {
884 final Intent intent = new Intent(Intent.ACTION_MAIN);
885 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
886 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
887 return intent;
888 }
889
890 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700891 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700892 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700893 if (TextUtils.isEmpty(mSearchQuery)) {
894 exitSearchUi();
895 }
Yorke Leef6673d32013-08-23 15:34:17 -0700896 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700897 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700898 exitSearchUi();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700899 hideInputMethod(parentLayout);
Yorke Leec3766332013-07-31 11:13:16 -0700900 } else {
901 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700902 }
Yorke Leec3766332013-07-31 11:13:16 -0700903 }
904
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700905 /**
906 * @return True if the search UI was exited, false otherwise
907 */
908 private boolean maybeExitSearchUi() {
909 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
910 exitSearchUi();
911 hideInputMethod(parentLayout);
912 return true;
913 }
914 return false;
915 }
916
Yorke Leec3766332013-07-31 11:13:16 -0700917 @Override
918 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700919 if (mSmartDialSearchFragment != null) {
920 mSmartDialSearchFragment.setAddToContactNumber(query);
921 }
Yorke Leec3766332013-07-31 11:13:16 -0700922 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
923 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700924
925 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700926 if (DEBUG) {
927 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
928 }
929 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
930 // This callback can happen if the dialpad fragment is recreated because of
931 // activity destruction. In that case, don't update the search view because
932 // that would bring the user back to the search fragment regardless of the
933 // previous state of the application. Instead, just return here and let the
934 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700935 if (!TextUtils.isEmpty(normalizedQuery)) {
936 mPendingSearchViewQuery = normalizedQuery;
937 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700938 return;
939 }
Yorke Lee53a22302014-04-29 18:13:46 -0700940 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700941 }
942 }
Yorke Leec3766332013-07-31 11:13:16 -0700943
944 @Override
945 public void onListFragmentScrollStateChange(int scrollState) {
946 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700947 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700948 hideInputMethod(getCurrentFocus());
949 }
950 }
951
952 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700953 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700954 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700955 // TODO: No-op for now. This should eventually show/hide the actionBar based on
956 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700957 }
958
Yorke Lee86e21f72014-04-02 16:49:38 -0700959 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700960 public void setFloatingActionButtonVisible(boolean visible) {
961 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700962 }
963
964 private boolean phoneIsInUse() {
965 final TelephonyManager tm = (TelephonyManager) getSystemService(
966 Context.TELEPHONY_SERVICE);
967 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
968 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700969
Yorke Leee1424812013-09-04 18:24:48 -0700970 public static Intent getAddNumberToContactIntent(CharSequence text) {
971 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
972 intent.putExtra(Intents.Insert.PHONE, text);
973 intent.setType(Contacts.CONTENT_ITEM_TYPE);
974 return intent;
975 }
976
Yorke Leeda0f9042013-12-05 14:25:51 -0800977 private boolean canIntentBeHandled(Intent intent) {
978 final PackageManager packageManager = getPackageManager();
979 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
980 PackageManager.MATCH_DEFAULT_ONLY);
981 return resolveInfo != null && resolveInfo.size() > 0;
982 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800983
Yorke Lee6a1461a2014-04-21 16:27:14 -0700984 @Override
985 public void showCallHistory() {
986 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
987 // CONTENT_TYPE, so that we always open our call log from our dialer
988 final Intent intent = new Intent(this, CallLogActivity.class);
989 startActivity(intent);
990 }
991
Yorke Lee86e21f72014-04-02 16:49:38 -0700992 /**
993 * Called when the user has long-pressed a contact tile to start a drag operation.
994 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800995 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700996 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee5253be02014-05-21 18:50:03 -0700997 mActionBarController.slideActionBarUp(true);
Yorke Lee28266192014-05-01 10:05:52 -0700998 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800999 }
1000
1001 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001002 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1003 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001004
Yorke Lee86e21f72014-04-02 16:49:38 -07001005 /**
1006 * Called when the user has released a contact tile after long-pressing it.
1007 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001008 @Override
1009 public void onDragFinished(int x, int y) {
Yorke Lee5253be02014-05-21 18:50:03 -07001010 mActionBarController.slideActionBarDown(true);
Yorke Lee28266192014-05-01 10:05:52 -07001011 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001012 }
1013
1014 @Override
1015 public void onDroppedOnRemove() {}
1016
1017 /**
Yorke Leed999b712014-04-23 15:10:58 -07001018 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001019 * once it has been attached to the activity.
1020 */
1021 @Override
1022 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001023 mDragDropController = dragController;
1024 ((RemoveView) findViewById(R.id.remove_view))
1025 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001026 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001027
1028 @Override
1029 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001030 // Specify call-origin so that users will see the previous tab instead of
1031 // CallLog screen (search UI will be automatically exited).
1032 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001033 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001034 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001035 }
1036
1037 @Override
1038 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001039 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
1040 startActivity(intent);
1041 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001042 }
1043
1044 @Override
1045 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001046 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001047 }
1048
1049 @Override
1050 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001051 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001052 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001053
Yorke Leecc4660d2014-04-24 11:22:57 -07001054 @Override
1055 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1056
1057 }
1058
1059 @Override
1060 public void onPageSelected(int position) {
1061 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001062 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001063 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001064 alignFloatingActionButtonByTab(mCurrentTabPosition);
1065 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001066 }
1067
1068 @Override
1069 public void onPageScrollStateChanged(int state) {
1070 }
1071
Andrew Lee2a58ab82014-05-15 02:16:04 -07001072 private void updateFloatingActionButton() {
1073 if (mIsDialpadShown) {
1074 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1075 mFloatingActionButton.setContentDescription(
1076 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001077 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001078 } else {
1079 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1080 mFloatingActionButton.setContentDescription(
1081 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001082 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001083 }
1084 }
1085
Yorke Leecc4660d2014-04-24 11:22:57 -07001086 private void alignFloatingActionButtonByTab(int position) {
1087 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1088 alignFloatingActionButtonMiddle();
1089 } else {
1090 alignFloatingActionButtonRight();
1091 }
1092 }
1093
1094 private void alignFloatingActionButtonRight() {
1095 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001096 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001097 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1098 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001099 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001100 }
1101
1102 private void alignFloatingActionButtonMiddle() {
1103 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001104 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001105 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1106 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001107 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001108 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001109
1110 /**
1111 * Convenience class which implements AnimationListener interface as null-op methods.
1112 */
1113 private class ActivityAnimationListener implements AnimationListener {
1114 @Override
1115 public void onAnimationStart(Animation animation) {
1116 }
1117
1118 @Override
1119 public void onAnimationEnd(Animation animation) {
1120 }
1121
1122 @Override
1123 public void onAnimationRepeat(Animation animation) {
1124 }
1125 }
Yorke Lee5253be02014-05-21 18:50:03 -07001126
1127 @Override
1128 public boolean isActionBarShowing() {
1129 return mActionBarController.isActionBarShowing();
1130 }
1131
1132 @Override
1133 public int getActionBarHideOffset() {
1134 return getActionBar().getHideOffset();
1135 }
1136
1137 @Override
1138 public int getActionBarHeight() {
1139 return mActionBarHeight;
1140 }
1141
1142 @Override
1143 public void setActionBarHideOffset(int hideOffset) {
1144 getActionBar().setHideOffset(hideOffset);
1145 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001146}