blob: ef1db39680c0566ba49f85bf9823972b48557940 [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 Lee86e21f72014-04-02 16:49:38 -070051import android.view.animation.AccelerateInterpolator;
Andrew Lee2a58ab82014-05-15 02:16:04 -070052import android.view.animation.Animation;
53import android.view.animation.Animation.AnimationListener;
54import android.view.animation.AnimationUtils;
Yorke Lee86e21f72014-04-02 16:49:38 -070055import android.view.animation.DecelerateInterpolator;
56import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070058import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070059import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070060import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070061import android.widget.PopupMenu;
Yorke Leecc4660d2014-04-24 11:22:57 -070062import android.widget.RelativeLayout;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080063import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070064
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070065import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070066import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070067import com.android.contacts.common.dialog.ClearFrequentsDialog;
68import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080069import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070070import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070072import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070073import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070074import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070075import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070076import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080077import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070078import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080079import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070080import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070081import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070082import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070083import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080084import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070085import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070086import com.android.dialer.list.SmartDialSearchFragment;
Andrew Leeb1903842014-05-15 16:11:24 -070087import com.android.dialer.widget.SearchEditTextLayout;
Yorke Lee0baa98b2013-08-22 10:55:16 -070088import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070089import com.android.internal.telephony.ITelephony;
90
Yorke Leec3766332013-07-31 11:13:16 -070091import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080092import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070093
Chiao Cheng94b10b52012-08-17 16:59:12 -070094/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070095 * The dialer tab's title is 'phone', a more common name (see strings.xml).
96 */
Yorke Leec3766332013-07-31 11:13:16 -070097public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070098 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -070099 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -0800100 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700101 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700102 SpeedDialFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700103 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700104 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700105 PopupMenu.OnMenuItemClickListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700106 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;
Andrew Leee74a10e2014-05-16 14:31:40 -0700196
Yorke Lee86e21f72014-04-02 16:49:38 -0700197 /**
198 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
199 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
200 * removed from the speed dial list.
201 */
Yorke Lee28266192014-05-01 10:05:52 -0700202 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700203
Yorke Lee86e21f72014-04-02 16:49:38 -0700204 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
205 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700206 private String mSearchQuery;
207
Yorke Leefce269a2013-08-12 11:56:04 -0700208 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700209 private DragDropController mDragDropController;
Yorke Leefce269a2013-08-12 11:56:04 -0700210
Andrew Leee74a10e2014-05-16 14:31:40 -0700211 private class OptionsPopupMenu extends PopupMenu {
212 public OptionsPopupMenu(Context context, View anchor) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700213 super(context, anchor);
214 }
215
216 @Override
217 public void show() {
218 final Menu menu = getMenu();
219 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700220 clearFrequents.setVisible(mListsFragment != null &&
221 mListsFragment.getSpeedDialFragment() != null &&
222 mListsFragment.getSpeedDialFragment().hasFrequents());
Yorke Leee00c9fe2014-04-12 12:42:06 -0700223 super.show();
224 }
225 }
226
Chiao Cheng94b10b52012-08-17 16:59:12 -0700227 /**
Yorke Lee28266192014-05-01 10:05:52 -0700228 * Listener that listens to drag events and sends their x and y coordinates to a
229 * {@link DragDropController}.
230 */
231 private class LayoutOnDragListener implements OnDragListener {
232 @Override
233 public boolean onDrag(View v, DragEvent event) {
234 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
235 mDragDropController.handleDragHovered(v, (int) event.getX(),
236 (int) event.getY());
237 }
238 return true;
239 }
240 }
241
242 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700243 * Listener used to send search queries to the phone search fragment.
244 */
Yorke Lee53a22302014-04-29 18:13:46 -0700245 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700246 @Override
247 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
248 }
Yorke Leec3766332013-07-31 11:13:16 -0700249
Andrew Lee99a570c2014-05-15 14:18:50 -0700250 @Override
251 public void onTextChanged(CharSequence s, int start, int before, int count) {
252 final String newText = s.toString();
253 if (newText.equals(mSearchQuery)) {
254 // If the query hasn't changed (perhaps due to activity being destroyed
255 // and restored, or user launching the same DIAL intent twice), then there is
256 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700257 return;
258 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700259 mSearchQuery = newText;
260 if (DEBUG) {
261 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leec3766332013-07-31 11:13:16 -0700262 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700263
264 // Show search result with non-empty text. Show a bare list otherwise.
265 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
266 (!mIsDialpadShown && mInRegularSearch);
267 if (!sameSearchMode) {
268 // call enterSearchUi only if we are switching search modes, or entering
269 // search ui for the first time
Andrew Leeb1903842014-05-15 16:11:24 -0700270 enterSearchUi(mIsDialpadShown, mSearchQuery);
Andrew Lee99a570c2014-05-15 14:18:50 -0700271 }
272
273 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700274 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700275 } else if (mRegularSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700276 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700277 }
278
279 if (TextUtils.isEmpty(newText)) {
280 mSearchViewCloseButton.setVisibility(View.GONE);
281 mVoiceSearchButton.setVisibility(View.VISIBLE);
282 } else {
283 mSearchViewCloseButton.setVisibility(View.VISIBLE);
284 mVoiceSearchButton.setVisibility(View.GONE);
285 }
286 }
287
288 @Override
289 public void afterTextChanged(Editable s) {
290 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291 };
292
Andrew Leeb1903842014-05-15 16:11:24 -0700293
294 /**
295 * Open the search UI when the user touches the search text view.
296 */
297 private final View.OnTouchListener mSearchViewOnTouchListener = new View.OnTouchListener() {
298 @Override
299 public boolean onTouch(View v, MotionEvent event) {
300 if (!isInSearchUi()) {
301 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
302 }
303 return false;
304 }
305 };
306
307 /**
308 * If the search term is empty and the user closes the soft keyboard, close the search UI.
309 */
310 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
311 @Override
312 public boolean onKey(View v, int keyCode, KeyEvent event) {
313 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
314 TextUtils.isEmpty(mSearchView.getText().toString())) {
315 onBackPressed();
316 }
317 return false;
318 }
319 };
320
Chiao Cheng94b10b52012-08-17 16:59:12 -0700321 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700322 protected void onCreate(Bundle savedInstanceState) {
323 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700324 mFirstLaunch = true;
325
Yorke Lee8898cd02013-08-08 10:24:27 -0700326 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800327 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700328
Yorke Lee53a22302014-04-29 18:13:46 -0700329 final ActionBar actionBar = getActionBar();
330 actionBar.setCustomView(R.layout.search_edittext);
331 actionBar.setDisplayShowCustomEnabled(true);
332
Andrew Leeb1903842014-05-15 16:11:24 -0700333 SearchEditTextLayout actionBarView = (SearchEditTextLayout) actionBar.getCustomView();
334 actionBarView.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700335
Andrew Leeb1903842014-05-15 16:11:24 -0700336 mSearchView = (EditText) actionBarView.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700337 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Leeb1903842014-05-15 16:11:24 -0700338 mSearchView.setOnTouchListener(mSearchViewOnTouchListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700339
Andrew Leeb1903842014-05-15 16:11:24 -0700340 mSearchViewCloseButton = actionBarView.findViewById(R.id.search_close_button);
Yorke Lee53a22302014-04-29 18:13:46 -0700341 mSearchViewCloseButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700342
Andrew Leee74a10e2014-05-16 14:31:40 -0700343 ImageButton optionsMenuButton =
344 (ImageButton) actionBarView.findViewById(R.id.dialtacts_options_menu_button);
345 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
Andrew Leea73e1892014-05-13 13:21:16 -0700372 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
373 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700374
Andrew Leea73e1892014-05-13 13:21:16 -0700375 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
376 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800377
Yorke Lee28266192014-05-01 10:05:52 -0700378 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800379
Yorke Lee0baa98b2013-08-22 10:55:16 -0700380 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700381 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700382 }
383
384 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700385 protected void onResume() {
386 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700387 if (mFirstLaunch) {
388 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700389 } else if (!phoneIsInUse() && mInCallDialpadUp) {
390 hideDialpadFragment(false, true);
391 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700392 }
393 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700394 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700395 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700396 }
397
398 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700399 protected void onPause() {
400 if (mClearSearchOnPause) {
401 hideDialpadAndSearchUi();
402 mClearSearchOnPause = false;
403 }
404 super.onPause();
405 }
406
407 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700408 protected void onSaveInstanceState(Bundle outState) {
409 super.onSaveInstanceState(outState);
410 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700411 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
412 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700413 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
414 }
415
416 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700417 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700418 if (fragment instanceof DialpadFragment) {
419 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700420 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700421 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700422 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700423 } else if (fragment instanceof SmartDialSearchFragment) {
424 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700425 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700426 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700427 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700428 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700429 } else if (fragment instanceof ListsFragment) {
430 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700431 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700432 }
Yorke Leec3766332013-07-31 11:13:16 -0700433 }
434
Alon Albertb453e5b2013-09-10 15:54:23 -0700435 protected void handleMenuSettings() {
436 openTelephonySetting(this);
437 }
438
439 public static void openTelephonySetting(Activity activity) {
440 final Intent settingsIntent = getCallSettingsIntent();
441 activity.startActivity(settingsIntent);
442 }
443
Chiao Cheng94b10b52012-08-17 16:59:12 -0700444 @Override
445 public void onClick(View view) {
446 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700447 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700448 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700449 mInCallDialpadUp = false;
450 showDialpadFragment(true);
451 } else {
452 // Dial button was pressed; tell the Dialpad fragment
453 mDialpadFragment.dialButtonPressed();
454 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700455 break;
Yorke Leec3766332013-07-31 11:13:16 -0700456 case R.id.search_close_button:
457 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700458 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700459 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700460 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700461 }
462 break;
Yorke Leec3766332013-07-31 11:13:16 -0700463 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800464 try {
465 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
466 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
467 } catch (ActivityNotFoundException e) {
468 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
469 Toast.LENGTH_SHORT).show();
470 }
Yorke Leec3766332013-07-31 11:13:16 -0700471 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700472 case R.id.dialtacts_options_menu_button:
473 buildOptionsMenu(view).show();
474 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700475 default: {
476 Log.wtf(TAG, "Unexpected onClick event from " + view);
477 break;
478 }
479 }
480 }
481
Yorke Leec3766332013-07-31 11:13:16 -0700482 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700483 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700484 switch (item.getItemId()) {
485 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700486 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700487 break;
488 case R.id.menu_add_contact:
489 try {
490 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
491 } catch (ActivityNotFoundException e) {
492 Toast toast = Toast.makeText(this,
493 R.string.add_contact_not_available,
494 Toast.LENGTH_SHORT);
495 toast.show();
496 }
497 break;
498 case R.id.menu_import_export:
499 // We hard-code the "contactsAreAvailable" argument because doing it properly would
500 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
501 // now in Dialtacts for (potential) performance reasons. Compare with how it is
502 // done in {@link PeopleActivity}.
503 ImportExportDialogFragment.show(getFragmentManager(), true,
504 DialtactsActivity.class);
505 return true;
506 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700507 ClearFrequentsDialog.show(getFragmentManager());
508 return true;
509 case R.id.menu_call_settings:
510 handleMenuSettings();
511 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700512 }
513 return false;
514 }
515
516 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700517 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
518 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
519 if (resultCode == RESULT_OK) {
520 final ArrayList<String> matches = data.getStringArrayListExtra(
521 RecognizerIntent.EXTRA_RESULTS);
522 if (matches.size() > 0) {
523 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700524 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700525 } else {
526 Log.e(TAG, "Voice search - nothing heard");
527 }
528 } else {
529 Log.e(TAG, "Voice search failed");
530 }
531 }
532 super.onActivityResult(requestCode, resultCode, data);
533 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700534
Andrew Lee2a58ab82014-05-15 02:16:04 -0700535 /**
536 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
537 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
538 * @see #onDialpadShown
539 */
Yorke Leec3766332013-07-31 11:13:16 -0700540 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700541 if (mIsDialpadShown) {
542 return;
543 }
544 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700545 mDialpadFragment.setAnimate(animate);
546
Chiao Cheng94b10b52012-08-17 16:59:12 -0700547 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700548 ft.show(mDialpadFragment);
549 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700550
551 if (!isInSearchUi()) {
552 enterSearchUi(true /* isSmartDial */, mSearchQuery);
553 }
Yorke Leec3766332013-07-31 11:13:16 -0700554 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700555
Andrew Lee2a58ab82014-05-15 02:16:04 -0700556 /**
557 * Callback from child DialpadFragment when the dialpad is shown.
558 */
559 public void onDialpadShown() {
560 updateFloatingActionButton();
561 if (mDialpadFragment.getAnimate()) {
562 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
563 mDialpadFragment.getView().startAnimation(slideIn);
564 } else {
565 mDialpadFragment.setYFraction(0);
566 }
567
Andrew Lee2a58ab82014-05-15 02:16:04 -0700568 updateSearchFragmentPosition();
569 getActionBar().hide();
570 }
571
572 /**
573 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
574 * a callback after the hide animation ends.
575 * @see #commitDialpadFragmentHide
576 */
Yorke Leeca195042013-09-25 16:29:38 -0700577 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700578 if (mDialpadFragment == null) {
579 return;
580 }
Yorke Leef6673d32013-08-23 15:34:17 -0700581 if (clearDialpad) {
582 mDialpadFragment.clearDialpad();
583 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700584 if (!mIsDialpadShown) {
585 return;
Yorke Leec3766332013-07-31 11:13:16 -0700586 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700587 mIsDialpadShown = false;
588 mDialpadFragment.setAnimate(animate);
589
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700590 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700591 updateFloatingActionButton();
592 if (animate) {
593 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
594 slideOut.setAnimationListener(new ActivityAnimationListener() {
595 @Override
596 public void onAnimationEnd(Animation animation) {
597 commitDialpadFragmentHide();
598 }
599 });
600 mDialpadFragment.getView().startAnimation(slideOut);
601 } else {
602 commitDialpadFragmentHide();
603 }
604
Andrew Leeb1903842014-05-15 16:11:24 -0700605 mListsFragment.maybeShowActionBar();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700606 }
607
608 /**
609 * Finishes hiding the dialpad fragment after any animations are completed.
610 */
611 private void commitDialpadFragmentHide() {
612 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700613 ft.hide(mDialpadFragment);
614 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700615 }
616
Andrew Lee2a58ab82014-05-15 02:16:04 -0700617 private void updateSearchFragmentPosition() {
618 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
619 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700620 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700621 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700622 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700623 fragment = mRegularSearchFragment;
624 }
625 if (fragment != null && fragment.isVisible()) {
626 fragment.getView().animate().translationY(translationValue)
627 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
628 }
629 }
630
Andrew Leeb1903842014-05-15 16:11:24 -0700631 private boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700632 return mInDialpadSearch || mInRegularSearch;
633 }
634
635 private void setNotInSearchUi() {
636 mInDialpadSearch = false;
637 mInRegularSearch = false;
638 }
639
Yorke Lee311969c2013-08-26 06:31:11 -0700640 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700641 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700642 hideDialpadFragment(false, true);
643 }
644
Yorke Leee00c9fe2014-04-12 12:42:06 -0700645 private void hideInputMethod(View view) {
646 final InputMethodManager imm = (InputMethodManager) getSystemService(
647 Context.INPUT_METHOD_SERVICE);
648 if (imm != null && view != null) {
649 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
650 }
651 }
652
Yorke Lee53a22302014-04-29 18:13:46 -0700653 private void prepareVoiceSearchButton() {
654 mVoiceSearchButton = getActionBar().getCustomView().findViewById(R.id.voice_search_button);
655 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
656 if (canIntentBeHandled(voiceIntent)) {
657 mVoiceSearchButton.setVisibility(View.VISIBLE);
658 mVoiceSearchButton.setOnClickListener(this);
659 } else {
660 mVoiceSearchButton.setVisibility(View.GONE);
661 }
662 }
663
Andrew Leee74a10e2014-05-16 14:31:40 -0700664 private OptionsPopupMenu buildOptionsMenu(View invoker) {
665 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
666 popupMenu.inflate(R.menu.dialtacts_options);
667 popupMenu.setOnMenuItemClickListener(this);
668 return popupMenu;
669 }
670
Yorke Lee86e21f72014-04-02 16:49:38 -0700671 @Override
672 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700673 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700674 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700675 mPendingSearchViewQuery = null;
676 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700677 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700678 }
679
Chiao Cheng94b10b52012-08-17 16:59:12 -0700680 /**
681 * Returns true if the intent is due to hitting the green send key (hardware call button:
682 * KEYCODE_CALL) while in a call.
683 *
684 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700685 * @return true if the intent is due to hitting the green send key while in a call
686 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700687 private boolean isSendKeyWhileInCall(Intent intent) {
688 // If there is a call in progress and the user launched the dialer by hitting the call
689 // button, go straight to the in-call screen.
690 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700691
Yorke Lee62e995c2014-03-28 10:02:56 -0700692 try {
693 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
694 if (callKey && phone != null && phone.showCallScreen()) {
695 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700696 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700697 } catch (RemoteException e) {
698 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700699 }
700
701 return false;
702 }
703
704 /**
705 * Sets the current tab based on the intent's request type
706 *
707 * @param intent Intent that contains information about which tab should be selected
708 */
Yorke Leec3766332013-07-31 11:13:16 -0700709 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700710 // 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 -0700711 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700712 finish();
713 return;
714 }
715
Yorke Lee669b6082013-09-17 15:43:38 -0700716 if (mDialpadFragment != null) {
717 final boolean phoneIsInUse = phoneIsInUse();
718 if (phoneIsInUse || isDialIntent(intent)) {
719 mDialpadFragment.setStartedFromNewIntent(true);
720 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
721 mInCallDialpadUp = true;
722 }
723 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700724 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700725 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700726 }
727
728 @Override
729 public void onNewIntent(Intent newIntent) {
730 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700731 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700732
Chiao Cheng94b10b52012-08-17 16:59:12 -0700733 invalidateOptionsMenu();
734 }
735
736 /** Returns true if the given intent contains a phone number to populate the dialer with */
737 private boolean isDialIntent(Intent intent) {
738 final String action = intent.getAction();
739 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
740 return true;
741 }
742 if (Intent.ACTION_VIEW.equals(action)) {
743 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700744 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700745 return true;
746 }
747 }
748 return false;
749 }
750
751 /**
752 * Returns an appropriate call origin for this Activity. May return null when no call origin
753 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
754 * for remembering the tab in which the user made a phone call, so the external app's DIAL
755 * request should not be counted.)
756 */
757 public String getCallOrigin() {
758 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
759 }
760
Chiao Cheng94b10b52012-08-17 16:59:12 -0700761 /**
Yorke Leec3766332013-07-31 11:13:16 -0700762 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700763 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700764 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Leeb1903842014-05-15 16:11:24 -0700765 if (getFragmentManager().isDestroyed() || mIsExitingSearch) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700766 // Weird race condition where fragment is doing work after the activity is destroyed
767 // due to talkback being on (b/10209937). Just return since we can't do any
768 // constructive here.
769 return;
770 }
771
Yorke Leeef2b7382013-08-09 17:39:25 -0700772 if (DEBUG) {
773 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
774 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700775
Yorke Leec3766332013-07-31 11:13:16 -0700776 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700777 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700778 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700779 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700780 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700781 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700782
Yorke Leeb207f8a2013-08-29 18:51:06 -0700783 final String tag;
784 if (smartDialSearch) {
785 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
786 } else {
787 tag = TAG_REGULAR_SEARCH_FRAGMENT;
788 }
789 mInDialpadSearch = smartDialSearch;
790 mInRegularSearch = !smartDialSearch;
791
Andrew Lee752956e2014-05-15 11:43:38 -0700792 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700793 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700794 if (fragment == null) {
795 if (smartDialSearch) {
796 fragment = new SmartDialSearchFragment();
797 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700798 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700799 }
Andrew Leeb1903842014-05-15 16:11:24 -0700800 transaction.add(R.id.dialtacts_frame, fragment, tag);
801 } else {
802 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700803 }
Andrew Leeb1903842014-05-15 16:11:24 -0700804
Yorke Lee86e21f72014-04-02 16:49:38 -0700805 // DialtactsActivity will provide the options menu
806 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700807 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700808 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700809 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700810
811 mListsFragment.getView().animate().alpha(0).withLayer();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700812 }
813
814 /**
Yorke Leec3766332013-07-31 11:13:16 -0700815 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700816 */
Yorke Leec3766332013-07-31 11:13:16 -0700817 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700818 // See related bug in enterSearchUI();
819 if (getFragmentManager().isDestroyed()) {
820 return;
821 }
Andrew Leeb1903842014-05-15 16:11:24 -0700822
823 mIsExitingSearch = true;
824
825 mSearchView.setText(null);
826 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700827 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700828
Andrew Leeb1903842014-05-15 16:11:24 -0700829 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
830 transaction.setCustomAnimations(0, android.R.animator.fade_out);
831
832 if (mSmartDialSearchFragment != null) {
833 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700834 }
Andrew Leeb1903842014-05-15 16:11:24 -0700835 if (mRegularSearchFragment != null) {
836 transaction.remove(mRegularSearchFragment);
837 }
838 transaction.commit();
839
840 mListsFragment.getView().animate().alpha(1).withLayer();
841
842 mIsExitingSearch = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700843 }
844
845 /** Returns an Intent to launch Call Settings screen */
846 public static Intent getCallSettingsIntent() {
847 final Intent intent = new Intent(Intent.ACTION_MAIN);
848 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
849 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
850 return intent;
851 }
852
853 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700854 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700855 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700856 if (TextUtils.isEmpty(mSearchQuery)) {
857 exitSearchUi();
858 }
Yorke Leef6673d32013-08-23 15:34:17 -0700859 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700860 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700861 exitSearchUi();
Yorke Leec3766332013-07-31 11:13:16 -0700862 } else {
863 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700864 }
Yorke Leec3766332013-07-31 11:13:16 -0700865 }
866
867 @Override
868 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700869 if (mSmartDialSearchFragment != null) {
870 mSmartDialSearchFragment.setAddToContactNumber(query);
871 }
Yorke Leec3766332013-07-31 11:13:16 -0700872 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
873 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700874
875 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700876 if (DEBUG) {
877 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
878 }
879 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
880 // This callback can happen if the dialpad fragment is recreated because of
881 // activity destruction. In that case, don't update the search view because
882 // that would bring the user back to the search fragment regardless of the
883 // previous state of the application. Instead, just return here and let the
884 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700885 if (!TextUtils.isEmpty(normalizedQuery)) {
886 mPendingSearchViewQuery = normalizedQuery;
887 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700888 return;
889 }
Yorke Lee53a22302014-04-29 18:13:46 -0700890 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700891 }
892 }
Yorke Leec3766332013-07-31 11:13:16 -0700893
894 @Override
895 public void onListFragmentScrollStateChange(int scrollState) {
896 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700897 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700898 hideInputMethod(getCurrentFocus());
899 }
900 }
901
902 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700903 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700904 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700905 // TODO: No-op for now. This should eventually show/hide the actionBar based on
906 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700907 }
908
Yorke Lee86e21f72014-04-02 16:49:38 -0700909 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700910 public void setFloatingActionButtonVisible(boolean visible) {
911 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700912 }
913
914 private boolean phoneIsInUse() {
915 final TelephonyManager tm = (TelephonyManager) getSystemService(
916 Context.TELEPHONY_SERVICE);
917 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
918 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700919
Yorke Leee1424812013-09-04 18:24:48 -0700920 public static Intent getAddNumberToContactIntent(CharSequence text) {
921 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
922 intent.putExtra(Intents.Insert.PHONE, text);
923 intent.setType(Contacts.CONTENT_ITEM_TYPE);
924 return intent;
925 }
926
Yorke Leeda0f9042013-12-05 14:25:51 -0800927 private boolean canIntentBeHandled(Intent intent) {
928 final PackageManager packageManager = getPackageManager();
929 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
930 PackageManager.MATCH_DEFAULT_ONLY);
931 return resolveInfo != null && resolveInfo.size() > 0;
932 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800933
Yorke Lee6a1461a2014-04-21 16:27:14 -0700934 @Override
935 public void showCallHistory() {
936 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
937 // CONTENT_TYPE, so that we always open our call log from our dialer
938 final Intent intent = new Intent(this, CallLogActivity.class);
939 startActivity(intent);
940 }
941
Yorke Lee86e21f72014-04-02 16:49:38 -0700942 /**
943 * Called when the user has long-pressed a contact tile to start a drag operation.
944 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800945 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700946 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700947 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700948 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800949 }
950
951 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700952 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
953 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800954
Yorke Lee86e21f72014-04-02 16:49:38 -0700955 /**
956 * Called when the user has released a contact tile after long-pressing it.
957 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800958 @Override
959 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700960 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700961 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800962 }
963
964 @Override
965 public void onDroppedOnRemove() {}
966
967 /**
Yorke Leed999b712014-04-23 15:10:58 -0700968 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800969 * once it has been attached to the activity.
970 */
971 @Override
972 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -0700973 mDragDropController = dragController;
974 ((RemoveView) findViewById(R.id.remove_view))
975 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800976 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700977
978 @Override
979 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700980 // Specify call-origin so that users will see the previous tab instead of
981 // CallLog screen (search UI will be automatically exited).
982 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -0700983 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700984 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700985 }
986
987 @Override
988 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700989 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
990 startActivity(intent);
991 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700992 }
993
994 @Override
995 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700996 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -0700997 }
998
999 @Override
1000 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001001 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001002 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001003
1004 public int getActionBarHeight() {
1005 return mActionBarHeight;
1006 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001007
1008 @Override
1009 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1010
1011 }
1012
1013 @Override
1014 public void onPageSelected(int position) {
1015 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001016 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001017 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001018 alignFloatingActionButtonByTab(mCurrentTabPosition);
1019 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001020 }
1021
1022 @Override
1023 public void onPageScrollStateChanged(int state) {
1024 }
1025
Andrew Lee2a58ab82014-05-15 02:16:04 -07001026 private void updateFloatingActionButton() {
1027 if (mIsDialpadShown) {
1028 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1029 mFloatingActionButton.setContentDescription(
1030 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001031 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001032 } else {
1033 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1034 mFloatingActionButton.setContentDescription(
1035 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001036 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001037 }
1038 }
1039
Yorke Leecc4660d2014-04-24 11:22:57 -07001040 private void alignFloatingActionButtonByTab(int position) {
1041 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1042 alignFloatingActionButtonMiddle();
1043 } else {
1044 alignFloatingActionButtonRight();
1045 }
1046 }
1047
1048 private void alignFloatingActionButtonRight() {
1049 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001050 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001051 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1052 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001053 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001054 }
1055
1056 private void alignFloatingActionButtonMiddle() {
1057 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001058 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001059 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1060 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001061 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001062 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001063
1064 /**
1065 * Convenience class which implements AnimationListener interface as null-op methods.
1066 */
1067 private class ActivityAnimationListener implements AnimationListener {
1068 @Override
1069 public void onAnimationStart(Animation animation) {
1070 }
1071
1072 @Override
1073 public void onAnimationEnd(Animation animation) {
1074 }
1075
1076 @Override
1077 public void onAnimationRepeat(Animation animation) {
1078 }
1079 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001080}