blob: 572a4f1950e1db81193d068d1e73338e3cfb25df [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;
144
145 // Padding view used to shift the fragments up when the dialpad is shown.
146 private View mBottomPaddingView;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700147 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700148
Yorke Leeb207f8a2013-08-29 18:51:06 -0700149 private boolean mInDialpadSearch;
150 private boolean mInRegularSearch;
151
Yorke Leeef2b7382013-08-09 17:39:25 -0700152 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700153 * True if the dialpad is only temporarily showing due to being in call
154 */
155 private boolean mInCallDialpadUp;
156
157 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700158 * True when this activity has been launched for the first time.
159 */
Yorke Lee98702de2013-08-06 12:03:32 -0700160 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700161 private View mSearchViewContainer;
162 private View mSearchViewCloseButton;
163 private View mVoiceSearchButton;
164 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700165
Yorke Leeef2b7382013-08-09 17:39:25 -0700166 private String mSearchQuery;
167
Yorke Leefce269a2013-08-12 11:56:04 -0700168 private DialerDatabaseHelper mDialerDatabaseHelper;
169
Yorke Lee02403e52013-09-24 07:24:39 -0700170 private static final int EVENT_DELAYED_HIDE_SEARCH = 1;
171
172 // Wait this much time (in ms) before hiding the search fragment after clicking on
173 // a search result to dial, to avoid jank
174 private static final int CLEAR_SEARCH_DELAY = 500;
175
176 final Handler mHandler = new Handler() {
177 @Override
178 public void handleMessage(Message msg) {
179 if (msg.what == EVENT_DELAYED_HIDE_SEARCH) {
180 hideDialpadAndSearchUi();
181 }
182 }
183 };
184
Chiao Cheng94b10b52012-08-17 16:59:12 -0700185 /**
186 * Listener used when one of phone numbers in search UI is selected. This will initiate a
187 * phone call using the phone number.
188 */
189 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
190 new OnPhoneNumberPickerActionListener() {
191 @Override
192 public void onPickPhoneNumberAction(Uri dataUri) {
193 // Specify call-origin so that users will see the previous tab instead of
194 // CallLog screen (search UI will be automatically exited).
195 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700196 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee02403e52013-09-24 07:24:39 -0700197 mHandler.sendEmptyMessageDelayed(EVENT_DELAYED_HIDE_SEARCH,
198 CLEAR_SEARCH_DELAY);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700199 }
200
201 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700202 public void onCallNumberDirectly(String phoneNumber) {
203 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
204 startActivity(intent);
Yorke Lee02403e52013-09-24 07:24:39 -0700205 mHandler.sendEmptyMessageDelayed(EVENT_DELAYED_HIDE_SEARCH,
206 CLEAR_SEARCH_DELAY);
Jay Shrauner31a760b2013-07-26 09:34:49 -0700207 }
208
209 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700210 public void onShortcutIntentCreated(Intent intent) {
211 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
212 }
213
214 @Override
215 public void onHomeInActionBarSelected() {
216 exitSearchUi();
217 }
218 };
219
220 /**
221 * Listener used to send search queries to the phone search fragment.
222 */
Yorke Leec3766332013-07-31 11:13:16 -0700223 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
224 @Override
225 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
226 }
227
228 @Override
229 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700230 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700231 if (newText.equals(mSearchQuery)) {
232 // If the query hasn't changed (perhaps due to activity being destroyed
233 // and restored, or user launching the same DIAL intent twice), then there is
234 // no need to do anything here.
235 return;
236 }
237 mSearchQuery = newText;
238 if (DEBUG) {
239 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
240 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700241 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700242
Yorke Leec3766332013-07-31 11:13:16 -0700243 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700244 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700245 exitSearchUi();
246 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700247 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700248 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700249 } else if (!TextUtils.isEmpty(newText)) {
250 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
251 (!dialpadSearch && mInRegularSearch);
252 if (!sameSearchMode) {
253 // call enterSearchUi only if we are switching search modes, or entering
254 // search ui for the first time
255 enterSearchUi(dialpadSearch, newText);
256 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700257
Yorke Leeb207f8a2013-08-29 18:51:06 -0700258 if (dialpadSearch && mSmartDialSearchFragment != null) {
259 mSmartDialSearchFragment.setQueryString(newText, false);
260 } else if (mRegularSearchFragment != null) {
261 mRegularSearchFragment.setQueryString(newText, false);
262 }
263 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700264 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700265 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700266 }
Yorke Leec3766332013-07-31 11:13:16 -0700267 }
268
269 @Override
270 public void afterTextChanged(Editable s) {
271 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700272 };
273
Yorke Leec3766332013-07-31 11:13:16 -0700274 private boolean isDialpadShowing() {
275 return mDialpadFragment != null && mDialpadFragment.isVisible();
276 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700277
278 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700279 protected void onCreate(Bundle savedInstanceState) {
280 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700281 mFirstLaunch = true;
282
Chiao Cheng94b10b52012-08-17 16:59:12 -0700283 final Intent intent = getIntent();
284 fixIntent(intent);
285
Yorke Lee8898cd02013-08-08 10:24:27 -0700286 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700287
Yorke Leec3766332013-07-31 11:13:16 -0700288 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700289
Yorke Leeef2b7382013-08-09 17:39:25 -0700290 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
291 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700292 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700293 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700294
Yorke Leec3766332013-07-31 11:13:16 -0700295 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700296 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
297 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700298 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700299 } else {
300 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700301 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
302 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700303 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700304 }
305
Yorke Leec3766332013-07-31 11:13:16 -0700306 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700307 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leec3766332013-07-31 11:13:16 -0700308 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700309
Chiao Cheng94b10b52012-08-17 16:59:12 -0700310 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700311 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700312 setupFilterText(intent);
313 }
Yorke Leefce269a2013-08-12 11:56:04 -0700314
Yorke Leeb207f8a2013-08-29 18:51:06 -0700315 setupFakeActionBarItems();
316
Yorke Lee0baa98b2013-08-22 10:55:16 -0700317 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700318 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700319 }
320
321 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700322 protected void onResume() {
323 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700324 if (mFirstLaunch) {
325 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700326 } else if (!phoneIsInUse() && mInCallDialpadUp) {
327 hideDialpadFragment(false, true);
328 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700329 }
Yorke Lee35127cd2013-09-10 14:09:29 -0700330
Yorke Lee98702de2013-08-06 12:03:32 -0700331 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700332 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700333 }
334
335 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700336 protected void onSaveInstanceState(Bundle outState) {
337 super.onSaveInstanceState(outState);
338 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700339 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
340 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700341 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
342 }
343
344 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700345 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700346 if (fragment instanceof DialpadFragment) {
347 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700348 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700349 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700350 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700351 } else if (fragment instanceof SmartDialSearchFragment) {
352 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700353 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
354 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700355 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700356 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700357 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
358 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700359 } else if (fragment instanceof PhoneFavoriteFragment) {
360 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700361 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700362 }
Yorke Leec3766332013-07-31 11:13:16 -0700363 }
364
365 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700366 public boolean onMenuItemClick(MenuItem item) {
367 switch (item.getItemId()) {
368 case R.id.menu_import_export:
369 // We hard-code the "contactsAreAvailable" argument because doing it properly would
370 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
371 // now in Dialtacts for (potential) performance reasons. Compare with how it is
372 // done in {@link PeopleActivity}.
373 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700374 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700375 return true;
376 case R.id.menu_clear_frequents:
377 ClearFrequentsDialog.show(getFragmentManager());
378 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700379 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700380 try {
381 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
382 } catch (ActivityNotFoundException e) {
383 Toast toast = Toast.makeText(this,
384 R.string.add_contact_not_available,
385 Toast.LENGTH_SHORT);
386 toast.show();
387 }
388 return true;
389 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700390 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700391 return true;
392 case R.id.menu_all_contacts:
393 onShowAllContacts();
394 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700395 }
396 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700397 }
398
Alon Albertb453e5b2013-09-10 15:54:23 -0700399 protected void handleMenuSettings() {
400 openTelephonySetting(this);
401 }
402
403 public static void openTelephonySetting(Activity activity) {
404 final Intent settingsIntent = getCallSettingsIntent();
405 activity.startActivity(settingsIntent);
406 }
407
Chiao Cheng94b10b52012-08-17 16:59:12 -0700408 @Override
409 public void onClick(View view) {
410 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700411 case R.id.overflow_menu: {
412 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
413 final Menu menu = popupMenu.getMenu();
Yorke Lee8898cd02013-08-08 10:24:27 -0700414 popupMenu.inflate(R.menu.dialtacts_options);
Yorke Leef420a532013-08-09 15:16:10 -0700415 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Lee0d391b42013-08-09 17:27:00 -0700416 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
Yorke Leec3766332013-07-31 11:13:16 -0700417 popupMenu.setOnMenuItemClickListener(this);
418 popupMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700419 break;
420 }
Yorke Leec3766332013-07-31 11:13:16 -0700421 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700422 // Reset the boolean flag that tracks whether the dialpad was up because
423 // we were in call. Regardless of whether it was true before, we want to
424 // show the dialpad because the user has explicitly clicked the dialpad
425 // button.
426 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700427 showDialpadFragment(true);
428 break;
429 case R.id.call_history_on_dialpad_button:
430 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700431 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
432 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700433 final Intent intent = new Intent(this, CallLogActivity.class);
434 startActivity(intent);
435 break;
436 case R.id.search_close_button:
437 // Clear the search field
438 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700439 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700440 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700441 }
442 break;
Yorke Leec3766332013-07-31 11:13:16 -0700443 case R.id.voice_search_button:
444 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
445 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
446 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700447 default: {
448 Log.wtf(TAG, "Unexpected onClick event from " + view);
449 break;
450 }
451 }
452 }
453
Yorke Leec3766332013-07-31 11:13:16 -0700454 @Override
455 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
456 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
457 if (resultCode == RESULT_OK) {
458 final ArrayList<String> matches = data.getStringArrayListExtra(
459 RecognizerIntent.EXTRA_RESULTS);
460 if (matches.size() > 0) {
461 final String match = matches.get(0);
462 mSearchView.setText(match);
463 } else {
464 Log.e(TAG, "Voice search - nothing heard");
465 }
466 } else {
467 Log.e(TAG, "Voice search failed");
468 }
469 }
470 super.onActivityResult(requestCode, resultCode, data);
471 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700472
Yorke Leec3766332013-07-31 11:13:16 -0700473 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700474 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700475 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700476 if (animate) {
477 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700478 } else {
479 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700480 }
481 ft.show(mDialpadFragment);
482 ft.commit();
483 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700484
Yorke Leeca195042013-09-25 16:29:38 -0700485 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700486 if (mDialpadFragment == null) return;
487 if (clearDialpad) {
488 mDialpadFragment.clearDialpad();
489 }
490 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700491 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700492 final FragmentTransaction ft = getFragmentManager().beginTransaction();
493 if (animate) {
494 ft.setCustomAnimations(0, R.anim.slide_out);
495 }
496 ft.hide(mDialpadFragment);
497 ft.commit();
498 }
499
Chiao Cheng94b10b52012-08-17 16:59:12 -0700500 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700501 mSearchViewContainer = findViewById(R.id.search_view_container);
502 mSearchViewCloseButton = findViewById(R.id.search_close_button);
503 mSearchViewCloseButton.setOnClickListener(this);
504 mVoiceSearchButton = findViewById(R.id.voice_search_button);
505 mVoiceSearchButton.setOnClickListener(this);
506 mSearchView = (EditText) findViewById(R.id.search_view);
507 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
508 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
509 mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700510 @Override
511 public void onFocusChange(View view, boolean hasFocus) {
512 if (hasFocus) {
513 showInputMethod(view.findFocus());
514 }
515 }
516 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700517 }
518
Yorke Leec3766332013-07-31 11:13:16 -0700519 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
520 @Override
521 public void onAnimationEnd(Animator animation) {
522 mSearchViewContainer.setVisibility(View.GONE);
523 }
524 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700525
Yorke Leeb207f8a2013-08-29 18:51:06 -0700526 private boolean getInSearchUi() {
527 return mInDialpadSearch || mInRegularSearch;
528 }
529
530 private void setNotInSearchUi() {
531 mInDialpadSearch = false;
532 mInRegularSearch = false;
533 }
534
Yorke Lee311969c2013-08-26 06:31:11 -0700535 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700536 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700537 hideDialpadFragment(false, true);
538 }
539
Yorke Leec3766332013-07-31 11:13:16 -0700540 public void hideSearchBar() {
541 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700542 }
543
Yorke Leec3766332013-07-31 11:13:16 -0700544 public void hideSearchBar(boolean shiftView) {
545 if (shiftView) {
546 mSearchViewContainer.animate().cancel();
547 mSearchViewContainer.setAlpha(1);
548 mSearchViewContainer.setTranslationY(0);
549 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
550 .setDuration(200).setListener(mHideListener);
551
Yorke Leeb207f8a2013-08-29 18:51:06 -0700552 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700553 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
554 new AnimatorListenerAdapter() {
555 @Override
556 public void onAnimationEnd(Animator animation) {
557 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700558 mFragmentsFrame.setTranslationY(0);
Yorke Leec3766332013-07-31 11:13:16 -0700559 }
560 });
561 } else {
562 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
563 }
564 }
565
566 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700567 mSearchViewContainer.animate().cancel();
568 mSearchViewContainer.setAlpha(0);
569 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
570 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
571 .setListener(new AnimatorListenerAdapter() {
572 @Override
573 public void onAnimationStart(Animator animation) {
574 mSearchViewContainer.setVisibility(View.VISIBLE);
575 }
576 });
577
Yorke Leeb207f8a2013-08-29 18:51:06 -0700578 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
579 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700580 .setListener(
581 new AnimatorListenerAdapter() {
582 @Override
583 public void onAnimationStart(Animator animation) {
584 mBottomPaddingView.setVisibility(View.GONE);
585 }
586 });
587 }
588
589
Yorke Leeb207f8a2013-08-29 18:51:06 -0700590 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700591 mMenuButton = findViewById(R.id.overflow_menu);
592 if (mMenuButton != null) {
593 mMenuButton.setOnClickListener(this);
594 }
595
596 mCallHistoryButton = findViewById(R.id.call_history_button);
597 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
598 mCallHistoryButton.setOnClickListener(this);
599
600 mDialpadButton = findViewById(R.id.dialpad_button);
601 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
602 mDialpadButton.setOnClickListener(this);
603 }
604
605 public void setupFakeActionBarItemsForDialpadFragment() {
606 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
607 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700608 }
609
610 private void fixIntent(Intent intent) {
611 // This should be cleaned up: the call key used to send an Intent
612 // that just said to go to the recent calls list. It now sends this
613 // abstract action, but this class hasn't been rewritten to deal with it.
614 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
615 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
616 intent.putExtra("call_key", true);
617 setIntent(intent);
618 }
619 }
620
Chiao Cheng94b10b52012-08-17 16:59:12 -0700621 /**
622 * Returns true if the intent is due to hitting the green send key (hardware call button:
623 * KEYCODE_CALL) while in a call.
624 *
625 * @param intent the intent that launched this activity
626 * @param recentCallsRequest true if the intent is requesting to view recent calls
627 * @return true if the intent is due to hitting the green send key while in a call
628 */
Yorke Leec3766332013-07-31 11:13:16 -0700629 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700630 // If there is a call in progress go to the call screen
631 if (recentCallsRequest) {
632 final boolean callKey = intent.getBooleanExtra("call_key", false);
633
634 try {
635 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
636 if (callKey && phone != null && phone.showCallScreen()) {
637 return true;
638 }
639 } catch (RemoteException e) {
640 Log.e(TAG, "Failed to handle send while in call", e);
641 }
642 }
643
644 return false;
645 }
646
647 /**
648 * Sets the current tab based on the intent's request type
649 *
650 * @param intent Intent that contains information about which tab should be selected
651 */
Yorke Leec3766332013-07-31 11:13:16 -0700652 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700653 // If we got here by hitting send and we're in call forward along to the in-call activity
654 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
655 getContentResolver()));
656 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
657 finish();
658 return;
659 }
660
Yorke Lee669b6082013-09-17 15:43:38 -0700661 if (mDialpadFragment != null) {
662 final boolean phoneIsInUse = phoneIsInUse();
663 if (phoneIsInUse || isDialIntent(intent)) {
664 mDialpadFragment.setStartedFromNewIntent(true);
665 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
666 mInCallDialpadUp = true;
667 }
668 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700669 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700670 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700671 }
672
673 @Override
674 public void onNewIntent(Intent newIntent) {
675 setIntent(newIntent);
676 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700677 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700678 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700679
Chiao Cheng94b10b52012-08-17 16:59:12 -0700680 invalidateOptionsMenu();
681 }
682
683 /** Returns true if the given intent contains a phone number to populate the dialer with */
684 private boolean isDialIntent(Intent intent) {
685 final String action = intent.getAction();
686 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
687 return true;
688 }
689 if (Intent.ACTION_VIEW.equals(action)) {
690 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700691 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700692 return true;
693 }
694 }
695 return false;
696 }
697
698 /**
699 * Returns an appropriate call origin for this Activity. May return null when no call origin
700 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
701 * for remembering the tab in which the user made a phone call, so the external app's DIAL
702 * request should not be counted.)
703 */
704 public String getCallOrigin() {
705 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
706 }
707
708 /**
709 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
710 * This text originally came from a FILTER_CONTACTS_ACTION intent received
711 * by this activity. The stored text will then be cleared after after this
712 * method returns.
713 *
714 * @return The stored filter text
715 */
716 public String getAndClearFilterText() {
717 String filterText = mFilterText;
718 mFilterText = null;
719 return filterText;
720 }
721
722 /**
723 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
724 * This is so child activities can check if they are supposed to display a filter.
725 *
726 * @param intent The intent received in {@link #onNewIntent(Intent)}
727 */
728 private void setupFilterText(Intent intent) {
729 // If the intent was relaunched from history, don't apply the filter text.
730 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
731 return;
732 }
733 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
734 if (filter != null && filter.length() > 0) {
735 mFilterText = filter;
736 }
737 }
738
Yorke Lee8898cd02013-08-08 10:24:27 -0700739 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
740 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700741 @Override
742 public void onContactSelected(Uri contactUri) {
743 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700744 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700745 }
746
747 @Override
748 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700749 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700750 startActivity(intent);
751 }
752 };
753
Yorke Leec3766332013-07-31 11:13:16 -0700754 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
755 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
756 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700757
758 @Override
759 public void startSearch(String initialQuery, boolean selectInitialQuery,
760 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700761 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700762 if (mInSearchUi) {
763 if (mSearchView.hasFocus()) {
764 showInputMethod(mSearchView.findFocus());
765 } else {
766 mSearchView.requestFocus();
767 }
768 } else {
769 enterSearchUi();
770 }
771 } else {
772 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
773 }
Yorke Leec3766332013-07-31 11:13:16 -0700774 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700775
776 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700777 final InputMethodManager imm = (InputMethodManager) getSystemService(
778 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700779 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700780 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700781 }
782 }
783
784 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700785 final InputMethodManager imm = (InputMethodManager) getSystemService(
786 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700787 if (imm != null && view != null) {
788 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
789 }
790 }
791
792 /**
Yorke Leec3766332013-07-31 11:13:16 -0700793 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700794 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700795 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700796 if (getFragmentManager().isDestroyed()) {
797 // Weird race condition where fragment is doing work after the activity is destroyed
798 // due to talkback being on (b/10209937). Just return since we can't do any
799 // constructive here.
800 return;
801 }
802
Yorke Leeef2b7382013-08-09 17:39:25 -0700803 if (DEBUG) {
804 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
805 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700806
Yorke Leec3766332013-07-31 11:13:16 -0700807 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
808 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700809
Yorke Leeef2b7382013-08-09 17:39:25 -0700810 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700811 if (mInDialpadSearch) {
812 transaction.remove(mSmartDialSearchFragment);
813 } else if (mInRegularSearch) {
814 transaction.remove(mRegularSearchFragment);
815 } else {
816 transaction.remove(mPhoneFavoriteFragment);
817 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700818
Yorke Leeb207f8a2013-08-29 18:51:06 -0700819 final String tag;
820 if (smartDialSearch) {
821 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
822 } else {
823 tag = TAG_REGULAR_SEARCH_FRAGMENT;
824 }
825 mInDialpadSearch = smartDialSearch;
826 mInRegularSearch = !smartDialSearch;
827
Yorke Leeef2b7382013-08-09 17:39:25 -0700828 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
829 if (fragment == null) {
830 if (smartDialSearch) {
831 fragment = new SmartDialSearchFragment();
832 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700833 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700834 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700835 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700836 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700837 transaction.addToBackStack(null);
838 fragment.setQueryString(query, false);
839 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700840 }
841
842 /**
Yorke Leec3766332013-07-31 11:13:16 -0700843 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700844 */
Yorke Leec3766332013-07-31 11:13:16 -0700845 private void exitSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700846 // Go all the way back to the favorites fragment, regardless of how many times we
847 // transitioned between search fragments
848 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
849 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
850 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700851 }
852
853 /** Returns an Intent to launch Call Settings screen */
854 public static Intent getCallSettingsIntent() {
855 final Intent intent = new Intent(Intent.ACTION_MAIN);
856 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
857 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
858 return intent;
859 }
860
861 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700862 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700863 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700864 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700865 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700866 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700867 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700868 } else if (isTaskRoot()) {
869 // Instead of stopping, simply push this to the back of the stack.
870 // This is only done when running at the top of the stack;
871 // otherwise, we have been launched by someone else so need to
872 // allow the user to go back to the caller.
873 moveTaskToBack(false);
874 } else {
875 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700876 }
Yorke Leec3766332013-07-31 11:13:16 -0700877 }
878
879 @Override
880 public void onDialpadQueryChanged(String query) {
881 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
882 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
883 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700884 if (DEBUG) {
885 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
886 }
887 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
888 // This callback can happen if the dialpad fragment is recreated because of
889 // activity destruction. In that case, don't update the search view because
890 // that would bring the user back to the search fragment regardless of the
891 // previous state of the application. Instead, just return here and let the
892 // fragment manager correctly figure out whatever fragment was last displayed.
893 return;
894 }
Yorke Leec3766332013-07-31 11:13:16 -0700895 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700896 }
897 }
Yorke Leec3766332013-07-31 11:13:16 -0700898
899 @Override
900 public void onListFragmentScrollStateChange(int scrollState) {
901 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700902 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700903 hideInputMethod(getCurrentFocus());
904 }
905 }
906
907 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700908 public void onDialpadFragmentStarted() {
909 setupFakeActionBarItemsForDialpadFragment();
910 }
911
912 private boolean phoneIsInUse() {
913 final TelephonyManager tm = (TelephonyManager) getSystemService(
914 Context.TELEPHONY_SERVICE);
915 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
916 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700917
918 @Override
919 public void onShowAllContacts() {
920 final Intent intent = new Intent(this, AllContactsActivity.class);
921 startActivity(intent);
922 }
Yorke Leee1424812013-09-04 18:24:48 -0700923
924 public static Intent getAddNumberToContactIntent(CharSequence text) {
925 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
926 intent.putExtra(Intents.Insert.PHONE, text);
927 intent.setType(Contacts.CONTENT_ITEM_TYPE);
928 return intent;
929 }
930
931 public static Intent getInsertContactWithNameIntent(CharSequence text) {
932 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
933 intent.putExtra(Intents.Insert.NAME, text);
934 return intent;
935 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700936}