blob: 8381c8dd471469fa00bce710b3e328bc07057a68 [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 Leec3766332013-07-31 11:13:16 -0700105 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
106 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
107 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
108 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
109 private static final String TAG_SHOW_ALL_CONTACTS_FRAGMENT = "show_all_contacts";
110
Chiao Cheng94b10b52012-08-17 16:59:12 -0700111 /**
112 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
113 */
114 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
115
Chiao Cheng94b10b52012-08-17 16:59:12 -0700116 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
117
Yorke Leec3766332013-07-31 11:13:16 -0700118 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119
120 private String mFilterText;
121
Yorke Leec3766332013-07-31 11:13:16 -0700122 /**
123 * The main fragment displaying the user's favorites and frequent contacts
124 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700125 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700126
127 /**
Yorke Leec3766332013-07-31 11:13:16 -0700128 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700129 */
Yorke Leef74011e2013-08-02 11:30:30 -0700130 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700131
132 /**
133 * Fragment for searching phone numbers using the alphanumeric keyboard.
134 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700135 private SearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700136
137 /**
138 * Fragment for searching phone numbers using the dialpad.
139 */
140 private SmartDialSearchFragment mSmartDialSearchFragment;
141
Yorke Leec3766332013-07-31 11:13:16 -0700142 private View mMenuButton;
143 private View mCallHistoryButton;
144 private View mDialpadButton;
145
146 // Padding view used to shift the fragments up when the dialpad is shown.
147 private View mBottomPaddingView;
148
Chiao Cheng94b10b52012-08-17 16:59:12 -0700149 /**
150 * True when this Activity is in its search UI (with a {@link SearchView} and
151 * {@link PhoneNumberPickerFragment}).
152 */
153 private boolean mInSearchUi;
Yorke Lee98702de2013-08-06 12:03:32 -0700154 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700155 private View mSearchViewContainer;
156 private View mSearchViewCloseButton;
157 private View mVoiceSearchButton;
158 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700159
160 /**
161 * Listener used when one of phone numbers in search UI is selected. This will initiate a
162 * phone call using the phone number.
163 */
164 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
165 new OnPhoneNumberPickerActionListener() {
166 @Override
167 public void onPickPhoneNumberAction(Uri dataUri) {
168 // Specify call-origin so that users will see the previous tab instead of
169 // CallLog screen (search UI will be automatically exited).
170 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700171 DialtactsActivity.this, dataUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700172 }
173
174 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700175 public void onCallNumberDirectly(String phoneNumber) {
176 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
177 startActivity(intent);
178 }
179
180 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700181 public void onShortcutIntentCreated(Intent intent) {
182 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
183 }
184
185 @Override
186 public void onHomeInActionBarSelected() {
187 exitSearchUi();
188 }
189 };
190
191 /**
192 * Listener used to send search queries to the phone search fragment.
193 */
Yorke Leec3766332013-07-31 11:13:16 -0700194 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
195 @Override
196 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
197 }
198
199 @Override
200 public void onTextChanged(CharSequence s, int start, int before, int count) {
201 // TODO krelease: populate the search fragments with the correct
202 // search query at the correct point in time of the fragment lifecycle.
203 // The current behavior is to simply return to the favorites screen
204 // (when docked), or returning to the Dialer after it has been
205 // swapped out of memory.
206 if (mDialpadFragment == null) return;
207 final boolean smartDialSearch = isDialpadShowing();
208 final String newText = s.toString();
209 // Show search result with non-empty text. Show a bare list otherwise.
210 if (TextUtils.isEmpty(newText) && mInSearchUi) {
211 exitSearchUi();
212 mSearchViewCloseButton.setVisibility(View.GONE);
213 return;
214 } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
215 enterSearchUi(smartDialSearch);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700216 }
217
Yorke Leec3766332013-07-31 11:13:16 -0700218 if (smartDialSearch) {
219 mSmartDialSearchFragment.setQueryString(newText, false);
220 } else {
221 mRegularSearchFragment.setQueryString(newText, false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700222 }
Yorke Leec3766332013-07-31 11:13:16 -0700223 mSearchViewCloseButton.setVisibility(View.VISIBLE);
224 return;
225 }
226
227 @Override
228 public void afterTextChanged(Editable s) {
229 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700230 };
231
Yorke Leec3766332013-07-31 11:13:16 -0700232 private boolean isDialpadShowing() {
233 return mDialpadFragment != null && mDialpadFragment.isVisible();
234 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700235
236 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700237 protected void onCreate(Bundle savedInstanceState) {
238 super.onCreate(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700239
Yorke Lee98702de2013-08-06 12:03:32 -0700240 mFirstLaunch = true;
241
Chiao Cheng94b10b52012-08-17 16:59:12 -0700242 final Intent intent = getIntent();
243 fixIntent(intent);
244
Yorke Lee8898cd02013-08-08 10:24:27 -0700245 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700246
Yorke Leec3766332013-07-31 11:13:16 -0700247 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700248
Yorke Leec3766332013-07-31 11:13:16 -0700249 if (savedInstanceState == null) {
Yorke Lee8898cd02013-08-08 10:24:27 -0700250 mPhoneFavoriteFragment = new PhoneFavoriteFragment();
Yorke Leec3766332013-07-31 11:13:16 -0700251 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700252
Yorke Lee8898cd02013-08-08 10:24:27 -0700253 mRegularSearchFragment = new SearchFragment();
Yorke Leec3766332013-07-31 11:13:16 -0700254 mSmartDialSearchFragment = new SmartDialSearchFragment();
Yorke Leef74011e2013-08-02 11:30:30 -0700255 mDialpadFragment = new DialpadFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700256
Yorke Leec3766332013-07-31 11:13:16 -0700257 // TODO krelease: load fragments on demand instead of creating all of them at run time
258 final FragmentTransaction ft = getFragmentManager().beginTransaction();
259 ft.add(R.id.dialtacts_frame, mPhoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
260 ft.add(R.id.dialtacts_frame, mRegularSearchFragment, TAG_REGULAR_SEARCH_FRAGMENT);
261 ft.add(R.id.dialtacts_frame, mSmartDialSearchFragment, TAG_SMARTDIAL_SEARCH_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700262 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
263 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700264 }
265
Yorke Leec3766332013-07-31 11:13:16 -0700266 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
267 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700268
Chiao Cheng94b10b52012-08-17 16:59:12 -0700269 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700270 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700271 setupFilterText(intent);
272 }
273 }
274
275 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700276 protected void onResume() {
277 super.onResume();
278 final FragmentManager fm = getFragmentManager();
Yorke Lee8898cd02013-08-08 10:24:27 -0700279 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fm.findFragmentByTag(
Yorke Leec3766332013-07-31 11:13:16 -0700280 TAG_FAVORITES_FRAGMENT);
Yorke Leef74011e2013-08-02 11:30:30 -0700281 mDialpadFragment = (DialpadFragment) fm.findFragmentByTag(TAG_DIALPAD_FRAGMENT);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700282
Yorke Lee8898cd02013-08-08 10:24:27 -0700283 mRegularSearchFragment = (SearchFragment) fm.findFragmentByTag(
Yorke Leec3766332013-07-31 11:13:16 -0700284 TAG_REGULAR_SEARCH_FRAGMENT);
285 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
286 mPhoneNumberPickerActionListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700287
Yorke Leec3766332013-07-31 11:13:16 -0700288 mSmartDialSearchFragment = (SmartDialSearchFragment) fm.findFragmentByTag(
289 TAG_SMARTDIAL_SEARCH_FRAGMENT);
290 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
291 mPhoneNumberPickerActionListener);
292
Yorke Lee98702de2013-08-06 12:03:32 -0700293 if (mFirstLaunch) {
294 displayFragment(getIntent());
295 }
296 mFirstLaunch = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700297 }
298
299 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700300 public void onAttachFragment(Fragment fragment) {
Yorke Lee8898cd02013-08-08 10:24:27 -0700301 if (fragment instanceof DialpadFragment || fragment instanceof SearchFragment
Yorke Lee8dd62002013-08-08 15:57:20 -0700302 || fragment instanceof SmartDialSearchFragment) {
Yorke Leec3766332013-07-31 11:13:16 -0700303 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
304 transaction.hide(fragment);
305 transaction.commit();
306 }
307 // TODO krelease: Save some kind of state here to show the appropriate fragment
308 // based on the state of the dialer when it was last paused
309 }
310
311 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700312 public boolean onMenuItemClick(MenuItem item) {
313 switch (item.getItemId()) {
314 case R.id.menu_import_export:
315 // We hard-code the "contactsAreAvailable" argument because doing it properly would
316 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
317 // now in Dialtacts for (potential) performance reasons. Compare with how it is
318 // done in {@link PeopleActivity}.
319 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700320 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700321 return true;
322 case R.id.menu_clear_frequents:
323 ClearFrequentsDialog.show(getFragmentManager());
324 return true;
325 case R.id.add_contact:
326 try {
327 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
328 } catch (ActivityNotFoundException e) {
329 Toast toast = Toast.makeText(this,
330 R.string.add_contact_not_available,
331 Toast.LENGTH_SHORT);
332 toast.show();
333 }
334 return true;
335 case R.id.menu_call_settings:
Yorke Leef74011e2013-08-02 11:30:30 -0700336 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
Yorke Leec3766332013-07-31 11:13:16 -0700337 startActivity(settingsIntent);
338 }
339 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700340 }
341
342 @Override
343 public void onClick(View view) {
344 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700345 case R.id.overflow_menu: {
346 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
347 final Menu menu = popupMenu.getMenu();
Yorke Lee8898cd02013-08-08 10:24:27 -0700348 popupMenu.inflate(R.menu.dialtacts_options);
Yorke Leef420a532013-08-09 15:16:10 -0700349 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Lee0d391b42013-08-09 17:27:00 -0700350 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
Yorke Leec3766332013-07-31 11:13:16 -0700351 popupMenu.setOnMenuItemClickListener(this);
352 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700353 break;
354 }
Yorke Leec3766332013-07-31 11:13:16 -0700355 case R.id.dialpad_button:
356 showDialpadFragment(true);
357 break;
358 case R.id.call_history_on_dialpad_button:
359 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700360 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
361 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700362 final Intent intent = new Intent(this, CallLogActivity.class);
363 startActivity(intent);
364 break;
365 case R.id.search_close_button:
366 // Clear the search field
367 if (!TextUtils.isEmpty(mSearchView.getText())) {
368 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700369 }
370 break;
Yorke Leec3766332013-07-31 11:13:16 -0700371 case R.id.voice_search_button:
372 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
373 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
374 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700375 default: {
376 Log.wtf(TAG, "Unexpected onClick event from " + view);
377 break;
378 }
379 }
380 }
381
Yorke Leec3766332013-07-31 11:13:16 -0700382 @Override
383 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
384 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
385 if (resultCode == RESULT_OK) {
386 final ArrayList<String> matches = data.getStringArrayListExtra(
387 RecognizerIntent.EXTRA_RESULTS);
388 if (matches.size() > 0) {
389 final String match = matches.get(0);
390 mSearchView.setText(match);
391 } else {
392 Log.e(TAG, "Voice search - nothing heard");
393 }
394 } else {
395 Log.e(TAG, "Voice search failed");
396 }
397 }
398 super.onActivityResult(requestCode, resultCode, data);
399 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700400
Yorke Leec3766332013-07-31 11:13:16 -0700401 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700402 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700403 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700404 if (animate) {
405 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700406 } else {
407 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700408 }
409 ft.show(mDialpadFragment);
410 ft.commit();
411 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700412
Yorke Leec3766332013-07-31 11:13:16 -0700413 private void hideDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700414 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700415 final FragmentTransaction ft = getFragmentManager().beginTransaction();
416 if (animate) {
417 ft.setCustomAnimations(0, R.anim.slide_out);
418 }
419 ft.hide(mDialpadFragment);
420 ft.commit();
421 }
422
Chiao Cheng94b10b52012-08-17 16:59:12 -0700423 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700424 mSearchViewContainer = findViewById(R.id.search_view_container);
425 mSearchViewCloseButton = findViewById(R.id.search_close_button);
426 mSearchViewCloseButton.setOnClickListener(this);
427 mVoiceSearchButton = findViewById(R.id.voice_search_button);
428 mVoiceSearchButton.setOnClickListener(this);
429 mSearchView = (EditText) findViewById(R.id.search_view);
430 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
431 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
432 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700433 @Override
434 public void onFocusChange(View view, boolean hasFocus) {
435 if (hasFocus) {
436 showInputMethod(view.findFocus());
437 }
438 }
439 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700440 }
441
Yorke Leec3766332013-07-31 11:13:16 -0700442 private void hideDialpadFragmentIfNecessary() {
443 if (mDialpadFragment.isVisible()) {
444 hideDialpadFragment(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700445 }
446 }
447
Yorke Leec3766332013-07-31 11:13:16 -0700448 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
449 @Override
450 public void onAnimationEnd(Animator animation) {
451 mSearchViewContainer.setVisibility(View.GONE);
452 }
453 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700454
Yorke Leec3766332013-07-31 11:13:16 -0700455 public void hideSearchBar() {
456 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700457 }
458
Yorke Leec3766332013-07-31 11:13:16 -0700459 public void hideSearchBar(boolean shiftView) {
460 if (shiftView) {
461 mSearchViewContainer.animate().cancel();
462 mSearchViewContainer.setAlpha(1);
463 mSearchViewContainer.setTranslationY(0);
464 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
465 .setDuration(200).setListener(mHideListener);
466
467 mPhoneFavoriteFragment.getView().animate().withLayer()
468 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
469 new AnimatorListenerAdapter() {
470 @Override
471 public void onAnimationEnd(Animator animation) {
472 mBottomPaddingView.setVisibility(View.VISIBLE);
473 mPhoneFavoriteFragment.getView().setTranslationY(0);
474 }
475 });
476 } else {
477 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
478 }
479 }
480
481 public void showSearchBar() {
482 // If the favorites fragment hasn't been fully created before the dialpad fragment
483 // is hidden (i.e. onResume), don't bother animating
484 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
485 return;
486 }
487 mSearchViewContainer.animate().cancel();
488 mSearchViewContainer.setAlpha(0);
489 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
490 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
491 .setListener(new AnimatorListenerAdapter() {
492 @Override
493 public void onAnimationStart(Animator animation) {
494 mSearchViewContainer.setVisibility(View.VISIBLE);
495 }
496 });
497
498 mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
499 mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
500 .setListener(
501 new AnimatorListenerAdapter() {
502 @Override
503 public void onAnimationStart(Animator animation) {
504 mBottomPaddingView.setVisibility(View.GONE);
505 }
506 });
507 }
508
509
510 public void setupFakeActionBarItemsForFavoritesFragment() {
511 mMenuButton = findViewById(R.id.overflow_menu);
512 if (mMenuButton != null) {
513 mMenuButton.setOnClickListener(this);
514 }
515
516 mCallHistoryButton = findViewById(R.id.call_history_button);
517 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
518 mCallHistoryButton.setOnClickListener(this);
519
520 mDialpadButton = findViewById(R.id.dialpad_button);
521 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
522 mDialpadButton.setOnClickListener(this);
523 }
524
525 public void setupFakeActionBarItemsForDialpadFragment() {
526 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
527 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700528 }
529
530 private void fixIntent(Intent intent) {
531 // This should be cleaned up: the call key used to send an Intent
532 // that just said to go to the recent calls list. It now sends this
533 // abstract action, but this class hasn't been rewritten to deal with it.
534 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
535 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
536 intent.putExtra("call_key", true);
537 setIntent(intent);
538 }
539 }
540
Chiao Cheng94b10b52012-08-17 16:59:12 -0700541 /**
542 * Returns true if the intent is due to hitting the green send key (hardware call button:
543 * KEYCODE_CALL) while in a call.
544 *
545 * @param intent the intent that launched this activity
546 * @param recentCallsRequest true if the intent is requesting to view recent calls
547 * @return true if the intent is due to hitting the green send key while in a call
548 */
Yorke Leec3766332013-07-31 11:13:16 -0700549 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700550 // If there is a call in progress go to the call screen
551 if (recentCallsRequest) {
552 final boolean callKey = intent.getBooleanExtra("call_key", false);
553
554 try {
555 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
556 if (callKey && phone != null && phone.showCallScreen()) {
557 return true;
558 }
559 } catch (RemoteException e) {
560 Log.e(TAG, "Failed to handle send while in call", e);
561 }
562 }
563
564 return false;
565 }
566
567 /**
568 * Sets the current tab based on the intent's request type
569 *
570 * @param intent Intent that contains information about which tab should be selected
571 */
Yorke Leec3766332013-07-31 11:13:16 -0700572 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700573 // If we got here by hitting send and we're in call forward along to the in-call activity
574 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
575 getContentResolver()));
576 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
577 finish();
578 return;
579 }
580
Yorke Lee98702de2013-08-06 12:03:32 -0700581 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700582 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leeea695cb2013-08-09 11:02:26 -0700583 showDialpadFragment(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700584 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700585 }
586
587 @Override
588 public void onNewIntent(Intent newIntent) {
589 setIntent(newIntent);
590 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700591 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700592 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700593
594 if (mInSearchUi || (mRegularSearchFragment != null && mRegularSearchFragment.isVisible())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700595 exitSearchUi();
596 }
597
Yorke Leec3766332013-07-31 11:13:16 -0700598 // TODO krelease: Handle onNewIntent for all other fragments
599 /*
600 *if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { if (mDialpadFragment != null) {
601 * mDialpadFragment.setStartedFromNewIntent(true); } else { Log.e(TAG,
602 * "DialpadFragment isn't ready yet when the tab is already selected."); } } else if
603 * (mViewPager.getCurrentItem() == TAB_INDEX_CALL_LOG) { if (mCallLogFragment != null) {
604 * mCallLogFragment.configureScreenFromIntent(newIntent); } else { Log.e(TAG,
605 * "CallLogFragment isn't ready yet when the tab is already selected."); } }
606 */
Chiao Cheng94b10b52012-08-17 16:59:12 -0700607 invalidateOptionsMenu();
608 }
609
610 /** Returns true if the given intent contains a phone number to populate the dialer with */
611 private boolean isDialIntent(Intent intent) {
612 final String action = intent.getAction();
613 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
614 return true;
615 }
616 if (Intent.ACTION_VIEW.equals(action)) {
617 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700618 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700619 return true;
620 }
621 }
622 return false;
623 }
624
625 /**
626 * Returns an appropriate call origin for this Activity. May return null when no call origin
627 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
628 * for remembering the tab in which the user made a phone call, so the external app's DIAL
629 * request should not be counted.)
630 */
631 public String getCallOrigin() {
632 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
633 }
634
635 /**
636 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
637 * This text originally came from a FILTER_CONTACTS_ACTION intent received
638 * by this activity. The stored text will then be cleared after after this
639 * method returns.
640 *
641 * @return The stored filter text
642 */
643 public String getAndClearFilterText() {
644 String filterText = mFilterText;
645 mFilterText = null;
646 return filterText;
647 }
648
649 /**
650 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
651 * This is so child activities can check if they are supposed to display a filter.
652 *
653 * @param intent The intent received in {@link #onNewIntent(Intent)}
654 */
655 private void setupFilterText(Intent intent) {
656 // If the intent was relaunched from history, don't apply the filter text.
657 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
658 return;
659 }
660 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
661 if (filter != null && filter.length() > 0) {
662 mFilterText = filter;
663 }
664 }
665
Yorke Lee8898cd02013-08-08 10:24:27 -0700666 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
667 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700668 @Override
669 public void onContactSelected(Uri contactUri) {
670 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700671 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700672 }
673
674 @Override
675 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700676 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700677 startActivity(intent);
678 }
679 };
680
Yorke Leec3766332013-07-31 11:13:16 -0700681 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
682 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
683 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700684
685 @Override
686 public void startSearch(String initialQuery, boolean selectInitialQuery,
687 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700688 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700689 if (mInSearchUi) {
690 if (mSearchView.hasFocus()) {
691 showInputMethod(mSearchView.findFocus());
692 } else {
693 mSearchView.requestFocus();
694 }
695 } else {
696 enterSearchUi();
697 }
698 } else {
699 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
700 }
Yorke Leec3766332013-07-31 11:13:16 -0700701 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700702
703 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700704 final InputMethodManager imm = (InputMethodManager) getSystemService(
705 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700706 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700707 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700708 }
709 }
710
711 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700712 final InputMethodManager imm = (InputMethodManager) getSystemService(
713 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700714 if (imm != null && view != null) {
715 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
716 }
717 }
718
719 /**
Yorke Leec3766332013-07-31 11:13:16 -0700720 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700721 */
Yorke Leec3766332013-07-31 11:13:16 -0700722 private void enterSearchUi(boolean smartDialSearch) {
723 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
724 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
725 transaction.hide(mPhoneFavoriteFragment);
726 if (smartDialSearch) {
727 transaction.show(mSmartDialSearchFragment);
728 } else {
729 transaction.show(mRegularSearchFragment);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730 }
Yorke Leec3766332013-07-31 11:13:16 -0700731 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700732
Yorke Leec3766332013-07-31 11:13:16 -0700733 mInSearchUi = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700734 }
735
736 /**
Yorke Leec3766332013-07-31 11:13:16 -0700737 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700738 */
Yorke Leec3766332013-07-31 11:13:16 -0700739 private void exitSearchUi() {
740 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
741 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
742 transaction.hide(mRegularSearchFragment);
743 transaction.hide(mSmartDialSearchFragment);
744 transaction.show(mPhoneFavoriteFragment);
745 transaction.commit();
746 mInSearchUi = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700747 }
748
749 /** Returns an Intent to launch Call Settings screen */
750 public static Intent getCallSettingsIntent() {
751 final Intent intent = new Intent(Intent.ACTION_MAIN);
752 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
753 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
754 return intent;
755 }
756
757 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700758 public void onBackPressed() {
759 if (mDialpadFragment.isVisible()) {
760 hideDialpadFragment(true);
761 } else if (mInSearchUi) {
762 mSearchView.setText(null);
Yorke Leec3766332013-07-31 11:13:16 -0700763 } else if (isTaskRoot()) {
764 // Instead of stopping, simply push this to the back of the stack.
765 // This is only done when running at the top of the stack;
766 // otherwise, we have been launched by someone else so need to
767 // allow the user to go back to the caller.
768 moveTaskToBack(false);
769 } else {
770 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700771 }
Yorke Leec3766332013-07-31 11:13:16 -0700772 }
773
774 @Override
775 public void onDialpadQueryChanged(String query) {
776 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
777 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
778 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
779 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700780 }
781 }
Yorke Leec3766332013-07-31 11:13:16 -0700782
783 @Override
784 public void onListFragmentScrollStateChange(int scrollState) {
785 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
786 hideDialpadFragmentIfNecessary();
787 hideInputMethod(getCurrentFocus());
788 }
789 }
790
791 @Override
792 public void onPhoneFavoriteFragmentStarted() {
793 setupFakeActionBarItemsForFavoritesFragment();
794 }
795
796 @Override
797 public void onDialpadFragmentStarted() {
798 setupFakeActionBarItemsForDialpadFragment();
799 }
800
801 private boolean phoneIsInUse() {
802 final TelephonyManager tm = (TelephonyManager) getSystemService(
803 Context.TELEPHONY_SERVICE);
804 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
805 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700806
807 @Override
808 public void onShowAllContacts() {
809 final Intent intent = new Intent(this, AllContactsActivity.class);
810 startActivity(intent);
811 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700812}