blob: 2d5df2b11c972808048b16f586247d8983ef4653 [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 Lee86e21f72014-04-02 16:49:38 -070048import android.view.View.OnLayoutChangeListener;
Yorke Leeb0d19762014-03-05 14:43:28 -080049import android.view.ViewGroup.LayoutParams;
50import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Yorke Lee86e21f72014-04-02 16:49:38 -070051import android.view.animation.AccelerateInterpolator;
52import android.view.animation.DecelerateInterpolator;
53import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070054import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070055import android.widget.AbsListView.OnScrollListener;
Yorke Leeb0d19762014-03-05 14:43:28 -080056import android.widget.LinearLayout;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.widget.PopupMenu;
Yorke Lee86e21f72014-04-02 16:49:38 -070058import android.widget.SearchView;
59import android.widget.SearchView.OnQueryTextListener;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080060import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070061
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070062import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070063import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070064import com.android.contacts.common.dialog.ClearFrequentsDialog;
65import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080066import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070067import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070068import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070069import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070071import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070072import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080073import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070074import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080075import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070076import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070077import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080078import com.android.dialer.list.PhoneFavoriteTileView;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070079import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070080import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080081import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070082import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070083import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070084import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070085import com.android.internal.telephony.ITelephony;
86
Yorke Leec3766332013-07-31 11:13:16 -070087import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080088import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070089
Chiao Cheng94b10b52012-08-17 16:59:12 -070090/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070091 * The dialer tab's title is 'phone', a more common name (see strings.xml).
92 */
Yorke Leec3766332013-07-31 11:13:16 -070093public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070094 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -070095 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080096 DialpadFragment.HostInterface,
Yorke Lee3cefcc62014-01-16 11:26:46 -080097 PhoneFavoriteFragment.HostInterface,
Yorke Leee00c9fe2014-04-12 12:42:06 -070098 OnDragDropListener, View.OnLongClickListener,
99 OnPhoneNumberPickerActionListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700100 private static final String TAG = "DialtactsActivity";
101
Ihab Awad526c0b82014-02-27 12:55:36 -0800102 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103
Yorke Leef74011e2013-08-02 11:30:30 -0700104 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
105
Chiao Cheng94b10b52012-08-17 16:59:12 -0700106 /** Used to open Call Setting */
107 private static final String PHONE_PACKAGE = "com.android.phone";
108 private static final String CALL_SETTINGS_CLASS_NAME =
109 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700110 /** @see #getCallOrigin() */
111 private static final String CALL_ORIGIN_DIALTACTS =
112 "com.android.dialer.DialtactsActivity";
113
Yorke Leeb207f8a2013-08-29 18:51:06 -0700114 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
115 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700116 private static final String KEY_SEARCH_QUERY = "search_query";
117 private static final String KEY_FIRST_LAUNCH = "first_launch";
118
Yorke Leec3766332013-07-31 11:13:16 -0700119 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
120 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
121 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
122 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700123
Chiao Cheng94b10b52012-08-17 16:59:12 -0700124 /**
125 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
126 */
127 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
128
Yorke Leec3766332013-07-31 11:13:16 -0700129 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700130
Yorke Lee86e21f72014-04-02 16:49:38 -0700131 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800132
Yorke Leec3766332013-07-31 11:13:16 -0700133 /**
Yorke Leec3766332013-07-31 11:13:16 -0700134 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700135 */
Yorke Leef74011e2013-08-02 11:30:30 -0700136 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700137
138 /**
139 * Fragment for searching phone numbers using the alphanumeric keyboard.
140 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700141 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700142
143 /**
144 * Fragment for searching phone numbers using the dialpad.
145 */
146 private SmartDialSearchFragment mSmartDialSearchFragment;
147
Yorke Leee00c9fe2014-04-12 12:42:06 -0700148 /**
149 * Fragment containing the speed dial list, recents list, and all contacts list.
150 */
151 private ListsFragment mListsFragment;
152
Ihab Awad526c0b82014-02-27 12:55:36 -0800153 private View mFakeActionBar;
Yorke Lee86e21f72014-04-02 16:49:38 -0700154 private View mMenuButton;
Yorke Leec3766332013-07-31 11:13:16 -0700155 private View mCallHistoryButton;
156 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800157 private View mDialButton;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700158 private PopupMenu mDialpadOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700159
Yorke Leeb207f8a2013-08-29 18:51:06 -0700160 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700161
Yorke Lee33932ff2014-04-16 13:34:32 -0700162 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700163 private boolean mInDialpadSearch;
164 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700165 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700166
Yorke Leeef2b7382013-08-09 17:39:25 -0700167 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700168 * True if the dialpad is only temporarily showing due to being in call
169 */
170 private boolean mInCallDialpadUp;
171
172 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700173 * True when this activity has been launched for the first time.
174 */
Yorke Lee98702de2013-08-06 12:03:32 -0700175 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700176
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700177 /**
178 * Search query to be applied to the SearchView in the ActionBar once
179 * onCreateOptionsMenu has been called.
180 */
181 private String mPendingSearchViewQuery;
182
Yorke Lee94f49042014-03-03 14:03:08 -0800183 // This view points to the Framelayout that houses both the search view and remove view
184 // containers.
185 private View mSearchAndRemoveViewContainer;
Yorke Lee86e21f72014-04-02 16:49:38 -0700186 private SearchView mSearchView;
187 /**
188 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
189 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
190 * removed from the speed dial list.
191 */
192 private RemoveView mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700193
Yorke Lee86e21f72014-04-02 16:49:38 -0700194 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
195 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700196 private String mSearchQuery;
197
Yorke Leefce269a2013-08-12 11:56:04 -0700198 private DialerDatabaseHelper mDialerDatabaseHelper;
199
Yorke Leee00c9fe2014-04-12 12:42:06 -0700200 private class OverflowPopupMenu extends PopupMenu {
201 public OverflowPopupMenu(Context context, View anchor) {
202 super(context, anchor);
203 }
204
205 @Override
206 public void show() {
207 final Menu menu = getMenu();
208 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
209 // TODO: Check mPhoneFavoriteFragment.hasFrequents()
210 clearFrequents.setVisible(true);
211 super.show();
212 }
213 }
214
Chiao Cheng94b10b52012-08-17 16:59:12 -0700215 /**
216 * Listener used when one of phone numbers in search UI is selected. This will initiate a
217 * phone call using the phone number.
218 */
219 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
220 new OnPhoneNumberPickerActionListener() {
221 @Override
222 public void onPickPhoneNumberAction(Uri dataUri) {
223 // Specify call-origin so that users will see the previous tab instead of
224 // CallLog screen (search UI will be automatically exited).
225 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700226 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700227 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700228 }
229
230 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700231 public void onCallNumberDirectly(String phoneNumber) {
232 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
233 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700234 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700235 }
236
237 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700238 public void onShortcutIntentCreated(Intent intent) {
239 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
240 }
241
242 @Override
243 public void onHomeInActionBarSelected() {
244 exitSearchUi();
245 }
246 };
247
248 /**
249 * Listener used to send search queries to the phone search fragment.
250 */
Yorke Lee86e21f72014-04-02 16:49:38 -0700251 private final OnQueryTextListener mPhoneSearchQueryTextListener = new OnQueryTextListener() {
Yorke Leec3766332013-07-31 11:13:16 -0700252 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700253 public boolean onQueryTextSubmit(String query) {
254 return false;
Yorke Leec3766332013-07-31 11:13:16 -0700255 }
256
257 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700258 public boolean onQueryTextChange(String newText) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700259 if (newText.equals(mSearchQuery)) {
260 // If the query hasn't changed (perhaps due to activity being destroyed
261 // and restored, or user launching the same DIAL intent twice), then there is
262 // no need to do anything here.
Yorke Lee86e21f72014-04-02 16:49:38 -0700263 return true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700264 }
265 mSearchQuery = newText;
266 if (DEBUG) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700267 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leeef2b7382013-08-09 17:39:25 -0700268 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700269 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700270
Yorke Leec3766332013-07-31 11:13:16 -0700271 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700272 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700273 exitSearchUi();
Yorke Lee86e21f72014-04-02 16:49:38 -0700274 return true;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700275 } else if (!TextUtils.isEmpty(newText)) {
276 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
277 (!dialpadSearch && mInRegularSearch);
278 if (!sameSearchMode) {
279 // call enterSearchUi only if we are switching search modes, or entering
280 // search ui for the first time
281 enterSearchUi(dialpadSearch, newText);
282 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700283
Yorke Leeb207f8a2013-08-29 18:51:06 -0700284 if (dialpadSearch && mSmartDialSearchFragment != null) {
285 mSmartDialSearchFragment.setQueryString(newText, false);
286 } else if (mRegularSearchFragment != null) {
287 mRegularSearchFragment.setQueryString(newText, false);
288 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700289 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700290 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700291 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700292 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700293 };
294
Yorke Leec3766332013-07-31 11:13:16 -0700295 private boolean isDialpadShowing() {
296 return mDialpadFragment != null && mDialpadFragment.isVisible();
297 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700298
299 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700300 protected void onCreate(Bundle savedInstanceState) {
301 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700302 mFirstLaunch = true;
303
Yorke Lee8898cd02013-08-08 10:24:27 -0700304 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800305 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700306
Yorke Lee86e21f72014-04-02 16:49:38 -0700307 getActionBar().setDisplayShowHomeEnabled(false);
308 getActionBar().setDisplayShowTitleEnabled(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700309
Yorke Lee33932ff2014-04-16 13:34:32 -0700310 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
311 new int[] { android.R.attr.actionBarSize });
312 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
313 styledAttributes.recycle();
314
Yorke Leeef2b7382013-08-09 17:39:25 -0700315 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
316 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700317 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800318 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700319 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800320 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
321 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700322 } else {
323 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700324 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
325 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700326 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700327 }
328
Yorke Leeb207f8a2013-08-29 18:51:06 -0700329 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee86e21f72014-04-02 16:49:38 -0700330
331 mFakeActionBar = findViewById(R.id.fake_action_bar);
332
333 mCallHistoryButton = findViewById(R.id.call_history_button);
334 mCallHistoryButton.setOnClickListener(this);
335 mDialButton = findViewById(R.id.dial_button);
336 mDialButton.setOnClickListener(this);
337 mDialpadButton = findViewById(R.id.dialpad_button);
338 mDialpadButton.setOnClickListener(this);
339 mMenuButton = findViewById(R.id.overflow_menu_button);
340 mMenuButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800341
Yorke Lee3cefcc62014-01-16 11:26:46 -0800342 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800343 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
344
Yorke Lee0baa98b2013-08-22 10:55:16 -0700345 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700346 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700347 }
348
349 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700350 protected void onResume() {
351 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700352 if (mFirstLaunch) {
353 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700354 } else if (!phoneIsInUse() && mInCallDialpadUp) {
355 hideDialpadFragment(false, true);
356 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700357 }
358 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700359 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700360 }
361
362 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700363 protected void onPause() {
364 if (mClearSearchOnPause) {
365 hideDialpadAndSearchUi();
366 mClearSearchOnPause = false;
367 }
368 super.onPause();
369 }
370
371 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700372 protected void onSaveInstanceState(Bundle outState) {
373 super.onSaveInstanceState(outState);
374 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700375 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
376 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700377 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
378 }
379
380 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700381 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700382 if (fragment instanceof DialpadFragment) {
383 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700384 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700385 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700386 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700387 } else if (fragment instanceof SmartDialSearchFragment) {
388 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700389 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
390 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700391 if (mFragmentsFrame != null) {
392 mFragmentsFrame.setAlpha(1.0f);
393 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700394 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700395 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700396 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
397 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700398 if (mFragmentsFrame != null) {
399 mFragmentsFrame.setAlpha(1.0f);
400 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700401 } else if (fragment instanceof ListsFragment) {
402 mListsFragment = (ListsFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700403 }
Yorke Leec3766332013-07-31 11:13:16 -0700404 }
405
Alon Albertb453e5b2013-09-10 15:54:23 -0700406 protected void handleMenuSettings() {
407 openTelephonySetting(this);
408 }
409
410 public static void openTelephonySetting(Activity activity) {
411 final Intent settingsIntent = getCallSettingsIntent();
412 activity.startActivity(settingsIntent);
413 }
414
Chiao Cheng94b10b52012-08-17 16:59:12 -0700415 @Override
416 public void onClick(View view) {
417 switch (view.getId()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700418 case R.id.overflow_menu_button:
419 mDialpadOverflowMenu.show();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700420 break;
Yorke Leec3766332013-07-31 11:13:16 -0700421 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700422 // Reset the boolean flag that tracks whether the dialpad was up because
423 // we were in call. Regardless of whether it was true before, we want to
424 // show the dialpad because the user has explicitly clicked the dialpad
425 // button.
426 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700427 showDialpadFragment(true);
428 break;
Yorke Leec3766332013-07-31 11:13:16 -0700429 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700430 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
431 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700432 final Intent intent = new Intent(this, CallLogActivity.class);
433 startActivity(intent);
434 break;
Yorke Lee86e21f72014-04-02 16:49:38 -0700435 case R.id.dial_button:
436 // Dial button was pressed; tell the Dialpad fragment
437 mDialpadFragment.dialButtonPressed();
438 break;
Yorke Leec3766332013-07-31 11:13:16 -0700439 case R.id.search_close_button:
440 // Clear the search field
Yorke Lee86e21f72014-04-02 16:49:38 -0700441 if (!TextUtils.isEmpty(mSearchView.getQuery())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700442 mDialpadFragment.clearDialpad();
Yorke Lee86e21f72014-04-02 16:49:38 -0700443 mSearchView.setQuery("", false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700444 }
445 break;
Yorke Leec3766332013-07-31 11:13:16 -0700446 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800447 try {
448 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
449 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
450 } catch (ActivityNotFoundException e) {
451 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
452 Toast.LENGTH_SHORT).show();
453 }
Yorke Leec3766332013-07-31 11:13:16 -0700454 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700455 default: {
456 Log.wtf(TAG, "Unexpected onClick event from " + view);
457 break;
458 }
459 }
460 }
461
Yorke Leec3766332013-07-31 11:13:16 -0700462 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700463 public boolean onOptionsItemSelected(MenuItem item) {
464 switch (item.getItemId()) {
465 case R.id.menu_history:
466 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
467 // CONTENT_TYPE, so that we always open our call log from our dialer
468 final Intent intent = new Intent(this, CallLogActivity.class);
469 startActivity(intent);
470 break;
471 case R.id.menu_add_contact:
472 try {
473 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
474 } catch (ActivityNotFoundException e) {
475 Toast toast = Toast.makeText(this,
476 R.string.add_contact_not_available,
477 Toast.LENGTH_SHORT);
478 toast.show();
479 }
480 break;
481 case R.id.menu_import_export:
482 // We hard-code the "contactsAreAvailable" argument because doing it properly would
483 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
484 // now in Dialtacts for (potential) performance reasons. Compare with how it is
485 // done in {@link PeopleActivity}.
486 ImportExportDialogFragment.show(getFragmentManager(), true,
487 DialtactsActivity.class);
488 return true;
489 case R.id.menu_clear_frequents:
490 // TODO: This should be enabled/disabled based on
491 // PhoneFavoritesFragments.hasFrequents
492 ClearFrequentsDialog.show(getFragmentManager());
493 return true;
494 case R.id.menu_call_settings:
495 handleMenuSettings();
496 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700497 }
498 return false;
499 }
500
501 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800502 public boolean onLongClick(View view) {
503 switch (view.getId()) {
504 case R.id.dial_button: {
505 // Dial button was pressed; tell the Dialpad fragment
506 mDialpadFragment.dialButtonPressed();
507 return true; // Consume the event
508 }
509 default: {
510 Log.wtf(TAG, "Unexpected onClick event from " + view);
511 break;
512 }
513 }
514 return false;
515 }
516
517 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700518 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
519 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
520 if (resultCode == RESULT_OK) {
521 final ArrayList<String> matches = data.getStringArrayListExtra(
522 RecognizerIntent.EXTRA_RESULTS);
523 if (matches.size() > 0) {
524 final String match = matches.get(0);
Yorke Lee86e21f72014-04-02 16:49:38 -0700525 mSearchView.setQuery(match, false);
Yorke Leec3766332013-07-31 11:13:16 -0700526 } else {
527 Log.e(TAG, "Voice search - nothing heard");
528 }
529 } else {
530 Log.e(TAG, "Voice search failed");
531 }
532 }
533 super.onActivityResult(requestCode, resultCode, data);
534 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700535
Yorke Leec3766332013-07-31 11:13:16 -0700536 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700537 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700538 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700539 if (animate) {
540 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700541 } else {
542 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700543 }
544 ft.show(mDialpadFragment);
545 ft.commit();
546 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700547
Yorke Leeca195042013-09-25 16:29:38 -0700548 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700549 if (mDialpadFragment == null) return;
550 if (clearDialpad) {
551 mDialpadFragment.clearDialpad();
552 }
553 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700554 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700555 final FragmentTransaction ft = getFragmentManager().beginTransaction();
556 if (animate) {
557 ft.setCustomAnimations(0, R.anim.slide_out);
558 }
559 ft.hide(mDialpadFragment);
560 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561 }
562
Yorke Leec3766332013-07-31 11:13:16 -0700563 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
564 @Override
565 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800566 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700567 }
568 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700569
Yorke Leeb207f8a2013-08-29 18:51:06 -0700570 private boolean getInSearchUi() {
571 return mInDialpadSearch || mInRegularSearch;
572 }
573
574 private void setNotInSearchUi() {
575 mInDialpadSearch = false;
576 mInRegularSearch = false;
577 }
578
Yorke Lee311969c2013-08-26 06:31:11 -0700579 private void hideDialpadAndSearchUi() {
Yorke Lee86e21f72014-04-02 16:49:38 -0700580 mSearchView.setQuery("", false);
Yorke Lee311969c2013-08-26 06:31:11 -0700581 hideDialpadFragment(false, true);
582 }
583
Yorke Lee86e21f72014-04-02 16:49:38 -0700584 /**
585 * Callback from child DialpadFragment when the dialpad is shown.
586 */
587 public void onDialpadShown() {
588 mDialButton.setVisibility(View.VISIBLE);
589 mDialpadButton.setVisibility(View.GONE);
590 mMenuButton.setVisibility(View.VISIBLE);
591 if (mDialpadOverflowMenu == null) {
592 mDialpadOverflowMenu = mDialpadFragment.buildOptionsMenu(mMenuButton);
593 mMenuButton.setOnTouchListener(mDialpadOverflowMenu.getDragToOpenListener());
594 }
Yorke Leeb0d19762014-03-05 14:43:28 -0800595
Yorke Lee86e21f72014-04-02 16:49:38 -0700596 SearchFragment fragment = null;
597 if (mInDialpadSearch) {
598 fragment = mSmartDialSearchFragment;
599 } else if (mInRegularSearch) {
600 fragment = mRegularSearchFragment;
601 }
602 if (fragment != null && fragment.isVisible()) {
Yorke Lee33932ff2014-04-16 13:34:32 -0700603 fragment.getListView().animate().translationY(-mActionBarHeight)
Yorke Lee86e21f72014-04-02 16:49:38 -0700604 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
605 }
Christine Chen9c1e0652013-05-23 15:40:19 -0700606
Yorke Lee33932ff2014-04-16 13:34:32 -0700607 if (mListsFragment != null && mListsFragment.isResumed() && mListsFragment.isVisible()) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700608 // If the favorites fragment is showing, fade to blank.
609 mFragmentsFrame.animate().alpha(0.0f);
610 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700611 getActionBar().hide();
Yorke Leec3766332013-07-31 11:13:16 -0700612 }
613
Yorke Lee86e21f72014-04-02 16:49:38 -0700614 /**
615 * Callback from child DialpadFragment when the dialpad is hidden.
616 */
617 public void onDialpadHidden() {
618 mDialButton.setVisibility(View.GONE);
619 mDialpadButton.setVisibility(View.VISIBLE);
620 mMenuButton.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700621
Yorke Lee86e21f72014-04-02 16:49:38 -0700622 SearchFragment fragment = null;
623 if (mInDialpadSearch) {
624 fragment = mSmartDialSearchFragment;
625 } else if (mInRegularSearch) {
626 fragment = mRegularSearchFragment;
627 }
628 if (fragment != null && fragment.isVisible()) {
629 fragment.getListView().animate().translationY(0)
630 .setInterpolator(showActionBarInterpolator).setDuration(ANIMATION_DURATION);
Yorke Leec3766332013-07-31 11:13:16 -0700631 }
632
Yorke Leee00c9fe2014-04-12 12:42:06 -0700633 if (mListsFragment != null && mListsFragment.isVisible()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700634 mFragmentsFrame.animate().alpha(1.0f);
635 }
636 getActionBar().show();
Yorke Leec3766332013-07-31 11:13:16 -0700637 }
638
Yorke Leee00c9fe2014-04-12 12:42:06 -0700639 private void hideInputMethod(View view) {
640 final InputMethodManager imm = (InputMethodManager) getSystemService(
641 Context.INPUT_METHOD_SERVICE);
642 if (imm != null && view != null) {
643 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
644 }
645 }
646
Yorke Lee86e21f72014-04-02 16:49:38 -0700647 @Override
648 public boolean onCreateOptionsMenu(Menu menu) {
649 if (DEBUG) {
650 Log.d(TAG, "onCreateOptionsMenu");
651 }
652 MenuInflater inflater = getMenuInflater();
653 inflater.inflate(R.menu.dialtacts_options, menu);
654 final MenuItem searchItem = menu.findItem(R.id.menu_search);
655 mSearchView = (SearchView) searchItem.getActionView();
656 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
657 mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
658 mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener);
659 mSearchView.setIconifiedByDefault(false);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700660
661 if (mPendingSearchViewQuery != null) {
662 mSearchView.setQuery(mPendingSearchViewQuery, false);
663 mPendingSearchViewQuery = null;
664 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700665 return super.onCreateOptionsMenu(menu);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700666 }
667
Chiao Cheng94b10b52012-08-17 16:59:12 -0700668 /**
669 * Returns true if the intent is due to hitting the green send key (hardware call button:
670 * KEYCODE_CALL) while in a call.
671 *
672 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700673 * @return true if the intent is due to hitting the green send key while in a call
674 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700675 private boolean isSendKeyWhileInCall(Intent intent) {
676 // If there is a call in progress and the user launched the dialer by hitting the call
677 // button, go straight to the in-call screen.
678 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700679
Yorke Lee62e995c2014-03-28 10:02:56 -0700680 try {
681 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
682 if (callKey && phone != null && phone.showCallScreen()) {
683 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700684 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700685 } catch (RemoteException e) {
686 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700687 }
688
689 return false;
690 }
691
692 /**
693 * Sets the current tab based on the intent's request type
694 *
695 * @param intent Intent that contains information about which tab should be selected
696 */
Yorke Leec3766332013-07-31 11:13:16 -0700697 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700698 // 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 -0700699 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700700 finish();
701 return;
702 }
703
Yorke Lee669b6082013-09-17 15:43:38 -0700704 if (mDialpadFragment != null) {
705 final boolean phoneIsInUse = phoneIsInUse();
706 if (phoneIsInUse || isDialIntent(intent)) {
707 mDialpadFragment.setStartedFromNewIntent(true);
708 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
709 mInCallDialpadUp = true;
710 }
711 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700712 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700713 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700714 }
715
716 @Override
717 public void onNewIntent(Intent newIntent) {
718 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700719 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700720
Chiao Cheng94b10b52012-08-17 16:59:12 -0700721 invalidateOptionsMenu();
722 }
723
724 /** Returns true if the given intent contains a phone number to populate the dialer with */
725 private boolean isDialIntent(Intent intent) {
726 final String action = intent.getAction();
727 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
728 return true;
729 }
730 if (Intent.ACTION_VIEW.equals(action)) {
731 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700732 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700733 return true;
734 }
735 }
736 return false;
737 }
738
739 /**
740 * Returns an appropriate call origin for this Activity. May return null when no call origin
741 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
742 * for remembering the tab in which the user made a phone call, so the external app's DIAL
743 * request should not be counted.)
744 */
745 public String getCallOrigin() {
746 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
747 }
748
Chiao Cheng94b10b52012-08-17 16:59:12 -0700749 /**
Yorke Leec3766332013-07-31 11:13:16 -0700750 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700751 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700752 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700753 if (getFragmentManager().isDestroyed()) {
754 // Weird race condition where fragment is doing work after the activity is destroyed
755 // due to talkback being on (b/10209937). Just return since we can't do any
756 // constructive here.
757 return;
758 }
759
Yorke Leeef2b7382013-08-09 17:39:25 -0700760 if (DEBUG) {
761 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
762 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700763
Yorke Leec3766332013-07-31 11:13:16 -0700764 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700765
Yorke Leeef2b7382013-08-09 17:39:25 -0700766 SearchFragment fragment;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700767 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700768 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700769 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700770 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700771 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700772
Yorke Leeb207f8a2013-08-29 18:51:06 -0700773 final String tag;
774 if (smartDialSearch) {
775 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
776 } else {
777 tag = TAG_REGULAR_SEARCH_FRAGMENT;
778 }
779 mInDialpadSearch = smartDialSearch;
780 mInRegularSearch = !smartDialSearch;
781
Yorke Leeef2b7382013-08-09 17:39:25 -0700782 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
783 if (fragment == null) {
784 if (smartDialSearch) {
785 fragment = new SmartDialSearchFragment();
786 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700787 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700788 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700789 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700790 // DialtactsActivity will provide the options menu
791 fragment.setHasOptionsMenu(false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700792 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700793 transaction.addToBackStack(null);
794 fragment.setQueryString(query, false);
795 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700796 }
797
798 /**
Yorke Leec3766332013-07-31 11:13:16 -0700799 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700800 */
Yorke Leec3766332013-07-31 11:13:16 -0700801 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700802 // See related bug in enterSearchUI();
803 if (getFragmentManager().isDestroyed()) {
804 return;
805 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700806 // Go all the way back to the favorites fragment, regardless of how many times we
807 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700808 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
809 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700810
811 if (isDialpadShowing()) {
812 mFragmentsFrame.setAlpha(0);
813 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700814 }
815
816 /** Returns an Intent to launch Call Settings screen */
817 public static Intent getCallSettingsIntent() {
818 final Intent intent = new Intent(Intent.ACTION_MAIN);
819 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
820 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
821 return intent;
822 }
823
824 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700825 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700826 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700827 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700828 } else if (getInSearchUi()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700829 mSearchView.setQuery(null, false);
Yorke Leef6673d32013-08-23 15:34:17 -0700830 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700831 } else {
832 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700833 }
Yorke Leec3766332013-07-31 11:13:16 -0700834 }
835
836 @Override
837 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700838 if (mSmartDialSearchFragment != null) {
839 mSmartDialSearchFragment.setAddToContactNumber(query);
840 }
Yorke Leec3766332013-07-31 11:13:16 -0700841 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
842 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700843 if (mSearchView == null) {
844 if (!TextUtils.isEmpty(normalizedQuery)) {
845 mPendingSearchViewQuery = normalizedQuery;
846 }
847 return;
848 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700849 if (!TextUtils.equals(mSearchView.getQuery(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700850 if (DEBUG) {
851 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
852 }
853 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
854 // This callback can happen if the dialpad fragment is recreated because of
855 // activity destruction. In that case, don't update the search view because
856 // that would bring the user back to the search fragment regardless of the
857 // previous state of the application. Instead, just return here and let the
858 // fragment manager correctly figure out whatever fragment was last displayed.
859 return;
860 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700861 if (mDialpadFragment != null) {
862 mDialpadOverflowMenu = mDialpadFragment.buildOptionsMenu(mMenuButton);
863 }
864 mSearchView.setQuery(normalizedQuery, false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700865 }
866 }
Yorke Leec3766332013-07-31 11:13:16 -0700867
868 @Override
869 public void onListFragmentScrollStateChange(int scrollState) {
870 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700871 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700872 hideInputMethod(getCurrentFocus());
873 }
874 }
875
876 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700877 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
878 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700879 // TODO: No-op for now. This should eventually show/hide the actionBar based on
880 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700881 }
882
Yorke Lee86e21f72014-04-02 16:49:38 -0700883 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800884 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700885 if (mDialButton != null) {
886 mDialButton.setEnabled(enabled);
887 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800888 }
889
890 @Override
891 public void setDialButtonContainerVisible(boolean visible) {
892 mFakeActionBar.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700893 }
894
895 private boolean phoneIsInUse() {
896 final TelephonyManager tm = (TelephonyManager) getSystemService(
897 Context.TELEPHONY_SERVICE);
898 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
899 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700900
Yorke Leee1424812013-09-04 18:24:48 -0700901 public static Intent getAddNumberToContactIntent(CharSequence text) {
902 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
903 intent.putExtra(Intents.Insert.PHONE, text);
904 intent.setType(Contacts.CONTENT_ITEM_TYPE);
905 return intent;
906 }
907
Yorke Leeda0f9042013-12-05 14:25:51 -0800908 private boolean canIntentBeHandled(Intent intent) {
909 final PackageManager packageManager = getPackageManager();
910 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
911 PackageManager.MATCH_DEFAULT_ONLY);
912 return resolveInfo != null && resolveInfo.size() > 0;
913 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800914
Yorke Lee86e21f72014-04-02 16:49:38 -0700915 /**
916 * Called when the user has long-pressed a contact tile to start a drag operation.
917 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800918 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700919 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700920 getActionBar().hide();
921 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800922 }
923
924 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700925 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {}
Yorke Lee3cefcc62014-01-16 11:26:46 -0800926
Yorke Lee86e21f72014-04-02 16:49:38 -0700927 /**
928 * Called when the user has released a contact tile after long-pressing it.
929 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800930 @Override
931 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700932 getActionBar().show();
933 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800934 }
935
936 @Override
937 public void onDroppedOnRemove() {}
938
939 /**
940 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
941 * once it has been attached to the activity.
942 */
943 @Override
944 public void setDragDropController(DragDropController dragController) {
945 mRemoveViewContainer.setDragDropController(dragController);
946 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700947
948 @Override
949 public void onPickPhoneNumberAction(Uri dataUri) {
950 mPhoneNumberPickerActionListener.onPickPhoneNumberAction(dataUri);
951 }
952
953 @Override
954 public void onCallNumberDirectly(String phoneNumber) {
955 mPhoneNumberPickerActionListener.onCallNumberDirectly(phoneNumber);
956 }
957
958 @Override
959 public void onShortcutIntentCreated(Intent intent) {
960 mPhoneNumberPickerActionListener.onShortcutIntentCreated(intent);
961 }
962
963 @Override
964 public void onHomeInActionBarSelected() {
965 mPhoneNumberPickerActionListener.onHomeInActionBarSelected();
966 }
Yorke Lee33932ff2014-04-16 13:34:32 -0700967
968 public int getActionBarHeight() {
969 return mActionBarHeight;
970 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700971}