blob: c59db95dc7314137c7491b0059ca74e1769bdb1a [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;
25import android.app.FragmentTransaction;
Yorke Lee86e21f72014-04-02 16:49:38 -070026import android.app.SearchManager;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080027import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import android.content.Context;
29import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080030import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
Yorke Lee86e21f72014-04-02 16:49:38 -070032import android.graphics.Outline;
Chiao Cheng94b10b52012-08-17 16:59:12 -070033import android.net.Uri;
34import android.os.Bundle;
35import android.os.RemoteException;
36import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070038import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070039import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070040import android.telephony.TelephonyManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.text.TextUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070042import android.util.Log;
43import android.view.Menu;
Yorke Lee86e21f72014-04-02 16:49:38 -070044import android.view.MenuInflater;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.View;
Yorke Lee86e21f72014-04-02 16:49:38 -070047import android.view.View.OnLayoutChangeListener;
Yorke Leeb0d19762014-03-05 14:43:28 -080048import android.view.ViewGroup.LayoutParams;
49import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Yorke Lee86e21f72014-04-02 16:49:38 -070050import android.view.animation.AccelerateInterpolator;
51import android.view.animation.DecelerateInterpolator;
52import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070054import android.widget.AbsListView.OnScrollListener;
Yorke Leeb0d19762014-03-05 14:43:28 -080055import android.widget.LinearLayout;
Chiao Cheng94b10b52012-08-17 16:59:12 -070056import android.widget.PopupMenu;
Yorke Lee86e21f72014-04-02 16:49:38 -070057import android.widget.SearchView;
58import android.widget.SearchView.OnQueryTextListener;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080059import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070060
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070061import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070062import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070063import com.android.contacts.common.dialog.ClearFrequentsDialog;
64import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080065import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070066import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070067import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070068import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070069import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070070import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070071import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070072import com.android.dialer.list.AllContactsActivity;
Yorke Lee3cefcc62014-01-16 11:26:46 -080073import com.android.dialer.list.DragDropController;
74import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070075import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070076import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080077import com.android.dialer.list.PhoneFavoriteTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070078import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080079import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070080import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070081import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070082import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070083import com.android.internal.telephony.ITelephony;
84
Yorke Leec3766332013-07-31 11:13:16 -070085import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080086import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070087
Chiao Cheng94b10b52012-08-17 16:59:12 -070088/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070089 * The dialer tab's title is 'phone', a more common name (see strings.xml).
90 */
Yorke Leec3766332013-07-31 11:13:16 -070091public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070092 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -070093 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080094 DialpadFragment.HostInterface,
Yorke Lee3cefcc62014-01-16 11:26:46 -080095 PhoneFavoriteFragment.OnShowAllContactsListener,
96 PhoneFavoriteFragment.HostInterface,
Ihab Awad526c0b82014-02-27 12:55:36 -080097 OnDragDropListener, View.OnLongClickListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070098 private static final String TAG = "DialtactsActivity";
99
Ihab Awad526c0b82014-02-27 12:55:36 -0800100 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101
Yorke Leef74011e2013-08-02 11:30:30 -0700102 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
103
Chiao Cheng94b10b52012-08-17 16:59:12 -0700104 /** Used to open Call Setting */
105 private static final String PHONE_PACKAGE = "com.android.phone";
106 private static final String CALL_SETTINGS_CLASS_NAME =
107 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700108 /** @see #getCallOrigin() */
109 private static final String CALL_ORIGIN_DIALTACTS =
110 "com.android.dialer.DialtactsActivity";
111
Yorke Leeb207f8a2013-08-29 18:51:06 -0700112 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
113 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700114 private static final String KEY_SEARCH_QUERY = "search_query";
115 private static final String KEY_FIRST_LAUNCH = "first_launch";
116
Yorke Leec3766332013-07-31 11:13:16 -0700117 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
118 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
119 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
120 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700121
Chiao Cheng94b10b52012-08-17 16:59:12 -0700122 /**
123 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
124 */
125 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
126
Yorke Leec3766332013-07-31 11:13:16 -0700127 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700128
Yorke Lee86e21f72014-04-02 16:49:38 -0700129 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800130
Yorke Leec3766332013-07-31 11:13:16 -0700131 /**
132 * The main fragment displaying the user's favorites and frequent contacts
133 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700134 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700135
136 /**
Yorke Leec3766332013-07-31 11:13:16 -0700137 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700138 */
Yorke Leef74011e2013-08-02 11:30:30 -0700139 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700140
141 /**
142 * Fragment for searching phone numbers using the alphanumeric keyboard.
143 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700144 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700145
146 /**
147 * Fragment for searching phone numbers using the dialpad.
148 */
149 private SmartDialSearchFragment mSmartDialSearchFragment;
150
Ihab Awad526c0b82014-02-27 12:55:36 -0800151 private View mFakeActionBar;
Yorke Lee86e21f72014-04-02 16:49:38 -0700152 private View mMenuButton;
Yorke Leec3766332013-07-31 11:13:16 -0700153 private View mCallHistoryButton;
154 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800155 private View mDialButton;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700156 private PopupMenu mDialpadOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700157
Yorke Leeb207f8a2013-08-29 18:51:06 -0700158 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700159
Yorke Leeb207f8a2013-08-29 18:51:06 -0700160 private boolean mInDialpadSearch;
161 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700162 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700163
Yorke Leeef2b7382013-08-09 17:39:25 -0700164 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700165 * True if the dialpad is only temporarily showing due to being in call
166 */
167 private boolean mInCallDialpadUp;
168
169 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700170 * True when this activity has been launched for the first time.
171 */
Yorke Lee98702de2013-08-06 12:03:32 -0700172 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700173
Yorke Lee94f49042014-03-03 14:03:08 -0800174 // This view points to the Framelayout that houses both the search view and remove view
175 // containers.
176 private View mSearchAndRemoveViewContainer;
Yorke Lee86e21f72014-04-02 16:49:38 -0700177 private SearchView mSearchView;
178 /**
179 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
180 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
181 * removed from the speed dial list.
182 */
183 private RemoveView mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700184
Yorke Lee86e21f72014-04-02 16:49:38 -0700185 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
186 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700187 private String mSearchQuery;
188
Yorke Leefce269a2013-08-12 11:56:04 -0700189 private DialerDatabaseHelper mDialerDatabaseHelper;
190
Chiao Cheng94b10b52012-08-17 16:59:12 -0700191 /**
192 * Listener used when one of phone numbers in search UI is selected. This will initiate a
193 * phone call using the phone number.
194 */
195 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
196 new OnPhoneNumberPickerActionListener() {
197 @Override
198 public void onPickPhoneNumberAction(Uri dataUri) {
199 // Specify call-origin so that users will see the previous tab instead of
200 // CallLog screen (search UI will be automatically exited).
201 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700202 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700203 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700204 }
205
206 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700207 public void onCallNumberDirectly(String phoneNumber) {
208 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
209 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700210 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700211 }
212
213 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700214 public void onShortcutIntentCreated(Intent intent) {
215 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
216 }
217
218 @Override
219 public void onHomeInActionBarSelected() {
220 exitSearchUi();
221 }
222 };
223
224 /**
225 * Listener used to send search queries to the phone search fragment.
226 */
Yorke Lee86e21f72014-04-02 16:49:38 -0700227 private final OnQueryTextListener mPhoneSearchQueryTextListener = new OnQueryTextListener() {
Yorke Leec3766332013-07-31 11:13:16 -0700228 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700229 public boolean onQueryTextSubmit(String query) {
230 return false;
Yorke Leec3766332013-07-31 11:13:16 -0700231 }
232
233 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700234 public boolean onQueryTextChange(String newText) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700235 if (newText.equals(mSearchQuery)) {
236 // If the query hasn't changed (perhaps due to activity being destroyed
237 // and restored, or user launching the same DIAL intent twice), then there is
238 // no need to do anything here.
Yorke Lee86e21f72014-04-02 16:49:38 -0700239 return true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700240 }
241 mSearchQuery = newText;
242 if (DEBUG) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700243 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leeef2b7382013-08-09 17:39:25 -0700244 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700245 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700246
Yorke Leec3766332013-07-31 11:13:16 -0700247 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700248 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700249 exitSearchUi();
Yorke Lee86e21f72014-04-02 16:49:38 -0700250 return true;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700251 } else if (!TextUtils.isEmpty(newText)) {
252 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
253 (!dialpadSearch && mInRegularSearch);
254 if (!sameSearchMode) {
255 // call enterSearchUi only if we are switching search modes, or entering
256 // search ui for the first time
257 enterSearchUi(dialpadSearch, newText);
258 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700259
Yorke Leeb207f8a2013-08-29 18:51:06 -0700260 if (dialpadSearch && mSmartDialSearchFragment != null) {
261 mSmartDialSearchFragment.setQueryString(newText, false);
262 } else if (mRegularSearchFragment != null) {
263 mRegularSearchFragment.setQueryString(newText, false);
264 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700265 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700266 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700267 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700268 }
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
Yorke Lee8898cd02013-08-08 10:24:27 -0700280 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800281 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700282
Yorke Lee86e21f72014-04-02 16:49:38 -0700283 getActionBar().setDisplayShowHomeEnabled(false);
284 getActionBar().setDisplayShowTitleEnabled(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700285
Yorke Leeef2b7382013-08-09 17:39:25 -0700286 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
287 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700288 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800289 getFragmentManager().beginTransaction()
290 .add(R.id.dialtacts_frame, new PhoneFavoriteFragment(), TAG_FAVORITES_FRAGMENT)
291 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
292 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700293 } else {
294 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700295 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
296 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700297 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700298 }
299
Yorke Leeb207f8a2013-08-29 18:51:06 -0700300 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee86e21f72014-04-02 16:49:38 -0700301
302 mFakeActionBar = findViewById(R.id.fake_action_bar);
303
304 mCallHistoryButton = findViewById(R.id.call_history_button);
305 mCallHistoryButton.setOnClickListener(this);
306 mDialButton = findViewById(R.id.dial_button);
307 mDialButton.setOnClickListener(this);
308 mDialpadButton = findViewById(R.id.dialpad_button);
309 mDialpadButton.setOnClickListener(this);
310 mMenuButton = findViewById(R.id.overflow_menu_button);
311 mMenuButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800312
Yorke Lee3cefcc62014-01-16 11:26:46 -0800313 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800314 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
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 }
329 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700330 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700331 }
332
333 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700334 protected void onPause() {
335 if (mClearSearchOnPause) {
336 hideDialpadAndSearchUi();
337 mClearSearchOnPause = false;
338 }
339 super.onPause();
340 }
341
342 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700343 protected void onSaveInstanceState(Bundle outState) {
344 super.onSaveInstanceState(outState);
345 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700346 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
347 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700348 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
349 }
350
351 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700352 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700353 if (fragment instanceof DialpadFragment) {
354 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700355 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700356 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700357 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700358 } else if (fragment instanceof SmartDialSearchFragment) {
359 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700360 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
361 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700362 if (mFragmentsFrame != null) {
363 mFragmentsFrame.setAlpha(1.0f);
364 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700365 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700366 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700367 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
368 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700369 if (mFragmentsFrame != null) {
370 mFragmentsFrame.setAlpha(1.0f);
371 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700372 } else if (fragment instanceof PhoneFavoriteFragment) {
373 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700374 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Lee86e21f72014-04-02 16:49:38 -0700375 if (mFragmentsFrame != null) {
376 mFragmentsFrame.setAlpha(1.0f);
377 }
Yorke Leec3766332013-07-31 11:13:16 -0700378 }
Yorke Leec3766332013-07-31 11:13:16 -0700379 }
380
Alon Albertb453e5b2013-09-10 15:54:23 -0700381 protected void handleMenuSettings() {
382 openTelephonySetting(this);
383 }
384
385 public static void openTelephonySetting(Activity activity) {
386 final Intent settingsIntent = getCallSettingsIntent();
387 activity.startActivity(settingsIntent);
388 }
389
Chiao Cheng94b10b52012-08-17 16:59:12 -0700390 @Override
391 public void onClick(View view) {
392 switch (view.getId()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700393 case R.id.overflow_menu_button:
394 mDialpadOverflowMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700395 break;
Yorke Leec3766332013-07-31 11:13:16 -0700396 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700397 // Reset the boolean flag that tracks whether the dialpad was up because
398 // we were in call. Regardless of whether it was true before, we want to
399 // show the dialpad because the user has explicitly clicked the dialpad
400 // button.
401 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700402 showDialpadFragment(true);
403 break;
Yorke Leec3766332013-07-31 11:13:16 -0700404 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700405 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
406 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700407 final Intent intent = new Intent(this, CallLogActivity.class);
408 startActivity(intent);
409 break;
Yorke Lee86e21f72014-04-02 16:49:38 -0700410 case R.id.dial_button:
411 // Dial button was pressed; tell the Dialpad fragment
412 mDialpadFragment.dialButtonPressed();
413 break;
Yorke Leec3766332013-07-31 11:13:16 -0700414 case R.id.search_close_button:
415 // Clear the search field
Yorke Lee86e21f72014-04-02 16:49:38 -0700416 if (!TextUtils.isEmpty(mSearchView.getQuery())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700417 mDialpadFragment.clearDialpad();
Yorke Lee86e21f72014-04-02 16:49:38 -0700418 mSearchView.setQuery("", false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700419 }
420 break;
Yorke Leec3766332013-07-31 11:13:16 -0700421 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800422 try {
423 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
424 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
425 } catch (ActivityNotFoundException e) {
426 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
427 Toast.LENGTH_SHORT).show();
428 }
Yorke Leec3766332013-07-31 11:13:16 -0700429 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700430 default: {
431 Log.wtf(TAG, "Unexpected onClick event from " + view);
432 break;
433 }
434 }
435 }
436
Yorke Leec3766332013-07-31 11:13:16 -0700437 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700438 public boolean onOptionsItemSelected(MenuItem item) {
439 switch (item.getItemId()) {
440 case R.id.menu_history:
441 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
442 // CONTENT_TYPE, so that we always open our call log from our dialer
443 final Intent intent = new Intent(this, CallLogActivity.class);
444 startActivity(intent);
445 break;
446 case R.id.menu_add_contact:
447 try {
448 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
449 } catch (ActivityNotFoundException e) {
450 Toast toast = Toast.makeText(this,
451 R.string.add_contact_not_available,
452 Toast.LENGTH_SHORT);
453 toast.show();
454 }
455 break;
456 case R.id.menu_import_export:
457 // We hard-code the "contactsAreAvailable" argument because doing it properly would
458 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
459 // now in Dialtacts for (potential) performance reasons. Compare with how it is
460 // done in {@link PeopleActivity}.
461 ImportExportDialogFragment.show(getFragmentManager(), true,
462 DialtactsActivity.class);
463 return true;
464 case R.id.menu_clear_frequents:
465 // TODO: This should be enabled/disabled based on
466 // PhoneFavoritesFragments.hasFrequents
467 ClearFrequentsDialog.show(getFragmentManager());
468 return true;
469 case R.id.menu_call_settings:
470 handleMenuSettings();
471 return true;
472 case R.id.menu_all_contacts:
473 onShowAllContacts();
474 return true;
475 }
476 return false;
477 }
478
479 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800480 public boolean onLongClick(View view) {
481 switch (view.getId()) {
482 case R.id.dial_button: {
483 // Dial button was pressed; tell the Dialpad fragment
484 mDialpadFragment.dialButtonPressed();
485 return true; // Consume the event
486 }
487 default: {
488 Log.wtf(TAG, "Unexpected onClick event from " + view);
489 break;
490 }
491 }
492 return false;
493 }
494
495 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700496 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
497 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
498 if (resultCode == RESULT_OK) {
499 final ArrayList<String> matches = data.getStringArrayListExtra(
500 RecognizerIntent.EXTRA_RESULTS);
501 if (matches.size() > 0) {
502 final String match = matches.get(0);
Yorke Lee86e21f72014-04-02 16:49:38 -0700503 mSearchView.setQuery(match, false);
Yorke Leec3766332013-07-31 11:13:16 -0700504 } else {
505 Log.e(TAG, "Voice search - nothing heard");
506 }
507 } else {
508 Log.e(TAG, "Voice search failed");
509 }
510 }
511 super.onActivityResult(requestCode, resultCode, data);
512 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700513
Yorke Leec3766332013-07-31 11:13:16 -0700514 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700515 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700516 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700517 if (animate) {
518 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700519 } else {
520 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700521 }
522 ft.show(mDialpadFragment);
523 ft.commit();
524 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700525
Yorke Leeca195042013-09-25 16:29:38 -0700526 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700527 if (mDialpadFragment == null) return;
528 if (clearDialpad) {
529 mDialpadFragment.clearDialpad();
530 }
531 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700532 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700533 final FragmentTransaction ft = getFragmentManager().beginTransaction();
534 if (animate) {
535 ft.setCustomAnimations(0, R.anim.slide_out);
536 }
537 ft.hide(mDialpadFragment);
538 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700539 }
540
Yorke Leec3766332013-07-31 11:13:16 -0700541 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
542 @Override
543 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800544 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700545 }
546 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700547
Yorke Leeb207f8a2013-08-29 18:51:06 -0700548 private boolean getInSearchUi() {
549 return mInDialpadSearch || mInRegularSearch;
550 }
551
552 private void setNotInSearchUi() {
553 mInDialpadSearch = false;
554 mInRegularSearch = false;
555 }
556
Yorke Lee311969c2013-08-26 06:31:11 -0700557 private void hideDialpadAndSearchUi() {
Yorke Lee86e21f72014-04-02 16:49:38 -0700558 mSearchView.setQuery("", false);
Yorke Lee311969c2013-08-26 06:31:11 -0700559 hideDialpadFragment(false, true);
560 }
561
Yorke Lee86e21f72014-04-02 16:49:38 -0700562 /**
563 * Callback from child DialpadFragment when the dialpad is shown.
564 */
565 public void onDialpadShown() {
566 mDialButton.setVisibility(View.VISIBLE);
567 mDialpadButton.setVisibility(View.GONE);
568 mMenuButton.setVisibility(View.VISIBLE);
569 if (mDialpadOverflowMenu == null) {
570 mDialpadOverflowMenu = mDialpadFragment.buildOptionsMenu(mMenuButton);
571 mMenuButton.setOnTouchListener(mDialpadOverflowMenu.getDragToOpenListener());
572 }
Yorke Leeb0d19762014-03-05 14:43:28 -0800573
Yorke Lee86e21f72014-04-02 16:49:38 -0700574 SearchFragment fragment = null;
575 if (mInDialpadSearch) {
576 fragment = mSmartDialSearchFragment;
577 } else if (mInRegularSearch) {
578 fragment = mRegularSearchFragment;
579 }
580 if (fragment != null && fragment.isVisible()) {
581 fragment.getListView().animate().translationY(-getActionBar().getHeight())
582 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
583 }
Christine Chen9c1e0652013-05-23 15:40:19 -0700584
Yorke Lee86e21f72014-04-02 16:49:38 -0700585 if (mPhoneFavoriteFragment != null && mPhoneFavoriteFragment.isVisible()) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700586 // If the favorites fragment is showing, fade to blank.
587 mFragmentsFrame.animate().alpha(0.0f);
588 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700589 getActionBar().hide();
Yorke Leec3766332013-07-31 11:13:16 -0700590 }
591
Yorke Lee86e21f72014-04-02 16:49:38 -0700592 /**
593 * Callback from child DialpadFragment when the dialpad is hidden.
594 */
595 public void onDialpadHidden() {
596 mDialButton.setVisibility(View.GONE);
597 mDialpadButton.setVisibility(View.VISIBLE);
598 mMenuButton.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700599
Yorke Lee86e21f72014-04-02 16:49:38 -0700600 SearchFragment fragment = null;
601 if (mInDialpadSearch) {
602 fragment = mSmartDialSearchFragment;
603 } else if (mInRegularSearch) {
604 fragment = mRegularSearchFragment;
605 }
606 if (fragment != null && fragment.isVisible()) {
607 fragment.getListView().animate().translationY(0)
608 .setInterpolator(showActionBarInterpolator).setDuration(ANIMATION_DURATION);
Yorke Leec3766332013-07-31 11:13:16 -0700609 }
610
Yorke Lee86e21f72014-04-02 16:49:38 -0700611 if (mPhoneFavoriteFragment != null && mPhoneFavoriteFragment.isVisible()) {
612 mFragmentsFrame.animate().alpha(1.0f);
613 }
614 getActionBar().show();
Yorke Leec3766332013-07-31 11:13:16 -0700615 }
616
Yorke Lee86e21f72014-04-02 16:49:38 -0700617 @Override
618 public boolean onCreateOptionsMenu(Menu menu) {
619 if (DEBUG) {
620 Log.d(TAG, "onCreateOptionsMenu");
621 }
622 MenuInflater inflater = getMenuInflater();
623 inflater.inflate(R.menu.dialtacts_options, menu);
624 final MenuItem searchItem = menu.findItem(R.id.menu_search);
625 mSearchView = (SearchView) searchItem.getActionView();
626 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
627 mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
628 mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener);
629 mSearchView.setIconifiedByDefault(false);
630 return super.onCreateOptionsMenu(menu);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700631 }
632
Chiao Cheng94b10b52012-08-17 16:59:12 -0700633 /**
634 * Returns true if the intent is due to hitting the green send key (hardware call button:
635 * KEYCODE_CALL) while in a call.
636 *
637 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700638 * @return true if the intent is due to hitting the green send key while in a call
639 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700640 private boolean isSendKeyWhileInCall(Intent intent) {
641 // If there is a call in progress and the user launched the dialer by hitting the call
642 // button, go straight to the in-call screen.
643 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700644
Yorke Lee62e995c2014-03-28 10:02:56 -0700645 try {
646 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
647 if (callKey && phone != null && phone.showCallScreen()) {
648 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700649 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700650 } catch (RemoteException e) {
651 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700652 }
653
654 return false;
655 }
656
657 /**
658 * Sets the current tab based on the intent's request type
659 *
660 * @param intent Intent that contains information about which tab should be selected
661 */
Yorke Leec3766332013-07-31 11:13:16 -0700662 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700663 // If we got here by hitting send and we're in call forward along to the in-call activity
Yorke Lee62e995c2014-03-28 10:02:56 -0700664 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700665 finish();
666 return;
667 }
668
Yorke Lee669b6082013-09-17 15:43:38 -0700669 if (mDialpadFragment != null) {
670 final boolean phoneIsInUse = phoneIsInUse();
671 if (phoneIsInUse || isDialIntent(intent)) {
672 mDialpadFragment.setStartedFromNewIntent(true);
673 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
674 mInCallDialpadUp = true;
675 }
676 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700677 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700678 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700679 }
680
681 @Override
682 public void onNewIntent(Intent newIntent) {
683 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700684 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700685
Chiao Cheng94b10b52012-08-17 16:59:12 -0700686 invalidateOptionsMenu();
687 }
688
689 /** Returns true if the given intent contains a phone number to populate the dialer with */
690 private boolean isDialIntent(Intent intent) {
691 final String action = intent.getAction();
692 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
693 return true;
694 }
695 if (Intent.ACTION_VIEW.equals(action)) {
696 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700697 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700698 return true;
699 }
700 }
701 return false;
702 }
703
704 /**
705 * Returns an appropriate call origin for this Activity. May return null when no call origin
706 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
707 * for remembering the tab in which the user made a phone call, so the external app's DIAL
708 * request should not be counted.)
709 */
710 public String getCallOrigin() {
711 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
712 }
713
Yorke Lee8898cd02013-08-08 10:24:27 -0700714 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
715 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700716 @Override
717 public void onContactSelected(Uri contactUri) {
718 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700719 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700720 }
721
722 @Override
723 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700724 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700725 startActivity(intent);
726 }
727 };
728
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700730 final InputMethodManager imm = (InputMethodManager) getSystemService(
731 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700732 if (imm != null && view != null) {
733 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
734 }
735 }
736
737 /**
Yorke Leec3766332013-07-31 11:13:16 -0700738 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700739 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700740 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700741 if (getFragmentManager().isDestroyed()) {
742 // Weird race condition where fragment is doing work after the activity is destroyed
743 // due to talkback being on (b/10209937). Just return since we can't do any
744 // constructive here.
745 return;
746 }
747
Yorke Leeef2b7382013-08-09 17:39:25 -0700748 if (DEBUG) {
749 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
750 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700751
Yorke Leec3766332013-07-31 11:13:16 -0700752 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700753
Yorke Leeef2b7382013-08-09 17:39:25 -0700754 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700755 if (mInDialpadSearch) {
756 transaction.remove(mSmartDialSearchFragment);
757 } else if (mInRegularSearch) {
758 transaction.remove(mRegularSearchFragment);
759 } else {
760 transaction.remove(mPhoneFavoriteFragment);
761 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700762
Yorke Leeb207f8a2013-08-29 18:51:06 -0700763 final String tag;
764 if (smartDialSearch) {
765 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
766 } else {
767 tag = TAG_REGULAR_SEARCH_FRAGMENT;
768 }
769 mInDialpadSearch = smartDialSearch;
770 mInRegularSearch = !smartDialSearch;
771
Yorke Leeef2b7382013-08-09 17:39:25 -0700772 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
773 if (fragment == null) {
774 if (smartDialSearch) {
775 fragment = new SmartDialSearchFragment();
776 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700777 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700778 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700779 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700780 // DialtactsActivity will provide the options menu
781 fragment.setHasOptionsMenu(false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700782 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700783 transaction.addToBackStack(null);
784 fragment.setQueryString(query, false);
785 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700786 }
787
788 /**
Yorke Leec3766332013-07-31 11:13:16 -0700789 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700790 */
Yorke Leec3766332013-07-31 11:13:16 -0700791 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700792 // See related bug in enterSearchUI();
793 if (getFragmentManager().isDestroyed()) {
794 return;
795 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700796 // Go all the way back to the favorites fragment, regardless of how many times we
797 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700798 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
799 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700800
801 if (isDialpadShowing()) {
802 mFragmentsFrame.setAlpha(0);
803 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700804 }
805
806 /** Returns an Intent to launch Call Settings screen */
807 public static Intent getCallSettingsIntent() {
808 final Intent intent = new Intent(Intent.ACTION_MAIN);
809 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
810 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
811 return intent;
812 }
813
814 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700815 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700816 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700817 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700818 } else if (getInSearchUi()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700819 mSearchView.setQuery(null, false);
Yorke Leef6673d32013-08-23 15:34:17 -0700820 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700821 } else {
822 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700823 }
Yorke Leec3766332013-07-31 11:13:16 -0700824 }
825
826 @Override
827 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700828 if (mSmartDialSearchFragment != null) {
829 mSmartDialSearchFragment.setAddToContactNumber(query);
830 }
Yorke Leec3766332013-07-31 11:13:16 -0700831 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
832 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee86e21f72014-04-02 16:49:38 -0700833 if (!TextUtils.equals(mSearchView.getQuery(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700834 if (DEBUG) {
835 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
836 }
837 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
838 // This callback can happen if the dialpad fragment is recreated because of
839 // activity destruction. In that case, don't update the search view because
840 // that would bring the user back to the search fragment regardless of the
841 // previous state of the application. Instead, just return here and let the
842 // fragment manager correctly figure out whatever fragment was last displayed.
843 return;
844 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700845 if (mDialpadFragment != null) {
846 mDialpadOverflowMenu = mDialpadFragment.buildOptionsMenu(mMenuButton);
847 }
848 mSearchView.setQuery(normalizedQuery, false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700849 }
850 }
Yorke Leec3766332013-07-31 11:13:16 -0700851
852 @Override
853 public void onListFragmentScrollStateChange(int scrollState) {
854 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700855 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700856 hideInputMethod(getCurrentFocus());
857 }
858 }
859
860 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700861 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
862 int totalItemCount) {
863
864 // Hide the action bar when scrolling down in the speed dial list, and show it again when
865 // scrolling back up.
866 if (firstVisibleItem > mPreviousFirstVisibleItem) {
867 getActionBar().hide();
868 } else if (firstVisibleItem < mPreviousFirstVisibleItem) {
869 getActionBar().show();
870 }
871 mPreviousFirstVisibleItem = firstVisibleItem;
872 }
873
874 private int mPreviousFirstVisibleItem = 0;
875
876 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800877 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700878 if (mDialButton != null) {
879 mDialButton.setEnabled(enabled);
880 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800881 }
882
883 @Override
884 public void setDialButtonContainerVisible(boolean visible) {
885 mFakeActionBar.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700886 }
887
888 private boolean phoneIsInUse() {
889 final TelephonyManager tm = (TelephonyManager) getSystemService(
890 Context.TELEPHONY_SERVICE);
891 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
892 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700893
894 @Override
895 public void onShowAllContacts() {
896 final Intent intent = new Intent(this, AllContactsActivity.class);
897 startActivity(intent);
898 }
Yorke Leee1424812013-09-04 18:24:48 -0700899
900 public static Intent getAddNumberToContactIntent(CharSequence text) {
901 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
902 intent.putExtra(Intents.Insert.PHONE, text);
903 intent.setType(Contacts.CONTENT_ITEM_TYPE);
904 return intent;
905 }
906
Yorke Leeda0f9042013-12-05 14:25:51 -0800907 private boolean canIntentBeHandled(Intent intent) {
908 final PackageManager packageManager = getPackageManager();
909 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
910 PackageManager.MATCH_DEFAULT_ONLY);
911 return resolveInfo != null && resolveInfo.size() > 0;
912 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800913
Yorke Lee86e21f72014-04-02 16:49:38 -0700914 /**
915 * Called when the user has long-pressed a contact tile to start a drag operation.
916 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800917 @Override
918 public void onDragStarted(int itemIndex, int x, int y, PhoneFavoriteTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700919 getActionBar().hide();
920 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800921 }
922
923 @Override
924 public void onDragHovered(int itemIndex, int x, int y) {}
925
Yorke Lee86e21f72014-04-02 16:49:38 -0700926 /**
927 * Called when the user has released a contact tile after long-pressing it.
928 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800929 @Override
930 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700931 getActionBar().show();
932 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800933 }
934
935 @Override
936 public void onDroppedOnRemove() {}
937
938 /**
939 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
940 * once it has been attached to the activity.
941 */
942 @Override
943 public void setDragDropController(DragDropController dragController) {
944 mRemoveViewContainer.setDragDropController(dragController);
945 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700946}