blob: 4b9a82d4496d60e0279a8fc63f621f55e9415b0d [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 Leed9999322013-07-19 09:22:03 -070041import android.text.Editable;
Yorke Lee6b049122013-07-16 10:38:02 -070042import android.text.TextUtils;
Yorke Leed9999322013-07-19 09:22:03 -070043import android.text.TextWatcher;
Yorke Lee6b049122013-07-16 10:38:02 -070044import android.util.Log;
45import android.view.Menu;
Yorke Lee6b049122013-07-16 10:38:02 -070046import android.view.MenuItem;
Yorke Lee6b049122013-07-16 10:38:02 -070047import android.view.View;
Yorke Lee6b049122013-07-16 10:38:02 -070048import android.view.View.OnFocusChangeListener;
49import android.view.ViewConfiguration;
Yorke Lee6b049122013-07-16 10:38:02 -070050import android.view.inputmethod.InputMethodManager;
Yorke Leedfb2eee2013-06-26 18:24:32 -070051import android.widget.AbsListView.OnScrollListener;
Yorke Leed9999322013-07-19 09:22:03 -070052import android.widget.EditText;
53import android.widget.ImageView;
Yorke Lee6b049122013-07-16 10:38:02 -070054import android.widget.PopupMenu;
55import android.widget.SearchView;
56import android.widget.SearchView.OnCloseListener;
57import android.widget.SearchView.OnQueryTextListener;
58import android.widget.Toast;
59
60import com.android.contacts.common.CallUtil;
61import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leedfb2eee2013-06-26 18:24:32 -070062import com.android.contacts.common.dialog.ClearFrequentsDialog;
63import com.android.contacts.common.interactions.ImportExportDialogFragment;
Yorke Lee6b049122013-07-16 10:38:02 -070064import com.android.contacts.common.list.ContactListItemView;
65import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
66import com.android.contacts.common.list.PhoneNumberPickerFragment;
Yorke Leedfb2eee2013-06-26 18:24:32 -070067import com.android.dialer.calllog.NewCallLogActivity;
68import com.android.dialer.dialpad.NewDialpadFragment;
69import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Lee6b049122013-07-16 10:38:02 -070070import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Leedfb2eee2013-06-26 18:24:32 -070071import com.android.dialer.list.NewPhoneFavoriteFragment;
72import com.android.dialer.list.OnListFragmentScrolledListener;
73import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee6b049122013-07-16 10:38:02 -070074import com.android.internal.telephony.ITelephony;
75
76/**
Yorke Lee6b049122013-07-16 10:38:02 -070077 * The dialer tab's title is 'phone', a more common name (see strings.xml).
Yorke Leedfb2eee2013-06-26 18:24:32 -070078 *
79 * TODO krelease: All classes currently prefixed with New will replace the original classes or
80 * be renamed more appropriately before shipping.
Yorke Lee6b049122013-07-16 10:38:02 -070081 */
Yorke Leedfb2eee2013-06-26 18:24:32 -070082public class NewDialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
83 NewDialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
84 OnListFragmentScrolledListener,
Yorke Lee714d6c72013-07-26 16:11:39 -070085 NewPhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener,
86 NewDialpadFragment.OnDialpadFragmentStartedListener {
Yorke Lee6b049122013-07-16 10:38:02 -070087 private static final String TAG = "DialtactsActivity";
88
89 public static final boolean DEBUG = false;
90
91 /** Used to open Call Setting */
92 private static final String PHONE_PACKAGE = "com.android.phone";
93 private static final String CALL_SETTINGS_CLASS_NAME =
94 "com.android.phone.CallFeaturesSetting";
95
96 /** @see #getCallOrigin() */
97 private static final String CALL_ORIGIN_DIALTACTS =
98 "com.android.dialer.DialtactsActivity";
99
Yorke Leedfb2eee2013-06-26 18:24:32 -0700100 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
101 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
102 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
103 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
104
Yorke Lee6b049122013-07-16 10:38:02 -0700105 /**
106 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
107 */
108 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
109
Yorke Lee6b049122013-07-16 10:38:02 -0700110 private SharedPreferences mPrefs;
111
112 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
113
114 /** Last manually selected tab index */
115 private static final String PREF_LAST_MANUALLY_SELECTED_TAB =
116 "DialtactsActivity_last_manually_selected_tab";
Yorke Lee6b049122013-07-16 10:38:02 -0700117
118 private static final int SUBACTIVITY_ACCOUNT_FILTER = 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
142 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
Yorke Lee6b049122013-07-16 10:38:02 -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 Leed9999322013-07-19 09:22:03 -0700154 private View mSearchViewContainer;
155 private View mSearchViewCloseButton;
156 private EditText mSearchView;
Yorke Lee6b049122013-07-16 10:38:02 -0700157
Yorke Lee6b049122013-07-16 10:38:02 -0700158 /**
159 * The index of the Fragment (or, the tab) that has last been manually selected.
160 * This value does not keep track of programmatically set Tabs (e.g. Call Log after a Call)
161 */
162 private int mLastManuallySelectedFragment;
163
Yorke Lee6b049122013-07-16 10:38:02 -0700164 /**
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 Leedfb2eee2013-06-26 18:24:32 -0700175 NewDialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee6b049122013-07-16 10:38:02 -0700176 }
177
178 @Override
179 public void onShortcutIntentCreated(Intent intent) {
180 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
181 }
182
183 @Override
184 public void onHomeInActionBarSelected() {
185 exitSearchUi();
186 }
187 };
188
189 /**
190 * Listener used to send search queries to the phone search fragment.
191 */
Yorke Leed9999322013-07-19 09:22:03 -0700192 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
193 @Override
194 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
195 }
196
197 @Override
198 public void onTextChanged(CharSequence s, int start, int before, int count) {
199 final boolean smartDialSearch = isDialpadShowing();
200 final String newText = s.toString();
201 // Show search result with non-empty text. Show a bare list otherwise.
202 if (TextUtils.isEmpty(newText) && mInSearchUi) {
203 exitSearchUi();
204 mSearchViewCloseButton.setVisibility(View.GONE);
205 return;
206 } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
207 enterSearchUi(smartDialSearch);
Yorke Lee6b049122013-07-16 10:38:02 -0700208 }
209
Yorke Leed9999322013-07-19 09:22:03 -0700210 if (isDialpadShowing()) {
211 mSmartDialSearchFragment.setQueryString(newText, false);
212 } else {
213 mRegularSearchFragment.setQueryString(newText, false);
Yorke Lee6b049122013-07-16 10:38:02 -0700214 }
Yorke Leed9999322013-07-19 09:22:03 -0700215 mSearchViewCloseButton.setVisibility(View.VISIBLE);
216 return;
217 }
218
219 @Override
220 public void afterTextChanged(Editable s) {
221 }
Yorke Lee6b049122013-07-16 10:38:02 -0700222 };
223
Yorke Leedfb2eee2013-06-26 18:24:32 -0700224 private boolean isDialpadShowing() {
225 return mDialpadFragment.isVisible();
226 }
227
Yorke Lee6b049122013-07-16 10:38:02 -0700228 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700229 protected void onCreate(Bundle savedInstanceState) {
230 super.onCreate(savedInstanceState);
Yorke Lee6b049122013-07-16 10:38:02 -0700231
232 final Intent intent = getIntent();
233 fixIntent(intent);
234
Yorke Leedfb2eee2013-06-26 18:24:32 -0700235 setContentView(R.layout.new_dialtacts_activity);
Yorke Lee6b049122013-07-16 10:38:02 -0700236
Yorke Leedfb2eee2013-06-26 18:24:32 -0700237 getActionBar().hide();
Yorke Lee6b049122013-07-16 10:38:02 -0700238
Yorke Leedfb2eee2013-06-26 18:24:32 -0700239 mPhoneFavoriteFragment = new NewPhoneFavoriteFragment();
240 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Lee6b049122013-07-16 10:38:02 -0700241
Yorke Leedfb2eee2013-06-26 18:24:32 -0700242 mRegularSearchFragment = new NewSearchFragment();
243 mSmartDialSearchFragment = new SmartDialSearchFragment();
244 mDialpadFragment = new NewDialpadFragment();
Yorke Lee6b049122013-07-16 10:38:02 -0700245
Yorke Leedfb2eee2013-06-26 18:24:32 -0700246 // TODO krelease: load fragments on demand instead of creating all of them at run time
247 final FragmentTransaction ft = getFragmentManager().beginTransaction();
248 ft.add(R.id.dialtacts_frame, mPhoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
249 ft.add(R.id.dialtacts_frame, mRegularSearchFragment, TAG_REGULAR_SEARCH_FRAGMENT);
250 ft.add(R.id.dialtacts_frame, mSmartDialSearchFragment, TAG_SMARTDIAL_SEARCH_FRAGMENT);
251 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
252 ft.hide(mRegularSearchFragment);
253 ft.hide(mDialpadFragment);
254 ft.hide(mSmartDialSearchFragment);
255 ft.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700256
Yorke Leedfb2eee2013-06-26 18:24:32 -0700257 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
258 prepareSearchView();
Yorke Lee6b049122013-07-16 10:38:02 -0700259
260 // Load the last manually loaded tab
261 mPrefs = this.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
Yorke Lee6b049122013-07-16 10:38:02 -0700262
Yorke Leedfb2eee2013-06-26 18:24:32 -0700263 /*
264 * TODO krelease : Remember which fragment was last displayed, and then redisplay it as
265 * necessary. mLastManuallySelectedFragment = mPrefs.getInt(PREF_LAST_MANUALLY_SELECTED_TAB,
266 * PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT); if (mLastManuallySelectedFragment >=
267 * TAB_INDEX_COUNT) { // Stored value may have exceeded the number of current tabs. Reset
268 * it. mLastManuallySelectedFragment = PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT; }
269 */
Yorke Lee6b049122013-07-16 10:38:02 -0700270
271 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leedfb2eee2013-06-26 18:24:32 -0700272 && savedInstanceState == null) {
Yorke Lee6b049122013-07-16 10:38:02 -0700273 setupFilterText(intent);
274 }
275 }
276
277 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700278 protected void onResume() {
279 super.onResume();
280 final FragmentManager fm = getFragmentManager();
281 mPhoneFavoriteFragment = (NewPhoneFavoriteFragment) fm.findFragmentByTag(
282 TAG_FAVORITES_FRAGMENT);
283 mDialpadFragment = (NewDialpadFragment) fm.findFragmentByTag(TAG_DIALPAD_FRAGMENT);
284
285 mRegularSearchFragment = (NewSearchFragment) fm.findFragmentByTag(
286 TAG_REGULAR_SEARCH_FRAGMENT);
287 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
288 mPhoneNumberPickerActionListener);
289 if (!mRegularSearchFragment.isHidden()) {
290 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
291 transaction.hide(mRegularSearchFragment);
292 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700293 }
294
Yorke Leedfb2eee2013-06-26 18:24:32 -0700295 mSmartDialSearchFragment = (SmartDialSearchFragment) fm.findFragmentByTag(
296 TAG_SMARTDIAL_SEARCH_FRAGMENT);
297 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
298 mPhoneNumberPickerActionListener);
299 if (!mSmartDialSearchFragment.isHidden()) {
300 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
301 transaction.hide(mSmartDialSearchFragment);
302 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700303 }
304 }
305
306 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700307 public boolean onMenuItemClick(MenuItem item) {
308 switch (item.getItemId()) {
309 case R.id.menu_import_export:
310 // We hard-code the "contactsAreAvailable" argument because doing it properly would
311 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
312 // now in Dialtacts for (potential) performance reasons. Compare with how it is
313 // done in {@link PeopleActivity}.
314 ImportExportDialogFragment.show(getFragmentManager(), true,
315 DialtactsActivity.class);
316 return true;
317 case R.id.menu_clear_frequents:
318 ClearFrequentsDialog.show(getFragmentManager());
319 return true;
320 case R.id.add_contact:
321 try {
322 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
323 } catch (ActivityNotFoundException e) {
324 Toast toast = Toast.makeText(this, R.string.add_contact_not_available,
325 Toast.LENGTH_SHORT);
326 toast.show();
327 }
328 return true;
329 case R.id.menu_call_settings:
330 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
331 startActivity(settingsIntent);
332 }
333 return false;
Yorke Lee6b049122013-07-16 10:38:02 -0700334 }
335
336 @Override
337 public void onClick(View view) {
338 switch (view.getId()) {
Yorke Lee714d6c72013-07-26 16:11:39 -0700339 case R.id.overflow_menu_on_dialpad:
Yorke Lee6b049122013-07-16 10:38:02 -0700340 case R.id.overflow_menu: {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700341 final PopupMenu popupMenu = new PopupMenu(NewDialtactsActivity.this, view);
342 final Menu menu = popupMenu.getMenu();
343 popupMenu.inflate(R.menu.dialtacts_options_new);
344 popupMenu.setOnMenuItemClickListener(this);
345 popupMenu.show();
Yorke Lee6b049122013-07-16 10:38:02 -0700346 break;
347 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700348 case R.id.dialpad_button:
349 showDialpadFragment();
350 break;
Yorke Lee714d6c72013-07-26 16:11:39 -0700351 case R.id.call_history_on_dialpad_button:
Yorke Leedfb2eee2013-06-26 18:24:32 -0700352 case R.id.call_history_button:
353 final Intent intent = new Intent(this, NewCallLogActivity.class);
354 startActivity(intent);
355 break;
Yorke Leed9999322013-07-19 09:22:03 -0700356 case R.id.search_close_button:
357 // Clear the search field
358 if (!TextUtils.isEmpty(mSearchView.getText())) {
359 mSearchView.setText("");
360 }
361 break;
Yorke Lee6b049122013-07-16 10:38:02 -0700362 default: {
363 Log.wtf(TAG, "Unexpected onClick event from " + view);
364 break;
365 }
366 }
367 }
368
Yorke Leedfb2eee2013-06-26 18:24:32 -0700369 private void showDialpadFragment() {
Yorke Lee6b049122013-07-16 10:38:02 -0700370 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leedfb2eee2013-06-26 18:24:32 -0700371 ft.setCustomAnimations(R.anim.slide_in, 0);
372 ft.show(mDialpadFragment);
373 ft.commit();
374 }
Yorke Lee6b049122013-07-16 10:38:02 -0700375
Yorke Leedfb2eee2013-06-26 18:24:32 -0700376 private void hideDialpadFragment() {
377 final FragmentTransaction ft = getFragmentManager().beginTransaction();
378 ft.setCustomAnimations(0, R.anim.slide_out);
379 ft.hide(mDialpadFragment);
380 ft.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700381 }
382
383 private void prepareSearchView() {
Yorke Leed9999322013-07-19 09:22:03 -0700384 mSearchViewContainer = findViewById(R.id.search_view_container);
385 mSearchViewCloseButton = findViewById(R.id.search_close_button);
386 mSearchViewCloseButton.setClickable(true);
387 mSearchViewCloseButton.setOnClickListener(this);
388 mSearchView = (EditText) findViewById(R.id.search_view);
389 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
390 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
391 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Yorke Lee6b049122013-07-16 10:38:02 -0700392 @Override
393 public void onFocusChange(View view, boolean hasFocus) {
394 if (hasFocus) {
395 showInputMethod(view.findFocus());
396 }
397 }
398 });
Yorke Lee6b049122013-07-16 10:38:02 -0700399 }
400
Yorke Leedfb2eee2013-06-26 18:24:32 -0700401 private void hideDialpadFragmentIfNecessary() {
402 if (mDialpadFragment.isVisible()) {
403 hideDialpadFragment();
404 }
405 }
Yorke Lee6b049122013-07-16 10:38:02 -0700406
Yorke Leedfb2eee2013-06-26 18:24:32 -0700407 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
408 @Override
409 public void onAnimationEnd(Animator animation) {
Yorke Leed9999322013-07-19 09:22:03 -0700410 mSearchViewContainer.setVisibility(View.GONE);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700411 }
412 };
413
414 public void hideSearchBar() {
Yorke Leed9999322013-07-19 09:22:03 -0700415 mSearchViewContainer.animate().cancel();
416 mSearchViewContainer.setAlpha(1);
417 mSearchViewContainer.setTranslationY(0);
418 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
419 .setDuration(200).setListener(mHideListener);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700420
421 mPhoneFavoriteFragment.getView().animate().withLayer()
Yorke Leed9999322013-07-19 09:22:03 -0700422 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700423 new AnimatorListenerAdapter() {
424 @Override
425 public void onAnimationEnd(Animator animation) {
426 mBottomPaddingView.setVisibility(View.VISIBLE);
427 mPhoneFavoriteFragment.getView().setTranslationY(0);
428 }
429 });
430 }
431
432 public void showSearchBar() {
Yorke Leed9999322013-07-19 09:22:03 -0700433 mSearchViewContainer.animate().cancel();
434 mSearchViewContainer.setAlpha(0);
435 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
436 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
Yorke Leedfb2eee2013-06-26 18:24:32 -0700437 .setListener(new AnimatorListenerAdapter() {
438 @Override
439 public void onAnimationStart(Animator animation) {
Yorke Leed9999322013-07-19 09:22:03 -0700440 mSearchViewContainer.setVisibility(View.VISIBLE);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700441 }
442 });
443
Yorke Leed9999322013-07-19 09:22:03 -0700444 mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
Yorke Leedfb2eee2013-06-26 18:24:32 -0700445 mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
446 .setListener(
447 new AnimatorListenerAdapter() {
448 @Override
449 public void onAnimationStart(Animator animation) {
450 mBottomPaddingView.setVisibility(View.GONE);
451 }
452 });
453 }
454
455
Yorke Lee714d6c72013-07-26 16:11:39 -0700456 public void setupFakeActionBarItemsForFavoritesFragment() {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700457 mMenuButton = findViewById(R.id.overflow_menu);
458 if (mMenuButton != null) {
459 // mMenuButton.setMinimumWidth(fakeMenuItemWidth);
460 if (ViewConfiguration.get(this).hasPermanentMenuKey()) {
461 // This is required for dialpad button's layout, so must not use GONE here.
462 mMenuButton.setVisibility(View.INVISIBLE);
463 } else {
464 mMenuButton.setOnClickListener(this);
Yorke Lee6b049122013-07-16 10:38:02 -0700465 }
466 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700467
468 mCallHistoryButton = findViewById(R.id.call_history_button);
469 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
470 mCallHistoryButton.setOnClickListener(this);
471
472 mDialpadButton = findViewById(R.id.dialpad_button);
473 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
474 mDialpadButton.setOnClickListener(this);
Yorke Lee6b049122013-07-16 10:38:02 -0700475 }
476
Yorke Lee714d6c72013-07-26 16:11:39 -0700477 public void setupFakeActionBarItemsForDialpadFragment() {
478 final View overflowButton = findViewById(R.id.overflow_menu_on_dialpad);
479 overflowButton.setOnClickListener(this);
480 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
481 callhistoryButton.setOnClickListener(this);
482 }
483
Yorke Lee6b049122013-07-16 10:38:02 -0700484 @Override
485 protected void onPause() {
486 super.onPause();
Yorke Lee6b049122013-07-16 10:38:02 -0700487 mPrefs.edit().putInt(PREF_LAST_MANUALLY_SELECTED_TAB, mLastManuallySelectedFragment)
488 .apply();
489 requestBackup();
490 }
491
492 private void requestBackup() {
493 final BackupManager bm = new BackupManager(this);
494 bm.dataChanged();
495 }
496
497 private void fixIntent(Intent intent) {
498 // This should be cleaned up: the call key used to send an Intent
499 // that just said to go to the recent calls list. It now sends this
500 // abstract action, but this class hasn't been rewritten to deal with it.
501 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
502 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
503 intent.putExtra("call_key", true);
504 setIntent(intent);
505 }
506 }
507
Yorke Lee6b049122013-07-16 10:38:02 -0700508 /**
509 * Returns true if the intent is due to hitting the green send key (hardware call button:
510 * KEYCODE_CALL) while in a call.
511 *
512 * @param intent the intent that launched this activity
513 * @param recentCallsRequest true if the intent is requesting to view recent calls
514 * @return true if the intent is due to hitting the green send key while in a call
515 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700516 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Yorke Lee6b049122013-07-16 10:38:02 -0700517 // If there is a call in progress go to the call screen
518 if (recentCallsRequest) {
519 final boolean callKey = intent.getBooleanExtra("call_key", false);
520
521 try {
522 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
523 if (callKey && phone != null && phone.showCallScreen()) {
524 return true;
525 }
526 } catch (RemoteException e) {
527 Log.e(TAG, "Failed to handle send while in call", e);
528 }
529 }
530
531 return false;
532 }
533
534 /**
535 * Sets the current tab based on the intent's request type
536 *
537 * @param intent Intent that contains information about which tab should be selected
538 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700539 private void displayFragment(Intent intent) {
540 // TODO krelease: Make navigation via intent work by displaying the correct fragment
541 // as appropriate.
542
Yorke Lee6b049122013-07-16 10:38:02 -0700543 // If we got here by hitting send and we're in call forward along to the in-call activity
544 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
545 getContentResolver()));
546 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
547 finish();
548 return;
549 }
Yorke Lee6b049122013-07-16 10:38:02 -0700550 }
551
552 @Override
553 public void onNewIntent(Intent newIntent) {
554 setIntent(newIntent);
555 fixIntent(newIntent);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700556 displayFragment(newIntent);
Yorke Lee6b049122013-07-16 10:38:02 -0700557 final String action = newIntent.getAction();
558 if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
559 setupFilterText(newIntent);
560 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700561 if (mInSearchUi || (mRegularSearchFragment != null && mRegularSearchFragment.isVisible())) {
Yorke Lee6b049122013-07-16 10:38:02 -0700562 exitSearchUi();
563 }
564
Yorke Leedfb2eee2013-06-26 18:24:32 -0700565 // TODO krelease: Handle onNewIntent for all other fragments
566 /*
567 *if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { if (mDialpadFragment != null) {
568 * mDialpadFragment.setStartedFromNewIntent(true); } else { Log.e(TAG,
569 * "DialpadFragment isn't ready yet when the tab is already selected."); } } else if
570 * (mViewPager.getCurrentItem() == TAB_INDEX_CALL_LOG) { if (mCallLogFragment != null) {
571 * mCallLogFragment.configureScreenFromIntent(newIntent); } else { Log.e(TAG,
572 * "CallLogFragment isn't ready yet when the tab is already selected."); } }
573 */
Yorke Lee6b049122013-07-16 10:38:02 -0700574 invalidateOptionsMenu();
575 }
576
577 /** Returns true if the given intent contains a phone number to populate the dialer with */
578 private boolean isDialIntent(Intent intent) {
579 final String action = intent.getAction();
580 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
581 return true;
582 }
583 if (Intent.ACTION_VIEW.equals(action)) {
584 final Uri data = intent.getData();
585 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
586 return true;
587 }
588 }
589 return false;
590 }
591
592 /**
593 * Returns an appropriate call origin for this Activity. May return null when no call origin
594 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
595 * for remembering the tab in which the user made a phone call, so the external app's DIAL
596 * request should not be counted.)
597 */
598 public String getCallOrigin() {
599 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
600 }
601
602 /**
603 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
604 * This text originally came from a FILTER_CONTACTS_ACTION intent received
605 * by this activity. The stored text will then be cleared after after this
606 * method returns.
607 *
608 * @return The stored filter text
609 */
610 public String getAndClearFilterText() {
611 String filterText = mFilterText;
612 mFilterText = null;
613 return filterText;
614 }
615
616 /**
617 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
618 * This is so child activities can check if they are supposed to display a filter.
619 *
620 * @param intent The intent received in {@link #onNewIntent(Intent)}
621 */
622 private void setupFilterText(Intent intent) {
623 // If the intent was relaunched from history, don't apply the filter text.
624 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
625 return;
626 }
627 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
628 if (filter != null && filter.length() > 0) {
629 mFilterText = filter;
630 }
631 }
632
Yorke Leedfb2eee2013-06-26 18:24:32 -0700633 private final NewPhoneFavoriteFragment.Listener mPhoneFavoriteListener =
634 new NewPhoneFavoriteFragment.Listener() {
Yorke Lee6b049122013-07-16 10:38:02 -0700635 @Override
636 public void onContactSelected(Uri contactUri) {
637 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700638 NewDialtactsActivity.this, contactUri, getCallOrigin());
Yorke Lee6b049122013-07-16 10:38:02 -0700639 }
640
641 @Override
642 public void onCallNumberDirectly(String phoneNumber) {
643 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
644 startActivity(intent);
645 }
646 };
647
Yorke Leedfb2eee2013-06-26 18:24:32 -0700648 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
649 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
650 * be showing when the search key is pressed so there is more state management involved.
Yorke Lee6b049122013-07-16 10:38:02 -0700651
652 @Override
653 public void startSearch(String initialQuery, boolean selectInitialQuery,
654 Bundle appSearchData, boolean globalSearch) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700655 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Yorke Lee6b049122013-07-16 10:38:02 -0700656 if (mInSearchUi) {
657 if (mSearchView.hasFocus()) {
658 showInputMethod(mSearchView.findFocus());
659 } else {
660 mSearchView.requestFocus();
661 }
662 } else {
663 enterSearchUi();
664 }
665 } else {
666 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
667 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700668 }*/
Yorke Lee6b049122013-07-16 10:38:02 -0700669
670 private void showInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700671 final InputMethodManager imm = (InputMethodManager) getSystemService(
672 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700673 if (imm != null) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700674 imm.showSoftInput(view, 0);
Yorke Lee6b049122013-07-16 10:38:02 -0700675 }
676 }
677
678 private void hideInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700679 final InputMethodManager imm = (InputMethodManager) getSystemService(
680 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700681 if (imm != null && view != null) {
682 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
683 }
684 }
685
686 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700687 * Shows the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700688 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700689 private void enterSearchUi(boolean smartDialSearch) {
690 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
691 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
692 transaction.hide(mPhoneFavoriteFragment);
693 if (smartDialSearch) {
694 transaction.show(mSmartDialSearchFragment);
695 } else {
696 transaction.show(mRegularSearchFragment);
Yorke Lee6b049122013-07-16 10:38:02 -0700697 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700698 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700699
Yorke Leedfb2eee2013-06-26 18:24:32 -0700700 mInSearchUi = true;
Yorke Lee6b049122013-07-16 10:38:02 -0700701 }
702
703 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700704 * Hides the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700705 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700706 private void exitSearchUi() {
707 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
708 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
709 transaction.hide(mRegularSearchFragment);
710 transaction.hide(mSmartDialSearchFragment);
711 transaction.show(mPhoneFavoriteFragment);
712 transaction.commit();
713 mInSearchUi = false;
Yorke Lee6b049122013-07-16 10:38:02 -0700714 }
715
716 /** Returns an Intent to launch Call Settings screen */
717 public static Intent getCallSettingsIntent() {
718 final Intent intent = new Intent(Intent.ACTION_MAIN);
719 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
720 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
721 return intent;
722 }
723
724 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700725 public void onBackPressed() {
726 if (mDialpadFragment.isVisible()) {
727 hideDialpadFragment();
728 } else if (mInSearchUi) {
Yorke Leed9999322013-07-19 09:22:03 -0700729 mSearchView.setText(null);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700730 } else if (isTaskRoot()) {
731 // Instead of stopping, simply push this to the back of the stack.
732 // This is only done when running at the top of the stack;
733 // otherwise, we have been launched by someone else so need to
734 // allow the user to go back to the caller.
735 moveTaskToBack(false);
736 } else {
737 super.onBackPressed();
Yorke Lee6b049122013-07-16 10:38:02 -0700738 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700739 }
740
741 @Override
742 public void onDialpadQueryChanged(String query) {
743 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
744 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Leed9999322013-07-19 09:22:03 -0700745 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
746 mSearchView.setText(normalizedQuery);
Yorke Lee6b049122013-07-16 10:38:02 -0700747 }
748 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700749
750 @Override
751 public void onListFragmentScrollStateChange(int scrollState) {
752 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
753 hideDialpadFragmentIfNecessary();
754 hideInputMethod(getCurrentFocus());
755 }
756 }
757
758 @Override
759 public void onPhoneFavoriteFragmentStarted() {
Yorke Lee714d6c72013-07-26 16:11:39 -0700760 setupFakeActionBarItemsForFavoritesFragment();
Yorke Leedfb2eee2013-06-26 18:24:32 -0700761 }
Yorke Lee714d6c72013-07-26 16:11:39 -0700762
763 @Override
764 public void onDialpadFragmentStarted() {
765 setupFakeActionBarItemsForDialpadFragment();
766 }
767
768
Yorke Lee6b049122013-07-16 10:38:02 -0700769}