blob: eee383831ce51d70f383feb711840a108aa3d51f [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
Yorke Leec3766332013-07-31 11:13:16 -07002 * Copyright (C) 2013 The Android Open Source Project
Chiao Cheng94b10b52012-08-17 16:59:12 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer;
18
Yorke Leec3766332013-07-31 11:13:16 -070019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.app.Activity;
23import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080026import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070027import android.content.Context;
28import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
Chiao Cheng94b10b52012-08-17 16:59:12 -070031import android.net.Uri;
32import android.os.Bundle;
33import android.os.RemoteException;
34import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.provider.CallLog.Calls;
36import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070037import android.provider.ContactsContract.Intents;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070039import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070040import android.telephony.TelephonyManager;
41import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070042import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070043import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.util.Log;
45import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.View;
Yorke Leeb0d19762014-03-05 14:43:28 -080048import android.view.ViewGroup.LayoutParams;
49import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070051import android.widget.AbsListView.OnScrollListener;
52import android.widget.EditText;
Yorke Leeb0d19762014-03-05 14:43:28 -080053import android.widget.LinearLayout;
Chiao Cheng94b10b52012-08-17 16:59:12 -070054import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080055import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070056
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070057import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070058import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070059import com.android.contacts.common.dialog.ClearFrequentsDialog;
60import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080061import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070062import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070063import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070064import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070065import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070066import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070067import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070068import com.android.dialer.list.AllContactsActivity;
Yorke Lee3cefcc62014-01-16 11:26:46 -080069import com.android.dialer.list.DragDropController;
70import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070072import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080073import com.android.dialer.list.PhoneFavoriteTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070074import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080075import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070076import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070077import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070078import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070079import com.android.internal.telephony.ITelephony;
80
Yorke Leec3766332013-07-31 11:13:16 -070081import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080082import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070083
Chiao Cheng94b10b52012-08-17 16:59:12 -070084/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070085 * The dialer tab's title is 'phone', a more common name (see strings.xml).
86 */
Yorke Leec3766332013-07-31 11:13:16 -070087public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070088 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070089 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080090 DialpadFragment.HostInterface,
Yorke Lee3cefcc62014-01-16 11:26:46 -080091 PhoneFavoriteFragment.OnShowAllContactsListener,
92 PhoneFavoriteFragment.HostInterface,
Ihab Awad526c0b82014-02-27 12:55:36 -080093 OnDragDropListener, View.OnLongClickListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070094 private static final String TAG = "DialtactsActivity";
95
Ihab Awad526c0b82014-02-27 12:55:36 -080096 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -070097
Yorke Leef74011e2013-08-02 11:30:30 -070098 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
99
Chiao Cheng94b10b52012-08-17 16:59:12 -0700100 /** Used to open Call Setting */
101 private static final String PHONE_PACKAGE = "com.android.phone";
102 private static final String CALL_SETTINGS_CLASS_NAME =
103 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700104 /** @see #getCallOrigin() */
105 private static final String CALL_ORIGIN_DIALTACTS =
106 "com.android.dialer.DialtactsActivity";
107
Yorke Leeb207f8a2013-08-29 18:51:06 -0700108 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
109 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700110 private static final String KEY_SEARCH_QUERY = "search_query";
111 private static final String KEY_FIRST_LAUNCH = "first_launch";
112
Yorke Leec3766332013-07-31 11:13:16 -0700113 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
114 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
115 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
116 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700117
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118 /**
119 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
120 */
121 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
122
Yorke Leec3766332013-07-31 11:13:16 -0700123 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700124
Yorke Lee3cefcc62014-01-16 11:26:46 -0800125 private static final int FADE_ANIMATION_DURATION = 200;
126
Chiao Cheng94b10b52012-08-17 16:59:12 -0700127 private String mFilterText;
128
Yorke Leec3766332013-07-31 11:13:16 -0700129 /**
130 * The main fragment displaying the user's favorites and frequent contacts
131 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700132 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700133
134 /**
Yorke Leec3766332013-07-31 11:13:16 -0700135 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700136 */
Yorke Leef74011e2013-08-02 11:30:30 -0700137 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700138
139 /**
140 * Fragment for searching phone numbers using the alphanumeric keyboard.
141 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700142 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700143
144 /**
145 * Fragment for searching phone numbers using the dialpad.
146 */
147 private SmartDialSearchFragment mSmartDialSearchFragment;
148
Yorke Leec3766332013-07-31 11:13:16 -0700149 private View mMenuButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800150 private View mFakeActionBar;
Yorke Leec3766332013-07-31 11:13:16 -0700151 private View mCallHistoryButton;
152 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800153 private View mDialButton;
Yorke Lee36c54132013-09-25 16:17:06 -0700154 private PopupMenu mOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700155
Yorke Leeb0d19762014-03-05 14:43:28 -0800156 // Padding view used to shift the fragment frame up when the dialpad is shown so that
157 // the contents of the fragment frame continue to exist in a layout of the same height
158 private View mFragmentsSpacer;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700159 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700160
Yorke Leeb207f8a2013-08-29 18:51:06 -0700161 private boolean mInDialpadSearch;
162 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700163 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700164
Yorke Leeef2b7382013-08-09 17:39:25 -0700165 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700166 * True if the dialpad is only temporarily showing due to being in call
167 */
168 private boolean mInCallDialpadUp;
169
170 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700171 * True when this activity has been launched for the first time.
172 */
Yorke Lee98702de2013-08-06 12:03:32 -0700173 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700174 private View mSearchViewContainer;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800175 private RemoveView mRemoveViewContainer;
Yorke Lee94f49042014-03-03 14:03:08 -0800176 // This view points to the Framelayout that houses both the search view and remove view
177 // containers.
178 private View mSearchAndRemoveViewContainer;
Yorke Leec3766332013-07-31 11:13:16 -0700179 private View mSearchViewCloseButton;
180 private View mVoiceSearchButton;
181 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700182
Yorke Leeef2b7382013-08-09 17:39:25 -0700183 private String mSearchQuery;
184
Yorke Leefce269a2013-08-12 11:56:04 -0700185 private DialerDatabaseHelper mDialerDatabaseHelper;
186
Yorke Lee36c54132013-09-25 16:17:06 -0700187 private class OverflowPopupMenu extends PopupMenu {
188 public OverflowPopupMenu(Context context, View anchor) {
189 super(context, anchor);
190 }
191
192 @Override
193 public void show() {
194 final Menu menu = getMenu();
195 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
196 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
197 super.show();
198 }
199 }
200
Chiao Cheng94b10b52012-08-17 16:59:12 -0700201 /**
202 * Listener used when one of phone numbers in search UI is selected. This will initiate a
203 * phone call using the phone number.
204 */
205 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
206 new OnPhoneNumberPickerActionListener() {
207 @Override
208 public void onPickPhoneNumberAction(Uri dataUri) {
209 // Specify call-origin so that users will see the previous tab instead of
210 // CallLog screen (search UI will be automatically exited).
211 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700212 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700213 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700214 }
215
216 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700217 public void onCallNumberDirectly(String phoneNumber) {
218 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
219 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700220 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700221 }
222
223 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700224 public void onShortcutIntentCreated(Intent intent) {
225 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
226 }
227
228 @Override
229 public void onHomeInActionBarSelected() {
230 exitSearchUi();
231 }
232 };
233
234 /**
235 * Listener used to send search queries to the phone search fragment.
236 */
Yorke Leec3766332013-07-31 11:13:16 -0700237 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
238 @Override
239 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
240 }
241
242 @Override
243 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700244 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700245 if (newText.equals(mSearchQuery)) {
246 // If the query hasn't changed (perhaps due to activity being destroyed
247 // and restored, or user launching the same DIAL intent twice), then there is
248 // no need to do anything here.
249 return;
250 }
251 mSearchQuery = newText;
252 if (DEBUG) {
253 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
254 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700255 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700256
Yorke Leec3766332013-07-31 11:13:16 -0700257 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700258 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700259 exitSearchUi();
260 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700261 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700262 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700263 } else if (!TextUtils.isEmpty(newText)) {
264 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
265 (!dialpadSearch && mInRegularSearch);
266 if (!sameSearchMode) {
267 // call enterSearchUi only if we are switching search modes, or entering
268 // search ui for the first time
269 enterSearchUi(dialpadSearch, newText);
270 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700271
Yorke Leeb207f8a2013-08-29 18:51:06 -0700272 if (dialpadSearch && mSmartDialSearchFragment != null) {
273 mSmartDialSearchFragment.setQueryString(newText, false);
274 } else if (mRegularSearchFragment != null) {
275 mRegularSearchFragment.setQueryString(newText, false);
276 }
277 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700278 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700279 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700280 }
Yorke Leec3766332013-07-31 11:13:16 -0700281 }
282
283 @Override
284 public void afterTextChanged(Editable s) {
285 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700286 };
287
Yorke Leec3766332013-07-31 11:13:16 -0700288 private boolean isDialpadShowing() {
289 return mDialpadFragment != null && mDialpadFragment.isVisible();
290 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291
292 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700293 protected void onCreate(Bundle savedInstanceState) {
294 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700295 mFirstLaunch = true;
296
Chiao Cheng94b10b52012-08-17 16:59:12 -0700297 final Intent intent = getIntent();
298 fixIntent(intent);
299
Yorke Lee8898cd02013-08-08 10:24:27 -0700300 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700301
Yorke Leec3766332013-07-31 11:13:16 -0700302 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700303
Yorke Leeef2b7382013-08-09 17:39:25 -0700304 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
305 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700306 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800307 getFragmentManager().beginTransaction()
308 .add(R.id.dialtacts_frame, new PhoneFavoriteFragment(), TAG_FAVORITES_FRAGMENT)
309 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
310 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700311 } else {
312 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700313 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
314 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700315 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700316 }
317
Yorke Leeb207f8a2013-08-29 18:51:06 -0700318 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leeb0d19762014-03-05 14:43:28 -0800319 mFragmentsSpacer = findViewById(R.id.contact_tile_frame_spacer);
Ihab Awad526c0b82014-02-27 12:55:36 -0800320
Yorke Lee3cefcc62014-01-16 11:26:46 -0800321 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800322 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
323
324 // When the first global layout pass is completed (and mSearchAndRemoveViewContainer has
325 // been assigned a valid height), assign that height to mFragmentsSpacer as well.
326 mSearchAndRemoveViewContainer.getViewTreeObserver().addOnGlobalLayoutListener(
327 new OnGlobalLayoutListener() {
328 @Override
329 public void onGlobalLayout() {
330 mSearchAndRemoveViewContainer.getViewTreeObserver()
331 .removeOnGlobalLayoutListener(this);
332 mFragmentsSpacer.setLayoutParams(
333 new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
334 mSearchAndRemoveViewContainer.getHeight()));
335 }
336 });
337
338
Yorke Leec3766332013-07-31 11:13:16 -0700339 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700340
Chiao Cheng94b10b52012-08-17 16:59:12 -0700341 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700342 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700343 setupFilterText(intent);
344 }
Yorke Leefce269a2013-08-12 11:56:04 -0700345
Ihab Awad526c0b82014-02-27 12:55:36 -0800346 hideDialpadFragment(false, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700347 setupFakeActionBarItems();
348
Yorke Lee0baa98b2013-08-22 10:55:16 -0700349 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700350 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700351 }
352
353 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700354 protected void onResume() {
355 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700356 if (mFirstLaunch) {
357 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700358 } else if (!phoneIsInUse() && mInCallDialpadUp) {
359 hideDialpadFragment(false, true);
360 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700361 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800362 prepareVoiceSearchButton();
Yorke Lee98702de2013-08-06 12:03:32 -0700363 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700364 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700365 }
366
367 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700368 protected void onPause() {
369 if (mClearSearchOnPause) {
370 hideDialpadAndSearchUi();
371 mClearSearchOnPause = false;
372 }
373 super.onPause();
374 }
375
376 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700377 protected void onSaveInstanceState(Bundle outState) {
378 super.onSaveInstanceState(outState);
379 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700380 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
381 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700382 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
383 }
384
385 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700386 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700387 if (fragment instanceof DialpadFragment) {
388 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700389 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700390 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700391 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700392 } else if (fragment instanceof SmartDialSearchFragment) {
393 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700394 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
395 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700396 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700397 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700398 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
399 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700400 } else if (fragment instanceof PhoneFavoriteFragment) {
401 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700402 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700403 }
Yorke Leec3766332013-07-31 11:13:16 -0700404 }
405
406 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700407 public boolean onMenuItemClick(MenuItem item) {
408 switch (item.getItemId()) {
409 case R.id.menu_import_export:
410 // We hard-code the "contactsAreAvailable" argument because doing it properly would
411 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
412 // now in Dialtacts for (potential) performance reasons. Compare with how it is
413 // done in {@link PeopleActivity}.
414 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700415 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700416 return true;
417 case R.id.menu_clear_frequents:
418 ClearFrequentsDialog.show(getFragmentManager());
419 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700420 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700421 try {
422 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
423 } catch (ActivityNotFoundException e) {
424 Toast toast = Toast.makeText(this,
425 R.string.add_contact_not_available,
426 Toast.LENGTH_SHORT);
427 toast.show();
428 }
429 return true;
430 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700431 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700432 return true;
433 case R.id.menu_all_contacts:
434 onShowAllContacts();
435 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700436 }
437 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700438 }
439
Alon Albertb453e5b2013-09-10 15:54:23 -0700440 protected void handleMenuSettings() {
441 openTelephonySetting(this);
442 }
443
444 public static void openTelephonySetting(Activity activity) {
445 final Intent settingsIntent = getCallSettingsIntent();
446 activity.startActivity(settingsIntent);
447 }
448
Chiao Cheng94b10b52012-08-17 16:59:12 -0700449 @Override
450 public void onClick(View view) {
451 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700452 case R.id.overflow_menu: {
Ihab Awad526c0b82014-02-27 12:55:36 -0800453 if (isDialpadShowing()) {
454 mDialpadFragment.optionsMenuInvoked(view);
455 } else {
456 mOverflowMenu.show();
457 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700458 break;
459 }
Yorke Leec3766332013-07-31 11:13:16 -0700460 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700461 // Reset the boolean flag that tracks whether the dialpad was up because
462 // we were in call. Regardless of whether it was true before, we want to
463 // show the dialpad because the user has explicitly clicked the dialpad
464 // button.
465 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700466 showDialpadFragment(true);
467 break;
Ihab Awad526c0b82014-02-27 12:55:36 -0800468 case R.id.dial_button:
469 // Dial button was pressed; tell the Dialpad fragment
470 mDialpadFragment.dialButtonPressed();
471 break;
Yorke Leec3766332013-07-31 11:13:16 -0700472 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700473 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
474 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700475 final Intent intent = new Intent(this, CallLogActivity.class);
476 startActivity(intent);
477 break;
478 case R.id.search_close_button:
479 // Clear the search field
480 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700481 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700482 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700483 }
484 break;
Yorke Leec3766332013-07-31 11:13:16 -0700485 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800486 try {
487 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
488 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
489 } catch (ActivityNotFoundException e) {
490 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
491 Toast.LENGTH_SHORT).show();
492 }
Yorke Leec3766332013-07-31 11:13:16 -0700493 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700494 default: {
495 Log.wtf(TAG, "Unexpected onClick event from " + view);
496 break;
497 }
498 }
499 }
500
Yorke Leec3766332013-07-31 11:13:16 -0700501 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800502 public boolean onLongClick(View view) {
503 switch (view.getId()) {
504 case R.id.dial_button: {
505 // Dial button was pressed; tell the Dialpad fragment
506 mDialpadFragment.dialButtonPressed();
507 return true; // Consume the event
508 }
509 default: {
510 Log.wtf(TAG, "Unexpected onClick event from " + view);
511 break;
512 }
513 }
514 return false;
515 }
516
517 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700518 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
519 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
520 if (resultCode == RESULT_OK) {
521 final ArrayList<String> matches = data.getStringArrayListExtra(
522 RecognizerIntent.EXTRA_RESULTS);
523 if (matches.size() > 0) {
524 final String match = matches.get(0);
525 mSearchView.setText(match);
526 } else {
527 Log.e(TAG, "Voice search - nothing heard");
528 }
529 } else {
530 Log.e(TAG, "Voice search failed");
531 }
532 }
533 super.onActivityResult(requestCode, resultCode, data);
534 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700535
Yorke Leec3766332013-07-31 11:13:16 -0700536 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700537 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700538 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700539 if (animate) {
540 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700541 } else {
542 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700543 }
544 ft.show(mDialpadFragment);
545 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800546 mDialButton.setVisibility(shouldShowOnscreenDialButton() ? View.VISIBLE : View.GONE);
547 mDialpadButton.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700548 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700549
Yorke Leeca195042013-09-25 16:29:38 -0700550 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700551 if (mDialpadFragment == null) return;
552 if (clearDialpad) {
553 mDialpadFragment.clearDialpad();
554 }
555 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700556 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700557 final FragmentTransaction ft = getFragmentManager().beginTransaction();
558 if (animate) {
559 ft.setCustomAnimations(0, R.anim.slide_out);
560 }
561 ft.hide(mDialpadFragment);
562 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800563 mDialButton.setVisibility(View.GONE);
564 mDialpadButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700565 }
566
Chiao Cheng94b10b52012-08-17 16:59:12 -0700567 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700568 mSearchViewContainer = findViewById(R.id.search_view_container);
569 mSearchViewCloseButton = findViewById(R.id.search_close_button);
570 mSearchViewCloseButton.setOnClickListener(this);
Yorke Leeda0f9042013-12-05 14:25:51 -0800571
Yorke Leec3766332013-07-31 11:13:16 -0700572 mSearchView = (EditText) findViewById(R.id.search_view);
573 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Tyler Gunna8ff9752014-02-24 11:57:57 -0800574 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
Yorke Leeda0f9042013-12-05 14:25:51 -0800575
576 prepareVoiceSearchButton();
577 }
578
579 private void prepareVoiceSearchButton() {
580 mVoiceSearchButton = findViewById(R.id.voice_search_button);
581 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
582 if (canIntentBeHandled(voiceIntent)) {
583 mVoiceSearchButton.setVisibility(View.VISIBLE);
584 mVoiceSearchButton.setOnClickListener(this);
585 } else {
586 mVoiceSearchButton.setVisibility(View.GONE);
587 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700588 }
589
Yorke Leec3766332013-07-31 11:13:16 -0700590 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
591 @Override
592 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800593 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700594 }
595 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700596
Yorke Leeb207f8a2013-08-29 18:51:06 -0700597 private boolean getInSearchUi() {
598 return mInDialpadSearch || mInRegularSearch;
599 }
600
601 private void setNotInSearchUi() {
602 mInDialpadSearch = false;
603 mInRegularSearch = false;
604 }
605
Yorke Lee311969c2013-08-26 06:31:11 -0700606 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700607 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700608 hideDialpadFragment(false, true);
609 }
610
Yorke Leec3766332013-07-31 11:13:16 -0700611 public void hideSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800612 final int height = mSearchAndRemoveViewContainer.getHeight();
Yorke Leeb0d19762014-03-05 14:43:28 -0800613
Yorke Lee94f49042014-03-03 14:03:08 -0800614 mSearchAndRemoveViewContainer.animate().cancel();
615 mSearchAndRemoveViewContainer.setAlpha(1);
616 mSearchAndRemoveViewContainer.setTranslationY(0);
617 mSearchAndRemoveViewContainer.animate().withLayer().alpha(0)
618 .translationY(-height).setDuration(200)
619 .setListener(mHideListener);
Christine Chen9c1e0652013-05-23 15:40:19 -0700620
Yorke Lee94f49042014-03-03 14:03:08 -0800621 mFragmentsFrame.animate().withLayer()
622 .translationY(-height).setDuration(200).setListener(
623 new AnimatorListenerAdapter() {
624 @Override
625 public void onAnimationEnd(Animator animation) {
626 mFragmentsFrame.setTranslationY(0);
Yorke Leeb0d19762014-03-05 14:43:28 -0800627 // Display the fragments spacer (which has the same height as the
628 // search box) now that the search box is hidden, so that
629 // mFragmentsFrame always retains the same height
630 mFragmentsSpacer.setVisibility(View.VISIBLE);
Yorke Lee94f49042014-03-03 14:03:08 -0800631 }
632 });
Yorke Leec3766332013-07-31 11:13:16 -0700633 }
634
635 public void showSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800636 final int height = mSearchAndRemoveViewContainer.getHeight();
637 mSearchAndRemoveViewContainer.animate().cancel();
638 mSearchAndRemoveViewContainer.setAlpha(0);
639 mSearchAndRemoveViewContainer.setTranslationY(-height);
640 mSearchAndRemoveViewContainer.animate().withLayer().alpha(1).translationY(0)
641 .setDuration(200).setListener(new AnimatorListenerAdapter() {
642 @Override
643 public void onAnimationStart(Animator animation) {
644 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
645 }
646 });
Yorke Leec3766332013-07-31 11:13:16 -0700647
Yorke Lee94f49042014-03-03 14:03:08 -0800648 mFragmentsFrame.setTranslationY(-height);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700649 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700650 .setListener(
651 new AnimatorListenerAdapter() {
652 @Override
653 public void onAnimationStart(Animator animation) {
Yorke Leeb0d19762014-03-05 14:43:28 -0800654 // Hide the fragment spacer now that the search box will
655 // be displayed again
656 mFragmentsSpacer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700657 }
658 });
659 }
660
661
Yorke Leeb207f8a2013-08-29 18:51:06 -0700662 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700663 mMenuButton = findViewById(R.id.overflow_menu);
664 if (mMenuButton != null) {
665 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700666
667 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
668 final Menu menu = mOverflowMenu.getMenu();
669 mOverflowMenu.inflate(R.menu.dialtacts_options);
670 mOverflowMenu.setOnMenuItemClickListener(this);
671 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700672 }
673
Ihab Awad526c0b82014-02-27 12:55:36 -0800674 mFakeActionBar = findViewById(R.id.fake_action_bar);
675
Yorke Leec3766332013-07-31 11:13:16 -0700676 mCallHistoryButton = findViewById(R.id.call_history_button);
677 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
678 mCallHistoryButton.setOnClickListener(this);
679
Ihab Awad526c0b82014-02-27 12:55:36 -0800680 mDialButton = findViewById(R.id.dial_button);
681 mDialButton.setOnClickListener(this);
682 mDialButton.setOnLongClickListener(this);
683
Yorke Leec3766332013-07-31 11:13:16 -0700684 mDialpadButton = findViewById(R.id.dialpad_button);
685 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
686 mDialpadButton.setOnClickListener(this);
687 }
688
Chiao Cheng94b10b52012-08-17 16:59:12 -0700689 private void fixIntent(Intent intent) {
690 // This should be cleaned up: the call key used to send an Intent
691 // that just said to go to the recent calls list. It now sends this
692 // abstract action, but this class hasn't been rewritten to deal with it.
693 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
694 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
695 intent.putExtra("call_key", true);
696 setIntent(intent);
697 }
698 }
699
Chiao Cheng94b10b52012-08-17 16:59:12 -0700700 /**
701 * Returns true if the intent is due to hitting the green send key (hardware call button:
702 * KEYCODE_CALL) while in a call.
703 *
704 * @param intent the intent that launched this activity
705 * @param recentCallsRequest true if the intent is requesting to view recent calls
706 * @return true if the intent is due to hitting the green send key while in a call
707 */
Yorke Leec3766332013-07-31 11:13:16 -0700708 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700709 // If there is a call in progress go to the call screen
710 if (recentCallsRequest) {
711 final boolean callKey = intent.getBooleanExtra("call_key", false);
712
713 try {
714 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
715 if (callKey && phone != null && phone.showCallScreen()) {
716 return true;
717 }
718 } catch (RemoteException e) {
719 Log.e(TAG, "Failed to handle send while in call", e);
720 }
721 }
722
723 return false;
724 }
725
726 /**
727 * Sets the current tab based on the intent's request type
728 *
729 * @param intent Intent that contains information about which tab should be selected
730 */
Yorke Leec3766332013-07-31 11:13:16 -0700731 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700732 // If we got here by hitting send and we're in call forward along to the in-call activity
733 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
734 getContentResolver()));
735 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
736 finish();
737 return;
738 }
739
Yorke Lee669b6082013-09-17 15:43:38 -0700740 if (mDialpadFragment != null) {
741 final boolean phoneIsInUse = phoneIsInUse();
742 if (phoneIsInUse || isDialIntent(intent)) {
743 mDialpadFragment.setStartedFromNewIntent(true);
744 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
745 mInCallDialpadUp = true;
746 }
747 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700748 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700749 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700750 }
751
752 @Override
753 public void onNewIntent(Intent newIntent) {
754 setIntent(newIntent);
755 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700756 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700757 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700758
Chiao Cheng94b10b52012-08-17 16:59:12 -0700759 invalidateOptionsMenu();
760 }
761
762 /** Returns true if the given intent contains a phone number to populate the dialer with */
763 private boolean isDialIntent(Intent intent) {
764 final String action = intent.getAction();
765 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
766 return true;
767 }
768 if (Intent.ACTION_VIEW.equals(action)) {
769 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700770 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700771 return true;
772 }
773 }
774 return false;
775 }
776
777 /**
778 * Returns an appropriate call origin for this Activity. May return null when no call origin
779 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
780 * for remembering the tab in which the user made a phone call, so the external app's DIAL
781 * request should not be counted.)
782 */
783 public String getCallOrigin() {
784 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
785 }
786
787 /**
788 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
789 * This text originally came from a FILTER_CONTACTS_ACTION intent received
790 * by this activity. The stored text will then be cleared after after this
791 * method returns.
792 *
793 * @return The stored filter text
794 */
795 public String getAndClearFilterText() {
796 String filterText = mFilterText;
797 mFilterText = null;
798 return filterText;
799 }
800
801 /**
802 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
803 * This is so child activities can check if they are supposed to display a filter.
804 *
805 * @param intent The intent received in {@link #onNewIntent(Intent)}
806 */
807 private void setupFilterText(Intent intent) {
808 // If the intent was relaunched from history, don't apply the filter text.
809 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
810 return;
811 }
812 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
813 if (filter != null && filter.length() > 0) {
814 mFilterText = filter;
815 }
816 }
817
Yorke Lee8898cd02013-08-08 10:24:27 -0700818 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
819 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700820 @Override
821 public void onContactSelected(Uri contactUri) {
822 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700823 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700824 }
825
826 @Override
827 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700828 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700829 startActivity(intent);
830 }
831 };
832
Yorke Leec3766332013-07-31 11:13:16 -0700833 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
834 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
835 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700836
837 @Override
838 public void startSearch(String initialQuery, boolean selectInitialQuery,
839 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700840 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700841 if (mInSearchUi) {
842 if (mSearchView.hasFocus()) {
843 showInputMethod(mSearchView.findFocus());
844 } else {
845 mSearchView.requestFocus();
846 }
847 } else {
848 enterSearchUi();
849 }
850 } else {
851 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
852 }
Yorke Leec3766332013-07-31 11:13:16 -0700853 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700854
855 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700856 final InputMethodManager imm = (InputMethodManager) getSystemService(
857 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700858 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700859 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700860 }
861 }
862
863 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700864 final InputMethodManager imm = (InputMethodManager) getSystemService(
865 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700866 if (imm != null && view != null) {
867 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
868 }
869 }
870
871 /**
Yorke Leec3766332013-07-31 11:13:16 -0700872 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700873 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700874 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700875 if (getFragmentManager().isDestroyed()) {
876 // Weird race condition where fragment is doing work after the activity is destroyed
877 // due to talkback being on (b/10209937). Just return since we can't do any
878 // constructive here.
879 return;
880 }
881
Yorke Leeef2b7382013-08-09 17:39:25 -0700882 if (DEBUG) {
883 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
884 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700885
Yorke Leec3766332013-07-31 11:13:16 -0700886 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
887 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700888
Yorke Leeef2b7382013-08-09 17:39:25 -0700889 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700890 if (mInDialpadSearch) {
891 transaction.remove(mSmartDialSearchFragment);
892 } else if (mInRegularSearch) {
893 transaction.remove(mRegularSearchFragment);
894 } else {
895 transaction.remove(mPhoneFavoriteFragment);
896 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700897
Yorke Leeb207f8a2013-08-29 18:51:06 -0700898 final String tag;
899 if (smartDialSearch) {
900 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
901 } else {
902 tag = TAG_REGULAR_SEARCH_FRAGMENT;
903 }
904 mInDialpadSearch = smartDialSearch;
905 mInRegularSearch = !smartDialSearch;
906
Yorke Leeef2b7382013-08-09 17:39:25 -0700907 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
908 if (fragment == null) {
909 if (smartDialSearch) {
910 fragment = new SmartDialSearchFragment();
911 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700912 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700913 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700914 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700915 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700916 transaction.addToBackStack(null);
917 fragment.setQueryString(query, false);
918 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700919 }
920
921 /**
Yorke Leec3766332013-07-31 11:13:16 -0700922 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700923 */
Yorke Leec3766332013-07-31 11:13:16 -0700924 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700925 // See related bug in enterSearchUI();
926 if (getFragmentManager().isDestroyed()) {
927 return;
928 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700929 // Go all the way back to the favorites fragment, regardless of how many times we
930 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700931 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
932 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700933 }
934
935 /** Returns an Intent to launch Call Settings screen */
936 public static Intent getCallSettingsIntent() {
937 final Intent intent = new Intent(Intent.ACTION_MAIN);
938 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
939 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
940 return intent;
941 }
942
943 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700944 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700945 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700946 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700947 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700948 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700949 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700950 } else {
951 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700952 }
Yorke Leec3766332013-07-31 11:13:16 -0700953 }
954
955 @Override
956 public void onDialpadQueryChanged(String query) {
957 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
958 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
959 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700960 if (DEBUG) {
961 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
962 }
963 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
964 // This callback can happen if the dialpad fragment is recreated because of
965 // activity destruction. In that case, don't update the search view because
966 // that would bring the user back to the search fragment regardless of the
967 // previous state of the application. Instead, just return here and let the
968 // fragment manager correctly figure out whatever fragment was last displayed.
969 return;
970 }
Yorke Leec3766332013-07-31 11:13:16 -0700971 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700972 }
973 }
Yorke Leec3766332013-07-31 11:13:16 -0700974
975 @Override
976 public void onListFragmentScrollStateChange(int scrollState) {
977 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700978 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700979 hideInputMethod(getCurrentFocus());
980 }
981 }
982
983 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800984 public void setDialButtonEnabled(boolean enabled) {
985 mDialButton.setEnabled(enabled);
986 }
987
988 @Override
989 public void setDialButtonContainerVisible(boolean visible) {
990 mFakeActionBar.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700991 }
992
993 private boolean phoneIsInUse() {
994 final TelephonyManager tm = (TelephonyManager) getSystemService(
995 Context.TELEPHONY_SERVICE);
996 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
997 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700998
999 @Override
1000 public void onShowAllContacts() {
1001 final Intent intent = new Intent(this, AllContactsActivity.class);
1002 startActivity(intent);
1003 }
Yorke Leee1424812013-09-04 18:24:48 -07001004
1005 public static Intent getAddNumberToContactIntent(CharSequence text) {
1006 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1007 intent.putExtra(Intents.Insert.PHONE, text);
1008 intent.setType(Contacts.CONTENT_ITEM_TYPE);
1009 return intent;
1010 }
1011
Yorke Leeda0f9042013-12-05 14:25:51 -08001012 private boolean canIntentBeHandled(Intent intent) {
1013 final PackageManager packageManager = getPackageManager();
1014 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1015 PackageManager.MATCH_DEFAULT_ONLY);
1016 return resolveInfo != null && resolveInfo.size() > 0;
1017 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001018
1019 @Override
1020 public void onDragStarted(int itemIndex, int x, int y, PhoneFavoriteTileView view) {
1021 crossfadeViews(mRemoveViewContainer, mSearchViewContainer, FADE_ANIMATION_DURATION);
1022 }
1023
1024 @Override
1025 public void onDragHovered(int itemIndex, int x, int y) {}
1026
1027 @Override
1028 public void onDragFinished(int x, int y) {
1029 crossfadeViews(mSearchViewContainer, mRemoveViewContainer, FADE_ANIMATION_DURATION);
1030 }
1031
1032 @Override
1033 public void onDroppedOnRemove() {}
1034
1035 /**
1036 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
1037 * once it has been attached to the activity.
1038 */
1039 @Override
1040 public void setDragDropController(DragDropController dragController) {
1041 mRemoveViewContainer.setDragDropController(dragController);
1042 }
1043
1044 /**
1045 * Crossfades two views so that the first one appears while the other one is fading
1046 * out of view.
1047 */
1048 private void crossfadeViews(final View fadeIn, final View fadeOut, int duration) {
1049 fadeOut.animate().alpha(0).setDuration(duration)
1050 .setListener(new AnimatorListenerAdapter() {
1051 @Override
1052 public void onAnimationEnd(Animator animation) {
1053 fadeOut.setVisibility(View.GONE);
1054 }
1055 });
1056
1057 fadeIn.setVisibility(View.VISIBLE);
1058 fadeIn.setAlpha(0);
1059 fadeIn.animate().alpha(1).setDuration(FADE_ANIMATION_DURATION)
1060 .setListener(null);
1061 }
Ihab Awad526c0b82014-02-27 12:55:36 -08001062
1063 private boolean shouldShowOnscreenDialButton() {
1064 return getResources().getBoolean(R.bool.config_show_onscreen_dial_button);
1065 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001066}