Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 1 | /* |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package com.android.dialer; |
| 18 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.Animator.AnimatorListener; |
| 21 | import android.animation.AnimatorListenerAdapter; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 22 | import android.app.Activity; |
Christine Chen | 9c1e065 | 2013-05-23 15:40:19 -0700 | [diff] [blame] | 23 | import android.app.backup.BackupManager; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 24 | import android.app.Fragment; |
| 25 | import android.app.FragmentManager; |
| 26 | import android.app.FragmentTransaction; |
Chiao Cheng | 073b5cf | 2012-12-07 11:36:00 -0800 | [diff] [blame] | 27 | import android.content.ActivityNotFoundException; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.SharedPreferences; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 31 | import android.content.res.Resources; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 32 | import android.net.Uri; |
| 33 | import android.os.Bundle; |
| 34 | import android.os.RemoteException; |
| 35 | import android.os.ServiceManager; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 36 | import android.provider.CallLog.Calls; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 37 | import android.provider.ContactsContract; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 38 | import android.provider.ContactsContract.Contacts; |
| 39 | import android.provider.ContactsContract.Intents.UI; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 40 | import android.provider.Settings; |
| 41 | import android.speech.RecognizerIntent; |
| 42 | import android.support.v4.app.NavUtils; |
| 43 | import android.telephony.TelephonyManager; |
| 44 | import android.text.Editable; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 45 | import android.text.TextUtils; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 46 | import android.text.TextWatcher; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 47 | import android.util.Log; |
| 48 | import android.view.Menu; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 49 | import android.view.MenuItem; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 50 | import android.view.View; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 51 | import android.view.View.OnFocusChangeListener; |
| 52 | import android.view.ViewConfiguration; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 53 | import android.view.inputmethod.InputMethodManager; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 54 | import android.widget.AbsListView.OnScrollListener; |
| 55 | import android.widget.EditText; |
| 56 | import android.widget.ImageView; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 57 | import android.widget.PopupMenu; |
| 58 | import android.widget.SearchView; |
| 59 | import android.widget.SearchView.OnCloseListener; |
| 60 | import android.widget.SearchView.OnQueryTextListener; |
Chiao Cheng | 073b5cf | 2012-12-07 11:36:00 -0800 | [diff] [blame] | 61 | import android.widget.Toast; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 62 | |
Chiao Cheng | 9d4f3b2 | 2012-09-05 16:00:16 -0700 | [diff] [blame] | 63 | import com.android.contacts.common.CallUtil; |
Chiao Cheng | 603ff68 | 2012-10-24 15:18:40 -0700 | [diff] [blame] | 64 | import com.android.contacts.common.activity.TransactionSafeActivity; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 65 | import com.android.contacts.common.dialog.ClearFrequentsDialog; |
| 66 | import com.android.contacts.common.interactions.ImportExportDialogFragment; |
Chiao Cheng | 1429f1a | 2012-11-01 16:35:28 -0700 | [diff] [blame] | 67 | import com.android.contacts.common.list.ContactListItemView; |
Chiao Cheng | 8efbcf9 | 2012-12-04 17:43:02 -0800 | [diff] [blame] | 68 | import com.android.contacts.common.list.OnPhoneNumberPickerActionListener; |
| 69 | import com.android.contacts.common.list.PhoneNumberPickerFragment; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 70 | import com.android.dialer.calllog.CallLogActivity; |
Yorke Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 71 | import com.android.dialer.dialpad.DialpadFragment; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 72 | import com.android.dialer.dialpad.SmartDialNameMatcher; |
Chiao Cheng | 1429f1a | 2012-11-01 16:35:28 -0700 | [diff] [blame] | 73 | import com.android.dialer.interactions.PhoneNumberInteraction; |
Yorke Lee | 8dd6200 | 2013-08-08 15:57:20 -0700 | [diff] [blame] | 74 | import com.android.dialer.list.AllContactsActivity; |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 75 | import com.android.dialer.list.PhoneFavoriteFragment; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 76 | import com.android.dialer.list.OnListFragmentScrolledListener; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 77 | import com.android.dialer.list.SmartDialSearchFragment; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 78 | import com.android.internal.telephony.ITelephony; |
| 79 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 80 | import java.util.ArrayList; |
| 81 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 82 | /** |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 83 | * The dialer tab's title is 'phone', a more common name (see strings.xml). |
| 84 | */ |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 85 | public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener, |
Yorke Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 86 | DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener, |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 87 | OnListFragmentScrolledListener, |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 88 | PhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener, |
Yorke Lee | 8dd6200 | 2013-08-08 15:57:20 -0700 | [diff] [blame] | 89 | DialpadFragment.OnDialpadFragmentStartedListener, |
| 90 | PhoneFavoriteFragment.OnShowAllContactsListener { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 91 | private static final String TAG = "DialtactsActivity"; |
| 92 | |
| 93 | public static final boolean DEBUG = false; |
| 94 | |
Yorke Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 95 | public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences"; |
| 96 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 97 | /** 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 101 | /** @see #getCallOrigin() */ |
| 102 | private static final String CALL_ORIGIN_DIALTACTS = |
| 103 | "com.android.dialer.DialtactsActivity"; |
| 104 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 105 | 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 109 | 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 113 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 114 | /** |
| 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 119 | private static final int SUBACTIVITY_ACCOUNT_FILTER = 1; |
| 120 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 121 | private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 122 | |
| 123 | private String mFilterText; |
| 124 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 125 | /** |
| 126 | * The main fragment displaying the user's favorites and frequent contacts |
| 127 | */ |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 128 | private PhoneFavoriteFragment mPhoneFavoriteFragment; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 131 | * Fragment containing the dialpad that slides into view |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 132 | */ |
Yorke Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 133 | private DialpadFragment mDialpadFragment; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Fragment for searching phone numbers using the alphanumeric keyboard. |
| 137 | */ |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 138 | private SearchFragment mRegularSearchFragment; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Fragment for searching phone numbers using the dialpad. |
| 142 | */ |
| 143 | private SmartDialSearchFragment mSmartDialSearchFragment; |
| 144 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 145 | 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 152 | /** |
| 153 | * True when this Activity is in its search UI (with a {@link SearchView} and |
| 154 | * {@link PhoneNumberPickerFragment}). |
| 155 | */ |
| 156 | private boolean mInSearchUi; |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 157 | /** |
| 158 | * True when this activity has been launched for the first time. |
| 159 | */ |
Yorke Lee | 98702de | 2013-08-06 12:03:32 -0700 | [diff] [blame] | 160 | private boolean mFirstLaunch; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 161 | private View mSearchViewContainer; |
| 162 | private View mSearchViewCloseButton; |
| 163 | private View mVoiceSearchButton; |
| 164 | private EditText mSearchView; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 165 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 166 | private String mSearchQuery; |
| 167 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 168 | /** |
| 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 179 | DialtactsActivity.this, dataUri, getCallOrigin()); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
Jay Shrauner | 31a760b | 2013-07-26 09:34:49 -0700 | [diff] [blame] | 183 | public void onCallNumberDirectly(String phoneNumber) { |
| 184 | Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin()); |
| 185 | startActivity(intent); |
| 186 | } |
| 187 | |
| 188 | @Override |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 189 | 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 202 | 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 209 | final String newText = s.toString(); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 210 | 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 222 | // 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 Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 228 | enterSearchUi(smartDialSearch, newText); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 231 | if (smartDialSearch && mSmartDialSearchFragment != null) { |
| 232 | mSmartDialSearchFragment.setQueryString(newText, false); |
| 233 | } else if (mRegularSearchFragment != null) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 234 | mRegularSearchFragment.setQueryString(newText, false); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 235 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 236 | mSearchViewCloseButton.setVisibility(View.VISIBLE); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void afterTextChanged(Editable s) { |
| 242 | } |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 245 | private boolean isDialpadShowing() { |
| 246 | return mDialpadFragment != null && mDialpadFragment.isVisible(); |
| 247 | } |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 248 | |
| 249 | @Override |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 250 | protected void onCreate(Bundle savedInstanceState) { |
| 251 | super.onCreate(savedInstanceState); |
Yorke Lee | 98702de | 2013-08-06 12:03:32 -0700 | [diff] [blame] | 252 | mFirstLaunch = true; |
| 253 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 254 | final Intent intent = getIntent(); |
| 255 | fixIntent(intent); |
| 256 | |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 257 | setContentView(R.layout.dialtacts_activity); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 258 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 259 | getActionBar().hide(); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 260 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 261 | // 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 263 | if (savedInstanceState == null) { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 264 | final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment(); |
| 265 | phoneFavoriteFragment.setListener(mPhoneFavoriteListener); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 266 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 267 | final FragmentTransaction ft = getFragmentManager().beginTransaction(); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 268 | ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT); |
| 269 | ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 270 | ft.commit(); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 271 | } else { |
| 272 | mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY); |
| 273 | mInSearchUi = savedInstanceState.getBoolean(KEY_IN_SEARCH_UI); |
| 274 | mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 277 | mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding); |
| 278 | prepareSearchView(); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 279 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 280 | if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction()) |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 281 | && savedInstanceState == null) { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 282 | setupFilterText(intent); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | @Override |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 287 | protected void onResume() { |
| 288 | super.onResume(); |
Yorke Lee | 98702de | 2013-08-06 12:03:32 -0700 | [diff] [blame] | 289 | if (mFirstLaunch) { |
| 290 | displayFragment(getIntent()); |
| 291 | } |
| 292 | mFirstLaunch = false; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Override |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 296 | 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 304 | public void onAttachFragment(Fragment fragment) { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 305 | if (fragment instanceof DialpadFragment) { |
| 306 | mDialpadFragment = (DialpadFragment) fragment; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 307 | final FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 308 | transaction.hide(mDialpadFragment); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 309 | transaction.commit(); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 310 | } 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 316 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | @Override |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 320 | 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 Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 328 | DialtactsActivity.class); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 329 | 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 Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 344 | final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent(); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 345 | startActivity(settingsIntent); |
| 346 | } |
| 347 | return false; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public void onClick(View view) { |
| 352 | switch (view.getId()) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 353 | case R.id.overflow_menu: { |
| 354 | final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view); |
| 355 | final Menu menu = popupMenu.getMenu(); |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 356 | popupMenu.inflate(R.menu.dialtacts_options); |
Yorke Lee | f420a53 | 2013-08-09 15:16:10 -0700 | [diff] [blame] | 357 | final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents); |
Yorke Lee | 0d391b4 | 2013-08-09 17:27:00 -0700 | [diff] [blame] | 358 | clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents()); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 359 | popupMenu.setOnMenuItemClickListener(this); |
| 360 | popupMenu.show(); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 361 | break; |
| 362 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 363 | 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 Lee | f420a53 | 2013-08-09 15:16:10 -0700 | [diff] [blame] | 368 | // Use explicit CallLogActivity intent instead of ACTION_VIEW + |
| 369 | // CONTENT_TYPE, so that we always open our call log from our dialer |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 370 | 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 377 | } |
| 378 | break; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 379 | 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 383 | default: { |
| 384 | Log.wtf(TAG, "Unexpected onClick event from " + view); |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 390 | @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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 408 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 409 | private void showDialpadFragment(boolean animate) { |
Yorke Lee | ea695cb | 2013-08-09 11:02:26 -0700 | [diff] [blame] | 410 | mDialpadFragment.setAdjustTranslationForAnimation(animate); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 411 | final FragmentTransaction ft = getFragmentManager().beginTransaction(); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 412 | if (animate) { |
| 413 | ft.setCustomAnimations(R.anim.slide_in, 0); |
Yorke Lee | ea695cb | 2013-08-09 11:02:26 -0700 | [diff] [blame] | 414 | } else { |
| 415 | mDialpadFragment.setYFraction(0); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 416 | } |
| 417 | ft.show(mDialpadFragment); |
| 418 | ft.commit(); |
| 419 | } |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 420 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 421 | private void hideDialpadFragment(boolean animate) { |
Yorke Lee | ea695cb | 2013-08-09 11:02:26 -0700 | [diff] [blame] | 422 | mDialpadFragment.setAdjustTranslationForAnimation(animate); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 423 | 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 431 | private void prepareSearchView() { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 432 | 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 441 | @Override |
| 442 | public void onFocusChange(View view, boolean hasFocus) { |
| 443 | if (hasFocus) { |
| 444 | showInputMethod(view.findFocus()); |
| 445 | } |
| 446 | } |
| 447 | }); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 450 | private void hideDialpadFragmentIfNecessary() { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 451 | if (mDialpadFragment != null && mDialpadFragment.isVisible()) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 452 | hideDialpadFragment(true); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 456 | final AnimatorListener mHideListener = new AnimatorListenerAdapter() { |
| 457 | @Override |
| 458 | public void onAnimationEnd(Animator animation) { |
| 459 | mSearchViewContainer.setVisibility(View.GONE); |
| 460 | } |
| 461 | }; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 462 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 463 | public void hideSearchBar() { |
| 464 | hideSearchBar(true); |
Christine Chen | 9c1e065 | 2013-05-23 15:40:19 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 467 | 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 Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 475 | if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) { |
| 476 | mBottomPaddingView.setVisibility(View.VISIBLE); |
| 477 | return; |
| 478 | } |
| 479 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 480 | 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 Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 486 | if (mPhoneFavoriteFragment.getView() != null) { |
| 487 | mPhoneFavoriteFragment.getView().setTranslationY(0); |
| 488 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 489 | } |
| 490 | }); |
| 491 | } else { |
| 492 | mSearchViewContainer.setTranslationY(-mSearchView.getHeight()); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | public void showSearchBar() { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 497 | |
| 498 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 499 | 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 Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 510 | // 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 516 | 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 546 | } |
| 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 559 | /** |
| 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 567 | private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 568 | // 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 590 | private void displayFragment(Intent intent) { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 591 | // 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 Lee | 98702de | 2013-08-06 12:03:32 -0700 | [diff] [blame] | 599 | if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 600 | mDialpadFragment.setStartedFromNewIntent(true); |
Yorke Lee | ea695cb | 2013-08-09 11:02:26 -0700 | [diff] [blame] | 601 | showDialpadFragment(false); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 602 | } |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | @Override |
| 606 | public void onNewIntent(Intent newIntent) { |
| 607 | setIntent(newIntent); |
| 608 | fixIntent(newIntent); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 609 | displayFragment(newIntent); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 610 | final String action = newIntent.getAction(); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 611 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 612 | 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 Cheng | 9d4f3b2 | 2012-09-05 16:00:16 -0700 | [diff] [blame] | 623 | if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 624 | 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 Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 671 | private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener = |
| 672 | new PhoneFavoriteFragment.Listener() { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 673 | @Override |
| 674 | public void onContactSelected(Uri contactUri) { |
| 675 | PhoneNumberInteraction.startInteractionForPhoneCall( |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 676 | DialtactsActivity.this, contactUri, getCallOrigin()); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | @Override |
| 680 | public void onCallNumberDirectly(String phoneNumber) { |
Chiao Cheng | 9d4f3b2 | 2012-09-05 16:00:16 -0700 | [diff] [blame] | 681 | Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin()); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 682 | startActivity(intent); |
| 683 | } |
| 684 | }; |
| 685 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 686 | /* 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 689 | |
| 690 | @Override |
| 691 | public void startSearch(String initialQuery, boolean selectInitialQuery, |
| 692 | Bundle appSearchData, boolean globalSearch) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 693 | if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 694 | 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 706 | }*/ |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 707 | |
| 708 | private void showInputMethod(View view) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 709 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 710 | Context.INPUT_METHOD_SERVICE); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 711 | if (imm != null) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 712 | imm.showSoftInput(view, 0); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
| 716 | private void hideInputMethod(View view) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 717 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 718 | Context.INPUT_METHOD_SERVICE); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 719 | if (imm != null && view != null) { |
| 720 | imm.hideSoftInputFromWindow(view.getWindowToken(), 0); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | /** |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 725 | * Shows the search fragment |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 726 | */ |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 727 | 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 734 | final FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
| 735 | transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 736 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 737 | 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 755 | mInSearchUi = true; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /** |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 759 | * Hides the search fragment |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 760 | */ |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 761 | private void exitSearchUi() { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 762 | getFragmentManager().popBackStack(); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 763 | mInSearchUi = false; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 764 | } |
| 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 775 | public void onBackPressed() { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 776 | if (mDialpadFragment != null && mDialpadFragment.isVisible()) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 777 | hideDialpadFragment(true); |
| 778 | } else if (mInSearchUi) { |
| 779 | mSearchView.setText(null); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 780 | } 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 788 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 789 | } |
| 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 Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame^] | 796 | 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 Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 807 | mSearchView.setText(normalizedQuery); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 808 | } |
| 809 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 810 | |
| 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 Lee | 8dd6200 | 2013-08-08 15:57:20 -0700 | [diff] [blame] | 834 | |
| 835 | @Override |
| 836 | public void onShowAllContacts() { |
| 837 | final Intent intent = new Intent(this, AllContactsActivity.class); |
| 838 | startActivity(intent); |
| 839 | } |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 840 | } |