blob: d89860008dffd33af1cdb47624179177857409a4 [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;
Andrew Leeb1903842014-05-15 16:11:24 -070045import android.view.KeyEvent;
Chiao Cheng94b10b52012-08-17 16:59:12 -070046import android.view.Menu;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.MenuItem;
Andrew Leeb1903842014-05-15 16:11:24 -070048import android.view.MotionEvent;
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;
Andrew Leeb1903842014-05-15 16:11:24 -070087import com.android.dialer.widget.SearchEditTextLayout;
Yorke Lee11ca39e2014-05-19 20:31:37 -070088import com.android.dialer.widget.SearchEditTextLayout.OnBackButtonClickedListener;
Yorke Lee0baa98b2013-08-22 10:55:16 -070089import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070090import com.android.internal.telephony.ITelephony;
91
Yorke Leec3766332013-07-31 11:13:16 -070092import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080093import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070094
Chiao Cheng94b10b52012-08-17 16:59:12 -070095/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070096 * The dialer tab's title is 'phone', a more common name (see strings.xml).
97 */
Yorke Leec3766332013-07-31 11:13:16 -070098public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070099 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700100 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -0800101 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700102 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700103 SpeedDialFragment.HostInterface,
Andrew Lee752956e2014-05-15 11:43:38 -0700104 OnDragDropListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700105 OnPhoneNumberPickerActionListener,
Andrew Leee74a10e2014-05-16 14:31:40 -0700106 PopupMenu.OnMenuItemClickListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700107 ViewPager.OnPageChangeListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700108 private static final String TAG = "DialtactsActivity";
109
Ihab Awad526c0b82014-02-27 12:55:36 -0800110 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700111
Yorke Leef74011e2013-08-02 11:30:30 -0700112 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
113
Chiao Cheng94b10b52012-08-17 16:59:12 -0700114 /** Used to open Call Setting */
115 private static final String PHONE_PACKAGE = "com.android.phone";
116 private static final String CALL_SETTINGS_CLASS_NAME =
117 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700118 /** @see #getCallOrigin() */
119 private static final String CALL_ORIGIN_DIALTACTS =
120 "com.android.dialer.DialtactsActivity";
121
Yorke Leeb207f8a2013-08-29 18:51:06 -0700122 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
123 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700124 private static final String KEY_SEARCH_QUERY = "search_query";
125 private static final String KEY_FIRST_LAUNCH = "first_launch";
126
Yorke Leec3766332013-07-31 11:13:16 -0700127 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
128 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
129 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
130 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700131
Chiao Cheng94b10b52012-08-17 16:59:12 -0700132 /**
133 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
134 */
135 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
136
Yorke Leec3766332013-07-31 11:13:16 -0700137 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700138
Yorke Lee86e21f72014-04-02 16:49:38 -0700139 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800140
Andrew Lee0c667702014-05-12 14:31:45 -0700141 private RelativeLayout parentLayout;
142
Yorke Leec3766332013-07-31 11:13:16 -0700143 /**
Yorke Leec3766332013-07-31 11:13:16 -0700144 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700145 */
Yorke Leef74011e2013-08-02 11:30:30 -0700146 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700147
148 /**
149 * Fragment for searching phone numbers using the alphanumeric keyboard.
150 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700151 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700152
153 /**
154 * Fragment for searching phone numbers using the dialpad.
155 */
156 private SmartDialSearchFragment mSmartDialSearchFragment;
157
Yorke Leee00c9fe2014-04-12 12:42:06 -0700158 /**
159 * Fragment containing the speed dial list, recents list, and all contacts list.
160 */
161 private ListsFragment mListsFragment;
162
Andrew Leea73e1892014-05-13 13:21:16 -0700163 private View mFloatingActionButtonContainer;
164 private ImageButton mFloatingActionButton;
Yorke Leec3766332013-07-31 11:13:16 -0700165
Yorke Lee33932ff2014-04-16 13:34:32 -0700166 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700167 private boolean mInDialpadSearch;
168 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700169 private boolean mClearSearchOnPause;
Andrew Lee2a58ab82014-05-15 02:16:04 -0700170 private boolean mIsDialpadShown;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700171
Yorke Leeef2b7382013-08-09 17:39:25 -0700172 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700173 * The position of the currently selected tab in the attached {@link ListsFragment}.
174 */
175 private int mCurrentTabPosition = 0;
176
177 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700178 * True if the dialpad is only temporarily showing due to being in call
179 */
180 private boolean mInCallDialpadUp;
181
182 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700183 * True when this activity has been launched for the first time.
184 */
Yorke Lee98702de2013-08-06 12:03:32 -0700185 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700186
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700187 /**
188 * Search query to be applied to the SearchView in the ActionBar once
189 * onCreateOptionsMenu has been called.
190 */
191 private String mPendingSearchViewQuery;
192
Yorke Lee53a22302014-04-29 18:13:46 -0700193 private EditText mSearchView;
Yorke Lee53a22302014-04-29 18:13:46 -0700194 private View mVoiceSearchButton;
Yorke Leeec489fb2014-05-19 15:39:27 -0700195 private SearchEditTextLayout mSearchEditTextLayout;
Andrew Leee74a10e2014-05-16 14:31:40 -0700196
Yorke Lee86e21f72014-04-02 16:49:38 -0700197 /**
198 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
199 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
200 * removed from the speed dial list.
201 */
Yorke Lee28266192014-05-01 10:05:52 -0700202 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700203
Yorke Lee86e21f72014-04-02 16:49:38 -0700204 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
205 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700206 private String mSearchQuery;
207
Yorke Leefce269a2013-08-12 11:56:04 -0700208 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700209 private DragDropController mDragDropController;
Yorke Leefce269a2013-08-12 11:56:04 -0700210
Andrew Leee74a10e2014-05-16 14:31:40 -0700211 private class OptionsPopupMenu extends PopupMenu {
212 public OptionsPopupMenu(Context context, View anchor) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700213 super(context, anchor);
214 }
215
216 @Override
217 public void show() {
218 final Menu menu = getMenu();
219 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Andrew Leee74a10e2014-05-16 14:31:40 -0700220 clearFrequents.setVisible(mListsFragment != null &&
221 mListsFragment.getSpeedDialFragment() != null &&
222 mListsFragment.getSpeedDialFragment().hasFrequents());
Yorke Leee00c9fe2014-04-12 12:42:06 -0700223 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
Andrew Lee90374a72014-05-16 15:01:09 -0700264 // Show search fragment only when the query string is changed to non-empty text.
265 if (!TextUtils.isEmpty(newText)) {
266 // Call enterSearchUi only if we are switching search modes, or showing a search
267 // fragment for the first time.
268 final boolean sameSearchMode = (mIsDialpadShown && mInDialpadSearch) ||
269 (!mIsDialpadShown && mInRegularSearch);
270 if (!sameSearchMode) {
271 enterSearchUi(mIsDialpadShown, mSearchQuery);
272 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700273 }
274
275 if (mIsDialpadShown && mSmartDialSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700276 mSmartDialSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700277 } else if (mRegularSearchFragment != null) {
Andrew Leeb1903842014-05-15 16:11:24 -0700278 mRegularSearchFragment.setQueryString(mSearchQuery, false /* delaySelection */);
Andrew Lee99a570c2014-05-15 14:18:50 -0700279 }
Andrew Lee99a570c2014-05-15 14:18:50 -0700280 }
281
282 @Override
283 public void afterTextChanged(Editable s) {
284 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700285 };
286
Andrew Leeb1903842014-05-15 16:11:24 -0700287
288 /**
Yorke Lee11ca39e2014-05-19 20:31:37 -0700289 * Open the search UI when the user clicks on the search box.
Andrew Leeb1903842014-05-15 16:11:24 -0700290 */
Yorke Lee11ca39e2014-05-19 20:31:37 -0700291 private final View.OnClickListener mSearchViewOnClickListener = new View.OnClickListener() {
Andrew Leeb1903842014-05-15 16:11:24 -0700292 @Override
Yorke Lee11ca39e2014-05-19 20:31:37 -0700293 public void onClick(View v) {
Andrew Leeb1903842014-05-15 16:11:24 -0700294 if (!isInSearchUi()) {
295 enterSearchUi(false /* smartDialSearch */, mSearchView.getText().toString());
296 }
Andrew Leeb1903842014-05-15 16:11:24 -0700297 }
298 };
299
300 /**
301 * If the search term is empty and the user closes the soft keyboard, close the search UI.
302 */
303 private final View.OnKeyListener mSearchEditTextLayoutListener = new View.OnKeyListener() {
304 @Override
305 public boolean onKey(View v, int keyCode, KeyEvent event) {
306 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
307 TextUtils.isEmpty(mSearchView.getText().toString())) {
308 onBackPressed();
309 }
310 return false;
311 }
312 };
313
Chiao Cheng94b10b52012-08-17 16:59:12 -0700314 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700315 protected void onCreate(Bundle savedInstanceState) {
316 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700317 mFirstLaunch = true;
318
Yorke Lee8898cd02013-08-08 10:24:27 -0700319 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800320 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700321
Yorke Lee53a22302014-04-29 18:13:46 -0700322 final ActionBar actionBar = getActionBar();
323 actionBar.setCustomView(R.layout.search_edittext);
324 actionBar.setDisplayShowCustomEnabled(true);
325
Yorke Leeec489fb2014-05-19 15:39:27 -0700326 mSearchEditTextLayout = (SearchEditTextLayout) actionBar.getCustomView();
327 mSearchEditTextLayout.setPreImeKeyListener(mSearchEditTextLayoutListener);
Yorke Lee53a22302014-04-29 18:13:46 -0700328
Yorke Leeec489fb2014-05-19 15:39:27 -0700329 mSearchView = (EditText) mSearchEditTextLayout.findViewById(R.id.search_view);
Yorke Lee53a22302014-04-29 18:13:46 -0700330 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
Yorke Lee11ca39e2014-05-19 20:31:37 -0700331 mVoiceSearchButton = mSearchEditTextLayout.findViewById(R.id.voice_search_button);
332 mSearchEditTextLayout.findViewById(R.id.search_box_start_search).setOnClickListener(
333 mSearchViewOnClickListener);
334 mSearchEditTextLayout.setOnBackButtonClickedListener(new OnBackButtonClickedListener() {
335 @Override
336 public void onBackButtonClicked() {
337 onBackPressed();
338 }
339 });
Chiao Cheng94b10b52012-08-17 16:59:12 -0700340
Yorke Leeec489fb2014-05-19 15:39:27 -0700341 ImageButton optionsMenuButton = (ImageButton) mSearchEditTextLayout.findViewById(
342 R.id.dialtacts_options_menu_button);
Andrew Leee74a10e2014-05-16 14:31:40 -0700343 optionsMenuButton.setOnClickListener(this);
344 final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
345 optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
346
Yorke Lee33932ff2014-04-16 13:34:32 -0700347 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
348 new int[] { android.R.attr.actionBarSize });
349 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
350 styledAttributes.recycle();
351
Yorke Leeef2b7382013-08-09 17:39:25 -0700352 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
353 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700354 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800355 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700356 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800357 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
358 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700359 } else {
360 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700361 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
362 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700363 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700364 }
365
Andrew Lee0c667702014-05-12 14:31:45 -0700366 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
367 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
368 parentLayout.setOnDragListener(new LayoutOnDragListener());
369
Andrew Leea73e1892014-05-13 13:21:16 -0700370 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
371 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700372
Andrew Leea73e1892014-05-13 13:21:16 -0700373 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
374 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800375
Yorke Lee28266192014-05-01 10:05:52 -0700376 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800377
Yorke Lee0baa98b2013-08-22 10:55:16 -0700378 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700379 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700380 }
381
382 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700383 protected void onResume() {
384 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700385 if (mFirstLaunch) {
386 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700387 } else if (!phoneIsInUse() && mInCallDialpadUp) {
388 hideDialpadFragment(false, true);
389 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700390 }
391 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700392 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700393 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700394 }
395
396 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700397 protected void onPause() {
398 if (mClearSearchOnPause) {
399 hideDialpadAndSearchUi();
400 mClearSearchOnPause = false;
401 }
402 super.onPause();
403 }
404
405 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700406 protected void onSaveInstanceState(Bundle outState) {
407 super.onSaveInstanceState(outState);
408 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700409 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
410 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700411 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
412 }
413
414 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700415 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700416 if (fragment instanceof DialpadFragment) {
417 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700418 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700419 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700420 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700421 } else if (fragment instanceof SmartDialSearchFragment) {
422 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700423 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leeef2b7382013-08-09 17:39:25 -0700424 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700425 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700426 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700427 } else if (fragment instanceof ListsFragment) {
428 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700429 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700430 }
Yorke Leec3766332013-07-31 11:13:16 -0700431 }
432
Alon Albertb453e5b2013-09-10 15:54:23 -0700433 protected void handleMenuSettings() {
434 openTelephonySetting(this);
435 }
436
437 public static void openTelephonySetting(Activity activity) {
438 final Intent settingsIntent = getCallSettingsIntent();
439 activity.startActivity(settingsIntent);
440 }
441
Chiao Cheng94b10b52012-08-17 16:59:12 -0700442 @Override
443 public void onClick(View view) {
444 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700445 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700446 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700447 mInCallDialpadUp = false;
448 showDialpadFragment(true);
449 } else {
450 // Dial button was pressed; tell the Dialpad fragment
451 mDialpadFragment.dialButtonPressed();
452 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700453 break;
Yorke Leec3766332013-07-31 11:13:16 -0700454 case R.id.search_close_button:
455 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700456 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700457 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700458 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700459 }
460 break;
Yorke Leec3766332013-07-31 11:13:16 -0700461 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800462 try {
463 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
464 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
465 } catch (ActivityNotFoundException e) {
466 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
467 Toast.LENGTH_SHORT).show();
468 }
Yorke Leec3766332013-07-31 11:13:16 -0700469 break;
Andrew Leee74a10e2014-05-16 14:31:40 -0700470 case R.id.dialtacts_options_menu_button:
471 buildOptionsMenu(view).show();
472 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700473 default: {
474 Log.wtf(TAG, "Unexpected onClick event from " + view);
475 break;
476 }
477 }
478 }
479
Yorke Leec3766332013-07-31 11:13:16 -0700480 @Override
Andrew Leee74a10e2014-05-16 14:31:40 -0700481 public boolean onMenuItemClick(MenuItem item) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700482 switch (item.getItemId()) {
483 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700484 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700485 break;
486 case R.id.menu_add_contact:
487 try {
488 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
489 } catch (ActivityNotFoundException e) {
490 Toast toast = Toast.makeText(this,
491 R.string.add_contact_not_available,
492 Toast.LENGTH_SHORT);
493 toast.show();
494 }
495 break;
496 case R.id.menu_import_export:
497 // We hard-code the "contactsAreAvailable" argument because doing it properly would
498 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
499 // now in Dialtacts for (potential) performance reasons. Compare with how it is
500 // done in {@link PeopleActivity}.
501 ImportExportDialogFragment.show(getFragmentManager(), true,
502 DialtactsActivity.class);
503 return true;
504 case R.id.menu_clear_frequents:
Yorke Lee86e21f72014-04-02 16:49:38 -0700505 ClearFrequentsDialog.show(getFragmentManager());
506 return true;
507 case R.id.menu_call_settings:
508 handleMenuSettings();
509 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700510 }
511 return false;
512 }
513
514 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700515 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
516 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
517 if (resultCode == RESULT_OK) {
518 final ArrayList<String> matches = data.getStringArrayListExtra(
519 RecognizerIntent.EXTRA_RESULTS);
520 if (matches.size() > 0) {
521 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700522 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700523 } else {
524 Log.e(TAG, "Voice search - nothing heard");
525 }
526 } else {
527 Log.e(TAG, "Voice search failed");
528 }
529 }
530 super.onActivityResult(requestCode, resultCode, data);
531 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700532
Andrew Lee2a58ab82014-05-15 02:16:04 -0700533 /**
534 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
535 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
536 * @see #onDialpadShown
537 */
Yorke Leec3766332013-07-31 11:13:16 -0700538 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700539 if (mIsDialpadShown) {
540 return;
541 }
542 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700543 mDialpadFragment.setAnimate(animate);
544
Chiao Cheng94b10b52012-08-17 16:59:12 -0700545 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700546 ft.show(mDialpadFragment);
547 ft.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700548
549 if (!isInSearchUi()) {
550 enterSearchUi(true /* isSmartDial */, mSearchQuery);
551 }
Yorke Leec3766332013-07-31 11:13:16 -0700552 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700553
Andrew Lee2a58ab82014-05-15 02:16:04 -0700554 /**
555 * Callback from child DialpadFragment when the dialpad is shown.
556 */
557 public void onDialpadShown() {
558 updateFloatingActionButton();
559 if (mDialpadFragment.getAnimate()) {
560 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
561 mDialpadFragment.getView().startAnimation(slideIn);
562 } else {
563 mDialpadFragment.setYFraction(0);
564 }
565
Andrew Lee2a58ab82014-05-15 02:16:04 -0700566 updateSearchFragmentPosition();
567 getActionBar().hide();
568 }
569
570 /**
571 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
572 * a callback after the hide animation ends.
573 * @see #commitDialpadFragmentHide
574 */
Yorke Leeca195042013-09-25 16:29:38 -0700575 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700576 if (mDialpadFragment == null) {
577 return;
578 }
Yorke Leef6673d32013-08-23 15:34:17 -0700579 if (clearDialpad) {
580 mDialpadFragment.clearDialpad();
581 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700582 if (!mIsDialpadShown) {
583 return;
Yorke Leec3766332013-07-31 11:13:16 -0700584 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700585 mIsDialpadShown = false;
586 mDialpadFragment.setAnimate(animate);
587
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700588 updateSearchFragmentPosition();
Andrew Lee2a58ab82014-05-15 02:16:04 -0700589 updateFloatingActionButton();
590 if (animate) {
591 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
592 slideOut.setAnimationListener(new ActivityAnimationListener() {
593 @Override
594 public void onAnimationEnd(Animation animation) {
595 commitDialpadFragmentHide();
596 }
597 });
598 mDialpadFragment.getView().startAnimation(slideOut);
599 } else {
600 commitDialpadFragmentHide();
601 }
602
Andrew Leeb1903842014-05-15 16:11:24 -0700603 mListsFragment.maybeShowActionBar();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700604
Andrew Leed4cde832014-05-16 15:56:48 -0700605 if (isInSearchUi()) {
Andrew Lee44e3daf2014-05-19 14:28:16 -0700606 if (TextUtils.isEmpty(mSearchQuery)) {
607 exitSearchUi();
Andrew Lee44e3daf2014-05-19 14:28:16 -0700608 }
Andrew Leed4cde832014-05-16 15:56:48 -0700609 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700610 }
611
612 /**
613 * Finishes hiding the dialpad fragment after any animations are completed.
614 */
615 private void commitDialpadFragmentHide() {
616 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700617 ft.hide(mDialpadFragment);
618 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700619 }
620
Andrew Lee2a58ab82014-05-15 02:16:04 -0700621 private void updateSearchFragmentPosition() {
622 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
623 SearchFragment fragment = null;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700624 if (mSmartDialSearchFragment != null && mSmartDialSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700625 fragment = mSmartDialSearchFragment;
Andrew Leedbe9a8c2014-05-16 15:18:40 -0700626 } else if (mRegularSearchFragment != null && mRegularSearchFragment.isVisible()) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700627 fragment = mRegularSearchFragment;
628 }
629 if (fragment != null && fragment.isVisible()) {
630 fragment.getView().animate().translationY(translationValue)
631 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
632 }
633 }
634
Andrew Leeb1903842014-05-15 16:11:24 -0700635 private boolean isInSearchUi() {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700636 return mInDialpadSearch || mInRegularSearch;
637 }
638
639 private void setNotInSearchUi() {
640 mInDialpadSearch = false;
641 mInRegularSearch = false;
642 }
643
Yorke Lee311969c2013-08-26 06:31:11 -0700644 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700645 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700646 hideDialpadFragment(false, true);
647 }
648
Yorke Leee00c9fe2014-04-12 12:42:06 -0700649 private void hideInputMethod(View view) {
650 final InputMethodManager imm = (InputMethodManager) getSystemService(
651 Context.INPUT_METHOD_SERVICE);
652 if (imm != null && view != null) {
653 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
654 }
655 }
656
Yorke Lee53a22302014-04-29 18:13:46 -0700657 private void prepareVoiceSearchButton() {
Yorke Lee53a22302014-04-29 18:13:46 -0700658 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
659 if (canIntentBeHandled(voiceIntent)) {
660 mVoiceSearchButton.setVisibility(View.VISIBLE);
661 mVoiceSearchButton.setOnClickListener(this);
662 } else {
663 mVoiceSearchButton.setVisibility(View.GONE);
664 }
665 }
666
Andrew Leee74a10e2014-05-16 14:31:40 -0700667 private OptionsPopupMenu buildOptionsMenu(View invoker) {
668 final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
669 popupMenu.inflate(R.menu.dialtacts_options);
670 popupMenu.setOnMenuItemClickListener(this);
671 return popupMenu;
672 }
673
Yorke Lee86e21f72014-04-02 16:49:38 -0700674 @Override
675 public boolean onCreateOptionsMenu(Menu menu) {
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700676 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700677 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700678 mPendingSearchViewQuery = null;
679 }
Andrew Leee74a10e2014-05-16 14:31:40 -0700680 return false;
Ihab Awadfb00cb82014-03-18 16:19:00 -0700681 }
682
Chiao Cheng94b10b52012-08-17 16:59:12 -0700683 /**
684 * Returns true if the intent is due to hitting the green send key (hardware call button:
685 * KEYCODE_CALL) while in a call.
686 *
687 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700688 * @return true if the intent is due to hitting the green send key while in a call
689 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700690 private boolean isSendKeyWhileInCall(Intent intent) {
691 // If there is a call in progress and the user launched the dialer by hitting the call
692 // button, go straight to the in-call screen.
693 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700694
Yorke Lee62e995c2014-03-28 10:02:56 -0700695 try {
696 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
697 if (callKey && phone != null && phone.showCallScreen()) {
698 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700699 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700700 } catch (RemoteException e) {
701 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700702 }
703
704 return false;
705 }
706
707 /**
708 * Sets the current tab based on the intent's request type
709 *
710 * @param intent Intent that contains information about which tab should be selected
711 */
Yorke Leec3766332013-07-31 11:13:16 -0700712 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700713 // If we got here by hitting send and we're in call forward along to the in-call activity
Yorke Lee62e995c2014-03-28 10:02:56 -0700714 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700715 finish();
716 return;
717 }
718
Yorke Lee669b6082013-09-17 15:43:38 -0700719 if (mDialpadFragment != null) {
720 final boolean phoneIsInUse = phoneIsInUse();
721 if (phoneIsInUse || isDialIntent(intent)) {
722 mDialpadFragment.setStartedFromNewIntent(true);
723 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
724 mInCallDialpadUp = true;
725 }
726 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700727 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700728 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700729 }
730
731 @Override
732 public void onNewIntent(Intent newIntent) {
733 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700734 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700735
Chiao Cheng94b10b52012-08-17 16:59:12 -0700736 invalidateOptionsMenu();
737 }
738
739 /** Returns true if the given intent contains a phone number to populate the dialer with */
740 private boolean isDialIntent(Intent intent) {
741 final String action = intent.getAction();
742 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
743 return true;
744 }
745 if (Intent.ACTION_VIEW.equals(action)) {
746 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700747 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700748 return true;
749 }
750 }
751 return false;
752 }
753
754 /**
755 * Returns an appropriate call origin for this Activity. May return null when no call origin
756 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
757 * for remembering the tab in which the user made a phone call, so the external app's DIAL
758 * request should not be counted.)
759 */
760 public String getCallOrigin() {
761 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
762 }
763
Chiao Cheng94b10b52012-08-17 16:59:12 -0700764 /**
Yorke Leec3766332013-07-31 11:13:16 -0700765 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700766 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700767 private void enterSearchUi(boolean smartDialSearch, String query) {
Andrew Lee90374a72014-05-16 15:01:09 -0700768 if (getFragmentManager().isDestroyed()) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700769 // Weird race condition where fragment is doing work after the activity is destroyed
770 // due to talkback being on (b/10209937). Just return since we can't do any
771 // constructive here.
772 return;
773 }
774
Yorke Leeef2b7382013-08-09 17:39:25 -0700775 if (DEBUG) {
776 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
777 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700778
Yorke Leec3766332013-07-31 11:13:16 -0700779 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700780 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700781 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700782 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700783 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700784 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700785
Yorke Leeb207f8a2013-08-29 18:51:06 -0700786 final String tag;
787 if (smartDialSearch) {
788 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
789 } else {
790 tag = TAG_REGULAR_SEARCH_FRAGMENT;
791 }
792 mInDialpadSearch = smartDialSearch;
793 mInRegularSearch = !smartDialSearch;
794
Andrew Lee752956e2014-05-15 11:43:38 -0700795 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Andrew Leeb1903842014-05-15 16:11:24 -0700796 transaction.setCustomAnimations(android.R.animator.fade_in, 0);
Yorke Leeef2b7382013-08-09 17:39:25 -0700797 if (fragment == null) {
798 if (smartDialSearch) {
799 fragment = new SmartDialSearchFragment();
800 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700801 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700802 }
Andrew Leeb1903842014-05-15 16:11:24 -0700803 transaction.add(R.id.dialtacts_frame, fragment, tag);
804 } else {
805 transaction.show(fragment);
Yorke Leeef2b7382013-08-09 17:39:25 -0700806 }
Andrew Leeb1903842014-05-15 16:11:24 -0700807
Yorke Lee86e21f72014-04-02 16:49:38 -0700808 // DialtactsActivity will provide the options menu
809 fragment.setHasOptionsMenu(false);
Andrew Leeb1903842014-05-15 16:11:24 -0700810 fragment.setShowEmptyListForNullQuery(true);
Andrew Lee99a570c2014-05-15 14:18:50 -0700811 fragment.setQueryString(query, false /* delaySelection */);
Yorke Leeef2b7382013-08-09 17:39:25 -0700812 transaction.commit();
Andrew Leeb1903842014-05-15 16:11:24 -0700813
814 mListsFragment.getView().animate().alpha(0).withLayer();
Yorke Leeec489fb2014-05-19 15:39:27 -0700815 mSearchEditTextLayout.animateExpandOrCollapse(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700816 }
817
818 /**
Yorke Leec3766332013-07-31 11:13:16 -0700819 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700820 */
Yorke Leec3766332013-07-31 11:13:16 -0700821 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700822 // See related bug in enterSearchUI();
823 if (getFragmentManager().isDestroyed()) {
824 return;
825 }
Andrew Leeb1903842014-05-15 16:11:24 -0700826
Andrew Leeb1903842014-05-15 16:11:24 -0700827 mSearchView.setText(null);
828 mDialpadFragment.clearDialpad();
Yorke Leeb207f8a2013-08-29 18:51:06 -0700829 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700830
Andrew Leeb1903842014-05-15 16:11:24 -0700831 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
832 transaction.setCustomAnimations(0, android.R.animator.fade_out);
Andrew Leeb1903842014-05-15 16:11:24 -0700833 if (mSmartDialSearchFragment != null) {
834 transaction.remove(mSmartDialSearchFragment);
Yorke Lee7eccf452014-03-14 12:52:42 -0700835 }
Andrew Leeb1903842014-05-15 16:11:24 -0700836 if (mRegularSearchFragment != null) {
837 transaction.remove(mRegularSearchFragment);
838 }
839 transaction.commit();
840
841 mListsFragment.getView().animate().alpha(1).withLayer();
Yorke Leeec489fb2014-05-19 15:39:27 -0700842 mSearchEditTextLayout.animateExpandOrCollapse(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700843 }
844
845 /** Returns an Intent to launch Call Settings screen */
846 public static Intent getCallSettingsIntent() {
847 final Intent intent = new Intent(Intent.ACTION_MAIN);
848 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
849 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
850 return intent;
851 }
852
853 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700854 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700855 if (mIsDialpadShown) {
Andrew Leeb1903842014-05-15 16:11:24 -0700856 if (TextUtils.isEmpty(mSearchQuery)) {
857 exitSearchUi();
858 }
Yorke Leef6673d32013-08-23 15:34:17 -0700859 hideDialpadFragment(true, false);
Andrew Leeb1903842014-05-15 16:11:24 -0700860 } else if (isInSearchUi()) {
Andrew Lee99a570c2014-05-15 14:18:50 -0700861 exitSearchUi();
Yorke Lee11ca39e2014-05-19 20:31:37 -0700862 hideInputMethod(parentLayout);
Yorke Leec3766332013-07-31 11:13:16 -0700863 } else {
864 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700865 }
Yorke Leec3766332013-07-31 11:13:16 -0700866 }
867
868 @Override
869 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700870 if (mSmartDialSearchFragment != null) {
871 mSmartDialSearchFragment.setAddToContactNumber(query);
872 }
Yorke Leec3766332013-07-31 11:13:16 -0700873 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
874 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700875
876 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700877 if (DEBUG) {
878 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
879 }
880 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
881 // This callback can happen if the dialpad fragment is recreated because of
882 // activity destruction. In that case, don't update the search view because
883 // that would bring the user back to the search fragment regardless of the
884 // previous state of the application. Instead, just return here and let the
885 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700886 if (!TextUtils.isEmpty(normalizedQuery)) {
887 mPendingSearchViewQuery = normalizedQuery;
888 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700889 return;
890 }
Yorke Lee53a22302014-04-29 18:13:46 -0700891 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700892 }
893 }
Yorke Leec3766332013-07-31 11:13:16 -0700894
895 @Override
896 public void onListFragmentScrollStateChange(int scrollState) {
897 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700898 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700899 hideInputMethod(getCurrentFocus());
900 }
901 }
902
903 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700904 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
Andrew Lee99a570c2014-05-15 14:18:50 -0700905 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700906 // TODO: No-op for now. This should eventually show/hide the actionBar based on
907 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700908 }
909
Yorke Lee86e21f72014-04-02 16:49:38 -0700910 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700911 public void setFloatingActionButtonVisible(boolean visible) {
912 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700913 }
914
915 private boolean phoneIsInUse() {
916 final TelephonyManager tm = (TelephonyManager) getSystemService(
917 Context.TELEPHONY_SERVICE);
918 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
919 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700920
Yorke Leee1424812013-09-04 18:24:48 -0700921 public static Intent getAddNumberToContactIntent(CharSequence text) {
922 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
923 intent.putExtra(Intents.Insert.PHONE, text);
924 intent.setType(Contacts.CONTENT_ITEM_TYPE);
925 return intent;
926 }
927
Yorke Leeda0f9042013-12-05 14:25:51 -0800928 private boolean canIntentBeHandled(Intent intent) {
929 final PackageManager packageManager = getPackageManager();
930 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
931 PackageManager.MATCH_DEFAULT_ONLY);
932 return resolveInfo != null && resolveInfo.size() > 0;
933 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800934
Yorke Lee6a1461a2014-04-21 16:27:14 -0700935 @Override
936 public void showCallHistory() {
937 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
938 // CONTENT_TYPE, so that we always open our call log from our dialer
939 final Intent intent = new Intent(this, CallLogActivity.class);
940 startActivity(intent);
941 }
942
Yorke Lee86e21f72014-04-02 16:49:38 -0700943 /**
944 * Called when the user has long-pressed a contact tile to start a drag operation.
945 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800946 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700947 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700948 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700949 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800950 }
951
952 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700953 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
954 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800955
Yorke Lee86e21f72014-04-02 16:49:38 -0700956 /**
957 * Called when the user has released a contact tile after long-pressing it.
958 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800959 @Override
960 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700961 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700962 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800963 }
964
965 @Override
966 public void onDroppedOnRemove() {}
967
968 /**
Yorke Leed999b712014-04-23 15:10:58 -0700969 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800970 * once it has been attached to the activity.
971 */
972 @Override
973 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -0700974 mDragDropController = dragController;
975 ((RemoveView) findViewById(R.id.remove_view))
976 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800977 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700978
979 @Override
980 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700981 // Specify call-origin so that users will see the previous tab instead of
982 // CallLog screen (search UI will be automatically exited).
983 PhoneNumberInteraction.startInteractionForPhoneCall(
Andrew Lee99a570c2014-05-15 14:18:50 -0700984 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700985 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700986 }
987
988 @Override
989 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700990 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
991 startActivity(intent);
992 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700993 }
994
995 @Override
996 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700997 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -0700998 }
999
1000 @Override
1001 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -07001002 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -07001003 }
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.
Andrew Lee2a58ab82014-05-15 02:16:04 -07001018 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -07001019 alignFloatingActionButtonByTab(mCurrentTabPosition);
1020 }
Yorke Leecc4660d2014-04-24 11:22:57 -07001021 }
1022
1023 @Override
1024 public void onPageScrollStateChanged(int state) {
1025 }
1026
Andrew Lee2a58ab82014-05-15 02:16:04 -07001027 private void updateFloatingActionButton() {
1028 if (mIsDialpadShown) {
1029 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
1030 mFloatingActionButton.setContentDescription(
1031 getResources().getString(R.string.description_dial_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001032 alignFloatingActionButtonMiddle();
Andrew Lee2a58ab82014-05-15 02:16:04 -07001033 } else {
1034 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
1035 mFloatingActionButton.setContentDescription(
1036 getResources().getString(R.string.action_menu_dialpad_button));
Andrew Leec4643f72014-05-15 11:27:39 -07001037 alignFloatingActionButtonByTab(mCurrentTabPosition);
Andrew Lee2a58ab82014-05-15 02:16:04 -07001038 }
1039 }
1040
Yorke Leecc4660d2014-04-24 11:22:57 -07001041 private void alignFloatingActionButtonByTab(int position) {
1042 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1043 alignFloatingActionButtonMiddle();
1044 } else {
1045 alignFloatingActionButtonRight();
1046 }
1047 }
1048
1049 private void alignFloatingActionButtonRight() {
1050 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001051 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001052 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1053 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001054 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001055 }
1056
1057 private void alignFloatingActionButtonMiddle() {
1058 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001059 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001060 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1061 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001062 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001063 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001064
1065 /**
1066 * Convenience class which implements AnimationListener interface as null-op methods.
1067 */
1068 private class ActivityAnimationListener implements AnimationListener {
1069 @Override
1070 public void onAnimationStart(Animation animation) {
1071 }
1072
1073 @Override
1074 public void onAnimationEnd(Animation animation) {
1075 }
1076
1077 @Override
1078 public void onAnimationRepeat(Animation animation) {
1079 }
1080 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001081}