blob: 9f7e47f0594b2ae4ced80453b828a5c89d050e0e [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);
Yorke Leebcef9402014-05-21 15:33:00 -0700326 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700327
Yorke Leeec489fb2014-05-19 15:39:27 -0700328 mSearchEditTextLayout = (SearchEditTextLayout) actionBar.getCustomView();
329 mSearchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700330
Yorke Leeec489fb2014-05-19 15:39:27 -0700331 mSearchView = (EditText) mSearchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700332 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Yorke Lee11ca39e2014-05-19 20:31:37 -0700333 mVoiceSearchButton = mSearchEditTextLayout.findViewById(R.id.voice_search_button);
334 mSearchEditTextLayout.findViewById(R.id.search_box_start_search).setOnClickListener(
335 mSearchViewOnClickListener);
336 mSearchEditTextLayout.setOnBackButtonClickedListener(new OnBackButtonClickedListener() {
337 @Override
338 public void onBackButtonClicked() {
339 onBackPressed();
340 }
341 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700342
Yorke Leeec489fb2014-05-19 15:39:27 -0700343 ImageButton optionsMenuButton = (ImageButton) mSearchEditTextLayout.findViewById(
344 R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700345 optionsMenuButton.setOnClickListener(this);
346 final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
347 optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
348
Yorke Lee3443d4a2014-05-23 19:30:35 -0700349 mActionBarHeight = getResources().getDimensionPixelSize(R.dimen.action_bar_height);
Yorke Lee33932ff2014-04-16 13:34:32 -0700350
Yorke Leeef2b7382013-08-09 17:39:25 -0700351 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
352 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700353 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800354 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700355 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800356 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
357 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700358 } else {
359 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700360 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
361 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700362 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700363 }
364
Andrew Lee0c667702014-05-12 14:31:45 -0700365 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
366 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
367 parentLayout.setOnDragListener(new LayoutOnDragListener());
368
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700369 setupActivityOverlay();
370
Andrew Leea73e1892014-05-13 13:21:16 -0700371 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
372 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700373
Andrew Leea73e1892014-05-13 13:21:16 -0700374 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
375 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800376
Yorke Lee28266192014-05-01 10:05:52 -0700377 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800378
Yorke Lee0baa98b2013-08-22 10:55:16 -0700379 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700380 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700381 }
382
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700383 private void setupActivityOverlay() {
384 final View activityOverlay = findViewById(R.id.activity_overlay);
385 activityOverlay.setOnTouchListener(new OnTouchListener() {
386 @Override
387 public boolean onTouch(View v, MotionEvent event) {
388 if (!mIsDialpadShown) {
389 maybeExitSearchUi();
390 }
391 return false;
392 }
393 });
394 }
395
Chiao Cheng94b10b52012-08-17 16:59:12 -0700396 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700397 protected void onResume() {
398 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700399 if (mFirstLaunch) {
400 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700401 } else if (!phoneIsInUse() && mInCallDialpadUp) {
402 hideDialpadFragment(false, true);
403 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700404 }
405 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700406 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700407 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700408 }
409
410 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700411 protected void onPause() {
412 if (mClearSearchOnPause) {
413 hideDialpadAndSearchUi();
414 mClearSearchOnPause = false;
415 }
416 super.onPause();
417 }
418
419 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700420 protected void onSaveInstanceState(Bundle outState) {
421 super.onSaveInstanceState(outState);
422 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700423 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
424 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700425 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
426 }
427
428 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700429 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700430 if (fragment instanceof DialpadFragment) {
431 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700432 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700433 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700434 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700435 } else if (fragment instanceof SmartDialSearchFragment) {
436 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700437 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700438 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700439 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700440 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700441 } else if (fragment instanceof ListsFragment) {
442 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700443 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700444 }
Yorke Leec3766332013-07-31 11:13:16 -0700445 }
446
Alon Albertb453e5b2013-09-10 15:54:23 -0700447 protected void handleMenuSettings() {
448 openTelephonySetting(this);
449 }
450
451 public static void openTelephonySetting(Activity activity) {
452 final Intent settingsIntent = getCallSettingsIntent();
453 activity.startActivity(settingsIntent);
454 }
455
Chiao Cheng94b10b52012-08-17 16:59:12 -0700456 @Override
457 public void onClick(View view) {
458 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700459 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700460 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700461 mInCallDialpadUp = false;
462 showDialpadFragment(true);
463 } else {
464 // Dial button was pressed; tell the Dialpad fragment
465 mDialpadFragment.dialButtonPressed();
466 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700467 break;
Yorke Leec3766332013-07-31 11:13:16 -0700468 case R.id.search_close_button:
469 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700470 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700471 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700472 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700473 }
474 break;
Yorke Leec3766332013-07-31 11:13:16 -0700475 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800476 try {
477 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
478 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
479 } catch (ActivityNotFoundException e) {
480 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
481 Toast.LENGTH_SHORT).show();
482 }
Yorke Leec3766332013-07-31 11:13:16 -0700483 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700484 case R.id.dialtacts_options_menu_button:
485 buildOptionsMenu(view).show();
486 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700487 default: {
488 Log.wtf(TAG, "Unexpected onClick event from " + view);
489 break;
490 }
491 }
492 }
493
Yorke Leec3766332013-07-31 11:13:16 -0700494 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700495 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700496 switch (item.getItemId()) {
497 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700498 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700499 break;
500 case R.id.menu_add_contact:
501 try {
502 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
503 } catch (ActivityNotFoundException e) {
504 Toast toast = Toast.makeText(this,
505 R.string.add_contact_not_available,
506 Toast.LENGTH_SHORT);
507 toast.show();
508 }
509 break;
510 case R.id.menu_import_export:
511 // We hard-code the "contactsAreAvailable" argument because doing it properly would
512 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
513 // now in Dialtacts for (potential) performance reasons. Compare with how it is
514 // done in {@link PeopleActivity}.
515 ImportExportDialogFragment.show(getFragmentManager(), true,
516 DialtactsActivity.class);
517 return true;
518 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700519 ClearFrequentsDialog.show(getFragmentManager());
520 return true;
521 case R.id.menu_call_settings:
522 handleMenuSettings();
523 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700524 }
525 return false;
526 }
527
528 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700529 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
530 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
531 if (resultCode == RESULT_OK) {
532 final ArrayList<String> matches = data.getStringArrayListExtra(
533 RecognizerIntent.EXTRA_RESULTS);
534 if (matches.size() > 0) {
535 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700536 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700537 } else {
538 Log.e(TAG, "Voice search - nothing heard");
539 }
540 } else {
541 Log.e(TAG, "Voice search failed");
542 }
543 }
544 super.onActivityResult(requestCode, resultCode, data);
545 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700546
Andrew Lee2a58ab82014-05-15 02:16:04 -0700547 /**
548 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
549 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
550 * @see #onDialpadShown
551 */
Yorke Leec3766332013-07-31 11:13:16 -0700552 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700553 if (mIsDialpadShown) {
554 return;
555 }
556 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700557 mDialpadFragment.setAnimate(animate);
558
Chiao Cheng94b10b52012-08-17 16:59:12 -0700559 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700560 ft.show(mDialpadFragment);
561 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700562
563 if (!isInSearchUi()) {
564 enterSearchUi(true /* isSmartDial */, mSearchQuery);
565 }
Yorke Leec3766332013-07-31 11:13:16 -0700566 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700567
Andrew Lee2a58ab82014-05-15 02:16:04 -0700568 /**
569 * Callback from child DialpadFragment when the dialpad is shown.
570 */
571 public void onDialpadShown() {
572 updateFloatingActionButton();
573 if (mDialpadFragment.getAnimate()) {
574 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
575 mDialpadFragment.getView().startAnimation(slideIn);
576 } else {
577 mDialpadFragment.setYFraction(0);
578 }
579
Andrew Lee2a58ab82014-05-15 02:16:04 -0700580 updateSearchFragmentPosition();
581 getActionBar().hide();
582 }
583
584 /**
585 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
586 * a callback after the hide animation ends.
587 * @see #commitDialpadFragmentHide
588 */
Yorke Leeca195042013-09-25 16:29:38 -0700589 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700590 if (mDialpadFragment == null) {
591 return;
592 }
Yorke Leef6673d32013-08-23 15:34:17 -0700593 if (clearDialpad) {
594 mDialpadFragment.clearDialpad();
595 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700596 if (!mIsDialpadShown) {
597 return;
Yorke Leec3766332013-07-31 11:13:16 -0700598 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700599 mIsDialpadShown = false;
600 mDialpadFragment.setAnimate(animate);
601
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700602 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700603 updateFloatingActionButton();
604 if (animate) {
605 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
606 slideOut.setAnimationListener(new ActivityAnimationListener() {
607 @Override
608 public void onAnimationEnd(Animation animation) {
609 commitDialpadFragmentHide();
610 }
611 });
612 mDialpadFragment.getView().startAnimation(slideOut);
613 } else {
614 commitDialpadFragmentHide();
615 }
616
Andrew Leeb1903842014-05-15 16:11:24 -0700617 mListsFragment.maybeShowActionBar();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700618
Andrew Leed4cde832014-05-16 15:56:48 -0700619 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700620 if (TextUtils.isEmpty(mSearchQuery)) {
621 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700622 }
Andrew Leed4cde832014-05-16 15:56:48 -0700623 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700624 }
625
626 /**
627 * Finishes hiding the dialpad fragment after any animations are completed.
628 */
629 private void commitDialpadFragmentHide() {
630 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700631 ft.hide(mDialpadFragment);
632 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700633 }
634
Andrew Lee2a58ab82014-05-15 02:16:04 -0700635 private void updateSearchFragmentPosition() {
636 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
637 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700638 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700639 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700640 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700641 fragment = mRegularSearchFragment;
642 }
643 if (fragment != null && fragment.isVisible()) {
644 fragment.getView().animate().translationY(translationValue)
645 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
646 }
647 }
648
Andrew Leeb1903842014-05-15 16:11:24 -0700649 private boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700650 return mInDialpadSearch || mInRegularSearch;
651 }
652
653 private void setNotInSearchUi() {
654 mInDialpadSearch = false;
655 mInRegularSearch = false;
656 }
657
Yorke Lee311969c2013-08-26 06:31:11 -0700658 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700659 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700660 hideDialpadFragment(false, true);
661 }
662
Yorke Leee00c9fe2014-04-12 12:42:06 -0700663 private void hideInputMethod(View view) {
664 final InputMethodManager imm = (InputMethodManager) getSystemService(
665 Context.INPUT_METHOD_SERVICE);
666 if (imm != null && view != null) {
667 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
668 }
669 }
670
Yorke Lee53a22302014-04-29 18:13:46 -0700671 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700672 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
673 if (canIntentBeHandled(voiceIntent)) {
674 mVoiceSearchButton.setVisibility(View.VISIBLE);
675 mVoiceSearchButton.setOnClickListener(this);
676 } else {
677 mVoiceSearchButton.setVisibility(View.GONE);
678 }
679 }
680
Andrew Leee74a10e2014-05-16 14:31:40 -0700681 private OptionsPopupMenu buildOptionsMenu(View invoker) {
682 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
683 popupMenu.inflate(R.menu.dialtacts_options);
684 popupMenu.setOnMenuItemClickListener(this);
685 return popupMenu;
686 }
687
Yorke Lee86e21f72014-04-02 16:49:38 -0700688 @Override
689 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700690 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700691 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700692 mPendingSearchViewQuery = null;
693 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700694 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700695 }
696
Chiao Cheng94b10b52012-08-17 16:59:12 -0700697 /**
698 * Returns true if the intent is due to hitting the green send key (hardware call button:
699 * KEYCODE_CALL) while in a call.
700 *
701 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700702 * @return true if the intent is due to hitting the green send key while in a call
703 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700704 private boolean isSendKeyWhileInCall(Intent intent) {
705 // If there is a call in progress and the user launched the dialer by hitting the call
706 // button, go straight to the in-call screen.
707 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700708
Yorke Lee62e995c2014-03-28 10:02:56 -0700709 try {
710 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
711 if (callKey && phone != null && phone.showCallScreen()) {
712 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700713 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700714 } catch (RemoteException e) {
715 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700716 }
717
718 return false;
719 }
720
721 /**
722 * Sets the current tab based on the intent's request type
723 *
724 * @param intent Intent that contains information about which tab should be selected
725 */
Yorke Leec3766332013-07-31 11:13:16 -0700726 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700727 // 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 -0700728 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729 finish();
730 return;
731 }
732
Yorke Lee669b6082013-09-17 15:43:38 -0700733 if (mDialpadFragment != null) {
734 final boolean phoneIsInUse = phoneIsInUse();
735 if (phoneIsInUse || isDialIntent(intent)) {
736 mDialpadFragment.setStartedFromNewIntent(true);
737 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
738 mInCallDialpadUp = true;
739 }
740 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700741 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700742 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700743 }
744
745 @Override
746 public void onNewIntent(Intent newIntent) {
747 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700748 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700749
Chiao Cheng94b10b52012-08-17 16:59:12 -0700750 invalidateOptionsMenu();
751 }
752
753 /** Returns true if the given intent contains a phone number to populate the dialer with */
754 private boolean isDialIntent(Intent intent) {
755 final String action = intent.getAction();
756 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
757 return true;
758 }
759 if (Intent.ACTION_VIEW.equals(action)) {
760 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700761 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700762 return true;
763 }
764 }
765 return false;
766 }
767
768 /**
769 * Returns an appropriate call origin for this Activity. May return null when no call origin
770 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
771 * for remembering the tab in which the user made a phone call, so the external app's DIAL
772 * request should not be counted.)
773 */
774 public String getCallOrigin() {
775 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
776 }
777
Chiao Cheng94b10b52012-08-17 16:59:12 -0700778 /**
Yorke Leec3766332013-07-31 11:13:16 -0700779 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700780 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700781 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700782 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700783 // Weird race condition where fragment is doing work after the activity is destroyed
784 // due to talkback being on (b/10209937). Just return since we can't do any
785 // constructive here.
786 return;
787 }
788
Yorke Leeef2b7382013-08-09 17:39:25 -0700789 if (DEBUG) {
790 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
791 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700792
Yorke Leec3766332013-07-31 11:13:16 -0700793 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700794 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700795 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700796 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700797 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700798 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700799
Yorke Leeb207f8a2013-08-29 18:51:06 -0700800 final String tag;
801 if (smartDialSearch) {
802 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
803 } else {
804 tag = TAG_REGULAR_SEARCH_FRAGMENT;
805 }
806 mInDialpadSearch = smartDialSearch;
807 mInRegularSearch = !smartDialSearch;
808
Andrew Lee752956e2014-05-15 11:43:38 -0700809 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700810 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700811 if (fragment == null) {
812 if (smartDialSearch) {
813 fragment = new SmartDialSearchFragment();
814 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700815 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700816 }
Andrew Leeb1903842014-05-15 16:11:24 -0700817 transaction.add(R.id.dialtacts_frame, fragment, tag);
818 } else {
819 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700820 }
Andrew Leeb1903842014-05-15 16:11:24 -0700821
Yorke Lee86e21f72014-04-02 16:49:38 -0700822 // DialtactsActivity will provide the options menu
823 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700824 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700825 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700826 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700827
828 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leeec489fb2014-05-19 15:39:27 -0700829 mSearchEditTextLayout.animateExpandOrCollapse(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700830 }
831
832 /**
Yorke Leec3766332013-07-31 11:13:16 -0700833 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700834 */
Yorke Leec3766332013-07-31 11:13:16 -0700835 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700836 // See related bug in enterSearchUI();
837 if (getFragmentManager().isDestroyed()) {
838 return;
839 }
Andrew Leeb1903842014-05-15 16:11:24 -0700840
Andrew Leeb1903842014-05-15 16:11:24 -0700841 mSearchView.setText(null);
842 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700843 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700844
Andrew Leeb1903842014-05-15 16:11:24 -0700845 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
846 transaction.setCustomAnimations(0, android.R.animator.fade_out);
Andrew Leeb1903842014-05-15 16:11:24 -0700847 if (mSmartDialSearchFragment != null) {
848 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700849 }
Andrew Leeb1903842014-05-15 16:11:24 -0700850 if (mRegularSearchFragment != null) {
851 transaction.remove(mRegularSearchFragment);
852 }
853 transaction.commit();
854
855 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leeec489fb2014-05-19 15:39:27 -0700856 mSearchEditTextLayout.animateExpandOrCollapse(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700857 }
858
859 /** Returns an Intent to launch Call Settings screen */
860 public static Intent getCallSettingsIntent() {
861 final Intent intent = new Intent(Intent.ACTION_MAIN);
862 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
863 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
864 return intent;
865 }
866
867 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700868 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700869 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700870 if (TextUtils.isEmpty(mSearchQuery)) {
871 exitSearchUi();
872 }
Yorke Leef6673d32013-08-23 15:34:17 -0700873 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700874 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700875 exitSearchUi();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700876 hideInputMethod(parentLayout);
Yorke Leec3766332013-07-31 11:13:16 -0700877 } else {
878 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700879 }
Yorke Leec3766332013-07-31 11:13:16 -0700880 }
881
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700882 /**
883 * @return True if the search UI was exited, false otherwise
884 */
885 private boolean maybeExitSearchUi() {
886 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
887 exitSearchUi();
888 hideInputMethod(parentLayout);
889 return true;
890 }
891 return false;
892 }
893
Yorke Leec3766332013-07-31 11:13:16 -0700894 @Override
895 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700896 if (mSmartDialSearchFragment != null) {
897 mSmartDialSearchFragment.setAddToContactNumber(query);
898 }
Yorke Leec3766332013-07-31 11:13:16 -0700899 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
900 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700901
902 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700903 if (DEBUG) {
904 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
905 }
906 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
907 // This callback can happen if the dialpad fragment is recreated because of
908 // activity destruction. In that case, don't update the search view because
909 // that would bring the user back to the search fragment regardless of the
910 // previous state of the application. Instead, just return here and let the
911 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700912 if (!TextUtils.isEmpty(normalizedQuery)) {
913 mPendingSearchViewQuery = normalizedQuery;
914 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700915 return;
916 }
Yorke Lee53a22302014-04-29 18:13:46 -0700917 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700918 }
919 }
Yorke Leec3766332013-07-31 11:13:16 -0700920
921 @Override
922 public void onListFragmentScrollStateChange(int scrollState) {
923 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700924 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700925 hideInputMethod(getCurrentFocus());
926 }
927 }
928
929 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700930 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700931 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700932 // TODO: No-op for now. This should eventually show/hide the actionBar based on
933 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700934 }
935
Yorke Lee86e21f72014-04-02 16:49:38 -0700936 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700937 public void setFloatingActionButtonVisible(boolean visible) {
938 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700939 }
940
941 private boolean phoneIsInUse() {
942 final TelephonyManager tm = (TelephonyManager) getSystemService(
943 Context.TELEPHONY_SERVICE);
944 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
945 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700946
Yorke Leee1424812013-09-04 18:24:48 -0700947 public static Intent getAddNumberToContactIntent(CharSequence text) {
948 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
949 intent.putExtra(Intents.Insert.PHONE, text);
950 intent.setType(Contacts.CONTENT_ITEM_TYPE);
951 return intent;
952 }
953
Yorke Leeda0f9042013-12-05 14:25:51 -0800954 private boolean canIntentBeHandled(Intent intent) {
955 final PackageManager packageManager = getPackageManager();
956 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
957 PackageManager.MATCH_DEFAULT_ONLY);
958 return resolveInfo != null && resolveInfo.size() > 0;
959 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800960
Yorke Lee6a1461a2014-04-21 16:27:14 -0700961 @Override
962 public void showCallHistory() {
963 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
964 // CONTENT_TYPE, so that we always open our call log from our dialer
965 final Intent intent = new Intent(this, CallLogActivity.class);
966 startActivity(intent);
967 }
968
Yorke Lee86e21f72014-04-02 16:49:38 -0700969 /**
970 * Called when the user has long-pressed a contact tile to start a drag operation.
971 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800972 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700973 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700974 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700975 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800976 }
977
978 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700979 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
980 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800981
Yorke Lee86e21f72014-04-02 16:49:38 -0700982 /**
983 * Called when the user has released a contact tile after long-pressing it.
984 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800985 @Override
986 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700987 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700988 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800989 }
990
991 @Override
992 public void onDroppedOnRemove() {}
993
994 /**
Yorke Leed999b712014-04-23 15:10:58 -0700995 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800996 * once it has been attached to the activity.
997 */
998 @Override
999 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001000 mDragDropController = dragController;
1001 ((RemoveView) findViewById(R.id.remove_view))
1002 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001003 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001004
1005 @Override
1006 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001007 // Specify call-origin so that users will see the previous tab instead of
1008 // CallLog screen (search UI will be automatically exited).
1009 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001010 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001011 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001012 }
1013
1014 @Override
1015 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001016 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
1017 startActivity(intent);
1018 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001019 }
1020
1021 @Override
1022 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001023 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001024 }
1025
1026 @Override
1027 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001028 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001029 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001030
1031 public int getActionBarHeight() {
1032 return mActionBarHeight;
1033 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001034
1035 @Override
1036 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1037
1038 }
1039
1040 @Override
1041 public void onPageSelected(int position) {
1042 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001043 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001044 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001045 alignFloatingActionButtonByTab(mCurrentTabPosition);
1046 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001047 }
1048
1049 @Override
1050 public void onPageScrollStateChanged(int state) {
1051 }
1052
Andrew Lee2a58ab82014-05-15 02:16:04 -07001053 private void updateFloatingActionButton() {
1054 if (mIsDialpadShown) {
1055 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1056 mFloatingActionButton.setContentDescription(
1057 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001058 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001059 } else {
1060 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1061 mFloatingActionButton.setContentDescription(
1062 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001063 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001064 }
1065 }
1066
Yorke Leecc4660d2014-04-24 11:22:57 -07001067 private void alignFloatingActionButtonByTab(int position) {
1068 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1069 alignFloatingActionButtonMiddle();
1070 } else {
1071 alignFloatingActionButtonRight();
1072 }
1073 }
1074
1075 private void alignFloatingActionButtonRight() {
1076 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001077 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001078 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1079 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001080 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001081 }
1082
1083 private void alignFloatingActionButtonMiddle() {
1084 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001085 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001086 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1087 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001088 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001089 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001090
1091 /**
1092 * Convenience class which implements AnimationListener interface as null-op methods.
1093 */
1094 private class ActivityAnimationListener implements AnimationListener {
1095 @Override
1096 public void onAnimationStart(Animation animation) {
1097 }
1098
1099 @Override
1100 public void onAnimationEnd(Animation animation) {
1101 }
1102
1103 @Override
1104 public void onAnimationRepeat(Animation animation) {
1105 }
1106 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001107}