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 | fce269a | 2013-08-12 11:56:04 -0700 | [diff] [blame] | 71 | import com.android.dialer.database.DialerDatabaseHelper; |
Yorke Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 72 | import com.android.dialer.dialpad.DialpadFragment; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 73 | import com.android.dialer.dialpad.SmartDialNameMatcher; |
Yorke Lee | fce269a | 2013-08-12 11:56:04 -0700 | [diff] [blame] | 74 | import com.android.dialer.dialpad.SmartDialPrefix; |
Chiao Cheng | 1429f1a | 2012-11-01 16:35:28 -0700 | [diff] [blame] | 75 | import com.android.dialer.interactions.PhoneNumberInteraction; |
Yorke Lee | 8dd6200 | 2013-08-08 15:57:20 -0700 | [diff] [blame] | 76 | import com.android.dialer.list.AllContactsActivity; |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 77 | import com.android.dialer.list.PhoneFavoriteFragment; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 78 | import com.android.dialer.list.OnListFragmentScrolledListener; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 79 | import com.android.dialer.list.SmartDialSearchFragment; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 80 | import com.android.internal.telephony.ITelephony; |
| 81 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 82 | import java.util.ArrayList; |
| 83 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 84 | /** |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 85 | * The dialer tab's title is 'phone', a more common name (see strings.xml). |
| 86 | */ |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 87 | public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener, |
Yorke Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 88 | DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener, |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 89 | OnListFragmentScrolledListener, |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 90 | PhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener, |
Yorke Lee | 8dd6200 | 2013-08-08 15:57:20 -0700 | [diff] [blame] | 91 | DialpadFragment.OnDialpadFragmentStartedListener, |
| 92 | PhoneFavoriteFragment.OnShowAllContactsListener { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 93 | private static final String TAG = "DialtactsActivity"; |
| 94 | |
| 95 | public static final boolean DEBUG = false; |
| 96 | |
Yorke Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 97 | public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences"; |
| 98 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 99 | /** Used to open Call Setting */ |
| 100 | private static final String PHONE_PACKAGE = "com.android.phone"; |
| 101 | private static final String CALL_SETTINGS_CLASS_NAME = |
| 102 | "com.android.phone.CallFeaturesSetting"; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 103 | /** @see #getCallOrigin() */ |
| 104 | private static final String CALL_ORIGIN_DIALTACTS = |
| 105 | "com.android.dialer.DialtactsActivity"; |
| 106 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 107 | private static final String KEY_IN_SEARCH_UI = "in_search_ui"; |
| 108 | private static final String KEY_SEARCH_QUERY = "search_query"; |
| 109 | private static final String KEY_FIRST_LAUNCH = "first_launch"; |
| 110 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 111 | private static final String TAG_DIALPAD_FRAGMENT = "dialpad"; |
| 112 | private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search"; |
| 113 | private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial"; |
| 114 | private static final String TAG_FAVORITES_FRAGMENT = "favorites"; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 115 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}. |
| 118 | */ |
| 119 | private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER"; |
| 120 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 121 | private static final int SUBACTIVITY_ACCOUNT_FILTER = 1; |
| 122 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 123 | private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 124 | |
| 125 | private String mFilterText; |
| 126 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 127 | /** |
| 128 | * The main fragment displaying the user's favorites and frequent contacts |
| 129 | */ |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 130 | private PhoneFavoriteFragment mPhoneFavoriteFragment; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 133 | * Fragment containing the dialpad that slides into view |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 134 | */ |
Yorke Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 135 | private DialpadFragment mDialpadFragment; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Fragment for searching phone numbers using the alphanumeric keyboard. |
| 139 | */ |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 140 | private SearchFragment mRegularSearchFragment; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Fragment for searching phone numbers using the dialpad. |
| 144 | */ |
| 145 | private SmartDialSearchFragment mSmartDialSearchFragment; |
| 146 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 147 | private View mMenuButton; |
| 148 | private View mCallHistoryButton; |
| 149 | private View mDialpadButton; |
| 150 | |
| 151 | // Padding view used to shift the fragments up when the dialpad is shown. |
| 152 | private View mBottomPaddingView; |
| 153 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 154 | /** |
| 155 | * True when this Activity is in its search UI (with a {@link SearchView} and |
| 156 | * {@link PhoneNumberPickerFragment}). |
| 157 | */ |
| 158 | private boolean mInSearchUi; |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 159 | /** |
| 160 | * True when this activity has been launched for the first time. |
| 161 | */ |
Yorke Lee | 98702de | 2013-08-06 12:03:32 -0700 | [diff] [blame] | 162 | private boolean mFirstLaunch; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 163 | private View mSearchViewContainer; |
| 164 | private View mSearchViewCloseButton; |
| 165 | private View mVoiceSearchButton; |
| 166 | private EditText mSearchView; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 167 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 168 | private String mSearchQuery; |
| 169 | |
Yorke Lee | fce269a | 2013-08-12 11:56:04 -0700 | [diff] [blame] | 170 | private DialerDatabaseHelper mDialerDatabaseHelper; |
| 171 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 172 | /** |
| 173 | * Listener used when one of phone numbers in search UI is selected. This will initiate a |
| 174 | * phone call using the phone number. |
| 175 | */ |
| 176 | private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener = |
| 177 | new OnPhoneNumberPickerActionListener() { |
| 178 | @Override |
| 179 | public void onPickPhoneNumberAction(Uri dataUri) { |
| 180 | // Specify call-origin so that users will see the previous tab instead of |
| 181 | // CallLog screen (search UI will be automatically exited). |
| 182 | PhoneNumberInteraction.startInteractionForPhoneCall( |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 183 | DialtactsActivity.this, dataUri, getCallOrigin()); |
Yorke Lee | f6673d3 | 2013-08-23 15:34:17 -0700 | [diff] [blame^] | 184 | hideDialpadAndSearchUi(); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
Jay Shrauner | 31a760b | 2013-07-26 09:34:49 -0700 | [diff] [blame] | 188 | public void onCallNumberDirectly(String phoneNumber) { |
| 189 | Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin()); |
| 190 | startActivity(intent); |
Yorke Lee | f6673d3 | 2013-08-23 15:34:17 -0700 | [diff] [blame^] | 191 | hideDialpadAndSearchUi(); |
Jay Shrauner | 31a760b | 2013-07-26 09:34:49 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 195 | public void onShortcutIntentCreated(Intent intent) { |
| 196 | Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring."); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void onHomeInActionBarSelected() { |
| 201 | exitSearchUi(); |
| 202 | } |
| 203 | }; |
| 204 | |
| 205 | /** |
| 206 | * Listener used to send search queries to the phone search fragment. |
| 207 | */ |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 208 | private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() { |
| 209 | @Override |
| 210 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 215 | final String newText = s.toString(); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 216 | if (newText.equals(mSearchQuery)) { |
| 217 | // If the query hasn't changed (perhaps due to activity being destroyed |
| 218 | // and restored, or user launching the same DIAL intent twice), then there is |
| 219 | // no need to do anything here. |
| 220 | return; |
| 221 | } |
| 222 | mSearchQuery = newText; |
| 223 | if (DEBUG) { |
| 224 | Log.d(TAG, "onTextChange for mSearchView called with new query: " + s); |
| 225 | } |
| 226 | final boolean smartDialSearch = isDialpadShowing(); |
| 227 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 228 | // Show search result with non-empty text. Show a bare list otherwise. |
| 229 | if (TextUtils.isEmpty(newText) && mInSearchUi) { |
| 230 | exitSearchUi(); |
| 231 | mSearchViewCloseButton.setVisibility(View.GONE); |
| 232 | return; |
| 233 | } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 234 | enterSearchUi(smartDialSearch, newText); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 237 | if (smartDialSearch && mSmartDialSearchFragment != null) { |
| 238 | mSmartDialSearchFragment.setQueryString(newText, false); |
| 239 | } else if (mRegularSearchFragment != null) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 240 | mRegularSearchFragment.setQueryString(newText, false); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 241 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 242 | mSearchViewCloseButton.setVisibility(View.VISIBLE); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public void afterTextChanged(Editable s) { |
| 248 | } |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 251 | private boolean isDialpadShowing() { |
| 252 | return mDialpadFragment != null && mDialpadFragment.isVisible(); |
| 253 | } |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 254 | |
| 255 | @Override |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 256 | protected void onCreate(Bundle savedInstanceState) { |
| 257 | super.onCreate(savedInstanceState); |
Yorke Lee | 98702de | 2013-08-06 12:03:32 -0700 | [diff] [blame] | 258 | mFirstLaunch = true; |
| 259 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 260 | final Intent intent = getIntent(); |
| 261 | fixIntent(intent); |
| 262 | |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 263 | setContentView(R.layout.dialtacts_activity); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 264 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 265 | getActionBar().hide(); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 266 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 267 | // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState |
| 268 | // is null. Otherwise the fragment manager takes care of recreating these fragments. |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 269 | if (savedInstanceState == null) { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 270 | final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment(); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 271 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 272 | final FragmentTransaction ft = getFragmentManager().beginTransaction(); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 273 | ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT); |
| 274 | ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 275 | ft.commit(); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 276 | } else { |
| 277 | mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY); |
| 278 | mInSearchUi = savedInstanceState.getBoolean(KEY_IN_SEARCH_UI); |
| 279 | mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 282 | mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding); |
| 283 | prepareSearchView(); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 284 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 285 | if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction()) |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 286 | && savedInstanceState == null) { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 287 | setupFilterText(intent); |
| 288 | } |
Yorke Lee | fce269a | 2013-08-12 11:56:04 -0700 | [diff] [blame] | 289 | |
| 290 | mDialerDatabaseHelper = DialerDatabaseHelper.getInstance(this); |
| 291 | SmartDialPrefix.initializeNanpSettings(this); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Override |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 295 | protected void onResume() { |
| 296 | super.onResume(); |
Yorke Lee | 98702de | 2013-08-06 12:03:32 -0700 | [diff] [blame] | 297 | if (mFirstLaunch) { |
| 298 | displayFragment(getIntent()); |
| 299 | } |
| 300 | mFirstLaunch = false; |
Yorke Lee | fce269a | 2013-08-12 11:56:04 -0700 | [diff] [blame] | 301 | mDialerDatabaseHelper.startSmartDialUpdateThread(); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 305 | protected void onSaveInstanceState(Bundle outState) { |
| 306 | super.onSaveInstanceState(outState); |
| 307 | outState.putString(KEY_SEARCH_QUERY, mSearchQuery); |
| 308 | outState.putBoolean(KEY_IN_SEARCH_UI, mInSearchUi); |
| 309 | outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch); |
| 310 | } |
| 311 | |
| 312 | @Override |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 313 | public void onAttachFragment(Fragment fragment) { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 314 | if (fragment instanceof DialpadFragment) { |
| 315 | mDialpadFragment = (DialpadFragment) fragment; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 316 | final FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 317 | transaction.hide(mDialpadFragment); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 318 | transaction.commit(); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 319 | } else if (fragment instanceof SmartDialSearchFragment) { |
| 320 | mSmartDialSearchFragment = (SmartDialSearchFragment) fragment; |
Yorke Lee | 63ea4cd | 2013-08-12 11:04:47 -0700 | [diff] [blame] | 321 | mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener( |
| 322 | mPhoneNumberPickerActionListener); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 323 | } else if (fragment instanceof SearchFragment) { |
| 324 | mRegularSearchFragment = (SearchFragment) fragment; |
Yorke Lee | 63ea4cd | 2013-08-12 11:04:47 -0700 | [diff] [blame] | 325 | mRegularSearchFragment.setOnPhoneNumberPickerActionListener( |
| 326 | mPhoneNumberPickerActionListener); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 327 | } else if (fragment instanceof PhoneFavoriteFragment) { |
| 328 | mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment; |
Yorke Lee | 5781afe | 2013-08-12 11:12:59 -0700 | [diff] [blame] | 329 | mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 330 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 334 | public boolean onMenuItemClick(MenuItem item) { |
| 335 | switch (item.getItemId()) { |
| 336 | case R.id.menu_import_export: |
| 337 | // We hard-code the "contactsAreAvailable" argument because doing it properly would |
| 338 | // involve querying a {@link ProviderStatusLoader}, which we don't want to do right |
| 339 | // now in Dialtacts for (potential) performance reasons. Compare with how it is |
| 340 | // done in {@link PeopleActivity}. |
| 341 | ImportExportDialogFragment.show(getFragmentManager(), true, |
Yorke Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 342 | DialtactsActivity.class); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 343 | return true; |
| 344 | case R.id.menu_clear_frequents: |
| 345 | ClearFrequentsDialog.show(getFragmentManager()); |
| 346 | return true; |
| 347 | case R.id.add_contact: |
| 348 | try { |
| 349 | startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI)); |
| 350 | } catch (ActivityNotFoundException e) { |
| 351 | Toast toast = Toast.makeText(this, |
| 352 | R.string.add_contact_not_available, |
| 353 | Toast.LENGTH_SHORT); |
| 354 | toast.show(); |
| 355 | } |
| 356 | return true; |
| 357 | case R.id.menu_call_settings: |
Yorke Lee | f74011e | 2013-08-02 11:30:30 -0700 | [diff] [blame] | 358 | final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent(); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 359 | startActivity(settingsIntent); |
| 360 | } |
| 361 | return false; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public void onClick(View view) { |
| 366 | switch (view.getId()) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 367 | case R.id.overflow_menu: { |
| 368 | final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view); |
| 369 | final Menu menu = popupMenu.getMenu(); |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 370 | popupMenu.inflate(R.menu.dialtacts_options); |
Yorke Lee | f420a53 | 2013-08-09 15:16:10 -0700 | [diff] [blame] | 371 | final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents); |
Yorke Lee | 0d391b4 | 2013-08-09 17:27:00 -0700 | [diff] [blame] | 372 | clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents()); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 373 | popupMenu.setOnMenuItemClickListener(this); |
| 374 | popupMenu.show(); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 375 | break; |
| 376 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 377 | case R.id.dialpad_button: |
| 378 | showDialpadFragment(true); |
| 379 | break; |
| 380 | case R.id.call_history_on_dialpad_button: |
| 381 | case R.id.call_history_button: |
Yorke Lee | f420a53 | 2013-08-09 15:16:10 -0700 | [diff] [blame] | 382 | // Use explicit CallLogActivity intent instead of ACTION_VIEW + |
| 383 | // 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] | 384 | final Intent intent = new Intent(this, CallLogActivity.class); |
| 385 | startActivity(intent); |
| 386 | break; |
| 387 | case R.id.search_close_button: |
| 388 | // Clear the search field |
| 389 | if (!TextUtils.isEmpty(mSearchView.getText())) { |
| 390 | mSearchView.setText(""); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 391 | } |
| 392 | break; |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 393 | case R.id.voice_search_button: |
| 394 | final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
| 395 | startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH); |
| 396 | break; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 397 | default: { |
| 398 | Log.wtf(TAG, "Unexpected onClick event from " + view); |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 404 | @Override |
| 405 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 406 | if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) { |
| 407 | if (resultCode == RESULT_OK) { |
| 408 | final ArrayList<String> matches = data.getStringArrayListExtra( |
| 409 | RecognizerIntent.EXTRA_RESULTS); |
| 410 | if (matches.size() > 0) { |
| 411 | final String match = matches.get(0); |
| 412 | mSearchView.setText(match); |
| 413 | } else { |
| 414 | Log.e(TAG, "Voice search - nothing heard"); |
| 415 | } |
| 416 | } else { |
| 417 | Log.e(TAG, "Voice search failed"); |
| 418 | } |
| 419 | } |
| 420 | super.onActivityResult(requestCode, resultCode, data); |
| 421 | } |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 422 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 423 | private void showDialpadFragment(boolean animate) { |
Yorke Lee | ea695cb | 2013-08-09 11:02:26 -0700 | [diff] [blame] | 424 | mDialpadFragment.setAdjustTranslationForAnimation(animate); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 425 | final FragmentTransaction ft = getFragmentManager().beginTransaction(); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 426 | if (animate) { |
| 427 | ft.setCustomAnimations(R.anim.slide_in, 0); |
Yorke Lee | ea695cb | 2013-08-09 11:02:26 -0700 | [diff] [blame] | 428 | } else { |
| 429 | mDialpadFragment.setYFraction(0); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 430 | } |
| 431 | ft.show(mDialpadFragment); |
| 432 | ft.commit(); |
| 433 | } |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 434 | |
Yorke Lee | f6673d3 | 2013-08-23 15:34:17 -0700 | [diff] [blame^] | 435 | private void hideDialpadFragment(boolean animate, boolean clearDialpad) { |
| 436 | if (mDialpadFragment == null) return; |
| 437 | if (clearDialpad) { |
| 438 | mDialpadFragment.clearDialpad(); |
| 439 | } |
| 440 | if (!mDialpadFragment.isVisible()) return; |
Yorke Lee | ea695cb | 2013-08-09 11:02:26 -0700 | [diff] [blame] | 441 | mDialpadFragment.setAdjustTranslationForAnimation(animate); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 442 | final FragmentTransaction ft = getFragmentManager().beginTransaction(); |
| 443 | if (animate) { |
| 444 | ft.setCustomAnimations(0, R.anim.slide_out); |
| 445 | } |
| 446 | ft.hide(mDialpadFragment); |
| 447 | ft.commit(); |
| 448 | } |
| 449 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 450 | private void prepareSearchView() { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 451 | mSearchViewContainer = findViewById(R.id.search_view_container); |
| 452 | mSearchViewCloseButton = findViewById(R.id.search_close_button); |
| 453 | mSearchViewCloseButton.setOnClickListener(this); |
| 454 | mVoiceSearchButton = findViewById(R.id.voice_search_button); |
| 455 | mVoiceSearchButton.setOnClickListener(this); |
| 456 | mSearchView = (EditText) findViewById(R.id.search_view); |
| 457 | mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener); |
| 458 | mSearchView.setHint(getString(R.string.dialer_hint_find_contact)); |
| 459 | mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 460 | @Override |
| 461 | public void onFocusChange(View view, boolean hasFocus) { |
| 462 | if (hasFocus) { |
| 463 | showInputMethod(view.findFocus()); |
| 464 | } |
| 465 | } |
| 466 | }); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 469 | final AnimatorListener mHideListener = new AnimatorListenerAdapter() { |
| 470 | @Override |
| 471 | public void onAnimationEnd(Animator animation) { |
| 472 | mSearchViewContainer.setVisibility(View.GONE); |
| 473 | } |
| 474 | }; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 475 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 476 | public void hideSearchBar() { |
| 477 | hideSearchBar(true); |
Christine Chen | 9c1e065 | 2013-05-23 15:40:19 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 480 | public void hideSearchBar(boolean shiftView) { |
| 481 | if (shiftView) { |
| 482 | mSearchViewContainer.animate().cancel(); |
| 483 | mSearchViewContainer.setAlpha(1); |
| 484 | mSearchViewContainer.setTranslationY(0); |
| 485 | mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight()) |
| 486 | .setDuration(200).setListener(mHideListener); |
| 487 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 488 | if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) { |
| 489 | mBottomPaddingView.setVisibility(View.VISIBLE); |
| 490 | return; |
| 491 | } |
| 492 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 493 | mPhoneFavoriteFragment.getView().animate().withLayer() |
| 494 | .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener( |
| 495 | new AnimatorListenerAdapter() { |
| 496 | @Override |
| 497 | public void onAnimationEnd(Animator animation) { |
| 498 | mBottomPaddingView.setVisibility(View.VISIBLE); |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 499 | if (mPhoneFavoriteFragment.getView() != null) { |
| 500 | mPhoneFavoriteFragment.getView().setTranslationY(0); |
| 501 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 502 | } |
| 503 | }); |
| 504 | } else { |
| 505 | mSearchViewContainer.setTranslationY(-mSearchView.getHeight()); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | public void showSearchBar() { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 510 | |
| 511 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 512 | mSearchViewContainer.animate().cancel(); |
| 513 | mSearchViewContainer.setAlpha(0); |
| 514 | mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight()); |
| 515 | mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200) |
| 516 | .setListener(new AnimatorListenerAdapter() { |
| 517 | @Override |
| 518 | public void onAnimationStart(Animator animation) { |
| 519 | mSearchViewContainer.setVisibility(View.VISIBLE); |
| 520 | } |
| 521 | }); |
| 522 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 523 | // If the favorites fragment hasn't been fully created before the dialpad fragment |
| 524 | // is hidden (i.e. onResume), don't bother animating |
| 525 | if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) { |
| 526 | mBottomPaddingView.setVisibility(View.GONE); |
| 527 | return; |
| 528 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 529 | mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight()); |
| 530 | mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200) |
| 531 | .setListener( |
| 532 | new AnimatorListenerAdapter() { |
| 533 | @Override |
| 534 | public void onAnimationStart(Animator animation) { |
| 535 | mBottomPaddingView.setVisibility(View.GONE); |
| 536 | } |
| 537 | }); |
| 538 | } |
| 539 | |
| 540 | |
| 541 | public void setupFakeActionBarItemsForFavoritesFragment() { |
| 542 | mMenuButton = findViewById(R.id.overflow_menu); |
| 543 | if (mMenuButton != null) { |
| 544 | mMenuButton.setOnClickListener(this); |
| 545 | } |
| 546 | |
| 547 | mCallHistoryButton = findViewById(R.id.call_history_button); |
| 548 | // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth); |
| 549 | mCallHistoryButton.setOnClickListener(this); |
| 550 | |
| 551 | mDialpadButton = findViewById(R.id.dialpad_button); |
| 552 | // DialpadButton.setMinimumWidth(fakeMenuItemWidth); |
| 553 | mDialpadButton.setOnClickListener(this); |
| 554 | } |
| 555 | |
| 556 | public void setupFakeActionBarItemsForDialpadFragment() { |
| 557 | final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button); |
| 558 | callhistoryButton.setOnClickListener(this); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | private void fixIntent(Intent intent) { |
| 562 | // This should be cleaned up: the call key used to send an Intent |
| 563 | // that just said to go to the recent calls list. It now sends this |
| 564 | // abstract action, but this class hasn't been rewritten to deal with it. |
| 565 | if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) { |
| 566 | intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE); |
| 567 | intent.putExtra("call_key", true); |
| 568 | setIntent(intent); |
| 569 | } |
| 570 | } |
| 571 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 572 | /** |
| 573 | * Returns true if the intent is due to hitting the green send key (hardware call button: |
| 574 | * KEYCODE_CALL) while in a call. |
| 575 | * |
| 576 | * @param intent the intent that launched this activity |
| 577 | * @param recentCallsRequest true if the intent is requesting to view recent calls |
| 578 | * @return true if the intent is due to hitting the green send key while in a call |
| 579 | */ |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 580 | private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 581 | // If there is a call in progress go to the call screen |
| 582 | if (recentCallsRequest) { |
| 583 | final boolean callKey = intent.getBooleanExtra("call_key", false); |
| 584 | |
| 585 | try { |
| 586 | ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); |
| 587 | if (callKey && phone != null && phone.showCallScreen()) { |
| 588 | return true; |
| 589 | } |
| 590 | } catch (RemoteException e) { |
| 591 | Log.e(TAG, "Failed to handle send while in call", e); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Sets the current tab based on the intent's request type |
| 600 | * |
| 601 | * @param intent Intent that contains information about which tab should be selected |
| 602 | */ |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 603 | private void displayFragment(Intent intent) { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 604 | // If we got here by hitting send and we're in call forward along to the in-call activity |
| 605 | boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType( |
| 606 | getContentResolver())); |
| 607 | if (isSendKeyWhileInCall(intent, recentCallsRequest)) { |
| 608 | finish(); |
| 609 | return; |
| 610 | } |
| 611 | |
Yorke Lee | 98702de | 2013-08-06 12:03:32 -0700 | [diff] [blame] | 612 | if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 613 | mDialpadFragment.setStartedFromNewIntent(true); |
Yorke Lee | ea695cb | 2013-08-09 11:02:26 -0700 | [diff] [blame] | 614 | showDialpadFragment(false); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 615 | } |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | @Override |
| 619 | public void onNewIntent(Intent newIntent) { |
| 620 | setIntent(newIntent); |
| 621 | fixIntent(newIntent); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 622 | displayFragment(newIntent); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 623 | final String action = newIntent.getAction(); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 624 | |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 625 | invalidateOptionsMenu(); |
| 626 | } |
| 627 | |
| 628 | /** Returns true if the given intent contains a phone number to populate the dialer with */ |
| 629 | private boolean isDialIntent(Intent intent) { |
| 630 | final String action = intent.getAction(); |
| 631 | if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) { |
| 632 | return true; |
| 633 | } |
| 634 | if (Intent.ACTION_VIEW.equals(action)) { |
| 635 | final Uri data = intent.getData(); |
Chiao Cheng | 9d4f3b2 | 2012-09-05 16:00:16 -0700 | [diff] [blame] | 636 | if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 637 | return true; |
| 638 | } |
| 639 | } |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Returns an appropriate call origin for this Activity. May return null when no call origin |
| 645 | * should be used (e.g. when some 3rd party application launched the screen. Call origin is |
| 646 | * for remembering the tab in which the user made a phone call, so the external app's DIAL |
| 647 | * request should not be counted.) |
| 648 | */ |
| 649 | public String getCallOrigin() { |
| 650 | return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Retrieves the filter text stored in {@link #setupFilterText(Intent)}. |
| 655 | * This text originally came from a FILTER_CONTACTS_ACTION intent received |
| 656 | * by this activity. The stored text will then be cleared after after this |
| 657 | * method returns. |
| 658 | * |
| 659 | * @return The stored filter text |
| 660 | */ |
| 661 | public String getAndClearFilterText() { |
| 662 | String filterText = mFilterText; |
| 663 | mFilterText = null; |
| 664 | return filterText; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent. |
| 669 | * This is so child activities can check if they are supposed to display a filter. |
| 670 | * |
| 671 | * @param intent The intent received in {@link #onNewIntent(Intent)} |
| 672 | */ |
| 673 | private void setupFilterText(Intent intent) { |
| 674 | // If the intent was relaunched from history, don't apply the filter text. |
| 675 | if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { |
| 676 | return; |
| 677 | } |
| 678 | String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY); |
| 679 | if (filter != null && filter.length() > 0) { |
| 680 | mFilterText = filter; |
| 681 | } |
| 682 | } |
| 683 | |
Yorke Lee | 8898cd0 | 2013-08-08 10:24:27 -0700 | [diff] [blame] | 684 | private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener = |
| 685 | new PhoneFavoriteFragment.Listener() { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 686 | @Override |
| 687 | public void onContactSelected(Uri contactUri) { |
| 688 | PhoneNumberInteraction.startInteractionForPhoneCall( |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 689 | DialtactsActivity.this, contactUri, getCallOrigin()); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | @Override |
| 693 | public void onCallNumberDirectly(String phoneNumber) { |
Chiao Cheng | 9d4f3b2 | 2012-09-05 16:00:16 -0700 | [diff] [blame] | 694 | Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin()); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 695 | startActivity(intent); |
| 696 | } |
| 697 | }; |
| 698 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 699 | /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e. |
| 700 | * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might |
| 701 | * 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] | 702 | |
| 703 | @Override |
| 704 | public void startSearch(String initialQuery, boolean selectInitialQuery, |
| 705 | Bundle appSearchData, boolean globalSearch) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 706 | if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) { |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 707 | if (mInSearchUi) { |
| 708 | if (mSearchView.hasFocus()) { |
| 709 | showInputMethod(mSearchView.findFocus()); |
| 710 | } else { |
| 711 | mSearchView.requestFocus(); |
| 712 | } |
| 713 | } else { |
| 714 | enterSearchUi(); |
| 715 | } |
| 716 | } else { |
| 717 | super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); |
| 718 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 719 | }*/ |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 720 | |
| 721 | private void showInputMethod(View view) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 722 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 723 | Context.INPUT_METHOD_SERVICE); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 724 | if (imm != null) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 725 | imm.showSoftInput(view, 0); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
| 729 | private void hideInputMethod(View view) { |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 730 | final InputMethodManager imm = (InputMethodManager) getSystemService( |
| 731 | Context.INPUT_METHOD_SERVICE); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 732 | if (imm != null && view != null) { |
| 733 | imm.hideSoftInputFromWindow(view.getWindowToken(), 0); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /** |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 738 | * Shows the search fragment |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 739 | */ |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 740 | private void enterSearchUi(boolean smartDialSearch, String query) { |
Yorke Lee | 34bdf87 | 2013-08-15 14:11:50 -0700 | [diff] [blame] | 741 | if (getFragmentManager().isDestroyed()) { |
| 742 | // Weird race condition where fragment is doing work after the activity is destroyed |
| 743 | // due to talkback being on (b/10209937). Just return since we can't do any |
| 744 | // constructive here. |
| 745 | return; |
| 746 | } |
| 747 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 748 | if (DEBUG) { |
| 749 | Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch); |
| 750 | } |
| 751 | final String tag = smartDialSearch ? TAG_SMARTDIAL_SEARCH_FRAGMENT : |
| 752 | TAG_REGULAR_SEARCH_FRAGMENT; |
| 753 | |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 754 | final FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
| 755 | transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 756 | |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 757 | SearchFragment fragment; |
| 758 | |
| 759 | transaction.remove(mPhoneFavoriteFragment); |
| 760 | fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag); |
| 761 | if (fragment == null) { |
| 762 | if (smartDialSearch) { |
| 763 | fragment = new SmartDialSearchFragment(); |
| 764 | } else { |
| 765 | fragment = new SearchFragment(); |
| 766 | } |
| 767 | transaction.replace(R.id.dialtacts_frame, fragment, tag); |
| 768 | } else { |
| 769 | transaction.attach(fragment); |
| 770 | } |
| 771 | |
| 772 | transaction.addToBackStack(null); |
| 773 | fragment.setQueryString(query, false); |
| 774 | transaction.commit(); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 775 | mInSearchUi = true; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /** |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 779 | * Hides the search fragment |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 780 | */ |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 781 | private void exitSearchUi() { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 782 | getFragmentManager().popBackStack(); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 783 | mInSearchUi = false; |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /** Returns an Intent to launch Call Settings screen */ |
| 787 | public static Intent getCallSettingsIntent() { |
| 788 | final Intent intent = new Intent(Intent.ACTION_MAIN); |
| 789 | intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME); |
| 790 | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 791 | return intent; |
| 792 | } |
| 793 | |
| 794 | @Override |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 795 | public void onBackPressed() { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 796 | if (mDialpadFragment != null && mDialpadFragment.isVisible()) { |
Yorke Lee | f6673d3 | 2013-08-23 15:34:17 -0700 | [diff] [blame^] | 797 | hideDialpadFragment(true, false); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 798 | } else if (mInSearchUi) { |
| 799 | mSearchView.setText(null); |
Yorke Lee | f6673d3 | 2013-08-23 15:34:17 -0700 | [diff] [blame^] | 800 | mDialpadFragment.clearDialpad(); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 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 Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 809 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 810 | } |
| 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)) { |
Yorke Lee | ef2b738 | 2013-08-09 17:39:25 -0700 | [diff] [blame] | 817 | if (DEBUG) { |
| 818 | Log.d(TAG, "onDialpadQueryChanged - new query: " + query); |
| 819 | } |
| 820 | if (mDialpadFragment == null || !mDialpadFragment.isVisible()) { |
| 821 | // This callback can happen if the dialpad fragment is recreated because of |
| 822 | // activity destruction. In that case, don't update the search view because |
| 823 | // that would bring the user back to the search fragment regardless of the |
| 824 | // previous state of the application. Instead, just return here and let the |
| 825 | // fragment manager correctly figure out whatever fragment was last displayed. |
| 826 | return; |
| 827 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 828 | mSearchView.setText(normalizedQuery); |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 829 | } |
| 830 | } |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 831 | |
| 832 | @Override |
| 833 | public void onListFragmentScrollStateChange(int scrollState) { |
| 834 | if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { |
Yorke Lee | f6673d3 | 2013-08-23 15:34:17 -0700 | [diff] [blame^] | 835 | hideDialpadFragment(true, false); |
Yorke Lee | c376633 | 2013-07-31 11:13:16 -0700 | [diff] [blame] | 836 | hideInputMethod(getCurrentFocus()); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | @Override |
| 841 | public void onPhoneFavoriteFragmentStarted() { |
| 842 | setupFakeActionBarItemsForFavoritesFragment(); |
| 843 | } |
| 844 | |
| 845 | @Override |
| 846 | public void onDialpadFragmentStarted() { |
| 847 | setupFakeActionBarItemsForDialpadFragment(); |
| 848 | } |
| 849 | |
| 850 | private boolean phoneIsInUse() { |
| 851 | final TelephonyManager tm = (TelephonyManager) getSystemService( |
| 852 | Context.TELEPHONY_SERVICE); |
| 853 | return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE; |
| 854 | } |
Yorke Lee | 8dd6200 | 2013-08-08 15:57:20 -0700 | [diff] [blame] | 855 | |
| 856 | @Override |
| 857 | public void onShowAllContacts() { |
| 858 | final Intent intent = new Intent(this, AllContactsActivity.class); |
| 859 | startActivity(intent); |
| 860 | } |
Chiao Cheng | 94b10b5 | 2012-08-17 16:59:12 -0700 | [diff] [blame] | 861 | } |