blob: 922b38351420579447927111d5af36aecdfa8a28 [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;
Chiao Cheng94b10b52012-08-17 16:59:12 -070023import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080024import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070025import android.content.Context;
26import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080027import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Sai Cheemalapati82d6da52014-06-03 13:54:23 -070029import android.content.res.Configuration;
Andrew Lee2423a2f2014-05-29 14:14:45 -070030import android.content.res.Resources;
Yorke Lee33932ff2014-04-16 13:34:32 -070031import android.content.res.TypedArray;
Chiao Cheng94b10b52012-08-17 16:59:12 -070032import android.net.Uri;
33import android.os.Bundle;
34import android.os.RemoteException;
35import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070037import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070038import android.speech.RecognizerIntent;
Yorke Leecc4660d2014-04-24 11:22:57 -070039import android.support.v4.view.ViewPager;
Yorke Leec3766332013-07-31 11:13:16 -070040import android.telephony.TelephonyManager;
Yorke Lee53a22302014-04-29 18:13:46 -070041import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070042import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070043import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070045import android.view.DragEvent;
Andrew Leeb1903842014-05-15 16:11:24 -070046import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070049import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.View;
Sai Cheemalapati41460652014-06-02 21:05:39 -070051import android.view.ViewTreeObserver;
Yorke Lee28266192014-05-01 10:05:52 -070052import android.view.View.OnDragListener;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070053import android.view.View.OnTouchListener;
Yorke Lee86e21f72014-04-02 16:49:38 -070054import android.view.animation.AccelerateInterpolator;
Andrew Lee2a58ab82014-05-15 02:16:04 -070055import android.view.animation.Animation;
56import android.view.animation.Animation.AnimationListener;
57import android.view.animation.AnimationUtils;
Yorke Lee86e21f72014-04-02 16:49:38 -070058import android.view.animation.DecelerateInterpolator;
59import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070060import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070061import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070062import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070063import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070064import android.widget.PopupMenu;
Yorke Leecc4660d2014-04-24 11:22:57 -070065import android.widget.RelativeLayout;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080066import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070067
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070068import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070069import com.android.contacts.common.activity.TransactionSafeActivity;
Andrew Lee3c14bc32014-05-28 15:43:03 -070070import com.android.contacts.common.animation.AnimationListenerAdapter;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.contacts.common.dialog.ClearFrequentsDialog;
72import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080073import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070074import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070075import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070076import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070077import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070078import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070079import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070080import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080081import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070082import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080083import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070084import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070085import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070086import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070087import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080088import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070089import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070090import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee5253be02014-05-21 18:50:03 -070091import com.android.dialer.widget.ActionBarController;
Sai Cheemalapati41460652014-06-02 21:05:39 -070092import com.android.dialer.widget.FloatingActionButtonController;
Andrew Leeb1903842014-05-15 16:11:24 -070093import com.android.dialer.widget.SearchEditTextLayout;
Yorke Lee11ca39e2014-05-19 20:31:37 -070094import com.android.dialer.widget.SearchEditTextLayout.OnBackButtonClickedListener;
Yorke Lee0baa98b2013-08-22 10:55:16 -070095import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070096
Yorke Leec3766332013-07-31 11:13:16 -070097import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080098import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070099
Chiao Cheng94b10b52012-08-17 16:59:12 -0700100/**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101 * The dialer tab's title is 'phone', a more common name (see strings.xml).
102 */
Yorke Leec3766332013-07-31 11:13:16 -0700103public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700104 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700105 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -0800106 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700107 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700108 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700109 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700110 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700111 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700112 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700113 ViewPager.OnPageChangeListener,
114 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 private static final String TAG = "DialtactsActivity";
116
Ihab Awad526c0b82014-02-27 12:55:36 -0800117 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118
Yorke Leef74011e2013-08-02 11:30:30 -0700119 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
120
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121 /** Used to open Call Setting */
122 private static final String PHONE_PACKAGE = "com.android.phone";
123 private static final String CALL_SETTINGS_CLASS_NAME =
124 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700125 /** @see #getCallOrigin() */
126 private static final String CALL_ORIGIN_DIALTACTS =
127 "com.android.dialer.DialtactsActivity";
128
Yorke Leeb207f8a2013-08-29 18:51:06 -0700129 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
130 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700131 private static final String KEY_SEARCH_QUERY = "search_query";
132 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700133 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700134
Yorke Leec3766332013-07-31 11:13:16 -0700135 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
136 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
137 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
138 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700139
Chiao Cheng94b10b52012-08-17 16:59:12 -0700140 /**
141 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
142 */
143 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
144
Yorke Leec3766332013-07-31 11:13:16 -0700145 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700146
Andrew Lee0c667702014-05-12 14:31:45 -0700147 private RelativeLayout parentLayout;
148
Yorke Leec3766332013-07-31 11:13:16 -0700149 /**
Yorke Leec3766332013-07-31 11:13:16 -0700150 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700151 */
Yorke Leef74011e2013-08-02 11:30:30 -0700152 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700153
154 /**
155 * Fragment for searching phone numbers using the alphanumeric keyboard.
156 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700157 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700158
159 /**
160 * Fragment for searching phone numbers using the dialpad.
161 */
162 private SmartDialSearchFragment mSmartDialSearchFragment;
163
Yorke Leee00c9fe2014-04-12 12:42:06 -0700164 /**
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700165 * Animation that slides in.
166 */
167 private Animation mSlideIn;
168
169 /**
170 * Animation that slides out.
171 */
172 private Animation mSlideOut;
173
174 /**
175 * Listener for after slide out animation completes on dialer fragment.
176 */
177 AnimationListenerAdapter mSlideOutListener = new AnimationListenerAdapter() {
178 @Override
179 public void onAnimationEnd(Animation animation) {
180 commitDialpadFragmentHide();
181 }
182 };
183
184 /**
Yorke Leee00c9fe2014-04-12 12:42:06 -0700185 * Fragment containing the speed dial list, recents list, and all contacts list.
186 */
187 private ListsFragment mListsFragment;
188
Yorke Leeb207f8a2013-08-29 18:51:06 -0700189 private boolean mInDialpadSearch;
190 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700191 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700192 private boolean mIsDialpadShown;
Yorke Lee50aba882014-05-28 20:04:31 -0700193 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700194
Yorke Leeef2b7382013-08-09 17:39:25 -0700195 /**
Sai Cheemalapati41460652014-06-02 21:05:39 -0700196 * Whether or not the device is in landscape orientation.
197 */
198 private boolean mIsLandscape;
199
200 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700201 * The position of the currently selected tab in the attached {@link ListsFragment}.
202 */
203 private int mCurrentTabPosition = 0;
204
205 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700206 * True if the dialpad is only temporarily showing due to being in call
207 */
208 private boolean mInCallDialpadUp;
209
210 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700211 * True when this activity has been launched for the first time.
212 */
Yorke Lee98702de2013-08-06 12:03:32 -0700213 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700214
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700215 /**
216 * Search query to be applied to the SearchView in the ActionBar once
217 * onCreateOptionsMenu has been called.
218 */
219 private String mPendingSearchViewQuery;
220
Yorke Lee53a22302014-04-29 18:13:46 -0700221 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700222 private View mVoiceSearchButton;
Yorke Leeec489fb2014-05-19 15:39:27 -0700223 private SearchEditTextLayout mSearchEditTextLayout;
Andrew Leee74a10e2014-05-16 14:31:40 -0700224
Yorke Lee86e21f72014-04-02 16:49:38 -0700225 /**
226 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
227 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
228 * removed from the speed dial list.
229 */
Yorke Lee28266192014-05-01 10:05:52 -0700230 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700231
Yorke Leeef2b7382013-08-09 17:39:25 -0700232 private String mSearchQuery;
233
Yorke Leefce269a2013-08-12 11:56:04 -0700234 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700235 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700236 private ActionBarController mActionBarController;
Sai Cheemalapati41460652014-06-02 21:05:39 -0700237 private FloatingActionButtonController mFloatingActionButtonController;
Yorke Leefce269a2013-08-12 11:56:04 -0700238
Andrew Lee2423a2f2014-05-29 14:14:45 -0700239 private int mActionBarHeight;
Andrew Lee2423a2f2014-05-29 14:14:45 -0700240
Andrew Leee74a10e2014-05-16 14:31:40 -0700241 private class OptionsPopupMenu extends PopupMenu {
242 public OptionsPopupMenu(Context context, View anchor) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700243 super(context, anchor);
244 }
245
246 @Override
247 public void show() {
248 final Menu menu = getMenu();
249 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700250 clearFrequents.setVisible(mListsFragment != null &&
251 mListsFragment.getSpeedDialFragment() != null &&
252 mListsFragment.getSpeedDialFragment().hasFrequents());
Yorke Leee00c9fe2014-04-12 12:42:06 -0700253 super.show();
254 }
255 }
256
Chiao Cheng94b10b52012-08-17 16:59:12 -0700257 /**
Yorke Lee28266192014-05-01 10:05:52 -0700258 * Listener that listens to drag events and sends their x and y coordinates to a
259 * {@link DragDropController}.
260 */
261 private class LayoutOnDragListener implements OnDragListener {
262 @Override
263 public boolean onDrag(View v, DragEvent event) {
264 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
265 mDragDropController.handleDragHovered(v, (int) event.getX(),
266 (int) event.getY());
267 }
268 return true;
269 }
270 }
271
272 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700273 * Listener used to send search queries to the phone search fragment.
274 */
Yorke Lee53a22302014-04-29 18:13:46 -0700275 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700276 @Override
277 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
278 }
Yorke Leec3766332013-07-31 11:13:16 -0700279
Andrew Lee99a570c2014-05-15 14:18:50 -0700280 @Override
281 public void onTextChanged(CharSequence s, int start, int before, int count) {
282 final String newText = s.toString();
283 if (newText.equals(mSearchQuery)) {
284 // If the query hasn't changed (perhaps due to activity being destroyed
285 // and restored, or user launching the same DIAL intent twice), then there is
286 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700287 return;
288 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700289 if (DEBUG) {
290 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Lee710709d2014-05-29 07:18:42 -0700291 Log.d(TAG, "Previous Query: " + mSearchQuery);
Yorke Leec3766332013-07-31 11:13:16 -0700292 }
Yorke Lee710709d2014-05-29 07:18:42 -0700293 mSearchQuery = newText;
Andrew Lee99a570c2014-05-15 14:18:50 -0700294
Andrew Lee90374a72014-05-16 15:01:09 -0700295 // Show search fragment only when the query string is changed to non-empty text.
296 if (!TextUtils.isEmpty(newText)) {
297 // Call enterSearchUi only if we are switching search modes, or showing a search
298 // fragment for the first time.
299 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
300 (!mIsDialpadShown && mInRegularSearch);
301 if (!sameSearchMode) {
302 enterSearchUi(mIsDialpadShown, mSearchQuery);
303 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700304 }
305
306 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700307 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700308 } else if (mRegularSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700309 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700310 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700311 }
312
313 @Override
314 public void afterTextChanged(Editable s) {
315 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700316 };
317
Andrew Leeb1903842014-05-15 16:11:24 -0700318
319 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700320 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700321 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700322 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700323 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700324 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700325 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700326 mActionBarController.onSearchBoxTapped();
Andrew Leeb1903842014-05-15 16:11:24 -0700327 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
328 }
Andrew Leeb1903842014-05-15 16:11:24 -0700329 }
330 };
331
332 /**
333 * If the search term is empty and the user closes the soft keyboard, close the search UI.
334 */
335 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
336 @Override
337 public boolean onKey(View v, int keyCode, KeyEvent event) {
338 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
339 TextUtils.isEmpty(mSearchView.getText().toString())) {
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700340 maybeExitSearchUi();
Andrew Leeb1903842014-05-15 16:11:24 -0700341 }
342 return false;
343 }
344 };
345
Chiao Cheng94b10b52012-08-17 16:59:12 -0700346 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700347 protected void onCreate(Bundle savedInstanceState) {
348 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700349 mFirstLaunch = true;
350
Andrew Lee2423a2f2014-05-29 14:14:45 -0700351 final Resources resources = getResources();
352 mActionBarHeight = resources.getDimensionPixelSize(R.dimen.action_bar_height);
Andrew Lee2423a2f2014-05-29 14:14:45 -0700353
Yorke Lee8898cd02013-08-08 10:24:27 -0700354 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800355 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700356
Yorke Lee53a22302014-04-29 18:13:46 -0700357 final ActionBar actionBar = getActionBar();
358 actionBar.setCustomView(R.layout.search_edittext);
359 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700360 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700361
Yorke Lee5253be02014-05-21 18:50:03 -0700362 mActionBarController = new ActionBarController(this,
363 (SearchEditTextLayout) actionBar.getCustomView());
364
Yorke Leeec489fb2014-05-19 15:39:27 -0700365 mSearchEditTextLayout = (SearchEditTextLayout) actionBar.getCustomView();
366 mSearchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700367
Yorke Leeec489fb2014-05-19 15:39:27 -0700368 mSearchView = (EditText) mSearchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700369 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Yorke Lee11ca39e2014-05-19 20:31:37 -0700370 mVoiceSearchButton = mSearchEditTextLayout.findViewById(R.id.voice_search_button);
371 mSearchEditTextLayout.findViewById(R.id.search_box_start_search).setOnClickListener(
372 mSearchViewOnClickListener);
373 mSearchEditTextLayout.setOnBackButtonClickedListener(new OnBackButtonClickedListener() {
374 @Override
375 public void onBackButtonClicked() {
376 onBackPressed();
377 }
378 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700379
Sai Cheemalapati41460652014-06-02 21:05:39 -0700380 boolean mIsLandscape = getResources().getConfiguration().orientation
381 == Configuration.ORIENTATION_LANDSCAPE;
382 View floatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
383 ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
384 floatingActionButton.setOnClickListener(this);
385 mFloatingActionButtonController = new FloatingActionButtonController(this, mIsLandscape,
386 floatingActionButtonContainer);
387
Yorke Leeec489fb2014-05-19 15:39:27 -0700388 ImageButton optionsMenuButton = (ImageButton) mSearchEditTextLayout.findViewById(
389 R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700390 optionsMenuButton.setOnClickListener(this);
391 final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
392 optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
393
Yorke Leeef2b7382013-08-09 17:39:25 -0700394 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
395 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700396 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800397 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700398 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800399 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
400 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700401 } else {
402 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700403 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
404 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700405 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700406 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700407 mActionBarController.restoreInstanceState(savedInstanceState);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700408 mFloatingActionButtonController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700409 }
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700410
411 mSlideIn = AnimationUtils.loadAnimation(this,
412 mIsLandscape ? R.anim.slide_in_right : R.anim.slide_in);
413 mSlideOut = AnimationUtils.loadAnimation(this,
414 mIsLandscape ? R.anim.slide_out_right : R.anim.slide_out);
415
416 mSlideOut.setAnimationListener(mSlideOutListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700417
Andrew Lee0c667702014-05-12 14:31:45 -0700418 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
419 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
420 parentLayout.setOnDragListener(new LayoutOnDragListener());
Sai Cheemalapati41460652014-06-02 21:05:39 -0700421 parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
422 new ViewTreeObserver.OnGlobalLayoutListener() {
423 @Override
424 public void onGlobalLayout() {
425 int screenWidth = parentLayout.getWidth();
426 mFloatingActionButtonController.setScreenWidth(screenWidth);
427 parentLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
428 }
429 });
Andrew Lee0c667702014-05-12 14:31:45 -0700430
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700431 setupActivityOverlay();
432
Yorke Lee28266192014-05-01 10:05:52 -0700433 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800434
Yorke Lee0baa98b2013-08-22 10:55:16 -0700435 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700436 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700437 }
438
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700439 private void setupActivityOverlay() {
440 final View activityOverlay = findViewById(R.id.activity_overlay);
441 activityOverlay.setOnTouchListener(new OnTouchListener() {
442 @Override
443 public boolean onTouch(View v, MotionEvent event) {
444 if (!mIsDialpadShown) {
445 maybeExitSearchUi();
446 }
447 return false;
448 }
449 });
450 }
451
Chiao Cheng94b10b52012-08-17 16:59:12 -0700452 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700453 protected void onResume() {
454 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700455 if (mFirstLaunch) {
456 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700457 } else if (!phoneIsInUse() && mInCallDialpadUp) {
458 hideDialpadFragment(false, true);
459 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700460 } else if (mShowDialpadOnResume) {
461 showDialpadFragment(false);
462 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700463 }
464 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700465 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700466 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700467 }
468
469 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700470 protected void onPause() {
471 if (mClearSearchOnPause) {
472 hideDialpadAndSearchUi();
473 mClearSearchOnPause = false;
474 }
475 super.onPause();
476 }
477
478 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700479 protected void onSaveInstanceState(Bundle outState) {
480 super.onSaveInstanceState(outState);
481 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700482 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
483 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700484 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700485 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700486 mActionBarController.saveInstanceState(outState);
Sai Cheemalapati41460652014-06-02 21:05:39 -0700487 mFloatingActionButtonController.saveInstanceState(outState);
Yorke Leeef2b7382013-08-09 17:39:25 -0700488 }
489
490 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700491 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700492 if (fragment instanceof DialpadFragment) {
493 mDialpadFragment = (DialpadFragment) fragment;
Yorke Lee50aba882014-05-28 20:04:31 -0700494 if (!mShowDialpadOnResume) {
495 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
496 transaction.hide(mDialpadFragment);
497 transaction.commit();
498 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700499 } else if (fragment instanceof SmartDialSearchFragment) {
500 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700501 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700502 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700503 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700504 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700505 } else if (fragment instanceof ListsFragment) {
506 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700507 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700508 }
Yorke Leec3766332013-07-31 11:13:16 -0700509 }
510
Alon Albertb453e5b2013-09-10 15:54:23 -0700511 protected void handleMenuSettings() {
512 openTelephonySetting(this);
513 }
514
515 public static void openTelephonySetting(Activity activity) {
516 final Intent settingsIntent = getCallSettingsIntent();
517 activity.startActivity(settingsIntent);
518 }
519
Chiao Cheng94b10b52012-08-17 16:59:12 -0700520 @Override
521 public void onClick(View view) {
522 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700523 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700524 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700525 mInCallDialpadUp = false;
526 showDialpadFragment(true);
527 } else {
528 // Dial button was pressed; tell the Dialpad fragment
529 mDialpadFragment.dialButtonPressed();
530 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700531 break;
Yorke Leec3766332013-07-31 11:13:16 -0700532 case R.id.search_close_button:
533 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700534 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700535 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700536 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700537 }
538 break;
Yorke Leec3766332013-07-31 11:13:16 -0700539 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800540 try {
541 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
542 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
543 } catch (ActivityNotFoundException e) {
544 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
545 Toast.LENGTH_SHORT).show();
546 }
Yorke Leec3766332013-07-31 11:13:16 -0700547 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700548 case R.id.dialtacts_options_menu_button:
549 buildOptionsMenu(view).show();
550 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700551 default: {
552 Log.wtf(TAG, "Unexpected onClick event from " + view);
553 break;
554 }
555 }
556 }
557
Yorke Leec3766332013-07-31 11:13:16 -0700558 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700559 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700560 switch (item.getItemId()) {
561 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700562 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700563 break;
564 case R.id.menu_add_contact:
565 try {
566 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
567 } catch (ActivityNotFoundException e) {
568 Toast toast = Toast.makeText(this,
569 R.string.add_contact_not_available,
570 Toast.LENGTH_SHORT);
571 toast.show();
572 }
573 break;
574 case R.id.menu_import_export:
575 // We hard-code the "contactsAreAvailable" argument because doing it properly would
576 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
577 // now in Dialtacts for (potential) performance reasons. Compare with how it is
578 // done in {@link PeopleActivity}.
579 ImportExportDialogFragment.show(getFragmentManager(), true,
580 DialtactsActivity.class);
581 return true;
582 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700583 ClearFrequentsDialog.show(getFragmentManager());
584 return true;
585 case R.id.menu_call_settings:
586 handleMenuSettings();
587 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700588 }
589 return false;
590 }
591
592 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700593 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
594 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
595 if (resultCode == RESULT_OK) {
596 final ArrayList<String> matches = data.getStringArrayListExtra(
597 RecognizerIntent.EXTRA_RESULTS);
598 if (matches.size() > 0) {
599 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700600 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700601 } else {
602 Log.e(TAG, "Voice search - nothing heard");
603 }
604 } else {
605 Log.e(TAG, "Voice search failed");
606 }
607 }
608 super.onActivityResult(requestCode, resultCode, data);
609 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700610
Andrew Lee2a58ab82014-05-15 02:16:04 -0700611 /**
612 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
613 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
614 * @see #onDialpadShown
615 */
Yorke Leec3766332013-07-31 11:13:16 -0700616 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700617 if (mIsDialpadShown) {
618 return;
619 }
620 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700621 mDialpadFragment.setAnimate(animate);
622
Chiao Cheng94b10b52012-08-17 16:59:12 -0700623 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700624 ft.show(mDialpadFragment);
625 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700626
Yorke Lee5253be02014-05-21 18:50:03 -0700627 mActionBarController.onDialpadUp();
628
Andrew Leeb1903842014-05-15 16:11:24 -0700629 if (!isInSearchUi()) {
630 enterSearchUi(true /* isSmartDial */, mSearchQuery);
631 }
Yorke Leec3766332013-07-31 11:13:16 -0700632 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700633
Andrew Lee2a58ab82014-05-15 02:16:04 -0700634 /**
635 * Callback from child DialpadFragment when the dialpad is shown.
636 */
637 public void onDialpadShown() {
Sai Cheemalapati41460652014-06-02 21:05:39 -0700638 mFloatingActionButtonController.updateByDialpadVisibility(true);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700639 if (mDialpadFragment.getAnimate()) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700640 mDialpadFragment.getView().startAnimation(mSlideIn);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700641 } else {
642 mDialpadFragment.setYFraction(0);
643 }
644
Andrew Lee2a58ab82014-05-15 02:16:04 -0700645 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700646 }
647
648 /**
649 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
650 * a callback after the hide animation ends.
651 * @see #commitDialpadFragmentHide
652 */
Yorke Leeca195042013-09-25 16:29:38 -0700653 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700654 if (mDialpadFragment == null) {
655 return;
656 }
Yorke Leef6673d32013-08-23 15:34:17 -0700657 if (clearDialpad) {
658 mDialpadFragment.clearDialpad();
659 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700660 if (!mIsDialpadShown) {
661 return;
Yorke Leec3766332013-07-31 11:13:16 -0700662 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700663 mIsDialpadShown = false;
664 mDialpadFragment.setAnimate(animate);
665
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700666 updateSearchFragmentPosition();
Sai Cheemalapati41460652014-06-02 21:05:39 -0700667 mFloatingActionButtonController.updateByDialpadVisibility(false);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700668 if (animate) {
Sai Cheemalapati82d6da52014-06-03 13:54:23 -0700669 mDialpadFragment.getView().startAnimation(mSlideOut);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700670 } else {
671 commitDialpadFragmentHide();
672 }
673
Yorke Lee5253be02014-05-21 18:50:03 -0700674 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700675
Andrew Leed4cde832014-05-16 15:56:48 -0700676 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700677 if (TextUtils.isEmpty(mSearchQuery)) {
678 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700679 }
Andrew Leed4cde832014-05-16 15:56:48 -0700680 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700681 }
682
683 /**
684 * Finishes hiding the dialpad fragment after any animations are completed.
685 */
686 private void commitDialpadFragmentHide() {
687 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700688 ft.hide(mDialpadFragment);
689 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700690 }
691
Andrew Lee2a58ab82014-05-15 02:16:04 -0700692 private void updateSearchFragmentPosition() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700693 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700694 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700695 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700696 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700697 fragment = mRegularSearchFragment;
698 }
699 if (fragment != null && fragment.isVisible()) {
Andrew Lee9da8fb42014-06-03 14:03:09 -0700700 fragment.updatePosition(true /* animate */);
Andrew Lee2a58ab82014-05-15 02:16:04 -0700701 }
702 }
703
Yorke Lee5253be02014-05-21 18:50:03 -0700704 @Override
705 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700706 return mInDialpadSearch || mInRegularSearch;
707 }
708
Yorke Lee5253be02014-05-21 18:50:03 -0700709 @Override
710 public boolean hasSearchQuery() {
711 return !TextUtils.isEmpty(mSearchQuery);
712 }
713
714 @Override
715 public boolean shouldShowActionBar() {
716 return mListsFragment.shouldShowActionBar();
717 }
718
Yorke Leeb207f8a2013-08-29 18:51:06 -0700719 private void setNotInSearchUi() {
720 mInDialpadSearch = false;
721 mInRegularSearch = false;
722 }
723
Yorke Lee311969c2013-08-26 06:31:11 -0700724 private void hideDialpadAndSearchUi() {
Yorke Lee710709d2014-05-29 07:18:42 -0700725 if (mIsDialpadShown) {
726 hideDialpadFragment(false, true);
727 } else {
728 exitSearchUi();
729 }
Yorke Lee311969c2013-08-26 06:31:11 -0700730 }
731
Yorke Leee00c9fe2014-04-12 12:42:06 -0700732 private void hideInputMethod(View view) {
733 final InputMethodManager imm = (InputMethodManager) getSystemService(
734 Context.INPUT_METHOD_SERVICE);
735 if (imm != null && view != null) {
736 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
737 }
738 }
739
Yorke Lee53a22302014-04-29 18:13:46 -0700740 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700741 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
742 if (canIntentBeHandled(voiceIntent)) {
743 mVoiceSearchButton.setVisibility(View.VISIBLE);
744 mVoiceSearchButton.setOnClickListener(this);
745 } else {
746 mVoiceSearchButton.setVisibility(View.GONE);
747 }
748 }
749
Andrew Leee74a10e2014-05-16 14:31:40 -0700750 private OptionsPopupMenu buildOptionsMenu(View invoker) {
751 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
752 popupMenu.inflate(R.menu.dialtacts_options);
753 popupMenu.setOnMenuItemClickListener(this);
754 return popupMenu;
755 }
756
Yorke Lee86e21f72014-04-02 16:49:38 -0700757 @Override
758 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700759 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700760 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700761 mPendingSearchViewQuery = null;
762 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700763 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700764 }
765
Chiao Cheng94b10b52012-08-17 16:59:12 -0700766 /**
767 * Returns true if the intent is due to hitting the green send key (hardware call button:
768 * KEYCODE_CALL) while in a call.
769 *
770 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700771 * @return true if the intent is due to hitting the green send key while in a call
772 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700773 private boolean isSendKeyWhileInCall(Intent intent) {
774 // If there is a call in progress and the user launched the dialer by hitting the call
775 // button, go straight to the in-call screen.
776 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700777
Santos Cordon1d7ef6a2014-05-29 21:46:33 -0700778 if (callKey) {
779 getTelephonyManager().showCallScreen();
780 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700781 }
782
783 return false;
784 }
785
786 /**
787 * Sets the current tab based on the intent's request type
788 *
789 * @param intent Intent that contains information about which tab should be selected
790 */
Yorke Leec3766332013-07-31 11:13:16 -0700791 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700792 // 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 -0700793 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700794 finish();
795 return;
796 }
797
Yorke Lee669b6082013-09-17 15:43:38 -0700798 if (mDialpadFragment != null) {
799 final boolean phoneIsInUse = phoneIsInUse();
800 if (phoneIsInUse || isDialIntent(intent)) {
801 mDialpadFragment.setStartedFromNewIntent(true);
802 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
803 mInCallDialpadUp = true;
804 }
805 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700806 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700807 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700808 }
809
810 @Override
811 public void onNewIntent(Intent newIntent) {
812 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700813 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700814
Chiao Cheng94b10b52012-08-17 16:59:12 -0700815 invalidateOptionsMenu();
816 }
817
818 /** Returns true if the given intent contains a phone number to populate the dialer with */
819 private boolean isDialIntent(Intent intent) {
820 final String action = intent.getAction();
821 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
822 return true;
823 }
824 if (Intent.ACTION_VIEW.equals(action)) {
825 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700826 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700827 return true;
828 }
829 }
830 return false;
831 }
832
833 /**
834 * Returns an appropriate call origin for this Activity. May return null when no call origin
835 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
836 * for remembering the tab in which the user made a phone call, so the external app's DIAL
837 * request should not be counted.)
838 */
839 public String getCallOrigin() {
840 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
841 }
842
Chiao Cheng94b10b52012-08-17 16:59:12 -0700843 /**
Yorke Leec3766332013-07-31 11:13:16 -0700844 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700845 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700846 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700847 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700848 // Weird race condition where fragment is doing work after the activity is destroyed
849 // due to talkback being on (b/10209937). Just return since we can't do any
850 // constructive here.
851 return;
852 }
853
Yorke Leeef2b7382013-08-09 17:39:25 -0700854 if (DEBUG) {
855 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
856 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700857
Yorke Leec3766332013-07-31 11:13:16 -0700858 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700859 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700860 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700861 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700862 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700863 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700864
Yorke Leeb207f8a2013-08-29 18:51:06 -0700865 final String tag;
866 if (smartDialSearch) {
867 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
868 } else {
869 tag = TAG_REGULAR_SEARCH_FRAGMENT;
870 }
871 mInDialpadSearch = smartDialSearch;
872 mInRegularSearch = !smartDialSearch;
873
Andrew Lee752956e2014-05-15 11:43:38 -0700874 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700875 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700876 if (fragment == null) {
877 if (smartDialSearch) {
878 fragment = new SmartDialSearchFragment();
879 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700880 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700881 }
Andrew Leeb1903842014-05-15 16:11:24 -0700882 transaction.add(R.id.dialtacts_frame, fragment, tag);
883 } else {
884 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700885 }
Andrew Leeb1903842014-05-15 16:11:24 -0700886
Yorke Lee86e21f72014-04-02 16:49:38 -0700887 // DialtactsActivity will provide the options menu
888 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700889 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700890 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700891 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700892
893 mListsFragment.getView().animate().alpha(0).withLayer();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700894 }
895
896 /**
Yorke Leec3766332013-07-31 11:13:16 -0700897 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700898 */
Yorke Leec3766332013-07-31 11:13:16 -0700899 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700900 // See related bug in enterSearchUI();
901 if (getFragmentManager().isDestroyed()) {
902 return;
903 }
Andrew Leeb1903842014-05-15 16:11:24 -0700904
Andrew Leeb1903842014-05-15 16:11:24 -0700905 mSearchView.setText(null);
906 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700907 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700908
Andrew Leeb1903842014-05-15 16:11:24 -0700909 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Andrew Leeb1903842014-05-15 16:11:24 -0700910 if (mSmartDialSearchFragment != null) {
911 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700912 }
Andrew Leeb1903842014-05-15 16:11:24 -0700913 if (mRegularSearchFragment != null) {
914 transaction.remove(mRegularSearchFragment);
915 }
916 transaction.commit();
917
918 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Lee5253be02014-05-21 18:50:03 -0700919 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700920 }
921
922 /** Returns an Intent to launch Call Settings screen */
923 public static Intent getCallSettingsIntent() {
924 final Intent intent = new Intent(Intent.ACTION_MAIN);
925 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
926 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
927 return intent;
928 }
929
930 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700931 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700932 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700933 if (TextUtils.isEmpty(mSearchQuery)) {
934 exitSearchUi();
935 }
Yorke Leef6673d32013-08-23 15:34:17 -0700936 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700937 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700938 exitSearchUi();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700939 hideInputMethod(parentLayout);
Yorke Leec3766332013-07-31 11:13:16 -0700940 } else {
941 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700942 }
Yorke Leec3766332013-07-31 11:13:16 -0700943 }
944
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700945 /**
946 * @return True if the search UI was exited, false otherwise
947 */
948 private boolean maybeExitSearchUi() {
949 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
950 exitSearchUi();
951 hideInputMethod(parentLayout);
952 return true;
953 }
954 return false;
955 }
956
Yorke Leec3766332013-07-31 11:13:16 -0700957 @Override
958 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700959 if (mSmartDialSearchFragment != null) {
960 mSmartDialSearchFragment.setAddToContactNumber(query);
961 }
Yorke Leec3766332013-07-31 11:13:16 -0700962 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
963 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700964
965 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700966 if (DEBUG) {
967 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
968 }
969 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
970 // This callback can happen if the dialpad fragment is recreated because of
971 // activity destruction. In that case, don't update the search view because
972 // that would bring the user back to the search fragment regardless of the
973 // previous state of the application. Instead, just return here and let the
974 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700975 if (!TextUtils.isEmpty(normalizedQuery)) {
976 mPendingSearchViewQuery = normalizedQuery;
977 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700978 return;
979 }
Yorke Lee53a22302014-04-29 18:13:46 -0700980 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700981 }
982 }
Yorke Leec3766332013-07-31 11:13:16 -0700983
984 @Override
985 public void onListFragmentScrollStateChange(int scrollState) {
986 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700987 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700988 hideInputMethod(getCurrentFocus());
989 }
990 }
991
992 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700993 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700994 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700995 // TODO: No-op for now. This should eventually show/hide the actionBar based on
996 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700997 }
998
Yorke Lee86e21f72014-04-02 16:49:38 -0700999 @Override
Andrew Leea73e1892014-05-13 13:21:16 -07001000 public void setFloatingActionButtonVisible(boolean visible) {
Sai Cheemalapati41460652014-06-02 21:05:39 -07001001 mFloatingActionButtonController.setVisible(visible);
Yorke Leec3766332013-07-31 11:13:16 -07001002 }
1003
1004 private boolean phoneIsInUse() {
Santos Cordon1d7ef6a2014-05-29 21:46:33 -07001005 // TODO(santoscordon): Replace with a TelecommService method call.
1006 return getTelephonyManager().getCallState() != TelephonyManager.CALL_STATE_IDLE;
Yorke Leec3766332013-07-31 11:13:16 -07001007 }
Yorke Lee8dd62002013-08-08 15:57:20 -07001008
Yorke Leee1424812013-09-04 18:24:48 -07001009 public static Intent getAddNumberToContactIntent(CharSequence text) {
1010 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1011 intent.putExtra(Intents.Insert.PHONE, text);
1012 intent.setType(Contacts.CONTENT_ITEM_TYPE);
1013 return intent;
1014 }
1015
Yorke Leeda0f9042013-12-05 14:25:51 -08001016 private boolean canIntentBeHandled(Intent intent) {
1017 final PackageManager packageManager = getPackageManager();
1018 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1019 PackageManager.MATCH_DEFAULT_ONLY);
1020 return resolveInfo != null && resolveInfo.size() > 0;
1021 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001022
Yorke Lee6a1461a2014-04-21 16:27:14 -07001023 @Override
1024 public void showCallHistory() {
1025 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
1026 // CONTENT_TYPE, so that we always open our call log from our dialer
1027 final Intent intent = new Intent(this, CallLogActivity.class);
1028 startActivity(intent);
1029 }
1030
Yorke Lee86e21f72014-04-02 16:49:38 -07001031 /**
1032 * Called when the user has long-pressed a contact tile to start a drag operation.
1033 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001034 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001035 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Andrew Leee9e0ba62014-05-30 14:03:05 -07001036 if (mListsFragment.isPaneOpen()) {
1037 mActionBarController.slideActionBarUp(true);
1038 }
Yorke Lee28266192014-05-01 10:05:52 -07001039 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001040 }
1041
1042 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001043 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1044 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001045
Yorke Lee86e21f72014-04-02 16:49:38 -07001046 /**
1047 * Called when the user has released a contact tile after long-pressing it.
1048 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001049 @Override
1050 public void onDragFinished(int x, int y) {
Andrew Leee9e0ba62014-05-30 14:03:05 -07001051 if (mListsFragment.isPaneOpen()) {
1052 mActionBarController.slideActionBarDown(true);
1053 }
Yorke Lee28266192014-05-01 10:05:52 -07001054 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001055 }
1056
1057 @Override
1058 public void onDroppedOnRemove() {}
1059
1060 /**
Yorke Leed999b712014-04-23 15:10:58 -07001061 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001062 * once it has been attached to the activity.
1063 */
1064 @Override
1065 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001066 mDragDropController = dragController;
1067 ((RemoveView) findViewById(R.id.remove_view))
1068 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001069 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001070
1071 @Override
1072 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001073 // Specify call-origin so that users will see the previous tab instead of
1074 // CallLog screen (search UI will be automatically exited).
1075 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001076 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001077 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001078 }
1079
1080 @Override
1081 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001082 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
1083 startActivity(intent);
1084 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001085 }
1086
1087 @Override
1088 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001089 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001090 }
1091
1092 @Override
1093 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001094 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001095 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001096
Yorke Leecc4660d2014-04-24 11:22:57 -07001097 @Override
1098 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Sai Cheemalapati41460652014-06-02 21:05:39 -07001099 mFloatingActionButtonController.onPageScrolled(position, positionOffset);
Yorke Leecc4660d2014-04-24 11:22:57 -07001100 }
1101
1102 @Override
1103 public void onPageSelected(int position) {
1104 mCurrentTabPosition = position;
Sai Cheemalapati41460652014-06-02 21:05:39 -07001105 mFloatingActionButtonController.updateByTab(position);
Yorke Leecc4660d2014-04-24 11:22:57 -07001106 }
1107
1108 @Override
1109 public void onPageScrollStateChanged(int state) {
1110 }
1111
Santos Cordon1d7ef6a2014-05-29 21:46:33 -07001112 private TelephonyManager getTelephonyManager() {
1113 return (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
1114 }
1115
Yorke Lee5253be02014-05-21 18:50:03 -07001116 @Override
1117 public boolean isActionBarShowing() {
1118 return mActionBarController.isActionBarShowing();
1119 }
1120
Andrew Lee9da8fb42014-06-03 14:03:09 -07001121 public boolean isDialpadShown() {
1122 return mIsDialpadShown;
1123 }
1124
Yorke Lee5253be02014-05-21 18:50:03 -07001125 @Override
1126 public int getActionBarHideOffset() {
1127 return getActionBar().getHideOffset();
1128 }
1129
1130 @Override
1131 public int getActionBarHeight() {
1132 return mActionBarHeight;
1133 }
1134
1135 @Override
1136 public void setActionBarHideOffset(int hideOffset) {
1137 getActionBar().setHideOffset(hideOffset);
1138 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001139}