blob: b7272d74887caea63ba634c400498ab671f679de [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;
52import android.view.animation.DecelerateInterpolator;
53import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070054import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070055import android.widget.AbsListView.OnScrollListener;
Yorke Lee53a22302014-04-29 18:13:46 -070056import android.widget.EditText;
Chiao Cheng94b10b52012-08-17 16:59:12 -070057import android.widget.PopupMenu;
Yorke Leecc4660d2014-04-24 11:22:57 -070058import android.widget.RelativeLayout;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080059import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070060
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070061import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070062import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070063import com.android.contacts.common.dialog.ClearFrequentsDialog;
64import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080065import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070066import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070067import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070068import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070069import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070071import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070072import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080073import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070074import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080075import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070076import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070077import com.android.dialer.list.SpeedDialFragment;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070078import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070079import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080080import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070081import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070082import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070083import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070084import com.android.internal.telephony.ITelephony;
85
Yorke Leec3766332013-07-31 11:13:16 -070086import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080087import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070088
Chiao Cheng94b10b52012-08-17 16:59:12 -070089/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070090 * The dialer tab's title is 'phone', a more common name (see strings.xml).
91 */
Yorke Leec3766332013-07-31 11:13:16 -070092public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -070093 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -070094 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -080095 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -070096 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -070097 SpeedDialFragment.HostInterface,
Yorke Leee00c9fe2014-04-12 12:42:06 -070098 OnDragDropListener, View.OnLongClickListener,
Yorke Leecc4660d2014-04-24 11:22:57 -070099 OnPhoneNumberPickerActionListener,
100 ViewPager.OnPageChangeListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101 private static final String TAG = "DialtactsActivity";
102
Ihab Awad526c0b82014-02-27 12:55:36 -0800103 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700104
Yorke Leef74011e2013-08-02 11:30:30 -0700105 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
106
Chiao Cheng94b10b52012-08-17 16:59:12 -0700107 /** Used to open Call Setting */
108 private static final String PHONE_PACKAGE = "com.android.phone";
109 private static final String CALL_SETTINGS_CLASS_NAME =
110 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700111 /** @see #getCallOrigin() */
112 private static final String CALL_ORIGIN_DIALTACTS =
113 "com.android.dialer.DialtactsActivity";
114
Yorke Leeb207f8a2013-08-29 18:51:06 -0700115 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
116 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700117 private static final String KEY_SEARCH_QUERY = "search_query";
118 private static final String KEY_FIRST_LAUNCH = "first_launch";
119
Yorke Leec3766332013-07-31 11:13:16 -0700120 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
121 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
122 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
123 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700124
Chiao Cheng94b10b52012-08-17 16:59:12 -0700125 /**
126 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
127 */
128 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
129
Yorke Leec3766332013-07-31 11:13:16 -0700130 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700131
Yorke Lee86e21f72014-04-02 16:49:38 -0700132 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800133
Andrew Lee0c667702014-05-12 14:31:45 -0700134 private RelativeLayout parentLayout;
135
Yorke Leec3766332013-07-31 11:13:16 -0700136 /**
Yorke Leec3766332013-07-31 11:13:16 -0700137 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700138 */
Yorke Leef74011e2013-08-02 11:30:30 -0700139 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700140
141 /**
142 * Fragment for searching phone numbers using the alphanumeric keyboard.
143 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700144 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700145
146 /**
147 * Fragment for searching phone numbers using the dialpad.
148 */
149 private SmartDialSearchFragment mSmartDialSearchFragment;
150
Yorke Leee00c9fe2014-04-12 12:42:06 -0700151 /**
152 * Fragment containing the speed dial list, recents list, and all contacts list.
153 */
154 private ListsFragment mListsFragment;
155
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700156 private View mFloatingActionButton;
Yorke Leec3766332013-07-31 11:13:16 -0700157 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800158 private View mDialButton;
Yorke Leec3766332013-07-31 11:13:16 -0700159
Yorke Leeb207f8a2013-08-29 18:51:06 -0700160 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700161
Yorke Lee33932ff2014-04-16 13:34:32 -0700162 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700163 private boolean mInDialpadSearch;
164 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700165 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700166
Yorke Leeef2b7382013-08-09 17:39:25 -0700167 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700168 * The position of the currently selected tab in the attached {@link ListsFragment}.
169 */
170 private int mCurrentTabPosition = 0;
171
172 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700173 * True if the dialpad is only temporarily showing due to being in call
174 */
175 private boolean mInCallDialpadUp;
176
177 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700178 * True when this activity has been launched for the first time.
179 */
Yorke Lee98702de2013-08-06 12:03:32 -0700180 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700181
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700182 /**
183 * Search query to be applied to the SearchView in the ActionBar once
184 * onCreateOptionsMenu has been called.
185 */
186 private String mPendingSearchViewQuery;
187
Yorke Lee53a22302014-04-29 18:13:46 -0700188 private EditText mSearchView;
189 private View mSearchViewCloseButton;
190 private View mVoiceSearchButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700191 /**
192 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
193 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
194 * removed from the speed dial list.
195 */
Yorke Lee28266192014-05-01 10:05:52 -0700196 private View mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700197
Yorke Lee86e21f72014-04-02 16:49:38 -0700198 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
199 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700200 private String mSearchQuery;
201
Yorke Leefce269a2013-08-12 11:56:04 -0700202 private DialerDatabaseHelper mDialerDatabaseHelper;
Yorke Lee28266192014-05-01 10:05:52 -0700203 private DragDropController mDragDropController;
Yorke Leefce269a2013-08-12 11:56:04 -0700204
Andrew Lee0c667702014-05-12 14:31:45 -0700205 private int mDialerBackgroundColor;
206 private int mContactListBackgroundColor;
207
Yorke Leee00c9fe2014-04-12 12:42:06 -0700208 private class OverflowPopupMenu extends PopupMenu {
209 public OverflowPopupMenu(Context context, View anchor) {
210 super(context, anchor);
211 }
212
213 @Override
214 public void show() {
215 final Menu menu = getMenu();
216 final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
Yorke Leed999b712014-04-23 15:10:58 -0700217 // TODO: Check mSpeedDialFragment.hasFrequents()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700218 clearFrequents.setVisible(true);
219 super.show();
220 }
221 }
222
Chiao Cheng94b10b52012-08-17 16:59:12 -0700223 /**
Yorke Lee28266192014-05-01 10:05:52 -0700224 * Listener that listens to drag events and sends their x and y coordinates to a
225 * {@link DragDropController}.
226 */
227 private class LayoutOnDragListener implements OnDragListener {
228 @Override
229 public boolean onDrag(View v, DragEvent event) {
230 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
231 mDragDropController.handleDragHovered(v, (int) event.getX(),
232 (int) event.getY());
233 }
234 return true;
235 }
236 }
237
238 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -0700239 * Listener used to send search queries to the phone search fragment.
240 */
Yorke Lee53a22302014-04-29 18:13:46 -0700241 private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
Yorke Leec3766332013-07-31 11:13:16 -0700242 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700243 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Yorke Leec3766332013-07-31 11:13:16 -0700244 }
245
246 @Override
Yorke Lee53a22302014-04-29 18:13:46 -0700247 public void onTextChanged(CharSequence s, int start, int before, int count) {
248 final String newText = s.toString();
Yorke Leeef2b7382013-08-09 17:39:25 -0700249 if (newText.equals(mSearchQuery)) {
250 // If the query hasn't changed (perhaps due to activity being destroyed
251 // and restored, or user launching the same DIAL intent twice), then there is
252 // no need to do anything here.
Yorke Lee53a22302014-04-29 18:13:46 -0700253 return;
Yorke Leeef2b7382013-08-09 17:39:25 -0700254 }
255 mSearchQuery = newText;
256 if (DEBUG) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700257 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leeef2b7382013-08-09 17:39:25 -0700258 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700259 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700260
Yorke Leec3766332013-07-31 11:13:16 -0700261 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700262 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700263 exitSearchUi();
Yorke Lee53a22302014-04-29 18:13:46 -0700264 mSearchViewCloseButton.setVisibility(View.GONE);
265 mVoiceSearchButton.setVisibility(View.VISIBLE);
266 return;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700267 } else if (!TextUtils.isEmpty(newText)) {
268 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
269 (!dialpadSearch && mInRegularSearch);
270 if (!sameSearchMode) {
271 // call enterSearchUi only if we are switching search modes, or entering
272 // search ui for the first time
273 enterSearchUi(dialpadSearch, newText);
274 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700275
Yorke Leeb207f8a2013-08-29 18:51:06 -0700276 if (dialpadSearch && mSmartDialSearchFragment != null) {
277 mSmartDialSearchFragment.setQueryString(newText, false);
278 } else if (mRegularSearchFragment != null) {
279 mRegularSearchFragment.setQueryString(newText, false);
280 }
Yorke Lee53a22302014-04-29 18:13:46 -0700281 mSearchViewCloseButton.setVisibility(View.VISIBLE);
282 mVoiceSearchButton.setVisibility(View.GONE);
283 return;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700284 }
Yorke Lee53a22302014-04-29 18:13:46 -0700285 return;
286 }
287
288 @Override
289 public void afterTextChanged(Editable s) {
Yorke Leec3766332013-07-31 11:13:16 -0700290 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700291 };
292
Yorke Leec3766332013-07-31 11:13:16 -0700293 private boolean isDialpadShowing() {
294 return mDialpadFragment != null && mDialpadFragment.isVisible();
295 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700296
297 @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
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700346 mFloatingActionButton = findViewById(R.id.floating_action_button);
Andrew Lee4cbc9732014-04-24 14:38:58 -0700347 ViewUtil.setupFloatingActionButton(mFloatingActionButton, getResources());
Yorke Lee86e21f72014-04-02 16:49:38 -0700348
Yorke Lee86e21f72014-04-02 16:49:38 -0700349 mDialButton = findViewById(R.id.dial_button);
350 mDialButton.setOnClickListener(this);
351 mDialpadButton = findViewById(R.id.dialpad_button);
352 mDialpadButton.setOnClickListener(this);
Ihab Awad526c0b82014-02-27 12:55:36 -0800353
Yorke Lee28266192014-05-01 10:05:52 -0700354 mRemoveViewContainer = findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800355
Yorke Lee0baa98b2013-08-22 10:55:16 -0700356 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700357 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700358 }
359
360 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700361 protected void onResume() {
362 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700363 if (mFirstLaunch) {
364 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700365 } else if (!phoneIsInUse() && mInCallDialpadUp) {
366 hideDialpadFragment(false, true);
367 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700368 }
369 mFirstLaunch = false;
Yorke Lee53a22302014-04-29 18:13:46 -0700370 prepareVoiceSearchButton();
Yorke Leefce269a2013-08-12 11:56:04 -0700371 mDialerDatabaseHelper.startSmartDialUpdateThread();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700372 }
373
374 @Override
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700375 protected void onPause() {
376 if (mClearSearchOnPause) {
377 hideDialpadAndSearchUi();
378 mClearSearchOnPause = false;
379 }
380 super.onPause();
381 }
382
383 @Override
Yorke Leeef2b7382013-08-09 17:39:25 -0700384 protected void onSaveInstanceState(Bundle outState) {
385 super.onSaveInstanceState(outState);
386 outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700387 outState.putBoolean(KEY_IN_REGULAR_SEARCH_UI, mInRegularSearch);
388 outState.putBoolean(KEY_IN_DIALPAD_SEARCH_UI, mInDialpadSearch);
Yorke Leeef2b7382013-08-09 17:39:25 -0700389 outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
390 }
391
392 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700393 public void onAttachFragment(Fragment fragment) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700394 if (fragment instanceof DialpadFragment) {
395 mDialpadFragment = (DialpadFragment) fragment;
Yorke Leec3766332013-07-31 11:13:16 -0700396 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
Yorke Leeef2b7382013-08-09 17:39:25 -0700397 transaction.hide(mDialpadFragment);
Yorke Leec3766332013-07-31 11:13:16 -0700398 transaction.commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700399 } else if (fragment instanceof SmartDialSearchFragment) {
400 mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700401 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Lee7eccf452014-03-14 12:52:42 -0700402 if (mFragmentsFrame != null) {
403 mFragmentsFrame.setAlpha(1.0f);
404 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700405 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700406 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700407 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(this);
Yorke Lee7eccf452014-03-14 12:52:42 -0700408 if (mFragmentsFrame != null) {
409 mFragmentsFrame.setAlpha(1.0f);
410 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700411 } else if (fragment instanceof ListsFragment) {
412 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700413 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700414 }
Yorke Leec3766332013-07-31 11:13:16 -0700415 }
416
Alon Albertb453e5b2013-09-10 15:54:23 -0700417 protected void handleMenuSettings() {
418 openTelephonySetting(this);
419 }
420
421 public static void openTelephonySetting(Activity activity) {
422 final Intent settingsIntent = getCallSettingsIntent();
423 activity.startActivity(settingsIntent);
424 }
425
Chiao Cheng94b10b52012-08-17 16:59:12 -0700426 @Override
427 public void onClick(View view) {
428 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700429 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700430 // Reset the boolean flag that tracks whether the dialpad was up because
431 // we were in call. Regardless of whether it was true before, we want to
432 // show the dialpad because the user has explicitly clicked the dialpad
433 // button.
434 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700435 showDialpadFragment(true);
436 break;
Yorke Lee86e21f72014-04-02 16:49:38 -0700437 case R.id.dial_button:
438 // Dial button was pressed; tell the Dialpad fragment
439 mDialpadFragment.dialButtonPressed();
440 break;
Yorke Leec3766332013-07-31 11:13:16 -0700441 case R.id.search_close_button:
442 // Clear the search field
Yorke Lee53a22302014-04-29 18:13:46 -0700443 if (!TextUtils.isEmpty(mSearchView.getText())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700444 mDialpadFragment.clearDialpad();
Yorke Lee53a22302014-04-29 18:13:46 -0700445 mSearchView.setText(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700446 }
447 break;
Yorke Leec3766332013-07-31 11:13:16 -0700448 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800449 try {
450 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
451 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
452 } catch (ActivityNotFoundException e) {
453 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
454 Toast.LENGTH_SHORT).show();
455 }
Yorke Leec3766332013-07-31 11:13:16 -0700456 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700457 default: {
458 Log.wtf(TAG, "Unexpected onClick event from " + view);
459 break;
460 }
461 }
462 }
463
Yorke Leec3766332013-07-31 11:13:16 -0700464 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700465 public boolean onOptionsItemSelected(MenuItem item) {
466 switch (item.getItemId()) {
467 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700468 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700469 break;
470 case R.id.menu_add_contact:
471 try {
472 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
473 } catch (ActivityNotFoundException e) {
474 Toast toast = Toast.makeText(this,
475 R.string.add_contact_not_available,
476 Toast.LENGTH_SHORT);
477 toast.show();
478 }
479 break;
480 case R.id.menu_import_export:
481 // We hard-code the "contactsAreAvailable" argument because doing it properly would
482 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
483 // now in Dialtacts for (potential) performance reasons. Compare with how it is
484 // done in {@link PeopleActivity}.
485 ImportExportDialogFragment.show(getFragmentManager(), true,
486 DialtactsActivity.class);
487 return true;
488 case R.id.menu_clear_frequents:
489 // TODO: This should be enabled/disabled based on
Yorke Leed999b712014-04-23 15:10:58 -0700490 // SpeedDialFragment.hasFrequents
Yorke Lee86e21f72014-04-02 16:49:38 -0700491 ClearFrequentsDialog.show(getFragmentManager());
492 return true;
493 case R.id.menu_call_settings:
494 handleMenuSettings();
495 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700496 }
497 return false;
498 }
499
500 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800501 public boolean onLongClick(View view) {
502 switch (view.getId()) {
503 case R.id.dial_button: {
504 // Dial button was pressed; tell the Dialpad fragment
505 mDialpadFragment.dialButtonPressed();
506 return true; // Consume the event
507 }
508 default: {
509 Log.wtf(TAG, "Unexpected onClick event from " + view);
510 break;
511 }
512 }
513 return false;
514 }
515
516 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700517 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
518 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
519 if (resultCode == RESULT_OK) {
520 final ArrayList<String> matches = data.getStringArrayListExtra(
521 RecognizerIntent.EXTRA_RESULTS);
522 if (matches.size() > 0) {
523 final String match = matches.get(0);
Yorke Lee53a22302014-04-29 18:13:46 -0700524 mSearchView.setText(match);
Yorke Leec3766332013-07-31 11:13:16 -0700525 } else {
526 Log.e(TAG, "Voice search - nothing heard");
527 }
528 } else {
529 Log.e(TAG, "Voice search failed");
530 }
531 }
532 super.onActivityResult(requestCode, resultCode, data);
533 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700534
Yorke Leec3766332013-07-31 11:13:16 -0700535 private void showDialpadFragment(boolean animate) {
Andrew Leecdfa6c62014-05-06 15:33:35 -0700536 mDialpadFragment.setAnimate(animate);
537
Chiao Cheng94b10b52012-08-17 16:59:12 -0700538 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700539 if (animate) {
540 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700541 } else {
542 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700543 }
544 ft.show(mDialpadFragment);
545 ft.commit();
546 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700547
Yorke Leeca195042013-09-25 16:29:38 -0700548 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700549 if (mDialpadFragment == null) return;
550 if (clearDialpad) {
551 mDialpadFragment.clearDialpad();
552 }
553 if (!mDialpadFragment.isVisible()) return;
Andrew Leecdfa6c62014-05-06 15:33:35 -0700554 mDialpadFragment.setAnimate(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700555 final FragmentTransaction ft = getFragmentManager().beginTransaction();
556 if (animate) {
557 ft.setCustomAnimations(0, R.anim.slide_out);
558 }
559 ft.hide(mDialpadFragment);
560 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700561 }
562
Yorke Leeb207f8a2013-08-29 18:51:06 -0700563 private boolean getInSearchUi() {
564 return mInDialpadSearch || mInRegularSearch;
565 }
566
567 private void setNotInSearchUi() {
568 mInDialpadSearch = false;
569 mInRegularSearch = false;
570 }
571
Yorke Lee311969c2013-08-26 06:31:11 -0700572 private void hideDialpadAndSearchUi() {
Yorke Lee53a22302014-04-29 18:13:46 -0700573 mSearchView.setText(null);
Yorke Lee311969c2013-08-26 06:31:11 -0700574 hideDialpadFragment(false, true);
575 }
576
Yorke Lee86e21f72014-04-02 16:49:38 -0700577 /**
578 * Callback from child DialpadFragment when the dialpad is shown.
579 */
580 public void onDialpadShown() {
581 mDialButton.setVisibility(View.VISIBLE);
582 mDialpadButton.setVisibility(View.GONE);
Yorke Leeb0d19762014-03-05 14:43:28 -0800583
Yorke Lee86e21f72014-04-02 16:49:38 -0700584 SearchFragment fragment = null;
585 if (mInDialpadSearch) {
586 fragment = mSmartDialSearchFragment;
587 } else if (mInRegularSearch) {
588 fragment = mRegularSearchFragment;
589 }
590 if (fragment != null && fragment.isVisible()) {
Yorke Leef42a1792014-04-25 16:56:42 -0700591 fragment.getView().animate().translationY(-mActionBarHeight)
Yorke Lee86e21f72014-04-02 16:49:38 -0700592 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
593 }
Christine Chen9c1e0652013-05-23 15:40:19 -0700594
Yorke Lee33932ff2014-04-16 13:34:32 -0700595 if (mListsFragment != null && mListsFragment.isResumed() && mListsFragment.isVisible()) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700596 // If the favorites fragment is showing, fade to blank.
597 mFragmentsFrame.animate().alpha(0.0f);
Andrew Lee0c667702014-05-12 14:31:45 -0700598 parentLayout.setBackgroundColor(mContactListBackgroundColor);
Yorke Lee7eccf452014-03-14 12:52:42 -0700599 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700600 getActionBar().hide();
Yorke Leecc4660d2014-04-24 11:22:57 -0700601 alignFloatingActionButtonMiddle();
Yorke Leec3766332013-07-31 11:13:16 -0700602 }
603
Yorke Lee86e21f72014-04-02 16:49:38 -0700604 /**
605 * Callback from child DialpadFragment when the dialpad is hidden.
606 */
607 public void onDialpadHidden() {
608 mDialButton.setVisibility(View.GONE);
609 mDialpadButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700610
Yorke Lee86e21f72014-04-02 16:49:38 -0700611 SearchFragment fragment = null;
612 if (mInDialpadSearch) {
613 fragment = mSmartDialSearchFragment;
614 } else if (mInRegularSearch) {
615 fragment = mRegularSearchFragment;
616 }
617 if (fragment != null && fragment.isVisible()) {
Yorke Leef42a1792014-04-25 16:56:42 -0700618 fragment.getView().animate().translationY(0)
Yorke Lee86e21f72014-04-02 16:49:38 -0700619 .setInterpolator(showActionBarInterpolator).setDuration(ANIMATION_DURATION);
Yorke Leec3766332013-07-31 11:13:16 -0700620 }
621
Yorke Leee00c9fe2014-04-12 12:42:06 -0700622 if (mListsFragment != null && mListsFragment.isVisible()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700623 mFragmentsFrame.animate().alpha(1.0f);
Andrew Lee0c667702014-05-12 14:31:45 -0700624 parentLayout.setBackgroundColor(mDialerBackgroundColor);
625
Yorke Lee86e21f72014-04-02 16:49:38 -0700626 }
627 getActionBar().show();
Yorke Leecc4660d2014-04-24 11:22:57 -0700628 alignFloatingActionButtonByTab(mCurrentTabPosition);
Yorke Leec3766332013-07-31 11:13:16 -0700629 }
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();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700762
Yorke Leeef2b7382013-08-09 17:39:25 -0700763 SearchFragment fragment;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700764 if (mInDialpadSearch && mSmartDialSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700765 transaction.remove(mSmartDialSearchFragment);
Yorke Leee00c9fe2014-04-12 12:42:06 -0700766 } else if (mInRegularSearch && mRegularSearchFragment != null) {
Yorke Leeb207f8a2013-08-29 18:51:06 -0700767 transaction.remove(mRegularSearchFragment);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700768 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700769
Yorke Leeb207f8a2013-08-29 18:51:06 -0700770 final String tag;
771 if (smartDialSearch) {
772 tag = TAG_SMARTDIAL_SEARCH_FRAGMENT;
773 } else {
774 tag = TAG_REGULAR_SEARCH_FRAGMENT;
775 }
776 mInDialpadSearch = smartDialSearch;
777 mInRegularSearch = !smartDialSearch;
778
Yorke Leeef2b7382013-08-09 17:39:25 -0700779 fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
780 if (fragment == null) {
781 if (smartDialSearch) {
782 fragment = new SmartDialSearchFragment();
783 } else {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700784 fragment = new RegularSearchFragment();
Yorke Leeef2b7382013-08-09 17:39:25 -0700785 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700786 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700787 // DialtactsActivity will provide the options menu
788 fragment.setHasOptionsMenu(false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700789 transaction.replace(R.id.dialtacts_frame, fragment, tag);
Yorke Leeef2b7382013-08-09 17:39:25 -0700790 transaction.addToBackStack(null);
791 fragment.setQueryString(query, false);
792 transaction.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700793 }
794
795 /**
Yorke Leec3766332013-07-31 11:13:16 -0700796 * Hides the search fragment
Chiao Cheng94b10b52012-08-17 16:59:12 -0700797 */
Yorke Leec3766332013-07-31 11:13:16 -0700798 private void exitSearchUi() {
Yorke Lee3487db72013-09-26 17:12:06 -0700799 // See related bug in enterSearchUI();
800 if (getFragmentManager().isDestroyed()) {
801 return;
802 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700803 // Go all the way back to the favorites fragment, regardless of how many times we
804 // transitioned between search fragments
Yorke Leeb207f8a2013-08-29 18:51:06 -0700805 getFragmentManager().popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
806 setNotInSearchUi();
Yorke Lee7eccf452014-03-14 12:52:42 -0700807
808 if (isDialpadShowing()) {
809 mFragmentsFrame.setAlpha(0);
810 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700811 }
812
813 /** Returns an Intent to launch Call Settings screen */
814 public static Intent getCallSettingsIntent() {
815 final Intent intent = new Intent(Intent.ACTION_MAIN);
816 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
817 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
818 return intent;
819 }
820
821 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700822 public void onBackPressed() {
Yorke Leeef2b7382013-08-09 17:39:25 -0700823 if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
Yorke Leef6673d32013-08-23 15:34:17 -0700824 hideDialpadFragment(true, false);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700825 } else if (getInSearchUi()) {
Yorke Lee53a22302014-04-29 18:13:46 -0700826 mSearchView.setText(null);
Yorke Leef6673d32013-08-23 15:34:17 -0700827 mDialpadFragment.clearDialpad();
Yorke Leec3766332013-07-31 11:13:16 -0700828 } else {
829 super.onBackPressed();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700830 }
Yorke Leec3766332013-07-31 11:13:16 -0700831 }
832
833 @Override
834 public void onDialpadQueryChanged(String query) {
Yorke Lee46635872014-04-11 11:20:15 -0700835 if (mSmartDialSearchFragment != null) {
836 mSmartDialSearchFragment.setAddToContactNumber(query);
837 }
Yorke Leec3766332013-07-31 11:13:16 -0700838 final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
839 SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
Yorke Lee53a22302014-04-29 18:13:46 -0700840
841 if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700842 if (DEBUG) {
843 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
844 }
845 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
846 // This callback can happen if the dialpad fragment is recreated because of
847 // activity destruction. In that case, don't update the search view because
848 // that would bring the user back to the search fragment regardless of the
849 // previous state of the application. Instead, just return here and let the
850 // fragment manager correctly figure out whatever fragment was last displayed.
Yorke Lee53a22302014-04-29 18:13:46 -0700851 if (!TextUtils.isEmpty(normalizedQuery)) {
852 mPendingSearchViewQuery = normalizedQuery;
853 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700854 return;
855 }
Yorke Lee53a22302014-04-29 18:13:46 -0700856 mSearchView.setText(normalizedQuery);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700857 }
858 }
Yorke Leec3766332013-07-31 11:13:16 -0700859
860 @Override
861 public void onListFragmentScrollStateChange(int scrollState) {
862 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700863 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700864 hideInputMethod(getCurrentFocus());
865 }
866 }
867
868 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700869 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
870 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700871 // TODO: No-op for now. This should eventually show/hide the actionBar based on
872 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700873 }
874
Yorke Lee86e21f72014-04-02 16:49:38 -0700875 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800876 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700877 if (mDialButton != null) {
878 mDialButton.setEnabled(enabled);
879 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800880 }
881
882 @Override
883 public void setDialButtonContainerVisible(boolean visible) {
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700884 mFloatingActionButton.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700885 }
886
887 private boolean phoneIsInUse() {
888 final TelephonyManager tm = (TelephonyManager) getSystemService(
889 Context.TELEPHONY_SERVICE);
890 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
891 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700892
Yorke Leee1424812013-09-04 18:24:48 -0700893 public static Intent getAddNumberToContactIntent(CharSequence text) {
894 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
895 intent.putExtra(Intents.Insert.PHONE, text);
896 intent.setType(Contacts.CONTENT_ITEM_TYPE);
897 return intent;
898 }
899
Yorke Leeda0f9042013-12-05 14:25:51 -0800900 private boolean canIntentBeHandled(Intent intent) {
901 final PackageManager packageManager = getPackageManager();
902 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
903 PackageManager.MATCH_DEFAULT_ONLY);
904 return resolveInfo != null && resolveInfo.size() > 0;
905 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800906
Yorke Lee6a1461a2014-04-21 16:27:14 -0700907 @Override
908 public void showCallHistory() {
909 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
910 // CONTENT_TYPE, so that we always open our call log from our dialer
911 final Intent intent = new Intent(this, CallLogActivity.class);
912 startActivity(intent);
913 }
914
Yorke Lee86e21f72014-04-02 16:49:38 -0700915 /**
916 * Called when the user has long-pressed a contact tile to start a drag operation.
917 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800918 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700919 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700920 getActionBar().hide();
Yorke Lee28266192014-05-01 10:05:52 -0700921 mRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800922 }
923
924 @Override
Yorke Lee28266192014-05-01 10:05:52 -0700925 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {
926 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800927
Yorke Lee86e21f72014-04-02 16:49:38 -0700928 /**
929 * Called when the user has released a contact tile after long-pressing it.
930 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800931 @Override
932 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700933 getActionBar().show();
Yorke Lee28266192014-05-01 10:05:52 -0700934 mRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800935 }
936
937 @Override
938 public void onDroppedOnRemove() {}
939
940 /**
Yorke Leed999b712014-04-23 15:10:58 -0700941 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800942 * once it has been attached to the activity.
943 */
944 @Override
945 public void setDragDropController(DragDropController dragController) {
Yorke Lee28266192014-05-01 10:05:52 -0700946 mDragDropController = dragController;
947 ((RemoveView) findViewById(R.id.remove_view))
948 .setDragDropController(dragController);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800949 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700950
951 @Override
952 public void onPickPhoneNumberAction(Uri dataUri) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700953 // Specify call-origin so that users will see the previous tab instead of
954 // CallLog screen (search UI will be automatically exited).
955 PhoneNumberInteraction.startInteractionForPhoneCall(
956 DialtactsActivity.this, dataUri, getCallOrigin());
957 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700958 }
959
960 @Override
961 public void onCallNumberDirectly(String phoneNumber) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700962 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
963 startActivity(intent);
964 mClearSearchOnPause = true;
Yorke Leee00c9fe2014-04-12 12:42:06 -0700965 }
966
967 @Override
968 public void onShortcutIntentCreated(Intent intent) {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700969 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
Yorke Leee00c9fe2014-04-12 12:42:06 -0700970 }
971
972 @Override
973 public void onHomeInActionBarSelected() {
Yorke Lee4e28c1d2014-05-13 15:38:10 -0700974 exitSearchUi();
Yorke Leee00c9fe2014-04-12 12:42:06 -0700975 }
Yorke Lee33932ff2014-04-16 13:34:32 -0700976
977 public int getActionBarHeight() {
978 return mActionBarHeight;
979 }
Yorke Leecc4660d2014-04-24 11:22:57 -0700980
981 @Override
982 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
983
984 }
985
986 @Override
987 public void onPageSelected(int position) {
988 mCurrentTabPosition = position;
Yorke Leec61d4542014-05-01 10:43:24 -0700989 // If the dialpad is showing, the floating action button should always be middle aligned.
990 if (!isDialpadShowing()) {
991 alignFloatingActionButtonByTab(mCurrentTabPosition);
992 }
Yorke Leecc4660d2014-04-24 11:22:57 -0700993 }
994
995 @Override
996 public void onPageScrollStateChanged(int state) {
997 }
998
999 private void alignFloatingActionButtonByTab(int position) {
1000 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
1001 alignFloatingActionButtonMiddle();
1002 } else {
1003 alignFloatingActionButtonRight();
1004 }
1005 }
1006
1007 private void alignFloatingActionButtonRight() {
1008 final RelativeLayout.LayoutParams params =
1009 (RelativeLayout.LayoutParams) mFloatingActionButton.getLayoutParams();
1010 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1011 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1012 mFloatingActionButton.setLayoutParams(params);
1013 }
1014
1015 private void alignFloatingActionButtonMiddle() {
1016 final RelativeLayout.LayoutParams params =
1017 (RelativeLayout.LayoutParams) mFloatingActionButton.getLayoutParams();
1018 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1019 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
1020 mFloatingActionButton.setLayoutParams(params);
1021 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001022}