blob: f438570b2bcf4980cd2f2b4ba202e36e662db0d9 [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 Leec3766332013-07-31 11:13:16 -070019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.app.Activity;
23import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080026import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070027import android.content.Context;
28import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
Chiao Cheng94b10b52012-08-17 16:59:12 -070031import android.net.Uri;
32import android.os.Bundle;
33import android.os.RemoteException;
34import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.provider.CallLog.Calls;
36import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070037import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070038import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070039import android.telephony.TelephonyManager;
40import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070042import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.util.Log;
44import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.View;
Yorke Leeb0d19762014-03-05 14:43:28 -080047import android.view.ViewGroup.LayoutParams;
48import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070050import android.widget.AbsListView.OnScrollListener;
51import android.widget.EditText;
Yorke Leeb0d19762014-03-05 14:43:28 -080052import android.widget.LinearLayout;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080054import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070055
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070056import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070057import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070058import com.android.contacts.common.dialog.ClearFrequentsDialog;
59import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080060import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070061import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070062import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070063import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070064import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070065import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070066import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070067import com.android.dialer.list.AllContactsActivity;
Yorke Lee3cefcc62014-01-16 11:26:46 -080068import com.android.dialer.list.DragDropController;
69import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070071import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080072import com.android.dialer.list.PhoneFavoriteTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070073import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080074import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070075import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070076import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070077import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070078import com.android.internal.telephony.ITelephony;
79
Yorke Leec3766332013-07-31 11:13:16 -070080import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080081import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070082
Chiao Cheng94b10b52012-08-17 16:59:12 -070083/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070084 * The dialer tab's title is 'phone', a more common name (see strings.xml).
85 */
Yorke Leec3766332013-07-31 11:13:16 -070086public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070087 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070088 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080089 DialpadFragment.HostInterface,
Yorke Lee3cefcc62014-01-16 11:26:46 -080090 PhoneFavoriteFragment.OnShowAllContactsListener,
91 PhoneFavoriteFragment.HostInterface,
Ihab Awad526c0b82014-02-27 12:55:36 -080092 OnDragDropListener, View.OnLongClickListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070093 private static final String TAG = "DialtactsActivity";
94
Ihab Awad526c0b82014-02-27 12:55:36 -080095 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -070096
Yorke Leef74011e2013-08-02 11:30:30 -070097 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
98
Chiao Cheng94b10b52012-08-17 16:59:12 -070099 /** Used to open Call Setting */
100 private static final String PHONE_PACKAGE = "com.android.phone";
101 private static final String CALL_SETTINGS_CLASS_NAME =
102 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103 /** @see #getCallOrigin() */
104 private static final String CALL_ORIGIN_DIALTACTS =
105 "com.android.dialer.DialtactsActivity";
106
Yorke Leeb207f8a2013-08-29 18:51:06 -0700107 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
108 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700109 private static final String KEY_SEARCH_QUERY = "search_query";
110 private static final String KEY_FIRST_LAUNCH = "first_launch";
111
Yorke Leec3766332013-07-31 11:13:16 -0700112 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
113 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
114 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
115 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700116
Chiao Cheng94b10b52012-08-17 16:59:12 -0700117 /**
118 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
119 */
120 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
121
Yorke Leec3766332013-07-31 11:13:16 -0700122 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700123
Yorke Lee7eccf452014-03-14 12:52:42 -0700124 private static final int ANIMATION_DURATION = 200;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800125
Yorke Leec3766332013-07-31 11:13:16 -0700126 /**
127 * The main fragment displaying the user's favorites and frequent contacts
128 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700129 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700130
131 /**
Yorke Leec3766332013-07-31 11:13:16 -0700132 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700133 */
Yorke Leef74011e2013-08-02 11:30:30 -0700134 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700135
136 /**
137 * Fragment for searching phone numbers using the alphanumeric keyboard.
138 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700139 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700140
141 /**
142 * Fragment for searching phone numbers using the dialpad.
143 */
144 private SmartDialSearchFragment mSmartDialSearchFragment;
145
Yorke Leec3766332013-07-31 11:13:16 -0700146 private View mMenuButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800147 private View mFakeActionBar;
Yorke Leec3766332013-07-31 11:13:16 -0700148 private View mCallHistoryButton;
149 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800150 private View mDialButton;
Yorke Lee36c54132013-09-25 16:17:06 -0700151 private PopupMenu mOverflowMenu;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700152 private PopupMenu mDialpadOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700153
Yorke Leeb0d19762014-03-05 14:43:28 -0800154 // Padding view used to shift the fragment frame up when the dialpad is shown so that
155 // the contents of the fragment frame continue to exist in a layout of the same height
156 private View mFragmentsSpacer;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700157 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700158
Yorke Leeb207f8a2013-08-29 18:51:06 -0700159 private boolean mInDialpadSearch;
160 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700161 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700162
Yorke Leeef2b7382013-08-09 17:39:25 -0700163 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700164 * True if the dialpad is only temporarily showing due to being in call
165 */
166 private boolean mInCallDialpadUp;
167
168 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700169 * True when this activity has been launched for the first time.
170 */
Yorke Lee98702de2013-08-06 12:03:32 -0700171 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700172 private View mSearchViewContainer;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800173 private RemoveView mRemoveViewContainer;
Yorke Lee94f49042014-03-03 14:03:08 -0800174 // This view points to the Framelayout that houses both the search view and remove view
175 // containers.
176 private View mSearchAndRemoveViewContainer;
Yorke Leec3766332013-07-31 11:13:16 -0700177 private View mSearchViewCloseButton;
178 private View mVoiceSearchButton;
179 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700180
Yorke Leeef2b7382013-08-09 17:39:25 -0700181 private String mSearchQuery;
182
Yorke Leefce269a2013-08-12 11:56:04 -0700183 private DialerDatabaseHelper mDialerDatabaseHelper;
184
Yorke Lee36c54132013-09-25 16:17:06 -0700185 private class OverflowPopupMenu extends PopupMenu {
186 public OverflowPopupMenu(Context context, View anchor) {
187 super(context, anchor);
188 }
189
190 @Override
191 public void show() {
192 final Menu menu = getMenu();
193 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
194 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
195 super.show();
196 }
197 }
198
Chiao Cheng94b10b52012-08-17 16:59:12 -0700199 /**
200 * Listener used when one of phone numbers in search UI is selected. This will initiate a
201 * phone call using the phone number.
202 */
203 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
204 new OnPhoneNumberPickerActionListener() {
205 @Override
206 public void onPickPhoneNumberAction(Uri dataUri) {
207 // Specify call-origin so that users will see the previous tab instead of
208 // CallLog screen (search UI will be automatically exited).
209 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700210 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700211 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700212 }
213
214 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700215 public void onCallNumberDirectly(String phoneNumber) {
216 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
217 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700218 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700219 }
220
221 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700222 public void onShortcutIntentCreated(Intent intent) {
223 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
224 }
225
226 @Override
227 public void onHomeInActionBarSelected() {
228 exitSearchUi();
229 }
230 };
231
232 /**
233 * Listener used to send search queries to the phone search fragment.
234 */
Yorke Leec3766332013-07-31 11:13:16 -0700235 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
236 @Override
237 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
238 }
239
240 @Override
241 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700242 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700243 if (newText.equals(mSearchQuery)) {
244 // If the query hasn't changed (perhaps due to activity being destroyed
245 // and restored, or user launching the same DIAL intent twice), then there is
246 // no need to do anything here.
247 return;
248 }
249 mSearchQuery = newText;
250 if (DEBUG) {
251 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
252 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700253 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700254
Yorke Leec3766332013-07-31 11:13:16 -0700255 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700256 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700257 exitSearchUi();
258 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700259 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700260 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700261 } else if (!TextUtils.isEmpty(newText)) {
262 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
263 (!dialpadSearch && mInRegularSearch);
264 if (!sameSearchMode) {
265 // call enterSearchUi only if we are switching search modes, or entering
266 // search ui for the first time
267 enterSearchUi(dialpadSearch, newText);
268 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700269
Yorke Leeb207f8a2013-08-29 18:51:06 -0700270 if (dialpadSearch && mSmartDialSearchFragment != null) {
271 mSmartDialSearchFragment.setQueryString(newText, false);
272 } else if (mRegularSearchFragment != null) {
273 mRegularSearchFragment.setQueryString(newText, false);
274 }
275 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700276 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700277 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700278 }
Yorke Leec3766332013-07-31 11:13:16 -0700279 }
280
281 @Override
282 public void afterTextChanged(Editable s) {
283 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700284 };
285
Yorke Leec3766332013-07-31 11:13:16 -0700286 private boolean isDialpadShowing() {
287 return mDialpadFragment != null && mDialpadFragment.isVisible();
288 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700289
290 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700291 protected void onCreate(Bundle savedInstanceState) {
292 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700293 mFirstLaunch = true;
294
Yorke Lee8898cd02013-08-08 10:24:27 -0700295 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800296 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700297
Yorke Leec3766332013-07-31 11:13:16 -0700298 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700299
Yorke Leeef2b7382013-08-09 17:39:25 -0700300 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
301 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700302 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800303 getFragmentManager().beginTransaction()
304 .add(R.id.dialtacts_frame, new PhoneFavoriteFragment(), TAG_FAVORITES_FRAGMENT)
305 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
306 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700307 } else {
308 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700309 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
310 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700311 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700312 }
313
Yorke Leeb207f8a2013-08-29 18:51:06 -0700314 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leeb0d19762014-03-05 14:43:28 -0800315 mFragmentsSpacer = findViewById(R.id.contact_tile_frame_spacer);
Ihab Awad526c0b82014-02-27 12:55:36 -0800316
Yorke Lee3cefcc62014-01-16 11:26:46 -0800317 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800318 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
319
320 // When the first global layout pass is completed (and mSearchAndRemoveViewContainer has
321 // been assigned a valid height), assign that height to mFragmentsSpacer as well.
322 mSearchAndRemoveViewContainer.getViewTreeObserver().addOnGlobalLayoutListener(
323 new OnGlobalLayoutListener() {
324 @Override
325 public void onGlobalLayout() {
326 mSearchAndRemoveViewContainer.getViewTreeObserver()
327 .removeOnGlobalLayoutListener(this);
328 mFragmentsSpacer.setLayoutParams(
329 new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
330 mSearchAndRemoveViewContainer.getHeight()));
331 }
332 });
333
Yorke Lee8d540002014-03-26 16:13:10 -0700334 setupFakeActionBarItems();
Yorke Leec3766332013-07-31 11:13:16 -0700335 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700336
Yorke Lee0baa98b2013-08-22 10:55:16 -0700337 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700338 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700339 }
340
341 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700342 protected void onResume() {
343 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700344 if (mFirstLaunch) {
345 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700346 } else if (!phoneIsInUse() && mInCallDialpadUp) {
347 hideDialpadFragment(false, true);
348 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700349 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800350 prepareVoiceSearchButton();
Yorke Lee98702de2013-08-06 12:03:32 -0700351 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700352 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700353 }
354
355 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700356 protected void onPause() {
357 if (mClearSearchOnPause) {
358 hideDialpadAndSearchUi();
359 mClearSearchOnPause = false;
360 }
361 super.onPause();
362 }
363
364 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700365 protected void onSaveInstanceState(Bundle outState) {
366 super.onSaveInstanceState(outState);
367 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700368 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
369 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700370 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
371 }
372
373 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700374 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700375 if (fragment instanceof DialpadFragment) {
376 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700377 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700378 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700379 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700380 } else if (fragment instanceof SmartDialSearchFragment) {
381 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700382 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
383 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700384 if (mFragmentsFrame != null) {
385 mFragmentsFrame.setAlpha(1.0f);
386 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700387 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700388 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700389 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
390 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700391 if (mFragmentsFrame != null) {
392 mFragmentsFrame.setAlpha(1.0f);
393 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700394 } else if (fragment instanceof PhoneFavoriteFragment) {
395 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700396 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700397 }
Yorke Leec3766332013-07-31 11:13:16 -0700398 }
399
400 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700401 public boolean onMenuItemClick(MenuItem item) {
402 switch (item.getItemId()) {
403 case R.id.menu_import_export:
404 // We hard-code the "contactsAreAvailable" argument because doing it properly would
405 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
406 // now in Dialtacts for (potential) performance reasons. Compare with how it is
407 // done in {@link PeopleActivity}.
408 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700409 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700410 return true;
411 case R.id.menu_clear_frequents:
412 ClearFrequentsDialog.show(getFragmentManager());
413 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700414 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700415 try {
416 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
417 } catch (ActivityNotFoundException e) {
418 Toast toast = Toast.makeText(this,
419 R.string.add_contact_not_available,
420 Toast.LENGTH_SHORT);
421 toast.show();
422 }
423 return true;
424 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700425 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700426 return true;
427 case R.id.menu_all_contacts:
428 onShowAllContacts();
429 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700430 }
431 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700432 }
433
Alon Albertb453e5b2013-09-10 15:54:23 -0700434 protected void handleMenuSettings() {
435 openTelephonySetting(this);
436 }
437
438 public static void openTelephonySetting(Activity activity) {
439 final Intent settingsIntent = getCallSettingsIntent();
440 activity.startActivity(settingsIntent);
441 }
442
Chiao Cheng94b10b52012-08-17 16:59:12 -0700443 @Override
444 public void onClick(View view) {
445 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700446 case R.id.overflow_menu: {
Ihab Awad526c0b82014-02-27 12:55:36 -0800447 if (isDialpadShowing()) {
Ihab Awadfb00cb82014-03-18 16:19:00 -0700448 mDialpadOverflowMenu.show();
Ihab Awad526c0b82014-02-27 12:55:36 -0800449 } else {
450 mOverflowMenu.show();
451 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700452 break;
453 }
Yorke Leec3766332013-07-31 11:13:16 -0700454 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700455 // Reset the boolean flag that tracks whether the dialpad was up because
456 // we were in call. Regardless of whether it was true before, we want to
457 // show the dialpad because the user has explicitly clicked the dialpad
458 // button.
459 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700460 showDialpadFragment(true);
461 break;
Ihab Awad526c0b82014-02-27 12:55:36 -0800462 case R.id.dial_button:
463 // Dial button was pressed; tell the Dialpad fragment
464 mDialpadFragment.dialButtonPressed();
465 break;
Yorke Leec3766332013-07-31 11:13:16 -0700466 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700467 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
468 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700469 final Intent intent = new Intent(this, CallLogActivity.class);
470 startActivity(intent);
471 break;
472 case R.id.search_close_button:
473 // Clear the search field
474 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700475 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700476 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700477 }
478 break;
Yorke Leec3766332013-07-31 11:13:16 -0700479 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800480 try {
481 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
482 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
483 } catch (ActivityNotFoundException e) {
484 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
485 Toast.LENGTH_SHORT).show();
486 }
Yorke Leec3766332013-07-31 11:13:16 -0700487 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700488 default: {
489 Log.wtf(TAG, "Unexpected onClick event from " + view);
490 break;
491 }
492 }
493 }
494
Yorke Leec3766332013-07-31 11:13:16 -0700495 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800496 public boolean onLongClick(View view) {
497 switch (view.getId()) {
498 case R.id.dial_button: {
499 // Dial button was pressed; tell the Dialpad fragment
500 mDialpadFragment.dialButtonPressed();
501 return true; // Consume the event
502 }
503 default: {
504 Log.wtf(TAG, "Unexpected onClick event from " + view);
505 break;
506 }
507 }
508 return false;
509 }
510
511 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700512 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
513 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
514 if (resultCode == RESULT_OK) {
515 final ArrayList<String> matches = data.getStringArrayListExtra(
516 RecognizerIntent.EXTRA_RESULTS);
517 if (matches.size() > 0) {
518 final String match = matches.get(0);
519 mSearchView.setText(match);
520 } else {
521 Log.e(TAG, "Voice search - nothing heard");
522 }
523 } else {
524 Log.e(TAG, "Voice search failed");
525 }
526 }
527 super.onActivityResult(requestCode, resultCode, data);
528 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700529
Yorke Leec3766332013-07-31 11:13:16 -0700530 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700531 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700532 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700533 if (animate) {
534 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700535 } else {
536 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700537 }
538 ft.show(mDialpadFragment);
539 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800540 mDialButton.setVisibility(shouldShowOnscreenDialButton() ? View.VISIBLE : View.GONE);
541 mDialpadButton.setVisibility(View.GONE);
Yorke Lee8d540002014-03-26 16:13:10 -0700542
543 if (mDialpadOverflowMenu == null) {
544 mDialpadOverflowMenu = mDialpadFragment.buildOptionsMenu(mMenuButton);
545 }
546
Ihab Awadfb00cb82014-03-18 16:19:00 -0700547 mMenuButton.setOnTouchListener(mDialpadOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700548 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700549
Yorke Leeca195042013-09-25 16:29:38 -0700550 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700551 if (mDialpadFragment == null) return;
552 if (clearDialpad) {
553 mDialpadFragment.clearDialpad();
554 }
555 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700556 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700557 final FragmentTransaction ft = getFragmentManager().beginTransaction();
558 if (animate) {
559 ft.setCustomAnimations(0, R.anim.slide_out);
560 }
561 ft.hide(mDialpadFragment);
562 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800563 mDialButton.setVisibility(View.GONE);
564 mDialpadButton.setVisibility(View.VISIBLE);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700565 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700566 }
567
Chiao Cheng94b10b52012-08-17 16:59:12 -0700568 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700569 mSearchViewContainer = findViewById(R.id.search_view_container);
570 mSearchViewCloseButton = findViewById(R.id.search_close_button);
571 mSearchViewCloseButton.setOnClickListener(this);
Yorke Leeda0f9042013-12-05 14:25:51 -0800572
Yorke Leec3766332013-07-31 11:13:16 -0700573 mSearchView = (EditText) findViewById(R.id.search_view);
574 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Tyler Gunna8ff9752014-02-24 11:57:57 -0800575 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
Yorke Leeda0f9042013-12-05 14:25:51 -0800576
577 prepareVoiceSearchButton();
578 }
579
580 private void prepareVoiceSearchButton() {
581 mVoiceSearchButton = findViewById(R.id.voice_search_button);
582 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
583 if (canIntentBeHandled(voiceIntent)) {
584 mVoiceSearchButton.setVisibility(View.VISIBLE);
585 mVoiceSearchButton.setOnClickListener(this);
586 } else {
587 mVoiceSearchButton.setVisibility(View.GONE);
588 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700589 }
590
Yorke Leec3766332013-07-31 11:13:16 -0700591 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
592 @Override
593 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800594 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700595 }
596 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700597
Yorke Leeb207f8a2013-08-29 18:51:06 -0700598 private boolean getInSearchUi() {
599 return mInDialpadSearch || mInRegularSearch;
600 }
601
602 private void setNotInSearchUi() {
603 mInDialpadSearch = false;
604 mInRegularSearch = false;
605 }
606
Yorke Lee311969c2013-08-26 06:31:11 -0700607 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700608 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700609 hideDialpadFragment(false, true);
610 }
611
Yorke Leec3766332013-07-31 11:13:16 -0700612 public void hideSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800613 final int height = mSearchAndRemoveViewContainer.getHeight();
Yorke Leeb0d19762014-03-05 14:43:28 -0800614
Yorke Lee94f49042014-03-03 14:03:08 -0800615 mSearchAndRemoveViewContainer.animate().cancel();
616 mSearchAndRemoveViewContainer.setAlpha(1);
617 mSearchAndRemoveViewContainer.setTranslationY(0);
618 mSearchAndRemoveViewContainer.animate().withLayer().alpha(0)
Yorke Lee7eccf452014-03-14 12:52:42 -0700619 .translationY(-height).setDuration(ANIMATION_DURATION)
Yorke Lee94f49042014-03-03 14:03:08 -0800620 .setListener(mHideListener);
Christine Chen9c1e0652013-05-23 15:40:19 -0700621
Yorke Lee94f49042014-03-03 14:03:08 -0800622 mFragmentsFrame.animate().withLayer()
Yorke Lee7eccf452014-03-14 12:52:42 -0700623 .translationY(-height).setDuration(ANIMATION_DURATION).setListener(
Yorke Lee94f49042014-03-03 14:03:08 -0800624 new AnimatorListenerAdapter() {
625 @Override
626 public void onAnimationEnd(Animator animation) {
627 mFragmentsFrame.setTranslationY(0);
Yorke Leeb0d19762014-03-05 14:43:28 -0800628 // Display the fragments spacer (which has the same height as the
629 // search box) now that the search box is hidden, so that
630 // mFragmentsFrame always retains the same height
631 mFragmentsSpacer.setVisibility(View.VISIBLE);
Yorke Lee94f49042014-03-03 14:03:08 -0800632 }
633 });
Yorke Lee7eccf452014-03-14 12:52:42 -0700634
635 if (!mInDialpadSearch && !mInRegularSearch) {
636 // If the favorites fragment is showing, fade to blank.
637 mFragmentsFrame.animate().alpha(0.0f);
638 }
Yorke Leec3766332013-07-31 11:13:16 -0700639 }
640
641 public void showSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800642 final int height = mSearchAndRemoveViewContainer.getHeight();
643 mSearchAndRemoveViewContainer.animate().cancel();
644 mSearchAndRemoveViewContainer.setAlpha(0);
645 mSearchAndRemoveViewContainer.setTranslationY(-height);
646 mSearchAndRemoveViewContainer.animate().withLayer().alpha(1).translationY(0)
Yorke Lee7eccf452014-03-14 12:52:42 -0700647 .setDuration(ANIMATION_DURATION).setListener(new AnimatorListenerAdapter() {
Yorke Lee94f49042014-03-03 14:03:08 -0800648 @Override
649 public void onAnimationStart(Animator animation) {
650 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
651 }
652 });
Yorke Leec3766332013-07-31 11:13:16 -0700653
Yorke Lee94f49042014-03-03 14:03:08 -0800654 mFragmentsFrame.setTranslationY(-height);
Yorke Lee7eccf452014-03-14 12:52:42 -0700655 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(ANIMATION_DURATION)
656 .alpha(1.0f)
Yorke Leec3766332013-07-31 11:13:16 -0700657 .setListener(
658 new AnimatorListenerAdapter() {
659 @Override
660 public void onAnimationStart(Animator animation) {
Yorke Leeb0d19762014-03-05 14:43:28 -0800661 // Hide the fragment spacer now that the search box will
662 // be displayed again
663 mFragmentsSpacer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700664 }
665 });
666 }
667
Ihab Awadfb00cb82014-03-18 16:19:00 -0700668 private void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700669 mMenuButton = findViewById(R.id.overflow_menu);
670 if (mMenuButton != null) {
671 mMenuButton.setOnClickListener(this);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700672 if (mOverflowMenu == null) {
673 mOverflowMenu = buildOptionsMenu(mMenuButton);
674 }
Yorke Lee36c54132013-09-25 16:17:06 -0700675 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700676 }
677
Ihab Awad526c0b82014-02-27 12:55:36 -0800678 mFakeActionBar = findViewById(R.id.fake_action_bar);
679
Yorke Leec3766332013-07-31 11:13:16 -0700680 mCallHistoryButton = findViewById(R.id.call_history_button);
Yorke Leec3766332013-07-31 11:13:16 -0700681 mCallHistoryButton.setOnClickListener(this);
682
Ihab Awad526c0b82014-02-27 12:55:36 -0800683 mDialButton = findViewById(R.id.dial_button);
684 mDialButton.setOnClickListener(this);
685 mDialButton.setOnLongClickListener(this);
686
Yorke Leec3766332013-07-31 11:13:16 -0700687 mDialpadButton = findViewById(R.id.dialpad_button);
Yorke Leec3766332013-07-31 11:13:16 -0700688 mDialpadButton.setOnClickListener(this);
689 }
690
Ihab Awadfb00cb82014-03-18 16:19:00 -0700691 private PopupMenu buildOptionsMenu(View invoker) {
692 PopupMenu menu = new OverflowPopupMenu(this, invoker);
693 menu.inflate(R.menu.dialtacts_options);
694 menu.setOnMenuItemClickListener(this);
695 return menu;
696 }
697
Chiao Cheng94b10b52012-08-17 16:59:12 -0700698 /**
699 * Returns true if the intent is due to hitting the green send key (hardware call button:
700 * KEYCODE_CALL) while in a call.
701 *
702 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700703 * @return true if the intent is due to hitting the green send key while in a call
704 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700705 private boolean isSendKeyWhileInCall(Intent intent) {
706 // If there is a call in progress and the user launched the dialer by hitting the call
707 // button, go straight to the in-call screen.
708 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700709
Yorke Lee62e995c2014-03-28 10:02:56 -0700710 try {
711 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
712 if (callKey && phone != null && phone.showCallScreen()) {
713 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700714 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700715 } catch (RemoteException e) {
716 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700717 }
718
719 return false;
720 }
721
722 /**
723 * Sets the current tab based on the intent's request type
724 *
725 * @param intent Intent that contains information about which tab should be selected
726 */
Yorke Leec3766332013-07-31 11:13:16 -0700727 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728 // 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 -0700729 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730 finish();
731 return;
732 }
733
Yorke Lee669b6082013-09-17 15:43:38 -0700734 if (mDialpadFragment != null) {
735 final boolean phoneIsInUse = phoneIsInUse();
736 if (phoneIsInUse || isDialIntent(intent)) {
737 mDialpadFragment.setStartedFromNewIntent(true);
738 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
739 mInCallDialpadUp = true;
740 }
741 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700742 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700743 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 }
745
746 @Override
747 public void onNewIntent(Intent newIntent) {
748 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700749 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700750
Chiao Cheng94b10b52012-08-17 16:59:12 -0700751 invalidateOptionsMenu();
752 }
753
754 /** Returns true if the given intent contains a phone number to populate the dialer with */
755 private boolean isDialIntent(Intent intent) {
756 final String action = intent.getAction();
757 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
758 return true;
759 }
760 if (Intent.ACTION_VIEW.equals(action)) {
761 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700762 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700763 return true;
764 }
765 }
766 return false;
767 }
768
769 /**
770 * Returns an appropriate call origin for this Activity. May return null when no call origin
771 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
772 * for remembering the tab in which the user made a phone call, so the external app's DIAL
773 * request should not be counted.)
774 */
775 public String getCallOrigin() {
776 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
777 }
778
Yorke Lee8898cd02013-08-08 10:24:27 -0700779 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
780 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700781 @Override
782 public void onContactSelected(Uri contactUri) {
783 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700784 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700785 }
786
787 @Override
788 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700789 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700790 startActivity(intent);
791 }
792 };
793
Chiao Cheng94b10b52012-08-17 16:59:12 -0700794 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700795 final InputMethodManager imm = (InputMethodManager) getSystemService(
796 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700797 if (imm != null && view != null) {
798 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
799 }
800 }
801
802 /**
Yorke Leec3766332013-07-31 11:13:16 -0700803 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700804 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700805 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700806 if (getFragmentManager().isDestroyed()) {
807 // Weird race condition where fragment is doing work after the activity is destroyed
808 // due to talkback being on (b/10209937). Just return since we can't do any
809 // constructive here.
810 return;
811 }
812
Yorke Leeef2b7382013-08-09 17:39:25 -0700813 if (DEBUG) {
814 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
815 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700816
Yorke Leec3766332013-07-31 11:13:16 -0700817 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700818
Yorke Leeef2b7382013-08-09 17:39:25 -0700819 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700820 if (mInDialpadSearch) {
821 transaction.remove(mSmartDialSearchFragment);
822 } else if (mInRegularSearch) {
823 transaction.remove(mRegularSearchFragment);
824 } else {
825 transaction.remove(mPhoneFavoriteFragment);
826 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700827
Yorke Leeb207f8a2013-08-29 18:51:06 -0700828 final String tag;
829 if (smartDialSearch) {
830 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
831 } else {
832 tag = TAG_REGULAR_SEARCH_FRAGMENT;
833 }
834 mInDialpadSearch = smartDialSearch;
835 mInRegularSearch = !smartDialSearch;
836
Yorke Leeef2b7382013-08-09 17:39:25 -0700837 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
838 if (fragment == null) {
839 if (smartDialSearch) {
840 fragment = new SmartDialSearchFragment();
841 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700842 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700843 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700844 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700845 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700846 transaction.addToBackStack(null);
847 fragment.setQueryString(query, false);
848 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700849 }
850
851 /**
Yorke Leec3766332013-07-31 11:13:16 -0700852 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700853 */
Yorke Leec3766332013-07-31 11:13:16 -0700854 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700855 // See related bug in enterSearchUI();
856 if (getFragmentManager().isDestroyed()) {
857 return;
858 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700859 // Go all the way back to the favorites fragment, regardless of how many times we
860 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700861 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
862 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700863
864 if (isDialpadShowing()) {
865 mFragmentsFrame.setAlpha(0);
866 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700867 }
868
869 /** Returns an Intent to launch Call Settings screen */
870 public static Intent getCallSettingsIntent() {
871 final Intent intent = new Intent(Intent.ACTION_MAIN);
872 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
873 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
874 return intent;
875 }
876
877 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700878 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700879 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700880 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700881 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700882 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700883 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700884 } else {
885 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700886 }
Yorke Leec3766332013-07-31 11:13:16 -0700887 }
888
889 @Override
890 public void onDialpadQueryChanged(String query) {
891 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
892 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
893 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700894 if (DEBUG) {
895 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
896 }
897 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
898 // This callback can happen if the dialpad fragment is recreated because of
899 // activity destruction. In that case, don't update the search view because
900 // that would bring the user back to the search fragment regardless of the
901 // previous state of the application. Instead, just return here and let the
902 // fragment manager correctly figure out whatever fragment was last displayed.
903 return;
904 }
Yorke Leec3766332013-07-31 11:13:16 -0700905 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700906 }
907 }
Yorke Leec3766332013-07-31 11:13:16 -0700908
909 @Override
910 public void onListFragmentScrollStateChange(int scrollState) {
911 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700912 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700913 hideInputMethod(getCurrentFocus());
914 }
915 }
916
917 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800918 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700919 if (mDialButton != null) {
920 mDialButton.setEnabled(enabled);
921 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800922 }
923
924 @Override
925 public void setDialButtonContainerVisible(boolean visible) {
926 mFakeActionBar.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700927 }
928
929 private boolean phoneIsInUse() {
930 final TelephonyManager tm = (TelephonyManager) getSystemService(
931 Context.TELEPHONY_SERVICE);
932 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
933 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700934
935 @Override
936 public void onShowAllContacts() {
937 final Intent intent = new Intent(this, AllContactsActivity.class);
938 startActivity(intent);
939 }
Yorke Leee1424812013-09-04 18:24:48 -0700940
941 public static Intent getAddNumberToContactIntent(CharSequence text) {
942 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
943 intent.putExtra(Intents.Insert.PHONE, text);
944 intent.setType(Contacts.CONTENT_ITEM_TYPE);
945 return intent;
946 }
947
Yorke Leeda0f9042013-12-05 14:25:51 -0800948 private boolean canIntentBeHandled(Intent intent) {
949 final PackageManager packageManager = getPackageManager();
950 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
951 PackageManager.MATCH_DEFAULT_ONLY);
952 return resolveInfo != null && resolveInfo.size() > 0;
953 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800954
955 @Override
956 public void onDragStarted(int itemIndex, int x, int y, PhoneFavoriteTileView view) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700957 crossfadeViews(mRemoveViewContainer, mSearchViewContainer, ANIMATION_DURATION);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800958 }
959
960 @Override
961 public void onDragHovered(int itemIndex, int x, int y) {}
962
963 @Override
964 public void onDragFinished(int x, int y) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700965 crossfadeViews(mSearchViewContainer, mRemoveViewContainer, ANIMATION_DURATION);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800966 }
967
968 @Override
969 public void onDroppedOnRemove() {}
970
971 /**
972 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
973 * once it has been attached to the activity.
974 */
975 @Override
976 public void setDragDropController(DragDropController dragController) {
977 mRemoveViewContainer.setDragDropController(dragController);
978 }
979
980 /**
981 * Crossfades two views so that the first one appears while the other one is fading
982 * out of view.
983 */
984 private void crossfadeViews(final View fadeIn, final View fadeOut, int duration) {
985 fadeOut.animate().alpha(0).setDuration(duration)
986 .setListener(new AnimatorListenerAdapter() {
987 @Override
988 public void onAnimationEnd(Animator animation) {
989 fadeOut.setVisibility(View.GONE);
990 }
991 });
992
993 fadeIn.setVisibility(View.VISIBLE);
994 fadeIn.setAlpha(0);
Yorke Lee7eccf452014-03-14 12:52:42 -0700995 fadeIn.animate().alpha(1).setDuration(ANIMATION_DURATION)
Yorke Lee3cefcc62014-01-16 11:26:46 -0800996 .setListener(null);
997 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800998
999 private boolean shouldShowOnscreenDialButton() {
1000 return getResources().getBoolean(R.bool.config_show_onscreen_dial_button);
1001 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001002}