blob: 8445ff76db89115876650d09664629c733d5db14 [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 Lee33932ff2014-04-16 13:34:32 -0700349 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
350 new int[] { android.R.attr.actionBarSize });
351 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
352 styledAttributes.recycle();
353
Yorke Leeef2b7382013-08-09 17:39:25 -0700354 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
355 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700356 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800357 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700358 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800359 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
360 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700361 } else {
362 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700363 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
364 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700365 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700366 }
367
Andrew Lee0c667702014-05-12 14:31:45 -0700368 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
369 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
370 parentLayout.setOnDragListener(new LayoutOnDragListener());
371
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700372 setupActivityOverlay();
373
Andrew Leea73e1892014-05-13 13:21:16 -0700374 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
375 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700376
Andrew Leea73e1892014-05-13 13:21:16 -0700377 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
378 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800379
Yorke Lee28266192014-05-01 10:05:52 -0700380 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800381
Yorke Lee0baa98b2013-08-22 10:55:16 -0700382 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700383 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700384 }
385
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700386 private void setupActivityOverlay() {
387 final View activityOverlay = findViewById(R.id.activity_overlay);
388 activityOverlay.setOnTouchListener(new OnTouchListener() {
389 @Override
390 public boolean onTouch(View v, MotionEvent event) {
391 if (!mIsDialpadShown) {
392 maybeExitSearchUi();
393 }
394 return false;
395 }
396 });
397 }
398
Chiao Cheng94b10b52012-08-17 16:59:12 -0700399 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700400 protected void onResume() {
401 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700402 if (mFirstLaunch) {
403 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700404 } else if (!phoneIsInUse() && mInCallDialpadUp) {
405 hideDialpadFragment(false, true);
406 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700407 }
408 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700409 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700410 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700411 }
412
413 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700414 protected void onPause() {
415 if (mClearSearchOnPause) {
416 hideDialpadAndSearchUi();
417 mClearSearchOnPause = false;
418 }
419 super.onPause();
420 }
421
422 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700423 protected void onSaveInstanceState(Bundle outState) {
424 super.onSaveInstanceState(outState);
425 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700426 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
427 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700428 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
429 }
430
431 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700432 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700433 if (fragment instanceof DialpadFragment) {
434 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700435 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700436 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700437 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700438 } else if (fragment instanceof SmartDialSearchFragment) {
439 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700440 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700441 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700442 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700443 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700444 } else if (fragment instanceof ListsFragment) {
445 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700446 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700447 }
Yorke Leec3766332013-07-31 11:13:16 -0700448 }
449
Alon Albertb453e5b2013-09-10 15:54:23 -0700450 protected void handleMenuSettings() {
451 openTelephonySetting(this);
452 }
453
454 public static void openTelephonySetting(Activity activity) {
455 final Intent settingsIntent = getCallSettingsIntent();
456 activity.startActivity(settingsIntent);
457 }
458
Chiao Cheng94b10b52012-08-17 16:59:12 -0700459 @Override
460 public void onClick(View view) {
461 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700462 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700463 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700464 mInCallDialpadUp = false;
465 showDialpadFragment(true);
466 } else {
467 // Dial button was pressed; tell the Dialpad fragment
468 mDialpadFragment.dialButtonPressed();
469 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700470 break;
Yorke Leec3766332013-07-31 11:13:16 -0700471 case R.id.search_close_button:
472 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700473 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700474 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700475 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700476 }
477 break;
Yorke Leec3766332013-07-31 11:13:16 -0700478 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800479 try {
480 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
481 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
482 } catch (ActivityNotFoundException e) {
483 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
484 Toast.LENGTH_SHORT).show();
485 }
Yorke Leec3766332013-07-31 11:13:16 -0700486 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700487 case R.id.dialtacts_options_menu_button:
488 buildOptionsMenu(view).show();
489 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700490 default: {
491 Log.wtf(TAG, "Unexpected onClick event from " + view);
492 break;
493 }
494 }
495 }
496
Yorke Leec3766332013-07-31 11:13:16 -0700497 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700498 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700499 switch (item.getItemId()) {
500 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700501 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700502 break;
503 case R.id.menu_add_contact:
504 try {
505 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
506 } catch (ActivityNotFoundException e) {
507 Toast toast = Toast.makeText(this,
508 R.string.add_contact_not_available,
509 Toast.LENGTH_SHORT);
510 toast.show();
511 }
512 break;
513 case R.id.menu_import_export:
514 // We hard-code the "contactsAreAvailable" argument because doing it properly would
515 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
516 // now in Dialtacts for (potential) performance reasons. Compare with how it is
517 // done in {@link PeopleActivity}.
518 ImportExportDialogFragment.show(getFragmentManager(), true,
519 DialtactsActivity.class);
520 return true;
521 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700522 ClearFrequentsDialog.show(getFragmentManager());
523 return true;
524 case R.id.menu_call_settings:
525 handleMenuSettings();
526 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700527 }
528 return false;
529 }
530
531 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700532 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
533 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
534 if (resultCode == RESULT_OK) {
535 final ArrayList<String> matches = data.getStringArrayListExtra(
536 RecognizerIntent.EXTRA_RESULTS);
537 if (matches.size() > 0) {
538 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700539 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700540 } else {
541 Log.e(TAG, "Voice search - nothing heard");
542 }
543 } else {
544 Log.e(TAG, "Voice search failed");
545 }
546 }
547 super.onActivityResult(requestCode, resultCode, data);
548 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700549
Andrew Lee2a58ab82014-05-15 02:16:04 -0700550 /**
551 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
552 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
553 * @see #onDialpadShown
554 */
Yorke Leec3766332013-07-31 11:13:16 -0700555 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700556 if (mIsDialpadShown) {
557 return;
558 }
559 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700560 mDialpadFragment.setAnimate(animate);
561
Chiao Cheng94b10b52012-08-17 16:59:12 -0700562 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700563 ft.show(mDialpadFragment);
564 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700565
566 if (!isInSearchUi()) {
567 enterSearchUi(true /* isSmartDial */, mSearchQuery);
568 }
Yorke Leec3766332013-07-31 11:13:16 -0700569 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700570
Andrew Lee2a58ab82014-05-15 02:16:04 -0700571 /**
572 * Callback from child DialpadFragment when the dialpad is shown.
573 */
574 public void onDialpadShown() {
575 updateFloatingActionButton();
576 if (mDialpadFragment.getAnimate()) {
577 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
578 mDialpadFragment.getView().startAnimation(slideIn);
579 } else {
580 mDialpadFragment.setYFraction(0);
581 }
582
Andrew Lee2a58ab82014-05-15 02:16:04 -0700583 updateSearchFragmentPosition();
584 getActionBar().hide();
585 }
586
587 /**
588 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
589 * a callback after the hide animation ends.
590 * @see #commitDialpadFragmentHide
591 */
Yorke Leeca195042013-09-25 16:29:38 -0700592 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700593 if (mDialpadFragment == null) {
594 return;
595 }
Yorke Leef6673d32013-08-23 15:34:17 -0700596 if (clearDialpad) {
597 mDialpadFragment.clearDialpad();
598 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700599 if (!mIsDialpadShown) {
600 return;
Yorke Leec3766332013-07-31 11:13:16 -0700601 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700602 mIsDialpadShown = false;
603 mDialpadFragment.setAnimate(animate);
604
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700605 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700606 updateFloatingActionButton();
607 if (animate) {
608 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
609 slideOut.setAnimationListener(new ActivityAnimationListener() {
610 @Override
611 public void onAnimationEnd(Animation animation) {
612 commitDialpadFragmentHide();
613 }
614 });
615 mDialpadFragment.getView().startAnimation(slideOut);
616 } else {
617 commitDialpadFragmentHide();
618 }
619
Andrew Leeb1903842014-05-15 16:11:24 -0700620 mListsFragment.maybeShowActionBar();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700621
Andrew Leed4cde832014-05-16 15:56:48 -0700622 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700623 if (TextUtils.isEmpty(mSearchQuery)) {
624 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700625 }
Andrew Leed4cde832014-05-16 15:56:48 -0700626 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700627 }
628
629 /**
630 * Finishes hiding the dialpad fragment after any animations are completed.
631 */
632 private void commitDialpadFragmentHide() {
633 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700634 ft.hide(mDialpadFragment);
635 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700636 }
637
Andrew Lee2a58ab82014-05-15 02:16:04 -0700638 private void updateSearchFragmentPosition() {
639 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
640 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700641 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700642 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700643 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700644 fragment = mRegularSearchFragment;
645 }
646 if (fragment != null && fragment.isVisible()) {
647 fragment.getView().animate().translationY(translationValue)
648 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
649 }
650 }
651
Andrew Leeb1903842014-05-15 16:11:24 -0700652 private boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700653 return mInDialpadSearch || mInRegularSearch;
654 }
655
656 private void setNotInSearchUi() {
657 mInDialpadSearch = false;
658 mInRegularSearch = false;
659 }
660
Yorke Lee311969c2013-08-26 06:31:11 -0700661 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700662 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700663 hideDialpadFragment(false, true);
664 }
665
Yorke Leee00c9fe2014-04-12 12:42:06 -0700666 private void hideInputMethod(View view) {
667 final InputMethodManager imm = (InputMethodManager) getSystemService(
668 Context.INPUT_METHOD_SERVICE);
669 if (imm != null && view != null) {
670 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
671 }
672 }
673
Yorke Lee53a22302014-04-29 18:13:46 -0700674 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700675 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
676 if (canIntentBeHandled(voiceIntent)) {
677 mVoiceSearchButton.setVisibility(View.VISIBLE);
678 mVoiceSearchButton.setOnClickListener(this);
679 } else {
680 mVoiceSearchButton.setVisibility(View.GONE);
681 }
682 }
683
Andrew Leee74a10e2014-05-16 14:31:40 -0700684 private OptionsPopupMenu buildOptionsMenu(View invoker) {
685 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
686 popupMenu.inflate(R.menu.dialtacts_options);
687 popupMenu.setOnMenuItemClickListener(this);
688 return popupMenu;
689 }
690
Yorke Lee86e21f72014-04-02 16:49:38 -0700691 @Override
692 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700693 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700694 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700695 mPendingSearchViewQuery = null;
696 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700697 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700698 }
699
Chiao Cheng94b10b52012-08-17 16:59:12 -0700700 /**
701 * Returns true if the intent is due to hitting the green send key (hardware call button:
702 * KEYCODE_CALL) while in a call.
703 *
704 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700705 * @return true if the intent is due to hitting the green send key while in a call
706 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700707 private boolean isSendKeyWhileInCall(Intent intent) {
708 // If there is a call in progress and the user launched the dialer by hitting the call
709 // button, go straight to the in-call screen.
710 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700711
Yorke Lee62e995c2014-03-28 10:02:56 -0700712 try {
713 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
714 if (callKey && phone != null && phone.showCallScreen()) {
715 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700716 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700717 } catch (RemoteException e) {
718 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719 }
720
721 return false;
722 }
723
724 /**
725 * Sets the current tab based on the intent's request type
726 *
727 * @param intent Intent that contains information about which tab should be selected
728 */
Yorke Leec3766332013-07-31 11:13:16 -0700729 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730 // 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 -0700731 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700732 finish();
733 return;
734 }
735
Yorke Lee669b6082013-09-17 15:43:38 -0700736 if (mDialpadFragment != null) {
737 final boolean phoneIsInUse = phoneIsInUse();
738 if (phoneIsInUse || isDialIntent(intent)) {
739 mDialpadFragment.setStartedFromNewIntent(true);
740 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
741 mInCallDialpadUp = true;
742 }
743 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700744 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700745 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700746 }
747
748 @Override
749 public void onNewIntent(Intent newIntent) {
750 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700751 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700752
Chiao Cheng94b10b52012-08-17 16:59:12 -0700753 invalidateOptionsMenu();
754 }
755
756 /** Returns true if the given intent contains a phone number to populate the dialer with */
757 private boolean isDialIntent(Intent intent) {
758 final String action = intent.getAction();
759 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
760 return true;
761 }
762 if (Intent.ACTION_VIEW.equals(action)) {
763 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700764 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700765 return true;
766 }
767 }
768 return false;
769 }
770
771 /**
772 * Returns an appropriate call origin for this Activity. May return null when no call origin
773 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
774 * for remembering the tab in which the user made a phone call, so the external app's DIAL
775 * request should not be counted.)
776 */
777 public String getCallOrigin() {
778 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
779 }
780
Chiao Cheng94b10b52012-08-17 16:59:12 -0700781 /**
Yorke Leec3766332013-07-31 11:13:16 -0700782 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700783 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700784 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700785 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700786 // Weird race condition where fragment is doing work after the activity is destroyed
787 // due to talkback being on (b/10209937). Just return since we can't do any
788 // constructive here.
789 return;
790 }
791
Yorke Leeef2b7382013-08-09 17:39:25 -0700792 if (DEBUG) {
793 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
794 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700795
Yorke Leec3766332013-07-31 11:13:16 -0700796 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700797 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700798 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700799 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700800 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700801 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700802
Yorke Leeb207f8a2013-08-29 18:51:06 -0700803 final String tag;
804 if (smartDialSearch) {
805 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
806 } else {
807 tag = TAG_REGULAR_SEARCH_FRAGMENT;
808 }
809 mInDialpadSearch = smartDialSearch;
810 mInRegularSearch = !smartDialSearch;
811
Andrew Lee752956e2014-05-15 11:43:38 -0700812 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700813 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700814 if (fragment == null) {
815 if (smartDialSearch) {
816 fragment = new SmartDialSearchFragment();
817 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700818 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700819 }
Andrew Leeb1903842014-05-15 16:11:24 -0700820 transaction.add(R.id.dialtacts_frame, fragment, tag);
821 } else {
822 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700823 }
Andrew Leeb1903842014-05-15 16:11:24 -0700824
Yorke Lee86e21f72014-04-02 16:49:38 -0700825 // DialtactsActivity will provide the options menu
826 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700827 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700828 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700829 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700830
831 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leeec489fb2014-05-19 15:39:27 -0700832 mSearchEditTextLayout.animateExpandOrCollapse(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700833 }
834
835 /**
Yorke Leec3766332013-07-31 11:13:16 -0700836 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700837 */
Yorke Leec3766332013-07-31 11:13:16 -0700838 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700839 // See related bug in enterSearchUI();
840 if (getFragmentManager().isDestroyed()) {
841 return;
842 }
Andrew Leeb1903842014-05-15 16:11:24 -0700843
Andrew Leeb1903842014-05-15 16:11:24 -0700844 mSearchView.setText(null);
845 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700846 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700847
Andrew Leeb1903842014-05-15 16:11:24 -0700848 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
849 transaction.setCustomAnimations(0, android.R.animator.fade_out);
Andrew Leeb1903842014-05-15 16:11:24 -0700850 if (mSmartDialSearchFragment != null) {
851 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700852 }
Andrew Leeb1903842014-05-15 16:11:24 -0700853 if (mRegularSearchFragment != null) {
854 transaction.remove(mRegularSearchFragment);
855 }
856 transaction.commit();
857
858 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leeec489fb2014-05-19 15:39:27 -0700859 mSearchEditTextLayout.animateExpandOrCollapse(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700860 }
861
862 /** Returns an Intent to launch Call Settings screen */
863 public static Intent getCallSettingsIntent() {
864 final Intent intent = new Intent(Intent.ACTION_MAIN);
865 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
866 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
867 return intent;
868 }
869
870 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700871 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700872 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700873 if (TextUtils.isEmpty(mSearchQuery)) {
874 exitSearchUi();
875 }
Yorke Leef6673d32013-08-23 15:34:17 -0700876 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700877 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700878 exitSearchUi();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700879 hideInputMethod(parentLayout);
Yorke Leec3766332013-07-31 11:13:16 -0700880 } else {
881 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700882 }
Yorke Leec3766332013-07-31 11:13:16 -0700883 }
884
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700885 /**
886 * @return True if the search UI was exited, false otherwise
887 */
888 private boolean maybeExitSearchUi() {
889 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
890 exitSearchUi();
891 hideInputMethod(parentLayout);
892 return true;
893 }
894 return false;
895 }
896
Yorke Leec3766332013-07-31 11:13:16 -0700897 @Override
898 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700899 if (mSmartDialSearchFragment != null) {
900 mSmartDialSearchFragment.setAddToContactNumber(query);
901 }
Yorke Leec3766332013-07-31 11:13:16 -0700902 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
903 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700904
905 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700906 if (DEBUG) {
907 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
908 }
909 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
910 // This callback can happen if the dialpad fragment is recreated because of
911 // activity destruction. In that case, don't update the search view because
912 // that would bring the user back to the search fragment regardless of the
913 // previous state of the application. Instead, just return here and let the
914 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700915 if (!TextUtils.isEmpty(normalizedQuery)) {
916 mPendingSearchViewQuery = normalizedQuery;
917 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700918 return;
919 }
Yorke Lee53a22302014-04-29 18:13:46 -0700920 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700921 }
922 }
Yorke Leec3766332013-07-31 11:13:16 -0700923
924 @Override
925 public void onListFragmentScrollStateChange(int scrollState) {
926 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700927 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700928 hideInputMethod(getCurrentFocus());
929 }
930 }
931
932 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700933 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700934 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700935 // TODO: No-op for now. This should eventually show/hide the actionBar based on
936 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700937 }
938
Yorke Lee86e21f72014-04-02 16:49:38 -0700939 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700940 public void setFloatingActionButtonVisible(boolean visible) {
941 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700942 }
943
944 private boolean phoneIsInUse() {
945 final TelephonyManager tm = (TelephonyManager) getSystemService(
946 Context.TELEPHONY_SERVICE);
947 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
948 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700949
Yorke Leee1424812013-09-04 18:24:48 -0700950 public static Intent getAddNumberToContactIntent(CharSequence text) {
951 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
952 intent.putExtra(Intents.Insert.PHONE, text);
953 intent.setType(Contacts.CONTENT_ITEM_TYPE);
954 return intent;
955 }
956
Yorke Leeda0f9042013-12-05 14:25:51 -0800957 private boolean canIntentBeHandled(Intent intent) {
958 final PackageManager packageManager = getPackageManager();
959 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
960 PackageManager.MATCH_DEFAULT_ONLY);
961 return resolveInfo != null && resolveInfo.size() > 0;
962 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800963
Yorke Lee6a1461a2014-04-21 16:27:14 -0700964 @Override
965 public void showCallHistory() {
966 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
967 // CONTENT_TYPE, so that we always open our call log from our dialer
968 final Intent intent = new Intent(this, CallLogActivity.class);
969 startActivity(intent);
970 }
971
Yorke Lee86e21f72014-04-02 16:49:38 -0700972 /**
973 * Called when the user has long-pressed a contact tile to start a drag operation.
974 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800975 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700976 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700977 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700978 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800979 }
980
981 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700982 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
983 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800984
Yorke Lee86e21f72014-04-02 16:49:38 -0700985 /**
986 * Called when the user has released a contact tile after long-pressing it.
987 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800988 @Override
989 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700990 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700991 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800992 }
993
994 @Override
995 public void onDroppedOnRemove() {}
996
997 /**
Yorke Leed999b712014-04-23 15:10:58 -0700998 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800999 * once it has been attached to the activity.
1000 */
1001 @Override
1002 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001003 mDragDropController = dragController;
1004 ((RemoveView) findViewById(R.id.remove_view))
1005 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001006 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001007
1008 @Override
1009 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001010 // Specify call-origin so that users will see the previous tab instead of
1011 // CallLog screen (search UI will be automatically exited).
1012 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001013 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001014 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001015 }
1016
1017 @Override
1018 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001019 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
1020 startActivity(intent);
1021 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001022 }
1023
1024 @Override
1025 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001026 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001027 }
1028
1029 @Override
1030 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001031 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001032 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001033
1034 public int getActionBarHeight() {
1035 return mActionBarHeight;
1036 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001037
1038 @Override
1039 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1040
1041 }
1042
1043 @Override
1044 public void onPageSelected(int position) {
1045 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001046 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001047 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001048 alignFloatingActionButtonByTab(mCurrentTabPosition);
1049 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001050 }
1051
1052 @Override
1053 public void onPageScrollStateChanged(int state) {
1054 }
1055
Andrew Lee2a58ab82014-05-15 02:16:04 -07001056 private void updateFloatingActionButton() {
1057 if (mIsDialpadShown) {
1058 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1059 mFloatingActionButton.setContentDescription(
1060 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001061 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001062 } else {
1063 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1064 mFloatingActionButton.setContentDescription(
1065 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001066 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001067 }
1068 }
1069
Yorke Leecc4660d2014-04-24 11:22:57 -07001070 private void alignFloatingActionButtonByTab(int position) {
1071 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1072 alignFloatingActionButtonMiddle();
1073 } else {
1074 alignFloatingActionButtonRight();
1075 }
1076 }
1077
1078 private void alignFloatingActionButtonRight() {
1079 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001080 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001081 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1082 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001083 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001084 }
1085
1086 private void alignFloatingActionButtonMiddle() {
1087 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001088 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001089 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1090 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001091 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001092 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001093
1094 /**
1095 * Convenience class which implements AnimationListener interface as null-op methods.
1096 */
1097 private class ActivityAnimationListener implements AnimationListener {
1098 @Override
1099 public void onAnimationStart(Animation animation) {
1100 }
1101
1102 @Override
1103 public void onAnimationEnd(Animation animation) {
1104 }
1105
1106 @Override
1107 public void onAnimationRepeat(Animation animation) {
1108 }
1109 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001110}