blob: 36ae894fd3539772662c3f29fe9b18c5874823d7 [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;
Yorke Leec3766332013-07-31 11:13:16 -070067import com.android.contacts.common.dialog.ClearFrequentsDialog;
68import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080069import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070070import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070072import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070073import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070074import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070075import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070076import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080077import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070078import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080079import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070080import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070081import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070082import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070083import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080084import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070085import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070086import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee5253be02014-05-21 18:50:03 -070087import com.android.dialer.widget.ActionBarController;
Andrew Leeb1903842014-05-15 16:11:24 -070088import com.android.dialer.widget.SearchEditTextLayout;
Yorke Lee11ca39e2014-05-19 20:31:37 -070089import com.android.dialer.widget.SearchEditTextLayout.OnBackButtonClickedListener;
Yorke Lee0baa98b2013-08-22 10:55:16 -070090import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070091import com.android.internal.telephony.ITelephony;
92
Yorke Leec3766332013-07-31 11:13:16 -070093import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080094import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070095
Chiao Cheng94b10b52012-08-17 16:59:12 -070096/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070097 * The dialer tab's title is 'phone', a more common name (see strings.xml).
98 */
Yorke Leec3766332013-07-31 11:13:16 -070099public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700100 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700101 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -0800102 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700103 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700104 SpeedDialFragment.HostInterface,
Yorke Lee5253be02014-05-21 18:50:03 -0700105 SearchFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700106 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700107 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700108 PopupMenu.OnMenuItemClickListener,
Yorke Lee5253be02014-05-21 18:50:03 -0700109 ViewPager.OnPageChangeListener,
110 ActionBarController.ActivityUi {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700111 private static final String TAG = "DialtactsActivity";
112
Ihab Awad526c0b82014-02-27 12:55:36 -0800113 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700114
Yorke Leef74011e2013-08-02 11:30:30 -0700115 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
116
Chiao Cheng94b10b52012-08-17 16:59:12 -0700117 /** Used to open Call Setting */
118 private static final String PHONE_PACKAGE = "com.android.phone";
119 private static final String CALL_SETTINGS_CLASS_NAME =
120 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121 /** @see #getCallOrigin() */
122 private static final String CALL_ORIGIN_DIALTACTS =
123 "com.android.dialer.DialtactsActivity";
124
Yorke Leeb207f8a2013-08-29 18:51:06 -0700125 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
126 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700127 private static final String KEY_SEARCH_QUERY = "search_query";
128 private static final String KEY_FIRST_LAUNCH = "first_launch";
Yorke Lee50aba882014-05-28 20:04:31 -0700129 private static final String KEY_IS_DIALPAD_SHOWN = "is_dialpad_shown";
Yorke Leeef2b7382013-08-09 17:39:25 -0700130
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 Lee50aba882014-05-28 20:04:31 -0700175 private boolean mShowDialpadOnResume;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700176
Yorke Leeef2b7382013-08-09 17:39:25 -0700177 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700178 * The position of the currently selected tab in the attached {@link ListsFragment}.
179 */
180 private int mCurrentTabPosition = 0;
181
182 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700183 * True if the dialpad is only temporarily showing due to being in call
184 */
185 private boolean mInCallDialpadUp;
186
187 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700188 * True when this activity has been launched for the first time.
189 */
Yorke Lee98702de2013-08-06 12:03:32 -0700190 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700191
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700192 /**
193 * Search query to be applied to the SearchView in the ActionBar once
194 * onCreateOptionsMenu has been called.
195 */
196 private String mPendingSearchViewQuery;
197
Yorke Lee53a22302014-04-29 18:13:46 -0700198 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700199 private View mVoiceSearchButton;
Yorke Leeec489fb2014-05-19 15:39:27 -0700200 private SearchEditTextLayout mSearchEditTextLayout;
Andrew Leee74a10e2014-05-16 14:31:40 -0700201
Yorke Lee86e21f72014-04-02 16:49:38 -0700202 /**
203 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
204 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
205 * removed from the speed dial list.
206 */
Yorke Lee28266192014-05-01 10:05:52 -0700207 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700208
Yorke Lee86e21f72014-04-02 16:49:38 -0700209 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
210 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700211 private String mSearchQuery;
212
Yorke Leefce269a2013-08-12 11:56:04 -0700213 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700214 private DragDropController mDragDropController;
Yorke Lee5253be02014-05-21 18:50:03 -0700215 private ActionBarController mActionBarController;
Yorke Leefce269a2013-08-12 11:56:04 -0700216
Andrew Leee74a10e2014-05-16 14:31:40 -0700217 private class OptionsPopupMenu extends PopupMenu {
218 public OptionsPopupMenu(Context context, View anchor) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700219 super(context, anchor);
220 }
221
222 @Override
223 public void show() {
224 final Menu menu = getMenu();
225 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700226 clearFrequents.setVisible(mListsFragment != null &&
227 mListsFragment.getSpeedDialFragment() != null &&
228 mListsFragment.getSpeedDialFragment().hasFrequents());
Yorke Leee00c9fe2014-04-12 12:42:06 -0700229 super.show();
230 }
231 }
232
Chiao Cheng94b10b52012-08-17 16:59:12 -0700233 /**
Yorke Lee28266192014-05-01 10:05:52 -0700234 * Listener that listens to drag events and sends their x and y coordinates to a
235 * {@link DragDropController}.
236 */
237 private class LayoutOnDragListener implements OnDragListener {
238 @Override
239 public boolean onDrag(View v, DragEvent event) {
240 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
241 mDragDropController.handleDragHovered(v, (int) event.getX(),
242 (int) event.getY());
243 }
244 return true;
245 }
246 }
247
248 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700249 * Listener used to send search queries to the phone search fragment.
250 */
Yorke Lee53a22302014-04-29 18:13:46 -0700251 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700252 @Override
253 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
254 }
Yorke Leec3766332013-07-31 11:13:16 -0700255
Andrew Lee99a570c2014-05-15 14:18:50 -0700256 @Override
257 public void onTextChanged(CharSequence s, int start, int before, int count) {
258 final String newText = s.toString();
259 if (newText.equals(mSearchQuery)) {
260 // If the query hasn't changed (perhaps due to activity being destroyed
261 // and restored, or user launching the same DIAL intent twice), then there is
262 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700263 return;
264 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700265 mSearchQuery = newText;
266 if (DEBUG) {
267 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leec3766332013-07-31 11:13:16 -0700268 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700269
Andrew Lee90374a72014-05-16 15:01:09 -0700270 // Show search fragment only when the query string is changed to non-empty text.
271 if (!TextUtils.isEmpty(newText)) {
272 // Call enterSearchUi only if we are switching search modes, or showing a search
273 // fragment for the first time.
274 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
275 (!mIsDialpadShown && mInRegularSearch);
276 if (!sameSearchMode) {
277 enterSearchUi(mIsDialpadShown, mSearchQuery);
278 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700279 }
280
281 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700282 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700283 } else if (mRegularSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700284 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700285 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700286 }
287
288 @Override
289 public void afterTextChanged(Editable s) {
290 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291 };
292
Andrew Leeb1903842014-05-15 16:11:24 -0700293
294 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700295 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700296 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700297 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700298 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700299 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700300 if (!isInSearchUi()) {
Yorke Lee5253be02014-05-21 18:50:03 -0700301 mActionBarController.onSearchBoxTapped();
Andrew Leeb1903842014-05-15 16:11:24 -0700302 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
303 }
Andrew Leeb1903842014-05-15 16:11:24 -0700304 }
305 };
306
307 /**
308 * If the search term is empty and the user closes the soft keyboard, close the search UI.
309 */
310 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
311 @Override
312 public boolean onKey(View v, int keyCode, KeyEvent event) {
313 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
314 TextUtils.isEmpty(mSearchView.getText().toString())) {
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700315 maybeExitSearchUi();
Andrew Leeb1903842014-05-15 16:11:24 -0700316 }
317 return false;
318 }
319 };
320
Chiao Cheng94b10b52012-08-17 16:59:12 -0700321 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700322 protected void onCreate(Bundle savedInstanceState) {
323 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700324 mFirstLaunch = true;
325
Yorke Lee8898cd02013-08-08 10:24:27 -0700326 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800327 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700328
Yorke Lee53a22302014-04-29 18:13:46 -0700329 final ActionBar actionBar = getActionBar();
330 actionBar.setCustomView(R.layout.search_edittext);
331 actionBar.setDisplayShowCustomEnabled(true);
Yorke Leebcef9402014-05-21 15:33:00 -0700332 actionBar.setBackgroundDrawable(null);
Yorke Lee53a22302014-04-29 18:13:46 -0700333
Yorke Lee5253be02014-05-21 18:50:03 -0700334 mActionBarController = new ActionBarController(this,
335 (SearchEditTextLayout) actionBar.getCustomView());
336
Yorke Leeec489fb2014-05-19 15:39:27 -0700337 mSearchEditTextLayout = (SearchEditTextLayout) actionBar.getCustomView();
338 mSearchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700339
Yorke Leeec489fb2014-05-19 15:39:27 -0700340 mSearchView = (EditText) mSearchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700341 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Yorke Lee11ca39e2014-05-19 20:31:37 -0700342 mVoiceSearchButton = mSearchEditTextLayout.findViewById(R.id.voice_search_button);
343 mSearchEditTextLayout.findViewById(R.id.search_box_start_search).setOnClickListener(
344 mSearchViewOnClickListener);
345 mSearchEditTextLayout.setOnBackButtonClickedListener(new OnBackButtonClickedListener() {
346 @Override
347 public void onBackButtonClicked() {
348 onBackPressed();
349 }
350 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700351
Yorke Leeec489fb2014-05-19 15:39:27 -0700352 ImageButton optionsMenuButton = (ImageButton) mSearchEditTextLayout.findViewById(
353 R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700354 optionsMenuButton.setOnClickListener(this);
355 final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
356 optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
357
Yorke Lee3443d4a2014-05-23 19:30:35 -0700358 mActionBarHeight = getResources().getDimensionPixelSize(R.dimen.action_bar_height);
Yorke Lee33932ff2014-04-16 13:34:32 -0700359
Yorke Leeef2b7382013-08-09 17:39:25 -0700360 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
361 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700362 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800363 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700364 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800365 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
366 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700367 } else {
368 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700369 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
370 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700371 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Yorke Lee50aba882014-05-28 20:04:31 -0700372 mShowDialpadOnResume = savedInstanceState.getBoolean(KEY_IS_DIALPAD_SHOWN);
Yorke Lee5253be02014-05-21 18:50:03 -0700373 mActionBarController.restoreInstanceState(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700374 }
375
Andrew Lee0c667702014-05-12 14:31:45 -0700376 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
377 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
378 parentLayout.setOnDragListener(new LayoutOnDragListener());
379
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700380 setupActivityOverlay();
381
Andrew Leea73e1892014-05-13 13:21:16 -0700382 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
383 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700384
Andrew Leea73e1892014-05-13 13:21:16 -0700385 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
386 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800387
Yorke Lee28266192014-05-01 10:05:52 -0700388 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800389
Yorke Lee0baa98b2013-08-22 10:55:16 -0700390 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700391 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700392 }
393
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700394 private void setupActivityOverlay() {
395 final View activityOverlay = findViewById(R.id.activity_overlay);
396 activityOverlay.setOnTouchListener(new OnTouchListener() {
397 @Override
398 public boolean onTouch(View v, MotionEvent event) {
399 if (!mIsDialpadShown) {
400 maybeExitSearchUi();
401 }
402 return false;
403 }
404 });
405 }
406
Chiao Cheng94b10b52012-08-17 16:59:12 -0700407 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700408 protected void onResume() {
409 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700410 if (mFirstLaunch) {
411 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700412 } else if (!phoneIsInUse() && mInCallDialpadUp) {
413 hideDialpadFragment(false, true);
414 mInCallDialpadUp = false;
Yorke Lee50aba882014-05-28 20:04:31 -0700415 } else if (mShowDialpadOnResume) {
416 showDialpadFragment(false);
417 mShowDialpadOnResume = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700418 }
419 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700420 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700421 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700422 }
423
424 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700425 protected void onPause() {
426 if (mClearSearchOnPause) {
427 hideDialpadAndSearchUi();
428 mClearSearchOnPause = false;
429 }
430 super.onPause();
431 }
432
433 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700434 protected void onSaveInstanceState(Bundle outState) {
435 super.onSaveInstanceState(outState);
436 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700437 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
438 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700439 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
Yorke Lee50aba882014-05-28 20:04:31 -0700440 outState.putBoolean(KEY_IS_DIALPAD_SHOWN, mIsDialpadShown);
Yorke Lee5253be02014-05-21 18:50:03 -0700441 mActionBarController.saveInstanceState(outState);
Yorke Leeef2b7382013-08-09 17:39:25 -0700442 }
443
444 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700445 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700446 if (fragment instanceof DialpadFragment) {
447 mDialpadFragment = (DialpadFragment) fragment;
Yorke Lee50aba882014-05-28 20:04:31 -0700448 if (!mShowDialpadOnResume) {
449 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
450 transaction.hide(mDialpadFragment);
451 transaction.commit();
452 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700453 } else if (fragment instanceof SmartDialSearchFragment) {
454 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700455 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700456 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700457 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700458 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700459 } else if (fragment instanceof ListsFragment) {
460 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700461 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700462 }
Yorke Leec3766332013-07-31 11:13:16 -0700463 }
464
Alon Albertb453e5b2013-09-10 15:54:23 -0700465 protected void handleMenuSettings() {
466 openTelephonySetting(this);
467 }
468
469 public static void openTelephonySetting(Activity activity) {
470 final Intent settingsIntent = getCallSettingsIntent();
471 activity.startActivity(settingsIntent);
472 }
473
Chiao Cheng94b10b52012-08-17 16:59:12 -0700474 @Override
475 public void onClick(View view) {
476 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700477 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700478 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700479 mInCallDialpadUp = false;
480 showDialpadFragment(true);
481 } else {
482 // Dial button was pressed; tell the Dialpad fragment
483 mDialpadFragment.dialButtonPressed();
484 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700485 break;
Yorke Leec3766332013-07-31 11:13:16 -0700486 case R.id.search_close_button:
487 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700488 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700489 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700490 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700491 }
492 break;
Yorke Leec3766332013-07-31 11:13:16 -0700493 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800494 try {
495 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
496 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
497 } catch (ActivityNotFoundException e) {
498 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
499 Toast.LENGTH_SHORT).show();
500 }
Yorke Leec3766332013-07-31 11:13:16 -0700501 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700502 case R.id.dialtacts_options_menu_button:
503 buildOptionsMenu(view).show();
504 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700505 default: {
506 Log.wtf(TAG, "Unexpected onClick event from " + view);
507 break;
508 }
509 }
510 }
511
Yorke Leec3766332013-07-31 11:13:16 -0700512 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700513 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700514 switch (item.getItemId()) {
515 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700516 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700517 break;
518 case R.id.menu_add_contact:
519 try {
520 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
521 } catch (ActivityNotFoundException e) {
522 Toast toast = Toast.makeText(this,
523 R.string.add_contact_not_available,
524 Toast.LENGTH_SHORT);
525 toast.show();
526 }
527 break;
528 case R.id.menu_import_export:
529 // We hard-code the "contactsAreAvailable" argument because doing it properly would
530 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
531 // now in Dialtacts for (potential) performance reasons. Compare with how it is
532 // done in {@link PeopleActivity}.
533 ImportExportDialogFragment.show(getFragmentManager(), true,
534 DialtactsActivity.class);
535 return true;
536 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700537 ClearFrequentsDialog.show(getFragmentManager());
538 return true;
539 case R.id.menu_call_settings:
540 handleMenuSettings();
541 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700542 }
543 return false;
544 }
545
546 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700547 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
548 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
549 if (resultCode == RESULT_OK) {
550 final ArrayList<String> matches = data.getStringArrayListExtra(
551 RecognizerIntent.EXTRA_RESULTS);
552 if (matches.size() > 0) {
553 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700554 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700555 } else {
556 Log.e(TAG, "Voice search - nothing heard");
557 }
558 } else {
559 Log.e(TAG, "Voice search failed");
560 }
561 }
562 super.onActivityResult(requestCode, resultCode, data);
563 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700564
Andrew Lee2a58ab82014-05-15 02:16:04 -0700565 /**
566 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
567 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
568 * @see #onDialpadShown
569 */
Yorke Leec3766332013-07-31 11:13:16 -0700570 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700571 if (mIsDialpadShown) {
572 return;
573 }
574 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700575 mDialpadFragment.setAnimate(animate);
576
Chiao Cheng94b10b52012-08-17 16:59:12 -0700577 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700578 ft.show(mDialpadFragment);
579 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700580
Yorke Lee5253be02014-05-21 18:50:03 -0700581 mActionBarController.onDialpadUp();
582
Andrew Leeb1903842014-05-15 16:11:24 -0700583 if (!isInSearchUi()) {
584 enterSearchUi(true /* isSmartDial */, mSearchQuery);
585 }
Yorke Leec3766332013-07-31 11:13:16 -0700586 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700587
Andrew Lee2a58ab82014-05-15 02:16:04 -0700588 /**
589 * Callback from child DialpadFragment when the dialpad is shown.
590 */
591 public void onDialpadShown() {
592 updateFloatingActionButton();
593 if (mDialpadFragment.getAnimate()) {
594 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
595 mDialpadFragment.getView().startAnimation(slideIn);
596 } else {
597 mDialpadFragment.setYFraction(0);
598 }
599
Andrew Lee2a58ab82014-05-15 02:16:04 -0700600 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700601 }
602
603 /**
604 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
605 * a callback after the hide animation ends.
606 * @see #commitDialpadFragmentHide
607 */
Yorke Leeca195042013-09-25 16:29:38 -0700608 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700609 if (mDialpadFragment == null) {
610 return;
611 }
Yorke Leef6673d32013-08-23 15:34:17 -0700612 if (clearDialpad) {
613 mDialpadFragment.clearDialpad();
614 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700615 if (!mIsDialpadShown) {
616 return;
Yorke Leec3766332013-07-31 11:13:16 -0700617 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700618 mIsDialpadShown = false;
619 mDialpadFragment.setAnimate(animate);
620
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700621 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700622 updateFloatingActionButton();
623 if (animate) {
624 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
625 slideOut.setAnimationListener(new ActivityAnimationListener() {
626 @Override
627 public void onAnimationEnd(Animation animation) {
628 commitDialpadFragmentHide();
629 }
630 });
631 mDialpadFragment.getView().startAnimation(slideOut);
632 } else {
633 commitDialpadFragmentHide();
634 }
635
Yorke Lee5253be02014-05-21 18:50:03 -0700636 mActionBarController.onDialpadDown();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700637
Andrew Leed4cde832014-05-16 15:56:48 -0700638 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700639 if (TextUtils.isEmpty(mSearchQuery)) {
640 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700641 }
Andrew Leed4cde832014-05-16 15:56:48 -0700642 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700643 }
644
645 /**
646 * Finishes hiding the dialpad fragment after any animations are completed.
647 */
648 private void commitDialpadFragmentHide() {
649 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700650 ft.hide(mDialpadFragment);
651 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700652 }
653
Andrew Lee2a58ab82014-05-15 02:16:04 -0700654 private void updateSearchFragmentPosition() {
655 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
656 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700657 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700658 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700659 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700660 fragment = mRegularSearchFragment;
661 }
662 if (fragment != null && fragment.isVisible()) {
663 fragment.getView().animate().translationY(translationValue)
664 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
665 }
666 }
667
Yorke Lee5253be02014-05-21 18:50:03 -0700668 @Override
669 public boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700670 return mInDialpadSearch || mInRegularSearch;
671 }
672
Yorke Lee5253be02014-05-21 18:50:03 -0700673 @Override
674 public boolean hasSearchQuery() {
675 return !TextUtils.isEmpty(mSearchQuery);
676 }
677
678 @Override
679 public boolean shouldShowActionBar() {
680 return mListsFragment.shouldShowActionBar();
681 }
682
Yorke Leeb207f8a2013-08-29 18:51:06 -0700683 private void setNotInSearchUi() {
684 mInDialpadSearch = false;
685 mInRegularSearch = false;
686 }
687
Yorke Lee311969c2013-08-26 06:31:11 -0700688 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700689 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700690 hideDialpadFragment(false, true);
691 }
692
Yorke Leee00c9fe2014-04-12 12:42:06 -0700693 private void hideInputMethod(View view) {
694 final InputMethodManager imm = (InputMethodManager) getSystemService(
695 Context.INPUT_METHOD_SERVICE);
696 if (imm != null && view != null) {
697 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
698 }
699 }
700
Yorke Lee53a22302014-04-29 18:13:46 -0700701 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700702 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
703 if (canIntentBeHandled(voiceIntent)) {
704 mVoiceSearchButton.setVisibility(View.VISIBLE);
705 mVoiceSearchButton.setOnClickListener(this);
706 } else {
707 mVoiceSearchButton.setVisibility(View.GONE);
708 }
709 }
710
Andrew Leee74a10e2014-05-16 14:31:40 -0700711 private OptionsPopupMenu buildOptionsMenu(View invoker) {
712 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
713 popupMenu.inflate(R.menu.dialtacts_options);
714 popupMenu.setOnMenuItemClickListener(this);
715 return popupMenu;
716 }
717
Yorke Lee86e21f72014-04-02 16:49:38 -0700718 @Override
719 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700720 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700721 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700722 mPendingSearchViewQuery = null;
723 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700724 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700725 }
726
Chiao Cheng94b10b52012-08-17 16:59:12 -0700727 /**
728 * Returns true if the intent is due to hitting the green send key (hardware call button:
729 * KEYCODE_CALL) while in a call.
730 *
731 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700732 * @return true if the intent is due to hitting the green send key while in a call
733 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700734 private boolean isSendKeyWhileInCall(Intent intent) {
735 // If there is a call in progress and the user launched the dialer by hitting the call
736 // button, go straight to the in-call screen.
737 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700738
Yorke Lee62e995c2014-03-28 10:02:56 -0700739 try {
740 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
741 if (callKey && phone != null && phone.showCallScreen()) {
742 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700743 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700744 } catch (RemoteException e) {
745 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700746 }
747
748 return false;
749 }
750
751 /**
752 * Sets the current tab based on the intent's request type
753 *
754 * @param intent Intent that contains information about which tab should be selected
755 */
Yorke Leec3766332013-07-31 11:13:16 -0700756 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700757 // 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 -0700758 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700759 finish();
760 return;
761 }
762
Yorke Lee669b6082013-09-17 15:43:38 -0700763 if (mDialpadFragment != null) {
764 final boolean phoneIsInUse = phoneIsInUse();
765 if (phoneIsInUse || isDialIntent(intent)) {
766 mDialpadFragment.setStartedFromNewIntent(true);
767 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
768 mInCallDialpadUp = true;
769 }
770 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700771 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700772 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700773 }
774
775 @Override
776 public void onNewIntent(Intent newIntent) {
777 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700778 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700779
Chiao Cheng94b10b52012-08-17 16:59:12 -0700780 invalidateOptionsMenu();
781 }
782
783 /** Returns true if the given intent contains a phone number to populate the dialer with */
784 private boolean isDialIntent(Intent intent) {
785 final String action = intent.getAction();
786 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
787 return true;
788 }
789 if (Intent.ACTION_VIEW.equals(action)) {
790 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700791 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700792 return true;
793 }
794 }
795 return false;
796 }
797
798 /**
799 * Returns an appropriate call origin for this Activity. May return null when no call origin
800 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
801 * for remembering the tab in which the user made a phone call, so the external app's DIAL
802 * request should not be counted.)
803 */
804 public String getCallOrigin() {
805 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
806 }
807
Chiao Cheng94b10b52012-08-17 16:59:12 -0700808 /**
Yorke Leec3766332013-07-31 11:13:16 -0700809 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700810 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700811 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700812 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700813 // Weird race condition where fragment is doing work after the activity is destroyed
814 // due to talkback being on (b/10209937). Just return since we can't do any
815 // constructive here.
816 return;
817 }
818
Yorke Leeef2b7382013-08-09 17:39:25 -0700819 if (DEBUG) {
820 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
821 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700822
Yorke Leec3766332013-07-31 11:13:16 -0700823 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700824 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700825 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700826 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700827 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700828 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700829
Yorke Leeb207f8a2013-08-29 18:51:06 -0700830 final String tag;
831 if (smartDialSearch) {
832 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
833 } else {
834 tag = TAG_REGULAR_SEARCH_FRAGMENT;
835 }
836 mInDialpadSearch = smartDialSearch;
837 mInRegularSearch = !smartDialSearch;
838
Andrew Lee752956e2014-05-15 11:43:38 -0700839 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700840 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700841 if (fragment == null) {
842 if (smartDialSearch) {
843 fragment = new SmartDialSearchFragment();
844 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700845 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700846 }
Andrew Leeb1903842014-05-15 16:11:24 -0700847 transaction.add(R.id.dialtacts_frame, fragment, tag);
848 } else {
849 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700850 }
Andrew Leeb1903842014-05-15 16:11:24 -0700851
Yorke Lee86e21f72014-04-02 16:49:38 -0700852 // DialtactsActivity will provide the options menu
853 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700854 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700855 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700856 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700857
858 mListsFragment.getView().animate().alpha(0).withLayer();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700859 }
860
861 /**
Yorke Leec3766332013-07-31 11:13:16 -0700862 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700863 */
Yorke Leec3766332013-07-31 11:13:16 -0700864 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700865 // See related bug in enterSearchUI();
866 if (getFragmentManager().isDestroyed()) {
867 return;
868 }
Andrew Leeb1903842014-05-15 16:11:24 -0700869
Andrew Leeb1903842014-05-15 16:11:24 -0700870 mSearchView.setText(null);
871 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700872 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700873
Andrew Leeb1903842014-05-15 16:11:24 -0700874 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
875 transaction.setCustomAnimations(0, android.R.animator.fade_out);
Andrew Leeb1903842014-05-15 16:11:24 -0700876 if (mSmartDialSearchFragment != null) {
877 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700878 }
Andrew Leeb1903842014-05-15 16:11:24 -0700879 if (mRegularSearchFragment != null) {
880 transaction.remove(mRegularSearchFragment);
881 }
882 transaction.commit();
883
884 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Lee5253be02014-05-21 18:50:03 -0700885 mActionBarController.onSearchUiExited();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700886 }
887
888 /** Returns an Intent to launch Call Settings screen */
889 public static Intent getCallSettingsIntent() {
890 final Intent intent = new Intent(Intent.ACTION_MAIN);
891 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
892 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
893 return intent;
894 }
895
896 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700897 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700898 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700899 if (TextUtils.isEmpty(mSearchQuery)) {
900 exitSearchUi();
901 }
Yorke Leef6673d32013-08-23 15:34:17 -0700902 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700903 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700904 exitSearchUi();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700905 hideInputMethod(parentLayout);
Yorke Leec3766332013-07-31 11:13:16 -0700906 } else {
907 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700908 }
Yorke Leec3766332013-07-31 11:13:16 -0700909 }
910
Yorke Leec7b2a0e2014-05-20 17:21:25 -0700911 /**
912 * @return True if the search UI was exited, false otherwise
913 */
914 private boolean maybeExitSearchUi() {
915 if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
916 exitSearchUi();
917 hideInputMethod(parentLayout);
918 return true;
919 }
920 return false;
921 }
922
Yorke Leec3766332013-07-31 11:13:16 -0700923 @Override
924 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700925 if (mSmartDialSearchFragment != null) {
926 mSmartDialSearchFragment.setAddToContactNumber(query);
927 }
Yorke Leec3766332013-07-31 11:13:16 -0700928 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
929 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700930
931 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700932 if (DEBUG) {
933 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
934 }
935 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
936 // This callback can happen if the dialpad fragment is recreated because of
937 // activity destruction. In that case, don't update the search view because
938 // that would bring the user back to the search fragment regardless of the
939 // previous state of the application. Instead, just return here and let the
940 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700941 if (!TextUtils.isEmpty(normalizedQuery)) {
942 mPendingSearchViewQuery = normalizedQuery;
943 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700944 return;
945 }
Yorke Lee53a22302014-04-29 18:13:46 -0700946 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700947 }
948 }
Yorke Leec3766332013-07-31 11:13:16 -0700949
950 @Override
951 public void onListFragmentScrollStateChange(int scrollState) {
952 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700953 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700954 hideInputMethod(getCurrentFocus());
955 }
956 }
957
958 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700959 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700960 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700961 // TODO: No-op for now. This should eventually show/hide the actionBar based on
962 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700963 }
964
Yorke Lee86e21f72014-04-02 16:49:38 -0700965 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700966 public void setFloatingActionButtonVisible(boolean visible) {
967 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700968 }
969
970 private boolean phoneIsInUse() {
971 final TelephonyManager tm = (TelephonyManager) getSystemService(
972 Context.TELEPHONY_SERVICE);
973 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
974 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700975
Yorke Leee1424812013-09-04 18:24:48 -0700976 public static Intent getAddNumberToContactIntent(CharSequence text) {
977 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
978 intent.putExtra(Intents.Insert.PHONE, text);
979 intent.setType(Contacts.CONTENT_ITEM_TYPE);
980 return intent;
981 }
982
Yorke Leeda0f9042013-12-05 14:25:51 -0800983 private boolean canIntentBeHandled(Intent intent) {
984 final PackageManager packageManager = getPackageManager();
985 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
986 PackageManager.MATCH_DEFAULT_ONLY);
987 return resolveInfo != null && resolveInfo.size() > 0;
988 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800989
Yorke Lee6a1461a2014-04-21 16:27:14 -0700990 @Override
991 public void showCallHistory() {
992 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
993 // CONTENT_TYPE, so that we always open our call log from our dialer
994 final Intent intent = new Intent(this, CallLogActivity.class);
995 startActivity(intent);
996 }
997
Yorke Lee86e21f72014-04-02 16:49:38 -0700998 /**
999 * Called when the user has long-pressed a contact tile to start a drag operation.
1000 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001001 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -07001002 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee5253be02014-05-21 18:50:03 -07001003 mActionBarController.slideActionBarUp(true);
Yorke Lee28266192014-05-01 10:05:52 -07001004 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001005 }
1006
1007 @Override
Yorke Lee28266192014-05-01 10:05:52 -07001008 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
1009 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001010
Yorke Lee86e21f72014-04-02 16:49:38 -07001011 /**
1012 * Called when the user has released a contact tile after long-pressing it.
1013 */
Yorke Lee3cefcc62014-01-16 11:26:46 -08001014 @Override
1015 public void onDragFinished(int x, int y) {
Yorke Lee5253be02014-05-21 18:50:03 -07001016 mActionBarController.slideActionBarDown(true);
Yorke Lee28266192014-05-01 10:05:52 -07001017 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001018 }
1019
1020 @Override
1021 public void onDroppedOnRemove() {}
1022
1023 /**
Yorke Leed999b712014-04-23 15:10:58 -07001024 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -08001025 * once it has been attached to the activity.
1026 */
1027 @Override
1028 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -07001029 mDragDropController = dragController;
1030 ((RemoveView) findViewById(R.id.remove_view))
1031 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001032 }
Yorke Leee00c9fe2014-04-12 12:42:06 -07001033
1034 @Override
1035 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001036 // Specify call-origin so that users will see the previous tab instead of
1037 // CallLog screen (search UI will be automatically exited).
1038 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -07001039 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001040 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001041 }
1042
1043 @Override
1044 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001045 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
1046 startActivity(intent);
1047 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -07001048 }
1049
1050 @Override
1051 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001052 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -07001053 }
1054
1055 @Override
1056 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001057 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001058 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001059
Yorke Leecc4660d2014-04-24 11:22:57 -07001060 @Override
1061 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1062
1063 }
1064
1065 @Override
1066 public void onPageSelected(int position) {
1067 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001068 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001069 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001070 alignFloatingActionButtonByTab(mCurrentTabPosition);
1071 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001072 }
1073
1074 @Override
1075 public void onPageScrollStateChanged(int state) {
1076 }
1077
Andrew Lee2a58ab82014-05-15 02:16:04 -07001078 private void updateFloatingActionButton() {
1079 if (mIsDialpadShown) {
1080 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1081 mFloatingActionButton.setContentDescription(
1082 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001083 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001084 } else {
1085 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1086 mFloatingActionButton.setContentDescription(
1087 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001088 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001089 }
1090 }
1091
Yorke Leecc4660d2014-04-24 11:22:57 -07001092 private void alignFloatingActionButtonByTab(int position) {
1093 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1094 alignFloatingActionButtonMiddle();
1095 } else {
1096 alignFloatingActionButtonRight();
1097 }
1098 }
1099
1100 private void alignFloatingActionButtonRight() {
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.CENTER_HORIZONTAL);
1104 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001105 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001106 }
1107
1108 private void alignFloatingActionButtonMiddle() {
1109 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001110 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001111 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1112 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001113 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001114 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001115
1116 /**
1117 * Convenience class which implements AnimationListener interface as null-op methods.
1118 */
1119 private class ActivityAnimationListener implements AnimationListener {
1120 @Override
1121 public void onAnimationStart(Animation animation) {
1122 }
1123
1124 @Override
1125 public void onAnimationEnd(Animation animation) {
1126 }
1127
1128 @Override
1129 public void onAnimationRepeat(Animation animation) {
1130 }
1131 }
Yorke Lee5253be02014-05-21 18:50:03 -07001132
1133 @Override
1134 public boolean isActionBarShowing() {
1135 return mActionBarController.isActionBarShowing();
1136 }
1137
1138 @Override
1139 public int getActionBarHideOffset() {
1140 return getActionBar().getHideOffset();
1141 }
1142
1143 @Override
1144 public int getActionBarHeight() {
1145 return mActionBarHeight;
1146 }
1147
1148 @Override
1149 public void setActionBarHideOffset(int hideOffset) {
1150 getActionBar().setHideOffset(hideOffset);
1151 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001152}