blob: e7a7de90cffdba277c14b156c686af9975ee026d [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 Leef74011e2013-08-02 11:30:30 -070071import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.dialpad.SmartDialNameMatcher;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070073import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070074import com.android.dialer.list.AllContactsActivity;
Yorke Lee8898cd02013-08-08 10:24:27 -070075import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Leec3766332013-07-31 11:13:16 -070076import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leec3766332013-07-31 11:13:16 -070077import com.android.dialer.list.SmartDialSearchFragment;
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;
81
Chiao Cheng94b10b52012-08-17 16:59:12 -070082/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070083 * The dialer tab's title is 'phone', a more common name (see strings.xml).
84 */
Yorke Leec3766332013-07-31 11:13:16 -070085public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070086 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070087 OnListFragmentScrolledListener,
Yorke Lee8898cd02013-08-08 10:24:27 -070088 PhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener,
Yorke Lee8dd62002013-08-08 15:57:20 -070089 DialpadFragment.OnDialpadFragmentStartedListener,
90 PhoneFavoriteFragment.OnShowAllContactsListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070091 private static final String TAG = "DialtactsActivity";
92
93 public static final boolean DEBUG = false;
94
Yorke Leef74011e2013-08-02 11:30:30 -070095 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
96
Chiao Cheng94b10b52012-08-17 16:59:12 -070097 /** Used to open Call Setting */
98 private static final String PHONE_PACKAGE = "com.android.phone";
99 private static final String CALL_SETTINGS_CLASS_NAME =
100 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101 /** @see #getCallOrigin() */
102 private static final String CALL_ORIGIN_DIALTACTS =
103 "com.android.dialer.DialtactsActivity";
104
Yorke Leeef2b7382013-08-09 17:39:25 -0700105 private static final String KEY_IN_SEARCH_UI = "in_search_ui";
106 private static final String KEY_SEARCH_QUERY = "search_query";
107 private static final String KEY_FIRST_LAUNCH = "first_launch";
108
Yorke Leec3766332013-07-31 11:13:16 -0700109 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
110 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
111 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
112 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700113
Chiao Cheng94b10b52012-08-17 16:59:12 -0700114 /**
115 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
116 */
117 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
118
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
120
Yorke Leec3766332013-07-31 11:13:16 -0700121 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700122
123 private String mFilterText;
124
Yorke Leec3766332013-07-31 11:13:16 -0700125 /**
126 * The main fragment displaying the user's favorites and frequent contacts
127 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700128 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700129
130 /**
Yorke Leec3766332013-07-31 11:13:16 -0700131 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700132 */
Yorke Leef74011e2013-08-02 11:30:30 -0700133 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700134
135 /**
136 * Fragment for searching phone numbers using the alphanumeric keyboard.
137 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700138 private SearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700139
140 /**
141 * Fragment for searching phone numbers using the dialpad.
142 */
143 private SmartDialSearchFragment mSmartDialSearchFragment;
144
Yorke Leec3766332013-07-31 11:13:16 -0700145 private View mMenuButton;
146 private View mCallHistoryButton;
147 private View mDialpadButton;
148
149 // Padding view used to shift the fragments up when the dialpad is shown.
150 private View mBottomPaddingView;
151
Chiao Cheng94b10b52012-08-17 16:59:12 -0700152 /**
153 * True when this Activity is in its search UI (with a {@link SearchView} and
154 * {@link PhoneNumberPickerFragment}).
155 */
156 private boolean mInSearchUi;
Yorke Leeef2b7382013-08-09 17:39:25 -0700157 /**
158 * True when this activity has been launched for the first time.
159 */
Yorke Lee98702de2013-08-06 12:03:32 -0700160 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700161 private View mSearchViewContainer;
162 private View mSearchViewCloseButton;
163 private View mVoiceSearchButton;
164 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700165
Yorke Leeef2b7382013-08-09 17:39:25 -0700166 private String mSearchQuery;
167
Chiao Cheng94b10b52012-08-17 16:59:12 -0700168 /**
169 * Listener used when one of phone numbers in search UI is selected. This will initiate a
170 * phone call using the phone number.
171 */
172 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
173 new OnPhoneNumberPickerActionListener() {
174 @Override
175 public void onPickPhoneNumberAction(Uri dataUri) {
176 // Specify call-origin so that users will see the previous tab instead of
177 // CallLog screen (search UI will be automatically exited).
178 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700179 DialtactsActivity.this, dataUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700180 }
181
182 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700183 public void onCallNumberDirectly(String phoneNumber) {
184 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
185 startActivity(intent);
186 }
187
188 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700189 public void onShortcutIntentCreated(Intent intent) {
190 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
191 }
192
193 @Override
194 public void onHomeInActionBarSelected() {
195 exitSearchUi();
196 }
197 };
198
199 /**
200 * Listener used to send search queries to the phone search fragment.
201 */
Yorke Leec3766332013-07-31 11:13:16 -0700202 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
203 @Override
204 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
205 }
206
207 @Override
208 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700209 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700210 if (newText.equals(mSearchQuery)) {
211 // If the query hasn't changed (perhaps due to activity being destroyed
212 // and restored, or user launching the same DIAL intent twice), then there is
213 // no need to do anything here.
214 return;
215 }
216 mSearchQuery = newText;
217 if (DEBUG) {
218 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
219 }
220 final boolean smartDialSearch = isDialpadShowing();
221
Yorke Leec3766332013-07-31 11:13:16 -0700222 // Show search result with non-empty text. Show a bare list otherwise.
223 if (TextUtils.isEmpty(newText) && mInSearchUi) {
224 exitSearchUi();
225 mSearchViewCloseButton.setVisibility(View.GONE);
226 return;
227 } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700228 enterSearchUi(smartDialSearch, newText);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700229 }
230
Yorke Leeef2b7382013-08-09 17:39:25 -0700231 if (smartDialSearch && mSmartDialSearchFragment != null) {
232 mSmartDialSearchFragment.setQueryString(newText, false);
233 } else if (mRegularSearchFragment != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700234 mRegularSearchFragment.setQueryString(newText, false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700235 }
Yorke Leec3766332013-07-31 11:13:16 -0700236 mSearchViewCloseButton.setVisibility(View.VISIBLE);
237 return;
238 }
239
240 @Override
241 public void afterTextChanged(Editable s) {
242 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700243 };
244
Yorke Leec3766332013-07-31 11:13:16 -0700245 private boolean isDialpadShowing() {
246 return mDialpadFragment != null && mDialpadFragment.isVisible();
247 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700248
249 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700250 protected void onCreate(Bundle savedInstanceState) {
251 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700252 mFirstLaunch = true;
253
Chiao Cheng94b10b52012-08-17 16:59:12 -0700254 final Intent intent = getIntent();
255 fixIntent(intent);
256
Yorke Lee8898cd02013-08-08 10:24:27 -0700257 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700258
Yorke Leec3766332013-07-31 11:13:16 -0700259 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700260
Yorke Leeef2b7382013-08-09 17:39:25 -0700261 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
262 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700263 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700264 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
265 phoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700266
Yorke Leec3766332013-07-31 11:13:16 -0700267 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700268 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
269 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700270 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700271 } else {
272 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
273 mInSearchUi = savedInstanceState.getBoolean(KEY_IN_SEARCH_UI);
274 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275 }
276
Yorke Leec3766332013-07-31 11:13:16 -0700277 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
278 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700279
Chiao Cheng94b10b52012-08-17 16:59:12 -0700280 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700281 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700282 setupFilterText(intent);
283 }
284 }
285
286 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700287 protected void onResume() {
288 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700289 if (mFirstLaunch) {
290 displayFragment(getIntent());
291 }
292 mFirstLaunch = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700293 }
294
295 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700296 protected void onSaveInstanceState(Bundle outState) {
297 super.onSaveInstanceState(outState);
298 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
299 outState.putBoolean(KEY_IN_SEARCH_UI, mInSearchUi);
300 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
301 }
302
303 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700304 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700305 if (fragment instanceof DialpadFragment) {
306 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700307 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700308 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700309 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700310 } else if (fragment instanceof SmartDialSearchFragment) {
311 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700312 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
313 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700314 } else if (fragment instanceof SearchFragment) {
315 mRegularSearchFragment = (SearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700316 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
317 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700318 } else if (fragment instanceof PhoneFavoriteFragment) {
319 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700320 }
Yorke Leec3766332013-07-31 11:13:16 -0700321 }
322
323 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700324 public boolean onMenuItemClick(MenuItem item) {
325 switch (item.getItemId()) {
326 case R.id.menu_import_export:
327 // We hard-code the "contactsAreAvailable" argument because doing it properly would
328 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
329 // now in Dialtacts for (potential) performance reasons. Compare with how it is
330 // done in {@link PeopleActivity}.
331 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700332 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700333 return true;
334 case R.id.menu_clear_frequents:
335 ClearFrequentsDialog.show(getFragmentManager());
336 return true;
337 case R.id.add_contact:
338 try {
339 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
340 } catch (ActivityNotFoundException e) {
341 Toast toast = Toast.makeText(this,
342 R.string.add_contact_not_available,
343 Toast.LENGTH_SHORT);
344 toast.show();
345 }
346 return true;
347 case R.id.menu_call_settings:
Yorke Leef74011e2013-08-02 11:30:30 -0700348 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
Yorke Leec3766332013-07-31 11:13:16 -0700349 startActivity(settingsIntent);
350 }
351 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700352 }
353
354 @Override
355 public void onClick(View view) {
356 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700357 case R.id.overflow_menu: {
358 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
359 final Menu menu = popupMenu.getMenu();
Yorke Lee8898cd02013-08-08 10:24:27 -0700360 popupMenu.inflate(R.menu.dialtacts_options);
Yorke Leef420a532013-08-09 15:16:10 -0700361 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Lee0d391b42013-08-09 17:27:00 -0700362 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
Yorke Leec3766332013-07-31 11:13:16 -0700363 popupMenu.setOnMenuItemClickListener(this);
364 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700365 break;
366 }
Yorke Leec3766332013-07-31 11:13:16 -0700367 case R.id.dialpad_button:
368 showDialpadFragment(true);
369 break;
370 case R.id.call_history_on_dialpad_button:
371 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700372 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
373 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700374 final Intent intent = new Intent(this, CallLogActivity.class);
375 startActivity(intent);
376 break;
377 case R.id.search_close_button:
378 // Clear the search field
379 if (!TextUtils.isEmpty(mSearchView.getText())) {
380 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700381 }
382 break;
Yorke Leec3766332013-07-31 11:13:16 -0700383 case R.id.voice_search_button:
384 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
385 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
386 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700387 default: {
388 Log.wtf(TAG, "Unexpected onClick event from " + view);
389 break;
390 }
391 }
392 }
393
Yorke Leec3766332013-07-31 11:13:16 -0700394 @Override
395 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
396 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
397 if (resultCode == RESULT_OK) {
398 final ArrayList<String> matches = data.getStringArrayListExtra(
399 RecognizerIntent.EXTRA_RESULTS);
400 if (matches.size() > 0) {
401 final String match = matches.get(0);
402 mSearchView.setText(match);
403 } else {
404 Log.e(TAG, "Voice search - nothing heard");
405 }
406 } else {
407 Log.e(TAG, "Voice search failed");
408 }
409 }
410 super.onActivityResult(requestCode, resultCode, data);
411 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700412
Yorke Leec3766332013-07-31 11:13:16 -0700413 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700414 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700415 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700416 if (animate) {
417 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700418 } else {
419 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700420 }
421 ft.show(mDialpadFragment);
422 ft.commit();
423 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700424
Yorke Leec3766332013-07-31 11:13:16 -0700425 private void hideDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700426 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700427 final FragmentTransaction ft = getFragmentManager().beginTransaction();
428 if (animate) {
429 ft.setCustomAnimations(0, R.anim.slide_out);
430 }
431 ft.hide(mDialpadFragment);
432 ft.commit();
433 }
434
Chiao Cheng94b10b52012-08-17 16:59:12 -0700435 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700436 mSearchViewContainer = findViewById(R.id.search_view_container);
437 mSearchViewCloseButton = findViewById(R.id.search_close_button);
438 mSearchViewCloseButton.setOnClickListener(this);
439 mVoiceSearchButton = findViewById(R.id.voice_search_button);
440 mVoiceSearchButton.setOnClickListener(this);
441 mSearchView = (EditText) findViewById(R.id.search_view);
442 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
443 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
444 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700445 @Override
446 public void onFocusChange(View view, boolean hasFocus) {
447 if (hasFocus) {
448 showInputMethod(view.findFocus());
449 }
450 }
451 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700452 }
453
Yorke Leec3766332013-07-31 11:13:16 -0700454 private void hideDialpadFragmentIfNecessary() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700455 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leec3766332013-07-31 11:13:16 -0700456 hideDialpadFragment(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700457 }
458 }
459
Yorke Leec3766332013-07-31 11:13:16 -0700460 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
461 @Override
462 public void onAnimationEnd(Animator animation) {
463 mSearchViewContainer.setVisibility(View.GONE);
464 }
465 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700466
Yorke Leec3766332013-07-31 11:13:16 -0700467 public void hideSearchBar() {
468 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700469 }
470
Yorke Leec3766332013-07-31 11:13:16 -0700471 public void hideSearchBar(boolean shiftView) {
472 if (shiftView) {
473 mSearchViewContainer.animate().cancel();
474 mSearchViewContainer.setAlpha(1);
475 mSearchViewContainer.setTranslationY(0);
476 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
477 .setDuration(200).setListener(mHideListener);
478
Yorke Leeef2b7382013-08-09 17:39:25 -0700479 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
480 mBottomPaddingView.setVisibility(View.VISIBLE);
481 return;
482 }
483
Yorke Leec3766332013-07-31 11:13:16 -0700484 mPhoneFavoriteFragment.getView().animate().withLayer()
485 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
486 new AnimatorListenerAdapter() {
487 @Override
488 public void onAnimationEnd(Animator animation) {
489 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeef2b7382013-08-09 17:39:25 -0700490 if (mPhoneFavoriteFragment.getView() != null) {
491 mPhoneFavoriteFragment.getView().setTranslationY(0);
492 }
Yorke Leec3766332013-07-31 11:13:16 -0700493 }
494 });
495 } else {
496 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
497 }
498 }
499
500 public void showSearchBar() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700501
502
Yorke Leec3766332013-07-31 11:13:16 -0700503 mSearchViewContainer.animate().cancel();
504 mSearchViewContainer.setAlpha(0);
505 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
506 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
507 .setListener(new AnimatorListenerAdapter() {
508 @Override
509 public void onAnimationStart(Animator animation) {
510 mSearchViewContainer.setVisibility(View.VISIBLE);
511 }
512 });
513
Yorke Leeef2b7382013-08-09 17:39:25 -0700514 // If the favorites fragment hasn't been fully created before the dialpad fragment
515 // is hidden (i.e. onResume), don't bother animating
516 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
517 mBottomPaddingView.setVisibility(View.GONE);
518 return;
519 }
Yorke Leec3766332013-07-31 11:13:16 -0700520 mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
521 mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
522 .setListener(
523 new AnimatorListenerAdapter() {
524 @Override
525 public void onAnimationStart(Animator animation) {
526 mBottomPaddingView.setVisibility(View.GONE);
527 }
528 });
529 }
530
531
532 public void setupFakeActionBarItemsForFavoritesFragment() {
533 mMenuButton = findViewById(R.id.overflow_menu);
534 if (mMenuButton != null) {
535 mMenuButton.setOnClickListener(this);
536 }
537
538 mCallHistoryButton = findViewById(R.id.call_history_button);
539 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
540 mCallHistoryButton.setOnClickListener(this);
541
542 mDialpadButton = findViewById(R.id.dialpad_button);
543 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
544 mDialpadButton.setOnClickListener(this);
545 }
546
547 public void setupFakeActionBarItemsForDialpadFragment() {
548 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
549 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700550 }
551
552 private void fixIntent(Intent intent) {
553 // This should be cleaned up: the call key used to send an Intent
554 // that just said to go to the recent calls list. It now sends this
555 // abstract action, but this class hasn't been rewritten to deal with it.
556 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
557 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
558 intent.putExtra("call_key", true);
559 setIntent(intent);
560 }
561 }
562
Chiao Cheng94b10b52012-08-17 16:59:12 -0700563 /**
564 * Returns true if the intent is due to hitting the green send key (hardware call button:
565 * KEYCODE_CALL) while in a call.
566 *
567 * @param intent the intent that launched this activity
568 * @param recentCallsRequest true if the intent is requesting to view recent calls
569 * @return true if the intent is due to hitting the green send key while in a call
570 */
Yorke Leec3766332013-07-31 11:13:16 -0700571 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700572 // If there is a call in progress go to the call screen
573 if (recentCallsRequest) {
574 final boolean callKey = intent.getBooleanExtra("call_key", false);
575
576 try {
577 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
578 if (callKey && phone != null && phone.showCallScreen()) {
579 return true;
580 }
581 } catch (RemoteException e) {
582 Log.e(TAG, "Failed to handle send while in call", e);
583 }
584 }
585
586 return false;
587 }
588
589 /**
590 * Sets the current tab based on the intent's request type
591 *
592 * @param intent Intent that contains information about which tab should be selected
593 */
Yorke Leec3766332013-07-31 11:13:16 -0700594 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700595 // If we got here by hitting send and we're in call forward along to the in-call activity
596 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
597 getContentResolver()));
598 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
599 finish();
600 return;
601 }
602
Yorke Lee98702de2013-08-06 12:03:32 -0700603 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700604 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leeea695cb2013-08-09 11:02:26 -0700605 showDialpadFragment(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700606 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700607 }
608
609 @Override
610 public void onNewIntent(Intent newIntent) {
611 setIntent(newIntent);
612 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700613 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700614 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700615
Chiao Cheng94b10b52012-08-17 16:59:12 -0700616 invalidateOptionsMenu();
617 }
618
619 /** Returns true if the given intent contains a phone number to populate the dialer with */
620 private boolean isDialIntent(Intent intent) {
621 final String action = intent.getAction();
622 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
623 return true;
624 }
625 if (Intent.ACTION_VIEW.equals(action)) {
626 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700627 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700628 return true;
629 }
630 }
631 return false;
632 }
633
634 /**
635 * Returns an appropriate call origin for this Activity. May return null when no call origin
636 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
637 * for remembering the tab in which the user made a phone call, so the external app's DIAL
638 * request should not be counted.)
639 */
640 public String getCallOrigin() {
641 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
642 }
643
644 /**
645 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
646 * This text originally came from a FILTER_CONTACTS_ACTION intent received
647 * by this activity. The stored text will then be cleared after after this
648 * method returns.
649 *
650 * @return The stored filter text
651 */
652 public String getAndClearFilterText() {
653 String filterText = mFilterText;
654 mFilterText = null;
655 return filterText;
656 }
657
658 /**
659 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
660 * This is so child activities can check if they are supposed to display a filter.
661 *
662 * @param intent The intent received in {@link #onNewIntent(Intent)}
663 */
664 private void setupFilterText(Intent intent) {
665 // If the intent was relaunched from history, don't apply the filter text.
666 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
667 return;
668 }
669 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
670 if (filter != null && filter.length() > 0) {
671 mFilterText = filter;
672 }
673 }
674
Yorke Lee8898cd02013-08-08 10:24:27 -0700675 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
676 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700677 @Override
678 public void onContactSelected(Uri contactUri) {
679 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700680 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700681 }
682
683 @Override
684 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700685 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700686 startActivity(intent);
687 }
688 };
689
Yorke Leec3766332013-07-31 11:13:16 -0700690 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
691 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
692 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700693
694 @Override
695 public void startSearch(String initialQuery, boolean selectInitialQuery,
696 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700697 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700698 if (mInSearchUi) {
699 if (mSearchView.hasFocus()) {
700 showInputMethod(mSearchView.findFocus());
701 } else {
702 mSearchView.requestFocus();
703 }
704 } else {
705 enterSearchUi();
706 }
707 } else {
708 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
709 }
Yorke Leec3766332013-07-31 11:13:16 -0700710 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700711
712 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700713 final InputMethodManager imm = (InputMethodManager) getSystemService(
714 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700715 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700716 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700717 }
718 }
719
720 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700721 final InputMethodManager imm = (InputMethodManager) getSystemService(
722 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700723 if (imm != null && view != null) {
724 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
725 }
726 }
727
728 /**
Yorke Leec3766332013-07-31 11:13:16 -0700729 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700731 private void enterSearchUi(boolean smartDialSearch, String query) {
732 if (DEBUG) {
733 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
734 }
735 final String tag = smartDialSearch ? TAG_SMARTDIAL_SEARCH_FRAGMENT :
736 TAG_REGULAR_SEARCH_FRAGMENT;
737
Yorke Leec3766332013-07-31 11:13:16 -0700738 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
739 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700740
Yorke Leeef2b7382013-08-09 17:39:25 -0700741 SearchFragment fragment;
742
743 transaction.remove(mPhoneFavoriteFragment);
744 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
745 if (fragment == null) {
746 if (smartDialSearch) {
747 fragment = new SmartDialSearchFragment();
748 } else {
749 fragment = new SearchFragment();
750 }
751 transaction.replace(R.id.dialtacts_frame, fragment, tag);
752 } else {
753 transaction.attach(fragment);
754 }
755
756 transaction.addToBackStack(null);
757 fragment.setQueryString(query, false);
758 transaction.commit();
Yorke Leec3766332013-07-31 11:13:16 -0700759 mInSearchUi = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700760 }
761
762 /**
Yorke Leec3766332013-07-31 11:13:16 -0700763 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 */
Yorke Leec3766332013-07-31 11:13:16 -0700765 private void exitSearchUi() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700766 getFragmentManager().popBackStack();
Yorke Leec3766332013-07-31 11:13:16 -0700767 mInSearchUi = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700768 }
769
770 /** Returns an Intent to launch Call Settings screen */
771 public static Intent getCallSettingsIntent() {
772 final Intent intent = new Intent(Intent.ACTION_MAIN);
773 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
774 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
775 return intent;
776 }
777
778 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700779 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700780 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leec3766332013-07-31 11:13:16 -0700781 hideDialpadFragment(true);
782 } else if (mInSearchUi) {
783 mSearchView.setText(null);
Yorke Leec3766332013-07-31 11:13:16 -0700784 } else if (isTaskRoot()) {
785 // Instead of stopping, simply push this to the back of the stack.
786 // This is only done when running at the top of the stack;
787 // otherwise, we have been launched by someone else so need to
788 // allow the user to go back to the caller.
789 moveTaskToBack(false);
790 } else {
791 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700792 }
Yorke Leec3766332013-07-31 11:13:16 -0700793 }
794
795 @Override
796 public void onDialpadQueryChanged(String query) {
797 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
798 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
799 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700800 if (DEBUG) {
801 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
802 }
803 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
804 // This callback can happen if the dialpad fragment is recreated because of
805 // activity destruction. In that case, don't update the search view because
806 // that would bring the user back to the search fragment regardless of the
807 // previous state of the application. Instead, just return here and let the
808 // fragment manager correctly figure out whatever fragment was last displayed.
809 return;
810 }
Yorke Leec3766332013-07-31 11:13:16 -0700811 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700812 }
813 }
Yorke Leec3766332013-07-31 11:13:16 -0700814
815 @Override
816 public void onListFragmentScrollStateChange(int scrollState) {
817 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
818 hideDialpadFragmentIfNecessary();
819 hideInputMethod(getCurrentFocus());
820 }
821 }
822
823 @Override
824 public void onPhoneFavoriteFragmentStarted() {
825 setupFakeActionBarItemsForFavoritesFragment();
826 }
827
828 @Override
829 public void onDialpadFragmentStarted() {
830 setupFakeActionBarItemsForDialpadFragment();
831 }
832
833 private boolean phoneIsInUse() {
834 final TelephonyManager tm = (TelephonyManager) getSystemService(
835 Context.TELEPHONY_SERVICE);
836 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
837 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700838
839 @Override
840 public void onShowAllContacts() {
841 final Intent intent = new Intent(this, AllContactsActivity.class);
842 startActivity(intent);
843 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700844}