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