blob: c51035537e8cc6eb223b34a48a2b14bf1c75a140 [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;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070049import android.widget.AbsListView.OnScrollListener;
50import android.widget.EditText;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080052import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070054import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070055import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070056import com.android.contacts.common.dialog.ClearFrequentsDialog;
57import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080058import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070059import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070060import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070061import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070062import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070063import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070064import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070065import com.android.dialer.list.AllContactsActivity;
Yorke Lee3cefcc62014-01-16 11:26:46 -080066import com.android.dialer.list.DragDropController;
67import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070068import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070069import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080070import com.android.dialer.list.PhoneFavoriteTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070071import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080072import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070073import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070074import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070075import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070076import com.android.internal.telephony.ITelephony;
77
Yorke Leec3766332013-07-31 11:13:16 -070078import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080079import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070080
Chiao Cheng94b10b52012-08-17 16:59:12 -070081/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070082 * The dialer tab's title is 'phone', a more common name (see strings.xml).
83 */
Yorke Leec3766332013-07-31 11:13:16 -070084public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070085 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070086 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080087 DialpadFragment.HostInterface,
Yorke Lee3cefcc62014-01-16 11:26:46 -080088 PhoneFavoriteFragment.OnShowAllContactsListener,
89 PhoneFavoriteFragment.HostInterface,
Ihab Awad526c0b82014-02-27 12:55:36 -080090 OnDragDropListener, View.OnLongClickListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070091 private static final String TAG = "DialtactsActivity";
92
Ihab Awad526c0b82014-02-27 12:55:36 -080093 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -070094
Yorke Leef74011e2013-08-02 11:30:30 -070095 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
96
Chiao Cheng94b10b52012-08-17 16:59:12 -070097 /** Used to open Call Setting */
98 private static final String PHONE_PACKAGE = "com.android.phone";
99 private static final String CALL_SETTINGS_CLASS_NAME =
100 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101 /** @see #getCallOrigin() */
102 private static final String CALL_ORIGIN_DIALTACTS =
103 "com.android.dialer.DialtactsActivity";
104
Yorke Leeb207f8a2013-08-29 18:51:06 -0700105 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
106 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700107 private static final String KEY_SEARCH_QUERY = "search_query";
108 private static final String KEY_FIRST_LAUNCH = "first_launch";
109
Yorke Leec3766332013-07-31 11:13:16 -0700110 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
111 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
112 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
113 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700114
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 /**
116 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
117 */
118 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
119
Yorke Leec3766332013-07-31 11:13:16 -0700120 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121
Yorke Lee3cefcc62014-01-16 11:26:46 -0800122 private static final int FADE_ANIMATION_DURATION = 200;
123
Chiao Cheng94b10b52012-08-17 16:59:12 -0700124 private String mFilterText;
125
Yorke Leec3766332013-07-31 11:13:16 -0700126 /**
127 * The main fragment displaying the user's favorites and frequent contacts
128 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700129 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700130
131 /**
Yorke Leec3766332013-07-31 11:13:16 -0700132 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700133 */
Yorke Leef74011e2013-08-02 11:30:30 -0700134 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700135
136 /**
137 * Fragment for searching phone numbers using the alphanumeric keyboard.
138 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700139 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700140
141 /**
142 * Fragment for searching phone numbers using the dialpad.
143 */
144 private SmartDialSearchFragment mSmartDialSearchFragment;
145
Yorke Leec3766332013-07-31 11:13:16 -0700146 private View mMenuButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800147 private View mFakeActionBar;
Yorke Leec3766332013-07-31 11:13:16 -0700148 private View mCallHistoryButton;
149 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800150 private View mDialButton;
Yorke Lee36c54132013-09-25 16:17:06 -0700151 private PopupMenu mOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700152
153 // Padding view used to shift the fragments up when the dialpad is shown.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700154 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700155
Yorke Leeb207f8a2013-08-29 18:51:06 -0700156 private boolean mInDialpadSearch;
157 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700158 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700159
Yorke Leeef2b7382013-08-09 17:39:25 -0700160 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700161 * True if the dialpad is only temporarily showing due to being in call
162 */
163 private boolean mInCallDialpadUp;
164
165 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700166 * True when this activity has been launched for the first time.
167 */
Yorke Lee98702de2013-08-06 12:03:32 -0700168 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700169 private View mSearchViewContainer;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800170 private RemoveView mRemoveViewContainer;
Yorke Lee94f49042014-03-03 14:03:08 -0800171 // This view points to the Framelayout that houses both the search view and remove view
172 // containers.
173 private View mSearchAndRemoveViewContainer;
Yorke Leec3766332013-07-31 11:13:16 -0700174 private View mSearchViewCloseButton;
175 private View mVoiceSearchButton;
176 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700177
Yorke Leeef2b7382013-08-09 17:39:25 -0700178 private String mSearchQuery;
179
Yorke Leefce269a2013-08-12 11:56:04 -0700180 private DialerDatabaseHelper mDialerDatabaseHelper;
181
Yorke Lee36c54132013-09-25 16:17:06 -0700182 private class OverflowPopupMenu extends PopupMenu {
183 public OverflowPopupMenu(Context context, View anchor) {
184 super(context, anchor);
185 }
186
187 @Override
188 public void show() {
189 final Menu menu = getMenu();
190 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
191 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
192 super.show();
193 }
194 }
195
Chiao Cheng94b10b52012-08-17 16:59:12 -0700196 /**
197 * Listener used when one of phone numbers in search UI is selected. This will initiate a
198 * phone call using the phone number.
199 */
200 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
201 new OnPhoneNumberPickerActionListener() {
202 @Override
203 public void onPickPhoneNumberAction(Uri dataUri) {
204 // Specify call-origin so that users will see the previous tab instead of
205 // CallLog screen (search UI will be automatically exited).
206 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700207 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700208 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700209 }
210
211 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700212 public void onCallNumberDirectly(String phoneNumber) {
213 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
214 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700215 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700216 }
217
218 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700219 public void onShortcutIntentCreated(Intent intent) {
220 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
221 }
222
223 @Override
224 public void onHomeInActionBarSelected() {
225 exitSearchUi();
226 }
227 };
228
229 /**
230 * Listener used to send search queries to the phone search fragment.
231 */
Yorke Leec3766332013-07-31 11:13:16 -0700232 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
233 @Override
234 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
235 }
236
237 @Override
238 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700239 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700240 if (newText.equals(mSearchQuery)) {
241 // If the query hasn't changed (perhaps due to activity being destroyed
242 // and restored, or user launching the same DIAL intent twice), then there is
243 // no need to do anything here.
244 return;
245 }
246 mSearchQuery = newText;
247 if (DEBUG) {
248 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
249 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700250 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700251
Yorke Leec3766332013-07-31 11:13:16 -0700252 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700253 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700254 exitSearchUi();
255 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700256 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700257 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700258 } else if (!TextUtils.isEmpty(newText)) {
259 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
260 (!dialpadSearch && mInRegularSearch);
261 if (!sameSearchMode) {
262 // call enterSearchUi only if we are switching search modes, or entering
263 // search ui for the first time
264 enterSearchUi(dialpadSearch, newText);
265 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700266
Yorke Leeb207f8a2013-08-29 18:51:06 -0700267 if (dialpadSearch && mSmartDialSearchFragment != null) {
268 mSmartDialSearchFragment.setQueryString(newText, false);
269 } else if (mRegularSearchFragment != null) {
270 mRegularSearchFragment.setQueryString(newText, false);
271 }
272 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700273 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700274 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275 }
Yorke Leec3766332013-07-31 11:13:16 -0700276 }
277
278 @Override
279 public void afterTextChanged(Editable s) {
280 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700281 };
282
Yorke Leec3766332013-07-31 11:13:16 -0700283 private boolean isDialpadShowing() {
284 return mDialpadFragment != null && mDialpadFragment.isVisible();
285 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700286
287 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700288 protected void onCreate(Bundle savedInstanceState) {
289 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700290 mFirstLaunch = true;
291
Chiao Cheng94b10b52012-08-17 16:59:12 -0700292 final Intent intent = getIntent();
293 fixIntent(intent);
294
Yorke Lee8898cd02013-08-08 10:24:27 -0700295 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700296
Yorke Leec3766332013-07-31 11:13:16 -0700297 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700298
Yorke Leeef2b7382013-08-09 17:39:25 -0700299 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
300 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700301 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800302 getFragmentManager().beginTransaction()
303 .add(R.id.dialtacts_frame, new PhoneFavoriteFragment(), TAG_FAVORITES_FRAGMENT)
304 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
305 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700306 } else {
307 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700308 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
309 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700310 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700311 }
312
Yorke Leeb207f8a2013-08-29 18:51:06 -0700313 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Ihab Awad526c0b82014-02-27 12:55:36 -0800314
Yorke Lee3cefcc62014-01-16 11:26:46 -0800315 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Lee94f49042014-03-03 14:03:08 -0800316 mSearchAndRemoveViewContainer = (View) findViewById(R.id.search_and_remove_view_container);
Yorke Leec3766332013-07-31 11:13:16 -0700317 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700318
Chiao Cheng94b10b52012-08-17 16:59:12 -0700319 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700320 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700321 setupFilterText(intent);
322 }
Yorke Leefce269a2013-08-12 11:56:04 -0700323
Ihab Awad526c0b82014-02-27 12:55:36 -0800324 hideDialpadFragment(false, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700325 setupFakeActionBarItems();
326
Yorke Lee0baa98b2013-08-22 10:55:16 -0700327 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700328 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700329 }
330
331 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700332 protected void onResume() {
333 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700334 if (mFirstLaunch) {
335 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700336 } else if (!phoneIsInUse() && mInCallDialpadUp) {
337 hideDialpadFragment(false, true);
338 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700339 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800340 prepareVoiceSearchButton();
Yorke Lee98702de2013-08-06 12:03:32 -0700341 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700342 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700343 }
344
345 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700346 protected void onPause() {
347 if (mClearSearchOnPause) {
348 hideDialpadAndSearchUi();
349 mClearSearchOnPause = false;
350 }
351 super.onPause();
352 }
353
354 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700355 protected void onSaveInstanceState(Bundle outState) {
356 super.onSaveInstanceState(outState);
357 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700358 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
359 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700360 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
361 }
362
363 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700364 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700365 if (fragment instanceof DialpadFragment) {
366 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700367 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700368 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700369 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700370 } else if (fragment instanceof SmartDialSearchFragment) {
371 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700372 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
373 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700374 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700375 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700376 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
377 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700378 } else if (fragment instanceof PhoneFavoriteFragment) {
379 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700380 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700381 }
Yorke Leec3766332013-07-31 11:13:16 -0700382 }
383
384 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700385 public boolean onMenuItemClick(MenuItem item) {
386 switch (item.getItemId()) {
387 case R.id.menu_import_export:
388 // We hard-code the "contactsAreAvailable" argument because doing it properly would
389 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
390 // now in Dialtacts for (potential) performance reasons. Compare with how it is
391 // done in {@link PeopleActivity}.
392 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700393 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700394 return true;
395 case R.id.menu_clear_frequents:
396 ClearFrequentsDialog.show(getFragmentManager());
397 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700398 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700399 try {
400 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
401 } catch (ActivityNotFoundException e) {
402 Toast toast = Toast.makeText(this,
403 R.string.add_contact_not_available,
404 Toast.LENGTH_SHORT);
405 toast.show();
406 }
407 return true;
408 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700409 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700410 return true;
411 case R.id.menu_all_contacts:
412 onShowAllContacts();
413 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700414 }
415 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700416 }
417
Alon Albertb453e5b2013-09-10 15:54:23 -0700418 protected void handleMenuSettings() {
419 openTelephonySetting(this);
420 }
421
422 public static void openTelephonySetting(Activity activity) {
423 final Intent settingsIntent = getCallSettingsIntent();
424 activity.startActivity(settingsIntent);
425 }
426
Chiao Cheng94b10b52012-08-17 16:59:12 -0700427 @Override
428 public void onClick(View view) {
429 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700430 case R.id.overflow_menu: {
Ihab Awad526c0b82014-02-27 12:55:36 -0800431 if (isDialpadShowing()) {
432 mDialpadFragment.optionsMenuInvoked(view);
433 } else {
434 mOverflowMenu.show();
435 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700436 break;
437 }
Yorke Leec3766332013-07-31 11:13:16 -0700438 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700439 // Reset the boolean flag that tracks whether the dialpad was up because
440 // we were in call. Regardless of whether it was true before, we want to
441 // show the dialpad because the user has explicitly clicked the dialpad
442 // button.
443 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700444 showDialpadFragment(true);
445 break;
Ihab Awad526c0b82014-02-27 12:55:36 -0800446 case R.id.dial_button:
447 // Dial button was pressed; tell the Dialpad fragment
448 mDialpadFragment.dialButtonPressed();
449 break;
Yorke Leec3766332013-07-31 11:13:16 -0700450 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700451 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
452 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700453 final Intent intent = new Intent(this, CallLogActivity.class);
454 startActivity(intent);
455 break;
456 case R.id.search_close_button:
457 // Clear the search field
458 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700459 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700460 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700461 }
462 break;
Yorke Leec3766332013-07-31 11:13:16 -0700463 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800464 try {
465 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
466 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
467 } catch (ActivityNotFoundException e) {
468 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
469 Toast.LENGTH_SHORT).show();
470 }
Yorke Leec3766332013-07-31 11:13:16 -0700471 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700472 default: {
473 Log.wtf(TAG, "Unexpected onClick event from " + view);
474 break;
475 }
476 }
477 }
478
Yorke Leec3766332013-07-31 11:13:16 -0700479 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800480 public boolean onLongClick(View view) {
481 switch (view.getId()) {
482 case R.id.dial_button: {
483 // Dial button was pressed; tell the Dialpad fragment
484 mDialpadFragment.dialButtonPressed();
485 return true; // Consume the event
486 }
487 default: {
488 Log.wtf(TAG, "Unexpected onClick event from " + view);
489 break;
490 }
491 }
492 return false;
493 }
494
495 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700496 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
497 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
498 if (resultCode == RESULT_OK) {
499 final ArrayList<String> matches = data.getStringArrayListExtra(
500 RecognizerIntent.EXTRA_RESULTS);
501 if (matches.size() > 0) {
502 final String match = matches.get(0);
503 mSearchView.setText(match);
504 } else {
505 Log.e(TAG, "Voice search - nothing heard");
506 }
507 } else {
508 Log.e(TAG, "Voice search failed");
509 }
510 }
511 super.onActivityResult(requestCode, resultCode, data);
512 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700513
Yorke Leec3766332013-07-31 11:13:16 -0700514 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700515 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700516 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700517 if (animate) {
518 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700519 } else {
520 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700521 }
522 ft.show(mDialpadFragment);
523 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800524 mDialButton.setVisibility(shouldShowOnscreenDialButton() ? View.VISIBLE : View.GONE);
525 mDialpadButton.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700526 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700527
Yorke Leeca195042013-09-25 16:29:38 -0700528 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700529 if (mDialpadFragment == null) return;
530 if (clearDialpad) {
531 mDialpadFragment.clearDialpad();
532 }
533 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700534 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700535 final FragmentTransaction ft = getFragmentManager().beginTransaction();
536 if (animate) {
537 ft.setCustomAnimations(0, R.anim.slide_out);
538 }
539 ft.hide(mDialpadFragment);
540 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800541 mDialButton.setVisibility(View.GONE);
542 mDialpadButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700543 }
544
Chiao Cheng94b10b52012-08-17 16:59:12 -0700545 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700546 mSearchViewContainer = findViewById(R.id.search_view_container);
547 mSearchViewCloseButton = findViewById(R.id.search_close_button);
548 mSearchViewCloseButton.setOnClickListener(this);
Yorke Leeda0f9042013-12-05 14:25:51 -0800549
Yorke Leec3766332013-07-31 11:13:16 -0700550 mSearchView = (EditText) findViewById(R.id.search_view);
551 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Tyler Gunna8ff9752014-02-24 11:57:57 -0800552 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
Yorke Leeda0f9042013-12-05 14:25:51 -0800553
554 prepareVoiceSearchButton();
555 }
556
557 private void prepareVoiceSearchButton() {
558 mVoiceSearchButton = findViewById(R.id.voice_search_button);
559 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
560 if (canIntentBeHandled(voiceIntent)) {
561 mVoiceSearchButton.setVisibility(View.VISIBLE);
562 mVoiceSearchButton.setOnClickListener(this);
563 } else {
564 mVoiceSearchButton.setVisibility(View.GONE);
565 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700566 }
567
Yorke Leec3766332013-07-31 11:13:16 -0700568 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
569 @Override
570 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800571 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700572 }
573 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700574
Yorke Leeb207f8a2013-08-29 18:51:06 -0700575 private boolean getInSearchUi() {
576 return mInDialpadSearch || mInRegularSearch;
577 }
578
579 private void setNotInSearchUi() {
580 mInDialpadSearch = false;
581 mInRegularSearch = false;
582 }
583
Yorke Lee311969c2013-08-26 06:31:11 -0700584 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700585 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700586 hideDialpadFragment(false, true);
587 }
588
Yorke Leec3766332013-07-31 11:13:16 -0700589 public void hideSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800590 final int height = mSearchAndRemoveViewContainer.getHeight();
591 mSearchAndRemoveViewContainer.animate().cancel();
592 mSearchAndRemoveViewContainer.setAlpha(1);
593 mSearchAndRemoveViewContainer.setTranslationY(0);
594 mSearchAndRemoveViewContainer.animate().withLayer().alpha(0)
595 .translationY(-height).setDuration(200)
596 .setListener(mHideListener);
Christine Chen9c1e0652013-05-23 15:40:19 -0700597
Yorke Lee94f49042014-03-03 14:03:08 -0800598 mFragmentsFrame.animate().withLayer()
599 .translationY(-height).setDuration(200).setListener(
600 new AnimatorListenerAdapter() {
601 @Override
602 public void onAnimationEnd(Animator animation) {
603 mFragmentsFrame.setTranslationY(0);
604 }
605 });
Yorke Leec3766332013-07-31 11:13:16 -0700606 }
607
608 public void showSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800609 final int height = mSearchAndRemoveViewContainer.getHeight();
610 mSearchAndRemoveViewContainer.animate().cancel();
611 mSearchAndRemoveViewContainer.setAlpha(0);
612 mSearchAndRemoveViewContainer.setTranslationY(-height);
613 mSearchAndRemoveViewContainer.animate().withLayer().alpha(1).translationY(0)
614 .setDuration(200).setListener(new AnimatorListenerAdapter() {
615 @Override
616 public void onAnimationStart(Animator animation) {
617 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
618 }
619 });
Yorke Leec3766332013-07-31 11:13:16 -0700620
Yorke Lee94f49042014-03-03 14:03:08 -0800621 mFragmentsFrame.setTranslationY(-height);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700622 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700623 .setListener(
624 new AnimatorListenerAdapter() {
625 @Override
626 public void onAnimationStart(Animator animation) {
Yorke Leec3766332013-07-31 11:13:16 -0700627 }
628 });
629 }
630
631
Yorke Leeb207f8a2013-08-29 18:51:06 -0700632 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700633 mMenuButton = findViewById(R.id.overflow_menu);
634 if (mMenuButton != null) {
635 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700636
637 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
638 final Menu menu = mOverflowMenu.getMenu();
639 mOverflowMenu.inflate(R.menu.dialtacts_options);
640 mOverflowMenu.setOnMenuItemClickListener(this);
641 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700642 }
643
Ihab Awad526c0b82014-02-27 12:55:36 -0800644 mFakeActionBar = findViewById(R.id.fake_action_bar);
645
Yorke Leec3766332013-07-31 11:13:16 -0700646 mCallHistoryButton = findViewById(R.id.call_history_button);
647 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
648 mCallHistoryButton.setOnClickListener(this);
649
Ihab Awad526c0b82014-02-27 12:55:36 -0800650 mDialButton = findViewById(R.id.dial_button);
651 mDialButton.setOnClickListener(this);
652 mDialButton.setOnLongClickListener(this);
653
Yorke Leec3766332013-07-31 11:13:16 -0700654 mDialpadButton = findViewById(R.id.dialpad_button);
655 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
656 mDialpadButton.setOnClickListener(this);
657 }
658
Chiao Cheng94b10b52012-08-17 16:59:12 -0700659 private void fixIntent(Intent intent) {
660 // This should be cleaned up: the call key used to send an Intent
661 // that just said to go to the recent calls list. It now sends this
662 // abstract action, but this class hasn't been rewritten to deal with it.
663 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
664 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
665 intent.putExtra("call_key", true);
666 setIntent(intent);
667 }
668 }
669
Chiao Cheng94b10b52012-08-17 16:59:12 -0700670 /**
671 * Returns true if the intent is due to hitting the green send key (hardware call button:
672 * KEYCODE_CALL) while in a call.
673 *
674 * @param intent the intent that launched this activity
675 * @param recentCallsRequest true if the intent is requesting to view recent calls
676 * @return true if the intent is due to hitting the green send key while in a call
677 */
Yorke Leec3766332013-07-31 11:13:16 -0700678 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700679 // If there is a call in progress go to the call screen
680 if (recentCallsRequest) {
681 final boolean callKey = intent.getBooleanExtra("call_key", false);
682
683 try {
684 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
685 if (callKey && phone != null && phone.showCallScreen()) {
686 return true;
687 }
688 } catch (RemoteException e) {
689 Log.e(TAG, "Failed to handle send while in call", e);
690 }
691 }
692
693 return false;
694 }
695
696 /**
697 * Sets the current tab based on the intent's request type
698 *
699 * @param intent Intent that contains information about which tab should be selected
700 */
Yorke Leec3766332013-07-31 11:13:16 -0700701 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700702 // If we got here by hitting send and we're in call forward along to the in-call activity
703 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
704 getContentResolver()));
705 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
706 finish();
707 return;
708 }
709
Yorke Lee669b6082013-09-17 15:43:38 -0700710 if (mDialpadFragment != null) {
711 final boolean phoneIsInUse = phoneIsInUse();
712 if (phoneIsInUse || isDialIntent(intent)) {
713 mDialpadFragment.setStartedFromNewIntent(true);
714 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
715 mInCallDialpadUp = true;
716 }
717 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700718 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700720 }
721
722 @Override
723 public void onNewIntent(Intent newIntent) {
724 setIntent(newIntent);
725 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700726 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700727 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700728
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729 invalidateOptionsMenu();
730 }
731
732 /** Returns true if the given intent contains a phone number to populate the dialer with */
733 private boolean isDialIntent(Intent intent) {
734 final String action = intent.getAction();
735 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
736 return true;
737 }
738 if (Intent.ACTION_VIEW.equals(action)) {
739 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700740 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700741 return true;
742 }
743 }
744 return false;
745 }
746
747 /**
748 * Returns an appropriate call origin for this Activity. May return null when no call origin
749 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
750 * for remembering the tab in which the user made a phone call, so the external app's DIAL
751 * request should not be counted.)
752 */
753 public String getCallOrigin() {
754 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
755 }
756
757 /**
758 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
759 * This text originally came from a FILTER_CONTACTS_ACTION intent received
760 * by this activity. The stored text will then be cleared after after this
761 * method returns.
762 *
763 * @return The stored filter text
764 */
765 public String getAndClearFilterText() {
766 String filterText = mFilterText;
767 mFilterText = null;
768 return filterText;
769 }
770
771 /**
772 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
773 * This is so child activities can check if they are supposed to display a filter.
774 *
775 * @param intent The intent received in {@link #onNewIntent(Intent)}
776 */
777 private void setupFilterText(Intent intent) {
778 // If the intent was relaunched from history, don't apply the filter text.
779 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
780 return;
781 }
782 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
783 if (filter != null && filter.length() > 0) {
784 mFilterText = filter;
785 }
786 }
787
Yorke Lee8898cd02013-08-08 10:24:27 -0700788 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
789 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700790 @Override
791 public void onContactSelected(Uri contactUri) {
792 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700793 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700794 }
795
796 @Override
797 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700798 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700799 startActivity(intent);
800 }
801 };
802
Yorke Leec3766332013-07-31 11:13:16 -0700803 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
804 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
805 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700806
807 @Override
808 public void startSearch(String initialQuery, boolean selectInitialQuery,
809 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700810 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700811 if (mInSearchUi) {
812 if (mSearchView.hasFocus()) {
813 showInputMethod(mSearchView.findFocus());
814 } else {
815 mSearchView.requestFocus();
816 }
817 } else {
818 enterSearchUi();
819 }
820 } else {
821 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
822 }
Yorke Leec3766332013-07-31 11:13:16 -0700823 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700824
825 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700826 final InputMethodManager imm = (InputMethodManager) getSystemService(
827 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700828 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700829 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700830 }
831 }
832
833 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700834 final InputMethodManager imm = (InputMethodManager) getSystemService(
835 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700836 if (imm != null && view != null) {
837 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
838 }
839 }
840
841 /**
Yorke Leec3766332013-07-31 11:13:16 -0700842 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700843 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700844 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700845 if (getFragmentManager().isDestroyed()) {
846 // Weird race condition where fragment is doing work after the activity is destroyed
847 // due to talkback being on (b/10209937). Just return since we can't do any
848 // constructive here.
849 return;
850 }
851
Yorke Leeef2b7382013-08-09 17:39:25 -0700852 if (DEBUG) {
853 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
854 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700855
Yorke Leec3766332013-07-31 11:13:16 -0700856 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
857 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700858
Yorke Leeef2b7382013-08-09 17:39:25 -0700859 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700860 if (mInDialpadSearch) {
861 transaction.remove(mSmartDialSearchFragment);
862 } else if (mInRegularSearch) {
863 transaction.remove(mRegularSearchFragment);
864 } else {
865 transaction.remove(mPhoneFavoriteFragment);
866 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700867
Yorke Leeb207f8a2013-08-29 18:51:06 -0700868 final String tag;
869 if (smartDialSearch) {
870 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
871 } else {
872 tag = TAG_REGULAR_SEARCH_FRAGMENT;
873 }
874 mInDialpadSearch = smartDialSearch;
875 mInRegularSearch = !smartDialSearch;
876
Yorke Leeef2b7382013-08-09 17:39:25 -0700877 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
878 if (fragment == null) {
879 if (smartDialSearch) {
880 fragment = new SmartDialSearchFragment();
881 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700882 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700883 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700884 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700885 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700886 transaction.addToBackStack(null);
887 fragment.setQueryString(query, false);
888 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700889 }
890
891 /**
Yorke Leec3766332013-07-31 11:13:16 -0700892 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700893 */
Yorke Leec3766332013-07-31 11:13:16 -0700894 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700895 // See related bug in enterSearchUI();
896 if (getFragmentManager().isDestroyed()) {
897 return;
898 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700899 // Go all the way back to the favorites fragment, regardless of how many times we
900 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700901 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
902 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700903 }
904
905 /** Returns an Intent to launch Call Settings screen */
906 public static Intent getCallSettingsIntent() {
907 final Intent intent = new Intent(Intent.ACTION_MAIN);
908 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
909 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
910 return intent;
911 }
912
913 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700914 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700915 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700916 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700917 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700918 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700919 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700920 } else {
921 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700922 }
Yorke Leec3766332013-07-31 11:13:16 -0700923 }
924
925 @Override
926 public void onDialpadQueryChanged(String query) {
927 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
928 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
929 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700930 if (DEBUG) {
931 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
932 }
933 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
934 // This callback can happen if the dialpad fragment is recreated because of
935 // activity destruction. In that case, don't update the search view because
936 // that would bring the user back to the search fragment regardless of the
937 // previous state of the application. Instead, just return here and let the
938 // fragment manager correctly figure out whatever fragment was last displayed.
939 return;
940 }
Yorke Leec3766332013-07-31 11:13:16 -0700941 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700942 }
943 }
Yorke Leec3766332013-07-31 11:13:16 -0700944
945 @Override
946 public void onListFragmentScrollStateChange(int scrollState) {
947 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700948 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700949 hideInputMethod(getCurrentFocus());
950 }
951 }
952
953 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800954 public void setDialButtonEnabled(boolean enabled) {
955 mDialButton.setEnabled(enabled);
956 }
957
958 @Override
959 public void setDialButtonContainerVisible(boolean visible) {
960 mFakeActionBar.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700961 }
962
963 private boolean phoneIsInUse() {
964 final TelephonyManager tm = (TelephonyManager) getSystemService(
965 Context.TELEPHONY_SERVICE);
966 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
967 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700968
969 @Override
970 public void onShowAllContacts() {
971 final Intent intent = new Intent(this, AllContactsActivity.class);
972 startActivity(intent);
973 }
Yorke Leee1424812013-09-04 18:24:48 -0700974
975 public static Intent getAddNumberToContactIntent(CharSequence text) {
976 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
977 intent.putExtra(Intents.Insert.PHONE, text);
978 intent.setType(Contacts.CONTENT_ITEM_TYPE);
979 return intent;
980 }
981
Yorke Leeda0f9042013-12-05 14:25:51 -0800982 private boolean canIntentBeHandled(Intent intent) {
983 final PackageManager packageManager = getPackageManager();
984 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
985 PackageManager.MATCH_DEFAULT_ONLY);
986 return resolveInfo != null && resolveInfo.size() > 0;
987 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800988
989 @Override
990 public void onDragStarted(int itemIndex, int x, int y, PhoneFavoriteTileView view) {
991 crossfadeViews(mRemoveViewContainer, mSearchViewContainer, FADE_ANIMATION_DURATION);
992 }
993
994 @Override
995 public void onDragHovered(int itemIndex, int x, int y) {}
996
997 @Override
998 public void onDragFinished(int x, int y) {
999 crossfadeViews(mSearchViewContainer, mRemoveViewContainer, FADE_ANIMATION_DURATION);
1000 }
1001
1002 @Override
1003 public void onDroppedOnRemove() {}
1004
1005 /**
1006 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
1007 * once it has been attached to the activity.
1008 */
1009 @Override
1010 public void setDragDropController(DragDropController dragController) {
1011 mRemoveViewContainer.setDragDropController(dragController);
1012 }
1013
1014 /**
1015 * Crossfades two views so that the first one appears while the other one is fading
1016 * out of view.
1017 */
1018 private void crossfadeViews(final View fadeIn, final View fadeOut, int duration) {
1019 fadeOut.animate().alpha(0).setDuration(duration)
1020 .setListener(new AnimatorListenerAdapter() {
1021 @Override
1022 public void onAnimationEnd(Animator animation) {
1023 fadeOut.setVisibility(View.GONE);
1024 }
1025 });
1026
1027 fadeIn.setVisibility(View.VISIBLE);
1028 fadeIn.setAlpha(0);
1029 fadeIn.animate().alpha(1).setDuration(FADE_ANIMATION_DURATION)
1030 .setListener(null);
1031 }
Ihab Awad526c0b82014-02-27 12:55:36 -08001032
1033 private boolean shouldShowOnscreenDialButton() {
1034 return getResources().getBoolean(R.bool.config_show_onscreen_dial_button);
1035 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001036}