blob: 5dabb930d3eb7d761de451bb0af6eb8188b57550 [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;
Yorke Leec3766332013-07-31 11:13:16 -070068import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070069import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070070import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070072import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070073import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080074import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070075import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080076import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070077import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070078import com.android.dialer.list.SpeedDialFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080079import com.android.dialer.list.PhoneFavoriteTileView;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070080import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070081import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080082import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070083import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070084import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070085import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070086import com.android.internal.telephony.ITelephony;
87
Yorke Leec3766332013-07-31 11:13:16 -070088import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080089import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070090
Chiao Cheng94b10b52012-08-17 16:59:12 -070091/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070092 * The dialer tab's title is 'phone', a more common name (see strings.xml).
93 */
Yorke Leec3766332013-07-31 11:13:16 -070094public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070095 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -070096 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080097 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -070098 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -070099 SpeedDialFragment.HostInterface,
Yorke Leee00c9fe2014-04-12 12:42:06 -0700100 OnDragDropListener, View.OnLongClickListener,
101 OnPhoneNumberPickerActionListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700102 private static final String TAG = "DialtactsActivity";
103
Ihab Awad526c0b82014-02-27 12:55:36 -0800104 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700105
Yorke Leef74011e2013-08-02 11:30:30 -0700106 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
107
Chiao Cheng94b10b52012-08-17 16:59:12 -0700108 /** Used to open Call Setting */
109 private static final String PHONE_PACKAGE = "com.android.phone";
110 private static final String CALL_SETTINGS_CLASS_NAME =
111 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700112 /** @see #getCallOrigin() */
113 private static final String CALL_ORIGIN_DIALTACTS =
114 "com.android.dialer.DialtactsActivity";
115
Yorke Leeb207f8a2013-08-29 18:51:06 -0700116 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
117 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700118 private static final String KEY_SEARCH_QUERY = "search_query";
119 private static final String KEY_FIRST_LAUNCH = "first_launch";
120
Yorke Leec3766332013-07-31 11:13:16 -0700121 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
122 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
123 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
124 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700125
Chiao Cheng94b10b52012-08-17 16:59:12 -0700126 /**
127 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
128 */
129 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
130
Yorke Leec3766332013-07-31 11:13:16 -0700131 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700132
Yorke Lee86e21f72014-04-02 16:49:38 -0700133 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800134
Yorke Leec3766332013-07-31 11:13:16 -0700135 /**
Yorke Leec3766332013-07-31 11:13:16 -0700136 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700137 */
Yorke Leef74011e2013-08-02 11:30:30 -0700138 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700139
140 /**
141 * Fragment for searching phone numbers using the alphanumeric keyboard.
142 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700143 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700144
145 /**
146 * Fragment for searching phone numbers using the dialpad.
147 */
148 private SmartDialSearchFragment mSmartDialSearchFragment;
149
Yorke Leee00c9fe2014-04-12 12:42:06 -0700150 /**
151 * Fragment containing the speed dial list, recents list, and all contacts list.
152 */
153 private ListsFragment mListsFragment;
154
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700155 private View mFloatingActionButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700156 private View mMenuButton;
Yorke Leec3766332013-07-31 11:13:16 -0700157 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800158 private View mDialButton;
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);
Yorke Leed999b712014-04-23 15:10:58 -0700209 // TODO: Check mSpeedDialFragment.hasFrequents()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700210 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
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700331 mFloatingActionButton = findViewById(R.id.floating_action_button);
332 setupFloatingActionButton(mFloatingActionButton);
Yorke Lee86e21f72014-04-02 16:49:38 -0700333
Yorke Lee86e21f72014-04-02 16:49:38 -0700334 mDialButton = findViewById(R.id.dial_button);
335 mDialButton.setOnClickListener(this);
336 mDialpadButton = findViewById(R.id.dialpad_button);
337 mDialpadButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800338
Yorke Lee3cefcc62014-01-16 11:26:46 -0800339 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800340 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
341
Yorke Lee0baa98b2013-08-22 10:55:16 -0700342 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700343 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700344 }
345
346 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700347 protected void onResume() {
348 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700349 if (mFirstLaunch) {
350 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700351 } else if (!phoneIsInUse() && mInCallDialpadUp) {
352 hideDialpadFragment(false, true);
353 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700354 }
355 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700356 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700357 }
358
359 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700360 protected void onPause() {
361 if (mClearSearchOnPause) {
362 hideDialpadAndSearchUi();
363 mClearSearchOnPause = false;
364 }
365 super.onPause();
366 }
367
368 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700369 protected void onSaveInstanceState(Bundle outState) {
370 super.onSaveInstanceState(outState);
371 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700372 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
373 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700374 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
375 }
376
377 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700378 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700379 if (fragment instanceof DialpadFragment) {
380 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700381 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700382 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700383 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700384 } else if (fragment instanceof SmartDialSearchFragment) {
385 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700386 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
387 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700388 if (mFragmentsFrame != null) {
389 mFragmentsFrame.setAlpha(1.0f);
390 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700391 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700392 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700393 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
394 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700395 if (mFragmentsFrame != null) {
396 mFragmentsFrame.setAlpha(1.0f);
397 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700398 } else if (fragment instanceof ListsFragment) {
399 mListsFragment = (ListsFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700400 }
Yorke Leec3766332013-07-31 11:13:16 -0700401 }
402
Alon Albertb453e5b2013-09-10 15:54:23 -0700403 protected void handleMenuSettings() {
404 openTelephonySetting(this);
405 }
406
407 public static void openTelephonySetting(Activity activity) {
408 final Intent settingsIntent = getCallSettingsIntent();
409 activity.startActivity(settingsIntent);
410 }
411
Chiao Cheng94b10b52012-08-17 16:59:12 -0700412 @Override
413 public void onClick(View view) {
414 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700415 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700416 // Reset the boolean flag that tracks whether the dialpad was up because
417 // we were in call. Regardless of whether it was true before, we want to
418 // show the dialpad because the user has explicitly clicked the dialpad
419 // button.
420 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700421 showDialpadFragment(true);
422 break;
Yorke Lee86e21f72014-04-02 16:49:38 -0700423 case R.id.dial_button:
424 // Dial button was pressed; tell the Dialpad fragment
425 mDialpadFragment.dialButtonPressed();
426 break;
Yorke Leec3766332013-07-31 11:13:16 -0700427 case R.id.search_close_button:
428 // Clear the search field
Yorke Lee86e21f72014-04-02 16:49:38 -0700429 if (!TextUtils.isEmpty(mSearchView.getQuery())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700430 mDialpadFragment.clearDialpad();
Yorke Lee86e21f72014-04-02 16:49:38 -0700431 mSearchView.setQuery("", false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700432 }
433 break;
Yorke Leec3766332013-07-31 11:13:16 -0700434 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800435 try {
436 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
437 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
438 } catch (ActivityNotFoundException e) {
439 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
440 Toast.LENGTH_SHORT).show();
441 }
Yorke Leec3766332013-07-31 11:13:16 -0700442 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700443 default: {
444 Log.wtf(TAG, "Unexpected onClick event from " + view);
445 break;
446 }
447 }
448 }
449
Yorke Leec3766332013-07-31 11:13:16 -0700450 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700451 public boolean onOptionsItemSelected(MenuItem item) {
452 switch (item.getItemId()) {
453 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700454 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700455 break;
456 case R.id.menu_add_contact:
457 try {
458 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
459 } catch (ActivityNotFoundException e) {
460 Toast toast = Toast.makeText(this,
461 R.string.add_contact_not_available,
462 Toast.LENGTH_SHORT);
463 toast.show();
464 }
465 break;
466 case R.id.menu_import_export:
467 // We hard-code the "contactsAreAvailable" argument because doing it properly would
468 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
469 // now in Dialtacts for (potential) performance reasons. Compare with how it is
470 // done in {@link PeopleActivity}.
471 ImportExportDialogFragment.show(getFragmentManager(), true,
472 DialtactsActivity.class);
473 return true;
474 case R.id.menu_clear_frequents:
475 // TODO: This should be enabled/disabled based on
Yorke Leed999b712014-04-23 15:10:58 -0700476 // SpeedDialFragment.hasFrequents
Yorke Lee86e21f72014-04-02 16:49:38 -0700477 ClearFrequentsDialog.show(getFragmentManager());
478 return true;
479 case R.id.menu_call_settings:
480 handleMenuSettings();
481 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700482 }
483 return false;
484 }
485
486 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800487 public boolean onLongClick(View view) {
488 switch (view.getId()) {
489 case R.id.dial_button: {
490 // Dial button was pressed; tell the Dialpad fragment
491 mDialpadFragment.dialButtonPressed();
492 return true; // Consume the event
493 }
494 default: {
495 Log.wtf(TAG, "Unexpected onClick event from " + view);
496 break;
497 }
498 }
499 return false;
500 }
501
502 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700503 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
504 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
505 if (resultCode == RESULT_OK) {
506 final ArrayList<String> matches = data.getStringArrayListExtra(
507 RecognizerIntent.EXTRA_RESULTS);
508 if (matches.size() > 0) {
509 final String match = matches.get(0);
Yorke Lee86e21f72014-04-02 16:49:38 -0700510 mSearchView.setQuery(match, false);
Yorke Leec3766332013-07-31 11:13:16 -0700511 } else {
512 Log.e(TAG, "Voice search - nothing heard");
513 }
514 } else {
515 Log.e(TAG, "Voice search failed");
516 }
517 }
518 super.onActivityResult(requestCode, resultCode, data);
519 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700520
Yorke Leec3766332013-07-31 11:13:16 -0700521 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700522 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700523 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700524 if (animate) {
525 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700526 } else {
527 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700528 }
529 ft.show(mDialpadFragment);
530 ft.commit();
531 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700532
Yorke Leeca195042013-09-25 16:29:38 -0700533 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700534 if (mDialpadFragment == null) return;
535 if (clearDialpad) {
536 mDialpadFragment.clearDialpad();
537 }
538 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700539 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700540 final FragmentTransaction ft = getFragmentManager().beginTransaction();
541 if (animate) {
542 ft.setCustomAnimations(0, R.anim.slide_out);
543 }
544 ft.hide(mDialpadFragment);
545 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700546 }
547
Yorke Leec3766332013-07-31 11:13:16 -0700548 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
549 @Override
550 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800551 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700552 }
553 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700554
Yorke Leeb207f8a2013-08-29 18:51:06 -0700555 private boolean getInSearchUi() {
556 return mInDialpadSearch || mInRegularSearch;
557 }
558
559 private void setNotInSearchUi() {
560 mInDialpadSearch = false;
561 mInRegularSearch = false;
562 }
563
Yorke Lee311969c2013-08-26 06:31:11 -0700564 private void hideDialpadAndSearchUi() {
Yorke Lee86e21f72014-04-02 16:49:38 -0700565 mSearchView.setQuery("", false);
Yorke Lee311969c2013-08-26 06:31:11 -0700566 hideDialpadFragment(false, true);
567 }
568
Yorke Lee86e21f72014-04-02 16:49:38 -0700569 /**
570 * Callback from child DialpadFragment when the dialpad is shown.
571 */
572 public void onDialpadShown() {
573 mDialButton.setVisibility(View.VISIBLE);
574 mDialpadButton.setVisibility(View.GONE);
Yorke Leeb0d19762014-03-05 14:43:28 -0800575
Yorke Lee86e21f72014-04-02 16:49:38 -0700576 SearchFragment fragment = null;
577 if (mInDialpadSearch) {
578 fragment = mSmartDialSearchFragment;
579 } else if (mInRegularSearch) {
580 fragment = mRegularSearchFragment;
581 }
582 if (fragment != null && fragment.isVisible()) {
Yorke Lee33932ff2014-04-16 13:34:32 -0700583 fragment.getListView().animate().translationY(-mActionBarHeight)
Yorke Lee86e21f72014-04-02 16:49:38 -0700584 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
585 }
Christine Chen9c1e0652013-05-23 15:40:19 -0700586
Yorke Lee33932ff2014-04-16 13:34:32 -0700587 if (mListsFragment != null && mListsFragment.isResumed() && mListsFragment.isVisible()) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700588 // If the favorites fragment is showing, fade to blank.
589 mFragmentsFrame.animate().alpha(0.0f);
590 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700591 getActionBar().hide();
Yorke Leec3766332013-07-31 11:13:16 -0700592 }
593
Yorke Lee86e21f72014-04-02 16:49:38 -0700594 /**
595 * Callback from child DialpadFragment when the dialpad is hidden.
596 */
597 public void onDialpadHidden() {
598 mDialButton.setVisibility(View.GONE);
599 mDialpadButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700600
Yorke Lee86e21f72014-04-02 16:49:38 -0700601 SearchFragment fragment = null;
602 if (mInDialpadSearch) {
603 fragment = mSmartDialSearchFragment;
604 } else if (mInRegularSearch) {
605 fragment = mRegularSearchFragment;
606 }
607 if (fragment != null && fragment.isVisible()) {
608 fragment.getListView().animate().translationY(0)
609 .setInterpolator(showActionBarInterpolator).setDuration(ANIMATION_DURATION);
Yorke Leec3766332013-07-31 11:13:16 -0700610 }
611
Yorke Leee00c9fe2014-04-12 12:42:06 -0700612 if (mListsFragment != null && mListsFragment.isVisible()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700613 mFragmentsFrame.animate().alpha(1.0f);
614 }
615 getActionBar().show();
Yorke Leec3766332013-07-31 11:13:16 -0700616 }
617
Yorke Leee00c9fe2014-04-12 12:42:06 -0700618 private void hideInputMethod(View view) {
619 final InputMethodManager imm = (InputMethodManager) getSystemService(
620 Context.INPUT_METHOD_SERVICE);
621 if (imm != null && view != null) {
622 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
623 }
624 }
625
Yorke Lee86e21f72014-04-02 16:49:38 -0700626 @Override
627 public boolean onCreateOptionsMenu(Menu menu) {
628 if (DEBUG) {
629 Log.d(TAG, "onCreateOptionsMenu");
630 }
631 MenuInflater inflater = getMenuInflater();
632 inflater.inflate(R.menu.dialtacts_options, menu);
633 final MenuItem searchItem = menu.findItem(R.id.menu_search);
634 mSearchView = (SearchView) searchItem.getActionView();
635 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
636 mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
637 mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener);
638 mSearchView.setIconifiedByDefault(false);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700639
640 if (mPendingSearchViewQuery != null) {
641 mSearchView.setQuery(mPendingSearchViewQuery, false);
642 mPendingSearchViewQuery = null;
643 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700644 return super.onCreateOptionsMenu(menu);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700645 }
646
Chiao Cheng94b10b52012-08-17 16:59:12 -0700647 /**
648 * Returns true if the intent is due to hitting the green send key (hardware call button:
649 * KEYCODE_CALL) while in a call.
650 *
651 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700652 * @return true if the intent is due to hitting the green send key while in a call
653 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700654 private boolean isSendKeyWhileInCall(Intent intent) {
655 // If there is a call in progress and the user launched the dialer by hitting the call
656 // button, go straight to the in-call screen.
657 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700658
Yorke Lee62e995c2014-03-28 10:02:56 -0700659 try {
660 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
661 if (callKey && phone != null && phone.showCallScreen()) {
662 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700663 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700664 } catch (RemoteException e) {
665 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700666 }
667
668 return false;
669 }
670
671 /**
672 * Sets the current tab based on the intent's request type
673 *
674 * @param intent Intent that contains information about which tab should be selected
675 */
Yorke Leec3766332013-07-31 11:13:16 -0700676 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700677 // 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 -0700678 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700679 finish();
680 return;
681 }
682
Yorke Lee669b6082013-09-17 15:43:38 -0700683 if (mDialpadFragment != null) {
684 final boolean phoneIsInUse = phoneIsInUse();
685 if (phoneIsInUse || isDialIntent(intent)) {
686 mDialpadFragment.setStartedFromNewIntent(true);
687 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
688 mInCallDialpadUp = true;
689 }
690 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700691 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700692 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700693 }
694
695 @Override
696 public void onNewIntent(Intent newIntent) {
697 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700698 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700699
Chiao Cheng94b10b52012-08-17 16:59:12 -0700700 invalidateOptionsMenu();
701 }
702
703 /** Returns true if the given intent contains a phone number to populate the dialer with */
704 private boolean isDialIntent(Intent intent) {
705 final String action = intent.getAction();
706 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
707 return true;
708 }
709 if (Intent.ACTION_VIEW.equals(action)) {
710 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700711 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700712 return true;
713 }
714 }
715 return false;
716 }
717
718 /**
719 * Returns an appropriate call origin for this Activity. May return null when no call origin
720 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
721 * for remembering the tab in which the user made a phone call, so the external app's DIAL
722 * request should not be counted.)
723 */
724 public String getCallOrigin() {
725 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
726 }
727
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728 /**
Yorke Leec3766332013-07-31 11:13:16 -0700729 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700731 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700732 if (getFragmentManager().isDestroyed()) {
733 // Weird race condition where fragment is doing work after the activity is destroyed
734 // due to talkback being on (b/10209937). Just return since we can't do any
735 // constructive here.
736 return;
737 }
738
Yorke Leeef2b7382013-08-09 17:39:25 -0700739 if (DEBUG) {
740 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
741 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700742
Yorke Leec3766332013-07-31 11:13:16 -0700743 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744
Yorke Leeef2b7382013-08-09 17:39:25 -0700745 SearchFragment fragment;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700746 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700747 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700748 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700749 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700750 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700751
Yorke Leeb207f8a2013-08-29 18:51:06 -0700752 final String tag;
753 if (smartDialSearch) {
754 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
755 } else {
756 tag = TAG_REGULAR_SEARCH_FRAGMENT;
757 }
758 mInDialpadSearch = smartDialSearch;
759 mInRegularSearch = !smartDialSearch;
760
Yorke Leeef2b7382013-08-09 17:39:25 -0700761 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
762 if (fragment == null) {
763 if (smartDialSearch) {
764 fragment = new SmartDialSearchFragment();
765 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700766 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700767 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700768 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700769 // DialtactsActivity will provide the options menu
770 fragment.setHasOptionsMenu(false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700771 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700772 transaction.addToBackStack(null);
773 fragment.setQueryString(query, false);
774 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700775 }
776
777 /**
Yorke Leec3766332013-07-31 11:13:16 -0700778 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700779 */
Yorke Leec3766332013-07-31 11:13:16 -0700780 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700781 // See related bug in enterSearchUI();
782 if (getFragmentManager().isDestroyed()) {
783 return;
784 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700785 // Go all the way back to the favorites fragment, regardless of how many times we
786 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700787 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
788 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700789
790 if (isDialpadShowing()) {
791 mFragmentsFrame.setAlpha(0);
792 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700793 }
794
795 /** Returns an Intent to launch Call Settings screen */
796 public static Intent getCallSettingsIntent() {
797 final Intent intent = new Intent(Intent.ACTION_MAIN);
798 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
799 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
800 return intent;
801 }
802
803 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700804 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700805 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700806 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700807 } else if (getInSearchUi()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700808 mSearchView.setQuery(null, false);
Yorke Leef6673d32013-08-23 15:34:17 -0700809 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700810 } else {
811 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700812 }
Yorke Leec3766332013-07-31 11:13:16 -0700813 }
814
815 @Override
816 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700817 if (mSmartDialSearchFragment != null) {
818 mSmartDialSearchFragment.setAddToContactNumber(query);
819 }
Yorke Leec3766332013-07-31 11:13:16 -0700820 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
821 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700822 if (mSearchView == null) {
823 if (!TextUtils.isEmpty(normalizedQuery)) {
824 mPendingSearchViewQuery = normalizedQuery;
825 }
826 return;
827 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700828 if (!TextUtils.equals(mSearchView.getQuery(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700829 if (DEBUG) {
830 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
831 }
832 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
833 // This callback can happen if the dialpad fragment is recreated because of
834 // activity destruction. In that case, don't update the search view because
835 // that would bring the user back to the search fragment regardless of the
836 // previous state of the application. Instead, just return here and let the
837 // fragment manager correctly figure out whatever fragment was last displayed.
838 return;
839 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700840 mSearchView.setQuery(normalizedQuery, false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700841 }
842 }
Yorke Leec3766332013-07-31 11:13:16 -0700843
844 @Override
845 public void onListFragmentScrollStateChange(int scrollState) {
846 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700847 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700848 hideInputMethod(getCurrentFocus());
849 }
850 }
851
852 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700853 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
854 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700855 // TODO: No-op for now. This should eventually show/hide the actionBar based on
856 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700857 }
858
Yorke Lee86e21f72014-04-02 16:49:38 -0700859 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800860 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700861 if (mDialButton != null) {
862 mDialButton.setEnabled(enabled);
863 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800864 }
865
866 @Override
867 public void setDialButtonContainerVisible(boolean visible) {
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700868 mFloatingActionButton.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700869 }
870
871 private boolean phoneIsInUse() {
872 final TelephonyManager tm = (TelephonyManager) getSystemService(
873 Context.TELEPHONY_SERVICE);
874 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
875 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700876
Yorke Leee1424812013-09-04 18:24:48 -0700877 public static Intent getAddNumberToContactIntent(CharSequence text) {
878 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
879 intent.putExtra(Intents.Insert.PHONE, text);
880 intent.setType(Contacts.CONTENT_ITEM_TYPE);
881 return intent;
882 }
883
Yorke Leeda0f9042013-12-05 14:25:51 -0800884 private boolean canIntentBeHandled(Intent intent) {
885 final PackageManager packageManager = getPackageManager();
886 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
887 PackageManager.MATCH_DEFAULT_ONLY);
888 return resolveInfo != null && resolveInfo.size() > 0;
889 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800890
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700891 private void setupFloatingActionButton(View view) {
892 // Once layout is complete and the floating action button has been assigned a width
893 // and height, assign the outline.
894 view.addOnLayoutChangeListener(new OnLayoutChangeListener() {
895 @Override
896 public void onLayoutChange(View v,
897 int left,
898 int top,
899 int right,
900 int bottom,
901 int oldLeft,
902 int oldTop,
903 int oldRight,
904 int oldBottom) {
905 final Outline outline = new Outline();
906 final int minDimension = Math.min(right - left, bottom - top);
907 if (minDimension <= 0) {
908 return;
909 }
910 outline.setRoundRect(0, 0, right - left, bottom - top, minDimension / 2);
911 v.setOutline(outline);
912 v.setClipToOutline(true);
913 }
914 });
915 view.setTranslationZ(getResources().getDimensionPixelSize(
916 R.dimen.floating_action_button_translation_z));
917 }
918
Yorke Lee6a1461a2014-04-21 16:27:14 -0700919 @Override
920 public void showCallHistory() {
921 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
922 // CONTENT_TYPE, so that we always open our call log from our dialer
923 final Intent intent = new Intent(this, CallLogActivity.class);
924 startActivity(intent);
925 }
926
Yorke Lee86e21f72014-04-02 16:49:38 -0700927 /**
928 * Called when the user has long-pressed a contact tile to start a drag operation.
929 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800930 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700931 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700932 getActionBar().hide();
933 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800934 }
935
936 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700937 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {}
Yorke Lee3cefcc62014-01-16 11:26:46 -0800938
Yorke Lee86e21f72014-04-02 16:49:38 -0700939 /**
940 * Called when the user has released a contact tile after long-pressing it.
941 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800942 @Override
943 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700944 getActionBar().show();
945 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800946 }
947
948 @Override
949 public void onDroppedOnRemove() {}
950
951 /**
Yorke Leed999b712014-04-23 15:10:58 -0700952 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800953 * once it has been attached to the activity.
954 */
955 @Override
956 public void setDragDropController(DragDropController dragController) {
957 mRemoveViewContainer.setDragDropController(dragController);
958 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700959
960 @Override
961 public void onPickPhoneNumberAction(Uri dataUri) {
962 mPhoneNumberPickerActionListener.onPickPhoneNumberAction(dataUri);
963 }
964
965 @Override
966 public void onCallNumberDirectly(String phoneNumber) {
967 mPhoneNumberPickerActionListener.onCallNumberDirectly(phoneNumber);
968 }
969
970 @Override
971 public void onShortcutIntentCreated(Intent intent) {
972 mPhoneNumberPickerActionListener.onShortcutIntentCreated(intent);
973 }
974
975 @Override
976 public void onHomeInActionBarSelected() {
977 mPhoneNumberPickerActionListener.onHomeInActionBarSelected();
978 }
Yorke Lee33932ff2014-04-16 13:34:32 -0700979
980 public int getActionBarHeight() {
981 return mActionBarHeight;
982 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700983}