blob: c164294c7765f082e08ebc9bc9f898ad9890fcfa [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;
23import android.app.FragmentManager;
24import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080025import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070026import android.content.Context;
27import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080028import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Yorke Lee33932ff2014-04-16 13:34:32 -070030import android.content.res.TypedArray;
Chiao Cheng94b10b52012-08-17 16:59:12 -070031import android.net.Uri;
32import android.os.Bundle;
33import android.os.RemoteException;
34import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070036import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070037import android.speech.RecognizerIntent;
Yorke Leecc4660d2014-04-24 11:22:57 -070038import android.support.v4.view.ViewPager;
Yorke Leec3766332013-07-31 11:13:16 -070039import android.telephony.TelephonyManager;
Yorke Lee53a22302014-04-29 18:13:46 -070040import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070042import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070044import android.view.DragEvent;
Andrew Leeb1903842014-05-15 16:11:24 -070045import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070048import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070050import android.view.View.OnDragListener;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070051import android.view.View.OnTouchListener;
Yorke Lee86e21f72014-04-02 16:49:38 -070052import android.view.animation.AccelerateInterpolator;
Andrew Lee2a58ab82014-05-15 02:16:04 -070053import android.view.animation.Animation;
54import android.view.animation.Animation.AnimationListener;
55import android.view.animation.AnimationUtils;
Yorke Lee86e21f72014-04-02 16:49:38 -070056import android.view.animation.DecelerateInterpolator;
57import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070058import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070059import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070060import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070061import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070062import android.widget.PopupMenu;
Yorke Leecc4660d2014-04-24 11:22:57 -070063import android.widget.RelativeLayout;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080064import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070065
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070066import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070067import com.android.contacts.common.activity.TransactionSafeActivity;
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;
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,
Andrew Lee752956e2014-05-15 11:43:38 -0700105 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700106 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700107 PopupMenu.OnMenuItemClickListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700108 ViewPager.OnPageChangeListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700109 private static final String TAG = "DialtactsActivity";
110
Ihab Awad526c0b82014-02-27 12:55:36 -0800111 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700112
Yorke Leef74011e2013-08-02 11:30:30 -0700113 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
114
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 /** Used to open Call Setting */
116 private static final String PHONE_PACKAGE = "com.android.phone";
117 private static final String CALL_SETTINGS_CLASS_NAME =
118 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119 /** @see #getCallOrigin() */
120 private static final String CALL_ORIGIN_DIALTACTS =
121 "com.android.dialer.DialtactsActivity";
122
Yorke Leeb207f8a2013-08-29 18:51:06 -0700123 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
124 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700125 private static final String KEY_SEARCH_QUERY = "search_query";
126 private static final String KEY_FIRST_LAUNCH = "first_launch";
127
Yorke Leec3766332013-07-31 11:13:16 -0700128 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
129 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
130 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
131 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700132
Chiao Cheng94b10b52012-08-17 16:59:12 -0700133 /**
134 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
135 */
136 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
137
Yorke Leec3766332013-07-31 11:13:16 -0700138 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700139
Yorke Lee86e21f72014-04-02 16:49:38 -0700140 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800141
Andrew Lee0c667702014-05-12 14:31:45 -0700142 private RelativeLayout parentLayout;
143
Yorke Leec3766332013-07-31 11:13:16 -0700144 /**
Yorke Leec3766332013-07-31 11:13:16 -0700145 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700146 */
Yorke Leef74011e2013-08-02 11:30:30 -0700147 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700148
149 /**
150 * Fragment for searching phone numbers using the alphanumeric keyboard.
151 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700152 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700153
154 /**
155 * Fragment for searching phone numbers using the dialpad.
156 */
157 private SmartDialSearchFragment mSmartDialSearchFragment;
158
Yorke Leee00c9fe2014-04-12 12:42:06 -0700159 /**
160 * Fragment containing the speed dial list, recents list, and all contacts list.
161 */
162 private ListsFragment mListsFragment;
163
Andrew Leea73e1892014-05-13 13:21:16 -0700164 private View mFloatingActionButtonContainer;
165 private ImageButton mFloatingActionButton;
Yorke Leec3766332013-07-31 11:13:16 -0700166
Yorke Lee33932ff2014-04-16 13:34:32 -0700167 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700168 private boolean mInDialpadSearch;
169 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700170 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700171 private boolean mIsDialpadShown;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700172
Yorke Leeef2b7382013-08-09 17:39:25 -0700173 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700174 * The position of the currently selected tab in the attached {@link ListsFragment}.
175 */
176 private int mCurrentTabPosition = 0;
177
178 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700179 * True if the dialpad is only temporarily showing due to being in call
180 */
181 private boolean mInCallDialpadUp;
182
183 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700184 * True when this activity has been launched for the first time.
185 */
Yorke Lee98702de2013-08-06 12:03:32 -0700186 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700187
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700188 /**
189 * Search query to be applied to the SearchView in the ActionBar once
190 * onCreateOptionsMenu has been called.
191 */
192 private String mPendingSearchViewQuery;
193
Yorke Lee53a22302014-04-29 18:13:46 -0700194 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700195 private View mVoiceSearchButton;
Yorke Leeec489fb2014-05-19 15:39:27 -0700196 private SearchEditTextLayout mSearchEditTextLayout;
Andrew Leee74a10e2014-05-16 14:31:40 -0700197
Yorke Lee86e21f72014-04-02 16:49:38 -0700198 /**
199 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
200 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
201 * removed from the speed dial list.
202 */
Yorke Lee28266192014-05-01 10:05:52 -0700203 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700204
Yorke Lee86e21f72014-04-02 16:49:38 -0700205 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
206 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700207 private String mSearchQuery;
208
Yorke Leefce269a2013-08-12 11:56:04 -0700209 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700210 private DragDropController mDragDropController;
Yorke Leefce269a2013-08-12 11:56:04 -0700211
Andrew Leee74a10e2014-05-16 14:31:40 -0700212 private class OptionsPopupMenu extends PopupMenu {
213 public OptionsPopupMenu(Context context, View anchor) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700214 super(context, anchor);
215 }
216
217 @Override
218 public void show() {
219 final Menu menu = getMenu();
220 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700221 clearFrequents.setVisible(mListsFragment != null &&
222 mListsFragment.getSpeedDialFragment() != null &&
223 mListsFragment.getSpeedDialFragment().hasFrequents());
Yorke Leee00c9fe2014-04-12 12:42:06 -0700224 super.show();
225 }
226 }
227
Chiao Cheng94b10b52012-08-17 16:59:12 -0700228 /**
Yorke Lee28266192014-05-01 10:05:52 -0700229 * Listener that listens to drag events and sends their x and y coordinates to a
230 * {@link DragDropController}.
231 */
232 private class LayoutOnDragListener implements OnDragListener {
233 @Override
234 public boolean onDrag(View v, DragEvent event) {
235 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
236 mDragDropController.handleDragHovered(v, (int) event.getX(),
237 (int) event.getY());
238 }
239 return true;
240 }
241 }
242
243 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700244 * Listener used to send search queries to the phone search fragment.
245 */
Yorke Lee53a22302014-04-29 18:13:46 -0700246 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700247 @Override
248 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
249 }
Yorke Leec3766332013-07-31 11:13:16 -0700250
Andrew Lee99a570c2014-05-15 14:18:50 -0700251 @Override
252 public void onTextChanged(CharSequence s, int start, int before, int count) {
253 final String newText = s.toString();
254 if (newText.equals(mSearchQuery)) {
255 // If the query hasn't changed (perhaps due to activity being destroyed
256 // and restored, or user launching the same DIAL intent twice), then there is
257 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700258 return;
259 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700260 mSearchQuery = newText;
261 if (DEBUG) {
262 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leec3766332013-07-31 11:13:16 -0700263 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700264
Andrew Lee90374a72014-05-16 15:01:09 -0700265 // Show search fragment only when the query string is changed to non-empty text.
266 if (!TextUtils.isEmpty(newText)) {
267 // Call enterSearchUi only if we are switching search modes, or showing a search
268 // fragment for the first time.
269 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
270 (!mIsDialpadShown && mInRegularSearch);
271 if (!sameSearchMode) {
272 enterSearchUi(mIsDialpadShown, mSearchQuery);
273 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700274 }
275
276 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700277 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700278 } else if (mRegularSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700279 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700280 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700281 }
282
283 @Override
284 public void afterTextChanged(Editable s) {
285 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700286 };
287
Andrew Leeb1903842014-05-15 16:11:24 -0700288
289 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700290 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700291 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700292 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700293 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700294 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700295 if (!isInSearchUi()) {
296 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
297 }
Andrew Leeb1903842014-05-15 16:11:24 -0700298 }
299 };
300
301 /**
302 * If the search term is empty and the user closes the soft keyboard, close the search UI.
303 */
304 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
305 @Override
306 public boolean onKey(View v, int keyCode, KeyEvent event) {
307 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
308 TextUtils.isEmpty(mSearchView.getText().toString())) {
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700309 maybeExitSearchUi();
Andrew Leeb1903842014-05-15 16:11:24 -0700310 }
311 return false;
312 }
313 };
314
Chiao Cheng94b10b52012-08-17 16:59:12 -0700315 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700316 protected void onCreate(Bundle savedInstanceState) {
317 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700318 mFirstLaunch = true;
319
Yorke Lee8898cd02013-08-08 10:24:27 -0700320 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800321 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700322
Yorke Lee53a22302014-04-29 18:13:46 -0700323 final ActionBar actionBar = getActionBar();
324 actionBar.setCustomView(R.layout.search_edittext);
325 actionBar.setDisplayShowCustomEnabled(true);
326
Yorke Leeec489fb2014-05-19 15:39:27 -0700327 mSearchEditTextLayout = (SearchEditTextLayout) actionBar.getCustomView();
328 mSearchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700329
Yorke Leeec489fb2014-05-19 15:39:27 -0700330 mSearchView = (EditText) mSearchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700331 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Yorke Lee11ca39e2014-05-19 20:31:37 -0700332 mVoiceSearchButton = mSearchEditTextLayout.findViewById(R.id.voice_search_button);
333 mSearchEditTextLayout.findViewById(R.id.search_box_start_search).setOnClickListener(
334 mSearchViewOnClickListener);
335 mSearchEditTextLayout.setOnBackButtonClickedListener(new OnBackButtonClickedListener() {
336 @Override
337 public void onBackButtonClicked() {
338 onBackPressed();
339 }
340 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700341
Yorke Leeec489fb2014-05-19 15:39:27 -0700342 ImageButton optionsMenuButton = (ImageButton) mSearchEditTextLayout.findViewById(
343 R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700344 optionsMenuButton.setOnClickListener(this);
345 final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
346 optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
347
Yorke Lee33932ff2014-04-16 13:34:32 -0700348 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
349 new int[] { android.R.attr.actionBarSize });
350 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
351 styledAttributes.recycle();
352
Yorke Leeef2b7382013-08-09 17:39:25 -0700353 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
354 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700355 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800356 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700357 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800358 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
359 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700360 } else {
361 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700362 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
363 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700364 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700365 }
366
Andrew Lee0c667702014-05-12 14:31:45 -0700367 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
368 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
369 parentLayout.setOnDragListener(new LayoutOnDragListener());
370
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700371 setupActivityOverlay();
372
Andrew Leea73e1892014-05-13 13:21:16 -0700373 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
374 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700375
Andrew Leea73e1892014-05-13 13:21:16 -0700376 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
377 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800378
Yorke Lee28266192014-05-01 10:05:52 -0700379 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800380
Yorke Lee0baa98b2013-08-22 10:55:16 -0700381 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700382 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700383 }
384
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700385 private void setupActivityOverlay() {
386 final View activityOverlay = findViewById(R.id.activity_overlay);
387 activityOverlay.setOnTouchListener(new OnTouchListener() {
388 @Override
389 public boolean onTouch(View v, MotionEvent event) {
390 if (!mIsDialpadShown) {
391 maybeExitSearchUi();
392 }
393 return false;
394 }
395 });
396 }
397
Chiao Cheng94b10b52012-08-17 16:59:12 -0700398 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700399 protected void onResume() {
400 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700401 if (mFirstLaunch) {
402 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700403 } else if (!phoneIsInUse() && mInCallDialpadUp) {
404 hideDialpadFragment(false, true);
405 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700406 }
407 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700408 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700409 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700410 }
411
412 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700413 protected void onPause() {
414 if (mClearSearchOnPause) {
415 hideDialpadAndSearchUi();
416 mClearSearchOnPause = false;
417 }
418 super.onPause();
419 }
420
421 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700422 protected void onSaveInstanceState(Bundle outState) {
423 super.onSaveInstanceState(outState);
424 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700425 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
426 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700427 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
428 }
429
430 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700431 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700432 if (fragment instanceof DialpadFragment) {
433 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700434 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700435 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700436 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700437 } else if (fragment instanceof SmartDialSearchFragment) {
438 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700439 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700440 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700441 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700442 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700443 } else if (fragment instanceof ListsFragment) {
444 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700445 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700446 }
Yorke Leec3766332013-07-31 11:13:16 -0700447 }
448
Alon Albertb453e5b2013-09-10 15:54:23 -0700449 protected void handleMenuSettings() {
450 openTelephonySetting(this);
451 }
452
453 public static void openTelephonySetting(Activity activity) {
454 final Intent settingsIntent = getCallSettingsIntent();
455 activity.startActivity(settingsIntent);
456 }
457
Chiao Cheng94b10b52012-08-17 16:59:12 -0700458 @Override
459 public void onClick(View view) {
460 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700461 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700462 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700463 mInCallDialpadUp = false;
464 showDialpadFragment(true);
465 } else {
466 // Dial button was pressed; tell the Dialpad fragment
467 mDialpadFragment.dialButtonPressed();
468 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700469 break;
Yorke Leec3766332013-07-31 11:13:16 -0700470 case R.id.search_close_button:
471 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700472 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700473 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700474 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700475 }
476 break;
Yorke Leec3766332013-07-31 11:13:16 -0700477 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800478 try {
479 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
480 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
481 } catch (ActivityNotFoundException e) {
482 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
483 Toast.LENGTH_SHORT).show();
484 }
Yorke Leec3766332013-07-31 11:13:16 -0700485 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700486 case R.id.dialtacts_options_menu_button:
487 buildOptionsMenu(view).show();
488 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700489 default: {
490 Log.wtf(TAG, "Unexpected onClick event from " + view);
491 break;
492 }
493 }
494 }
495
Yorke Leec3766332013-07-31 11:13:16 -0700496 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700497 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700498 switch (item.getItemId()) {
499 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700500 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700501 break;
502 case R.id.menu_add_contact:
503 try {
504 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
505 } catch (ActivityNotFoundException e) {
506 Toast toast = Toast.makeText(this,
507 R.string.add_contact_not_available,
508 Toast.LENGTH_SHORT);
509 toast.show();
510 }
511 break;
512 case R.id.menu_import_export:
513 // We hard-code the "contactsAreAvailable" argument because doing it properly would
514 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
515 // now in Dialtacts for (potential) performance reasons. Compare with how it is
516 // done in {@link PeopleActivity}.
517 ImportExportDialogFragment.show(getFragmentManager(), true,
518 DialtactsActivity.class);
519 return true;
520 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700521 ClearFrequentsDialog.show(getFragmentManager());
522 return true;
523 case R.id.menu_call_settings:
524 handleMenuSettings();
525 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700526 }
527 return false;
528 }
529
530 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700531 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
532 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
533 if (resultCode == RESULT_OK) {
534 final ArrayList<String> matches = data.getStringArrayListExtra(
535 RecognizerIntent.EXTRA_RESULTS);
536 if (matches.size() > 0) {
537 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700538 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700539 } else {
540 Log.e(TAG, "Voice search - nothing heard");
541 }
542 } else {
543 Log.e(TAG, "Voice search failed");
544 }
545 }
546 super.onActivityResult(requestCode, resultCode, data);
547 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700548
Andrew Lee2a58ab82014-05-15 02:16:04 -0700549 /**
550 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
551 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
552 * @see #onDialpadShown
553 */
Yorke Leec3766332013-07-31 11:13:16 -0700554 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700555 if (mIsDialpadShown) {
556 return;
557 }
558 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700559 mDialpadFragment.setAnimate(animate);
560
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700562 ft.show(mDialpadFragment);
563 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700564
565 if (!isInSearchUi()) {
566 enterSearchUi(true /* isSmartDial */, mSearchQuery);
567 }
Yorke Leec3766332013-07-31 11:13:16 -0700568 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700569
Andrew Lee2a58ab82014-05-15 02:16:04 -0700570 /**
571 * Callback from child DialpadFragment when the dialpad is shown.
572 */
573 public void onDialpadShown() {
574 updateFloatingActionButton();
575 if (mDialpadFragment.getAnimate()) {
576 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
577 mDialpadFragment.getView().startAnimation(slideIn);
578 } else {
579 mDialpadFragment.setYFraction(0);
580 }
581
Andrew Lee2a58ab82014-05-15 02:16:04 -0700582 updateSearchFragmentPosition();
583 getActionBar().hide();
584 }
585
586 /**
587 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
588 * a callback after the hide animation ends.
589 * @see #commitDialpadFragmentHide
590 */
Yorke Leeca195042013-09-25 16:29:38 -0700591 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700592 if (mDialpadFragment == null) {
593 return;
594 }
Yorke Leef6673d32013-08-23 15:34:17 -0700595 if (clearDialpad) {
596 mDialpadFragment.clearDialpad();
597 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700598 if (!mIsDialpadShown) {
599 return;
Yorke Leec3766332013-07-31 11:13:16 -0700600 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700601 mIsDialpadShown = false;
602 mDialpadFragment.setAnimate(animate);
603
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700604 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700605 updateFloatingActionButton();
606 if (animate) {
607 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
608 slideOut.setAnimationListener(new ActivityAnimationListener() {
609 @Override
610 public void onAnimationEnd(Animation animation) {
611 commitDialpadFragmentHide();
612 }
613 });
614 mDialpadFragment.getView().startAnimation(slideOut);
615 } else {
616 commitDialpadFragmentHide();
617 }
618
Andrew Leeb1903842014-05-15 16:11:24 -0700619 mListsFragment.maybeShowActionBar();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700620
Andrew Leed4cde832014-05-16 15:56:48 -0700621 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700622 if (TextUtils.isEmpty(mSearchQuery)) {
623 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700624 }
Andrew Leed4cde832014-05-16 15:56:48 -0700625 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700626 }
627
628 /**
629 * Finishes hiding the dialpad fragment after any animations are completed.
630 */
631 private void commitDialpadFragmentHide() {
632 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700633 ft.hide(mDialpadFragment);
634 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700635 }
636
Andrew Lee2a58ab82014-05-15 02:16:04 -0700637 private void updateSearchFragmentPosition() {
638 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
639 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700640 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700641 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700642 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700643 fragment = mRegularSearchFragment;
644 }
645 if (fragment != null && fragment.isVisible()) {
646 fragment.getView().animate().translationY(translationValue)
647 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
648 }
649 }
650
Andrew Leeb1903842014-05-15 16:11:24 -0700651 private boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700652 return mInDialpadSearch || mInRegularSearch;
653 }
654
655 private void setNotInSearchUi() {
656 mInDialpadSearch = false;
657 mInRegularSearch = false;
658 }
659
Yorke Lee311969c2013-08-26 06:31:11 -0700660 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700661 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700662 hideDialpadFragment(false, true);
663 }
664
Yorke Leee00c9fe2014-04-12 12:42:06 -0700665 private void hideInputMethod(View view) {
666 final InputMethodManager imm = (InputMethodManager) getSystemService(
667 Context.INPUT_METHOD_SERVICE);
668 if (imm != null && view != null) {
669 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
670 }
671 }
672
Yorke Lee53a22302014-04-29 18:13:46 -0700673 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700674 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
675 if (canIntentBeHandled(voiceIntent)) {
676 mVoiceSearchButton.setVisibility(View.VISIBLE);
677 mVoiceSearchButton.setOnClickListener(this);
678 } else {
679 mVoiceSearchButton.setVisibility(View.GONE);
680 }
681 }
682
Andrew Leee74a10e2014-05-16 14:31:40 -0700683 private OptionsPopupMenu buildOptionsMenu(View invoker) {
684 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
685 popupMenu.inflate(R.menu.dialtacts_options);
686 popupMenu.setOnMenuItemClickListener(this);
687 return popupMenu;
688 }
689
Yorke Lee86e21f72014-04-02 16:49:38 -0700690 @Override
691 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700692 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700693 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700694 mPendingSearchViewQuery = null;
695 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700696 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700697 }
698
Chiao Cheng94b10b52012-08-17 16:59:12 -0700699 /**
700 * Returns true if the intent is due to hitting the green send key (hardware call button:
701 * KEYCODE_CALL) while in a call.
702 *
703 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700704 * @return true if the intent is due to hitting the green send key while in a call
705 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700706 private boolean isSendKeyWhileInCall(Intent intent) {
707 // If there is a call in progress and the user launched the dialer by hitting the call
708 // button, go straight to the in-call screen.
709 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700710
Yorke Lee62e995c2014-03-28 10:02:56 -0700711 try {
712 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
713 if (callKey && phone != null && phone.showCallScreen()) {
714 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700715 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700716 } catch (RemoteException e) {
717 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700718 }
719
720 return false;
721 }
722
723 /**
724 * Sets the current tab based on the intent's request type
725 *
726 * @param intent Intent that contains information about which tab should be selected
727 */
Yorke Leec3766332013-07-31 11:13:16 -0700728 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729 // 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 -0700730 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700731 finish();
732 return;
733 }
734
Yorke Lee669b6082013-09-17 15:43:38 -0700735 if (mDialpadFragment != null) {
736 final boolean phoneIsInUse = phoneIsInUse();
737 if (phoneIsInUse || isDialIntent(intent)) {
738 mDialpadFragment.setStartedFromNewIntent(true);
739 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
740 mInCallDialpadUp = true;
741 }
742 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700743 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700745 }
746
747 @Override
748 public void onNewIntent(Intent newIntent) {
749 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700750 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700751
Chiao Cheng94b10b52012-08-17 16:59:12 -0700752 invalidateOptionsMenu();
753 }
754
755 /** Returns true if the given intent contains a phone number to populate the dialer with */
756 private boolean isDialIntent(Intent intent) {
757 final String action = intent.getAction();
758 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
759 return true;
760 }
761 if (Intent.ACTION_VIEW.equals(action)) {
762 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700763 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 return true;
765 }
766 }
767 return false;
768 }
769
770 /**
771 * Returns an appropriate call origin for this Activity. May return null when no call origin
772 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
773 * for remembering the tab in which the user made a phone call, so the external app's DIAL
774 * request should not be counted.)
775 */
776 public String getCallOrigin() {
777 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
778 }
779
Chiao Cheng94b10b52012-08-17 16:59:12 -0700780 /**
Yorke Leec3766332013-07-31 11:13:16 -0700781 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700782 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700783 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700784 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700785 // Weird race condition where fragment is doing work after the activity is destroyed
786 // due to talkback being on (b/10209937). Just return since we can't do any
787 // constructive here.
788 return;
789 }
790
Yorke Leeef2b7382013-08-09 17:39:25 -0700791 if (DEBUG) {
792 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
793 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700794
Yorke Leec3766332013-07-31 11:13:16 -0700795 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700796 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700797 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700798 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700799 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700800 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700801
Yorke Leeb207f8a2013-08-29 18:51:06 -0700802 final String tag;
803 if (smartDialSearch) {
804 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
805 } else {
806 tag = TAG_REGULAR_SEARCH_FRAGMENT;
807 }
808 mInDialpadSearch = smartDialSearch;
809 mInRegularSearch = !smartDialSearch;
810
Andrew Lee752956e2014-05-15 11:43:38 -0700811 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700812 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700813 if (fragment == null) {
814 if (smartDialSearch) {
815 fragment = new SmartDialSearchFragment();
816 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700817 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700818 }
Andrew Leeb1903842014-05-15 16:11:24 -0700819 transaction.add(R.id.dialtacts_frame, fragment, tag);
820 } else {
821 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700822 }
Andrew Leeb1903842014-05-15 16:11:24 -0700823
Yorke Lee86e21f72014-04-02 16:49:38 -0700824 // DialtactsActivity will provide the options menu
825 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700826 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700827 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700828 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700829
830 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leeec489fb2014-05-19 15:39:27 -0700831 mSearchEditTextLayout.animateExpandOrCollapse(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700832 }
833
834 /**
Yorke Leec3766332013-07-31 11:13:16 -0700835 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700836 */
Yorke Leec3766332013-07-31 11:13:16 -0700837 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700838 // See related bug in enterSearchUI();
839 if (getFragmentManager().isDestroyed()) {
840 return;
841 }
Andrew Leeb1903842014-05-15 16:11:24 -0700842
Andrew Leeb1903842014-05-15 16:11:24 -0700843 mSearchView.setText(null);
844 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700845 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700846
Andrew Leeb1903842014-05-15 16:11:24 -0700847 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
848 transaction.setCustomAnimations(0, android.R.animator.fade_out);
Andrew Leeb1903842014-05-15 16:11:24 -0700849 if (mSmartDialSearchFragment != null) {
850 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700851 }
Andrew Leeb1903842014-05-15 16:11:24 -0700852 if (mRegularSearchFragment != null) {
853 transaction.remove(mRegularSearchFragment);
854 }
855 transaction.commit();
856
857 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leeec489fb2014-05-19 15:39:27 -0700858 mSearchEditTextLayout.animateExpandOrCollapse(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700859 }
860
861 /** Returns an Intent to launch Call Settings screen */
862 public static Intent getCallSettingsIntent() {
863 final Intent intent = new Intent(Intent.ACTION_MAIN);
864 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
865 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
866 return intent;
867 }
868
869 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700870 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700871 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700872 if (TextUtils.isEmpty(mSearchQuery)) {
873 exitSearchUi();
874 }
Yorke Leef6673d32013-08-23 15:34:17 -0700875 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700876 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700877 exitSearchUi();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700878 hideInputMethod(parentLayout);
Yorke Leec3766332013-07-31 11:13:16 -0700879 } else {
880 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700881 }
Yorke Leec3766332013-07-31 11:13:16 -0700882 }
883
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700884 /**
885 * @return True if the search UI was exited, false otherwise
886 */
887 private boolean maybeExitSearchUi() {
888 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
889 exitSearchUi();
890 hideInputMethod(parentLayout);
891 return true;
892 }
893 return false;
894 }
895
Yorke Leec3766332013-07-31 11:13:16 -0700896 @Override
897 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700898 if (mSmartDialSearchFragment != null) {
899 mSmartDialSearchFragment.setAddToContactNumber(query);
900 }
Yorke Leec3766332013-07-31 11:13:16 -0700901 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
902 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700903
904 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700905 if (DEBUG) {
906 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
907 }
908 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
909 // This callback can happen if the dialpad fragment is recreated because of
910 // activity destruction. In that case, don't update the search view because
911 // that would bring the user back to the search fragment regardless of the
912 // previous state of the application. Instead, just return here and let the
913 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700914 if (!TextUtils.isEmpty(normalizedQuery)) {
915 mPendingSearchViewQuery = normalizedQuery;
916 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700917 return;
918 }
Yorke Lee53a22302014-04-29 18:13:46 -0700919 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700920 }
921 }
Yorke Leec3766332013-07-31 11:13:16 -0700922
923 @Override
924 public void onListFragmentScrollStateChange(int scrollState) {
925 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700926 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700927 hideInputMethod(getCurrentFocus());
928 }
929 }
930
931 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700932 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700933 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700934 // TODO: No-op for now. This should eventually show/hide the actionBar based on
935 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700936 }
937
Yorke Lee86e21f72014-04-02 16:49:38 -0700938 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700939 public void setFloatingActionButtonVisible(boolean visible) {
940 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700941 }
942
943 private boolean phoneIsInUse() {
944 final TelephonyManager tm = (TelephonyManager) getSystemService(
945 Context.TELEPHONY_SERVICE);
946 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
947 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700948
Yorke Leee1424812013-09-04 18:24:48 -0700949 public static Intent getAddNumberToContactIntent(CharSequence text) {
950 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
951 intent.putExtra(Intents.Insert.PHONE, text);
952 intent.setType(Contacts.CONTENT_ITEM_TYPE);
953 return intent;
954 }
955
Yorke Leeda0f9042013-12-05 14:25:51 -0800956 private boolean canIntentBeHandled(Intent intent) {
957 final PackageManager packageManager = getPackageManager();
958 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
959 PackageManager.MATCH_DEFAULT_ONLY);
960 return resolveInfo != null && resolveInfo.size() > 0;
961 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800962
Yorke Lee6a1461a2014-04-21 16:27:14 -0700963 @Override
964 public void showCallHistory() {
965 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
966 // CONTENT_TYPE, so that we always open our call log from our dialer
967 final Intent intent = new Intent(this, CallLogActivity.class);
968 startActivity(intent);
969 }
970
Yorke Lee86e21f72014-04-02 16:49:38 -0700971 /**
972 * Called when the user has long-pressed a contact tile to start a drag operation.
973 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800974 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700975 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700976 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700977 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800978 }
979
980 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700981 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
982 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800983
Yorke Lee86e21f72014-04-02 16:49:38 -0700984 /**
985 * Called when the user has released a contact tile after long-pressing it.
986 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800987 @Override
988 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700989 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700990 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800991 }
992
993 @Override
994 public void onDroppedOnRemove() {}
995
996 /**
Yorke Leed999b712014-04-23 15:10:58 -0700997 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800998 * once it has been attached to the activity.
999 */
1000 @Override
1001 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001002 mDragDropController = dragController;
1003 ((RemoveView) findViewById(R.id.remove_view))
1004 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001005 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001006
1007 @Override
1008 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001009 // Specify call-origin so that users will see the previous tab instead of
1010 // CallLog screen (search UI will be automatically exited).
1011 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001012 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001013 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001014 }
1015
1016 @Override
1017 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001018 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
1019 startActivity(intent);
1020 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001021 }
1022
1023 @Override
1024 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001025 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001026 }
1027
1028 @Override
1029 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001030 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001031 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001032
1033 public int getActionBarHeight() {
1034 return mActionBarHeight;
1035 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001036
1037 @Override
1038 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1039
1040 }
1041
1042 @Override
1043 public void onPageSelected(int position) {
1044 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001045 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001046 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001047 alignFloatingActionButtonByTab(mCurrentTabPosition);
1048 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001049 }
1050
1051 @Override
1052 public void onPageScrollStateChanged(int state) {
1053 }
1054
Andrew Lee2a58ab82014-05-15 02:16:04 -07001055 private void updateFloatingActionButton() {
1056 if (mIsDialpadShown) {
1057 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1058 mFloatingActionButton.setContentDescription(
1059 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001060 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001061 } else {
1062 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1063 mFloatingActionButton.setContentDescription(
1064 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001065 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001066 }
1067 }
1068
Yorke Leecc4660d2014-04-24 11:22:57 -07001069 private void alignFloatingActionButtonByTab(int position) {
1070 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1071 alignFloatingActionButtonMiddle();
1072 } else {
1073 alignFloatingActionButtonRight();
1074 }
1075 }
1076
1077 private void alignFloatingActionButtonRight() {
1078 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001079 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001080 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1081 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001082 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001083 }
1084
1085 private void alignFloatingActionButtonMiddle() {
1086 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001087 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001088 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1089 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001090 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001091 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001092
1093 /**
1094 * Convenience class which implements AnimationListener interface as null-op methods.
1095 */
1096 private class ActivityAnimationListener implements AnimationListener {
1097 @Override
1098 public void onAnimationStart(Animation animation) {
1099 }
1100
1101 @Override
1102 public void onAnimationEnd(Animation animation) {
1103 }
1104
1105 @Override
1106 public void onAnimationRepeat(Animation animation) {
1107 }
1108 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001109}