blob: cc45a988ee48ddbc8c214ace9391484483ed2f91 [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;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080026import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070027import android.content.Context;
28import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
Chiao Cheng94b10b52012-08-17 16:59:12 -070031import android.net.Uri;
32import android.os.Bundle;
33import android.os.RemoteException;
34import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.provider.CallLog.Calls;
36import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070037import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070038import android.speech.RecognizerIntent;
Yorke Leec3766332013-07-31 11:13:16 -070039import android.telephony.TelephonyManager;
40import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.text.TextUtils;
Yorke Leec3766332013-07-31 11:13:16 -070042import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.util.Log;
44import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.View;
Yorke Leeb0d19762014-03-05 14:43:28 -080047import android.view.ViewGroup.LayoutParams;
48import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070050import android.widget.AbsListView.OnScrollListener;
51import android.widget.EditText;
Yorke Leeb0d19762014-03-05 14:43:28 -080052import android.widget.LinearLayout;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053import android.widget.PopupMenu;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080054import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070055
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070056import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070057import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070058import com.android.contacts.common.dialog.ClearFrequentsDialog;
59import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080060import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Yorke Leec3766332013-07-31 11:13:16 -070061import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070062import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070063import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070064import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070065import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070066import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee8dd62002013-08-08 15:57:20 -070067import com.android.dialer.list.AllContactsActivity;
Yorke Lee3cefcc62014-01-16 11:26:46 -080068import com.android.dialer.list.DragDropController;
69import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.dialer.list.OnListFragmentScrolledListener;
Alon Albertb453e5b2013-09-10 15:54:23 -070071import com.android.dialer.list.PhoneFavoriteFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080072import com.android.dialer.list.PhoneFavoriteTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070073import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080074import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070075import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070076import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070077import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070078import com.android.internal.telephony.ITelephony;
79
Yorke Leec3766332013-07-31 11:13:16 -070080import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080081import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070082
Chiao Cheng94b10b52012-08-17 16:59:12 -070083/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070084 * The dialer tab's title is 'phone', a more common name (see strings.xml).
85 */
Yorke Leec3766332013-07-31 11:13:16 -070086public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Leef74011e2013-08-02 11:30:30 -070087 DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
Yorke Leec3766332013-07-31 11:13:16 -070088 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080089 DialpadFragment.HostInterface,
Yorke Lee3cefcc62014-01-16 11:26:46 -080090 PhoneFavoriteFragment.OnShowAllContactsListener,
91 PhoneFavoriteFragment.HostInterface,
Ihab Awad526c0b82014-02-27 12:55:36 -080092 OnDragDropListener, View.OnLongClickListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070093 private static final String TAG = "DialtactsActivity";
94
Ihab Awad526c0b82014-02-27 12:55:36 -080095 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -070096
Yorke Leef74011e2013-08-02 11:30:30 -070097 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
98
Chiao Cheng94b10b52012-08-17 16:59:12 -070099 /** Used to open Call Setting */
100 private static final String PHONE_PACKAGE = "com.android.phone";
101 private static final String CALL_SETTINGS_CLASS_NAME =
102 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103 /** @see #getCallOrigin() */
104 private static final String CALL_ORIGIN_DIALTACTS =
105 "com.android.dialer.DialtactsActivity";
106
Yorke Leeb207f8a2013-08-29 18:51:06 -0700107 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
108 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700109 private static final String KEY_SEARCH_QUERY = "search_query";
110 private static final String KEY_FIRST_LAUNCH = "first_launch";
111
Yorke Leec3766332013-07-31 11:13:16 -0700112 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
113 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
114 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
115 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700116
Chiao Cheng94b10b52012-08-17 16:59:12 -0700117 /**
118 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
119 */
120 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
121
Yorke Leec3766332013-07-31 11:13:16 -0700122 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700123
Yorke Lee7eccf452014-03-14 12:52:42 -0700124 private static final int ANIMATION_DURATION = 200;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800125
Chiao Cheng94b10b52012-08-17 16:59:12 -0700126 private String mFilterText;
127
Yorke Leec3766332013-07-31 11:13:16 -0700128 /**
129 * The main fragment displaying the user's favorites and frequent contacts
130 */
Yorke Lee8898cd02013-08-08 10:24:27 -0700131 private PhoneFavoriteFragment mPhoneFavoriteFragment;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700132
133 /**
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 Leec3766332013-07-31 11:13:16 -0700148 private View mMenuButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800149 private View mFakeActionBar;
Yorke Leec3766332013-07-31 11:13:16 -0700150 private View mCallHistoryButton;
151 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800152 private View mDialButton;
Yorke Lee36c54132013-09-25 16:17:06 -0700153 private PopupMenu mOverflowMenu;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700154 private PopupMenu mDialpadOverflowMenu;
Yorke Leec3766332013-07-31 11:13:16 -0700155
Yorke Leeb0d19762014-03-05 14:43:28 -0800156 // Padding view used to shift the fragment frame up when the dialpad is shown so that
157 // the contents of the fragment frame continue to exist in a layout of the same height
158 private View mFragmentsSpacer;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700159 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700160
Yorke Leeb207f8a2013-08-29 18:51:06 -0700161 private boolean mInDialpadSearch;
162 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700163 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700164
Yorke Leeef2b7382013-08-09 17:39:25 -0700165 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700166 * True if the dialpad is only temporarily showing due to being in call
167 */
168 private boolean mInCallDialpadUp;
169
170 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700171 * True when this activity has been launched for the first time.
172 */
Yorke Lee98702de2013-08-06 12:03:32 -0700173 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700174 private View mSearchViewContainer;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800175 private RemoveView mRemoveViewContainer;
Yorke Lee94f49042014-03-03 14:03:08 -0800176 // This view points to the Framelayout that houses both the search view and remove view
177 // containers.
178 private View mSearchAndRemoveViewContainer;
Yorke Leec3766332013-07-31 11:13:16 -0700179 private View mSearchViewCloseButton;
180 private View mVoiceSearchButton;
181 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700182
Yorke Leeef2b7382013-08-09 17:39:25 -0700183 private String mSearchQuery;
184
Yorke Leefce269a2013-08-12 11:56:04 -0700185 private DialerDatabaseHelper mDialerDatabaseHelper;
186
Yorke Lee36c54132013-09-25 16:17:06 -0700187 private class OverflowPopupMenu extends PopupMenu {
188 public OverflowPopupMenu(Context context, View anchor) {
189 super(context, anchor);
190 }
191
192 @Override
193 public void show() {
194 final Menu menu = getMenu();
195 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
196 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
197 super.show();
198 }
199 }
200
Chiao Cheng94b10b52012-08-17 16:59:12 -0700201 /**
202 * Listener used when one of phone numbers in search UI is selected. This will initiate a
203 * phone call using the phone number.
204 */
205 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
206 new OnPhoneNumberPickerActionListener() {
207 @Override
208 public void onPickPhoneNumberAction(Uri dataUri) {
209 // Specify call-origin so that users will see the previous tab instead of
210 // CallLog screen (search UI will be automatically exited).
211 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700212 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700213 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700214 }
215
216 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700217 public void onCallNumberDirectly(String phoneNumber) {
218 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
219 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700220 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700221 }
222
223 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700224 public void onShortcutIntentCreated(Intent intent) {
225 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
226 }
227
228 @Override
229 public void onHomeInActionBarSelected() {
230 exitSearchUi();
231 }
232 };
233
234 /**
235 * Listener used to send search queries to the phone search fragment.
236 */
Yorke Leec3766332013-07-31 11:13:16 -0700237 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
238 @Override
239 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
240 }
241
242 @Override
243 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700244 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700245 if (newText.equals(mSearchQuery)) {
246 // If the query hasn't changed (perhaps due to activity being destroyed
247 // and restored, or user launching the same DIAL intent twice), then there is
248 // no need to do anything here.
249 return;
250 }
251 mSearchQuery = newText;
252 if (DEBUG) {
253 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
254 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700255 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700256
Yorke Leec3766332013-07-31 11:13:16 -0700257 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700258 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700259 exitSearchUi();
260 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700261 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700262 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700263 } else if (!TextUtils.isEmpty(newText)) {
264 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
265 (!dialpadSearch && mInRegularSearch);
266 if (!sameSearchMode) {
267 // call enterSearchUi only if we are switching search modes, or entering
268 // search ui for the first time
269 enterSearchUi(dialpadSearch, newText);
270 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700271
Yorke Leeb207f8a2013-08-29 18:51:06 -0700272 if (dialpadSearch && mSmartDialSearchFragment != null) {
273 mSmartDialSearchFragment.setQueryString(newText, false);
274 } else if (mRegularSearchFragment != null) {
275 mRegularSearchFragment.setQueryString(newText, false);
276 }
277 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700278 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700279 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700280 }
Yorke Leec3766332013-07-31 11:13:16 -0700281 }
282
283 @Override
284 public void afterTextChanged(Editable s) {
285 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700286 };
287
Yorke Leec3766332013-07-31 11:13:16 -0700288 private boolean isDialpadShowing() {
289 return mDialpadFragment != null && mDialpadFragment.isVisible();
290 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291
292 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700293 protected void onCreate(Bundle savedInstanceState) {
294 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700295 mFirstLaunch = true;
296
Yorke Lee8898cd02013-08-08 10:24:27 -0700297 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800298 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700299
Yorke Leec3766332013-07-31 11:13:16 -0700300 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700301
Yorke Leeef2b7382013-08-09 17:39:25 -0700302 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
303 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700304 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800305 getFragmentManager().beginTransaction()
306 .add(R.id.dialtacts_frame, new PhoneFavoriteFragment(), TAG_FAVORITES_FRAGMENT)
307 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
308 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700309 } else {
310 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700311 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
312 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700313 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700314 }
315
Yorke Leeb207f8a2013-08-29 18:51:06 -0700316 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leeb0d19762014-03-05 14:43:28 -0800317 mFragmentsSpacer = findViewById(R.id.contact_tile_frame_spacer);
Ihab Awad526c0b82014-02-27 12:55:36 -0800318
Yorke Lee3cefcc62014-01-16 11:26:46 -0800319 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800320 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
321
322 // When the first global layout pass is completed (and mSearchAndRemoveViewContainer has
323 // been assigned a valid height), assign that height to mFragmentsSpacer as well.
324 mSearchAndRemoveViewContainer.getViewTreeObserver().addOnGlobalLayoutListener(
325 new OnGlobalLayoutListener() {
326 @Override
327 public void onGlobalLayout() {
328 mSearchAndRemoveViewContainer.getViewTreeObserver()
329 .removeOnGlobalLayoutListener(this);
330 mFragmentsSpacer.setLayoutParams(
331 new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
332 mSearchAndRemoveViewContainer.getHeight()));
333 }
334 });
335
Yorke Lee8d540002014-03-26 16:13:10 -0700336 setupFakeActionBarItems();
Yorke Leec3766332013-07-31 11:13:16 -0700337 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700338
Yorke Lee0baa98b2013-08-22 10:55:16 -0700339 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700340 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700341 }
342
343 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700344 protected void onResume() {
345 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700346 if (mFirstLaunch) {
347 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700348 } else if (!phoneIsInUse() && mInCallDialpadUp) {
349 hideDialpadFragment(false, true);
350 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700351 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800352 prepareVoiceSearchButton();
Yorke Lee98702de2013-08-06 12:03:32 -0700353 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700354 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700355 }
356
357 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700358 protected void onPause() {
359 if (mClearSearchOnPause) {
360 hideDialpadAndSearchUi();
361 mClearSearchOnPause = false;
362 }
363 super.onPause();
364 }
365
366 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700367 protected void onSaveInstanceState(Bundle outState) {
368 super.onSaveInstanceState(outState);
369 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700370 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
371 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700372 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
373 }
374
375 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700376 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700377 if (fragment instanceof DialpadFragment) {
378 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700379 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700380 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700381 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700382 } else if (fragment instanceof SmartDialSearchFragment) {
383 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700384 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
385 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700386 if (mFragmentsFrame != null) {
387 mFragmentsFrame.setAlpha(1.0f);
388 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700389 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700390 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700391 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
392 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700393 if (mFragmentsFrame != null) {
394 mFragmentsFrame.setAlpha(1.0f);
395 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700396 } else if (fragment instanceof PhoneFavoriteFragment) {
397 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700398 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700399 }
Yorke Leec3766332013-07-31 11:13:16 -0700400 }
401
402 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700403 public boolean onMenuItemClick(MenuItem item) {
404 switch (item.getItemId()) {
405 case R.id.menu_import_export:
406 // We hard-code the "contactsAreAvailable" argument because doing it properly would
407 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
408 // now in Dialtacts for (potential) performance reasons. Compare with how it is
409 // done in {@link PeopleActivity}.
410 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700411 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700412 return true;
413 case R.id.menu_clear_frequents:
414 ClearFrequentsDialog.show(getFragmentManager());
415 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700416 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700417 try {
418 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
419 } catch (ActivityNotFoundException e) {
420 Toast toast = Toast.makeText(this,
421 R.string.add_contact_not_available,
422 Toast.LENGTH_SHORT);
423 toast.show();
424 }
425 return true;
426 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700427 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700428 return true;
429 case R.id.menu_all_contacts:
430 onShowAllContacts();
431 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700432 }
433 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700434 }
435
Alon Albertb453e5b2013-09-10 15:54:23 -0700436 protected void handleMenuSettings() {
437 openTelephonySetting(this);
438 }
439
440 public static void openTelephonySetting(Activity activity) {
441 final Intent settingsIntent = getCallSettingsIntent();
442 activity.startActivity(settingsIntent);
443 }
444
Chiao Cheng94b10b52012-08-17 16:59:12 -0700445 @Override
446 public void onClick(View view) {
447 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700448 case R.id.overflow_menu: {
Ihab Awad526c0b82014-02-27 12:55:36 -0800449 if (isDialpadShowing()) {
Ihab Awadfb00cb82014-03-18 16:19:00 -0700450 mDialpadOverflowMenu.show();
Ihab Awad526c0b82014-02-27 12:55:36 -0800451 } else {
452 mOverflowMenu.show();
453 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700454 break;
455 }
Yorke Leec3766332013-07-31 11:13:16 -0700456 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700457 // Reset the boolean flag that tracks whether the dialpad was up because
458 // we were in call. Regardless of whether it was true before, we want to
459 // show the dialpad because the user has explicitly clicked the dialpad
460 // button.
461 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700462 showDialpadFragment(true);
463 break;
Ihab Awad526c0b82014-02-27 12:55:36 -0800464 case R.id.dial_button:
465 // Dial button was pressed; tell the Dialpad fragment
466 mDialpadFragment.dialButtonPressed();
467 break;
Yorke Leec3766332013-07-31 11:13:16 -0700468 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700469 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
470 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700471 final Intent intent = new Intent(this, CallLogActivity.class);
472 startActivity(intent);
473 break;
474 case R.id.search_close_button:
475 // Clear the search field
476 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700477 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700478 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700479 }
480 break;
Yorke Leec3766332013-07-31 11:13:16 -0700481 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800482 try {
483 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
484 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
485 } catch (ActivityNotFoundException e) {
486 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
487 Toast.LENGTH_SHORT).show();
488 }
Yorke Leec3766332013-07-31 11:13:16 -0700489 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700490 default: {
491 Log.wtf(TAG, "Unexpected onClick event from " + view);
492 break;
493 }
494 }
495 }
496
Yorke Leec3766332013-07-31 11:13:16 -0700497 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800498 public boolean onLongClick(View view) {
499 switch (view.getId()) {
500 case R.id.dial_button: {
501 // Dial button was pressed; tell the Dialpad fragment
502 mDialpadFragment.dialButtonPressed();
503 return true; // Consume the event
504 }
505 default: {
506 Log.wtf(TAG, "Unexpected onClick event from " + view);
507 break;
508 }
509 }
510 return false;
511 }
512
513 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700514 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
515 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
516 if (resultCode == RESULT_OK) {
517 final ArrayList<String> matches = data.getStringArrayListExtra(
518 RecognizerIntent.EXTRA_RESULTS);
519 if (matches.size() > 0) {
520 final String match = matches.get(0);
521 mSearchView.setText(match);
522 } else {
523 Log.e(TAG, "Voice search - nothing heard");
524 }
525 } else {
526 Log.e(TAG, "Voice search failed");
527 }
528 }
529 super.onActivityResult(requestCode, resultCode, data);
530 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700531
Yorke Leec3766332013-07-31 11:13:16 -0700532 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700533 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700534 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700535 if (animate) {
536 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700537 } else {
538 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700539 }
540 ft.show(mDialpadFragment);
541 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800542 mDialButton.setVisibility(shouldShowOnscreenDialButton() ? View.VISIBLE : View.GONE);
543 mDialpadButton.setVisibility(View.GONE);
Yorke Lee8d540002014-03-26 16:13:10 -0700544
545 if (mDialpadOverflowMenu == null) {
546 mDialpadOverflowMenu = mDialpadFragment.buildOptionsMenu(mMenuButton);
547 }
548
Ihab Awadfb00cb82014-03-18 16:19:00 -0700549 mMenuButton.setOnTouchListener(mDialpadOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700550 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700551
Yorke Leeca195042013-09-25 16:29:38 -0700552 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700553 if (mDialpadFragment == null) return;
554 if (clearDialpad) {
555 mDialpadFragment.clearDialpad();
556 }
557 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700558 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700559 final FragmentTransaction ft = getFragmentManager().beginTransaction();
560 if (animate) {
561 ft.setCustomAnimations(0, R.anim.slide_out);
562 }
563 ft.hide(mDialpadFragment);
564 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800565 mDialButton.setVisibility(View.GONE);
566 mDialpadButton.setVisibility(View.VISIBLE);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700567 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700568 }
569
Chiao Cheng94b10b52012-08-17 16:59:12 -0700570 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700571 mSearchViewContainer = findViewById(R.id.search_view_container);
572 mSearchViewCloseButton = findViewById(R.id.search_close_button);
573 mSearchViewCloseButton.setOnClickListener(this);
Yorke Leeda0f9042013-12-05 14:25:51 -0800574
Yorke Leec3766332013-07-31 11:13:16 -0700575 mSearchView = (EditText) findViewById(R.id.search_view);
576 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Tyler Gunna8ff9752014-02-24 11:57:57 -0800577 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
Yorke Leeda0f9042013-12-05 14:25:51 -0800578
579 prepareVoiceSearchButton();
580 }
581
582 private void prepareVoiceSearchButton() {
583 mVoiceSearchButton = findViewById(R.id.voice_search_button);
584 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
585 if (canIntentBeHandled(voiceIntent)) {
586 mVoiceSearchButton.setVisibility(View.VISIBLE);
587 mVoiceSearchButton.setOnClickListener(this);
588 } else {
589 mVoiceSearchButton.setVisibility(View.GONE);
590 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700591 }
592
Yorke Leec3766332013-07-31 11:13:16 -0700593 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
594 @Override
595 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800596 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700597 }
598 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700599
Yorke Leeb207f8a2013-08-29 18:51:06 -0700600 private boolean getInSearchUi() {
601 return mInDialpadSearch || mInRegularSearch;
602 }
603
604 private void setNotInSearchUi() {
605 mInDialpadSearch = false;
606 mInRegularSearch = false;
607 }
608
Yorke Lee311969c2013-08-26 06:31:11 -0700609 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700610 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700611 hideDialpadFragment(false, true);
612 }
613
Yorke Leec3766332013-07-31 11:13:16 -0700614 public void hideSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800615 final int height = mSearchAndRemoveViewContainer.getHeight();
Yorke Leeb0d19762014-03-05 14:43:28 -0800616
Yorke Lee94f49042014-03-03 14:03:08 -0800617 mSearchAndRemoveViewContainer.animate().cancel();
618 mSearchAndRemoveViewContainer.setAlpha(1);
619 mSearchAndRemoveViewContainer.setTranslationY(0);
620 mSearchAndRemoveViewContainer.animate().withLayer().alpha(0)
Yorke Lee7eccf452014-03-14 12:52:42 -0700621 .translationY(-height).setDuration(ANIMATION_DURATION)
Yorke Lee94f49042014-03-03 14:03:08 -0800622 .setListener(mHideListener);
Christine Chen9c1e0652013-05-23 15:40:19 -0700623
Yorke Lee94f49042014-03-03 14:03:08 -0800624 mFragmentsFrame.animate().withLayer()
Yorke Lee7eccf452014-03-14 12:52:42 -0700625 .translationY(-height).setDuration(ANIMATION_DURATION).setListener(
Yorke Lee94f49042014-03-03 14:03:08 -0800626 new AnimatorListenerAdapter() {
627 @Override
628 public void onAnimationEnd(Animator animation) {
629 mFragmentsFrame.setTranslationY(0);
Yorke Leeb0d19762014-03-05 14:43:28 -0800630 // Display the fragments spacer (which has the same height as the
631 // search box) now that the search box is hidden, so that
632 // mFragmentsFrame always retains the same height
633 mFragmentsSpacer.setVisibility(View.VISIBLE);
Yorke Lee94f49042014-03-03 14:03:08 -0800634 }
635 });
Yorke Lee7eccf452014-03-14 12:52:42 -0700636
637 if (!mInDialpadSearch && !mInRegularSearch) {
638 // If the favorites fragment is showing, fade to blank.
639 mFragmentsFrame.animate().alpha(0.0f);
640 }
Yorke Leec3766332013-07-31 11:13:16 -0700641 }
642
643 public void showSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800644 final int height = mSearchAndRemoveViewContainer.getHeight();
645 mSearchAndRemoveViewContainer.animate().cancel();
646 mSearchAndRemoveViewContainer.setAlpha(0);
647 mSearchAndRemoveViewContainer.setTranslationY(-height);
648 mSearchAndRemoveViewContainer.animate().withLayer().alpha(1).translationY(0)
Yorke Lee7eccf452014-03-14 12:52:42 -0700649 .setDuration(ANIMATION_DURATION).setListener(new AnimatorListenerAdapter() {
Yorke Lee94f49042014-03-03 14:03:08 -0800650 @Override
651 public void onAnimationStart(Animator animation) {
652 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
653 }
654 });
Yorke Leec3766332013-07-31 11:13:16 -0700655
Yorke Lee94f49042014-03-03 14:03:08 -0800656 mFragmentsFrame.setTranslationY(-height);
Yorke Lee7eccf452014-03-14 12:52:42 -0700657 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(ANIMATION_DURATION)
658 .alpha(1.0f)
Yorke Leec3766332013-07-31 11:13:16 -0700659 .setListener(
660 new AnimatorListenerAdapter() {
661 @Override
662 public void onAnimationStart(Animator animation) {
Yorke Leeb0d19762014-03-05 14:43:28 -0800663 // Hide the fragment spacer now that the search box will
664 // be displayed again
665 mFragmentsSpacer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700666 }
667 });
668 }
669
Ihab Awadfb00cb82014-03-18 16:19:00 -0700670 private void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700671 mMenuButton = findViewById(R.id.overflow_menu);
672 if (mMenuButton != null) {
673 mMenuButton.setOnClickListener(this);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700674 if (mOverflowMenu == null) {
675 mOverflowMenu = buildOptionsMenu(mMenuButton);
676 }
Yorke Lee36c54132013-09-25 16:17:06 -0700677 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700678 }
679
Ihab Awad526c0b82014-02-27 12:55:36 -0800680 mFakeActionBar = findViewById(R.id.fake_action_bar);
681
Yorke Leec3766332013-07-31 11:13:16 -0700682 mCallHistoryButton = findViewById(R.id.call_history_button);
Yorke Leec3766332013-07-31 11:13:16 -0700683 mCallHistoryButton.setOnClickListener(this);
684
Ihab Awad526c0b82014-02-27 12:55:36 -0800685 mDialButton = findViewById(R.id.dial_button);
686 mDialButton.setOnClickListener(this);
687 mDialButton.setOnLongClickListener(this);
688
Yorke Leec3766332013-07-31 11:13:16 -0700689 mDialpadButton = findViewById(R.id.dialpad_button);
Yorke Leec3766332013-07-31 11:13:16 -0700690 mDialpadButton.setOnClickListener(this);
691 }
692
Ihab Awadfb00cb82014-03-18 16:19:00 -0700693 private PopupMenu buildOptionsMenu(View invoker) {
694 PopupMenu menu = new OverflowPopupMenu(this, invoker);
695 menu.inflate(R.menu.dialtacts_options);
696 menu.setOnMenuItemClickListener(this);
697 return menu;
698 }
699
Chiao Cheng94b10b52012-08-17 16:59:12 -0700700 /**
701 * Returns true if the intent is due to hitting the green send key (hardware call button:
702 * KEYCODE_CALL) while in a call.
703 *
704 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700705 * @return true if the intent is due to hitting the green send key while in a call
706 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700707 private boolean isSendKeyWhileInCall(Intent intent) {
708 // If there is a call in progress and the user launched the dialer by hitting the call
709 // button, go straight to the in-call screen.
710 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700711
Yorke Lee62e995c2014-03-28 10:02:56 -0700712 try {
713 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
714 if (callKey && phone != null && phone.showCallScreen()) {
715 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700716 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700717 } catch (RemoteException e) {
718 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700719 }
720
721 return false;
722 }
723
724 /**
725 * Sets the current tab based on the intent's request type
726 *
727 * @param intent Intent that contains information about which tab should be selected
728 */
Yorke Leec3766332013-07-31 11:13:16 -0700729 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730 // 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 -0700731 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700732 finish();
733 return;
734 }
735
Yorke Lee669b6082013-09-17 15:43:38 -0700736 if (mDialpadFragment != null) {
737 final boolean phoneIsInUse = phoneIsInUse();
738 if (phoneIsInUse || isDialIntent(intent)) {
739 mDialpadFragment.setStartedFromNewIntent(true);
740 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
741 mInCallDialpadUp = true;
742 }
743 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700744 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700745 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700746 }
747
748 @Override
749 public void onNewIntent(Intent newIntent) {
750 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700751 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700752 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700753
Chiao Cheng94b10b52012-08-17 16:59:12 -0700754 invalidateOptionsMenu();
755 }
756
757 /** Returns true if the given intent contains a phone number to populate the dialer with */
758 private boolean isDialIntent(Intent intent) {
759 final String action = intent.getAction();
760 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
761 return true;
762 }
763 if (Intent.ACTION_VIEW.equals(action)) {
764 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700765 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700766 return true;
767 }
768 }
769 return false;
770 }
771
772 /**
773 * Returns an appropriate call origin for this Activity. May return null when no call origin
774 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
775 * for remembering the tab in which the user made a phone call, so the external app's DIAL
776 * request should not be counted.)
777 */
778 public String getCallOrigin() {
779 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
780 }
781
Yorke Lee8898cd02013-08-08 10:24:27 -0700782 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
783 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700784 @Override
785 public void onContactSelected(Uri contactUri) {
786 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700787 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700788 }
789
790 @Override
791 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700792 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700793 startActivity(intent);
794 }
795 };
796
Chiao Cheng94b10b52012-08-17 16:59:12 -0700797 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700798 final InputMethodManager imm = (InputMethodManager) getSystemService(
799 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700800 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700801 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700802 }
803 }
804
805 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700806 final InputMethodManager imm = (InputMethodManager) getSystemService(
807 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700808 if (imm != null && view != null) {
809 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
810 }
811 }
812
813 /**
Yorke Leec3766332013-07-31 11:13:16 -0700814 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700815 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700816 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700817 if (getFragmentManager().isDestroyed()) {
818 // Weird race condition where fragment is doing work after the activity is destroyed
819 // due to talkback being on (b/10209937). Just return since we can't do any
820 // constructive here.
821 return;
822 }
823
Yorke Leeef2b7382013-08-09 17:39:25 -0700824 if (DEBUG) {
825 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
826 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700827
Yorke Leec3766332013-07-31 11:13:16 -0700828 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700829
Yorke Leeef2b7382013-08-09 17:39:25 -0700830 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700831 if (mInDialpadSearch) {
832 transaction.remove(mSmartDialSearchFragment);
833 } else if (mInRegularSearch) {
834 transaction.remove(mRegularSearchFragment);
835 } else {
836 transaction.remove(mPhoneFavoriteFragment);
837 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700838
Yorke Leeb207f8a2013-08-29 18:51:06 -0700839 final String tag;
840 if (smartDialSearch) {
841 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
842 } else {
843 tag = TAG_REGULAR_SEARCH_FRAGMENT;
844 }
845 mInDialpadSearch = smartDialSearch;
846 mInRegularSearch = !smartDialSearch;
847
Yorke Leeef2b7382013-08-09 17:39:25 -0700848 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
849 if (fragment == null) {
850 if (smartDialSearch) {
851 fragment = new SmartDialSearchFragment();
852 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700853 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700854 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700855 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700856 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700857 transaction.addToBackStack(null);
858 fragment.setQueryString(query, false);
859 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700860 }
861
862 /**
Yorke Leec3766332013-07-31 11:13:16 -0700863 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700864 */
Yorke Leec3766332013-07-31 11:13:16 -0700865 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700866 // See related bug in enterSearchUI();
867 if (getFragmentManager().isDestroyed()) {
868 return;
869 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700870 // Go all the way back to the favorites fragment, regardless of how many times we
871 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700872 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
873 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700874
875 if (isDialpadShowing()) {
876 mFragmentsFrame.setAlpha(0);
877 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700878 }
879
880 /** Returns an Intent to launch Call Settings screen */
881 public static Intent getCallSettingsIntent() {
882 final Intent intent = new Intent(Intent.ACTION_MAIN);
883 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
884 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
885 return intent;
886 }
887
888 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700889 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700890 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700891 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700892 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700893 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700894 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700895 } else {
896 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700897 }
Yorke Leec3766332013-07-31 11:13:16 -0700898 }
899
900 @Override
901 public void onDialpadQueryChanged(String query) {
902 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
903 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
904 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700905 if (DEBUG) {
906 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
907 }
908 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
909 // This callback can happen if the dialpad fragment is recreated because of
910 // activity destruction. In that case, don't update the search view because
911 // that would bring the user back to the search fragment regardless of the
912 // previous state of the application. Instead, just return here and let the
913 // fragment manager correctly figure out whatever fragment was last displayed.
914 return;
915 }
Yorke Leec3766332013-07-31 11:13:16 -0700916 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700917 }
918 }
Yorke Leec3766332013-07-31 11:13:16 -0700919
920 @Override
921 public void onListFragmentScrollStateChange(int scrollState) {
922 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700923 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700924 hideInputMethod(getCurrentFocus());
925 }
926 }
927
928 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800929 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700930 if (mDialButton != null) {
931 mDialButton.setEnabled(enabled);
932 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800933 }
934
935 @Override
936 public void setDialButtonContainerVisible(boolean visible) {
937 mFakeActionBar.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700938 }
939
940 private boolean phoneIsInUse() {
941 final TelephonyManager tm = (TelephonyManager) getSystemService(
942 Context.TELEPHONY_SERVICE);
943 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
944 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700945
946 @Override
947 public void onShowAllContacts() {
948 final Intent intent = new Intent(this, AllContactsActivity.class);
949 startActivity(intent);
950 }
Yorke Leee1424812013-09-04 18:24:48 -0700951
952 public static Intent getAddNumberToContactIntent(CharSequence text) {
953 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
954 intent.putExtra(Intents.Insert.PHONE, text);
955 intent.setType(Contacts.CONTENT_ITEM_TYPE);
956 return intent;
957 }
958
Yorke Leeda0f9042013-12-05 14:25:51 -0800959 private boolean canIntentBeHandled(Intent intent) {
960 final PackageManager packageManager = getPackageManager();
961 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
962 PackageManager.MATCH_DEFAULT_ONLY);
963 return resolveInfo != null && resolveInfo.size() > 0;
964 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800965
966 @Override
967 public void onDragStarted(int itemIndex, int x, int y, PhoneFavoriteTileView view) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700968 crossfadeViews(mRemoveViewContainer, mSearchViewContainer, ANIMATION_DURATION);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800969 }
970
971 @Override
972 public void onDragHovered(int itemIndex, int x, int y) {}
973
974 @Override
975 public void onDragFinished(int x, int y) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700976 crossfadeViews(mSearchViewContainer, mRemoveViewContainer, ANIMATION_DURATION);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800977 }
978
979 @Override
980 public void onDroppedOnRemove() {}
981
982 /**
983 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
984 * once it has been attached to the activity.
985 */
986 @Override
987 public void setDragDropController(DragDropController dragController) {
988 mRemoveViewContainer.setDragDropController(dragController);
989 }
990
991 /**
992 * Crossfades two views so that the first one appears while the other one is fading
993 * out of view.
994 */
995 private void crossfadeViews(final View fadeIn, final View fadeOut, int duration) {
996 fadeOut.animate().alpha(0).setDuration(duration)
997 .setListener(new AnimatorListenerAdapter() {
998 @Override
999 public void onAnimationEnd(Animator animation) {
1000 fadeOut.setVisibility(View.GONE);
1001 }
1002 });
1003
1004 fadeIn.setVisibility(View.VISIBLE);
1005 fadeIn.setAlpha(0);
Yorke Lee7eccf452014-03-14 12:52:42 -07001006 fadeIn.animate().alpha(1).setDuration(ANIMATION_DURATION)
Yorke Lee3cefcc62014-01-16 11:26:46 -08001007 .setListener(null);
1008 }
Ihab Awad526c0b82014-02-27 12:55:36 -08001009
1010 private boolean shouldShowOnscreenDialButton() {
1011 return getResources().getBoolean(R.bool.config_show_onscreen_dial_button);
1012 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001013}