blob: e9bc979f8b8377222559850d7072dd1934b54579 [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;
Christine Chen9c1e0652013-05-23 15:40:19 -070023import android.app.backup.BackupManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070024import android.app.Fragment;
25import android.app.FragmentManager;
26import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080027import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.content.Context;
29import android.content.Intent;
30import android.content.SharedPreferences;
Yorke Leec3766332013-07-31 11:13:16 -070031import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070032import android.net.Uri;
33import android.os.Bundle;
34import android.os.RemoteException;
35import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070037import android.provider.ContactsContract;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.provider.ContactsContract.Contacts;
39import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070040import android.provider.Settings;
41import android.speech.RecognizerIntent;
42import android.support.v4.app.NavUtils;
43import android.telephony.TelephonyManager;
44import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070046import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.util.Log;
48import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.view.View.OnFocusChangeListener;
52import android.view.ViewConfiguration;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070054import android.widget.AbsListView.OnScrollListener;
55import android.widget.EditText;
56import android.widget.ImageView;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.widget.PopupMenu;
58import android.widget.SearchView;
59import android.widget.SearchView.OnCloseListener;
60import android.widget.SearchView.OnQueryTextListener;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080061import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070062
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070063import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070064import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070065import com.android.contacts.common.dialog.ClearFrequentsDialog;
66import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070067import com.android.contacts.common.list.ContactListItemView;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080068import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
69import com.android.contacts.common.list.PhoneNumberPickerFragment;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070071import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070072import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070073import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070074import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070075import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070076import com.android.dialer.list.AllContactsActivity;
Yorke Lee8898cd02013-08-08 10:24:27 -070077import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Leec3766332013-07-31 11:13:16 -070078import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leec3766332013-07-31 11:13:16 -070079import com.android.dialer.list.SmartDialSearchFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -070080import com.android.internal.telephony.ITelephony;
81
Yorke Leec3766332013-07-31 11:13:16 -070082import java.util.ArrayList;
83
Chiao Cheng94b10b52012-08-17 16:59:12 -070084/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070085 * The dialer tab's title is 'phone', a more common name (see strings.xml).
86 */
Yorke Leec3766332013-07-31 11:13:16 -070087public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070088 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070089 OnListFragmentScrolledListener,
Yorke Lee8898cd02013-08-08 10:24:27 -070090 PhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener,
Yorke Lee8dd62002013-08-08 15:57:20 -070091 DialpadFragment.OnDialpadFragmentStartedListener,
92 PhoneFavoriteFragment.OnShowAllContactsListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070093 private static final String TAG = "DialtactsActivity";
94
95 public static final boolean DEBUG = false;
96
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 Leeef2b7382013-08-09 17:39:25 -0700107 private static final String KEY_IN_SEARCH_UI = "in_search_ui";
108 private static final String KEY_SEARCH_QUERY = "search_query";
109 private static final String KEY_FIRST_LAUNCH = "first_launch";
110
Yorke Leec3766332013-07-31 11:13:16 -0700111 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
112 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
113 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
114 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700115
Chiao Cheng94b10b52012-08-17 16:59:12 -0700116 /**
117 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
118 */
119 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
120
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
122
Yorke Leec3766332013-07-31 11:13:16 -0700123 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700124
125 private String mFilterText;
126
Yorke Leec3766332013-07-31 11:13:16 -0700127 /**
128 * The main fragment displaying the user's favorites and frequent contacts
129 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700130 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700131
132 /**
Yorke Leec3766332013-07-31 11:13:16 -0700133 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700134 */
Yorke Leef74011e2013-08-02 11:30:30 -0700135 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700136
137 /**
138 * Fragment for searching phone numbers using the alphanumeric keyboard.
139 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700140 private SearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700141
142 /**
143 * Fragment for searching phone numbers using the dialpad.
144 */
145 private SmartDialSearchFragment mSmartDialSearchFragment;
146
Yorke Leec3766332013-07-31 11:13:16 -0700147 private View mMenuButton;
148 private View mCallHistoryButton;
149 private View mDialpadButton;
150
151 // Padding view used to shift the fragments up when the dialpad is shown.
152 private View mBottomPaddingView;
153
Chiao Cheng94b10b52012-08-17 16:59:12 -0700154 /**
155 * True when this Activity is in its search UI (with a {@link SearchView} and
156 * {@link PhoneNumberPickerFragment}).
157 */
158 private boolean mInSearchUi;
Yorke Leeef2b7382013-08-09 17:39:25 -0700159 /**
160 * True when this activity has been launched for the first time.
161 */
Yorke Lee98702de2013-08-06 12:03:32 -0700162 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700163 private View mSearchViewContainer;
164 private View mSearchViewCloseButton;
165 private View mVoiceSearchButton;
166 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700167
Yorke Leeef2b7382013-08-09 17:39:25 -0700168 private String mSearchQuery;
169
Yorke Leefce269a2013-08-12 11:56:04 -0700170 private DialerDatabaseHelper mDialerDatabaseHelper;
171
Chiao Cheng94b10b52012-08-17 16:59:12 -0700172 /**
173 * Listener used when one of phone numbers in search UI is selected. This will initiate a
174 * phone call using the phone number.
175 */
176 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
177 new OnPhoneNumberPickerActionListener() {
178 @Override
179 public void onPickPhoneNumberAction(Uri dataUri) {
180 // Specify call-origin so that users will see the previous tab instead of
181 // CallLog screen (search UI will be automatically exited).
182 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700183 DialtactsActivity.this, dataUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700184 }
185
186 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700187 public void onCallNumberDirectly(String phoneNumber) {
188 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
189 startActivity(intent);
190 }
191
192 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700193 public void onShortcutIntentCreated(Intent intent) {
194 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
195 }
196
197 @Override
198 public void onHomeInActionBarSelected() {
199 exitSearchUi();
200 }
201 };
202
203 /**
204 * Listener used to send search queries to the phone search fragment.
205 */
Yorke Leec3766332013-07-31 11:13:16 -0700206 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
207 @Override
208 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
209 }
210
211 @Override
212 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700213 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700214 if (newText.equals(mSearchQuery)) {
215 // If the query hasn't changed (perhaps due to activity being destroyed
216 // and restored, or user launching the same DIAL intent twice), then there is
217 // no need to do anything here.
218 return;
219 }
220 mSearchQuery = newText;
221 if (DEBUG) {
222 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
223 }
224 final boolean smartDialSearch = isDialpadShowing();
225
Yorke Leec3766332013-07-31 11:13:16 -0700226 // Show search result with non-empty text. Show a bare list otherwise.
227 if (TextUtils.isEmpty(newText) && mInSearchUi) {
228 exitSearchUi();
229 mSearchViewCloseButton.setVisibility(View.GONE);
230 return;
231 } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700232 enterSearchUi(smartDialSearch, newText);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700233 }
234
Yorke Leeef2b7382013-08-09 17:39:25 -0700235 if (smartDialSearch && mSmartDialSearchFragment != null) {
236 mSmartDialSearchFragment.setQueryString(newText, false);
237 } else if (mRegularSearchFragment != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700238 mRegularSearchFragment.setQueryString(newText, false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700239 }
Yorke Leec3766332013-07-31 11:13:16 -0700240 mSearchViewCloseButton.setVisibility(View.VISIBLE);
241 return;
242 }
243
244 @Override
245 public void afterTextChanged(Editable s) {
246 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700247 };
248
Yorke Leec3766332013-07-31 11:13:16 -0700249 private boolean isDialpadShowing() {
250 return mDialpadFragment != null && mDialpadFragment.isVisible();
251 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700252
253 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700254 protected void onCreate(Bundle savedInstanceState) {
255 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700256 mFirstLaunch = true;
257
Chiao Cheng94b10b52012-08-17 16:59:12 -0700258 final Intent intent = getIntent();
259 fixIntent(intent);
260
Yorke Lee8898cd02013-08-08 10:24:27 -0700261 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700262
Yorke Leec3766332013-07-31 11:13:16 -0700263 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700264
Yorke Leeef2b7382013-08-09 17:39:25 -0700265 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
266 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700267 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700268 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
269 phoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700270
Yorke Leec3766332013-07-31 11:13:16 -0700271 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700272 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
273 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700274 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700275 } else {
276 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
277 mInSearchUi = savedInstanceState.getBoolean(KEY_IN_SEARCH_UI);
278 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700279 }
280
Yorke Leec3766332013-07-31 11:13:16 -0700281 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
282 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700283
Chiao Cheng94b10b52012-08-17 16:59:12 -0700284 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700285 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700286 setupFilterText(intent);
287 }
Yorke Leefce269a2013-08-12 11:56:04 -0700288
289 mDialerDatabaseHelper = DialerDatabaseHelper.getInstance(this);
290 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291 }
292
293 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700294 protected void onResume() {
295 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700296 if (mFirstLaunch) {
297 displayFragment(getIntent());
298 }
299 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700300 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700301 }
302
303 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700304 protected void onSaveInstanceState(Bundle outState) {
305 super.onSaveInstanceState(outState);
306 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
307 outState.putBoolean(KEY_IN_SEARCH_UI, mInSearchUi);
308 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
309 }
310
311 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700312 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700313 if (fragment instanceof DialpadFragment) {
314 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700315 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700316 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700317 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700318 } else if (fragment instanceof SmartDialSearchFragment) {
319 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
320 } else if (fragment instanceof SearchFragment) {
321 mRegularSearchFragment = (SearchFragment) fragment;
322 } else if (fragment instanceof PhoneFavoriteFragment) {
323 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700324 }
Yorke Leec3766332013-07-31 11:13:16 -0700325 }
326
327 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700328 public boolean onMenuItemClick(MenuItem item) {
329 switch (item.getItemId()) {
330 case R.id.menu_import_export:
331 // We hard-code the "contactsAreAvailable" argument because doing it properly would
332 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
333 // now in Dialtacts for (potential) performance reasons. Compare with how it is
334 // done in {@link PeopleActivity}.
335 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700336 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700337 return true;
338 case R.id.menu_clear_frequents:
339 ClearFrequentsDialog.show(getFragmentManager());
340 return true;
341 case R.id.add_contact:
342 try {
343 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
344 } catch (ActivityNotFoundException e) {
345 Toast toast = Toast.makeText(this,
346 R.string.add_contact_not_available,
347 Toast.LENGTH_SHORT);
348 toast.show();
349 }
350 return true;
351 case R.id.menu_call_settings:
Yorke Leef74011e2013-08-02 11:30:30 -0700352 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
Yorke Leec3766332013-07-31 11:13:16 -0700353 startActivity(settingsIntent);
354 }
355 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700356 }
357
358 @Override
359 public void onClick(View view) {
360 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700361 case R.id.overflow_menu: {
362 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
363 final Menu menu = popupMenu.getMenu();
Yorke Lee8898cd02013-08-08 10:24:27 -0700364 popupMenu.inflate(R.menu.dialtacts_options);
Yorke Leef420a532013-08-09 15:16:10 -0700365 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Lee0d391b42013-08-09 17:27:00 -0700366 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
Yorke Leec3766332013-07-31 11:13:16 -0700367 popupMenu.setOnMenuItemClickListener(this);
368 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700369 break;
370 }
Yorke Leec3766332013-07-31 11:13:16 -0700371 case R.id.dialpad_button:
372 showDialpadFragment(true);
373 break;
374 case R.id.call_history_on_dialpad_button:
375 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700376 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
377 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700378 final Intent intent = new Intent(this, CallLogActivity.class);
379 startActivity(intent);
380 break;
381 case R.id.search_close_button:
382 // Clear the search field
383 if (!TextUtils.isEmpty(mSearchView.getText())) {
384 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700385 }
386 break;
Yorke Leec3766332013-07-31 11:13:16 -0700387 case R.id.voice_search_button:
388 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
389 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
390 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700391 default: {
392 Log.wtf(TAG, "Unexpected onClick event from " + view);
393 break;
394 }
395 }
396 }
397
Yorke Leec3766332013-07-31 11:13:16 -0700398 @Override
399 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
400 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
401 if (resultCode == RESULT_OK) {
402 final ArrayList<String> matches = data.getStringArrayListExtra(
403 RecognizerIntent.EXTRA_RESULTS);
404 if (matches.size() > 0) {
405 final String match = matches.get(0);
406 mSearchView.setText(match);
407 } else {
408 Log.e(TAG, "Voice search - nothing heard");
409 }
410 } else {
411 Log.e(TAG, "Voice search failed");
412 }
413 }
414 super.onActivityResult(requestCode, resultCode, data);
415 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700416
Yorke Leec3766332013-07-31 11:13:16 -0700417 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700418 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700419 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700420 if (animate) {
421 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700422 } else {
423 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700424 }
425 ft.show(mDialpadFragment);
426 ft.commit();
427 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700428
Yorke Leec3766332013-07-31 11:13:16 -0700429 private void hideDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700430 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700431 final FragmentTransaction ft = getFragmentManager().beginTransaction();
432 if (animate) {
433 ft.setCustomAnimations(0, R.anim.slide_out);
434 }
435 ft.hide(mDialpadFragment);
436 ft.commit();
437 }
438
Chiao Cheng94b10b52012-08-17 16:59:12 -0700439 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700440 mSearchViewContainer = findViewById(R.id.search_view_container);
441 mSearchViewCloseButton = findViewById(R.id.search_close_button);
442 mSearchViewCloseButton.setOnClickListener(this);
443 mVoiceSearchButton = findViewById(R.id.voice_search_button);
444 mVoiceSearchButton.setOnClickListener(this);
445 mSearchView = (EditText) findViewById(R.id.search_view);
446 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
447 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
448 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700449 @Override
450 public void onFocusChange(View view, boolean hasFocus) {
451 if (hasFocus) {
452 showInputMethod(view.findFocus());
453 }
454 }
455 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700456 }
457
Yorke Leec3766332013-07-31 11:13:16 -0700458 private void hideDialpadFragmentIfNecessary() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700459 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leec3766332013-07-31 11:13:16 -0700460 hideDialpadFragment(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700461 }
462 }
463
Yorke Leec3766332013-07-31 11:13:16 -0700464 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
465 @Override
466 public void onAnimationEnd(Animator animation) {
467 mSearchViewContainer.setVisibility(View.GONE);
468 }
469 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700470
Yorke Leec3766332013-07-31 11:13:16 -0700471 public void hideSearchBar() {
472 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700473 }
474
Yorke Leec3766332013-07-31 11:13:16 -0700475 public void hideSearchBar(boolean shiftView) {
476 if (shiftView) {
477 mSearchViewContainer.animate().cancel();
478 mSearchViewContainer.setAlpha(1);
479 mSearchViewContainer.setTranslationY(0);
480 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
481 .setDuration(200).setListener(mHideListener);
482
Yorke Leeef2b7382013-08-09 17:39:25 -0700483 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
484 mBottomPaddingView.setVisibility(View.VISIBLE);
485 return;
486 }
487
Yorke Leec3766332013-07-31 11:13:16 -0700488 mPhoneFavoriteFragment.getView().animate().withLayer()
489 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
490 new AnimatorListenerAdapter() {
491 @Override
492 public void onAnimationEnd(Animator animation) {
493 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeef2b7382013-08-09 17:39:25 -0700494 if (mPhoneFavoriteFragment.getView() != null) {
495 mPhoneFavoriteFragment.getView().setTranslationY(0);
496 }
Yorke Leec3766332013-07-31 11:13:16 -0700497 }
498 });
499 } else {
500 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
501 }
502 }
503
504 public void showSearchBar() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700505
506
Yorke Leec3766332013-07-31 11:13:16 -0700507 mSearchViewContainer.animate().cancel();
508 mSearchViewContainer.setAlpha(0);
509 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
510 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
511 .setListener(new AnimatorListenerAdapter() {
512 @Override
513 public void onAnimationStart(Animator animation) {
514 mSearchViewContainer.setVisibility(View.VISIBLE);
515 }
516 });
517
Yorke Leeef2b7382013-08-09 17:39:25 -0700518 // If the favorites fragment hasn't been fully created before the dialpad fragment
519 // is hidden (i.e. onResume), don't bother animating
520 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
521 mBottomPaddingView.setVisibility(View.GONE);
522 return;
523 }
Yorke Leec3766332013-07-31 11:13:16 -0700524 mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
525 mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
526 .setListener(
527 new AnimatorListenerAdapter() {
528 @Override
529 public void onAnimationStart(Animator animation) {
530 mBottomPaddingView.setVisibility(View.GONE);
531 }
532 });
533 }
534
535
536 public void setupFakeActionBarItemsForFavoritesFragment() {
537 mMenuButton = findViewById(R.id.overflow_menu);
538 if (mMenuButton != null) {
539 mMenuButton.setOnClickListener(this);
540 }
541
542 mCallHistoryButton = findViewById(R.id.call_history_button);
543 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
544 mCallHistoryButton.setOnClickListener(this);
545
546 mDialpadButton = findViewById(R.id.dialpad_button);
547 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
548 mDialpadButton.setOnClickListener(this);
549 }
550
551 public void setupFakeActionBarItemsForDialpadFragment() {
552 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
553 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700554 }
555
556 private void fixIntent(Intent intent) {
557 // This should be cleaned up: the call key used to send an Intent
558 // that just said to go to the recent calls list. It now sends this
559 // abstract action, but this class hasn't been rewritten to deal with it.
560 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
561 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
562 intent.putExtra("call_key", true);
563 setIntent(intent);
564 }
565 }
566
Chiao Cheng94b10b52012-08-17 16:59:12 -0700567 /**
568 * Returns true if the intent is due to hitting the green send key (hardware call button:
569 * KEYCODE_CALL) while in a call.
570 *
571 * @param intent the intent that launched this activity
572 * @param recentCallsRequest true if the intent is requesting to view recent calls
573 * @return true if the intent is due to hitting the green send key while in a call
574 */
Yorke Leec3766332013-07-31 11:13:16 -0700575 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700576 // If there is a call in progress go to the call screen
577 if (recentCallsRequest) {
578 final boolean callKey = intent.getBooleanExtra("call_key", false);
579
580 try {
581 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
582 if (callKey && phone != null && phone.showCallScreen()) {
583 return true;
584 }
585 } catch (RemoteException e) {
586 Log.e(TAG, "Failed to handle send while in call", e);
587 }
588 }
589
590 return false;
591 }
592
593 /**
594 * Sets the current tab based on the intent's request type
595 *
596 * @param intent Intent that contains information about which tab should be selected
597 */
Yorke Leec3766332013-07-31 11:13:16 -0700598 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700599 // If we got here by hitting send and we're in call forward along to the in-call activity
600 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
601 getContentResolver()));
602 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
603 finish();
604 return;
605 }
606
Yorke Lee98702de2013-08-06 12:03:32 -0700607 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700608 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leeea695cb2013-08-09 11:02:26 -0700609 showDialpadFragment(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700610 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700611 }
612
613 @Override
614 public void onNewIntent(Intent newIntent) {
615 setIntent(newIntent);
616 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700617 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700618 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700619
Chiao Cheng94b10b52012-08-17 16:59:12 -0700620 invalidateOptionsMenu();
621 }
622
623 /** Returns true if the given intent contains a phone number to populate the dialer with */
624 private boolean isDialIntent(Intent intent) {
625 final String action = intent.getAction();
626 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
627 return true;
628 }
629 if (Intent.ACTION_VIEW.equals(action)) {
630 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700631 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700632 return true;
633 }
634 }
635 return false;
636 }
637
638 /**
639 * Returns an appropriate call origin for this Activity. May return null when no call origin
640 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
641 * for remembering the tab in which the user made a phone call, so the external app's DIAL
642 * request should not be counted.)
643 */
644 public String getCallOrigin() {
645 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
646 }
647
648 /**
649 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
650 * This text originally came from a FILTER_CONTACTS_ACTION intent received
651 * by this activity. The stored text will then be cleared after after this
652 * method returns.
653 *
654 * @return The stored filter text
655 */
656 public String getAndClearFilterText() {
657 String filterText = mFilterText;
658 mFilterText = null;
659 return filterText;
660 }
661
662 /**
663 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
664 * This is so child activities can check if they are supposed to display a filter.
665 *
666 * @param intent The intent received in {@link #onNewIntent(Intent)}
667 */
668 private void setupFilterText(Intent intent) {
669 // If the intent was relaunched from history, don't apply the filter text.
670 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
671 return;
672 }
673 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
674 if (filter != null && filter.length() > 0) {
675 mFilterText = filter;
676 }
677 }
678
Yorke Lee8898cd02013-08-08 10:24:27 -0700679 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
680 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700681 @Override
682 public void onContactSelected(Uri contactUri) {
683 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700684 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700685 }
686
687 @Override
688 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700689 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700690 startActivity(intent);
691 }
692 };
693
Yorke Leec3766332013-07-31 11:13:16 -0700694 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
695 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
696 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700697
698 @Override
699 public void startSearch(String initialQuery, boolean selectInitialQuery,
700 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700701 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700702 if (mInSearchUi) {
703 if (mSearchView.hasFocus()) {
704 showInputMethod(mSearchView.findFocus());
705 } else {
706 mSearchView.requestFocus();
707 }
708 } else {
709 enterSearchUi();
710 }
711 } else {
712 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
713 }
Yorke Leec3766332013-07-31 11:13:16 -0700714 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700715
716 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700717 final InputMethodManager imm = (InputMethodManager) getSystemService(
718 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700720 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700721 }
722 }
723
724 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700725 final InputMethodManager imm = (InputMethodManager) getSystemService(
726 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700727 if (imm != null && view != null) {
728 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
729 }
730 }
731
732 /**
Yorke Leec3766332013-07-31 11:13:16 -0700733 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700734 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700735 private void enterSearchUi(boolean smartDialSearch, String query) {
736 if (DEBUG) {
737 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
738 }
739 final String tag = smartDialSearch ? TAG_SMARTDIAL_SEARCH_FRAGMENT :
740 TAG_REGULAR_SEARCH_FRAGMENT;
741
Yorke Leec3766332013-07-31 11:13:16 -0700742 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
743 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744
Yorke Leeef2b7382013-08-09 17:39:25 -0700745 SearchFragment fragment;
746
747 transaction.remove(mPhoneFavoriteFragment);
748 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
749 if (fragment == null) {
750 if (smartDialSearch) {
751 fragment = new SmartDialSearchFragment();
752 } else {
753 fragment = new SearchFragment();
754 }
755 transaction.replace(R.id.dialtacts_frame, fragment, tag);
756 } else {
757 transaction.attach(fragment);
758 }
759
760 transaction.addToBackStack(null);
761 fragment.setQueryString(query, false);
762 transaction.commit();
Yorke Leec3766332013-07-31 11:13:16 -0700763 mInSearchUi = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 }
765
766 /**
Yorke Leec3766332013-07-31 11:13:16 -0700767 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700768 */
Yorke Leec3766332013-07-31 11:13:16 -0700769 private void exitSearchUi() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700770 getFragmentManager().popBackStack();
Yorke Leec3766332013-07-31 11:13:16 -0700771 mInSearchUi = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700772 }
773
774 /** Returns an Intent to launch Call Settings screen */
775 public static Intent getCallSettingsIntent() {
776 final Intent intent = new Intent(Intent.ACTION_MAIN);
777 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
778 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
779 return intent;
780 }
781
782 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700783 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700784 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leec3766332013-07-31 11:13:16 -0700785 hideDialpadFragment(true);
786 } else if (mInSearchUi) {
787 mSearchView.setText(null);
Yorke Leec3766332013-07-31 11:13:16 -0700788 } else if (isTaskRoot()) {
789 // Instead of stopping, simply push this to the back of the stack.
790 // This is only done when running at the top of the stack;
791 // otherwise, we have been launched by someone else so need to
792 // allow the user to go back to the caller.
793 moveTaskToBack(false);
794 } else {
795 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700796 }
Yorke Leec3766332013-07-31 11:13:16 -0700797 }
798
799 @Override
800 public void onDialpadQueryChanged(String query) {
801 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
802 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
803 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700804 if (DEBUG) {
805 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
806 }
807 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
808 // This callback can happen if the dialpad fragment is recreated because of
809 // activity destruction. In that case, don't update the search view because
810 // that would bring the user back to the search fragment regardless of the
811 // previous state of the application. Instead, just return here and let the
812 // fragment manager correctly figure out whatever fragment was last displayed.
813 return;
814 }
Yorke Leec3766332013-07-31 11:13:16 -0700815 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700816 }
817 }
Yorke Leec3766332013-07-31 11:13:16 -0700818
819 @Override
820 public void onListFragmentScrollStateChange(int scrollState) {
821 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
822 hideDialpadFragmentIfNecessary();
823 hideInputMethod(getCurrentFocus());
824 }
825 }
826
827 @Override
828 public void onPhoneFavoriteFragmentStarted() {
829 setupFakeActionBarItemsForFavoritesFragment();
830 }
831
832 @Override
833 public void onDialpadFragmentStarted() {
834 setupFakeActionBarItemsForDialpadFragment();
835 }
836
837 private boolean phoneIsInUse() {
838 final TelephonyManager tm = (TelephonyManager) getSystemService(
839 Context.TELEPHONY_SERVICE);
840 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
841 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700842
843 @Override
844 public void onShowAllContacts() {
845 final Intent intent = new Intent(this, AllContactsActivity.class);
846 startActivity(intent);
847 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700848}