blob: b8faaaf6407236a515d7ea9fbc627fab3d831018 [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);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800301 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700302
Yorke Leec3766332013-07-31 11:13:16 -0700303 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700304
Yorke Leeef2b7382013-08-09 17:39:25 -0700305 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
306 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700307 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800308 getFragmentManager().beginTransaction()
309 .add(R.id.dialtacts_frame, new PhoneFavoriteFragment(), TAG_FAVORITES_FRAGMENT)
310 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
311 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700312 } else {
313 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700314 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
315 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700316 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700317 }
318
Yorke Leeb207f8a2013-08-29 18:51:06 -0700319 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leeb0d19762014-03-05 14:43:28 -0800320 mFragmentsSpacer = findViewById(R.id.contact_tile_frame_spacer);
Ihab Awad526c0b82014-02-27 12:55:36 -0800321
Yorke Lee3cefcc62014-01-16 11:26:46 -0800322 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800323 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
324
325 // When the first global layout pass is completed (and mSearchAndRemoveViewContainer has
326 // been assigned a valid height), assign that height to mFragmentsSpacer as well.
327 mSearchAndRemoveViewContainer.getViewTreeObserver().addOnGlobalLayoutListener(
328 new OnGlobalLayoutListener() {
329 @Override
330 public void onGlobalLayout() {
331 mSearchAndRemoveViewContainer.getViewTreeObserver()
332 .removeOnGlobalLayoutListener(this);
333 mFragmentsSpacer.setLayoutParams(
334 new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
335 mSearchAndRemoveViewContainer.getHeight()));
336 }
337 });
338
Yorke Lee8d540002014-03-26 16:13:10 -0700339 setupFakeActionBarItems();
Yorke Leec3766332013-07-31 11:13:16 -0700340 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700341
Yorke Lee0baa98b2013-08-22 10:55:16 -0700342 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700343 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700344 }
345
346 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700347 protected void onResume() {
348 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700349 if (mFirstLaunch) {
350 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700351 } else if (!phoneIsInUse() && mInCallDialpadUp) {
352 hideDialpadFragment(false, true);
353 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700354 }
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);
Yorke Lee8d540002014-03-26 16:13:10 -0700547
548 if (mDialpadOverflowMenu == null) {
549 mDialpadOverflowMenu = mDialpadFragment.buildOptionsMenu(mMenuButton);
550 }
551
Ihab Awadfb00cb82014-03-18 16:19:00 -0700552 mMenuButton.setOnTouchListener(mDialpadOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700553 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700554
Yorke Leeca195042013-09-25 16:29:38 -0700555 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700556 if (mDialpadFragment == null) return;
557 if (clearDialpad) {
558 mDialpadFragment.clearDialpad();
559 }
560 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700561 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700562 final FragmentTransaction ft = getFragmentManager().beginTransaction();
563 if (animate) {
564 ft.setCustomAnimations(0, R.anim.slide_out);
565 }
566 ft.hide(mDialpadFragment);
567 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800568 mDialButton.setVisibility(View.GONE);
569 mDialpadButton.setVisibility(View.VISIBLE);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700570 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700571 }
572
Chiao Cheng94b10b52012-08-17 16:59:12 -0700573 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700574 mSearchViewContainer = findViewById(R.id.search_view_container);
575 mSearchViewCloseButton = findViewById(R.id.search_close_button);
576 mSearchViewCloseButton.setOnClickListener(this);
Yorke Leeda0f9042013-12-05 14:25:51 -0800577
Yorke Leec3766332013-07-31 11:13:16 -0700578 mSearchView = (EditText) findViewById(R.id.search_view);
579 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Tyler Gunna8ff9752014-02-24 11:57:57 -0800580 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
Yorke Leeda0f9042013-12-05 14:25:51 -0800581
582 prepareVoiceSearchButton();
583 }
584
585 private void prepareVoiceSearchButton() {
586 mVoiceSearchButton = findViewById(R.id.voice_search_button);
587 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
588 if (canIntentBeHandled(voiceIntent)) {
589 mVoiceSearchButton.setVisibility(View.VISIBLE);
590 mVoiceSearchButton.setOnClickListener(this);
591 } else {
592 mVoiceSearchButton.setVisibility(View.GONE);
593 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700594 }
595
Yorke Leec3766332013-07-31 11:13:16 -0700596 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
597 @Override
598 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800599 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700600 }
601 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700602
Yorke Leeb207f8a2013-08-29 18:51:06 -0700603 private boolean getInSearchUi() {
604 return mInDialpadSearch || mInRegularSearch;
605 }
606
607 private void setNotInSearchUi() {
608 mInDialpadSearch = false;
609 mInRegularSearch = false;
610 }
611
Yorke Lee311969c2013-08-26 06:31:11 -0700612 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700613 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700614 hideDialpadFragment(false, true);
615 }
616
Yorke Leec3766332013-07-31 11:13:16 -0700617 public void hideSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800618 final int height = mSearchAndRemoveViewContainer.getHeight();
Yorke Leeb0d19762014-03-05 14:43:28 -0800619
Yorke Lee94f49042014-03-03 14:03:08 -0800620 mSearchAndRemoveViewContainer.animate().cancel();
621 mSearchAndRemoveViewContainer.setAlpha(1);
622 mSearchAndRemoveViewContainer.setTranslationY(0);
623 mSearchAndRemoveViewContainer.animate().withLayer().alpha(0)
Yorke Lee7eccf452014-03-14 12:52:42 -0700624 .translationY(-height).setDuration(ANIMATION_DURATION)
Yorke Lee94f49042014-03-03 14:03:08 -0800625 .setListener(mHideListener);
Christine Chen9c1e0652013-05-23 15:40:19 -0700626
Yorke Lee94f49042014-03-03 14:03:08 -0800627 mFragmentsFrame.animate().withLayer()
Yorke Lee7eccf452014-03-14 12:52:42 -0700628 .translationY(-height).setDuration(ANIMATION_DURATION).setListener(
Yorke Lee94f49042014-03-03 14:03:08 -0800629 new AnimatorListenerAdapter() {
630 @Override
631 public void onAnimationEnd(Animator animation) {
632 mFragmentsFrame.setTranslationY(0);
Yorke Leeb0d19762014-03-05 14:43:28 -0800633 // Display the fragments spacer (which has the same height as the
634 // search box) now that the search box is hidden, so that
635 // mFragmentsFrame always retains the same height
636 mFragmentsSpacer.setVisibility(View.VISIBLE);
Yorke Lee94f49042014-03-03 14:03:08 -0800637 }
638 });
Yorke Lee7eccf452014-03-14 12:52:42 -0700639
640 if (!mInDialpadSearch && !mInRegularSearch) {
641 // If the favorites fragment is showing, fade to blank.
642 mFragmentsFrame.animate().alpha(0.0f);
643 }
Yorke Leec3766332013-07-31 11:13:16 -0700644 }
645
646 public void showSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800647 final int height = mSearchAndRemoveViewContainer.getHeight();
648 mSearchAndRemoveViewContainer.animate().cancel();
649 mSearchAndRemoveViewContainer.setAlpha(0);
650 mSearchAndRemoveViewContainer.setTranslationY(-height);
651 mSearchAndRemoveViewContainer.animate().withLayer().alpha(1).translationY(0)
Yorke Lee7eccf452014-03-14 12:52:42 -0700652 .setDuration(ANIMATION_DURATION).setListener(new AnimatorListenerAdapter() {
Yorke Lee94f49042014-03-03 14:03:08 -0800653 @Override
654 public void onAnimationStart(Animator animation) {
655 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
656 }
657 });
Yorke Leec3766332013-07-31 11:13:16 -0700658
Yorke Lee94f49042014-03-03 14:03:08 -0800659 mFragmentsFrame.setTranslationY(-height);
Yorke Lee7eccf452014-03-14 12:52:42 -0700660 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(ANIMATION_DURATION)
661 .alpha(1.0f)
Yorke Leec3766332013-07-31 11:13:16 -0700662 .setListener(
663 new AnimatorListenerAdapter() {
664 @Override
665 public void onAnimationStart(Animator animation) {
Yorke Leeb0d19762014-03-05 14:43:28 -0800666 // Hide the fragment spacer now that the search box will
667 // be displayed again
668 mFragmentsSpacer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700669 }
670 });
671 }
672
Ihab Awadfb00cb82014-03-18 16:19:00 -0700673 private void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700674 mMenuButton = findViewById(R.id.overflow_menu);
675 if (mMenuButton != null) {
676 mMenuButton.setOnClickListener(this);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700677 if (mOverflowMenu == null) {
678 mOverflowMenu = buildOptionsMenu(mMenuButton);
679 }
Yorke Lee36c54132013-09-25 16:17:06 -0700680 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700681 }
682
Ihab Awad526c0b82014-02-27 12:55:36 -0800683 mFakeActionBar = findViewById(R.id.fake_action_bar);
684
Yorke Leec3766332013-07-31 11:13:16 -0700685 mCallHistoryButton = findViewById(R.id.call_history_button);
Yorke Leec3766332013-07-31 11:13:16 -0700686 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);
Yorke Leec3766332013-07-31 11:13:16 -0700693 mDialpadButton.setOnClickListener(this);
694 }
695
Ihab Awadfb00cb82014-03-18 16:19:00 -0700696 private PopupMenu buildOptionsMenu(View invoker) {
697 PopupMenu menu = new OverflowPopupMenu(this, invoker);
698 menu.inflate(R.menu.dialtacts_options);
699 menu.setOnMenuItemClickListener(this);
700 return menu;
701 }
702
Chiao Cheng94b10b52012-08-17 16:59:12 -0700703 private void fixIntent(Intent intent) {
704 // This should be cleaned up: the call key used to send an Intent
705 // that just said to go to the recent calls list. It now sends this
706 // abstract action, but this class hasn't been rewritten to deal with it.
707 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
708 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
709 intent.putExtra("call_key", true);
710 setIntent(intent);
711 }
712 }
713
Chiao Cheng94b10b52012-08-17 16:59:12 -0700714 /**
715 * Returns true if the intent is due to hitting the green send key (hardware call button:
716 * KEYCODE_CALL) while in a call.
717 *
718 * @param intent the intent that launched this activity
719 * @param recentCallsRequest true if the intent is requesting to view recent calls
720 * @return true if the intent is due to hitting the green send key while in a call
721 */
Yorke Leec3766332013-07-31 11:13:16 -0700722 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700723 // If there is a call in progress go to the call screen
724 if (recentCallsRequest) {
725 final boolean callKey = intent.getBooleanExtra("call_key", false);
726
727 try {
728 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
729 if (callKey && phone != null && phone.showCallScreen()) {
730 return true;
731 }
732 } catch (RemoteException e) {
733 Log.e(TAG, "Failed to handle send while in call", e);
734 }
735 }
736
737 return false;
738 }
739
740 /**
741 * Sets the current tab based on the intent's request type
742 *
743 * @param intent Intent that contains information about which tab should be selected
744 */
Yorke Leec3766332013-07-31 11:13:16 -0700745 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700746 // If we got here by hitting send and we're in call forward along to the in-call activity
747 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
748 getContentResolver()));
749 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
750 finish();
751 return;
752 }
753
Yorke Lee669b6082013-09-17 15:43:38 -0700754 if (mDialpadFragment != null) {
755 final boolean phoneIsInUse = phoneIsInUse();
756 if (phoneIsInUse || isDialIntent(intent)) {
757 mDialpadFragment.setStartedFromNewIntent(true);
758 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
759 mInCallDialpadUp = true;
760 }
761 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700762 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700763 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 }
765
766 @Override
767 public void onNewIntent(Intent newIntent) {
768 setIntent(newIntent);
769 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700770 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700771 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700772
Chiao Cheng94b10b52012-08-17 16:59:12 -0700773 invalidateOptionsMenu();
774 }
775
776 /** Returns true if the given intent contains a phone number to populate the dialer with */
777 private boolean isDialIntent(Intent intent) {
778 final String action = intent.getAction();
779 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
780 return true;
781 }
782 if (Intent.ACTION_VIEW.equals(action)) {
783 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700784 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700785 return true;
786 }
787 }
788 return false;
789 }
790
791 /**
792 * Returns an appropriate call origin for this Activity. May return null when no call origin
793 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
794 * for remembering the tab in which the user made a phone call, so the external app's DIAL
795 * request should not be counted.)
796 */
797 public String getCallOrigin() {
798 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
799 }
800
Yorke Lee8898cd02013-08-08 10:24:27 -0700801 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
802 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700803 @Override
804 public void onContactSelected(Uri contactUri) {
805 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700806 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700807 }
808
809 @Override
810 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700811 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700812 startActivity(intent);
813 }
814 };
815
Yorke Leec3766332013-07-31 11:13:16 -0700816 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
817 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
818 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700819
820 @Override
821 public void startSearch(String initialQuery, boolean selectInitialQuery,
822 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700823 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700824 if (mInSearchUi) {
825 if (mSearchView.hasFocus()) {
826 showInputMethod(mSearchView.findFocus());
827 } else {
828 mSearchView.requestFocus();
829 }
830 } else {
831 enterSearchUi();
832 }
833 } else {
834 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
835 }
Yorke Leec3766332013-07-31 11:13:16 -0700836 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700837
838 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700839 final InputMethodManager imm = (InputMethodManager) getSystemService(
840 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700841 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700842 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700843 }
844 }
845
846 private void hideInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700847 final InputMethodManager imm = (InputMethodManager) getSystemService(
848 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700849 if (imm != null && view != null) {
850 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
851 }
852 }
853
854 /**
Yorke Leec3766332013-07-31 11:13:16 -0700855 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700856 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700857 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700858 if (getFragmentManager().isDestroyed()) {
859 // Weird race condition where fragment is doing work after the activity is destroyed
860 // due to talkback being on (b/10209937). Just return since we can't do any
861 // constructive here.
862 return;
863 }
864
Yorke Leeef2b7382013-08-09 17:39:25 -0700865 if (DEBUG) {
866 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
867 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700868
Yorke Leec3766332013-07-31 11:13:16 -0700869 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700870
Yorke Leeef2b7382013-08-09 17:39:25 -0700871 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700872 if (mInDialpadSearch) {
873 transaction.remove(mSmartDialSearchFragment);
874 } else if (mInRegularSearch) {
875 transaction.remove(mRegularSearchFragment);
876 } else {
877 transaction.remove(mPhoneFavoriteFragment);
878 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700879
Yorke Leeb207f8a2013-08-29 18:51:06 -0700880 final String tag;
881 if (smartDialSearch) {
882 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
883 } else {
884 tag = TAG_REGULAR_SEARCH_FRAGMENT;
885 }
886 mInDialpadSearch = smartDialSearch;
887 mInRegularSearch = !smartDialSearch;
888
Yorke Leeef2b7382013-08-09 17:39:25 -0700889 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
890 if (fragment == null) {
891 if (smartDialSearch) {
892 fragment = new SmartDialSearchFragment();
893 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700894 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700895 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700896 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700897 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700898 transaction.addToBackStack(null);
899 fragment.setQueryString(query, false);
900 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700901 }
902
903 /**
Yorke Leec3766332013-07-31 11:13:16 -0700904 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700905 */
Yorke Leec3766332013-07-31 11:13:16 -0700906 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700907 // See related bug in enterSearchUI();
908 if (getFragmentManager().isDestroyed()) {
909 return;
910 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700911 // Go all the way back to the favorites fragment, regardless of how many times we
912 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700913 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
914 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700915
916 if (isDialpadShowing()) {
917 mFragmentsFrame.setAlpha(0);
918 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700919 }
920
921 /** Returns an Intent to launch Call Settings screen */
922 public static Intent getCallSettingsIntent() {
923 final Intent intent = new Intent(Intent.ACTION_MAIN);
924 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
925 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
926 return intent;
927 }
928
929 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700930 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700931 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700932 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700933 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700934 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700935 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700936 } else {
937 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700938 }
Yorke Leec3766332013-07-31 11:13:16 -0700939 }
940
941 @Override
942 public void onDialpadQueryChanged(String query) {
943 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
944 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
945 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700946 if (DEBUG) {
947 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
948 }
949 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
950 // This callback can happen if the dialpad fragment is recreated because of
951 // activity destruction. In that case, don't update the search view because
952 // that would bring the user back to the search fragment regardless of the
953 // previous state of the application. Instead, just return here and let the
954 // fragment manager correctly figure out whatever fragment was last displayed.
955 return;
956 }
Yorke Leec3766332013-07-31 11:13:16 -0700957 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700958 }
959 }
Yorke Leec3766332013-07-31 11:13:16 -0700960
961 @Override
962 public void onListFragmentScrollStateChange(int scrollState) {
963 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700964 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700965 hideInputMethod(getCurrentFocus());
966 }
967 }
968
969 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800970 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700971 if (mDialButton != null) {
972 mDialButton.setEnabled(enabled);
973 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800974 }
975
976 @Override
977 public void setDialButtonContainerVisible(boolean visible) {
978 mFakeActionBar.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700979 }
980
981 private boolean phoneIsInUse() {
982 final TelephonyManager tm = (TelephonyManager) getSystemService(
983 Context.TELEPHONY_SERVICE);
984 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
985 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700986
987 @Override
988 public void onShowAllContacts() {
989 final Intent intent = new Intent(this, AllContactsActivity.class);
990 startActivity(intent);
991 }
Yorke Leee1424812013-09-04 18:24:48 -0700992
993 public static Intent getAddNumberToContactIntent(CharSequence text) {
994 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
995 intent.putExtra(Intents.Insert.PHONE, text);
996 intent.setType(Contacts.CONTENT_ITEM_TYPE);
997 return intent;
998 }
999
Yorke Leeda0f9042013-12-05 14:25:51 -08001000 private boolean canIntentBeHandled(Intent intent) {
1001 final PackageManager packageManager = getPackageManager();
1002 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
1003 PackageManager.MATCH_DEFAULT_ONLY);
1004 return resolveInfo != null && resolveInfo.size() > 0;
1005 }
Yorke Lee3cefcc62014-01-16 11:26:46 -08001006
1007 @Override
1008 public void onDragStarted(int itemIndex, int x, int y, PhoneFavoriteTileView view) {
Yorke Lee7eccf452014-03-14 12:52:42 -07001009 crossfadeViews(mRemoveViewContainer, mSearchViewContainer, ANIMATION_DURATION);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001010 }
1011
1012 @Override
1013 public void onDragHovered(int itemIndex, int x, int y) {}
1014
1015 @Override
1016 public void onDragFinished(int x, int y) {
Yorke Lee7eccf452014-03-14 12:52:42 -07001017 crossfadeViews(mSearchViewContainer, mRemoveViewContainer, ANIMATION_DURATION);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001018 }
1019
1020 @Override
1021 public void onDroppedOnRemove() {}
1022
1023 /**
1024 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
1025 * once it has been attached to the activity.
1026 */
1027 @Override
1028 public void setDragDropController(DragDropController dragController) {
1029 mRemoveViewContainer.setDragDropController(dragController);
1030 }
1031
1032 /**
1033 * Crossfades two views so that the first one appears while the other one is fading
1034 * out of view.
1035 */
1036 private void crossfadeViews(final View fadeIn, final View fadeOut, int duration) {
1037 fadeOut.animate().alpha(0).setDuration(duration)
1038 .setListener(new AnimatorListenerAdapter() {
1039 @Override
1040 public void onAnimationEnd(Animator animation) {
1041 fadeOut.setVisibility(View.GONE);
1042 }
1043 });
1044
1045 fadeIn.setVisibility(View.VISIBLE);
1046 fadeIn.setAlpha(0);
Yorke Lee7eccf452014-03-14 12:52:42 -07001047 fadeIn.animate().alpha(1).setDuration(ANIMATION_DURATION)
Yorke Lee3cefcc62014-01-16 11:26:46 -08001048 .setListener(null);
1049 }
Ihab Awad526c0b82014-02-27 12:55:36 -08001050
1051 private boolean shouldShowOnscreenDialButton() {
1052 return getResources().getBoolean(R.bool.config_show_onscreen_dial_button);
1053 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001054}