blob: 63a31a0ee419d2f55e1f50d1fa6aad549a1ef3bd [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;
Yorke Lee4e28c1d2014-05-13 15:38:10 -070043import android.util.AttributeSet;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.util.Log;
Yorke Lee28266192014-05-01 10:05:52 -070045import android.view.DragEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.Menu;
Yorke Lee86e21f72014-04-02 16:49:38 -070047import android.view.MenuInflater;
Chiao Cheng94b10b52012-08-17 16:59:12 -070048import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.View;
Yorke Lee28266192014-05-01 10:05:52 -070050import android.view.View.OnDragListener;
Yorke Lee86e21f72014-04-02 16:49:38 -070051import android.view.animation.AccelerateInterpolator;
Andrew Lee2a58ab82014-05-15 02:16:04 -070052import android.view.animation.Animation;
53import android.view.animation.Animation.AnimationListener;
54import android.view.animation.AnimationUtils;
Yorke Lee86e21f72014-04-02 16:49:38 -070055import android.view.animation.DecelerateInterpolator;
56import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070058import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070059import android.widget.EditText;
Andrew Leea73e1892014-05-13 13:21:16 -070060import android.widget.ImageButton;
Chiao Cheng94b10b52012-08-17 16:59:12 -070061import android.widget.PopupMenu;
Yorke Leecc4660d2014-04-24 11:22:57 -070062import android.widget.RelativeLayout;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080063import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070064
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070065import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070066import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070067import com.android.contacts.common.dialog.ClearFrequentsDialog;
68import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080069import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070070import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070071import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070072import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070073import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070074import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070075import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070076import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080077import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070078import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080079import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070080import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070081import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070082import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070083import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080084import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070085import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070086import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070087import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070088import com.android.internal.telephony.ITelephony;
89
Yorke Leec3766332013-07-31 11:13:16 -070090import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080091import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070092
Chiao Cheng94b10b52012-08-17 16:59:12 -070093/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070094 * The dialer tab's title is 'phone', a more common name (see strings.xml).
95 */
Yorke Leec3766332013-07-31 11:13:16 -070096public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070097 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -070098 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080099 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700100 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700101 SpeedDialFragment.HostInterface,
Andrew Lee99a570c2014-05-15 14:18:50 -0700102 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700103 OnPhoneNumberPickerActionListener,
104 ViewPager.OnPageChangeListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700105 private static final String TAG = "DialtactsActivity";
106
Ihab Awad526c0b82014-02-27 12:55:36 -0800107 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700108
Yorke Leef74011e2013-08-02 11:30:30 -0700109 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
110
Chiao Cheng94b10b52012-08-17 16:59:12 -0700111 /** Used to open Call Setting */
112 private static final String PHONE_PACKAGE = "com.android.phone";
113 private static final String CALL_SETTINGS_CLASS_NAME =
114 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 /** @see #getCallOrigin() */
116 private static final String CALL_ORIGIN_DIALTACTS =
117 "com.android.dialer.DialtactsActivity";
118
Yorke Leeb207f8a2013-08-29 18:51:06 -0700119 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
120 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700121 private static final String KEY_SEARCH_QUERY = "search_query";
122 private static final String KEY_FIRST_LAUNCH = "first_launch";
123
Yorke Leec3766332013-07-31 11:13:16 -0700124 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
125 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
126 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
127 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700128
Chiao Cheng94b10b52012-08-17 16:59:12 -0700129 /**
130 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
131 */
132 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
133
Yorke Leec3766332013-07-31 11:13:16 -0700134 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700135
Yorke Lee86e21f72014-04-02 16:49:38 -0700136 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800137
Andrew Lee0c667702014-05-12 14:31:45 -0700138 private RelativeLayout parentLayout;
139
Yorke Leec3766332013-07-31 11:13:16 -0700140 /**
Yorke Leec3766332013-07-31 11:13:16 -0700141 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700142 */
Yorke Leef74011e2013-08-02 11:30:30 -0700143 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700144
145 /**
146 * Fragment for searching phone numbers using the alphanumeric keyboard.
147 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700148 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700149
150 /**
151 * Fragment for searching phone numbers using the dialpad.
152 */
153 private SmartDialSearchFragment mSmartDialSearchFragment;
154
Yorke Leee00c9fe2014-04-12 12:42:06 -0700155 /**
156 * Fragment containing the speed dial list, recents list, and all contacts list.
157 */
158 private ListsFragment mListsFragment;
159
Andrew Leea73e1892014-05-13 13:21:16 -0700160 private View mFloatingActionButtonContainer;
161 private ImageButton mFloatingActionButton;
Yorke Leec3766332013-07-31 11:13:16 -0700162
Yorke Leeb207f8a2013-08-29 18:51:06 -0700163 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700164
Yorke Lee33932ff2014-04-16 13:34:32 -0700165 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700166 private boolean mInDialpadSearch;
167 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700168 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700169 private boolean mIsDialpadShown;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700170
Yorke Leeef2b7382013-08-09 17:39:25 -0700171 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700172 * The position of the currently selected tab in the attached {@link ListsFragment}.
173 */
174 private int mCurrentTabPosition = 0;
175
176 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700177 * True if the dialpad is only temporarily showing due to being in call
178 */
179 private boolean mInCallDialpadUp;
180
181 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700182 * True when this activity has been launched for the first time.
183 */
Yorke Lee98702de2013-08-06 12:03:32 -0700184 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700185
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700186 /**
187 * Search query to be applied to the SearchView in the ActionBar once
188 * onCreateOptionsMenu has been called.
189 */
190 private String mPendingSearchViewQuery;
191
Yorke Lee53a22302014-04-29 18:13:46 -0700192 private EditText mSearchView;
193 private View mSearchViewCloseButton;
194 private View mVoiceSearchButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700195 /**
196 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
197 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
198 * removed from the speed dial list.
199 */
Yorke Lee28266192014-05-01 10:05:52 -0700200 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700201
Yorke Lee86e21f72014-04-02 16:49:38 -0700202 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
203 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700204 private String mSearchQuery;
205
Yorke Leefce269a2013-08-12 11:56:04 -0700206 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700207 private DragDropController mDragDropController;
Yorke Leefce269a2013-08-12 11:56:04 -0700208
Andrew Lee0c667702014-05-12 14:31:45 -0700209 private int mDialerBackgroundColor;
210 private int mContactListBackgroundColor;
211
Yorke Leee00c9fe2014-04-12 12:42:06 -0700212 private class OverflowPopupMenu extends PopupMenu {
213 public OverflowPopupMenu(Context context, View anchor) {
214 super(context, anchor);
215 }
216
217 @Override
218 public void show() {
219 final Menu menu = getMenu();
220 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Leed999b712014-04-23 15:10:58 -0700221 // TODO: Check mSpeedDialFragment.hasFrequents()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700222 clearFrequents.setVisible(true);
223 super.show();
224 }
225 }
226
Chiao Cheng94b10b52012-08-17 16:59:12 -0700227 /**
Yorke Lee28266192014-05-01 10:05:52 -0700228 * Listener that listens to drag events and sends their x and y coordinates to a
229 * {@link DragDropController}.
230 */
231 private class LayoutOnDragListener implements OnDragListener {
232 @Override
233 public boolean onDrag(View v, DragEvent event) {
234 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
235 mDragDropController.handleDragHovered(v, (int) event.getX(),
236 (int) event.getY());
237 }
238 return true;
239 }
240 }
241
242 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700243 * Listener used to send search queries to the phone search fragment.
244 */
Yorke Lee53a22302014-04-29 18:13:46 -0700245 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Andrew Lee99a570c2014-05-15 14:18:50 -0700246 @Override
247 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
248 }
Yorke Leec3766332013-07-31 11:13:16 -0700249
Andrew Lee99a570c2014-05-15 14:18:50 -0700250 @Override
251 public void onTextChanged(CharSequence s, int start, int before, int count) {
252 final String newText = s.toString();
253 if (newText.equals(mSearchQuery)) {
254 // If the query hasn't changed (perhaps due to activity being destroyed
255 // and restored, or user launching the same DIAL intent twice), then there is
256 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700257 return;
258 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700259 mSearchQuery = newText;
260 if (DEBUG) {
261 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leec3766332013-07-31 11:13:16 -0700262 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700263
264 // Show search result with non-empty text. Show a bare list otherwise.
265 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
266 (!mIsDialpadShown && mInRegularSearch);
267 if (!sameSearchMode) {
268 // call enterSearchUi only if we are switching search modes, or entering
269 // search ui for the first time
270 enterSearchUi(mIsDialpadShown, newText);
271 }
272
273 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
274 mSmartDialSearchFragment.setQueryString(newText, false /* delaySelection */);
275 } else if (mRegularSearchFragment != null) {
276 mRegularSearchFragment.setQueryString(newText, false /* delaySelection */);
277 }
278
279 if (TextUtils.isEmpty(newText)) {
280 mSearchViewCloseButton.setVisibility(View.GONE);
281 mVoiceSearchButton.setVisibility(View.VISIBLE);
282 } else {
283 mSearchViewCloseButton.setVisibility(View.VISIBLE);
284 mVoiceSearchButton.setVisibility(View.GONE);
285 }
286 }
287
288 @Override
289 public void afterTextChanged(Editable s) {
290 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291 };
292
Chiao Cheng94b10b52012-08-17 16:59:12 -0700293 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700294 protected void onCreate(Bundle savedInstanceState) {
295 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700296 mFirstLaunch = true;
297
Yorke Lee8898cd02013-08-08 10:24:27 -0700298 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800299 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700300
Yorke Lee53a22302014-04-29 18:13:46 -0700301 final ActionBar actionBar = getActionBar();
302 actionBar.setCustomView(R.layout.search_edittext);
303 actionBar.setDisplayShowCustomEnabled(true);
304
305 final View customView = actionBar.getCustomView();
306
307 mSearchView = (EditText) customView.findViewById(R.id.search_view);
308 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
309
310 mSearchViewCloseButton = customView.findViewById(R.id.search_close_button);
311 mSearchViewCloseButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700312
Yorke Lee33932ff2014-04-16 13:34:32 -0700313 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
314 new int[] { android.R.attr.actionBarSize });
315 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
316 styledAttributes.recycle();
317
Yorke Leeef2b7382013-08-09 17:39:25 -0700318 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
319 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700320 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800321 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700322 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800323 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
324 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700325 } else {
326 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700327 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
328 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700329 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700330 }
331
Andrew Lee0c667702014-05-12 14:31:45 -0700332 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
333 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
334 parentLayout.setOnDragListener(new LayoutOnDragListener());
335
336 mDialerBackgroundColor = getResources().getColor(R.color.background_dialer_light);
337 mContactListBackgroundColor =
338 getResources().getColor(R.color.contact_list_background_color);
Yorke Leecc4660d2014-04-24 11:22:57 -0700339
Yorke Leeb207f8a2013-08-29 18:51:06 -0700340 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee86e21f72014-04-02 16:49:38 -0700341
Andrew Leea73e1892014-05-13 13:21:16 -0700342 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
343 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700344
Andrew Leea73e1892014-05-13 13:21:16 -0700345 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
346 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800347
Yorke Lee28266192014-05-01 10:05:52 -0700348 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800349
Yorke Lee0baa98b2013-08-22 10:55:16 -0700350 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700351 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700352 }
353
354 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700355 protected void onResume() {
356 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700357 if (mFirstLaunch) {
358 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700359 } else if (!phoneIsInUse() && mInCallDialpadUp) {
360 hideDialpadFragment(false, true);
361 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700362 }
363 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700364 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700365 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700366 }
367
368 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700369 protected void onPause() {
370 if (mClearSearchOnPause) {
371 hideDialpadAndSearchUi();
372 mClearSearchOnPause = false;
373 }
374 super.onPause();
375 }
376
377 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700378 protected void onSaveInstanceState(Bundle outState) {
379 super.onSaveInstanceState(outState);
380 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700381 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
382 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700383 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
384 }
385
386 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700387 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700388 if (fragment instanceof DialpadFragment) {
389 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700390 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700391 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700392 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700393 } else if (fragment instanceof SmartDialSearchFragment) {
394 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700395 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Andrew Lee99a570c2014-05-15 14:18:50 -0700396 mSmartDialSearchFragment.setShowEmptyListForNullQuery(true);
Yorke Lee7eccf452014-03-14 12:52:42 -0700397 if (mFragmentsFrame != null) {
398 mFragmentsFrame.setAlpha(1.0f);
399 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700400 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700401 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700402 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Andrew Lee99a570c2014-05-15 14:18:50 -0700403 mRegularSearchFragment.setShowEmptyListForNullQuery(true);
Yorke Lee7eccf452014-03-14 12:52:42 -0700404 if (mFragmentsFrame != null) {
405 mFragmentsFrame.setAlpha(1.0f);
406 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700407 } else if (fragment instanceof ListsFragment) {
408 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700409 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700410 }
Yorke Leec3766332013-07-31 11:13:16 -0700411 }
412
Alon Albertb453e5b2013-09-10 15:54:23 -0700413 protected void handleMenuSettings() {
414 openTelephonySetting(this);
415 }
416
417 public static void openTelephonySetting(Activity activity) {
418 final Intent settingsIntent = getCallSettingsIntent();
419 activity.startActivity(settingsIntent);
420 }
421
Chiao Cheng94b10b52012-08-17 16:59:12 -0700422 @Override
423 public void onClick(View view) {
424 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700425 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700426 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700427 mInCallDialpadUp = false;
428 showDialpadFragment(true);
429 } else {
430 // Dial button was pressed; tell the Dialpad fragment
431 mDialpadFragment.dialButtonPressed();
432 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700433 break;
Yorke Leec3766332013-07-31 11:13:16 -0700434 case R.id.search_close_button:
435 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700436 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700437 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700438 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700439 }
440 break;
Yorke Leec3766332013-07-31 11:13:16 -0700441 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800442 try {
443 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
444 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
445 } catch (ActivityNotFoundException e) {
446 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
447 Toast.LENGTH_SHORT).show();
448 }
Yorke Leec3766332013-07-31 11:13:16 -0700449 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700450 default: {
451 Log.wtf(TAG, "Unexpected onClick event from " + view);
452 break;
453 }
454 }
455 }
456
Yorke Leec3766332013-07-31 11:13:16 -0700457 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700458 public boolean onOptionsItemSelected(MenuItem item) {
459 switch (item.getItemId()) {
460 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700461 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700462 break;
463 case R.id.menu_add_contact:
464 try {
465 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
466 } catch (ActivityNotFoundException e) {
467 Toast toast = Toast.makeText(this,
468 R.string.add_contact_not_available,
469 Toast.LENGTH_SHORT);
470 toast.show();
471 }
472 break;
473 case R.id.menu_import_export:
474 // We hard-code the "contactsAreAvailable" argument because doing it properly would
475 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
476 // now in Dialtacts for (potential) performance reasons. Compare with how it is
477 // done in {@link PeopleActivity}.
478 ImportExportDialogFragment.show(getFragmentManager(), true,
479 DialtactsActivity.class);
480 return true;
481 case R.id.menu_clear_frequents:
482 // TODO: This should be enabled/disabled based on
Yorke Leed999b712014-04-23 15:10:58 -0700483 // SpeedDialFragment.hasFrequents
Yorke Lee86e21f72014-04-02 16:49:38 -0700484 ClearFrequentsDialog.show(getFragmentManager());
485 return true;
486 case R.id.menu_call_settings:
487 handleMenuSettings();
488 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700489 }
490 return false;
491 }
492
493 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700494 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
495 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
496 if (resultCode == RESULT_OK) {
497 final ArrayList<String> matches = data.getStringArrayListExtra(
498 RecognizerIntent.EXTRA_RESULTS);
499 if (matches.size() > 0) {
500 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700501 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700502 } else {
503 Log.e(TAG, "Voice search - nothing heard");
504 }
505 } else {
506 Log.e(TAG, "Voice search failed");
507 }
508 }
509 super.onActivityResult(requestCode, resultCode, data);
510 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700511
Andrew Lee2a58ab82014-05-15 02:16:04 -0700512 /**
513 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
514 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
515 * @see #onDialpadShown
516 */
Yorke Leec3766332013-07-31 11:13:16 -0700517 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700518 if (mIsDialpadShown) {
519 return;
520 }
521 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700522 mDialpadFragment.setAnimate(animate);
523
Chiao Cheng94b10b52012-08-17 16:59:12 -0700524 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700525 ft.show(mDialpadFragment);
526 ft.commit();
527 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700528
Andrew Lee2a58ab82014-05-15 02:16:04 -0700529 /**
530 * Callback from child DialpadFragment when the dialpad is shown.
531 */
532 public void onDialpadShown() {
533 updateFloatingActionButton();
534 if (mDialpadFragment.getAnimate()) {
535 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
536 mDialpadFragment.getView().startAnimation(slideIn);
537 } else {
538 mDialpadFragment.setYFraction(0);
539 }
540
541 if (mListsFragment != null && mListsFragment.isResumed() && mListsFragment.isVisible()) {
542 // If the favorites fragment is showing, fade to blank.
543 mFragmentsFrame.animate().alpha(0.0f);
544 parentLayout.setBackgroundColor(mContactListBackgroundColor);
545 }
546
547 updateSearchFragmentPosition();
548 getActionBar().hide();
549 }
550
551 /**
552 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
553 * a callback after the hide animation ends.
554 * @see #commitDialpadFragmentHide
555 */
Yorke Leeca195042013-09-25 16:29:38 -0700556 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700557 if (mDialpadFragment == null) {
558 return;
559 }
Yorke Leef6673d32013-08-23 15:34:17 -0700560 if (clearDialpad) {
561 mDialpadFragment.clearDialpad();
562 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700563 if (!mIsDialpadShown) {
564 return;
Yorke Leec3766332013-07-31 11:13:16 -0700565 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700566 mIsDialpadShown = false;
567 mDialpadFragment.setAnimate(animate);
568
569 updateFloatingActionButton();
570 if (animate) {
571 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
572 slideOut.setAnimationListener(new ActivityAnimationListener() {
573 @Override
574 public void onAnimationEnd(Animation animation) {
575 commitDialpadFragmentHide();
576 }
577 });
578 mDialpadFragment.getView().startAnimation(slideOut);
579 } else {
580 commitDialpadFragmentHide();
581 }
582
583 if (mListsFragment != null && mListsFragment.isVisible()) {
584 mFragmentsFrame.animate().alpha(1.0f);
585 parentLayout.setBackgroundColor(mDialerBackgroundColor);
586 }
587
588 updateSearchFragmentPosition();
589 getActionBar().show();
590 }
591
592 /**
593 * Finishes hiding the dialpad fragment after any animations are completed.
594 */
595 private void commitDialpadFragmentHide() {
596 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700597 ft.hide(mDialpadFragment);
598 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700599 }
600
Andrew Lee2a58ab82014-05-15 02:16:04 -0700601 private void updateSearchFragmentPosition() {
602 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
603 SearchFragment fragment = null;
604 if (mInDialpadSearch) {
605 fragment = mSmartDialSearchFragment;
606 } else if (mInRegularSearch) {
607 fragment = mRegularSearchFragment;
608 }
609 if (fragment != null && fragment.isVisible()) {
610 fragment.getView().animate().translationY(translationValue)
611 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
612 }
613 }
614
Yorke Leeb207f8a2013-08-29 18:51:06 -0700615 private boolean getInSearchUi() {
616 return mInDialpadSearch || mInRegularSearch;
617 }
618
619 private void setNotInSearchUi() {
620 mInDialpadSearch = false;
621 mInRegularSearch = false;
622 }
623
Yorke Lee311969c2013-08-26 06:31:11 -0700624 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700625 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700626 hideDialpadFragment(false, true);
627 }
628
Yorke Leee00c9fe2014-04-12 12:42:06 -0700629 private void hideInputMethod(View view) {
630 final InputMethodManager imm = (InputMethodManager) getSystemService(
631 Context.INPUT_METHOD_SERVICE);
632 if (imm != null && view != null) {
633 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
634 }
635 }
636
Yorke Lee53a22302014-04-29 18:13:46 -0700637 private void prepareVoiceSearchButton() {
638 mVoiceSearchButton = getActionBar().getCustomView().findViewById(R.id.voice_search_button);
639 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
640 if (canIntentBeHandled(voiceIntent)) {
641 mVoiceSearchButton.setVisibility(View.VISIBLE);
642 mVoiceSearchButton.setOnClickListener(this);
643 } else {
644 mVoiceSearchButton.setVisibility(View.GONE);
645 }
646 }
647
Yorke Lee86e21f72014-04-02 16:49:38 -0700648 @Override
649 public boolean onCreateOptionsMenu(Menu menu) {
650 if (DEBUG) {
651 Log.d(TAG, "onCreateOptionsMenu");
652 }
653 MenuInflater inflater = getMenuInflater();
654 inflater.inflate(R.menu.dialtacts_options, menu);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700655
656 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700657 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700658 mPendingSearchViewQuery = null;
659 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700660 return super.onCreateOptionsMenu(menu);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700661 }
662
Chiao Cheng94b10b52012-08-17 16:59:12 -0700663 /**
664 * Returns true if the intent is due to hitting the green send key (hardware call button:
665 * KEYCODE_CALL) while in a call.
666 *
667 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700668 * @return true if the intent is due to hitting the green send key while in a call
669 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700670 private boolean isSendKeyWhileInCall(Intent intent) {
671 // If there is a call in progress and the user launched the dialer by hitting the call
672 // button, go straight to the in-call screen.
673 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700674
Yorke Lee62e995c2014-03-28 10:02:56 -0700675 try {
676 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
677 if (callKey && phone != null && phone.showCallScreen()) {
678 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700679 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700680 } catch (RemoteException e) {
681 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700682 }
683
684 return false;
685 }
686
687 /**
688 * Sets the current tab based on the intent's request type
689 *
690 * @param intent Intent that contains information about which tab should be selected
691 */
Yorke Leec3766332013-07-31 11:13:16 -0700692 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700693 // 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 -0700694 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700695 finish();
696 return;
697 }
698
Yorke Lee669b6082013-09-17 15:43:38 -0700699 if (mDialpadFragment != null) {
700 final boolean phoneIsInUse = phoneIsInUse();
701 if (phoneIsInUse || isDialIntent(intent)) {
702 mDialpadFragment.setStartedFromNewIntent(true);
703 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
704 mInCallDialpadUp = true;
705 }
706 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700707 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700708 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700709 }
710
711 @Override
712 public void onNewIntent(Intent newIntent) {
713 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700714 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700715
Chiao Cheng94b10b52012-08-17 16:59:12 -0700716 invalidateOptionsMenu();
717 }
718
719 /** Returns true if the given intent contains a phone number to populate the dialer with */
720 private boolean isDialIntent(Intent intent) {
721 final String action = intent.getAction();
722 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
723 return true;
724 }
725 if (Intent.ACTION_VIEW.equals(action)) {
726 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700727 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728 return true;
729 }
730 }
731 return false;
732 }
733
734 /**
735 * Returns an appropriate call origin for this Activity. May return null when no call origin
736 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
737 * for remembering the tab in which the user made a phone call, so the external app's DIAL
738 * request should not be counted.)
739 */
740 public String getCallOrigin() {
741 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
742 }
743
Chiao Cheng94b10b52012-08-17 16:59:12 -0700744 /**
Yorke Leec3766332013-07-31 11:13:16 -0700745 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700746 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700747 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700748 if (getFragmentManager().isDestroyed()) {
749 // Weird race condition where fragment is doing work after the activity is destroyed
750 // due to talkback being on (b/10209937). Just return since we can't do any
751 // constructive here.
752 return;
753 }
754
Yorke Leeef2b7382013-08-09 17:39:25 -0700755 if (DEBUG) {
756 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
757 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700758
Yorke Leec3766332013-07-31 11:13:16 -0700759 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700760 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700761 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700762 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700763 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700764 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700765
Yorke Leeb207f8a2013-08-29 18:51:06 -0700766 final String tag;
767 if (smartDialSearch) {
768 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
769 } else {
770 tag = TAG_REGULAR_SEARCH_FRAGMENT;
771 }
772 mInDialpadSearch = smartDialSearch;
773 mInRegularSearch = !smartDialSearch;
774
Andrew Lee99a570c2014-05-15 14:18:50 -0700775 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700776 if (fragment == null) {
777 if (smartDialSearch) {
778 fragment = new SmartDialSearchFragment();
779 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700780 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700781 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700782 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700783 // DialtactsActivity will provide the options menu
784 fragment.setHasOptionsMenu(false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700785 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700786 transaction.addToBackStack(null);
Andrew Lee99a570c2014-05-15 14:18:50 -0700787 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700788 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700789 }
790
791 /**
Yorke Leec3766332013-07-31 11:13:16 -0700792 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700793 */
Yorke Leec3766332013-07-31 11:13:16 -0700794 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700795 // See related bug in enterSearchUI();
796 if (getFragmentManager().isDestroyed()) {
797 return;
798 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700799 // Go all the way back to the favorites fragment, regardless of how many times we
800 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700801 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
802 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700803
Andrew Lee2a58ab82014-05-15 02:16:04 -0700804 if (mIsDialpadShown) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700805 mFragmentsFrame.setAlpha(0);
806 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700807 }
808
809 /** Returns an Intent to launch Call Settings screen */
810 public static Intent getCallSettingsIntent() {
811 final Intent intent = new Intent(Intent.ACTION_MAIN);
812 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
813 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
814 return intent;
815 }
816
817 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700818 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700819 if (mIsDialpadShown) {
Yorke Leef6673d32013-08-23 15:34:17 -0700820 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700821 } else if (getInSearchUi()) {
Yorke Lee53a22302014-04-29 18:13:46 -0700822 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700823 mDialpadFragment.clearDialpad();
Andrew Lee99a570c2014-05-15 14:18:50 -0700824 exitSearchUi();
Yorke Leec3766332013-07-31 11:13:16 -0700825 } else {
826 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700827 }
Yorke Leec3766332013-07-31 11:13:16 -0700828 }
829
830 @Override
831 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700832 if (mSmartDialSearchFragment != null) {
833 mSmartDialSearchFragment.setAddToContactNumber(query);
834 }
Yorke Leec3766332013-07-31 11:13:16 -0700835 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
836 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700837
838 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700839 if (DEBUG) {
840 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
841 }
842 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
843 // This callback can happen if the dialpad fragment is recreated because of
844 // activity destruction. In that case, don't update the search view because
845 // that would bring the user back to the search fragment regardless of the
846 // previous state of the application. Instead, just return here and let the
847 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700848 if (!TextUtils.isEmpty(normalizedQuery)) {
849 mPendingSearchViewQuery = normalizedQuery;
850 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700851 return;
852 }
Yorke Lee53a22302014-04-29 18:13:46 -0700853 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700854 }
855 }
Yorke Leec3766332013-07-31 11:13:16 -0700856
857 @Override
858 public void onListFragmentScrollStateChange(int scrollState) {
859 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700860 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700861 hideInputMethod(getCurrentFocus());
862 }
863 }
864
865 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700866 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700867 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700868 // TODO: No-op for now. This should eventually show/hide the actionBar based on
869 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700870 }
871
Yorke Lee86e21f72014-04-02 16:49:38 -0700872 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700873 public void setFloatingActionButtonVisible(boolean visible) {
874 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700875 }
876
877 private boolean phoneIsInUse() {
878 final TelephonyManager tm = (TelephonyManager) getSystemService(
879 Context.TELEPHONY_SERVICE);
880 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
881 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700882
Yorke Leee1424812013-09-04 18:24:48 -0700883 public static Intent getAddNumberToContactIntent(CharSequence text) {
884 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
885 intent.putExtra(Intents.Insert.PHONE, text);
886 intent.setType(Contacts.CONTENT_ITEM_TYPE);
887 return intent;
888 }
889
Yorke Leeda0f9042013-12-05 14:25:51 -0800890 private boolean canIntentBeHandled(Intent intent) {
891 final PackageManager packageManager = getPackageManager();
892 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
893 PackageManager.MATCH_DEFAULT_ONLY);
894 return resolveInfo != null && resolveInfo.size() > 0;
895 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800896
Yorke Lee6a1461a2014-04-21 16:27:14 -0700897 @Override
898 public void showCallHistory() {
899 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
900 // CONTENT_TYPE, so that we always open our call log from our dialer
901 final Intent intent = new Intent(this, CallLogActivity.class);
902 startActivity(intent);
903 }
904
Yorke Lee86e21f72014-04-02 16:49:38 -0700905 /**
906 * Called when the user has long-pressed a contact tile to start a drag operation.
907 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800908 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700909 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700910 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700911 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800912 }
913
914 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700915 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
916 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800917
Yorke Lee86e21f72014-04-02 16:49:38 -0700918 /**
919 * Called when the user has released a contact tile after long-pressing it.
920 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800921 @Override
922 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700923 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700924 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800925 }
926
927 @Override
928 public void onDroppedOnRemove() {}
929
930 /**
Yorke Leed999b712014-04-23 15:10:58 -0700931 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800932 * once it has been attached to the activity.
933 */
934 @Override
935 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -0700936 mDragDropController = dragController;
937 ((RemoveView) findViewById(R.id.remove_view))
938 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800939 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700940
941 @Override
942 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700943 // Specify call-origin so that users will see the previous tab instead of
944 // CallLog screen (search UI will be automatically exited).
945 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -0700946 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700947 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700948 }
949
950 @Override
951 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700952 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
953 startActivity(intent);
954 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700955 }
956
957 @Override
958 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700959 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -0700960 }
961
962 @Override
963 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700964 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700965 }
Yorke Lee33932ff2014-04-16 13:34:32 -0700966
967 public int getActionBarHeight() {
968 return mActionBarHeight;
969 }
Yorke Leecc4660d2014-04-24 11:22:57 -0700970
971 @Override
972 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
973
974 }
975
976 @Override
977 public void onPageSelected(int position) {
978 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -0700979 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -0700980 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -0700981 alignFloatingActionButtonByTab(mCurrentTabPosition);
982 }
Yorke Leecc4660d2014-04-24 11:22:57 -0700983 }
984
985 @Override
986 public void onPageScrollStateChanged(int state) {
987 }
988
Andrew Lee2a58ab82014-05-15 02:16:04 -0700989 private void updateFloatingActionButton() {
990 if (mIsDialpadShown) {
991 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
992 mFloatingActionButton.setContentDescription(
993 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -0700994 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700995 } else {
996 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
997 mFloatingActionButton.setContentDescription(
998 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -0700999 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001000 }
1001 }
1002
Yorke Leecc4660d2014-04-24 11:22:57 -07001003 private void alignFloatingActionButtonByTab(int position) {
1004 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1005 alignFloatingActionButtonMiddle();
1006 } else {
1007 alignFloatingActionButtonRight();
1008 }
1009 }
1010
1011 private void alignFloatingActionButtonRight() {
1012 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001013 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001014 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1015 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001016 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001017 }
1018
1019 private void alignFloatingActionButtonMiddle() {
1020 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001021 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001022 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1023 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001024 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001025 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001026
1027 /**
1028 * Convenience class which implements AnimationListener interface as null-op methods.
1029 */
1030 private class ActivityAnimationListener implements AnimationListener {
1031 @Override
1032 public void onAnimationStart(Animation animation) {
1033 }
1034
1035 @Override
1036 public void onAnimationEnd(Animation animation) {
1037 }
1038
1039 @Override
1040 public void onAnimationRepeat(Animation animation) {
1041 }
1042 }
Andrew Lee99a570c2014-05-15 14:18:50 -07001043}