blob: 0adc27ddcdea81818a02e3e77b325e2c2ee902cf [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;
Yorke Lee33932ff2014-04-16 13:34:32 -070029import android.content.res.TypedArray;
Chiao Cheng94b10b52012-08-17 16:59:12 -070030import android.net.Uri;
31import android.os.Bundle;
32import android.os.RemoteException;
33import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070035import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070036import android.speech.RecognizerIntent;
Yorke Leecc4660d2014-04-24 11:22:57 -070037import android.support.v4.view.ViewPager;
Yorke Leec3766332013-07-31 11:13:16 -070038import android.telephony.TelephonyManager;
Yorke Lee53a22302014-04-29 18:13:46 -070039import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070040import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070041import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070042import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070043import android.view.DragEvent;
Andrew Leeb1903842014-05-15 16:11:24 -070044import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070047import android.view.MotionEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070049import android.view.View.OnDragListener;
Yorke Leec7b2a0e2014-05-20 17:21:25 -070050import android.view.View.OnTouchListener;
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;
Andrew Lee3c14bc32014-05-28 15:43:03 -070067import com.android.contacts.common.animation.AnimationListenerAdapter;
Yorke Leec3766332013-07-31 11:13:16 -070068import com.android.contacts.common.dialog.ClearFrequentsDialog;
69import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080070import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070071import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070073import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070074import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070075import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070076import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070077import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080078import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070079import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080080import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070081import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070082import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070083import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070084import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080085import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070086import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070087import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee5253be02014-05-21 18:50:03 -070088import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070089import com.android.dialer.widget.SearchEditTextLayout;
Yorke Lee11ca39e2014-05-19 20:31:37 -070090import com.android.dialer.widget.SearchEditTextLayout.OnBackButtonClickedListener;
Yorke Lee0baa98b2013-08-22 10:55:16 -070091import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070092import com.android.internal.telephony.ITelephony;
93
Yorke Leec3766332013-07-31 11:13:16 -070094import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080095import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070096
Chiao Cheng94b10b52012-08-17 16:59:12 -070097/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070098 * The dialer tab's title is 'phone', a more common name (see strings.xml).
99 */
Yorke Leec3766332013-07-31 11:13:16 -0700100public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700101 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700102 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -0800103 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700104 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700105 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700106 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700107 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700108 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700109 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700110 ViewPager.OnPageChangeListener,
111 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700112 private static final String TAG = "DialtactsActivity";
113
Ihab Awad526c0b82014-02-27 12:55:36 -0800114 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115
Yorke Leef74011e2013-08-02 11:30:30 -0700116 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
117
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118 /** Used to open Call Setting */
119 private static final String PHONE_PACKAGE = "com.android.phone";
120 private static final String CALL_SETTINGS_CLASS_NAME =
121 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700122 /** @see #getCallOrigin() */
123 private static final String CALL_ORIGIN_DIALTACTS =
124 "com.android.dialer.DialtactsActivity";
125
Yorke Leeb207f8a2013-08-29 18:51:06 -0700126 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
127 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700128 private static final String KEY_SEARCH_QUERY = "search_query";
129 private static final String KEY_FIRST_LAUNCH = "first_launch";
130
Yorke Leec3766332013-07-31 11:13:16 -0700131 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
132 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
133 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
134 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700135
Chiao Cheng94b10b52012-08-17 16:59:12 -0700136 /**
137 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
138 */
139 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
140
Yorke Leec3766332013-07-31 11:13:16 -0700141 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700142
Yorke Lee86e21f72014-04-02 16:49:38 -0700143 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800144
Andrew Lee0c667702014-05-12 14:31:45 -0700145 private RelativeLayout parentLayout;
146
Yorke Leec3766332013-07-31 11:13:16 -0700147 /**
Yorke Leec3766332013-07-31 11:13:16 -0700148 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700149 */
Yorke Leef74011e2013-08-02 11:30:30 -0700150 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700151
152 /**
153 * Fragment for searching phone numbers using the alphanumeric keyboard.
154 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700155 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700156
157 /**
158 * Fragment for searching phone numbers using the dialpad.
159 */
160 private SmartDialSearchFragment mSmartDialSearchFragment;
161
Yorke Leee00c9fe2014-04-12 12:42:06 -0700162 /**
163 * Fragment containing the speed dial list, recents list, and all contacts list.
164 */
165 private ListsFragment mListsFragment;
166
Andrew Leea73e1892014-05-13 13:21:16 -0700167 private View mFloatingActionButtonContainer;
168 private ImageButton mFloatingActionButton;
Yorke Leec3766332013-07-31 11:13:16 -0700169
Yorke Lee33932ff2014-04-16 13:34:32 -0700170 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700171 private boolean mInDialpadSearch;
172 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700173 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700174 private boolean mIsDialpadShown;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700175
Yorke Leeef2b7382013-08-09 17:39:25 -0700176 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700177 * The position of the currently selected tab in the attached {@link ListsFragment}.
178 */
179 private int mCurrentTabPosition = 0;
180
181 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700182 * True if the dialpad is only temporarily showing due to being in call
183 */
184 private boolean mInCallDialpadUp;
185
186 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700187 * True when this activity has been launched for the first time.
188 */
Yorke Lee98702de2013-08-06 12:03:32 -0700189 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700190
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700191 /**
192 * Search query to be applied to the SearchView in the ActionBar once
193 * onCreateOptionsMenu has been called.
194 */
195 private String mPendingSearchViewQuery;
196
Yorke Lee53a22302014-04-29 18:13:46 -0700197 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700198 private View mVoiceSearchButton;
Yorke Leeec489fb2014-05-19 15:39:27 -0700199 private SearchEditTextLayout mSearchEditTextLayout;
Andrew Leee74a10e2014-05-16 14:31:40 -0700200
Yorke Lee86e21f72014-04-02 16:49:38 -0700201 /**
202 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
203 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
204 * removed from the speed dial list.
205 */
Yorke Lee28266192014-05-01 10:05:52 -0700206 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700207
Yorke Lee86e21f72014-04-02 16:49:38 -0700208 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
209 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700210 private String mSearchQuery;
211
Yorke Leefce269a2013-08-12 11:56:04 -0700212 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700213 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700214 private ActionBarController mActionBarController;
Yorke Leefce269a2013-08-12 11:56:04 -0700215
Andrew Leee74a10e2014-05-16 14:31:40 -0700216 private class OptionsPopupMenu extends PopupMenu {
217 public OptionsPopupMenu(Context context, View anchor) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700218 super(context, anchor);
219 }
220
221 @Override
222 public void show() {
223 final Menu menu = getMenu();
224 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700225 clearFrequents.setVisible(mListsFragment != null &&
226 mListsFragment.getSpeedDialFragment() != null &&
227 mListsFragment.getSpeedDialFragment().hasFrequents());
Yorke Leee00c9fe2014-04-12 12:42:06 -0700228 super.show();
229 }
230 }
231
Chiao Cheng94b10b52012-08-17 16:59:12 -0700232 /**
Yorke Lee28266192014-05-01 10:05:52 -0700233 * Listener that listens to drag events and sends their x and y coordinates to a
234 * {@link DragDropController}.
235 */
236 private class LayoutOnDragListener implements OnDragListener {
237 @Override
238 public boolean onDrag(View v, DragEvent event) {
239 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
240 mDragDropController.handleDragHovered(v, (int) event.getX(),
241 (int) event.getY());
242 }
243 return true;
244 }
245 }
246
247 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700248 * Listener used to send search queries to the phone search fragment.
249 */
Yorke Lee53a22302014-04-29 18:13:46 -0700250 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700251 @Override
252 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
253 }
Yorke Leec3766332013-07-31 11:13:16 -0700254
Andrew Lee99a570c2014-05-15 14:18:50 -0700255 @Override
256 public void onTextChanged(CharSequence s, int start, int before, int count) {
257 final String newText = s.toString();
258 if (newText.equals(mSearchQuery)) {
259 // If the query hasn't changed (perhaps due to activity being destroyed
260 // and restored, or user launching the same DIAL intent twice), then there is
261 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700262 return;
263 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700264 mSearchQuery = newText;
265 if (DEBUG) {
266 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leec3766332013-07-31 11:13:16 -0700267 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700268
Andrew Lee90374a72014-05-16 15:01:09 -0700269 // Show search fragment only when the query string is changed to non-empty text.
270 if (!TextUtils.isEmpty(newText)) {
271 // Call enterSearchUi only if we are switching search modes, or showing a search
272 // fragment for the first time.
273 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
274 (!mIsDialpadShown && mInRegularSearch);
275 if (!sameSearchMode) {
276 enterSearchUi(mIsDialpadShown, mSearchQuery);
277 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700278 }
279
280 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700281 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700282 } else if (mRegularSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700283 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700284 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700285 }
286
287 @Override
288 public void afterTextChanged(Editable s) {
289 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700290 };
291
Andrew Leeb1903842014-05-15 16:11:24 -0700292
293 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700294 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700295 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700296 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700297 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700298 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700299 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700300 mActionBarController.onSearchBoxTapped();
Andrew Leeb1903842014-05-15 16:11:24 -0700301 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
302 }
Andrew Leeb1903842014-05-15 16:11:24 -0700303 }
304 };
305
306 /**
307 * If the search term is empty and the user closes the soft keyboard, close the search UI.
308 */
309 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
310 @Override
311 public boolean onKey(View v, int keyCode, KeyEvent event) {
312 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
313 TextUtils.isEmpty(mSearchView.getText().toString())) {
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700314 maybeExitSearchUi();
Andrew Leeb1903842014-05-15 16:11:24 -0700315 }
316 return false;
317 }
318 };
319
Chiao Cheng94b10b52012-08-17 16:59:12 -0700320 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700321 protected void onCreate(Bundle savedInstanceState) {
322 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700323 mFirstLaunch = true;
324
Yorke Lee8898cd02013-08-08 10:24:27 -0700325 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800326 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700327
Yorke Lee53a22302014-04-29 18:13:46 -0700328 final ActionBar actionBar = getActionBar();
329 actionBar.setCustomView(R.layout.search_edittext);
330 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700331 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700332
Yorke Lee5253be02014-05-21 18:50:03 -0700333 mActionBarController = new ActionBarController(this,
334 (SearchEditTextLayout) actionBar.getCustomView());
335
Yorke Leeec489fb2014-05-19 15:39:27 -0700336 mSearchEditTextLayout = (SearchEditTextLayout) actionBar.getCustomView();
337 mSearchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700338
Yorke Leeec489fb2014-05-19 15:39:27 -0700339 mSearchView = (EditText) mSearchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700340 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Yorke Lee11ca39e2014-05-19 20:31:37 -0700341 mVoiceSearchButton = mSearchEditTextLayout.findViewById(R.id.voice_search_button);
342 mSearchEditTextLayout.findViewById(R.id.search_box_start_search).setOnClickListener(
343 mSearchViewOnClickListener);
344 mSearchEditTextLayout.setOnBackButtonClickedListener(new OnBackButtonClickedListener() {
345 @Override
346 public void onBackButtonClicked() {
347 onBackPressed();
348 }
349 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700350
Yorke Leeec489fb2014-05-19 15:39:27 -0700351 ImageButton optionsMenuButton = (ImageButton) mSearchEditTextLayout.findViewById(
352 R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700353 optionsMenuButton.setOnClickListener(this);
354 final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
355 optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
356
Yorke Lee3443d4a2014-05-23 19:30:35 -0700357 mActionBarHeight = getResources().getDimensionPixelSize(R.dimen.action_bar_height);
Yorke Lee33932ff2014-04-16 13:34:32 -0700358
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);
Yorke Lee5253be02014-05-21 18:50:03 -0700371 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700372 }
373
Andrew Lee0c667702014-05-12 14:31:45 -0700374 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
375 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
376 parentLayout.setOnDragListener(new LayoutOnDragListener());
377
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700378 setupActivityOverlay();
379
Andrew Leea73e1892014-05-13 13:21:16 -0700380 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
381 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700382
Andrew Leea73e1892014-05-13 13:21:16 -0700383 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
384 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800385
Yorke Lee28266192014-05-01 10:05:52 -0700386 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800387
Yorke Lee0baa98b2013-08-22 10:55:16 -0700388 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700389 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700390 }
391
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700392 private void setupActivityOverlay() {
393 final View activityOverlay = findViewById(R.id.activity_overlay);
394 activityOverlay.setOnTouchListener(new OnTouchListener() {
395 @Override
396 public boolean onTouch(View v, MotionEvent event) {
397 if (!mIsDialpadShown) {
398 maybeExitSearchUi();
399 }
400 return false;
401 }
402 });
403 }
404
Chiao Cheng94b10b52012-08-17 16:59:12 -0700405 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700406 protected void onResume() {
407 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700408 if (mFirstLaunch) {
409 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700410 } else if (!phoneIsInUse() && mInCallDialpadUp) {
411 hideDialpadFragment(false, true);
412 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700413 }
414 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700415 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700416 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700417 }
418
419 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700420 protected void onPause() {
421 if (mClearSearchOnPause) {
422 hideDialpadAndSearchUi();
423 mClearSearchOnPause = false;
424 }
425 super.onPause();
426 }
427
428 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700429 protected void onSaveInstanceState(Bundle outState) {
430 super.onSaveInstanceState(outState);
431 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700432 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
433 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700434 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee5253be02014-05-21 18:50:03 -0700435 mActionBarController.saveInstanceState(outState);
Yorke Leeef2b7382013-08-09 17:39:25 -0700436 }
437
438 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700439 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700440 if (fragment instanceof DialpadFragment) {
441 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700442 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700443 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700444 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700445 } else if (fragment instanceof SmartDialSearchFragment) {
446 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700447 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700448 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700449 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700450 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700451 } else if (fragment instanceof ListsFragment) {
452 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700453 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700454 }
Yorke Leec3766332013-07-31 11:13:16 -0700455 }
456
Alon Albertb453e5b2013-09-10 15:54:23 -0700457 protected void handleMenuSettings() {
458 openTelephonySetting(this);
459 }
460
461 public static void openTelephonySetting(Activity activity) {
462 final Intent settingsIntent = getCallSettingsIntent();
463 activity.startActivity(settingsIntent);
464 }
465
Chiao Cheng94b10b52012-08-17 16:59:12 -0700466 @Override
467 public void onClick(View view) {
468 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700469 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700470 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700471 mInCallDialpadUp = false;
472 showDialpadFragment(true);
473 } else {
474 // Dial button was pressed; tell the Dialpad fragment
475 mDialpadFragment.dialButtonPressed();
476 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700477 break;
Yorke Leec3766332013-07-31 11:13:16 -0700478 case R.id.search_close_button:
479 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700480 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700481 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700482 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700483 }
484 break;
Yorke Leec3766332013-07-31 11:13:16 -0700485 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800486 try {
487 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
488 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
489 } catch (ActivityNotFoundException e) {
490 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
491 Toast.LENGTH_SHORT).show();
492 }
Yorke Leec3766332013-07-31 11:13:16 -0700493 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700494 case R.id.dialtacts_options_menu_button:
495 buildOptionsMenu(view).show();
496 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700497 default: {
498 Log.wtf(TAG, "Unexpected onClick event from " + view);
499 break;
500 }
501 }
502 }
503
Yorke Leec3766332013-07-31 11:13:16 -0700504 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700505 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700506 switch (item.getItemId()) {
507 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700508 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700509 break;
510 case R.id.menu_add_contact:
511 try {
512 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
513 } catch (ActivityNotFoundException e) {
514 Toast toast = Toast.makeText(this,
515 R.string.add_contact_not_available,
516 Toast.LENGTH_SHORT);
517 toast.show();
518 }
519 break;
520 case R.id.menu_import_export:
521 // We hard-code the "contactsAreAvailable" argument because doing it properly would
522 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
523 // now in Dialtacts for (potential) performance reasons. Compare with how it is
524 // done in {@link PeopleActivity}.
525 ImportExportDialogFragment.show(getFragmentManager(), true,
526 DialtactsActivity.class);
527 return true;
528 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700529 ClearFrequentsDialog.show(getFragmentManager());
530 return true;
531 case R.id.menu_call_settings:
532 handleMenuSettings();
533 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700534 }
535 return false;
536 }
537
538 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700539 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
540 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
541 if (resultCode == RESULT_OK) {
542 final ArrayList<String> matches = data.getStringArrayListExtra(
543 RecognizerIntent.EXTRA_RESULTS);
544 if (matches.size() > 0) {
545 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700546 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700547 } else {
548 Log.e(TAG, "Voice search - nothing heard");
549 }
550 } else {
551 Log.e(TAG, "Voice search failed");
552 }
553 }
554 super.onActivityResult(requestCode, resultCode, data);
555 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700556
Andrew Lee2a58ab82014-05-15 02:16:04 -0700557 /**
558 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
559 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
560 * @see #onDialpadShown
561 */
Yorke Leec3766332013-07-31 11:13:16 -0700562 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700563 if (mIsDialpadShown) {
564 return;
565 }
566 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700567 mDialpadFragment.setAnimate(animate);
568
Chiao Cheng94b10b52012-08-17 16:59:12 -0700569 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700570 ft.show(mDialpadFragment);
571 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700572
Yorke Lee5253be02014-05-21 18:50:03 -0700573 mActionBarController.onDialpadUp();
574
Andrew Leeb1903842014-05-15 16:11:24 -0700575 if (!isInSearchUi()) {
576 enterSearchUi(true /* isSmartDial */, mSearchQuery);
577 }
Yorke Leec3766332013-07-31 11:13:16 -0700578 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700579
Andrew Lee2a58ab82014-05-15 02:16:04 -0700580 /**
581 * Callback from child DialpadFragment when the dialpad is shown.
582 */
583 public void onDialpadShown() {
584 updateFloatingActionButton();
585 if (mDialpadFragment.getAnimate()) {
586 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
587 mDialpadFragment.getView().startAnimation(slideIn);
588 } else {
589 mDialpadFragment.setYFraction(0);
590 }
591
Andrew Lee2a58ab82014-05-15 02:16:04 -0700592 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700593 }
594
595 /**
596 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
597 * a callback after the hide animation ends.
598 * @see #commitDialpadFragmentHide
599 */
Yorke Leeca195042013-09-25 16:29:38 -0700600 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700601 if (mDialpadFragment == null) {
602 return;
603 }
Yorke Leef6673d32013-08-23 15:34:17 -0700604 if (clearDialpad) {
605 mDialpadFragment.clearDialpad();
606 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700607 if (!mIsDialpadShown) {
608 return;
Yorke Leec3766332013-07-31 11:13:16 -0700609 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700610 mIsDialpadShown = false;
611 mDialpadFragment.setAnimate(animate);
612
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700613 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700614 updateFloatingActionButton();
615 if (animate) {
616 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
Andrew Lee3c14bc32014-05-28 15:43:03 -0700617 slideOut.setAnimationListener(new AnimationListenerAdapter() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700618 @Override
619 public void onAnimationEnd(Animation animation) {
620 commitDialpadFragmentHide();
621 }
622 });
623 mDialpadFragment.getView().startAnimation(slideOut);
624 } else {
625 commitDialpadFragmentHide();
626 }
627
Yorke Lee5253be02014-05-21 18:50:03 -0700628 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700629
Andrew Leed4cde832014-05-16 15:56:48 -0700630 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700631 if (TextUtils.isEmpty(mSearchQuery)) {
632 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700633 }
Andrew Leed4cde832014-05-16 15:56:48 -0700634 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700635 }
636
637 /**
638 * Finishes hiding the dialpad fragment after any animations are completed.
639 */
640 private void commitDialpadFragmentHide() {
641 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700642 ft.hide(mDialpadFragment);
643 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700644 }
645
Andrew Lee2a58ab82014-05-15 02:16:04 -0700646 private void updateSearchFragmentPosition() {
647 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
648 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700649 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700650 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700651 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700652 fragment = mRegularSearchFragment;
653 }
654 if (fragment != null && fragment.isVisible()) {
655 fragment.getView().animate().translationY(translationValue)
656 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
657 }
658 }
659
Yorke Lee5253be02014-05-21 18:50:03 -0700660 @Override
661 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700662 return mInDialpadSearch || mInRegularSearch;
663 }
664
Yorke Lee5253be02014-05-21 18:50:03 -0700665 @Override
666 public boolean hasSearchQuery() {
667 return !TextUtils.isEmpty(mSearchQuery);
668 }
669
670 @Override
671 public boolean shouldShowActionBar() {
672 return mListsFragment.shouldShowActionBar();
673 }
674
Yorke Leeb207f8a2013-08-29 18:51:06 -0700675 private void setNotInSearchUi() {
676 mInDialpadSearch = false;
677 mInRegularSearch = false;
678 }
679
Yorke Lee311969c2013-08-26 06:31:11 -0700680 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700681 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700682 hideDialpadFragment(false, true);
683 }
684
Yorke Leee00c9fe2014-04-12 12:42:06 -0700685 private void hideInputMethod(View view) {
686 final InputMethodManager imm = (InputMethodManager) getSystemService(
687 Context.INPUT_METHOD_SERVICE);
688 if (imm != null && view != null) {
689 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
690 }
691 }
692
Yorke Lee53a22302014-04-29 18:13:46 -0700693 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700694 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
695 if (canIntentBeHandled(voiceIntent)) {
696 mVoiceSearchButton.setVisibility(View.VISIBLE);
697 mVoiceSearchButton.setOnClickListener(this);
698 } else {
699 mVoiceSearchButton.setVisibility(View.GONE);
700 }
701 }
702
Andrew Leee74a10e2014-05-16 14:31:40 -0700703 private OptionsPopupMenu buildOptionsMenu(View invoker) {
704 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
705 popupMenu.inflate(R.menu.dialtacts_options);
706 popupMenu.setOnMenuItemClickListener(this);
707 return popupMenu;
708 }
709
Yorke Lee86e21f72014-04-02 16:49:38 -0700710 @Override
711 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700712 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700713 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700714 mPendingSearchViewQuery = null;
715 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700716 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700717 }
718
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719 /**
720 * Returns true if the intent is due to hitting the green send key (hardware call button:
721 * KEYCODE_CALL) while in a call.
722 *
723 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700724 * @return true if the intent is due to hitting the green send key while in a call
725 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700726 private boolean isSendKeyWhileInCall(Intent intent) {
727 // If there is a call in progress and the user launched the dialer by hitting the call
728 // button, go straight to the in-call screen.
729 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730
Yorke Lee62e995c2014-03-28 10:02:56 -0700731 try {
732 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
733 if (callKey && phone != null && phone.showCallScreen()) {
734 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700735 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700736 } catch (RemoteException e) {
737 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700738 }
739
740 return false;
741 }
742
743 /**
744 * Sets the current tab based on the intent's request type
745 *
746 * @param intent Intent that contains information about which tab should be selected
747 */
Yorke Leec3766332013-07-31 11:13:16 -0700748 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700749 // 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 -0700750 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700751 finish();
752 return;
753 }
754
Yorke Lee669b6082013-09-17 15:43:38 -0700755 if (mDialpadFragment != null) {
756 final boolean phoneIsInUse = phoneIsInUse();
757 if (phoneIsInUse || isDialIntent(intent)) {
758 mDialpadFragment.setStartedFromNewIntent(true);
759 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
760 mInCallDialpadUp = true;
761 }
762 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700763 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700765 }
766
767 @Override
768 public void onNewIntent(Intent newIntent) {
769 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700770 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700771
Chiao Cheng94b10b52012-08-17 16:59:12 -0700772 invalidateOptionsMenu();
773 }
774
775 /** Returns true if the given intent contains a phone number to populate the dialer with */
776 private boolean isDialIntent(Intent intent) {
777 final String action = intent.getAction();
778 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
779 return true;
780 }
781 if (Intent.ACTION_VIEW.equals(action)) {
782 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700783 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700784 return true;
785 }
786 }
787 return false;
788 }
789
790 /**
791 * Returns an appropriate call origin for this Activity. May return null when no call origin
792 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
793 * for remembering the tab in which the user made a phone call, so the external app's DIAL
794 * request should not be counted.)
795 */
796 public String getCallOrigin() {
797 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
798 }
799
Chiao Cheng94b10b52012-08-17 16:59:12 -0700800 /**
Yorke Leec3766332013-07-31 11:13:16 -0700801 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700802 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700803 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700804 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700805 // Weird race condition where fragment is doing work after the activity is destroyed
806 // due to talkback being on (b/10209937). Just return since we can't do any
807 // constructive here.
808 return;
809 }
810
Yorke Leeef2b7382013-08-09 17:39:25 -0700811 if (DEBUG) {
812 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
813 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700814
Yorke Leec3766332013-07-31 11:13:16 -0700815 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700816 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700817 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700818 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700819 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700820 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700821
Yorke Leeb207f8a2013-08-29 18:51:06 -0700822 final String tag;
823 if (smartDialSearch) {
824 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
825 } else {
826 tag = TAG_REGULAR_SEARCH_FRAGMENT;
827 }
828 mInDialpadSearch = smartDialSearch;
829 mInRegularSearch = !smartDialSearch;
830
Andrew Lee752956e2014-05-15 11:43:38 -0700831 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700832 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700833 if (fragment == null) {
834 if (smartDialSearch) {
835 fragment = new SmartDialSearchFragment();
836 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700837 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700838 }
Andrew Leeb1903842014-05-15 16:11:24 -0700839 transaction.add(R.id.dialtacts_frame, fragment, tag);
840 } else {
841 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700842 }
Andrew Leeb1903842014-05-15 16:11:24 -0700843
Yorke Lee86e21f72014-04-02 16:49:38 -0700844 // DialtactsActivity will provide the options menu
845 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700846 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700847 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700848 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700849
850 mListsFragment.getView().animate().alpha(0).withLayer();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700851 }
852
853 /**
Yorke Leec3766332013-07-31 11:13:16 -0700854 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700855 */
Yorke Leec3766332013-07-31 11:13:16 -0700856 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700857 // See related bug in enterSearchUI();
858 if (getFragmentManager().isDestroyed()) {
859 return;
860 }
Andrew Leeb1903842014-05-15 16:11:24 -0700861
Andrew Leeb1903842014-05-15 16:11:24 -0700862 mSearchView.setText(null);
863 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700864 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700865
Andrew Leeb1903842014-05-15 16:11:24 -0700866 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
867 transaction.setCustomAnimations(0, android.R.animator.fade_out);
Andrew Leeb1903842014-05-15 16:11:24 -0700868 if (mSmartDialSearchFragment != null) {
869 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700870 }
Andrew Leeb1903842014-05-15 16:11:24 -0700871 if (mRegularSearchFragment != null) {
872 transaction.remove(mRegularSearchFragment);
873 }
874 transaction.commit();
875
876 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Lee5253be02014-05-21 18:50:03 -0700877 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700878 }
879
880 /** Returns an Intent to launch Call Settings screen */
881 public static Intent getCallSettingsIntent() {
882 final Intent intent = new Intent(Intent.ACTION_MAIN);
883 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
884 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
885 return intent;
886 }
887
888 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700889 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700890 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700891 if (TextUtils.isEmpty(mSearchQuery)) {
892 exitSearchUi();
893 }
Yorke Leef6673d32013-08-23 15:34:17 -0700894 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700895 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700896 exitSearchUi();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700897 hideInputMethod(parentLayout);
Yorke Leec3766332013-07-31 11:13:16 -0700898 } else {
899 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700900 }
Yorke Leec3766332013-07-31 11:13:16 -0700901 }
902
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700903 /**
904 * @return True if the search UI was exited, false otherwise
905 */
906 private boolean maybeExitSearchUi() {
907 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
908 exitSearchUi();
909 hideInputMethod(parentLayout);
910 return true;
911 }
912 return false;
913 }
914
Yorke Leec3766332013-07-31 11:13:16 -0700915 @Override
916 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700917 if (mSmartDialSearchFragment != null) {
918 mSmartDialSearchFragment.setAddToContactNumber(query);
919 }
Yorke Leec3766332013-07-31 11:13:16 -0700920 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
921 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700922
923 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700924 if (DEBUG) {
925 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
926 }
927 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
928 // This callback can happen if the dialpad fragment is recreated because of
929 // activity destruction. In that case, don't update the search view because
930 // that would bring the user back to the search fragment regardless of the
931 // previous state of the application. Instead, just return here and let the
932 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700933 if (!TextUtils.isEmpty(normalizedQuery)) {
934 mPendingSearchViewQuery = normalizedQuery;
935 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700936 return;
937 }
Yorke Lee53a22302014-04-29 18:13:46 -0700938 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700939 }
940 }
Yorke Leec3766332013-07-31 11:13:16 -0700941
942 @Override
943 public void onListFragmentScrollStateChange(int scrollState) {
944 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700945 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700946 hideInputMethod(getCurrentFocus());
947 }
948 }
949
950 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700951 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700952 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700953 // TODO: No-op for now. This should eventually show/hide the actionBar based on
954 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700955 }
956
Yorke Lee86e21f72014-04-02 16:49:38 -0700957 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700958 public void setFloatingActionButtonVisible(boolean visible) {
959 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700960 }
961
962 private boolean phoneIsInUse() {
963 final TelephonyManager tm = (TelephonyManager) getSystemService(
964 Context.TELEPHONY_SERVICE);
965 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
966 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700967
Yorke Leee1424812013-09-04 18:24:48 -0700968 public static Intent getAddNumberToContactIntent(CharSequence text) {
969 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
970 intent.putExtra(Intents.Insert.PHONE, text);
971 intent.setType(Contacts.CONTENT_ITEM_TYPE);
972 return intent;
973 }
974
Yorke Leeda0f9042013-12-05 14:25:51 -0800975 private boolean canIntentBeHandled(Intent intent) {
976 final PackageManager packageManager = getPackageManager();
977 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
978 PackageManager.MATCH_DEFAULT_ONLY);
979 return resolveInfo != null && resolveInfo.size() > 0;
980 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800981
Yorke Lee6a1461a2014-04-21 16:27:14 -0700982 @Override
983 public void showCallHistory() {
984 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
985 // CONTENT_TYPE, so that we always open our call log from our dialer
986 final Intent intent = new Intent(this, CallLogActivity.class);
987 startActivity(intent);
988 }
989
Yorke Lee86e21f72014-04-02 16:49:38 -0700990 /**
991 * Called when the user has long-pressed a contact tile to start a drag operation.
992 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800993 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700994 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee5253be02014-05-21 18:50:03 -0700995 mActionBarController.slideActionBarUp(true);
Yorke Lee28266192014-05-01 10:05:52 -0700996 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800997 }
998
999 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001000 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1001 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001002
Yorke Lee86e21f72014-04-02 16:49:38 -07001003 /**
1004 * Called when the user has released a contact tile after long-pressing it.
1005 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001006 @Override
1007 public void onDragFinished(int x, int y) {
Yorke Lee5253be02014-05-21 18:50:03 -07001008 mActionBarController.slideActionBarDown(true);
Yorke Lee28266192014-05-01 10:05:52 -07001009 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001010 }
1011
1012 @Override
1013 public void onDroppedOnRemove() {}
1014
1015 /**
Yorke Leed999b712014-04-23 15:10:58 -07001016 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001017 * once it has been attached to the activity.
1018 */
1019 @Override
1020 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001021 mDragDropController = dragController;
1022 ((RemoveView) findViewById(R.id.remove_view))
1023 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001024 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001025
1026 @Override
1027 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001028 // Specify call-origin so that users will see the previous tab instead of
1029 // CallLog screen (search UI will be automatically exited).
1030 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001031 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001032 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001033 }
1034
1035 @Override
1036 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001037 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
1038 startActivity(intent);
1039 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001040 }
1041
1042 @Override
1043 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001044 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001045 }
1046
1047 @Override
1048 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001049 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001050 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001051
Yorke Leecc4660d2014-04-24 11:22:57 -07001052 @Override
1053 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1054
1055 }
1056
1057 @Override
1058 public void onPageSelected(int position) {
1059 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001060 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001061 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001062 alignFloatingActionButtonByTab(mCurrentTabPosition);
1063 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001064 }
1065
1066 @Override
1067 public void onPageScrollStateChanged(int state) {
1068 }
1069
Andrew Lee2a58ab82014-05-15 02:16:04 -07001070 private void updateFloatingActionButton() {
1071 if (mIsDialpadShown) {
1072 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1073 mFloatingActionButton.setContentDescription(
1074 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001075 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001076 } else {
1077 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1078 mFloatingActionButton.setContentDescription(
1079 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001080 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001081 }
1082 }
1083
Yorke Leecc4660d2014-04-24 11:22:57 -07001084 private void alignFloatingActionButtonByTab(int position) {
1085 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1086 alignFloatingActionButtonMiddle();
1087 } else {
1088 alignFloatingActionButtonRight();
1089 }
1090 }
1091
1092 private void alignFloatingActionButtonRight() {
1093 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001094 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001095 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1096 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001097 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001098 }
1099
1100 private void alignFloatingActionButtonMiddle() {
1101 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001102 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001103 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1104 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001105 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001106 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001107
Yorke Lee5253be02014-05-21 18:50:03 -07001108 @Override
1109 public boolean isActionBarShowing() {
1110 return mActionBarController.isActionBarShowing();
1111 }
1112
1113 @Override
1114 public int getActionBarHideOffset() {
1115 return getActionBar().getHideOffset();
1116 }
1117
1118 @Override
1119 public int getActionBarHeight() {
1120 return mActionBarHeight;
1121 }
1122
1123 @Override
1124 public void setActionBarHideOffset(int hideOffset) {
1125 getActionBar().setHideOffset(hideOffset);
1126 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001127}