blob: 82d5c3feeaca44729fd3b118db06ece8d2a7198c [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 Lee5f3ad542013-10-08 08:14:36 -0700147 private View mActionBar;
Yorke Leec3766332013-07-31 11:13:16 -0700148
Yorke Leeb207f8a2013-08-29 18:51:06 -0700149 private boolean mInDialpadSearch;
150 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700151 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700152
Yorke Leeef2b7382013-08-09 17:39:25 -0700153 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700154 * True if the dialpad is only temporarily showing due to being in call
155 */
156 private boolean mInCallDialpadUp;
157
158 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700159 * True when this activity has been launched for the first time.
160 */
Yorke Lee98702de2013-08-06 12:03:32 -0700161 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700162 private View mSearchViewContainer;
163 private View mSearchViewCloseButton;
164 private View mVoiceSearchButton;
165 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700166
Yorke Leeef2b7382013-08-09 17:39:25 -0700167 private String mSearchQuery;
168
Yorke Leefce269a2013-08-12 11:56:04 -0700169 private DialerDatabaseHelper mDialerDatabaseHelper;
170
Yorke Lee36c54132013-09-25 16:17:06 -0700171 private class OverflowPopupMenu extends PopupMenu {
172 public OverflowPopupMenu(Context context, View anchor) {
173 super(context, anchor);
174 }
175
176 @Override
177 public void show() {
178 final Menu menu = getMenu();
179 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
180 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
181 super.show();
182 }
183 }
184
Chiao Cheng94b10b52012-08-17 16:59:12 -0700185 /**
186 * Listener used when one of phone numbers in search UI is selected. This will initiate a
187 * phone call using the phone number.
188 */
189 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
190 new OnPhoneNumberPickerActionListener() {
191 @Override
192 public void onPickPhoneNumberAction(Uri dataUri) {
193 // Specify call-origin so that users will see the previous tab instead of
194 // CallLog screen (search UI will be automatically exited).
195 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700196 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700197 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700198 }
199
200 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700201 public void onCallNumberDirectly(String phoneNumber) {
202 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
203 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700204 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700205 }
206
207 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700208 public void onShortcutIntentCreated(Intent intent) {
209 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
210 }
211
212 @Override
213 public void onHomeInActionBarSelected() {
214 exitSearchUi();
215 }
216 };
217
218 /**
219 * Listener used to send search queries to the phone search fragment.
220 */
Yorke Leec3766332013-07-31 11:13:16 -0700221 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
222 @Override
223 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
224 }
225
226 @Override
227 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700228 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700229 if (newText.equals(mSearchQuery)) {
230 // If the query hasn't changed (perhaps due to activity being destroyed
231 // and restored, or user launching the same DIAL intent twice), then there is
232 // no need to do anything here.
233 return;
234 }
235 mSearchQuery = newText;
236 if (DEBUG) {
237 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
238 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700239 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700240
Yorke Leec3766332013-07-31 11:13:16 -0700241 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700242 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700243 exitSearchUi();
244 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700245 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700246 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700247 } else if (!TextUtils.isEmpty(newText)) {
248 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
249 (!dialpadSearch && mInRegularSearch);
250 if (!sameSearchMode) {
251 // call enterSearchUi only if we are switching search modes, or entering
252 // search ui for the first time
253 enterSearchUi(dialpadSearch, newText);
254 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700255
Yorke Leeb207f8a2013-08-29 18:51:06 -0700256 if (dialpadSearch && mSmartDialSearchFragment != null) {
257 mSmartDialSearchFragment.setQueryString(newText, false);
258 } else if (mRegularSearchFragment != null) {
259 mRegularSearchFragment.setQueryString(newText, false);
260 }
261 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700262 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700263 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700264 }
Yorke Leec3766332013-07-31 11:13:16 -0700265 }
266
267 @Override
268 public void afterTextChanged(Editable s) {
269 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700270 };
271
Yorke Leec3766332013-07-31 11:13:16 -0700272 private boolean isDialpadShowing() {
273 return mDialpadFragment != null && mDialpadFragment.isVisible();
274 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275
276 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700277 protected void onCreate(Bundle savedInstanceState) {
278 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700279 mFirstLaunch = true;
280
Chiao Cheng94b10b52012-08-17 16:59:12 -0700281 final Intent intent = getIntent();
282 fixIntent(intent);
283
Yorke Lee8898cd02013-08-08 10:24:27 -0700284 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700285
Yorke Leec3766332013-07-31 11:13:16 -0700286 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700287
Yorke Leeef2b7382013-08-09 17:39:25 -0700288 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
289 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700290 if (savedInstanceState == null) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700291 final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700292
Yorke Leec3766332013-07-31 11:13:16 -0700293 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700294 ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
295 ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
Yorke Leec3766332013-07-31 11:13:16 -0700296 ft.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700297 } else {
298 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700299 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
300 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700301 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700302 }
303
Yorke Leec3766332013-07-31 11:13:16 -0700304 mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700305 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700306 mActionBar = findViewById(R.id.fake_action_bar);
Yorke Leec3766332013-07-31 11:13:16 -0700307 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700308
Chiao Cheng94b10b52012-08-17 16:59:12 -0700309 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Yorke Leec3766332013-07-31 11:13:16 -0700310 && savedInstanceState == null) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700311 setupFilterText(intent);
312 }
Yorke Leefce269a2013-08-12 11:56:04 -0700313
Yorke Leeb207f8a2013-08-29 18:51:06 -0700314 setupFakeActionBarItems();
315
Yorke Lee0baa98b2013-08-22 10:55:16 -0700316 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700317 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700318 }
319
320 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700321 protected void onResume() {
322 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700323 if (mFirstLaunch) {
324 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700325 } else if (!phoneIsInUse() && mInCallDialpadUp) {
326 hideDialpadFragment(false, true);
327 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700328 }
Yorke Lee35127cd2013-09-10 14:09:29 -0700329
Yorke Lee98702de2013-08-06 12:03:32 -0700330 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700331 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700332 }
333
334 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700335 protected void onPause() {
336 if (mClearSearchOnPause) {
337 hideDialpadAndSearchUi();
338 mClearSearchOnPause = false;
339 }
340 super.onPause();
341 }
342
343 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700344 protected void onSaveInstanceState(Bundle outState) {
345 super.onSaveInstanceState(outState);
346 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700347 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
348 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700349 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
350 }
351
352 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700353 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700354 if (fragment instanceof DialpadFragment) {
355 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700356 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700357 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700358 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700359 } else if (fragment instanceof SmartDialSearchFragment) {
360 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700361 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
362 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700363 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700364 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700365 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
366 mPhoneNumberPickerActionListener);
Yorke Leeef2b7382013-08-09 17:39:25 -0700367 } else if (fragment instanceof PhoneFavoriteFragment) {
368 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700369 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700370 }
Yorke Leec3766332013-07-31 11:13:16 -0700371 }
372
373 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700374 public boolean onMenuItemClick(MenuItem item) {
375 switch (item.getItemId()) {
376 case R.id.menu_import_export:
377 // We hard-code the "contactsAreAvailable" argument because doing it properly would
378 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
379 // now in Dialtacts for (potential) performance reasons. Compare with how it is
380 // done in {@link PeopleActivity}.
381 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700382 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700383 return true;
384 case R.id.menu_clear_frequents:
385 ClearFrequentsDialog.show(getFragmentManager());
386 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700387 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700388 try {
389 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
390 } catch (ActivityNotFoundException e) {
391 Toast toast = Toast.makeText(this,
392 R.string.add_contact_not_available,
393 Toast.LENGTH_SHORT);
394 toast.show();
395 }
396 return true;
397 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700398 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700399 return true;
400 case R.id.menu_all_contacts:
401 onShowAllContacts();
402 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700403 }
404 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700405 }
406
Alon Albertb453e5b2013-09-10 15:54:23 -0700407 protected void handleMenuSettings() {
408 openTelephonySetting(this);
409 }
410
411 public static void openTelephonySetting(Activity activity) {
412 final Intent settingsIntent = getCallSettingsIntent();
413 activity.startActivity(settingsIntent);
414 }
415
Chiao Cheng94b10b52012-08-17 16:59:12 -0700416 @Override
417 public void onClick(View view) {
418 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700419 case R.id.overflow_menu: {
Yorke Lee36c54132013-09-25 16:17:06 -0700420 mOverflowMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700421 break;
422 }
Yorke Leec3766332013-07-31 11:13:16 -0700423 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700424 // Reset the boolean flag that tracks whether the dialpad was up because
425 // we were in call. Regardless of whether it was true before, we want to
426 // show the dialpad because the user has explicitly clicked the dialpad
427 // button.
428 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700429 showDialpadFragment(true);
430 break;
431 case R.id.call_history_on_dialpad_button:
432 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700433 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
434 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700435 final Intent intent = new Intent(this, CallLogActivity.class);
436 startActivity(intent);
437 break;
438 case R.id.search_close_button:
439 // Clear the search field
440 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700441 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700442 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700443 }
444 break;
Yorke Leec3766332013-07-31 11:13:16 -0700445 case R.id.voice_search_button:
446 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
447 startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
448 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700449 default: {
450 Log.wtf(TAG, "Unexpected onClick event from " + view);
451 break;
452 }
453 }
454 }
455
Yorke Leec3766332013-07-31 11:13:16 -0700456 @Override
457 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
458 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
459 if (resultCode == RESULT_OK) {
460 final ArrayList<String> matches = data.getStringArrayListExtra(
461 RecognizerIntent.EXTRA_RESULTS);
462 if (matches.size() > 0) {
463 final String match = matches.get(0);
464 mSearchView.setText(match);
465 } else {
466 Log.e(TAG, "Voice search - nothing heard");
467 }
468 } else {
469 Log.e(TAG, "Voice search failed");
470 }
471 }
472 super.onActivityResult(requestCode, resultCode, data);
473 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700474
Yorke Leec3766332013-07-31 11:13:16 -0700475 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700476 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700477 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700478 if (animate) {
479 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700480 } else {
481 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700482 }
483 ft.show(mDialpadFragment);
484 ft.commit();
485 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700486
Yorke Leeca195042013-09-25 16:29:38 -0700487 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700488 if (mDialpadFragment == null) return;
489 if (clearDialpad) {
490 mDialpadFragment.clearDialpad();
491 }
492 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700493 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700494 final FragmentTransaction ft = getFragmentManager().beginTransaction();
495 if (animate) {
496 ft.setCustomAnimations(0, R.anim.slide_out);
497 }
498 ft.hide(mDialpadFragment);
499 ft.commit();
500 }
501
Chiao Cheng94b10b52012-08-17 16:59:12 -0700502 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700503 mSearchViewContainer = findViewById(R.id.search_view_container);
504 mSearchViewCloseButton = findViewById(R.id.search_close_button);
505 mSearchViewCloseButton.setOnClickListener(this);
506 mVoiceSearchButton = findViewById(R.id.voice_search_button);
507 mVoiceSearchButton.setOnClickListener(this);
508 mSearchView = (EditText) findViewById(R.id.search_view);
509 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
510 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
Chiao Cheng94b10b52012-08-17 16:59:12 -0700511 }
512
Yorke Leec3766332013-07-31 11:13:16 -0700513 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
514 @Override
515 public void onAnimationEnd(Animator animation) {
516 mSearchViewContainer.setVisibility(View.GONE);
517 }
518 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700519
Yorke Leeb207f8a2013-08-29 18:51:06 -0700520 private boolean getInSearchUi() {
521 return mInDialpadSearch || mInRegularSearch;
522 }
523
524 private void setNotInSearchUi() {
525 mInDialpadSearch = false;
526 mInRegularSearch = false;
527 }
528
Yorke Lee311969c2013-08-26 06:31:11 -0700529 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700530 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700531 hideDialpadFragment(false, true);
532 }
533
Yorke Leec3766332013-07-31 11:13:16 -0700534 public void hideSearchBar() {
535 hideSearchBar(true);
Christine Chen9c1e0652013-05-23 15:40:19 -0700536 }
537
Yorke Leec3766332013-07-31 11:13:16 -0700538 public void hideSearchBar(boolean shiftView) {
539 if (shiftView) {
540 mSearchViewContainer.animate().cancel();
541 mSearchViewContainer.setAlpha(1);
542 mSearchViewContainer.setTranslationY(0);
543 mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
544 .setDuration(200).setListener(mHideListener);
545
Yorke Leeb207f8a2013-08-29 18:51:06 -0700546 mFragmentsFrame.animate().withLayer()
Yorke Leec3766332013-07-31 11:13:16 -0700547 .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
548 new AnimatorListenerAdapter() {
549 @Override
550 public void onAnimationEnd(Animator animation) {
551 mBottomPaddingView.setVisibility(View.VISIBLE);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700552 mFragmentsFrame.setTranslationY(0);
553 mActionBar.setVisibility(View.INVISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700554 }
555 });
556 } else {
557 mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
Yorke Lee5f3ad542013-10-08 08:14:36 -0700558 mActionBar.setVisibility(View.INVISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700559 }
560 }
561
562 public void showSearchBar() {
Yorke Leec3766332013-07-31 11:13:16 -0700563 mSearchViewContainer.animate().cancel();
564 mSearchViewContainer.setAlpha(0);
565 mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
566 mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
567 .setListener(new AnimatorListenerAdapter() {
568 @Override
569 public void onAnimationStart(Animator animation) {
570 mSearchViewContainer.setVisibility(View.VISIBLE);
Yorke Lee5f3ad542013-10-08 08:14:36 -0700571 mActionBar.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700572 }
573 });
574
Yorke Leeb207f8a2013-08-29 18:51:06 -0700575 mFragmentsFrame.setTranslationY(-mSearchViewContainer.getHeight());
576 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(200)
Yorke Leec3766332013-07-31 11:13:16 -0700577 .setListener(
578 new AnimatorListenerAdapter() {
579 @Override
580 public void onAnimationStart(Animator animation) {
581 mBottomPaddingView.setVisibility(View.GONE);
582 }
583 });
584 }
585
586
Yorke Leeb207f8a2013-08-29 18:51:06 -0700587 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700588 mMenuButton = findViewById(R.id.overflow_menu);
589 if (mMenuButton != null) {
590 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700591
592 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
593 final Menu menu = mOverflowMenu.getMenu();
594 mOverflowMenu.inflate(R.menu.dialtacts_options);
595 mOverflowMenu.setOnMenuItemClickListener(this);
596 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700597 }
598
599 mCallHistoryButton = findViewById(R.id.call_history_button);
600 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
601 mCallHistoryButton.setOnClickListener(this);
602
603 mDialpadButton = findViewById(R.id.dialpad_button);
604 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
605 mDialpadButton.setOnClickListener(this);
606 }
607
608 public void setupFakeActionBarItemsForDialpadFragment() {
609 final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
610 callhistoryButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700611 }
612
613 private void fixIntent(Intent intent) {
614 // This should be cleaned up: the call key used to send an Intent
615 // that just said to go to the recent calls list. It now sends this
616 // abstract action, but this class hasn't been rewritten to deal with it.
617 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
618 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
619 intent.putExtra("call_key", true);
620 setIntent(intent);
621 }
622 }
623
Chiao Cheng94b10b52012-08-17 16:59:12 -0700624 /**
625 * Returns true if the intent is due to hitting the green send key (hardware call button:
626 * KEYCODE_CALL) while in a call.
627 *
628 * @param intent the intent that launched this activity
629 * @param recentCallsRequest true if the intent is requesting to view recent calls
630 * @return true if the intent is due to hitting the green send key while in a call
631 */
Yorke Leec3766332013-07-31 11:13:16 -0700632 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700633 // If there is a call in progress go to the call screen
634 if (recentCallsRequest) {
635 final boolean callKey = intent.getBooleanExtra("call_key", false);
636
637 try {
638 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
639 if (callKey && phone != null && phone.showCallScreen()) {
640 return true;
641 }
642 } catch (RemoteException e) {
643 Log.e(TAG, "Failed to handle send while in call", e);
644 }
645 }
646
647 return false;
648 }
649
650 /**
651 * Sets the current tab based on the intent's request type
652 *
653 * @param intent Intent that contains information about which tab should be selected
654 */
Yorke Leec3766332013-07-31 11:13:16 -0700655 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700656 // If we got here by hitting send and we're in call forward along to the in-call activity
657 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
658 getContentResolver()));
659 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
660 finish();
661 return;
662 }
663
Yorke Lee669b6082013-09-17 15:43:38 -0700664 if (mDialpadFragment != null) {
665 final boolean phoneIsInUse = phoneIsInUse();
666 if (phoneIsInUse || isDialIntent(intent)) {
667 mDialpadFragment.setStartedFromNewIntent(true);
668 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
669 mInCallDialpadUp = true;
670 }
671 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700672 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700673 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700674 }
675
676 @Override
677 public void onNewIntent(Intent newIntent) {
678 setIntent(newIntent);
679 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700680 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700681 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700682
Chiao Cheng94b10b52012-08-17 16:59:12 -0700683 invalidateOptionsMenu();
684 }
685
686 /** Returns true if the given intent contains a phone number to populate the dialer with */
687 private boolean isDialIntent(Intent intent) {
688 final String action = intent.getAction();
689 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
690 return true;
691 }
692 if (Intent.ACTION_VIEW.equals(action)) {
693 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700694 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700695 return true;
696 }
697 }
698 return false;
699 }
700
701 /**
702 * Returns an appropriate call origin for this Activity. May return null when no call origin
703 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
704 * for remembering the tab in which the user made a phone call, so the external app's DIAL
705 * request should not be counted.)
706 */
707 public String getCallOrigin() {
708 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
709 }
710
711 /**
712 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
713 * This text originally came from a FILTER_CONTACTS_ACTION intent received
714 * by this activity. The stored text will then be cleared after after this
715 * method returns.
716 *
717 * @return The stored filter text
718 */
719 public String getAndClearFilterText() {
720 String filterText = mFilterText;
721 mFilterText = null;
722 return filterText;
723 }
724
725 /**
726 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
727 * This is so child activities can check if they are supposed to display a filter.
728 *
729 * @param intent The intent received in {@link #onNewIntent(Intent)}
730 */
731 private void setupFilterText(Intent intent) {
732 // If the intent was relaunched from history, don't apply the filter text.
733 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
734 return;
735 }
736 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
737 if (filter != null && filter.length() > 0) {
738 mFilterText = filter;
739 }
740 }
741
Yorke Lee8898cd02013-08-08 10:24:27 -0700742 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
743 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 @Override
745 public void onContactSelected(Uri contactUri) {
746 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700747 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700748 }
749
750 @Override
751 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700752 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700753 startActivity(intent);
754 }
755 };
756
Yorke Leec3766332013-07-31 11:13:16 -0700757 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
758 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
759 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700760
761 @Override
762 public void startSearch(String initialQuery, boolean selectInitialQuery,
763 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700764 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700765 if (mInSearchUi) {
766 if (mSearchView.hasFocus()) {
767 showInputMethod(mSearchView.findFocus());
768 } else {
769 mSearchView.requestFocus();
770 }
771 } else {
772 enterSearchUi();
773 }
774 } else {
775 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
776 }
Yorke Leec3766332013-07-31 11:13:16 -0700777 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700778
779 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700780 final InputMethodManager imm = (InputMethodManager) getSystemService(
781 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700782 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700783 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700784 }
785 }
786
787 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700788 final InputMethodManager imm = (InputMethodManager) getSystemService(
789 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700790 if (imm != null && view != null) {
791 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
792 }
793 }
794
795 /**
Yorke Leec3766332013-07-31 11:13:16 -0700796 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700797 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700798 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700799 if (getFragmentManager().isDestroyed()) {
800 // Weird race condition where fragment is doing work after the activity is destroyed
801 // due to talkback being on (b/10209937). Just return since we can't do any
802 // constructive here.
803 return;
804 }
805
Yorke Leeef2b7382013-08-09 17:39:25 -0700806 if (DEBUG) {
807 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
808 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700809
Yorke Leec3766332013-07-31 11:13:16 -0700810 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
811 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700812
Yorke Leeef2b7382013-08-09 17:39:25 -0700813 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700814 if (mInDialpadSearch) {
815 transaction.remove(mSmartDialSearchFragment);
816 } else if (mInRegularSearch) {
817 transaction.remove(mRegularSearchFragment);
818 } else {
819 transaction.remove(mPhoneFavoriteFragment);
820 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700821
Yorke Leeb207f8a2013-08-29 18:51:06 -0700822 final String tag;
823 if (smartDialSearch) {
824 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
825 } else {
826 tag = TAG_REGULAR_SEARCH_FRAGMENT;
827 }
828 mInDialpadSearch = smartDialSearch;
829 mInRegularSearch = !smartDialSearch;
830
Yorke Leeef2b7382013-08-09 17:39:25 -0700831 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
832 if (fragment == null) {
833 if (smartDialSearch) {
834 fragment = new SmartDialSearchFragment();
835 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700836 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700837 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700838 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700839 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700840 transaction.addToBackStack(null);
841 fragment.setQueryString(query, false);
842 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700843 }
844
845 /**
Yorke Leec3766332013-07-31 11:13:16 -0700846 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700847 */
Yorke Leec3766332013-07-31 11:13:16 -0700848 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700849 // See related bug in enterSearchUI();
850 if (getFragmentManager().isDestroyed()) {
851 return;
852 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700853 // Go all the way back to the favorites fragment, regardless of how many times we
854 // transitioned between search fragments
855 final BackStackEntry entry = getFragmentManager().getBackStackEntryAt(0);
856 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
857 setNotInSearchUi();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700858 }
859
860 /** Returns an Intent to launch Call Settings screen */
861 public static Intent getCallSettingsIntent() {
862 final Intent intent = new Intent(Intent.ACTION_MAIN);
863 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
864 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
865 return intent;
866 }
867
868 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700869 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700870 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700871 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700872 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700873 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700874 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700875 } else if (isTaskRoot()) {
876 // Instead of stopping, simply push this to the back of the stack.
877 // This is only done when running at the top of the stack;
878 // otherwise, we have been launched by someone else so need to
879 // allow the user to go back to the caller.
880 moveTaskToBack(false);
881 } else {
882 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700883 }
Yorke Leec3766332013-07-31 11:13:16 -0700884 }
885
886 @Override
887 public void onDialpadQueryChanged(String query) {
888 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
889 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
890 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700891 if (DEBUG) {
892 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
893 }
894 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
895 // This callback can happen if the dialpad fragment is recreated because of
896 // activity destruction. In that case, don't update the search view because
897 // that would bring the user back to the search fragment regardless of the
898 // previous state of the application. Instead, just return here and let the
899 // fragment manager correctly figure out whatever fragment was last displayed.
900 return;
901 }
Yorke Leec3766332013-07-31 11:13:16 -0700902 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700903 }
904 }
Yorke Leec3766332013-07-31 11:13:16 -0700905
906 @Override
907 public void onListFragmentScrollStateChange(int scrollState) {
908 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700909 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700910 hideInputMethod(getCurrentFocus());
911 }
912 }
913
914 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700915 public void onDialpadFragmentStarted() {
916 setupFakeActionBarItemsForDialpadFragment();
917 }
918
919 private boolean phoneIsInUse() {
920 final TelephonyManager tm = (TelephonyManager) getSystemService(
921 Context.TELEPHONY_SERVICE);
922 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
923 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700924
925 @Override
926 public void onShowAllContacts() {
927 final Intent intent = new Intent(this, AllContactsActivity.class);
928 startActivity(intent);
929 }
Yorke Leee1424812013-09-04 18:24:48 -0700930
931 public static Intent getAddNumberToContactIntent(CharSequence text) {
932 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
933 intent.putExtra(Intents.Insert.PHONE, text);
934 intent.setType(Contacts.CONTENT_ITEM_TYPE);
935 return intent;
936 }
937
938 public static Intent getInsertContactWithNameIntent(CharSequence text) {
939 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
940 intent.putExtra(Intents.Insert.NAME, text);
941 return intent;
942 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700943}