blob: 1a1713316e1e1c9c6287e6c0125479cc970eabfc [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;
Yorke Leeb207f8a2013-08-29 18:51:06 -070025import android.app.FragmentManager.BackStackEntry;
Chiao Cheng94b10b52012-08-17 16:59:12 -070026import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080027import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.content.Context;
29import android.content.Intent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070030import android.net.Uri;
31import android.os.Bundle;
Yorke Lee02403e52013-09-24 07:24:39 -070032import android.os.Handler;
33import android.os.Message;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.os.RemoteException;
35import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036import android.provider.CallLog.Calls;
37import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070038import android.provider.ContactsContract.Intents;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070040import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070041import android.telephony.TelephonyManager;
42import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070044import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.util.Log;
46import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.View.OnFocusChangeListener;
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;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080054import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070055
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070056import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070057import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070058import com.android.contacts.common.dialog.ClearFrequentsDialog;
59import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080060import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070061import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070062import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070063import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070064import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070065import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070066import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070067import com.android.dialer.list.AllContactsActivity;
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;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070070import com.android.dialer.list.RegularSearchFragment;
71import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070073import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070074import com.android.internal.telephony.ITelephony;
75
Yorke Leec3766332013-07-31 11:13:16 -070076import java.util.ArrayList;
77
Chiao Cheng94b10b52012-08-17 16:59:12 -070078/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070079 * The dialer tab's title is 'phone', a more common name (see strings.xml).
80 */
Yorke Leec3766332013-07-31 11:13:16 -070081public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070082 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070083 OnListFragmentScrolledListener,
Yorke Lee8dd62002013-08-08 15:57:20 -070084 DialpadFragment.OnDialpadFragmentStartedListener,
85 PhoneFavoriteFragment.OnShowAllContactsListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070086 private static final String TAG = "DialtactsActivity";
87
88 public static final boolean DEBUG = false;
89
Yorke Leef74011e2013-08-02 11:30:30 -070090 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
91
Chiao Cheng94b10b52012-08-17 16:59:12 -070092 /** Used to open Call Setting */
93 private static final String PHONE_PACKAGE = "com.android.phone";
94 private static final String CALL_SETTINGS_CLASS_NAME =
95 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -070096 /** @see #getCallOrigin() */
97 private static final String CALL_ORIGIN_DIALTACTS =
98 "com.android.dialer.DialtactsActivity";
99
Yorke Leeb207f8a2013-08-29 18:51:06 -0700100 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
101 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700102 private static final String KEY_SEARCH_QUERY = "search_query";
103 private static final String KEY_FIRST_LAUNCH = "first_launch";
104
Yorke Leec3766332013-07-31 11:13:16 -0700105 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
106 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
107 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
108 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700109
Chiao Cheng94b10b52012-08-17 16:59:12 -0700110 /**
111 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
112 */
113 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
114
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
116
Yorke Leec3766332013-07-31 11:13:16 -0700117 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118
119 private String mFilterText;
120
Yorke Leec3766332013-07-31 11:13:16 -0700121 /**
122 * The main fragment displaying the user's favorites and frequent contacts
123 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700124 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700125
126 /**
Yorke Leec3766332013-07-31 11:13:16 -0700127 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700128 */
Yorke Leef74011e2013-08-02 11:30:30 -0700129 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700130
131 /**
132 * Fragment for searching phone numbers using the alphanumeric keyboard.
133 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700134 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700135
136 /**
137 * Fragment for searching phone numbers using the dialpad.
138 */
139 private SmartDialSearchFragment mSmartDialSearchFragment;
140
Yorke Leec3766332013-07-31 11:13:16 -0700141 private View mMenuButton;
142 private View mCallHistoryButton;
143 private View mDialpadButton;
Yorke Lee36c54132013-09-25 16:17:06 -0700144 private PopupMenu mOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700145
146 // Padding view used to shift the fragments up when the dialpad is shown.
147 private View mBottomPaddingView;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700148 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700149
Yorke Leeb207f8a2013-08-29 18:51:06 -0700150 private boolean mInDialpadSearch;
151 private boolean mInRegularSearch;
152
Yorke Leeef2b7382013-08-09 17:39:25 -0700153 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700154 * True if the dialpad is only temporarily showing due to being in call
155 */
156 private boolean mInCallDialpadUp;
157
158 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700159 * True when this activity has been launched for the first time.
160 */
Yorke Lee98702de2013-08-06 12:03:32 -0700161 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700162 private View mSearchViewContainer;
163 private View mSearchViewCloseButton;
164 private View mVoiceSearchButton;
165 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700166
Yorke Leeef2b7382013-08-09 17:39:25 -0700167 private String mSearchQuery;
168
Yorke Leefce269a2013-08-12 11:56:04 -0700169 private DialerDatabaseHelper mDialerDatabaseHelper;
170
Yorke Lee02403e52013-09-24 07:24:39 -0700171 private static final int EVENT_DELAYED_HIDE_SEARCH = 1;
172
173 // Wait this much time (in ms) before hiding the search fragment after clicking on
174 // a search result to dial, to avoid jank
175 private static final int CLEAR_SEARCH_DELAY = 500;
176
177 final Handler mHandler = new Handler() {
178 @Override
179 public void handleMessage(Message msg) {
180 if (msg.what == EVENT_DELAYED_HIDE_SEARCH) {
181 hideDialpadAndSearchUi();
182 }
183 }
184 };
185
Yorke Lee36c54132013-09-25 16:17:06 -0700186 private class OverflowPopupMenu extends PopupMenu {
187 public OverflowPopupMenu(Context context, View anchor) {
188 super(context, anchor);
189 }
190
191 @Override
192 public void show() {
193 final Menu menu = getMenu();
194 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
195 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
196 super.show();
197 }
198 }
199
Chiao Cheng94b10b52012-08-17 16:59:12 -0700200 /**
201 * Listener used when one of phone numbers in search UI is selected. This will initiate a
202 * phone call using the phone number.
203 */
204 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
205 new OnPhoneNumberPickerActionListener() {
206 @Override
207 public void onPickPhoneNumberAction(Uri dataUri) {
208 // Specify call-origin so that users will see the previous tab instead of
209 // CallLog screen (search UI will be automatically exited).
210 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700211 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee02403e52013-09-24 07:24:39 -0700212 mHandler.sendEmptyMessageDelayed(EVENT_DELAYED_HIDE_SEARCH,
213 CLEAR_SEARCH_DELAY);
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 Lee02403e52013-09-24 07:24:39 -0700220 mHandler.sendEmptyMessageDelayed(EVENT_DELAYED_HIDE_SEARCH,
221 CLEAR_SEARCH_DELAY);
Jay Shrauner31a760b2013-07-26 09:34:49 -0700222 }
223
224 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700225 public void onShortcutIntentCreated(Intent intent) {
226 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
227 }
228
229 @Override
230 public void onHomeInActionBarSelected() {
231 exitSearchUi();
232 }
233 };
234
235 /**
236 * Listener used to send search queries to the phone search fragment.
237 */
Yorke Leec3766332013-07-31 11:13:16 -0700238 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
239 @Override
240 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
241 }
242
243 @Override
244 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700245 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700246 if (newText.equals(mSearchQuery)) {
247 // If the query hasn't changed (perhaps due to activity being destroyed
248 // and restored, or user launching the same DIAL intent twice), then there is
249 // no need to do anything here.
250 return;
251 }
252 mSearchQuery = newText;
253 if (DEBUG) {
254 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
255 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700256 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700257
Yorke Leec3766332013-07-31 11:13:16 -0700258 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700259 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700260 exitSearchUi();
261 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700262 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700263 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700264 } else if (!TextUtils.isEmpty(newText)) {
265 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
266 (!dialpadSearch && mInRegularSearch);
267 if (!sameSearchMode) {
268 // call enterSearchUi only if we are switching search modes, or entering
269 // search ui for the first time
270 enterSearchUi(dialpadSearch, newText);
271 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700272
Yorke Leeb207f8a2013-08-29 18:51:06 -0700273 if (dialpadSearch && mSmartDialSearchFragment != null) {
274 mSmartDialSearchFragment.setQueryString(newText, false);
275 } else if (mRegularSearchFragment != null) {
276 mRegularSearchFragment.setQueryString(newText, false);
277 }
278 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700279 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700280 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700281 }
Yorke Leec3766332013-07-31 11:13:16 -0700282 }
283
284 @Override
285 public void afterTextChanged(Editable s) {
286 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700287 };
288
Yorke Leec3766332013-07-31 11:13:16 -0700289 private boolean isDialpadShowing() {
290 return mDialpadFragment != null && mDialpadFragment.isVisible();
291 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700292
293 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700294 protected void onCreate(Bundle savedInstanceState) {
295 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700296 mFirstLaunch = true;
297
Chiao Cheng94b10b52012-08-17 16:59:12 -0700298 final Intent intent = getIntent();
299 fixIntent(intent);
300
Yorke Lee8898cd02013-08-08 10:24:27 -0700301 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700302
Yorke Leec3766332013-07-31 11:13:16 -0700303 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700304
Yorke Leeef2b7382013-08-09 17:39:25 -0700305 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
306 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700307 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700308 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700309
Yorke Leec3766332013-07-31 11:13:16 -0700310 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700311 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
312 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700313 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700314 } else {
315 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700316 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
317 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700318 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700319 }
320
Yorke Leec3766332013-07-31 11:13:16 -0700321 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700322 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leec3766332013-07-31 11:13:16 -0700323 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700324
Chiao Cheng94b10b52012-08-17 16:59:12 -0700325 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700326 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700327 setupFilterText(intent);
328 }
Yorke Leefce269a2013-08-12 11:56:04 -0700329
Yorke Leeb207f8a2013-08-29 18:51:06 -0700330 setupFakeActionBarItems();
331
Yorke Lee0baa98b2013-08-22 10:55:16 -0700332 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700333 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700334 }
335
336 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700337 protected void onResume() {
338 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700339 if (mFirstLaunch) {
340 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700341 } else if (!phoneIsInUse() && mInCallDialpadUp) {
342 hideDialpadFragment(false, true);
343 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700344 }
Yorke Lee35127cd2013-09-10 14:09:29 -0700345
Yorke Lee98702de2013-08-06 12:03:32 -0700346 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700347 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700348 }
349
350 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700351 protected void onSaveInstanceState(Bundle outState) {
352 super.onSaveInstanceState(outState);
353 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700354 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
355 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700356 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
357 }
358
359 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700360 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700361 if (fragment instanceof DialpadFragment) {
362 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700363 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700364 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700365 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700366 } else if (fragment instanceof SmartDialSearchFragment) {
367 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700368 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
369 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700370 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700371 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700372 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
373 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700374 } else if (fragment instanceof PhoneFavoriteFragment) {
375 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700376 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700377 }
Yorke Leec3766332013-07-31 11:13:16 -0700378 }
379
380 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700381 public boolean onMenuItemClick(MenuItem item) {
382 switch (item.getItemId()) {
383 case R.id.menu_import_export:
384 // We hard-code the "contactsAreAvailable" argument because doing it properly would
385 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
386 // now in Dialtacts for (potential) performance reasons. Compare with how it is
387 // done in {@link PeopleActivity}.
388 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700389 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700390 return true;
391 case R.id.menu_clear_frequents:
392 ClearFrequentsDialog.show(getFragmentManager());
393 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700394 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700395 try {
396 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
397 } catch (ActivityNotFoundException e) {
398 Toast toast = Toast.makeText(this,
399 R.string.add_contact_not_available,
400 Toast.LENGTH_SHORT);
401 toast.show();
402 }
403 return true;
404 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700405 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700406 return true;
407 case R.id.menu_all_contacts:
408 onShowAllContacts();
409 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700410 }
411 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700412 }
413
Alon Albertb453e5b2013-09-10 15:54:23 -0700414 protected void handleMenuSettings() {
415 openTelephonySetting(this);
416 }
417
418 public static void openTelephonySetting(Activity activity) {
419 final Intent settingsIntent = getCallSettingsIntent();
420 activity.startActivity(settingsIntent);
421 }
422
Chiao Cheng94b10b52012-08-17 16:59:12 -0700423 @Override
424 public void onClick(View view) {
425 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700426 case R.id.overflow_menu: {
Yorke Lee36c54132013-09-25 16:17:06 -0700427 mOverflowMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700428 break;
429 }
Yorke Leec3766332013-07-31 11:13:16 -0700430 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700431 // Reset the boolean flag that tracks whether the dialpad was up because
432 // we were in call. Regardless of whether it was true before, we want to
433 // show the dialpad because the user has explicitly clicked the dialpad
434 // button.
435 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700436 showDialpadFragment(true);
437 break;
438 case R.id.call_history_on_dialpad_button:
439 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700440 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
441 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700442 final Intent intent = new Intent(this, CallLogActivity.class);
443 startActivity(intent);
444 break;
445 case R.id.search_close_button:
446 // Clear the search field
447 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700448 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700449 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700450 }
451 break;
Yorke Leec3766332013-07-31 11:13:16 -0700452 case R.id.voice_search_button:
453 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
454 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
455 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700456 default: {
457 Log.wtf(TAG, "Unexpected onClick event from " + view);
458 break;
459 }
460 }
461 }
462
Yorke Leec3766332013-07-31 11:13:16 -0700463 @Override
464 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
465 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
466 if (resultCode == RESULT_OK) {
467 final ArrayList<String> matches = data.getStringArrayListExtra(
468 RecognizerIntent.EXTRA_RESULTS);
469 if (matches.size() > 0) {
470 final String match = matches.get(0);
471 mSearchView.setText(match);
472 } else {
473 Log.e(TAG, "Voice search - nothing heard");
474 }
475 } else {
476 Log.e(TAG, "Voice search failed");
477 }
478 }
479 super.onActivityResult(requestCode, resultCode, data);
480 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700481
Yorke Leec3766332013-07-31 11:13:16 -0700482 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700483 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700484 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700485 if (animate) {
486 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700487 } else {
488 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700489 }
490 ft.show(mDialpadFragment);
491 ft.commit();
492 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700493
Yorke Leeca195042013-09-25 16:29:38 -0700494 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700495 if (mDialpadFragment == null) return;
496 if (clearDialpad) {
497 mDialpadFragment.clearDialpad();
498 }
499 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700500 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700501 final FragmentTransaction ft = getFragmentManager().beginTransaction();
502 if (animate) {
503 ft.setCustomAnimations(0, R.anim.slide_out);
504 }
505 ft.hide(mDialpadFragment);
506 ft.commit();
507 }
508
Chiao Cheng94b10b52012-08-17 16:59:12 -0700509 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700510 mSearchViewContainer = findViewById(R.id.search_view_container);
511 mSearchViewCloseButton = findViewById(R.id.search_close_button);
512 mSearchViewCloseButton.setOnClickListener(this);
513 mVoiceSearchButton = findViewById(R.id.voice_search_button);
514 mVoiceSearchButton.setOnClickListener(this);
515 mSearchView = (EditText) findViewById(R.id.search_view);
516 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
517 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
518 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700519 @Override
520 public void onFocusChange(View view, boolean hasFocus) {
521 if (hasFocus) {
522 showInputMethod(view.findFocus());
523 }
524 }
525 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700526 }
527
Yorke Leec3766332013-07-31 11:13:16 -0700528 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
529 @Override
530 public void onAnimationEnd(Animator animation) {
531 mSearchViewContainer.setVisibility(View.GONE);
532 }
533 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700534
Yorke Leeb207f8a2013-08-29 18:51:06 -0700535 private boolean getInSearchUi() {
536 return mInDialpadSearch || mInRegularSearch;
537 }
538
539 private void setNotInSearchUi() {
540 mInDialpadSearch = false;
541 mInRegularSearch = false;
542 }
543
Yorke Lee311969c2013-08-26 06:31:11 -0700544 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700545 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700546 hideDialpadFragment(false, true);
547 }
548
Yorke Leec3766332013-07-31 11:13:16 -0700549 public void hideSearchBar() {
550 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700551 }
552
Yorke Leec3766332013-07-31 11:13:16 -0700553 public void hideSearchBar(boolean shiftView) {
554 if (shiftView) {
555 mSearchViewContainer.animate().cancel();
556 mSearchViewContainer.setAlpha(1);
557 mSearchViewContainer.setTranslationY(0);
558 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
559 .setDuration(200).setListener(mHideListener);
560
Yorke Leeb207f8a2013-08-29 18:51:06 -0700561 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700562 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
563 new AnimatorListenerAdapter() {
564 @Override
565 public void onAnimationEnd(Animator animation) {
566 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700567 mFragmentsFrame.setTranslationY(0);
Yorke Leec3766332013-07-31 11:13:16 -0700568 }
569 });
570 } else {
571 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
572 }
573 }
574
575 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700576 mSearchViewContainer.animate().cancel();
577 mSearchViewContainer.setAlpha(0);
578 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
579 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
580 .setListener(new AnimatorListenerAdapter() {
581 @Override
582 public void onAnimationStart(Animator animation) {
583 mSearchViewContainer.setVisibility(View.VISIBLE);
584 }
585 });
586
Yorke Leeb207f8a2013-08-29 18:51:06 -0700587 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
588 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700589 .setListener(
590 new AnimatorListenerAdapter() {
591 @Override
592 public void onAnimationStart(Animator animation) {
593 mBottomPaddingView.setVisibility(View.GONE);
594 }
595 });
596 }
597
598
Yorke Leeb207f8a2013-08-29 18:51:06 -0700599 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700600 mMenuButton = findViewById(R.id.overflow_menu);
601 if (mMenuButton != null) {
602 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700603
604 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
605 final Menu menu = mOverflowMenu.getMenu();
606 mOverflowMenu.inflate(R.menu.dialtacts_options);
607 mOverflowMenu.setOnMenuItemClickListener(this);
608 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700609 }
610
611 mCallHistoryButton = findViewById(R.id.call_history_button);
612 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
613 mCallHistoryButton.setOnClickListener(this);
614
615 mDialpadButton = findViewById(R.id.dialpad_button);
616 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
617 mDialpadButton.setOnClickListener(this);
618 }
619
620 public void setupFakeActionBarItemsForDialpadFragment() {
621 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
622 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700623 }
624
625 private void fixIntent(Intent intent) {
626 // This should be cleaned up: the call key used to send an Intent
627 // that just said to go to the recent calls list. It now sends this
628 // abstract action, but this class hasn't been rewritten to deal with it.
629 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
630 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
631 intent.putExtra("call_key", true);
632 setIntent(intent);
633 }
634 }
635
Chiao Cheng94b10b52012-08-17 16:59:12 -0700636 /**
637 * Returns true if the intent is due to hitting the green send key (hardware call button:
638 * KEYCODE_CALL) while in a call.
639 *
640 * @param intent the intent that launched this activity
641 * @param recentCallsRequest true if the intent is requesting to view recent calls
642 * @return true if the intent is due to hitting the green send key while in a call
643 */
Yorke Leec3766332013-07-31 11:13:16 -0700644 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700645 // If there is a call in progress go to the call screen
646 if (recentCallsRequest) {
647 final boolean callKey = intent.getBooleanExtra("call_key", false);
648
649 try {
650 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
651 if (callKey && phone != null && phone.showCallScreen()) {
652 return true;
653 }
654 } catch (RemoteException e) {
655 Log.e(TAG, "Failed to handle send while in call", e);
656 }
657 }
658
659 return false;
660 }
661
662 /**
663 * Sets the current tab based on the intent's request type
664 *
665 * @param intent Intent that contains information about which tab should be selected
666 */
Yorke Leec3766332013-07-31 11:13:16 -0700667 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700668 // If we got here by hitting send and we're in call forward along to the in-call activity
669 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
670 getContentResolver()));
671 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
672 finish();
673 return;
674 }
675
Yorke Lee669b6082013-09-17 15:43:38 -0700676 if (mDialpadFragment != null) {
677 final boolean phoneIsInUse = phoneIsInUse();
678 if (phoneIsInUse || isDialIntent(intent)) {
679 mDialpadFragment.setStartedFromNewIntent(true);
680 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
681 mInCallDialpadUp = true;
682 }
683 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700684 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700685 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700686 }
687
688 @Override
689 public void onNewIntent(Intent newIntent) {
690 setIntent(newIntent);
691 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700692 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700693 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700694
Chiao Cheng94b10b52012-08-17 16:59:12 -0700695 invalidateOptionsMenu();
696 }
697
698 /** Returns true if the given intent contains a phone number to populate the dialer with */
699 private boolean isDialIntent(Intent intent) {
700 final String action = intent.getAction();
701 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
702 return true;
703 }
704 if (Intent.ACTION_VIEW.equals(action)) {
705 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700706 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700707 return true;
708 }
709 }
710 return false;
711 }
712
713 /**
714 * Returns an appropriate call origin for this Activity. May return null when no call origin
715 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
716 * for remembering the tab in which the user made a phone call, so the external app's DIAL
717 * request should not be counted.)
718 */
719 public String getCallOrigin() {
720 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
721 }
722
723 /**
724 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
725 * This text originally came from a FILTER_CONTACTS_ACTION intent received
726 * by this activity. The stored text will then be cleared after after this
727 * method returns.
728 *
729 * @return The stored filter text
730 */
731 public String getAndClearFilterText() {
732 String filterText = mFilterText;
733 mFilterText = null;
734 return filterText;
735 }
736
737 /**
738 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
739 * This is so child activities can check if they are supposed to display a filter.
740 *
741 * @param intent The intent received in {@link #onNewIntent(Intent)}
742 */
743 private void setupFilterText(Intent intent) {
744 // If the intent was relaunched from history, don't apply the filter text.
745 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
746 return;
747 }
748 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
749 if (filter != null && filter.length() > 0) {
750 mFilterText = filter;
751 }
752 }
753
Yorke Lee8898cd02013-08-08 10:24:27 -0700754 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
755 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700756 @Override
757 public void onContactSelected(Uri contactUri) {
758 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700759 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700760 }
761
762 @Override
763 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700764 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700765 startActivity(intent);
766 }
767 };
768
Yorke Leec3766332013-07-31 11:13:16 -0700769 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
770 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
771 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700772
773 @Override
774 public void startSearch(String initialQuery, boolean selectInitialQuery,
775 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700776 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700777 if (mInSearchUi) {
778 if (mSearchView.hasFocus()) {
779 showInputMethod(mSearchView.findFocus());
780 } else {
781 mSearchView.requestFocus();
782 }
783 } else {
784 enterSearchUi();
785 }
786 } else {
787 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
788 }
Yorke Leec3766332013-07-31 11:13:16 -0700789 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700790
791 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700792 final InputMethodManager imm = (InputMethodManager) getSystemService(
793 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700794 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700795 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700796 }
797 }
798
799 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700800 final InputMethodManager imm = (InputMethodManager) getSystemService(
801 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700802 if (imm != null && view != null) {
803 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
804 }
805 }
806
807 /**
Yorke Leec3766332013-07-31 11:13:16 -0700808 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700809 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700810 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700811 if (getFragmentManager().isDestroyed()) {
812 // Weird race condition where fragment is doing work after the activity is destroyed
813 // due to talkback being on (b/10209937). Just return since we can't do any
814 // constructive here.
815 return;
816 }
817
Yorke Leeef2b7382013-08-09 17:39:25 -0700818 if (DEBUG) {
819 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
820 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700821
Yorke Leec3766332013-07-31 11:13:16 -0700822 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
823 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700824
Yorke Leeef2b7382013-08-09 17:39:25 -0700825 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700826 if (mInDialpadSearch) {
827 transaction.remove(mSmartDialSearchFragment);
828 } else if (mInRegularSearch) {
829 transaction.remove(mRegularSearchFragment);
830 } else {
831 transaction.remove(mPhoneFavoriteFragment);
832 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700833
Yorke Leeb207f8a2013-08-29 18:51:06 -0700834 final String tag;
835 if (smartDialSearch) {
836 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
837 } else {
838 tag = TAG_REGULAR_SEARCH_FRAGMENT;
839 }
840 mInDialpadSearch = smartDialSearch;
841 mInRegularSearch = !smartDialSearch;
842
Yorke Leeef2b7382013-08-09 17:39:25 -0700843 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
844 if (fragment == null) {
845 if (smartDialSearch) {
846 fragment = new SmartDialSearchFragment();
847 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700848 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700849 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700850 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700851 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700852 transaction.addToBackStack(null);
853 fragment.setQueryString(query, false);
854 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700855 }
856
857 /**
Yorke Leec3766332013-07-31 11:13:16 -0700858 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700859 */
Yorke Leec3766332013-07-31 11:13:16 -0700860 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700861 // See related bug in enterSearchUI();
862 if (getFragmentManager().isDestroyed()) {
863 return;
864 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700865 // Go all the way back to the favorites fragment, regardless of how many times we
866 // transitioned between search fragments
867 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
868 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
869 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700870 }
871
872 /** Returns an Intent to launch Call Settings screen */
873 public static Intent getCallSettingsIntent() {
874 final Intent intent = new Intent(Intent.ACTION_MAIN);
875 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
876 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
877 return intent;
878 }
879
880 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700881 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700882 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700883 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700884 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700885 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700886 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700887 } else if (isTaskRoot()) {
888 // Instead of stopping, simply push this to the back of the stack.
889 // This is only done when running at the top of the stack;
890 // otherwise, we have been launched by someone else so need to
891 // allow the user to go back to the caller.
892 moveTaskToBack(false);
893 } else {
894 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700895 }
Yorke Leec3766332013-07-31 11:13:16 -0700896 }
897
898 @Override
899 public void onDialpadQueryChanged(String query) {
900 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
901 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
902 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700903 if (DEBUG) {
904 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
905 }
906 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
907 // This callback can happen if the dialpad fragment is recreated because of
908 // activity destruction. In that case, don't update the search view because
909 // that would bring the user back to the search fragment regardless of the
910 // previous state of the application. Instead, just return here and let the
911 // fragment manager correctly figure out whatever fragment was last displayed.
912 return;
913 }
Yorke Leec3766332013-07-31 11:13:16 -0700914 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700915 }
916 }
Yorke Leec3766332013-07-31 11:13:16 -0700917
918 @Override
919 public void onListFragmentScrollStateChange(int scrollState) {
920 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700921 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700922 hideInputMethod(getCurrentFocus());
923 }
924 }
925
926 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700927 public void onDialpadFragmentStarted() {
928 setupFakeActionBarItemsForDialpadFragment();
929 }
930
931 private boolean phoneIsInUse() {
932 final TelephonyManager tm = (TelephonyManager) getSystemService(
933 Context.TELEPHONY_SERVICE);
934 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
935 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700936
937 @Override
938 public void onShowAllContacts() {
939 final Intent intent = new Intent(this, AllContactsActivity.class);
940 startActivity(intent);
941 }
Yorke Leee1424812013-09-04 18:24:48 -0700942
943 public static Intent getAddNumberToContactIntent(CharSequence text) {
944 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
945 intent.putExtra(Intents.Insert.PHONE, text);
946 intent.setType(Contacts.CONTENT_ITEM_TYPE);
947 return intent;
948 }
949
950 public static Intent getInsertContactWithNameIntent(CharSequence text) {
951 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
952 intent.putExtra(Intents.Insert.NAME, text);
953 return intent;
954 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700955}