Yorke Lee | 6b04912 | 2013-07-16 10:38:02 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 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 | |
| 17 | package com.android.dialer; |
| 18 | |
| 19 | import android.app.ActionBar; |
| 20 | import android.app.ActionBar.LayoutParams; |
| 21 | import android.app.ActionBar.Tab; |
| 22 | import android.app.ActionBar.TabListener; |
| 23 | import android.app.Activity; |
| 24 | import android.app.backup.BackupManager; |
| 25 | import android.app.Fragment; |
| 26 | import android.app.FragmentManager; |
| 27 | import android.app.FragmentTransaction; |
| 28 | import android.content.ActivityNotFoundException; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.SharedPreferences; |
| 32 | import android.net.Uri; |
| 33 | import android.os.Bundle; |
| 34 | import android.os.RemoteException; |
| 35 | import android.os.ServiceManager; |
| 36 | import android.preference.PreferenceManager; |
| 37 | import android.provider.CallLog.Calls; |
| 38 | import android.provider.ContactsContract.Contacts; |
| 39 | import android.provider.ContactsContract.Intents.UI; |
| 40 | import android.support.v13.app.FragmentPagerAdapter; |
| 41 | import android.support.v4.view.ViewPager; |
| 42 | import android.support.v4.view.ViewPager.OnPageChangeListener; |
| 43 | import android.text.TextUtils; |
| 44 | import android.util.DisplayMetrics; |
| 45 | import android.util.Log; |
| 46 | import android.view.Menu; |
| 47 | import android.view.MenuInflater; |
| 48 | import android.view.MenuItem; |
| 49 | import android.view.MenuItem.OnMenuItemClickListener; |
| 50 | import android.view.View; |
| 51 | import android.view.View.OnClickListener; |
| 52 | import android.view.View.OnFocusChangeListener; |
| 53 | import android.view.ViewConfiguration; |
| 54 | import android.view.ViewGroup; |
| 55 | import android.view.inputmethod.InputMethodManager; |
| 56 | import android.widget.PopupMenu; |
| 57 | import android.widget.SearchView; |
| 58 | import android.widget.SearchView.OnCloseListener; |
| 59 | import android.widget.SearchView.OnQueryTextListener; |
| 60 | import android.widget.Toast; |
| 61 | |
| 62 | import com.android.contacts.common.CallUtil; |
| 63 | import com.android.contacts.common.activity.TransactionSafeActivity; |
| 64 | import com.android.contacts.common.list.ContactListFilterController; |
| 65 | import com.android.contacts.common.list.ContactListFilterController.ContactListFilterListener; |
| 66 | import com.android.contacts.common.list.ContactListItemView; |
| 67 | import com.android.contacts.common.list.OnPhoneNumberPickerActionListener; |
| 68 | import com.android.contacts.common.list.PhoneNumberPickerFragment; |
| 69 | import com.android.contacts.common.util.AccountFilterUtil; |
| 70 | import com.android.dialer.calllog.CallLogFragment; |
| 71 | import com.android.dialer.dialpad.DialpadFragment; |
| 72 | import com.android.dialer.interactions.PhoneNumberInteraction; |
| 73 | import com.android.dialer.list.PhoneFavoriteFragment; |
| 74 | import com.android.dialer.util.OrientationUtil; |
| 75 | import com.android.internal.telephony.ITelephony; |
| 76 | |
| 77 | /** |
| 78 | * The dialer activity that has one tab with the virtual 12key |
| 79 | * dialer, a tab with recent calls in it, a tab with the contacts and |
| 80 | * a tab with the favorite. This is the container and the tabs are |
| 81 | * embedded using intents. |
| 82 | * The dialer tab's title is 'phone', a more common name (see strings.xml). |
| 83 | */ |
| 84 | public class NewDialtactsActivity extends TransactionSafeActivity implements View.OnClickListener { |
| 85 | private static final String TAG = "DialtactsActivity"; |
| 86 | |
| 87 | public static final boolean DEBUG = false; |
| 88 | |
| 89 | /** Used to open Call Setting */ |
| 90 | private static final String PHONE_PACKAGE = "com.android.phone"; |
| 91 | private static final String CALL_SETTINGS_CLASS_NAME = |
| 92 | "com.android.phone.CallFeaturesSetting"; |
| 93 | |
| 94 | /** @see #getCallOrigin() */ |
| 95 | private static final String CALL_ORIGIN_DIALTACTS = |
| 96 | "com.android.dialer.DialtactsActivity"; |
| 97 | |
| 98 | /** |
| 99 | * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}. |
| 100 | */ |
| 101 | private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER"; |
| 102 | |
| 103 | /** Used both by {@link ActionBar} and {@link ViewPagerAdapter} */ |
| 104 | private static final int TAB_INDEX_DIALER = 0; |
| 105 | private static final int TAB_INDEX_CALL_LOG = 1; |
| 106 | private static final int TAB_INDEX_FAVORITES = 2; |
| 107 | |
| 108 | private static final int TAB_INDEX_COUNT = 3; |
| 109 | |
| 110 | private SharedPreferences mPrefs; |
| 111 | |
| 112 | public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences"; |
| 113 | |
| 114 | /** Last manually selected tab index */ |
| 115 | private static final String PREF_LAST_MANUALLY_SELECTED_TAB = |
| 116 | "DialtactsActivity_last_manually_selected_tab"; |
| 117 | private static final int PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT = TAB_INDEX_DIALER; |
| 118 | |
| 119 | private static final int SUBACTIVITY_ACCOUNT_FILTER = 1; |
| 120 | |
| 121 | public class ViewPagerAdapter extends FragmentPagerAdapter { |
| 122 | public ViewPagerAdapter(FragmentManager fm) { |
| 123 | super(fm); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Fragment getItem(int position) { |
| 128 | switch (position) { |
| 129 | case TAB_INDEX_DIALER: |
| 130 | return new DialpadFragment(); |
| 131 | case TAB_INDEX_CALL_LOG: |
| 132 | return new CallLogFragment(); |
| 133 | case TAB_INDEX_FAVORITES: |
| 134 | return new PhoneFavoriteFragment(); |
| 135 | } |
| 136 | throw new IllegalStateException("No fragment at position " + position); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void setPrimaryItem(ViewGroup container, int position, Object object) { |
| 141 | // The parent's setPrimaryItem() also calls setMenuVisibility(), so we want to know |
| 142 | // when it happens. |
| 143 | if (DEBUG) { |
| 144 | Log.d(TAG, "FragmentPagerAdapter#setPrimaryItem(), position: " + position); |
| 145 | } |
| 146 | super.setPrimaryItem(container, position, object); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public int getCount() { |
| 151 | return TAB_INDEX_COUNT; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * True when the app detects user's drag event. This variable should not become true when |
| 157 | * mUserTabClick is true. |
| 158 | * |
| 159 | * During user's drag or tab click, we shouldn't show fake buttons but just show real |
| 160 | * ActionBar at the bottom of the screen, for transition animation. |
| 161 | */ |
| 162 | boolean mDuringSwipe = false; |
| 163 | /** |
| 164 | * True when the app detects user's tab click (at the top of the screen). This variable should |
| 165 | * not become true when mDuringSwipe is true. |
| 166 | * |
| 167 | * During user's drag or tab click, we shouldn't show fake buttons but just show real |
| 168 | * ActionBar at the bottom of the screen, for transition animation. |
| 169 | */ |
| 170 | boolean mUserTabClick = false; |
| 171 | |
| 172 | private class PageChangeListener implements OnPageChangeListener { |
| 173 | private int mCurrentPosition = -1; |
| 174 | /** |
| 175 | * Used during page migration, to remember the next position {@link #onPageSelected(int)} |
| 176 | * specified. |
| 177 | */ |
| 178 | private int mNextPosition = -1; |
| 179 | |
| 180 | @Override |
| 181 | public void onPageScrolled( |
| 182 | int position, float positionOffset, int positionOffsetPixels) { |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void onPageSelected(int position) { |
| 187 | if (DEBUG) Log.d(TAG, "onPageSelected: position: " + position); |
| 188 | final ActionBar actionBar = getActionBar(); |
| 189 | if (mDialpadFragment != null) { |
| 190 | if (mDuringSwipe && position == TAB_INDEX_DIALER) { |
| 191 | // TODO: Figure out if we want this or not. Right now |
| 192 | // - with this call, both fake buttons and real action bar overlap |
| 193 | // - without this call, there's tiny flicker happening to search/menu buttons. |
| 194 | // If we can reduce the flicker without this call, it would be much better. |
| 195 | // updateFakeMenuButtonsVisibility(true); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (mCurrentPosition == position) { |
| 200 | Log.w(TAG, "Previous position and next position became same (" + position + ")"); |
| 201 | } |
| 202 | |
| 203 | actionBar.selectTab(actionBar.getTabAt(position)); |
| 204 | mNextPosition = position; |
| 205 | } |
| 206 | |
| 207 | public void setCurrentPosition(int position) { |
| 208 | mCurrentPosition = position; |
| 209 | } |
| 210 | |
| 211 | public int getCurrentPosition() { |
| 212 | return mCurrentPosition; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void onPageScrollStateChanged(int state) { |
| 217 | switch (state) { |
| 218 | case ViewPager.SCROLL_STATE_IDLE: { |
| 219 | if (mNextPosition == -1) { |
| 220 | // This happens when the user drags the screen just after launching the |
| 221 | // application, and settle down the same screen without actually swiping it. |
| 222 | // At that moment mNextPosition is apparently -1 yet, and we expect it |
| 223 | // being updated by onPageSelected(), which is *not* called if the user |
| 224 | // settle down the exact same tab after the dragging. |
| 225 | if (DEBUG) { |
| 226 | Log.d(TAG, "Next position is not specified correctly. Use current tab (" |
| 227 | + mViewPager.getCurrentItem() + ")"); |
| 228 | } |
| 229 | mNextPosition = mViewPager.getCurrentItem(); |
| 230 | } |
| 231 | if (DEBUG) { |
| 232 | Log.d(TAG, "onPageScrollStateChanged() with SCROLL_STATE_IDLE. " |
| 233 | + "mCurrentPosition: " + mCurrentPosition |
| 234 | + ", mNextPosition: " + mNextPosition); |
| 235 | } |
| 236 | // Interpret IDLE as the end of migration (both swipe and tab click) |
| 237 | mDuringSwipe = false; |
| 238 | mUserTabClick = false; |
| 239 | |
| 240 | updateFakeMenuButtonsVisibility(mNextPosition == TAB_INDEX_DIALER); |
| 241 | sendFragmentVisibilityChange(mCurrentPosition, false); |
| 242 | sendFragmentVisibilityChange(mNextPosition, true); |
| 243 | |
| 244 | invalidateOptionsMenu(); |
| 245 | |
| 246 | mCurrentPosition = mNextPosition; |
| 247 | break; |
| 248 | } |
| 249 | case ViewPager.SCROLL_STATE_DRAGGING: { |
| 250 | if (DEBUG) Log.d(TAG, "onPageScrollStateChanged() with SCROLL_STATE_DRAGGING"); |
| 251 | mDuringSwipe = true; |
| 252 | mUserTabClick = false; |
| 253 | break; |
| 254 | } |
| 255 | case ViewPager.SCROLL_STATE_SETTLING: { |
| 256 | if (DEBUG) Log.d(TAG, "onPageScrollStateChanged() with SCROLL_STATE_SETTLING"); |
| 257 | mDuringSwipe = true; |
| 258 | mUserTabClick = false; |
| 259 | break; |
| 260 | } |
| 261 | default: |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | private String mFilterText; |
| 268 | |
| 269 | /** Enables horizontal swipe between Fragments. */ |
| 270 | private ViewPager mViewPager; |
| 271 | private final PageChangeListener mPageChangeListener = new PageChangeListener(); |
| 272 | private DialpadFragment mDialpadFragment; |
| 273 | private CallLogFragment mCallLogFragment; |
| 274 | private PhoneFavoriteFragment mPhoneFavoriteFragment; |
| 275 | |
| 276 | private View mSearchButton; |
| 277 | private View mMenuButton; |
| 278 | |
| 279 | private final ContactListFilterListener mContactListFilterListener = |
| 280 | new ContactListFilterListener() { |
| 281 | @Override |
| 282 | public void onContactListFilterChanged() { |
| 283 | boolean doInvalidateOptionsMenu = false; |
| 284 | |
| 285 | if (mPhoneFavoriteFragment != null && mPhoneFavoriteFragment.isAdded()) { |
| 286 | mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter()); |
| 287 | doInvalidateOptionsMenu = true; |
| 288 | } |
| 289 | |
| 290 | if (mSearchFragment != null && mSearchFragment.isAdded()) { |
| 291 | mSearchFragment.setFilter(mContactListFilterController.getFilter()); |
| 292 | doInvalidateOptionsMenu = true; |
| 293 | } else { |
| 294 | Log.w(TAG, "Search Fragment isn't available when ContactListFilter is changed"); |
| 295 | } |
| 296 | |
| 297 | if (doInvalidateOptionsMenu) { |
| 298 | invalidateOptionsMenu(); |
| 299 | } |
| 300 | } |
| 301 | }; |
| 302 | |
| 303 | private final TabListener mTabListener = new TabListener() { |
| 304 | @Override |
| 305 | public void onTabUnselected(Tab tab, FragmentTransaction ft) { |
| 306 | if (DEBUG) Log.d(TAG, "onTabUnselected(). tab: " + tab); |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public void onTabSelected(Tab tab, FragmentTransaction ft) { |
| 311 | if (DEBUG) { |
| 312 | Log.d(TAG, "onTabSelected(). tab: " + tab + ", mDuringSwipe: " + mDuringSwipe); |
| 313 | } |
| 314 | // When the user swipes the screen horizontally, this method will be called after |
| 315 | // ViewPager.SCROLL_STATE_DRAGGING and ViewPager.SCROLL_STATE_SETTLING events, while |
| 316 | // when the user clicks a tab at the ActionBar at the top, this will be called before |
| 317 | // them. This logic interprets the order difference as a difference of the user action. |
| 318 | if (!mDuringSwipe) { |
| 319 | if (DEBUG) { |
| 320 | Log.d(TAG, "Tab select. from: " + mPageChangeListener.getCurrentPosition() |
| 321 | + ", to: " + tab.getPosition()); |
| 322 | } |
| 323 | if (mDialpadFragment != null) { |
| 324 | updateFakeMenuButtonsVisibility(tab.getPosition() == TAB_INDEX_DIALER); |
| 325 | } |
| 326 | mUserTabClick = true; |
| 327 | } |
| 328 | |
| 329 | if (mViewPager.getCurrentItem() != tab.getPosition()) { |
| 330 | mViewPager.setCurrentItem(tab.getPosition(), true); |
| 331 | } |
| 332 | |
| 333 | // During the call, we don't remember the tab position. |
| 334 | if (mDialpadFragment == null || !mDialpadFragment.phoneIsInUse()) { |
| 335 | // Remember this tab index. This function is also called, if the tab is set |
| 336 | // automatically in which case the setter (setCurrentTab) has to set this to its old |
| 337 | // value afterwards |
| 338 | mLastManuallySelectedFragment = tab.getPosition(); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public void onTabReselected(Tab tab, FragmentTransaction ft) { |
| 344 | if (DEBUG) Log.d(TAG, "onTabReselected"); |
| 345 | } |
| 346 | }; |
| 347 | |
| 348 | /** |
| 349 | * Fragment for searching phone numbers. Unlike the other Fragments, this doesn't correspond |
| 350 | * to tab but is shown by a search action. |
| 351 | */ |
| 352 | private PhoneNumberPickerFragment mSearchFragment; |
| 353 | /** |
| 354 | * True when this Activity is in its search UI (with a {@link SearchView} and |
| 355 | * {@link PhoneNumberPickerFragment}). |
| 356 | */ |
| 357 | private boolean mInSearchUi; |
| 358 | private SearchView mSearchView; |
| 359 | |
| 360 | private final OnClickListener mFilterOptionClickListener = new OnClickListener() { |
| 361 | @Override |
| 362 | public void onClick(View view) { |
| 363 | final PopupMenu popupMenu = new PopupMenu(NewDialtactsActivity.this, view); |
| 364 | final Menu menu = popupMenu.getMenu(); |
| 365 | popupMenu.inflate(R.menu.dialtacts_search_options); |
| 366 | final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option); |
| 367 | filterOptionMenuItem.setOnMenuItemClickListener(mFilterOptionsMenuItemClickListener); |
| 368 | final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact); |
| 369 | addContactOptionMenuItem.setIntent( |
| 370 | new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI)); |
| 371 | popupMenu.show(); |
| 372 | } |
| 373 | }; |
| 374 | |
| 375 | /** |
| 376 | * The index of the Fragment (or, the tab) that has last been manually selected. |
| 377 | * This value does not keep track of programmatically set Tabs (e.g. Call Log after a Call) |
| 378 | */ |
| 379 | private int mLastManuallySelectedFragment; |
| 380 | |
| 381 | private ContactListFilterController mContactListFilterController; |
| 382 | private OnMenuItemClickListener mFilterOptionsMenuItemClickListener = |
| 383 | new OnMenuItemClickListener() { |
| 384 | @Override |
| 385 | public boolean onMenuItemClick(MenuItem item) { |
| 386 | AccountFilterUtil.startAccountFilterActivityForResult( |
| 387 | NewDialtactsActivity.this, SUBACTIVITY_ACCOUNT_FILTER, |
| 388 | mContactListFilterController.getFilter()); |
| 389 | return true; |
| 390 | } |
| 391 | }; |
| 392 | |
| 393 | private OnMenuItemClickListener mSearchMenuItemClickListener = |
| 394 | new OnMenuItemClickListener() { |
| 395 | @Override |
| 396 | public boolean onMenuItemClick(MenuItem item) { |
| 397 | enterSearchUi(); |
| 398 | return true; |
| 399 | } |
| 400 | }; |
| 401 | |
| 402 | /** |
| 403 | * Listener used when one of phone numbers in search UI is selected. This will initiate a |
| 404 | * phone call using the phone number. |
| 405 | */ |
| 406 | private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener = |
| 407 | new OnPhoneNumberPickerActionListener() { |
| 408 | @Override |
| 409 | public void onPickPhoneNumberAction(Uri dataUri) { |
| 410 | // Specify call-origin so that users will see the previous tab instead of |
| 411 | // CallLog screen (search UI will be automatically exited). |
| 412 | PhoneNumberInteraction.startInteractionForPhoneCall( |
| 413 | NewDialtactsActivity.this, dataUri, getCallOrigin()); |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public void onShortcutIntentCreated(Intent intent) { |
| 418 | Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring."); |
| 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public void onHomeInActionBarSelected() { |
| 423 | exitSearchUi(); |
| 424 | } |
| 425 | }; |
| 426 | |
| 427 | /** |
| 428 | * Listener used to send search queries to the phone search fragment. |
| 429 | */ |
| 430 | private final OnQueryTextListener mPhoneSearchQueryTextListener = |
| 431 | new OnQueryTextListener() { |
| 432 | @Override |
| 433 | public boolean onQueryTextSubmit(String query) { |
| 434 | View view = getCurrentFocus(); |
| 435 | if (view != null) { |
| 436 | hideInputMethod(view); |
| 437 | view.clearFocus(); |
| 438 | } |
| 439 | return true; |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public boolean onQueryTextChange(String newText) { |
| 444 | // Show search result with non-empty text. Show a bare list otherwise. |
| 445 | if (mSearchFragment != null) { |
| 446 | mSearchFragment.setQueryString(newText, true); |
| 447 | } |
| 448 | return true; |
| 449 | } |
| 450 | }; |
| 451 | |
| 452 | /** |
| 453 | * Listener used to handle the "close" button on the right side of {@link SearchView}. |
| 454 | * If some text is in the search view, this will clean it up. Otherwise this will exit |
| 455 | * the search UI and let users go back to usual Phone UI. |
| 456 | * |
| 457 | * This does _not_ handle back button. |
| 458 | */ |
| 459 | private final OnCloseListener mPhoneSearchCloseListener = |
| 460 | new OnCloseListener() { |
| 461 | @Override |
| 462 | public boolean onClose() { |
| 463 | if (!TextUtils.isEmpty(mSearchView.getQuery())) { |
| 464 | mSearchView.setQuery(null, true); |
| 465 | } |
| 466 | return true; |
| 467 | } |
| 468 | }; |
| 469 | |
| 470 | private final View.OnLayoutChangeListener mFirstLayoutListener |
| 471 | = new View.OnLayoutChangeListener() { |
| 472 | @Override |
| 473 | public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, |
| 474 | int oldTop, int oldRight, int oldBottom) { |
| 475 | v.removeOnLayoutChangeListener(this); // Unregister self. |
| 476 | addSearchFragment(); |
| 477 | } |
| 478 | }; |
| 479 | |
| 480 | @Override |
| 481 | protected void onCreate(Bundle icicle) { |
| 482 | super.onCreate(icicle); |
| 483 | |
| 484 | final Intent intent = getIntent(); |
| 485 | fixIntent(intent); |
| 486 | |
| 487 | setContentView(R.layout.dialtacts_activity); |
| 488 | |
| 489 | mContactListFilterController = ContactListFilterController.getInstance(this); |
| 490 | mContactListFilterController.addListener(mContactListFilterListener); |
| 491 | |
| 492 | findViewById(R.id.dialtacts_frame).addOnLayoutChangeListener(mFirstLayoutListener); |
| 493 | |
| 494 | mViewPager = (ViewPager) findViewById(R.id.pager); |
| 495 | mViewPager.setAdapter(new ViewPagerAdapter(getFragmentManager())); |
| 496 | mViewPager.setOnPageChangeListener(mPageChangeListener); |
| 497 | mViewPager.setOffscreenPageLimit(2); |
| 498 | |
| 499 | // Do same width calculation as ActionBar does |
| 500 | DisplayMetrics dm = getResources().getDisplayMetrics(); |
| 501 | int minCellSize = getResources().getDimensionPixelSize(R.dimen.fake_menu_button_min_width); |
| 502 | int cellCount = dm.widthPixels / minCellSize; |
| 503 | int fakeMenuItemWidth = dm.widthPixels / cellCount; |
| 504 | if (DEBUG) Log.d(TAG, "The size of fake menu buttons (in pixel): " + fakeMenuItemWidth); |
| 505 | |
| 506 | // Soft menu button should appear only when there's no hardware menu button. |
| 507 | mMenuButton = findViewById(R.id.overflow_menu); |
| 508 | if (mMenuButton != null) { |
| 509 | mMenuButton.setMinimumWidth(fakeMenuItemWidth); |
| 510 | if (ViewConfiguration.get(this).hasPermanentMenuKey()) { |
| 511 | // This is required for dialpad button's layout, so must not use GONE here. |
| 512 | mMenuButton.setVisibility(View.INVISIBLE); |
| 513 | } else { |
| 514 | mMenuButton.setOnClickListener(this); |
| 515 | } |
| 516 | } |
| 517 | mSearchButton = findViewById(R.id.searchButton); |
| 518 | if (mSearchButton != null) { |
| 519 | mSearchButton.setMinimumWidth(fakeMenuItemWidth); |
| 520 | mSearchButton.setOnClickListener(this); |
| 521 | } |
| 522 | |
| 523 | // Setup the ActionBar tabs (the order matches the tab-index contants TAB_INDEX_*) |
| 524 | setupDialer(); |
| 525 | setupCallLog(); |
| 526 | setupFavorites(); |
| 527 | getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
| 528 | getActionBar().setDisplayShowTitleEnabled(false); |
| 529 | getActionBar().setDisplayShowHomeEnabled(false); |
| 530 | |
| 531 | // Load the last manually loaded tab |
| 532 | mPrefs = this.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE); |
| 533 | mLastManuallySelectedFragment = mPrefs.getInt(PREF_LAST_MANUALLY_SELECTED_TAB, |
| 534 | PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT); |
| 535 | if (mLastManuallySelectedFragment >= TAB_INDEX_COUNT) { |
| 536 | // Stored value may have exceeded the number of current tabs. Reset it. |
| 537 | mLastManuallySelectedFragment = PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT; |
| 538 | } |
| 539 | |
| 540 | setCurrentTab(intent); |
| 541 | |
| 542 | if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction()) |
| 543 | && icicle == null) { |
| 544 | setupFilterText(intent); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | @Override |
| 549 | public void onStart() { |
| 550 | super.onStart(); |
| 551 | if (mPhoneFavoriteFragment != null) { |
| 552 | mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter()); |
| 553 | } |
| 554 | if (mSearchFragment != null) { |
| 555 | mSearchFragment.setFilter(mContactListFilterController.getFilter()); |
| 556 | } |
| 557 | |
| 558 | if (mDuringSwipe || mUserTabClick) { |
| 559 | if (DEBUG) Log.d(TAG, "reset buggy flag state.."); |
| 560 | mDuringSwipe = false; |
| 561 | mUserTabClick = false; |
| 562 | } |
| 563 | |
| 564 | final int currentPosition = mPageChangeListener.getCurrentPosition(); |
| 565 | if (DEBUG) { |
| 566 | Log.d(TAG, "onStart(). current position: " + mPageChangeListener.getCurrentPosition() |
| 567 | + ". Reset all menu visibility state."); |
| 568 | } |
| 569 | updateFakeMenuButtonsVisibility(currentPosition == TAB_INDEX_DIALER && !mInSearchUi); |
| 570 | for (int i = 0; i < TAB_INDEX_COUNT; i++) { |
| 571 | sendFragmentVisibilityChange(i, i == currentPosition); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | @Override |
| 576 | public void onDestroy() { |
| 577 | super.onDestroy(); |
| 578 | mContactListFilterController.removeListener(mContactListFilterListener); |
| 579 | } |
| 580 | |
| 581 | @Override |
| 582 | public void onClick(View view) { |
| 583 | switch (view.getId()) { |
| 584 | case R.id.searchButton: { |
| 585 | enterSearchUi(); |
| 586 | break; |
| 587 | } |
| 588 | case R.id.overflow_menu: { |
| 589 | if (mDialpadFragment != null) { |
| 590 | PopupMenu popup = mDialpadFragment.constructPopupMenu(view); |
| 591 | if (popup != null) { |
| 592 | popup.show(); |
| 593 | } |
| 594 | } else { |
| 595 | Log.w(TAG, "DialpadFragment is null during onClick() event for " + view); |
| 596 | } |
| 597 | break; |
| 598 | } |
| 599 | default: { |
| 600 | Log.wtf(TAG, "Unexpected onClick event from " + view); |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Add search fragment. Note this is called during onLayout, so there's some restrictions, |
| 608 | * such as executePendingTransaction can't be used in it. |
| 609 | */ |
| 610 | private void addSearchFragment() { |
| 611 | // In order to take full advantage of "fragment deferred start", we need to create the |
| 612 | // search fragment after all other fragments are created. |
| 613 | // The other fragments are created by the ViewPager on the first onMeasure(). |
| 614 | // We use the first onLayout call, which is after onMeasure(). |
| 615 | |
| 616 | // Just return if the fragment is already created, which happens after configuration |
| 617 | // changes. |
| 618 | if (mSearchFragment != null) return; |
| 619 | |
| 620 | final FragmentTransaction ft = getFragmentManager().beginTransaction(); |
| 621 | final Fragment searchFragment = new PhoneNumberPickerFragment(); |
| 622 | |
| 623 | searchFragment.setUserVisibleHint(false); |
| 624 | ft.add(R.id.dialtacts_frame, searchFragment); |
| 625 | ft.hide(searchFragment); |
| 626 | ft.commitAllowingStateLoss(); |
| 627 | } |
| 628 | |
| 629 | private void prepareSearchView() { |
| 630 | final View searchViewLayout = |
| 631 | getLayoutInflater().inflate(R.layout.dialtacts_custom_action_bar, null); |
| 632 | mSearchView = (SearchView) searchViewLayout.findViewById(R.id.search_view); |
| 633 | mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener); |
| 634 | mSearchView.setOnCloseListener(mPhoneSearchCloseListener); |
| 635 | // Since we're using a custom layout for showing SearchView instead of letting the |
| 636 | // search menu icon do that job, we need to manually configure the View so it looks |
| 637 | // "shown via search menu". |
| 638 | // - it should be iconified by default |
| 639 | // - it should not be iconified at this time |
| 640 | // See also comments for onActionViewExpanded()/onActionViewCollapsed() |
| 641 | mSearchView.setIconifiedByDefault(true); |
| 642 | mSearchView.setQueryHint(getString(R.string.hint_findContacts)); |
| 643 | mSearchView.setIconified(false); |
| 644 | mSearchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() { |
| 645 | @Override |
| 646 | public void onFocusChange(View view, boolean hasFocus) { |
| 647 | if (hasFocus) { |
| 648 | showInputMethod(view.findFocus()); |
| 649 | } |
| 650 | } |
| 651 | }); |
| 652 | |
| 653 | if (!ViewConfiguration.get(this).hasPermanentMenuKey()) { |
| 654 | // Filter option menu should be shown on the right side of SearchView. |
| 655 | final View filterOptionView = searchViewLayout.findViewById(R.id.search_option); |
| 656 | filterOptionView.setVisibility(View.VISIBLE); |
| 657 | filterOptionView.setOnClickListener(mFilterOptionClickListener); |
| 658 | } |
| 659 | |
| 660 | getActionBar().setCustomView(searchViewLayout, |
| 661 | new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); |
| 662 | } |
| 663 | |
| 664 | @Override |
| 665 | public void onAttachFragment(Fragment fragment) { |
| 666 | // This method can be called before onCreate(), at which point we cannot rely on ViewPager. |
| 667 | // In that case, we will setup the "current position" soon after the ViewPager is ready. |
| 668 | final int currentPosition = mViewPager != null ? mViewPager.getCurrentItem() : -1; |
| 669 | |
| 670 | if (fragment instanceof DialpadFragment) { |
| 671 | mDialpadFragment = (DialpadFragment) fragment; |
| 672 | } else if (fragment instanceof CallLogFragment) { |
| 673 | mCallLogFragment = (CallLogFragment) fragment; |
| 674 | } else if (fragment instanceof PhoneFavoriteFragment) { |
| 675 | mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment; |
| 676 | mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener); |
| 677 | if (mContactListFilterController != null |
| 678 | && mContactListFilterController.getFilter() != null) { |
| 679 | mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter()); |
| 680 | } |
| 681 | } else if (fragment instanceof PhoneNumberPickerFragment) { |
| 682 | mSearchFragment = (PhoneNumberPickerFragment) fragment; |
| 683 | mSearchFragment.setOnPhoneNumberPickerActionListener(mPhoneNumberPickerActionListener); |
| 684 | mSearchFragment.setQuickContactEnabled(true); |
| 685 | mSearchFragment.setDarkTheme(true); |
| 686 | mSearchFragment.setPhotoPosition(ContactListItemView.getDefaultPhotoPosition( |
| 687 | true /* opposite */)); |
| 688 | mSearchFragment.setUseCallableUri(true); |
| 689 | if (mContactListFilterController != null |
| 690 | && mContactListFilterController.getFilter() != null) { |
| 691 | mSearchFragment.setFilter(mContactListFilterController.getFilter()); |
| 692 | } |
| 693 | // Here we assume that we're not on the search mode, so let's hide the fragment. |
| 694 | // |
| 695 | // We get here either when the fragment is created (normal case), or after configuration |
| 696 | // changes. In the former case, we're not in search mode because we can only |
| 697 | // enter search mode if the fragment is created. (see enterSearchUi()) |
| 698 | // In the latter case we're not in search mode either because we don't retain |
| 699 | // mInSearchUi -- ideally we should but at this point it's not supported. |
| 700 | mSearchFragment.setUserVisibleHint(false); |
| 701 | // After configuration changes fragments will forget their "hidden" state, so make |
| 702 | // sure to hide it. |
| 703 | if (!mSearchFragment.isHidden()) { |
| 704 | final FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
| 705 | transaction.hide(mSearchFragment); |
| 706 | transaction.commitAllowingStateLoss(); |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | @Override |
| 712 | protected void onPause() { |
| 713 | super.onPause(); |
| 714 | |
| 715 | mPrefs.edit().putInt(PREF_LAST_MANUALLY_SELECTED_TAB, mLastManuallySelectedFragment) |
| 716 | .apply(); |
| 717 | requestBackup(); |
| 718 | } |
| 719 | |
| 720 | private void requestBackup() { |
| 721 | final BackupManager bm = new BackupManager(this); |
| 722 | bm.dataChanged(); |
| 723 | } |
| 724 | |
| 725 | private void fixIntent(Intent intent) { |
| 726 | // This should be cleaned up: the call key used to send an Intent |
| 727 | // that just said to go to the recent calls list. It now sends this |
| 728 | // abstract action, but this class hasn't been rewritten to deal with it. |
| 729 | if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) { |
| 730 | intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE); |
| 731 | intent.putExtra("call_key", true); |
| 732 | setIntent(intent); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | private void setupDialer() { |
| 737 | final Tab tab = getActionBar().newTab(); |
| 738 | tab.setContentDescription(R.string.dialerIconLabel); |
| 739 | tab.setTabListener(mTabListener); |
| 740 | tab.setIcon(R.drawable.ic_tab_dialer); |
| 741 | getActionBar().addTab(tab); |
| 742 | } |
| 743 | |
| 744 | private void setupCallLog() { |
| 745 | final Tab tab = getActionBar().newTab(); |
| 746 | tab.setContentDescription(R.string.recentCallsIconLabel); |
| 747 | tab.setIcon(R.drawable.ic_tab_recent); |
| 748 | tab.setTabListener(mTabListener); |
| 749 | getActionBar().addTab(tab); |
| 750 | } |
| 751 | |
| 752 | private void setupFavorites() { |
| 753 | final Tab tab = getActionBar().newTab(); |
| 754 | tab.setContentDescription(R.string.contactsFavoritesLabel); |
| 755 | tab.setIcon(R.drawable.ic_tab_all); |
| 756 | tab.setTabListener(mTabListener); |
| 757 | getActionBar().addTab(tab); |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Returns true if the intent is due to hitting the green send key (hardware call button: |
| 762 | * KEYCODE_CALL) while in a call. |
| 763 | * |
| 764 | * @param intent the intent that launched this activity |
| 765 | * @param recentCallsRequest true if the intent is requesting to view recent calls |
| 766 | * @return true if the intent is due to hitting the green send key while in a call |
| 767 | */ |
| 768 | private boolean isSendKeyWhileInCall(final Intent intent, |
| 769 | final boolean recentCallsRequest) { |
| 770 | // If there is a call in progress go to the call screen |
| 771 | if (recentCallsRequest) { |
| 772 | final boolean callKey = intent.getBooleanExtra("call_key", false); |
| 773 | |
| 774 | try { |
| 775 | ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); |
| 776 | if (callKey && phone != null && phone.showCallScreen()) { |
| 777 | return true; |
| 778 | } |
| 779 | } catch (RemoteException e) { |
| 780 | Log.e(TAG, "Failed to handle send while in call", e); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return false; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * Sets the current tab based on the intent's request type |
| 789 | * |
| 790 | * @param intent Intent that contains information about which tab should be selected |
| 791 | */ |
| 792 | private void setCurrentTab(Intent intent) { |
| 793 | // If we got here by hitting send and we're in call forward along to the in-call activity |
| 794 | boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType( |
| 795 | getContentResolver())); |
| 796 | if (isSendKeyWhileInCall(intent, recentCallsRequest)) { |
| 797 | finish(); |
| 798 | return; |
| 799 | } |
| 800 | |
| 801 | // Remember the old manually selected tab index so that it can be restored if it is |
| 802 | // overwritten by one of the programmatic tab selections |
| 803 | final int savedTabIndex = mLastManuallySelectedFragment; |
| 804 | |
| 805 | final int tabIndex; |
| 806 | if ((mDialpadFragment != null && mDialpadFragment.phoneIsInUse()) |
| 807 | || isDialIntent(intent)) { |
| 808 | tabIndex = TAB_INDEX_DIALER; |
| 809 | } else if (recentCallsRequest) { |
| 810 | tabIndex = TAB_INDEX_CALL_LOG; |
| 811 | } else { |
| 812 | tabIndex = mLastManuallySelectedFragment; |
| 813 | } |
| 814 | |
| 815 | final int previousItemIndex = mViewPager.getCurrentItem(); |
| 816 | mViewPager.setCurrentItem(tabIndex, false /* smoothScroll */); |
| 817 | if (previousItemIndex != tabIndex) { |
| 818 | sendFragmentVisibilityChange(previousItemIndex, false /* not visible */ ); |
| 819 | } |
| 820 | mPageChangeListener.setCurrentPosition(tabIndex); |
| 821 | sendFragmentVisibilityChange(tabIndex, true /* visible */ ); |
| 822 | |
| 823 | // Restore to the previous manual selection |
| 824 | mLastManuallySelectedFragment = savedTabIndex; |
| 825 | mDuringSwipe = false; |
| 826 | mUserTabClick = false; |
| 827 | } |
| 828 | |
| 829 | @Override |
| 830 | public void onNewIntent(Intent newIntent) { |
| 831 | setIntent(newIntent); |
| 832 | fixIntent(newIntent); |
| 833 | setCurrentTab(newIntent); |
| 834 | final String action = newIntent.getAction(); |
| 835 | if (UI.FILTER_CONTACTS_ACTION.equals(action)) { |
| 836 | setupFilterText(newIntent); |
| 837 | } |
| 838 | if (mInSearchUi || (mSearchFragment != null && mSearchFragment.isVisible())) { |
| 839 | exitSearchUi(); |
| 840 | } |
| 841 | |
| 842 | if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { |
| 843 | if (mDialpadFragment != null) { |
| 844 | mDialpadFragment.setStartedFromNewIntent(true); |
| 845 | } else { |
| 846 | Log.e(TAG, "DialpadFragment isn't ready yet when the tab is already selected."); |
| 847 | } |
| 848 | } else if (mViewPager.getCurrentItem() == TAB_INDEX_CALL_LOG) { |
| 849 | if (mCallLogFragment != null) { |
| 850 | mCallLogFragment.configureScreenFromIntent(newIntent); |
| 851 | } else { |
| 852 | Log.e(TAG, "CallLogFragment isn't ready yet when the tab is already selected."); |
| 853 | } |
| 854 | } |
| 855 | invalidateOptionsMenu(); |
| 856 | } |
| 857 | |
| 858 | /** Returns true if the given intent contains a phone number to populate the dialer with */ |
| 859 | private boolean isDialIntent(Intent intent) { |
| 860 | final String action = intent.getAction(); |
| 861 | if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) { |
| 862 | return true; |
| 863 | } |
| 864 | if (Intent.ACTION_VIEW.equals(action)) { |
| 865 | final Uri data = intent.getData(); |
| 866 | if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) { |
| 867 | return true; |
| 868 | } |
| 869 | } |
| 870 | return false; |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * Returns an appropriate call origin for this Activity. May return null when no call origin |
| 875 | * should be used (e.g. when some 3rd party application launched the screen. Call origin is |
| 876 | * for remembering the tab in which the user made a phone call, so the external app's DIAL |
| 877 | * request should not be counted.) |
| 878 | */ |
| 879 | public String getCallOrigin() { |
| 880 | return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null; |
| 881 | } |
| 882 | |
| 883 | /** |
| 884 | * Retrieves the filter text stored in {@link #setupFilterText(Intent)}. |
| 885 | * This text originally came from a FILTER_CONTACTS_ACTION intent received |
| 886 | * by this activity. The stored text will then be cleared after after this |
| 887 | * method returns. |
| 888 | * |
| 889 | * @return The stored filter text |
| 890 | */ |
| 891 | public String getAndClearFilterText() { |
| 892 | String filterText = mFilterText; |
| 893 | mFilterText = null; |
| 894 | return filterText; |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent. |
| 899 | * This is so child activities can check if they are supposed to display a filter. |
| 900 | * |
| 901 | * @param intent The intent received in {@link #onNewIntent(Intent)} |
| 902 | */ |
| 903 | private void setupFilterText(Intent intent) { |
| 904 | // If the intent was relaunched from history, don't apply the filter text. |
| 905 | if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { |
| 906 | return; |
| 907 | } |
| 908 | String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY); |
| 909 | if (filter != null && filter.length() > 0) { |
| 910 | mFilterText = filter; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | @Override |
| 915 | public void onBackPressed() { |
| 916 | if (mInSearchUi) { |
| 917 | // We should let the user go back to usual screens with tabs. |
| 918 | exitSearchUi(); |
| 919 | } else if (isTaskRoot()) { |
| 920 | // Instead of stopping, simply push this to the back of the stack. |
| 921 | // This is only done when running at the top of the stack; |
| 922 | // otherwise, we have been launched by someone else so need to |
| 923 | // allow the user to go back to the caller. |
| 924 | moveTaskToBack(false); |
| 925 | } else { |
| 926 | super.onBackPressed(); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener = |
| 931 | new PhoneFavoriteFragment.Listener() { |
| 932 | @Override |
| 933 | public void onContactSelected(Uri contactUri) { |
| 934 | PhoneNumberInteraction.startInteractionForPhoneCall( |
| 935 | NewDialtactsActivity.this, contactUri, getCallOrigin()); |
| 936 | } |
| 937 | |
| 938 | @Override |
| 939 | public void onCallNumberDirectly(String phoneNumber) { |
| 940 | Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin()); |
| 941 | startActivity(intent); |
| 942 | } |
| 943 | }; |
| 944 | |
| 945 | @Override |
| 946 | public boolean onCreateOptionsMenu(Menu menu) { |
| 947 | MenuInflater inflater = getMenuInflater(); |
| 948 | inflater.inflate(R.menu.dialtacts_options, menu); |
| 949 | |
| 950 | // set up intents and onClick listeners |
| 951 | final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings); |
| 952 | final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar); |
| 953 | final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option); |
| 954 | |
| 955 | callSettingsMenuItem.setIntent(DialtactsActivity.getCallSettingsIntent()); |
| 956 | searchMenuItem.setOnMenuItemClickListener(mSearchMenuItemClickListener); |
| 957 | filterOptionMenuItem.setOnMenuItemClickListener(mFilterOptionsMenuItemClickListener); |
| 958 | |
| 959 | return true; |
| 960 | } |
| 961 | |
| 962 | @Override |
| 963 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 964 | if (mInSearchUi) { |
| 965 | prepareOptionsMenuInSearchMode(menu); |
| 966 | } else { |
| 967 | // get reference to the currently selected tab |
| 968 | final Tab tab = getActionBar().getSelectedTab(); |
| 969 | if (tab != null) { |
| 970 | switch(tab.getPosition()) { |
| 971 | case TAB_INDEX_DIALER: |
| 972 | prepareOptionsMenuForDialerTab(menu); |
| 973 | break; |
| 974 | case TAB_INDEX_CALL_LOG: |
| 975 | prepareOptionsMenuForCallLogTab(menu); |
| 976 | break; |
| 977 | case TAB_INDEX_FAVORITES: |
| 978 | prepareOptionsMenuForFavoritesTab(menu); |
| 979 | break; |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | return true; |
| 984 | } |
| 985 | |
| 986 | @Override |
| 987 | public boolean onOptionsItemSelected(MenuItem item) { |
| 988 | switch (item.getItemId()) { |
| 989 | case R.id.add_contact: |
| 990 | try { |
| 991 | startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI)); |
| 992 | } catch (ActivityNotFoundException e) { |
| 993 | Toast toast = Toast.makeText(this, R.string.add_contact_not_available, |
| 994 | Toast.LENGTH_SHORT); |
| 995 | toast.show(); |
| 996 | } |
| 997 | return true; |
| 998 | } |
| 999 | return super.onOptionsItemSelected(item); |
| 1000 | } |
| 1001 | |
| 1002 | private void prepareOptionsMenuInSearchMode(Menu menu) { |
| 1003 | // get references to menu items |
| 1004 | final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar); |
| 1005 | final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option); |
| 1006 | final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact); |
| 1007 | final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings); |
| 1008 | final MenuItem emptyRightMenuItem = menu.findItem(R.id.empty_right_menu_item); |
| 1009 | |
| 1010 | // prepare the menu items |
| 1011 | searchMenuItem.setVisible(false); |
| 1012 | filterOptionMenuItem.setVisible(ViewConfiguration.get(this).hasPermanentMenuKey()); |
| 1013 | addContactOptionMenuItem.setVisible(false); |
| 1014 | callSettingsMenuItem.setVisible(false); |
| 1015 | emptyRightMenuItem.setVisible(false); |
| 1016 | } |
| 1017 | |
| 1018 | private void prepareOptionsMenuForDialerTab(Menu menu) { |
| 1019 | if (DEBUG) { |
| 1020 | Log.d(TAG, "onPrepareOptionsMenu(dialer). swipe: " + mDuringSwipe |
| 1021 | + ", user tab click: " + mUserTabClick); |
| 1022 | } |
| 1023 | |
| 1024 | // get references to menu items |
| 1025 | final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar); |
| 1026 | final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option); |
| 1027 | final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact); |
| 1028 | final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings); |
| 1029 | final MenuItem emptyRightMenuItem = menu.findItem(R.id.empty_right_menu_item); |
| 1030 | |
| 1031 | // prepare the menu items |
| 1032 | filterOptionMenuItem.setVisible(false); |
| 1033 | addContactOptionMenuItem.setVisible(false); |
| 1034 | if (mDuringSwipe || mUserTabClick) { |
| 1035 | // During horizontal movement, the real ActionBar menu items are shown |
| 1036 | searchMenuItem.setVisible(true); |
| 1037 | callSettingsMenuItem.setVisible(true); |
| 1038 | // When there is a permanent menu key, there is no overflow icon on the right of |
| 1039 | // the action bar which would force the search menu item (if it is visible) to the |
| 1040 | // left. This is the purpose of showing the emptyRightMenuItem. |
| 1041 | emptyRightMenuItem.setVisible(ViewConfiguration.get(this).hasPermanentMenuKey()); |
| 1042 | } else { |
| 1043 | // This is when the user is looking at the dialer pad. In this case, the real |
| 1044 | // ActionBar is hidden and fake menu items are shown. |
| 1045 | // Except in landscape, in which case the real search menu item is shown. |
| 1046 | searchMenuItem.setVisible(OrientationUtil.isLandscape(this)); |
| 1047 | // If a permanent menu key is available, then we need to show the call settings item |
| 1048 | // so that the call settings item can be invoked by the permanent menu key. |
| 1049 | callSettingsMenuItem.setVisible(ViewConfiguration.get(this).hasPermanentMenuKey()); |
| 1050 | emptyRightMenuItem.setVisible(false); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | private void prepareOptionsMenuForCallLogTab(Menu menu) { |
| 1055 | // get references to menu items |
| 1056 | final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar); |
| 1057 | final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option); |
| 1058 | final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact); |
| 1059 | final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings); |
| 1060 | final MenuItem emptyRightMenuItem = menu.findItem(R.id.empty_right_menu_item); |
| 1061 | |
| 1062 | // prepare the menu items |
| 1063 | searchMenuItem.setVisible(true); |
| 1064 | filterOptionMenuItem.setVisible(false); |
| 1065 | addContactOptionMenuItem.setVisible(false); |
| 1066 | callSettingsMenuItem.setVisible(true); |
| 1067 | emptyRightMenuItem.setVisible(ViewConfiguration.get(this).hasPermanentMenuKey()); |
| 1068 | } |
| 1069 | |
| 1070 | private void prepareOptionsMenuForFavoritesTab(Menu menu) { |
| 1071 | // get references to menu items |
| 1072 | final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar); |
| 1073 | final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option); |
| 1074 | final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact); |
| 1075 | final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings); |
| 1076 | final MenuItem emptyRightMenuItem = menu.findItem(R.id.empty_right_menu_item); |
| 1077 | |
| 1078 | // prepare the menu items |
| 1079 | searchMenuItem.setVisible(true); |
| 1080 | filterOptionMenuItem.setVisible(true); |
| 1081 | addContactOptionMenuItem.setVisible(true); |
| 1082 | callSettingsMenuItem.setVisible(true); |
| 1083 | emptyRightMenuItem.setVisible(false); |
| 1084 | } |
| 1085 | |
| 1086 | @Override |
| 1087 | public void startSearch(String initialQuery, boolean selectInitialQuery, |
| 1088 | Bundle appSearchData, boolean globalSearch) { |
| 1089 | if (mSearchFragment != null && mSearchFragment.isAdded() && !globalSearch) { |
| 1090 | if (mInSearchUi) { |
| 1091 | if (mSearchView.hasFocus()) { |
| 1092 | showInputMethod(mSearchView.findFocus()); |
| 1093 | } else { |
| 1094 | mSearchView.requestFocus(); |
| 1095 | } |
| 1096 | } else { |
| 1097 | enterSearchUi(); |
| 1098 | } |
| 1099 | } else { |
| 1100 | super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | /** |
| 1105 | * Hides every tab and shows search UI for phone lookup. |
| 1106 | */ |
| 1107 | private void enterSearchUi() { |
| 1108 | if (mSearchFragment == null) { |
| 1109 | // We add the search fragment dynamically in the first onLayoutChange() and |
| 1110 | // mSearchFragment is set sometime later when the fragment transaction is actually |
| 1111 | // executed, which means there's a window when users are able to hit the (physical) |
| 1112 | // search key but mSearchFragment is still null. |
| 1113 | // It's quite hard to handle this case right, so let's just ignore the search key |
| 1114 | // in this case. Users can just hit it again and it will work this time. |
| 1115 | return; |
| 1116 | } |
| 1117 | if (mSearchView == null) { |
| 1118 | prepareSearchView(); |
| 1119 | } |
| 1120 | |
| 1121 | final ActionBar actionBar = getActionBar(); |
| 1122 | |
| 1123 | final Tab tab = actionBar.getSelectedTab(); |
| 1124 | |
| 1125 | // User can search during the call, but we don't want to remember the status. |
| 1126 | if (tab != null && (mDialpadFragment == null || |
| 1127 | !mDialpadFragment.phoneIsInUse())) { |
| 1128 | mLastManuallySelectedFragment = tab.getPosition(); |
| 1129 | } |
| 1130 | |
| 1131 | mSearchView.setQuery(null, true); |
| 1132 | |
| 1133 | actionBar.setDisplayShowCustomEnabled(true); |
| 1134 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); |
| 1135 | actionBar.setDisplayShowHomeEnabled(true); |
| 1136 | actionBar.setDisplayHomeAsUpEnabled(true); |
| 1137 | |
| 1138 | updateFakeMenuButtonsVisibility(false); |
| 1139 | |
| 1140 | for (int i = 0; i < TAB_INDEX_COUNT; i++) { |
| 1141 | sendFragmentVisibilityChange(i, false /* not visible */ ); |
| 1142 | } |
| 1143 | |
| 1144 | // Show the search fragment and hide everything else. |
| 1145 | mSearchFragment.setUserVisibleHint(true); |
| 1146 | final FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
| 1147 | transaction.show(mSearchFragment); |
| 1148 | transaction.commitAllowingStateLoss(); |
| 1149 | mViewPager.setVisibility(View.GONE); |
| 1150 | |
| 1151 | // We need to call this and onActionViewCollapsed() manually, since we are using a custom |
| 1152 | // layout instead of asking the search menu item to take care of SearchView. |
| 1153 | mSearchView.onActionViewExpanded(); |
| 1154 | mInSearchUi = true; |
| 1155 | } |
| 1156 | |
| 1157 | private void showInputMethod(View view) { |
| 1158 | InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); |
| 1159 | if (imm != null) { |
| 1160 | if (!imm.showSoftInput(view, 0)) { |
| 1161 | Log.w(TAG, "Failed to show soft input method."); |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | private void hideInputMethod(View view) { |
| 1167 | InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); |
| 1168 | if (imm != null && view != null) { |
| 1169 | imm.hideSoftInputFromWindow(view.getWindowToken(), 0); |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | /** |
| 1174 | * Goes back to usual Phone UI with tags. Previously selected Tag and associated Fragment |
| 1175 | * should be automatically focused again. |
| 1176 | */ |
| 1177 | private void exitSearchUi() { |
| 1178 | final ActionBar actionBar = getActionBar(); |
| 1179 | |
| 1180 | // Hide the search fragment, if exists. |
| 1181 | if (mSearchFragment != null) { |
| 1182 | mSearchFragment.setUserVisibleHint(false); |
| 1183 | |
| 1184 | final FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
| 1185 | transaction.hide(mSearchFragment); |
| 1186 | transaction.commitAllowingStateLoss(); |
| 1187 | } |
| 1188 | |
| 1189 | // We want to hide SearchView and show Tabs. Also focus on previously selected one. |
| 1190 | actionBar.setDisplayShowCustomEnabled(false); |
| 1191 | actionBar.setDisplayShowHomeEnabled(false); |
| 1192 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
| 1193 | |
| 1194 | for (int i = 0; i < TAB_INDEX_COUNT; i++) { |
| 1195 | sendFragmentVisibilityChange(i, i == mViewPager.getCurrentItem()); |
| 1196 | } |
| 1197 | |
| 1198 | // Before exiting the search screen, reset swipe state. |
| 1199 | mDuringSwipe = false; |
| 1200 | mUserTabClick = false; |
| 1201 | |
| 1202 | mViewPager.setVisibility(View.VISIBLE); |
| 1203 | |
| 1204 | hideInputMethod(getCurrentFocus()); |
| 1205 | |
| 1206 | // Request to update option menu. |
| 1207 | invalidateOptionsMenu(); |
| 1208 | |
| 1209 | // See comments in onActionViewExpanded() |
| 1210 | mSearchView.onActionViewCollapsed(); |
| 1211 | mInSearchUi = false; |
| 1212 | } |
| 1213 | |
| 1214 | private Fragment getFragmentAt(int position) { |
| 1215 | switch (position) { |
| 1216 | case TAB_INDEX_DIALER: |
| 1217 | return mDialpadFragment; |
| 1218 | case TAB_INDEX_CALL_LOG: |
| 1219 | return mCallLogFragment; |
| 1220 | case TAB_INDEX_FAVORITES: |
| 1221 | return mPhoneFavoriteFragment; |
| 1222 | default: |
| 1223 | throw new IllegalStateException("Unknown fragment index: " + position); |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | private void sendFragmentVisibilityChange(int position, boolean visibility) { |
| 1228 | if (DEBUG) { |
| 1229 | Log.d(TAG, "sendFragmentVisibiltyChange(). position: " + position |
| 1230 | + ", visibility: " + visibility); |
| 1231 | } |
| 1232 | // Position can be -1 initially. See PageChangeListener. |
| 1233 | if (position >= 0) { |
| 1234 | final Fragment fragment = getFragmentAt(position); |
| 1235 | if (fragment != null) { |
| 1236 | fragment.setMenuVisibility(visibility); |
| 1237 | fragment.setUserVisibleHint(visibility); |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Update visibility of the search button and menu button at the bottom. |
| 1244 | * They should be invisible when bottom ActionBar's real items are available, and be visible |
| 1245 | * otherwise. |
| 1246 | * |
| 1247 | * @param visible True when visible. |
| 1248 | */ |
| 1249 | private void updateFakeMenuButtonsVisibility(boolean visible) { |
| 1250 | // Note: Landscape mode does not have the fake menu and search buttons. |
| 1251 | if (DEBUG) { |
| 1252 | Log.d(TAG, "updateFakeMenuButtonVisibility(" + visible + ")"); |
| 1253 | } |
| 1254 | |
| 1255 | if (mSearchButton != null) { |
| 1256 | if (visible) { |
| 1257 | mSearchButton.setVisibility(View.VISIBLE); |
| 1258 | } else { |
| 1259 | mSearchButton.setVisibility(View.INVISIBLE); |
| 1260 | } |
| 1261 | } |
| 1262 | if (mMenuButton != null) { |
| 1263 | if (visible && !ViewConfiguration.get(this).hasPermanentMenuKey()) { |
| 1264 | mMenuButton.setVisibility(View.VISIBLE); |
| 1265 | } else { |
| 1266 | mMenuButton.setVisibility(View.INVISIBLE); |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | /** Returns an Intent to launch Call Settings screen */ |
| 1272 | public static Intent getCallSettingsIntent() { |
| 1273 | final Intent intent = new Intent(Intent.ACTION_MAIN); |
| 1274 | intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME); |
| 1275 | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 1276 | return intent; |
| 1277 | } |
| 1278 | |
| 1279 | @Override |
| 1280 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 1281 | if (resultCode != Activity.RESULT_OK) { |
| 1282 | return; |
| 1283 | } |
| 1284 | switch (requestCode) { |
| 1285 | case SUBACTIVITY_ACCOUNT_FILTER: { |
| 1286 | AccountFilterUtil.handleAccountFilterResult( |
| 1287 | mContactListFilterController, resultCode, data); |
| 1288 | } |
| 1289 | break; |
| 1290 | } |
| 1291 | } |
| 1292 | } |