blob: 4149f398634359b6750de8de181f0094b9f4c876 [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;
Yorke Leeec489fb2014-05-19 15:39:27 -0700196 private SearchEditTextLayout mSearchEditTextLayout;
Andrew Leee74a10e2014-05-16 14:31:40 -0700197
Yorke Lee86e21f72014-04-02 16:49:38 -0700198 /**
199 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
200 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
201 * removed from the speed dial list.
202 */
Yorke Lee28266192014-05-01 10:05:52 -0700203 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700204
Yorke Lee86e21f72014-04-02 16:49:38 -0700205 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
206 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700207 private String mSearchQuery;
208
Yorke Leefce269a2013-08-12 11:56:04 -0700209 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700210 private DragDropController mDragDropController;
Yorke Leefce269a2013-08-12 11:56:04 -0700211
Andrew Leee74a10e2014-05-16 14:31:40 -0700212 private class OptionsPopupMenu extends PopupMenu {
213 public OptionsPopupMenu(Context context, View anchor) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700214 super(context, anchor);
215 }
216
217 @Override
218 public void show() {
219 final Menu menu = getMenu();
220 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700221 clearFrequents.setVisible(mListsFragment != null &&
222 mListsFragment.getSpeedDialFragment() != null &&
223 mListsFragment.getSpeedDialFragment().hasFrequents());
Yorke Leee00c9fe2014-04-12 12:42:06 -0700224 super.show();
225 }
226 }
227
Chiao Cheng94b10b52012-08-17 16:59:12 -0700228 /**
Yorke Lee28266192014-05-01 10:05:52 -0700229 * Listener that listens to drag events and sends their x and y coordinates to a
230 * {@link DragDropController}.
231 */
232 private class LayoutOnDragListener implements OnDragListener {
233 @Override
234 public boolean onDrag(View v, DragEvent event) {
235 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
236 mDragDropController.handleDragHovered(v, (int) event.getX(),
237 (int) event.getY());
238 }
239 return true;
240 }
241 }
242
243 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700244 * Listener used to send search queries to the phone search fragment.
245 */
Yorke Lee53a22302014-04-29 18:13:46 -0700246 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700247 @Override
248 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
249 }
Yorke Leec3766332013-07-31 11:13:16 -0700250
Andrew Lee99a570c2014-05-15 14:18:50 -0700251 @Override
252 public void onTextChanged(CharSequence s, int start, int before, int count) {
253 final String newText = s.toString();
254 if (newText.equals(mSearchQuery)) {
255 // If the query hasn't changed (perhaps due to activity being destroyed
256 // and restored, or user launching the same DIAL intent twice), then there is
257 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700258 return;
259 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700260 mSearchQuery = newText;
261 if (DEBUG) {
262 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leec3766332013-07-31 11:13:16 -0700263 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700264
Andrew Lee90374a72014-05-16 15:01:09 -0700265 // Show search fragment only when the query string is changed to non-empty text.
266 if (!TextUtils.isEmpty(newText)) {
267 // Call enterSearchUi only if we are switching search modes, or showing a search
268 // fragment for the first time.
269 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
270 (!mIsDialpadShown && mInRegularSearch);
271 if (!sameSearchMode) {
272 enterSearchUi(mIsDialpadShown, mSearchQuery);
273 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700274 }
275
276 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700277 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700278 } else if (mRegularSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700279 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700280 }
281
282 if (TextUtils.isEmpty(newText)) {
283 mSearchViewCloseButton.setVisibility(View.GONE);
284 mVoiceSearchButton.setVisibility(View.VISIBLE);
285 } else {
286 mSearchViewCloseButton.setVisibility(View.VISIBLE);
287 mVoiceSearchButton.setVisibility(View.GONE);
288 }
289 }
290
291 @Override
292 public void afterTextChanged(Editable s) {
293 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700294 };
295
Andrew Leeb1903842014-05-15 16:11:24 -0700296
297 /**
298 * Open the search UI when the user touches the search text view.
299 */
300 private final View.OnTouchListener mSearchViewOnTouchListener = new View.OnTouchListener() {
301 @Override
302 public boolean onTouch(View v, MotionEvent event) {
303 if (!isInSearchUi()) {
304 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
305 }
306 return false;
307 }
308 };
309
310 /**
311 * If the search term is empty and the user closes the soft keyboard, close the search UI.
312 */
313 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
314 @Override
315 public boolean onKey(View v, int keyCode, KeyEvent event) {
316 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
317 TextUtils.isEmpty(mSearchView.getText().toString())) {
318 onBackPressed();
319 }
320 return false;
321 }
322 };
323
Chiao Cheng94b10b52012-08-17 16:59:12 -0700324 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700325 protected void onCreate(Bundle savedInstanceState) {
326 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700327 mFirstLaunch = true;
328
Yorke Lee8898cd02013-08-08 10:24:27 -0700329 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800330 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700331
Yorke Lee53a22302014-04-29 18:13:46 -0700332 final ActionBar actionBar = getActionBar();
333 actionBar.setCustomView(R.layout.search_edittext);
334 actionBar.setDisplayShowCustomEnabled(true);
335
Yorke Leeec489fb2014-05-19 15:39:27 -0700336 mSearchEditTextLayout = (SearchEditTextLayout) actionBar.getCustomView();
337 mSearchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700338
Yorke Leeec489fb2014-05-19 15:39:27 -0700339 mSearchIcon = mSearchEditTextLayout.findViewById(R.id.search_magnifying_glass);
340 mVoiceSearchButton = mSearchEditTextLayout.findViewById(R.id.voice_search_button);
Andrew Leed4cde832014-05-16 15:56:48 -0700341
Yorke Leeec489fb2014-05-19 15:39:27 -0700342 mSearchView = (EditText) mSearchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700343 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Leeb1903842014-05-15 16:11:24 -0700344 mSearchView.setOnTouchListener(mSearchViewOnTouchListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700345
Yorke Leeec489fb2014-05-19 15:39:27 -0700346 mSearchViewCloseButton = mSearchEditTextLayout.findViewById(R.id.search_close_button);
Yorke Lee53a22302014-04-29 18:13:46 -0700347 mSearchViewCloseButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700348
Yorke Leeec489fb2014-05-19 15:39:27 -0700349 ImageButton optionsMenuButton = (ImageButton) mSearchEditTextLayout.findViewById(
350 R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700351 optionsMenuButton.setOnClickListener(this);
352 final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
353 optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
354
Yorke Lee33932ff2014-04-16 13:34:32 -0700355 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
356 new int[] { android.R.attr.actionBarSize });
357 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
358 styledAttributes.recycle();
359
Yorke Leeef2b7382013-08-09 17:39:25 -0700360 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
361 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700362 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800363 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700364 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800365 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
366 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700367 } else {
368 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700369 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
370 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700371 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700372 }
373
Andrew Lee0c667702014-05-12 14:31:45 -0700374 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
375 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
376 parentLayout.setOnDragListener(new LayoutOnDragListener());
377
Andrew Leea73e1892014-05-13 13:21:16 -0700378 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
379 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700380
Andrew Leea73e1892014-05-13 13:21:16 -0700381 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
382 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800383
Yorke Lee28266192014-05-01 10:05:52 -0700384 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800385
Yorke Lee0baa98b2013-08-22 10:55:16 -0700386 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700387 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700388 }
389
390 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700391 protected void onResume() {
392 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700393 if (mFirstLaunch) {
394 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700395 } else if (!phoneIsInUse() && mInCallDialpadUp) {
396 hideDialpadFragment(false, true);
397 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700398 }
399 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700400 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700401 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700402 }
403
404 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700405 protected void onPause() {
406 if (mClearSearchOnPause) {
407 hideDialpadAndSearchUi();
408 mClearSearchOnPause = false;
409 }
410 super.onPause();
411 }
412
413 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700414 protected void onSaveInstanceState(Bundle outState) {
415 super.onSaveInstanceState(outState);
416 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700417 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
418 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700419 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
420 }
421
422 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700423 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700424 if (fragment instanceof DialpadFragment) {
425 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700426 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700427 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700428 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700429 } else if (fragment instanceof SmartDialSearchFragment) {
430 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700431 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700432 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700433 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700434 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700435 } else if (fragment instanceof ListsFragment) {
436 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700437 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700438 }
Yorke Leec3766332013-07-31 11:13:16 -0700439 }
440
Alon Albertb453e5b2013-09-10 15:54:23 -0700441 protected void handleMenuSettings() {
442 openTelephonySetting(this);
443 }
444
445 public static void openTelephonySetting(Activity activity) {
446 final Intent settingsIntent = getCallSettingsIntent();
447 activity.startActivity(settingsIntent);
448 }
449
Chiao Cheng94b10b52012-08-17 16:59:12 -0700450 @Override
451 public void onClick(View view) {
452 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700453 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700454 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700455 mInCallDialpadUp = false;
456 showDialpadFragment(true);
457 } else {
458 // Dial button was pressed; tell the Dialpad fragment
459 mDialpadFragment.dialButtonPressed();
460 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700461 break;
Yorke Leec3766332013-07-31 11:13:16 -0700462 case R.id.search_close_button:
463 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700464 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700465 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700466 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700467 }
468 break;
Yorke Leec3766332013-07-31 11:13:16 -0700469 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800470 try {
471 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
472 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
473 } catch (ActivityNotFoundException e) {
474 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
475 Toast.LENGTH_SHORT).show();
476 }
Yorke Leec3766332013-07-31 11:13:16 -0700477 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700478 case R.id.dialtacts_options_menu_button:
479 buildOptionsMenu(view).show();
480 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700481 default: {
482 Log.wtf(TAG, "Unexpected onClick event from " + view);
483 break;
484 }
485 }
486 }
487
Yorke Leec3766332013-07-31 11:13:16 -0700488 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700489 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700490 switch (item.getItemId()) {
491 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700492 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700493 break;
494 case R.id.menu_add_contact:
495 try {
496 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
497 } catch (ActivityNotFoundException e) {
498 Toast toast = Toast.makeText(this,
499 R.string.add_contact_not_available,
500 Toast.LENGTH_SHORT);
501 toast.show();
502 }
503 break;
504 case R.id.menu_import_export:
505 // We hard-code the "contactsAreAvailable" argument because doing it properly would
506 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
507 // now in Dialtacts for (potential) performance reasons. Compare with how it is
508 // done in {@link PeopleActivity}.
509 ImportExportDialogFragment.show(getFragmentManager(), true,
510 DialtactsActivity.class);
511 return true;
512 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700513 ClearFrequentsDialog.show(getFragmentManager());
514 return true;
515 case R.id.menu_call_settings:
516 handleMenuSettings();
517 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700518 }
519 return false;
520 }
521
522 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700523 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
524 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
525 if (resultCode == RESULT_OK) {
526 final ArrayList<String> matches = data.getStringArrayListExtra(
527 RecognizerIntent.EXTRA_RESULTS);
528 if (matches.size() > 0) {
529 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700530 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700531 } else {
532 Log.e(TAG, "Voice search - nothing heard");
533 }
534 } else {
535 Log.e(TAG, "Voice search failed");
536 }
537 }
538 super.onActivityResult(requestCode, resultCode, data);
539 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700540
Andrew Lee2a58ab82014-05-15 02:16:04 -0700541 /**
542 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
543 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
544 * @see #onDialpadShown
545 */
Yorke Leec3766332013-07-31 11:13:16 -0700546 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700547 if (mIsDialpadShown) {
548 return;
549 }
550 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700551 mDialpadFragment.setAnimate(animate);
552
Chiao Cheng94b10b52012-08-17 16:59:12 -0700553 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700554 ft.show(mDialpadFragment);
555 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700556
557 if (!isInSearchUi()) {
558 enterSearchUi(true /* isSmartDial */, mSearchQuery);
559 }
Yorke Leec3766332013-07-31 11:13:16 -0700560 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561
Andrew Lee2a58ab82014-05-15 02:16:04 -0700562 /**
563 * Callback from child DialpadFragment when the dialpad is shown.
564 */
565 public void onDialpadShown() {
566 updateFloatingActionButton();
567 if (mDialpadFragment.getAnimate()) {
568 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
569 mDialpadFragment.getView().startAnimation(slideIn);
570 } else {
571 mDialpadFragment.setYFraction(0);
572 }
573
Andrew Lee2a58ab82014-05-15 02:16:04 -0700574 updateSearchFragmentPosition();
575 getActionBar().hide();
576 }
577
578 /**
579 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
580 * a callback after the hide animation ends.
581 * @see #commitDialpadFragmentHide
582 */
Yorke Leeca195042013-09-25 16:29:38 -0700583 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700584 if (mDialpadFragment == null) {
585 return;
586 }
Yorke Leef6673d32013-08-23 15:34:17 -0700587 if (clearDialpad) {
588 mDialpadFragment.clearDialpad();
589 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700590 if (!mIsDialpadShown) {
591 return;
Yorke Leec3766332013-07-31 11:13:16 -0700592 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700593 mIsDialpadShown = false;
594 mDialpadFragment.setAnimate(animate);
595
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700596 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700597 updateFloatingActionButton();
598 if (animate) {
599 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
600 slideOut.setAnimationListener(new ActivityAnimationListener() {
601 @Override
602 public void onAnimationEnd(Animation animation) {
603 commitDialpadFragmentHide();
604 }
605 });
606 mDialpadFragment.getView().startAnimation(slideOut);
607 } else {
608 commitDialpadFragmentHide();
609 }
610
Andrew Leeb1903842014-05-15 16:11:24 -0700611 mListsFragment.maybeShowActionBar();
Andrew Leed4cde832014-05-16 15:56:48 -0700612 if (isInSearchUi()) {
613 mSearchIcon.setVisibility(View.GONE);
614 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700615 }
616
617 /**
618 * Finishes hiding the dialpad fragment after any animations are completed.
619 */
620 private void commitDialpadFragmentHide() {
621 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700622 ft.hide(mDialpadFragment);
623 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700624 }
625
Andrew Lee2a58ab82014-05-15 02:16:04 -0700626 private void updateSearchFragmentPosition() {
627 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
628 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700629 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700630 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700631 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700632 fragment = mRegularSearchFragment;
633 }
634 if (fragment != null && fragment.isVisible()) {
635 fragment.getView().animate().translationY(translationValue)
636 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
637 }
638 }
639
Andrew Leeb1903842014-05-15 16:11:24 -0700640 private boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700641 return mInDialpadSearch || mInRegularSearch;
642 }
643
644 private void setNotInSearchUi() {
645 mInDialpadSearch = false;
646 mInRegularSearch = false;
647 }
648
Yorke Lee311969c2013-08-26 06:31:11 -0700649 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700650 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700651 hideDialpadFragment(false, true);
652 }
653
Yorke Leee00c9fe2014-04-12 12:42:06 -0700654 private void hideInputMethod(View view) {
655 final InputMethodManager imm = (InputMethodManager) getSystemService(
656 Context.INPUT_METHOD_SERVICE);
657 if (imm != null && view != null) {
658 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
659 }
660 }
661
Yorke Lee53a22302014-04-29 18:13:46 -0700662 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700663 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
664 if (canIntentBeHandled(voiceIntent)) {
665 mVoiceSearchButton.setVisibility(View.VISIBLE);
666 mVoiceSearchButton.setOnClickListener(this);
667 } else {
668 mVoiceSearchButton.setVisibility(View.GONE);
669 }
670 }
671
Andrew Leee74a10e2014-05-16 14:31:40 -0700672 private OptionsPopupMenu buildOptionsMenu(View invoker) {
673 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
674 popupMenu.inflate(R.menu.dialtacts_options);
675 popupMenu.setOnMenuItemClickListener(this);
676 return popupMenu;
677 }
678
Yorke Lee86e21f72014-04-02 16:49:38 -0700679 @Override
680 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700681 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700682 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700683 mPendingSearchViewQuery = null;
684 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700685 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700686 }
687
Chiao Cheng94b10b52012-08-17 16:59:12 -0700688 /**
689 * Returns true if the intent is due to hitting the green send key (hardware call button:
690 * KEYCODE_CALL) while in a call.
691 *
692 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700693 * @return true if the intent is due to hitting the green send key while in a call
694 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700695 private boolean isSendKeyWhileInCall(Intent intent) {
696 // If there is a call in progress and the user launched the dialer by hitting the call
697 // button, go straight to the in-call screen.
698 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700699
Yorke Lee62e995c2014-03-28 10:02:56 -0700700 try {
701 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
702 if (callKey && phone != null && phone.showCallScreen()) {
703 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700704 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700705 } catch (RemoteException e) {
706 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700707 }
708
709 return false;
710 }
711
712 /**
713 * Sets the current tab based on the intent's request type
714 *
715 * @param intent Intent that contains information about which tab should be selected
716 */
Yorke Leec3766332013-07-31 11:13:16 -0700717 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700718 // 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 -0700719 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700720 finish();
721 return;
722 }
723
Yorke Lee669b6082013-09-17 15:43:38 -0700724 if (mDialpadFragment != null) {
725 final boolean phoneIsInUse = phoneIsInUse();
726 if (phoneIsInUse || isDialIntent(intent)) {
727 mDialpadFragment.setStartedFromNewIntent(true);
728 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
729 mInCallDialpadUp = true;
730 }
731 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700732 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700733 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700734 }
735
736 @Override
737 public void onNewIntent(Intent newIntent) {
738 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700739 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700740
Chiao Cheng94b10b52012-08-17 16:59:12 -0700741 invalidateOptionsMenu();
742 }
743
744 /** Returns true if the given intent contains a phone number to populate the dialer with */
745 private boolean isDialIntent(Intent intent) {
746 final String action = intent.getAction();
747 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
748 return true;
749 }
750 if (Intent.ACTION_VIEW.equals(action)) {
751 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700752 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700753 return true;
754 }
755 }
756 return false;
757 }
758
759 /**
760 * Returns an appropriate call origin for this Activity. May return null when no call origin
761 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
762 * for remembering the tab in which the user made a phone call, so the external app's DIAL
763 * request should not be counted.)
764 */
765 public String getCallOrigin() {
766 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
767 }
768
Chiao Cheng94b10b52012-08-17 16:59:12 -0700769 /**
Yorke Leec3766332013-07-31 11:13:16 -0700770 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700771 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700772 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700773 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700774 // Weird race condition where fragment is doing work after the activity is destroyed
775 // due to talkback being on (b/10209937). Just return since we can't do any
776 // constructive here.
777 return;
778 }
779
Yorke Leeef2b7382013-08-09 17:39:25 -0700780 if (DEBUG) {
781 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
782 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700783
Yorke Leec3766332013-07-31 11:13:16 -0700784 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700785 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700786 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700787 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700788 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700789 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700790
Yorke Leeb207f8a2013-08-29 18:51:06 -0700791 final String tag;
792 if (smartDialSearch) {
793 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
794 } else {
795 tag = TAG_REGULAR_SEARCH_FRAGMENT;
796 }
797 mInDialpadSearch = smartDialSearch;
798 mInRegularSearch = !smartDialSearch;
799
Andrew Lee752956e2014-05-15 11:43:38 -0700800 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700801 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700802 if (fragment == null) {
803 if (smartDialSearch) {
804 fragment = new SmartDialSearchFragment();
805 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700806 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700807 }
Andrew Leeb1903842014-05-15 16:11:24 -0700808 transaction.add(R.id.dialtacts_frame, fragment, tag);
809 } else {
810 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700811 }
Andrew Leeb1903842014-05-15 16:11:24 -0700812
Yorke Lee86e21f72014-04-02 16:49:38 -0700813 // DialtactsActivity will provide the options menu
814 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700815 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700816 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700817 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700818
819 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leeec489fb2014-05-19 15:39:27 -0700820 mSearchEditTextLayout.animateExpandOrCollapse(true);
Andrew Leed4cde832014-05-16 15:56:48 -0700821
822 if (!mIsDialpadShown) {
823 mSearchIcon.setVisibility(View.GONE);
824 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700825 }
826
827 /**
Yorke Leec3766332013-07-31 11:13:16 -0700828 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700829 */
Yorke Leec3766332013-07-31 11:13:16 -0700830 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700831 // See related bug in enterSearchUI();
832 if (getFragmentManager().isDestroyed()) {
833 return;
834 }
Andrew Leeb1903842014-05-15 16:11:24 -0700835
Andrew Leeb1903842014-05-15 16:11:24 -0700836 mSearchView.setText(null);
837 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700838 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700839
Andrew Leeb1903842014-05-15 16:11:24 -0700840 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
841 transaction.setCustomAnimations(0, android.R.animator.fade_out);
842
843 if (mSmartDialSearchFragment != null) {
844 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700845 }
Andrew Leeb1903842014-05-15 16:11:24 -0700846 if (mRegularSearchFragment != null) {
847 transaction.remove(mRegularSearchFragment);
848 }
849 transaction.commit();
850
851 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leeec489fb2014-05-19 15:39:27 -0700852 mSearchEditTextLayout.animateExpandOrCollapse(false);
Andrew Leed4cde832014-05-16 15:56:48 -0700853 mSearchIcon.setVisibility(View.VISIBLE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700854 }
855
856 /** Returns an Intent to launch Call Settings screen */
857 public static Intent getCallSettingsIntent() {
858 final Intent intent = new Intent(Intent.ACTION_MAIN);
859 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
860 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
861 return intent;
862 }
863
864 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700865 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700866 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700867 if (TextUtils.isEmpty(mSearchQuery)) {
868 exitSearchUi();
869 }
Yorke Leef6673d32013-08-23 15:34:17 -0700870 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700871 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700872 exitSearchUi();
Yorke Leec3766332013-07-31 11:13:16 -0700873 } else {
874 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700875 }
Yorke Leec3766332013-07-31 11:13:16 -0700876 }
877
878 @Override
879 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700880 if (mSmartDialSearchFragment != null) {
881 mSmartDialSearchFragment.setAddToContactNumber(query);
882 }
Yorke Leec3766332013-07-31 11:13:16 -0700883 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
884 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700885
886 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700887 if (DEBUG) {
888 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
889 }
890 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
891 // This callback can happen if the dialpad fragment is recreated because of
892 // activity destruction. In that case, don't update the search view because
893 // that would bring the user back to the search fragment regardless of the
894 // previous state of the application. Instead, just return here and let the
895 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700896 if (!TextUtils.isEmpty(normalizedQuery)) {
897 mPendingSearchViewQuery = normalizedQuery;
898 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700899 return;
900 }
Yorke Lee53a22302014-04-29 18:13:46 -0700901 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700902 }
903 }
Yorke Leec3766332013-07-31 11:13:16 -0700904
905 @Override
906 public void onListFragmentScrollStateChange(int scrollState) {
907 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700908 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700909 hideInputMethod(getCurrentFocus());
910 }
911 }
912
913 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700914 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700915 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700916 // TODO: No-op for now. This should eventually show/hide the actionBar based on
917 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700918 }
919
Yorke Lee86e21f72014-04-02 16:49:38 -0700920 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700921 public void setFloatingActionButtonVisible(boolean visible) {
922 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700923 }
924
925 private boolean phoneIsInUse() {
926 final TelephonyManager tm = (TelephonyManager) getSystemService(
927 Context.TELEPHONY_SERVICE);
928 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
929 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700930
Yorke Leee1424812013-09-04 18:24:48 -0700931 public static Intent getAddNumberToContactIntent(CharSequence text) {
932 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
933 intent.putExtra(Intents.Insert.PHONE, text);
934 intent.setType(Contacts.CONTENT_ITEM_TYPE);
935 return intent;
936 }
937
Yorke Leeda0f9042013-12-05 14:25:51 -0800938 private boolean canIntentBeHandled(Intent intent) {
939 final PackageManager packageManager = getPackageManager();
940 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
941 PackageManager.MATCH_DEFAULT_ONLY);
942 return resolveInfo != null && resolveInfo.size() > 0;
943 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800944
Yorke Lee6a1461a2014-04-21 16:27:14 -0700945 @Override
946 public void showCallHistory() {
947 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
948 // CONTENT_TYPE, so that we always open our call log from our dialer
949 final Intent intent = new Intent(this, CallLogActivity.class);
950 startActivity(intent);
951 }
952
Yorke Lee86e21f72014-04-02 16:49:38 -0700953 /**
954 * Called when the user has long-pressed a contact tile to start a drag operation.
955 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800956 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700957 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700958 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700959 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800960 }
961
962 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700963 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
964 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800965
Yorke Lee86e21f72014-04-02 16:49:38 -0700966 /**
967 * Called when the user has released a contact tile after long-pressing it.
968 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800969 @Override
970 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700971 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700972 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800973 }
974
975 @Override
976 public void onDroppedOnRemove() {}
977
978 /**
Yorke Leed999b712014-04-23 15:10:58 -0700979 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800980 * once it has been attached to the activity.
981 */
982 @Override
983 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -0700984 mDragDropController = dragController;
985 ((RemoveView) findViewById(R.id.remove_view))
986 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800987 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700988
989 @Override
990 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700991 // Specify call-origin so that users will see the previous tab instead of
992 // CallLog screen (search UI will be automatically exited).
993 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -0700994 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700995 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700996 }
997
998 @Override
999 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001000 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
1001 startActivity(intent);
1002 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001003 }
1004
1005 @Override
1006 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001007 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001008 }
1009
1010 @Override
1011 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001012 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001013 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001014
1015 public int getActionBarHeight() {
1016 return mActionBarHeight;
1017 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001018
1019 @Override
1020 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1021
1022 }
1023
1024 @Override
1025 public void onPageSelected(int position) {
1026 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001027 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001028 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001029 alignFloatingActionButtonByTab(mCurrentTabPosition);
1030 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001031 }
1032
1033 @Override
1034 public void onPageScrollStateChanged(int state) {
1035 }
1036
Andrew Lee2a58ab82014-05-15 02:16:04 -07001037 private void updateFloatingActionButton() {
1038 if (mIsDialpadShown) {
1039 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1040 mFloatingActionButton.setContentDescription(
1041 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001042 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001043 } else {
1044 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1045 mFloatingActionButton.setContentDescription(
1046 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001047 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001048 }
1049 }
1050
Yorke Leecc4660d2014-04-24 11:22:57 -07001051 private void alignFloatingActionButtonByTab(int position) {
1052 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1053 alignFloatingActionButtonMiddle();
1054 } else {
1055 alignFloatingActionButtonRight();
1056 }
1057 }
1058
1059 private void alignFloatingActionButtonRight() {
1060 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001061 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001062 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1063 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001064 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001065 }
1066
1067 private void alignFloatingActionButtonMiddle() {
1068 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001069 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001070 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1071 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001072 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001073 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001074
1075 /**
1076 * Convenience class which implements AnimationListener interface as null-op methods.
1077 */
1078 private class ActivityAnimationListener implements AnimationListener {
1079 @Override
1080 public void onAnimationStart(Animation animation) {
1081 }
1082
1083 @Override
1084 public void onAnimationEnd(Animation animation) {
1085 }
1086
1087 @Override
1088 public void onAnimationRepeat(Animation animation) {
1089 }
1090 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001091}