blob: ed92add3805a2a9612310b35beb6b6cbeb7297fa [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 Lee752956e2014-05-15 11:43:38 -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() {
Yorke Leec3766332013-07-31 11:13:16 -0700246 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700247 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Yorke Leec3766332013-07-31 11:13:16 -0700248 }
249
250 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700251 public void onTextChanged(CharSequence s, int start, int before, int count) {
252 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700253 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;
Yorke Leeef2b7382013-08-09 17:39:25 -0700258 }
259 mSearchQuery = newText;
260 if (DEBUG) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700261 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leeef2b7382013-08-09 17:39:25 -0700262 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700263 final boolean dialpadSearch = mIsDialpadShown;
Yorke Leeef2b7382013-08-09 17:39:25 -0700264
Yorke Leec3766332013-07-31 11:13:16 -0700265 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700266 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700267 exitSearchUi();
Yorke Lee53a22302014-04-29 18:13:46 -0700268 mSearchViewCloseButton.setVisibility(View.GONE);
269 mVoiceSearchButton.setVisibility(View.VISIBLE);
270 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700271 } else if (!TextUtils.isEmpty(newText)) {
272 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
273 (!dialpadSearch && mInRegularSearch);
274 if (!sameSearchMode) {
275 // call enterSearchUi only if we are switching search modes, or entering
276 // search ui for the first time
277 enterSearchUi(dialpadSearch, newText);
278 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700279
Yorke Leeb207f8a2013-08-29 18:51:06 -0700280 if (dialpadSearch && mSmartDialSearchFragment != null) {
281 mSmartDialSearchFragment.setQueryString(newText, false);
282 } else if (mRegularSearchFragment != null) {
283 mRegularSearchFragment.setQueryString(newText, false);
284 }
Yorke Lee53a22302014-04-29 18:13:46 -0700285 mSearchViewCloseButton.setVisibility(View.VISIBLE);
286 mVoiceSearchButton.setVisibility(View.GONE);
287 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700288 }
Yorke Lee53a22302014-04-29 18:13:46 -0700289 return;
290 }
291
292 @Override
293 public void afterTextChanged(Editable s) {
Yorke Leec3766332013-07-31 11:13:16 -0700294 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700295 };
296
Chiao Cheng94b10b52012-08-17 16:59:12 -0700297 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700298 protected void onCreate(Bundle savedInstanceState) {
299 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700300 mFirstLaunch = true;
301
Yorke Lee8898cd02013-08-08 10:24:27 -0700302 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800303 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700304
Yorke Lee53a22302014-04-29 18:13:46 -0700305 final ActionBar actionBar = getActionBar();
306 actionBar.setCustomView(R.layout.search_edittext);
307 actionBar.setDisplayShowCustomEnabled(true);
308
309 final View customView = actionBar.getCustomView();
310
311 mSearchView = (EditText) customView.findViewById(R.id.search_view);
312 mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
313
314 mSearchViewCloseButton = customView.findViewById(R.id.search_close_button);
315 mSearchViewCloseButton.setOnClickListener(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700316
Yorke Lee33932ff2014-04-16 13:34:32 -0700317 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
318 new int[] { android.R.attr.actionBarSize });
319 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
320 styledAttributes.recycle();
321
Yorke Leeef2b7382013-08-09 17:39:25 -0700322 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
323 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700324 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800325 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700326 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800327 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
328 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700329 } else {
330 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700331 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
332 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700333 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700334 }
335
Andrew Lee0c667702014-05-12 14:31:45 -0700336 parentLayout = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
337 parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
338 parentLayout.setOnDragListener(new LayoutOnDragListener());
339
340 mDialerBackgroundColor = getResources().getColor(R.color.background_dialer_light);
341 mContactListBackgroundColor =
342 getResources().getColor(R.color.contact_list_background_color);
Yorke Leecc4660d2014-04-24 11:22:57 -0700343
Yorke Leeb207f8a2013-08-29 18:51:06 -0700344 mFragmentsFrame = findViewById(R.id.dialtacts_frame);
Yorke Lee86e21f72014-04-02 16:49:38 -0700345
Andrew Leea73e1892014-05-13 13:21:16 -0700346 mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
347 ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700348
Andrew Leea73e1892014-05-13 13:21:16 -0700349 mFloatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
350 mFloatingActionButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800351
Yorke Lee28266192014-05-01 10:05:52 -0700352 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800353
Yorke Lee0baa98b2013-08-22 10:55:16 -0700354 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700355 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700356 }
357
358 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700359 protected void onResume() {
360 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700361 if (mFirstLaunch) {
362 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700363 } else if (!phoneIsInUse() && mInCallDialpadUp) {
364 hideDialpadFragment(false, true);
365 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700366 }
367 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700368 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700369 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700370 }
371
372 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700373 protected void onPause() {
374 if (mClearSearchOnPause) {
375 hideDialpadAndSearchUi();
376 mClearSearchOnPause = false;
377 }
378 super.onPause();
379 }
380
381 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700382 protected void onSaveInstanceState(Bundle outState) {
383 super.onSaveInstanceState(outState);
384 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700385 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
386 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700387 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
388 }
389
390 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700391 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700392 if (fragment instanceof DialpadFragment) {
393 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700394 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700395 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700396 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700397 } else if (fragment instanceof SmartDialSearchFragment) {
398 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700399 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Lee7eccf452014-03-14 12:52:42 -0700400 if (mFragmentsFrame != null) {
401 mFragmentsFrame.setAlpha(1.0f);
402 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700403 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700404 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700405 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Lee7eccf452014-03-14 12:52:42 -0700406 if (mFragmentsFrame != null) {
407 mFragmentsFrame.setAlpha(1.0f);
408 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700409 } else if (fragment instanceof ListsFragment) {
410 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700411 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700412 }
Yorke Leec3766332013-07-31 11:13:16 -0700413 }
414
Alon Albertb453e5b2013-09-10 15:54:23 -0700415 protected void handleMenuSettings() {
416 openTelephonySetting(this);
417 }
418
419 public static void openTelephonySetting(Activity activity) {
420 final Intent settingsIntent = getCallSettingsIntent();
421 activity.startActivity(settingsIntent);
422 }
423
Chiao Cheng94b10b52012-08-17 16:59:12 -0700424 @Override
425 public void onClick(View view) {
426 switch (view.getId()) {
Andrew Leea73e1892014-05-13 13:21:16 -0700427 case R.id.floating_action_button:
Andrew Lee2a58ab82014-05-15 02:16:04 -0700428 if (!mIsDialpadShown) {
Andrew Leea73e1892014-05-13 13:21:16 -0700429 mInCallDialpadUp = false;
430 showDialpadFragment(true);
431 } else {
432 // Dial button was pressed; tell the Dialpad fragment
433 mDialpadFragment.dialButtonPressed();
434 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700435 break;
Yorke Leec3766332013-07-31 11:13:16 -0700436 case R.id.search_close_button:
437 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700438 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700439 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700440 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700441 }
442 break;
Yorke Leec3766332013-07-31 11:13:16 -0700443 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800444 try {
445 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
446 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
447 } catch (ActivityNotFoundException e) {
448 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
449 Toast.LENGTH_SHORT).show();
450 }
Yorke Leec3766332013-07-31 11:13:16 -0700451 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700452 default: {
453 Log.wtf(TAG, "Unexpected onClick event from " + view);
454 break;
455 }
456 }
457 }
458
Yorke Leec3766332013-07-31 11:13:16 -0700459 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700460 public boolean onOptionsItemSelected(MenuItem item) {
461 switch (item.getItemId()) {
462 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700463 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700464 break;
465 case R.id.menu_add_contact:
466 try {
467 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
468 } catch (ActivityNotFoundException e) {
469 Toast toast = Toast.makeText(this,
470 R.string.add_contact_not_available,
471 Toast.LENGTH_SHORT);
472 toast.show();
473 }
474 break;
475 case R.id.menu_import_export:
476 // We hard-code the "contactsAreAvailable" argument because doing it properly would
477 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
478 // now in Dialtacts for (potential) performance reasons. Compare with how it is
479 // done in {@link PeopleActivity}.
480 ImportExportDialogFragment.show(getFragmentManager(), true,
481 DialtactsActivity.class);
482 return true;
483 case R.id.menu_clear_frequents:
484 // TODO: This should be enabled/disabled based on
Yorke Leed999b712014-04-23 15:10:58 -0700485 // SpeedDialFragment.hasFrequents
Yorke Lee86e21f72014-04-02 16:49:38 -0700486 ClearFrequentsDialog.show(getFragmentManager());
487 return true;
488 case R.id.menu_call_settings:
489 handleMenuSettings();
490 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700491 }
492 return false;
493 }
494
495 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700496 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
497 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
498 if (resultCode == RESULT_OK) {
499 final ArrayList<String> matches = data.getStringArrayListExtra(
500 RecognizerIntent.EXTRA_RESULTS);
501 if (matches.size() > 0) {
502 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700503 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700504 } else {
505 Log.e(TAG, "Voice search - nothing heard");
506 }
507 } else {
508 Log.e(TAG, "Voice search failed");
509 }
510 }
511 super.onActivityResult(requestCode, resultCode, data);
512 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700513
Andrew Lee2a58ab82014-05-15 02:16:04 -0700514 /**
515 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
516 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
517 * @see #onDialpadShown
518 */
Yorke Leec3766332013-07-31 11:13:16 -0700519 private void showDialpadFragment(boolean animate) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700520 if (mIsDialpadShown) {
521 return;
522 }
523 mIsDialpadShown = true;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700524 mDialpadFragment.setAnimate(animate);
525
Chiao Cheng94b10b52012-08-17 16:59:12 -0700526 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700527 ft.show(mDialpadFragment);
528 ft.commit();
529 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700530
Andrew Lee2a58ab82014-05-15 02:16:04 -0700531 /**
532 * Callback from child DialpadFragment when the dialpad is shown.
533 */
534 public void onDialpadShown() {
535 updateFloatingActionButton();
536 if (mDialpadFragment.getAnimate()) {
537 Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
538 mDialpadFragment.getView().startAnimation(slideIn);
539 } else {
540 mDialpadFragment.setYFraction(0);
541 }
542
543 if (mListsFragment != null && mListsFragment.isResumed() && mListsFragment.isVisible()) {
544 // If the favorites fragment is showing, fade to blank.
545 mFragmentsFrame.animate().alpha(0.0f);
546 parentLayout.setBackgroundColor(mContactListBackgroundColor);
547 }
548
549 updateSearchFragmentPosition();
550 getActionBar().hide();
551 }
552
553 /**
554 * Initiates animations and other visual updates to hide the dialpad. The fragment is hidden in
555 * a callback after the hide animation ends.
556 * @see #commitDialpadFragmentHide
557 */
Yorke Leeca195042013-09-25 16:29:38 -0700558 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700559 if (mDialpadFragment == null) {
560 return;
561 }
Yorke Leef6673d32013-08-23 15:34:17 -0700562 if (clearDialpad) {
563 mDialpadFragment.clearDialpad();
564 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700565 if (!mIsDialpadShown) {
566 return;
Yorke Leec3766332013-07-31 11:13:16 -0700567 }
Andrew Lee2a58ab82014-05-15 02:16:04 -0700568 mIsDialpadShown = false;
569 mDialpadFragment.setAnimate(animate);
570
571 updateFloatingActionButton();
572 if (animate) {
573 Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);
574 slideOut.setAnimationListener(new ActivityAnimationListener() {
575 @Override
576 public void onAnimationEnd(Animation animation) {
577 commitDialpadFragmentHide();
578 }
579 });
580 mDialpadFragment.getView().startAnimation(slideOut);
581 } else {
582 commitDialpadFragmentHide();
583 }
584
585 if (mListsFragment != null && mListsFragment.isVisible()) {
586 mFragmentsFrame.animate().alpha(1.0f);
587 parentLayout.setBackgroundColor(mDialerBackgroundColor);
588 }
589
590 updateSearchFragmentPosition();
591 getActionBar().show();
592 }
593
594 /**
595 * Finishes hiding the dialpad fragment after any animations are completed.
596 */
597 private void commitDialpadFragmentHide() {
598 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700599 ft.hide(mDialpadFragment);
600 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700601 }
602
Andrew Lee2a58ab82014-05-15 02:16:04 -0700603 private void updateSearchFragmentPosition() {
604 int translationValue = mIsDialpadShown ? -mActionBarHeight : 0;
605 SearchFragment fragment = null;
606 if (mInDialpadSearch) {
607 fragment = mSmartDialSearchFragment;
608 } else if (mInRegularSearch) {
609 fragment = mRegularSearchFragment;
610 }
611 if (fragment != null && fragment.isVisible()) {
612 fragment.getView().animate().translationY(translationValue)
613 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
614 }
615 }
616
Yorke Leeb207f8a2013-08-29 18:51:06 -0700617 private boolean getInSearchUi() {
618 return mInDialpadSearch || mInRegularSearch;
619 }
620
621 private void setNotInSearchUi() {
622 mInDialpadSearch = false;
623 mInRegularSearch = false;
624 }
625
Yorke Lee311969c2013-08-26 06:31:11 -0700626 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700627 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700628 hideDialpadFragment(false, true);
629 }
630
Yorke Leee00c9fe2014-04-12 12:42:06 -0700631 private void hideInputMethod(View view) {
632 final InputMethodManager imm = (InputMethodManager) getSystemService(
633 Context.INPUT_METHOD_SERVICE);
634 if (imm != null && view != null) {
635 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
636 }
637 }
638
Yorke Lee53a22302014-04-29 18:13:46 -0700639 private void prepareVoiceSearchButton() {
640 mVoiceSearchButton = getActionBar().getCustomView().findViewById(R.id.voice_search_button);
641 final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
642 if (canIntentBeHandled(voiceIntent)) {
643 mVoiceSearchButton.setVisibility(View.VISIBLE);
644 mVoiceSearchButton.setOnClickListener(this);
645 } else {
646 mVoiceSearchButton.setVisibility(View.GONE);
647 }
648 }
649
Yorke Lee86e21f72014-04-02 16:49:38 -0700650 @Override
651 public boolean onCreateOptionsMenu(Menu menu) {
652 if (DEBUG) {
653 Log.d(TAG, "onCreateOptionsMenu");
654 }
655 MenuInflater inflater = getMenuInflater();
656 inflater.inflate(R.menu.dialtacts_options, menu);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700657
658 if (mPendingSearchViewQuery != null) {
Yorke Lee53a22302014-04-29 18:13:46 -0700659 mSearchView.setText(mPendingSearchViewQuery);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700660 mPendingSearchViewQuery = null;
661 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700662 return super.onCreateOptionsMenu(menu);
Ihab Awadfb00cb82014-03-18 16:19:00 -0700663 }
664
Chiao Cheng94b10b52012-08-17 16:59:12 -0700665 /**
666 * Returns true if the intent is due to hitting the green send key (hardware call button:
667 * KEYCODE_CALL) while in a call.
668 *
669 * @param intent the intent that launched this activity
Chiao Cheng94b10b52012-08-17 16:59:12 -0700670 * @return true if the intent is due to hitting the green send key while in a call
671 */
Yorke Lee62e995c2014-03-28 10:02:56 -0700672 private boolean isSendKeyWhileInCall(Intent intent) {
673 // If there is a call in progress and the user launched the dialer by hitting the call
674 // button, go straight to the in-call screen.
675 final boolean callKey = Intent.ACTION_CALL_BUTTON.equals(intent.getAction());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700676
Yorke Lee62e995c2014-03-28 10:02:56 -0700677 try {
678 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
679 if (callKey && phone != null && phone.showCallScreen()) {
680 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700681 }
Yorke Lee62e995c2014-03-28 10:02:56 -0700682 } catch (RemoteException e) {
683 Log.e(TAG, "Failed to handle send while in call", e);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700684 }
685
686 return false;
687 }
688
689 /**
690 * Sets the current tab based on the intent's request type
691 *
692 * @param intent Intent that contains information about which tab should be selected
693 */
Yorke Leec3766332013-07-31 11:13:16 -0700694 private void displayFragment(Intent intent) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700695 // 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 -0700696 if (isSendKeyWhileInCall(intent)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700697 finish();
698 return;
699 }
700
Yorke Lee669b6082013-09-17 15:43:38 -0700701 if (mDialpadFragment != null) {
702 final boolean phoneIsInUse = phoneIsInUse();
703 if (phoneIsInUse || isDialIntent(intent)) {
704 mDialpadFragment.setStartedFromNewIntent(true);
705 if (phoneIsInUse && !mDialpadFragment.isVisible()) {
706 mInCallDialpadUp = true;
707 }
708 showDialpadFragment(false);
Yorke Lee35127cd2013-09-10 14:09:29 -0700709 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700710 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700711 }
712
713 @Override
714 public void onNewIntent(Intent newIntent) {
715 setIntent(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700716 displayFragment(newIntent);
Yorke Leec3766332013-07-31 11:13:16 -0700717
Chiao Cheng94b10b52012-08-17 16:59:12 -0700718 invalidateOptionsMenu();
719 }
720
721 /** Returns true if the given intent contains a phone number to populate the dialer with */
722 private boolean isDialIntent(Intent intent) {
723 final String action = intent.getAction();
724 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
725 return true;
726 }
727 if (Intent.ACTION_VIEW.equals(action)) {
728 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700729 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700730 return true;
731 }
732 }
733 return false;
734 }
735
736 /**
737 * Returns an appropriate call origin for this Activity. May return null when no call origin
738 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
739 * for remembering the tab in which the user made a phone call, so the external app's DIAL
740 * request should not be counted.)
741 */
742 public String getCallOrigin() {
743 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
744 }
745
Chiao Cheng94b10b52012-08-17 16:59:12 -0700746 /**
Yorke Leec3766332013-07-31 11:13:16 -0700747 * Shows the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700748 */
Yorke Leeef2b7382013-08-09 17:39:25 -0700749 private void enterSearchUi(boolean smartDialSearch, String query) {
Yorke Lee34bdf872013-08-15 14:11:50 -0700750 if (getFragmentManager().isDestroyed()) {
751 // Weird race condition where fragment is doing work after the activity is destroyed
752 // due to talkback being on (b/10209937). Just return since we can't do any
753 // constructive here.
754 return;
755 }
756
Yorke Leeef2b7382013-08-09 17:39:25 -0700757 if (DEBUG) {
758 Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
759 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700760
Yorke Leec3766332013-07-31 11:13:16 -0700761 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700762 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700763 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700764 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700765 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700766 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700767
Yorke Leeb207f8a2013-08-29 18:51:06 -0700768 final String tag;
769 if (smartDialSearch) {
770 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
771 } else {
772 tag = TAG_REGULAR_SEARCH_FRAGMENT;
773 }
774 mInDialpadSearch = smartDialSearch;
775 mInRegularSearch = !smartDialSearch;
776
Andrew Lee752956e2014-05-15 11:43:38 -0700777 SearchFragment fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700778 if (fragment == null) {
779 if (smartDialSearch) {
780 fragment = new SmartDialSearchFragment();
781 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700782 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700783 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700784 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700785 // DialtactsActivity will provide the options menu
786 fragment.setHasOptionsMenu(false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700787 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700788 transaction.addToBackStack(null);
789 fragment.setQueryString(query, false);
790 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700791 }
792
793 /**
Yorke Leec3766332013-07-31 11:13:16 -0700794 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700795 */
Yorke Leec3766332013-07-31 11:13:16 -0700796 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700797 // See related bug in enterSearchUI();
798 if (getFragmentManager().isDestroyed()) {
799 return;
800 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700801 // Go all the way back to the favorites fragment, regardless of how many times we
802 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700803 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
804 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700805
Andrew Lee2a58ab82014-05-15 02:16:04 -0700806 if (mIsDialpadShown) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700807 mFragmentsFrame.setAlpha(0);
808 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700809 }
810
811 /** Returns an Intent to launch Call Settings screen */
812 public static Intent getCallSettingsIntent() {
813 final Intent intent = new Intent(Intent.ACTION_MAIN);
814 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
815 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
816 return intent;
817 }
818
819 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700820 public void onBackPressed() {
Andrew Lee2a58ab82014-05-15 02:16:04 -0700821 if (mIsDialpadShown) {
Yorke Leef6673d32013-08-23 15:34:17 -0700822 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700823 } else if (getInSearchUi()) {
Yorke Lee53a22302014-04-29 18:13:46 -0700824 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700825 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700826 } else {
827 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700828 }
Yorke Leec3766332013-07-31 11:13:16 -0700829 }
830
831 @Override
832 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700833 if (mSmartDialSearchFragment != null) {
834 mSmartDialSearchFragment.setAddToContactNumber(query);
835 }
Yorke Leec3766332013-07-31 11:13:16 -0700836 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
837 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700838
839 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700840 if (DEBUG) {
841 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
842 }
843 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
844 // This callback can happen if the dialpad fragment is recreated because of
845 // activity destruction. In that case, don't update the search view because
846 // that would bring the user back to the search fragment regardless of the
847 // previous state of the application. Instead, just return here and let the
848 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700849 if (!TextUtils.isEmpty(normalizedQuery)) {
850 mPendingSearchViewQuery = normalizedQuery;
851 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700852 return;
853 }
Yorke Lee53a22302014-04-29 18:13:46 -0700854 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700855 }
856 }
Yorke Leec3766332013-07-31 11:13:16 -0700857
858 @Override
859 public void onListFragmentScrollStateChange(int scrollState) {
860 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700861 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700862 hideInputMethod(getCurrentFocus());
863 }
864 }
865
866 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700867 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
868 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700869 // TODO: No-op for now. This should eventually show/hide the actionBar based on
870 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700871 }
872
Yorke Lee86e21f72014-04-02 16:49:38 -0700873 @Override
Andrew Leea73e1892014-05-13 13:21:16 -0700874 public void setFloatingActionButtonVisible(boolean visible) {
875 mFloatingActionButtonContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700876 }
877
878 private boolean phoneIsInUse() {
879 final TelephonyManager tm = (TelephonyManager) getSystemService(
880 Context.TELEPHONY_SERVICE);
881 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
882 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700883
Yorke Leee1424812013-09-04 18:24:48 -0700884 public static Intent getAddNumberToContactIntent(CharSequence text) {
885 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
886 intent.putExtra(Intents.Insert.PHONE, text);
887 intent.setType(Contacts.CONTENT_ITEM_TYPE);
888 return intent;
889 }
890
Yorke Leeda0f9042013-12-05 14:25:51 -0800891 private boolean canIntentBeHandled(Intent intent) {
892 final PackageManager packageManager = getPackageManager();
893 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
894 PackageManager.MATCH_DEFAULT_ONLY);
895 return resolveInfo != null && resolveInfo.size() > 0;
896 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800897
Yorke Lee6a1461a2014-04-21 16:27:14 -0700898 @Override
899 public void showCallHistory() {
900 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
901 // CONTENT_TYPE, so that we always open our call log from our dialer
902 final Intent intent = new Intent(this, CallLogActivity.class);
903 startActivity(intent);
904 }
905
Yorke Lee86e21f72014-04-02 16:49:38 -0700906 /**
907 * Called when the user has long-pressed a contact tile to start a drag operation.
908 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800909 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700910 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700911 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700912 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800913 }
914
915 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700916 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
917 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800918
Yorke Lee86e21f72014-04-02 16:49:38 -0700919 /**
920 * Called when the user has released a contact tile after long-pressing it.
921 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800922 @Override
923 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700924 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700925 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800926 }
927
928 @Override
929 public void onDroppedOnRemove() {}
930
931 /**
Yorke Leed999b712014-04-23 15:10:58 -0700932 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800933 * once it has been attached to the activity.
934 */
935 @Override
936 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -0700937 mDragDropController = dragController;
938 ((RemoveView) findViewById(R.id.remove_view))
939 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800940 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700941
942 @Override
943 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700944 // Specify call-origin so that users will see the previous tab instead of
945 // CallLog screen (search UI will be automatically exited).
946 PhoneNumberInteraction.startInteractionForPhoneCall(
947 DialtactsActivity.this, dataUri, getCallOrigin());
948 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700949 }
950
951 @Override
952 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700953 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
954 startActivity(intent);
955 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700956 }
957
958 @Override
959 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700960 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -0700961 }
962
963 @Override
964 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700965 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700966 }
Yorke Lee33932ff2014-04-16 13:34:32 -0700967
968 public int getActionBarHeight() {
969 return mActionBarHeight;
970 }
Yorke Leecc4660d2014-04-24 11:22:57 -0700971
972 @Override
973 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
974
975 }
976
977 @Override
978 public void onPageSelected(int position) {
979 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -0700980 // If the dialpad is showing, the floating action button should always be middle aligned.
Andrew Lee2a58ab82014-05-15 02:16:04 -0700981 if (!mIsDialpadShown) {
Yorke Leec61d4542014-05-01 10:43:24 -0700982 alignFloatingActionButtonByTab(mCurrentTabPosition);
983 }
Yorke Leecc4660d2014-04-24 11:22:57 -0700984 }
985
986 @Override
987 public void onPageScrollStateChanged(int state) {
988 }
989
Andrew Lee2a58ab82014-05-15 02:16:04 -0700990 private void updateFloatingActionButton() {
991 if (mIsDialpadShown) {
992 mFloatingActionButton.setImageResource(R.drawable.fab_ic_call);
993 mFloatingActionButton.setContentDescription(
994 getResources().getString(R.string.description_dial_button));
995 alignFloatingActionButtonByTab(mCurrentTabPosition);
996 } else {
997 mFloatingActionButton.setImageResource(R.drawable.fab_ic_dial);
998 mFloatingActionButton.setContentDescription(
999 getResources().getString(R.string.action_menu_dialpad_button));
1000 alignFloatingActionButtonMiddle();
1001 }
1002 }
1003
Yorke Leecc4660d2014-04-24 11:22:57 -07001004 private void alignFloatingActionButtonByTab(int position) {
1005 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1006 alignFloatingActionButtonMiddle();
1007 } else {
1008 alignFloatingActionButtonRight();
1009 }
1010 }
1011
1012 private void alignFloatingActionButtonRight() {
1013 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001014 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001015 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1016 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Andrew Leea73e1892014-05-13 13:21:16 -07001017 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001018 }
1019
1020 private void alignFloatingActionButtonMiddle() {
1021 final RelativeLayout.LayoutParams params =
Andrew Leea73e1892014-05-13 13:21:16 -07001022 (RelativeLayout.LayoutParams) mFloatingActionButtonContainer.getLayoutParams();
Yorke Leecc4660d2014-04-24 11:22:57 -07001023 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1024 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Andrew Leea73e1892014-05-13 13:21:16 -07001025 mFloatingActionButtonContainer.setLayoutParams(params);
Yorke Leecc4660d2014-04-24 11:22:57 -07001026 }
Andrew Lee2a58ab82014-05-15 02:16:04 -07001027
1028 /**
1029 * Convenience class which implements AnimationListener interface as null-op methods.
1030 */
1031 private class ActivityAnimationListener implements AnimationListener {
1032 @Override
1033 public void onAnimationStart(Animation animation) {
1034 }
1035
1036 @Override
1037 public void onAnimationEnd(Animation animation) {
1038 }
1039
1040 @Override
1041 public void onAnimationRepeat(Animation animation) {
1042 }
1043 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001044}