blob: 95d9ea7df575a189caca27bacc65b1dd2163e14d [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 Lee191df332013-07-30 17:21:28 -070041import android.speech.RecognizerIntent;
Yorke Lee80a05ed2013-07-26 17:05:04 -070042import android.telephony.TelephonyManager;
Yorke Leed9999322013-07-19 09:22:03 -070043import android.text.Editable;
Yorke Lee6b049122013-07-16 10:38:02 -070044import android.text.TextUtils;
Yorke Leed9999322013-07-19 09:22:03 -070045import android.text.TextWatcher;
Yorke Lee6b049122013-07-16 10:38:02 -070046import android.util.Log;
47import android.view.Menu;
Yorke Lee6b049122013-07-16 10:38:02 -070048import android.view.MenuItem;
Yorke Lee6b049122013-07-16 10:38:02 -070049import android.view.View;
Yorke Lee6b049122013-07-16 10:38:02 -070050import android.view.View.OnFocusChangeListener;
51import android.view.ViewConfiguration;
Yorke Lee6b049122013-07-16 10:38:02 -070052import android.view.inputmethod.InputMethodManager;
Yorke Leedfb2eee2013-06-26 18:24:32 -070053import android.widget.AbsListView.OnScrollListener;
Yorke Leed9999322013-07-19 09:22:03 -070054import android.widget.EditText;
55import android.widget.ImageView;
Yorke Lee6b049122013-07-16 10:38:02 -070056import android.widget.PopupMenu;
57import android.widget.SearchView;
58import android.widget.SearchView.OnCloseListener;
59import android.widget.SearchView.OnQueryTextListener;
60import android.widget.Toast;
61
62import com.android.contacts.common.CallUtil;
63import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leedfb2eee2013-06-26 18:24:32 -070064import com.android.contacts.common.dialog.ClearFrequentsDialog;
65import com.android.contacts.common.interactions.ImportExportDialogFragment;
Yorke Lee6b049122013-07-16 10:38:02 -070066import com.android.contacts.common.list.ContactListItemView;
67import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
68import com.android.contacts.common.list.PhoneNumberPickerFragment;
Yorke Leedfb2eee2013-06-26 18:24:32 -070069import com.android.dialer.calllog.NewCallLogActivity;
70import com.android.dialer.dialpad.NewDialpadFragment;
71import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Lee6b049122013-07-16 10:38:02 -070072import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Leedfb2eee2013-06-26 18:24:32 -070073import com.android.dialer.list.NewPhoneFavoriteFragment;
74import com.android.dialer.list.OnListFragmentScrolledListener;
75import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee6b049122013-07-16 10:38:02 -070076import com.android.internal.telephony.ITelephony;
77
Yorke Lee191df332013-07-30 17:21:28 -070078import java.util.ArrayList;
79
Yorke Lee6b049122013-07-16 10:38:02 -070080/**
Yorke Lee6b049122013-07-16 10:38:02 -070081 * The dialer tab's title is 'phone', a more common name (see strings.xml).
Yorke Leedfb2eee2013-06-26 18:24:32 -070082 *
83 * TODO krelease: All classes currently prefixed with New will replace the original classes or
84 * be renamed more appropriately before shipping.
Yorke Lee6b049122013-07-16 10:38:02 -070085 */
Yorke Leedfb2eee2013-06-26 18:24:32 -070086public class NewDialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
87 NewDialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
88 OnListFragmentScrolledListener,
Yorke Lee714d6c72013-07-26 16:11:39 -070089 NewPhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener,
90 NewDialpadFragment.OnDialpadFragmentStartedListener {
Yorke Lee6b049122013-07-16 10:38:02 -070091 private static final String TAG = "DialtactsActivity";
92
93 public static final boolean DEBUG = false;
94
95 /** Used to open Call Setting */
96 private static final String PHONE_PACKAGE = "com.android.phone";
97 private static final String CALL_SETTINGS_CLASS_NAME =
98 "com.android.phone.CallFeaturesSetting";
99
100 /** @see #getCallOrigin() */
101 private static final String CALL_ORIGIN_DIALTACTS =
102 "com.android.dialer.DialtactsActivity";
103
Yorke Leedfb2eee2013-06-26 18:24:32 -0700104 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
105 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
106 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
107 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
108
Yorke Lee6b049122013-07-16 10:38:02 -0700109 /**
110 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
111 */
112 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
113
Yorke Lee6b049122013-07-16 10:38:02 -0700114 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
115
Yorke Lee191df332013-07-30 17:21:28 -0700116 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
117
Yorke Lee6b049122013-07-16 10:38:02 -0700118 private String mFilterText;
119
Yorke Leedfb2eee2013-06-26 18:24:32 -0700120 /**
121 * The main fragment displaying the user's favorites and frequent contacts
122 */
123 private NewPhoneFavoriteFragment mPhoneFavoriteFragment;
Yorke Lee6b049122013-07-16 10:38:02 -0700124
125 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700126 * Fragment containing the dialpad that slides into view
Yorke Lee6b049122013-07-16 10:38:02 -0700127 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700128 private NewDialpadFragment mDialpadFragment;
129
130 /**
131 * Fragment for searching phone numbers using the alphanumeric keyboard.
132 */
133 private NewSearchFragment mRegularSearchFragment;
134
135 /**
136 * Fragment for searching phone numbers using the dialpad.
137 */
138 private SmartDialSearchFragment mSmartDialSearchFragment;
139
140 private View mMenuButton;
141 private View mCallHistoryButton;
142 private View mDialpadButton;
143
144 // Padding view used to shift the fragments up when the dialpad is shown.
145 private View mBottomPaddingView;
146
Yorke Lee6b049122013-07-16 10:38:02 -0700147 /**
148 * True when this Activity is in its search UI (with a {@link SearchView} and
149 * {@link PhoneNumberPickerFragment}).
150 */
151 private boolean mInSearchUi;
Yorke Leed9999322013-07-19 09:22:03 -0700152 private View mSearchViewContainer;
153 private View mSearchViewCloseButton;
Yorke Lee191df332013-07-30 17:21:28 -0700154 private View mVoiceSearchButton;
Yorke Leed9999322013-07-19 09:22:03 -0700155 private EditText mSearchView;
Yorke Lee6b049122013-07-16 10:38:02 -0700156
Yorke Lee6b049122013-07-16 10:38:02 -0700157 /**
Yorke Lee6b049122013-07-16 10:38:02 -0700158 * Listener used when one of phone numbers in search UI is selected. This will initiate a
159 * phone call using the phone number.
160 */
161 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
162 new OnPhoneNumberPickerActionListener() {
163 @Override
164 public void onPickPhoneNumberAction(Uri dataUri) {
165 // Specify call-origin so that users will see the previous tab instead of
166 // CallLog screen (search UI will be automatically exited).
167 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700168 NewDialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee6b049122013-07-16 10:38:02 -0700169 }
170
171 @Override
172 public void onShortcutIntentCreated(Intent intent) {
173 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
174 }
175
176 @Override
177 public void onHomeInActionBarSelected() {
178 exitSearchUi();
179 }
180 };
181
182 /**
183 * Listener used to send search queries to the phone search fragment.
184 */
Yorke Leed9999322013-07-19 09:22:03 -0700185 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
186 @Override
187 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
188 }
189
190 @Override
191 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Lee936e4432013-07-29 09:54:42 -0700192 // TODO krelease: populate the search fragments with the correct
193 // search query at the correct point in time of the fragment lifecycle.
194 // The current behavior is to simply return to the favorites screen
195 // (when docked), or returning to the Dialer after it has been
196 // swapped out of memory.
197 if (mDialpadFragment == null) return;
198 final boolean smartDialSearch = isDialpadShowing();
Yorke Leed9999322013-07-19 09:22:03 -0700199 final String newText = s.toString();
200 // Show search result with non-empty text. Show a bare list otherwise.
201 if (TextUtils.isEmpty(newText) && mInSearchUi) {
202 exitSearchUi();
203 mSearchViewCloseButton.setVisibility(View.GONE);
204 return;
205 } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
206 enterSearchUi(smartDialSearch);
Yorke Lee6b049122013-07-16 10:38:02 -0700207 }
208
Yorke Lee936e4432013-07-29 09:54:42 -0700209 if (smartDialSearch) {
Yorke Leed9999322013-07-19 09:22:03 -0700210 mSmartDialSearchFragment.setQueryString(newText, false);
211 } else {
212 mRegularSearchFragment.setQueryString(newText, false);
Yorke Lee6b049122013-07-16 10:38:02 -0700213 }
Yorke Leed9999322013-07-19 09:22:03 -0700214 mSearchViewCloseButton.setVisibility(View.VISIBLE);
215 return;
216 }
217
218 @Override
219 public void afterTextChanged(Editable s) {
220 }
Yorke Lee6b049122013-07-16 10:38:02 -0700221 };
222
Yorke Leedfb2eee2013-06-26 18:24:32 -0700223 private boolean isDialpadShowing() {
Yorke Lee936e4432013-07-29 09:54:42 -0700224 return mDialpadFragment != null && mDialpadFragment.isVisible();
Yorke Leedfb2eee2013-06-26 18:24:32 -0700225 }
226
Yorke Lee6b049122013-07-16 10:38:02 -0700227 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700228 protected void onCreate(Bundle savedInstanceState) {
229 super.onCreate(savedInstanceState);
Yorke Lee6b049122013-07-16 10:38:02 -0700230
231 final Intent intent = getIntent();
232 fixIntent(intent);
233
Yorke Leedfb2eee2013-06-26 18:24:32 -0700234 setContentView(R.layout.new_dialtacts_activity);
Yorke Lee6b049122013-07-16 10:38:02 -0700235
Yorke Leedfb2eee2013-06-26 18:24:32 -0700236 getActionBar().hide();
Yorke Lee6b049122013-07-16 10:38:02 -0700237
Yorke Lee936e4432013-07-29 09:54:42 -0700238 if (savedInstanceState == null) {
239 mPhoneFavoriteFragment = new NewPhoneFavoriteFragment();
240 mPhoneFavoriteFragment.setRetainInstance(true);
241 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Lee6b049122013-07-16 10:38:02 -0700242
Yorke Lee936e4432013-07-29 09:54:42 -0700243 mRegularSearchFragment = new NewSearchFragment();
244 mSmartDialSearchFragment = new SmartDialSearchFragment();
245 mDialpadFragment = new NewDialpadFragment();
Yorke Lee6b049122013-07-16 10:38:02 -0700246
Yorke Lee936e4432013-07-29 09:54:42 -0700247 // TODO krelease: load fragments on demand instead of creating all of them at run time
248 final FragmentTransaction ft = getFragmentManager().beginTransaction();
249 ft.add(R.id.dialtacts_frame, mPhoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
250 ft.add(R.id.dialtacts_frame, mRegularSearchFragment, TAG_REGULAR_SEARCH_FRAGMENT);
251 ft.add(R.id.dialtacts_frame, mSmartDialSearchFragment, TAG_SMARTDIAL_SEARCH_FRAGMENT);
252 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
253 // Fragments will be hidden as necessary in onAttachFragment
254 ft.commit();
255 }
Yorke Lee6b049122013-07-16 10:38:02 -0700256
Yorke Leedfb2eee2013-06-26 18:24:32 -0700257 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
258 prepareSearchView();
Yorke Lee6b049122013-07-16 10:38:02 -0700259
Yorke Lee80a05ed2013-07-26 17:05:04 -0700260 displayFragment(intent);
Yorke Lee6b049122013-07-16 10:38:02 -0700261
262 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leedfb2eee2013-06-26 18:24:32 -0700263 && savedInstanceState == null) {
Yorke Lee6b049122013-07-16 10:38:02 -0700264 setupFilterText(intent);
265 }
266 }
267
268 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700269 protected void onResume() {
270 super.onResume();
271 final FragmentManager fm = getFragmentManager();
272 mPhoneFavoriteFragment = (NewPhoneFavoriteFragment) fm.findFragmentByTag(
273 TAG_FAVORITES_FRAGMENT);
274 mDialpadFragment = (NewDialpadFragment) fm.findFragmentByTag(TAG_DIALPAD_FRAGMENT);
275
276 mRegularSearchFragment = (NewSearchFragment) fm.findFragmentByTag(
277 TAG_REGULAR_SEARCH_FRAGMENT);
278 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
279 mPhoneNumberPickerActionListener);
Yorke Lee6b049122013-07-16 10:38:02 -0700280
Yorke Leedfb2eee2013-06-26 18:24:32 -0700281 mSmartDialSearchFragment = (SmartDialSearchFragment) fm.findFragmentByTag(
282 TAG_SMARTDIAL_SEARCH_FRAGMENT);
283 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
284 mPhoneNumberPickerActionListener);
Yorke Lee936e4432013-07-29 09:54:42 -0700285 }
286
287 @Override
288 public void onAttachFragment(Fragment fragment) {
289 if (fragment instanceof NewDialpadFragment || fragment instanceof NewSearchFragment
290 || fragment instanceof SmartDialSearchFragment) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700291 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Lee936e4432013-07-29 09:54:42 -0700292 transaction.hide(fragment);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700293 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700294 }
Yorke Lee936e4432013-07-29 09:54:42 -0700295 // TODO krelease: Save some kind of state here to show the appropriate fragment
296 // based on the state of the dialer when it was last paused
Yorke Lee6b049122013-07-16 10:38:02 -0700297 }
298
299 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700300 public boolean onMenuItemClick(MenuItem item) {
301 switch (item.getItemId()) {
302 case R.id.menu_import_export:
303 // We hard-code the "contactsAreAvailable" argument because doing it properly would
304 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
305 // now in Dialtacts for (potential) performance reasons. Compare with how it is
306 // done in {@link PeopleActivity}.
307 ImportExportDialogFragment.show(getFragmentManager(), true,
308 DialtactsActivity.class);
309 return true;
310 case R.id.menu_clear_frequents:
311 ClearFrequentsDialog.show(getFragmentManager());
312 return true;
313 case R.id.add_contact:
314 try {
315 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
316 } catch (ActivityNotFoundException e) {
317 Toast toast = Toast.makeText(this, R.string.add_contact_not_available,
318 Toast.LENGTH_SHORT);
319 toast.show();
320 }
321 return true;
322 case R.id.menu_call_settings:
323 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
324 startActivity(settingsIntent);
325 }
326 return false;
Yorke Lee6b049122013-07-16 10:38:02 -0700327 }
328
329 @Override
330 public void onClick(View view) {
331 switch (view.getId()) {
Yorke Lee6b049122013-07-16 10:38:02 -0700332 case R.id.overflow_menu: {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700333 final PopupMenu popupMenu = new PopupMenu(NewDialtactsActivity.this, view);
334 final Menu menu = popupMenu.getMenu();
335 popupMenu.inflate(R.menu.dialtacts_options_new);
336 popupMenu.setOnMenuItemClickListener(this);
337 popupMenu.show();
Yorke Lee6b049122013-07-16 10:38:02 -0700338 break;
339 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700340 case R.id.dialpad_button:
Yorke Lee80a05ed2013-07-26 17:05:04 -0700341 showDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700342 break;
Yorke Lee714d6c72013-07-26 16:11:39 -0700343 case R.id.call_history_on_dialpad_button:
Yorke Leedfb2eee2013-06-26 18:24:32 -0700344 case R.id.call_history_button:
Yorke Lee80a05ed2013-07-26 17:05:04 -0700345 // TODO krelease: This should start an intent with content type
346 // CallLog.Calls.CONTENT_TYPE, once the intent filters for the call log activity
347 // is enabled
Yorke Leedfb2eee2013-06-26 18:24:32 -0700348 final Intent intent = new Intent(this, NewCallLogActivity.class);
349 startActivity(intent);
350 break;
Yorke Leed9999322013-07-19 09:22:03 -0700351 case R.id.search_close_button:
352 // Clear the search field
353 if (!TextUtils.isEmpty(mSearchView.getText())) {
354 mSearchView.setText("");
355 }
356 break;
Yorke Lee191df332013-07-30 17:21:28 -0700357 case R.id.voice_search_button:
358 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
359 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
360 break;
Yorke Lee6b049122013-07-16 10:38:02 -0700361 default: {
362 Log.wtf(TAG, "Unexpected onClick event from " + view);
363 break;
364 }
365 }
366 }
367
Yorke Lee191df332013-07-30 17:21:28 -0700368 @Override
369 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
370 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
371 if (resultCode == RESULT_OK) {
372 final ArrayList<String> matches = data.getStringArrayListExtra(
373 RecognizerIntent.EXTRA_RESULTS);
374 if (matches.size() > 0) {
375 final String match = matches.get(0);
376 mSearchView.setText(match);
377 } else {
378 Log.e(TAG, "Voice search - nothing heard");
379 }
380 } else {
381 Log.e(TAG, "Voice search failed");
382 }
383 }
384 super.onActivityResult(requestCode, resultCode, data);
385 }
386
Yorke Lee80a05ed2013-07-26 17:05:04 -0700387 private void showDialpadFragment(boolean animate) {
Yorke Lee6b049122013-07-16 10:38:02 -0700388 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700389 if (animate) {
390 ft.setCustomAnimations(R.anim.slide_in, 0);
391 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700392 ft.show(mDialpadFragment);
393 ft.commit();
394 }
Yorke Lee6b049122013-07-16 10:38:02 -0700395
Yorke Lee80a05ed2013-07-26 17:05:04 -0700396 private void hideDialpadFragment(boolean animate) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700397 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700398 if (animate) {
399 ft.setCustomAnimations(0, R.anim.slide_out);
400 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700401 ft.hide(mDialpadFragment);
402 ft.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700403 }
404
405 private void prepareSearchView() {
Yorke Leed9999322013-07-19 09:22:03 -0700406 mSearchViewContainer = findViewById(R.id.search_view_container);
407 mSearchViewCloseButton = findViewById(R.id.search_close_button);
Yorke Leed9999322013-07-19 09:22:03 -0700408 mSearchViewCloseButton.setOnClickListener(this);
Yorke Lee191df332013-07-30 17:21:28 -0700409 mVoiceSearchButton = findViewById(R.id.voice_search_button);
410 mVoiceSearchButton.setOnClickListener(this);
Yorke Leed9999322013-07-19 09:22:03 -0700411 mSearchView = (EditText) findViewById(R.id.search_view);
412 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
413 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
414 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Yorke Lee6b049122013-07-16 10:38:02 -0700415 @Override
416 public void onFocusChange(View view, boolean hasFocus) {
417 if (hasFocus) {
418 showInputMethod(view.findFocus());
419 }
420 }
421 });
Yorke Lee6b049122013-07-16 10:38:02 -0700422 }
423
Yorke Leedfb2eee2013-06-26 18:24:32 -0700424 private void hideDialpadFragmentIfNecessary() {
425 if (mDialpadFragment.isVisible()) {
Yorke Lee80a05ed2013-07-26 17:05:04 -0700426 hideDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700427 }
428 }
Yorke Lee6b049122013-07-16 10:38:02 -0700429
Yorke Leedfb2eee2013-06-26 18:24:32 -0700430 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
431 @Override
432 public void onAnimationEnd(Animator animation) {
Yorke Leed9999322013-07-19 09:22:03 -0700433 mSearchViewContainer.setVisibility(View.GONE);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700434 }
435 };
436
437 public void hideSearchBar() {
Yorke Lee936e4432013-07-29 09:54:42 -0700438 // If the favorites fragment hasn't been fully created before the dialpad fragment
439 // is hidden (i.e. onResume), don't bother animating
440 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
441 return;
442 }
Yorke Leed9999322013-07-19 09:22:03 -0700443 mSearchViewContainer.animate().cancel();
444 mSearchViewContainer.setAlpha(1);
445 mSearchViewContainer.setTranslationY(0);
446 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
447 .setDuration(200).setListener(mHideListener);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700448
449 mPhoneFavoriteFragment.getView().animate().withLayer()
Yorke Leed9999322013-07-19 09:22:03 -0700450 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700451 new AnimatorListenerAdapter() {
452 @Override
453 public void onAnimationEnd(Animator animation) {
454 mBottomPaddingView.setVisibility(View.VISIBLE);
455 mPhoneFavoriteFragment.getView().setTranslationY(0);
456 }
457 });
458 }
459
460 public void showSearchBar() {
Yorke Lee936e4432013-07-29 09:54:42 -0700461 // If the favorites fragment hasn't been fully created before the dialpad fragment
462 // is hidden (i.e. onResume), don't bother animating
463 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
464 return;
465 }
Yorke Leed9999322013-07-19 09:22:03 -0700466 mSearchViewContainer.animate().cancel();
467 mSearchViewContainer.setAlpha(0);
468 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
469 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
Yorke Leedfb2eee2013-06-26 18:24:32 -0700470 .setListener(new AnimatorListenerAdapter() {
471 @Override
472 public void onAnimationStart(Animator animation) {
Yorke Leed9999322013-07-19 09:22:03 -0700473 mSearchViewContainer.setVisibility(View.VISIBLE);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700474 }
475 });
476
Yorke Leed9999322013-07-19 09:22:03 -0700477 mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
Yorke Leedfb2eee2013-06-26 18:24:32 -0700478 mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
479 .setListener(
480 new AnimatorListenerAdapter() {
481 @Override
482 public void onAnimationStart(Animator animation) {
483 mBottomPaddingView.setVisibility(View.GONE);
484 }
485 });
486 }
487
488
Yorke Lee714d6c72013-07-26 16:11:39 -0700489 public void setupFakeActionBarItemsForFavoritesFragment() {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700490 mMenuButton = findViewById(R.id.overflow_menu);
491 if (mMenuButton != null) {
Yorke Leef00e1cf2013-07-30 16:11:09 -0700492 mMenuButton.setOnClickListener(this);
Yorke Lee6b049122013-07-16 10:38:02 -0700493 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700494
495 mCallHistoryButton = findViewById(R.id.call_history_button);
496 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
497 mCallHistoryButton.setOnClickListener(this);
498
499 mDialpadButton = findViewById(R.id.dialpad_button);
500 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
501 mDialpadButton.setOnClickListener(this);
Yorke Lee6b049122013-07-16 10:38:02 -0700502 }
503
Yorke Lee714d6c72013-07-26 16:11:39 -0700504 public void setupFakeActionBarItemsForDialpadFragment() {
Yorke Lee714d6c72013-07-26 16:11:39 -0700505 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
506 callhistoryButton.setOnClickListener(this);
507 }
508
Yorke Lee6b049122013-07-16 10:38:02 -0700509 private void fixIntent(Intent intent) {
510 // This should be cleaned up: the call key used to send an Intent
511 // that just said to go to the recent calls list. It now sends this
512 // abstract action, but this class hasn't been rewritten to deal with it.
513 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
514 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
515 intent.putExtra("call_key", true);
516 setIntent(intent);
517 }
518 }
519
Yorke Lee6b049122013-07-16 10:38:02 -0700520 /**
521 * Returns true if the intent is due to hitting the green send key (hardware call button:
522 * KEYCODE_CALL) while in a call.
523 *
524 * @param intent the intent that launched this activity
525 * @param recentCallsRequest true if the intent is requesting to view recent calls
526 * @return true if the intent is due to hitting the green send key while in a call
527 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700528 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Yorke Lee6b049122013-07-16 10:38:02 -0700529 // If there is a call in progress go to the call screen
530 if (recentCallsRequest) {
531 final boolean callKey = intent.getBooleanExtra("call_key", false);
532
533 try {
534 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
535 if (callKey && phone != null && phone.showCallScreen()) {
536 return true;
537 }
538 } catch (RemoteException e) {
539 Log.e(TAG, "Failed to handle send while in call", e);
540 }
541 }
542
543 return false;
544 }
545
546 /**
547 * Sets the current tab based on the intent's request type
548 *
549 * @param intent Intent that contains information about which tab should be selected
550 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700551 private void displayFragment(Intent intent) {
552 // TODO krelease: Make navigation via intent work by displaying the correct fragment
553 // as appropriate.
554
Yorke Lee6b049122013-07-16 10:38:02 -0700555 // If we got here by hitting send and we're in call forward along to the in-call activity
556 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
557 getContentResolver()));
558 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
559 finish();
560 return;
561 }
Yorke Lee80a05ed2013-07-26 17:05:04 -0700562
563 if ((mDialpadFragment != null && phoneIsInUse())
564 || isDialIntent(intent)) {
565 mDialpadFragment.setStartedFromNewIntent(true);
566 // TODO krelease: This should use showDialpadFragment(false) to avoid animating
567 // the dialpad in. Need to fix the onPreDrawListener in NewDialpadFragment first.
568 showDialpadFragment(true);
569 }
Yorke Lee6b049122013-07-16 10:38:02 -0700570 }
571
572 @Override
573 public void onNewIntent(Intent newIntent) {
574 setIntent(newIntent);
575 fixIntent(newIntent);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700576 displayFragment(newIntent);
Yorke Lee6b049122013-07-16 10:38:02 -0700577 final String action = newIntent.getAction();
Yorke Lee80a05ed2013-07-26 17:05:04 -0700578
Yorke Leedfb2eee2013-06-26 18:24:32 -0700579 if (mInSearchUi || (mRegularSearchFragment != null && mRegularSearchFragment.isVisible())) {
Yorke Lee6b049122013-07-16 10:38:02 -0700580 exitSearchUi();
581 }
582
Yorke Leedfb2eee2013-06-26 18:24:32 -0700583 // TODO krelease: Handle onNewIntent for all other fragments
584 /*
585 *if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { if (mDialpadFragment != null) {
586 * mDialpadFragment.setStartedFromNewIntent(true); } else { Log.e(TAG,
587 * "DialpadFragment isn't ready yet when the tab is already selected."); } } else if
588 * (mViewPager.getCurrentItem() == TAB_INDEX_CALL_LOG) { if (mCallLogFragment != null) {
589 * mCallLogFragment.configureScreenFromIntent(newIntent); } else { Log.e(TAG,
590 * "CallLogFragment isn't ready yet when the tab is already selected."); } }
591 */
Yorke Lee6b049122013-07-16 10:38:02 -0700592 invalidateOptionsMenu();
593 }
594
595 /** Returns true if the given intent contains a phone number to populate the dialer with */
596 private boolean isDialIntent(Intent intent) {
597 final String action = intent.getAction();
598 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
599 return true;
600 }
601 if (Intent.ACTION_VIEW.equals(action)) {
602 final Uri data = intent.getData();
603 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
604 return true;
605 }
606 }
607 return false;
608 }
609
610 /**
611 * Returns an appropriate call origin for this Activity. May return null when no call origin
612 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
613 * for remembering the tab in which the user made a phone call, so the external app's DIAL
614 * request should not be counted.)
615 */
616 public String getCallOrigin() {
617 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
618 }
619
620 /**
621 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
622 * This text originally came from a FILTER_CONTACTS_ACTION intent received
623 * by this activity. The stored text will then be cleared after after this
624 * method returns.
625 *
626 * @return The stored filter text
627 */
628 public String getAndClearFilterText() {
629 String filterText = mFilterText;
630 mFilterText = null;
631 return filterText;
632 }
633
634 /**
635 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
636 * This is so child activities can check if they are supposed to display a filter.
637 *
638 * @param intent The intent received in {@link #onNewIntent(Intent)}
639 */
640 private void setupFilterText(Intent intent) {
641 // If the intent was relaunched from history, don't apply the filter text.
642 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
643 return;
644 }
645 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
646 if (filter != null && filter.length() > 0) {
647 mFilterText = filter;
648 }
649 }
650
Yorke Leedfb2eee2013-06-26 18:24:32 -0700651 private final NewPhoneFavoriteFragment.Listener mPhoneFavoriteListener =
652 new NewPhoneFavoriteFragment.Listener() {
Yorke Lee6b049122013-07-16 10:38:02 -0700653 @Override
654 public void onContactSelected(Uri contactUri) {
655 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leedfb2eee2013-06-26 18:24:32 -0700656 NewDialtactsActivity.this, contactUri, getCallOrigin());
Yorke Lee6b049122013-07-16 10:38:02 -0700657 }
658
659 @Override
660 public void onCallNumberDirectly(String phoneNumber) {
661 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
662 startActivity(intent);
663 }
664 };
665
Yorke Leedfb2eee2013-06-26 18:24:32 -0700666 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
667 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
668 * be showing when the search key is pressed so there is more state management involved.
Yorke Lee6b049122013-07-16 10:38:02 -0700669
670 @Override
671 public void startSearch(String initialQuery, boolean selectInitialQuery,
672 Bundle appSearchData, boolean globalSearch) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700673 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Yorke Lee6b049122013-07-16 10:38:02 -0700674 if (mInSearchUi) {
675 if (mSearchView.hasFocus()) {
676 showInputMethod(mSearchView.findFocus());
677 } else {
678 mSearchView.requestFocus();
679 }
680 } else {
681 enterSearchUi();
682 }
683 } else {
684 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
685 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700686 }*/
Yorke Lee6b049122013-07-16 10:38:02 -0700687
688 private void showInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700689 final InputMethodManager imm = (InputMethodManager) getSystemService(
690 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700691 if (imm != null) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700692 imm.showSoftInput(view, 0);
Yorke Lee6b049122013-07-16 10:38:02 -0700693 }
694 }
695
696 private void hideInputMethod(View view) {
Yorke Leedfb2eee2013-06-26 18:24:32 -0700697 final InputMethodManager imm = (InputMethodManager) getSystemService(
698 Context.INPUT_METHOD_SERVICE);
Yorke Lee6b049122013-07-16 10:38:02 -0700699 if (imm != null && view != null) {
700 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
701 }
702 }
703
704 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700705 * Shows the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700706 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700707 private void enterSearchUi(boolean smartDialSearch) {
708 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
709 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
710 transaction.hide(mPhoneFavoriteFragment);
711 if (smartDialSearch) {
712 transaction.show(mSmartDialSearchFragment);
713 } else {
714 transaction.show(mRegularSearchFragment);
Yorke Lee6b049122013-07-16 10:38:02 -0700715 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700716 transaction.commit();
Yorke Lee6b049122013-07-16 10:38:02 -0700717
Yorke Leedfb2eee2013-06-26 18:24:32 -0700718 mInSearchUi = true;
Yorke Lee6b049122013-07-16 10:38:02 -0700719 }
720
721 /**
Yorke Leedfb2eee2013-06-26 18:24:32 -0700722 * Hides the search fragment
Yorke Lee6b049122013-07-16 10:38:02 -0700723 */
Yorke Leedfb2eee2013-06-26 18:24:32 -0700724 private void exitSearchUi() {
725 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
726 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
727 transaction.hide(mRegularSearchFragment);
728 transaction.hide(mSmartDialSearchFragment);
729 transaction.show(mPhoneFavoriteFragment);
730 transaction.commit();
731 mInSearchUi = false;
Yorke Lee6b049122013-07-16 10:38:02 -0700732 }
733
734 /** Returns an Intent to launch Call Settings screen */
735 public static Intent getCallSettingsIntent() {
736 final Intent intent = new Intent(Intent.ACTION_MAIN);
737 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
738 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
739 return intent;
740 }
741
742 @Override
Yorke Leedfb2eee2013-06-26 18:24:32 -0700743 public void onBackPressed() {
744 if (mDialpadFragment.isVisible()) {
Yorke Lee80a05ed2013-07-26 17:05:04 -0700745 hideDialpadFragment(true);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700746 } else if (mInSearchUi) {
Yorke Leed9999322013-07-19 09:22:03 -0700747 mSearchView.setText(null);
Yorke Leedfb2eee2013-06-26 18:24:32 -0700748 } else if (isTaskRoot()) {
749 // Instead of stopping, simply push this to the back of the stack.
750 // This is only done when running at the top of the stack;
751 // otherwise, we have been launched by someone else so need to
752 // allow the user to go back to the caller.
753 moveTaskToBack(false);
754 } else {
755 super.onBackPressed();
Yorke Lee6b049122013-07-16 10:38:02 -0700756 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700757 }
758
759 @Override
760 public void onDialpadQueryChanged(String query) {
761 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
762 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Leed9999322013-07-19 09:22:03 -0700763 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
764 mSearchView.setText(normalizedQuery);
Yorke Lee6b049122013-07-16 10:38:02 -0700765 }
766 }
Yorke Leedfb2eee2013-06-26 18:24:32 -0700767
768 @Override
769 public void onListFragmentScrollStateChange(int scrollState) {
770 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
771 hideDialpadFragmentIfNecessary();
772 hideInputMethod(getCurrentFocus());
773 }
774 }
775
776 @Override
777 public void onPhoneFavoriteFragmentStarted() {
Yorke Lee714d6c72013-07-26 16:11:39 -0700778 setupFakeActionBarItemsForFavoritesFragment();
Yorke Leedfb2eee2013-06-26 18:24:32 -0700779 }
Yorke Lee714d6c72013-07-26 16:11:39 -0700780
781 @Override
782 public void onDialpadFragmentStarted() {
783 setupFakeActionBarItemsForDialpadFragment();
784 }
785
Yorke Lee80a05ed2013-07-26 17:05:04 -0700786 private boolean phoneIsInUse() {
787 final TelephonyManager tm = (TelephonyManager) getSystemService(
788 Context.TELEPHONY_SERVICE);
789 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
790 }
Yorke Lee6b049122013-07-16 10:38:02 -0700791}