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