DialtactsActivity.java revision b6553457d0c3e84eefa0a853090b680090a7b2f9
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 17package com.android.contacts.activities; 18 19import com.android.contacts.R; 20import com.android.contacts.calllog.CallLogFragment; 21import com.android.contacts.dialpad.DialpadFragment; 22import com.android.contacts.interactions.PhoneNumberInteraction; 23import com.android.contacts.list.ContactListFilterController; 24import com.android.contacts.list.ContactListFilterController.ContactListFilterListener; 25import com.android.contacts.list.ContactListItemView; 26import com.android.contacts.list.OnPhoneNumberPickerActionListener; 27import com.android.contacts.list.PhoneFavoriteFragment; 28import com.android.contacts.list.PhoneNumberPickerFragment; 29import com.android.contacts.activities.TransactionSafeActivity; 30import com.android.contacts.util.AccountFilterUtil; 31import com.android.internal.telephony.ITelephony; 32 33import android.app.ActionBar; 34import android.app.ActionBar.LayoutParams; 35import android.app.ActionBar.Tab; 36import android.app.ActionBar.TabListener; 37import android.app.Activity; 38import android.app.Fragment; 39import android.app.FragmentManager; 40import android.app.FragmentTransaction; 41import android.content.Context; 42import android.content.Intent; 43import android.content.SharedPreferences; 44import android.net.Uri; 45import android.os.Bundle; 46import android.os.RemoteException; 47import android.os.ServiceManager; 48import android.preference.PreferenceManager; 49import android.provider.CallLog.Calls; 50import android.provider.ContactsContract.Contacts; 51import android.provider.ContactsContract.Intents.UI; 52import android.support.v13.app.FragmentPagerAdapter; 53import android.support.v4.view.ViewPager; 54import android.support.v4.view.ViewPager.OnPageChangeListener; 55import android.text.TextUtils; 56import android.util.Log; 57import android.view.Menu; 58import android.view.MenuInflater; 59import android.view.MenuItem; 60import android.view.MenuItem.OnMenuItemClickListener; 61import android.view.View; 62import android.view.View.OnClickListener; 63import android.view.View.OnFocusChangeListener; 64import android.view.ViewConfiguration; 65import android.view.inputmethod.InputMethodManager; 66import android.widget.PopupMenu; 67import android.widget.SearchView; 68import android.widget.SearchView.OnCloseListener; 69import android.widget.SearchView.OnQueryTextListener; 70 71/** 72 * The dialer activity that has one tab with the virtual 12key 73 * dialer, a tab with recent calls in it, a tab with the contacts and 74 * a tab with the favorite. This is the container and the tabs are 75 * embedded using intents. 76 * The dialer tab's title is 'phone', a more common name (see strings.xml). 77 */ 78public class DialtactsActivity extends TransactionSafeActivity { 79 private static final String TAG = "DialtactsActivity"; 80 81 /** Used to open Call Setting */ 82 private static final String PHONE_PACKAGE = "com.android.phone"; 83 private static final String CALL_SETTINGS_CLASS_NAME = 84 "com.android.phone.CallFeaturesSetting"; 85 86 /** 87 * Copied from PhoneApp. See comments in Phone app for more detail. 88 */ 89 public static final String EXTRA_CALL_ORIGIN = "com.android.phone.CALL_ORIGIN"; 90 public static final String CALL_ORIGIN_DIALTACTS = 91 "com.android.contacts.activities.DialtactsActivity"; 92 93 /** 94 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}. 95 */ 96 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER"; 97 98 /** Used both by {@link ActionBar} and {@link ViewPagerAdapter} */ 99 private static final int TAB_INDEX_DIALER = 0; 100 private static final int TAB_INDEX_CALL_LOG = 1; 101 private static final int TAB_INDEX_FAVORITES = 2; 102 103 private static final int TAB_INDEX_COUNT = 3; 104 105 private SharedPreferences mPrefs; 106 107 /** Last manually selected tab index */ 108 private static final String PREF_LAST_MANUALLY_SELECTED_TAB = 109 "DialtactsActivity_last_manually_selected_tab"; 110 private static final int PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT = TAB_INDEX_DIALER; 111 112 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1; 113 114 public class ViewPagerAdapter extends FragmentPagerAdapter { 115 public ViewPagerAdapter(FragmentManager fm) { 116 super(fm); 117 } 118 119 @Override 120 public Fragment getItem(int position) { 121 switch (position) { 122 case TAB_INDEX_DIALER: 123 return new DialpadFragment(); 124 case TAB_INDEX_CALL_LOG: 125 return new CallLogFragment(); 126 case TAB_INDEX_FAVORITES: 127 return new PhoneFavoriteFragment(); 128 } 129 throw new IllegalStateException("No fragment at position " + position); 130 } 131 132 @Override 133 public int getCount() { 134 return TAB_INDEX_COUNT; 135 } 136 } 137 138 private class PageChangeListener implements OnPageChangeListener { 139 private int mCurrentPosition = -1; 140 /** 141 * Used during page migration, to remember the next position {@link #onPageSelected(int)} 142 * specified. 143 */ 144 private int mNextPosition = -1; 145 146 @Override 147 public void onPageScrolled( 148 int position, float positionOffset, int positionOffsetPixels) { 149 } 150 151 @Override 152 public void onPageSelected(int position) { 153 final ActionBar actionBar = getActionBar(); 154 if (mCurrentPosition == position) { 155 Log.w(TAG, "Previous position and next position became same (" + position + ")"); 156 } 157 158 actionBar.selectTab(actionBar.getTabAt(position)); 159 mNextPosition = position; 160 161 // This method is called halfway between swiping between the two pages. 162 // When the next page is fully selected, the ViewPager will go back to IDLE state in 163 // onPageScrollStateChanged(). The order should be: 164 // (user's swipe) -> onPageSelected() -> IDLE in onPageScrollStateChanged() 165 // 166 // sendFragmentVisibilityChange() must be called from here or in the IDLE state to 167 // notify the visibility change events to two pages: the current page (pointed by 168 // mCurrentPosition) should receive sendFragmentVisibilityChange() with the second 169 // argument false, meaning "the page is now invisible", while the next page (pointed by 170 // mNextPosition) should receive the method with the second argument true, meaning 171 // "the page becomes visible". 172 // 173 // To make transition animation smooth enough, we need to delay the event in some cases: 174 // - We should delay both method calls when the dialpad screen is involved. 175 // The screen does not have the bottom action bar, requiring different layout to 176 // fill the screen. The layout refresh takes some time and thus should be done after 177 // the page migration being completed. 178 // - We should delay the method for the call log screen. The screen will update 179 // its internal state and may query full call log. which is too costly to do when 180 // setMenuVisibility() is called, making the animation slower. 181 // - We should *not* delay the method for the phone favorite screen. The screen has 182 // another icon the call log screen doesn't have. We want to show/hide it immediately 183 // after user's choosing pages. 184 if (mCurrentPosition == TAB_INDEX_CALL_LOG && mNextPosition == TAB_INDEX_FAVORITES) { 185 sendFragmentVisibilityChange(mNextPosition, true /* visible */ ); 186 invalidateOptionsMenu(); 187 } else if (mCurrentPosition == TAB_INDEX_FAVORITES 188 && mNextPosition == TAB_INDEX_CALL_LOG) { 189 sendFragmentVisibilityChange(mCurrentPosition, false /* not visible */ ); 190 invalidateOptionsMenu(); 191 } else { 192 // Delay sendFragmentVisibilityChange() for both positions. 193 } 194 } 195 196 public void setCurrentPosition(int position) { 197 mCurrentPosition = position; 198 } 199 200 @Override 201 public void onPageScrollStateChanged(int state) { 202 switch (state) { 203 case ViewPager.SCROLL_STATE_IDLE: { 204 // Call delayed sendFragmentVisibilityChange() call(s). 205 // See comments in onPageSelected() for more details. 206 if (mCurrentPosition == TAB_INDEX_CALL_LOG 207 && mNextPosition == TAB_INDEX_FAVORITES) { 208 sendFragmentVisibilityChange(mCurrentPosition, false /* not visible */ ); 209 } else if (mCurrentPosition == TAB_INDEX_FAVORITES 210 && mNextPosition == TAB_INDEX_CALL_LOG) { 211 sendFragmentVisibilityChange(mNextPosition, true /* visible */ ); 212 } else { 213 sendFragmentVisibilityChange(mCurrentPosition, false /* not visible */ ); 214 sendFragmentVisibilityChange(mNextPosition, true /* visible */ ); 215 } 216 invalidateOptionsMenu(); 217 218 mCurrentPosition = mNextPosition; 219 break; 220 } 221 case ViewPager.SCROLL_STATE_DRAGGING: 222 case ViewPager.SCROLL_STATE_SETTLING: 223 default: 224 break; 225 } 226 } 227 } 228 229 private String mFilterText; 230 231 /** Enables horizontal swipe between Fragments. */ 232 private ViewPager mViewPager; 233 private final PageChangeListener mPageChangeListener = new PageChangeListener(); 234 private DialpadFragment mDialpadFragment; 235 private CallLogFragment mCallLogFragment; 236 private PhoneFavoriteFragment mPhoneFavoriteFragment; 237 238 private final ContactListFilterListener mContactListFilterListener = 239 new ContactListFilterListener() { 240 @Override 241 public void onContactListFilterChanged() { 242 boolean doInvalidateOptionsMenu = false; 243 244 if (mPhoneFavoriteFragment != null && mPhoneFavoriteFragment.isAdded()) { 245 mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter()); 246 doInvalidateOptionsMenu = true; 247 } 248 249 if (mSearchFragment != null && mSearchFragment.isAdded()) { 250 mSearchFragment.setFilter(mContactListFilterController.getFilter()); 251 doInvalidateOptionsMenu = true; 252 } else { 253 Log.w(TAG, "Search Fragment isn't available when ContactListFilter is changed"); 254 } 255 256 if (doInvalidateOptionsMenu) { 257 invalidateOptionsMenu(); 258 } 259 } 260 }; 261 262 private final TabListener mTabListener = new TabListener() { 263 @Override 264 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 265 } 266 267 @Override 268 public void onTabSelected(Tab tab, FragmentTransaction ft) { 269 if (mViewPager.getCurrentItem() != tab.getPosition()) { 270 mViewPager.setCurrentItem(tab.getPosition(), true); 271 } 272 273 // During the call, we don't remember the tab position. 274 if (!DialpadFragment.phoneIsInUse()) { 275 // Remember this tab index. This function is also called, if the tab is set 276 // automatically in which case the setter (setCurrentTab) has to set this to its old 277 // value afterwards 278 mLastManuallySelectedFragment = tab.getPosition(); 279 } 280 } 281 282 @Override 283 public void onTabReselected(Tab tab, FragmentTransaction ft) { 284 } 285 }; 286 287 /** 288 * Fragment for searching phone numbers. Unlike the other Fragments, this doesn't correspond 289 * to tab but is shown by a search action. 290 */ 291 private PhoneNumberPickerFragment mSearchFragment; 292 /** 293 * True when this Activity is in its search UI (with a {@link SearchView} and 294 * {@link PhoneNumberPickerFragment}). 295 */ 296 private boolean mInSearchUi; 297 private SearchView mSearchView; 298 299 private final OnClickListener mFilterOptionClickListener = new OnClickListener() { 300 @Override 301 public void onClick(View view) { 302 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view); 303 final Menu menu = popupMenu.getMenu(); 304 popupMenu.inflate(R.menu.dialtacts_search_options); 305 final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option); 306 filterOptionMenuItem.setOnMenuItemClickListener(mFilterOptionsMenuItemClickListener); 307 final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact); 308 addContactOptionMenuItem.setIntent( 309 new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI)); 310 popupMenu.show(); 311 } 312 }; 313 314 /** 315 * The index of the Fragment (or, the tab) that has last been manually selected. 316 * This value does not keep track of programmatically set Tabs (e.g. Call Log after a Call) 317 */ 318 private int mLastManuallySelectedFragment; 319 320 private ContactListFilterController mContactListFilterController; 321 private OnMenuItemClickListener mFilterOptionsMenuItemClickListener = 322 new OnMenuItemClickListener() { 323 @Override 324 public boolean onMenuItemClick(MenuItem item) { 325 AccountFilterUtil.startAccountFilterActivityForResult( 326 DialtactsActivity.this, SUBACTIVITY_ACCOUNT_FILTER, 327 mContactListFilterController.getFilter()); 328 return true; 329 } 330 }; 331 332 private OnMenuItemClickListener mSearchMenuItemClickListener = 333 new OnMenuItemClickListener() { 334 @Override 335 public boolean onMenuItemClick(MenuItem item) { 336 enterSearchUi(); 337 return true; 338 } 339 }; 340 341 /** 342 * Listener used when one of phone numbers in search UI is selected. This will initiate a 343 * phone call using the phone number. 344 */ 345 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener = 346 new OnPhoneNumberPickerActionListener() { 347 @Override 348 public void onPickPhoneNumberAction(Uri dataUri) { 349 // Specify call-origin so that users will see the previous tab instead of 350 // CallLog screen (search UI will be automatically exited). 351 PhoneNumberInteraction.startInteractionForPhoneCall( 352 DialtactsActivity.this, dataUri, 353 CALL_ORIGIN_DIALTACTS); 354 } 355 356 @Override 357 public void onShortcutIntentCreated(Intent intent) { 358 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring."); 359 } 360 361 @Override 362 public void onHomeInActionBarSelected() { 363 exitSearchUi(); 364 } 365 }; 366 367 /** 368 * Listener used to send search queries to the phone search fragment. 369 */ 370 private final OnQueryTextListener mPhoneSearchQueryTextListener = 371 new OnQueryTextListener() { 372 @Override 373 public boolean onQueryTextSubmit(String query) { 374 View view = getCurrentFocus(); 375 if (view != null) { 376 hideInputMethod(view); 377 view.clearFocus(); 378 } 379 return true; 380 } 381 382 @Override 383 public boolean onQueryTextChange(String newText) { 384 // Show search result with non-empty text. Show a bare list otherwise. 385 if (mSearchFragment != null) { 386 mSearchFragment.setQueryString(newText, true); 387 } 388 return true; 389 } 390 }; 391 392 /** 393 * Listener used to handle the "close" button on the right side of {@link SearchView}. 394 * If some text is in the search view, this will clean it up. Otherwise this will exit 395 * the search UI and let users go back to usual Phone UI. 396 * 397 * This does _not_ handle back button. 398 */ 399 private final OnCloseListener mPhoneSearchCloseListener = 400 new OnCloseListener() { 401 @Override 402 public boolean onClose() { 403 if (!TextUtils.isEmpty(mSearchView.getQuery())) { 404 mSearchView.setQuery(null, true); 405 } 406 return true; 407 } 408 }; 409 410 private final View.OnLayoutChangeListener mFirstLayoutListener 411 = new View.OnLayoutChangeListener() { 412 @Override 413 public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, 414 int oldTop, int oldRight, int oldBottom) { 415 v.removeOnLayoutChangeListener(this); // Unregister self. 416 addSearchFragment(); 417 } 418 }; 419 420 @Override 421 protected void onCreate(Bundle icicle) { 422 super.onCreate(icicle); 423 424 final Intent intent = getIntent(); 425 fixIntent(intent); 426 427 setContentView(R.layout.dialtacts_activity); 428 429 mContactListFilterController = ContactListFilterController.getInstance(this); 430 mContactListFilterController.addListener(mContactListFilterListener); 431 432 findViewById(R.id.dialtacts_frame).addOnLayoutChangeListener(mFirstLayoutListener); 433 434 mViewPager = (ViewPager) findViewById(R.id.pager); 435 mViewPager.setAdapter(new ViewPagerAdapter(getFragmentManager())); 436 mViewPager.setOnPageChangeListener(mPageChangeListener); 437 438 // Setup the ActionBar tabs (the order matches the tab-index contants TAB_INDEX_*) 439 setupDialer(); 440 setupCallLog(); 441 setupFavorites(); 442 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 443 getActionBar().setDisplayShowTitleEnabled(false); 444 getActionBar().setDisplayShowHomeEnabled(false); 445 446 // Load the last manually loaded tab 447 mPrefs = PreferenceManager.getDefaultSharedPreferences(this); 448 mLastManuallySelectedFragment = mPrefs.getInt(PREF_LAST_MANUALLY_SELECTED_TAB, 449 PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT); 450 if (mLastManuallySelectedFragment >= TAB_INDEX_COUNT) { 451 // Stored value may have exceeded the number of current tabs. Reset it. 452 mLastManuallySelectedFragment = PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT; 453 } 454 455 setCurrentTab(intent); 456 457 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction()) 458 && icicle == null) { 459 setupFilterText(intent); 460 } 461 } 462 463 @Override 464 public void onStart() { 465 super.onStart(); 466 if (mPhoneFavoriteFragment != null) { 467 mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter()); 468 } 469 if (mSearchFragment != null) { 470 mSearchFragment.setFilter(mContactListFilterController.getFilter()); 471 } 472 } 473 474 @Override 475 public void onDestroy() { 476 super.onDestroy(); 477 mContactListFilterController.removeListener(mContactListFilterListener); 478 } 479 480 /** 481 * Add search fragment. Note this is called during onLayout, so there's some restrictions, 482 * such as executePendingTransaction can't be used in it. 483 */ 484 private void addSearchFragment() { 485 // In order to take full advantage of "fragment deferred start", we need to create the 486 // search fragment after all other fragments are created. 487 // The other fragments are created by the ViewPager on the first onMeasure(). 488 // We use the first onLayout call, which is after onMeasure(). 489 490 // Just return if the fragment is already created, which happens after configuration 491 // changes. 492 if (mSearchFragment != null) return; 493 494 final FragmentTransaction ft = getFragmentManager().beginTransaction(); 495 final Fragment searchFragment = new PhoneNumberPickerFragment(); 496 497 searchFragment.setUserVisibleHint(false); 498 ft.add(R.id.dialtacts_frame, searchFragment); 499 ft.hide(searchFragment); 500 ft.commitAllowingStateLoss(); 501 } 502 503 private void prepareSearchView() { 504 final View searchViewLayout = 505 getLayoutInflater().inflate(R.layout.dialtacts_custom_action_bar, null); 506 mSearchView = (SearchView) searchViewLayout.findViewById(R.id.search_view); 507 mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener); 508 mSearchView.setOnCloseListener(mPhoneSearchCloseListener); 509 // Since we're using a custom layout for showing SearchView instead of letting the 510 // search menu icon do that job, we need to manually configure the View so it looks 511 // "shown via search menu". 512 // - it should be iconified by default 513 // - it should not be iconified at this time 514 // See also comments for onActionViewExpanded()/onActionViewCollapsed() 515 mSearchView.setIconifiedByDefault(true); 516 mSearchView.setQueryHint(getString(R.string.hint_findContacts)); 517 mSearchView.setIconified(false); 518 mSearchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() { 519 @Override 520 public void onFocusChange(View view, boolean hasFocus) { 521 if (hasFocus) { 522 showInputMethod(view.findFocus()); 523 } 524 } 525 }); 526 527 if (!ViewConfiguration.get(this).hasPermanentMenuKey()) { 528 // Filter option menu should be shown on the right side of SearchView. 529 final View filterOptionView = searchViewLayout.findViewById(R.id.search_option); 530 filterOptionView.setVisibility(View.VISIBLE); 531 filterOptionView.setOnClickListener(mFilterOptionClickListener); 532 } 533 534 getActionBar().setCustomView(searchViewLayout, 535 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 536 } 537 538 @Override 539 public void onAttachFragment(Fragment fragment) { 540 // This method can be called before onCreate(), at which point we cannot rely on ViewPager. 541 // In that case, we will setup the "current position" soon after the ViewPager is ready. 542 final int currentPosition = mViewPager != null ? mViewPager.getCurrentItem() : -1; 543 544 if (fragment instanceof DialpadFragment) { 545 mDialpadFragment = (DialpadFragment) fragment; 546 mDialpadFragment.setListener(mDialpadListener); 547 } else if (fragment instanceof CallLogFragment) { 548 mCallLogFragment = (CallLogFragment) fragment; 549 } else if (fragment instanceof PhoneFavoriteFragment) { 550 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment; 551 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener); 552 if (mContactListFilterController != null 553 && mContactListFilterController.getFilter() != null) { 554 mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter()); 555 } 556 } else if (fragment instanceof PhoneNumberPickerFragment) { 557 mSearchFragment = (PhoneNumberPickerFragment) fragment; 558 mSearchFragment.setOnPhoneNumberPickerActionListener(mPhoneNumberPickerActionListener); 559 mSearchFragment.setQuickContactEnabled(true); 560 mSearchFragment.setDarkTheme(true); 561 mSearchFragment.setPhotoPosition(ContactListItemView.PhotoPosition.LEFT); 562 mSearchFragment.setUseCallableUri(true); 563 if (mContactListFilterController != null 564 && mContactListFilterController.getFilter() != null) { 565 mSearchFragment.setFilter(mContactListFilterController.getFilter()); 566 } 567 // Here we assume that we're not on the search mode, so let's hide the fragment. 568 // 569 // We get here either when the fragment is created (normal case), or after configuration 570 // changes. In the former case, we're not in search mode because we can only 571 // enter search mode if the fragment is created. (see enterSearchUi()) 572 // In the latter case we're not in search mode either because we don't retain 573 // mInSearchUi -- ideally we should but at this point it's not supported. 574 mSearchFragment.setUserVisibleHint(false); 575 // After configuration changes fragments will forget their "hidden" state, so make 576 // sure to hide it. 577 if (!mSearchFragment.isHidden()) { 578 final FragmentTransaction transaction = getFragmentManager().beginTransaction(); 579 transaction.hide(mSearchFragment); 580 transaction.commitAllowingStateLoss(); 581 } 582 } 583 } 584 585 @Override 586 protected void onPause() { 587 super.onPause(); 588 589 mPrefs.edit().putInt(PREF_LAST_MANUALLY_SELECTED_TAB, mLastManuallySelectedFragment) 590 .apply(); 591 } 592 593 private void fixIntent(Intent intent) { 594 // This should be cleaned up: the call key used to send an Intent 595 // that just said to go to the recent calls list. It now sends this 596 // abstract action, but this class hasn't been rewritten to deal with it. 597 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) { 598 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE); 599 intent.putExtra("call_key", true); 600 setIntent(intent); 601 } 602 } 603 604 private void setupDialer() { 605 final Tab tab = getActionBar().newTab(); 606 tab.setContentDescription(R.string.dialerIconLabel); 607 tab.setTabListener(mTabListener); 608 tab.setIcon(R.drawable.ic_tab_dialer); 609 getActionBar().addTab(tab); 610 } 611 612 private void setupCallLog() { 613 final Tab tab = getActionBar().newTab(); 614 tab.setContentDescription(R.string.recentCallsIconLabel); 615 tab.setIcon(R.drawable.ic_tab_recent); 616 tab.setTabListener(mTabListener); 617 getActionBar().addTab(tab); 618 } 619 620 private void setupFavorites() { 621 final Tab tab = getActionBar().newTab(); 622 tab.setContentDescription(R.string.contactsFavoritesLabel); 623 tab.setIcon(R.drawable.ic_tab_all); 624 tab.setTabListener(mTabListener); 625 getActionBar().addTab(tab); 626 } 627 628 /** 629 * Returns true if the intent is due to hitting the green send key while in a call. 630 * 631 * @param intent the intent that launched this activity 632 * @param recentCallsRequest true if the intent is requesting to view recent calls 633 * @return true if the intent is due to hitting the green send key while in a call 634 */ 635 private boolean isSendKeyWhileInCall(final Intent intent, 636 final boolean recentCallsRequest) { 637 // If there is a call in progress go to the call screen 638 if (recentCallsRequest) { 639 final boolean callKey = intent.getBooleanExtra("call_key", false); 640 641 try { 642 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); 643 if (callKey && phone != null && phone.showCallScreen()) { 644 return true; 645 } 646 } catch (RemoteException e) { 647 Log.e(TAG, "Failed to handle send while in call", e); 648 } 649 } 650 651 return false; 652 } 653 654 /** 655 * Sets the current tab based on the intent's request type 656 * 657 * @param intent Intent that contains information about which tab should be selected 658 */ 659 private void setCurrentTab(Intent intent) { 660 // If we got here by hitting send and we're in call forward along to the in-call activity 661 final boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.getType()); 662 if (isSendKeyWhileInCall(intent, recentCallsRequest)) { 663 finish(); 664 return; 665 } 666 667 // Remember the old manually selected tab index so that it can be restored if it is 668 // overwritten by one of the programmatic tab selections 669 final int savedTabIndex = mLastManuallySelectedFragment; 670 671 final int tabIndex; 672 if (DialpadFragment.phoneIsInUse() || isDialIntent(intent)) { 673 tabIndex = TAB_INDEX_DIALER; 674 } else if (recentCallsRequest) { 675 tabIndex = TAB_INDEX_CALL_LOG; 676 } else { 677 tabIndex = mLastManuallySelectedFragment; 678 } 679 680 final int previousItemIndex = mViewPager.getCurrentItem(); 681 mViewPager.setCurrentItem(tabIndex, false /* smoothScroll */); 682 if (previousItemIndex != tabIndex) { 683 sendFragmentVisibilityChange(previousItemIndex, false /* not visible */ ); 684 } 685 mPageChangeListener.setCurrentPosition(tabIndex); 686 sendFragmentVisibilityChange(tabIndex, true /* visible */ ); 687 688 // Restore to the previous manual selection 689 mLastManuallySelectedFragment = savedTabIndex; 690 } 691 692 @Override 693 public void onNewIntent(Intent newIntent) { 694 setIntent(newIntent); 695 fixIntent(newIntent); 696 setCurrentTab(newIntent); 697 final String action = newIntent.getAction(); 698 if (UI.FILTER_CONTACTS_ACTION.equals(action)) { 699 setupFilterText(newIntent); 700 } 701 if (mInSearchUi || (mSearchFragment != null && mSearchFragment.isVisible())) { 702 exitSearchUi(); 703 } 704 705 if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { 706 if (mDialpadFragment != null) { 707 mDialpadFragment.configureScreenFromIntent(newIntent); 708 } else { 709 Log.e(TAG, "DialpadFragment isn't ready yet when the tab is already selected."); 710 } 711 } 712 invalidateOptionsMenu(); 713 } 714 715 /** Returns true if the given intent contains a phone number to populate the dialer with */ 716 private boolean isDialIntent(Intent intent) { 717 final String action = intent.getAction(); 718 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) { 719 return true; 720 } 721 if (Intent.ACTION_VIEW.equals(action)) { 722 final Uri data = intent.getData(); 723 if (data != null && "tel".equals(data.getScheme())) { 724 return true; 725 } 726 } 727 return false; 728 } 729 730 /** 731 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}. 732 * This text originally came from a FILTER_CONTACTS_ACTION intent received 733 * by this activity. The stored text will then be cleared after after this 734 * method returns. 735 * 736 * @return The stored filter text 737 */ 738 public String getAndClearFilterText() { 739 String filterText = mFilterText; 740 mFilterText = null; 741 return filterText; 742 } 743 744 /** 745 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent. 746 * This is so child activities can check if they are supposed to display a filter. 747 * 748 * @param intent The intent received in {@link #onNewIntent(Intent)} 749 */ 750 private void setupFilterText(Intent intent) { 751 // If the intent was relaunched from history, don't apply the filter text. 752 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { 753 return; 754 } 755 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY); 756 if (filter != null && filter.length() > 0) { 757 mFilterText = filter; 758 } 759 } 760 761 @Override 762 public void onBackPressed() { 763 if (mInSearchUi) { 764 // We should let the user go back to usual screens with tabs. 765 exitSearchUi(); 766 } else if (isTaskRoot()) { 767 // Instead of stopping, simply push this to the back of the stack. 768 // This is only done when running at the top of the stack; 769 // otherwise, we have been launched by someone else so need to 770 // allow the user to go back to the caller. 771 moveTaskToBack(false); 772 } else { 773 super.onBackPressed(); 774 } 775 } 776 777 private DialpadFragment.Listener mDialpadListener = new DialpadFragment.Listener() { 778 @Override 779 public void onSearchButtonPressed() { 780 enterSearchUi(); 781 } 782 }; 783 784 private PhoneFavoriteFragment.Listener mPhoneFavoriteListener = 785 new PhoneFavoriteFragment.Listener() { 786 @Override 787 public void onContactSelected(Uri contactUri) { 788 PhoneNumberInteraction.startInteractionForPhoneCall( 789 DialtactsActivity.this, contactUri, 790 CALL_ORIGIN_DIALTACTS); 791 } 792 }; 793 794 @Override 795 public boolean onCreateOptionsMenu(Menu menu) { 796 MenuInflater inflater = getMenuInflater(); 797 inflater.inflate(R.menu.dialtacts_options, menu); 798 return true; 799 } 800 801 @Override 802 public boolean onPrepareOptionsMenu(Menu menu) { 803 final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar); 804 final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option); 805 final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact); 806 final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings); 807 final Tab tab = getActionBar().getSelectedTab(); 808 if (mInSearchUi) { 809 searchMenuItem.setVisible(false); 810 if (ViewConfiguration.get(this).hasPermanentMenuKey()) { 811 filterOptionMenuItem.setVisible(true); 812 filterOptionMenuItem.setOnMenuItemClickListener( 813 mFilterOptionsMenuItemClickListener); 814 } else { 815 // Filter option menu should be not be shown as a overflow menu. 816 filterOptionMenuItem.setVisible(false); 817 } 818 addContactOptionMenuItem.setVisible(false); 819 callSettingsMenuItem.setVisible(false); 820 } else { 821 final boolean showCallSettingsMenu; 822 if (tab != null && tab.getPosition() == TAB_INDEX_DIALER) { 823 searchMenuItem.setVisible(false); 824 // When permanent menu key is _not_ available, the call settings menu should be 825 // available via DialpadFragment. 826 showCallSettingsMenu = ViewConfiguration.get(this).hasPermanentMenuKey(); 827 } else { 828 searchMenuItem.setVisible(true); 829 searchMenuItem.setOnMenuItemClickListener(mSearchMenuItemClickListener); 830 showCallSettingsMenu = true; 831 } 832 if (tab != null && tab.getPosition() == TAB_INDEX_FAVORITES) { 833 filterOptionMenuItem.setVisible(true); 834 filterOptionMenuItem.setOnMenuItemClickListener( 835 mFilterOptionsMenuItemClickListener); 836 addContactOptionMenuItem.setVisible(true); 837 addContactOptionMenuItem.setIntent( 838 new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI)); 839 } else { 840 filterOptionMenuItem.setVisible(false); 841 addContactOptionMenuItem.setVisible(false); 842 } 843 844 if (showCallSettingsMenu) { 845 callSettingsMenuItem.setVisible(true); 846 callSettingsMenuItem.setIntent(DialtactsActivity.getCallSettingsIntent()); 847 } else { 848 callSettingsMenuItem.setVisible(false); 849 } 850 } 851 852 return true; 853 } 854 855 @Override 856 public void startSearch(String initialQuery, boolean selectInitialQuery, 857 Bundle appSearchData, boolean globalSearch) { 858 if (mSearchFragment != null && mSearchFragment.isAdded() && !globalSearch) { 859 if (mInSearchUi) { 860 if (mSearchView.hasFocus()) { 861 showInputMethod(mSearchView.findFocus()); 862 } else { 863 mSearchView.requestFocus(); 864 } 865 } else { 866 enterSearchUi(); 867 } 868 } else { 869 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); 870 } 871 } 872 873 /** 874 * Hides every tab and shows search UI for phone lookup. 875 */ 876 private void enterSearchUi() { 877 if (mSearchFragment == null) { 878 // We add the search fragment dynamically in the first onLayoutChange() and 879 // mSearchFragment is set sometime later when the fragment transaction is actually 880 // executed, which means there's a window when users are able to hit the (physical) 881 // search key but mSearchFragment is still null. 882 // It's quite hard to handle this case right, so let's just ignore the search key 883 // in this case. Users can just hit it again and it will work this time. 884 return; 885 } 886 if (mSearchView == null) { 887 prepareSearchView(); 888 } 889 890 final ActionBar actionBar = getActionBar(); 891 892 final Tab tab = actionBar.getSelectedTab(); 893 894 // User can search during the call, but we don't want to remember the status. 895 if (tab != null && !DialpadFragment.phoneIsInUse()) { 896 mLastManuallySelectedFragment = tab.getPosition(); 897 } 898 899 mSearchView.setQuery(null, true); 900 901 actionBar.setDisplayShowCustomEnabled(true); 902 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 903 actionBar.setDisplayShowHomeEnabled(true); 904 actionBar.setDisplayHomeAsUpEnabled(true); 905 906 sendFragmentVisibilityChange(mViewPager.getCurrentItem(), false /* not visible */ ); 907 908 // Show the search fragment and hide everything else. 909 mSearchFragment.setUserVisibleHint(true); 910 final FragmentTransaction transaction = getFragmentManager().beginTransaction(); 911 transaction.show(mSearchFragment); 912 transaction.commitAllowingStateLoss(); 913 mViewPager.setVisibility(View.GONE); 914 915 // We need to call this and onActionViewCollapsed() manually, since we are using a custom 916 // layout instead of asking the search menu item to take care of SearchView. 917 mSearchView.onActionViewExpanded(); 918 mInSearchUi = true; 919 } 920 921 private void showInputMethod(View view) { 922 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 923 if (imm != null) { 924 if (!imm.showSoftInput(view, 0)) { 925 Log.w(TAG, "Failed to show soft input method."); 926 } 927 } 928 } 929 930 private void hideInputMethod(View view) { 931 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 932 if (imm != null && view != null) { 933 imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 934 } 935 } 936 937 /** 938 * Goes back to usual Phone UI with tags. Previously selected Tag and associated Fragment 939 * should be automatically focused again. 940 */ 941 private void exitSearchUi() { 942 final ActionBar actionBar = getActionBar(); 943 944 // Hide the search fragment, if exists. 945 if (mSearchFragment != null) { 946 mSearchFragment.setUserVisibleHint(false); 947 948 final FragmentTransaction transaction = getFragmentManager().beginTransaction(); 949 transaction.hide(mSearchFragment); 950 transaction.commitAllowingStateLoss(); 951 } 952 953 // We want to hide SearchView and show Tabs. Also focus on previously selected one. 954 actionBar.setDisplayShowCustomEnabled(false); 955 actionBar.setDisplayShowHomeEnabled(false); 956 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 957 958 sendFragmentVisibilityChange(mViewPager.getCurrentItem(), true /* visible */ ); 959 960 mViewPager.setVisibility(View.VISIBLE); 961 962 hideInputMethod(getCurrentFocus()); 963 964 // Request to update option menu. 965 invalidateOptionsMenu(); 966 967 // See comments in onActionViewExpanded() 968 mSearchView.onActionViewCollapsed(); 969 mInSearchUi = false; 970 } 971 972 private Fragment getFragmentAt(int position) { 973 switch (position) { 974 case TAB_INDEX_DIALER: 975 return mDialpadFragment; 976 case TAB_INDEX_CALL_LOG: 977 return mCallLogFragment; 978 case TAB_INDEX_FAVORITES: 979 return mPhoneFavoriteFragment; 980 default: 981 throw new IllegalStateException("Unknown fragment index: " + position); 982 } 983 } 984 985 private void sendFragmentVisibilityChange(int position, boolean visibility) { 986 // Position can be -1 initially. See PageChangeListener. 987 if (position >= 0) { 988 final Fragment fragment = getFragmentAt(position); 989 if (fragment != null) { 990 fragment.setMenuVisibility(visibility); 991 } 992 } 993 } 994 995 /** Returns an Intent to launch Call Settings screen */ 996 public static Intent getCallSettingsIntent() { 997 final Intent intent = new Intent(Intent.ACTION_MAIN); 998 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME); 999 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 1000 return intent; 1001 } 1002 1003 @Override 1004 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 1005 if (resultCode != Activity.RESULT_OK) { 1006 return; 1007 } 1008 switch (requestCode) { 1009 case SUBACTIVITY_ACCOUNT_FILTER: { 1010 AccountFilterUtil.handleAccountFilterResult( 1011 mContactListFilterController, resultCode, data); 1012 } 1013 break; 1014 } 1015 } 1016} 1017