blob: 0c0fcdafedad5b23982656b73cc0aed6a16546e4 [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;
Yorke Leeda0f9042013-12-05 14:25:51 -080030import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
Yorke Lee41a24b42013-11-13 16:14:41 -080032import android.graphics.drawable.Drawable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070033import android.net.Uri;
34import android.os.Bundle;
35import android.os.RemoteException;
36import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037import android.provider.CallLog.Calls;
38import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070039import android.provider.ContactsContract.Intents;
Chiao Cheng94b10b52012-08-17 16:59:12 -070040import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070041import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070042import android.telephony.TelephonyManager;
43import android.text.Editable;
Yorke Lee41a24b42013-11-13 16:14:41 -080044import android.text.Spannable;
45import android.text.SpannableStringBuilder;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070047import android.text.TextWatcher;
Yorke Lee41a24b42013-11-13 16:14:41 -080048import android.text.style.ImageSpan;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.util.Log;
50import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070052import android.view.View;
Yorke Leeda0f9042013-12-05 14:25:51 -080053import android.view.View.OnClickListener;
Chiao Cheng94b10b52012-08-17 16:59:12 -070054import android.view.View.OnFocusChangeListener;
Chiao Cheng94b10b52012-08-17 16:59:12 -070055import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070056import android.widget.AbsListView.OnScrollListener;
57import android.widget.EditText;
Chiao Cheng94b10b52012-08-17 16:59:12 -070058import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080059import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070060
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070061import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070062import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070063import com.android.contacts.common.dialog.ClearFrequentsDialog;
64import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080065import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070066import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070067import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070068import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070069import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070070import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070071import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070072import com.android.dialer.list.AllContactsActivity;
Yorke Leec3766332013-07-31 11:13:16 -070073import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070074import com.android.dialer.list.PhoneFavoriteFragment;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070075import com.android.dialer.list.RegularSearchFragment;
76import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070077import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070078import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070079import com.android.internal.telephony.ITelephony;
80
Yorke Leec3766332013-07-31 11:13:16 -070081import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080082import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070083
Chiao Cheng94b10b52012-08-17 16:59:12 -070084/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070085 * The dialer tab's title is 'phone', a more common name (see strings.xml).
86 */
Yorke Leec3766332013-07-31 11:13:16 -070087public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070088 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070089 OnListFragmentScrolledListener,
Yorke Lee8dd62002013-08-08 15:57:20 -070090 DialpadFragment.OnDialpadFragmentStartedListener,
91 PhoneFavoriteFragment.OnShowAllContactsListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070092 private static final String TAG = "DialtactsActivity";
93
94 public static final boolean DEBUG = false;
95
Yorke Leef74011e2013-08-02 11:30:30 -070096 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
97
Chiao Cheng94b10b52012-08-17 16:59:12 -070098 /** Used to open Call Setting */
99 private static final String PHONE_PACKAGE = "com.android.phone";
100 private static final String CALL_SETTINGS_CLASS_NAME =
101 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700102 /** @see #getCallOrigin() */
103 private static final String CALL_ORIGIN_DIALTACTS =
104 "com.android.dialer.DialtactsActivity";
105
Yorke Leeb207f8a2013-08-29 18:51:06 -0700106 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
107 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700108 private static final String KEY_SEARCH_QUERY = "search_query";
109 private static final String KEY_FIRST_LAUNCH = "first_launch";
110
Yorke Leec3766332013-07-31 11:13:16 -0700111 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
112 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
113 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
114 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700115
Chiao Cheng94b10b52012-08-17 16:59:12 -0700116 /**
117 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
118 */
119 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
120
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
122
Yorke Leec3766332013-07-31 11:13:16 -0700123 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700124
125 private String mFilterText;
126
Yorke Leec3766332013-07-31 11:13:16 -0700127 /**
128 * The main fragment displaying the user's favorites and frequent contacts
129 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700130 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700131
132 /**
Yorke Leec3766332013-07-31 11:13:16 -0700133 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700134 */
Yorke Leef74011e2013-08-02 11:30:30 -0700135 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700136
137 /**
138 * Fragment for searching phone numbers using the alphanumeric keyboard.
139 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700140 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700141
142 /**
143 * Fragment for searching phone numbers using the dialpad.
144 */
145 private SmartDialSearchFragment mSmartDialSearchFragment;
146
Yorke Leec3766332013-07-31 11:13:16 -0700147 private View mMenuButton;
148 private View mCallHistoryButton;
149 private View mDialpadButton;
Yorke Lee36c54132013-09-25 16:17:06 -0700150 private PopupMenu mOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700151
152 // Padding view used to shift the fragments up when the dialpad is shown.
153 private View mBottomPaddingView;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700154 private View mFragmentsFrame;
Yorke Lee5f3ad542013-10-08 08:14:36 -0700155 private View mActionBar;
Yorke Leec3766332013-07-31 11:13:16 -0700156
Yorke Leeb207f8a2013-08-29 18:51:06 -0700157 private boolean mInDialpadSearch;
158 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700159 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700160
Yorke Leeef2b7382013-08-09 17:39:25 -0700161 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700162 * True if the dialpad is only temporarily showing due to being in call
163 */
164 private boolean mInCallDialpadUp;
165
166 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700167 * True when this activity has been launched for the first time.
168 */
Yorke Lee98702de2013-08-06 12:03:32 -0700169 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700170 private View mSearchViewContainer;
171 private View mSearchViewCloseButton;
172 private View mVoiceSearchButton;
173 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700174
Yorke Leeef2b7382013-08-09 17:39:25 -0700175 private String mSearchQuery;
176
Yorke Leefce269a2013-08-12 11:56:04 -0700177 private DialerDatabaseHelper mDialerDatabaseHelper;
178
Yorke Lee36c54132013-09-25 16:17:06 -0700179 private class OverflowPopupMenu extends PopupMenu {
180 public OverflowPopupMenu(Context context, View anchor) {
181 super(context, anchor);
182 }
183
184 @Override
185 public void show() {
186 final Menu menu = getMenu();
187 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
188 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
189 super.show();
190 }
191 }
192
Chiao Cheng94b10b52012-08-17 16:59:12 -0700193 /**
194 * Listener used when one of phone numbers in search UI is selected. This will initiate a
195 * phone call using the phone number.
196 */
197 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
198 new OnPhoneNumberPickerActionListener() {
199 @Override
200 public void onPickPhoneNumberAction(Uri dataUri) {
201 // Specify call-origin so that users will see the previous tab instead of
202 // CallLog screen (search UI will be automatically exited).
203 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700204 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700205 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700206 }
207
208 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700209 public void onCallNumberDirectly(String phoneNumber) {
210 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
211 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700212 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700213 }
214
215 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700216 public void onShortcutIntentCreated(Intent intent) {
217 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
218 }
219
220 @Override
221 public void onHomeInActionBarSelected() {
222 exitSearchUi();
223 }
224 };
225
226 /**
227 * Listener used to send search queries to the phone search fragment.
228 */
Yorke Leec3766332013-07-31 11:13:16 -0700229 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
230 @Override
231 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
232 }
233
234 @Override
235 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700236 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700237 if (newText.equals(mSearchQuery)) {
238 // If the query hasn't changed (perhaps due to activity being destroyed
239 // and restored, or user launching the same DIAL intent twice), then there is
240 // no need to do anything here.
241 return;
242 }
243 mSearchQuery = newText;
244 if (DEBUG) {
245 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
246 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700247 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700248
Yorke Leec3766332013-07-31 11:13:16 -0700249 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700250 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700251 exitSearchUi();
252 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700253 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700254 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700255 } else if (!TextUtils.isEmpty(newText)) {
256 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
257 (!dialpadSearch && mInRegularSearch);
258 if (!sameSearchMode) {
259 // call enterSearchUi only if we are switching search modes, or entering
260 // search ui for the first time
261 enterSearchUi(dialpadSearch, newText);
262 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700263
Yorke Leeb207f8a2013-08-29 18:51:06 -0700264 if (dialpadSearch && mSmartDialSearchFragment != null) {
265 mSmartDialSearchFragment.setQueryString(newText, false);
266 } else if (mRegularSearchFragment != null) {
267 mRegularSearchFragment.setQueryString(newText, false);
268 }
269 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700270 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700271 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700272 }
Yorke Leec3766332013-07-31 11:13:16 -0700273 }
274
275 @Override
276 public void afterTextChanged(Editable s) {
277 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700278 };
279
Yorke Leec3766332013-07-31 11:13:16 -0700280 private boolean isDialpadShowing() {
281 return mDialpadFragment != null && mDialpadFragment.isVisible();
282 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700283
284 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700285 protected void onCreate(Bundle savedInstanceState) {
286 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700287 mFirstLaunch = true;
288
Chiao Cheng94b10b52012-08-17 16:59:12 -0700289 final Intent intent = getIntent();
290 fixIntent(intent);
291
Yorke Lee8898cd02013-08-08 10:24:27 -0700292 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700293
Yorke Leec3766332013-07-31 11:13:16 -0700294 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700295
Yorke Leeef2b7382013-08-09 17:39:25 -0700296 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
297 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700298 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700299 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700300
Yorke Leec3766332013-07-31 11:13:16 -0700301 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700302 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
303 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700304 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700305 } else {
306 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700307 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
308 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700309 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700310 }
311
Yorke Leec3766332013-07-31 11:13:16 -0700312 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700313 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700314 mActionBar = findViewById(R.id.fake_action_bar);
Yorke Leec3766332013-07-31 11:13:16 -0700315 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700316
Chiao Cheng94b10b52012-08-17 16:59:12 -0700317 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700318 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700319 setupFilterText(intent);
320 }
Yorke Leefce269a2013-08-12 11:56:04 -0700321
Yorke Leeb207f8a2013-08-29 18:51:06 -0700322 setupFakeActionBarItems();
323
Yorke Lee0baa98b2013-08-22 10:55:16 -0700324 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700325 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700326 }
327
328 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700329 protected void onResume() {
330 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700331 if (mFirstLaunch) {
332 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700333 } else if (!phoneIsInUse() && mInCallDialpadUp) {
334 hideDialpadFragment(false, true);
335 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700336 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800337 prepareVoiceSearchButton();
Yorke Lee98702de2013-08-06 12:03:32 -0700338 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700339 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700340 }
341
342 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700343 protected void onPause() {
344 if (mClearSearchOnPause) {
345 hideDialpadAndSearchUi();
346 mClearSearchOnPause = false;
347 }
348 super.onPause();
349 }
350
351 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700352 protected void onSaveInstanceState(Bundle outState) {
353 super.onSaveInstanceState(outState);
354 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700355 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
356 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700357 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
358 }
359
360 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700361 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700362 if (fragment instanceof DialpadFragment) {
363 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700364 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700365 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700366 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700367 } else if (fragment instanceof SmartDialSearchFragment) {
368 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700369 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
370 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700371 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700372 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700373 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
374 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700375 } else if (fragment instanceof PhoneFavoriteFragment) {
376 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700377 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700378 }
Yorke Leec3766332013-07-31 11:13:16 -0700379 }
380
381 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700382 public boolean onMenuItemClick(MenuItem item) {
383 switch (item.getItemId()) {
384 case R.id.menu_import_export:
385 // We hard-code the "contactsAreAvailable" argument because doing it properly would
386 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
387 // now in Dialtacts for (potential) performance reasons. Compare with how it is
388 // done in {@link PeopleActivity}.
389 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700390 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700391 return true;
392 case R.id.menu_clear_frequents:
393 ClearFrequentsDialog.show(getFragmentManager());
394 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700395 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700396 try {
397 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
398 } catch (ActivityNotFoundException e) {
399 Toast toast = Toast.makeText(this,
400 R.string.add_contact_not_available,
401 Toast.LENGTH_SHORT);
402 toast.show();
403 }
404 return true;
405 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700406 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700407 return true;
408 case R.id.menu_all_contacts:
409 onShowAllContacts();
410 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700411 }
412 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700413 }
414
Alon Albertb453e5b2013-09-10 15:54:23 -0700415 protected void handleMenuSettings() {
416 openTelephonySetting(this);
417 }
418
419 public static void openTelephonySetting(Activity activity) {
420 final Intent settingsIntent = getCallSettingsIntent();
421 activity.startActivity(settingsIntent);
422 }
423
Chiao Cheng94b10b52012-08-17 16:59:12 -0700424 @Override
425 public void onClick(View view) {
426 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700427 case R.id.overflow_menu: {
Yorke Lee36c54132013-09-25 16:17:06 -0700428 mOverflowMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700429 break;
430 }
Yorke Leec3766332013-07-31 11:13:16 -0700431 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700432 // Reset the boolean flag that tracks whether the dialpad was up because
433 // we were in call. Regardless of whether it was true before, we want to
434 // show the dialpad because the user has explicitly clicked the dialpad
435 // button.
436 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700437 showDialpadFragment(true);
438 break;
439 case R.id.call_history_on_dialpad_button:
440 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700441 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
442 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700443 final Intent intent = new Intent(this, CallLogActivity.class);
444 startActivity(intent);
445 break;
446 case R.id.search_close_button:
447 // Clear the search field
448 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700449 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700450 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700451 }
452 break;
Yorke Leec3766332013-07-31 11:13:16 -0700453 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800454 try {
455 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
456 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
457 } catch (ActivityNotFoundException e) {
458 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
459 Toast.LENGTH_SHORT).show();
460 }
Yorke Leec3766332013-07-31 11:13:16 -0700461 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700462 default: {
463 Log.wtf(TAG, "Unexpected onClick event from " + view);
464 break;
465 }
466 }
467 }
468
Yorke Leec3766332013-07-31 11:13:16 -0700469 @Override
470 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
471 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
472 if (resultCode == RESULT_OK) {
473 final ArrayList<String> matches = data.getStringArrayListExtra(
474 RecognizerIntent.EXTRA_RESULTS);
475 if (matches.size() > 0) {
476 final String match = matches.get(0);
477 mSearchView.setText(match);
478 } else {
479 Log.e(TAG, "Voice search - nothing heard");
480 }
481 } else {
482 Log.e(TAG, "Voice search failed");
483 }
484 }
485 super.onActivityResult(requestCode, resultCode, data);
486 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700487
Yorke Leec3766332013-07-31 11:13:16 -0700488 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700489 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700490 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700491 if (animate) {
492 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700493 } else {
494 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700495 }
496 ft.show(mDialpadFragment);
497 ft.commit();
498 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700499
Yorke Leeca195042013-09-25 16:29:38 -0700500 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700501 if (mDialpadFragment == null) return;
502 if (clearDialpad) {
503 mDialpadFragment.clearDialpad();
504 }
505 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700506 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700507 final FragmentTransaction ft = getFragmentManager().beginTransaction();
508 if (animate) {
509 ft.setCustomAnimations(0, R.anim.slide_out);
510 }
511 ft.hide(mDialpadFragment);
512 ft.commit();
513 }
514
Chiao Cheng94b10b52012-08-17 16:59:12 -0700515 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700516 mSearchViewContainer = findViewById(R.id.search_view_container);
517 mSearchViewCloseButton = findViewById(R.id.search_close_button);
518 mSearchViewCloseButton.setOnClickListener(this);
Yorke Leeda0f9042013-12-05 14:25:51 -0800519
Yorke Leec3766332013-07-31 11:13:16 -0700520 mSearchView = (EditText) findViewById(R.id.search_view);
521 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Yorke Lee41a24b42013-11-13 16:14:41 -0800522
523 final String hintText = getString(R.string.dialer_hint_find_contact);
524
525 // The following code is used to insert an icon into a CharSequence (copied from
526 // SearchView)
527 final SpannableStringBuilder ssb = new SpannableStringBuilder(" "); // for the icon
528 ssb.append(hintText);
529 final Drawable searchIcon = getResources().getDrawable(R.drawable.ic_ab_search);
530 final int textSize = (int) (mSearchView.getTextSize() * 1.20);
531 searchIcon.setBounds(0, 0, textSize, textSize);
532 ssb.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
533
534 mSearchView.setHint(ssb);
Yorke Leeda0f9042013-12-05 14:25:51 -0800535
536 prepareVoiceSearchButton();
537 }
538
539 private void prepareVoiceSearchButton() {
540 mVoiceSearchButton = findViewById(R.id.voice_search_button);
541 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
542 if (canIntentBeHandled(voiceIntent)) {
543 mVoiceSearchButton.setVisibility(View.VISIBLE);
544 mVoiceSearchButton.setOnClickListener(this);
545 } else {
546 mVoiceSearchButton.setVisibility(View.GONE);
547 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700548 }
549
Yorke Leec3766332013-07-31 11:13:16 -0700550 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
551 @Override
552 public void onAnimationEnd(Animator animation) {
553 mSearchViewContainer.setVisibility(View.GONE);
554 }
555 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700556
Yorke Leeb207f8a2013-08-29 18:51:06 -0700557 private boolean getInSearchUi() {
558 return mInDialpadSearch || mInRegularSearch;
559 }
560
561 private void setNotInSearchUi() {
562 mInDialpadSearch = false;
563 mInRegularSearch = false;
564 }
565
Yorke Lee311969c2013-08-26 06:31:11 -0700566 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700567 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700568 hideDialpadFragment(false, true);
569 }
570
Yorke Leec3766332013-07-31 11:13:16 -0700571 public void hideSearchBar() {
572 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700573 }
574
Yorke Leec3766332013-07-31 11:13:16 -0700575 public void hideSearchBar(boolean shiftView) {
576 if (shiftView) {
577 mSearchViewContainer.animate().cancel();
578 mSearchViewContainer.setAlpha(1);
579 mSearchViewContainer.setTranslationY(0);
580 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
581 .setDuration(200).setListener(mHideListener);
582
Yorke Leeb207f8a2013-08-29 18:51:06 -0700583 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700584 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
585 new AnimatorListenerAdapter() {
586 @Override
587 public void onAnimationEnd(Animator animation) {
588 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700589 mFragmentsFrame.setTranslationY(0);
590 mActionBar.setVisibility(View.INVISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700591 }
592 });
593 } else {
594 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
Yorke Lee5f3ad542013-10-08 08:14:36 -0700595 mActionBar.setVisibility(View.INVISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700596 }
597 }
598
599 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700600 mSearchViewContainer.animate().cancel();
601 mSearchViewContainer.setAlpha(0);
602 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
603 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
604 .setListener(new AnimatorListenerAdapter() {
605 @Override
606 public void onAnimationStart(Animator animation) {
607 mSearchViewContainer.setVisibility(View.VISIBLE);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700608 mActionBar.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700609 }
610 });
611
Yorke Leeb207f8a2013-08-29 18:51:06 -0700612 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
613 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700614 .setListener(
615 new AnimatorListenerAdapter() {
616 @Override
617 public void onAnimationStart(Animator animation) {
618 mBottomPaddingView.setVisibility(View.GONE);
619 }
620 });
621 }
622
623
Yorke Leeb207f8a2013-08-29 18:51:06 -0700624 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700625 mMenuButton = findViewById(R.id.overflow_menu);
626 if (mMenuButton != null) {
627 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700628
629 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
630 final Menu menu = mOverflowMenu.getMenu();
631 mOverflowMenu.inflate(R.menu.dialtacts_options);
632 mOverflowMenu.setOnMenuItemClickListener(this);
633 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700634 }
635
636 mCallHistoryButton = findViewById(R.id.call_history_button);
637 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
638 mCallHistoryButton.setOnClickListener(this);
639
640 mDialpadButton = findViewById(R.id.dialpad_button);
641 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
642 mDialpadButton.setOnClickListener(this);
643 }
644
645 public void setupFakeActionBarItemsForDialpadFragment() {
646 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
647 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700648 }
649
650 private void fixIntent(Intent intent) {
651 // This should be cleaned up: the call key used to send an Intent
652 // that just said to go to the recent calls list. It now sends this
653 // abstract action, but this class hasn't been rewritten to deal with it.
654 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
655 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
656 intent.putExtra("call_key", true);
657 setIntent(intent);
658 }
659 }
660
Chiao Cheng94b10b52012-08-17 16:59:12 -0700661 /**
662 * Returns true if the intent is due to hitting the green send key (hardware call button:
663 * KEYCODE_CALL) while in a call.
664 *
665 * @param intent the intent that launched this activity
666 * @param recentCallsRequest true if the intent is requesting to view recent calls
667 * @return true if the intent is due to hitting the green send key while in a call
668 */
Yorke Leec3766332013-07-31 11:13:16 -0700669 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700670 // If there is a call in progress go to the call screen
671 if (recentCallsRequest) {
672 final boolean callKey = intent.getBooleanExtra("call_key", false);
673
674 try {
675 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
676 if (callKey && phone != null && phone.showCallScreen()) {
677 return true;
678 }
679 } catch (RemoteException e) {
680 Log.e(TAG, "Failed to handle send while in call", e);
681 }
682 }
683
684 return false;
685 }
686
687 /**
688 * Sets the current tab based on the intent's request type
689 *
690 * @param intent Intent that contains information about which tab should be selected
691 */
Yorke Leec3766332013-07-31 11:13:16 -0700692 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700693 // If we got here by hitting send and we're in call forward along to the in-call activity
694 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
695 getContentResolver()));
696 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
697 finish();
698 return;
699 }
700
Yorke Lee669b6082013-09-17 15:43:38 -0700701 if (mDialpadFragment != null) {
702 final boolean phoneIsInUse = phoneIsInUse();
703 if (phoneIsInUse || isDialIntent(intent)) {
704 mDialpadFragment.setStartedFromNewIntent(true);
705 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
706 mInCallDialpadUp = true;
707 }
708 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700709 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700710 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700711 }
712
713 @Override
714 public void onNewIntent(Intent newIntent) {
715 setIntent(newIntent);
716 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700717 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700718 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700719
Chiao Cheng94b10b52012-08-17 16:59:12 -0700720 invalidateOptionsMenu();
721 }
722
723 /** Returns true if the given intent contains a phone number to populate the dialer with */
724 private boolean isDialIntent(Intent intent) {
725 final String action = intent.getAction();
726 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
727 return true;
728 }
729 if (Intent.ACTION_VIEW.equals(action)) {
730 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700731 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700732 return true;
733 }
734 }
735 return false;
736 }
737
738 /**
739 * Returns an appropriate call origin for this Activity. May return null when no call origin
740 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
741 * for remembering the tab in which the user made a phone call, so the external app's DIAL
742 * request should not be counted.)
743 */
744 public String getCallOrigin() {
745 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
746 }
747
748 /**
749 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
750 * This text originally came from a FILTER_CONTACTS_ACTION intent received
751 * by this activity. The stored text will then be cleared after after this
752 * method returns.
753 *
754 * @return The stored filter text
755 */
756 public String getAndClearFilterText() {
757 String filterText = mFilterText;
758 mFilterText = null;
759 return filterText;
760 }
761
762 /**
763 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
764 * This is so child activities can check if they are supposed to display a filter.
765 *
766 * @param intent The intent received in {@link #onNewIntent(Intent)}
767 */
768 private void setupFilterText(Intent intent) {
769 // If the intent was relaunched from history, don't apply the filter text.
770 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
771 return;
772 }
773 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
774 if (filter != null && filter.length() > 0) {
775 mFilterText = filter;
776 }
777 }
778
Yorke Lee8898cd02013-08-08 10:24:27 -0700779 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
780 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700781 @Override
782 public void onContactSelected(Uri contactUri) {
783 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700784 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700785 }
786
787 @Override
788 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700789 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700790 startActivity(intent);
791 }
792 };
793
Yorke Leec3766332013-07-31 11:13:16 -0700794 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
795 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
796 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700797
798 @Override
799 public void startSearch(String initialQuery, boolean selectInitialQuery,
800 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700801 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700802 if (mInSearchUi) {
803 if (mSearchView.hasFocus()) {
804 showInputMethod(mSearchView.findFocus());
805 } else {
806 mSearchView.requestFocus();
807 }
808 } else {
809 enterSearchUi();
810 }
811 } else {
812 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
813 }
Yorke Leec3766332013-07-31 11:13:16 -0700814 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700815
816 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700817 final InputMethodManager imm = (InputMethodManager) getSystemService(
818 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700819 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700820 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700821 }
822 }
823
824 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700825 final InputMethodManager imm = (InputMethodManager) getSystemService(
826 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700827 if (imm != null && view != null) {
828 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
829 }
830 }
831
832 /**
Yorke Leec3766332013-07-31 11:13:16 -0700833 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700834 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700835 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700836 if (getFragmentManager().isDestroyed()) {
837 // Weird race condition where fragment is doing work after the activity is destroyed
838 // due to talkback being on (b/10209937). Just return since we can't do any
839 // constructive here.
840 return;
841 }
842
Yorke Leeef2b7382013-08-09 17:39:25 -0700843 if (DEBUG) {
844 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
845 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700846
Yorke Leec3766332013-07-31 11:13:16 -0700847 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
848 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700849
Yorke Leeef2b7382013-08-09 17:39:25 -0700850 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700851 if (mInDialpadSearch) {
852 transaction.remove(mSmartDialSearchFragment);
853 } else if (mInRegularSearch) {
854 transaction.remove(mRegularSearchFragment);
855 } else {
856 transaction.remove(mPhoneFavoriteFragment);
857 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700858
Yorke Leeb207f8a2013-08-29 18:51:06 -0700859 final String tag;
860 if (smartDialSearch) {
861 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
862 } else {
863 tag = TAG_REGULAR_SEARCH_FRAGMENT;
864 }
865 mInDialpadSearch = smartDialSearch;
866 mInRegularSearch = !smartDialSearch;
867
Yorke Leeef2b7382013-08-09 17:39:25 -0700868 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
869 if (fragment == null) {
870 if (smartDialSearch) {
871 fragment = new SmartDialSearchFragment();
872 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700873 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700874 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700875 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700876 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700877 transaction.addToBackStack(null);
878 fragment.setQueryString(query, false);
879 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700880 }
881
882 /**
Yorke Leec3766332013-07-31 11:13:16 -0700883 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700884 */
Yorke Leec3766332013-07-31 11:13:16 -0700885 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700886 // See related bug in enterSearchUI();
887 if (getFragmentManager().isDestroyed()) {
888 return;
889 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700890 // Go all the way back to the favorites fragment, regardless of how many times we
891 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700892 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
893 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700894 }
895
896 /** Returns an Intent to launch Call Settings screen */
897 public static Intent getCallSettingsIntent() {
898 final Intent intent = new Intent(Intent.ACTION_MAIN);
899 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
900 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
901 return intent;
902 }
903
904 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700905 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700906 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700907 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700908 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700909 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700910 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700911 } else {
912 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700913 }
Yorke Leec3766332013-07-31 11:13:16 -0700914 }
915
916 @Override
917 public void onDialpadQueryChanged(String query) {
918 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
919 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
920 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700921 if (DEBUG) {
922 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
923 }
924 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
925 // This callback can happen if the dialpad fragment is recreated because of
926 // activity destruction. In that case, don't update the search view because
927 // that would bring the user back to the search fragment regardless of the
928 // previous state of the application. Instead, just return here and let the
929 // fragment manager correctly figure out whatever fragment was last displayed.
930 return;
931 }
Yorke Leec3766332013-07-31 11:13:16 -0700932 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700933 }
934 }
Yorke Leec3766332013-07-31 11:13:16 -0700935
936 @Override
937 public void onListFragmentScrollStateChange(int scrollState) {
938 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700939 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700940 hideInputMethod(getCurrentFocus());
941 }
942 }
943
944 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700945 public void onDialpadFragmentStarted() {
946 setupFakeActionBarItemsForDialpadFragment();
947 }
948
949 private boolean phoneIsInUse() {
950 final TelephonyManager tm = (TelephonyManager) getSystemService(
951 Context.TELEPHONY_SERVICE);
952 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
953 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700954
955 @Override
956 public void onShowAllContacts() {
957 final Intent intent = new Intent(this, AllContactsActivity.class);
958 startActivity(intent);
959 }
Yorke Leee1424812013-09-04 18:24:48 -0700960
961 public static Intent getAddNumberToContactIntent(CharSequence text) {
962 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
963 intent.putExtra(Intents.Insert.PHONE, text);
964 intent.setType(Contacts.CONTENT_ITEM_TYPE);
965 return intent;
966 }
967
968 public static Intent getInsertContactWithNameIntent(CharSequence text) {
969 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
970 intent.putExtra(Intents.Insert.NAME, text);
971 return intent;
972 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800973
974 private boolean canIntentBeHandled(Intent intent) {
975 final PackageManager packageManager = getPackageManager();
976 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
977 PackageManager.MATCH_DEFAULT_ONLY);
978 return resolveInfo != null && resolveInfo.size() > 0;
979 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700980}