blob: 3603696bfbc0ec509647e2f5ec5a04a13eed9791 [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 Lee33932ff2014-04-16 13:34:32 -070032import android.content.res.TypedArray;
Yorke Lee86e21f72014-04-02 16:49:38 -070033import android.graphics.Outline;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.net.Uri;
35import android.os.Bundle;
36import android.os.RemoteException;
37import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070039import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070040import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070041import android.telephony.TelephonyManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070042import android.text.TextUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.util.Log;
44import android.view.Menu;
Yorke Lee86e21f72014-04-02 16:49:38 -070045import android.view.MenuInflater;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.View;
Yorke Leea5d5cdf2014-04-22 17:20:00 -070048import android.view.View.OnClickListener;
Yorke Lee86e21f72014-04-02 16:49:38 -070049import android.view.View.OnLayoutChangeListener;
Yorke Leeb0d19762014-03-05 14:43:28 -080050import android.view.ViewGroup.LayoutParams;
51import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Yorke Lee86e21f72014-04-02 16:49:38 -070052import android.view.animation.AccelerateInterpolator;
53import android.view.animation.DecelerateInterpolator;
54import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070055import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070056import android.widget.AbsListView.OnScrollListener;
Yorke Leeb0d19762014-03-05 14:43:28 -080057import android.widget.LinearLayout;
Chiao Cheng94b10b52012-08-17 16:59:12 -070058import android.widget.PopupMenu;
Yorke Lee86e21f72014-04-02 16:49:38 -070059import android.widget.SearchView;
60import android.widget.SearchView.OnQueryTextListener;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080061import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070062
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070063import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070064import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070065import com.android.contacts.common.dialog.ClearFrequentsDialog;
66import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080067import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070068import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070069import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070070import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070071import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070073import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070074import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080075import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070076import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080077import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070078import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070079import com.android.dialer.list.SpeedDialFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080080import com.android.dialer.list.PhoneFavoriteTileView;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070081import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070082import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080083import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070084import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070085import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070086import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070087import com.android.internal.telephony.ITelephony;
88
Yorke Leec3766332013-07-31 11:13:16 -070089import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080090import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070091
Chiao Cheng94b10b52012-08-17 16:59:12 -070092/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070093 * The dialer tab's title is 'phone', a more common name (see strings.xml).
94 */
Yorke Leec3766332013-07-31 11:13:16 -070095public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070096 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -070097 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080098 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -070099 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700100 SpeedDialFragment.HostInterface,
Yorke Leee00c9fe2014-04-12 12:42:06 -0700101 OnDragDropListener, View.OnLongClickListener,
102 OnPhoneNumberPickerActionListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103 private static final String TAG = "DialtactsActivity";
104
Ihab Awad526c0b82014-02-27 12:55:36 -0800105 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700106
Yorke Leef74011e2013-08-02 11:30:30 -0700107 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
108
Chiao Cheng94b10b52012-08-17 16:59:12 -0700109 /** Used to open Call Setting */
110 private static final String PHONE_PACKAGE = "com.android.phone";
111 private static final String CALL_SETTINGS_CLASS_NAME =
112 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 /** @see #getCallOrigin() */
114 private static final String CALL_ORIGIN_DIALTACTS =
115 "com.android.dialer.DialtactsActivity";
116
Yorke Leeb207f8a2013-08-29 18:51:06 -0700117 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
118 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700119 private static final String KEY_SEARCH_QUERY = "search_query";
120 private static final String KEY_FIRST_LAUNCH = "first_launch";
121
Yorke Leec3766332013-07-31 11:13:16 -0700122 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
123 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
124 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
125 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700126
Chiao Cheng94b10b52012-08-17 16:59:12 -0700127 /**
128 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
129 */
130 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
131
Yorke Leec3766332013-07-31 11:13:16 -0700132 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700133
Yorke Lee86e21f72014-04-02 16:49:38 -0700134 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800135
Yorke Leec3766332013-07-31 11:13:16 -0700136 /**
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
Yorke Leee00c9fe2014-04-12 12:42:06 -0700151 /**
152 * Fragment containing the speed dial list, recents list, and all contacts list.
153 */
154 private ListsFragment mListsFragment;
155
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700156 private View mFloatingActionButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700157 private View mMenuButton;
Yorke Leec3766332013-07-31 11:13:16 -0700158 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800159 private View mDialButton;
Yorke Leec3766332013-07-31 11:13:16 -0700160
Yorke Leeb207f8a2013-08-29 18:51:06 -0700161 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700162
Yorke Lee33932ff2014-04-16 13:34:32 -0700163 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700164 private boolean mInDialpadSearch;
165 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700166 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700167
Yorke Leeef2b7382013-08-09 17:39:25 -0700168 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700169 * True if the dialpad is only temporarily showing due to being in call
170 */
171 private boolean mInCallDialpadUp;
172
173 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700174 * True when this activity has been launched for the first time.
175 */
Yorke Lee98702de2013-08-06 12:03:32 -0700176 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700177
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700178 /**
179 * Search query to be applied to the SearchView in the ActionBar once
180 * onCreateOptionsMenu has been called.
181 */
182 private String mPendingSearchViewQuery;
183
Yorke Lee94f49042014-03-03 14:03:08 -0800184 // This view points to the Framelayout that houses both the search view and remove view
185 // containers.
186 private View mSearchAndRemoveViewContainer;
Yorke Lee86e21f72014-04-02 16:49:38 -0700187 private SearchView mSearchView;
188 /**
189 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
190 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
191 * removed from the speed dial list.
192 */
193 private RemoveView mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700194
Yorke Lee86e21f72014-04-02 16:49:38 -0700195 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
196 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700197 private String mSearchQuery;
198
Yorke Leefce269a2013-08-12 11:56:04 -0700199 private DialerDatabaseHelper mDialerDatabaseHelper;
200
Yorke Leee00c9fe2014-04-12 12:42:06 -0700201 private class OverflowPopupMenu extends PopupMenu {
202 public OverflowPopupMenu(Context context, View anchor) {
203 super(context, anchor);
204 }
205
206 @Override
207 public void show() {
208 final Menu menu = getMenu();
209 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Leed999b712014-04-23 15:10:58 -0700210 // TODO: Check mSpeedDialFragment.hasFrequents()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700211 clearFrequents.setVisible(true);
212 super.show();
213 }
214 }
215
Chiao Cheng94b10b52012-08-17 16:59:12 -0700216 /**
217 * Listener used when one of phone numbers in search UI is selected. This will initiate a
218 * phone call using the phone number.
219 */
220 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
221 new OnPhoneNumberPickerActionListener() {
222 @Override
223 public void onPickPhoneNumberAction(Uri dataUri) {
224 // Specify call-origin so that users will see the previous tab instead of
225 // CallLog screen (search UI will be automatically exited).
226 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700227 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700228 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700229 }
230
231 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700232 public void onCallNumberDirectly(String phoneNumber) {
233 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
234 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700235 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700236 }
237
238 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700239 public void onShortcutIntentCreated(Intent intent) {
240 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
241 }
242
243 @Override
244 public void onHomeInActionBarSelected() {
245 exitSearchUi();
246 }
247 };
248
249 /**
250 * Listener used to send search queries to the phone search fragment.
251 */
Yorke Lee86e21f72014-04-02 16:49:38 -0700252 private final OnQueryTextListener mPhoneSearchQueryTextListener = new OnQueryTextListener() {
Yorke Leec3766332013-07-31 11:13:16 -0700253 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700254 public boolean onQueryTextSubmit(String query) {
255 return false;
Yorke Leec3766332013-07-31 11:13:16 -0700256 }
257
258 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700259 public boolean onQueryTextChange(String newText) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700260 if (newText.equals(mSearchQuery)) {
261 // If the query hasn't changed (perhaps due to activity being destroyed
262 // and restored, or user launching the same DIAL intent twice), then there is
263 // no need to do anything here.
Yorke Lee86e21f72014-04-02 16:49:38 -0700264 return true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700265 }
266 mSearchQuery = newText;
267 if (DEBUG) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700268 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leeef2b7382013-08-09 17:39:25 -0700269 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700270 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700271
Yorke Leec3766332013-07-31 11:13:16 -0700272 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700273 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700274 exitSearchUi();
Yorke Lee86e21f72014-04-02 16:49:38 -0700275 return true;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700276 } else if (!TextUtils.isEmpty(newText)) {
277 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
278 (!dialpadSearch && mInRegularSearch);
279 if (!sameSearchMode) {
280 // call enterSearchUi only if we are switching search modes, or entering
281 // search ui for the first time
282 enterSearchUi(dialpadSearch, newText);
283 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700284
Yorke Leeb207f8a2013-08-29 18:51:06 -0700285 if (dialpadSearch && mSmartDialSearchFragment != null) {
286 mSmartDialSearchFragment.setQueryString(newText, false);
287 } else if (mRegularSearchFragment != null) {
288 mRegularSearchFragment.setQueryString(newText, false);
289 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700290 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700292 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700293 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700294 };
295
Yorke Leec3766332013-07-31 11:13:16 -0700296 private boolean isDialpadShowing() {
297 return mDialpadFragment != null && mDialpadFragment.isVisible();
298 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700299
300 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700301 protected void onCreate(Bundle savedInstanceState) {
302 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700303 mFirstLaunch = true;
304
Yorke Lee8898cd02013-08-08 10:24:27 -0700305 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800306 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700307
Yorke Lee86e21f72014-04-02 16:49:38 -0700308 getActionBar().setDisplayShowHomeEnabled(false);
309 getActionBar().setDisplayShowTitleEnabled(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700310
Yorke Lee33932ff2014-04-16 13:34:32 -0700311 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
312 new int[] { android.R.attr.actionBarSize });
313 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
314 styledAttributes.recycle();
315
Yorke Leeef2b7382013-08-09 17:39:25 -0700316 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
317 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700318 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800319 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700320 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800321 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
322 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700323 } else {
324 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700325 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
326 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700327 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700328 }
329
Yorke Leeb207f8a2013-08-29 18:51:06 -0700330 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee86e21f72014-04-02 16:49:38 -0700331
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700332 mFloatingActionButton = findViewById(R.id.floating_action_button);
Andrew Lee4cbc9732014-04-24 14:38:58 -0700333 ViewUtil.setupFloatingActionButton(mFloatingActionButton, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700334
Yorke Lee86e21f72014-04-02 16:49:38 -0700335 mDialButton = findViewById(R.id.dial_button);
336 mDialButton.setOnClickListener(this);
337 mDialpadButton = findViewById(R.id.dialpad_button);
338 mDialpadButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800339
Yorke Lee3cefcc62014-01-16 11:26:46 -0800340 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800341 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
342
Yorke Lee0baa98b2013-08-22 10:55:16 -0700343 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700344 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700345 }
346
347 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700348 protected void onResume() {
349 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700350 if (mFirstLaunch) {
351 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700352 } else if (!phoneIsInUse() && mInCallDialpadUp) {
353 hideDialpadFragment(false, true);
354 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700355 }
356 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700357 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700358 }
359
360 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700361 protected void onPause() {
362 if (mClearSearchOnPause) {
363 hideDialpadAndSearchUi();
364 mClearSearchOnPause = false;
365 }
366 super.onPause();
367 }
368
369 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700370 protected void onSaveInstanceState(Bundle outState) {
371 super.onSaveInstanceState(outState);
372 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700373 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
374 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700375 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
376 }
377
378 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700379 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700380 if (fragment instanceof DialpadFragment) {
381 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700382 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700383 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700384 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700385 } else if (fragment instanceof SmartDialSearchFragment) {
386 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700387 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
388 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700389 if (mFragmentsFrame != null) {
390 mFragmentsFrame.setAlpha(1.0f);
391 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700392 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700393 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700394 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
395 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700396 if (mFragmentsFrame != null) {
397 mFragmentsFrame.setAlpha(1.0f);
398 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700399 } else if (fragment instanceof ListsFragment) {
400 mListsFragment = (ListsFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700401 }
Yorke Leec3766332013-07-31 11:13:16 -0700402 }
403
Alon Albertb453e5b2013-09-10 15:54:23 -0700404 protected void handleMenuSettings() {
405 openTelephonySetting(this);
406 }
407
408 public static void openTelephonySetting(Activity activity) {
409 final Intent settingsIntent = getCallSettingsIntent();
410 activity.startActivity(settingsIntent);
411 }
412
Chiao Cheng94b10b52012-08-17 16:59:12 -0700413 @Override
414 public void onClick(View view) {
415 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700416 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700417 // Reset the boolean flag that tracks whether the dialpad was up because
418 // we were in call. Regardless of whether it was true before, we want to
419 // show the dialpad because the user has explicitly clicked the dialpad
420 // button.
421 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700422 showDialpadFragment(true);
423 break;
Yorke Lee86e21f72014-04-02 16:49:38 -0700424 case R.id.dial_button:
425 // Dial button was pressed; tell the Dialpad fragment
426 mDialpadFragment.dialButtonPressed();
427 break;
Yorke Leec3766332013-07-31 11:13:16 -0700428 case R.id.search_close_button:
429 // Clear the search field
Yorke Lee86e21f72014-04-02 16:49:38 -0700430 if (!TextUtils.isEmpty(mSearchView.getQuery())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700431 mDialpadFragment.clearDialpad();
Yorke Lee86e21f72014-04-02 16:49:38 -0700432 mSearchView.setQuery("", false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700433 }
434 break;
Yorke Leec3766332013-07-31 11:13:16 -0700435 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800436 try {
437 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
438 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
439 } catch (ActivityNotFoundException e) {
440 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
441 Toast.LENGTH_SHORT).show();
442 }
Yorke Leec3766332013-07-31 11:13:16 -0700443 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700444 default: {
445 Log.wtf(TAG, "Unexpected onClick event from " + view);
446 break;
447 }
448 }
449 }
450
Yorke Leec3766332013-07-31 11:13:16 -0700451 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700452 public boolean onOptionsItemSelected(MenuItem item) {
453 switch (item.getItemId()) {
454 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700455 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700456 break;
457 case R.id.menu_add_contact:
458 try {
459 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
460 } catch (ActivityNotFoundException e) {
461 Toast toast = Toast.makeText(this,
462 R.string.add_contact_not_available,
463 Toast.LENGTH_SHORT);
464 toast.show();
465 }
466 break;
467 case R.id.menu_import_export:
468 // We hard-code the "contactsAreAvailable" argument because doing it properly would
469 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
470 // now in Dialtacts for (potential) performance reasons. Compare with how it is
471 // done in {@link PeopleActivity}.
472 ImportExportDialogFragment.show(getFragmentManager(), true,
473 DialtactsActivity.class);
474 return true;
475 case R.id.menu_clear_frequents:
476 // TODO: This should be enabled/disabled based on
Yorke Leed999b712014-04-23 15:10:58 -0700477 // SpeedDialFragment.hasFrequents
Yorke Lee86e21f72014-04-02 16:49:38 -0700478 ClearFrequentsDialog.show(getFragmentManager());
479 return true;
480 case R.id.menu_call_settings:
481 handleMenuSettings();
482 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700483 }
484 return false;
485 }
486
487 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800488 public boolean onLongClick(View view) {
489 switch (view.getId()) {
490 case R.id.dial_button: {
491 // Dial button was pressed; tell the Dialpad fragment
492 mDialpadFragment.dialButtonPressed();
493 return true; // Consume the event
494 }
495 default: {
496 Log.wtf(TAG, "Unexpected onClick event from " + view);
497 break;
498 }
499 }
500 return false;
501 }
502
503 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700504 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
505 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
506 if (resultCode == RESULT_OK) {
507 final ArrayList<String> matches = data.getStringArrayListExtra(
508 RecognizerIntent.EXTRA_RESULTS);
509 if (matches.size() > 0) {
510 final String match = matches.get(0);
Yorke Lee86e21f72014-04-02 16:49:38 -0700511 mSearchView.setQuery(match, false);
Yorke Leec3766332013-07-31 11:13:16 -0700512 } else {
513 Log.e(TAG, "Voice search - nothing heard");
514 }
515 } else {
516 Log.e(TAG, "Voice search failed");
517 }
518 }
519 super.onActivityResult(requestCode, resultCode, data);
520 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700521
Yorke Leec3766332013-07-31 11:13:16 -0700522 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700523 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700524 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700525 if (animate) {
526 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700527 } else {
528 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700529 }
530 ft.show(mDialpadFragment);
531 ft.commit();
532 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700533
Yorke Leeca195042013-09-25 16:29:38 -0700534 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700535 if (mDialpadFragment == null) return;
536 if (clearDialpad) {
537 mDialpadFragment.clearDialpad();
538 }
539 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700540 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700541 final FragmentTransaction ft = getFragmentManager().beginTransaction();
542 if (animate) {
543 ft.setCustomAnimations(0, R.anim.slide_out);
544 }
545 ft.hide(mDialpadFragment);
546 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700547 }
548
Yorke Leec3766332013-07-31 11:13:16 -0700549 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
550 @Override
551 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800552 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700553 }
554 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700555
Yorke Leeb207f8a2013-08-29 18:51:06 -0700556 private boolean getInSearchUi() {
557 return mInDialpadSearch || mInRegularSearch;
558 }
559
560 private void setNotInSearchUi() {
561 mInDialpadSearch = false;
562 mInRegularSearch = false;
563 }
564
Yorke Lee311969c2013-08-26 06:31:11 -0700565 private void hideDialpadAndSearchUi() {
Yorke Lee86e21f72014-04-02 16:49:38 -0700566 mSearchView.setQuery("", false);
Yorke Lee311969c2013-08-26 06:31:11 -0700567 hideDialpadFragment(false, true);
568 }
569
Yorke Lee86e21f72014-04-02 16:49:38 -0700570 /**
571 * Callback from child DialpadFragment when the dialpad is shown.
572 */
573 public void onDialpadShown() {
574 mDialButton.setVisibility(View.VISIBLE);
575 mDialpadButton.setVisibility(View.GONE);
Yorke Leeb0d19762014-03-05 14:43:28 -0800576
Yorke Lee86e21f72014-04-02 16:49:38 -0700577 SearchFragment fragment = null;
578 if (mInDialpadSearch) {
579 fragment = mSmartDialSearchFragment;
580 } else if (mInRegularSearch) {
581 fragment = mRegularSearchFragment;
582 }
583 if (fragment != null && fragment.isVisible()) {
Yorke Lee33932ff2014-04-16 13:34:32 -0700584 fragment.getListView().animate().translationY(-mActionBarHeight)
Yorke Lee86e21f72014-04-02 16:49:38 -0700585 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
586 }
Christine Chen9c1e0652013-05-23 15:40:19 -0700587
Yorke Lee33932ff2014-04-16 13:34:32 -0700588 if (mListsFragment != null && mListsFragment.isResumed() && mListsFragment.isVisible()) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700589 // If the favorites fragment is showing, fade to blank.
590 mFragmentsFrame.animate().alpha(0.0f);
591 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700592 getActionBar().hide();
Yorke Leec3766332013-07-31 11:13:16 -0700593 }
594
Yorke Lee86e21f72014-04-02 16:49:38 -0700595 /**
596 * Callback from child DialpadFragment when the dialpad is hidden.
597 */
598 public void onDialpadHidden() {
599 mDialButton.setVisibility(View.GONE);
600 mDialpadButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700601
Yorke Lee86e21f72014-04-02 16:49:38 -0700602 SearchFragment fragment = null;
603 if (mInDialpadSearch) {
604 fragment = mSmartDialSearchFragment;
605 } else if (mInRegularSearch) {
606 fragment = mRegularSearchFragment;
607 }
608 if (fragment != null && fragment.isVisible()) {
609 fragment.getListView().animate().translationY(0)
610 .setInterpolator(showActionBarInterpolator).setDuration(ANIMATION_DURATION);
Yorke Leec3766332013-07-31 11:13:16 -0700611 }
612
Yorke Leee00c9fe2014-04-12 12:42:06 -0700613 if (mListsFragment != null && mListsFragment.isVisible()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700614 mFragmentsFrame.animate().alpha(1.0f);
615 }
616 getActionBar().show();
Yorke Leec3766332013-07-31 11:13:16 -0700617 }
618
Yorke Leee00c9fe2014-04-12 12:42:06 -0700619 private void hideInputMethod(View view) {
620 final InputMethodManager imm = (InputMethodManager) getSystemService(
621 Context.INPUT_METHOD_SERVICE);
622 if (imm != null && view != null) {
623 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
624 }
625 }
626
Yorke Lee86e21f72014-04-02 16:49:38 -0700627 @Override
628 public boolean onCreateOptionsMenu(Menu menu) {
629 if (DEBUG) {
630 Log.d(TAG, "onCreateOptionsMenu");
631 }
632 MenuInflater inflater = getMenuInflater();
633 inflater.inflate(R.menu.dialtacts_options, menu);
634 final MenuItem searchItem = menu.findItem(R.id.menu_search);
635 mSearchView = (SearchView) searchItem.getActionView();
636 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
637 mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
638 mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener);
639 mSearchView.setIconifiedByDefault(false);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700640
641 if (mPendingSearchViewQuery != null) {
642 mSearchView.setQuery(mPendingSearchViewQuery, false);
643 mPendingSearchViewQuery = null;
644 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700645 return super.onCreateOptionsMenu(menu);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700646 }
647
Chiao Cheng94b10b52012-08-17 16:59:12 -0700648 /**
649 * Returns true if the intent is due to hitting the green send key (hardware call button:
650 * KEYCODE_CALL) while in a call.
651 *
652 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700653 * @return true if the intent is due to hitting the green send key while in a call
654 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700655 private boolean isSendKeyWhileInCall(Intent intent) {
656 // If there is a call in progress and the user launched the dialer by hitting the call
657 // button, go straight to the in-call screen.
658 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700659
Yorke Lee62e995c2014-03-28 10:02:56 -0700660 try {
661 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
662 if (callKey && phone != null && phone.showCallScreen()) {
663 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700664 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700665 } catch (RemoteException e) {
666 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700667 }
668
669 return false;
670 }
671
672 /**
673 * Sets the current tab based on the intent's request type
674 *
675 * @param intent Intent that contains information about which tab should be selected
676 */
Yorke Leec3766332013-07-31 11:13:16 -0700677 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700678 // 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 -0700679 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700680 finish();
681 return;
682 }
683
Yorke Lee669b6082013-09-17 15:43:38 -0700684 if (mDialpadFragment != null) {
685 final boolean phoneIsInUse = phoneIsInUse();
686 if (phoneIsInUse || isDialIntent(intent)) {
687 mDialpadFragment.setStartedFromNewIntent(true);
688 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
689 mInCallDialpadUp = true;
690 }
691 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700692 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700693 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700694 }
695
696 @Override
697 public void onNewIntent(Intent newIntent) {
698 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700699 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700700
Chiao Cheng94b10b52012-08-17 16:59:12 -0700701 invalidateOptionsMenu();
702 }
703
704 /** Returns true if the given intent contains a phone number to populate the dialer with */
705 private boolean isDialIntent(Intent intent) {
706 final String action = intent.getAction();
707 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
708 return true;
709 }
710 if (Intent.ACTION_VIEW.equals(action)) {
711 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700712 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700713 return true;
714 }
715 }
716 return false;
717 }
718
719 /**
720 * Returns an appropriate call origin for this Activity. May return null when no call origin
721 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
722 * for remembering the tab in which the user made a phone call, so the external app's DIAL
723 * request should not be counted.)
724 */
725 public String getCallOrigin() {
726 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
727 }
728
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729 /**
Yorke Leec3766332013-07-31 11:13:16 -0700730 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700731 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700732 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700733 if (getFragmentManager().isDestroyed()) {
734 // Weird race condition where fragment is doing work after the activity is destroyed
735 // due to talkback being on (b/10209937). Just return since we can't do any
736 // constructive here.
737 return;
738 }
739
Yorke Leeef2b7382013-08-09 17:39:25 -0700740 if (DEBUG) {
741 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
742 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700743
Yorke Leec3766332013-07-31 11:13:16 -0700744 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700745
Yorke Leeef2b7382013-08-09 17:39:25 -0700746 SearchFragment fragment;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700747 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700748 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700749 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700750 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700751 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700752
Yorke Leeb207f8a2013-08-29 18:51:06 -0700753 final String tag;
754 if (smartDialSearch) {
755 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
756 } else {
757 tag = TAG_REGULAR_SEARCH_FRAGMENT;
758 }
759 mInDialpadSearch = smartDialSearch;
760 mInRegularSearch = !smartDialSearch;
761
Yorke Leeef2b7382013-08-09 17:39:25 -0700762 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
763 if (fragment == null) {
764 if (smartDialSearch) {
765 fragment = new SmartDialSearchFragment();
766 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700767 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700768 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700769 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700770 // DialtactsActivity will provide the options menu
771 fragment.setHasOptionsMenu(false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700772 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700773 transaction.addToBackStack(null);
774 fragment.setQueryString(query, false);
775 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700776 }
777
778 /**
Yorke Leec3766332013-07-31 11:13:16 -0700779 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700780 */
Yorke Leec3766332013-07-31 11:13:16 -0700781 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700782 // See related bug in enterSearchUI();
783 if (getFragmentManager().isDestroyed()) {
784 return;
785 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700786 // Go all the way back to the favorites fragment, regardless of how many times we
787 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700788 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
789 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700790
791 if (isDialpadShowing()) {
792 mFragmentsFrame.setAlpha(0);
793 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700794 }
795
796 /** Returns an Intent to launch Call Settings screen */
797 public static Intent getCallSettingsIntent() {
798 final Intent intent = new Intent(Intent.ACTION_MAIN);
799 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
800 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
801 return intent;
802 }
803
804 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700805 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700806 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700807 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700808 } else if (getInSearchUi()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700809 mSearchView.setQuery(null, false);
Yorke Leef6673d32013-08-23 15:34:17 -0700810 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700811 } else {
812 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700813 }
Yorke Leec3766332013-07-31 11:13:16 -0700814 }
815
816 @Override
817 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700818 if (mSmartDialSearchFragment != null) {
819 mSmartDialSearchFragment.setAddToContactNumber(query);
820 }
Yorke Leec3766332013-07-31 11:13:16 -0700821 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
822 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700823 if (mSearchView == null) {
824 if (!TextUtils.isEmpty(normalizedQuery)) {
825 mPendingSearchViewQuery = normalizedQuery;
826 }
827 return;
828 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700829 if (!TextUtils.equals(mSearchView.getQuery(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700830 if (DEBUG) {
831 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
832 }
833 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
834 // This callback can happen if the dialpad fragment is recreated because of
835 // activity destruction. In that case, don't update the search view because
836 // that would bring the user back to the search fragment regardless of the
837 // previous state of the application. Instead, just return here and let the
838 // fragment manager correctly figure out whatever fragment was last displayed.
839 return;
840 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700841 mSearchView.setQuery(normalizedQuery, false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700842 }
843 }
Yorke Leec3766332013-07-31 11:13:16 -0700844
845 @Override
846 public void onListFragmentScrollStateChange(int scrollState) {
847 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700848 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700849 hideInputMethod(getCurrentFocus());
850 }
851 }
852
853 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700854 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
855 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700856 // TODO: No-op for now. This should eventually show/hide the actionBar based on
857 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700858 }
859
Yorke Lee86e21f72014-04-02 16:49:38 -0700860 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800861 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700862 if (mDialButton != null) {
863 mDialButton.setEnabled(enabled);
864 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800865 }
866
867 @Override
868 public void setDialButtonContainerVisible(boolean visible) {
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700869 mFloatingActionButton.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700870 }
871
872 private boolean phoneIsInUse() {
873 final TelephonyManager tm = (TelephonyManager) getSystemService(
874 Context.TELEPHONY_SERVICE);
875 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
876 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700877
Yorke Leee1424812013-09-04 18:24:48 -0700878 public static Intent getAddNumberToContactIntent(CharSequence text) {
879 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
880 intent.putExtra(Intents.Insert.PHONE, text);
881 intent.setType(Contacts.CONTENT_ITEM_TYPE);
882 return intent;
883 }
884
Yorke Leeda0f9042013-12-05 14:25:51 -0800885 private boolean canIntentBeHandled(Intent intent) {
886 final PackageManager packageManager = getPackageManager();
887 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
888 PackageManager.MATCH_DEFAULT_ONLY);
889 return resolveInfo != null && resolveInfo.size() > 0;
890 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800891
Yorke Lee6a1461a2014-04-21 16:27:14 -0700892 @Override
893 public void showCallHistory() {
894 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
895 // CONTENT_TYPE, so that we always open our call log from our dialer
896 final Intent intent = new Intent(this, CallLogActivity.class);
897 startActivity(intent);
898 }
899
Yorke Lee86e21f72014-04-02 16:49:38 -0700900 /**
901 * Called when the user has long-pressed a contact tile to start a drag operation.
902 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800903 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700904 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700905 getActionBar().hide();
906 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800907 }
908
909 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700910 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {}
Yorke Lee3cefcc62014-01-16 11:26:46 -0800911
Yorke Lee86e21f72014-04-02 16:49:38 -0700912 /**
913 * Called when the user has released a contact tile after long-pressing it.
914 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800915 @Override
916 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700917 getActionBar().show();
918 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800919 }
920
921 @Override
922 public void onDroppedOnRemove() {}
923
924 /**
Yorke Leed999b712014-04-23 15:10:58 -0700925 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800926 * once it has been attached to the activity.
927 */
928 @Override
929 public void setDragDropController(DragDropController dragController) {
930 mRemoveViewContainer.setDragDropController(dragController);
931 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700932
933 @Override
934 public void onPickPhoneNumberAction(Uri dataUri) {
935 mPhoneNumberPickerActionListener.onPickPhoneNumberAction(dataUri);
936 }
937
938 @Override
939 public void onCallNumberDirectly(String phoneNumber) {
940 mPhoneNumberPickerActionListener.onCallNumberDirectly(phoneNumber);
941 }
942
943 @Override
944 public void onShortcutIntentCreated(Intent intent) {
945 mPhoneNumberPickerActionListener.onShortcutIntentCreated(intent);
946 }
947
948 @Override
949 public void onHomeInActionBarSelected() {
950 mPhoneNumberPickerActionListener.onHomeInActionBarSelected();
951 }
Yorke Lee33932ff2014-04-16 13:34:32 -0700952
953 public int getActionBarHeight() {
954 return mActionBarHeight;
955 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700956}