blob: f302f29535a07da2b9a0d8c1fffe1448203fd123 [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
Chiao Cheng94b10b52012-08-17 16:59:12 -0700297 final Intent intent = getIntent();
298 fixIntent(intent);
299
Yorke Lee8898cd02013-08-08 10:24:27 -0700300 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700301
Yorke Leec3766332013-07-31 11:13:16 -0700302 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700303
Yorke Leeef2b7382013-08-09 17:39:25 -0700304 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
305 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700306 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800307 getFragmentManager().beginTransaction()
308 .add(R.id.dialtacts_frame, new PhoneFavoriteFragment(), TAG_FAVORITES_FRAGMENT)
309 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
310 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700311 } else {
312 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700313 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
314 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700315 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700316 }
317
Yorke Leeb207f8a2013-08-29 18:51:06 -0700318 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leeb0d19762014-03-05 14:43:28 -0800319 mFragmentsSpacer = findViewById(R.id.contact_tile_frame_spacer);
Ihab Awad526c0b82014-02-27 12:55:36 -0800320
Yorke Lee3cefcc62014-01-16 11:26:46 -0800321 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800322 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
323
324 // When the first global layout pass is completed (and mSearchAndRemoveViewContainer has
325 // been assigned a valid height), assign that height to mFragmentsSpacer as well.
326 mSearchAndRemoveViewContainer.getViewTreeObserver().addOnGlobalLayoutListener(
327 new OnGlobalLayoutListener() {
328 @Override
329 public void onGlobalLayout() {
330 mSearchAndRemoveViewContainer.getViewTreeObserver()
331 .removeOnGlobalLayoutListener(this);
332 mFragmentsSpacer.setLayoutParams(
333 new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
334 mSearchAndRemoveViewContainer.getHeight()));
335 }
336 });
337
338
Yorke Leec3766332013-07-31 11:13:16 -0700339 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700340
Yorke Lee0baa98b2013-08-22 10:55:16 -0700341 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700342 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700343 }
344
345 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700346 protected void onResume() {
347 super.onResume();
Ihab Awadfb00cb82014-03-18 16:19:00 -0700348 setupFakeActionBarItems();
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 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800355 prepareVoiceSearchButton();
Yorke Lee98702de2013-08-06 12:03:32 -0700356 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700357 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700358 }
359
360 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700361 protected void onPause() {
362 if (mClearSearchOnPause) {
363 hideDialpadAndSearchUi();
364 mClearSearchOnPause = false;
365 }
366 super.onPause();
367 }
368
369 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700370 protected void onSaveInstanceState(Bundle outState) {
371 super.onSaveInstanceState(outState);
372 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700373 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
374 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700375 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
376 }
377
378 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700379 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700380 if (fragment instanceof DialpadFragment) {
381 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700382 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700383 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700384 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700385 } else if (fragment instanceof SmartDialSearchFragment) {
386 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700387 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
388 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700389 if (mFragmentsFrame != null) {
390 mFragmentsFrame.setAlpha(1.0f);
391 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700392 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700393 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700394 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
395 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700396 if (mFragmentsFrame != null) {
397 mFragmentsFrame.setAlpha(1.0f);
398 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700399 } else if (fragment instanceof PhoneFavoriteFragment) {
400 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700401 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700402 }
Yorke Leec3766332013-07-31 11:13:16 -0700403 }
404
405 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700406 public boolean onMenuItemClick(MenuItem item) {
407 switch (item.getItemId()) {
408 case R.id.menu_import_export:
409 // We hard-code the "contactsAreAvailable" argument because doing it properly would
410 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
411 // now in Dialtacts for (potential) performance reasons. Compare with how it is
412 // done in {@link PeopleActivity}.
413 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700414 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700415 return true;
416 case R.id.menu_clear_frequents:
417 ClearFrequentsDialog.show(getFragmentManager());
418 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700419 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700420 try {
421 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
422 } catch (ActivityNotFoundException e) {
423 Toast toast = Toast.makeText(this,
424 R.string.add_contact_not_available,
425 Toast.LENGTH_SHORT);
426 toast.show();
427 }
428 return true;
429 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700430 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700431 return true;
432 case R.id.menu_all_contacts:
433 onShowAllContacts();
434 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700435 }
436 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700437 }
438
Alon Albertb453e5b2013-09-10 15:54:23 -0700439 protected void handleMenuSettings() {
440 openTelephonySetting(this);
441 }
442
443 public static void openTelephonySetting(Activity activity) {
444 final Intent settingsIntent = getCallSettingsIntent();
445 activity.startActivity(settingsIntent);
446 }
447
Chiao Cheng94b10b52012-08-17 16:59:12 -0700448 @Override
449 public void onClick(View view) {
450 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700451 case R.id.overflow_menu: {
Ihab Awad526c0b82014-02-27 12:55:36 -0800452 if (isDialpadShowing()) {
Ihab Awadfb00cb82014-03-18 16:19:00 -0700453 mDialpadOverflowMenu.show();
Ihab Awad526c0b82014-02-27 12:55:36 -0800454 } else {
455 mOverflowMenu.show();
456 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700457 break;
458 }
Yorke Leec3766332013-07-31 11:13:16 -0700459 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700460 // Reset the boolean flag that tracks whether the dialpad was up because
461 // we were in call. Regardless of whether it was true before, we want to
462 // show the dialpad because the user has explicitly clicked the dialpad
463 // button.
464 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700465 showDialpadFragment(true);
466 break;
Ihab Awad526c0b82014-02-27 12:55:36 -0800467 case R.id.dial_button:
468 // Dial button was pressed; tell the Dialpad fragment
469 mDialpadFragment.dialButtonPressed();
470 break;
Yorke Leec3766332013-07-31 11:13:16 -0700471 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700472 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
473 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700474 final Intent intent = new Intent(this, CallLogActivity.class);
475 startActivity(intent);
476 break;
477 case R.id.search_close_button:
478 // Clear the search field
479 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700480 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700481 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700482 }
483 break;
Yorke Leec3766332013-07-31 11:13:16 -0700484 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800485 try {
486 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
487 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
488 } catch (ActivityNotFoundException e) {
489 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
490 Toast.LENGTH_SHORT).show();
491 }
Yorke Leec3766332013-07-31 11:13:16 -0700492 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700493 default: {
494 Log.wtf(TAG, "Unexpected onClick event from " + view);
495 break;
496 }
497 }
498 }
499
Yorke Leec3766332013-07-31 11:13:16 -0700500 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800501 public boolean onLongClick(View view) {
502 switch (view.getId()) {
503 case R.id.dial_button: {
504 // Dial button was pressed; tell the Dialpad fragment
505 mDialpadFragment.dialButtonPressed();
506 return true; // Consume the event
507 }
508 default: {
509 Log.wtf(TAG, "Unexpected onClick event from " + view);
510 break;
511 }
512 }
513 return false;
514 }
515
516 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700517 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
518 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
519 if (resultCode == RESULT_OK) {
520 final ArrayList<String> matches = data.getStringArrayListExtra(
521 RecognizerIntent.EXTRA_RESULTS);
522 if (matches.size() > 0) {
523 final String match = matches.get(0);
524 mSearchView.setText(match);
525 } else {
526 Log.e(TAG, "Voice search - nothing heard");
527 }
528 } else {
529 Log.e(TAG, "Voice search failed");
530 }
531 }
532 super.onActivityResult(requestCode, resultCode, data);
533 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700534
Yorke Leec3766332013-07-31 11:13:16 -0700535 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700536 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700537 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700538 if (animate) {
539 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700540 } else {
541 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700542 }
543 ft.show(mDialpadFragment);
544 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800545 mDialButton.setVisibility(shouldShowOnscreenDialButton() ? View.VISIBLE : View.GONE);
546 mDialpadButton.setVisibility(View.GONE);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700547 mMenuButton.setOnTouchListener(mDialpadOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700548 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700549
Yorke Leeca195042013-09-25 16:29:38 -0700550 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700551 if (mDialpadFragment == null) return;
552 if (clearDialpad) {
553 mDialpadFragment.clearDialpad();
554 }
555 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700556 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700557 final FragmentTransaction ft = getFragmentManager().beginTransaction();
558 if (animate) {
559 ft.setCustomAnimations(0, R.anim.slide_out);
560 }
561 ft.hide(mDialpadFragment);
562 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800563 mDialButton.setVisibility(View.GONE);
564 mDialpadButton.setVisibility(View.VISIBLE);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700565 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700566 }
567
Chiao Cheng94b10b52012-08-17 16:59:12 -0700568 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700569 mSearchViewContainer = findViewById(R.id.search_view_container);
570 mSearchViewCloseButton = findViewById(R.id.search_close_button);
571 mSearchViewCloseButton.setOnClickListener(this);
Yorke Leeda0f9042013-12-05 14:25:51 -0800572
Yorke Leec3766332013-07-31 11:13:16 -0700573 mSearchView = (EditText) findViewById(R.id.search_view);
574 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Tyler Gunna8ff9752014-02-24 11:57:57 -0800575 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
Yorke Leeda0f9042013-12-05 14:25:51 -0800576
577 prepareVoiceSearchButton();
578 }
579
580 private void prepareVoiceSearchButton() {
581 mVoiceSearchButton = findViewById(R.id.voice_search_button);
582 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
583 if (canIntentBeHandled(voiceIntent)) {
584 mVoiceSearchButton.setVisibility(View.VISIBLE);
585 mVoiceSearchButton.setOnClickListener(this);
586 } else {
587 mVoiceSearchButton.setVisibility(View.GONE);
588 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700589 }
590
Yorke Leec3766332013-07-31 11:13:16 -0700591 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
592 @Override
593 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800594 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700595 }
596 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700597
Yorke Leeb207f8a2013-08-29 18:51:06 -0700598 private boolean getInSearchUi() {
599 return mInDialpadSearch || mInRegularSearch;
600 }
601
602 private void setNotInSearchUi() {
603 mInDialpadSearch = false;
604 mInRegularSearch = false;
605 }
606
Yorke Lee311969c2013-08-26 06:31:11 -0700607 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700608 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700609 hideDialpadFragment(false, true);
610 }
611
Yorke Leec3766332013-07-31 11:13:16 -0700612 public void hideSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800613 final int height = mSearchAndRemoveViewContainer.getHeight();
Yorke Leeb0d19762014-03-05 14:43:28 -0800614
Yorke Lee94f49042014-03-03 14:03:08 -0800615 mSearchAndRemoveViewContainer.animate().cancel();
616 mSearchAndRemoveViewContainer.setAlpha(1);
617 mSearchAndRemoveViewContainer.setTranslationY(0);
618 mSearchAndRemoveViewContainer.animate().withLayer().alpha(0)
Yorke Lee7eccf452014-03-14 12:52:42 -0700619 .translationY(-height).setDuration(ANIMATION_DURATION)
Yorke Lee94f49042014-03-03 14:03:08 -0800620 .setListener(mHideListener);
Christine Chen9c1e0652013-05-23 15:40:19 -0700621
Yorke Lee94f49042014-03-03 14:03:08 -0800622 mFragmentsFrame.animate().withLayer()
Yorke Lee7eccf452014-03-14 12:52:42 -0700623 .translationY(-height).setDuration(ANIMATION_DURATION).setListener(
Yorke Lee94f49042014-03-03 14:03:08 -0800624 new AnimatorListenerAdapter() {
625 @Override
626 public void onAnimationEnd(Animator animation) {
627 mFragmentsFrame.setTranslationY(0);
Yorke Leeb0d19762014-03-05 14:43:28 -0800628 // Display the fragments spacer (which has the same height as the
629 // search box) now that the search box is hidden, so that
630 // mFragmentsFrame always retains the same height
631 mFragmentsSpacer.setVisibility(View.VISIBLE);
Yorke Lee94f49042014-03-03 14:03:08 -0800632 }
633 });
Yorke Lee7eccf452014-03-14 12:52:42 -0700634
635 if (!mInDialpadSearch && !mInRegularSearch) {
636 // If the favorites fragment is showing, fade to blank.
637 mFragmentsFrame.animate().alpha(0.0f);
638 }
Yorke Leec3766332013-07-31 11:13:16 -0700639 }
640
641 public void showSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800642 final int height = mSearchAndRemoveViewContainer.getHeight();
643 mSearchAndRemoveViewContainer.animate().cancel();
644 mSearchAndRemoveViewContainer.setAlpha(0);
645 mSearchAndRemoveViewContainer.setTranslationY(-height);
646 mSearchAndRemoveViewContainer.animate().withLayer().alpha(1).translationY(0)
Yorke Lee7eccf452014-03-14 12:52:42 -0700647 .setDuration(ANIMATION_DURATION).setListener(new AnimatorListenerAdapter() {
Yorke Lee94f49042014-03-03 14:03:08 -0800648 @Override
649 public void onAnimationStart(Animator animation) {
650 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
651 }
652 });
Yorke Leec3766332013-07-31 11:13:16 -0700653
Yorke Lee94f49042014-03-03 14:03:08 -0800654 mFragmentsFrame.setTranslationY(-height);
Yorke Lee7eccf452014-03-14 12:52:42 -0700655 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(ANIMATION_DURATION)
656 .alpha(1.0f)
Yorke Leec3766332013-07-31 11:13:16 -0700657 .setListener(
658 new AnimatorListenerAdapter() {
659 @Override
660 public void onAnimationStart(Animator animation) {
Yorke Leeb0d19762014-03-05 14:43:28 -0800661 // Hide the fragment spacer now that the search box will
662 // be displayed again
663 mFragmentsSpacer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700664 }
665 });
666 }
667
Ihab Awadfb00cb82014-03-18 16:19:00 -0700668 private void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700669 mMenuButton = findViewById(R.id.overflow_menu);
670 if (mMenuButton != null) {
671 mMenuButton.setOnClickListener(this);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700672 if (mOverflowMenu == null) {
673 mOverflowMenu = buildOptionsMenu(mMenuButton);
674 }
675 if (mDialpadOverflowMenu == null) {
676 mDialpadOverflowMenu = mDialpadFragment.buildOptionsMenu(mMenuButton);
677 }
678 // Initial state is with dialpad fragment not shown
Yorke Lee36c54132013-09-25 16:17:06 -0700679 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700680 }
681
Ihab Awad526c0b82014-02-27 12:55:36 -0800682 mFakeActionBar = findViewById(R.id.fake_action_bar);
683
Yorke Leec3766332013-07-31 11:13:16 -0700684 mCallHistoryButton = findViewById(R.id.call_history_button);
685 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
686 mCallHistoryButton.setOnClickListener(this);
687
Ihab Awad526c0b82014-02-27 12:55:36 -0800688 mDialButton = findViewById(R.id.dial_button);
689 mDialButton.setOnClickListener(this);
690 mDialButton.setOnLongClickListener(this);
691
Yorke Leec3766332013-07-31 11:13:16 -0700692 mDialpadButton = findViewById(R.id.dialpad_button);
693 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
694 mDialpadButton.setOnClickListener(this);
695 }
696
Ihab Awadfb00cb82014-03-18 16:19:00 -0700697 private PopupMenu buildOptionsMenu(View invoker) {
698 PopupMenu menu = new OverflowPopupMenu(this, invoker);
699 menu.inflate(R.menu.dialtacts_options);
700 menu.setOnMenuItemClickListener(this);
701 return menu;
702 }
703
Chiao Cheng94b10b52012-08-17 16:59:12 -0700704 private void fixIntent(Intent intent) {
705 // This should be cleaned up: the call key used to send an Intent
706 // that just said to go to the recent calls list. It now sends this
707 // abstract action, but this class hasn't been rewritten to deal with it.
708 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
709 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
710 intent.putExtra("call_key", true);
711 setIntent(intent);
712 }
713 }
714
Chiao Cheng94b10b52012-08-17 16:59:12 -0700715 /**
716 * Returns true if the intent is due to hitting the green send key (hardware call button:
717 * KEYCODE_CALL) while in a call.
718 *
719 * @param intent the intent that launched this activity
720 * @param recentCallsRequest true if the intent is requesting to view recent calls
721 * @return true if the intent is due to hitting the green send key while in a call
722 */
Yorke Leec3766332013-07-31 11:13:16 -0700723 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700724 // If there is a call in progress go to the call screen
725 if (recentCallsRequest) {
726 final boolean callKey = intent.getBooleanExtra("call_key", false);
727
728 try {
729 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
730 if (callKey && phone != null && phone.showCallScreen()) {
731 return true;
732 }
733 } catch (RemoteException e) {
734 Log.e(TAG, "Failed to handle send while in call", e);
735 }
736 }
737
738 return false;
739 }
740
741 /**
742 * Sets the current tab based on the intent's request type
743 *
744 * @param intent Intent that contains information about which tab should be selected
745 */
Yorke Leec3766332013-07-31 11:13:16 -0700746 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700747 // If we got here by hitting send and we're in call forward along to the in-call activity
748 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
749 getContentResolver()));
750 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
751 finish();
752 return;
753 }
754
Yorke Lee669b6082013-09-17 15:43:38 -0700755 if (mDialpadFragment != null) {
756 final boolean phoneIsInUse = phoneIsInUse();
757 if (phoneIsInUse || isDialIntent(intent)) {
758 mDialpadFragment.setStartedFromNewIntent(true);
759 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
760 mInCallDialpadUp = true;
761 }
762 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700763 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700765 }
766
767 @Override
768 public void onNewIntent(Intent newIntent) {
769 setIntent(newIntent);
770 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700771 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700772 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700773
Chiao Cheng94b10b52012-08-17 16:59:12 -0700774 invalidateOptionsMenu();
775 }
776
777 /** Returns true if the given intent contains a phone number to populate the dialer with */
778 private boolean isDialIntent(Intent intent) {
779 final String action = intent.getAction();
780 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
781 return true;
782 }
783 if (Intent.ACTION_VIEW.equals(action)) {
784 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700785 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700786 return true;
787 }
788 }
789 return false;
790 }
791
792 /**
793 * Returns an appropriate call origin for this Activity. May return null when no call origin
794 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
795 * for remembering the tab in which the user made a phone call, so the external app's DIAL
796 * request should not be counted.)
797 */
798 public String getCallOrigin() {
799 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
800 }
801
Yorke Lee8898cd02013-08-08 10:24:27 -0700802 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
803 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700804 @Override
805 public void onContactSelected(Uri contactUri) {
806 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700807 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700808 }
809
810 @Override
811 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700812 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700813 startActivity(intent);
814 }
815 };
816
Yorke Leec3766332013-07-31 11:13:16 -0700817 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
818 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
819 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700820
821 @Override
822 public void startSearch(String initialQuery, boolean selectInitialQuery,
823 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700824 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700825 if (mInSearchUi) {
826 if (mSearchView.hasFocus()) {
827 showInputMethod(mSearchView.findFocus());
828 } else {
829 mSearchView.requestFocus();
830 }
831 } else {
832 enterSearchUi();
833 }
834 } else {
835 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
836 }
Yorke Leec3766332013-07-31 11:13:16 -0700837 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700838
839 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700840 final InputMethodManager imm = (InputMethodManager) getSystemService(
841 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700842 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700843 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700844 }
845 }
846
847 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700848 final InputMethodManager imm = (InputMethodManager) getSystemService(
849 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700850 if (imm != null && view != null) {
851 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
852 }
853 }
854
855 /**
Yorke Leec3766332013-07-31 11:13:16 -0700856 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700857 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700858 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700859 if (getFragmentManager().isDestroyed()) {
860 // Weird race condition where fragment is doing work after the activity is destroyed
861 // due to talkback being on (b/10209937). Just return since we can't do any
862 // constructive here.
863 return;
864 }
865
Yorke Leeef2b7382013-08-09 17:39:25 -0700866 if (DEBUG) {
867 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
868 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700869
Yorke Leec3766332013-07-31 11:13:16 -0700870 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700871
Yorke Leeef2b7382013-08-09 17:39:25 -0700872 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700873 if (mInDialpadSearch) {
874 transaction.remove(mSmartDialSearchFragment);
875 } else if (mInRegularSearch) {
876 transaction.remove(mRegularSearchFragment);
877 } else {
878 transaction.remove(mPhoneFavoriteFragment);
879 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700880
Yorke Leeb207f8a2013-08-29 18:51:06 -0700881 final String tag;
882 if (smartDialSearch) {
883 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
884 } else {
885 tag = TAG_REGULAR_SEARCH_FRAGMENT;
886 }
887 mInDialpadSearch = smartDialSearch;
888 mInRegularSearch = !smartDialSearch;
889
Yorke Leeef2b7382013-08-09 17:39:25 -0700890 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
891 if (fragment == null) {
892 if (smartDialSearch) {
893 fragment = new SmartDialSearchFragment();
894 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700895 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700896 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700897 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700898 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700899 transaction.addToBackStack(null);
900 fragment.setQueryString(query, false);
901 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700902 }
903
904 /**
Yorke Leec3766332013-07-31 11:13:16 -0700905 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700906 */
Yorke Leec3766332013-07-31 11:13:16 -0700907 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700908 // See related bug in enterSearchUI();
909 if (getFragmentManager().isDestroyed()) {
910 return;
911 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700912 // Go all the way back to the favorites fragment, regardless of how many times we
913 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700914 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
915 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700916
917 if (isDialpadShowing()) {
918 mFragmentsFrame.setAlpha(0);
919 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700920 }
921
922 /** Returns an Intent to launch Call Settings screen */
923 public static Intent getCallSettingsIntent() {
924 final Intent intent = new Intent(Intent.ACTION_MAIN);
925 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
926 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
927 return intent;
928 }
929
930 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700931 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700932 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700933 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700934 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700935 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700936 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700937 } else {
938 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700939 }
Yorke Leec3766332013-07-31 11:13:16 -0700940 }
941
942 @Override
943 public void onDialpadQueryChanged(String query) {
944 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
945 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
946 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700947 if (DEBUG) {
948 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
949 }
950 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
951 // This callback can happen if the dialpad fragment is recreated because of
952 // activity destruction. In that case, don't update the search view because
953 // that would bring the user back to the search fragment regardless of the
954 // previous state of the application. Instead, just return here and let the
955 // fragment manager correctly figure out whatever fragment was last displayed.
956 return;
957 }
Yorke Leec3766332013-07-31 11:13:16 -0700958 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700959 }
960 }
Yorke Leec3766332013-07-31 11:13:16 -0700961
962 @Override
963 public void onListFragmentScrollStateChange(int scrollState) {
964 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700965 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700966 hideInputMethod(getCurrentFocus());
967 }
968 }
969
970 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800971 public void setDialButtonEnabled(boolean enabled) {
972 mDialButton.setEnabled(enabled);
973 }
974
975 @Override
976 public void setDialButtonContainerVisible(boolean visible) {
977 mFakeActionBar.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700978 }
979
980 private boolean phoneIsInUse() {
981 final TelephonyManager tm = (TelephonyManager) getSystemService(
982 Context.TELEPHONY_SERVICE);
983 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
984 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700985
986 @Override
987 public void onShowAllContacts() {
988 final Intent intent = new Intent(this, AllContactsActivity.class);
989 startActivity(intent);
990 }
Yorke Leee1424812013-09-04 18:24:48 -0700991
992 public static Intent getAddNumberToContactIntent(CharSequence text) {
993 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
994 intent.putExtra(Intents.Insert.PHONE, text);
995 intent.setType(Contacts.CONTENT_ITEM_TYPE);
996 return intent;
997 }
998
Yorke Leeda0f9042013-12-05 14:25:51 -0800999 private boolean canIntentBeHandled(Intent intent) {
1000 final PackageManager packageManager = getPackageManager();
1001 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1002 PackageManager.MATCH_DEFAULT_ONLY);
1003 return resolveInfo != null && resolveInfo.size() > 0;
1004 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001005
1006 @Override
1007 public void onDragStarted(int itemIndex, int x, int y, PhoneFavoriteTileView view) {
Yorke Lee7eccf452014-03-14 12:52:42 -07001008 crossfadeViews(mRemoveViewContainer, mSearchViewContainer, ANIMATION_DURATION);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001009 }
1010
1011 @Override
1012 public void onDragHovered(int itemIndex, int x, int y) {}
1013
1014 @Override
1015 public void onDragFinished(int x, int y) {
Yorke Lee7eccf452014-03-14 12:52:42 -07001016 crossfadeViews(mSearchViewContainer, mRemoveViewContainer, ANIMATION_DURATION);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001017 }
1018
1019 @Override
1020 public void onDroppedOnRemove() {}
1021
1022 /**
1023 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
1024 * once it has been attached to the activity.
1025 */
1026 @Override
1027 public void setDragDropController(DragDropController dragController) {
1028 mRemoveViewContainer.setDragDropController(dragController);
1029 }
1030
1031 /**
1032 * Crossfades two views so that the first one appears while the other one is fading
1033 * out of view.
1034 */
1035 private void crossfadeViews(final View fadeIn, final View fadeOut, int duration) {
1036 fadeOut.animate().alpha(0).setDuration(duration)
1037 .setListener(new AnimatorListenerAdapter() {
1038 @Override
1039 public void onAnimationEnd(Animator animation) {
1040 fadeOut.setVisibility(View.GONE);
1041 }
1042 });
1043
1044 fadeIn.setVisibility(View.VISIBLE);
1045 fadeIn.setAlpha(0);
Yorke Lee7eccf452014-03-14 12:52:42 -07001046 fadeIn.animate().alpha(1).setDuration(ANIMATION_DURATION)
Yorke Lee3cefcc62014-01-16 11:26:46 -08001047 .setListener(null);
1048 }
Ihab Awad526c0b82014-02-27 12:55:36 -08001049
1050 private boolean shouldShowOnscreenDialButton() {
1051 return getResources().getBoolean(R.bool.config_show_onscreen_dial_button);
1052 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001053}