blob: 0229c6bfec917f8e76dfd262fb8cbc9ff687c06e [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) {
186 final boolean smartDialSearch = isDialpadShowing();
187 final String newText = s.toString();
188 // Show search result with non-empty text. Show a bare list otherwise.
189 if (TextUtils.isEmpty(newText) && mInSearchUi) {
190 exitSearchUi();
191 mSearchViewCloseButton.setVisibility(View.GONE);
192 return;
193 } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
194 enterSearchUi(smartDialSearch);
Yorke Lee6b049122013-07-16 10:38:02 -0700195 }
196
Yorke Leed9999322013-07-19 09:22:03 -0700197 if (isDialpadShowing()) {
198 mSmartDialSearchFragment.setQueryString(newText, false);
199 } else {
200 mRegularSearchFragment.setQueryString(newText, false);
Yorke Lee6b049122013-07-16 10:38:02 -0700201 }
Yorke Leed9999322013-07-19 09:22:03 -0700202 mSearchViewCloseButton.setVisibility(View.VISIBLE);
203 return;
204 }
205
206 @Override
207 public void afterTextChanged(Editable s) {
208 }
Yorke Lee6b049122013-07-16 10:38:02 -0700209 };
210
Yorke Leedfb2eee2013-06-26 18:24:32 -0700211 private boolean isDialpadShowing() {
212 return mDialpadFragment.isVisible();
213 }
214
Yorke Lee6b049122013-07-16 10:38:02 -0700215 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700216 protected void onCreate(Bundle savedInstanceState) {
217 super.onCreate(savedInstanceState);
Yorke Lee6b049122013-07-16 10:38:02 -0700218
219 final Intent intent = getIntent();
220 fixIntent(intent);
221
Yorke Leedfb2eee2013-06-26 18:24:32 -0700222 setContentView(R.layout.new_dialtacts_activity);
Yorke Lee6b049122013-07-16 10:38:02 -0700223
Yorke Leedfb2eee2013-06-26 18:24:32 -0700224 getActionBar().hide();
Yorke Lee6b049122013-07-16 10:38:02 -0700225
Yorke Leedfb2eee2013-06-26 18:24:32 -0700226 mPhoneFavoriteFragment = new NewPhoneFavoriteFragment();
227 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Lee6b049122013-07-16 10:38:02 -0700228
Yorke Leedfb2eee2013-06-26 18:24:32 -0700229 mRegularSearchFragment = new NewSearchFragment();
230 mSmartDialSearchFragment = new SmartDialSearchFragment();
231 mDialpadFragment = new NewDialpadFragment();
Yorke Lee6b049122013-07-16 10:38:02 -0700232
Yorke Leedfb2eee2013-06-26 18:24:32 -0700233 // TODO krelease: load fragments on demand instead of creating all of them at run time
234 final FragmentTransaction ft = getFragmentManager().beginTransaction();
235 ft.add(R.id.dialtacts_frame, mPhoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
236 ft.add(R.id.dialtacts_frame, mRegularSearchFragment, TAG_REGULAR_SEARCH_FRAGMENT);
237 ft.add(R.id.dialtacts_frame, mSmartDialSearchFragment, TAG_SMARTDIAL_SEARCH_FRAGMENT);
238 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
239 ft.hide(mRegularSearchFragment);
240 ft.hide(mDialpadFragment);
241 ft.hide(mSmartDialSearchFragment);
242 ft.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700243
Yorke Leedfb2eee2013-06-26 18:24:32 -0700244 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
245 prepareSearchView();
Yorke Lee6b049122013-07-16 10:38:02 -0700246
Yorke Lee80a05ed2013-07-26 17:05:04 -0700247 displayFragment(intent);
Yorke Lee6b049122013-07-16 10:38:02 -0700248
249 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leedfb2eee2013-06-26 18:24:32 -0700250 && savedInstanceState == null) {
Yorke Lee6b049122013-07-16 10:38:02 -0700251 setupFilterText(intent);
252 }
253 }
254
255 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700256 protected void onResume() {
257 super.onResume();
258 final FragmentManager fm = getFragmentManager();
259 mPhoneFavoriteFragment = (NewPhoneFavoriteFragment) fm.findFragmentByTag(
260 TAG_FAVORITES_FRAGMENT);
261 mDialpadFragment = (NewDialpadFragment) fm.findFragmentByTag(TAG_DIALPAD_FRAGMENT);
262
263 mRegularSearchFragment = (NewSearchFragment) fm.findFragmentByTag(
264 TAG_REGULAR_SEARCH_FRAGMENT);
265 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
266 mPhoneNumberPickerActionListener);
267 if (!mRegularSearchFragment.isHidden()) {
268 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
269 transaction.hide(mRegularSearchFragment);
270 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700271 }
272
Yorke Leedfb2eee2013-06-26 18:24:32 -0700273 mSmartDialSearchFragment = (SmartDialSearchFragment) fm.findFragmentByTag(
274 TAG_SMARTDIAL_SEARCH_FRAGMENT);
275 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
276 mPhoneNumberPickerActionListener);
277 if (!mSmartDialSearchFragment.isHidden()) {
278 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
279 transaction.hide(mSmartDialSearchFragment);
280 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700281 }
282 }
283
284 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700285 public boolean onMenuItemClick(MenuItem item) {
286 switch (item.getItemId()) {
287 case R.id.menu_import_export:
288 // We hard-code the "contactsAreAvailable" argument because doing it properly would
289 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
290 // now in Dialtacts for (potential) performance reasons. Compare with how it is
291 // done in {@link PeopleActivity}.
292 ImportExportDialogFragment.show(getFragmentManager(), true,
293 DialtactsActivity.class);
294 return true;
295 case R.id.menu_clear_frequents:
296 ClearFrequentsDialog.show(getFragmentManager());
297 return true;
298 case R.id.add_contact:
299 try {
300 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
301 } catch (ActivityNotFoundException e) {
302 Toast toast = Toast.makeText(this, R.string.add_contact_not_available,
303 Toast.LENGTH_SHORT);
304 toast.show();
305 }
306 return true;
307 case R.id.menu_call_settings:
308 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
309 startActivity(settingsIntent);
310 }
311 return false;
Yorke Lee6b049122013-07-16 10:38:02 -0700312 }
313
314 @Override
315 public void onClick(View view) {
316 switch (view.getId()) {
Yorke Lee714d6c72013-07-26 16:11:39 -0700317 case R.id.overflow_menu_on_dialpad:
Yorke Lee6b049122013-07-16 10:38:02 -0700318 case R.id.overflow_menu: {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700319 final PopupMenu popupMenu = new PopupMenu(NewDialtactsActivity.this, view);
320 final Menu menu = popupMenu.getMenu();
321 popupMenu.inflate(R.menu.dialtacts_options_new);
322 popupMenu.setOnMenuItemClickListener(this);
323 popupMenu.show();
Yorke Lee6b049122013-07-16 10:38:02 -0700324 break;
325 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700326 case R.id.dialpad_button:
Yorke Lee80a05ed2013-07-26 17:05:04 -0700327 showDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700328 break;
Yorke Lee714d6c72013-07-26 16:11:39 -0700329 case R.id.call_history_on_dialpad_button:
Yorke Leedfb2eee2013-06-26 18:24:32 -0700330 case R.id.call_history_button:
Yorke Lee80a05ed2013-07-26 17:05:04 -0700331 // TODO krelease: This should start an intent with content type
332 // CallLog.Calls.CONTENT_TYPE, once the intent filters for the call log activity
333 // is enabled
Yorke Leedfb2eee2013-06-26 18:24:32 -0700334 final Intent intent = new Intent(this, NewCallLogActivity.class);
335 startActivity(intent);
336 break;
Yorke Leed9999322013-07-19 09:22:03 -0700337 case R.id.search_close_button:
338 // Clear the search field
339 if (!TextUtils.isEmpty(mSearchView.getText())) {
340 mSearchView.setText("");
341 }
342 break;
Yorke Lee6b049122013-07-16 10:38:02 -0700343 default: {
344 Log.wtf(TAG, "Unexpected onClick event from " + view);
345 break;
346 }
347 }
348 }
349
Yorke Lee80a05ed2013-07-26 17:05:04 -0700350 private void showDialpadFragment(boolean animate) {
Yorke Lee6b049122013-07-16 10:38:02 -0700351 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700352 if (animate) {
353 ft.setCustomAnimations(R.anim.slide_in, 0);
354 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700355 ft.show(mDialpadFragment);
356 ft.commit();
357 }
Yorke Lee6b049122013-07-16 10:38:02 -0700358
Yorke Lee80a05ed2013-07-26 17:05:04 -0700359 private void hideDialpadFragment(boolean animate) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700360 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700361 if (animate) {
362 ft.setCustomAnimations(0, R.anim.slide_out);
363 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700364 ft.hide(mDialpadFragment);
365 ft.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700366 }
367
368 private void prepareSearchView() {
Yorke Leed9999322013-07-19 09:22:03 -0700369 mSearchViewContainer = findViewById(R.id.search_view_container);
370 mSearchViewCloseButton = findViewById(R.id.search_close_button);
371 mSearchViewCloseButton.setClickable(true);
372 mSearchViewCloseButton.setOnClickListener(this);
373 mSearchView = (EditText) findViewById(R.id.search_view);
374 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
375 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
376 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Yorke Lee6b049122013-07-16 10:38:02 -0700377 @Override
378 public void onFocusChange(View view, boolean hasFocus) {
379 if (hasFocus) {
380 showInputMethod(view.findFocus());
381 }
382 }
383 });
Yorke Lee6b049122013-07-16 10:38:02 -0700384 }
385
Yorke Leedfb2eee2013-06-26 18:24:32 -0700386 private void hideDialpadFragmentIfNecessary() {
387 if (mDialpadFragment.isVisible()) {
Yorke Lee80a05ed2013-07-26 17:05:04 -0700388 hideDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700389 }
390 }
Yorke Lee6b049122013-07-16 10:38:02 -0700391
Yorke Leedfb2eee2013-06-26 18:24:32 -0700392 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
393 @Override
394 public void onAnimationEnd(Animator animation) {
Yorke Leed9999322013-07-19 09:22:03 -0700395 mSearchViewContainer.setVisibility(View.GONE);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700396 }
397 };
398
399 public void hideSearchBar() {
Yorke Leed9999322013-07-19 09:22:03 -0700400 mSearchViewContainer.animate().cancel();
401 mSearchViewContainer.setAlpha(1);
402 mSearchViewContainer.setTranslationY(0);
403 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
404 .setDuration(200).setListener(mHideListener);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700405
406 mPhoneFavoriteFragment.getView().animate().withLayer()
Yorke Leed9999322013-07-19 09:22:03 -0700407 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700408 new AnimatorListenerAdapter() {
409 @Override
410 public void onAnimationEnd(Animator animation) {
411 mBottomPaddingView.setVisibility(View.VISIBLE);
412 mPhoneFavoriteFragment.getView().setTranslationY(0);
413 }
414 });
415 }
416
417 public void showSearchBar() {
Yorke Leed9999322013-07-19 09:22:03 -0700418 mSearchViewContainer.animate().cancel();
419 mSearchViewContainer.setAlpha(0);
420 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
421 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
Yorke Leedfb2eee2013-06-26 18:24:32 -0700422 .setListener(new AnimatorListenerAdapter() {
423 @Override
424 public void onAnimationStart(Animator animation) {
Yorke Leed9999322013-07-19 09:22:03 -0700425 mSearchViewContainer.setVisibility(View.VISIBLE);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700426 }
427 });
428
Yorke Leed9999322013-07-19 09:22:03 -0700429 mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
Yorke Leedfb2eee2013-06-26 18:24:32 -0700430 mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
431 .setListener(
432 new AnimatorListenerAdapter() {
433 @Override
434 public void onAnimationStart(Animator animation) {
435 mBottomPaddingView.setVisibility(View.GONE);
436 }
437 });
438 }
439
440
Yorke Lee714d6c72013-07-26 16:11:39 -0700441 public void setupFakeActionBarItemsForFavoritesFragment() {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700442 mMenuButton = findViewById(R.id.overflow_menu);
443 if (mMenuButton != null) {
444 // mMenuButton.setMinimumWidth(fakeMenuItemWidth);
445 if (ViewConfiguration.get(this).hasPermanentMenuKey()) {
446 // This is required for dialpad button's layout, so must not use GONE here.
447 mMenuButton.setVisibility(View.INVISIBLE);
448 } else {
449 mMenuButton.setOnClickListener(this);
Yorke Lee6b049122013-07-16 10:38:02 -0700450 }
451 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700452
453 mCallHistoryButton = findViewById(R.id.call_history_button);
454 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
455 mCallHistoryButton.setOnClickListener(this);
456
457 mDialpadButton = findViewById(R.id.dialpad_button);
458 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
459 mDialpadButton.setOnClickListener(this);
Yorke Lee6b049122013-07-16 10:38:02 -0700460 }
461
Yorke Lee714d6c72013-07-26 16:11:39 -0700462 public void setupFakeActionBarItemsForDialpadFragment() {
463 final View overflowButton = findViewById(R.id.overflow_menu_on_dialpad);
464 overflowButton.setOnClickListener(this);
465 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
466 callhistoryButton.setOnClickListener(this);
467 }
468
Yorke Lee6b049122013-07-16 10:38:02 -0700469 private void fixIntent(Intent intent) {
470 // This should be cleaned up: the call key used to send an Intent
471 // that just said to go to the recent calls list. It now sends this
472 // abstract action, but this class hasn't been rewritten to deal with it.
473 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
474 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
475 intent.putExtra("call_key", true);
476 setIntent(intent);
477 }
478 }
479
Yorke Lee6b049122013-07-16 10:38:02 -0700480 /**
481 * Returns true if the intent is due to hitting the green send key (hardware call button:
482 * KEYCODE_CALL) while in a call.
483 *
484 * @param intent the intent that launched this activity
485 * @param recentCallsRequest true if the intent is requesting to view recent calls
486 * @return true if the intent is due to hitting the green send key while in a call
487 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700488 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Yorke Lee6b049122013-07-16 10:38:02 -0700489 // If there is a call in progress go to the call screen
490 if (recentCallsRequest) {
491 final boolean callKey = intent.getBooleanExtra("call_key", false);
492
493 try {
494 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
495 if (callKey && phone != null && phone.showCallScreen()) {
496 return true;
497 }
498 } catch (RemoteException e) {
499 Log.e(TAG, "Failed to handle send while in call", e);
500 }
501 }
502
503 return false;
504 }
505
506 /**
507 * Sets the current tab based on the intent's request type
508 *
509 * @param intent Intent that contains information about which tab should be selected
510 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700511 private void displayFragment(Intent intent) {
512 // TODO krelease: Make navigation via intent work by displaying the correct fragment
513 // as appropriate.
514
Yorke Lee6b049122013-07-16 10:38:02 -0700515 // If we got here by hitting send and we're in call forward along to the in-call activity
516 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
517 getContentResolver()));
518 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
519 finish();
520 return;
521 }
Yorke Lee80a05ed2013-07-26 17:05:04 -0700522
523 if ((mDialpadFragment != null && phoneIsInUse())
524 || isDialIntent(intent)) {
525 mDialpadFragment.setStartedFromNewIntent(true);
526 // TODO krelease: This should use showDialpadFragment(false) to avoid animating
527 // the dialpad in. Need to fix the onPreDrawListener in NewDialpadFragment first.
528 showDialpadFragment(true);
529 }
Yorke Lee6b049122013-07-16 10:38:02 -0700530 }
531
532 @Override
533 public void onNewIntent(Intent newIntent) {
534 setIntent(newIntent);
535 fixIntent(newIntent);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700536 displayFragment(newIntent);
Yorke Lee6b049122013-07-16 10:38:02 -0700537 final String action = newIntent.getAction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700538
Yorke Leedfb2eee2013-06-26 18:24:32 -0700539 if (mInSearchUi || (mRegularSearchFragment != null && mRegularSearchFragment.isVisible())) {
Yorke Lee6b049122013-07-16 10:38:02 -0700540 exitSearchUi();
541 }
542
Yorke Leedfb2eee2013-06-26 18:24:32 -0700543 // TODO krelease: Handle onNewIntent for all other fragments
544 /*
545 *if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { if (mDialpadFragment != null) {
546 * mDialpadFragment.setStartedFromNewIntent(true); } else { Log.e(TAG,
547 * "DialpadFragment isn't ready yet when the tab is already selected."); } } else if
548 * (mViewPager.getCurrentItem() == TAB_INDEX_CALL_LOG) { if (mCallLogFragment != null) {
549 * mCallLogFragment.configureScreenFromIntent(newIntent); } else { Log.e(TAG,
550 * "CallLogFragment isn't ready yet when the tab is already selected."); } }
551 */
Yorke Lee6b049122013-07-16 10:38:02 -0700552 invalidateOptionsMenu();
553 }
554
555 /** Returns true if the given intent contains a phone number to populate the dialer with */
556 private boolean isDialIntent(Intent intent) {
557 final String action = intent.getAction();
558 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
559 return true;
560 }
561 if (Intent.ACTION_VIEW.equals(action)) {
562 final Uri data = intent.getData();
563 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
564 return true;
565 }
566 }
567 return false;
568 }
569
570 /**
571 * Returns an appropriate call origin for this Activity. May return null when no call origin
572 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
573 * for remembering the tab in which the user made a phone call, so the external app's DIAL
574 * request should not be counted.)
575 */
576 public String getCallOrigin() {
577 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
578 }
579
580 /**
581 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
582 * This text originally came from a FILTER_CONTACTS_ACTION intent received
583 * by this activity. The stored text will then be cleared after after this
584 * method returns.
585 *
586 * @return The stored filter text
587 */
588 public String getAndClearFilterText() {
589 String filterText = mFilterText;
590 mFilterText = null;
591 return filterText;
592 }
593
594 /**
595 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
596 * This is so child activities can check if they are supposed to display a filter.
597 *
598 * @param intent The intent received in {@link #onNewIntent(Intent)}
599 */
600 private void setupFilterText(Intent intent) {
601 // If the intent was relaunched from history, don't apply the filter text.
602 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
603 return;
604 }
605 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
606 if (filter != null && filter.length() > 0) {
607 mFilterText = filter;
608 }
609 }
610
Yorke Leedfb2eee2013-06-26 18:24:32 -0700611 private final NewPhoneFavoriteFragment.Listener mPhoneFavoriteListener =
612 new NewPhoneFavoriteFragment.Listener() {
Yorke Lee6b049122013-07-16 10:38:02 -0700613 @Override
614 public void onContactSelected(Uri contactUri) {
615 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700616 NewDialtactsActivity.this, contactUri, getCallOrigin());
Yorke Lee6b049122013-07-16 10:38:02 -0700617 }
618
619 @Override
620 public void onCallNumberDirectly(String phoneNumber) {
621 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
622 startActivity(intent);
623 }
624 };
625
Yorke Leedfb2eee2013-06-26 18:24:32 -0700626 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
627 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
628 * be showing when the search key is pressed so there is more state management involved.
Yorke Lee6b049122013-07-16 10:38:02 -0700629
630 @Override
631 public void startSearch(String initialQuery, boolean selectInitialQuery,
632 Bundle appSearchData, boolean globalSearch) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700633 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Yorke Lee6b049122013-07-16 10:38:02 -0700634 if (mInSearchUi) {
635 if (mSearchView.hasFocus()) {
636 showInputMethod(mSearchView.findFocus());
637 } else {
638 mSearchView.requestFocus();
639 }
640 } else {
641 enterSearchUi();
642 }
643 } else {
644 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
645 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700646 }*/
Yorke Lee6b049122013-07-16 10:38:02 -0700647
648 private void showInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700649 final InputMethodManager imm = (InputMethodManager) getSystemService(
650 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700651 if (imm != null) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700652 imm.showSoftInput(view, 0);
Yorke Lee6b049122013-07-16 10:38:02 -0700653 }
654 }
655
656 private void hideInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700657 final InputMethodManager imm = (InputMethodManager) getSystemService(
658 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700659 if (imm != null && view != null) {
660 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
661 }
662 }
663
664 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700665 * Shows the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700666 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700667 private void enterSearchUi(boolean smartDialSearch) {
668 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
669 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
670 transaction.hide(mPhoneFavoriteFragment);
671 if (smartDialSearch) {
672 transaction.show(mSmartDialSearchFragment);
673 } else {
674 transaction.show(mRegularSearchFragment);
Yorke Lee6b049122013-07-16 10:38:02 -0700675 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700676 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700677
Yorke Leedfb2eee2013-06-26 18:24:32 -0700678 mInSearchUi = true;
Yorke Lee6b049122013-07-16 10:38:02 -0700679 }
680
681 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700682 * Hides the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700683 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700684 private void exitSearchUi() {
685 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
686 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
687 transaction.hide(mRegularSearchFragment);
688 transaction.hide(mSmartDialSearchFragment);
689 transaction.show(mPhoneFavoriteFragment);
690 transaction.commit();
691 mInSearchUi = false;
Yorke Lee6b049122013-07-16 10:38:02 -0700692 }
693
694 /** Returns an Intent to launch Call Settings screen */
695 public static Intent getCallSettingsIntent() {
696 final Intent intent = new Intent(Intent.ACTION_MAIN);
697 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
698 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
699 return intent;
700 }
701
702 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700703 public void onBackPressed() {
704 if (mDialpadFragment.isVisible()) {
Yorke Lee80a05ed2013-07-26 17:05:04 -0700705 hideDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700706 } else if (mInSearchUi) {
Yorke Leed9999322013-07-19 09:22:03 -0700707 mSearchView.setText(null);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700708 } else if (isTaskRoot()) {
709 // Instead of stopping, simply push this to the back of the stack.
710 // This is only done when running at the top of the stack;
711 // otherwise, we have been launched by someone else so need to
712 // allow the user to go back to the caller.
713 moveTaskToBack(false);
714 } else {
715 super.onBackPressed();
Yorke Lee6b049122013-07-16 10:38:02 -0700716 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700717 }
718
719 @Override
720 public void onDialpadQueryChanged(String query) {
721 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
722 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Leed9999322013-07-19 09:22:03 -0700723 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
724 mSearchView.setText(normalizedQuery);
Yorke Lee6b049122013-07-16 10:38:02 -0700725 }
726 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700727
728 @Override
729 public void onListFragmentScrollStateChange(int scrollState) {
730 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
731 hideDialpadFragmentIfNecessary();
732 hideInputMethod(getCurrentFocus());
733 }
734 }
735
736 @Override
737 public void onPhoneFavoriteFragmentStarted() {
Yorke Lee714d6c72013-07-26 16:11:39 -0700738 setupFakeActionBarItemsForFavoritesFragment();
Yorke Leedfb2eee2013-06-26 18:24:32 -0700739 }
Yorke Lee714d6c72013-07-26 16:11:39 -0700740
741 @Override
742 public void onDialpadFragmentStarted() {
743 setupFakeActionBarItemsForDialpadFragment();
744 }
745
Yorke Lee80a05ed2013-07-26 17:05:04 -0700746 private boolean phoneIsInUse() {
747 final TelephonyManager tm = (TelephonyManager) getSystemService(
748 Context.TELEPHONY_SERVICE);
749 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
750 }
Yorke Lee6b049122013-07-16 10:38:02 -0700751}