blob: 42cbbf4c114b09fad38f0a7932d996b954ccf155 [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;
32import android.os.RemoteException;
33import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.provider.CallLog.Calls;
35import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070036import android.provider.ContactsContract.Intents;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037import android.provider.ContactsContract.Intents.UI;
Yorke Leec3766332013-07-31 11:13:16 -070038import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070039import android.telephony.TelephonyManager;
40import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070042import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.util.Log;
44import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.View;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.View.OnFocusChangeListener;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070049import android.widget.AbsListView.OnScrollListener;
50import android.widget.EditText;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080052import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070054import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070055import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070056import com.android.contacts.common.dialog.ClearFrequentsDialog;
57import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080058import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070059import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070060import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070061import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070062import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070063import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070064import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070065import com.android.dialer.list.AllContactsActivity;
Yorke Leec3766332013-07-31 11:13:16 -070066import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070067import com.android.dialer.list.PhoneFavoriteFragment;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070068import com.android.dialer.list.RegularSearchFragment;
69import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070071import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070072import com.android.internal.telephony.ITelephony;
73
Yorke Leec3766332013-07-31 11:13:16 -070074import java.util.ArrayList;
75
Chiao Cheng94b10b52012-08-17 16:59:12 -070076/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070077 * The dialer tab's title is 'phone', a more common name (see strings.xml).
78 */
Yorke Leec3766332013-07-31 11:13:16 -070079public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070080 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070081 OnListFragmentScrolledListener,
Yorke Lee8dd62002013-08-08 15:57:20 -070082 DialpadFragment.OnDialpadFragmentStartedListener,
83 PhoneFavoriteFragment.OnShowAllContactsListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070084 private static final String TAG = "DialtactsActivity";
85
86 public static final boolean DEBUG = false;
87
Yorke Leef74011e2013-08-02 11:30:30 -070088 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
89
Chiao Cheng94b10b52012-08-17 16:59:12 -070090 /** Used to open Call Setting */
91 private static final String PHONE_PACKAGE = "com.android.phone";
92 private static final String CALL_SETTINGS_CLASS_NAME =
93 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -070094 /** @see #getCallOrigin() */
95 private static final String CALL_ORIGIN_DIALTACTS =
96 "com.android.dialer.DialtactsActivity";
97
Yorke Leeb207f8a2013-08-29 18:51:06 -070098 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
99 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700100 private static final String KEY_SEARCH_QUERY = "search_query";
101 private static final String KEY_FIRST_LAUNCH = "first_launch";
102
Yorke Leec3766332013-07-31 11:13:16 -0700103 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
104 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
105 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
106 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700107
Chiao Cheng94b10b52012-08-17 16:59:12 -0700108 /**
109 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
110 */
111 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
112
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
114
Yorke Leec3766332013-07-31 11:13:16 -0700115 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700116
117 private String mFilterText;
118
Yorke Leec3766332013-07-31 11:13:16 -0700119 /**
120 * The main fragment displaying the user's favorites and frequent contacts
121 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700122 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700123
124 /**
Yorke Leec3766332013-07-31 11:13:16 -0700125 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700126 */
Yorke Leef74011e2013-08-02 11:30:30 -0700127 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700128
129 /**
130 * Fragment for searching phone numbers using the alphanumeric keyboard.
131 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700132 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700133
134 /**
135 * Fragment for searching phone numbers using the dialpad.
136 */
137 private SmartDialSearchFragment mSmartDialSearchFragment;
138
Yorke Leec3766332013-07-31 11:13:16 -0700139 private View mMenuButton;
140 private View mCallHistoryButton;
141 private View mDialpadButton;
Yorke Lee36c54132013-09-25 16:17:06 -0700142 private PopupMenu mOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700143
144 // Padding view used to shift the fragments up when the dialpad is shown.
145 private View mBottomPaddingView;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700146 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700147
Yorke Leeb207f8a2013-08-29 18:51:06 -0700148 private boolean mInDialpadSearch;
149 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700150 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700151
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 Lee36c54132013-09-25 16:17:06 -0700170 private class OverflowPopupMenu extends PopupMenu {
171 public OverflowPopupMenu(Context context, View anchor) {
172 super(context, anchor);
173 }
174
175 @Override
176 public void show() {
177 final Menu menu = getMenu();
178 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
179 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
180 super.show();
181 }
182 }
183
Chiao Cheng94b10b52012-08-17 16:59:12 -0700184 /**
185 * Listener used when one of phone numbers in search UI is selected. This will initiate a
186 * phone call using the phone number.
187 */
188 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
189 new OnPhoneNumberPickerActionListener() {
190 @Override
191 public void onPickPhoneNumberAction(Uri dataUri) {
192 // Specify call-origin so that users will see the previous tab instead of
193 // CallLog screen (search UI will be automatically exited).
194 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700195 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700196 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700197 }
198
199 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700200 public void onCallNumberDirectly(String phoneNumber) {
201 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
202 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700203 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700204 }
205
206 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700207 public void onShortcutIntentCreated(Intent intent) {
208 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
209 }
210
211 @Override
212 public void onHomeInActionBarSelected() {
213 exitSearchUi();
214 }
215 };
216
217 /**
218 * Listener used to send search queries to the phone search fragment.
219 */
Yorke Leec3766332013-07-31 11:13:16 -0700220 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
221 @Override
222 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
223 }
224
225 @Override
226 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700227 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700228 if (newText.equals(mSearchQuery)) {
229 // If the query hasn't changed (perhaps due to activity being destroyed
230 // and restored, or user launching the same DIAL intent twice), then there is
231 // no need to do anything here.
232 return;
233 }
234 mSearchQuery = newText;
235 if (DEBUG) {
236 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
237 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700238 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700239
Yorke Leec3766332013-07-31 11:13:16 -0700240 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700241 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700242 exitSearchUi();
243 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700244 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700245 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700246 } else if (!TextUtils.isEmpty(newText)) {
247 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
248 (!dialpadSearch && mInRegularSearch);
249 if (!sameSearchMode) {
250 // call enterSearchUi only if we are switching search modes, or entering
251 // search ui for the first time
252 enterSearchUi(dialpadSearch, newText);
253 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700254
Yorke Leeb207f8a2013-08-29 18:51:06 -0700255 if (dialpadSearch && mSmartDialSearchFragment != null) {
256 mSmartDialSearchFragment.setQueryString(newText, false);
257 } else if (mRegularSearchFragment != null) {
258 mRegularSearchFragment.setQueryString(newText, false);
259 }
260 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700261 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700262 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700263 }
Yorke Leec3766332013-07-31 11:13:16 -0700264 }
265
266 @Override
267 public void afterTextChanged(Editable s) {
268 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700269 };
270
Yorke Leec3766332013-07-31 11:13:16 -0700271 private boolean isDialpadShowing() {
272 return mDialpadFragment != null && mDialpadFragment.isVisible();
273 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700274
275 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700276 protected void onCreate(Bundle savedInstanceState) {
277 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700278 mFirstLaunch = true;
279
Chiao Cheng94b10b52012-08-17 16:59:12 -0700280 final Intent intent = getIntent();
281 fixIntent(intent);
282
Yorke Lee8898cd02013-08-08 10:24:27 -0700283 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700284
Yorke Leec3766332013-07-31 11:13:16 -0700285 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700286
Yorke Leeef2b7382013-08-09 17:39:25 -0700287 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
288 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700289 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700290 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291
Yorke Leec3766332013-07-31 11:13:16 -0700292 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700293 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
294 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700295 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700296 } else {
297 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700298 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
299 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700300 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700301 }
302
Yorke Leec3766332013-07-31 11:13:16 -0700303 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700304 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leec3766332013-07-31 11:13:16 -0700305 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700306
Chiao Cheng94b10b52012-08-17 16:59:12 -0700307 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700308 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700309 setupFilterText(intent);
310 }
Yorke Leefce269a2013-08-12 11:56:04 -0700311
Yorke Leeb207f8a2013-08-29 18:51:06 -0700312 setupFakeActionBarItems();
313
Yorke Lee0baa98b2013-08-22 10:55:16 -0700314 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700315 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700316 }
317
318 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700319 protected void onResume() {
320 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700321 if (mFirstLaunch) {
322 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700323 } else if (!phoneIsInUse() && mInCallDialpadUp) {
324 hideDialpadFragment(false, true);
325 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700326 }
Yorke Lee35127cd2013-09-10 14:09:29 -0700327
Yorke Lee98702de2013-08-06 12:03:32 -0700328 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700329 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700330 }
331
332 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700333 protected void onPause() {
334 if (mClearSearchOnPause) {
335 hideDialpadAndSearchUi();
336 mClearSearchOnPause = false;
337 }
338 super.onPause();
339 }
340
341 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700342 protected void onSaveInstanceState(Bundle outState) {
343 super.onSaveInstanceState(outState);
344 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700345 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
346 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700347 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
348 }
349
350 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700351 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700352 if (fragment instanceof DialpadFragment) {
353 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700354 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700355 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700356 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700357 } else if (fragment instanceof SmartDialSearchFragment) {
358 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700359 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
360 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700361 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700362 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700363 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
364 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700365 } else if (fragment instanceof PhoneFavoriteFragment) {
366 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700367 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700368 }
Yorke Leec3766332013-07-31 11:13:16 -0700369 }
370
371 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700372 public boolean onMenuItemClick(MenuItem item) {
373 switch (item.getItemId()) {
374 case R.id.menu_import_export:
375 // We hard-code the "contactsAreAvailable" argument because doing it properly would
376 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
377 // now in Dialtacts for (potential) performance reasons. Compare with how it is
378 // done in {@link PeopleActivity}.
379 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700380 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700381 return true;
382 case R.id.menu_clear_frequents:
383 ClearFrequentsDialog.show(getFragmentManager());
384 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700385 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700386 try {
387 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
388 } catch (ActivityNotFoundException e) {
389 Toast toast = Toast.makeText(this,
390 R.string.add_contact_not_available,
391 Toast.LENGTH_SHORT);
392 toast.show();
393 }
394 return true;
395 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700396 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700397 return true;
398 case R.id.menu_all_contacts:
399 onShowAllContacts();
400 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700401 }
402 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700403 }
404
Alon Albertb453e5b2013-09-10 15:54:23 -0700405 protected void handleMenuSettings() {
406 openTelephonySetting(this);
407 }
408
409 public static void openTelephonySetting(Activity activity) {
410 final Intent settingsIntent = getCallSettingsIntent();
411 activity.startActivity(settingsIntent);
412 }
413
Chiao Cheng94b10b52012-08-17 16:59:12 -0700414 @Override
415 public void onClick(View view) {
416 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700417 case R.id.overflow_menu: {
Yorke Lee36c54132013-09-25 16:17:06 -0700418 mOverflowMenu.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));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700509 }
510
Yorke Leec3766332013-07-31 11:13:16 -0700511 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
512 @Override
513 public void onAnimationEnd(Animator animation) {
514 mSearchViewContainer.setVisibility(View.GONE);
515 }
516 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700517
Yorke Leeb207f8a2013-08-29 18:51:06 -0700518 private boolean getInSearchUi() {
519 return mInDialpadSearch || mInRegularSearch;
520 }
521
522 private void setNotInSearchUi() {
523 mInDialpadSearch = false;
524 mInRegularSearch = false;
525 }
526
Yorke Lee311969c2013-08-26 06:31:11 -0700527 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700528 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700529 hideDialpadFragment(false, true);
530 }
531
Yorke Leec3766332013-07-31 11:13:16 -0700532 public void hideSearchBar() {
533 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700534 }
535
Yorke Leec3766332013-07-31 11:13:16 -0700536 public void hideSearchBar(boolean shiftView) {
537 if (shiftView) {
538 mSearchViewContainer.animate().cancel();
539 mSearchViewContainer.setAlpha(1);
540 mSearchViewContainer.setTranslationY(0);
541 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
542 .setDuration(200).setListener(mHideListener);
543
Yorke Leeb207f8a2013-08-29 18:51:06 -0700544 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700545 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
546 new AnimatorListenerAdapter() {
547 @Override
548 public void onAnimationEnd(Animator animation) {
549 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700550 mFragmentsFrame.setTranslationY(0);
Yorke Leec3766332013-07-31 11:13:16 -0700551 }
552 });
553 } else {
554 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
555 }
556 }
557
558 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700559 mSearchViewContainer.animate().cancel();
560 mSearchViewContainer.setAlpha(0);
561 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
562 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
563 .setListener(new AnimatorListenerAdapter() {
564 @Override
565 public void onAnimationStart(Animator animation) {
566 mSearchViewContainer.setVisibility(View.VISIBLE);
567 }
568 });
569
Yorke Leeb207f8a2013-08-29 18:51:06 -0700570 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
571 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700572 .setListener(
573 new AnimatorListenerAdapter() {
574 @Override
575 public void onAnimationStart(Animator animation) {
576 mBottomPaddingView.setVisibility(View.GONE);
577 }
578 });
579 }
580
581
Yorke Leeb207f8a2013-08-29 18:51:06 -0700582 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700583 mMenuButton = findViewById(R.id.overflow_menu);
584 if (mMenuButton != null) {
585 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700586
587 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
588 final Menu menu = mOverflowMenu.getMenu();
589 mOverflowMenu.inflate(R.menu.dialtacts_options);
590 mOverflowMenu.setOnMenuItemClickListener(this);
591 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700592 }
593
594 mCallHistoryButton = findViewById(R.id.call_history_button);
595 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
596 mCallHistoryButton.setOnClickListener(this);
597
598 mDialpadButton = findViewById(R.id.dialpad_button);
599 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
600 mDialpadButton.setOnClickListener(this);
601 }
602
603 public void setupFakeActionBarItemsForDialpadFragment() {
604 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
605 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700606 }
607
608 private void fixIntent(Intent intent) {
609 // This should be cleaned up: the call key used to send an Intent
610 // that just said to go to the recent calls list. It now sends this
611 // abstract action, but this class hasn't been rewritten to deal with it.
612 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
613 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
614 intent.putExtra("call_key", true);
615 setIntent(intent);
616 }
617 }
618
Chiao Cheng94b10b52012-08-17 16:59:12 -0700619 /**
620 * Returns true if the intent is due to hitting the green send key (hardware call button:
621 * KEYCODE_CALL) while in a call.
622 *
623 * @param intent the intent that launched this activity
624 * @param recentCallsRequest true if the intent is requesting to view recent calls
625 * @return true if the intent is due to hitting the green send key while in a call
626 */
Yorke Leec3766332013-07-31 11:13:16 -0700627 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700628 // If there is a call in progress go to the call screen
629 if (recentCallsRequest) {
630 final boolean callKey = intent.getBooleanExtra("call_key", false);
631
632 try {
633 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
634 if (callKey && phone != null && phone.showCallScreen()) {
635 return true;
636 }
637 } catch (RemoteException e) {
638 Log.e(TAG, "Failed to handle send while in call", e);
639 }
640 }
641
642 return false;
643 }
644
645 /**
646 * Sets the current tab based on the intent's request type
647 *
648 * @param intent Intent that contains information about which tab should be selected
649 */
Yorke Leec3766332013-07-31 11:13:16 -0700650 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700651 // If we got here by hitting send and we're in call forward along to the in-call activity
652 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
653 getContentResolver()));
654 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
655 finish();
656 return;
657 }
658
Yorke Lee669b6082013-09-17 15:43:38 -0700659 if (mDialpadFragment != null) {
660 final boolean phoneIsInUse = phoneIsInUse();
661 if (phoneIsInUse || isDialIntent(intent)) {
662 mDialpadFragment.setStartedFromNewIntent(true);
663 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
664 mInCallDialpadUp = true;
665 }
666 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700667 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700668 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700669 }
670
671 @Override
672 public void onNewIntent(Intent newIntent) {
673 setIntent(newIntent);
674 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700675 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700676 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700677
Chiao Cheng94b10b52012-08-17 16:59:12 -0700678 invalidateOptionsMenu();
679 }
680
681 /** Returns true if the given intent contains a phone number to populate the dialer with */
682 private boolean isDialIntent(Intent intent) {
683 final String action = intent.getAction();
684 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
685 return true;
686 }
687 if (Intent.ACTION_VIEW.equals(action)) {
688 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700689 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700690 return true;
691 }
692 }
693 return false;
694 }
695
696 /**
697 * Returns an appropriate call origin for this Activity. May return null when no call origin
698 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
699 * for remembering the tab in which the user made a phone call, so the external app's DIAL
700 * request should not be counted.)
701 */
702 public String getCallOrigin() {
703 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
704 }
705
706 /**
707 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
708 * This text originally came from a FILTER_CONTACTS_ACTION intent received
709 * by this activity. The stored text will then be cleared after after this
710 * method returns.
711 *
712 * @return The stored filter text
713 */
714 public String getAndClearFilterText() {
715 String filterText = mFilterText;
716 mFilterText = null;
717 return filterText;
718 }
719
720 /**
721 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
722 * This is so child activities can check if they are supposed to display a filter.
723 *
724 * @param intent The intent received in {@link #onNewIntent(Intent)}
725 */
726 private void setupFilterText(Intent intent) {
727 // If the intent was relaunched from history, don't apply the filter text.
728 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
729 return;
730 }
731 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
732 if (filter != null && filter.length() > 0) {
733 mFilterText = filter;
734 }
735 }
736
Yorke Lee8898cd02013-08-08 10:24:27 -0700737 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
738 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700739 @Override
740 public void onContactSelected(Uri contactUri) {
741 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700742 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700743 }
744
745 @Override
746 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700747 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700748 startActivity(intent);
749 }
750 };
751
Yorke Leec3766332013-07-31 11:13:16 -0700752 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
753 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
754 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700755
756 @Override
757 public void startSearch(String initialQuery, boolean selectInitialQuery,
758 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700759 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700760 if (mInSearchUi) {
761 if (mSearchView.hasFocus()) {
762 showInputMethod(mSearchView.findFocus());
763 } else {
764 mSearchView.requestFocus();
765 }
766 } else {
767 enterSearchUi();
768 }
769 } else {
770 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
771 }
Yorke Leec3766332013-07-31 11:13:16 -0700772 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700773
774 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700775 final InputMethodManager imm = (InputMethodManager) getSystemService(
776 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700777 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700778 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700779 }
780 }
781
782 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700783 final InputMethodManager imm = (InputMethodManager) getSystemService(
784 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700785 if (imm != null && view != null) {
786 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
787 }
788 }
789
790 /**
Yorke Leec3766332013-07-31 11:13:16 -0700791 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700792 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700793 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700794 if (getFragmentManager().isDestroyed()) {
795 // Weird race condition where fragment is doing work after the activity is destroyed
796 // due to talkback being on (b/10209937). Just return since we can't do any
797 // constructive here.
798 return;
799 }
800
Yorke Leeef2b7382013-08-09 17:39:25 -0700801 if (DEBUG) {
802 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
803 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700804
Yorke Leec3766332013-07-31 11:13:16 -0700805 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
806 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700807
Yorke Leeef2b7382013-08-09 17:39:25 -0700808 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700809 if (mInDialpadSearch) {
810 transaction.remove(mSmartDialSearchFragment);
811 } else if (mInRegularSearch) {
812 transaction.remove(mRegularSearchFragment);
813 } else {
814 transaction.remove(mPhoneFavoriteFragment);
815 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700816
Yorke Leeb207f8a2013-08-29 18:51:06 -0700817 final String tag;
818 if (smartDialSearch) {
819 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
820 } else {
821 tag = TAG_REGULAR_SEARCH_FRAGMENT;
822 }
823 mInDialpadSearch = smartDialSearch;
824 mInRegularSearch = !smartDialSearch;
825
Yorke Leeef2b7382013-08-09 17:39:25 -0700826 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
827 if (fragment == null) {
828 if (smartDialSearch) {
829 fragment = new SmartDialSearchFragment();
830 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700831 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700832 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700833 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700834 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700835 transaction.addToBackStack(null);
836 fragment.setQueryString(query, false);
837 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700838 }
839
840 /**
Yorke Leec3766332013-07-31 11:13:16 -0700841 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700842 */
Yorke Leec3766332013-07-31 11:13:16 -0700843 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700844 // See related bug in enterSearchUI();
845 if (getFragmentManager().isDestroyed()) {
846 return;
847 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700848 // Go all the way back to the favorites fragment, regardless of how many times we
849 // transitioned between search fragments
850 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
851 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
852 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700853 }
854
855 /** Returns an Intent to launch Call Settings screen */
856 public static Intent getCallSettingsIntent() {
857 final Intent intent = new Intent(Intent.ACTION_MAIN);
858 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
859 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
860 return intent;
861 }
862
863 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700864 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700865 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700866 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700867 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700868 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700869 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700870 } else if (isTaskRoot()) {
871 // Instead of stopping, simply push this to the back of the stack.
872 // This is only done when running at the top of the stack;
873 // otherwise, we have been launched by someone else so need to
874 // allow the user to go back to the caller.
875 moveTaskToBack(false);
876 } else {
877 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700878 }
Yorke Leec3766332013-07-31 11:13:16 -0700879 }
880
881 @Override
882 public void onDialpadQueryChanged(String query) {
883 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
884 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
885 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700886 if (DEBUG) {
887 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
888 }
889 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
890 // This callback can happen if the dialpad fragment is recreated because of
891 // activity destruction. In that case, don't update the search view because
892 // that would bring the user back to the search fragment regardless of the
893 // previous state of the application. Instead, just return here and let the
894 // fragment manager correctly figure out whatever fragment was last displayed.
895 return;
896 }
Yorke Leec3766332013-07-31 11:13:16 -0700897 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700898 }
899 }
Yorke Leec3766332013-07-31 11:13:16 -0700900
901 @Override
902 public void onListFragmentScrollStateChange(int scrollState) {
903 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700904 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700905 hideInputMethod(getCurrentFocus());
906 }
907 }
908
909 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700910 public void onDialpadFragmentStarted() {
911 setupFakeActionBarItemsForDialpadFragment();
912 }
913
914 private boolean phoneIsInUse() {
915 final TelephonyManager tm = (TelephonyManager) getSystemService(
916 Context.TELEPHONY_SERVICE);
917 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
918 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700919
920 @Override
921 public void onShowAllContacts() {
922 final Intent intent = new Intent(this, AllContactsActivity.class);
923 startActivity(intent);
924 }
Yorke Leee1424812013-09-04 18:24:48 -0700925
926 public static Intent getAddNumberToContactIntent(CharSequence text) {
927 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
928 intent.putExtra(Intents.Insert.PHONE, text);
929 intent.setType(Contacts.CONTENT_ITEM_TYPE);
930 return intent;
931 }
932
933 public static Intent getInsertContactWithNameIntent(CharSequence text) {
934 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
935 intent.putExtra(Intents.Insert.NAME, text);
936 return intent;
937 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700938}