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