blob: 316465e7492a24d3f52891a47b3e8b6b4413a4fb [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 Lee8dd62002013-08-08 15:57:20 -070074import com.android.dialer.list.AllContactsActivity;
Yorke Lee8898cd02013-08-08 10:24:27 -070075import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Leec3766332013-07-31 11:13:16 -070076import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leec3766332013-07-31 11:13:16 -070077import 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 Lee8dd62002013-08-08 15:57:20 -070089 DialpadFragment.OnDialpadFragmentStartedListener,
90 PhoneFavoriteFragment.OnShowAllContactsListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070091 private static final String TAG = "DialtactsActivity";
92
93 public static final boolean DEBUG = false;
94
Yorke Leef74011e2013-08-02 11:30:30 -070095 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
96
Chiao Cheng94b10b52012-08-17 16:59:12 -070097 /** Used to open Call Setting */
98 private static final String PHONE_PACKAGE = "com.android.phone";
99 private static final String CALL_SETTINGS_CLASS_NAME =
100 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101 /** @see #getCallOrigin() */
102 private static final String CALL_ORIGIN_DIALTACTS =
103 "com.android.dialer.DialtactsActivity";
104
Yorke Leeef2b7382013-08-09 17:39:25 -0700105 private static final String KEY_IN_SEARCH_UI = "in_search_ui";
106 private static final String KEY_SEARCH_QUERY = "search_query";
107 private static final String KEY_FIRST_LAUNCH = "first_launch";
108
Yorke Leec3766332013-07-31 11:13:16 -0700109 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
110 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
111 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
112 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700113
Chiao Cheng94b10b52012-08-17 16:59:12 -0700114 /**
115 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
116 */
117 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
118
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
120
Yorke Leec3766332013-07-31 11:13:16 -0700121 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700122
123 private String mFilterText;
124
Yorke Leec3766332013-07-31 11:13:16 -0700125 /**
126 * The main fragment displaying the user's favorites and frequent contacts
127 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700128 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700129
130 /**
Yorke Leec3766332013-07-31 11:13:16 -0700131 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700132 */
Yorke Leef74011e2013-08-02 11:30:30 -0700133 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700134
135 /**
136 * Fragment for searching phone numbers using the alphanumeric keyboard.
137 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700138 private SearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700139
140 /**
141 * Fragment for searching phone numbers using the dialpad.
142 */
143 private SmartDialSearchFragment mSmartDialSearchFragment;
144
Yorke Leec3766332013-07-31 11:13:16 -0700145 private View mMenuButton;
146 private View mCallHistoryButton;
147 private View mDialpadButton;
148
149 // Padding view used to shift the fragments up when the dialpad is shown.
150 private View mBottomPaddingView;
151
Chiao Cheng94b10b52012-08-17 16:59:12 -0700152 /**
153 * True when this Activity is in its search UI (with a {@link SearchView} and
154 * {@link PhoneNumberPickerFragment}).
155 */
156 private boolean mInSearchUi;
Yorke Leeef2b7382013-08-09 17:39:25 -0700157 /**
158 * True when this activity has been launched for the first time.
159 */
Yorke Lee98702de2013-08-06 12:03:32 -0700160 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700161 private View mSearchViewContainer;
162 private View mSearchViewCloseButton;
163 private View mVoiceSearchButton;
164 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700165
Yorke Leeef2b7382013-08-09 17:39:25 -0700166 private String mSearchQuery;
167
Chiao Cheng94b10b52012-08-17 16:59:12 -0700168 /**
169 * Listener used when one of phone numbers in search UI is selected. This will initiate a
170 * phone call using the phone number.
171 */
172 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
173 new OnPhoneNumberPickerActionListener() {
174 @Override
175 public void onPickPhoneNumberAction(Uri dataUri) {
176 // Specify call-origin so that users will see the previous tab instead of
177 // CallLog screen (search UI will be automatically exited).
178 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700179 DialtactsActivity.this, dataUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700180 }
181
182 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700183 public void onCallNumberDirectly(String phoneNumber) {
184 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
185 startActivity(intent);
186 }
187
188 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700189 public void onShortcutIntentCreated(Intent intent) {
190 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
191 }
192
193 @Override
194 public void onHomeInActionBarSelected() {
195 exitSearchUi();
196 }
197 };
198
199 /**
200 * Listener used to send search queries to the phone search fragment.
201 */
Yorke Leec3766332013-07-31 11:13:16 -0700202 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
203 @Override
204 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
205 }
206
207 @Override
208 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700209 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700210 if (newText.equals(mSearchQuery)) {
211 // If the query hasn't changed (perhaps due to activity being destroyed
212 // and restored, or user launching the same DIAL intent twice), then there is
213 // no need to do anything here.
214 return;
215 }
216 mSearchQuery = newText;
217 if (DEBUG) {
218 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
219 }
220 final boolean smartDialSearch = isDialpadShowing();
221
Yorke Leec3766332013-07-31 11:13:16 -0700222 // Show search result with non-empty text. Show a bare list otherwise.
223 if (TextUtils.isEmpty(newText) && mInSearchUi) {
224 exitSearchUi();
225 mSearchViewCloseButton.setVisibility(View.GONE);
226 return;
227 } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700228 enterSearchUi(smartDialSearch, newText);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700229 }
230
Yorke Leeef2b7382013-08-09 17:39:25 -0700231 if (smartDialSearch && mSmartDialSearchFragment != null) {
232 mSmartDialSearchFragment.setQueryString(newText, false);
233 } else if (mRegularSearchFragment != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700234 mRegularSearchFragment.setQueryString(newText, false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700235 }
Yorke Leec3766332013-07-31 11:13:16 -0700236 mSearchViewCloseButton.setVisibility(View.VISIBLE);
237 return;
238 }
239
240 @Override
241 public void afterTextChanged(Editable s) {
242 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700243 };
244
Yorke Leec3766332013-07-31 11:13:16 -0700245 private boolean isDialpadShowing() {
246 return mDialpadFragment != null && mDialpadFragment.isVisible();
247 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700248
249 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700250 protected void onCreate(Bundle savedInstanceState) {
251 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700252 mFirstLaunch = true;
253
Chiao Cheng94b10b52012-08-17 16:59:12 -0700254 final Intent intent = getIntent();
255 fixIntent(intent);
256
Yorke Lee8898cd02013-08-08 10:24:27 -0700257 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700258
Yorke Leec3766332013-07-31 11:13:16 -0700259 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700260
Yorke Leeef2b7382013-08-09 17:39:25 -0700261 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
262 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700263 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700264 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
265 phoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700266
Yorke Leec3766332013-07-31 11:13:16 -0700267 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700268 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
269 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700270 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700271 } else {
272 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
273 mInSearchUi = savedInstanceState.getBoolean(KEY_IN_SEARCH_UI);
274 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275 }
276
Yorke Leec3766332013-07-31 11:13:16 -0700277 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
278 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700279
Chiao Cheng94b10b52012-08-17 16:59:12 -0700280 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700281 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700282 setupFilterText(intent);
283 }
284 }
285
286 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700287 protected void onResume() {
288 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700289 if (mFirstLaunch) {
290 displayFragment(getIntent());
291 }
292 mFirstLaunch = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700293 }
294
295 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700296 protected void onSaveInstanceState(Bundle outState) {
297 super.onSaveInstanceState(outState);
298 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
299 outState.putBoolean(KEY_IN_SEARCH_UI, mInSearchUi);
300 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
301 }
302
303 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700304 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700305 if (fragment instanceof DialpadFragment) {
306 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700307 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700308 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700309 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700310 } else if (fragment instanceof SmartDialSearchFragment) {
311 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
312 } else if (fragment instanceof SearchFragment) {
313 mRegularSearchFragment = (SearchFragment) fragment;
314 } else if (fragment instanceof PhoneFavoriteFragment) {
315 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700316 }
Yorke Leec3766332013-07-31 11:13:16 -0700317 }
318
319 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700320 public boolean onMenuItemClick(MenuItem item) {
321 switch (item.getItemId()) {
322 case R.id.menu_import_export:
323 // We hard-code the "contactsAreAvailable" argument because doing it properly would
324 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
325 // now in Dialtacts for (potential) performance reasons. Compare with how it is
326 // done in {@link PeopleActivity}.
327 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700328 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700329 return true;
330 case R.id.menu_clear_frequents:
331 ClearFrequentsDialog.show(getFragmentManager());
332 return true;
333 case R.id.add_contact:
334 try {
335 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
336 } catch (ActivityNotFoundException e) {
337 Toast toast = Toast.makeText(this,
338 R.string.add_contact_not_available,
339 Toast.LENGTH_SHORT);
340 toast.show();
341 }
342 return true;
343 case R.id.menu_call_settings:
Yorke Leef74011e2013-08-02 11:30:30 -0700344 final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
Yorke Leec3766332013-07-31 11:13:16 -0700345 startActivity(settingsIntent);
346 }
347 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700348 }
349
350 @Override
351 public void onClick(View view) {
352 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700353 case R.id.overflow_menu: {
354 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
355 final Menu menu = popupMenu.getMenu();
Yorke Lee8898cd02013-08-08 10:24:27 -0700356 popupMenu.inflate(R.menu.dialtacts_options);
Yorke Leef420a532013-08-09 15:16:10 -0700357 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Lee0d391b42013-08-09 17:27:00 -0700358 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
Yorke Leec3766332013-07-31 11:13:16 -0700359 popupMenu.setOnMenuItemClickListener(this);
360 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700361 break;
362 }
Yorke Leec3766332013-07-31 11:13:16 -0700363 case R.id.dialpad_button:
364 showDialpadFragment(true);
365 break;
366 case R.id.call_history_on_dialpad_button:
367 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700368 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
369 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700370 final Intent intent = new Intent(this, CallLogActivity.class);
371 startActivity(intent);
372 break;
373 case R.id.search_close_button:
374 // Clear the search field
375 if (!TextUtils.isEmpty(mSearchView.getText())) {
376 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700377 }
378 break;
Yorke Leec3766332013-07-31 11:13:16 -0700379 case R.id.voice_search_button:
380 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
381 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
382 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700383 default: {
384 Log.wtf(TAG, "Unexpected onClick event from " + view);
385 break;
386 }
387 }
388 }
389
Yorke Leec3766332013-07-31 11:13:16 -0700390 @Override
391 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
392 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
393 if (resultCode == RESULT_OK) {
394 final ArrayList<String> matches = data.getStringArrayListExtra(
395 RecognizerIntent.EXTRA_RESULTS);
396 if (matches.size() > 0) {
397 final String match = matches.get(0);
398 mSearchView.setText(match);
399 } else {
400 Log.e(TAG, "Voice search - nothing heard");
401 }
402 } else {
403 Log.e(TAG, "Voice search failed");
404 }
405 }
406 super.onActivityResult(requestCode, resultCode, data);
407 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700408
Yorke Leec3766332013-07-31 11:13:16 -0700409 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700410 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700411 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700412 if (animate) {
413 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700414 } else {
415 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700416 }
417 ft.show(mDialpadFragment);
418 ft.commit();
419 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700420
Yorke Leec3766332013-07-31 11:13:16 -0700421 private void hideDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700422 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700423 final FragmentTransaction ft = getFragmentManager().beginTransaction();
424 if (animate) {
425 ft.setCustomAnimations(0, R.anim.slide_out);
426 }
427 ft.hide(mDialpadFragment);
428 ft.commit();
429 }
430
Chiao Cheng94b10b52012-08-17 16:59:12 -0700431 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700432 mSearchViewContainer = findViewById(R.id.search_view_container);
433 mSearchViewCloseButton = findViewById(R.id.search_close_button);
434 mSearchViewCloseButton.setOnClickListener(this);
435 mVoiceSearchButton = findViewById(R.id.voice_search_button);
436 mVoiceSearchButton.setOnClickListener(this);
437 mSearchView = (EditText) findViewById(R.id.search_view);
438 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
439 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
440 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700441 @Override
442 public void onFocusChange(View view, boolean hasFocus) {
443 if (hasFocus) {
444 showInputMethod(view.findFocus());
445 }
446 }
447 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700448 }
449
Yorke Leec3766332013-07-31 11:13:16 -0700450 private void hideDialpadFragmentIfNecessary() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700451 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leec3766332013-07-31 11:13:16 -0700452 hideDialpadFragment(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700453 }
454 }
455
Yorke Leec3766332013-07-31 11:13:16 -0700456 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
457 @Override
458 public void onAnimationEnd(Animator animation) {
459 mSearchViewContainer.setVisibility(View.GONE);
460 }
461 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700462
Yorke Leec3766332013-07-31 11:13:16 -0700463 public void hideSearchBar() {
464 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700465 }
466
Yorke Leec3766332013-07-31 11:13:16 -0700467 public void hideSearchBar(boolean shiftView) {
468 if (shiftView) {
469 mSearchViewContainer.animate().cancel();
470 mSearchViewContainer.setAlpha(1);
471 mSearchViewContainer.setTranslationY(0);
472 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
473 .setDuration(200).setListener(mHideListener);
474
Yorke Leeef2b7382013-08-09 17:39:25 -0700475 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
476 mBottomPaddingView.setVisibility(View.VISIBLE);
477 return;
478 }
479
Yorke Leec3766332013-07-31 11:13:16 -0700480 mPhoneFavoriteFragment.getView().animate().withLayer()
481 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
482 new AnimatorListenerAdapter() {
483 @Override
484 public void onAnimationEnd(Animator animation) {
485 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeef2b7382013-08-09 17:39:25 -0700486 if (mPhoneFavoriteFragment.getView() != null) {
487 mPhoneFavoriteFragment.getView().setTranslationY(0);
488 }
Yorke Leec3766332013-07-31 11:13:16 -0700489 }
490 });
491 } else {
492 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
493 }
494 }
495
496 public void showSearchBar() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700497
498
Yorke Leec3766332013-07-31 11:13:16 -0700499 mSearchViewContainer.animate().cancel();
500 mSearchViewContainer.setAlpha(0);
501 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
502 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
503 .setListener(new AnimatorListenerAdapter() {
504 @Override
505 public void onAnimationStart(Animator animation) {
506 mSearchViewContainer.setVisibility(View.VISIBLE);
507 }
508 });
509
Yorke Leeef2b7382013-08-09 17:39:25 -0700510 // If the favorites fragment hasn't been fully created before the dialpad fragment
511 // is hidden (i.e. onResume), don't bother animating
512 if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
513 mBottomPaddingView.setVisibility(View.GONE);
514 return;
515 }
Yorke Leec3766332013-07-31 11:13:16 -0700516 mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
517 mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
518 .setListener(
519 new AnimatorListenerAdapter() {
520 @Override
521 public void onAnimationStart(Animator animation) {
522 mBottomPaddingView.setVisibility(View.GONE);
523 }
524 });
525 }
526
527
528 public void setupFakeActionBarItemsForFavoritesFragment() {
529 mMenuButton = findViewById(R.id.overflow_menu);
530 if (mMenuButton != null) {
531 mMenuButton.setOnClickListener(this);
532 }
533
534 mCallHistoryButton = findViewById(R.id.call_history_button);
535 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
536 mCallHistoryButton.setOnClickListener(this);
537
538 mDialpadButton = findViewById(R.id.dialpad_button);
539 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
540 mDialpadButton.setOnClickListener(this);
541 }
542
543 public void setupFakeActionBarItemsForDialpadFragment() {
544 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
545 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700546 }
547
548 private void fixIntent(Intent intent) {
549 // This should be cleaned up: the call key used to send an Intent
550 // that just said to go to the recent calls list. It now sends this
551 // abstract action, but this class hasn't been rewritten to deal with it.
552 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
553 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
554 intent.putExtra("call_key", true);
555 setIntent(intent);
556 }
557 }
558
Chiao Cheng94b10b52012-08-17 16:59:12 -0700559 /**
560 * Returns true if the intent is due to hitting the green send key (hardware call button:
561 * KEYCODE_CALL) while in a call.
562 *
563 * @param intent the intent that launched this activity
564 * @param recentCallsRequest true if the intent is requesting to view recent calls
565 * @return true if the intent is due to hitting the green send key while in a call
566 */
Yorke Leec3766332013-07-31 11:13:16 -0700567 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700568 // If there is a call in progress go to the call screen
569 if (recentCallsRequest) {
570 final boolean callKey = intent.getBooleanExtra("call_key", false);
571
572 try {
573 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
574 if (callKey && phone != null && phone.showCallScreen()) {
575 return true;
576 }
577 } catch (RemoteException e) {
578 Log.e(TAG, "Failed to handle send while in call", e);
579 }
580 }
581
582 return false;
583 }
584
585 /**
586 * Sets the current tab based on the intent's request type
587 *
588 * @param intent Intent that contains information about which tab should be selected
589 */
Yorke Leec3766332013-07-31 11:13:16 -0700590 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700591 // If we got here by hitting send and we're in call forward along to the in-call activity
592 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
593 getContentResolver()));
594 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
595 finish();
596 return;
597 }
598
Yorke Lee98702de2013-08-06 12:03:32 -0700599 if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
Yorke Leec3766332013-07-31 11:13:16 -0700600 mDialpadFragment.setStartedFromNewIntent(true);
Yorke Leeea695cb2013-08-09 11:02:26 -0700601 showDialpadFragment(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700602 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700603 }
604
605 @Override
606 public void onNewIntent(Intent newIntent) {
607 setIntent(newIntent);
608 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700609 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700610 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700611
Chiao Cheng94b10b52012-08-17 16:59:12 -0700612 invalidateOptionsMenu();
613 }
614
615 /** Returns true if the given intent contains a phone number to populate the dialer with */
616 private boolean isDialIntent(Intent intent) {
617 final String action = intent.getAction();
618 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
619 return true;
620 }
621 if (Intent.ACTION_VIEW.equals(action)) {
622 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700623 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700624 return true;
625 }
626 }
627 return false;
628 }
629
630 /**
631 * Returns an appropriate call origin for this Activity. May return null when no call origin
632 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
633 * for remembering the tab in which the user made a phone call, so the external app's DIAL
634 * request should not be counted.)
635 */
636 public String getCallOrigin() {
637 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
638 }
639
640 /**
641 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
642 * This text originally came from a FILTER_CONTACTS_ACTION intent received
643 * by this activity. The stored text will then be cleared after after this
644 * method returns.
645 *
646 * @return The stored filter text
647 */
648 public String getAndClearFilterText() {
649 String filterText = mFilterText;
650 mFilterText = null;
651 return filterText;
652 }
653
654 /**
655 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
656 * This is so child activities can check if they are supposed to display a filter.
657 *
658 * @param intent The intent received in {@link #onNewIntent(Intent)}
659 */
660 private void setupFilterText(Intent intent) {
661 // If the intent was relaunched from history, don't apply the filter text.
662 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
663 return;
664 }
665 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
666 if (filter != null && filter.length() > 0) {
667 mFilterText = filter;
668 }
669 }
670
Yorke Lee8898cd02013-08-08 10:24:27 -0700671 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
672 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700673 @Override
674 public void onContactSelected(Uri contactUri) {
675 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700676 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700677 }
678
679 @Override
680 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700681 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700682 startActivity(intent);
683 }
684 };
685
Yorke Leec3766332013-07-31 11:13:16 -0700686 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
687 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
688 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700689
690 @Override
691 public void startSearch(String initialQuery, boolean selectInitialQuery,
692 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700693 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700694 if (mInSearchUi) {
695 if (mSearchView.hasFocus()) {
696 showInputMethod(mSearchView.findFocus());
697 } else {
698 mSearchView.requestFocus();
699 }
700 } else {
701 enterSearchUi();
702 }
703 } else {
704 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
705 }
Yorke Leec3766332013-07-31 11:13:16 -0700706 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700707
708 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700709 final InputMethodManager imm = (InputMethodManager) getSystemService(
710 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700711 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700712 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700713 }
714 }
715
716 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700717 final InputMethodManager imm = (InputMethodManager) getSystemService(
718 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719 if (imm != null && view != null) {
720 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
721 }
722 }
723
724 /**
Yorke Leec3766332013-07-31 11:13:16 -0700725 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700726 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700727 private void enterSearchUi(boolean smartDialSearch, String query) {
728 if (DEBUG) {
729 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
730 }
731 final String tag = smartDialSearch ? TAG_SMARTDIAL_SEARCH_FRAGMENT :
732 TAG_REGULAR_SEARCH_FRAGMENT;
733
Yorke Leec3766332013-07-31 11:13:16 -0700734 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
735 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700736
Yorke Leeef2b7382013-08-09 17:39:25 -0700737 SearchFragment fragment;
738
739 transaction.remove(mPhoneFavoriteFragment);
740 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
741 if (fragment == null) {
742 if (smartDialSearch) {
743 fragment = new SmartDialSearchFragment();
744 } else {
745 fragment = new SearchFragment();
746 }
747 transaction.replace(R.id.dialtacts_frame, fragment, tag);
748 } else {
749 transaction.attach(fragment);
750 }
751
752 transaction.addToBackStack(null);
753 fragment.setQueryString(query, false);
754 transaction.commit();
Yorke Leec3766332013-07-31 11:13:16 -0700755 mInSearchUi = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700756 }
757
758 /**
Yorke Leec3766332013-07-31 11:13:16 -0700759 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700760 */
Yorke Leec3766332013-07-31 11:13:16 -0700761 private void exitSearchUi() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700762 getFragmentManager().popBackStack();
Yorke Leec3766332013-07-31 11:13:16 -0700763 mInSearchUi = false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 }
765
766 /** Returns an Intent to launch Call Settings screen */
767 public static Intent getCallSettingsIntent() {
768 final Intent intent = new Intent(Intent.ACTION_MAIN);
769 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
770 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
771 return intent;
772 }
773
774 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700775 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700776 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leec3766332013-07-31 11:13:16 -0700777 hideDialpadFragment(true);
778 } else if (mInSearchUi) {
779 mSearchView.setText(null);
Yorke Leec3766332013-07-31 11:13:16 -0700780 } else if (isTaskRoot()) {
781 // Instead of stopping, simply push this to the back of the stack.
782 // This is only done when running at the top of the stack;
783 // otherwise, we have been launched by someone else so need to
784 // allow the user to go back to the caller.
785 moveTaskToBack(false);
786 } else {
787 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700788 }
Yorke Leec3766332013-07-31 11:13:16 -0700789 }
790
791 @Override
792 public void onDialpadQueryChanged(String query) {
793 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
794 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
795 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700796 if (DEBUG) {
797 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
798 }
799 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
800 // This callback can happen if the dialpad fragment is recreated because of
801 // activity destruction. In that case, don't update the search view because
802 // that would bring the user back to the search fragment regardless of the
803 // previous state of the application. Instead, just return here and let the
804 // fragment manager correctly figure out whatever fragment was last displayed.
805 return;
806 }
Yorke Leec3766332013-07-31 11:13:16 -0700807 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700808 }
809 }
Yorke Leec3766332013-07-31 11:13:16 -0700810
811 @Override
812 public void onListFragmentScrollStateChange(int scrollState) {
813 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
814 hideDialpadFragmentIfNecessary();
815 hideInputMethod(getCurrentFocus());
816 }
817 }
818
819 @Override
820 public void onPhoneFavoriteFragmentStarted() {
821 setupFakeActionBarItemsForFavoritesFragment();
822 }
823
824 @Override
825 public void onDialpadFragmentStarted() {
826 setupFakeActionBarItemsForDialpadFragment();
827 }
828
829 private boolean phoneIsInUse() {
830 final TelephonyManager tm = (TelephonyManager) getSystemService(
831 Context.TELEPHONY_SERVICE);
832 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
833 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700834
835 @Override
836 public void onShowAllContacts() {
837 final Intent intent = new Intent(this, AllContactsActivity.class);
838 startActivity(intent);
839 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700840}