blob: 52c4a5b5975100c329b9423cc0596b9bc75e9ecc [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
Yorke Leec3766332013-07-31 11:13:16 -07002 * Copyright (C) 2013 The Android Open Source Project
Chiao Cheng94b10b52012-08-17 16:59:12 -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 Leec3766332013-07-31 11:13:16 -070019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.app.Activity;
Christine Chen9c1e0652013-05-23 15:40:19 -070023import android.app.backup.BackupManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070024import android.app.Fragment;
25import android.app.FragmentManager;
26import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080027import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.content.Context;
29import android.content.Intent;
30import android.content.SharedPreferences;
Yorke Leec3766332013-07-31 11:13:16 -070031import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070032import android.net.Uri;
33import android.os.Bundle;
34import android.os.RemoteException;
35import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036import android.provider.CallLog.Calls;
Yorke Leec3766332013-07-31 11:13:16 -070037import android.provider.ContactsContract;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.provider.ContactsContract.Contacts;
39import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070040import android.provider.Settings;
41import android.speech.RecognizerIntent;
42import android.support.v4.app.NavUtils;
43import android.telephony.TelephonyManager;
44import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070046import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.util.Log;
48import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.view.View.OnFocusChangeListener;
52import android.view.ViewConfiguration;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070054import android.widget.AbsListView.OnScrollListener;
55import android.widget.EditText;
56import android.widget.ImageView;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.widget.PopupMenu;
58import android.widget.SearchView;
59import android.widget.SearchView.OnCloseListener;
60import android.widget.SearchView.OnQueryTextListener;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080061import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070062
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070063import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070064import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070065import com.android.contacts.common.dialog.ClearFrequentsDialog;
66import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070067import com.android.contacts.common.list.ContactListItemView;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080068import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
69import com.android.contacts.common.list.PhoneNumberPickerFragment;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.dialer.calllog.CallLogActivity;
Yorke Leef74011e2013-08-02 11:30:30 -070071import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.dialpad.SmartDialNameMatcher;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070073import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8898cd02013-08-08 10:24:27 -070074import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Leec3766332013-07-31 11:13:16 -070075import com.android.dialer.list.OnListFragmentScrolledListener;
76import com.android.dialer.list.ShowAllContactsFragment;
77import com.android.dialer.list.SmartDialSearchFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -070078import com.android.internal.telephony.ITelephony;
79
Yorke Leec3766332013-07-31 11:13:16 -070080import java.util.ArrayList;
81
Chiao Cheng94b10b52012-08-17 16:59:12 -070082/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070083 * The dialer tab's title is 'phone', a more common name (see strings.xml).
84 */
Yorke Leec3766332013-07-31 11:13:16 -070085public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070086 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070087 OnListFragmentScrolledListener,
Yorke Lee8898cd02013-08-08 10:24:27 -070088 PhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener,
Yorke Leef74011e2013-08-02 11:30:30 -070089 DialpadFragment.OnDialpadFragmentStartedListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070090 private static final String TAG = "DialtactsActivity";
91
92 public static final boolean DEBUG = false;
93
Yorke Leef74011e2013-08-02 11:30:30 -070094 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
95
Chiao Cheng94b10b52012-08-17 16:59:12 -070096 /** Used to open Call Setting */
97 private static final String PHONE_PACKAGE = "com.android.phone";
98 private static final String CALL_SETTINGS_CLASS_NAME =
99 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700100 /** @see #getCallOrigin() */
101 private static final String CALL_ORIGIN_DIALTACTS =
102 "com.android.dialer.DialtactsActivity";
103
Yorke Leec3766332013-07-31 11:13:16 -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 private static final String TAG_SHOW_ALL_CONTACTS_FRAGMENT = "show_all_contacts";
109
Chiao Cheng94b10b52012-08-17 16:59:12 -0700110 /**
111 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
112 */
113 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
114
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
116
Yorke Leec3766332013-07-31 11:13:16 -0700117 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118
119 private String mFilterText;
120
Yorke Leec3766332013-07-31 11:13:16 -0700121 /**
122 * The main fragment displaying the user's favorites and frequent contacts
123 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700124 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700125
126 /**
Yorke Leec3766332013-07-31 11:13:16 -0700127 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700128 */
Yorke Leef74011e2013-08-02 11:30:30 -0700129 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700130
131 /**
132 * Fragment for searching phone numbers using the alphanumeric keyboard.
133 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700134 private SearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700135
136 /**
137 * Fragment for searching phone numbers using the dialpad.
138 */
139 private SmartDialSearchFragment mSmartDialSearchFragment;
140
141 private ShowAllContactsFragment mShowAllContactsFragment;
142
143 private View mMenuButton;
144 private View mCallHistoryButton;
145 private View mDialpadButton;
146
147 // Padding view used to shift the fragments up when the dialpad is shown.
148 private View mBottomPaddingView;
149
Chiao Cheng94b10b52012-08-17 16:59:12 -0700150 /**
151 * True when this Activity is in its search UI (with a {@link SearchView} and
152 * {@link PhoneNumberPickerFragment}).
153 */
154 private boolean mInSearchUi;
Yorke Lee98702de2013-08-06 12:03:32 -0700155 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700156 private View mSearchViewContainer;
157 private View mSearchViewCloseButton;
158 private View mVoiceSearchButton;
159 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700160
161 /**
162 * Listener used when one of phone numbers in search UI is selected. This will initiate a
163 * phone call using the phone number.
164 */
165 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
166 new OnPhoneNumberPickerActionListener() {
167 @Override
168 public void onPickPhoneNumberAction(Uri dataUri) {
169 // Specify call-origin so that users will see the previous tab instead of
170 // CallLog screen (search UI will be automatically exited).
171 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700172 DialtactsActivity.this, dataUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700173 }
174
175 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700176 public void onCallNumberDirectly(String phoneNumber) {
177 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
178 startActivity(intent);
179 }
180
181 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700182 public void onShortcutIntentCreated(Intent intent) {
183 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
184 }
185
186 @Override
187 public void onHomeInActionBarSelected() {
188 exitSearchUi();
189 }
190 };
191
192 /**
193 * Listener used to send search queries to the phone search fragment.
194 */
Yorke Leec3766332013-07-31 11:13:16 -0700195 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
196 @Override
197 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
198 }
199
200 @Override
201 public void onTextChanged(CharSequence s, int start, int before, int count) {
202 // TODO krelease: populate the search fragments with the correct
203 // search query at the correct point in time of the fragment lifecycle.
204 // The current behavior is to simply return to the favorites screen
205 // (when docked), or returning to the Dialer after it has been
206 // swapped out of memory.
207 if (mDialpadFragment == null) return;
208 final boolean smartDialSearch = isDialpadShowing();
209 final String newText = s.toString();
210 // Show search result with non-empty text. Show a bare list otherwise.
211 if (TextUtils.isEmpty(newText) && mInSearchUi) {
212 exitSearchUi();
213 mSearchViewCloseButton.setVisibility(View.GONE);
214 return;
215 } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
216 enterSearchUi(smartDialSearch);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700217 }
218
Yorke Leec3766332013-07-31 11:13:16 -0700219 if (smartDialSearch) {
220 mSmartDialSearchFragment.setQueryString(newText, false);
221 } else {
222 mRegularSearchFragment.setQueryString(newText, false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700223 }
Yorke Leec3766332013-07-31 11:13:16 -0700224 mSearchViewCloseButton.setVisibility(View.VISIBLE);
225 return;
226 }
227
228 @Override
229 public void afterTextChanged(Editable s) {
230 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700231 };
232
Yorke Leec3766332013-07-31 11:13:16 -0700233 private boolean isDialpadShowing() {
234 return mDialpadFragment != null && mDialpadFragment.isVisible();
235 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700236
237 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700238 protected void onCreate(Bundle savedInstanceState) {
239 super.onCreate(savedInstanceState);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700240
Yorke Lee98702de2013-08-06 12:03:32 -0700241 mFirstLaunch = true;
242
Chiao Cheng94b10b52012-08-17 16:59:12 -0700243 final Intent intent = getIntent();
244 fixIntent(intent);
245
Yorke Lee8898cd02013-08-08 10:24:27 -0700246 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700247
Yorke Leec3766332013-07-31 11:13:16 -0700248 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700249
Yorke Leec3766332013-07-31 11:13:16 -0700250 if (savedInstanceState == null) {
Yorke Lee8898cd02013-08-08 10:24:27 -0700251 mPhoneFavoriteFragment = new PhoneFavoriteFragment();
Yorke Leec3766332013-07-31 11:13:16 -0700252 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700253
Yorke Lee8898cd02013-08-08 10:24:27 -0700254 mRegularSearchFragment = new SearchFragment();
Yorke Leec3766332013-07-31 11:13:16 -0700255 mSmartDialSearchFragment = new SmartDialSearchFragment();
Yorke Leef74011e2013-08-02 11:30:30 -0700256 mDialpadFragment = new DialpadFragment();
Yorke Leec3766332013-07-31 11:13:16 -0700257 mShowAllContactsFragment = new ShowAllContactsFragment();
258 mShowAllContactsFragment.setOnPhoneNumberPickerActionListener(
259 mPhoneNumberPickerActionListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700260
Yorke Leec3766332013-07-31 11:13:16 -0700261 // TODO krelease: load fragments on demand instead of creating all of them at run time
262 final FragmentTransaction ft = getFragmentManager().beginTransaction();
263 ft.add(R.id.dialtacts_frame, mPhoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
264 ft.add(R.id.dialtacts_frame, mRegularSearchFragment, TAG_REGULAR_SEARCH_FRAGMENT);
265 ft.add(R.id.dialtacts_frame, mSmartDialSearchFragment, TAG_SMARTDIAL_SEARCH_FRAGMENT);
266 ft.add(R.id.dialtacts_frame, mShowAllContactsFragment, TAG_SHOW_ALL_CONTACTS_FRAGMENT);
267 ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
268 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700269 }
270
Yorke Leec3766332013-07-31 11:13:16 -0700271 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
272 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700273
Chiao Cheng94b10b52012-08-17 16:59:12 -0700274 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700275 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700276 setupFilterText(intent);
277 }
278 }
279
280 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700281 protected void onResume() {
282 super.onResume();
283 final FragmentManager fm = getFragmentManager();
Yorke Lee8898cd02013-08-08 10:24:27 -0700284 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fm.findFragmentByTag(
Yorke Leec3766332013-07-31 11:13:16 -0700285 TAG_FAVORITES_FRAGMENT);
Yorke Leef74011e2013-08-02 11:30:30 -0700286 mDialpadFragment = (DialpadFragment) fm.findFragmentByTag(TAG_DIALPAD_FRAGMENT);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700287
Yorke Lee8898cd02013-08-08 10:24:27 -0700288 mRegularSearchFragment = (SearchFragment) fm.findFragmentByTag(
Yorke Leec3766332013-07-31 11:13:16 -0700289 TAG_REGULAR_SEARCH_FRAGMENT);
290 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
291 mPhoneNumberPickerActionListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700292
Yorke Leec3766332013-07-31 11:13:16 -0700293 mSmartDialSearchFragment = (SmartDialSearchFragment) fm.findFragmentByTag(
294 TAG_SMARTDIAL_SEARCH_FRAGMENT);
295 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
296 mPhoneNumberPickerActionListener);
297
298 mShowAllContactsFragment = (ShowAllContactsFragment) fm.findFragmentByTag(
299 TAG_SHOW_ALL_CONTACTS_FRAGMENT);
300 mShowAllContactsFragment.setOnPhoneNumberPickerActionListener(
301 mPhoneNumberPickerActionListener);
Yorke Lee98702de2013-08-06 12:03:32 -0700302
303 if (mFirstLaunch) {
304 displayFragment(getIntent());
305 }
306 mFirstLaunch = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700307 }
308
309 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700310 public void onAttachFragment(Fragment fragment) {
Yorke Lee8898cd02013-08-08 10:24:27 -0700311 if (fragment instanceof DialpadFragment || fragment instanceof SearchFragment
Yorke Leec3766332013-07-31 11:13:16 -0700312 || fragment instanceof SmartDialSearchFragment
313 || fragment instanceof ShowAllContactsFragment) {
314 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
315 transaction.hide(fragment);
316 transaction.commit();
317 }
318 // TODO krelease: Save some kind of state here to show the appropriate fragment
319 // based on the state of the dialer when it was last paused
320 }
321
322 @Override
323 public boolean onOptionsItemSelected(MenuItem item) {
324 switch (item.getItemId()) {
325 // Respond to the action bar's Up/Home button
326 case android.R.id.home:
327 hideAllContactsFragment();
328 }
329 return super.onOptionsItemSelected(item);
330 }
331
332 @Override
333 public boolean onMenuItemClick(MenuItem item) {
334 switch (item.getItemId()) {
335 case R.id.menu_import_export:
336 // We hard-code the "contactsAreAvailable" argument because doing it properly would
337 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
338 // now in Dialtacts for (potential) performance reasons. Compare with how it is
339 // done in {@link PeopleActivity}.
340 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700341 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700342 return true;
343 case R.id.menu_clear_frequents:
344 ClearFrequentsDialog.show(getFragmentManager());
345 return true;
346 case R.id.add_contact:
347 try {
348 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
349 } catch (ActivityNotFoundException e) {
350 Toast toast = Toast.makeText(this,
351 R.string.add_contact_not_available,
352 Toast.LENGTH_SHORT);
353 toast.show();
354 }
355 return true;
356 case R.id.menu_call_settings:
Yorke Leef74011e2013-08-02 11:30:30 -0700357 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
Yorke Leec3766332013-07-31 11:13:16 -0700358 startActivity(settingsIntent);
359 }
360 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700361 }
362
363 @Override
364 public void onClick(View view) {
365 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700366 case R.id.overflow_menu: {
367 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
368 final Menu menu = popupMenu.getMenu();
Yorke Lee8898cd02013-08-08 10:24:27 -0700369 popupMenu.inflate(R.menu.dialtacts_options);
Yorke Leef420a532013-08-09 15:16:10 -0700370 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
371 clearFrequents.setEnabled(mPhoneFavoriteFragment.hasFrequents());
Yorke Leec3766332013-07-31 11:13:16 -0700372 popupMenu.setOnMenuItemClickListener(this);
373 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700374 break;
375 }
Yorke Leec3766332013-07-31 11:13:16 -0700376 case R.id.dialpad_button:
377 showDialpadFragment(true);
378 break;
379 case R.id.call_history_on_dialpad_button:
380 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700381 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
382 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700383 final Intent intent = new Intent(this, CallLogActivity.class);
384 startActivity(intent);
385 break;
386 case R.id.search_close_button:
387 // Clear the search field
388 if (!TextUtils.isEmpty(mSearchView.getText())) {
389 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700390 }
391 break;
Yorke Leec3766332013-07-31 11:13:16 -0700392 case R.id.voice_search_button:
393 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
394 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
395 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700396 default: {
397 Log.wtf(TAG, "Unexpected onClick event from " + view);
398 break;
399 }
400 }
401 }
402
Yorke Leec3766332013-07-31 11:13:16 -0700403 @Override
404 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
405 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
406 if (resultCode == RESULT_OK) {
407 final ArrayList<String> matches = data.getStringArrayListExtra(
408 RecognizerIntent.EXTRA_RESULTS);
409 if (matches.size() > 0) {
410 final String match = matches.get(0);
411 mSearchView.setText(match);
412 } else {
413 Log.e(TAG, "Voice search - nothing heard");
414 }
415 } else {
416 Log.e(TAG, "Voice search failed");
417 }
418 }
419 super.onActivityResult(requestCode, resultCode, data);
420 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700421
Yorke Leec3766332013-07-31 11:13:16 -0700422 private void showDialpadFragment(boolean animate) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700423 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700424 if (animate) {
425 ft.setCustomAnimations(R.anim.slide_in, 0);
426 }
427 ft.show(mDialpadFragment);
428 ft.commit();
429 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700430
Yorke Leec3766332013-07-31 11:13:16 -0700431 private void hideDialpadFragment(boolean animate) {
432 final FragmentTransaction ft = getFragmentManager().beginTransaction();
433 if (animate) {
434 ft.setCustomAnimations(0, R.anim.slide_out);
435 }
436 ft.hide(mDialpadFragment);
437 ft.commit();
438 }
439
440 public void showAllContactsFragment() {
441 final FragmentTransaction ft = getFragmentManager().beginTransaction();
442 ft.hide(mPhoneFavoriteFragment);
443 ft.show(mShowAllContactsFragment);
444 // TODO{klp} Add animation
445 ft.commit();
446 hideSearchBar(false);
447 }
448
449 private void hideAllContactsFragment() {
450 final FragmentTransaction ft = getFragmentManager().beginTransaction();
451 ft.hide(mShowAllContactsFragment);
452 ft.show(mPhoneFavoriteFragment);
453 ft.commit();
454 showSearchBar();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700455 }
456
457 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700458 mSearchViewContainer = findViewById(R.id.search_view_container);
459 mSearchViewCloseButton = findViewById(R.id.search_close_button);
460 mSearchViewCloseButton.setOnClickListener(this);
461 mVoiceSearchButton = findViewById(R.id.voice_search_button);
462 mVoiceSearchButton.setOnClickListener(this);
463 mSearchView = (EditText) findViewById(R.id.search_view);
464 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
465 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
466 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700467 @Override
468 public void onFocusChange(View view, boolean hasFocus) {
469 if (hasFocus) {
470 showInputMethod(view.findFocus());
471 }
472 }
473 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700474 }
475
Yorke Leec3766332013-07-31 11:13:16 -0700476 private void hideDialpadFragmentIfNecessary() {
477 if (mDialpadFragment.isVisible()) {
478 hideDialpadFragment(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700479 }
480 }
481
Yorke Leec3766332013-07-31 11:13:16 -0700482 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
483 @Override
484 public void onAnimationEnd(Animator animation) {
485 mSearchViewContainer.setVisibility(View.GONE);
486 }
487 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700488
Yorke Leec3766332013-07-31 11:13:16 -0700489 public void hideSearchBar() {
490 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700491 }
492
Yorke Leec3766332013-07-31 11:13:16 -0700493 public void hideSearchBar(boolean shiftView) {
494 if (shiftView) {
495 mSearchViewContainer.animate().cancel();
496 mSearchViewContainer.setAlpha(1);
497 mSearchViewContainer.setTranslationY(0);
498 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
499 .setDuration(200).setListener(mHideListener);
500
501 mPhoneFavoriteFragment.getView().animate().withLayer()
502 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
503 new AnimatorListenerAdapter() {
504 @Override
505 public void onAnimationEnd(Animator animation) {
506 mBottomPaddingView.setVisibility(View.VISIBLE);
507 mPhoneFavoriteFragment.getView().setTranslationY(0);
508 }
509 });
510 } else {
511 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
512 }
513 }
514
515 public void showSearchBar() {
516 // If the favorites fragment hasn't been fully created before the dialpad fragment
517 // is hidden (i.e. onResume), don't bother animating
518 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
519 return;
520 }
521 mSearchViewContainer.animate().cancel();
522 mSearchViewContainer.setAlpha(0);
523 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
524 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
525 .setListener(new AnimatorListenerAdapter() {
526 @Override
527 public void onAnimationStart(Animator animation) {
528 mSearchViewContainer.setVisibility(View.VISIBLE);
529 }
530 });
531
532 mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
533 mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
534 .setListener(
535 new AnimatorListenerAdapter() {
536 @Override
537 public void onAnimationStart(Animator animation) {
538 mBottomPaddingView.setVisibility(View.GONE);
539 }
540 });
541 }
542
543
544 public void setupFakeActionBarItemsForFavoritesFragment() {
545 mMenuButton = findViewById(R.id.overflow_menu);
546 if (mMenuButton != null) {
547 mMenuButton.setOnClickListener(this);
548 }
549
550 mCallHistoryButton = findViewById(R.id.call_history_button);
551 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
552 mCallHistoryButton.setOnClickListener(this);
553
554 mDialpadButton = findViewById(R.id.dialpad_button);
555 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
556 mDialpadButton.setOnClickListener(this);
557 }
558
559 public void setupFakeActionBarItemsForDialpadFragment() {
560 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
561 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700562 }
563
564 private void fixIntent(Intent intent) {
565 // This should be cleaned up: the call key used to send an Intent
566 // that just said to go to the recent calls list. It now sends this
567 // abstract action, but this class hasn't been rewritten to deal with it.
568 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
569 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
570 intent.putExtra("call_key", true);
571 setIntent(intent);
572 }
573 }
574
Chiao Cheng94b10b52012-08-17 16:59:12 -0700575 /**
576 * Returns true if the intent is due to hitting the green send key (hardware call button:
577 * KEYCODE_CALL) while in a call.
578 *
579 * @param intent the intent that launched this activity
580 * @param recentCallsRequest true if the intent is requesting to view recent calls
581 * @return true if the intent is due to hitting the green send key while in a call
582 */
Yorke Leec3766332013-07-31 11:13:16 -0700583 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700584 // If there is a call in progress go to the call screen
585 if (recentCallsRequest) {
586 final boolean callKey = intent.getBooleanExtra("call_key", false);
587
588 try {
589 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
590 if (callKey && phone != null && phone.showCallScreen()) {
591 return true;
592 }
593 } catch (RemoteException e) {
594 Log.e(TAG, "Failed to handle send while in call", e);
595 }
596 }
597
598 return false;
599 }
600
601 /**
602 * Sets the current tab based on the intent's request type
603 *
604 * @param intent Intent that contains information about which tab should be selected
605 */
Yorke Leec3766332013-07-31 11:13:16 -0700606 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700607 // If we got here by hitting send and we're in call forward along to the in-call activity
608 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
609 getContentResolver()));
610 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
611 finish();
612 return;
613 }
614
Yorke Lee98702de2013-08-06 12:03:32 -0700615 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700616 mDialpadFragment.setStartedFromNewIntent(true);
617 // TODO krelease: This should use showDialpadFragment(false) to avoid animating
618 // the dialpad in. Need to fix the onPreDrawListener in NewDialpadFragment first.
619 showDialpadFragment(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700620 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700621 }
622
623 @Override
624 public void onNewIntent(Intent newIntent) {
625 setIntent(newIntent);
626 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700627 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700628 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700629
630 if (mInSearchUi || (mRegularSearchFragment != null && mRegularSearchFragment.isVisible())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700631 exitSearchUi();
632 }
633
Yorke Leec3766332013-07-31 11:13:16 -0700634 // TODO krelease: Handle onNewIntent for all other fragments
635 /*
636 *if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { if (mDialpadFragment != null) {
637 * mDialpadFragment.setStartedFromNewIntent(true); } else { Log.e(TAG,
638 * "DialpadFragment isn't ready yet when the tab is already selected."); } } else if
639 * (mViewPager.getCurrentItem() == TAB_INDEX_CALL_LOG) { if (mCallLogFragment != null) {
640 * mCallLogFragment.configureScreenFromIntent(newIntent); } else { Log.e(TAG,
641 * "CallLogFragment isn't ready yet when the tab is already selected."); } }
642 */
Chiao Cheng94b10b52012-08-17 16:59:12 -0700643 invalidateOptionsMenu();
644 }
645
646 /** Returns true if the given intent contains a phone number to populate the dialer with */
647 private boolean isDialIntent(Intent intent) {
648 final String action = intent.getAction();
649 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
650 return true;
651 }
652 if (Intent.ACTION_VIEW.equals(action)) {
653 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700654 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700655 return true;
656 }
657 }
658 return false;
659 }
660
661 /**
662 * Returns an appropriate call origin for this Activity. May return null when no call origin
663 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
664 * for remembering the tab in which the user made a phone call, so the external app's DIAL
665 * request should not be counted.)
666 */
667 public String getCallOrigin() {
668 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
669 }
670
671 /**
672 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
673 * This text originally came from a FILTER_CONTACTS_ACTION intent received
674 * by this activity. The stored text will then be cleared after after this
675 * method returns.
676 *
677 * @return The stored filter text
678 */
679 public String getAndClearFilterText() {
680 String filterText = mFilterText;
681 mFilterText = null;
682 return filterText;
683 }
684
685 /**
686 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
687 * This is so child activities can check if they are supposed to display a filter.
688 *
689 * @param intent The intent received in {@link #onNewIntent(Intent)}
690 */
691 private void setupFilterText(Intent intent) {
692 // If the intent was relaunched from history, don't apply the filter text.
693 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
694 return;
695 }
696 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
697 if (filter != null && filter.length() > 0) {
698 mFilterText = filter;
699 }
700 }
701
Yorke Lee8898cd02013-08-08 10:24:27 -0700702 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
703 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700704 @Override
705 public void onContactSelected(Uri contactUri) {
706 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700707 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700708 }
709
710 @Override
711 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700712 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700713 startActivity(intent);
714 }
715 };
716
Yorke Leec3766332013-07-31 11:13:16 -0700717 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
718 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
719 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700720
721 @Override
722 public void startSearch(String initialQuery, boolean selectInitialQuery,
723 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700724 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700725 if (mInSearchUi) {
726 if (mSearchView.hasFocus()) {
727 showInputMethod(mSearchView.findFocus());
728 } else {
729 mSearchView.requestFocus();
730 }
731 } else {
732 enterSearchUi();
733 }
734 } else {
735 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
736 }
Yorke Leec3766332013-07-31 11:13:16 -0700737 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700738
739 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700740 final InputMethodManager imm = (InputMethodManager) getSystemService(
741 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700742 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700743 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 }
745 }
746
747 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700748 final InputMethodManager imm = (InputMethodManager) getSystemService(
749 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700750 if (imm != null && view != null) {
751 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
752 }
753 }
754
755 /**
Yorke Leec3766332013-07-31 11:13:16 -0700756 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700757 */
Yorke Leec3766332013-07-31 11:13:16 -0700758 private void enterSearchUi(boolean smartDialSearch) {
759 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
760 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
761 transaction.hide(mPhoneFavoriteFragment);
762 if (smartDialSearch) {
763 transaction.show(mSmartDialSearchFragment);
764 } else {
765 transaction.show(mRegularSearchFragment);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700766 }
Yorke Leec3766332013-07-31 11:13:16 -0700767 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700768
Yorke Leec3766332013-07-31 11:13:16 -0700769 mInSearchUi = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700770 }
771
772 /**
Yorke Leec3766332013-07-31 11:13:16 -0700773 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700774 */
Yorke Leec3766332013-07-31 11:13:16 -0700775 private void exitSearchUi() {
776 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
777 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
778 transaction.hide(mRegularSearchFragment);
779 transaction.hide(mSmartDialSearchFragment);
780 transaction.show(mPhoneFavoriteFragment);
781 transaction.commit();
782 mInSearchUi = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700783 }
784
785 /** Returns an Intent to launch Call Settings screen */
786 public static Intent getCallSettingsIntent() {
787 final Intent intent = new Intent(Intent.ACTION_MAIN);
788 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
789 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
790 return intent;
791 }
792
793 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700794 public void onBackPressed() {
795 if (mDialpadFragment.isVisible()) {
796 hideDialpadFragment(true);
797 } else if (mInSearchUi) {
798 mSearchView.setText(null);
799 } else if (mShowAllContactsFragment.isVisible()) {
800 hideAllContactsFragment();
801 } else if (isTaskRoot()) {
802 // Instead of stopping, simply push this to the back of the stack.
803 // This is only done when running at the top of the stack;
804 // otherwise, we have been launched by someone else so need to
805 // allow the user to go back to the caller.
806 moveTaskToBack(false);
807 } else {
808 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700809 }
Yorke Leec3766332013-07-31 11:13:16 -0700810 }
811
812 @Override
813 public void onDialpadQueryChanged(String query) {
814 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
815 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
816 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
817 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700818 }
819 }
Yorke Leec3766332013-07-31 11:13:16 -0700820
821 @Override
822 public void onListFragmentScrollStateChange(int scrollState) {
823 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
824 hideDialpadFragmentIfNecessary();
825 hideInputMethod(getCurrentFocus());
826 }
827 }
828
829 @Override
830 public void onPhoneFavoriteFragmentStarted() {
831 setupFakeActionBarItemsForFavoritesFragment();
832 }
833
834 @Override
835 public void onDialpadFragmentStarted() {
836 setupFakeActionBarItemsForDialpadFragment();
837 }
838
839 private boolean phoneIsInUse() {
840 final TelephonyManager tm = (TelephonyManager) getSystemService(
841 Context.TELEPHONY_SERVICE);
842 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
843 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700844}