blob: 9586d252e48d4e0e67d0c5d750c8bf3e74f9c6c9 [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);
Yorke Leeddfb0012014-05-18 18:21:01 -0700339 mVoiceSearchButton = actionBarView.findViewById(R.id.voice_search_button);
Andrew Leed4cde832014-05-16 15:56:48 -0700340
Andrew Leeb1903842014-05-15 16:11:24 -0700341 mSearchView = (EditText) actionBarView.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700342 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Andrew Leeb1903842014-05-15 16:11:24 -0700343 mSearchView.setOnTouchListener(mSearchViewOnTouchListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700344
Andrew Leeb1903842014-05-15 16:11:24 -0700345 mSearchViewCloseButton = actionBarView.findViewById(R.id.search_close_button);
Yorke Lee53a22302014-04-29 18:13:46 -0700346 mSearchViewCloseButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700347
Andrew Leee74a10e2014-05-16 14:31:40 -0700348 ImageButton optionsMenuButton =
349 (ImageButton) actionBarView.findViewById(R.id.dialtacts_options_menu_button);
350 optionsMenuButton.setOnClickListener(this);
351 final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
352 optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
353
Yorke Lee33932ff2014-04-16 13:34:32 -0700354 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
355 new int[] { android.R.attr.actionBarSize });
356 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
357 styledAttributes.recycle();
358
Yorke Leeef2b7382013-08-09 17:39:25 -0700359 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
360 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700361 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800362 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700363 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800364 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
365 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700366 } else {
367 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700368 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
369 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700370 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700371 }
372
Andrew Lee0c667702014-05-12 14:31:45 -0700373 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
374 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
375 parentLayout.setOnDragListener(new LayoutOnDragListener());
376
Andrew Leea73e1892014-05-13 13:21:16 -0700377 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
378 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700379
Andrew Leea73e1892014-05-13 13:21:16 -0700380 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
381 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800382
Yorke Lee28266192014-05-01 10:05:52 -0700383 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800384
Yorke Lee0baa98b2013-08-22 10:55:16 -0700385 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700386 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700387 }
388
389 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700390 protected void onResume() {
391 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700392 if (mFirstLaunch) {
393 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700394 } else if (!phoneIsInUse() && mInCallDialpadUp) {
395 hideDialpadFragment(false, true);
396 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700397 }
398 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700399 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700400 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700401 }
402
403 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700404 protected void onPause() {
405 if (mClearSearchOnPause) {
406 hideDialpadAndSearchUi();
407 mClearSearchOnPause = false;
408 }
409 super.onPause();
410 }
411
412 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700413 protected void onSaveInstanceState(Bundle outState) {
414 super.onSaveInstanceState(outState);
415 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700416 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
417 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700418 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
419 }
420
421 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700422 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700423 if (fragment instanceof DialpadFragment) {
424 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700425 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700426 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700427 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700428 } else if (fragment instanceof SmartDialSearchFragment) {
429 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700430 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700431 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700432 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700433 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700434 } else if (fragment instanceof ListsFragment) {
435 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700436 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700437 }
Yorke Leec3766332013-07-31 11:13:16 -0700438 }
439
Alon Albertb453e5b2013-09-10 15:54:23 -0700440 protected void handleMenuSettings() {
441 openTelephonySetting(this);
442 }
443
444 public static void openTelephonySetting(Activity activity) {
445 final Intent settingsIntent = getCallSettingsIntent();
446 activity.startActivity(settingsIntent);
447 }
448
Chiao Cheng94b10b52012-08-17 16:59:12 -0700449 @Override
450 public void onClick(View view) {
451 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700452 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700453 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700454 mInCallDialpadUp = false;
455 showDialpadFragment(true);
456 } else {
457 // Dial button was pressed; tell the Dialpad fragment
458 mDialpadFragment.dialButtonPressed();
459 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700460 break;
Yorke Leec3766332013-07-31 11:13:16 -0700461 case R.id.search_close_button:
462 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700463 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700464 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700465 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700466 }
467 break;
Yorke Leec3766332013-07-31 11:13:16 -0700468 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800469 try {
470 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
471 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
472 } catch (ActivityNotFoundException e) {
473 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
474 Toast.LENGTH_SHORT).show();
475 }
Yorke Leec3766332013-07-31 11:13:16 -0700476 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700477 case R.id.dialtacts_options_menu_button:
478 buildOptionsMenu(view).show();
479 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700480 default: {
481 Log.wtf(TAG, "Unexpected onClick event from " + view);
482 break;
483 }
484 }
485 }
486
Yorke Leec3766332013-07-31 11:13:16 -0700487 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700488 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700489 switch (item.getItemId()) {
490 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700491 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700492 break;
493 case R.id.menu_add_contact:
494 try {
495 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
496 } catch (ActivityNotFoundException e) {
497 Toast toast = Toast.makeText(this,
498 R.string.add_contact_not_available,
499 Toast.LENGTH_SHORT);
500 toast.show();
501 }
502 break;
503 case R.id.menu_import_export:
504 // We hard-code the "contactsAreAvailable" argument because doing it properly would
505 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
506 // now in Dialtacts for (potential) performance reasons. Compare with how it is
507 // done in {@link PeopleActivity}.
508 ImportExportDialogFragment.show(getFragmentManager(), true,
509 DialtactsActivity.class);
510 return true;
511 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700512 ClearFrequentsDialog.show(getFragmentManager());
513 return true;
514 case R.id.menu_call_settings:
515 handleMenuSettings();
516 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700517 }
518 return false;
519 }
520
521 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700522 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
523 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
524 if (resultCode == RESULT_OK) {
525 final ArrayList<String> matches = data.getStringArrayListExtra(
526 RecognizerIntent.EXTRA_RESULTS);
527 if (matches.size() > 0) {
528 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700529 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700530 } else {
531 Log.e(TAG, "Voice search - nothing heard");
532 }
533 } else {
534 Log.e(TAG, "Voice search failed");
535 }
536 }
537 super.onActivityResult(requestCode, resultCode, data);
538 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700539
Andrew Lee2a58ab82014-05-15 02:16:04 -0700540 /**
541 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
542 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
543 * @see #onDialpadShown
544 */
Yorke Leec3766332013-07-31 11:13:16 -0700545 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700546 if (mIsDialpadShown) {
547 return;
548 }
549 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700550 mDialpadFragment.setAnimate(animate);
551
Chiao Cheng94b10b52012-08-17 16:59:12 -0700552 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700553 ft.show(mDialpadFragment);
554 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700555
556 if (!isInSearchUi()) {
557 enterSearchUi(true /* isSmartDial */, mSearchQuery);
558 }
Yorke Leec3766332013-07-31 11:13:16 -0700559 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700560
Andrew Lee2a58ab82014-05-15 02:16:04 -0700561 /**
562 * Callback from child DialpadFragment when the dialpad is shown.
563 */
564 public void onDialpadShown() {
565 updateFloatingActionButton();
566 if (mDialpadFragment.getAnimate()) {
567 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
568 mDialpadFragment.getView().startAnimation(slideIn);
569 } else {
570 mDialpadFragment.setYFraction(0);
571 }
572
Andrew Lee2a58ab82014-05-15 02:16:04 -0700573 updateSearchFragmentPosition();
574 getActionBar().hide();
575 }
576
577 /**
578 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
579 * a callback after the hide animation ends.
580 * @see #commitDialpadFragmentHide
581 */
Yorke Leeca195042013-09-25 16:29:38 -0700582 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700583 if (mDialpadFragment == null) {
584 return;
585 }
Yorke Leef6673d32013-08-23 15:34:17 -0700586 if (clearDialpad) {
587 mDialpadFragment.clearDialpad();
588 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700589 if (!mIsDialpadShown) {
590 return;
Yorke Leec3766332013-07-31 11:13:16 -0700591 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700592 mIsDialpadShown = false;
593 mDialpadFragment.setAnimate(animate);
594
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700595 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700596 updateFloatingActionButton();
597 if (animate) {
598 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
599 slideOut.setAnimationListener(new ActivityAnimationListener() {
600 @Override
601 public void onAnimationEnd(Animation animation) {
602 commitDialpadFragmentHide();
603 }
604 });
605 mDialpadFragment.getView().startAnimation(slideOut);
606 } else {
607 commitDialpadFragmentHide();
608 }
609
Andrew Leeb1903842014-05-15 16:11:24 -0700610 mListsFragment.maybeShowActionBar();
Andrew Leed4cde832014-05-16 15:56:48 -0700611 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700612 if (TextUtils.isEmpty(mSearchQuery)) {
613 exitSearchUi();
614 } else {
615 mSearchIcon.setVisibility(View.GONE);
616 }
Andrew Leed4cde832014-05-16 15:56:48 -0700617 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700618 }
619
620 /**
621 * Finishes hiding the dialpad fragment after any animations are completed.
622 */
623 private void commitDialpadFragmentHide() {
624 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700625 ft.hide(mDialpadFragment);
626 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700627 }
628
Andrew Lee2a58ab82014-05-15 02:16:04 -0700629 private void updateSearchFragmentPosition() {
630 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
631 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700632 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700633 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700634 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700635 fragment = mRegularSearchFragment;
636 }
637 if (fragment != null && fragment.isVisible()) {
638 fragment.getView().animate().translationY(translationValue)
639 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
640 }
641 }
642
Andrew Leeb1903842014-05-15 16:11:24 -0700643 private boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700644 return mInDialpadSearch || mInRegularSearch;
645 }
646
647 private void setNotInSearchUi() {
648 mInDialpadSearch = false;
649 mInRegularSearch = false;
650 }
651
Yorke Lee311969c2013-08-26 06:31:11 -0700652 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700653 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700654 hideDialpadFragment(false, true);
655 }
656
Yorke Leee00c9fe2014-04-12 12:42:06 -0700657 private void hideInputMethod(View view) {
658 final InputMethodManager imm = (InputMethodManager) getSystemService(
659 Context.INPUT_METHOD_SERVICE);
660 if (imm != null && view != null) {
661 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
662 }
663 }
664
Yorke Lee53a22302014-04-29 18:13:46 -0700665 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700666 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
667 if (canIntentBeHandled(voiceIntent)) {
668 mVoiceSearchButton.setVisibility(View.VISIBLE);
669 mVoiceSearchButton.setOnClickListener(this);
670 } else {
671 mVoiceSearchButton.setVisibility(View.GONE);
672 }
673 }
674
Andrew Leee74a10e2014-05-16 14:31:40 -0700675 private OptionsPopupMenu buildOptionsMenu(View invoker) {
676 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
677 popupMenu.inflate(R.menu.dialtacts_options);
678 popupMenu.setOnMenuItemClickListener(this);
679 return popupMenu;
680 }
681
Yorke Lee86e21f72014-04-02 16:49:38 -0700682 @Override
683 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700684 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700685 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700686 mPendingSearchViewQuery = null;
687 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700688 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700689 }
690
Chiao Cheng94b10b52012-08-17 16:59:12 -0700691 /**
692 * Returns true if the intent is due to hitting the green send key (hardware call button:
693 * KEYCODE_CALL) while in a call.
694 *
695 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700696 * @return true if the intent is due to hitting the green send key while in a call
697 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700698 private boolean isSendKeyWhileInCall(Intent intent) {
699 // If there is a call in progress and the user launched the dialer by hitting the call
700 // button, go straight to the in-call screen.
701 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700702
Yorke Lee62e995c2014-03-28 10:02:56 -0700703 try {
704 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
705 if (callKey && phone != null && phone.showCallScreen()) {
706 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700707 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700708 } catch (RemoteException e) {
709 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700710 }
711
712 return false;
713 }
714
715 /**
716 * Sets the current tab based on the intent's request type
717 *
718 * @param intent Intent that contains information about which tab should be selected
719 */
Yorke Leec3766332013-07-31 11:13:16 -0700720 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700721 // 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 -0700722 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700723 finish();
724 return;
725 }
726
Yorke Lee669b6082013-09-17 15:43:38 -0700727 if (mDialpadFragment != null) {
728 final boolean phoneIsInUse = phoneIsInUse();
729 if (phoneIsInUse || isDialIntent(intent)) {
730 mDialpadFragment.setStartedFromNewIntent(true);
731 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
732 mInCallDialpadUp = true;
733 }
734 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700735 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700736 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700737 }
738
739 @Override
740 public void onNewIntent(Intent newIntent) {
741 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700742 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700743
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 invalidateOptionsMenu();
745 }
746
747 /** Returns true if the given intent contains a phone number to populate the dialer with */
748 private boolean isDialIntent(Intent intent) {
749 final String action = intent.getAction();
750 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
751 return true;
752 }
753 if (Intent.ACTION_VIEW.equals(action)) {
754 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700755 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700756 return true;
757 }
758 }
759 return false;
760 }
761
762 /**
763 * Returns an appropriate call origin for this Activity. May return null when no call origin
764 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
765 * for remembering the tab in which the user made a phone call, so the external app's DIAL
766 * request should not be counted.)
767 */
768 public String getCallOrigin() {
769 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
770 }
771
Chiao Cheng94b10b52012-08-17 16:59:12 -0700772 /**
Yorke Leec3766332013-07-31 11:13:16 -0700773 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700774 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700775 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700776 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700777 // Weird race condition where fragment is doing work after the activity is destroyed
778 // due to talkback being on (b/10209937). Just return since we can't do any
779 // constructive here.
780 return;
781 }
782
Yorke Leeef2b7382013-08-09 17:39:25 -0700783 if (DEBUG) {
784 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
785 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700786
Yorke Leec3766332013-07-31 11:13:16 -0700787 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700788 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700789 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700790 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700791 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700792 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700793
Yorke Leeb207f8a2013-08-29 18:51:06 -0700794 final String tag;
795 if (smartDialSearch) {
796 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
797 } else {
798 tag = TAG_REGULAR_SEARCH_FRAGMENT;
799 }
800 mInDialpadSearch = smartDialSearch;
801 mInRegularSearch = !smartDialSearch;
802
Andrew Lee752956e2014-05-15 11:43:38 -0700803 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700804 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700805 if (fragment == null) {
806 if (smartDialSearch) {
807 fragment = new SmartDialSearchFragment();
808 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700809 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700810 }
Andrew Leeb1903842014-05-15 16:11:24 -0700811 transaction.add(R.id.dialtacts_frame, fragment, tag);
812 } else {
813 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700814 }
Andrew Leeb1903842014-05-15 16:11:24 -0700815
Yorke Lee86e21f72014-04-02 16:49:38 -0700816 // DialtactsActivity will provide the options menu
817 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700818 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700819 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700820 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700821
822 mListsFragment.getView().animate().alpha(0).withLayer();
Andrew Leed4cde832014-05-16 15:56:48 -0700823
824 if (!mIsDialpadShown) {
825 mSearchIcon.setVisibility(View.GONE);
826 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700827 }
828
829 /**
Yorke Leec3766332013-07-31 11:13:16 -0700830 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700831 */
Yorke Leec3766332013-07-31 11:13:16 -0700832 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700833 // See related bug in enterSearchUI();
834 if (getFragmentManager().isDestroyed()) {
835 return;
836 }
Andrew Leeb1903842014-05-15 16:11:24 -0700837
Andrew Leeb1903842014-05-15 16:11:24 -0700838 mSearchView.setText(null);
839 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700840 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700841
Andrew Leeb1903842014-05-15 16:11:24 -0700842 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
843 transaction.setCustomAnimations(0, android.R.animator.fade_out);
844
845 if (mSmartDialSearchFragment != null) {
846 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700847 }
Andrew Leeb1903842014-05-15 16:11:24 -0700848 if (mRegularSearchFragment != null) {
849 transaction.remove(mRegularSearchFragment);
850 }
851 transaction.commit();
852
853 mListsFragment.getView().animate().alpha(1).withLayer();
Andrew Leed4cde832014-05-16 15:56:48 -0700854 mSearchIcon.setVisibility(View.VISIBLE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700855 }
856
857 /** Returns an Intent to launch Call Settings screen */
858 public static Intent getCallSettingsIntent() {
859 final Intent intent = new Intent(Intent.ACTION_MAIN);
860 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
861 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
862 return intent;
863 }
864
865 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700866 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700867 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700868 if (TextUtils.isEmpty(mSearchQuery)) {
869 exitSearchUi();
870 }
Yorke Leef6673d32013-08-23 15:34:17 -0700871 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700872 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700873 exitSearchUi();
Yorke Leec3766332013-07-31 11:13:16 -0700874 } else {
875 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700876 }
Yorke Leec3766332013-07-31 11:13:16 -0700877 }
878
879 @Override
880 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700881 if (mSmartDialSearchFragment != null) {
882 mSmartDialSearchFragment.setAddToContactNumber(query);
883 }
Yorke Leec3766332013-07-31 11:13:16 -0700884 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
885 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700886
887 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700888 if (DEBUG) {
889 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
890 }
891 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
892 // This callback can happen if the dialpad fragment is recreated because of
893 // activity destruction. In that case, don't update the search view because
894 // that would bring the user back to the search fragment regardless of the
895 // previous state of the application. Instead, just return here and let the
896 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700897 if (!TextUtils.isEmpty(normalizedQuery)) {
898 mPendingSearchViewQuery = normalizedQuery;
899 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700900 return;
901 }
Yorke Lee53a22302014-04-29 18:13:46 -0700902 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700903 }
904 }
Yorke Leec3766332013-07-31 11:13:16 -0700905
906 @Override
907 public void onListFragmentScrollStateChange(int scrollState) {
908 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700909 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700910 hideInputMethod(getCurrentFocus());
911 }
912 }
913
914 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700915 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700916 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700917 // TODO: No-op for now. This should eventually show/hide the actionBar based on
918 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700919 }
920
Yorke Lee86e21f72014-04-02 16:49:38 -0700921 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700922 public void setFloatingActionButtonVisible(boolean visible) {
923 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700924 }
925
926 private boolean phoneIsInUse() {
927 final TelephonyManager tm = (TelephonyManager) getSystemService(
928 Context.TELEPHONY_SERVICE);
929 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
930 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700931
Yorke Leee1424812013-09-04 18:24:48 -0700932 public static Intent getAddNumberToContactIntent(CharSequence text) {
933 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
934 intent.putExtra(Intents.Insert.PHONE, text);
935 intent.setType(Contacts.CONTENT_ITEM_TYPE);
936 return intent;
937 }
938
Yorke Leeda0f9042013-12-05 14:25:51 -0800939 private boolean canIntentBeHandled(Intent intent) {
940 final PackageManager packageManager = getPackageManager();
941 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
942 PackageManager.MATCH_DEFAULT_ONLY);
943 return resolveInfo != null && resolveInfo.size() > 0;
944 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800945
Yorke Lee6a1461a2014-04-21 16:27:14 -0700946 @Override
947 public void showCallHistory() {
948 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
949 // CONTENT_TYPE, so that we always open our call log from our dialer
950 final Intent intent = new Intent(this, CallLogActivity.class);
951 startActivity(intent);
952 }
953
Yorke Lee86e21f72014-04-02 16:49:38 -0700954 /**
955 * Called when the user has long-pressed a contact tile to start a drag operation.
956 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800957 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700958 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700959 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700960 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800961 }
962
963 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700964 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
965 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800966
Yorke Lee86e21f72014-04-02 16:49:38 -0700967 /**
968 * Called when the user has released a contact tile after long-pressing it.
969 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800970 @Override
971 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700972 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700973 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800974 }
975
976 @Override
977 public void onDroppedOnRemove() {}
978
979 /**
Yorke Leed999b712014-04-23 15:10:58 -0700980 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800981 * once it has been attached to the activity.
982 */
983 @Override
984 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -0700985 mDragDropController = dragController;
986 ((RemoveView) findViewById(R.id.remove_view))
987 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800988 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700989
990 @Override
991 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700992 // Specify call-origin so that users will see the previous tab instead of
993 // CallLog screen (search UI will be automatically exited).
994 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -0700995 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700996 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700997 }
998
999 @Override
1000 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001001 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
1002 startActivity(intent);
1003 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001004 }
1005
1006 @Override
1007 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001008 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001009 }
1010
1011 @Override
1012 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001013 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001014 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001015
1016 public int getActionBarHeight() {
1017 return mActionBarHeight;
1018 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001019
1020 @Override
1021 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1022
1023 }
1024
1025 @Override
1026 public void onPageSelected(int position) {
1027 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001028 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001029 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001030 alignFloatingActionButtonByTab(mCurrentTabPosition);
1031 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001032 }
1033
1034 @Override
1035 public void onPageScrollStateChanged(int state) {
1036 }
1037
Andrew Lee2a58ab82014-05-15 02:16:04 -07001038 private void updateFloatingActionButton() {
1039 if (mIsDialpadShown) {
1040 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1041 mFloatingActionButton.setContentDescription(
1042 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001043 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001044 } else {
1045 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1046 mFloatingActionButton.setContentDescription(
1047 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001048 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001049 }
1050 }
1051
Yorke Leecc4660d2014-04-24 11:22:57 -07001052 private void alignFloatingActionButtonByTab(int position) {
1053 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1054 alignFloatingActionButtonMiddle();
1055 } else {
1056 alignFloatingActionButtonRight();
1057 }
1058 }
1059
1060 private void alignFloatingActionButtonRight() {
1061 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001062 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001063 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1064 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001065 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001066 }
1067
1068 private void alignFloatingActionButtonMiddle() {
1069 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001070 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001071 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1072 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001073 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001074 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001075
1076 /**
1077 * Convenience class which implements AnimationListener interface as null-op methods.
1078 */
1079 private class ActivityAnimationListener implements AnimationListener {
1080 @Override
1081 public void onAnimationStart(Animation animation) {
1082 }
1083
1084 @Override
1085 public void onAnimationEnd(Animation animation) {
1086 }
1087
1088 @Override
1089 public void onAnimationRepeat(Animation animation) {
1090 }
1091 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001092}