blob: 7f9200eb462273404070eaab5337c6f899c328ad [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 Leecc4660d2014-04-24 11:22:57 -070019import android.animation.LayoutTransition;
Yorke Lee53a22302014-04-29 18:13:46 -070020import android.app.ActionBar;
Chiao Cheng94b10b52012-08-17 16:59:12 -070021import android.app.Activity;
22import android.app.Fragment;
23import android.app.FragmentManager;
24import android.app.FragmentTransaction;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080025import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070026import android.content.Context;
27import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080028import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Yorke Lee33932ff2014-04-16 13:34:32 -070030import android.content.res.TypedArray;
Chiao Cheng94b10b52012-08-17 16:59:12 -070031import android.net.Uri;
32import android.os.Bundle;
33import android.os.RemoteException;
34import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070036import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070037import android.speech.RecognizerIntent;
Yorke Leecc4660d2014-04-24 11:22:57 -070038import android.support.v4.view.ViewPager;
Yorke Leec3766332013-07-31 11:13:16 -070039import android.telephony.TelephonyManager;
Yorke Lee53a22302014-04-29 18:13:46 -070040import android.text.Editable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041import android.text.TextUtils;
Yorke Lee53a22302014-04-29 18:13:46 -070042import android.text.TextWatcher;
Chiao Cheng94b10b52012-08-17 16:59:12 -070043import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070044import android.view.DragEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.view.Menu;
Yorke Lee86e21f72014-04-02 16:49:38 -070046import android.view.MenuInflater;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070049import android.view.View.OnDragListener;
Yorke Lee86e21f72014-04-02 16:49:38 -070050import android.view.animation.AccelerateInterpolator;
51import android.view.animation.DecelerateInterpolator;
52import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070053import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070054import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070055import android.widget.EditText;
Chiao Cheng94b10b52012-08-17 16:59:12 -070056import android.widget.PopupMenu;
Yorke Leecc4660d2014-04-24 11:22:57 -070057import android.widget.RelativeLayout;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080058import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070059
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070060import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070061import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070062import com.android.contacts.common.dialog.ClearFrequentsDialog;
63import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080064import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070065import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070066import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070067import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070068import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070069import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070070import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070071import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080072import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070073import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080074import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070075import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070076import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070077import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070078import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080079import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070080import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070081import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070082import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070083import com.android.internal.telephony.ITelephony;
84
Yorke Leec3766332013-07-31 11:13:16 -070085import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080086import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070087
Chiao Cheng94b10b52012-08-17 16:59:12 -070088/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070089 * The dialer tab's title is 'phone', a more common name (see strings.xml).
90 */
Yorke Leec3766332013-07-31 11:13:16 -070091public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070092 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -070093 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080094 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -070095 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -070096 SpeedDialFragment.HostInterface,
Yorke Leee00c9fe2014-04-12 12:42:06 -070097 OnDragDropListener, View.OnLongClickListener,
Yorke Leecc4660d2014-04-24 11:22:57 -070098 OnPhoneNumberPickerActionListener,
99 ViewPager.OnPageChangeListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700100 private static final String TAG = "DialtactsActivity";
101
Ihab Awad526c0b82014-02-27 12:55:36 -0800102 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700103
Yorke Leef74011e2013-08-02 11:30:30 -0700104 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
105
Chiao Cheng94b10b52012-08-17 16:59:12 -0700106 /** Used to open Call Setting */
107 private static final String PHONE_PACKAGE = "com.android.phone";
108 private static final String CALL_SETTINGS_CLASS_NAME =
109 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700110 /** @see #getCallOrigin() */
111 private static final String CALL_ORIGIN_DIALTACTS =
112 "com.android.dialer.DialtactsActivity";
113
Yorke Leeb207f8a2013-08-29 18:51:06 -0700114 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
115 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700116 private static final String KEY_SEARCH_QUERY = "search_query";
117 private static final String KEY_FIRST_LAUNCH = "first_launch";
118
Yorke Leec3766332013-07-31 11:13:16 -0700119 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
120 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
121 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
122 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700123
Chiao Cheng94b10b52012-08-17 16:59:12 -0700124 /**
125 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
126 */
127 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
128
Yorke Leec3766332013-07-31 11:13:16 -0700129 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700130
Yorke Lee86e21f72014-04-02 16:49:38 -0700131 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800132
Andrew Lee0c667702014-05-12 14:31:45 -0700133 private RelativeLayout parentLayout;
134
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 Leec3766332013-07-31 11:13:16 -0700156 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800157 private View mDialButton;
Yorke Leec3766332013-07-31 11:13:16 -0700158
Yorke Leeb207f8a2013-08-29 18:51:06 -0700159 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700160
Yorke Lee33932ff2014-04-16 13:34:32 -0700161 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700162 private boolean mInDialpadSearch;
163 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700164 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700165
Yorke Leeef2b7382013-08-09 17:39:25 -0700166 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700167 * The position of the currently selected tab in the attached {@link ListsFragment}.
168 */
169 private int mCurrentTabPosition = 0;
170
171 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700172 * True if the dialpad is only temporarily showing due to being in call
173 */
174 private boolean mInCallDialpadUp;
175
176 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700177 * True when this activity has been launched for the first time.
178 */
Yorke Lee98702de2013-08-06 12:03:32 -0700179 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700180
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700181 /**
182 * Search query to be applied to the SearchView in the ActionBar once
183 * onCreateOptionsMenu has been called.
184 */
185 private String mPendingSearchViewQuery;
186
Yorke Lee53a22302014-04-29 18:13:46 -0700187 private EditText mSearchView;
188 private View mSearchViewCloseButton;
189 private View mVoiceSearchButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700190 /**
191 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
192 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
193 * removed from the speed dial list.
194 */
Yorke Lee28266192014-05-01 10:05:52 -0700195 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700196
Yorke Lee86e21f72014-04-02 16:49:38 -0700197 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
198 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700199 private String mSearchQuery;
200
Yorke Leefce269a2013-08-12 11:56:04 -0700201 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700202 private DragDropController mDragDropController;
Yorke Leefce269a2013-08-12 11:56:04 -0700203
Andrew Lee0c667702014-05-12 14:31:45 -0700204 private int mDialerBackgroundColor;
205 private int mContactListBackgroundColor;
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 /**
Yorke Lee28266192014-05-01 10:05:52 -0700223 * Listener that listens to drag events and sends their x and y coordinates to a
224 * {@link DragDropController}.
225 */
226 private class LayoutOnDragListener implements OnDragListener {
227 @Override
228 public boolean onDrag(View v, DragEvent event) {
229 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
230 mDragDropController.handleDragHovered(v, (int) event.getX(),
231 (int) event.getY());
232 }
233 return true;
234 }
235 }
236
237 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700238 * Listener used when one of phone numbers in search UI is selected. This will initiate a
239 * phone call using the phone number.
240 */
241 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
242 new OnPhoneNumberPickerActionListener() {
243 @Override
244 public void onPickPhoneNumberAction(Uri dataUri) {
245 // Specify call-origin so that users will see the previous tab instead of
246 // CallLog screen (search UI will be automatically exited).
247 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700248 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700249 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700250 }
251
252 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700253 public void onCallNumberDirectly(String phoneNumber) {
254 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
255 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700256 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700257 }
258
259 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700260 public void onShortcutIntentCreated(Intent intent) {
261 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
262 }
263
264 @Override
265 public void onHomeInActionBarSelected() {
266 exitSearchUi();
267 }
268 };
269
270 /**
271 * Listener used to send search queries to the phone search fragment.
272 */
Yorke Lee53a22302014-04-29 18:13:46 -0700273 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Yorke Leec3766332013-07-31 11:13:16 -0700274 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700275 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Yorke Leec3766332013-07-31 11:13:16 -0700276 }
277
278 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700279 public void onTextChanged(CharSequence s, int start, int before, int count) {
280 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700281 if (newText.equals(mSearchQuery)) {
282 // If the query hasn't changed (perhaps due to activity being destroyed
283 // and restored, or user launching the same DIAL intent twice), then there is
284 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700285 return;
Yorke Leeef2b7382013-08-09 17:39:25 -0700286 }
287 mSearchQuery = newText;
288 if (DEBUG) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700289 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leeef2b7382013-08-09 17:39:25 -0700290 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700291 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700292
Yorke Leec3766332013-07-31 11:13:16 -0700293 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700294 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700295 exitSearchUi();
Yorke Lee53a22302014-04-29 18:13:46 -0700296 mSearchViewCloseButton.setVisibility(View.GONE);
297 mVoiceSearchButton.setVisibility(View.VISIBLE);
298 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700299 } else if (!TextUtils.isEmpty(newText)) {
300 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
301 (!dialpadSearch && mInRegularSearch);
302 if (!sameSearchMode) {
303 // call enterSearchUi only if we are switching search modes, or entering
304 // search ui for the first time
305 enterSearchUi(dialpadSearch, newText);
306 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700307
Yorke Leeb207f8a2013-08-29 18:51:06 -0700308 if (dialpadSearch && mSmartDialSearchFragment != null) {
309 mSmartDialSearchFragment.setQueryString(newText, false);
310 } else if (mRegularSearchFragment != null) {
311 mRegularSearchFragment.setQueryString(newText, false);
312 }
Yorke Lee53a22302014-04-29 18:13:46 -0700313 mSearchViewCloseButton.setVisibility(View.VISIBLE);
314 mVoiceSearchButton.setVisibility(View.GONE);
315 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700316 }
Yorke Lee53a22302014-04-29 18:13:46 -0700317 return;
318 }
319
320 @Override
321 public void afterTextChanged(Editable s) {
Yorke Leec3766332013-07-31 11:13:16 -0700322 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700323 };
324
Yorke Leec3766332013-07-31 11:13:16 -0700325 private boolean isDialpadShowing() {
326 return mDialpadFragment != null && mDialpadFragment.isVisible();
327 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700328
329 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700330 protected void onCreate(Bundle savedInstanceState) {
331 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700332 mFirstLaunch = true;
333
Yorke Lee8898cd02013-08-08 10:24:27 -0700334 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800335 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700336
Yorke Lee53a22302014-04-29 18:13:46 -0700337 final ActionBar actionBar = getActionBar();
338 actionBar.setCustomView(R.layout.search_edittext);
339 actionBar.setDisplayShowCustomEnabled(true);
340
341 final View customView = actionBar.getCustomView();
342
343 mSearchView = (EditText) customView.findViewById(R.id.search_view);
344 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
345
346 mSearchViewCloseButton = customView.findViewById(R.id.search_close_button);
347 mSearchViewCloseButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700348
Yorke Lee33932ff2014-04-16 13:34:32 -0700349 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
350 new int[] { android.R.attr.actionBarSize });
351 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
352 styledAttributes.recycle();
353
Yorke Leeef2b7382013-08-09 17:39:25 -0700354 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
355 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700356 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800357 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700358 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800359 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
360 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700361 } else {
362 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700363 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
364 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700365 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700366 }
367
Andrew Lee0c667702014-05-12 14:31:45 -0700368 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
369 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
370 parentLayout.setOnDragListener(new LayoutOnDragListener());
371
372 mDialerBackgroundColor = getResources().getColor(R.color.background_dialer_light);
373 mContactListBackgroundColor =
374 getResources().getColor(R.color.contact_list_background_color);
Yorke Leecc4660d2014-04-24 11:22:57 -0700375
Yorke Leeb207f8a2013-08-29 18:51:06 -0700376 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee86e21f72014-04-02 16:49:38 -0700377
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700378 mFloatingActionButton = findViewById(R.id.floating_action_button);
Andrew Lee4cbc9732014-04-24 14:38:58 -0700379 ViewUtil.setupFloatingActionButton(mFloatingActionButton, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700380
Yorke Lee86e21f72014-04-02 16:49:38 -0700381 mDialButton = findViewById(R.id.dial_button);
382 mDialButton.setOnClickListener(this);
383 mDialpadButton = findViewById(R.id.dialpad_button);
384 mDialpadButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800385
Yorke Lee28266192014-05-01 10:05:52 -0700386 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800387
Yorke Lee0baa98b2013-08-22 10:55:16 -0700388 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700389 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700390 }
391
392 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700393 protected void onResume() {
394 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700395 if (mFirstLaunch) {
396 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700397 } else if (!phoneIsInUse() && mInCallDialpadUp) {
398 hideDialpadFragment(false, true);
399 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700400 }
401 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700402 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700403 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700404 }
405
406 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700407 protected void onPause() {
408 if (mClearSearchOnPause) {
409 hideDialpadAndSearchUi();
410 mClearSearchOnPause = false;
411 }
412 super.onPause();
413 }
414
415 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700416 protected void onSaveInstanceState(Bundle outState) {
417 super.onSaveInstanceState(outState);
418 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700419 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
420 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700421 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
422 }
423
424 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700425 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700426 if (fragment instanceof DialpadFragment) {
427 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700428 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700429 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700430 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700431 } else if (fragment instanceof SmartDialSearchFragment) {
432 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700433 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
434 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700435 if (mFragmentsFrame != null) {
436 mFragmentsFrame.setAlpha(1.0f);
437 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700438 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700439 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700440 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
441 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700442 if (mFragmentsFrame != null) {
443 mFragmentsFrame.setAlpha(1.0f);
444 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700445 } else if (fragment instanceof ListsFragment) {
446 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700447 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700448 }
Yorke Leec3766332013-07-31 11:13:16 -0700449 }
450
Alon Albertb453e5b2013-09-10 15:54:23 -0700451 protected void handleMenuSettings() {
452 openTelephonySetting(this);
453 }
454
455 public static void openTelephonySetting(Activity activity) {
456 final Intent settingsIntent = getCallSettingsIntent();
457 activity.startActivity(settingsIntent);
458 }
459
Chiao Cheng94b10b52012-08-17 16:59:12 -0700460 @Override
461 public void onClick(View view) {
462 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700463 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700464 // Reset the boolean flag that tracks whether the dialpad was up because
465 // we were in call. Regardless of whether it was true before, we want to
466 // show the dialpad because the user has explicitly clicked the dialpad
467 // button.
468 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700469 showDialpadFragment(true);
470 break;
Yorke Lee86e21f72014-04-02 16:49:38 -0700471 case R.id.dial_button:
472 // Dial button was pressed; tell the Dialpad fragment
473 mDialpadFragment.dialButtonPressed();
474 break;
Yorke Leec3766332013-07-31 11:13:16 -0700475 case R.id.search_close_button:
476 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700477 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700478 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700479 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700480 }
481 break;
Yorke Leec3766332013-07-31 11:13:16 -0700482 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800483 try {
484 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
485 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
486 } catch (ActivityNotFoundException e) {
487 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
488 Toast.LENGTH_SHORT).show();
489 }
Yorke Leec3766332013-07-31 11:13:16 -0700490 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700491 default: {
492 Log.wtf(TAG, "Unexpected onClick event from " + view);
493 break;
494 }
495 }
496 }
497
Yorke Leec3766332013-07-31 11:13:16 -0700498 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700499 public boolean onOptionsItemSelected(MenuItem item) {
500 switch (item.getItemId()) {
501 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700502 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700503 break;
504 case R.id.menu_add_contact:
505 try {
506 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
507 } catch (ActivityNotFoundException e) {
508 Toast toast = Toast.makeText(this,
509 R.string.add_contact_not_available,
510 Toast.LENGTH_SHORT);
511 toast.show();
512 }
513 break;
514 case R.id.menu_import_export:
515 // We hard-code the "contactsAreAvailable" argument because doing it properly would
516 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
517 // now in Dialtacts for (potential) performance reasons. Compare with how it is
518 // done in {@link PeopleActivity}.
519 ImportExportDialogFragment.show(getFragmentManager(), true,
520 DialtactsActivity.class);
521 return true;
522 case R.id.menu_clear_frequents:
523 // TODO: This should be enabled/disabled based on
Yorke Leed999b712014-04-23 15:10:58 -0700524 // SpeedDialFragment.hasFrequents
Yorke Lee86e21f72014-04-02 16:49:38 -0700525 ClearFrequentsDialog.show(getFragmentManager());
526 return true;
527 case R.id.menu_call_settings:
528 handleMenuSettings();
529 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700530 }
531 return false;
532 }
533
534 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800535 public boolean onLongClick(View view) {
536 switch (view.getId()) {
537 case R.id.dial_button: {
538 // Dial button was pressed; tell the Dialpad fragment
539 mDialpadFragment.dialButtonPressed();
540 return true; // Consume the event
541 }
542 default: {
543 Log.wtf(TAG, "Unexpected onClick event from " + view);
544 break;
545 }
546 }
547 return false;
548 }
549
550 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700551 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
552 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
553 if (resultCode == RESULT_OK) {
554 final ArrayList<String> matches = data.getStringArrayListExtra(
555 RecognizerIntent.EXTRA_RESULTS);
556 if (matches.size() > 0) {
557 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700558 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700559 } else {
560 Log.e(TAG, "Voice search - nothing heard");
561 }
562 } else {
563 Log.e(TAG, "Voice search failed");
564 }
565 }
566 super.onActivityResult(requestCode, resultCode, data);
567 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700568
Yorke Leec3766332013-07-31 11:13:16 -0700569 private void showDialpadFragment(boolean animate) {
Andrew Leecdfa6c62014-05-06 15:33:35 -0700570 mDialpadFragment.setAnimate(animate);
571
Chiao Cheng94b10b52012-08-17 16:59:12 -0700572 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700573 if (animate) {
574 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700575 } else {
576 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700577 }
578 ft.show(mDialpadFragment);
579 ft.commit();
580 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700581
Yorke Leeca195042013-09-25 16:29:38 -0700582 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700583 if (mDialpadFragment == null) return;
584 if (clearDialpad) {
585 mDialpadFragment.clearDialpad();
586 }
587 if (!mDialpadFragment.isVisible()) return;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700588 mDialpadFragment.setAnimate(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700589 final FragmentTransaction ft = getFragmentManager().beginTransaction();
590 if (animate) {
591 ft.setCustomAnimations(0, R.anim.slide_out);
592 }
593 ft.hide(mDialpadFragment);
594 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700595 }
596
Yorke Leeb207f8a2013-08-29 18:51:06 -0700597 private boolean getInSearchUi() {
598 return mInDialpadSearch || mInRegularSearch;
599 }
600
601 private void setNotInSearchUi() {
602 mInDialpadSearch = false;
603 mInRegularSearch = false;
604 }
605
Yorke Lee311969c2013-08-26 06:31:11 -0700606 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700607 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700608 hideDialpadFragment(false, true);
609 }
610
Yorke Lee86e21f72014-04-02 16:49:38 -0700611 /**
612 * Callback from child DialpadFragment when the dialpad is shown.
613 */
614 public void onDialpadShown() {
615 mDialButton.setVisibility(View.VISIBLE);
616 mDialpadButton.setVisibility(View.GONE);
Yorke Leeb0d19762014-03-05 14:43:28 -0800617
Yorke Lee86e21f72014-04-02 16:49:38 -0700618 SearchFragment fragment = null;
619 if (mInDialpadSearch) {
620 fragment = mSmartDialSearchFragment;
621 } else if (mInRegularSearch) {
622 fragment = mRegularSearchFragment;
623 }
624 if (fragment != null && fragment.isVisible()) {
Yorke Leef42a1792014-04-25 16:56:42 -0700625 fragment.getView().animate().translationY(-mActionBarHeight)
Yorke Lee86e21f72014-04-02 16:49:38 -0700626 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
627 }
Christine Chen9c1e0652013-05-23 15:40:19 -0700628
Yorke Lee33932ff2014-04-16 13:34:32 -0700629 if (mListsFragment != null && mListsFragment.isResumed() && mListsFragment.isVisible()) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700630 // If the favorites fragment is showing, fade to blank.
631 mFragmentsFrame.animate().alpha(0.0f);
Andrew Lee0c667702014-05-12 14:31:45 -0700632 parentLayout.setBackgroundColor(mContactListBackgroundColor);
Yorke Lee7eccf452014-03-14 12:52:42 -0700633 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700634 getActionBar().hide();
Yorke Leecc4660d2014-04-24 11:22:57 -0700635 alignFloatingActionButtonMiddle();
Yorke Leec3766332013-07-31 11:13:16 -0700636 }
637
Yorke Lee86e21f72014-04-02 16:49:38 -0700638 /**
639 * Callback from child DialpadFragment when the dialpad is hidden.
640 */
641 public void onDialpadHidden() {
642 mDialButton.setVisibility(View.GONE);
643 mDialpadButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700644
Yorke Lee86e21f72014-04-02 16:49:38 -0700645 SearchFragment fragment = null;
646 if (mInDialpadSearch) {
647 fragment = mSmartDialSearchFragment;
648 } else if (mInRegularSearch) {
649 fragment = mRegularSearchFragment;
650 }
651 if (fragment != null && fragment.isVisible()) {
Yorke Leef42a1792014-04-25 16:56:42 -0700652 fragment.getView().animate().translationY(0)
Yorke Lee86e21f72014-04-02 16:49:38 -0700653 .setInterpolator(showActionBarInterpolator).setDuration(ANIMATION_DURATION);
Yorke Leec3766332013-07-31 11:13:16 -0700654 }
655
Yorke Leee00c9fe2014-04-12 12:42:06 -0700656 if (mListsFragment != null && mListsFragment.isVisible()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700657 mFragmentsFrame.animate().alpha(1.0f);
Andrew Lee0c667702014-05-12 14:31:45 -0700658 parentLayout.setBackgroundColor(mDialerBackgroundColor);
659
Yorke Lee86e21f72014-04-02 16:49:38 -0700660 }
661 getActionBar().show();
Yorke Leecc4660d2014-04-24 11:22:57 -0700662 alignFloatingActionButtonByTab(mCurrentTabPosition);
Yorke Leec3766332013-07-31 11:13:16 -0700663 }
664
Yorke Leee00c9fe2014-04-12 12:42:06 -0700665 private void hideInputMethod(View view) {
666 final InputMethodManager imm = (InputMethodManager) getSystemService(
667 Context.INPUT_METHOD_SERVICE);
668 if (imm != null && view != null) {
669 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
670 }
671 }
672
Yorke Lee53a22302014-04-29 18:13:46 -0700673 private void prepareVoiceSearchButton() {
674 mVoiceSearchButton = getActionBar().getCustomView().findViewById(R.id.voice_search_button);
675 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
676 if (canIntentBeHandled(voiceIntent)) {
677 mVoiceSearchButton.setVisibility(View.VISIBLE);
678 mVoiceSearchButton.setOnClickListener(this);
679 } else {
680 mVoiceSearchButton.setVisibility(View.GONE);
681 }
682 }
683
Yorke Lee86e21f72014-04-02 16:49:38 -0700684 @Override
685 public boolean onCreateOptionsMenu(Menu menu) {
686 if (DEBUG) {
687 Log.d(TAG, "onCreateOptionsMenu");
688 }
689 MenuInflater inflater = getMenuInflater();
690 inflater.inflate(R.menu.dialtacts_options, menu);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700691
692 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700693 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700694 mPendingSearchViewQuery = null;
695 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700696 return super.onCreateOptionsMenu(menu);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700697 }
698
Chiao Cheng94b10b52012-08-17 16:59:12 -0700699 /**
700 * Returns true if the intent is due to hitting the green send key (hardware call button:
701 * KEYCODE_CALL) while in a call.
702 *
703 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700704 * @return true if the intent is due to hitting the green send key while in a call
705 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700706 private boolean isSendKeyWhileInCall(Intent intent) {
707 // If there is a call in progress and the user launched the dialer by hitting the call
708 // button, go straight to the in-call screen.
709 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700710
Yorke Lee62e995c2014-03-28 10:02:56 -0700711 try {
712 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
713 if (callKey && phone != null && phone.showCallScreen()) {
714 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700715 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700716 } catch (RemoteException e) {
717 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700718 }
719
720 return false;
721 }
722
723 /**
724 * Sets the current tab based on the intent's request type
725 *
726 * @param intent Intent that contains information about which tab should be selected
727 */
Yorke Leec3766332013-07-31 11:13:16 -0700728 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729 // 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 -0700730 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700731 finish();
732 return;
733 }
734
Yorke Lee669b6082013-09-17 15:43:38 -0700735 if (mDialpadFragment != null) {
736 final boolean phoneIsInUse = phoneIsInUse();
737 if (phoneIsInUse || isDialIntent(intent)) {
738 mDialpadFragment.setStartedFromNewIntent(true);
739 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
740 mInCallDialpadUp = true;
741 }
742 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700743 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700745 }
746
747 @Override
748 public void onNewIntent(Intent newIntent) {
749 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700750 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700751
Chiao Cheng94b10b52012-08-17 16:59:12 -0700752 invalidateOptionsMenu();
753 }
754
755 /** Returns true if the given intent contains a phone number to populate the dialer with */
756 private boolean isDialIntent(Intent intent) {
757 final String action = intent.getAction();
758 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
759 return true;
760 }
761 if (Intent.ACTION_VIEW.equals(action)) {
762 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700763 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 return true;
765 }
766 }
767 return false;
768 }
769
770 /**
771 * Returns an appropriate call origin for this Activity. May return null when no call origin
772 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
773 * for remembering the tab in which the user made a phone call, so the external app's DIAL
774 * request should not be counted.)
775 */
776 public String getCallOrigin() {
777 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
778 }
779
Chiao Cheng94b10b52012-08-17 16:59:12 -0700780 /**
Yorke Leec3766332013-07-31 11:13:16 -0700781 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700782 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700783 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700784 if (getFragmentManager().isDestroyed()) {
785 // Weird race condition where fragment is doing work after the activity is destroyed
786 // due to talkback being on (b/10209937). Just return since we can't do any
787 // constructive here.
788 return;
789 }
790
Yorke Leeef2b7382013-08-09 17:39:25 -0700791 if (DEBUG) {
792 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
793 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700794
Yorke Leec3766332013-07-31 11:13:16 -0700795 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700796
Yorke Leeef2b7382013-08-09 17:39:25 -0700797 SearchFragment fragment;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700798 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700799 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700800 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700801 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700802 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700803
Yorke Leeb207f8a2013-08-29 18:51:06 -0700804 final String tag;
805 if (smartDialSearch) {
806 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
807 } else {
808 tag = TAG_REGULAR_SEARCH_FRAGMENT;
809 }
810 mInDialpadSearch = smartDialSearch;
811 mInRegularSearch = !smartDialSearch;
812
Yorke Leeef2b7382013-08-09 17:39:25 -0700813 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
814 if (fragment == null) {
815 if (smartDialSearch) {
816 fragment = new SmartDialSearchFragment();
817 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700818 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700819 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700820 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700821 // DialtactsActivity will provide the options menu
822 fragment.setHasOptionsMenu(false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700823 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700824 transaction.addToBackStack(null);
825 fragment.setQueryString(query, false);
826 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700827 }
828
829 /**
Yorke Leec3766332013-07-31 11:13:16 -0700830 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700831 */
Yorke Leec3766332013-07-31 11:13:16 -0700832 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700833 // See related bug in enterSearchUI();
834 if (getFragmentManager().isDestroyed()) {
835 return;
836 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700837 // Go all the way back to the favorites fragment, regardless of how many times we
838 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700839 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
840 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700841
842 if (isDialpadShowing()) {
843 mFragmentsFrame.setAlpha(0);
844 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700845 }
846
847 /** Returns an Intent to launch Call Settings screen */
848 public static Intent getCallSettingsIntent() {
849 final Intent intent = new Intent(Intent.ACTION_MAIN);
850 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
851 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
852 return intent;
853 }
854
855 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700856 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700857 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700858 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700859 } else if (getInSearchUi()) {
Yorke Lee53a22302014-04-29 18:13:46 -0700860 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700861 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700862 } else {
863 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700864 }
Yorke Leec3766332013-07-31 11:13:16 -0700865 }
866
867 @Override
868 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700869 if (mSmartDialSearchFragment != null) {
870 mSmartDialSearchFragment.setAddToContactNumber(query);
871 }
Yorke Leec3766332013-07-31 11:13:16 -0700872 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
873 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700874
875 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700876 if (DEBUG) {
877 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
878 }
879 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
880 // This callback can happen if the dialpad fragment is recreated because of
881 // activity destruction. In that case, don't update the search view because
882 // that would bring the user back to the search fragment regardless of the
883 // previous state of the application. Instead, just return here and let the
884 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700885 if (!TextUtils.isEmpty(normalizedQuery)) {
886 mPendingSearchViewQuery = normalizedQuery;
887 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700888 return;
889 }
Yorke Lee53a22302014-04-29 18:13:46 -0700890 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700891 }
892 }
Yorke Leec3766332013-07-31 11:13:16 -0700893
894 @Override
895 public void onListFragmentScrollStateChange(int scrollState) {
896 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700897 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700898 hideInputMethod(getCurrentFocus());
899 }
900 }
901
902 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700903 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
904 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700905 // TODO: No-op for now. This should eventually show/hide the actionBar based on
906 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700907 }
908
Yorke Lee86e21f72014-04-02 16:49:38 -0700909 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800910 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700911 if (mDialButton != null) {
912 mDialButton.setEnabled(enabled);
913 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800914 }
915
916 @Override
917 public void setDialButtonContainerVisible(boolean visible) {
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700918 mFloatingActionButton.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700919 }
920
921 private boolean phoneIsInUse() {
922 final TelephonyManager tm = (TelephonyManager) getSystemService(
923 Context.TELEPHONY_SERVICE);
924 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
925 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700926
Yorke Leee1424812013-09-04 18:24:48 -0700927 public static Intent getAddNumberToContactIntent(CharSequence text) {
928 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
929 intent.putExtra(Intents.Insert.PHONE, text);
930 intent.setType(Contacts.CONTENT_ITEM_TYPE);
931 return intent;
932 }
933
Yorke Leeda0f9042013-12-05 14:25:51 -0800934 private boolean canIntentBeHandled(Intent intent) {
935 final PackageManager packageManager = getPackageManager();
936 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
937 PackageManager.MATCH_DEFAULT_ONLY);
938 return resolveInfo != null && resolveInfo.size() > 0;
939 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800940
Yorke Lee6a1461a2014-04-21 16:27:14 -0700941 @Override
942 public void showCallHistory() {
943 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
944 // CONTENT_TYPE, so that we always open our call log from our dialer
945 final Intent intent = new Intent(this, CallLogActivity.class);
946 startActivity(intent);
947 }
948
Yorke Lee86e21f72014-04-02 16:49:38 -0700949 /**
950 * Called when the user has long-pressed a contact tile to start a drag operation.
951 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800952 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700953 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700954 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700955 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800956 }
957
958 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700959 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
960 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800961
Yorke Lee86e21f72014-04-02 16:49:38 -0700962 /**
963 * Called when the user has released a contact tile after long-pressing it.
964 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800965 @Override
966 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700967 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700968 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800969 }
970
971 @Override
972 public void onDroppedOnRemove() {}
973
974 /**
Yorke Leed999b712014-04-23 15:10:58 -0700975 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800976 * once it has been attached to the activity.
977 */
978 @Override
979 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -0700980 mDragDropController = dragController;
981 ((RemoveView) findViewById(R.id.remove_view))
982 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800983 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700984
985 @Override
986 public void onPickPhoneNumberAction(Uri dataUri) {
987 mPhoneNumberPickerActionListener.onPickPhoneNumberAction(dataUri);
988 }
989
990 @Override
991 public void onCallNumberDirectly(String phoneNumber) {
992 mPhoneNumberPickerActionListener.onCallNumberDirectly(phoneNumber);
993 }
994
995 @Override
996 public void onShortcutIntentCreated(Intent intent) {
997 mPhoneNumberPickerActionListener.onShortcutIntentCreated(intent);
998 }
999
1000 @Override
1001 public void onHomeInActionBarSelected() {
1002 mPhoneNumberPickerActionListener.onHomeInActionBarSelected();
1003 }
Yorke Lee33932ff2014-04-16 13:34:32 -07001004
1005 public int getActionBarHeight() {
1006 return mActionBarHeight;
1007 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001008
1009 @Override
1010 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
1011
1012 }
1013
1014 @Override
1015 public void onPageSelected(int position) {
1016 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -07001017 // If the dialpad is showing, the floating action button should always be middle aligned.
1018 if (!isDialpadShowing()) {
1019 alignFloatingActionButtonByTab(mCurrentTabPosition);
1020 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001021 }
1022
1023 @Override
1024 public void onPageScrollStateChanged(int state) {
1025 }
1026
1027 private void alignFloatingActionButtonByTab(int position) {
1028 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1029 alignFloatingActionButtonMiddle();
1030 } else {
1031 alignFloatingActionButtonRight();
1032 }
1033 }
1034
1035 private void alignFloatingActionButtonRight() {
1036 final RelativeLayout.LayoutParams params =
1037 (RelativeLayout.LayoutParams) mFloatingActionButton.getLayoutParams();
1038 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1039 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1040 mFloatingActionButton.setLayoutParams(params);
1041 }
1042
1043 private void alignFloatingActionButtonMiddle() {
1044 final RelativeLayout.LayoutParams params =
1045 (RelativeLayout.LayoutParams) mFloatingActionButton.getLayoutParams();
1046 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1047 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
1048 mFloatingActionButton.setLayoutParams(params);
1049 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001050}