blob: 2b66355cf9f67678597a055e5d0cee7b688bf317 [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;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700170
Yorke Leeef2b7382013-08-09 17:39:25 -0700171 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700172 * The position of the currently selected tab in the attached {@link ListsFragment}.
173 */
174 private int mCurrentTabPosition = 0;
175
176 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700177 * True if the dialpad is only temporarily showing due to being in call
178 */
179 private boolean mInCallDialpadUp;
180
181 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700182 * True when this activity has been launched for the first time.
183 */
Yorke Lee98702de2013-08-06 12:03:32 -0700184 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700185
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700186 /**
187 * Search query to be applied to the SearchView in the ActionBar once
188 * onCreateOptionsMenu has been called.
189 */
190 private String mPendingSearchViewQuery;
191
Yorke Lee53a22302014-04-29 18:13:46 -0700192 private EditText mSearchView;
Andrew Leed4cde832014-05-16 15:56:48 -0700193 private View mSearchIcon;
Yorke Lee53a22302014-04-29 18:13:46 -0700194 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
Andrew Lee90374a72014-05-16 15:01:09 -0700264 // Show search fragment only when the query string is changed to non-empty text.
265 if (!TextUtils.isEmpty(newText)) {
266 // Call enterSearchUi only if we are switching search modes, or showing a search
267 // fragment for the first time.
268 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
269 (!mIsDialpadShown && mInRegularSearch);
270 if (!sameSearchMode) {
271 enterSearchUi(mIsDialpadShown, mSearchQuery);
272 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700273 }
274
275 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700276 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700277 } else if (mRegularSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700278 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700279 }
280
281 if (TextUtils.isEmpty(newText)) {
282 mSearchViewCloseButton.setVisibility(View.GONE);
283 mVoiceSearchButton.setVisibility(View.VISIBLE);
284 } else {
285 mSearchViewCloseButton.setVisibility(View.VISIBLE);
286 mVoiceSearchButton.setVisibility(View.GONE);
287 }
288 }
289
290 @Override
291 public void afterTextChanged(Editable s) {
292 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700293 };
294
Andrew Leeb1903842014-05-15 16:11:24 -0700295
296 /**
297 * Open the search UI when the user touches the search text view.
298 */
299 private final View.OnTouchListener mSearchViewOnTouchListener = new View.OnTouchListener() {
300 @Override
301 public boolean onTouch(View v, MotionEvent event) {
302 if (!isInSearchUi()) {
303 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
304 }
305 return false;
306 }
307 };
308
309 /**
310 * If the search term is empty and the user closes the soft keyboard, close the search UI.
311 */
312 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
313 @Override
314 public boolean onKey(View v, int keyCode, KeyEvent event) {
315 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
316 TextUtils.isEmpty(mSearchView.getText().toString())) {
317 onBackPressed();
318 }
319 return false;
320 }
321 };
322
Chiao Cheng94b10b52012-08-17 16:59:12 -0700323 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700324 protected void onCreate(Bundle savedInstanceState) {
325 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700326 mFirstLaunch = true;
327
Yorke Lee8898cd02013-08-08 10:24:27 -0700328 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800329 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700330
Yorke Lee53a22302014-04-29 18:13:46 -0700331 final ActionBar actionBar = getActionBar();
332 actionBar.setCustomView(R.layout.search_edittext);
333 actionBar.setDisplayShowCustomEnabled(true);
334
Andrew Leeb1903842014-05-15 16:11:24 -0700335 SearchEditTextLayout actionBarView = (SearchEditTextLayout) actionBar.getCustomView();
336 actionBarView.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700337
Andrew Leed4cde832014-05-16 15:56:48 -0700338 mSearchIcon = actionBarView.findViewById(R.id.search_magnifying_glass);
339
Andrew Leeb1903842014-05-15 16:11:24 -0700340 mSearchView = (EditText) actionBarView.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700341 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Leeb1903842014-05-15 16:11:24 -0700342 mSearchView.setOnTouchListener(mSearchViewOnTouchListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700343
Andrew Leeb1903842014-05-15 16:11:24 -0700344 mSearchViewCloseButton = actionBarView.findViewById(R.id.search_close_button);
Yorke Lee53a22302014-04-29 18:13:46 -0700345 mSearchViewCloseButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700346
Andrew Leee74a10e2014-05-16 14:31:40 -0700347 ImageButton optionsMenuButton =
348 (ImageButton) actionBarView.findViewById(R.id.dialtacts_options_menu_button);
349 optionsMenuButton.setOnClickListener(this);
350 final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
351 optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
352
Yorke Lee33932ff2014-04-16 13:34:32 -0700353 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
354 new int[] { android.R.attr.actionBarSize });
355 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
356 styledAttributes.recycle();
357
Yorke Leeef2b7382013-08-09 17:39:25 -0700358 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
359 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700360 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800361 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700362 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800363 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
364 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700365 } else {
366 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700367 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
368 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700369 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700370 }
371
Andrew Lee0c667702014-05-12 14:31:45 -0700372 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
373 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
374 parentLayout.setOnDragListener(new LayoutOnDragListener());
375
Andrew Leea73e1892014-05-13 13:21:16 -0700376 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
377 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700378
Andrew Leea73e1892014-05-13 13:21:16 -0700379 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
380 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800381
Yorke Lee28266192014-05-01 10:05:52 -0700382 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800383
Yorke Lee0baa98b2013-08-22 10:55:16 -0700384 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700385 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700386 }
387
388 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700389 protected void onResume() {
390 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700391 if (mFirstLaunch) {
392 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700393 } else if (!phoneIsInUse() && mInCallDialpadUp) {
394 hideDialpadFragment(false, true);
395 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700396 }
397 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700398 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700399 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700400 }
401
402 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700403 protected void onPause() {
404 if (mClearSearchOnPause) {
405 hideDialpadAndSearchUi();
406 mClearSearchOnPause = false;
407 }
408 super.onPause();
409 }
410
411 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700412 protected void onSaveInstanceState(Bundle outState) {
413 super.onSaveInstanceState(outState);
414 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700415 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
416 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700417 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
418 }
419
420 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700421 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700422 if (fragment instanceof DialpadFragment) {
423 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700424 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700425 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700426 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700427 } else if (fragment instanceof SmartDialSearchFragment) {
428 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700429 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700430 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700431 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700432 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700433 } else if (fragment instanceof ListsFragment) {
434 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700435 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700436 }
Yorke Leec3766332013-07-31 11:13:16 -0700437 }
438
Alon Albertb453e5b2013-09-10 15:54:23 -0700439 protected void handleMenuSettings() {
440 openTelephonySetting(this);
441 }
442
443 public static void openTelephonySetting(Activity activity) {
444 final Intent settingsIntent = getCallSettingsIntent();
445 activity.startActivity(settingsIntent);
446 }
447
Chiao Cheng94b10b52012-08-17 16:59:12 -0700448 @Override
449 public void onClick(View view) {
450 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700451 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700452 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700453 mInCallDialpadUp = false;
454 showDialpadFragment(true);
455 } else {
456 // Dial button was pressed; tell the Dialpad fragment
457 mDialpadFragment.dialButtonPressed();
458 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700459 break;
Yorke Leec3766332013-07-31 11:13:16 -0700460 case R.id.search_close_button:
461 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700462 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700463 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700464 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700465 }
466 break;
Yorke Leec3766332013-07-31 11:13:16 -0700467 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800468 try {
469 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
470 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
471 } catch (ActivityNotFoundException e) {
472 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
473 Toast.LENGTH_SHORT).show();
474 }
Yorke Leec3766332013-07-31 11:13:16 -0700475 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700476 case R.id.dialtacts_options_menu_button:
477 buildOptionsMenu(view).show();
478 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700479 default: {
480 Log.wtf(TAG, "Unexpected onClick event from " + view);
481 break;
482 }
483 }
484 }
485
Yorke Leec3766332013-07-31 11:13:16 -0700486 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700487 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700488 switch (item.getItemId()) {
489 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700490 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700491 break;
492 case R.id.menu_add_contact:
493 try {
494 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
495 } catch (ActivityNotFoundException e) {
496 Toast toast = Toast.makeText(this,
497 R.string.add_contact_not_available,
498 Toast.LENGTH_SHORT);
499 toast.show();
500 }
501 break;
502 case R.id.menu_import_export:
503 // We hard-code the "contactsAreAvailable" argument because doing it properly would
504 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
505 // now in Dialtacts for (potential) performance reasons. Compare with how it is
506 // done in {@link PeopleActivity}.
507 ImportExportDialogFragment.show(getFragmentManager(), true,
508 DialtactsActivity.class);
509 return true;
510 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700511 ClearFrequentsDialog.show(getFragmentManager());
512 return true;
513 case R.id.menu_call_settings:
514 handleMenuSettings();
515 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700516 }
517 return false;
518 }
519
520 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700521 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
522 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
523 if (resultCode == RESULT_OK) {
524 final ArrayList<String> matches = data.getStringArrayListExtra(
525 RecognizerIntent.EXTRA_RESULTS);
526 if (matches.size() > 0) {
527 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700528 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700529 } else {
530 Log.e(TAG, "Voice search - nothing heard");
531 }
532 } else {
533 Log.e(TAG, "Voice search failed");
534 }
535 }
536 super.onActivityResult(requestCode, resultCode, data);
537 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700538
Andrew Lee2a58ab82014-05-15 02:16:04 -0700539 /**
540 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
541 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
542 * @see #onDialpadShown
543 */
Yorke Leec3766332013-07-31 11:13:16 -0700544 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700545 if (mIsDialpadShown) {
546 return;
547 }
548 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700549 mDialpadFragment.setAnimate(animate);
550
Chiao Cheng94b10b52012-08-17 16:59:12 -0700551 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700552 ft.show(mDialpadFragment);
553 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700554
555 if (!isInSearchUi()) {
556 enterSearchUi(true /* isSmartDial */, mSearchQuery);
557 }
Yorke Leec3766332013-07-31 11:13:16 -0700558 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700559
Andrew Lee2a58ab82014-05-15 02:16:04 -0700560 /**
561 * Callback from child DialpadFragment when the dialpad is shown.
562 */
563 public void onDialpadShown() {
564 updateFloatingActionButton();
565 if (mDialpadFragment.getAnimate()) {
566 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
567 mDialpadFragment.getView().startAnimation(slideIn);
568 } else {
569 mDialpadFragment.setYFraction(0);
570 }
571
Andrew Lee2a58ab82014-05-15 02:16:04 -0700572 updateSearchFragmentPosition();
573 getActionBar().hide();
574 }
575
576 /**
577 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
578 * a callback after the hide animation ends.
579 * @see #commitDialpadFragmentHide
580 */
Yorke Leeca195042013-09-25 16:29:38 -0700581 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700582 if (mDialpadFragment == null) {
583 return;
584 }
Yorke Leef6673d32013-08-23 15:34:17 -0700585 if (clearDialpad) {
586 mDialpadFragment.clearDialpad();
587 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700588 if (!mIsDialpadShown) {
589 return;
Yorke Leec3766332013-07-31 11:13:16 -0700590 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700591 mIsDialpadShown = false;
592 mDialpadFragment.setAnimate(animate);
593
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700594 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700595 updateFloatingActionButton();
596 if (animate) {
597 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
598 slideOut.setAnimationListener(new ActivityAnimationListener() {
599 @Override
600 public void onAnimationEnd(Animation animation) {
601 commitDialpadFragmentHide();
602 }
603 });
604 mDialpadFragment.getView().startAnimation(slideOut);
605 } else {
606 commitDialpadFragmentHide();
607 }
608
Andrew Leeb1903842014-05-15 16:11:24 -0700609 mListsFragment.maybeShowActionBar();
Andrew Leed4cde832014-05-16 15:56:48 -0700610 if (isInSearchUi()) {
611 mSearchIcon.setVisibility(View.GONE);
612 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700613 }
614
615 /**
616 * Finishes hiding the dialpad fragment after any animations are completed.
617 */
618 private void commitDialpadFragmentHide() {
619 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700620 ft.hide(mDialpadFragment);
621 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700622 }
623
Andrew Lee2a58ab82014-05-15 02:16:04 -0700624 private void updateSearchFragmentPosition() {
625 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
626 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700627 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700628 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700629 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700630 fragment = mRegularSearchFragment;
631 }
632 if (fragment != null && fragment.isVisible()) {
633 fragment.getView().animate().translationY(translationValue)
634 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
635 }
636 }
637
Andrew Leeb1903842014-05-15 16:11:24 -0700638 private boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700639 return mInDialpadSearch || mInRegularSearch;
640 }
641
642 private void setNotInSearchUi() {
643 mInDialpadSearch = false;
644 mInRegularSearch = false;
645 }
646
Yorke Lee311969c2013-08-26 06:31:11 -0700647 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700648 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700649 hideDialpadFragment(false, true);
650 }
651
Yorke Leee00c9fe2014-04-12 12:42:06 -0700652 private void hideInputMethod(View view) {
653 final InputMethodManager imm = (InputMethodManager) getSystemService(
654 Context.INPUT_METHOD_SERVICE);
655 if (imm != null && view != null) {
656 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
657 }
658 }
659
Yorke Lee53a22302014-04-29 18:13:46 -0700660 private void prepareVoiceSearchButton() {
661 mVoiceSearchButton = getActionBar().getCustomView().findViewById(R.id.voice_search_button);
662 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
663 if (canIntentBeHandled(voiceIntent)) {
664 mVoiceSearchButton.setVisibility(View.VISIBLE);
665 mVoiceSearchButton.setOnClickListener(this);
666 } else {
667 mVoiceSearchButton.setVisibility(View.GONE);
668 }
669 }
670
Andrew Leee74a10e2014-05-16 14:31:40 -0700671 private OptionsPopupMenu buildOptionsMenu(View invoker) {
672 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
673 popupMenu.inflate(R.menu.dialtacts_options);
674 popupMenu.setOnMenuItemClickListener(this);
675 return popupMenu;
676 }
677
Yorke Lee86e21f72014-04-02 16:49:38 -0700678 @Override
679 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700680 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700681 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700682 mPendingSearchViewQuery = null;
683 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700684 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700685 }
686
Chiao Cheng94b10b52012-08-17 16:59:12 -0700687 /**
688 * Returns true if the intent is due to hitting the green send key (hardware call button:
689 * KEYCODE_CALL) while in a call.
690 *
691 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700692 * @return true if the intent is due to hitting the green send key while in a call
693 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700694 private boolean isSendKeyWhileInCall(Intent intent) {
695 // If there is a call in progress and the user launched the dialer by hitting the call
696 // button, go straight to the in-call screen.
697 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700698
Yorke Lee62e995c2014-03-28 10:02:56 -0700699 try {
700 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
701 if (callKey && phone != null && phone.showCallScreen()) {
702 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700703 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700704 } catch (RemoteException e) {
705 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700706 }
707
708 return false;
709 }
710
711 /**
712 * Sets the current tab based on the intent's request type
713 *
714 * @param intent Intent that contains information about which tab should be selected
715 */
Yorke Leec3766332013-07-31 11:13:16 -0700716 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700717 // 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 -0700718 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719 finish();
720 return;
721 }
722
Yorke Lee669b6082013-09-17 15:43:38 -0700723 if (mDialpadFragment != null) {
724 final boolean phoneIsInUse = phoneIsInUse();
725 if (phoneIsInUse || isDialIntent(intent)) {
726 mDialpadFragment.setStartedFromNewIntent(true);
727 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
728 mInCallDialpadUp = true;
729 }
730 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700731 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700732 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700733 }
734
735 @Override
736 public void onNewIntent(Intent newIntent) {
737 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700738 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700739
Chiao Cheng94b10b52012-08-17 16:59:12 -0700740 invalidateOptionsMenu();
741 }
742
743 /** Returns true if the given intent contains a phone number to populate the dialer with */
744 private boolean isDialIntent(Intent intent) {
745 final String action = intent.getAction();
746 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
747 return true;
748 }
749 if (Intent.ACTION_VIEW.equals(action)) {
750 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700751 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700752 return true;
753 }
754 }
755 return false;
756 }
757
758 /**
759 * Returns an appropriate call origin for this Activity. May return null when no call origin
760 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
761 * for remembering the tab in which the user made a phone call, so the external app's DIAL
762 * request should not be counted.)
763 */
764 public String getCallOrigin() {
765 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
766 }
767
Chiao Cheng94b10b52012-08-17 16:59:12 -0700768 /**
Yorke Leec3766332013-07-31 11:13:16 -0700769 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700770 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700771 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700772 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700773 // Weird race condition where fragment is doing work after the activity is destroyed
774 // due to talkback being on (b/10209937). Just return since we can't do any
775 // constructive here.
776 return;
777 }
778
Yorke Leeef2b7382013-08-09 17:39:25 -0700779 if (DEBUG) {
780 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
781 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700782
Yorke Leec3766332013-07-31 11:13:16 -0700783 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700784 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700785 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700786 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700787 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700788 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700789
Yorke Leeb207f8a2013-08-29 18:51:06 -0700790 final String tag;
791 if (smartDialSearch) {
792 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
793 } else {
794 tag = TAG_REGULAR_SEARCH_FRAGMENT;
795 }
796 mInDialpadSearch = smartDialSearch;
797 mInRegularSearch = !smartDialSearch;
798
Andrew Lee752956e2014-05-15 11:43:38 -0700799 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700800 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700801 if (fragment == null) {
802 if (smartDialSearch) {
803 fragment = new SmartDialSearchFragment();
804 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700805 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700806 }
Andrew Leeb1903842014-05-15 16:11:24 -0700807 transaction.add(R.id.dialtacts_frame, fragment, tag);
808 } else {
809 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700810 }
Andrew Leeb1903842014-05-15 16:11:24 -0700811
Yorke Lee86e21f72014-04-02 16:49:38 -0700812 // DialtactsActivity will provide the options menu
813 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700814 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700815 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700816 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700817
818 mListsFragment.getView().animate().alpha(0).withLayer();
Andrew Leed4cde832014-05-16 15:56:48 -0700819
820 if (!mIsDialpadShown) {
821 mSearchIcon.setVisibility(View.GONE);
822 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700823 }
824
825 /**
Yorke Leec3766332013-07-31 11:13:16 -0700826 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700827 */
Yorke Leec3766332013-07-31 11:13:16 -0700828 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700829 // See related bug in enterSearchUI();
830 if (getFragmentManager().isDestroyed()) {
831 return;
832 }
Andrew Leeb1903842014-05-15 16:11:24 -0700833
Andrew Leeb1903842014-05-15 16:11:24 -0700834 mSearchView.setText(null);
835 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700836 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700837
Andrew Leeb1903842014-05-15 16:11:24 -0700838 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
839 transaction.setCustomAnimations(0, android.R.animator.fade_out);
840
841 if (mSmartDialSearchFragment != null) {
842 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700843 }
Andrew Leeb1903842014-05-15 16:11:24 -0700844 if (mRegularSearchFragment != null) {
845 transaction.remove(mRegularSearchFragment);
846 }
847 transaction.commit();
848
849 mListsFragment.getView().animate().alpha(1).withLayer();
Andrew Leed4cde832014-05-16 15:56:48 -0700850 mSearchIcon.setVisibility(View.VISIBLE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700851 }
852
853 /** Returns an Intent to launch Call Settings screen */
854 public static Intent getCallSettingsIntent() {
855 final Intent intent = new Intent(Intent.ACTION_MAIN);
856 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
857 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
858 return intent;
859 }
860
861 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700862 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700863 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700864 if (TextUtils.isEmpty(mSearchQuery)) {
865 exitSearchUi();
866 }
Yorke Leef6673d32013-08-23 15:34:17 -0700867 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700868 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700869 exitSearchUi();
Yorke Leec3766332013-07-31 11:13:16 -0700870 } else {
871 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700872 }
Yorke Leec3766332013-07-31 11:13:16 -0700873 }
874
875 @Override
876 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700877 if (mSmartDialSearchFragment != null) {
878 mSmartDialSearchFragment.setAddToContactNumber(query);
879 }
Yorke Leec3766332013-07-31 11:13:16 -0700880 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
881 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700882
883 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700884 if (DEBUG) {
885 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
886 }
887 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
888 // This callback can happen if the dialpad fragment is recreated because of
889 // activity destruction. In that case, don't update the search view because
890 // that would bring the user back to the search fragment regardless of the
891 // previous state of the application. Instead, just return here and let the
892 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700893 if (!TextUtils.isEmpty(normalizedQuery)) {
894 mPendingSearchViewQuery = normalizedQuery;
895 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700896 return;
897 }
Yorke Lee53a22302014-04-29 18:13:46 -0700898 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700899 }
900 }
Yorke Leec3766332013-07-31 11:13:16 -0700901
902 @Override
903 public void onListFragmentScrollStateChange(int scrollState) {
904 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700905 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700906 hideInputMethod(getCurrentFocus());
907 }
908 }
909
910 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700911 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700912 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700913 // TODO: No-op for now. This should eventually show/hide the actionBar based on
914 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700915 }
916
Yorke Lee86e21f72014-04-02 16:49:38 -0700917 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700918 public void setFloatingActionButtonVisible(boolean visible) {
919 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700920 }
921
922 private boolean phoneIsInUse() {
923 final TelephonyManager tm = (TelephonyManager) getSystemService(
924 Context.TELEPHONY_SERVICE);
925 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
926 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700927
Yorke Leee1424812013-09-04 18:24:48 -0700928 public static Intent getAddNumberToContactIntent(CharSequence text) {
929 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
930 intent.putExtra(Intents.Insert.PHONE, text);
931 intent.setType(Contacts.CONTENT_ITEM_TYPE);
932 return intent;
933 }
934
Yorke Leeda0f9042013-12-05 14:25:51 -0800935 private boolean canIntentBeHandled(Intent intent) {
936 final PackageManager packageManager = getPackageManager();
937 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
938 PackageManager.MATCH_DEFAULT_ONLY);
939 return resolveInfo != null && resolveInfo.size() > 0;
940 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800941
Yorke Lee6a1461a2014-04-21 16:27:14 -0700942 @Override
943 public void showCallHistory() {
944 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
945 // CONTENT_TYPE, so that we always open our call log from our dialer
946 final Intent intent = new Intent(this, CallLogActivity.class);
947 startActivity(intent);
948 }
949
Yorke Lee86e21f72014-04-02 16:49:38 -0700950 /**
951 * Called when the user has long-pressed a contact tile to start a drag operation.
952 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800953 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700954 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700955 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700956 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800957 }
958
959 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700960 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
961 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800962
Yorke Lee86e21f72014-04-02 16:49:38 -0700963 /**
964 * Called when the user has released a contact tile after long-pressing it.
965 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800966 @Override
967 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700968 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700969 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800970 }
971
972 @Override
973 public void onDroppedOnRemove() {}
974
975 /**
Yorke Leed999b712014-04-23 15:10:58 -0700976 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800977 * once it has been attached to the activity.
978 */
979 @Override
980 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -0700981 mDragDropController = dragController;
982 ((RemoveView) findViewById(R.id.remove_view))
983 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800984 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700985
986 @Override
987 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700988 // Specify call-origin so that users will see the previous tab instead of
989 // CallLog screen (search UI will be automatically exited).
990 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -0700991 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700992 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700993 }
994
995 @Override
996 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700997 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
998 startActivity(intent);
999 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001000 }
1001
1002 @Override
1003 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001004 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001005 }
1006
1007 @Override
1008 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001009 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001010 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001011
1012 public int getActionBarHeight() {
1013 return mActionBarHeight;
1014 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001015
1016 @Override
1017 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1018
1019 }
1020
1021 @Override
1022 public void onPageSelected(int position) {
1023 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001024 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001025 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001026 alignFloatingActionButtonByTab(mCurrentTabPosition);
1027 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001028 }
1029
1030 @Override
1031 public void onPageScrollStateChanged(int state) {
1032 }
1033
Andrew Lee2a58ab82014-05-15 02:16:04 -07001034 private void updateFloatingActionButton() {
1035 if (mIsDialpadShown) {
1036 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1037 mFloatingActionButton.setContentDescription(
1038 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001039 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001040 } else {
1041 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1042 mFloatingActionButton.setContentDescription(
1043 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001044 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001045 }
1046 }
1047
Yorke Leecc4660d2014-04-24 11:22:57 -07001048 private void alignFloatingActionButtonByTab(int position) {
1049 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1050 alignFloatingActionButtonMiddle();
1051 } else {
1052 alignFloatingActionButtonRight();
1053 }
1054 }
1055
1056 private void alignFloatingActionButtonRight() {
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.CENTER_HORIZONTAL);
1060 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001061 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001062 }
1063
1064 private void alignFloatingActionButtonMiddle() {
1065 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001066 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001067 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1068 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001069 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001070 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001071
1072 /**
1073 * Convenience class which implements AnimationListener interface as null-op methods.
1074 */
1075 private class ActivityAnimationListener implements AnimationListener {
1076 @Override
1077 public void onAnimationStart(Animation animation) {
1078 }
1079
1080 @Override
1081 public void onAnimationEnd(Animation animation) {
1082 }
1083
1084 @Override
1085 public void onAnimationRepeat(Animation animation) {
1086 }
1087 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001088}