blob: 8fa4ae66cb1c44e6ba3ecb1af1fe6ec7ab27a662 [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;
Yorke Lee86e21f72014-04-02 16:49:38 -070047import android.view.MenuInflater;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070049import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070051import android.view.View.OnDragListener;
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 Lee0baa98b2013-08-22 10:55:16 -070089import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070090import com.android.internal.telephony.ITelephony;
91
Yorke Leec3766332013-07-31 11:13:16 -070092import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080093import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070094
Chiao Cheng94b10b52012-08-17 16:59:12 -070095/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070096 * The dialer tab's title is 'phone', a more common name (see strings.xml).
97 */
Yorke Leec3766332013-07-31 11:13:16 -070098public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070099 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700100 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -0800101 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700102 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700103 SpeedDialFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700104 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700105 OnPhoneNumberPickerActionListener,
106 ViewPager.OnPageChangeListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700107 private static final String TAG = "DialtactsActivity";
108
Ihab Awad526c0b82014-02-27 12:55:36 -0800109 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700110
Yorke Leef74011e2013-08-02 11:30:30 -0700111 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
112
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 /** Used to open Call Setting */
114 private static final String PHONE_PACKAGE = "com.android.phone";
115 private static final String CALL_SETTINGS_CLASS_NAME =
116 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700117 /** @see #getCallOrigin() */
118 private static final String CALL_ORIGIN_DIALTACTS =
119 "com.android.dialer.DialtactsActivity";
120
Yorke Leeb207f8a2013-08-29 18:51:06 -0700121 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
122 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700123 private static final String KEY_SEARCH_QUERY = "search_query";
124 private static final String KEY_FIRST_LAUNCH = "first_launch";
125
Yorke Leec3766332013-07-31 11:13:16 -0700126 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
127 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
128 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
129 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700130
Chiao Cheng94b10b52012-08-17 16:59:12 -0700131 /**
132 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
133 */
134 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
135
Yorke Leec3766332013-07-31 11:13:16 -0700136 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700137
Yorke Lee86e21f72014-04-02 16:49:38 -0700138 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800139
Andrew Lee0c667702014-05-12 14:31:45 -0700140 private RelativeLayout parentLayout;
141
Yorke Leec3766332013-07-31 11:13:16 -0700142 /**
Yorke Leec3766332013-07-31 11:13:16 -0700143 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700144 */
Yorke Leef74011e2013-08-02 11:30:30 -0700145 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700146
147 /**
148 * Fragment for searching phone numbers using the alphanumeric keyboard.
149 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700150 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700151
152 /**
153 * Fragment for searching phone numbers using the dialpad.
154 */
155 private SmartDialSearchFragment mSmartDialSearchFragment;
156
Yorke Leee00c9fe2014-04-12 12:42:06 -0700157 /**
158 * Fragment containing the speed dial list, recents list, and all contacts list.
159 */
160 private ListsFragment mListsFragment;
161
Andrew Leea73e1892014-05-13 13:21:16 -0700162 private View mFloatingActionButtonContainer;
163 private ImageButton mFloatingActionButton;
Yorke Leec3766332013-07-31 11:13:16 -0700164
Yorke Lee33932ff2014-04-16 13:34:32 -0700165 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700166 private boolean mInDialpadSearch;
167 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700168 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700169 private boolean mIsDialpadShown;
Andrew Leeb1903842014-05-15 16:11:24 -0700170 private boolean mIsExitingSearch;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700171
Yorke Leeef2b7382013-08-09 17:39:25 -0700172 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700173 * The position of the currently selected tab in the attached {@link ListsFragment}.
174 */
175 private int mCurrentTabPosition = 0;
176
177 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700178 * True if the dialpad is only temporarily showing due to being in call
179 */
180 private boolean mInCallDialpadUp;
181
182 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700183 * True when this activity has been launched for the first time.
184 */
Yorke Lee98702de2013-08-06 12:03:32 -0700185 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700186
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700187 /**
188 * Search query to be applied to the SearchView in the ActionBar once
189 * onCreateOptionsMenu has been called.
190 */
191 private String mPendingSearchViewQuery;
192
Yorke Lee53a22302014-04-29 18:13:46 -0700193 private EditText mSearchView;
194 private View mSearchViewCloseButton;
195 private View mVoiceSearchButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700196 /**
197 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
198 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
199 * removed from the speed dial list.
200 */
Yorke Lee28266192014-05-01 10:05:52 -0700201 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700202
Yorke Lee86e21f72014-04-02 16:49:38 -0700203 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
204 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700205 private String mSearchQuery;
206
Yorke Leefce269a2013-08-12 11:56:04 -0700207 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700208 private DragDropController mDragDropController;
Yorke Leefce269a2013-08-12 11:56:04 -0700209
Yorke Leee00c9fe2014-04-12 12:42:06 -0700210 private class OverflowPopupMenu extends PopupMenu {
211 public OverflowPopupMenu(Context context, View anchor) {
212 super(context, anchor);
213 }
214
215 @Override
216 public void show() {
217 final Menu menu = getMenu();
218 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Leed999b712014-04-23 15:10:58 -0700219 // TODO: Check mSpeedDialFragment.hasFrequents()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700220 clearFrequents.setVisible(true);
221 super.show();
222 }
223 }
224
Chiao Cheng94b10b52012-08-17 16:59:12 -0700225 /**
Yorke Lee28266192014-05-01 10:05:52 -0700226 * Listener that listens to drag events and sends their x and y coordinates to a
227 * {@link DragDropController}.
228 */
229 private class LayoutOnDragListener implements OnDragListener {
230 @Override
231 public boolean onDrag(View v, DragEvent event) {
232 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
233 mDragDropController.handleDragHovered(v, (int) event.getX(),
234 (int) event.getY());
235 }
236 return true;
237 }
238 }
239
240 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700241 * Listener used to send search queries to the phone search fragment.
242 */
Yorke Lee53a22302014-04-29 18:13:46 -0700243 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700244 @Override
245 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
246 }
Yorke Leec3766332013-07-31 11:13:16 -0700247
Andrew Lee99a570c2014-05-15 14:18:50 -0700248 @Override
249 public void onTextChanged(CharSequence s, int start, int before, int count) {
250 final String newText = s.toString();
251 if (newText.equals(mSearchQuery)) {
252 // If the query hasn't changed (perhaps due to activity being destroyed
253 // and restored, or user launching the same DIAL intent twice), then there is
254 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700255 return;
256 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700257 mSearchQuery = newText;
258 if (DEBUG) {
259 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leec3766332013-07-31 11:13:16 -0700260 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700261
262 // Show search result with non-empty text. Show a bare list otherwise.
263 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
264 (!mIsDialpadShown && mInRegularSearch);
265 if (!sameSearchMode) {
266 // call enterSearchUi only if we are switching search modes, or entering
267 // search ui for the first time
Andrew Leeb1903842014-05-15 16:11:24 -0700268 enterSearchUi(mIsDialpadShown, mSearchQuery);
Andrew Lee99a570c2014-05-15 14:18:50 -0700269 }
270
271 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700272 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700273 } else if (mRegularSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700274 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700275 }
276
277 if (TextUtils.isEmpty(newText)) {
278 mSearchViewCloseButton.setVisibility(View.GONE);
279 mVoiceSearchButton.setVisibility(View.VISIBLE);
280 } else {
281 mSearchViewCloseButton.setVisibility(View.VISIBLE);
282 mVoiceSearchButton.setVisibility(View.GONE);
283 }
284 }
285
286 @Override
287 public void afterTextChanged(Editable s) {
288 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700289 };
290
Andrew Leeb1903842014-05-15 16:11:24 -0700291
292 /**
293 * Open the search UI when the user touches the search text view.
294 */
295 private final View.OnTouchListener mSearchViewOnTouchListener = new View.OnTouchListener() {
296 @Override
297 public boolean onTouch(View v, MotionEvent event) {
298 if (!isInSearchUi()) {
299 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
300 }
301 return false;
302 }
303 };
304
305 /**
306 * If the search term is empty and the user closes the soft keyboard, close the search UI.
307 */
308 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
309 @Override
310 public boolean onKey(View v, int keyCode, KeyEvent event) {
311 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
312 TextUtils.isEmpty(mSearchView.getText().toString())) {
313 onBackPressed();
314 }
315 return false;
316 }
317 };
318
Chiao Cheng94b10b52012-08-17 16:59:12 -0700319 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700320 protected void onCreate(Bundle savedInstanceState) {
321 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700322 mFirstLaunch = true;
323
Yorke Lee8898cd02013-08-08 10:24:27 -0700324 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800325 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700326
Yorke Lee53a22302014-04-29 18:13:46 -0700327 final ActionBar actionBar = getActionBar();
328 actionBar.setCustomView(R.layout.search_edittext);
329 actionBar.setDisplayShowCustomEnabled(true);
330
Andrew Leeb1903842014-05-15 16:11:24 -0700331 SearchEditTextLayout actionBarView = (SearchEditTextLayout) actionBar.getCustomView();
332 actionBarView.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700333
Andrew Leeb1903842014-05-15 16:11:24 -0700334 mSearchView = (EditText) actionBarView.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700335 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Leeb1903842014-05-15 16:11:24 -0700336 mSearchView.setOnTouchListener(mSearchViewOnTouchListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700337
Andrew Leeb1903842014-05-15 16:11:24 -0700338
339 mSearchViewCloseButton = actionBarView.findViewById(R.id.search_close_button);
Yorke Lee53a22302014-04-29 18:13:46 -0700340 mSearchViewCloseButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700341
Yorke Lee33932ff2014-04-16 13:34:32 -0700342 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
343 new int[] { android.R.attr.actionBarSize });
344 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
345 styledAttributes.recycle();
346
Yorke Leeef2b7382013-08-09 17:39:25 -0700347 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
348 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700349 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800350 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700351 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800352 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
353 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700354 } else {
355 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700356 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
357 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700358 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700359 }
360
Andrew Lee0c667702014-05-12 14:31:45 -0700361 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
362 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
363 parentLayout.setOnDragListener(new LayoutOnDragListener());
364
Andrew Leea73e1892014-05-13 13:21:16 -0700365 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
366 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700367
Andrew Leea73e1892014-05-13 13:21:16 -0700368 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
369 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800370
Yorke Lee28266192014-05-01 10:05:52 -0700371 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800372
Yorke Lee0baa98b2013-08-22 10:55:16 -0700373 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700374 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700375 }
376
377 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700378 protected void onResume() {
379 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700380 if (mFirstLaunch) {
381 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700382 } else if (!phoneIsInUse() && mInCallDialpadUp) {
383 hideDialpadFragment(false, true);
384 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700385 }
386 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700387 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700388 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700389 }
390
391 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700392 protected void onPause() {
393 if (mClearSearchOnPause) {
394 hideDialpadAndSearchUi();
395 mClearSearchOnPause = false;
396 }
397 super.onPause();
398 }
399
400 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700401 protected void onSaveInstanceState(Bundle outState) {
402 super.onSaveInstanceState(outState);
403 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700404 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
405 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700406 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
407 }
408
409 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700410 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700411 if (fragment instanceof DialpadFragment) {
412 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700413 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700414 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700415 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700416 } else if (fragment instanceof SmartDialSearchFragment) {
417 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700418 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700419 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700420 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700421 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700422 } else if (fragment instanceof ListsFragment) {
423 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700424 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700425 }
Yorke Leec3766332013-07-31 11:13:16 -0700426 }
427
Alon Albertb453e5b2013-09-10 15:54:23 -0700428 protected void handleMenuSettings() {
429 openTelephonySetting(this);
430 }
431
432 public static void openTelephonySetting(Activity activity) {
433 final Intent settingsIntent = getCallSettingsIntent();
434 activity.startActivity(settingsIntent);
435 }
436
Chiao Cheng94b10b52012-08-17 16:59:12 -0700437 @Override
438 public void onClick(View view) {
439 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700440 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700441 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700442 mInCallDialpadUp = false;
443 showDialpadFragment(true);
444 } else {
445 // Dial button was pressed; tell the Dialpad fragment
446 mDialpadFragment.dialButtonPressed();
447 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700448 break;
Yorke Leec3766332013-07-31 11:13:16 -0700449 case R.id.search_close_button:
450 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700451 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700452 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700453 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700454 }
455 break;
Yorke Leec3766332013-07-31 11:13:16 -0700456 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800457 try {
458 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
459 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
460 } catch (ActivityNotFoundException e) {
461 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
462 Toast.LENGTH_SHORT).show();
463 }
Yorke Leec3766332013-07-31 11:13:16 -0700464 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700465 default: {
466 Log.wtf(TAG, "Unexpected onClick event from " + view);
467 break;
468 }
469 }
470 }
471
Yorke Leec3766332013-07-31 11:13:16 -0700472 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700473 public boolean onOptionsItemSelected(MenuItem item) {
474 switch (item.getItemId()) {
475 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700476 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700477 break;
478 case R.id.menu_add_contact:
479 try {
480 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
481 } catch (ActivityNotFoundException e) {
482 Toast toast = Toast.makeText(this,
483 R.string.add_contact_not_available,
484 Toast.LENGTH_SHORT);
485 toast.show();
486 }
487 break;
488 case R.id.menu_import_export:
489 // We hard-code the "contactsAreAvailable" argument because doing it properly would
490 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
491 // now in Dialtacts for (potential) performance reasons. Compare with how it is
492 // done in {@link PeopleActivity}.
493 ImportExportDialogFragment.show(getFragmentManager(), true,
494 DialtactsActivity.class);
495 return true;
496 case R.id.menu_clear_frequents:
497 // TODO: This should be enabled/disabled based on
Yorke Leed999b712014-04-23 15:10:58 -0700498 // SpeedDialFragment.hasFrequents
Yorke Lee86e21f72014-04-02 16:49:38 -0700499 ClearFrequentsDialog.show(getFragmentManager());
500 return true;
501 case R.id.menu_call_settings:
502 handleMenuSettings();
503 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700504 }
505 return false;
506 }
507
508 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700509 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
510 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
511 if (resultCode == RESULT_OK) {
512 final ArrayList<String> matches = data.getStringArrayListExtra(
513 RecognizerIntent.EXTRA_RESULTS);
514 if (matches.size() > 0) {
515 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700516 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700517 } else {
518 Log.e(TAG, "Voice search - nothing heard");
519 }
520 } else {
521 Log.e(TAG, "Voice search failed");
522 }
523 }
524 super.onActivityResult(requestCode, resultCode, data);
525 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700526
Andrew Lee2a58ab82014-05-15 02:16:04 -0700527 /**
528 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
529 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
530 * @see #onDialpadShown
531 */
Yorke Leec3766332013-07-31 11:13:16 -0700532 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700533 if (mIsDialpadShown) {
534 return;
535 }
536 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700537 mDialpadFragment.setAnimate(animate);
538
Chiao Cheng94b10b52012-08-17 16:59:12 -0700539 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700540 ft.show(mDialpadFragment);
541 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700542
543 if (!isInSearchUi()) {
544 enterSearchUi(true /* isSmartDial */, mSearchQuery);
545 }
Yorke Leec3766332013-07-31 11:13:16 -0700546 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700547
Andrew Lee2a58ab82014-05-15 02:16:04 -0700548 /**
549 * Callback from child DialpadFragment when the dialpad is shown.
550 */
551 public void onDialpadShown() {
552 updateFloatingActionButton();
553 if (mDialpadFragment.getAnimate()) {
554 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
555 mDialpadFragment.getView().startAnimation(slideIn);
556 } else {
557 mDialpadFragment.setYFraction(0);
558 }
559
Andrew Lee2a58ab82014-05-15 02:16:04 -0700560 updateSearchFragmentPosition();
561 getActionBar().hide();
562 }
563
564 /**
565 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
566 * a callback after the hide animation ends.
567 * @see #commitDialpadFragmentHide
568 */
Yorke Leeca195042013-09-25 16:29:38 -0700569 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700570 if (mDialpadFragment == null) {
571 return;
572 }
Yorke Leef6673d32013-08-23 15:34:17 -0700573 if (clearDialpad) {
574 mDialpadFragment.clearDialpad();
575 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700576 if (!mIsDialpadShown) {
577 return;
Yorke Leec3766332013-07-31 11:13:16 -0700578 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700579 mIsDialpadShown = false;
580 mDialpadFragment.setAnimate(animate);
581
582 updateFloatingActionButton();
583 if (animate) {
584 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
585 slideOut.setAnimationListener(new ActivityAnimationListener() {
586 @Override
587 public void onAnimationEnd(Animation animation) {
588 commitDialpadFragmentHide();
589 }
590 });
591 mDialpadFragment.getView().startAnimation(slideOut);
592 } else {
593 commitDialpadFragmentHide();
594 }
595
Andrew Lee2a58ab82014-05-15 02:16:04 -0700596 updateSearchFragmentPosition();
Andrew Leeb1903842014-05-15 16:11:24 -0700597 mListsFragment.maybeShowActionBar();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700598 }
599
600 /**
601 * Finishes hiding the dialpad fragment after any animations are completed.
602 */
603 private void commitDialpadFragmentHide() {
604 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700605 ft.hide(mDialpadFragment);
606 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700607 }
608
Andrew Lee2a58ab82014-05-15 02:16:04 -0700609 private void updateSearchFragmentPosition() {
610 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
611 SearchFragment fragment = null;
612 if (mInDialpadSearch) {
613 fragment = mSmartDialSearchFragment;
614 } else if (mInRegularSearch) {
615 fragment = mRegularSearchFragment;
616 }
617 if (fragment != null && fragment.isVisible()) {
618 fragment.getView().animate().translationY(translationValue)
619 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
620 }
621 }
622
Andrew Leeb1903842014-05-15 16:11:24 -0700623 private boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700624 return mInDialpadSearch || mInRegularSearch;
625 }
626
627 private void setNotInSearchUi() {
628 mInDialpadSearch = false;
629 mInRegularSearch = false;
630 }
631
Yorke Lee311969c2013-08-26 06:31:11 -0700632 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700633 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700634 hideDialpadFragment(false, true);
635 }
636
Yorke Leee00c9fe2014-04-12 12:42:06 -0700637 private void hideInputMethod(View view) {
638 final InputMethodManager imm = (InputMethodManager) getSystemService(
639 Context.INPUT_METHOD_SERVICE);
640 if (imm != null && view != null) {
641 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
642 }
643 }
644
Yorke Lee53a22302014-04-29 18:13:46 -0700645 private void prepareVoiceSearchButton() {
646 mVoiceSearchButton = getActionBar().getCustomView().findViewById(R.id.voice_search_button);
647 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
648 if (canIntentBeHandled(voiceIntent)) {
649 mVoiceSearchButton.setVisibility(View.VISIBLE);
650 mVoiceSearchButton.setOnClickListener(this);
651 } else {
652 mVoiceSearchButton.setVisibility(View.GONE);
653 }
654 }
655
Yorke Lee86e21f72014-04-02 16:49:38 -0700656 @Override
657 public boolean onCreateOptionsMenu(Menu menu) {
658 if (DEBUG) {
659 Log.d(TAG, "onCreateOptionsMenu");
660 }
661 MenuInflater inflater = getMenuInflater();
662 inflater.inflate(R.menu.dialtacts_options, menu);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700663
664 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700665 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700666 mPendingSearchViewQuery = null;
667 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700668 return super.onCreateOptionsMenu(menu);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700669 }
670
Chiao Cheng94b10b52012-08-17 16:59:12 -0700671 /**
672 * Returns true if the intent is due to hitting the green send key (hardware call button:
673 * KEYCODE_CALL) while in a call.
674 *
675 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700676 * @return true if the intent is due to hitting the green send key while in a call
677 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700678 private boolean isSendKeyWhileInCall(Intent intent) {
679 // If there is a call in progress and the user launched the dialer by hitting the call
680 // button, go straight to the in-call screen.
681 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700682
Yorke Lee62e995c2014-03-28 10:02:56 -0700683 try {
684 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
685 if (callKey && phone != null && phone.showCallScreen()) {
686 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700687 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700688 } catch (RemoteException e) {
689 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700690 }
691
692 return false;
693 }
694
695 /**
696 * Sets the current tab based on the intent's request type
697 *
698 * @param intent Intent that contains information about which tab should be selected
699 */
Yorke Leec3766332013-07-31 11:13:16 -0700700 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700701 // 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 -0700702 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700703 finish();
704 return;
705 }
706
Yorke Lee669b6082013-09-17 15:43:38 -0700707 if (mDialpadFragment != null) {
708 final boolean phoneIsInUse = phoneIsInUse();
709 if (phoneIsInUse || isDialIntent(intent)) {
710 mDialpadFragment.setStartedFromNewIntent(true);
711 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
712 mInCallDialpadUp = true;
713 }
714 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700715 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700716 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700717 }
718
719 @Override
720 public void onNewIntent(Intent newIntent) {
721 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700722 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700723
Chiao Cheng94b10b52012-08-17 16:59:12 -0700724 invalidateOptionsMenu();
725 }
726
727 /** Returns true if the given intent contains a phone number to populate the dialer with */
728 private boolean isDialIntent(Intent intent) {
729 final String action = intent.getAction();
730 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
731 return true;
732 }
733 if (Intent.ACTION_VIEW.equals(action)) {
734 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700735 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700736 return true;
737 }
738 }
739 return false;
740 }
741
742 /**
743 * Returns an appropriate call origin for this Activity. May return null when no call origin
744 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
745 * for remembering the tab in which the user made a phone call, so the external app's DIAL
746 * request should not be counted.)
747 */
748 public String getCallOrigin() {
749 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
750 }
751
Chiao Cheng94b10b52012-08-17 16:59:12 -0700752 /**
Yorke Leec3766332013-07-31 11:13:16 -0700753 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700754 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700755 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Leeb1903842014-05-15 16:11:24 -0700756 if (getFragmentManager().isDestroyed() || mIsExitingSearch) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700757 // Weird race condition where fragment is doing work after the activity is destroyed
758 // due to talkback being on (b/10209937). Just return since we can't do any
759 // constructive here.
760 return;
761 }
762
Yorke Leeef2b7382013-08-09 17:39:25 -0700763 if (DEBUG) {
764 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
765 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700766
Yorke Leec3766332013-07-31 11:13:16 -0700767 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700768 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700769 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700770 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700771 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700772 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700773
Yorke Leeb207f8a2013-08-29 18:51:06 -0700774 final String tag;
775 if (smartDialSearch) {
776 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
777 } else {
778 tag = TAG_REGULAR_SEARCH_FRAGMENT;
779 }
780 mInDialpadSearch = smartDialSearch;
781 mInRegularSearch = !smartDialSearch;
782
Andrew Lee752956e2014-05-15 11:43:38 -0700783 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700784 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700785 if (fragment == null) {
786 if (smartDialSearch) {
787 fragment = new SmartDialSearchFragment();
788 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700789 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700790 }
Andrew Leeb1903842014-05-15 16:11:24 -0700791 transaction.add(R.id.dialtacts_frame, fragment, tag);
792 } else {
793 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700794 }
Andrew Leeb1903842014-05-15 16:11:24 -0700795
Yorke Lee86e21f72014-04-02 16:49:38 -0700796 // DialtactsActivity will provide the options menu
797 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700798 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700799 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700800 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700801
802 mListsFragment.getView().animate().alpha(0).withLayer();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700803 }
804
805 /**
Yorke Leec3766332013-07-31 11:13:16 -0700806 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700807 */
Yorke Leec3766332013-07-31 11:13:16 -0700808 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700809 // See related bug in enterSearchUI();
810 if (getFragmentManager().isDestroyed()) {
811 return;
812 }
Andrew Leeb1903842014-05-15 16:11:24 -0700813
814 mIsExitingSearch = true;
815
816 mSearchView.setText(null);
817 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700818 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700819
Andrew Leeb1903842014-05-15 16:11:24 -0700820 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
821 transaction.setCustomAnimations(0, android.R.animator.fade_out);
822
823 if (mSmartDialSearchFragment != null) {
824 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700825 }
Andrew Leeb1903842014-05-15 16:11:24 -0700826 if (mRegularSearchFragment != null) {
827 transaction.remove(mRegularSearchFragment);
828 }
829 transaction.commit();
830
831 mListsFragment.getView().animate().alpha(1).withLayer();
832
833 mIsExitingSearch = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700834 }
835
836 /** Returns an Intent to launch Call Settings screen */
837 public static Intent getCallSettingsIntent() {
838 final Intent intent = new Intent(Intent.ACTION_MAIN);
839 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
840 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
841 return intent;
842 }
843
844 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700845 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700846 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700847 if (TextUtils.isEmpty(mSearchQuery)) {
848 exitSearchUi();
849 }
Yorke Leef6673d32013-08-23 15:34:17 -0700850 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700851 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700852 exitSearchUi();
Yorke Leec3766332013-07-31 11:13:16 -0700853 } else {
854 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700855 }
Yorke Leec3766332013-07-31 11:13:16 -0700856 }
857
858 @Override
859 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700860 if (mSmartDialSearchFragment != null) {
861 mSmartDialSearchFragment.setAddToContactNumber(query);
862 }
Yorke Leec3766332013-07-31 11:13:16 -0700863 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
864 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700865
866 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700867 if (DEBUG) {
868 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
869 }
870 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
871 // This callback can happen if the dialpad fragment is recreated because of
872 // activity destruction. In that case, don't update the search view because
873 // that would bring the user back to the search fragment regardless of the
874 // previous state of the application. Instead, just return here and let the
875 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700876 if (!TextUtils.isEmpty(normalizedQuery)) {
877 mPendingSearchViewQuery = normalizedQuery;
878 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700879 return;
880 }
Yorke Lee53a22302014-04-29 18:13:46 -0700881 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700882 }
883 }
Yorke Leec3766332013-07-31 11:13:16 -0700884
885 @Override
886 public void onListFragmentScrollStateChange(int scrollState) {
887 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700888 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700889 hideInputMethod(getCurrentFocus());
890 }
891 }
892
893 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700894 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700895 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700896 // TODO: No-op for now. This should eventually show/hide the actionBar based on
897 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700898 }
899
Yorke Lee86e21f72014-04-02 16:49:38 -0700900 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700901 public void setFloatingActionButtonVisible(boolean visible) {
902 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700903 }
904
905 private boolean phoneIsInUse() {
906 final TelephonyManager tm = (TelephonyManager) getSystemService(
907 Context.TELEPHONY_SERVICE);
908 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
909 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700910
Yorke Leee1424812013-09-04 18:24:48 -0700911 public static Intent getAddNumberToContactIntent(CharSequence text) {
912 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
913 intent.putExtra(Intents.Insert.PHONE, text);
914 intent.setType(Contacts.CONTENT_ITEM_TYPE);
915 return intent;
916 }
917
Yorke Leeda0f9042013-12-05 14:25:51 -0800918 private boolean canIntentBeHandled(Intent intent) {
919 final PackageManager packageManager = getPackageManager();
920 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
921 PackageManager.MATCH_DEFAULT_ONLY);
922 return resolveInfo != null && resolveInfo.size() > 0;
923 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800924
Yorke Lee6a1461a2014-04-21 16:27:14 -0700925 @Override
926 public void showCallHistory() {
927 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
928 // CONTENT_TYPE, so that we always open our call log from our dialer
929 final Intent intent = new Intent(this, CallLogActivity.class);
930 startActivity(intent);
931 }
932
Yorke Lee86e21f72014-04-02 16:49:38 -0700933 /**
934 * Called when the user has long-pressed a contact tile to start a drag operation.
935 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800936 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700937 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700938 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700939 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800940 }
941
942 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700943 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
944 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800945
Yorke Lee86e21f72014-04-02 16:49:38 -0700946 /**
947 * Called when the user has released a contact tile after long-pressing it.
948 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800949 @Override
950 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700951 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700952 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800953 }
954
955 @Override
956 public void onDroppedOnRemove() {}
957
958 /**
Yorke Leed999b712014-04-23 15:10:58 -0700959 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800960 * once it has been attached to the activity.
961 */
962 @Override
963 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -0700964 mDragDropController = dragController;
965 ((RemoveView) findViewById(R.id.remove_view))
966 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800967 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700968
969 @Override
970 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700971 // Specify call-origin so that users will see the previous tab instead of
972 // CallLog screen (search UI will be automatically exited).
973 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -0700974 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700975 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700976 }
977
978 @Override
979 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700980 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
981 startActivity(intent);
982 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700983 }
984
985 @Override
986 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700987 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -0700988 }
989
990 @Override
991 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700992 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700993 }
Yorke Lee33932ff2014-04-16 13:34:32 -0700994
995 public int getActionBarHeight() {
996 return mActionBarHeight;
997 }
Yorke Leecc4660d2014-04-24 11:22:57 -0700998
999 @Override
1000 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1001
1002 }
1003
1004 @Override
1005 public void onPageSelected(int position) {
1006 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001007 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001008 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001009 alignFloatingActionButtonByTab(mCurrentTabPosition);
1010 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001011 }
1012
1013 @Override
1014 public void onPageScrollStateChanged(int state) {
1015 }
1016
Andrew Lee2a58ab82014-05-15 02:16:04 -07001017 private void updateFloatingActionButton() {
1018 if (mIsDialpadShown) {
1019 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1020 mFloatingActionButton.setContentDescription(
1021 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001022 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001023 } else {
1024 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1025 mFloatingActionButton.setContentDescription(
1026 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001027 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001028 }
1029 }
1030
Yorke Leecc4660d2014-04-24 11:22:57 -07001031 private void alignFloatingActionButtonByTab(int position) {
1032 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1033 alignFloatingActionButtonMiddle();
1034 } else {
1035 alignFloatingActionButtonRight();
1036 }
1037 }
1038
1039 private void alignFloatingActionButtonRight() {
1040 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001041 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001042 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1043 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001044 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001045 }
1046
1047 private void alignFloatingActionButtonMiddle() {
1048 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001049 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001050 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1051 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001052 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001053 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001054
1055 /**
1056 * Convenience class which implements AnimationListener interface as null-op methods.
1057 */
1058 private class ActivityAnimationListener implements AnimationListener {
1059 @Override
1060 public void onAnimationStart(Animation animation) {
1061 }
1062
1063 @Override
1064 public void onAnimationEnd(Animation animation) {
1065 }
1066
1067 @Override
1068 public void onAnimationRepeat(Animation animation) {
1069 }
1070 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001071}