blob: ce71245314d7e01bcf5b4517760e30227f1a3191 [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;
Yorke Lee86e21f72014-04-02 16:49:38 -070059import android.widget.SearchView;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080060import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070061
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070062import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070063import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070064import com.android.contacts.common.dialog.ClearFrequentsDialog;
65import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080066import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070067import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070068import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070069import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070070import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070072import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070073import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080074import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070075import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080076import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070077import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070078import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070079import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070080import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080081import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070082import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070083import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070084import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070085import com.android.internal.telephony.ITelephony;
86
Yorke Leec3766332013-07-31 11:13:16 -070087import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080088import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070089
Chiao Cheng94b10b52012-08-17 16:59:12 -070090/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070091 * The dialer tab's title is 'phone', a more common name (see strings.xml).
92 */
Yorke Leec3766332013-07-31 11:13:16 -070093public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070094 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -070095 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080096 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -070097 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -070098 SpeedDialFragment.HostInterface,
Yorke Leee00c9fe2014-04-12 12:42:06 -070099 OnDragDropListener, View.OnLongClickListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700100 OnPhoneNumberPickerActionListener,
101 ViewPager.OnPageChangeListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700102 private static final String TAG = "DialtactsActivity";
103
Ihab Awad526c0b82014-02-27 12:55:36 -0800104 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700105
Yorke Leef74011e2013-08-02 11:30:30 -0700106 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
107
Chiao Cheng94b10b52012-08-17 16:59:12 -0700108 /** Used to open Call Setting */
109 private static final String PHONE_PACKAGE = "com.android.phone";
110 private static final String CALL_SETTINGS_CLASS_NAME =
111 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700112 /** @see #getCallOrigin() */
113 private static final String CALL_ORIGIN_DIALTACTS =
114 "com.android.dialer.DialtactsActivity";
115
Yorke Leeb207f8a2013-08-29 18:51:06 -0700116 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
117 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700118 private static final String KEY_SEARCH_QUERY = "search_query";
119 private static final String KEY_FIRST_LAUNCH = "first_launch";
120
Yorke Leec3766332013-07-31 11:13:16 -0700121 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
122 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
123 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
124 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700125
Chiao Cheng94b10b52012-08-17 16:59:12 -0700126 /**
127 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
128 */
129 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
130
Yorke Leec3766332013-07-31 11:13:16 -0700131 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700132
Yorke Lee86e21f72014-04-02 16:49:38 -0700133 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800134
Yorke Leec3766332013-07-31 11:13:16 -0700135 /**
Yorke Leec3766332013-07-31 11:13:16 -0700136 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700137 */
Yorke Leef74011e2013-08-02 11:30:30 -0700138 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700139
140 /**
141 * Fragment for searching phone numbers using the alphanumeric keyboard.
142 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700143 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700144
145 /**
146 * Fragment for searching phone numbers using the dialpad.
147 */
148 private SmartDialSearchFragment mSmartDialSearchFragment;
149
Yorke Leee00c9fe2014-04-12 12:42:06 -0700150 /**
151 * Fragment containing the speed dial list, recents list, and all contacts list.
152 */
153 private ListsFragment mListsFragment;
154
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700155 private View mFloatingActionButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700156 private View mMenuButton;
Yorke Leec3766332013-07-31 11:13:16 -0700157 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800158 private View mDialButton;
Yorke Leec3766332013-07-31 11:13:16 -0700159
Yorke Leeb207f8a2013-08-29 18:51:06 -0700160 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700161
Yorke Lee33932ff2014-04-16 13:34:32 -0700162 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700163 private boolean mInDialpadSearch;
164 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700165 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700166
Yorke Leeef2b7382013-08-09 17:39:25 -0700167 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700168 * The position of the currently selected tab in the attached {@link ListsFragment}.
169 */
170 private int mCurrentTabPosition = 0;
171
172 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700173 * True if the dialpad is only temporarily showing due to being in call
174 */
175 private boolean mInCallDialpadUp;
176
177 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700178 * True when this activity has been launched for the first time.
179 */
Yorke Lee98702de2013-08-06 12:03:32 -0700180 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700181
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700182 /**
183 * Search query to be applied to the SearchView in the ActionBar once
184 * onCreateOptionsMenu has been called.
185 */
186 private String mPendingSearchViewQuery;
187
Yorke Lee94f49042014-03-03 14:03:08 -0800188 // This view points to the Framelayout that houses both the search view and remove view
189 // containers.
190 private View mSearchAndRemoveViewContainer;
Yorke Lee53a22302014-04-29 18:13:46 -0700191 private EditText mSearchView;
192 private View mSearchViewCloseButton;
193 private View mVoiceSearchButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700194 /**
195 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
196 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
197 * removed from the speed dial list.
198 */
199 private RemoveView mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700200
Yorke Lee86e21f72014-04-02 16:49:38 -0700201 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
202 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700203 private String mSearchQuery;
204
Yorke Leefce269a2013-08-12 11:56:04 -0700205 private DialerDatabaseHelper mDialerDatabaseHelper;
206
Yorke Leee00c9fe2014-04-12 12:42:06 -0700207 private class OverflowPopupMenu extends PopupMenu {
208 public OverflowPopupMenu(Context context, View anchor) {
209 super(context, anchor);
210 }
211
212 @Override
213 public void show() {
214 final Menu menu = getMenu();
215 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Leed999b712014-04-23 15:10:58 -0700216 // TODO: Check mSpeedDialFragment.hasFrequents()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700217 clearFrequents.setVisible(true);
218 super.show();
219 }
220 }
221
Chiao Cheng94b10b52012-08-17 16:59:12 -0700222 /**
223 * Listener used when one of phone numbers in search UI is selected. This will initiate a
224 * phone call using the phone number.
225 */
226 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
227 new OnPhoneNumberPickerActionListener() {
228 @Override
229 public void onPickPhoneNumberAction(Uri dataUri) {
230 // Specify call-origin so that users will see the previous tab instead of
231 // CallLog screen (search UI will be automatically exited).
232 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700233 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700234 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700235 }
236
237 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700238 public void onCallNumberDirectly(String phoneNumber) {
239 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
240 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700241 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700242 }
243
244 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700245 public void onShortcutIntentCreated(Intent intent) {
246 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
247 }
248
249 @Override
250 public void onHomeInActionBarSelected() {
251 exitSearchUi();
252 }
253 };
254
255 /**
256 * Listener used to send search queries to the phone search fragment.
257 */
Yorke Lee53a22302014-04-29 18:13:46 -0700258 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Yorke Leec3766332013-07-31 11:13:16 -0700259 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700260 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Yorke Leec3766332013-07-31 11:13:16 -0700261 }
262
263 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700264 public void onTextChanged(CharSequence s, int start, int before, int count) {
265 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700266 if (newText.equals(mSearchQuery)) {
267 // If the query hasn't changed (perhaps due to activity being destroyed
268 // and restored, or user launching the same DIAL intent twice), then there is
269 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700270 return;
Yorke Leeef2b7382013-08-09 17:39:25 -0700271 }
272 mSearchQuery = newText;
273 if (DEBUG) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700274 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leeef2b7382013-08-09 17:39:25 -0700275 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700276 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700277
Yorke Leec3766332013-07-31 11:13:16 -0700278 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700279 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700280 exitSearchUi();
Yorke Lee53a22302014-04-29 18:13:46 -0700281 mSearchViewCloseButton.setVisibility(View.GONE);
282 mVoiceSearchButton.setVisibility(View.VISIBLE);
283 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700284 } else if (!TextUtils.isEmpty(newText)) {
285 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
286 (!dialpadSearch && mInRegularSearch);
287 if (!sameSearchMode) {
288 // call enterSearchUi only if we are switching search modes, or entering
289 // search ui for the first time
290 enterSearchUi(dialpadSearch, newText);
291 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700292
Yorke Leeb207f8a2013-08-29 18:51:06 -0700293 if (dialpadSearch && mSmartDialSearchFragment != null) {
294 mSmartDialSearchFragment.setQueryString(newText, false);
295 } else if (mRegularSearchFragment != null) {
296 mRegularSearchFragment.setQueryString(newText, false);
297 }
Yorke Lee53a22302014-04-29 18:13:46 -0700298 mSearchViewCloseButton.setVisibility(View.VISIBLE);
299 mVoiceSearchButton.setVisibility(View.GONE);
300 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700301 }
Yorke Lee53a22302014-04-29 18:13:46 -0700302 return;
303 }
304
305 @Override
306 public void afterTextChanged(Editable s) {
Yorke Leec3766332013-07-31 11:13:16 -0700307 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700308 };
309
Yorke Leec3766332013-07-31 11:13:16 -0700310 private boolean isDialpadShowing() {
311 return mDialpadFragment != null && mDialpadFragment.isVisible();
312 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700313
314 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700315 protected void onCreate(Bundle savedInstanceState) {
316 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700317 mFirstLaunch = true;
318
Yorke Lee8898cd02013-08-08 10:24:27 -0700319 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800320 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700321
Yorke Lee53a22302014-04-29 18:13:46 -0700322 final ActionBar actionBar = getActionBar();
323 actionBar.setCustomView(R.layout.search_edittext);
324 actionBar.setDisplayShowCustomEnabled(true);
325
326 final View customView = actionBar.getCustomView();
327
328 mSearchView = (EditText) customView.findViewById(R.id.search_view);
329 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
330
331 mSearchViewCloseButton = customView.findViewById(R.id.search_close_button);
332 mSearchViewCloseButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700333
Yorke Lee33932ff2014-04-16 13:34:32 -0700334 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
335 new int[] { android.R.attr.actionBarSize });
336 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
337 styledAttributes.recycle();
338
Yorke Leeef2b7382013-08-09 17:39:25 -0700339 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
340 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700341 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800342 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700343 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800344 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
345 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700346 } else {
347 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700348 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
349 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700350 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700351 }
352
Yorke Leecc4660d2014-04-24 11:22:57 -0700353 RelativeLayout parent = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
354 parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
355
Yorke Leeb207f8a2013-08-29 18:51:06 -0700356 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee86e21f72014-04-02 16:49:38 -0700357
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700358 mFloatingActionButton = findViewById(R.id.floating_action_button);
Andrew Lee4cbc9732014-04-24 14:38:58 -0700359 ViewUtil.setupFloatingActionButton(mFloatingActionButton, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700360
Yorke Lee86e21f72014-04-02 16:49:38 -0700361 mDialButton = findViewById(R.id.dial_button);
362 mDialButton.setOnClickListener(this);
363 mDialpadButton = findViewById(R.id.dialpad_button);
364 mDialpadButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800365
Yorke Lee3cefcc62014-01-16 11:26:46 -0800366 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800367 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
368
Yorke Lee0baa98b2013-08-22 10:55:16 -0700369 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700370 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700371 }
372
373 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700374 protected void onResume() {
375 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700376 if (mFirstLaunch) {
377 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700378 } else if (!phoneIsInUse() && mInCallDialpadUp) {
379 hideDialpadFragment(false, true);
380 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700381 }
382 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700383 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700384 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700385 }
386
387 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700388 protected void onPause() {
389 if (mClearSearchOnPause) {
390 hideDialpadAndSearchUi();
391 mClearSearchOnPause = false;
392 }
393 super.onPause();
394 }
395
396 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700397 protected void onSaveInstanceState(Bundle outState) {
398 super.onSaveInstanceState(outState);
399 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700400 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
401 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700402 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
403 }
404
405 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700406 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700407 if (fragment instanceof DialpadFragment) {
408 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700409 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700410 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700411 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700412 } else if (fragment instanceof SmartDialSearchFragment) {
413 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700414 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
415 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700416 if (mFragmentsFrame != null) {
417 mFragmentsFrame.setAlpha(1.0f);
418 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700419 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700420 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700421 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
422 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700423 if (mFragmentsFrame != null) {
424 mFragmentsFrame.setAlpha(1.0f);
425 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700426 } else if (fragment instanceof ListsFragment) {
427 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700428 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700429 }
Yorke Leec3766332013-07-31 11:13:16 -0700430 }
431
Alon Albertb453e5b2013-09-10 15:54:23 -0700432 protected void handleMenuSettings() {
433 openTelephonySetting(this);
434 }
435
436 public static void openTelephonySetting(Activity activity) {
437 final Intent settingsIntent = getCallSettingsIntent();
438 activity.startActivity(settingsIntent);
439 }
440
Chiao Cheng94b10b52012-08-17 16:59:12 -0700441 @Override
442 public void onClick(View view) {
443 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700444 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700445 // Reset the boolean flag that tracks whether the dialpad was up because
446 // we were in call. Regardless of whether it was true before, we want to
447 // show the dialpad because the user has explicitly clicked the dialpad
448 // button.
449 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700450 showDialpadFragment(true);
451 break;
Yorke Lee86e21f72014-04-02 16:49:38 -0700452 case R.id.dial_button:
453 // Dial button was pressed; tell the Dialpad fragment
454 mDialpadFragment.dialButtonPressed();
455 break;
Yorke Leec3766332013-07-31 11:13:16 -0700456 case R.id.search_close_button:
457 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700458 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700459 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700460 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700461 }
462 break;
Yorke Leec3766332013-07-31 11:13:16 -0700463 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800464 try {
465 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
466 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
467 } catch (ActivityNotFoundException e) {
468 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
469 Toast.LENGTH_SHORT).show();
470 }
Yorke Leec3766332013-07-31 11:13:16 -0700471 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700472 default: {
473 Log.wtf(TAG, "Unexpected onClick event from " + view);
474 break;
475 }
476 }
477 }
478
Yorke Leec3766332013-07-31 11:13:16 -0700479 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700480 public boolean onOptionsItemSelected(MenuItem item) {
481 switch (item.getItemId()) {
482 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700483 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700484 break;
485 case R.id.menu_add_contact:
486 try {
487 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
488 } catch (ActivityNotFoundException e) {
489 Toast toast = Toast.makeText(this,
490 R.string.add_contact_not_available,
491 Toast.LENGTH_SHORT);
492 toast.show();
493 }
494 break;
495 case R.id.menu_import_export:
496 // We hard-code the "contactsAreAvailable" argument because doing it properly would
497 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
498 // now in Dialtacts for (potential) performance reasons. Compare with how it is
499 // done in {@link PeopleActivity}.
500 ImportExportDialogFragment.show(getFragmentManager(), true,
501 DialtactsActivity.class);
502 return true;
503 case R.id.menu_clear_frequents:
504 // TODO: This should be enabled/disabled based on
Yorke Leed999b712014-04-23 15:10:58 -0700505 // SpeedDialFragment.hasFrequents
Yorke Lee86e21f72014-04-02 16:49:38 -0700506 ClearFrequentsDialog.show(getFragmentManager());
507 return true;
508 case R.id.menu_call_settings:
509 handleMenuSettings();
510 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700511 }
512 return false;
513 }
514
515 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800516 public boolean onLongClick(View view) {
517 switch (view.getId()) {
518 case R.id.dial_button: {
519 // Dial button was pressed; tell the Dialpad fragment
520 mDialpadFragment.dialButtonPressed();
521 return true; // Consume the event
522 }
523 default: {
524 Log.wtf(TAG, "Unexpected onClick event from " + view);
525 break;
526 }
527 }
528 return false;
529 }
530
531 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700532 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
533 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
534 if (resultCode == RESULT_OK) {
535 final ArrayList<String> matches = data.getStringArrayListExtra(
536 RecognizerIntent.EXTRA_RESULTS);
537 if (matches.size() > 0) {
538 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700539 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700540 } else {
541 Log.e(TAG, "Voice search - nothing heard");
542 }
543 } else {
544 Log.e(TAG, "Voice search failed");
545 }
546 }
547 super.onActivityResult(requestCode, resultCode, data);
548 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700549
Yorke Leec3766332013-07-31 11:13:16 -0700550 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700551 mDialpadFragment.setAdjustTranslationForAnimation(animate);
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;
Yorke Leeea695cb2013-08-09 11:02:26 -0700568 mDialpadFragment.setAdjustTranslationForAnimation(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}