blob: 89e5b5a6b31b063f1c87e1fd9a52e73c26849b83 [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 Leec3766332013-07-31 11:13:16 -070074import com.android.dialer.list.NewPhoneFavoriteFragment;
75import com.android.dialer.list.OnListFragmentScrolledListener;
76import com.android.dialer.list.ShowAllContactsFragment;
77import 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).
Yorke Leec3766332013-07-31 11:13:16 -070084 *
85 * TODO krelease: All classes currently prefixed with New will replace the original classes or
86 * be renamed more appropriately before shipping.
Chiao Cheng94b10b52012-08-17 16:59:12 -070087 */
Yorke Leec3766332013-07-31 11:13:16 -070088public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070089 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070090 OnListFragmentScrolledListener,
91 NewPhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener,
Yorke Leef74011e2013-08-02 11:30:30 -070092 DialpadFragment.OnDialpadFragmentStartedListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070093 private static final String TAG = "DialtactsActivity";
94
95 public static final boolean DEBUG = false;
96
Yorke Leef74011e2013-08-02 11:30:30 -070097 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
98
Chiao Cheng94b10b52012-08-17 16:59:12 -070099 /** Used to open Call Setting */
100 private static final String PHONE_PACKAGE = "com.android.phone";
101 private static final String CALL_SETTINGS_CLASS_NAME =
102 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103 /** @see #getCallOrigin() */
104 private static final String CALL_ORIGIN_DIALTACTS =
105 "com.android.dialer.DialtactsActivity";
106
Yorke Leec3766332013-07-31 11:13:16 -0700107 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
108 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
109 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
110 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
111 private static final String TAG_SHOW_ALL_CONTACTS_FRAGMENT = "show_all_contacts";
112
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 /**
114 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
115 */
116 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
117
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
119
Yorke Leec3766332013-07-31 11:13:16 -0700120 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121
122 private String mFilterText;
123
Yorke Leec3766332013-07-31 11:13:16 -0700124 /**
125 * The main fragment displaying the user's favorites and frequent contacts
126 */
127 private NewPhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700128
129 /**
Yorke Leec3766332013-07-31 11:13:16 -0700130 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700131 */
Yorke Leef74011e2013-08-02 11:30:30 -0700132 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700133
134 /**
135 * Fragment for searching phone numbers using the alphanumeric keyboard.
136 */
137 private NewSearchFragment mRegularSearchFragment;
138
139 /**
140 * Fragment for searching phone numbers using the dialpad.
141 */
142 private SmartDialSearchFragment mSmartDialSearchFragment;
143
144 private ShowAllContactsFragment mShowAllContactsFragment;
145
146 private View mMenuButton;
147 private View mCallHistoryButton;
148 private View mDialpadButton;
149
150 // Padding view used to shift the fragments up when the dialpad is shown.
151 private View mBottomPaddingView;
152
Chiao Cheng94b10b52012-08-17 16:59:12 -0700153 /**
154 * True when this Activity is in its search UI (with a {@link SearchView} and
155 * {@link PhoneNumberPickerFragment}).
156 */
157 private boolean mInSearchUi;
Yorke Lee98702de2013-08-06 12:03:32 -0700158 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700159 private View mSearchViewContainer;
160 private View mSearchViewCloseButton;
161 private View mVoiceSearchButton;
162 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700163
164 /**
165 * Listener used when one of phone numbers in search UI is selected. This will initiate a
166 * phone call using the phone number.
167 */
168 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
169 new OnPhoneNumberPickerActionListener() {
170 @Override
171 public void onPickPhoneNumberAction(Uri dataUri) {
172 // Specify call-origin so that users will see the previous tab instead of
173 // CallLog screen (search UI will be automatically exited).
174 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700175 DialtactsActivity.this, dataUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700176 }
177
178 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700179 public void onCallNumberDirectly(String phoneNumber) {
180 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
181 startActivity(intent);
182 }
183
184 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700185 public void onShortcutIntentCreated(Intent intent) {
186 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
187 }
188
189 @Override
190 public void onHomeInActionBarSelected() {
191 exitSearchUi();
192 }
193 };
194
195 /**
196 * Listener used to send search queries to the phone search fragment.
197 */
Yorke Leec3766332013-07-31 11:13:16 -0700198 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
199 @Override
200 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
201 }
202
203 @Override
204 public void onTextChanged(CharSequence s, int start, int before, int count) {
205 // TODO krelease: populate the search fragments with the correct
206 // search query at the correct point in time of the fragment lifecycle.
207 // The current behavior is to simply return to the favorites screen
208 // (when docked), or returning to the Dialer after it has been
209 // swapped out of memory.
210 if (mDialpadFragment == null) return;
211 final boolean smartDialSearch = isDialpadShowing();
212 final String newText = s.toString();
213 // Show search result with non-empty text. Show a bare list otherwise.
214 if (TextUtils.isEmpty(newText) && mInSearchUi) {
215 exitSearchUi();
216 mSearchViewCloseButton.setVisibility(View.GONE);
217 return;
218 } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
219 enterSearchUi(smartDialSearch);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700220 }
221
Yorke Leec3766332013-07-31 11:13:16 -0700222 if (smartDialSearch) {
223 mSmartDialSearchFragment.setQueryString(newText, false);
224 } else {
225 mRegularSearchFragment.setQueryString(newText, false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700226 }
Yorke Leec3766332013-07-31 11:13:16 -0700227 mSearchViewCloseButton.setVisibility(View.VISIBLE);
228 return;
229 }
230
231 @Override
232 public void afterTextChanged(Editable s) {
233 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700234 };
235
Yorke Leec3766332013-07-31 11:13:16 -0700236 private boolean isDialpadShowing() {
237 return mDialpadFragment != null && mDialpadFragment.isVisible();
238 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700239
240 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700241 protected void onCreate(Bundle savedInstanceState) {
242 super.onCreate(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700243
Yorke Lee98702de2013-08-06 12:03:32 -0700244 mFirstLaunch = true;
245
Chiao Cheng94b10b52012-08-17 16:59:12 -0700246 final Intent intent = getIntent();
247 fixIntent(intent);
248
Yorke Leec3766332013-07-31 11:13:16 -0700249 setContentView(R.layout.new_dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700250
Yorke Leec3766332013-07-31 11:13:16 -0700251 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700252
Yorke Leec3766332013-07-31 11:13:16 -0700253 if (savedInstanceState == null) {
254 mPhoneFavoriteFragment = new NewPhoneFavoriteFragment();
255 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700256
Yorke Leec3766332013-07-31 11:13:16 -0700257 mRegularSearchFragment = new NewSearchFragment();
258 mSmartDialSearchFragment = new SmartDialSearchFragment();
Yorke Leef74011e2013-08-02 11:30:30 -0700259 mDialpadFragment = new DialpadFragment();
Yorke Leec3766332013-07-31 11:13:16 -0700260 mShowAllContactsFragment = new ShowAllContactsFragment();
261 mShowAllContactsFragment.setOnPhoneNumberPickerActionListener(
262 mPhoneNumberPickerActionListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700263
Yorke Leec3766332013-07-31 11:13:16 -0700264 // TODO krelease: load fragments on demand instead of creating all of them at run time
265 final FragmentTransaction ft = getFragmentManager().beginTransaction();
266 ft.add(R.id.dialtacts_frame, mPhoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
267 ft.add(R.id.dialtacts_frame, mRegularSearchFragment, TAG_REGULAR_SEARCH_FRAGMENT);
268 ft.add(R.id.dialtacts_frame, mSmartDialSearchFragment, TAG_SMARTDIAL_SEARCH_FRAGMENT);
269 ft.add(R.id.dialtacts_frame, mShowAllContactsFragment, TAG_SHOW_ALL_CONTACTS_FRAGMENT);
270 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
271 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700272 }
273
Yorke Leec3766332013-07-31 11:13:16 -0700274 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
275 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700276
Chiao Cheng94b10b52012-08-17 16:59:12 -0700277 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700278 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700279 setupFilterText(intent);
280 }
281 }
282
283 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700284 protected void onResume() {
285 super.onResume();
286 final FragmentManager fm = getFragmentManager();
287 mPhoneFavoriteFragment = (NewPhoneFavoriteFragment) fm.findFragmentByTag(
288 TAG_FAVORITES_FRAGMENT);
Yorke Leef74011e2013-08-02 11:30:30 -0700289 mDialpadFragment = (DialpadFragment) fm.findFragmentByTag(TAG_DIALPAD_FRAGMENT);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700290
Yorke Leec3766332013-07-31 11:13:16 -0700291 mRegularSearchFragment = (NewSearchFragment) fm.findFragmentByTag(
292 TAG_REGULAR_SEARCH_FRAGMENT);
293 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
294 mPhoneNumberPickerActionListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700295
Yorke Leec3766332013-07-31 11:13:16 -0700296 mSmartDialSearchFragment = (SmartDialSearchFragment) fm.findFragmentByTag(
297 TAG_SMARTDIAL_SEARCH_FRAGMENT);
298 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
299 mPhoneNumberPickerActionListener);
300
301 mShowAllContactsFragment = (ShowAllContactsFragment) fm.findFragmentByTag(
302 TAG_SHOW_ALL_CONTACTS_FRAGMENT);
303 mShowAllContactsFragment.setOnPhoneNumberPickerActionListener(
304 mPhoneNumberPickerActionListener);
Yorke Lee98702de2013-08-06 12:03:32 -0700305
306 if (mFirstLaunch) {
307 displayFragment(getIntent());
308 }
309 mFirstLaunch = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700310 }
311
312 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700313 public void onAttachFragment(Fragment fragment) {
Yorke Leef74011e2013-08-02 11:30:30 -0700314 if (fragment instanceof DialpadFragment || fragment instanceof NewSearchFragment
Yorke Leec3766332013-07-31 11:13:16 -0700315 || fragment instanceof SmartDialSearchFragment
316 || fragment instanceof ShowAllContactsFragment) {
317 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
318 transaction.hide(fragment);
319 transaction.commit();
320 }
321 // TODO krelease: Save some kind of state here to show the appropriate fragment
322 // based on the state of the dialer when it was last paused
323 }
324
325 @Override
326 public boolean onOptionsItemSelected(MenuItem item) {
327 switch (item.getItemId()) {
328 // Respond to the action bar's Up/Home button
329 case android.R.id.home:
330 hideAllContactsFragment();
331 }
332 return super.onOptionsItemSelected(item);
333 }
334
335 @Override
336 public boolean onMenuItemClick(MenuItem item) {
337 switch (item.getItemId()) {
338 case R.id.menu_import_export:
339 // We hard-code the "contactsAreAvailable" argument because doing it properly would
340 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
341 // now in Dialtacts for (potential) performance reasons. Compare with how it is
342 // done in {@link PeopleActivity}.
343 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700344 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700345 return true;
346 case R.id.menu_clear_frequents:
347 ClearFrequentsDialog.show(getFragmentManager());
348 return true;
349 case R.id.add_contact:
350 try {
351 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
352 } catch (ActivityNotFoundException e) {
353 Toast toast = Toast.makeText(this,
354 R.string.add_contact_not_available,
355 Toast.LENGTH_SHORT);
356 toast.show();
357 }
358 return true;
359 case R.id.menu_call_settings:
Yorke Leef74011e2013-08-02 11:30:30 -0700360 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
Yorke Leec3766332013-07-31 11:13:16 -0700361 startActivity(settingsIntent);
362 }
363 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700364 }
365
366 @Override
367 public void onClick(View view) {
368 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700369 case R.id.overflow_menu: {
370 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
371 final Menu menu = popupMenu.getMenu();
372 popupMenu.inflate(R.menu.dialtacts_options_new);
373 popupMenu.setOnMenuItemClickListener(this);
374 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700375 break;
376 }
Yorke Leec3766332013-07-31 11:13:16 -0700377 case R.id.dialpad_button:
378 showDialpadFragment(true);
379 break;
380 case R.id.call_history_on_dialpad_button:
381 case R.id.call_history_button:
382 // TODO krelease: This should start an intent with content type
383 // CallLog.Calls.CONTENT_TYPE, once the intent filters for the call log activity
384 // is enabled
385 final Intent intent = new Intent(this, CallLogActivity.class);
386 startActivity(intent);
387 break;
388 case R.id.search_close_button:
389 // Clear the search field
390 if (!TextUtils.isEmpty(mSearchView.getText())) {
391 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700392 }
393 break;
Yorke Leec3766332013-07-31 11:13:16 -0700394 case R.id.voice_search_button:
395 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
396 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
397 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700398 default: {
399 Log.wtf(TAG, "Unexpected onClick event from " + view);
400 break;
401 }
402 }
403 }
404
Yorke Leec3766332013-07-31 11:13:16 -0700405 @Override
406 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
407 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
408 if (resultCode == RESULT_OK) {
409 final ArrayList<String> matches = data.getStringArrayListExtra(
410 RecognizerIntent.EXTRA_RESULTS);
411 if (matches.size() > 0) {
412 final String match = matches.get(0);
413 mSearchView.setText(match);
414 } else {
415 Log.e(TAG, "Voice search - nothing heard");
416 }
417 } else {
418 Log.e(TAG, "Voice search failed");
419 }
420 }
421 super.onActivityResult(requestCode, resultCode, data);
422 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700423
Yorke Leec3766332013-07-31 11:13:16 -0700424 private void showDialpadFragment(boolean animate) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700425 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700426 if (animate) {
427 ft.setCustomAnimations(R.anim.slide_in, 0);
428 }
429 ft.show(mDialpadFragment);
430 ft.commit();
431 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700432
Yorke Leec3766332013-07-31 11:13:16 -0700433 private void hideDialpadFragment(boolean animate) {
434 final FragmentTransaction ft = getFragmentManager().beginTransaction();
435 if (animate) {
436 ft.setCustomAnimations(0, R.anim.slide_out);
437 }
438 ft.hide(mDialpadFragment);
439 ft.commit();
440 }
441
442 public void showAllContactsFragment() {
443 final FragmentTransaction ft = getFragmentManager().beginTransaction();
444 ft.hide(mPhoneFavoriteFragment);
445 ft.show(mShowAllContactsFragment);
446 // TODO{klp} Add animation
447 ft.commit();
448 hideSearchBar(false);
449 }
450
451 private void hideAllContactsFragment() {
452 final FragmentTransaction ft = getFragmentManager().beginTransaction();
453 ft.hide(mShowAllContactsFragment);
454 ft.show(mPhoneFavoriteFragment);
455 ft.commit();
456 showSearchBar();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700457 }
458
459 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700460 mSearchViewContainer = findViewById(R.id.search_view_container);
461 mSearchViewCloseButton = findViewById(R.id.search_close_button);
462 mSearchViewCloseButton.setOnClickListener(this);
463 mVoiceSearchButton = findViewById(R.id.voice_search_button);
464 mVoiceSearchButton.setOnClickListener(this);
465 mSearchView = (EditText) findViewById(R.id.search_view);
466 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
467 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
468 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700469 @Override
470 public void onFocusChange(View view, boolean hasFocus) {
471 if (hasFocus) {
472 showInputMethod(view.findFocus());
473 }
474 }
475 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700476 }
477
Yorke Leec3766332013-07-31 11:13:16 -0700478 private void hideDialpadFragmentIfNecessary() {
479 if (mDialpadFragment.isVisible()) {
480 hideDialpadFragment(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700481 }
482 }
483
Yorke Leec3766332013-07-31 11:13:16 -0700484 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
485 @Override
486 public void onAnimationEnd(Animator animation) {
487 mSearchViewContainer.setVisibility(View.GONE);
488 }
489 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700490
Yorke Leec3766332013-07-31 11:13:16 -0700491 public void hideSearchBar() {
492 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700493 }
494
Yorke Leec3766332013-07-31 11:13:16 -0700495 public void hideSearchBar(boolean shiftView) {
496 if (shiftView) {
497 mSearchViewContainer.animate().cancel();
498 mSearchViewContainer.setAlpha(1);
499 mSearchViewContainer.setTranslationY(0);
500 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
501 .setDuration(200).setListener(mHideListener);
502
503 mPhoneFavoriteFragment.getView().animate().withLayer()
504 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
505 new AnimatorListenerAdapter() {
506 @Override
507 public void onAnimationEnd(Animator animation) {
508 mBottomPaddingView.setVisibility(View.VISIBLE);
509 mPhoneFavoriteFragment.getView().setTranslationY(0);
510 }
511 });
512 } else {
513 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
514 }
515 }
516
517 public void showSearchBar() {
518 // If the favorites fragment hasn't been fully created before the dialpad fragment
519 // is hidden (i.e. onResume), don't bother animating
520 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
521 return;
522 }
523 mSearchViewContainer.animate().cancel();
524 mSearchViewContainer.setAlpha(0);
525 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
526 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
527 .setListener(new AnimatorListenerAdapter() {
528 @Override
529 public void onAnimationStart(Animator animation) {
530 mSearchViewContainer.setVisibility(View.VISIBLE);
531 }
532 });
533
534 mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
535 mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
536 .setListener(
537 new AnimatorListenerAdapter() {
538 @Override
539 public void onAnimationStart(Animator animation) {
540 mBottomPaddingView.setVisibility(View.GONE);
541 }
542 });
543 }
544
545
546 public void setupFakeActionBarItemsForFavoritesFragment() {
547 mMenuButton = findViewById(R.id.overflow_menu);
548 if (mMenuButton != null) {
549 mMenuButton.setOnClickListener(this);
550 }
551
552 mCallHistoryButton = findViewById(R.id.call_history_button);
553 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
554 mCallHistoryButton.setOnClickListener(this);
555
556 mDialpadButton = findViewById(R.id.dialpad_button);
557 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
558 mDialpadButton.setOnClickListener(this);
559 }
560
561 public void setupFakeActionBarItemsForDialpadFragment() {
562 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
563 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700564 }
565
566 private void fixIntent(Intent intent) {
567 // This should be cleaned up: the call key used to send an Intent
568 // that just said to go to the recent calls list. It now sends this
569 // abstract action, but this class hasn't been rewritten to deal with it.
570 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
571 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
572 intent.putExtra("call_key", true);
573 setIntent(intent);
574 }
575 }
576
Chiao Cheng94b10b52012-08-17 16:59:12 -0700577 /**
578 * Returns true if the intent is due to hitting the green send key (hardware call button:
579 * KEYCODE_CALL) while in a call.
580 *
581 * @param intent the intent that launched this activity
582 * @param recentCallsRequest true if the intent is requesting to view recent calls
583 * @return true if the intent is due to hitting the green send key while in a call
584 */
Yorke Leec3766332013-07-31 11:13:16 -0700585 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700586 // If there is a call in progress go to the call screen
587 if (recentCallsRequest) {
588 final boolean callKey = intent.getBooleanExtra("call_key", false);
589
590 try {
591 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
592 if (callKey && phone != null && phone.showCallScreen()) {
593 return true;
594 }
595 } catch (RemoteException e) {
596 Log.e(TAG, "Failed to handle send while in call", e);
597 }
598 }
599
600 return false;
601 }
602
603 /**
604 * Sets the current tab based on the intent's request type
605 *
606 * @param intent Intent that contains information about which tab should be selected
607 */
Yorke Leec3766332013-07-31 11:13:16 -0700608 private void displayFragment(Intent intent) {
609 // TODO krelease: Make navigation via intent work by displaying the correct fragment
610 // as appropriate.
611
Chiao Cheng94b10b52012-08-17 16:59:12 -0700612 // If we got here by hitting send and we're in call forward along to the in-call activity
613 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
614 getContentResolver()));
615 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
616 finish();
617 return;
618 }
619
Yorke Lee98702de2013-08-06 12:03:32 -0700620 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700621 mDialpadFragment.setStartedFromNewIntent(true);
622 // TODO krelease: This should use showDialpadFragment(false) to avoid animating
623 // the dialpad in. Need to fix the onPreDrawListener in NewDialpadFragment first.
624 showDialpadFragment(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700625 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700626 }
627
628 @Override
629 public void onNewIntent(Intent newIntent) {
630 setIntent(newIntent);
631 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700632 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700633 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700634
635 if (mInSearchUi || (mRegularSearchFragment != null && mRegularSearchFragment.isVisible())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700636 exitSearchUi();
637 }
638
Yorke Leec3766332013-07-31 11:13:16 -0700639 // TODO krelease: Handle onNewIntent for all other fragments
640 /*
641 *if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { if (mDialpadFragment != null) {
642 * mDialpadFragment.setStartedFromNewIntent(true); } else { Log.e(TAG,
643 * "DialpadFragment isn't ready yet when the tab is already selected."); } } else if
644 * (mViewPager.getCurrentItem() == TAB_INDEX_CALL_LOG) { if (mCallLogFragment != null) {
645 * mCallLogFragment.configureScreenFromIntent(newIntent); } else { Log.e(TAG,
646 * "CallLogFragment isn't ready yet when the tab is already selected."); } }
647 */
Chiao Cheng94b10b52012-08-17 16:59:12 -0700648 invalidateOptionsMenu();
649 }
650
651 /** Returns true if the given intent contains a phone number to populate the dialer with */
652 private boolean isDialIntent(Intent intent) {
653 final String action = intent.getAction();
654 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
655 return true;
656 }
657 if (Intent.ACTION_VIEW.equals(action)) {
658 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700659 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700660 return true;
661 }
662 }
663 return false;
664 }
665
666 /**
667 * Returns an appropriate call origin for this Activity. May return null when no call origin
668 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
669 * for remembering the tab in which the user made a phone call, so the external app's DIAL
670 * request should not be counted.)
671 */
672 public String getCallOrigin() {
673 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
674 }
675
676 /**
677 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
678 * This text originally came from a FILTER_CONTACTS_ACTION intent received
679 * by this activity. The stored text will then be cleared after after this
680 * method returns.
681 *
682 * @return The stored filter text
683 */
684 public String getAndClearFilterText() {
685 String filterText = mFilterText;
686 mFilterText = null;
687 return filterText;
688 }
689
690 /**
691 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
692 * This is so child activities can check if they are supposed to display a filter.
693 *
694 * @param intent The intent received in {@link #onNewIntent(Intent)}
695 */
696 private void setupFilterText(Intent intent) {
697 // If the intent was relaunched from history, don't apply the filter text.
698 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
699 return;
700 }
701 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
702 if (filter != null && filter.length() > 0) {
703 mFilterText = filter;
704 }
705 }
706
Yorke Leec3766332013-07-31 11:13:16 -0700707 private final NewPhoneFavoriteFragment.Listener mPhoneFavoriteListener =
708 new NewPhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700709 @Override
710 public void onContactSelected(Uri contactUri) {
711 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700712 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700713 }
714
715 @Override
716 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700717 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700718 startActivity(intent);
719 }
720 };
721
Yorke Leec3766332013-07-31 11:13:16 -0700722 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
723 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
724 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700725
726 @Override
727 public void startSearch(String initialQuery, boolean selectInitialQuery,
728 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700729 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730 if (mInSearchUi) {
731 if (mSearchView.hasFocus()) {
732 showInputMethod(mSearchView.findFocus());
733 } else {
734 mSearchView.requestFocus();
735 }
736 } else {
737 enterSearchUi();
738 }
739 } else {
740 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
741 }
Yorke Leec3766332013-07-31 11:13:16 -0700742 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700743
744 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700745 final InputMethodManager imm = (InputMethodManager) getSystemService(
746 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700747 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700748 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700749 }
750 }
751
752 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700753 final InputMethodManager imm = (InputMethodManager) getSystemService(
754 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700755 if (imm != null && view != null) {
756 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
757 }
758 }
759
760 /**
Yorke Leec3766332013-07-31 11:13:16 -0700761 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700762 */
Yorke Leec3766332013-07-31 11:13:16 -0700763 private void enterSearchUi(boolean smartDialSearch) {
764 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
765 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
766 transaction.hide(mPhoneFavoriteFragment);
767 if (smartDialSearch) {
768 transaction.show(mSmartDialSearchFragment);
769 } else {
770 transaction.show(mRegularSearchFragment);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700771 }
Yorke Leec3766332013-07-31 11:13:16 -0700772 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700773
Yorke Leec3766332013-07-31 11:13:16 -0700774 mInSearchUi = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700775 }
776
777 /**
Yorke Leec3766332013-07-31 11:13:16 -0700778 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700779 */
Yorke Leec3766332013-07-31 11:13:16 -0700780 private void exitSearchUi() {
781 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
782 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
783 transaction.hide(mRegularSearchFragment);
784 transaction.hide(mSmartDialSearchFragment);
785 transaction.show(mPhoneFavoriteFragment);
786 transaction.commit();
787 mInSearchUi = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700788 }
789
790 /** Returns an Intent to launch Call Settings screen */
791 public static Intent getCallSettingsIntent() {
792 final Intent intent = new Intent(Intent.ACTION_MAIN);
793 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
794 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
795 return intent;
796 }
797
798 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700799 public void onBackPressed() {
800 if (mDialpadFragment.isVisible()) {
801 hideDialpadFragment(true);
802 } else if (mInSearchUi) {
803 mSearchView.setText(null);
804 } else if (mShowAllContactsFragment.isVisible()) {
805 hideAllContactsFragment();
806 } else if (isTaskRoot()) {
807 // Instead of stopping, simply push this to the back of the stack.
808 // This is only done when running at the top of the stack;
809 // otherwise, we have been launched by someone else so need to
810 // allow the user to go back to the caller.
811 moveTaskToBack(false);
812 } else {
813 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700814 }
Yorke Leec3766332013-07-31 11:13:16 -0700815 }
816
817 @Override
818 public void onDialpadQueryChanged(String query) {
819 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
820 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
821 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
822 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700823 }
824 }
Yorke Leec3766332013-07-31 11:13:16 -0700825
826 @Override
827 public void onListFragmentScrollStateChange(int scrollState) {
828 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
829 hideDialpadFragmentIfNecessary();
830 hideInputMethod(getCurrentFocus());
831 }
832 }
833
834 @Override
835 public void onPhoneFavoriteFragmentStarted() {
836 setupFakeActionBarItemsForFavoritesFragment();
837 }
838
839 @Override
840 public void onDialpadFragmentStarted() {
841 setupFakeActionBarItemsForDialpadFragment();
842 }
843
844 private boolean phoneIsInUse() {
845 final TelephonyManager tm = (TelephonyManager) getSystemService(
846 Context.TELEPHONY_SERVICE);
847 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
848 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700849}