blob: 8bca7dd8bdcf046f367b11405ba0876b2cba41b8 [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;
Yorke Leec3766332013-07-31 11:13:16 -0700154
Yorke Leeb0d19762014-03-05 14:43:28 -0800155 // Padding view used to shift the fragment frame up when the dialpad is shown so that
156 // the contents of the fragment frame continue to exist in a layout of the same height
157 private View mFragmentsSpacer;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700158 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700159
Yorke Leeb207f8a2013-08-29 18:51:06 -0700160 private boolean mInDialpadSearch;
161 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700162 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700163
Yorke Leeef2b7382013-08-09 17:39:25 -0700164 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700165 * True if the dialpad is only temporarily showing due to being in call
166 */
167 private boolean mInCallDialpadUp;
168
169 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700170 * True when this activity has been launched for the first time.
171 */
Yorke Lee98702de2013-08-06 12:03:32 -0700172 private boolean mFirstLaunch;
Yorke Leec3766332013-07-31 11:13:16 -0700173 private View mSearchViewContainer;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800174 private RemoveView mRemoveViewContainer;
Yorke Lee94f49042014-03-03 14:03:08 -0800175 // This view points to the Framelayout that houses both the search view and remove view
176 // containers.
177 private View mSearchAndRemoveViewContainer;
Yorke Leec3766332013-07-31 11:13:16 -0700178 private View mSearchViewCloseButton;
179 private View mVoiceSearchButton;
180 private EditText mSearchView;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700181
Yorke Leeef2b7382013-08-09 17:39:25 -0700182 private String mSearchQuery;
183
Yorke Leefce269a2013-08-12 11:56:04 -0700184 private DialerDatabaseHelper mDialerDatabaseHelper;
185
Yorke Lee36c54132013-09-25 16:17:06 -0700186 private class OverflowPopupMenu extends PopupMenu {
187 public OverflowPopupMenu(Context context, View anchor) {
188 super(context, anchor);
189 }
190
191 @Override
192 public void show() {
193 final Menu menu = getMenu();
194 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
195 clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
196 super.show();
197 }
198 }
199
Chiao Cheng94b10b52012-08-17 16:59:12 -0700200 /**
201 * Listener used when one of phone numbers in search UI is selected. This will initiate a
202 * phone call using the phone number.
203 */
204 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
205 new OnPhoneNumberPickerActionListener() {
206 @Override
207 public void onPickPhoneNumberAction(Uri dataUri) {
208 // Specify call-origin so that users will see the previous tab instead of
209 // CallLog screen (search UI will be automatically exited).
210 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700211 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700212 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700213 }
214
215 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700216 public void onCallNumberDirectly(String phoneNumber) {
217 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
218 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700219 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700220 }
221
222 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700223 public void onShortcutIntentCreated(Intent intent) {
224 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
225 }
226
227 @Override
228 public void onHomeInActionBarSelected() {
229 exitSearchUi();
230 }
231 };
232
233 /**
234 * Listener used to send search queries to the phone search fragment.
235 */
Yorke Leec3766332013-07-31 11:13:16 -0700236 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
237 @Override
238 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
239 }
240
241 @Override
242 public void onTextChanged(CharSequence s, int start, int before, int count) {
Yorke Leec3766332013-07-31 11:13:16 -0700243 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700244 if (newText.equals(mSearchQuery)) {
245 // If the query hasn't changed (perhaps due to activity being destroyed
246 // and restored, or user launching the same DIAL intent twice), then there is
247 // no need to do anything here.
248 return;
249 }
250 mSearchQuery = newText;
251 if (DEBUG) {
252 Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
253 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700254 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700255
Yorke Leec3766332013-07-31 11:13:16 -0700256 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700257 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700258 exitSearchUi();
259 mSearchViewCloseButton.setVisibility(View.GONE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700260 mVoiceSearchButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700261 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700262 } else if (!TextUtils.isEmpty(newText)) {
263 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
264 (!dialpadSearch && mInRegularSearch);
265 if (!sameSearchMode) {
266 // call enterSearchUi only if we are switching search modes, or entering
267 // search ui for the first time
268 enterSearchUi(dialpadSearch, newText);
269 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700270
Yorke Leeb207f8a2013-08-29 18:51:06 -0700271 if (dialpadSearch && mSmartDialSearchFragment != null) {
272 mSmartDialSearchFragment.setQueryString(newText, false);
273 } else if (mRegularSearchFragment != null) {
274 mRegularSearchFragment.setQueryString(newText, false);
275 }
276 mSearchViewCloseButton.setVisibility(View.VISIBLE);
Yorke Lee3b82fc12013-09-09 08:42:38 -0700277 mVoiceSearchButton.setVisibility(View.GONE);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700278 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700279 }
Yorke Leec3766332013-07-31 11:13:16 -0700280 }
281
282 @Override
283 public void afterTextChanged(Editable s) {
284 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700285 };
286
Yorke Leec3766332013-07-31 11:13:16 -0700287 private boolean isDialpadShowing() {
288 return mDialpadFragment != null && mDialpadFragment.isVisible();
289 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700290
291 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700292 protected void onCreate(Bundle savedInstanceState) {
293 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700294 mFirstLaunch = true;
295
Chiao Cheng94b10b52012-08-17 16:59:12 -0700296 final Intent intent = getIntent();
297 fixIntent(intent);
298
Yorke Lee8898cd02013-08-08 10:24:27 -0700299 setContentView(R.layout.dialtacts_activity);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700300
Yorke Leec3766332013-07-31 11:13:16 -0700301 getActionBar().hide();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700302
Yorke Leeef2b7382013-08-09 17:39:25 -0700303 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
304 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700305 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800306 getFragmentManager().beginTransaction()
307 .add(R.id.dialtacts_frame, new PhoneFavoriteFragment(), TAG_FAVORITES_FRAGMENT)
308 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
309 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700310 } else {
311 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700312 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
313 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700314 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700315 }
316
Yorke Leeb207f8a2013-08-29 18:51:06 -0700317 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Leeb0d19762014-03-05 14:43:28 -0800318 mFragmentsSpacer = findViewById(R.id.contact_tile_frame_spacer);
Ihab Awad526c0b82014-02-27 12:55:36 -0800319
Yorke Lee3cefcc62014-01-16 11:26:46 -0800320 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800321 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
322
323 // When the first global layout pass is completed (and mSearchAndRemoveViewContainer has
324 // been assigned a valid height), assign that height to mFragmentsSpacer as well.
325 mSearchAndRemoveViewContainer.getViewTreeObserver().addOnGlobalLayoutListener(
326 new OnGlobalLayoutListener() {
327 @Override
328 public void onGlobalLayout() {
329 mSearchAndRemoveViewContainer.getViewTreeObserver()
330 .removeOnGlobalLayoutListener(this);
331 mFragmentsSpacer.setLayoutParams(
332 new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
333 mSearchAndRemoveViewContainer.getHeight()));
334 }
335 });
336
337
Yorke Leec3766332013-07-31 11:13:16 -0700338 prepareSearchView();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700339
Ihab Awad526c0b82014-02-27 12:55:36 -0800340 hideDialpadFragment(false, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700341 setupFakeActionBarItems();
342
Yorke Lee0baa98b2013-08-22 10:55:16 -0700343 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700344 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700345 }
346
347 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700348 protected void onResume() {
349 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700350 if (mFirstLaunch) {
351 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700352 } else if (!phoneIsInUse() && mInCallDialpadUp) {
353 hideDialpadFragment(false, true);
354 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700355 }
Yorke Leeda0f9042013-12-05 14:25:51 -0800356 prepareVoiceSearchButton();
Yorke Lee98702de2013-08-06 12:03:32 -0700357 mFirstLaunch = false;
Yorke Leefce269a2013-08-12 11:56:04 -0700358 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700359 }
360
361 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700362 protected void onPause() {
363 if (mClearSearchOnPause) {
364 hideDialpadAndSearchUi();
365 mClearSearchOnPause = false;
366 }
367 super.onPause();
368 }
369
370 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700371 protected void onSaveInstanceState(Bundle outState) {
372 super.onSaveInstanceState(outState);
373 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700374 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
375 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700376 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
377 }
378
379 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700380 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700381 if (fragment instanceof DialpadFragment) {
382 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700383 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700384 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700385 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700386 } else if (fragment instanceof SmartDialSearchFragment) {
387 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700388 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
389 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700390 if (mFragmentsFrame != null) {
391 mFragmentsFrame.setAlpha(1.0f);
392 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700393 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700394 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700395 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
396 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700397 if (mFragmentsFrame != null) {
398 mFragmentsFrame.setAlpha(1.0f);
399 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700400 } else if (fragment instanceof PhoneFavoriteFragment) {
401 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
Yorke Lee5781afe2013-08-12 11:12:59 -0700402 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
Yorke Leec3766332013-07-31 11:13:16 -0700403 }
Yorke Leec3766332013-07-31 11:13:16 -0700404 }
405
406 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700407 public boolean onMenuItemClick(MenuItem item) {
408 switch (item.getItemId()) {
409 case R.id.menu_import_export:
410 // We hard-code the "contactsAreAvailable" argument because doing it properly would
411 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
412 // now in Dialtacts for (potential) performance reasons. Compare with how it is
413 // done in {@link PeopleActivity}.
414 ImportExportDialogFragment.show(getFragmentManager(), true,
Yorke Leef74011e2013-08-02 11:30:30 -0700415 DialtactsActivity.class);
Yorke Leec3766332013-07-31 11:13:16 -0700416 return true;
417 case R.id.menu_clear_frequents:
418 ClearFrequentsDialog.show(getFragmentManager());
419 return true;
Yorke Lee5e931972013-08-30 11:37:53 -0700420 case R.id.menu_add_contact:
Yorke Leec3766332013-07-31 11:13:16 -0700421 try {
422 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
423 } catch (ActivityNotFoundException e) {
424 Toast toast = Toast.makeText(this,
425 R.string.add_contact_not_available,
426 Toast.LENGTH_SHORT);
427 toast.show();
428 }
429 return true;
430 case R.id.menu_call_settings:
Alon Albertb453e5b2013-09-10 15:54:23 -0700431 handleMenuSettings();
Yorke Lee5e931972013-08-30 11:37:53 -0700432 return true;
433 case R.id.menu_all_contacts:
434 onShowAllContacts();
435 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700436 }
437 return false;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700438 }
439
Alon Albertb453e5b2013-09-10 15:54:23 -0700440 protected void handleMenuSettings() {
441 openTelephonySetting(this);
442 }
443
444 public static void openTelephonySetting(Activity activity) {
445 final Intent settingsIntent = getCallSettingsIntent();
446 activity.startActivity(settingsIntent);
447 }
448
Chiao Cheng94b10b52012-08-17 16:59:12 -0700449 @Override
450 public void onClick(View view) {
451 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700452 case R.id.overflow_menu: {
Ihab Awad526c0b82014-02-27 12:55:36 -0800453 if (isDialpadShowing()) {
454 mDialpadFragment.optionsMenuInvoked(view);
455 } else {
456 mOverflowMenu.show();
457 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700458 break;
459 }
Yorke Leec3766332013-07-31 11:13:16 -0700460 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700461 // Reset the boolean flag that tracks whether the dialpad was up because
462 // we were in call. Regardless of whether it was true before, we want to
463 // show the dialpad because the user has explicitly clicked the dialpad
464 // button.
465 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700466 showDialpadFragment(true);
467 break;
Ihab Awad526c0b82014-02-27 12:55:36 -0800468 case R.id.dial_button:
469 // Dial button was pressed; tell the Dialpad fragment
470 mDialpadFragment.dialButtonPressed();
471 break;
Yorke Leec3766332013-07-31 11:13:16 -0700472 case R.id.call_history_button:
Yorke Leef420a532013-08-09 15:16:10 -0700473 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
474 // CONTENT_TYPE, so that we always open our call log from our dialer
Yorke Leec3766332013-07-31 11:13:16 -0700475 final Intent intent = new Intent(this, CallLogActivity.class);
476 startActivity(intent);
477 break;
478 case R.id.search_close_button:
479 // Clear the search field
480 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700481 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700482 mSearchView.setText("");
Chiao Cheng94b10b52012-08-17 16:59:12 -0700483 }
484 break;
Yorke Leec3766332013-07-31 11:13:16 -0700485 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800486 try {
487 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
488 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
489 } catch (ActivityNotFoundException e) {
490 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
491 Toast.LENGTH_SHORT).show();
492 }
Yorke Leec3766332013-07-31 11:13:16 -0700493 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700494 default: {
495 Log.wtf(TAG, "Unexpected onClick event from " + view);
496 break;
497 }
498 }
499 }
500
Yorke Leec3766332013-07-31 11:13:16 -0700501 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800502 public boolean onLongClick(View view) {
503 switch (view.getId()) {
504 case R.id.dial_button: {
505 // Dial button was pressed; tell the Dialpad fragment
506 mDialpadFragment.dialButtonPressed();
507 return true; // Consume the event
508 }
509 default: {
510 Log.wtf(TAG, "Unexpected onClick event from " + view);
511 break;
512 }
513 }
514 return false;
515 }
516
517 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700518 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
519 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
520 if (resultCode == RESULT_OK) {
521 final ArrayList<String> matches = data.getStringArrayListExtra(
522 RecognizerIntent.EXTRA_RESULTS);
523 if (matches.size() > 0) {
524 final String match = matches.get(0);
525 mSearchView.setText(match);
526 } else {
527 Log.e(TAG, "Voice search - nothing heard");
528 }
529 } else {
530 Log.e(TAG, "Voice search failed");
531 }
532 }
533 super.onActivityResult(requestCode, resultCode, data);
534 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700535
Yorke Leec3766332013-07-31 11:13:16 -0700536 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700537 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700538 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700539 if (animate) {
540 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700541 } else {
542 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700543 }
544 ft.show(mDialpadFragment);
545 ft.commit();
Ihab Awad526c0b82014-02-27 12:55:36 -0800546 mDialButton.setVisibility(shouldShowOnscreenDialButton() ? View.VISIBLE : View.GONE);
547 mDialpadButton.setVisibility(View.GONE);
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);
Yorke Leec3766332013-07-31 11:13:16 -0700565 }
566
Chiao Cheng94b10b52012-08-17 16:59:12 -0700567 private void prepareSearchView() {
Yorke Leec3766332013-07-31 11:13:16 -0700568 mSearchViewContainer = findViewById(R.id.search_view_container);
569 mSearchViewCloseButton = findViewById(R.id.search_close_button);
570 mSearchViewCloseButton.setOnClickListener(this);
Yorke Leeda0f9042013-12-05 14:25:51 -0800571
Yorke Leec3766332013-07-31 11:13:16 -0700572 mSearchView = (EditText) findViewById(R.id.search_view);
573 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Tyler Gunna8ff9752014-02-24 11:57:57 -0800574 mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
Yorke Leeda0f9042013-12-05 14:25:51 -0800575
576 prepareVoiceSearchButton();
577 }
578
579 private void prepareVoiceSearchButton() {
580 mVoiceSearchButton = findViewById(R.id.voice_search_button);
581 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
582 if (canIntentBeHandled(voiceIntent)) {
583 mVoiceSearchButton.setVisibility(View.VISIBLE);
584 mVoiceSearchButton.setOnClickListener(this);
585 } else {
586 mVoiceSearchButton.setVisibility(View.GONE);
587 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700588 }
589
Yorke Leec3766332013-07-31 11:13:16 -0700590 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
591 @Override
592 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800593 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700594 }
595 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700596
Yorke Leeb207f8a2013-08-29 18:51:06 -0700597 private boolean getInSearchUi() {
598 return mInDialpadSearch || mInRegularSearch;
599 }
600
601 private void setNotInSearchUi() {
602 mInDialpadSearch = false;
603 mInRegularSearch = false;
604 }
605
Yorke Lee311969c2013-08-26 06:31:11 -0700606 private void hideDialpadAndSearchUi() {
Yorke Lee7a06c442013-09-04 15:49:51 -0700607 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700608 hideDialpadFragment(false, true);
609 }
610
Yorke Leec3766332013-07-31 11:13:16 -0700611 public void hideSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800612 final int height = mSearchAndRemoveViewContainer.getHeight();
Yorke Leeb0d19762014-03-05 14:43:28 -0800613
Yorke Lee94f49042014-03-03 14:03:08 -0800614 mSearchAndRemoveViewContainer.animate().cancel();
615 mSearchAndRemoveViewContainer.setAlpha(1);
616 mSearchAndRemoveViewContainer.setTranslationY(0);
617 mSearchAndRemoveViewContainer.animate().withLayer().alpha(0)
Yorke Lee7eccf452014-03-14 12:52:42 -0700618 .translationY(-height).setDuration(ANIMATION_DURATION)
Yorke Lee94f49042014-03-03 14:03:08 -0800619 .setListener(mHideListener);
Christine Chen9c1e0652013-05-23 15:40:19 -0700620
Yorke Lee94f49042014-03-03 14:03:08 -0800621 mFragmentsFrame.animate().withLayer()
Yorke Lee7eccf452014-03-14 12:52:42 -0700622 .translationY(-height).setDuration(ANIMATION_DURATION).setListener(
Yorke Lee94f49042014-03-03 14:03:08 -0800623 new AnimatorListenerAdapter() {
624 @Override
625 public void onAnimationEnd(Animator animation) {
626 mFragmentsFrame.setTranslationY(0);
Yorke Leeb0d19762014-03-05 14:43:28 -0800627 // Display the fragments spacer (which has the same height as the
628 // search box) now that the search box is hidden, so that
629 // mFragmentsFrame always retains the same height
630 mFragmentsSpacer.setVisibility(View.VISIBLE);
Yorke Lee94f49042014-03-03 14:03:08 -0800631 }
632 });
Yorke Lee7eccf452014-03-14 12:52:42 -0700633
634 if (!mInDialpadSearch && !mInRegularSearch) {
635 // If the favorites fragment is showing, fade to blank.
636 mFragmentsFrame.animate().alpha(0.0f);
637 }
Yorke Leec3766332013-07-31 11:13:16 -0700638 }
639
640 public void showSearchBar() {
Yorke Lee94f49042014-03-03 14:03:08 -0800641 final int height = mSearchAndRemoveViewContainer.getHeight();
642 mSearchAndRemoveViewContainer.animate().cancel();
643 mSearchAndRemoveViewContainer.setAlpha(0);
644 mSearchAndRemoveViewContainer.setTranslationY(-height);
645 mSearchAndRemoveViewContainer.animate().withLayer().alpha(1).translationY(0)
Yorke Lee7eccf452014-03-14 12:52:42 -0700646 .setDuration(ANIMATION_DURATION).setListener(new AnimatorListenerAdapter() {
Yorke Lee94f49042014-03-03 14:03:08 -0800647 @Override
648 public void onAnimationStart(Animator animation) {
649 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
650 }
651 });
Yorke Leec3766332013-07-31 11:13:16 -0700652
Yorke Lee94f49042014-03-03 14:03:08 -0800653 mFragmentsFrame.setTranslationY(-height);
Yorke Lee7eccf452014-03-14 12:52:42 -0700654 mFragmentsFrame.animate().withLayer().translationY(0).setDuration(ANIMATION_DURATION)
655 .alpha(1.0f)
Yorke Leec3766332013-07-31 11:13:16 -0700656 .setListener(
657 new AnimatorListenerAdapter() {
658 @Override
659 public void onAnimationStart(Animator animation) {
Yorke Leeb0d19762014-03-05 14:43:28 -0800660 // Hide the fragment spacer now that the search box will
661 // be displayed again
662 mFragmentsSpacer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700663 }
664 });
665 }
666
667
Yorke Leeb207f8a2013-08-29 18:51:06 -0700668 public void setupFakeActionBarItems() {
Yorke Leec3766332013-07-31 11:13:16 -0700669 mMenuButton = findViewById(R.id.overflow_menu);
670 if (mMenuButton != null) {
671 mMenuButton.setOnClickListener(this);
Yorke Lee36c54132013-09-25 16:17:06 -0700672
673 mOverflowMenu = new OverflowPopupMenu(DialtactsActivity.this, mMenuButton);
674 final Menu menu = mOverflowMenu.getMenu();
675 mOverflowMenu.inflate(R.menu.dialtacts_options);
676 mOverflowMenu.setOnMenuItemClickListener(this);
677 mMenuButton.setOnTouchListener(mOverflowMenu.getDragToOpenListener());
Yorke Leec3766332013-07-31 11:13:16 -0700678 }
679
Ihab Awad526c0b82014-02-27 12:55:36 -0800680 mFakeActionBar = findViewById(R.id.fake_action_bar);
681
Yorke Leec3766332013-07-31 11:13:16 -0700682 mCallHistoryButton = findViewById(R.id.call_history_button);
683 // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
684 mCallHistoryButton.setOnClickListener(this);
685
Ihab Awad526c0b82014-02-27 12:55:36 -0800686 mDialButton = findViewById(R.id.dial_button);
687 mDialButton.setOnClickListener(this);
688 mDialButton.setOnLongClickListener(this);
689
Yorke Leec3766332013-07-31 11:13:16 -0700690 mDialpadButton = findViewById(R.id.dialpad_button);
691 // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
692 mDialpadButton.setOnClickListener(this);
693 }
694
Chiao Cheng94b10b52012-08-17 16:59:12 -0700695 private void fixIntent(Intent intent) {
696 // This should be cleaned up: the call key used to send an Intent
697 // that just said to go to the recent calls list. It now sends this
698 // abstract action, but this class hasn't been rewritten to deal with it.
699 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
700 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
701 intent.putExtra("call_key", true);
702 setIntent(intent);
703 }
704 }
705
Chiao Cheng94b10b52012-08-17 16:59:12 -0700706 /**
707 * Returns true if the intent is due to hitting the green send key (hardware call button:
708 * KEYCODE_CALL) while in a call.
709 *
710 * @param intent the intent that launched this activity
711 * @param recentCallsRequest true if the intent is requesting to view recent calls
712 * @return true if the intent is due to hitting the green send key while in a call
713 */
Yorke Leec3766332013-07-31 11:13:16 -0700714 private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700715 // If there is a call in progress go to the call screen
716 if (recentCallsRequest) {
717 final boolean callKey = intent.getBooleanExtra("call_key", false);
718
719 try {
720 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
721 if (callKey && phone != null && phone.showCallScreen()) {
722 return true;
723 }
724 } catch (RemoteException e) {
725 Log.e(TAG, "Failed to handle send while in call", e);
726 }
727 }
728
729 return false;
730 }
731
732 /**
733 * Sets the current tab based on the intent's request type
734 *
735 * @param intent Intent that contains information about which tab should be selected
736 */
Yorke Leec3766332013-07-31 11:13:16 -0700737 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700738 // If we got here by hitting send and we're in call forward along to the in-call activity
739 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
740 getContentResolver()));
741 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
742 finish();
743 return;
744 }
745
Yorke Lee669b6082013-09-17 15:43:38 -0700746 if (mDialpadFragment != null) {
747 final boolean phoneIsInUse = phoneIsInUse();
748 if (phoneIsInUse || isDialIntent(intent)) {
749 mDialpadFragment.setStartedFromNewIntent(true);
750 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
751 mInCallDialpadUp = true;
752 }
753 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700754 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700755 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700756 }
757
758 @Override
759 public void onNewIntent(Intent newIntent) {
760 setIntent(newIntent);
761 fixIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700762 displayFragment(newIntent);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700763 final String action = newIntent.getAction();
Yorke Leec3766332013-07-31 11:13:16 -0700764
Chiao Cheng94b10b52012-08-17 16:59:12 -0700765 invalidateOptionsMenu();
766 }
767
768 /** Returns true if the given intent contains a phone number to populate the dialer with */
769 private boolean isDialIntent(Intent intent) {
770 final String action = intent.getAction();
771 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
772 return true;
773 }
774 if (Intent.ACTION_VIEW.equals(action)) {
775 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700776 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700777 return true;
778 }
779 }
780 return false;
781 }
782
783 /**
784 * Returns an appropriate call origin for this Activity. May return null when no call origin
785 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
786 * for remembering the tab in which the user made a phone call, so the external app's DIAL
787 * request should not be counted.)
788 */
789 public String getCallOrigin() {
790 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
791 }
792
Yorke Lee8898cd02013-08-08 10:24:27 -0700793 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
794 new PhoneFavoriteFragment.Listener() {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700795 @Override
796 public void onContactSelected(Uri contactUri) {
797 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700798 DialtactsActivity.this, contactUri, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700799 }
800
801 @Override
802 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700803 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700804 startActivity(intent);
805 }
806 };
807
Yorke Leec3766332013-07-31 11:13:16 -0700808 /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
809 * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
810 * be showing when the search key is pressed so there is more state management involved.
Chiao Cheng94b10b52012-08-17 16:59:12 -0700811
812 @Override
813 public void startSearch(String initialQuery, boolean selectInitialQuery,
814 Bundle appSearchData, boolean globalSearch) {
Yorke Leec3766332013-07-31 11:13:16 -0700815 if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700816 if (mInSearchUi) {
817 if (mSearchView.hasFocus()) {
818 showInputMethod(mSearchView.findFocus());
819 } else {
820 mSearchView.requestFocus();
821 }
822 } else {
823 enterSearchUi();
824 }
825 } else {
826 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
827 }
Yorke Leec3766332013-07-31 11:13:16 -0700828 }*/
Chiao Cheng94b10b52012-08-17 16:59:12 -0700829
830 private void showInputMethod(View view) {
Yorke Leec3766332013-07-31 11:13:16 -0700831 final InputMethodManager imm = (InputMethodManager) getSystemService(
832 Context.INPUT_METHOD_SERVICE);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700833 if (imm != null) {
Yorke Leec3766332013-07-31 11:13:16 -0700834 imm.showSoftInput(view, 0);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700835 }
836 }
837
838 private void hideInputMethod(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 && view != null) {
842 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
843 }
844 }
845
846 /**
Yorke Leec3766332013-07-31 11:13:16 -0700847 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700848 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700849 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700850 if (getFragmentManager().isDestroyed()) {
851 // Weird race condition where fragment is doing work after the activity is destroyed
852 // due to talkback being on (b/10209937). Just return since we can't do any
853 // constructive here.
854 return;
855 }
856
Yorke Leeef2b7382013-08-09 17:39:25 -0700857 if (DEBUG) {
858 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
859 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700860
Yorke Leec3766332013-07-31 11:13:16 -0700861 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700862
Yorke Leeef2b7382013-08-09 17:39:25 -0700863 SearchFragment fragment;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700864 if (mInDialpadSearch) {
865 transaction.remove(mSmartDialSearchFragment);
866 } else if (mInRegularSearch) {
867 transaction.remove(mRegularSearchFragment);
868 } else {
869 transaction.remove(mPhoneFavoriteFragment);
870 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700871
Yorke Leeb207f8a2013-08-29 18:51:06 -0700872 final String tag;
873 if (smartDialSearch) {
874 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
875 } else {
876 tag = TAG_REGULAR_SEARCH_FRAGMENT;
877 }
878 mInDialpadSearch = smartDialSearch;
879 mInRegularSearch = !smartDialSearch;
880
Yorke Leeef2b7382013-08-09 17:39:25 -0700881 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
882 if (fragment == null) {
883 if (smartDialSearch) {
884 fragment = new SmartDialSearchFragment();
885 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700886 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700887 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700888 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700889 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700890 transaction.addToBackStack(null);
891 fragment.setQueryString(query, false);
892 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700893 }
894
895 /**
Yorke Leec3766332013-07-31 11:13:16 -0700896 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700897 */
Yorke Leec3766332013-07-31 11:13:16 -0700898 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700899 // See related bug in enterSearchUI();
900 if (getFragmentManager().isDestroyed()) {
901 return;
902 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700903 // Go all the way back to the favorites fragment, regardless of how many times we
904 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700905 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
906 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700907
908 if (isDialpadShowing()) {
909 mFragmentsFrame.setAlpha(0);
910 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700911 }
912
913 /** Returns an Intent to launch Call Settings screen */
914 public static Intent getCallSettingsIntent() {
915 final Intent intent = new Intent(Intent.ACTION_MAIN);
916 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
917 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
918 return intent;
919 }
920
921 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700922 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700923 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700924 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700925 } else if (getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700926 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700927 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700928 } else {
929 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700930 }
Yorke Leec3766332013-07-31 11:13:16 -0700931 }
932
933 @Override
934 public void onDialpadQueryChanged(String query) {
935 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
936 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
937 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700938 if (DEBUG) {
939 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
940 }
941 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
942 // This callback can happen if the dialpad fragment is recreated because of
943 // activity destruction. In that case, don't update the search view because
944 // that would bring the user back to the search fragment regardless of the
945 // previous state of the application. Instead, just return here and let the
946 // fragment manager correctly figure out whatever fragment was last displayed.
947 return;
948 }
Yorke Leec3766332013-07-31 11:13:16 -0700949 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700950 }
951 }
Yorke Leec3766332013-07-31 11:13:16 -0700952
953 @Override
954 public void onListFragmentScrollStateChange(int scrollState) {
955 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700956 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700957 hideInputMethod(getCurrentFocus());
958 }
959 }
960
961 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800962 public void setDialButtonEnabled(boolean enabled) {
963 mDialButton.setEnabled(enabled);
964 }
965
966 @Override
967 public void setDialButtonContainerVisible(boolean visible) {
968 mFakeActionBar.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700969 }
970
971 private boolean phoneIsInUse() {
972 final TelephonyManager tm = (TelephonyManager) getSystemService(
973 Context.TELEPHONY_SERVICE);
974 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
975 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700976
977 @Override
978 public void onShowAllContacts() {
979 final Intent intent = new Intent(this, AllContactsActivity.class);
980 startActivity(intent);
981 }
Yorke Leee1424812013-09-04 18:24:48 -0700982
983 public static Intent getAddNumberToContactIntent(CharSequence text) {
984 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
985 intent.putExtra(Intents.Insert.PHONE, text);
986 intent.setType(Contacts.CONTENT_ITEM_TYPE);
987 return intent;
988 }
989
Yorke Leeda0f9042013-12-05 14:25:51 -0800990 private boolean canIntentBeHandled(Intent intent) {
991 final PackageManager packageManager = getPackageManager();
992 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
993 PackageManager.MATCH_DEFAULT_ONLY);
994 return resolveInfo != null && resolveInfo.size() > 0;
995 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800996
997 @Override
998 public void onDragStarted(int itemIndex, int x, int y, PhoneFavoriteTileView view) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700999 crossfadeViews(mRemoveViewContainer, mSearchViewContainer, ANIMATION_DURATION);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001000 }
1001
1002 @Override
1003 public void onDragHovered(int itemIndex, int x, int y) {}
1004
1005 @Override
1006 public void onDragFinished(int x, int y) {
Yorke Lee7eccf452014-03-14 12:52:42 -07001007 crossfadeViews(mSearchViewContainer, mRemoveViewContainer, ANIMATION_DURATION);
Yorke Lee3cefcc62014-01-16 11:26:46 -08001008 }
1009
1010 @Override
1011 public void onDroppedOnRemove() {}
1012
1013 /**
1014 * Allows the PhoneFavoriteFragment to attach the drag controller to mRemoveViewContainer
1015 * once it has been attached to the activity.
1016 */
1017 @Override
1018 public void setDragDropController(DragDropController dragController) {
1019 mRemoveViewContainer.setDragDropController(dragController);
1020 }
1021
1022 /**
1023 * Crossfades two views so that the first one appears while the other one is fading
1024 * out of view.
1025 */
1026 private void crossfadeViews(final View fadeIn, final View fadeOut, int duration) {
1027 fadeOut.animate().alpha(0).setDuration(duration)
1028 .setListener(new AnimatorListenerAdapter() {
1029 @Override
1030 public void onAnimationEnd(Animator animation) {
1031 fadeOut.setVisibility(View.GONE);
1032 }
1033 });
1034
1035 fadeIn.setVisibility(View.VISIBLE);
1036 fadeIn.setAlpha(0);
Yorke Lee7eccf452014-03-14 12:52:42 -07001037 fadeIn.animate().alpha(1).setDuration(ANIMATION_DURATION)
Yorke Lee3cefcc62014-01-16 11:26:46 -08001038 .setListener(null);
1039 }
Ihab Awad526c0b82014-02-27 12:55:36 -08001040
1041 private boolean shouldShowOnscreenDialButton() {
1042 return getResources().getBoolean(R.bool.config_show_onscreen_dial_button);
1043 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001044}