blob: 36e4a546adcb3cc79e3158a31aa979e6fd7b40d0 [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) {
Yorke Leef00e1cf2013-07-30 16:11:09 -0700463 mMenuButton.setOnClickListener(this);
Yorke Lee6b049122013-07-16 10:38:02 -0700464 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700465
466 mCallHistoryButton = findViewById(R.id.call_history_button);
467 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
468 mCallHistoryButton.setOnClickListener(this);
469
470 mDialpadButton = findViewById(R.id.dialpad_button);
471 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
472 mDialpadButton.setOnClickListener(this);
Yorke Lee6b049122013-07-16 10:38:02 -0700473 }
474
Yorke Lee714d6c72013-07-26 16:11:39 -0700475 public void setupFakeActionBarItemsForDialpadFragment() {
476 final View overflowButton = findViewById(R.id.overflow_menu_on_dialpad);
477 overflowButton.setOnClickListener(this);
478 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
479 callhistoryButton.setOnClickListener(this);
480 }
481
Yorke Lee6b049122013-07-16 10:38:02 -0700482 private void fixIntent(Intent intent) {
483 // This should be cleaned up: the call key used to send an Intent
484 // that just said to go to the recent calls list. It now sends this
485 // abstract action, but this class hasn't been rewritten to deal with it.
486 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
487 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
488 intent.putExtra("call_key", true);
489 setIntent(intent);
490 }
491 }
492
Yorke Lee6b049122013-07-16 10:38:02 -0700493 /**
494 * Returns true if the intent is due to hitting the green send key (hardware call button:
495 * KEYCODE_CALL) while in a call.
496 *
497 * @param intent the intent that launched this activity
498 * @param recentCallsRequest true if the intent is requesting to view recent calls
499 * @return true if the intent is due to hitting the green send key while in a call
500 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700501 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Yorke Lee6b049122013-07-16 10:38:02 -0700502 // If there is a call in progress go to the call screen
503 if (recentCallsRequest) {
504 final boolean callKey = intent.getBooleanExtra("call_key", false);
505
506 try {
507 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
508 if (callKey && phone != null && phone.showCallScreen()) {
509 return true;
510 }
511 } catch (RemoteException e) {
512 Log.e(TAG, "Failed to handle send while in call", e);
513 }
514 }
515
516 return false;
517 }
518
519 /**
520 * Sets the current tab based on the intent's request type
521 *
522 * @param intent Intent that contains information about which tab should be selected
523 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700524 private void displayFragment(Intent intent) {
525 // TODO krelease: Make navigation via intent work by displaying the correct fragment
526 // as appropriate.
527
Yorke Lee6b049122013-07-16 10:38:02 -0700528 // If we got here by hitting send and we're in call forward along to the in-call activity
529 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
530 getContentResolver()));
531 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
532 finish();
533 return;
534 }
Yorke Lee80a05ed2013-07-26 17:05:04 -0700535
536 if ((mDialpadFragment != null && phoneIsInUse())
537 || isDialIntent(intent)) {
538 mDialpadFragment.setStartedFromNewIntent(true);
539 // TODO krelease: This should use showDialpadFragment(false) to avoid animating
540 // the dialpad in. Need to fix the onPreDrawListener in NewDialpadFragment first.
541 showDialpadFragment(true);
542 }
Yorke Lee6b049122013-07-16 10:38:02 -0700543 }
544
545 @Override
546 public void onNewIntent(Intent newIntent) {
547 setIntent(newIntent);
548 fixIntent(newIntent);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700549 displayFragment(newIntent);
Yorke Lee6b049122013-07-16 10:38:02 -0700550 final String action = newIntent.getAction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700551
Yorke Leedfb2eee2013-06-26 18:24:32 -0700552 if (mInSearchUi || (mRegularSearchFragment != null && mRegularSearchFragment.isVisible())) {
Yorke Lee6b049122013-07-16 10:38:02 -0700553 exitSearchUi();
554 }
555
Yorke Leedfb2eee2013-06-26 18:24:32 -0700556 // TODO krelease: Handle onNewIntent for all other fragments
557 /*
558 *if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { if (mDialpadFragment != null) {
559 * mDialpadFragment.setStartedFromNewIntent(true); } else { Log.e(TAG,
560 * "DialpadFragment isn't ready yet when the tab is already selected."); } } else if
561 * (mViewPager.getCurrentItem() == TAB_INDEX_CALL_LOG) { if (mCallLogFragment != null) {
562 * mCallLogFragment.configureScreenFromIntent(newIntent); } else { Log.e(TAG,
563 * "CallLogFragment isn't ready yet when the tab is already selected."); } }
564 */
Yorke Lee6b049122013-07-16 10:38:02 -0700565 invalidateOptionsMenu();
566 }
567
568 /** Returns true if the given intent contains a phone number to populate the dialer with */
569 private boolean isDialIntent(Intent intent) {
570 final String action = intent.getAction();
571 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
572 return true;
573 }
574 if (Intent.ACTION_VIEW.equals(action)) {
575 final Uri data = intent.getData();
576 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
577 return true;
578 }
579 }
580 return false;
581 }
582
583 /**
584 * Returns an appropriate call origin for this Activity. May return null when no call origin
585 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
586 * for remembering the tab in which the user made a phone call, so the external app's DIAL
587 * request should not be counted.)
588 */
589 public String getCallOrigin() {
590 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
591 }
592
593 /**
594 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
595 * This text originally came from a FILTER_CONTACTS_ACTION intent received
596 * by this activity. The stored text will then be cleared after after this
597 * method returns.
598 *
599 * @return The stored filter text
600 */
601 public String getAndClearFilterText() {
602 String filterText = mFilterText;
603 mFilterText = null;
604 return filterText;
605 }
606
607 /**
608 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
609 * This is so child activities can check if they are supposed to display a filter.
610 *
611 * @param intent The intent received in {@link #onNewIntent(Intent)}
612 */
613 private void setupFilterText(Intent intent) {
614 // If the intent was relaunched from history, don't apply the filter text.
615 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
616 return;
617 }
618 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
619 if (filter != null && filter.length() > 0) {
620 mFilterText = filter;
621 }
622 }
623
Yorke Leedfb2eee2013-06-26 18:24:32 -0700624 private final NewPhoneFavoriteFragment.Listener mPhoneFavoriteListener =
625 new NewPhoneFavoriteFragment.Listener() {
Yorke Lee6b049122013-07-16 10:38:02 -0700626 @Override
627 public void onContactSelected(Uri contactUri) {
628 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700629 NewDialtactsActivity.this, contactUri, getCallOrigin());
Yorke Lee6b049122013-07-16 10:38:02 -0700630 }
631
632 @Override
633 public void onCallNumberDirectly(String phoneNumber) {
634 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
635 startActivity(intent);
636 }
637 };
638
Yorke Leedfb2eee2013-06-26 18:24:32 -0700639 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
640 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
641 * be showing when the search key is pressed so there is more state management involved.
Yorke Lee6b049122013-07-16 10:38:02 -0700642
643 @Override
644 public void startSearch(String initialQuery, boolean selectInitialQuery,
645 Bundle appSearchData, boolean globalSearch) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700646 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Yorke Lee6b049122013-07-16 10:38:02 -0700647 if (mInSearchUi) {
648 if (mSearchView.hasFocus()) {
649 showInputMethod(mSearchView.findFocus());
650 } else {
651 mSearchView.requestFocus();
652 }
653 } else {
654 enterSearchUi();
655 }
656 } else {
657 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
658 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700659 }*/
Yorke Lee6b049122013-07-16 10:38:02 -0700660
661 private void showInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700662 final InputMethodManager imm = (InputMethodManager) getSystemService(
663 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700664 if (imm != null) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700665 imm.showSoftInput(view, 0);
Yorke Lee6b049122013-07-16 10:38:02 -0700666 }
667 }
668
669 private void hideInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700670 final InputMethodManager imm = (InputMethodManager) getSystemService(
671 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700672 if (imm != null && view != null) {
673 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
674 }
675 }
676
677 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700678 * Shows the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700679 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700680 private void enterSearchUi(boolean smartDialSearch) {
681 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
682 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
683 transaction.hide(mPhoneFavoriteFragment);
684 if (smartDialSearch) {
685 transaction.show(mSmartDialSearchFragment);
686 } else {
687 transaction.show(mRegularSearchFragment);
Yorke Lee6b049122013-07-16 10:38:02 -0700688 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700689 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700690
Yorke Leedfb2eee2013-06-26 18:24:32 -0700691 mInSearchUi = true;
Yorke Lee6b049122013-07-16 10:38:02 -0700692 }
693
694 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700695 * Hides the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700696 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700697 private void exitSearchUi() {
698 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
699 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
700 transaction.hide(mRegularSearchFragment);
701 transaction.hide(mSmartDialSearchFragment);
702 transaction.show(mPhoneFavoriteFragment);
703 transaction.commit();
704 mInSearchUi = false;
Yorke Lee6b049122013-07-16 10:38:02 -0700705 }
706
707 /** Returns an Intent to launch Call Settings screen */
708 public static Intent getCallSettingsIntent() {
709 final Intent intent = new Intent(Intent.ACTION_MAIN);
710 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
711 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
712 return intent;
713 }
714
715 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700716 public void onBackPressed() {
717 if (mDialpadFragment.isVisible()) {
Yorke Lee80a05ed2013-07-26 17:05:04 -0700718 hideDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700719 } else if (mInSearchUi) {
Yorke Leed9999322013-07-19 09:22:03 -0700720 mSearchView.setText(null);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700721 } else if (isTaskRoot()) {
722 // Instead of stopping, simply push this to the back of the stack.
723 // This is only done when running at the top of the stack;
724 // otherwise, we have been launched by someone else so need to
725 // allow the user to go back to the caller.
726 moveTaskToBack(false);
727 } else {
728 super.onBackPressed();
Yorke Lee6b049122013-07-16 10:38:02 -0700729 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700730 }
731
732 @Override
733 public void onDialpadQueryChanged(String query) {
734 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
735 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Leed9999322013-07-19 09:22:03 -0700736 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
737 mSearchView.setText(normalizedQuery);
Yorke Lee6b049122013-07-16 10:38:02 -0700738 }
739 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700740
741 @Override
742 public void onListFragmentScrollStateChange(int scrollState) {
743 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
744 hideDialpadFragmentIfNecessary();
745 hideInputMethod(getCurrentFocus());
746 }
747 }
748
749 @Override
750 public void onPhoneFavoriteFragmentStarted() {
Yorke Lee714d6c72013-07-26 16:11:39 -0700751 setupFakeActionBarItemsForFavoritesFragment();
Yorke Leedfb2eee2013-06-26 18:24:32 -0700752 }
Yorke Lee714d6c72013-07-26 16:11:39 -0700753
754 @Override
755 public void onDialpadFragmentStarted() {
756 setupFakeActionBarItemsForDialpadFragment();
757 }
758
Yorke Lee80a05ed2013-07-26 17:05:04 -0700759 private boolean phoneIsInUse() {
760 final TelephonyManager tm = (TelephonyManager) getSystemService(
761 Context.TELEPHONY_SERVICE);
762 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
763 }
Yorke Lee6b049122013-07-16 10:38:02 -0700764}