blob: 82b78381cf9907f3989dd8292f34f12651456cc4 [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;
Andrew Lee3c14bc32014-05-28 15:43:03 -070067import com.android.contacts.common.animation.AnimationListenerAdapter;
Yorke Leec3766332013-07-31 11:13:16 -070068import com.android.contacts.common.dialog.ClearFrequentsDialog;
69import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080070import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070071import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070073import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070074import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070075import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070076import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070077import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080078import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070079import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080080import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070081import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070082import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070083import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070084import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080085import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070086import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070087import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee5253be02014-05-21 18:50:03 -070088import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070089import com.android.dialer.widget.SearchEditTextLayout;
Yorke Lee11ca39e2014-05-19 20:31:37 -070090import com.android.dialer.widget.SearchEditTextLayout.OnBackButtonClickedListener;
Yorke Lee0baa98b2013-08-22 10:55:16 -070091import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070092import com.android.internal.telephony.ITelephony;
93
Yorke Leec3766332013-07-31 11:13:16 -070094import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080095import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070096
Chiao Cheng94b10b52012-08-17 16:59:12 -070097/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070098 * The dialer tab's title is 'phone', a more common name (see strings.xml).
99 */
Yorke Leec3766332013-07-31 11:13:16 -0700100public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700101 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700102 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -0800103 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700104 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700105 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700106 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700107 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700108 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700109 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700110 ViewPager.OnPageChangeListener,
111 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700112 private static final String TAG = "DialtactsActivity";
113
Ihab Awad526c0b82014-02-27 12:55:36 -0800114 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115
Yorke Leef74011e2013-08-02 11:30:30 -0700116 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
117
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118 /** Used to open Call Setting */
119 private static final String PHONE_PACKAGE = "com.android.phone";
120 private static final String CALL_SETTINGS_CLASS_NAME =
121 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700122 /** @see #getCallOrigin() */
123 private static final String CALL_ORIGIN_DIALTACTS =
124 "com.android.dialer.DialtactsActivity";
125
Yorke Leeb207f8a2013-08-29 18:51:06 -0700126 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
127 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700128 private static final String KEY_SEARCH_QUERY = "search_query";
129 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700130 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700131
Yorke Leec3766332013-07-31 11:13:16 -0700132 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
133 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
134 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
135 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700136
Chiao Cheng94b10b52012-08-17 16:59:12 -0700137 /**
138 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
139 */
140 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
141
Yorke Leec3766332013-07-31 11:13:16 -0700142 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700143
Yorke Lee86e21f72014-04-02 16:49:38 -0700144 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800145
Andrew Lee0c667702014-05-12 14:31:45 -0700146 private RelativeLayout parentLayout;
147
Yorke Leec3766332013-07-31 11:13:16 -0700148 /**
Yorke Leec3766332013-07-31 11:13:16 -0700149 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700150 */
Yorke Leef74011e2013-08-02 11:30:30 -0700151 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700152
153 /**
154 * Fragment for searching phone numbers using the alphanumeric keyboard.
155 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700156 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700157
158 /**
159 * Fragment for searching phone numbers using the dialpad.
160 */
161 private SmartDialSearchFragment mSmartDialSearchFragment;
162
Yorke Leee00c9fe2014-04-12 12:42:06 -0700163 /**
164 * Fragment containing the speed dial list, recents list, and all contacts list.
165 */
166 private ListsFragment mListsFragment;
167
Andrew Leea73e1892014-05-13 13:21:16 -0700168 private View mFloatingActionButtonContainer;
169 private ImageButton mFloatingActionButton;
Yorke Leec3766332013-07-31 11:13:16 -0700170
Yorke Lee33932ff2014-04-16 13:34:32 -0700171 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700172 private boolean mInDialpadSearch;
173 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700174 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700175 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700176 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700177
Yorke Leeef2b7382013-08-09 17:39:25 -0700178 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700179 * The position of the currently selected tab in the attached {@link ListsFragment}.
180 */
181 private int mCurrentTabPosition = 0;
182
183 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700184 * True if the dialpad is only temporarily showing due to being in call
185 */
186 private boolean mInCallDialpadUp;
187
188 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700189 * True when this activity has been launched for the first time.
190 */
Yorke Lee98702de2013-08-06 12:03:32 -0700191 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700192
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700193 /**
194 * Search query to be applied to the SearchView in the ActionBar once
195 * onCreateOptionsMenu has been called.
196 */
197 private String mPendingSearchViewQuery;
198
Yorke Lee53a22302014-04-29 18:13:46 -0700199 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700200 private View mVoiceSearchButton;
Yorke Leeec489fb2014-05-19 15:39:27 -0700201 private SearchEditTextLayout mSearchEditTextLayout;
Andrew Leee74a10e2014-05-16 14:31:40 -0700202
Yorke Lee86e21f72014-04-02 16:49:38 -0700203 /**
204 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
205 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
206 * removed from the speed dial list.
207 */
Yorke Lee28266192014-05-01 10:05:52 -0700208 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700209
Yorke Lee86e21f72014-04-02 16:49:38 -0700210 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
211 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700212 private String mSearchQuery;
213
Yorke Leefce269a2013-08-12 11:56:04 -0700214 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700215 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700216 private ActionBarController mActionBarController;
Yorke Leefce269a2013-08-12 11:56:04 -0700217
Andrew Leee74a10e2014-05-16 14:31:40 -0700218 private class OptionsPopupMenu extends PopupMenu {
219 public OptionsPopupMenu(Context context, View anchor) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700220 super(context, anchor);
221 }
222
223 @Override
224 public void show() {
225 final Menu menu = getMenu();
226 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700227 clearFrequents.setVisible(mListsFragment != null &&
228 mListsFragment.getSpeedDialFragment() != null &&
229 mListsFragment.getSpeedDialFragment().hasFrequents());
Yorke Leee00c9fe2014-04-12 12:42:06 -0700230 super.show();
231 }
232 }
233
Chiao Cheng94b10b52012-08-17 16:59:12 -0700234 /**
Yorke Lee28266192014-05-01 10:05:52 -0700235 * Listener that listens to drag events and sends their x and y coordinates to a
236 * {@link DragDropController}.
237 */
238 private class LayoutOnDragListener implements OnDragListener {
239 @Override
240 public boolean onDrag(View v, DragEvent event) {
241 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
242 mDragDropController.handleDragHovered(v, (int) event.getX(),
243 (int) event.getY());
244 }
245 return true;
246 }
247 }
248
249 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700250 * Listener used to send search queries to the phone search fragment.
251 */
Yorke Lee53a22302014-04-29 18:13:46 -0700252 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700253 @Override
254 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
255 }
Yorke Leec3766332013-07-31 11:13:16 -0700256
Andrew Lee99a570c2014-05-15 14:18:50 -0700257 @Override
258 public void onTextChanged(CharSequence s, int start, int before, int count) {
259 final String newText = s.toString();
260 if (newText.equals(mSearchQuery)) {
261 // If the query hasn't changed (perhaps due to activity being destroyed
262 // and restored, or user launching the same DIAL intent twice), then there is
263 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700264 return;
265 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700266 mSearchQuery = newText;
267 if (DEBUG) {
268 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leec3766332013-07-31 11:13:16 -0700269 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700270
Andrew Lee90374a72014-05-16 15:01:09 -0700271 // Show search fragment only when the query string is changed to non-empty text.
272 if (!TextUtils.isEmpty(newText)) {
273 // Call enterSearchUi only if we are switching search modes, or showing a search
274 // fragment for the first time.
275 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
276 (!mIsDialpadShown && mInRegularSearch);
277 if (!sameSearchMode) {
278 enterSearchUi(mIsDialpadShown, mSearchQuery);
279 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700280 }
281
282 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700283 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700284 } else if (mRegularSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700285 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700286 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700287 }
288
289 @Override
290 public void afterTextChanged(Editable s) {
291 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700292 };
293
Andrew Leeb1903842014-05-15 16:11:24 -0700294
295 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700296 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700297 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700298 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700299 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700300 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700301 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700302 mActionBarController.onSearchBoxTapped();
Andrew Leeb1903842014-05-15 16:11:24 -0700303 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
304 }
Andrew Leeb1903842014-05-15 16:11:24 -0700305 }
306 };
307
308 /**
309 * If the search term is empty and the user closes the soft keyboard, close the search UI.
310 */
311 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
312 @Override
313 public boolean onKey(View v, int keyCode, KeyEvent event) {
314 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
315 TextUtils.isEmpty(mSearchView.getText().toString())) {
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700316 maybeExitSearchUi();
Andrew Leeb1903842014-05-15 16:11:24 -0700317 }
318 return false;
319 }
320 };
321
Chiao Cheng94b10b52012-08-17 16:59:12 -0700322 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700323 protected void onCreate(Bundle savedInstanceState) {
324 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700325 mFirstLaunch = true;
326
Yorke Lee8898cd02013-08-08 10:24:27 -0700327 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800328 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700329
Yorke Lee53a22302014-04-29 18:13:46 -0700330 final ActionBar actionBar = getActionBar();
331 actionBar.setCustomView(R.layout.search_edittext);
332 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700333 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700334
Yorke Lee5253be02014-05-21 18:50:03 -0700335 mActionBarController = new ActionBarController(this,
336 (SearchEditTextLayout) actionBar.getCustomView());
337
Yorke Leeec489fb2014-05-19 15:39:27 -0700338 mSearchEditTextLayout = (SearchEditTextLayout) actionBar.getCustomView();
339 mSearchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700340
Yorke Leeec489fb2014-05-19 15:39:27 -0700341 mSearchView = (EditText) mSearchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700342 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Yorke Lee11ca39e2014-05-19 20:31:37 -0700343 mVoiceSearchButton = mSearchEditTextLayout.findViewById(R.id.voice_search_button);
344 mSearchEditTextLayout.findViewById(R.id.search_box_start_search).setOnClickListener(
345 mSearchViewOnClickListener);
346 mSearchEditTextLayout.setOnBackButtonClickedListener(new OnBackButtonClickedListener() {
347 @Override
348 public void onBackButtonClicked() {
349 onBackPressed();
350 }
351 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700352
Yorke Leeec489fb2014-05-19 15:39:27 -0700353 ImageButton optionsMenuButton = (ImageButton) mSearchEditTextLayout.findViewById(
354 R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700355 optionsMenuButton.setOnClickListener(this);
356 final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
357 optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
358
Yorke Lee3443d4a2014-05-23 19:30:35 -0700359 mActionBarHeight = getResources().getDimensionPixelSize(R.dimen.action_bar_height);
Yorke Lee33932ff2014-04-16 13:34:32 -0700360
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 Lee50aba882014-05-28 20:04:31 -0700373 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700374 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700375 }
376
Andrew Lee0c667702014-05-12 14:31:45 -0700377 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
378 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
379 parentLayout.setOnDragListener(new LayoutOnDragListener());
380
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700381 setupActivityOverlay();
382
Andrew Leea73e1892014-05-13 13:21:16 -0700383 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
384 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700385
Andrew Leea73e1892014-05-13 13:21:16 -0700386 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
387 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800388
Yorke Lee28266192014-05-01 10:05:52 -0700389 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800390
Yorke Lee0baa98b2013-08-22 10:55:16 -0700391 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700392 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700393 }
394
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700395 private void setupActivityOverlay() {
396 final View activityOverlay = findViewById(R.id.activity_overlay);
397 activityOverlay.setOnTouchListener(new OnTouchListener() {
398 @Override
399 public boolean onTouch(View v, MotionEvent event) {
400 if (!mIsDialpadShown) {
401 maybeExitSearchUi();
402 }
403 return false;
404 }
405 });
406 }
407
Chiao Cheng94b10b52012-08-17 16:59:12 -0700408 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700409 protected void onResume() {
410 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700411 if (mFirstLaunch) {
412 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700413 } else if (!phoneIsInUse() && mInCallDialpadUp) {
414 hideDialpadFragment(false, true);
415 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700416 } else if (mShowDialpadOnResume) {
417 showDialpadFragment(false);
418 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700419 }
420 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700421 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700422 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700423 }
424
425 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700426 protected void onPause() {
427 if (mClearSearchOnPause) {
428 hideDialpadAndSearchUi();
429 mClearSearchOnPause = false;
430 }
431 super.onPause();
432 }
433
434 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700435 protected void onSaveInstanceState(Bundle outState) {
436 super.onSaveInstanceState(outState);
437 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700438 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
439 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700440 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700441 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700442 mActionBarController.saveInstanceState(outState);
Yorke Leeef2b7382013-08-09 17:39:25 -0700443 }
444
445 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700446 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700447 if (fragment instanceof DialpadFragment) {
448 mDialpadFragment = (DialpadFragment) fragment;
Yorke Lee50aba882014-05-28 20:04:31 -0700449 if (!mShowDialpadOnResume) {
450 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
451 transaction.hide(mDialpadFragment);
452 transaction.commit();
453 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700454 } else if (fragment instanceof SmartDialSearchFragment) {
455 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700456 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700457 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700458 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700459 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700460 } else if (fragment instanceof ListsFragment) {
461 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700462 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700463 }
Yorke Leec3766332013-07-31 11:13:16 -0700464 }
465
Alon Albertb453e5b2013-09-10 15:54:23 -0700466 protected void handleMenuSettings() {
467 openTelephonySetting(this);
468 }
469
470 public static void openTelephonySetting(Activity activity) {
471 final Intent settingsIntent = getCallSettingsIntent();
472 activity.startActivity(settingsIntent);
473 }
474
Chiao Cheng94b10b52012-08-17 16:59:12 -0700475 @Override
476 public void onClick(View view) {
477 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700478 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700479 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700480 mInCallDialpadUp = false;
481 showDialpadFragment(true);
482 } else {
483 // Dial button was pressed; tell the Dialpad fragment
484 mDialpadFragment.dialButtonPressed();
485 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700486 break;
Yorke Leec3766332013-07-31 11:13:16 -0700487 case R.id.search_close_button:
488 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700489 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700490 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700491 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700492 }
493 break;
Yorke Leec3766332013-07-31 11:13:16 -0700494 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800495 try {
496 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
497 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
498 } catch (ActivityNotFoundException e) {
499 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
500 Toast.LENGTH_SHORT).show();
501 }
Yorke Leec3766332013-07-31 11:13:16 -0700502 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700503 case R.id.dialtacts_options_menu_button:
504 buildOptionsMenu(view).show();
505 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700506 default: {
507 Log.wtf(TAG, "Unexpected onClick event from " + view);
508 break;
509 }
510 }
511 }
512
Yorke Leec3766332013-07-31 11:13:16 -0700513 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700514 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700515 switch (item.getItemId()) {
516 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700517 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700518 break;
519 case R.id.menu_add_contact:
520 try {
521 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
522 } catch (ActivityNotFoundException e) {
523 Toast toast = Toast.makeText(this,
524 R.string.add_contact_not_available,
525 Toast.LENGTH_SHORT);
526 toast.show();
527 }
528 break;
529 case R.id.menu_import_export:
530 // We hard-code the "contactsAreAvailable" argument because doing it properly would
531 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
532 // now in Dialtacts for (potential) performance reasons. Compare with how it is
533 // done in {@link PeopleActivity}.
534 ImportExportDialogFragment.show(getFragmentManager(), true,
535 DialtactsActivity.class);
536 return true;
537 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700538 ClearFrequentsDialog.show(getFragmentManager());
539 return true;
540 case R.id.menu_call_settings:
541 handleMenuSettings();
542 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700543 }
544 return false;
545 }
546
547 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700548 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
549 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
550 if (resultCode == RESULT_OK) {
551 final ArrayList<String> matches = data.getStringArrayListExtra(
552 RecognizerIntent.EXTRA_RESULTS);
553 if (matches.size() > 0) {
554 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700555 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700556 } else {
557 Log.e(TAG, "Voice search - nothing heard");
558 }
559 } else {
560 Log.e(TAG, "Voice search failed");
561 }
562 }
563 super.onActivityResult(requestCode, resultCode, data);
564 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700565
Andrew Lee2a58ab82014-05-15 02:16:04 -0700566 /**
567 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
568 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
569 * @see #onDialpadShown
570 */
Yorke Leec3766332013-07-31 11:13:16 -0700571 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700572 if (mIsDialpadShown) {
573 return;
574 }
575 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700576 mDialpadFragment.setAnimate(animate);
577
Chiao Cheng94b10b52012-08-17 16:59:12 -0700578 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700579 ft.show(mDialpadFragment);
580 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700581
Yorke Lee5253be02014-05-21 18:50:03 -0700582 mActionBarController.onDialpadUp();
583
Andrew Leeb1903842014-05-15 16:11:24 -0700584 if (!isInSearchUi()) {
585 enterSearchUi(true /* isSmartDial */, mSearchQuery);
586 }
Yorke Leec3766332013-07-31 11:13:16 -0700587 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700588
Andrew Lee2a58ab82014-05-15 02:16:04 -0700589 /**
590 * Callback from child DialpadFragment when the dialpad is shown.
591 */
592 public void onDialpadShown() {
593 updateFloatingActionButton();
594 if (mDialpadFragment.getAnimate()) {
595 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
596 mDialpadFragment.getView().startAnimation(slideIn);
597 } else {
598 mDialpadFragment.setYFraction(0);
599 }
600
Andrew Lee2a58ab82014-05-15 02:16:04 -0700601 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700602 }
603
604 /**
605 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
606 * a callback after the hide animation ends.
607 * @see #commitDialpadFragmentHide
608 */
Yorke Leeca195042013-09-25 16:29:38 -0700609 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700610 if (mDialpadFragment == null) {
611 return;
612 }
Yorke Leef6673d32013-08-23 15:34:17 -0700613 if (clearDialpad) {
614 mDialpadFragment.clearDialpad();
615 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700616 if (!mIsDialpadShown) {
617 return;
Yorke Leec3766332013-07-31 11:13:16 -0700618 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700619 mIsDialpadShown = false;
620 mDialpadFragment.setAnimate(animate);
621
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700622 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700623 updateFloatingActionButton();
624 if (animate) {
625 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
Andrew Lee3c14bc32014-05-28 15:43:03 -0700626 slideOut.setAnimationListener(new AnimationListenerAdapter() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700627 @Override
628 public void onAnimationEnd(Animation animation) {
629 commitDialpadFragmentHide();
630 }
631 });
632 mDialpadFragment.getView().startAnimation(slideOut);
633 } else {
634 commitDialpadFragmentHide();
635 }
636
Yorke Lee5253be02014-05-21 18:50:03 -0700637 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700638
Andrew Leed4cde832014-05-16 15:56:48 -0700639 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700640 if (TextUtils.isEmpty(mSearchQuery)) {
641 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700642 }
Andrew Leed4cde832014-05-16 15:56:48 -0700643 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700644 }
645
646 /**
647 * Finishes hiding the dialpad fragment after any animations are completed.
648 */
649 private void commitDialpadFragmentHide() {
650 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700651 ft.hide(mDialpadFragment);
652 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700653 }
654
Andrew Lee2a58ab82014-05-15 02:16:04 -0700655 private void updateSearchFragmentPosition() {
656 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
657 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700658 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700659 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700660 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700661 fragment = mRegularSearchFragment;
662 }
663 if (fragment != null && fragment.isVisible()) {
664 fragment.getView().animate().translationY(translationValue)
665 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
666 }
667 }
668
Yorke Lee5253be02014-05-21 18:50:03 -0700669 @Override
670 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700671 return mInDialpadSearch || mInRegularSearch;
672 }
673
Yorke Lee5253be02014-05-21 18:50:03 -0700674 @Override
675 public boolean hasSearchQuery() {
676 return !TextUtils.isEmpty(mSearchQuery);
677 }
678
679 @Override
680 public boolean shouldShowActionBar() {
681 return mListsFragment.shouldShowActionBar();
682 }
683
Yorke Leeb207f8a2013-08-29 18:51:06 -0700684 private void setNotInSearchUi() {
685 mInDialpadSearch = false;
686 mInRegularSearch = false;
687 }
688
Yorke Lee311969c2013-08-26 06:31:11 -0700689 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700690 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700691 hideDialpadFragment(false, true);
692 }
693
Yorke Leee00c9fe2014-04-12 12:42:06 -0700694 private void hideInputMethod(View view) {
695 final InputMethodManager imm = (InputMethodManager) getSystemService(
696 Context.INPUT_METHOD_SERVICE);
697 if (imm != null && view != null) {
698 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
699 }
700 }
701
Yorke Lee53a22302014-04-29 18:13:46 -0700702 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700703 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
704 if (canIntentBeHandled(voiceIntent)) {
705 mVoiceSearchButton.setVisibility(View.VISIBLE);
706 mVoiceSearchButton.setOnClickListener(this);
707 } else {
708 mVoiceSearchButton.setVisibility(View.GONE);
709 }
710 }
711
Andrew Leee74a10e2014-05-16 14:31:40 -0700712 private OptionsPopupMenu buildOptionsMenu(View invoker) {
713 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
714 popupMenu.inflate(R.menu.dialtacts_options);
715 popupMenu.setOnMenuItemClickListener(this);
716 return popupMenu;
717 }
718
Yorke Lee86e21f72014-04-02 16:49:38 -0700719 @Override
720 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700721 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700722 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700723 mPendingSearchViewQuery = null;
724 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700725 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700726 }
727
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728 /**
729 * Returns true if the intent is due to hitting the green send key (hardware call button:
730 * KEYCODE_CALL) while in a call.
731 *
732 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700733 * @return true if the intent is due to hitting the green send key while in a call
734 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700735 private boolean isSendKeyWhileInCall(Intent intent) {
736 // If there is a call in progress and the user launched the dialer by hitting the call
737 // button, go straight to the in-call screen.
738 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700739
Yorke Lee62e995c2014-03-28 10:02:56 -0700740 try {
741 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
742 if (callKey && phone != null && phone.showCallScreen()) {
743 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700745 } catch (RemoteException e) {
746 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700747 }
748
749 return false;
750 }
751
752 /**
753 * Sets the current tab based on the intent's request type
754 *
755 * @param intent Intent that contains information about which tab should be selected
756 */
Yorke Leec3766332013-07-31 11:13:16 -0700757 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700758 // 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 -0700759 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700760 finish();
761 return;
762 }
763
Yorke Lee669b6082013-09-17 15:43:38 -0700764 if (mDialpadFragment != null) {
765 final boolean phoneIsInUse = phoneIsInUse();
766 if (phoneIsInUse || isDialIntent(intent)) {
767 mDialpadFragment.setStartedFromNewIntent(true);
768 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
769 mInCallDialpadUp = true;
770 }
771 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700772 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700773 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700774 }
775
776 @Override
777 public void onNewIntent(Intent newIntent) {
778 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700779 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700780
Chiao Cheng94b10b52012-08-17 16:59:12 -0700781 invalidateOptionsMenu();
782 }
783
784 /** Returns true if the given intent contains a phone number to populate the dialer with */
785 private boolean isDialIntent(Intent intent) {
786 final String action = intent.getAction();
787 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
788 return true;
789 }
790 if (Intent.ACTION_VIEW.equals(action)) {
791 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700792 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700793 return true;
794 }
795 }
796 return false;
797 }
798
799 /**
800 * Returns an appropriate call origin for this Activity. May return null when no call origin
801 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
802 * for remembering the tab in which the user made a phone call, so the external app's DIAL
803 * request should not be counted.)
804 */
805 public String getCallOrigin() {
806 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
807 }
808
Chiao Cheng94b10b52012-08-17 16:59:12 -0700809 /**
Yorke Leec3766332013-07-31 11:13:16 -0700810 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700811 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700812 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700813 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700814 // Weird race condition where fragment is doing work after the activity is destroyed
815 // due to talkback being on (b/10209937). Just return since we can't do any
816 // constructive here.
817 return;
818 }
819
Yorke Leeef2b7382013-08-09 17:39:25 -0700820 if (DEBUG) {
821 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
822 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700823
Yorke Leec3766332013-07-31 11:13:16 -0700824 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700825 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700826 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700827 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700828 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700829 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700830
Yorke Leeb207f8a2013-08-29 18:51:06 -0700831 final String tag;
832 if (smartDialSearch) {
833 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
834 } else {
835 tag = TAG_REGULAR_SEARCH_FRAGMENT;
836 }
837 mInDialpadSearch = smartDialSearch;
838 mInRegularSearch = !smartDialSearch;
839
Andrew Lee752956e2014-05-15 11:43:38 -0700840 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700841 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700842 if (fragment == null) {
843 if (smartDialSearch) {
844 fragment = new SmartDialSearchFragment();
845 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700846 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700847 }
Andrew Leeb1903842014-05-15 16:11:24 -0700848 transaction.add(R.id.dialtacts_frame, fragment, tag);
849 } else {
850 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700851 }
Andrew Leeb1903842014-05-15 16:11:24 -0700852
Yorke Lee86e21f72014-04-02 16:49:38 -0700853 // DialtactsActivity will provide the options menu
854 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700855 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700856 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700857 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700858
859 mListsFragment.getView().animate().alpha(0).withLayer();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700860 }
861
862 /**
Yorke Leec3766332013-07-31 11:13:16 -0700863 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700864 */
Yorke Leec3766332013-07-31 11:13:16 -0700865 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700866 // See related bug in enterSearchUI();
867 if (getFragmentManager().isDestroyed()) {
868 return;
869 }
Andrew Leeb1903842014-05-15 16:11:24 -0700870
Andrew Leeb1903842014-05-15 16:11:24 -0700871 mSearchView.setText(null);
872 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700873 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700874
Andrew Leeb1903842014-05-15 16:11:24 -0700875 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
876 transaction.setCustomAnimations(0, android.R.animator.fade_out);
Andrew Leeb1903842014-05-15 16:11:24 -0700877 if (mSmartDialSearchFragment != null) {
878 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700879 }
Andrew Leeb1903842014-05-15 16:11:24 -0700880 if (mRegularSearchFragment != null) {
881 transaction.remove(mRegularSearchFragment);
882 }
883 transaction.commit();
884
885 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Lee5253be02014-05-21 18:50:03 -0700886 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700887 }
888
889 /** Returns an Intent to launch Call Settings screen */
890 public static Intent getCallSettingsIntent() {
891 final Intent intent = new Intent(Intent.ACTION_MAIN);
892 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
893 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
894 return intent;
895 }
896
897 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700898 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700899 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700900 if (TextUtils.isEmpty(mSearchQuery)) {
901 exitSearchUi();
902 }
Yorke Leef6673d32013-08-23 15:34:17 -0700903 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700904 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700905 exitSearchUi();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700906 hideInputMethod(parentLayout);
Yorke Leec3766332013-07-31 11:13:16 -0700907 } else {
908 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700909 }
Yorke Leec3766332013-07-31 11:13:16 -0700910 }
911
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700912 /**
913 * @return True if the search UI was exited, false otherwise
914 */
915 private boolean maybeExitSearchUi() {
916 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
917 exitSearchUi();
918 hideInputMethod(parentLayout);
919 return true;
920 }
921 return false;
922 }
923
Yorke Leec3766332013-07-31 11:13:16 -0700924 @Override
925 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700926 if (mSmartDialSearchFragment != null) {
927 mSmartDialSearchFragment.setAddToContactNumber(query);
928 }
Yorke Leec3766332013-07-31 11:13:16 -0700929 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
930 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700931
932 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700933 if (DEBUG) {
934 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
935 }
936 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
937 // This callback can happen if the dialpad fragment is recreated because of
938 // activity destruction. In that case, don't update the search view because
939 // that would bring the user back to the search fragment regardless of the
940 // previous state of the application. Instead, just return here and let the
941 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700942 if (!TextUtils.isEmpty(normalizedQuery)) {
943 mPendingSearchViewQuery = normalizedQuery;
944 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700945 return;
946 }
Yorke Lee53a22302014-04-29 18:13:46 -0700947 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700948 }
949 }
Yorke Leec3766332013-07-31 11:13:16 -0700950
951 @Override
952 public void onListFragmentScrollStateChange(int scrollState) {
953 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700954 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700955 hideInputMethod(getCurrentFocus());
956 }
957 }
958
959 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700960 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700961 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700962 // TODO: No-op for now. This should eventually show/hide the actionBar based on
963 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700964 }
965
Yorke Lee86e21f72014-04-02 16:49:38 -0700966 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700967 public void setFloatingActionButtonVisible(boolean visible) {
968 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700969 }
970
971 private boolean phoneIsInUse() {
972 final TelephonyManager tm = (TelephonyManager) getSystemService(
973 Context.TELEPHONY_SERVICE);
974 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
975 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700976
Yorke Leee1424812013-09-04 18:24:48 -0700977 public static Intent getAddNumberToContactIntent(CharSequence text) {
978 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
979 intent.putExtra(Intents.Insert.PHONE, text);
980 intent.setType(Contacts.CONTENT_ITEM_TYPE);
981 return intent;
982 }
983
Yorke Leeda0f9042013-12-05 14:25:51 -0800984 private boolean canIntentBeHandled(Intent intent) {
985 final PackageManager packageManager = getPackageManager();
986 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
987 PackageManager.MATCH_DEFAULT_ONLY);
988 return resolveInfo != null && resolveInfo.size() > 0;
989 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800990
Yorke Lee6a1461a2014-04-21 16:27:14 -0700991 @Override
992 public void showCallHistory() {
993 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
994 // CONTENT_TYPE, so that we always open our call log from our dialer
995 final Intent intent = new Intent(this, CallLogActivity.class);
996 startActivity(intent);
997 }
998
Yorke Lee86e21f72014-04-02 16:49:38 -0700999 /**
1000 * Called when the user has long-pressed a contact tile to start a drag operation.
1001 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001002 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001003 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee5253be02014-05-21 18:50:03 -07001004 mActionBarController.slideActionBarUp(true);
Yorke Lee28266192014-05-01 10:05:52 -07001005 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001006 }
1007
1008 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001009 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1010 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001011
Yorke Lee86e21f72014-04-02 16:49:38 -07001012 /**
1013 * Called when the user has released a contact tile after long-pressing it.
1014 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001015 @Override
1016 public void onDragFinished(int x, int y) {
Yorke Lee5253be02014-05-21 18:50:03 -07001017 mActionBarController.slideActionBarDown(true);
Yorke Lee28266192014-05-01 10:05:52 -07001018 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001019 }
1020
1021 @Override
1022 public void onDroppedOnRemove() {}
1023
1024 /**
Yorke Leed999b712014-04-23 15:10:58 -07001025 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001026 * once it has been attached to the activity.
1027 */
1028 @Override
1029 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001030 mDragDropController = dragController;
1031 ((RemoveView) findViewById(R.id.remove_view))
1032 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001033 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001034
1035 @Override
1036 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001037 // Specify call-origin so that users will see the previous tab instead of
1038 // CallLog screen (search UI will be automatically exited).
1039 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001040 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001041 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001042 }
1043
1044 @Override
1045 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001046 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
1047 startActivity(intent);
1048 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001049 }
1050
1051 @Override
1052 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001053 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001054 }
1055
1056 @Override
1057 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001058 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001059 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001060
Yorke Leecc4660d2014-04-24 11:22:57 -07001061 @Override
1062 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1063
1064 }
1065
1066 @Override
1067 public void onPageSelected(int position) {
1068 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001069 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001070 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001071 alignFloatingActionButtonByTab(mCurrentTabPosition);
1072 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001073 }
1074
1075 @Override
1076 public void onPageScrollStateChanged(int state) {
1077 }
1078
Andrew Lee2a58ab82014-05-15 02:16:04 -07001079 private void updateFloatingActionButton() {
1080 if (mIsDialpadShown) {
1081 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1082 mFloatingActionButton.setContentDescription(
1083 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001084 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001085 } else {
1086 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1087 mFloatingActionButton.setContentDescription(
1088 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001089 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001090 }
1091 }
1092
Yorke Leecc4660d2014-04-24 11:22:57 -07001093 private void alignFloatingActionButtonByTab(int position) {
1094 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1095 alignFloatingActionButtonMiddle();
1096 } else {
1097 alignFloatingActionButtonRight();
1098 }
1099 }
1100
1101 private void alignFloatingActionButtonRight() {
1102 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001103 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001104 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1105 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001106 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001107 }
1108
1109 private void alignFloatingActionButtonMiddle() {
1110 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001111 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001112 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1113 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001114 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001115 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001116
Yorke Lee5253be02014-05-21 18:50:03 -07001117 @Override
1118 public boolean isActionBarShowing() {
1119 return mActionBarController.isActionBarShowing();
1120 }
1121
1122 @Override
1123 public int getActionBarHideOffset() {
1124 return getActionBar().getHideOffset();
1125 }
1126
1127 @Override
1128 public int getActionBarHeight() {
1129 return mActionBarHeight;
1130 }
1131
1132 @Override
1133 public void setActionBarHideOffset(int hideOffset) {
1134 getActionBar().setHideOffset(hideOffset);
1135 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001136}