blob: 7ac1b6e861c98950179d747edffa7e837342ae4b [file] [log] [blame]
Yorke Lee6b049122013-07-16 10:38:02 -07001/*
Yorke Leedfb2eee2013-06-26 18:24:32 -07002 * Copyright (C) 2013 The Android Open Source Project
Yorke Lee6b049122013-07-16 10:38:02 -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 Leedfb2eee2013-06-26 18:24:32 -070019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorListenerAdapter;
Yorke Lee6b049122013-07-16 10:38:02 -070022import android.app.Activity;
23import android.app.backup.BackupManager;
24import android.app.Fragment;
25import android.app.FragmentManager;
26import android.app.FragmentTransaction;
27import android.content.ActivityNotFoundException;
28import android.content.Context;
29import android.content.Intent;
30import android.content.SharedPreferences;
Yorke Leed9999322013-07-19 09:22:03 -070031import android.content.res.Resources;
Yorke Lee6b049122013-07-16 10:38:02 -070032import android.net.Uri;
33import android.os.Bundle;
34import android.os.RemoteException;
35import android.os.ServiceManager;
Yorke Lee6b049122013-07-16 10:38:02 -070036import android.provider.CallLog.Calls;
Yorke Leedfb2eee2013-06-26 18:24:32 -070037import android.provider.ContactsContract;
Yorke Lee6b049122013-07-16 10:38:02 -070038import android.provider.ContactsContract.Contacts;
39import android.provider.ContactsContract.Intents.UI;
Yorke Leedfb2eee2013-06-26 18:24:32 -070040import android.provider.Settings;
Yorke Lee191df332013-07-30 17:21:28 -070041import android.speech.RecognizerIntent;
Christine Chen60914732013-07-30 14:23:54 -070042import android.support.v4.app.NavUtils;
Yorke Lee80a05ed2013-07-26 17:05:04 -070043import android.telephony.TelephonyManager;
Yorke Leed9999322013-07-19 09:22:03 -070044import android.text.Editable;
Yorke Lee6b049122013-07-16 10:38:02 -070045import android.text.TextUtils;
Yorke Leed9999322013-07-19 09:22:03 -070046import android.text.TextWatcher;
Yorke Lee6b049122013-07-16 10:38:02 -070047import android.util.Log;
48import android.view.Menu;
Yorke Lee6b049122013-07-16 10:38:02 -070049import android.view.MenuItem;
Yorke Lee6b049122013-07-16 10:38:02 -070050import android.view.View;
Yorke Lee6b049122013-07-16 10:38:02 -070051import android.view.View.OnFocusChangeListener;
52import android.view.ViewConfiguration;
Yorke Lee6b049122013-07-16 10:38:02 -070053import android.view.inputmethod.InputMethodManager;
Yorke Leedfb2eee2013-06-26 18:24:32 -070054import android.widget.AbsListView.OnScrollListener;
Yorke Leed9999322013-07-19 09:22:03 -070055import android.widget.EditText;
56import android.widget.ImageView;
Yorke Lee6b049122013-07-16 10:38:02 -070057import android.widget.PopupMenu;
58import android.widget.SearchView;
59import android.widget.SearchView.OnCloseListener;
60import android.widget.SearchView.OnQueryTextListener;
61import android.widget.Toast;
62
63import com.android.contacts.common.CallUtil;
64import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leedfb2eee2013-06-26 18:24:32 -070065import com.android.contacts.common.dialog.ClearFrequentsDialog;
66import com.android.contacts.common.interactions.ImportExportDialogFragment;
Yorke Lee6b049122013-07-16 10:38:02 -070067import com.android.contacts.common.list.ContactListItemView;
68import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
69import com.android.contacts.common.list.PhoneNumberPickerFragment;
Yorke Leedfb2eee2013-06-26 18:24:32 -070070import com.android.dialer.calllog.NewCallLogActivity;
71import com.android.dialer.dialpad.NewDialpadFragment;
72import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Lee6b049122013-07-16 10:38:02 -070073import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Leedfb2eee2013-06-26 18:24:32 -070074import com.android.dialer.list.NewPhoneFavoriteFragment;
75import com.android.dialer.list.OnListFragmentScrolledListener;
Christine Chen60914732013-07-30 14:23:54 -070076import com.android.dialer.list.ShowAllContactsFragment;
Yorke Leedfb2eee2013-06-26 18:24:32 -070077import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee6b049122013-07-16 10:38:02 -070078import com.android.internal.telephony.ITelephony;
79
Yorke Lee191df332013-07-30 17:21:28 -070080import java.util.ArrayList;
81
Yorke Lee6b049122013-07-16 10:38:02 -070082/**
Yorke Lee6b049122013-07-16 10:38:02 -070083 * The dialer tab's title is 'phone', a more common name (see strings.xml).
Yorke Leedfb2eee2013-06-26 18:24:32 -070084 *
85 * TODO krelease: All classes currently prefixed with New will replace the original classes or
86 * be renamed more appropriately before shipping.
Yorke Lee6b049122013-07-16 10:38:02 -070087 */
Yorke Leedfb2eee2013-06-26 18:24:32 -070088public class NewDialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
89 NewDialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
90 OnListFragmentScrolledListener,
Yorke Lee714d6c72013-07-26 16:11:39 -070091 NewPhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener,
92 NewDialpadFragment.OnDialpadFragmentStartedListener {
Yorke Lee6b049122013-07-16 10:38:02 -070093 private static final String TAG = "DialtactsActivity";
94
95 public static final boolean DEBUG = false;
96
97 /** 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";
Yorke Lee6b049122013-07-16 10:38:02 -0700101 /** @see #getCallOrigin() */
102 private static final String CALL_ORIGIN_DIALTACTS =
103 "com.android.dialer.DialtactsActivity";
104
Yorke Leedfb2eee2013-06-26 18:24:32 -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";
Christine Chen60914732013-07-30 14:23:54 -0700109 private static final String TAG_SHOW_ALL_CONTACTS_FRAGMENT = "show_all_contacts";
Yorke Leedfb2eee2013-06-26 18:24:32 -0700110
Yorke Lee6b049122013-07-16 10:38:02 -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
Yorke Lee6b049122013-07-16 10:38:02 -0700116 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
117
Yorke Lee191df332013-07-30 17:21:28 -0700118 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
119
Yorke Lee6b049122013-07-16 10:38:02 -0700120 private String mFilterText;
121
Yorke Leedfb2eee2013-06-26 18:24:32 -0700122 /**
123 * The main fragment displaying the user's favorites and frequent contacts
124 */
125 private NewPhoneFavoriteFragment mPhoneFavoriteFragment;
Yorke Lee6b049122013-07-16 10:38:02 -0700126
127 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700128 * Fragment containing the dialpad that slides into view
Yorke Lee6b049122013-07-16 10:38:02 -0700129 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700130 private NewDialpadFragment mDialpadFragment;
131
132 /**
133 * Fragment for searching phone numbers using the alphanumeric keyboard.
134 */
135 private NewSearchFragment mRegularSearchFragment;
136
137 /**
138 * Fragment for searching phone numbers using the dialpad.
139 */
140 private SmartDialSearchFragment mSmartDialSearchFragment;
141
Christine Chen60914732013-07-30 14:23:54 -0700142 private ShowAllContactsFragment mShowAllContactsFragment;
143
Yorke Leedfb2eee2013-06-26 18:24:32 -0700144 private View mMenuButton;
145 private View mCallHistoryButton;
146 private View mDialpadButton;
147
148 // Padding view used to shift the fragments up when the dialpad is shown.
149 private View mBottomPaddingView;
150
Yorke Lee6b049122013-07-16 10:38:02 -0700151 /**
152 * True when this Activity is in its search UI (with a {@link SearchView} and
153 * {@link PhoneNumberPickerFragment}).
154 */
155 private boolean mInSearchUi;
Yorke Leed9999322013-07-19 09:22:03 -0700156 private View mSearchViewContainer;
157 private View mSearchViewCloseButton;
Yorke Lee191df332013-07-30 17:21:28 -0700158 private View mVoiceSearchButton;
Yorke Leed9999322013-07-19 09:22:03 -0700159 private EditText mSearchView;
Yorke Lee6b049122013-07-16 10:38:02 -0700160
Yorke Lee6b049122013-07-16 10:38:02 -0700161 /**
Yorke Lee6b049122013-07-16 10:38:02 -0700162 * Listener used when one of phone numbers in search UI is selected. This will initiate a
163 * phone call using the phone number.
164 */
165 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
166 new OnPhoneNumberPickerActionListener() {
167 @Override
168 public void onPickPhoneNumberAction(Uri dataUri) {
169 // Specify call-origin so that users will see the previous tab instead of
170 // CallLog screen (search UI will be automatically exited).
171 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700172 NewDialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee6b049122013-07-16 10:38:02 -0700173 }
174
175 @Override
176 public void onShortcutIntentCreated(Intent intent) {
177 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
178 }
179
180 @Override
181 public void onHomeInActionBarSelected() {
182 exitSearchUi();
183 }
184 };
185
186 /**
187 * Listener used to send search queries to the phone search fragment.
188 */
Yorke Leed9999322013-07-19 09:22:03 -0700189 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
190 @Override
191 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
192 }
193
194 @Override
195 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Lee936e4432013-07-29 09:54:42 -0700196 // TODO krelease: populate the search fragments with the correct
197 // search query at the correct point in time of the fragment lifecycle.
198 // The current behavior is to simply return to the favorites screen
199 // (when docked), or returning to the Dialer after it has been
200 // swapped out of memory.
201 if (mDialpadFragment == null) return;
202 final boolean smartDialSearch = isDialpadShowing();
Yorke Leed9999322013-07-19 09:22:03 -0700203 final String newText = s.toString();
204 // Show search result with non-empty text. Show a bare list otherwise.
205 if (TextUtils.isEmpty(newText) && mInSearchUi) {
206 exitSearchUi();
207 mSearchViewCloseButton.setVisibility(View.GONE);
208 return;
209 } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
210 enterSearchUi(smartDialSearch);
Yorke Lee6b049122013-07-16 10:38:02 -0700211 }
212
Yorke Lee936e4432013-07-29 09:54:42 -0700213 if (smartDialSearch) {
Yorke Leed9999322013-07-19 09:22:03 -0700214 mSmartDialSearchFragment.setQueryString(newText, false);
215 } else {
216 mRegularSearchFragment.setQueryString(newText, false);
Yorke Lee6b049122013-07-16 10:38:02 -0700217 }
Yorke Leed9999322013-07-19 09:22:03 -0700218 mSearchViewCloseButton.setVisibility(View.VISIBLE);
219 return;
220 }
221
222 @Override
223 public void afterTextChanged(Editable s) {
224 }
Yorke Lee6b049122013-07-16 10:38:02 -0700225 };
226
Yorke Leedfb2eee2013-06-26 18:24:32 -0700227 private boolean isDialpadShowing() {
Yorke Lee936e4432013-07-29 09:54:42 -0700228 return mDialpadFragment != null && mDialpadFragment.isVisible();
Yorke Leedfb2eee2013-06-26 18:24:32 -0700229 }
230
Yorke Lee6b049122013-07-16 10:38:02 -0700231 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700232 protected void onCreate(Bundle savedInstanceState) {
233 super.onCreate(savedInstanceState);
Yorke Lee6b049122013-07-16 10:38:02 -0700234
235 final Intent intent = getIntent();
236 fixIntent(intent);
237
Yorke Leedfb2eee2013-06-26 18:24:32 -0700238 setContentView(R.layout.new_dialtacts_activity);
Yorke Lee6b049122013-07-16 10:38:02 -0700239
Yorke Leedfb2eee2013-06-26 18:24:32 -0700240 getActionBar().hide();
Yorke Lee6b049122013-07-16 10:38:02 -0700241
Yorke Lee936e4432013-07-29 09:54:42 -0700242 if (savedInstanceState == null) {
243 mPhoneFavoriteFragment = new NewPhoneFavoriteFragment();
Yorke Lee936e4432013-07-29 09:54:42 -0700244 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Lee6b049122013-07-16 10:38:02 -0700245
Yorke Lee936e4432013-07-29 09:54:42 -0700246 mRegularSearchFragment = new NewSearchFragment();
247 mSmartDialSearchFragment = new SmartDialSearchFragment();
248 mDialpadFragment = new NewDialpadFragment();
Christine Chen60914732013-07-30 14:23:54 -0700249 mShowAllContactsFragment = new ShowAllContactsFragment();
250 mShowAllContactsFragment.setOnPhoneNumberPickerActionListener(
251 mPhoneNumberPickerActionListener);
Yorke Lee6b049122013-07-16 10:38:02 -0700252
Yorke Lee936e4432013-07-29 09:54:42 -0700253 // TODO krelease: load fragments on demand instead of creating all of them at run time
254 final FragmentTransaction ft = getFragmentManager().beginTransaction();
255 ft.add(R.id.dialtacts_frame, mPhoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
256 ft.add(R.id.dialtacts_frame, mRegularSearchFragment, TAG_REGULAR_SEARCH_FRAGMENT);
257 ft.add(R.id.dialtacts_frame, mSmartDialSearchFragment, TAG_SMARTDIAL_SEARCH_FRAGMENT);
Christine Chen60914732013-07-30 14:23:54 -0700258 ft.add(R.id.dialtacts_frame, mShowAllContactsFragment, TAG_SHOW_ALL_CONTACTS_FRAGMENT);
Yorke Lee936e4432013-07-29 09:54:42 -0700259 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
Yorke Lee936e4432013-07-29 09:54:42 -0700260 ft.commit();
261 }
Yorke Lee6b049122013-07-16 10:38:02 -0700262
Yorke Leedfb2eee2013-06-26 18:24:32 -0700263 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
264 prepareSearchView();
Yorke Lee6b049122013-07-16 10:38:02 -0700265
Yorke Lee80a05ed2013-07-26 17:05:04 -0700266 displayFragment(intent);
Yorke Lee6b049122013-07-16 10:38:02 -0700267
268 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leedfb2eee2013-06-26 18:24:32 -0700269 && savedInstanceState == null) {
Yorke Lee6b049122013-07-16 10:38:02 -0700270 setupFilterText(intent);
271 }
272 }
273
274 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700275 protected void onResume() {
276 super.onResume();
277 final FragmentManager fm = getFragmentManager();
278 mPhoneFavoriteFragment = (NewPhoneFavoriteFragment) fm.findFragmentByTag(
279 TAG_FAVORITES_FRAGMENT);
280 mDialpadFragment = (NewDialpadFragment) fm.findFragmentByTag(TAG_DIALPAD_FRAGMENT);
281
282 mRegularSearchFragment = (NewSearchFragment) fm.findFragmentByTag(
283 TAG_REGULAR_SEARCH_FRAGMENT);
284 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
285 mPhoneNumberPickerActionListener);
Yorke Lee6b049122013-07-16 10:38:02 -0700286
Yorke Leedfb2eee2013-06-26 18:24:32 -0700287 mSmartDialSearchFragment = (SmartDialSearchFragment) fm.findFragmentByTag(
288 TAG_SMARTDIAL_SEARCH_FRAGMENT);
289 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
290 mPhoneNumberPickerActionListener);
Christine Chen60914732013-07-30 14:23:54 -0700291
292 mShowAllContactsFragment = (ShowAllContactsFragment) fm.findFragmentByTag(
293 TAG_SHOW_ALL_CONTACTS_FRAGMENT);
294 mShowAllContactsFragment.setOnPhoneNumberPickerActionListener(
295 mPhoneNumberPickerActionListener);
Yorke Lee936e4432013-07-29 09:54:42 -0700296 }
297
298 @Override
299 public void onAttachFragment(Fragment fragment) {
300 if (fragment instanceof NewDialpadFragment || fragment instanceof NewSearchFragment
Christine Chen60914732013-07-30 14:23:54 -0700301 || fragment instanceof SmartDialSearchFragment
302 || fragment instanceof ShowAllContactsFragment) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700303 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Lee936e4432013-07-29 09:54:42 -0700304 transaction.hide(fragment);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700305 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700306 }
Yorke Lee936e4432013-07-29 09:54:42 -0700307 // 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
Yorke Lee6b049122013-07-16 10:38:02 -0700309 }
310
311 @Override
Christine Chen60914732013-07-30 14:23:54 -0700312 public boolean onOptionsItemSelected(MenuItem item) {
313 switch (item.getItemId()) {
314 // Respond to the action bar's Up/Home button
315 case android.R.id.home:
316 hideAllContactsFragment();
317 }
318 return super.onOptionsItemSelected(item);
319 }
320
321 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700322 public boolean onMenuItemClick(MenuItem item) {
323 switch (item.getItemId()) {
324 case R.id.menu_import_export:
325 // We hard-code the "contactsAreAvailable" argument because doing it properly would
326 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
327 // now in Dialtacts for (potential) performance reasons. Compare with how it is
328 // done in {@link PeopleActivity}.
329 ImportExportDialogFragment.show(getFragmentManager(), true,
330 DialtactsActivity.class);
331 return true;
332 case R.id.menu_clear_frequents:
333 ClearFrequentsDialog.show(getFragmentManager());
334 return true;
335 case R.id.add_contact:
336 try {
337 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
338 } catch (ActivityNotFoundException e) {
Christine Chen60914732013-07-30 14:23:54 -0700339 Toast toast = Toast.makeText(this,
340 R.string.add_contact_not_available,
Yorke Leedfb2eee2013-06-26 18:24:32 -0700341 Toast.LENGTH_SHORT);
342 toast.show();
343 }
344 return true;
345 case R.id.menu_call_settings:
346 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
347 startActivity(settingsIntent);
348 }
349 return false;
Yorke Lee6b049122013-07-16 10:38:02 -0700350 }
351
352 @Override
353 public void onClick(View view) {
354 switch (view.getId()) {
Yorke Lee6b049122013-07-16 10:38:02 -0700355 case R.id.overflow_menu: {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700356 final PopupMenu popupMenu = new PopupMenu(NewDialtactsActivity.this, view);
357 final Menu menu = popupMenu.getMenu();
358 popupMenu.inflate(R.menu.dialtacts_options_new);
359 popupMenu.setOnMenuItemClickListener(this);
360 popupMenu.show();
Yorke Lee6b049122013-07-16 10:38:02 -0700361 break;
362 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700363 case R.id.dialpad_button:
Yorke Lee80a05ed2013-07-26 17:05:04 -0700364 showDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700365 break;
Yorke Lee714d6c72013-07-26 16:11:39 -0700366 case R.id.call_history_on_dialpad_button:
Yorke Leedfb2eee2013-06-26 18:24:32 -0700367 case R.id.call_history_button:
Yorke Lee80a05ed2013-07-26 17:05:04 -0700368 // TODO krelease: This should start an intent with content type
369 // CallLog.Calls.CONTENT_TYPE, once the intent filters for the call log activity
370 // is enabled
Yorke Leedfb2eee2013-06-26 18:24:32 -0700371 final Intent intent = new Intent(this, NewCallLogActivity.class);
372 startActivity(intent);
373 break;
Yorke Leed9999322013-07-19 09:22:03 -0700374 case R.id.search_close_button:
375 // Clear the search field
376 if (!TextUtils.isEmpty(mSearchView.getText())) {
377 mSearchView.setText("");
378 }
379 break;
Yorke Lee191df332013-07-30 17:21:28 -0700380 case R.id.voice_search_button:
381 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
382 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
383 break;
Yorke Lee6b049122013-07-16 10:38:02 -0700384 default: {
385 Log.wtf(TAG, "Unexpected onClick event from " + view);
386 break;
387 }
388 }
389 }
390
Yorke Lee191df332013-07-30 17:21:28 -0700391 @Override
392 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
393 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
394 if (resultCode == RESULT_OK) {
395 final ArrayList<String> matches = data.getStringArrayListExtra(
396 RecognizerIntent.EXTRA_RESULTS);
397 if (matches.size() > 0) {
398 final String match = matches.get(0);
399 mSearchView.setText(match);
400 } else {
401 Log.e(TAG, "Voice search - nothing heard");
402 }
403 } else {
404 Log.e(TAG, "Voice search failed");
405 }
406 }
407 super.onActivityResult(requestCode, resultCode, data);
408 }
409
Yorke Lee80a05ed2013-07-26 17:05:04 -0700410 private void showDialpadFragment(boolean animate) {
Yorke Lee6b049122013-07-16 10:38:02 -0700411 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700412 if (animate) {
413 ft.setCustomAnimations(R.anim.slide_in, 0);
414 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700415 ft.show(mDialpadFragment);
416 ft.commit();
417 }
Yorke Lee6b049122013-07-16 10:38:02 -0700418
Yorke Lee80a05ed2013-07-26 17:05:04 -0700419 private void hideDialpadFragment(boolean animate) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700420 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700421 if (animate) {
422 ft.setCustomAnimations(0, R.anim.slide_out);
423 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700424 ft.hide(mDialpadFragment);
425 ft.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700426 }
427
Christine Chen60914732013-07-30 14:23:54 -0700428 public void showAllContactsFragment() {
429 final FragmentTransaction ft = getFragmentManager().beginTransaction();
430 ft.hide(mPhoneFavoriteFragment);
431 ft.show(mShowAllContactsFragment);
432 // TODO{klp} Add animation
433 ft.commit();
434 hideSearchBar(false);
435 }
436
437 private void hideAllContactsFragment() {
438 final FragmentTransaction ft = getFragmentManager().beginTransaction();
439 ft.hide(mShowAllContactsFragment);
440 ft.show(mPhoneFavoriteFragment);
441 ft.commit();
442 showSearchBar();
443 }
444
Yorke Lee6b049122013-07-16 10:38:02 -0700445 private void prepareSearchView() {
Yorke Leed9999322013-07-19 09:22:03 -0700446 mSearchViewContainer = findViewById(R.id.search_view_container);
447 mSearchViewCloseButton = findViewById(R.id.search_close_button);
Yorke Leed9999322013-07-19 09:22:03 -0700448 mSearchViewCloseButton.setOnClickListener(this);
Yorke Lee191df332013-07-30 17:21:28 -0700449 mVoiceSearchButton = findViewById(R.id.voice_search_button);
450 mVoiceSearchButton.setOnClickListener(this);
Yorke Leed9999322013-07-19 09:22:03 -0700451 mSearchView = (EditText) findViewById(R.id.search_view);
452 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
453 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
454 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Yorke Lee6b049122013-07-16 10:38:02 -0700455 @Override
456 public void onFocusChange(View view, boolean hasFocus) {
457 if (hasFocus) {
458 showInputMethod(view.findFocus());
459 }
460 }
461 });
Yorke Lee6b049122013-07-16 10:38:02 -0700462 }
463
Yorke Leedfb2eee2013-06-26 18:24:32 -0700464 private void hideDialpadFragmentIfNecessary() {
465 if (mDialpadFragment.isVisible()) {
Yorke Lee80a05ed2013-07-26 17:05:04 -0700466 hideDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700467 }
468 }
Yorke Lee6b049122013-07-16 10:38:02 -0700469
Yorke Leedfb2eee2013-06-26 18:24:32 -0700470 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
471 @Override
472 public void onAnimationEnd(Animator animation) {
Yorke Leed9999322013-07-19 09:22:03 -0700473 mSearchViewContainer.setVisibility(View.GONE);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700474 }
475 };
476
477 public void hideSearchBar() {
Christine Chen60914732013-07-30 14:23:54 -0700478 hideSearchBar(true);
479 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700480
Christine Chen60914732013-07-30 14:23:54 -0700481 public void hideSearchBar(boolean shiftView) {
482 if (shiftView) {
483 mSearchViewContainer.animate().cancel();
484 mSearchViewContainer.setAlpha(1);
485 mSearchViewContainer.setTranslationY(0);
486 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
487 .setDuration(200).setListener(mHideListener);
488
489 mPhoneFavoriteFragment.getView().animate().withLayer()
490 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700491 new AnimatorListenerAdapter() {
Christine Chen60914732013-07-30 14:23:54 -0700492 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700493 public void onAnimationEnd(Animator animation) {
494 mBottomPaddingView.setVisibility(View.VISIBLE);
495 mPhoneFavoriteFragment.getView().setTranslationY(0);
496 }
497 });
Christine Chen60914732013-07-30 14:23:54 -0700498 } else {
499 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
500 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700501 }
502
503 public void showSearchBar() {
Yorke Lee936e4432013-07-29 09:54:42 -0700504 // If the favorites fragment hasn't been fully created before the dialpad fragment
505 // is hidden (i.e. onResume), don't bother animating
506 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
507 return;
508 }
Yorke Leed9999322013-07-19 09:22:03 -0700509 mSearchViewContainer.animate().cancel();
510 mSearchViewContainer.setAlpha(0);
511 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
512 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
Yorke Leedfb2eee2013-06-26 18:24:32 -0700513 .setListener(new AnimatorListenerAdapter() {
Christine Chen60914732013-07-30 14:23:54 -0700514 @Override
515 public void onAnimationStart(Animator animation) {
Yorke Leed9999322013-07-19 09:22:03 -0700516 mSearchViewContainer.setVisibility(View.VISIBLE);
Christine Chen60914732013-07-30 14:23:54 -0700517 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700518 });
519
Yorke Leed9999322013-07-19 09:22:03 -0700520 mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
Yorke Leedfb2eee2013-06-26 18:24:32 -0700521 mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
522 .setListener(
523 new AnimatorListenerAdapter() {
Christine Chen60914732013-07-30 14:23:54 -0700524 @Override
525 public void onAnimationStart(Animator animation) {
526 mBottomPaddingView.setVisibility(View.GONE);
527 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700528 });
529 }
530
531
Yorke Lee714d6c72013-07-26 16:11:39 -0700532 public void setupFakeActionBarItemsForFavoritesFragment() {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700533 mMenuButton = findViewById(R.id.overflow_menu);
534 if (mMenuButton != null) {
Yorke Leef00e1cf2013-07-30 16:11:09 -0700535 mMenuButton.setOnClickListener(this);
Yorke Lee6b049122013-07-16 10:38:02 -0700536 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700537
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);
Yorke Lee6b049122013-07-16 10:38:02 -0700545 }
546
Yorke Lee714d6c72013-07-26 16:11:39 -0700547 public void setupFakeActionBarItemsForDialpadFragment() {
Yorke Lee714d6c72013-07-26 16:11:39 -0700548 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
549 callhistoryButton.setOnClickListener(this);
550 }
551
Yorke Lee6b049122013-07-16 10:38:02 -0700552 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
Yorke Lee6b049122013-07-16 10:38:02 -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 Leedfb2eee2013-06-26 18:24:32 -0700571 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Yorke Lee6b049122013-07-16 10:38:02 -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 Leedfb2eee2013-06-26 18:24:32 -0700594 private void displayFragment(Intent intent) {
595 // TODO krelease: Make navigation via intent work by displaying the correct fragment
596 // as appropriate.
597
Yorke Lee6b049122013-07-16 10:38:02 -0700598 // If we got here by hitting send and we're in call forward along to the in-call activity
599 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
600 getContentResolver()));
601 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
602 finish();
603 return;
604 }
Yorke Lee80a05ed2013-07-26 17:05:04 -0700605
606 if ((mDialpadFragment != null && phoneIsInUse())
607 || isDialIntent(intent)) {
608 mDialpadFragment.setStartedFromNewIntent(true);
609 // TODO krelease: This should use showDialpadFragment(false) to avoid animating
610 // the dialpad in. Need to fix the onPreDrawListener in NewDialpadFragment first.
611 showDialpadFragment(true);
612 }
Yorke Lee6b049122013-07-16 10:38:02 -0700613 }
614
615 @Override
616 public void onNewIntent(Intent newIntent) {
617 setIntent(newIntent);
618 fixIntent(newIntent);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700619 displayFragment(newIntent);
Yorke Lee6b049122013-07-16 10:38:02 -0700620 final String action = newIntent.getAction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700621
Yorke Leedfb2eee2013-06-26 18:24:32 -0700622 if (mInSearchUi || (mRegularSearchFragment != null && mRegularSearchFragment.isVisible())) {
Yorke Lee6b049122013-07-16 10:38:02 -0700623 exitSearchUi();
624 }
625
Yorke Leedfb2eee2013-06-26 18:24:32 -0700626 // TODO krelease: Handle onNewIntent for all other fragments
627 /*
628 *if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { if (mDialpadFragment != null) {
629 * mDialpadFragment.setStartedFromNewIntent(true); } else { Log.e(TAG,
630 * "DialpadFragment isn't ready yet when the tab is already selected."); } } else if
631 * (mViewPager.getCurrentItem() == TAB_INDEX_CALL_LOG) { if (mCallLogFragment != null) {
632 * mCallLogFragment.configureScreenFromIntent(newIntent); } else { Log.e(TAG,
633 * "CallLogFragment isn't ready yet when the tab is already selected."); } }
634 */
Yorke Lee6b049122013-07-16 10:38:02 -0700635 invalidateOptionsMenu();
636 }
637
638 /** Returns true if the given intent contains a phone number to populate the dialer with */
639 private boolean isDialIntent(Intent intent) {
640 final String action = intent.getAction();
641 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
642 return true;
643 }
644 if (Intent.ACTION_VIEW.equals(action)) {
645 final Uri data = intent.getData();
646 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
647 return true;
648 }
649 }
650 return false;
651 }
652
653 /**
654 * Returns an appropriate call origin for this Activity. May return null when no call origin
655 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
656 * for remembering the tab in which the user made a phone call, so the external app's DIAL
657 * request should not be counted.)
658 */
659 public String getCallOrigin() {
660 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
661 }
662
663 /**
664 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
665 * This text originally came from a FILTER_CONTACTS_ACTION intent received
666 * by this activity. The stored text will then be cleared after after this
667 * method returns.
668 *
669 * @return The stored filter text
670 */
671 public String getAndClearFilterText() {
672 String filterText = mFilterText;
673 mFilterText = null;
674 return filterText;
675 }
676
677 /**
678 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
679 * This is so child activities can check if they are supposed to display a filter.
680 *
681 * @param intent The intent received in {@link #onNewIntent(Intent)}
682 */
683 private void setupFilterText(Intent intent) {
684 // If the intent was relaunched from history, don't apply the filter text.
685 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
686 return;
687 }
688 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
689 if (filter != null && filter.length() > 0) {
690 mFilterText = filter;
691 }
692 }
693
Yorke Leedfb2eee2013-06-26 18:24:32 -0700694 private final NewPhoneFavoriteFragment.Listener mPhoneFavoriteListener =
695 new NewPhoneFavoriteFragment.Listener() {
Yorke Lee6b049122013-07-16 10:38:02 -0700696 @Override
697 public void onContactSelected(Uri contactUri) {
698 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700699 NewDialtactsActivity.this, contactUri, getCallOrigin());
Yorke Lee6b049122013-07-16 10:38:02 -0700700 }
701
702 @Override
703 public void onCallNumberDirectly(String phoneNumber) {
704 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
705 startActivity(intent);
706 }
707 };
708
Yorke Leedfb2eee2013-06-26 18:24:32 -0700709 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
710 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
711 * be showing when the search key is pressed so there is more state management involved.
Yorke Lee6b049122013-07-16 10:38:02 -0700712
713 @Override
714 public void startSearch(String initialQuery, boolean selectInitialQuery,
715 Bundle appSearchData, boolean globalSearch) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700716 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Yorke Lee6b049122013-07-16 10:38:02 -0700717 if (mInSearchUi) {
718 if (mSearchView.hasFocus()) {
719 showInputMethod(mSearchView.findFocus());
720 } else {
721 mSearchView.requestFocus();
722 }
723 } else {
724 enterSearchUi();
725 }
726 } else {
727 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
728 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700729 }*/
Yorke Lee6b049122013-07-16 10:38:02 -0700730
731 private void showInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700732 final InputMethodManager imm = (InputMethodManager) getSystemService(
733 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700734 if (imm != null) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700735 imm.showSoftInput(view, 0);
Yorke Lee6b049122013-07-16 10:38:02 -0700736 }
737 }
738
739 private void hideInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700740 final InputMethodManager imm = (InputMethodManager) getSystemService(
741 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700742 if (imm != null && view != null) {
743 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
744 }
745 }
746
747 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700748 * Shows the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700749 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700750 private void enterSearchUi(boolean smartDialSearch) {
751 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
752 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
753 transaction.hide(mPhoneFavoriteFragment);
754 if (smartDialSearch) {
755 transaction.show(mSmartDialSearchFragment);
756 } else {
757 transaction.show(mRegularSearchFragment);
Yorke Lee6b049122013-07-16 10:38:02 -0700758 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700759 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700760
Yorke Leedfb2eee2013-06-26 18:24:32 -0700761 mInSearchUi = true;
Yorke Lee6b049122013-07-16 10:38:02 -0700762 }
763
764 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700765 * Hides the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700766 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700767 private void exitSearchUi() {
768 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
769 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
770 transaction.hide(mRegularSearchFragment);
771 transaction.hide(mSmartDialSearchFragment);
772 transaction.show(mPhoneFavoriteFragment);
773 transaction.commit();
774 mInSearchUi = false;
Yorke Lee6b049122013-07-16 10:38:02 -0700775 }
776
777 /** Returns an Intent to launch Call Settings screen */
778 public static Intent getCallSettingsIntent() {
779 final Intent intent = new Intent(Intent.ACTION_MAIN);
780 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
781 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
782 return intent;
783 }
784
785 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700786 public void onBackPressed() {
787 if (mDialpadFragment.isVisible()) {
Yorke Lee80a05ed2013-07-26 17:05:04 -0700788 hideDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700789 } else if (mInSearchUi) {
Yorke Leed9999322013-07-19 09:22:03 -0700790 mSearchView.setText(null);
Christine Chen60914732013-07-30 14:23:54 -0700791 } else if (mShowAllContactsFragment.isVisible()) {
792 hideAllContactsFragment();
Yorke Leedfb2eee2013-06-26 18:24:32 -0700793 } else if (isTaskRoot()) {
794 // Instead of stopping, simply push this to the back of the stack.
795 // This is only done when running at the top of the stack;
796 // otherwise, we have been launched by someone else so need to
797 // allow the user to go back to the caller.
798 moveTaskToBack(false);
799 } else {
800 super.onBackPressed();
Yorke Lee6b049122013-07-16 10:38:02 -0700801 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700802 }
803
804 @Override
805 public void onDialpadQueryChanged(String query) {
806 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
807 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Leed9999322013-07-19 09:22:03 -0700808 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
809 mSearchView.setText(normalizedQuery);
Yorke Lee6b049122013-07-16 10:38:02 -0700810 }
811 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700812
813 @Override
814 public void onListFragmentScrollStateChange(int scrollState) {
815 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
816 hideDialpadFragmentIfNecessary();
817 hideInputMethod(getCurrentFocus());
818 }
819 }
820
821 @Override
822 public void onPhoneFavoriteFragmentStarted() {
Yorke Lee714d6c72013-07-26 16:11:39 -0700823 setupFakeActionBarItemsForFavoritesFragment();
Yorke Leedfb2eee2013-06-26 18:24:32 -0700824 }
Yorke Lee714d6c72013-07-26 16:11:39 -0700825
826 @Override
827 public void onDialpadFragmentStarted() {
828 setupFakeActionBarItemsForDialpadFragment();
829 }
830
Yorke Lee80a05ed2013-07-26 17:05:04 -0700831 private boolean phoneIsInUse() {
832 final TelephonyManager tm = (TelephonyManager) getSystemService(
833 Context.TELEPHONY_SERVICE);
834 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
835 }
Yorke Lee6b049122013-07-16 10:38:02 -0700836}