blob: e0e9762855e3c00f40705d0a5eca98debf2c6ca6 [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;
Yorke Leecc4660d2014-04-24 11:22:57 -070020import android.animation.LayoutTransition;
Yorke Leec3766332013-07-31 11:13:16 -070021import android.animation.Animator.AnimatorListener;
22import android.animation.AnimatorListenerAdapter;
Yorke Lee53a22302014-04-29 18:13:46 -070023import android.app.ActionBar;
Chiao Cheng94b10b52012-08-17 16:59:12 -070024import android.app.Activity;
25import android.app.Fragment;
26import android.app.FragmentManager;
27import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080028import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070029import android.content.Context;
30import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080031import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
Yorke Lee33932ff2014-04-16 13:34:32 -070033import android.content.res.TypedArray;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import android.net.Uri;
35import android.os.Bundle;
36import android.os.RemoteException;
37import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070038import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070039import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070040import android.speech.RecognizerIntent;
Yorke Leecc4660d2014-04-24 11:22:57 -070041import android.support.v4.view.ViewPager;
Yorke Leec3766332013-07-31 11:13:16 -070042import android.telephony.TelephonyManager;
Yorke Lee53a22302014-04-29 18:13:46 -070043import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070045import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.util.Log;
47import android.view.Menu;
Yorke Lee86e21f72014-04-02 16:49:38 -070048import android.view.MenuInflater;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.View;
Yorke Lee86e21f72014-04-02 16:49:38 -070051import android.view.animation.AccelerateInterpolator;
52import android.view.animation.DecelerateInterpolator;
53import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070054import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070055import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070056import android.widget.EditText;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.widget.PopupMenu;
Yorke Leecc4660d2014-04-24 11:22:57 -070058import android.widget.RelativeLayout;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080059import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070060
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070061import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070062import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070063import com.android.contacts.common.dialog.ClearFrequentsDialog;
64import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080065import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070066import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070067import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070068import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070069import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070071import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070072import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080073import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070074import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080075import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070076import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070077import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070078import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070079import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080080import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070081import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070082import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070083import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070084import com.android.internal.telephony.ITelephony;
85
Yorke Leec3766332013-07-31 11:13:16 -070086import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080087import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070088
Chiao Cheng94b10b52012-08-17 16:59:12 -070089/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070090 * The dialer tab's title is 'phone', a more common name (see strings.xml).
91 */
Yorke Leec3766332013-07-31 11:13:16 -070092public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070093 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -070094 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080095 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -070096 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -070097 SpeedDialFragment.HostInterface,
Yorke Leee00c9fe2014-04-12 12:42:06 -070098 OnDragDropListener, View.OnLongClickListener,
Yorke Leecc4660d2014-04-24 11:22:57 -070099 OnPhoneNumberPickerActionListener,
100 ViewPager.OnPageChangeListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101 private static final String TAG = "DialtactsActivity";
102
Ihab Awad526c0b82014-02-27 12:55:36 -0800103 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700104
Yorke Leef74011e2013-08-02 11:30:30 -0700105 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
106
Chiao Cheng94b10b52012-08-17 16:59:12 -0700107 /** Used to open Call Setting */
108 private static final String PHONE_PACKAGE = "com.android.phone";
109 private static final String CALL_SETTINGS_CLASS_NAME =
110 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700111 /** @see #getCallOrigin() */
112 private static final String CALL_ORIGIN_DIALTACTS =
113 "com.android.dialer.DialtactsActivity";
114
Yorke Leeb207f8a2013-08-29 18:51:06 -0700115 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
116 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700117 private static final String KEY_SEARCH_QUERY = "search_query";
118 private static final String KEY_FIRST_LAUNCH = "first_launch";
119
Yorke Leec3766332013-07-31 11:13:16 -0700120 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
121 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
122 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
123 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700124
Chiao Cheng94b10b52012-08-17 16:59:12 -0700125 /**
126 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
127 */
128 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
129
Yorke Leec3766332013-07-31 11:13:16 -0700130 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700131
Yorke Lee86e21f72014-04-02 16:49:38 -0700132 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800133
Yorke Leec3766332013-07-31 11:13:16 -0700134 /**
Yorke Leec3766332013-07-31 11:13:16 -0700135 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700136 */
Yorke Leef74011e2013-08-02 11:30:30 -0700137 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700138
139 /**
140 * Fragment for searching phone numbers using the alphanumeric keyboard.
141 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700142 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700143
144 /**
145 * Fragment for searching phone numbers using the dialpad.
146 */
147 private SmartDialSearchFragment mSmartDialSearchFragment;
148
Yorke Leee00c9fe2014-04-12 12:42:06 -0700149 /**
150 * Fragment containing the speed dial list, recents list, and all contacts list.
151 */
152 private ListsFragment mListsFragment;
153
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700154 private View mFloatingActionButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700155 private View mMenuButton;
Yorke Leec3766332013-07-31 11:13:16 -0700156 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800157 private View mDialButton;
Yorke Leec3766332013-07-31 11:13:16 -0700158
Yorke Leeb207f8a2013-08-29 18:51:06 -0700159 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700160
Yorke Lee33932ff2014-04-16 13:34:32 -0700161 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700162 private boolean mInDialpadSearch;
163 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700164 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700165
Yorke Leeef2b7382013-08-09 17:39:25 -0700166 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700167 * The position of the currently selected tab in the attached {@link ListsFragment}.
168 */
169 private int mCurrentTabPosition = 0;
170
171 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700172 * True if the dialpad is only temporarily showing due to being in call
173 */
174 private boolean mInCallDialpadUp;
175
176 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700177 * True when this activity has been launched for the first time.
178 */
Yorke Lee98702de2013-08-06 12:03:32 -0700179 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700180
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700181 /**
182 * Search query to be applied to the SearchView in the ActionBar once
183 * onCreateOptionsMenu has been called.
184 */
185 private String mPendingSearchViewQuery;
186
Yorke Lee94f49042014-03-03 14:03:08 -0800187 // This view points to the Framelayout that houses both the search view and remove view
188 // containers.
189 private View mSearchAndRemoveViewContainer;
Yorke Lee53a22302014-04-29 18:13:46 -0700190 private EditText mSearchView;
191 private View mSearchViewCloseButton;
192 private View mVoiceSearchButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700193 /**
194 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
195 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
196 * removed from the speed dial list.
197 */
198 private RemoveView mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700199
Yorke Lee86e21f72014-04-02 16:49:38 -0700200 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
201 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700202 private String mSearchQuery;
203
Yorke Leefce269a2013-08-12 11:56:04 -0700204 private DialerDatabaseHelper mDialerDatabaseHelper;
205
Yorke Leee00c9fe2014-04-12 12:42:06 -0700206 private class OverflowPopupMenu extends PopupMenu {
207 public OverflowPopupMenu(Context context, View anchor) {
208 super(context, anchor);
209 }
210
211 @Override
212 public void show() {
213 final Menu menu = getMenu();
214 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Leed999b712014-04-23 15:10:58 -0700215 // TODO: Check mSpeedDialFragment.hasFrequents()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700216 clearFrequents.setVisible(true);
217 super.show();
218 }
219 }
220
Chiao Cheng94b10b52012-08-17 16:59:12 -0700221 /**
222 * Listener used when one of phone numbers in search UI is selected. This will initiate a
223 * phone call using the phone number.
224 */
225 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
226 new OnPhoneNumberPickerActionListener() {
227 @Override
228 public void onPickPhoneNumberAction(Uri dataUri) {
229 // Specify call-origin so that users will see the previous tab instead of
230 // CallLog screen (search UI will be automatically exited).
231 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700232 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700233 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700234 }
235
236 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700237 public void onCallNumberDirectly(String phoneNumber) {
238 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
239 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700240 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700241 }
242
243 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700244 public void onShortcutIntentCreated(Intent intent) {
245 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
246 }
247
248 @Override
249 public void onHomeInActionBarSelected() {
250 exitSearchUi();
251 }
252 };
253
254 /**
255 * Listener used to send search queries to the phone search fragment.
256 */
Yorke Lee53a22302014-04-29 18:13:46 -0700257 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Yorke Leec3766332013-07-31 11:13:16 -0700258 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700259 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Yorke Leec3766332013-07-31 11:13:16 -0700260 }
261
262 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700263 public void onTextChanged(CharSequence s, int start, int before, int count) {
264 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700265 if (newText.equals(mSearchQuery)) {
266 // If the query hasn't changed (perhaps due to activity being destroyed
267 // and restored, or user launching the same DIAL intent twice), then there is
268 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700269 return;
Yorke Leeef2b7382013-08-09 17:39:25 -0700270 }
271 mSearchQuery = newText;
272 if (DEBUG) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700273 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leeef2b7382013-08-09 17:39:25 -0700274 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700275 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700276
Yorke Leec3766332013-07-31 11:13:16 -0700277 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700278 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700279 exitSearchUi();
Yorke Lee53a22302014-04-29 18:13:46 -0700280 mSearchViewCloseButton.setVisibility(View.GONE);
281 mVoiceSearchButton.setVisibility(View.VISIBLE);
282 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700283 } else if (!TextUtils.isEmpty(newText)) {
284 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
285 (!dialpadSearch && mInRegularSearch);
286 if (!sameSearchMode) {
287 // call enterSearchUi only if we are switching search modes, or entering
288 // search ui for the first time
289 enterSearchUi(dialpadSearch, newText);
290 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291
Yorke Leeb207f8a2013-08-29 18:51:06 -0700292 if (dialpadSearch && mSmartDialSearchFragment != null) {
293 mSmartDialSearchFragment.setQueryString(newText, false);
294 } else if (mRegularSearchFragment != null) {
295 mRegularSearchFragment.setQueryString(newText, false);
296 }
Yorke Lee53a22302014-04-29 18:13:46 -0700297 mSearchViewCloseButton.setVisibility(View.VISIBLE);
298 mVoiceSearchButton.setVisibility(View.GONE);
299 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700300 }
Yorke Lee53a22302014-04-29 18:13:46 -0700301 return;
302 }
303
304 @Override
305 public void afterTextChanged(Editable s) {
Yorke Leec3766332013-07-31 11:13:16 -0700306 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700307 };
308
Yorke Leec3766332013-07-31 11:13:16 -0700309 private boolean isDialpadShowing() {
310 return mDialpadFragment != null && mDialpadFragment.isVisible();
311 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700312
313 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700314 protected void onCreate(Bundle savedInstanceState) {
315 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700316 mFirstLaunch = true;
317
Yorke Lee8898cd02013-08-08 10:24:27 -0700318 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800319 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700320
Yorke Lee53a22302014-04-29 18:13:46 -0700321 final ActionBar actionBar = getActionBar();
322 actionBar.setCustomView(R.layout.search_edittext);
323 actionBar.setDisplayShowCustomEnabled(true);
324
325 final View customView = actionBar.getCustomView();
326
327 mSearchView = (EditText) customView.findViewById(R.id.search_view);
328 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
329
330 mSearchViewCloseButton = customView.findViewById(R.id.search_close_button);
331 mSearchViewCloseButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700332
Yorke Lee33932ff2014-04-16 13:34:32 -0700333 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
334 new int[] { android.R.attr.actionBarSize });
335 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
336 styledAttributes.recycle();
337
Yorke Leeef2b7382013-08-09 17:39:25 -0700338 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
339 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700340 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800341 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700342 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800343 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
344 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700345 } else {
346 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700347 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
348 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700349 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700350 }
351
Yorke Leecc4660d2014-04-24 11:22:57 -0700352 RelativeLayout parent = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
353 parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
354
Yorke Leeb207f8a2013-08-29 18:51:06 -0700355 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee86e21f72014-04-02 16:49:38 -0700356
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700357 mFloatingActionButton = findViewById(R.id.floating_action_button);
Andrew Lee4cbc9732014-04-24 14:38:58 -0700358 ViewUtil.setupFloatingActionButton(mFloatingActionButton, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700359
Yorke Lee86e21f72014-04-02 16:49:38 -0700360 mDialButton = findViewById(R.id.dial_button);
361 mDialButton.setOnClickListener(this);
362 mDialpadButton = findViewById(R.id.dialpad_button);
363 mDialpadButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800364
Yorke Lee3cefcc62014-01-16 11:26:46 -0800365 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800366 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
367
Yorke Lee0baa98b2013-08-22 10:55:16 -0700368 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700369 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700370 }
371
372 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700373 protected void onResume() {
374 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700375 if (mFirstLaunch) {
376 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700377 } else if (!phoneIsInUse() && mInCallDialpadUp) {
378 hideDialpadFragment(false, true);
379 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700380 }
381 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700382 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700383 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700384 }
385
386 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700387 protected void onPause() {
388 if (mClearSearchOnPause) {
389 hideDialpadAndSearchUi();
390 mClearSearchOnPause = false;
391 }
392 super.onPause();
393 }
394
395 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700396 protected void onSaveInstanceState(Bundle outState) {
397 super.onSaveInstanceState(outState);
398 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700399 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
400 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700401 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
402 }
403
404 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700405 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700406 if (fragment instanceof DialpadFragment) {
407 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700408 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700409 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700410 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700411 } else if (fragment instanceof SmartDialSearchFragment) {
412 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700413 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
414 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700415 if (mFragmentsFrame != null) {
416 mFragmentsFrame.setAlpha(1.0f);
417 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700418 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700419 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700420 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
421 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700422 if (mFragmentsFrame != null) {
423 mFragmentsFrame.setAlpha(1.0f);
424 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700425 } else if (fragment instanceof ListsFragment) {
426 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700427 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700428 }
Yorke Leec3766332013-07-31 11:13:16 -0700429 }
430
Alon Albertb453e5b2013-09-10 15:54:23 -0700431 protected void handleMenuSettings() {
432 openTelephonySetting(this);
433 }
434
435 public static void openTelephonySetting(Activity activity) {
436 final Intent settingsIntent = getCallSettingsIntent();
437 activity.startActivity(settingsIntent);
438 }
439
Chiao Cheng94b10b52012-08-17 16:59:12 -0700440 @Override
441 public void onClick(View view) {
442 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700443 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700444 // Reset the boolean flag that tracks whether the dialpad was up because
445 // we were in call. Regardless of whether it was true before, we want to
446 // show the dialpad because the user has explicitly clicked the dialpad
447 // button.
448 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700449 showDialpadFragment(true);
450 break;
Yorke Lee86e21f72014-04-02 16:49:38 -0700451 case R.id.dial_button:
452 // Dial button was pressed; tell the Dialpad fragment
453 mDialpadFragment.dialButtonPressed();
454 break;
Yorke Leec3766332013-07-31 11:13:16 -0700455 case R.id.search_close_button:
456 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700457 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700458 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700459 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700460 }
461 break;
Yorke Leec3766332013-07-31 11:13:16 -0700462 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800463 try {
464 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
465 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
466 } catch (ActivityNotFoundException e) {
467 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
468 Toast.LENGTH_SHORT).show();
469 }
Yorke Leec3766332013-07-31 11:13:16 -0700470 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700471 default: {
472 Log.wtf(TAG, "Unexpected onClick event from " + view);
473 break;
474 }
475 }
476 }
477
Yorke Leec3766332013-07-31 11:13:16 -0700478 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700479 public boolean onOptionsItemSelected(MenuItem item) {
480 switch (item.getItemId()) {
481 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700482 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700483 break;
484 case R.id.menu_add_contact:
485 try {
486 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
487 } catch (ActivityNotFoundException e) {
488 Toast toast = Toast.makeText(this,
489 R.string.add_contact_not_available,
490 Toast.LENGTH_SHORT);
491 toast.show();
492 }
493 break;
494 case R.id.menu_import_export:
495 // We hard-code the "contactsAreAvailable" argument because doing it properly would
496 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
497 // now in Dialtacts for (potential) performance reasons. Compare with how it is
498 // done in {@link PeopleActivity}.
499 ImportExportDialogFragment.show(getFragmentManager(), true,
500 DialtactsActivity.class);
501 return true;
502 case R.id.menu_clear_frequents:
503 // TODO: This should be enabled/disabled based on
Yorke Leed999b712014-04-23 15:10:58 -0700504 // SpeedDialFragment.hasFrequents
Yorke Lee86e21f72014-04-02 16:49:38 -0700505 ClearFrequentsDialog.show(getFragmentManager());
506 return true;
507 case R.id.menu_call_settings:
508 handleMenuSettings();
509 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700510 }
511 return false;
512 }
513
514 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800515 public boolean onLongClick(View view) {
516 switch (view.getId()) {
517 case R.id.dial_button: {
518 // Dial button was pressed; tell the Dialpad fragment
519 mDialpadFragment.dialButtonPressed();
520 return true; // Consume the event
521 }
522 default: {
523 Log.wtf(TAG, "Unexpected onClick event from " + view);
524 break;
525 }
526 }
527 return false;
528 }
529
530 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700531 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
532 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
533 if (resultCode == RESULT_OK) {
534 final ArrayList<String> matches = data.getStringArrayListExtra(
535 RecognizerIntent.EXTRA_RESULTS);
536 if (matches.size() > 0) {
537 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700538 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700539 } else {
540 Log.e(TAG, "Voice search - nothing heard");
541 }
542 } else {
543 Log.e(TAG, "Voice search failed");
544 }
545 }
546 super.onActivityResult(requestCode, resultCode, data);
547 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700548
Yorke Leec3766332013-07-31 11:13:16 -0700549 private void showDialpadFragment(boolean animate) {
Andrew Leecdfa6c62014-05-06 15:33:35 -0700550 mDialpadFragment.setAnimate(animate);
551
Chiao Cheng94b10b52012-08-17 16:59:12 -0700552 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700553 if (animate) {
554 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700555 } else {
556 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700557 }
558 ft.show(mDialpadFragment);
559 ft.commit();
560 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561
Yorke Leeca195042013-09-25 16:29:38 -0700562 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700563 if (mDialpadFragment == null) return;
564 if (clearDialpad) {
565 mDialpadFragment.clearDialpad();
566 }
567 if (!mDialpadFragment.isVisible()) return;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700568 mDialpadFragment.setAnimate(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700569 final FragmentTransaction ft = getFragmentManager().beginTransaction();
570 if (animate) {
571 ft.setCustomAnimations(0, R.anim.slide_out);
572 }
573 ft.hide(mDialpadFragment);
574 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700575 }
576
Yorke Leec3766332013-07-31 11:13:16 -0700577 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
578 @Override
579 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800580 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700581 }
582 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700583
Yorke Leeb207f8a2013-08-29 18:51:06 -0700584 private boolean getInSearchUi() {
585 return mInDialpadSearch || mInRegularSearch;
586 }
587
588 private void setNotInSearchUi() {
589 mInDialpadSearch = false;
590 mInRegularSearch = false;
591 }
592
Yorke Lee311969c2013-08-26 06:31:11 -0700593 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700594 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700595 hideDialpadFragment(false, true);
596 }
597
Yorke Lee86e21f72014-04-02 16:49:38 -0700598 /**
599 * Callback from child DialpadFragment when the dialpad is shown.
600 */
601 public void onDialpadShown() {
602 mDialButton.setVisibility(View.VISIBLE);
603 mDialpadButton.setVisibility(View.GONE);
Yorke Leeb0d19762014-03-05 14:43:28 -0800604
Yorke Lee86e21f72014-04-02 16:49:38 -0700605 SearchFragment fragment = null;
606 if (mInDialpadSearch) {
607 fragment = mSmartDialSearchFragment;
608 } else if (mInRegularSearch) {
609 fragment = mRegularSearchFragment;
610 }
611 if (fragment != null && fragment.isVisible()) {
Yorke Leef42a1792014-04-25 16:56:42 -0700612 fragment.getView().animate().translationY(-mActionBarHeight)
Yorke Lee86e21f72014-04-02 16:49:38 -0700613 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
614 }
Christine Chen9c1e0652013-05-23 15:40:19 -0700615
Yorke Lee33932ff2014-04-16 13:34:32 -0700616 if (mListsFragment != null && mListsFragment.isResumed() && mListsFragment.isVisible()) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700617 // If the favorites fragment is showing, fade to blank.
618 mFragmentsFrame.animate().alpha(0.0f);
619 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700620 getActionBar().hide();
Yorke Leecc4660d2014-04-24 11:22:57 -0700621 alignFloatingActionButtonMiddle();
Yorke Leec3766332013-07-31 11:13:16 -0700622 }
623
Yorke Lee86e21f72014-04-02 16:49:38 -0700624 /**
625 * Callback from child DialpadFragment when the dialpad is hidden.
626 */
627 public void onDialpadHidden() {
628 mDialButton.setVisibility(View.GONE);
629 mDialpadButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700630
Yorke Lee86e21f72014-04-02 16:49:38 -0700631 SearchFragment fragment = null;
632 if (mInDialpadSearch) {
633 fragment = mSmartDialSearchFragment;
634 } else if (mInRegularSearch) {
635 fragment = mRegularSearchFragment;
636 }
637 if (fragment != null && fragment.isVisible()) {
Yorke Leef42a1792014-04-25 16:56:42 -0700638 fragment.getView().animate().translationY(0)
Yorke Lee86e21f72014-04-02 16:49:38 -0700639 .setInterpolator(showActionBarInterpolator).setDuration(ANIMATION_DURATION);
Yorke Leec3766332013-07-31 11:13:16 -0700640 }
641
Yorke Leee00c9fe2014-04-12 12:42:06 -0700642 if (mListsFragment != null && mListsFragment.isVisible()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700643 mFragmentsFrame.animate().alpha(1.0f);
644 }
645 getActionBar().show();
Yorke Leecc4660d2014-04-24 11:22:57 -0700646 alignFloatingActionButtonByTab(mCurrentTabPosition);
Yorke Leec3766332013-07-31 11:13:16 -0700647 }
648
Yorke Leee00c9fe2014-04-12 12:42:06 -0700649 private void hideInputMethod(View view) {
650 final InputMethodManager imm = (InputMethodManager) getSystemService(
651 Context.INPUT_METHOD_SERVICE);
652 if (imm != null && view != null) {
653 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
654 }
655 }
656
Yorke Lee53a22302014-04-29 18:13:46 -0700657 private void prepareVoiceSearchButton() {
658 mVoiceSearchButton = getActionBar().getCustomView().findViewById(R.id.voice_search_button);
659 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
660 if (canIntentBeHandled(voiceIntent)) {
661 mVoiceSearchButton.setVisibility(View.VISIBLE);
662 mVoiceSearchButton.setOnClickListener(this);
663 } else {
664 mVoiceSearchButton.setVisibility(View.GONE);
665 }
666 }
667
Yorke Lee86e21f72014-04-02 16:49:38 -0700668 @Override
669 public boolean onCreateOptionsMenu(Menu menu) {
670 if (DEBUG) {
671 Log.d(TAG, "onCreateOptionsMenu");
672 }
673 MenuInflater inflater = getMenuInflater();
674 inflater.inflate(R.menu.dialtacts_options, menu);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700675
676 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700677 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700678 mPendingSearchViewQuery = null;
679 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700680 return super.onCreateOptionsMenu(menu);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700681 }
682
Chiao Cheng94b10b52012-08-17 16:59:12 -0700683 /**
684 * Returns true if the intent is due to hitting the green send key (hardware call button:
685 * KEYCODE_CALL) while in a call.
686 *
687 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700688 * @return true if the intent is due to hitting the green send key while in a call
689 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700690 private boolean isSendKeyWhileInCall(Intent intent) {
691 // If there is a call in progress and the user launched the dialer by hitting the call
692 // button, go straight to the in-call screen.
693 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700694
Yorke Lee62e995c2014-03-28 10:02:56 -0700695 try {
696 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
697 if (callKey && phone != null && phone.showCallScreen()) {
698 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700699 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700700 } catch (RemoteException e) {
701 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700702 }
703
704 return false;
705 }
706
707 /**
708 * Sets the current tab based on the intent's request type
709 *
710 * @param intent Intent that contains information about which tab should be selected
711 */
Yorke Leec3766332013-07-31 11:13:16 -0700712 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700713 // If we got here by hitting send and we're in call forward along to the in-call activity
Yorke Lee62e995c2014-03-28 10:02:56 -0700714 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700715 finish();
716 return;
717 }
718
Yorke Lee669b6082013-09-17 15:43:38 -0700719 if (mDialpadFragment != null) {
720 final boolean phoneIsInUse = phoneIsInUse();
721 if (phoneIsInUse || isDialIntent(intent)) {
722 mDialpadFragment.setStartedFromNewIntent(true);
723 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
724 mInCallDialpadUp = true;
725 }
726 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700727 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729 }
730
731 @Override
732 public void onNewIntent(Intent newIntent) {
733 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700734 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700735
Chiao Cheng94b10b52012-08-17 16:59:12 -0700736 invalidateOptionsMenu();
737 }
738
739 /** Returns true if the given intent contains a phone number to populate the dialer with */
740 private boolean isDialIntent(Intent intent) {
741 final String action = intent.getAction();
742 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
743 return true;
744 }
745 if (Intent.ACTION_VIEW.equals(action)) {
746 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700747 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700748 return true;
749 }
750 }
751 return false;
752 }
753
754 /**
755 * Returns an appropriate call origin for this Activity. May return null when no call origin
756 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
757 * for remembering the tab in which the user made a phone call, so the external app's DIAL
758 * request should not be counted.)
759 */
760 public String getCallOrigin() {
761 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
762 }
763
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 /**
Yorke Leec3766332013-07-31 11:13:16 -0700765 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700766 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700767 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700768 if (getFragmentManager().isDestroyed()) {
769 // Weird race condition where fragment is doing work after the activity is destroyed
770 // due to talkback being on (b/10209937). Just return since we can't do any
771 // constructive here.
772 return;
773 }
774
Yorke Leeef2b7382013-08-09 17:39:25 -0700775 if (DEBUG) {
776 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
777 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700778
Yorke Leec3766332013-07-31 11:13:16 -0700779 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700780
Yorke Leeef2b7382013-08-09 17:39:25 -0700781 SearchFragment fragment;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700782 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700783 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700784 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700785 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700786 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700787
Yorke Leeb207f8a2013-08-29 18:51:06 -0700788 final String tag;
789 if (smartDialSearch) {
790 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
791 } else {
792 tag = TAG_REGULAR_SEARCH_FRAGMENT;
793 }
794 mInDialpadSearch = smartDialSearch;
795 mInRegularSearch = !smartDialSearch;
796
Yorke Leeef2b7382013-08-09 17:39:25 -0700797 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
798 if (fragment == null) {
799 if (smartDialSearch) {
800 fragment = new SmartDialSearchFragment();
801 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700802 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700803 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700804 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700805 // DialtactsActivity will provide the options menu
806 fragment.setHasOptionsMenu(false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700807 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700808 transaction.addToBackStack(null);
809 fragment.setQueryString(query, false);
810 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700811 }
812
813 /**
Yorke Leec3766332013-07-31 11:13:16 -0700814 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700815 */
Yorke Leec3766332013-07-31 11:13:16 -0700816 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700817 // See related bug in enterSearchUI();
818 if (getFragmentManager().isDestroyed()) {
819 return;
820 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700821 // Go all the way back to the favorites fragment, regardless of how many times we
822 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700823 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
824 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700825
826 if (isDialpadShowing()) {
827 mFragmentsFrame.setAlpha(0);
828 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700829 }
830
831 /** Returns an Intent to launch Call Settings screen */
832 public static Intent getCallSettingsIntent() {
833 final Intent intent = new Intent(Intent.ACTION_MAIN);
834 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
835 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
836 return intent;
837 }
838
839 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700840 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700841 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700842 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700843 } else if (getInSearchUi()) {
Yorke Lee53a22302014-04-29 18:13:46 -0700844 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700845 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700846 } else {
847 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700848 }
Yorke Leec3766332013-07-31 11:13:16 -0700849 }
850
851 @Override
852 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700853 if (mSmartDialSearchFragment != null) {
854 mSmartDialSearchFragment.setAddToContactNumber(query);
855 }
Yorke Leec3766332013-07-31 11:13:16 -0700856 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
857 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700858
859 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700860 if (DEBUG) {
861 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
862 }
863 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
864 // This callback can happen if the dialpad fragment is recreated because of
865 // activity destruction. In that case, don't update the search view because
866 // that would bring the user back to the search fragment regardless of the
867 // previous state of the application. Instead, just return here and let the
868 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700869 if (!TextUtils.isEmpty(normalizedQuery)) {
870 mPendingSearchViewQuery = normalizedQuery;
871 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700872 return;
873 }
Yorke Lee53a22302014-04-29 18:13:46 -0700874 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700875 }
876 }
Yorke Leec3766332013-07-31 11:13:16 -0700877
878 @Override
879 public void onListFragmentScrollStateChange(int scrollState) {
880 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700881 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700882 hideInputMethod(getCurrentFocus());
883 }
884 }
885
886 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700887 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
888 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700889 // TODO: No-op for now. This should eventually show/hide the actionBar based on
890 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700891 }
892
Yorke Lee86e21f72014-04-02 16:49:38 -0700893 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800894 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700895 if (mDialButton != null) {
896 mDialButton.setEnabled(enabled);
897 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800898 }
899
900 @Override
901 public void setDialButtonContainerVisible(boolean visible) {
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700902 mFloatingActionButton.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700903 }
904
905 private boolean phoneIsInUse() {
906 final TelephonyManager tm = (TelephonyManager) getSystemService(
907 Context.TELEPHONY_SERVICE);
908 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
909 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700910
Yorke Leee1424812013-09-04 18:24:48 -0700911 public static Intent getAddNumberToContactIntent(CharSequence text) {
912 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
913 intent.putExtra(Intents.Insert.PHONE, text);
914 intent.setType(Contacts.CONTENT_ITEM_TYPE);
915 return intent;
916 }
917
Yorke Leeda0f9042013-12-05 14:25:51 -0800918 private boolean canIntentBeHandled(Intent intent) {
919 final PackageManager packageManager = getPackageManager();
920 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
921 PackageManager.MATCH_DEFAULT_ONLY);
922 return resolveInfo != null && resolveInfo.size() > 0;
923 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800924
Yorke Lee6a1461a2014-04-21 16:27:14 -0700925 @Override
926 public void showCallHistory() {
927 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
928 // CONTENT_TYPE, so that we always open our call log from our dialer
929 final Intent intent = new Intent(this, CallLogActivity.class);
930 startActivity(intent);
931 }
932
Yorke Lee86e21f72014-04-02 16:49:38 -0700933 /**
934 * Called when the user has long-pressed a contact tile to start a drag operation.
935 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800936 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700937 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700938 getActionBar().hide();
939 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800940 }
941
942 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700943 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {}
Yorke Lee3cefcc62014-01-16 11:26:46 -0800944
Yorke Lee86e21f72014-04-02 16:49:38 -0700945 /**
946 * Called when the user has released a contact tile after long-pressing it.
947 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800948 @Override
949 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700950 getActionBar().show();
951 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800952 }
953
954 @Override
955 public void onDroppedOnRemove() {}
956
957 /**
Yorke Leed999b712014-04-23 15:10:58 -0700958 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800959 * once it has been attached to the activity.
960 */
961 @Override
962 public void setDragDropController(DragDropController dragController) {
963 mRemoveViewContainer.setDragDropController(dragController);
964 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700965
966 @Override
967 public void onPickPhoneNumberAction(Uri dataUri) {
968 mPhoneNumberPickerActionListener.onPickPhoneNumberAction(dataUri);
969 }
970
971 @Override
972 public void onCallNumberDirectly(String phoneNumber) {
973 mPhoneNumberPickerActionListener.onCallNumberDirectly(phoneNumber);
974 }
975
976 @Override
977 public void onShortcutIntentCreated(Intent intent) {
978 mPhoneNumberPickerActionListener.onShortcutIntentCreated(intent);
979 }
980
981 @Override
982 public void onHomeInActionBarSelected() {
983 mPhoneNumberPickerActionListener.onHomeInActionBarSelected();
984 }
Yorke Lee33932ff2014-04-16 13:34:32 -0700985
986 public int getActionBarHeight() {
987 return mActionBarHeight;
988 }
Yorke Leecc4660d2014-04-24 11:22:57 -0700989
990 @Override
991 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
992
993 }
994
995 @Override
996 public void onPageSelected(int position) {
997 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -0700998 // If the dialpad is showing, the floating action button should always be middle aligned.
999 if (!isDialpadShowing()) {
1000 alignFloatingActionButtonByTab(mCurrentTabPosition);
1001 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001002 }
1003
1004 @Override
1005 public void onPageScrollStateChanged(int state) {
1006 }
1007
1008 private void alignFloatingActionButtonByTab(int position) {
1009 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1010 alignFloatingActionButtonMiddle();
1011 } else {
1012 alignFloatingActionButtonRight();
1013 }
1014 }
1015
1016 private void alignFloatingActionButtonRight() {
1017 final RelativeLayout.LayoutParams params =
1018 (RelativeLayout.LayoutParams) mFloatingActionButton.getLayoutParams();
1019 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1020 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1021 mFloatingActionButton.setLayoutParams(params);
1022 }
1023
1024 private void alignFloatingActionButtonMiddle() {
1025 final RelativeLayout.LayoutParams params =
1026 (RelativeLayout.LayoutParams) mFloatingActionButton.getLayoutParams();
1027 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1028 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
1029 mFloatingActionButton.setLayoutParams(params);
1030 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001031}