blob: 6d2bb3fdbcb115a0fe303056a8b7a94264383fea [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 Lee80a05ed2013-07-26 17:05:04 -070041import android.telephony.TelephonyManager;
Yorke Leed9999322013-07-19 09:22:03 -070042import android.text.Editable;
Yorke Lee6b049122013-07-16 10:38:02 -070043import android.text.TextUtils;
Yorke Leed9999322013-07-19 09:22:03 -070044import android.text.TextWatcher;
Yorke Lee6b049122013-07-16 10:38:02 -070045import android.util.Log;
46import android.view.Menu;
Yorke Lee6b049122013-07-16 10:38:02 -070047import android.view.MenuItem;
Yorke Lee6b049122013-07-16 10:38:02 -070048import android.view.View;
Yorke Lee6b049122013-07-16 10:38:02 -070049import android.view.View.OnFocusChangeListener;
50import android.view.ViewConfiguration;
Yorke Lee6b049122013-07-16 10:38:02 -070051import android.view.inputmethod.InputMethodManager;
Yorke Leedfb2eee2013-06-26 18:24:32 -070052import android.widget.AbsListView.OnScrollListener;
Yorke Leed9999322013-07-19 09:22:03 -070053import android.widget.EditText;
54import android.widget.ImageView;
Yorke Lee6b049122013-07-16 10:38:02 -070055import android.widget.PopupMenu;
56import android.widget.SearchView;
57import android.widget.SearchView.OnCloseListener;
58import android.widget.SearchView.OnQueryTextListener;
59import android.widget.Toast;
60
61import com.android.contacts.common.CallUtil;
62import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leedfb2eee2013-06-26 18:24:32 -070063import com.android.contacts.common.dialog.ClearFrequentsDialog;
64import com.android.contacts.common.interactions.ImportExportDialogFragment;
Yorke Lee6b049122013-07-16 10:38:02 -070065import com.android.contacts.common.list.ContactListItemView;
66import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
67import com.android.contacts.common.list.PhoneNumberPickerFragment;
Yorke Leedfb2eee2013-06-26 18:24:32 -070068import com.android.dialer.calllog.NewCallLogActivity;
69import com.android.dialer.dialpad.NewDialpadFragment;
70import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Lee6b049122013-07-16 10:38:02 -070071import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Leedfb2eee2013-06-26 18:24:32 -070072import com.android.dialer.list.NewPhoneFavoriteFragment;
73import com.android.dialer.list.OnListFragmentScrolledListener;
74import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee6b049122013-07-16 10:38:02 -070075import com.android.internal.telephony.ITelephony;
76
77/**
Yorke Lee6b049122013-07-16 10:38:02 -070078 * The dialer tab's title is 'phone', a more common name (see strings.xml).
Yorke Leedfb2eee2013-06-26 18:24:32 -070079 *
80 * TODO krelease: All classes currently prefixed with New will replace the original classes or
81 * be renamed more appropriately before shipping.
Yorke Lee6b049122013-07-16 10:38:02 -070082 */
Yorke Leedfb2eee2013-06-26 18:24:32 -070083public class NewDialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
84 NewDialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
85 OnListFragmentScrolledListener,
Yorke Lee714d6c72013-07-26 16:11:39 -070086 NewPhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener,
87 NewDialpadFragment.OnDialpadFragmentStartedListener {
Yorke Lee6b049122013-07-16 10:38:02 -070088 private static final String TAG = "DialtactsActivity";
89
90 public static final boolean DEBUG = false;
91
92 /** Used to open Call Setting */
93 private static final String PHONE_PACKAGE = "com.android.phone";
94 private static final String CALL_SETTINGS_CLASS_NAME =
95 "com.android.phone.CallFeaturesSetting";
96
97 /** @see #getCallOrigin() */
98 private static final String CALL_ORIGIN_DIALTACTS =
99 "com.android.dialer.DialtactsActivity";
100
Yorke Leedfb2eee2013-06-26 18:24:32 -0700101 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
102 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
103 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
104 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
105
Yorke Lee6b049122013-07-16 10:38:02 -0700106 /**
107 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
108 */
109 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
110
Yorke Lee6b049122013-07-16 10:38:02 -0700111 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
112
Yorke Lee6b049122013-07-16 10:38:02 -0700113 private String mFilterText;
114
Yorke Leedfb2eee2013-06-26 18:24:32 -0700115 /**
116 * The main fragment displaying the user's favorites and frequent contacts
117 */
118 private NewPhoneFavoriteFragment mPhoneFavoriteFragment;
Yorke Lee6b049122013-07-16 10:38:02 -0700119
120 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700121 * Fragment containing the dialpad that slides into view
Yorke Lee6b049122013-07-16 10:38:02 -0700122 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700123 private NewDialpadFragment mDialpadFragment;
124
125 /**
126 * Fragment for searching phone numbers using the alphanumeric keyboard.
127 */
128 private NewSearchFragment mRegularSearchFragment;
129
130 /**
131 * Fragment for searching phone numbers using the dialpad.
132 */
133 private SmartDialSearchFragment mSmartDialSearchFragment;
134
135 private View mMenuButton;
136 private View mCallHistoryButton;
137 private View mDialpadButton;
138
139 // Padding view used to shift the fragments up when the dialpad is shown.
140 private View mBottomPaddingView;
141
Yorke Lee6b049122013-07-16 10:38:02 -0700142 /**
143 * True when this Activity is in its search UI (with a {@link SearchView} and
144 * {@link PhoneNumberPickerFragment}).
145 */
146 private boolean mInSearchUi;
Yorke Leed9999322013-07-19 09:22:03 -0700147 private View mSearchViewContainer;
148 private View mSearchViewCloseButton;
149 private EditText mSearchView;
Yorke Lee6b049122013-07-16 10:38:02 -0700150
Yorke Lee6b049122013-07-16 10:38:02 -0700151 /**
Yorke Lee6b049122013-07-16 10:38:02 -0700152 * Listener used when one of phone numbers in search UI is selected. This will initiate a
153 * phone call using the phone number.
154 */
155 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
156 new OnPhoneNumberPickerActionListener() {
157 @Override
158 public void onPickPhoneNumberAction(Uri dataUri) {
159 // Specify call-origin so that users will see the previous tab instead of
160 // CallLog screen (search UI will be automatically exited).
161 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700162 NewDialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee6b049122013-07-16 10:38:02 -0700163 }
164
165 @Override
166 public void onShortcutIntentCreated(Intent intent) {
167 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
168 }
169
170 @Override
171 public void onHomeInActionBarSelected() {
172 exitSearchUi();
173 }
174 };
175
176 /**
177 * Listener used to send search queries to the phone search fragment.
178 */
Yorke Leed9999322013-07-19 09:22:03 -0700179 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
180 @Override
181 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
182 }
183
184 @Override
185 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Lee936e4432013-07-29 09:54:42 -0700186 // TODO krelease: populate the search fragments with the correct
187 // search query at the correct point in time of the fragment lifecycle.
188 // The current behavior is to simply return to the favorites screen
189 // (when docked), or returning to the Dialer after it has been
190 // swapped out of memory.
191 if (mDialpadFragment == null) return;
192 final boolean smartDialSearch = isDialpadShowing();
Yorke Leed9999322013-07-19 09:22:03 -0700193 final String newText = s.toString();
194 // Show search result with non-empty text. Show a bare list otherwise.
195 if (TextUtils.isEmpty(newText) && mInSearchUi) {
196 exitSearchUi();
197 mSearchViewCloseButton.setVisibility(View.GONE);
198 return;
199 } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
200 enterSearchUi(smartDialSearch);
Yorke Lee6b049122013-07-16 10:38:02 -0700201 }
202
Yorke Lee936e4432013-07-29 09:54:42 -0700203 if (smartDialSearch) {
Yorke Leed9999322013-07-19 09:22:03 -0700204 mSmartDialSearchFragment.setQueryString(newText, false);
205 } else {
206 mRegularSearchFragment.setQueryString(newText, false);
Yorke Lee6b049122013-07-16 10:38:02 -0700207 }
Yorke Leed9999322013-07-19 09:22:03 -0700208 mSearchViewCloseButton.setVisibility(View.VISIBLE);
209 return;
210 }
211
212 @Override
213 public void afterTextChanged(Editable s) {
214 }
Yorke Lee6b049122013-07-16 10:38:02 -0700215 };
216
Yorke Leedfb2eee2013-06-26 18:24:32 -0700217 private boolean isDialpadShowing() {
Yorke Lee936e4432013-07-29 09:54:42 -0700218 return mDialpadFragment != null && mDialpadFragment.isVisible();
Yorke Leedfb2eee2013-06-26 18:24:32 -0700219 }
220
Yorke Lee6b049122013-07-16 10:38:02 -0700221 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700222 protected void onCreate(Bundle savedInstanceState) {
223 super.onCreate(savedInstanceState);
Yorke Lee6b049122013-07-16 10:38:02 -0700224
225 final Intent intent = getIntent();
226 fixIntent(intent);
227
Yorke Leedfb2eee2013-06-26 18:24:32 -0700228 setContentView(R.layout.new_dialtacts_activity);
Yorke Lee6b049122013-07-16 10:38:02 -0700229
Yorke Leedfb2eee2013-06-26 18:24:32 -0700230 getActionBar().hide();
Yorke Lee6b049122013-07-16 10:38:02 -0700231
Yorke Lee936e4432013-07-29 09:54:42 -0700232 if (savedInstanceState == null) {
233 mPhoneFavoriteFragment = new NewPhoneFavoriteFragment();
234 mPhoneFavoriteFragment.setRetainInstance(true);
235 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Lee6b049122013-07-16 10:38:02 -0700236
Yorke Lee936e4432013-07-29 09:54:42 -0700237 mRegularSearchFragment = new NewSearchFragment();
238 mSmartDialSearchFragment = new SmartDialSearchFragment();
239 mDialpadFragment = new NewDialpadFragment();
Yorke Lee6b049122013-07-16 10:38:02 -0700240
Yorke Lee936e4432013-07-29 09:54:42 -0700241 // TODO krelease: load fragments on demand instead of creating all of them at run time
242 final FragmentTransaction ft = getFragmentManager().beginTransaction();
243 ft.add(R.id.dialtacts_frame, mPhoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
244 ft.add(R.id.dialtacts_frame, mRegularSearchFragment, TAG_REGULAR_SEARCH_FRAGMENT);
245 ft.add(R.id.dialtacts_frame, mSmartDialSearchFragment, TAG_SMARTDIAL_SEARCH_FRAGMENT);
246 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
247 // Fragments will be hidden as necessary in onAttachFragment
248 ft.commit();
249 }
Yorke Lee6b049122013-07-16 10:38:02 -0700250
Yorke Leedfb2eee2013-06-26 18:24:32 -0700251 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
252 prepareSearchView();
Yorke Lee6b049122013-07-16 10:38:02 -0700253
Yorke Lee80a05ed2013-07-26 17:05:04 -0700254 displayFragment(intent);
Yorke Lee6b049122013-07-16 10:38:02 -0700255
256 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leedfb2eee2013-06-26 18:24:32 -0700257 && savedInstanceState == null) {
Yorke Lee6b049122013-07-16 10:38:02 -0700258 setupFilterText(intent);
259 }
260 }
261
262 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700263 protected void onResume() {
264 super.onResume();
265 final FragmentManager fm = getFragmentManager();
266 mPhoneFavoriteFragment = (NewPhoneFavoriteFragment) fm.findFragmentByTag(
267 TAG_FAVORITES_FRAGMENT);
268 mDialpadFragment = (NewDialpadFragment) fm.findFragmentByTag(TAG_DIALPAD_FRAGMENT);
269
270 mRegularSearchFragment = (NewSearchFragment) fm.findFragmentByTag(
271 TAG_REGULAR_SEARCH_FRAGMENT);
272 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
273 mPhoneNumberPickerActionListener);
Yorke Lee6b049122013-07-16 10:38:02 -0700274
Yorke Leedfb2eee2013-06-26 18:24:32 -0700275 mSmartDialSearchFragment = (SmartDialSearchFragment) fm.findFragmentByTag(
276 TAG_SMARTDIAL_SEARCH_FRAGMENT);
277 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
278 mPhoneNumberPickerActionListener);
Yorke Lee936e4432013-07-29 09:54:42 -0700279 }
280
281 @Override
282 public void onAttachFragment(Fragment fragment) {
283 if (fragment instanceof NewDialpadFragment || fragment instanceof NewSearchFragment
284 || fragment instanceof SmartDialSearchFragment) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700285 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Lee936e4432013-07-29 09:54:42 -0700286 transaction.hide(fragment);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700287 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700288 }
Yorke Lee936e4432013-07-29 09:54:42 -0700289 // TODO krelease: Save some kind of state here to show the appropriate fragment
290 // based on the state of the dialer when it was last paused
Yorke Lee6b049122013-07-16 10:38:02 -0700291 }
292
293 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700294 public boolean onMenuItemClick(MenuItem item) {
295 switch (item.getItemId()) {
296 case R.id.menu_import_export:
297 // We hard-code the "contactsAreAvailable" argument because doing it properly would
298 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
299 // now in Dialtacts for (potential) performance reasons. Compare with how it is
300 // done in {@link PeopleActivity}.
301 ImportExportDialogFragment.show(getFragmentManager(), true,
302 DialtactsActivity.class);
303 return true;
304 case R.id.menu_clear_frequents:
305 ClearFrequentsDialog.show(getFragmentManager());
306 return true;
307 case R.id.add_contact:
308 try {
309 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
310 } catch (ActivityNotFoundException e) {
311 Toast toast = Toast.makeText(this, R.string.add_contact_not_available,
312 Toast.LENGTH_SHORT);
313 toast.show();
314 }
315 return true;
316 case R.id.menu_call_settings:
317 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
318 startActivity(settingsIntent);
319 }
320 return false;
Yorke Lee6b049122013-07-16 10:38:02 -0700321 }
322
323 @Override
324 public void onClick(View view) {
325 switch (view.getId()) {
Yorke Lee714d6c72013-07-26 16:11:39 -0700326 case R.id.overflow_menu_on_dialpad:
Yorke Lee6b049122013-07-16 10:38:02 -0700327 case R.id.overflow_menu: {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700328 final PopupMenu popupMenu = new PopupMenu(NewDialtactsActivity.this, view);
329 final Menu menu = popupMenu.getMenu();
330 popupMenu.inflate(R.menu.dialtacts_options_new);
331 popupMenu.setOnMenuItemClickListener(this);
332 popupMenu.show();
Yorke Lee6b049122013-07-16 10:38:02 -0700333 break;
334 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700335 case R.id.dialpad_button:
Yorke Lee80a05ed2013-07-26 17:05:04 -0700336 showDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700337 break;
Yorke Lee714d6c72013-07-26 16:11:39 -0700338 case R.id.call_history_on_dialpad_button:
Yorke Leedfb2eee2013-06-26 18:24:32 -0700339 case R.id.call_history_button:
Yorke Lee80a05ed2013-07-26 17:05:04 -0700340 // TODO krelease: This should start an intent with content type
341 // CallLog.Calls.CONTENT_TYPE, once the intent filters for the call log activity
342 // is enabled
Yorke Leedfb2eee2013-06-26 18:24:32 -0700343 final Intent intent = new Intent(this, NewCallLogActivity.class);
344 startActivity(intent);
345 break;
Yorke Leed9999322013-07-19 09:22:03 -0700346 case R.id.search_close_button:
347 // Clear the search field
348 if (!TextUtils.isEmpty(mSearchView.getText())) {
349 mSearchView.setText("");
350 }
351 break;
Yorke Lee6b049122013-07-16 10:38:02 -0700352 default: {
353 Log.wtf(TAG, "Unexpected onClick event from " + view);
354 break;
355 }
356 }
357 }
358
Yorke Lee80a05ed2013-07-26 17:05:04 -0700359 private void showDialpadFragment(boolean animate) {
Yorke Lee6b049122013-07-16 10:38:02 -0700360 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700361 if (animate) {
362 ft.setCustomAnimations(R.anim.slide_in, 0);
363 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700364 ft.show(mDialpadFragment);
365 ft.commit();
366 }
Yorke Lee6b049122013-07-16 10:38:02 -0700367
Yorke Lee80a05ed2013-07-26 17:05:04 -0700368 private void hideDialpadFragment(boolean animate) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700369 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700370 if (animate) {
371 ft.setCustomAnimations(0, R.anim.slide_out);
372 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700373 ft.hide(mDialpadFragment);
374 ft.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700375 }
376
377 private void prepareSearchView() {
Yorke Leed9999322013-07-19 09:22:03 -0700378 mSearchViewContainer = findViewById(R.id.search_view_container);
379 mSearchViewCloseButton = findViewById(R.id.search_close_button);
380 mSearchViewCloseButton.setClickable(true);
381 mSearchViewCloseButton.setOnClickListener(this);
382 mSearchView = (EditText) findViewById(R.id.search_view);
383 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
384 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
385 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Yorke Lee6b049122013-07-16 10:38:02 -0700386 @Override
387 public void onFocusChange(View view, boolean hasFocus) {
388 if (hasFocus) {
389 showInputMethod(view.findFocus());
390 }
391 }
392 });
Yorke Lee6b049122013-07-16 10:38:02 -0700393 }
394
Yorke Leedfb2eee2013-06-26 18:24:32 -0700395 private void hideDialpadFragmentIfNecessary() {
396 if (mDialpadFragment.isVisible()) {
Yorke Lee80a05ed2013-07-26 17:05:04 -0700397 hideDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700398 }
399 }
Yorke Lee6b049122013-07-16 10:38:02 -0700400
Yorke Leedfb2eee2013-06-26 18:24:32 -0700401 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
402 @Override
403 public void onAnimationEnd(Animator animation) {
Yorke Leed9999322013-07-19 09:22:03 -0700404 mSearchViewContainer.setVisibility(View.GONE);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700405 }
406 };
407
408 public void hideSearchBar() {
Yorke Lee936e4432013-07-29 09:54:42 -0700409 // If the favorites fragment hasn't been fully created before the dialpad fragment
410 // is hidden (i.e. onResume), don't bother animating
411 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
412 return;
413 }
Yorke Leed9999322013-07-19 09:22:03 -0700414 mSearchViewContainer.animate().cancel();
415 mSearchViewContainer.setAlpha(1);
416 mSearchViewContainer.setTranslationY(0);
417 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
418 .setDuration(200).setListener(mHideListener);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700419
420 mPhoneFavoriteFragment.getView().animate().withLayer()
Yorke Leed9999322013-07-19 09:22:03 -0700421 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700422 new AnimatorListenerAdapter() {
423 @Override
424 public void onAnimationEnd(Animator animation) {
425 mBottomPaddingView.setVisibility(View.VISIBLE);
426 mPhoneFavoriteFragment.getView().setTranslationY(0);
427 }
428 });
429 }
430
431 public void showSearchBar() {
Yorke Lee936e4432013-07-29 09:54:42 -0700432 // If the favorites fragment hasn't been fully created before the dialpad fragment
433 // is hidden (i.e. onResume), don't bother animating
434 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
435 return;
436 }
Yorke Leed9999322013-07-19 09:22:03 -0700437 mSearchViewContainer.animate().cancel();
438 mSearchViewContainer.setAlpha(0);
439 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
440 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
Yorke Leedfb2eee2013-06-26 18:24:32 -0700441 .setListener(new AnimatorListenerAdapter() {
442 @Override
443 public void onAnimationStart(Animator animation) {
Yorke Leed9999322013-07-19 09:22:03 -0700444 mSearchViewContainer.setVisibility(View.VISIBLE);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700445 }
446 });
447
Yorke Leed9999322013-07-19 09:22:03 -0700448 mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
Yorke Leedfb2eee2013-06-26 18:24:32 -0700449 mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
450 .setListener(
451 new AnimatorListenerAdapter() {
452 @Override
453 public void onAnimationStart(Animator animation) {
454 mBottomPaddingView.setVisibility(View.GONE);
455 }
456 });
457 }
458
459
Yorke Lee714d6c72013-07-26 16:11:39 -0700460 public void setupFakeActionBarItemsForFavoritesFragment() {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700461 mMenuButton = findViewById(R.id.overflow_menu);
462 if (mMenuButton != null) {
463 // mMenuButton.setMinimumWidth(fakeMenuItemWidth);
464 if (ViewConfiguration.get(this).hasPermanentMenuKey()) {
465 // This is required for dialpad button's layout, so must not use GONE here.
466 mMenuButton.setVisibility(View.INVISIBLE);
467 } else {
468 mMenuButton.setOnClickListener(this);
Yorke Lee6b049122013-07-16 10:38:02 -0700469 }
470 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700471
472 mCallHistoryButton = findViewById(R.id.call_history_button);
473 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
474 mCallHistoryButton.setOnClickListener(this);
475
476 mDialpadButton = findViewById(R.id.dialpad_button);
477 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
478 mDialpadButton.setOnClickListener(this);
Yorke Lee6b049122013-07-16 10:38:02 -0700479 }
480
Yorke Lee714d6c72013-07-26 16:11:39 -0700481 public void setupFakeActionBarItemsForDialpadFragment() {
482 final View overflowButton = findViewById(R.id.overflow_menu_on_dialpad);
483 overflowButton.setOnClickListener(this);
484 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
485 callhistoryButton.setOnClickListener(this);
486 }
487
Yorke Lee6b049122013-07-16 10:38:02 -0700488 private void fixIntent(Intent intent) {
489 // This should be cleaned up: the call key used to send an Intent
490 // that just said to go to the recent calls list. It now sends this
491 // abstract action, but this class hasn't been rewritten to deal with it.
492 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
493 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
494 intent.putExtra("call_key", true);
495 setIntent(intent);
496 }
497 }
498
Yorke Lee6b049122013-07-16 10:38:02 -0700499 /**
500 * Returns true if the intent is due to hitting the green send key (hardware call button:
501 * KEYCODE_CALL) while in a call.
502 *
503 * @param intent the intent that launched this activity
504 * @param recentCallsRequest true if the intent is requesting to view recent calls
505 * @return true if the intent is due to hitting the green send key while in a call
506 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700507 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Yorke Lee6b049122013-07-16 10:38:02 -0700508 // If there is a call in progress go to the call screen
509 if (recentCallsRequest) {
510 final boolean callKey = intent.getBooleanExtra("call_key", false);
511
512 try {
513 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
514 if (callKey && phone != null && phone.showCallScreen()) {
515 return true;
516 }
517 } catch (RemoteException e) {
518 Log.e(TAG, "Failed to handle send while in call", e);
519 }
520 }
521
522 return false;
523 }
524
525 /**
526 * Sets the current tab based on the intent's request type
527 *
528 * @param intent Intent that contains information about which tab should be selected
529 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700530 private void displayFragment(Intent intent) {
531 // TODO krelease: Make navigation via intent work by displaying the correct fragment
532 // as appropriate.
533
Yorke Lee6b049122013-07-16 10:38:02 -0700534 // If we got here by hitting send and we're in call forward along to the in-call activity
535 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
536 getContentResolver()));
537 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
538 finish();
539 return;
540 }
Yorke Lee80a05ed2013-07-26 17:05:04 -0700541
542 if ((mDialpadFragment != null && phoneIsInUse())
543 || isDialIntent(intent)) {
544 mDialpadFragment.setStartedFromNewIntent(true);
545 // TODO krelease: This should use showDialpadFragment(false) to avoid animating
546 // the dialpad in. Need to fix the onPreDrawListener in NewDialpadFragment first.
547 showDialpadFragment(true);
548 }
Yorke Lee6b049122013-07-16 10:38:02 -0700549 }
550
551 @Override
552 public void onNewIntent(Intent newIntent) {
553 setIntent(newIntent);
554 fixIntent(newIntent);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700555 displayFragment(newIntent);
Yorke Lee6b049122013-07-16 10:38:02 -0700556 final String action = newIntent.getAction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700557
Yorke Leedfb2eee2013-06-26 18:24:32 -0700558 if (mInSearchUi || (mRegularSearchFragment != null && mRegularSearchFragment.isVisible())) {
Yorke Lee6b049122013-07-16 10:38:02 -0700559 exitSearchUi();
560 }
561
Yorke Leedfb2eee2013-06-26 18:24:32 -0700562 // TODO krelease: Handle onNewIntent for all other fragments
563 /*
564 *if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { if (mDialpadFragment != null) {
565 * mDialpadFragment.setStartedFromNewIntent(true); } else { Log.e(TAG,
566 * "DialpadFragment isn't ready yet when the tab is already selected."); } } else if
567 * (mViewPager.getCurrentItem() == TAB_INDEX_CALL_LOG) { if (mCallLogFragment != null) {
568 * mCallLogFragment.configureScreenFromIntent(newIntent); } else { Log.e(TAG,
569 * "CallLogFragment isn't ready yet when the tab is already selected."); } }
570 */
Yorke Lee6b049122013-07-16 10:38:02 -0700571 invalidateOptionsMenu();
572 }
573
574 /** Returns true if the given intent contains a phone number to populate the dialer with */
575 private boolean isDialIntent(Intent intent) {
576 final String action = intent.getAction();
577 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
578 return true;
579 }
580 if (Intent.ACTION_VIEW.equals(action)) {
581 final Uri data = intent.getData();
582 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
583 return true;
584 }
585 }
586 return false;
587 }
588
589 /**
590 * Returns an appropriate call origin for this Activity. May return null when no call origin
591 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
592 * for remembering the tab in which the user made a phone call, so the external app's DIAL
593 * request should not be counted.)
594 */
595 public String getCallOrigin() {
596 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
597 }
598
599 /**
600 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
601 * This text originally came from a FILTER_CONTACTS_ACTION intent received
602 * by this activity. The stored text will then be cleared after after this
603 * method returns.
604 *
605 * @return The stored filter text
606 */
607 public String getAndClearFilterText() {
608 String filterText = mFilterText;
609 mFilterText = null;
610 return filterText;
611 }
612
613 /**
614 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
615 * This is so child activities can check if they are supposed to display a filter.
616 *
617 * @param intent The intent received in {@link #onNewIntent(Intent)}
618 */
619 private void setupFilterText(Intent intent) {
620 // If the intent was relaunched from history, don't apply the filter text.
621 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
622 return;
623 }
624 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
625 if (filter != null && filter.length() > 0) {
626 mFilterText = filter;
627 }
628 }
629
Yorke Leedfb2eee2013-06-26 18:24:32 -0700630 private final NewPhoneFavoriteFragment.Listener mPhoneFavoriteListener =
631 new NewPhoneFavoriteFragment.Listener() {
Yorke Lee6b049122013-07-16 10:38:02 -0700632 @Override
633 public void onContactSelected(Uri contactUri) {
634 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700635 NewDialtactsActivity.this, contactUri, getCallOrigin());
Yorke Lee6b049122013-07-16 10:38:02 -0700636 }
637
638 @Override
639 public void onCallNumberDirectly(String phoneNumber) {
640 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
641 startActivity(intent);
642 }
643 };
644
Yorke Leedfb2eee2013-06-26 18:24:32 -0700645 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
646 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
647 * be showing when the search key is pressed so there is more state management involved.
Yorke Lee6b049122013-07-16 10:38:02 -0700648
649 @Override
650 public void startSearch(String initialQuery, boolean selectInitialQuery,
651 Bundle appSearchData, boolean globalSearch) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700652 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Yorke Lee6b049122013-07-16 10:38:02 -0700653 if (mInSearchUi) {
654 if (mSearchView.hasFocus()) {
655 showInputMethod(mSearchView.findFocus());
656 } else {
657 mSearchView.requestFocus();
658 }
659 } else {
660 enterSearchUi();
661 }
662 } else {
663 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
664 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700665 }*/
Yorke Lee6b049122013-07-16 10:38:02 -0700666
667 private void showInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700668 final InputMethodManager imm = (InputMethodManager) getSystemService(
669 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700670 if (imm != null) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700671 imm.showSoftInput(view, 0);
Yorke Lee6b049122013-07-16 10:38:02 -0700672 }
673 }
674
675 private void hideInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700676 final InputMethodManager imm = (InputMethodManager) getSystemService(
677 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700678 if (imm != null && view != null) {
679 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
680 }
681 }
682
683 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700684 * Shows the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700685 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700686 private void enterSearchUi(boolean smartDialSearch) {
687 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
688 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
689 transaction.hide(mPhoneFavoriteFragment);
690 if (smartDialSearch) {
691 transaction.show(mSmartDialSearchFragment);
692 } else {
693 transaction.show(mRegularSearchFragment);
Yorke Lee6b049122013-07-16 10:38:02 -0700694 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700695 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700696
Yorke Leedfb2eee2013-06-26 18:24:32 -0700697 mInSearchUi = true;
Yorke Lee6b049122013-07-16 10:38:02 -0700698 }
699
700 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700701 * Hides the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700702 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700703 private void exitSearchUi() {
704 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
705 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
706 transaction.hide(mRegularSearchFragment);
707 transaction.hide(mSmartDialSearchFragment);
708 transaction.show(mPhoneFavoriteFragment);
709 transaction.commit();
710 mInSearchUi = false;
Yorke Lee6b049122013-07-16 10:38:02 -0700711 }
712
713 /** Returns an Intent to launch Call Settings screen */
714 public static Intent getCallSettingsIntent() {
715 final Intent intent = new Intent(Intent.ACTION_MAIN);
716 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
717 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
718 return intent;
719 }
720
721 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700722 public void onBackPressed() {
723 if (mDialpadFragment.isVisible()) {
Yorke Lee80a05ed2013-07-26 17:05:04 -0700724 hideDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700725 } else if (mInSearchUi) {
Yorke Leed9999322013-07-19 09:22:03 -0700726 mSearchView.setText(null);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700727 } else if (isTaskRoot()) {
728 // Instead of stopping, simply push this to the back of the stack.
729 // This is only done when running at the top of the stack;
730 // otherwise, we have been launched by someone else so need to
731 // allow the user to go back to the caller.
732 moveTaskToBack(false);
733 } else {
734 super.onBackPressed();
Yorke Lee6b049122013-07-16 10:38:02 -0700735 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700736 }
737
738 @Override
739 public void onDialpadQueryChanged(String query) {
740 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
741 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Leed9999322013-07-19 09:22:03 -0700742 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
743 mSearchView.setText(normalizedQuery);
Yorke Lee6b049122013-07-16 10:38:02 -0700744 }
745 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700746
747 @Override
748 public void onListFragmentScrollStateChange(int scrollState) {
749 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
750 hideDialpadFragmentIfNecessary();
751 hideInputMethod(getCurrentFocus());
752 }
753 }
754
755 @Override
756 public void onPhoneFavoriteFragmentStarted() {
Yorke Lee714d6c72013-07-26 16:11:39 -0700757 setupFakeActionBarItemsForFavoritesFragment();
Yorke Leedfb2eee2013-06-26 18:24:32 -0700758 }
Yorke Lee714d6c72013-07-26 16:11:39 -0700759
760 @Override
761 public void onDialpadFragmentStarted() {
762 setupFakeActionBarItemsForDialpadFragment();
763 }
764
Yorke Lee80a05ed2013-07-26 17:05:04 -0700765 private boolean phoneIsInUse() {
766 final TelephonyManager tm = (TelephonyManager) getSystemService(
767 Context.TELEPHONY_SERVICE);
768 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
769 }
Yorke Lee6b049122013-07-16 10:38:02 -0700770}