blob: 1b4002cd13e02cd950f7b65ee5f70a6d775123e3 [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 Leec3766332013-07-31 11:13:16 -070019import android.animation.Animator;
Yorke Leecc4660d2014-04-24 11:22:57 -070020import android.animation.LayoutTransition;
Yorke Leec3766332013-07-31 11:13:16 -070021import android.animation.Animator.AnimatorListener;
22import android.animation.AnimatorListenerAdapter;
Chiao Cheng94b10b52012-08-17 16:59:12 -070023import android.app.Activity;
24import android.app.Fragment;
25import android.app.FragmentManager;
26import android.app.FragmentTransaction;
Yorke Lee86e21f72014-04-02 16:49:38 -070027import android.app.SearchManager;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080028import android.content.ActivityNotFoundException;
Chiao Cheng94b10b52012-08-17 16:59:12 -070029import android.content.Context;
30import android.content.Intent;
Yorke Leeda0f9042013-12-05 14:25:51 -080031import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
Yorke Lee33932ff2014-04-16 13:34:32 -070033import android.content.res.TypedArray;
Yorke Lee86e21f72014-04-02 16:49:38 -070034import android.graphics.Outline;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import android.net.Uri;
36import android.os.Bundle;
37import android.os.RemoteException;
38import android.os.ServiceManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039import android.provider.ContactsContract.Contacts;
Yorke Leee1424812013-09-04 18:24:48 -070040import android.provider.ContactsContract.Intents;
Yorke Leec3766332013-07-31 11:13:16 -070041import android.speech.RecognizerIntent;
Yorke Leecc4660d2014-04-24 11:22:57 -070042import android.support.v4.view.ViewPager;
Yorke Leec3766332013-07-31 11:13:16 -070043import android.telephony.TelephonyManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070044import android.text.TextUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045import android.util.Log;
Yorke Leecc4660d2014-04-24 11:22:57 -070046import android.view.Gravity;
Chiao Cheng94b10b52012-08-17 16:59:12 -070047import android.view.Menu;
Yorke Lee86e21f72014-04-02 16:49:38 -070048import android.view.MenuInflater;
Chiao Cheng94b10b52012-08-17 16:59:12 -070049import android.view.MenuItem;
Chiao Cheng94b10b52012-08-17 16:59:12 -070050import android.view.View;
Yorke Leea5d5cdf2014-04-22 17:20:00 -070051import android.view.View.OnClickListener;
Yorke Lee86e21f72014-04-02 16:49:38 -070052import android.view.View.OnLayoutChangeListener;
Yorke Leeb0d19762014-03-05 14:43:28 -080053import android.view.ViewGroup.LayoutParams;
54import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Yorke Lee86e21f72014-04-02 16:49:38 -070055import android.view.animation.AccelerateInterpolator;
56import android.view.animation.DecelerateInterpolator;
57import android.view.animation.Interpolator;
Chiao Cheng94b10b52012-08-17 16:59:12 -070058import android.view.inputmethod.InputMethodManager;
Yorke Leec3766332013-07-31 11:13:16 -070059import android.widget.AbsListView.OnScrollListener;
Yorke Leecc4660d2014-04-24 11:22:57 -070060import android.widget.FrameLayout;
Yorke Leeb0d19762014-03-05 14:43:28 -080061import android.widget.LinearLayout;
Chiao Cheng94b10b52012-08-17 16:59:12 -070062import android.widget.PopupMenu;
Yorke Leecc4660d2014-04-24 11:22:57 -070063import android.widget.RelativeLayout;
Yorke Lee86e21f72014-04-02 16:49:38 -070064import android.widget.SearchView;
65import android.widget.SearchView.OnQueryTextListener;
Chiao Cheng073b5cf2012-12-07 11:36:00 -080066import android.widget.Toast;
Chiao Cheng94b10b52012-08-17 16:59:12 -070067
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070068import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070069import com.android.contacts.common.activity.TransactionSafeActivity;
Yorke Leec3766332013-07-31 11:13:16 -070070import com.android.contacts.common.dialog.ClearFrequentsDialog;
71import com.android.contacts.common.interactions.ImportExportDialogFragment;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080072import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
Andrew Lee4cbc9732014-04-24 14:38:58 -070073import com.android.contacts.common.util.ViewUtil;
Yorke Leec3766332013-07-31 11:13:16 -070074import com.android.dialer.calllog.CallLogActivity;
Yorke Leefce269a2013-08-12 11:56:04 -070075import com.android.dialer.database.DialerDatabaseHelper;
Yorke Leef74011e2013-08-02 11:30:30 -070076import com.android.dialer.dialpad.DialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -070077import com.android.dialer.dialpad.SmartDialNameMatcher;
Yorke Leefce269a2013-08-12 11:56:04 -070078import com.android.dialer.dialpad.SmartDialPrefix;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070079import com.android.dialer.interactions.PhoneNumberInteraction;
Yorke Lee3cefcc62014-01-16 11:26:46 -080080import com.android.dialer.list.DragDropController;
Yorke Leee00c9fe2014-04-12 12:42:06 -070081import com.android.dialer.list.ListsFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080082import com.android.dialer.list.OnDragDropListener;
Yorke Leec3766332013-07-31 11:13:16 -070083import com.android.dialer.list.OnListFragmentScrolledListener;
Yorke Leed999b712014-04-23 15:10:58 -070084import com.android.dialer.list.SpeedDialFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080085import com.android.dialer.list.PhoneFavoriteTileView;
Yorke Leeefb2d9c2014-04-18 14:05:01 -070086import com.android.dialer.list.PhoneFavoriteSquareTileView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070087import com.android.dialer.list.RegularSearchFragment;
Yorke Lee3cefcc62014-01-16 11:26:46 -080088import com.android.dialer.list.RemoveView;
Jay Shrauner2e5b34b2013-08-29 10:52:40 -070089import com.android.dialer.list.SearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -070090import com.android.dialer.list.SmartDialSearchFragment;
Yorke Lee0baa98b2013-08-22 10:55:16 -070091import com.android.dialerbind.DatabaseHelperManager;
Chiao Cheng94b10b52012-08-17 16:59:12 -070092import com.android.internal.telephony.ITelephony;
93
Yorke Leec3766332013-07-31 11:13:16 -070094import java.util.ArrayList;
Yorke Leeda0f9042013-12-05 14:25:51 -080095import java.util.List;
Yorke Leec3766332013-07-31 11:13:16 -070096
Chiao Cheng94b10b52012-08-17 16:59:12 -070097/**
Chiao Cheng94b10b52012-08-17 16:59:12 -070098 * The dialer tab's title is 'phone', a more common name (see strings.xml).
99 */
Yorke Leec3766332013-07-31 11:13:16 -0700100public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
Yorke Lee86e21f72014-04-02 16:49:38 -0700101 DialpadFragment.OnDialpadQueryChangedListener,
Yorke Leec3766332013-07-31 11:13:16 -0700102 OnListFragmentScrolledListener,
Ihab Awad526c0b82014-02-27 12:55:36 -0800103 DialpadFragment.HostInterface,
Yorke Lee6a1461a2014-04-21 16:27:14 -0700104 ListsFragment.HostInterface,
Yorke Leed999b712014-04-23 15:10:58 -0700105 SpeedDialFragment.HostInterface,
Yorke Leee00c9fe2014-04-12 12:42:06 -0700106 OnDragDropListener, View.OnLongClickListener,
Yorke Leecc4660d2014-04-24 11:22:57 -0700107 OnPhoneNumberPickerActionListener,
108 ViewPager.OnPageChangeListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700109 private static final String TAG = "DialtactsActivity";
110
Ihab Awad526c0b82014-02-27 12:55:36 -0800111 public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700112
Yorke Leef74011e2013-08-02 11:30:30 -0700113 public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
114
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 /** Used to open Call Setting */
116 private static final String PHONE_PACKAGE = "com.android.phone";
117 private static final String CALL_SETTINGS_CLASS_NAME =
118 "com.android.phone.CallFeaturesSetting";
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119 /** @see #getCallOrigin() */
120 private static final String CALL_ORIGIN_DIALTACTS =
121 "com.android.dialer.DialtactsActivity";
122
Yorke Leeb207f8a2013-08-29 18:51:06 -0700123 private static final String KEY_IN_REGULAR_SEARCH_UI = "in_regular_search_ui";
124 private static final String KEY_IN_DIALPAD_SEARCH_UI = "in_dialpad_search_ui";
Yorke Leeef2b7382013-08-09 17:39:25 -0700125 private static final String KEY_SEARCH_QUERY = "search_query";
126 private static final String KEY_FIRST_LAUNCH = "first_launch";
127
Yorke Leec3766332013-07-31 11:13:16 -0700128 private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
129 private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
130 private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
131 private static final String TAG_FAVORITES_FRAGMENT = "favorites";
Yorke Leec3766332013-07-31 11:13:16 -0700132
Chiao Cheng94b10b52012-08-17 16:59:12 -0700133 /**
134 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
135 */
136 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
137
Yorke Leec3766332013-07-31 11:13:16 -0700138 private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700139
Yorke Lee86e21f72014-04-02 16:49:38 -0700140 private static final int ANIMATION_DURATION = 250;
Yorke Lee3cefcc62014-01-16 11:26:46 -0800141
Yorke Leec3766332013-07-31 11:13:16 -0700142 /**
Yorke Leec3766332013-07-31 11:13:16 -0700143 * Fragment containing the dialpad that slides into view
Chiao Cheng94b10b52012-08-17 16:59:12 -0700144 */
Yorke Leef74011e2013-08-02 11:30:30 -0700145 private DialpadFragment mDialpadFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700146
147 /**
148 * Fragment for searching phone numbers using the alphanumeric keyboard.
149 */
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700150 private RegularSearchFragment mRegularSearchFragment;
Yorke Leec3766332013-07-31 11:13:16 -0700151
152 /**
153 * Fragment for searching phone numbers using the dialpad.
154 */
155 private SmartDialSearchFragment mSmartDialSearchFragment;
156
Yorke Leee00c9fe2014-04-12 12:42:06 -0700157 /**
158 * Fragment containing the speed dial list, recents list, and all contacts list.
159 */
160 private ListsFragment mListsFragment;
161
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700162 private View mFloatingActionButton;
Yorke Lee86e21f72014-04-02 16:49:38 -0700163 private View mMenuButton;
Yorke Leec3766332013-07-31 11:13:16 -0700164 private View mDialpadButton;
Ihab Awad526c0b82014-02-27 12:55:36 -0800165 private View mDialButton;
Yorke Leec3766332013-07-31 11:13:16 -0700166
Yorke Leeb207f8a2013-08-29 18:51:06 -0700167 private View mFragmentsFrame;
Yorke Leec3766332013-07-31 11:13:16 -0700168
Yorke Lee33932ff2014-04-16 13:34:32 -0700169 private int mActionBarHeight;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700170 private boolean mInDialpadSearch;
171 private boolean mInRegularSearch;
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700172 private boolean mClearSearchOnPause;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700173
Yorke Leeef2b7382013-08-09 17:39:25 -0700174 /**
Yorke Leecc4660d2014-04-24 11:22:57 -0700175 * The position of the currently selected tab in the attached {@link ListsFragment}.
176 */
177 private int mCurrentTabPosition = 0;
178
179 /**
Yorke Lee35127cd2013-09-10 14:09:29 -0700180 * True if the dialpad is only temporarily showing due to being in call
181 */
182 private boolean mInCallDialpadUp;
183
184 /**
Yorke Leeef2b7382013-08-09 17:39:25 -0700185 * True when this activity has been launched for the first time.
186 */
Yorke Lee98702de2013-08-06 12:03:32 -0700187 private boolean mFirstLaunch;
Yorke Lee86e21f72014-04-02 16:49:38 -0700188
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700189 /**
190 * Search query to be applied to the SearchView in the ActionBar once
191 * onCreateOptionsMenu has been called.
192 */
193 private String mPendingSearchViewQuery;
194
Yorke Lee94f49042014-03-03 14:03:08 -0800195 // This view points to the Framelayout that houses both the search view and remove view
196 // containers.
197 private View mSearchAndRemoveViewContainer;
Yorke Lee86e21f72014-04-02 16:49:38 -0700198 private SearchView mSearchView;
199 /**
200 * View that contains the "Remove" dialog that shows up when the user long presses a contact.
201 * If the user releases a contact when hovering on top of this, the contact is unfavorited and
202 * removed from the speed dial list.
203 */
204 private RemoveView mRemoveViewContainer;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700205
Yorke Lee86e21f72014-04-02 16:49:38 -0700206 final Interpolator hideActionBarInterpolator = new AccelerateInterpolator(1.5f);
207 final Interpolator showActionBarInterpolator = new DecelerateInterpolator(1.5f);
Yorke Leeef2b7382013-08-09 17:39:25 -0700208 private String mSearchQuery;
209
Yorke Leefce269a2013-08-12 11:56:04 -0700210 private DialerDatabaseHelper mDialerDatabaseHelper;
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 /**
228 * Listener used when one of phone numbers in search UI is selected. This will initiate a
229 * phone call using the phone number.
230 */
231 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
232 new OnPhoneNumberPickerActionListener() {
233 @Override
234 public void onPickPhoneNumberAction(Uri dataUri) {
235 // Specify call-origin so that users will see the previous tab instead of
236 // CallLog screen (search UI will be automatically exited).
237 PhoneNumberInteraction.startInteractionForPhoneCall(
Yorke Leec3766332013-07-31 11:13:16 -0700238 DialtactsActivity.this, dataUri, getCallOrigin());
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700239 mClearSearchOnPause = true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700240 }
241
242 @Override
Jay Shrauner31a760b2013-07-26 09:34:49 -0700243 public void onCallNumberDirectly(String phoneNumber) {
244 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
245 startActivity(intent);
Yorke Lee8e5c8b12013-10-01 14:37:55 -0700246 mClearSearchOnPause = true;
Jay Shrauner31a760b2013-07-26 09:34:49 -0700247 }
248
249 @Override
Chiao Cheng94b10b52012-08-17 16:59:12 -0700250 public void onShortcutIntentCreated(Intent intent) {
251 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
252 }
253
254 @Override
255 public void onHomeInActionBarSelected() {
256 exitSearchUi();
257 }
258 };
259
260 /**
261 * Listener used to send search queries to the phone search fragment.
262 */
Yorke Lee86e21f72014-04-02 16:49:38 -0700263 private final OnQueryTextListener mPhoneSearchQueryTextListener = new OnQueryTextListener() {
Yorke Leec3766332013-07-31 11:13:16 -0700264 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700265 public boolean onQueryTextSubmit(String query) {
266 return false;
Yorke Leec3766332013-07-31 11:13:16 -0700267 }
268
269 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700270 public boolean onQueryTextChange(String newText) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700271 if (newText.equals(mSearchQuery)) {
272 // If the query hasn't changed (perhaps due to activity being destroyed
273 // and restored, or user launching the same DIAL intent twice), then there is
274 // no need to do anything here.
Yorke Lee86e21f72014-04-02 16:49:38 -0700275 return true;
Yorke Leeef2b7382013-08-09 17:39:25 -0700276 }
277 mSearchQuery = newText;
278 if (DEBUG) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700279 Log.d(TAG, "onTextChange for mSearchView called with new query: " + newText);
Yorke Leeef2b7382013-08-09 17:39:25 -0700280 }
Yorke Leeb207f8a2013-08-29 18:51:06 -0700281 final boolean dialpadSearch = isDialpadShowing();
Yorke Leeef2b7382013-08-09 17:39:25 -0700282
Yorke Leec3766332013-07-31 11:13:16 -0700283 // Show search result with non-empty text. Show a bare list otherwise.
Yorke Leeb207f8a2013-08-29 18:51:06 -0700284 if (TextUtils.isEmpty(newText) && getInSearchUi()) {
Yorke Leec3766332013-07-31 11:13:16 -0700285 exitSearchUi();
Yorke Lee86e21f72014-04-02 16:49:38 -0700286 return true;
Yorke Leeb207f8a2013-08-29 18:51:06 -0700287 } else if (!TextUtils.isEmpty(newText)) {
288 final boolean sameSearchMode = (dialpadSearch && mInDialpadSearch) ||
289 (!dialpadSearch && mInRegularSearch);
290 if (!sameSearchMode) {
291 // call enterSearchUi only if we are switching search modes, or entering
292 // search ui for the first time
293 enterSearchUi(dialpadSearch, newText);
294 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700295
Yorke Leeb207f8a2013-08-29 18:51:06 -0700296 if (dialpadSearch && mSmartDialSearchFragment != null) {
297 mSmartDialSearchFragment.setQueryString(newText, false);
298 } else if (mRegularSearchFragment != null) {
299 mRegularSearchFragment.setQueryString(newText, false);
300 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700301 return true;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700302 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700303 return true;
Yorke Leec3766332013-07-31 11:13:16 -0700304 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700305 };
306
Yorke Leec3766332013-07-31 11:13:16 -0700307 private boolean isDialpadShowing() {
308 return mDialpadFragment != null && mDialpadFragment.isVisible();
309 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700310
311 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700312 protected void onCreate(Bundle savedInstanceState) {
313 super.onCreate(savedInstanceState);
Yorke Lee98702de2013-08-06 12:03:32 -0700314 mFirstLaunch = true;
315
Yorke Lee8898cd02013-08-08 10:24:27 -0700316 setContentView(R.layout.dialtacts_activity);
Yorke Lee9e91bb02014-03-05 17:50:50 -0800317 getWindow().setBackgroundDrawable(null);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700318
Yorke Lee86e21f72014-04-02 16:49:38 -0700319 getActionBar().setDisplayShowHomeEnabled(false);
320 getActionBar().setDisplayShowTitleEnabled(false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700321
Yorke Lee33932ff2014-04-16 13:34:32 -0700322 final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
323 new int[] { android.R.attr.actionBarSize });
324 mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
325 styledAttributes.recycle();
326
Yorke Leeef2b7382013-08-09 17:39:25 -0700327 // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
328 // is null. Otherwise the fragment manager takes care of recreating these fragments.
Yorke Leec3766332013-07-31 11:13:16 -0700329 if (savedInstanceState == null) {
Ihab Awad526c0b82014-02-27 12:55:36 -0800330 getFragmentManager().beginTransaction()
Yorke Leee00c9fe2014-04-12 12:42:06 -0700331 .add(R.id.dialtacts_frame, new ListsFragment(), TAG_FAVORITES_FRAGMENT)
Ihab Awad526c0b82014-02-27 12:55:36 -0800332 .add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT)
333 .commit();
Yorke Leeef2b7382013-08-09 17:39:25 -0700334 } else {
335 mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
Yorke Leeb207f8a2013-08-29 18:51:06 -0700336 mInRegularSearch = savedInstanceState.getBoolean(KEY_IN_REGULAR_SEARCH_UI);
337 mInDialpadSearch = savedInstanceState.getBoolean(KEY_IN_DIALPAD_SEARCH_UI);
Yorke Leeef2b7382013-08-09 17:39:25 -0700338 mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700339 }
340
Yorke Leecc4660d2014-04-24 11:22:57 -0700341 RelativeLayout parent = (RelativeLayout) findViewById(R.id.dialtacts_mainlayout);
342 parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
343
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 Lee3cefcc62014-01-16 11:26:46 -0800354 mRemoveViewContainer = (RemoveView) findViewById(R.id.remove_view_container);
Yorke Leeb0d19762014-03-05 14:43:28 -0800355 mSearchAndRemoveViewContainer = findViewById(R.id.search_and_remove_view_container);
356
Yorke Lee0baa98b2013-08-22 10:55:16 -0700357 mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
Yorke Leefce269a2013-08-12 11:56:04 -0700358 SmartDialPrefix.initializeNanpSettings(this);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700359 }
360
361 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700362 protected void onResume() {
363 super.onResume();
Yorke Lee98702de2013-08-06 12:03:32 -0700364 if (mFirstLaunch) {
365 displayFragment(getIntent());
Yorke Lee35127cd2013-09-10 14:09:29 -0700366 } else if (!phoneIsInUse() && mInCallDialpadUp) {
367 hideDialpadFragment(false, true);
368 mInCallDialpadUp = false;
Yorke Lee98702de2013-08-06 12:03:32 -0700369 }
370 mFirstLaunch = false;
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 Lee63ea4cd2013-08-12 11:04:47 -0700401 mSmartDialSearchFragment.setOnPhoneNumberPickerActionListener(
402 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700403 if (mFragmentsFrame != null) {
404 mFragmentsFrame.setAlpha(1.0f);
405 }
Yorke Leeef2b7382013-08-09 17:39:25 -0700406 } else if (fragment instanceof SearchFragment) {
Jay Shrauner2e5b34b2013-08-29 10:52:40 -0700407 mRegularSearchFragment = (RegularSearchFragment) fragment;
Yorke Lee63ea4cd2013-08-12 11:04:47 -0700408 mRegularSearchFragment.setOnPhoneNumberPickerActionListener(
409 mPhoneNumberPickerActionListener);
Yorke Lee7eccf452014-03-14 12:52:42 -0700410 if (mFragmentsFrame != null) {
411 mFragmentsFrame.setAlpha(1.0f);
412 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700413 } else if (fragment instanceof ListsFragment) {
414 mListsFragment = (ListsFragment) fragment;
Yorke Leecc4660d2014-04-24 11:22:57 -0700415 mListsFragment.addOnPageChangeListener(this);
Yorke Leec3766332013-07-31 11:13:16 -0700416 }
Yorke Leec3766332013-07-31 11:13:16 -0700417 }
418
Alon Albertb453e5b2013-09-10 15:54:23 -0700419 protected void handleMenuSettings() {
420 openTelephonySetting(this);
421 }
422
423 public static void openTelephonySetting(Activity activity) {
424 final Intent settingsIntent = getCallSettingsIntent();
425 activity.startActivity(settingsIntent);
426 }
427
Chiao Cheng94b10b52012-08-17 16:59:12 -0700428 @Override
429 public void onClick(View view) {
430 switch (view.getId()) {
Yorke Leec3766332013-07-31 11:13:16 -0700431 case R.id.dialpad_button:
Yorke Lee35127cd2013-09-10 14:09:29 -0700432 // Reset the boolean flag that tracks whether the dialpad was up because
433 // we were in call. Regardless of whether it was true before, we want to
434 // show the dialpad because the user has explicitly clicked the dialpad
435 // button.
436 mInCallDialpadUp = false;
Yorke Leec3766332013-07-31 11:13:16 -0700437 showDialpadFragment(true);
438 break;
Yorke Lee86e21f72014-04-02 16:49:38 -0700439 case R.id.dial_button:
440 // Dial button was pressed; tell the Dialpad fragment
441 mDialpadFragment.dialButtonPressed();
442 break;
Yorke Leec3766332013-07-31 11:13:16 -0700443 case R.id.search_close_button:
444 // Clear the search field
Yorke Lee86e21f72014-04-02 16:49:38 -0700445 if (!TextUtils.isEmpty(mSearchView.getQuery())) {
Yorke Lee2eafb7a2013-09-10 14:34:01 -0700446 mDialpadFragment.clearDialpad();
Yorke Lee86e21f72014-04-02 16:49:38 -0700447 mSearchView.setQuery("", false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700448 }
449 break;
Yorke Leec3766332013-07-31 11:13:16 -0700450 case R.id.voice_search_button:
Yorke Leeda0f9042013-12-05 14:25:51 -0800451 try {
452 startActivityForResult(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
453 ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
454 } catch (ActivityNotFoundException e) {
455 Toast.makeText(DialtactsActivity.this, R.string.voice_search_not_available,
456 Toast.LENGTH_SHORT).show();
457 }
Yorke Leec3766332013-07-31 11:13:16 -0700458 break;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700459 default: {
460 Log.wtf(TAG, "Unexpected onClick event from " + view);
461 break;
462 }
463 }
464 }
465
Yorke Leec3766332013-07-31 11:13:16 -0700466 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700467 public boolean onOptionsItemSelected(MenuItem item) {
468 switch (item.getItemId()) {
469 case R.id.menu_history:
Yorke Lee6a1461a2014-04-21 16:27:14 -0700470 showCallHistory();
Yorke Lee86e21f72014-04-02 16:49:38 -0700471 break;
472 case R.id.menu_add_contact:
473 try {
474 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
475 } catch (ActivityNotFoundException e) {
476 Toast toast = Toast.makeText(this,
477 R.string.add_contact_not_available,
478 Toast.LENGTH_SHORT);
479 toast.show();
480 }
481 break;
482 case R.id.menu_import_export:
483 // We hard-code the "contactsAreAvailable" argument because doing it properly would
484 // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
485 // now in Dialtacts for (potential) performance reasons. Compare with how it is
486 // done in {@link PeopleActivity}.
487 ImportExportDialogFragment.show(getFragmentManager(), true,
488 DialtactsActivity.class);
489 return true;
490 case R.id.menu_clear_frequents:
491 // TODO: This should be enabled/disabled based on
Yorke Leed999b712014-04-23 15:10:58 -0700492 // SpeedDialFragment.hasFrequents
Yorke Lee86e21f72014-04-02 16:49:38 -0700493 ClearFrequentsDialog.show(getFragmentManager());
494 return true;
495 case R.id.menu_call_settings:
496 handleMenuSettings();
497 return true;
Yorke Lee86e21f72014-04-02 16:49:38 -0700498 }
499 return false;
500 }
501
502 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800503 public boolean onLongClick(View view) {
504 switch (view.getId()) {
505 case R.id.dial_button: {
506 // Dial button was pressed; tell the Dialpad fragment
507 mDialpadFragment.dialButtonPressed();
508 return true; // Consume the event
509 }
510 default: {
511 Log.wtf(TAG, "Unexpected onClick event from " + view);
512 break;
513 }
514 }
515 return false;
516 }
517
518 @Override
Yorke Leec3766332013-07-31 11:13:16 -0700519 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
520 if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
521 if (resultCode == RESULT_OK) {
522 final ArrayList<String> matches = data.getStringArrayListExtra(
523 RecognizerIntent.EXTRA_RESULTS);
524 if (matches.size() > 0) {
525 final String match = matches.get(0);
Yorke Lee86e21f72014-04-02 16:49:38 -0700526 mSearchView.setQuery(match, false);
Yorke Leec3766332013-07-31 11:13:16 -0700527 } else {
528 Log.e(TAG, "Voice search - nothing heard");
529 }
530 } else {
531 Log.e(TAG, "Voice search failed");
532 }
533 }
534 super.onActivityResult(requestCode, resultCode, data);
535 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700536
Yorke Leec3766332013-07-31 11:13:16 -0700537 private void showDialpadFragment(boolean animate) {
Yorke Leeea695cb2013-08-09 11:02:26 -0700538 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700539 final FragmentTransaction ft = getFragmentManager().beginTransaction();
Yorke Leec3766332013-07-31 11:13:16 -0700540 if (animate) {
541 ft.setCustomAnimations(R.anim.slide_in, 0);
Yorke Leeea695cb2013-08-09 11:02:26 -0700542 } else {
543 mDialpadFragment.setYFraction(0);
Yorke Leec3766332013-07-31 11:13:16 -0700544 }
545 ft.show(mDialpadFragment);
546 ft.commit();
547 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700548
Yorke Leeca195042013-09-25 16:29:38 -0700549 public void hideDialpadFragment(boolean animate, boolean clearDialpad) {
Yorke Leef6673d32013-08-23 15:34:17 -0700550 if (mDialpadFragment == null) return;
551 if (clearDialpad) {
552 mDialpadFragment.clearDialpad();
553 }
554 if (!mDialpadFragment.isVisible()) return;
Yorke Leeea695cb2013-08-09 11:02:26 -0700555 mDialpadFragment.setAdjustTranslationForAnimation(animate);
Yorke Leec3766332013-07-31 11:13:16 -0700556 final FragmentTransaction ft = getFragmentManager().beginTransaction();
557 if (animate) {
558 ft.setCustomAnimations(0, R.anim.slide_out);
559 }
560 ft.hide(mDialpadFragment);
561 ft.commit();
Chiao Cheng94b10b52012-08-17 16:59:12 -0700562 }
563
Yorke Leec3766332013-07-31 11:13:16 -0700564 final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
565 @Override
566 public void onAnimationEnd(Animator animation) {
Yorke Lee94f49042014-03-03 14:03:08 -0800567 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700568 }
569 };
Chiao Cheng94b10b52012-08-17 16:59:12 -0700570
Yorke Leeb207f8a2013-08-29 18:51:06 -0700571 private boolean getInSearchUi() {
572 return mInDialpadSearch || mInRegularSearch;
573 }
574
575 private void setNotInSearchUi() {
576 mInDialpadSearch = false;
577 mInRegularSearch = false;
578 }
579
Yorke Lee311969c2013-08-26 06:31:11 -0700580 private void hideDialpadAndSearchUi() {
Yorke Lee86e21f72014-04-02 16:49:38 -0700581 mSearchView.setQuery("", false);
Yorke Lee311969c2013-08-26 06:31:11 -0700582 hideDialpadFragment(false, true);
583 }
584
Yorke Lee86e21f72014-04-02 16:49:38 -0700585 /**
586 * Callback from child DialpadFragment when the dialpad is shown.
587 */
588 public void onDialpadShown() {
589 mDialButton.setVisibility(View.VISIBLE);
590 mDialpadButton.setVisibility(View.GONE);
Yorke Leeb0d19762014-03-05 14:43:28 -0800591
Yorke Lee86e21f72014-04-02 16:49:38 -0700592 SearchFragment fragment = null;
593 if (mInDialpadSearch) {
594 fragment = mSmartDialSearchFragment;
595 } else if (mInRegularSearch) {
596 fragment = mRegularSearchFragment;
597 }
598 if (fragment != null && fragment.isVisible()) {
Yorke Lee33932ff2014-04-16 13:34:32 -0700599 fragment.getListView().animate().translationY(-mActionBarHeight)
Yorke Lee86e21f72014-04-02 16:49:38 -0700600 .setInterpolator(hideActionBarInterpolator).setDuration(ANIMATION_DURATION);
601 }
Christine Chen9c1e0652013-05-23 15:40:19 -0700602
Yorke Lee33932ff2014-04-16 13:34:32 -0700603 if (mListsFragment != null && mListsFragment.isResumed() && mListsFragment.isVisible()) {
Yorke Lee7eccf452014-03-14 12:52:42 -0700604 // If the favorites fragment is showing, fade to blank.
605 mFragmentsFrame.animate().alpha(0.0f);
606 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700607 getActionBar().hide();
Yorke Leecc4660d2014-04-24 11:22:57 -0700608 alignFloatingActionButtonMiddle();
Yorke Leec3766332013-07-31 11:13:16 -0700609 }
610
Yorke Lee86e21f72014-04-02 16:49:38 -0700611 /**
612 * Callback from child DialpadFragment when the dialpad is hidden.
613 */
614 public void onDialpadHidden() {
615 mDialButton.setVisibility(View.GONE);
616 mDialpadButton.setVisibility(View.VISIBLE);
Yorke Leec3766332013-07-31 11:13:16 -0700617
Yorke Lee86e21f72014-04-02 16:49:38 -0700618 SearchFragment fragment = null;
619 if (mInDialpadSearch) {
620 fragment = mSmartDialSearchFragment;
621 } else if (mInRegularSearch) {
622 fragment = mRegularSearchFragment;
623 }
624 if (fragment != null && fragment.isVisible()) {
625 fragment.getListView().animate().translationY(0)
626 .setInterpolator(showActionBarInterpolator).setDuration(ANIMATION_DURATION);
Yorke Leec3766332013-07-31 11:13:16 -0700627 }
628
Yorke Leee00c9fe2014-04-12 12:42:06 -0700629 if (mListsFragment != null && mListsFragment.isVisible()) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700630 mFragmentsFrame.animate().alpha(1.0f);
631 }
632 getActionBar().show();
Yorke Leecc4660d2014-04-24 11:22:57 -0700633 alignFloatingActionButtonByTab(mCurrentTabPosition);
Yorke Leec3766332013-07-31 11:13:16 -0700634 }
635
Yorke Leee00c9fe2014-04-12 12:42:06 -0700636 private void hideInputMethod(View view) {
637 final InputMethodManager imm = (InputMethodManager) getSystemService(
638 Context.INPUT_METHOD_SERVICE);
639 if (imm != null && view != null) {
640 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
641 }
642 }
643
Yorke Lee86e21f72014-04-02 16:49:38 -0700644 @Override
645 public boolean onCreateOptionsMenu(Menu menu) {
646 if (DEBUG) {
647 Log.d(TAG, "onCreateOptionsMenu");
648 }
649 MenuInflater inflater = getMenuInflater();
650 inflater.inflate(R.menu.dialtacts_options, menu);
651 final MenuItem searchItem = menu.findItem(R.id.menu_search);
652 mSearchView = (SearchView) searchItem.getActionView();
653 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
654 mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
655 mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener);
656 mSearchView.setIconifiedByDefault(false);
Yorke Lee3c9e7cd2014-04-16 11:37:21 -0700657
658 if (mPendingSearchViewQuery != null) {
659 mSearchView.setQuery(mPendingSearchViewQuery, false);
660 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 Lee86e21f72014-04-02 16:49:38 -0700826 mSearchView.setQuery(null, false);
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 Lee3c9e7cd2014-04-16 11:37:21 -0700840 if (mSearchView == null) {
841 if (!TextUtils.isEmpty(normalizedQuery)) {
842 mPendingSearchViewQuery = normalizedQuery;
843 }
844 return;
845 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700846 if (!TextUtils.equals(mSearchView.getQuery(), normalizedQuery)) {
Yorke Leeef2b7382013-08-09 17:39:25 -0700847 if (DEBUG) {
848 Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
849 }
850 if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
851 // This callback can happen if the dialpad fragment is recreated because of
852 // activity destruction. In that case, don't update the search view because
853 // that would bring the user back to the search fragment regardless of the
854 // previous state of the application. Instead, just return here and let the
855 // fragment manager correctly figure out whatever fragment was last displayed.
856 return;
857 }
Yorke Lee86e21f72014-04-02 16:49:38 -0700858 mSearchView.setQuery(normalizedQuery, false);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700859 }
860 }
Yorke Leec3766332013-07-31 11:13:16 -0700861
862 @Override
863 public void onListFragmentScrollStateChange(int scrollState) {
864 if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Yorke Leef6673d32013-08-23 15:34:17 -0700865 hideDialpadFragment(true, false);
Yorke Leec3766332013-07-31 11:13:16 -0700866 hideInputMethod(getCurrentFocus());
867 }
868 }
869
870 @Override
Yorke Lee86e21f72014-04-02 16:49:38 -0700871 public void onListFragmentScroll(int firstVisibleItem, int visibleItemCount,
872 int totalItemCount) {
Yorke Leee00c9fe2014-04-12 12:42:06 -0700873 // TODO: No-op for now. This should eventually show/hide the actionBar based on
874 // interactions with the ListsFragments.
Yorke Lee86e21f72014-04-02 16:49:38 -0700875 }
876
Yorke Lee86e21f72014-04-02 16:49:38 -0700877 @Override
Ihab Awad526c0b82014-02-27 12:55:36 -0800878 public void setDialButtonEnabled(boolean enabled) {
Yorke Lee1dfa8dd2014-03-25 00:11:31 -0700879 if (mDialButton != null) {
880 mDialButton.setEnabled(enabled);
881 }
Ihab Awad526c0b82014-02-27 12:55:36 -0800882 }
883
884 @Override
885 public void setDialButtonContainerVisible(boolean visible) {
Yorke Leea5d5cdf2014-04-22 17:20:00 -0700886 mFloatingActionButton.setVisibility(visible ? View.VISIBLE : View.GONE);
Yorke Leec3766332013-07-31 11:13:16 -0700887 }
888
889 private boolean phoneIsInUse() {
890 final TelephonyManager tm = (TelephonyManager) getSystemService(
891 Context.TELEPHONY_SERVICE);
892 return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
893 }
Yorke Lee8dd62002013-08-08 15:57:20 -0700894
Yorke Leee1424812013-09-04 18:24:48 -0700895 public static Intent getAddNumberToContactIntent(CharSequence text) {
896 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
897 intent.putExtra(Intents.Insert.PHONE, text);
898 intent.setType(Contacts.CONTENT_ITEM_TYPE);
899 return intent;
900 }
901
Yorke Leeda0f9042013-12-05 14:25:51 -0800902 private boolean canIntentBeHandled(Intent intent) {
903 final PackageManager packageManager = getPackageManager();
904 final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
905 PackageManager.MATCH_DEFAULT_ONLY);
906 return resolveInfo != null && resolveInfo.size() > 0;
907 }
Yorke Lee3cefcc62014-01-16 11:26:46 -0800908
Yorke Lee6a1461a2014-04-21 16:27:14 -0700909 @Override
910 public void showCallHistory() {
911 // Use explicit CallLogActivity intent instead of ACTION_VIEW +
912 // CONTENT_TYPE, so that we always open our call log from our dialer
913 final Intent intent = new Intent(this, CallLogActivity.class);
914 startActivity(intent);
915 }
916
Yorke Lee86e21f72014-04-02 16:49:38 -0700917 /**
918 * Called when the user has long-pressed a contact tile to start a drag operation.
919 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800920 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700921 public void onDragStarted(int x, int y, PhoneFavoriteSquareTileView view) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700922 getActionBar().hide();
923 mSearchAndRemoveViewContainer.setVisibility(View.VISIBLE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800924 }
925
926 @Override
Yorke Leeefb2d9c2014-04-18 14:05:01 -0700927 public void onDragHovered(int x, int y, PhoneFavoriteSquareTileView view) {}
Yorke Lee3cefcc62014-01-16 11:26:46 -0800928
Yorke Lee86e21f72014-04-02 16:49:38 -0700929 /**
930 * Called when the user has released a contact tile after long-pressing it.
931 */
Yorke Lee3cefcc62014-01-16 11:26:46 -0800932 @Override
933 public void onDragFinished(int x, int y) {
Yorke Lee86e21f72014-04-02 16:49:38 -0700934 getActionBar().show();
935 mSearchAndRemoveViewContainer.setVisibility(View.GONE);
Yorke Lee3cefcc62014-01-16 11:26:46 -0800936 }
937
938 @Override
939 public void onDroppedOnRemove() {}
940
941 /**
Yorke Leed999b712014-04-23 15:10:58 -0700942 * Allows the SpeedDialFragment to attach the drag controller to mRemoveViewContainer
Yorke Lee3cefcc62014-01-16 11:26:46 -0800943 * once it has been attached to the activity.
944 */
945 @Override
946 public void setDragDropController(DragDropController dragController) {
947 mRemoveViewContainer.setDragDropController(dragController);
948 }
Yorke Leee00c9fe2014-04-12 12:42:06 -0700949
950 @Override
951 public void onPickPhoneNumberAction(Uri dataUri) {
952 mPhoneNumberPickerActionListener.onPickPhoneNumberAction(dataUri);
953 }
954
955 @Override
956 public void onCallNumberDirectly(String phoneNumber) {
957 mPhoneNumberPickerActionListener.onCallNumberDirectly(phoneNumber);
958 }
959
960 @Override
961 public void onShortcutIntentCreated(Intent intent) {
962 mPhoneNumberPickerActionListener.onShortcutIntentCreated(intent);
963 }
964
965 @Override
966 public void onHomeInActionBarSelected() {
967 mPhoneNumberPickerActionListener.onHomeInActionBarSelected();
968 }
Yorke Lee33932ff2014-04-16 13:34:32 -0700969
970 public int getActionBarHeight() {
971 return mActionBarHeight;
972 }
Yorke Leecc4660d2014-04-24 11:22:57 -0700973
974 @Override
975 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
976
977 }
978
979 @Override
980 public void onPageSelected(int position) {
981 mCurrentTabPosition = position;
982 alignFloatingActionButtonByTab(mCurrentTabPosition);
983 }
984
985 @Override
986 public void onPageScrollStateChanged(int state) {
987 }
988
989 private void alignFloatingActionButtonByTab(int position) {
990 if (position == ListsFragment.TAB_INDEX_SPEED_DIAL) {
991 alignFloatingActionButtonMiddle();
992 } else {
993 alignFloatingActionButtonRight();
994 }
995 }
996
997 private void alignFloatingActionButtonRight() {
998 final RelativeLayout.LayoutParams params =
999 (RelativeLayout.LayoutParams) mFloatingActionButton.getLayoutParams();
1000 params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
1001 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1002 mFloatingActionButton.setLayoutParams(params);
1003 }
1004
1005 private void alignFloatingActionButtonMiddle() {
1006 final RelativeLayout.LayoutParams params =
1007 (RelativeLayout.LayoutParams) mFloatingActionButton.getLayoutParams();
1008 params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
1009 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
1010 mFloatingActionButton.setLayoutParams(params);
1011 }
Chiao Cheng94b10b52012-08-17 16:59:12 -07001012}