blob: b965530746cd36471a8ba122374f044354b0f95d [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;
Yorke Lee28266192014-05-01 10:05:52 -070047import android.view.DragEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.Menu;
Yorke Lee86e21f72014-04-02 16:49:38 -070049import android.view.MenuInflater;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070052import android.view.View.OnDragListener;
Yorke Lee86e21f72014-04-02 16:49:38 -070053import android.view.animation.AccelerateInterpolator;
54import android.view.animation.DecelerateInterpolator;
55import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070056import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070057import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070058import android.widget.EditText;
Chiao Cheng94b10b52012-08-17 16:59:12 -070059import android.widget.PopupMenu;
Yorke Leecc4660d2014-04-24 11:22:57 -070060import android.widget.RelativeLayout;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080061import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070062
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070063import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070064import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070065import com.android.contacts.common.dialog.ClearFrequentsDialog;
66import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080067import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070068import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070069import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070070import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070071import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070072import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070073import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070074import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080075import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070076import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080077import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070078import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070079import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070080import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070081import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080082import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070083import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070084import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070085import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070086import com.android.internal.telephony.ITelephony;
87
Yorke Leec3766332013-07-31 11:13:16 -070088import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080089import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070090
Chiao Cheng94b10b52012-08-17 16:59:12 -070091/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070092 * The dialer tab's title is 'phone', a more common name (see strings.xml).
93 */
Yorke Leec3766332013-07-31 11:13:16 -070094public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070095 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -070096 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080097 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -070098 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -070099 SpeedDialFragment.HostInterface,
Yorke Leee00c9fe2014-04-12 12:42:06 -0700100 OnDragDropListener, View.OnLongClickListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700101 OnPhoneNumberPickerActionListener,
102 ViewPager.OnPageChangeListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103 private static final String TAG = "DialtactsActivity";
104
Ihab Awad526c0b82014-02-27 12:55:36 -0800105 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700106
Yorke Leef74011e2013-08-02 11:30:30 -0700107 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
108
Chiao Cheng94b10b52012-08-17 16:59:12 -0700109 /** Used to open Call Setting */
110 private static final String PHONE_PACKAGE = "com.android.phone";
111 private static final String CALL_SETTINGS_CLASS_NAME =
112 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 /** @see #getCallOrigin() */
114 private static final String CALL_ORIGIN_DIALTACTS =
115 "com.android.dialer.DialtactsActivity";
116
Yorke Leeb207f8a2013-08-29 18:51:06 -0700117 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
118 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700119 private static final String KEY_SEARCH_QUERY = "search_query";
120 private static final String KEY_FIRST_LAUNCH = "first_launch";
121
Yorke Leec3766332013-07-31 11:13:16 -0700122 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
123 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
124 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
125 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700126
Chiao Cheng94b10b52012-08-17 16:59:12 -0700127 /**
128 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
129 */
130 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
131
Yorke Leec3766332013-07-31 11:13:16 -0700132 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700133
Yorke Lee86e21f72014-04-02 16:49:38 -0700134 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800135
Yorke Leec3766332013-07-31 11:13:16 -0700136 /**
Yorke Leec3766332013-07-31 11:13:16 -0700137 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700138 */
Yorke Leef74011e2013-08-02 11:30:30 -0700139 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700140
141 /**
142 * Fragment for searching phone numbers using the alphanumeric keyboard.
143 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700144 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700145
146 /**
147 * Fragment for searching phone numbers using the dialpad.
148 */
149 private SmartDialSearchFragment mSmartDialSearchFragment;
150
Yorke Leee00c9fe2014-04-12 12:42:06 -0700151 /**
152 * Fragment containing the speed dial list, recents list, and all contacts list.
153 */
154 private ListsFragment mListsFragment;
155
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700156 private View mFloatingActionButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700157 private View mMenuButton;
Yorke Leec3766332013-07-31 11:13:16 -0700158 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800159 private View mDialButton;
Yorke Leec3766332013-07-31 11:13:16 -0700160
Yorke Leeb207f8a2013-08-29 18:51:06 -0700161 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700162
Yorke Lee33932ff2014-04-16 13:34:32 -0700163 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700164 private boolean mInDialpadSearch;
165 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700166 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700167
Yorke Leeef2b7382013-08-09 17:39:25 -0700168 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700169 * The position of the currently selected tab in the attached {@link ListsFragment}.
170 */
171 private int mCurrentTabPosition = 0;
172
173 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700174 * True if the dialpad is only temporarily showing due to being in call
175 */
176 private boolean mInCallDialpadUp;
177
178 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700179 * True when this activity has been launched for the first time.
180 */
Yorke Lee98702de2013-08-06 12:03:32 -0700181 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700182
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700183 /**
184 * Search query to be applied to the SearchView in the ActionBar once
185 * onCreateOptionsMenu has been called.
186 */
187 private String mPendingSearchViewQuery;
188
Yorke Lee53a22302014-04-29 18:13:46 -0700189 private EditText mSearchView;
190 private View mSearchViewCloseButton;
191 private View mVoiceSearchButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700192 /**
193 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
194 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
195 * removed from the speed dial list.
196 */
Yorke Lee28266192014-05-01 10:05:52 -0700197 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700198
Yorke Lee86e21f72014-04-02 16:49:38 -0700199 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
200 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700201 private String mSearchQuery;
202
Yorke Leefce269a2013-08-12 11:56:04 -0700203 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700204 private DragDropController mDragDropController;
Yorke Leefce269a2013-08-12 11:56:04 -0700205
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 /**
Yorke Lee28266192014-05-01 10:05:52 -0700222 * Listener that listens to drag events and sends their x and y coordinates to a
223 * {@link DragDropController}.
224 */
225 private class LayoutOnDragListener implements OnDragListener {
226 @Override
227 public boolean onDrag(View v, DragEvent event) {
228 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
229 mDragDropController.handleDragHovered(v, (int) event.getX(),
230 (int) event.getY());
231 }
232 return true;
233 }
234 }
235
236 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700237 * Listener used when one of phone numbers in search UI is selected. This will initiate a
238 * phone call using the phone number.
239 */
240 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
241 new OnPhoneNumberPickerActionListener() {
242 @Override
243 public void onPickPhoneNumberAction(Uri dataUri) {
244 // Specify call-origin so that users will see the previous tab instead of
245 // CallLog screen (search UI will be automatically exited).
246 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700247 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700248 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700249 }
250
251 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700252 public void onCallNumberDirectly(String phoneNumber) {
253 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
254 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700255 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700256 }
257
258 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700259 public void onShortcutIntentCreated(Intent intent) {
260 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
261 }
262
263 @Override
264 public void onHomeInActionBarSelected() {
265 exitSearchUi();
266 }
267 };
268
269 /**
270 * Listener used to send search queries to the phone search fragment.
271 */
Yorke Lee53a22302014-04-29 18:13:46 -0700272 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Yorke Leec3766332013-07-31 11:13:16 -0700273 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700274 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Yorke Leec3766332013-07-31 11:13:16 -0700275 }
276
277 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700278 public void onTextChanged(CharSequence s, int start, int before, int count) {
279 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700280 if (newText.equals(mSearchQuery)) {
281 // If the query hasn't changed (perhaps due to activity being destroyed
282 // and restored, or user launching the same DIAL intent twice), then there is
283 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700284 return;
Yorke Leeef2b7382013-08-09 17:39:25 -0700285 }
286 mSearchQuery = newText;
287 if (DEBUG) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700288 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leeef2b7382013-08-09 17:39:25 -0700289 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700290 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700291
Yorke Leec3766332013-07-31 11:13:16 -0700292 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700293 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700294 exitSearchUi();
Yorke Lee53a22302014-04-29 18:13:46 -0700295 mSearchViewCloseButton.setVisibility(View.GONE);
296 mVoiceSearchButton.setVisibility(View.VISIBLE);
297 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700298 } else if (!TextUtils.isEmpty(newText)) {
299 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
300 (!dialpadSearch && mInRegularSearch);
301 if (!sameSearchMode) {
302 // call enterSearchUi only if we are switching search modes, or entering
303 // search ui for the first time
304 enterSearchUi(dialpadSearch, newText);
305 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700306
Yorke Leeb207f8a2013-08-29 18:51:06 -0700307 if (dialpadSearch && mSmartDialSearchFragment != null) {
308 mSmartDialSearchFragment.setQueryString(newText, false);
309 } else if (mRegularSearchFragment != null) {
310 mRegularSearchFragment.setQueryString(newText, false);
311 }
Yorke Lee53a22302014-04-29 18:13:46 -0700312 mSearchViewCloseButton.setVisibility(View.VISIBLE);
313 mVoiceSearchButton.setVisibility(View.GONE);
314 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700315 }
Yorke Lee53a22302014-04-29 18:13:46 -0700316 return;
317 }
318
319 @Override
320 public void afterTextChanged(Editable s) {
Yorke Leec3766332013-07-31 11:13:16 -0700321 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700322 };
323
Yorke Leec3766332013-07-31 11:13:16 -0700324 private boolean isDialpadShowing() {
325 return mDialpadFragment != null && mDialpadFragment.isVisible();
326 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700327
328 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700329 protected void onCreate(Bundle savedInstanceState) {
330 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700331 mFirstLaunch = true;
332
Yorke Lee8898cd02013-08-08 10:24:27 -0700333 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800334 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700335
Yorke Lee53a22302014-04-29 18:13:46 -0700336 final ActionBar actionBar = getActionBar();
337 actionBar.setCustomView(R.layout.search_edittext);
338 actionBar.setDisplayShowCustomEnabled(true);
339
340 final View customView = actionBar.getCustomView();
341
342 mSearchView = (EditText) customView.findViewById(R.id.search_view);
343 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
344
345 mSearchViewCloseButton = customView.findViewById(R.id.search_close_button);
346 mSearchViewCloseButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700347
Yorke Lee33932ff2014-04-16 13:34:32 -0700348 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
349 new int[] { android.R.attr.actionBarSize });
350 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
351 styledAttributes.recycle();
352
Yorke Leeef2b7382013-08-09 17:39:25 -0700353 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
354 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700355 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800356 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700357 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800358 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
359 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700360 } else {
361 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700362 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
363 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700364 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700365 }
366
Yorke Leecc4660d2014-04-24 11:22:57 -0700367 RelativeLayout parent = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
368 parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
369
Yorke Leeb207f8a2013-08-29 18:51:06 -0700370 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee86e21f72014-04-02 16:49:38 -0700371
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700372 mFloatingActionButton = findViewById(R.id.floating_action_button);
Andrew Lee4cbc9732014-04-24 14:38:58 -0700373 ViewUtil.setupFloatingActionButton(mFloatingActionButton, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700374
Yorke Lee86e21f72014-04-02 16:49:38 -0700375 mDialButton = findViewById(R.id.dial_button);
376 mDialButton.setOnClickListener(this);
377 mDialpadButton = findViewById(R.id.dialpad_button);
378 mDialpadButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800379
Yorke Lee28266192014-05-01 10:05:52 -0700380 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800381
Yorke Lee0baa98b2013-08-22 10:55:16 -0700382 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700383 SmartDialPrefix.initializeNanpSettings(this);
Yorke Lee28266192014-05-01 10:05:52 -0700384
385 findViewById(R.id.dialtacts_mainlayout).setOnDragListener(new LayoutOnDragListener());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700386 }
387
388 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700389 protected void onResume() {
390 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700391 if (mFirstLaunch) {
392 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700393 } else if (!phoneIsInUse() && mInCallDialpadUp) {
394 hideDialpadFragment(false, true);
395 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700396 }
397 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700398 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700399 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700400 }
401
402 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700403 protected void onPause() {
404 if (mClearSearchOnPause) {
405 hideDialpadAndSearchUi();
406 mClearSearchOnPause = false;
407 }
408 super.onPause();
409 }
410
411 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700412 protected void onSaveInstanceState(Bundle outState) {
413 super.onSaveInstanceState(outState);
414 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700415 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
416 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700417 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
418 }
419
420 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700421 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700422 if (fragment instanceof DialpadFragment) {
423 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700424 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700425 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700426 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700427 } else if (fragment instanceof SmartDialSearchFragment) {
428 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700429 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
430 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700431 if (mFragmentsFrame != null) {
432 mFragmentsFrame.setAlpha(1.0f);
433 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700434 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700435 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700436 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
437 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700438 if (mFragmentsFrame != null) {
439 mFragmentsFrame.setAlpha(1.0f);
440 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700441 } else if (fragment instanceof ListsFragment) {
442 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700443 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700444 }
Yorke Leec3766332013-07-31 11:13:16 -0700445 }
446
Alon Albertb453e5b2013-09-10 15:54:23 -0700447 protected void handleMenuSettings() {
448 openTelephonySetting(this);
449 }
450
451 public static void openTelephonySetting(Activity activity) {
452 final Intent settingsIntent = getCallSettingsIntent();
453 activity.startActivity(settingsIntent);
454 }
455
Chiao Cheng94b10b52012-08-17 16:59:12 -0700456 @Override
457 public void onClick(View view) {
458 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700459 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700460 // Reset the boolean flag that tracks whether the dialpad was up because
461 // we were in call. Regardless of whether it was true before, we want to
462 // show the dialpad because the user has explicitly clicked the dialpad
463 // button.
464 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700465 showDialpadFragment(true);
466 break;
Yorke Lee86e21f72014-04-02 16:49:38 -0700467 case R.id.dial_button:
468 // Dial button was pressed; tell the Dialpad fragment
469 mDialpadFragment.dialButtonPressed();
470 break;
Yorke Leec3766332013-07-31 11:13:16 -0700471 case R.id.search_close_button:
472 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700473 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700474 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700475 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700476 }
477 break;
Yorke Leec3766332013-07-31 11:13:16 -0700478 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800479 try {
480 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
481 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
482 } catch (ActivityNotFoundException e) {
483 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
484 Toast.LENGTH_SHORT).show();
485 }
Yorke Leec3766332013-07-31 11:13:16 -0700486 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700487 default: {
488 Log.wtf(TAG, "Unexpected onClick event from " + view);
489 break;
490 }
491 }
492 }
493
Yorke Leec3766332013-07-31 11:13:16 -0700494 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700495 public boolean onOptionsItemSelected(MenuItem item) {
496 switch (item.getItemId()) {
497 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700498 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700499 break;
500 case R.id.menu_add_contact:
501 try {
502 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
503 } catch (ActivityNotFoundException e) {
504 Toast toast = Toast.makeText(this,
505 R.string.add_contact_not_available,
506 Toast.LENGTH_SHORT);
507 toast.show();
508 }
509 break;
510 case R.id.menu_import_export:
511 // We hard-code the "contactsAreAvailable" argument because doing it properly would
512 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
513 // now in Dialtacts for (potential) performance reasons. Compare with how it is
514 // done in {@link PeopleActivity}.
515 ImportExportDialogFragment.show(getFragmentManager(), true,
516 DialtactsActivity.class);
517 return true;
518 case R.id.menu_clear_frequents:
519 // TODO: This should be enabled/disabled based on
Yorke Leed999b712014-04-23 15:10:58 -0700520 // SpeedDialFragment.hasFrequents
Yorke Lee86e21f72014-04-02 16:49:38 -0700521 ClearFrequentsDialog.show(getFragmentManager());
522 return true;
523 case R.id.menu_call_settings:
524 handleMenuSettings();
525 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700526 }
527 return false;
528 }
529
530 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800531 public boolean onLongClick(View view) {
532 switch (view.getId()) {
533 case R.id.dial_button: {
534 // Dial button was pressed; tell the Dialpad fragment
535 mDialpadFragment.dialButtonPressed();
536 return true; // Consume the event
537 }
538 default: {
539 Log.wtf(TAG, "Unexpected onClick event from " + view);
540 break;
541 }
542 }
543 return false;
544 }
545
546 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700547 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
548 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
549 if (resultCode == RESULT_OK) {
550 final ArrayList<String> matches = data.getStringArrayListExtra(
551 RecognizerIntent.EXTRA_RESULTS);
552 if (matches.size() > 0) {
553 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700554 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700555 } else {
556 Log.e(TAG, "Voice search - nothing heard");
557 }
558 } else {
559 Log.e(TAG, "Voice search failed");
560 }
561 }
562 super.onActivityResult(requestCode, resultCode, data);
563 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700564
Yorke Leec3766332013-07-31 11:13:16 -0700565 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700566 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700567 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700568 if (animate) {
569 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700570 } else {
571 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700572 }
573 ft.show(mDialpadFragment);
574 ft.commit();
575 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700576
Yorke Leeca195042013-09-25 16:29:38 -0700577 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700578 if (mDialpadFragment == null) return;
579 if (clearDialpad) {
580 mDialpadFragment.clearDialpad();
581 }
582 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700583 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700584 final FragmentTransaction ft = getFragmentManager().beginTransaction();
585 if (animate) {
586 ft.setCustomAnimations(0, R.anim.slide_out);
587 }
588 ft.hide(mDialpadFragment);
589 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700590 }
591
Yorke Leeb207f8a2013-08-29 18:51:06 -0700592 private boolean getInSearchUi() {
593 return mInDialpadSearch || mInRegularSearch;
594 }
595
596 private void setNotInSearchUi() {
597 mInDialpadSearch = false;
598 mInRegularSearch = false;
599 }
600
Yorke Lee311969c2013-08-26 06:31:11 -0700601 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700602 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700603 hideDialpadFragment(false, true);
604 }
605
Yorke Lee86e21f72014-04-02 16:49:38 -0700606 /**
607 * Callback from child DialpadFragment when the dialpad is shown.
608 */
609 public void onDialpadShown() {
610 mDialButton.setVisibility(View.VISIBLE);
611 mDialpadButton.setVisibility(View.GONE);
Yorke Leeb0d19762014-03-05 14:43:28 -0800612
Yorke Lee86e21f72014-04-02 16:49:38 -0700613 SearchFragment fragment = null;
614 if (mInDialpadSearch) {
615 fragment = mSmartDialSearchFragment;
616 } else if (mInRegularSearch) {
617 fragment = mRegularSearchFragment;
618 }
619 if (fragment != null && fragment.isVisible()) {
Yorke Leef42a1792014-04-25 16:56:42 -0700620 fragment.getView().animate().translationY(-mActionBarHeight)
Yorke Lee86e21f72014-04-02 16:49:38 -0700621 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
622 }
Christine Chen9c1e0652013-05-23 15:40:19 -0700623
Yorke Lee33932ff2014-04-16 13:34:32 -0700624 if (mListsFragment != null && mListsFragment.isResumed() && mListsFragment.isVisible()) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700625 // If the favorites fragment is showing, fade to blank.
626 mFragmentsFrame.animate().alpha(0.0f);
627 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700628 getActionBar().hide();
Yorke Leecc4660d2014-04-24 11:22:57 -0700629 alignFloatingActionButtonMiddle();
Yorke Leec3766332013-07-31 11:13:16 -0700630 }
631
Yorke Lee86e21f72014-04-02 16:49:38 -0700632 /**
633 * Callback from child DialpadFragment when the dialpad is hidden.
634 */
635 public void onDialpadHidden() {
636 mDialButton.setVisibility(View.GONE);
637 mDialpadButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700638
Yorke Lee86e21f72014-04-02 16:49:38 -0700639 SearchFragment fragment = null;
640 if (mInDialpadSearch) {
641 fragment = mSmartDialSearchFragment;
642 } else if (mInRegularSearch) {
643 fragment = mRegularSearchFragment;
644 }
645 if (fragment != null && fragment.isVisible()) {
Yorke Leef42a1792014-04-25 16:56:42 -0700646 fragment.getView().animate().translationY(0)
Yorke Lee86e21f72014-04-02 16:49:38 -0700647 .setInterpolator(showActionBarInterpolator).setDuration(ANIMATION_DURATION);
Yorke Leec3766332013-07-31 11:13:16 -0700648 }
649
Yorke Leee00c9fe2014-04-12 12:42:06 -0700650 if (mListsFragment != null && mListsFragment.isVisible()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700651 mFragmentsFrame.animate().alpha(1.0f);
652 }
653 getActionBar().show();
Yorke Leecc4660d2014-04-24 11:22:57 -0700654 alignFloatingActionButtonByTab(mCurrentTabPosition);
Yorke Leec3766332013-07-31 11:13:16 -0700655 }
656
Yorke Leee00c9fe2014-04-12 12:42:06 -0700657 private void hideInputMethod(View view) {
658 final InputMethodManager imm = (InputMethodManager) getSystemService(
659 Context.INPUT_METHOD_SERVICE);
660 if (imm != null && view != null) {
661 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
662 }
663 }
664
Yorke Lee53a22302014-04-29 18:13:46 -0700665 private void prepareVoiceSearchButton() {
666 mVoiceSearchButton = getActionBar().getCustomView().findViewById(R.id.voice_search_button);
667 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
668 if (canIntentBeHandled(voiceIntent)) {
669 mVoiceSearchButton.setVisibility(View.VISIBLE);
670 mVoiceSearchButton.setOnClickListener(this);
671 } else {
672 mVoiceSearchButton.setVisibility(View.GONE);
673 }
674 }
675
Yorke Lee86e21f72014-04-02 16:49:38 -0700676 @Override
677 public boolean onCreateOptionsMenu(Menu menu) {
678 if (DEBUG) {
679 Log.d(TAG, "onCreateOptionsMenu");
680 }
681 MenuInflater inflater = getMenuInflater();
682 inflater.inflate(R.menu.dialtacts_options, menu);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700683
684 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700685 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700686 mPendingSearchViewQuery = null;
687 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700688 return super.onCreateOptionsMenu(menu);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700689 }
690
Chiao Cheng94b10b52012-08-17 16:59:12 -0700691 /**
692 * Returns true if the intent is due to hitting the green send key (hardware call button:
693 * KEYCODE_CALL) while in a call.
694 *
695 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700696 * @return true if the intent is due to hitting the green send key while in a call
697 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700698 private boolean isSendKeyWhileInCall(Intent intent) {
699 // If there is a call in progress and the user launched the dialer by hitting the call
700 // button, go straight to the in-call screen.
701 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700702
Yorke Lee62e995c2014-03-28 10:02:56 -0700703 try {
704 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
705 if (callKey && phone != null && phone.showCallScreen()) {
706 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700707 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700708 } catch (RemoteException e) {
709 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700710 }
711
712 return false;
713 }
714
715 /**
716 * Sets the current tab based on the intent's request type
717 *
718 * @param intent Intent that contains information about which tab should be selected
719 */
Yorke Leec3766332013-07-31 11:13:16 -0700720 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700721 // 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 -0700722 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700723 finish();
724 return;
725 }
726
Yorke Lee669b6082013-09-17 15:43:38 -0700727 if (mDialpadFragment != null) {
728 final boolean phoneIsInUse = phoneIsInUse();
729 if (phoneIsInUse || isDialIntent(intent)) {
730 mDialpadFragment.setStartedFromNewIntent(true);
731 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
732 mInCallDialpadUp = true;
733 }
734 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700735 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700736 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700737 }
738
739 @Override
740 public void onNewIntent(Intent newIntent) {
741 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700742 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700743
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 invalidateOptionsMenu();
745 }
746
747 /** Returns true if the given intent contains a phone number to populate the dialer with */
748 private boolean isDialIntent(Intent intent) {
749 final String action = intent.getAction();
750 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
751 return true;
752 }
753 if (Intent.ACTION_VIEW.equals(action)) {
754 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700755 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700756 return true;
757 }
758 }
759 return false;
760 }
761
762 /**
763 * Returns an appropriate call origin for this Activity. May return null when no call origin
764 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
765 * for remembering the tab in which the user made a phone call, so the external app's DIAL
766 * request should not be counted.)
767 */
768 public String getCallOrigin() {
769 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
770 }
771
Chiao Cheng94b10b52012-08-17 16:59:12 -0700772 /**
Yorke Leec3766332013-07-31 11:13:16 -0700773 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700774 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700775 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700776 if (getFragmentManager().isDestroyed()) {
777 // Weird race condition where fragment is doing work after the activity is destroyed
778 // due to talkback being on (b/10209937). Just return since we can't do any
779 // constructive here.
780 return;
781 }
782
Yorke Leeef2b7382013-08-09 17:39:25 -0700783 if (DEBUG) {
784 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
785 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700786
Yorke Leec3766332013-07-31 11:13:16 -0700787 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700788
Yorke Leeef2b7382013-08-09 17:39:25 -0700789 SearchFragment fragment;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700790 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700791 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700792 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700793 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700794 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700795
Yorke Leeb207f8a2013-08-29 18:51:06 -0700796 final String tag;
797 if (smartDialSearch) {
798 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
799 } else {
800 tag = TAG_REGULAR_SEARCH_FRAGMENT;
801 }
802 mInDialpadSearch = smartDialSearch;
803 mInRegularSearch = !smartDialSearch;
804
Yorke Leeef2b7382013-08-09 17:39:25 -0700805 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
806 if (fragment == null) {
807 if (smartDialSearch) {
808 fragment = new SmartDialSearchFragment();
809 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700810 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700811 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700812 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700813 // DialtactsActivity will provide the options menu
814 fragment.setHasOptionsMenu(false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700815 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700816 transaction.addToBackStack(null);
817 fragment.setQueryString(query, false);
818 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700819 }
820
821 /**
Yorke Leec3766332013-07-31 11:13:16 -0700822 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700823 */
Yorke Leec3766332013-07-31 11:13:16 -0700824 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700825 // See related bug in enterSearchUI();
826 if (getFragmentManager().isDestroyed()) {
827 return;
828 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700829 // Go all the way back to the favorites fragment, regardless of how many times we
830 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700831 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
832 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700833
834 if (isDialpadShowing()) {
835 mFragmentsFrame.setAlpha(0);
836 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700837 }
838
839 /** Returns an Intent to launch Call Settings screen */
840 public static Intent getCallSettingsIntent() {
841 final Intent intent = new Intent(Intent.ACTION_MAIN);
842 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
843 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
844 return intent;
845 }
846
847 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700848 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700849 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700850 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700851 } else if (getInSearchUi()) {
Yorke Lee53a22302014-04-29 18:13:46 -0700852 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700853 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700854 } else {
855 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700856 }
Yorke Leec3766332013-07-31 11:13:16 -0700857 }
858
859 @Override
860 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700861 if (mSmartDialSearchFragment != null) {
862 mSmartDialSearchFragment.setAddToContactNumber(query);
863 }
Yorke Leec3766332013-07-31 11:13:16 -0700864 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
865 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700866
867 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700868 if (DEBUG) {
869 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
870 }
871 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
872 // This callback can happen if the dialpad fragment is recreated because of
873 // activity destruction. In that case, don't update the search view because
874 // that would bring the user back to the search fragment regardless of the
875 // previous state of the application. Instead, just return here and let the
876 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700877 if (!TextUtils.isEmpty(normalizedQuery)) {
878 mPendingSearchViewQuery = normalizedQuery;
879 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700880 return;
881 }
Yorke Lee53a22302014-04-29 18:13:46 -0700882 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700883 }
884 }
Yorke Leec3766332013-07-31 11:13:16 -0700885
886 @Override
887 public void onListFragmentScrollStateChange(int scrollState) {
888 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700889 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700890 hideInputMethod(getCurrentFocus());
891 }
892 }
893
894 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700895 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
896 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700897 // TODO: No-op for now. This should eventually show/hide the actionBar based on
898 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700899 }
900
Yorke Lee86e21f72014-04-02 16:49:38 -0700901 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800902 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700903 if (mDialButton != null) {
904 mDialButton.setEnabled(enabled);
905 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800906 }
907
908 @Override
909 public void setDialButtonContainerVisible(boolean visible) {
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700910 mFloatingActionButton.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700911 }
912
913 private boolean phoneIsInUse() {
914 final TelephonyManager tm = (TelephonyManager) getSystemService(
915 Context.TELEPHONY_SERVICE);
916 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
917 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700918
Yorke Leee1424812013-09-04 18:24:48 -0700919 public static Intent getAddNumberToContactIntent(CharSequence text) {
920 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
921 intent.putExtra(Intents.Insert.PHONE, text);
922 intent.setType(Contacts.CONTENT_ITEM_TYPE);
923 return intent;
924 }
925
Yorke Leeda0f9042013-12-05 14:25:51 -0800926 private boolean canIntentBeHandled(Intent intent) {
927 final PackageManager packageManager = getPackageManager();
928 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
929 PackageManager.MATCH_DEFAULT_ONLY);
930 return resolveInfo != null && resolveInfo.size() > 0;
931 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800932
Yorke Lee6a1461a2014-04-21 16:27:14 -0700933 @Override
934 public void showCallHistory() {
935 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
936 // CONTENT_TYPE, so that we always open our call log from our dialer
937 final Intent intent = new Intent(this, CallLogActivity.class);
938 startActivity(intent);
939 }
940
Yorke Lee86e21f72014-04-02 16:49:38 -0700941 /**
942 * Called when the user has long-pressed a contact tile to start a drag operation.
943 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800944 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700945 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700946 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700947 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800948 }
949
950 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700951 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
952 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800953
Yorke Lee86e21f72014-04-02 16:49:38 -0700954 /**
955 * Called when the user has released a contact tile after long-pressing it.
956 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800957 @Override
958 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700959 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700960 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800961 }
962
963 @Override
964 public void onDroppedOnRemove() {}
965
966 /**
Yorke Leed999b712014-04-23 15:10:58 -0700967 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800968 * once it has been attached to the activity.
969 */
970 @Override
971 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -0700972 mDragDropController = dragController;
973 ((RemoveView) findViewById(R.id.remove_view))
974 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800975 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700976
977 @Override
978 public void onPickPhoneNumberAction(Uri dataUri) {
979 mPhoneNumberPickerActionListener.onPickPhoneNumberAction(dataUri);
980 }
981
982 @Override
983 public void onCallNumberDirectly(String phoneNumber) {
984 mPhoneNumberPickerActionListener.onCallNumberDirectly(phoneNumber);
985 }
986
987 @Override
988 public void onShortcutIntentCreated(Intent intent) {
989 mPhoneNumberPickerActionListener.onShortcutIntentCreated(intent);
990 }
991
992 @Override
993 public void onHomeInActionBarSelected() {
994 mPhoneNumberPickerActionListener.onHomeInActionBarSelected();
995 }
Yorke Lee33932ff2014-04-16 13:34:32 -0700996
997 public int getActionBarHeight() {
998 return mActionBarHeight;
999 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001000
1001 @Override
1002 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1003
1004 }
1005
1006 @Override
1007 public void onPageSelected(int position) {
1008 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001009 // If the dialpad is showing, the floating action button should always be middle aligned.
1010 if (!isDialpadShowing()) {
1011 alignFloatingActionButtonByTab(mCurrentTabPosition);
1012 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001013 }
1014
1015 @Override
1016 public void onPageScrollStateChanged(int state) {
1017 }
1018
1019 private void alignFloatingActionButtonByTab(int position) {
1020 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1021 alignFloatingActionButtonMiddle();
1022 } else {
1023 alignFloatingActionButtonRight();
1024 }
1025 }
1026
1027 private void alignFloatingActionButtonRight() {
1028 final RelativeLayout.LayoutParams params =
1029 (RelativeLayout.LayoutParams) mFloatingActionButton.getLayoutParams();
1030 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1031 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1032 mFloatingActionButton.setLayoutParams(params);
1033 }
1034
1035 private void alignFloatingActionButtonMiddle() {
1036 final RelativeLayout.LayoutParams params =
1037 (RelativeLayout.LayoutParams) mFloatingActionButton.getLayoutParams();
1038 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1039 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
1040 mFloatingActionButton.setLayoutParams(params);
1041 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001042}