DialtactsActivity.java revision a07abf7f425f2da7487d58851c10c30c03ebda91
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.AccountFilterActivity; 24import com.android.contacts.list.ContactListFilter; 25import com.android.contacts.list.ContactListFilterController; 26import com.android.contacts.list.ContactListFilterController.ContactListFilterListener; 27import com.android.contacts.list.ContactTileAdapter.DisplayType; 28import com.android.contacts.list.ContactTileListFragment; 29import com.android.contacts.list.OnPhoneNumberPickerActionListener; 30import com.android.contacts.list.PhoneNumberPickerFragment; 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.provider.CallLog.Calls; 49import android.provider.ContactsContract.Intents.UI; 50import android.support.v13.app.FragmentPagerAdapter; 51import android.support.v4.view.ViewPager; 52import android.support.v4.view.ViewPager.OnPageChangeListener; 53import android.text.TextUtils; 54import android.util.Log; 55import android.view.Menu; 56import android.view.MenuInflater; 57import android.view.MenuItem; 58import android.view.MenuItem.OnMenuItemClickListener; 59import android.view.View; 60import android.view.View.OnAttachStateChangeListener; 61import android.view.inputmethod.InputMethodManager; 62import android.widget.SearchView; 63import android.widget.SearchView.OnCloseListener; 64import android.widget.SearchView.OnQueryTextListener; 65 66/** 67 * The dialer activity that has one tab with the virtual 12key 68 * dialer, a tab with recent calls in it, a tab with the contacts and 69 * a tab with the favorite. This is the container and the tabs are 70 * embedded using intents. 71 * The dialer tab's title is 'phone', a more common name (see strings.xml). 72 */ 73public class DialtactsActivity extends Activity { 74 private static final String TAG = "DialtactsActivity"; 75 76 /** Used to open Call Setting */ 77 private static final String PHONE_PACKAGE = "com.android.phone"; 78 private static final String CALL_SETTINGS_CLASS_NAME = 79 "com.android.phone.CallFeaturesSetting"; 80 81 /** Used both by {@link ActionBar} and {@link ViewPagerAdapter} */ 82 private static final int TAB_INDEX_DIALER = 0; 83 private static final int TAB_INDEX_CALL_LOG = 1; 84 private static final int TAB_INDEX_FAVORITES = 2; 85 86 private static final int TAB_INDEX_COUNT = 3; 87 88 private static final int SUBACTIVITY_ACCOUNT_FILTER = 0; 89 90 /** Name of the dialtacts shared preferences */ 91 static final String PREFS_DIALTACTS = "dialtacts"; 92 static final boolean PREF_FAVORITES_AS_CONTACTS_DEFAULT = false; 93 94 /** Last manually selected tab index */ 95 private static final String PREF_LAST_MANUALLY_SELECTED_TAB = "last_manually_selected_tab"; 96 private static final int PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT = TAB_INDEX_DIALER; 97 98 /** 99 * Listener interface for Fragments accommodated in {@link ViewPager} enabling them to know 100 * when it becomes visible or invisible inside the ViewPager. 101 */ 102 public interface ViewPagerVisibilityListener { 103 public void onVisibilityChanged(boolean visible); 104 } 105 106 public class ViewPagerAdapter extends FragmentPagerAdapter { 107 private DialpadFragment mDialpadFragment; 108 private CallLogFragment mCallLogFragment; 109 private ContactTileListFragment mContactTileListFragment; 110 111 public ViewPagerAdapter(FragmentManager fm) { 112 super(fm); 113 } 114 115 @Override 116 public Fragment getItem(int position) { 117 switch (position) { 118 case TAB_INDEX_DIALER: 119 if (mDialpadFragment == null) { 120 mDialpadFragment = new DialpadFragment(); 121 } 122 return mDialpadFragment; 123 case TAB_INDEX_CALL_LOG: 124 if (mCallLogFragment == null) { 125 mCallLogFragment = new CallLogFragment(); 126 } 127 return mCallLogFragment; 128 case TAB_INDEX_FAVORITES: 129 if (mContactTileListFragment == null) { 130 mContactTileListFragment = new ContactTileListFragment(); 131 } 132 return mContactTileListFragment; 133 } 134 throw new IllegalStateException("No fragment at position " + position); 135 } 136 137 @Override 138 public int getCount() { 139 return TAB_INDEX_COUNT; 140 } 141 } 142 143 private class PageChangeListener implements OnPageChangeListener { 144 private int mCurrentPosition = -1; 145 /** 146 * Used during page migration, to remember the next position {@link #onPageSelected(int)} 147 * specified. 148 */ 149 private int mNextPosition = -1; 150 151 @Override 152 public void onPageScrolled( 153 int position, float positionOffset, int positionOffsetPixels) { 154 } 155 156 @Override 157 public void onPageSelected(int position) { 158 final ActionBar actionBar = getActionBar(); 159 if (mCurrentPosition == position) { 160 Log.w(TAG, "Previous position and next position became same (" + position + ")"); 161 } 162 163 actionBar.selectTab(actionBar.getTabAt(position)); 164 mNextPosition = position; 165 } 166 167 public void setCurrentPosition(int position) { 168 mCurrentPosition = position; 169 } 170 171 @Override 172 public void onPageScrollStateChanged(int state) { 173 switch (state) { 174 case ViewPager.SCROLL_STATE_IDLE: { 175 if (mCurrentPosition >= 0) { 176 sendFragmentVisibilityChange(mCurrentPosition, false); 177 } 178 if (mNextPosition >= 0) { 179 sendFragmentVisibilityChange(mNextPosition, true); 180 } 181 invalidateOptionsMenu(); 182 183 mCurrentPosition = mNextPosition; 184 break; 185 } 186 case ViewPager.SCROLL_STATE_DRAGGING: 187 case ViewPager.SCROLL_STATE_SETTLING: 188 default: 189 break; 190 } 191 } 192 } 193 194 private String mFilterText; 195 private Uri mDialUri; 196 197 /** Enables horizontal swipe between Fragments. */ 198 private ViewPager mViewPager; 199 private final PageChangeListener mPageChangeListener = new PageChangeListener(); 200 private DialpadFragment mDialpadFragment; 201 private CallLogFragment mCallLogFragment; 202 private ContactTileListFragment mStrequentFragment; 203 204 private final TabListener mTabListener = new TabListener() { 205 @Override 206 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 207 } 208 209 @Override 210 public void onTabSelected(Tab tab, FragmentTransaction ft) { 211 if (mViewPager.getCurrentItem() != tab.getPosition()) { 212 mViewPager.setCurrentItem(tab.getPosition(), true); 213 } 214 215 // During the call, we don't remember the tab position. 216 if (!DialpadFragment.phoneIsInUse()) { 217 // Remember this tab index. This function is also called, if the tab is set 218 // automatically in which case the setter (setCurrentTab) has to set this to its old 219 // value afterwards 220 mLastManuallySelectedFragment = tab.getPosition(); 221 } 222 } 223 224 @Override 225 public void onTabReselected(Tab tab, FragmentTransaction ft) { 226 } 227 }; 228 229 /** 230 * Fragment for searching phone numbers. Unlike the other Fragments, this doesn't correspond 231 * to tab but is shown by a search action. 232 */ 233 private PhoneNumberPickerFragment mSearchFragment; 234 /** 235 * True when this Activity is in its search UI (with a {@link SearchView} and 236 * {@link PhoneNumberPickerFragment}). 237 */ 238 private boolean mInSearchUi; 239 private SearchView mSearchView; 240 241 /** 242 * The index of the Fragment (or, the tab) that has last been manually selected. 243 * This value does not keep track of programmatically set Tabs (e.g. Call Log after a Call) 244 */ 245 private int mLastManuallySelectedFragment; 246 247 private ContactListFilterController mContactListFilterController; 248 private OnMenuItemClickListener mFilterOptionsMenuItemClickListener = 249 new OnMenuItemClickListener() { 250 @Override 251 public boolean onMenuItemClick(MenuItem item) { 252 final Intent intent = 253 new Intent(DialtactsActivity.this, AccountFilterActivity.class); 254 ContactListFilter filter = mContactListFilterController.getFilter(); 255 startActivityForResult(intent, SUBACTIVITY_ACCOUNT_FILTER); 256 return true; 257 } 258 }; 259 260 private OnMenuItemClickListener mSearchMenuItemClickListener = 261 new OnMenuItemClickListener() { 262 @Override 263 public boolean onMenuItemClick(MenuItem item) { 264 enterSearchUi(); 265 return true; 266 } 267 }; 268 269 /** 270 * Listener used when one of phone numbers in search UI is selected. This will initiate a 271 * phone call using the phone number. 272 */ 273 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener = 274 new OnPhoneNumberPickerActionListener() { 275 @Override 276 public void onPickPhoneNumberAction(Uri dataUri) { 277 PhoneNumberInteraction.startInteractionForPhoneCall( 278 DialtactsActivity.this, dataUri); 279 } 280 281 @Override 282 public void onShortcutIntentCreated(Intent intent) { 283 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring."); 284 } 285 286 @Override 287 public void onHomeInActionBarSelected() { 288 exitSearchUi(); 289 } 290 }; 291 292 /** 293 * Listener used to send search queries to the phone search fragment. 294 */ 295 private final OnQueryTextListener mPhoneSearchQueryTextListener = 296 new OnQueryTextListener() { 297 @Override 298 public boolean onQueryTextSubmit(String query) { 299 View view = getCurrentFocus(); 300 if (view != null) { 301 hideInputMethod(view); 302 view.clearFocus(); 303 } 304 return true; 305 } 306 307 @Override 308 public boolean onQueryTextChange(String newText) { 309 // Show search result with non-empty text. Show a bare list otherwise. 310 mSearchFragment.setQueryString(newText, true); 311 mSearchFragment.setSearchMode(!TextUtils.isEmpty(newText)); 312 return true; 313 } 314 }; 315 316 /** 317 * Listener used to handle the "close" button on the right side of {@link SearchView}. 318 * If some text is in the search view, this will clean it up. Otherwise this will exit 319 * the search UI and let users go back to usual Phone UI. 320 * 321 * This does _not_ handle back button. 322 */ 323 private final OnCloseListener mPhoneSearchCloseListener = 324 new OnCloseListener() { 325 @Override 326 public boolean onClose() { 327 if (!TextUtils.isEmpty(mSearchView.getQuery())) { 328 mSearchView.setQuery(null, true); 329 } 330 return true; 331 } 332 }; 333 334 @Override 335 protected void onCreate(Bundle icicle) { 336 super.onCreate(icicle); 337 338 final Intent intent = getIntent(); 339 fixIntent(intent); 340 341 setContentView(R.layout.dialtacts_activity); 342 343 mContactListFilterController = new ContactListFilterController(this); 344 mContactListFilterController.addListener(new ContactListFilterListener() { 345 @Override 346 public void onContactListFilterChanged() { 347 if (mSearchFragment == null || !mSearchFragment.isAdded()) { 348 Log.w(TAG, "Search Fragment isn't available when ContactListFilter is changed"); 349 return; 350 } 351 mSearchFragment .setFilter(mContactListFilterController.getFilter()); 352 353 invalidateOptionsMenu(); 354 } 355 }); 356 357 mViewPager = (ViewPager) findViewById(R.id.pager); 358 mViewPager.setAdapter(new ViewPagerAdapter(getFragmentManager())); 359 mViewPager.setOnPageChangeListener(mPageChangeListener); 360 361 // Setup the ActionBar tabs (the order matches the tab-index contants TAB_INDEX_*) 362 setupDialer(); 363 setupCallLog(); 364 setupFavorites(); 365 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 366 getActionBar().setDisplayShowTitleEnabled(false); 367 getActionBar().setDisplayShowHomeEnabled(false); 368 369 // Load the last manually loaded tab 370 final SharedPreferences prefs = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE); 371 mLastManuallySelectedFragment = prefs.getInt(PREF_LAST_MANUALLY_SELECTED_TAB, 372 PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT); 373 if (mLastManuallySelectedFragment >= TAB_INDEX_COUNT) { 374 // Stored value may have exceeded the number of current tabs. Reset it. 375 mLastManuallySelectedFragment = PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT; 376 } 377 378 setCurrentTab(intent); 379 380 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction()) 381 && icicle == null) { 382 setupFilterText(intent); 383 } 384 } 385 386 @Override 387 public void onAttachFragment(Fragment fragment) { 388 // This method can be called before onCreate(), at which point we cannot rely on ViewPager. 389 // In that case, we will setup the "current position" soon after the ViewPager is ready. 390 final int currentPosition = mViewPager != null ? mViewPager.getCurrentItem() : -1; 391 392 if (fragment instanceof DialpadFragment) { 393 mDialpadFragment = (DialpadFragment) fragment; 394 mDialpadFragment.setListener(mDialpadListener); 395 if (currentPosition == TAB_INDEX_DIALER) { 396 mDialpadFragment.onVisibilityChanged(true); 397 } 398 } else if (fragment instanceof CallLogFragment) { 399 mCallLogFragment = (CallLogFragment) fragment; 400 if (currentPosition == TAB_INDEX_CALL_LOG) { 401 mCallLogFragment.onVisibilityChanged(true); 402 } 403 } else if (fragment instanceof ContactTileListFragment) { 404 mStrequentFragment = (ContactTileListFragment) fragment; 405 mStrequentFragment.enableQuickContact(false); 406 mStrequentFragment.setListener(mStrequentListener); 407 mStrequentFragment.setDisplayType(DisplayType.STREQUENT_PHONE_ONLY); 408 } else if (fragment instanceof PhoneNumberPickerFragment) { 409 mSearchFragment = (PhoneNumberPickerFragment) fragment; 410 mSearchFragment.setOnPhoneNumberPickerActionListener(mPhoneNumberPickerActionListener); 411 mSearchFragment.setQuickContactEnabled(true); 412 final FragmentTransaction transaction = getFragmentManager().beginTransaction(); 413 if (mInSearchUi) { 414 transaction.show(mSearchFragment); 415 } else { 416 transaction.hide(mSearchFragment); 417 } 418 transaction.commit(); 419 } 420 } 421 422 @Override 423 protected void onPause() { 424 super.onPause(); 425 426 final SharedPreferences.Editor editor = 427 getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE).edit(); 428 editor.putInt(PREF_LAST_MANUALLY_SELECTED_TAB, mLastManuallySelectedFragment); 429 430 editor.apply(); 431 } 432 433 private void fixIntent(Intent intent) { 434 // This should be cleaned up: the call key used to send an Intent 435 // that just said to go to the recent calls list. It now sends this 436 // abstract action, but this class hasn't been rewritten to deal with it. 437 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) { 438 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE); 439 intent.putExtra("call_key", true); 440 setIntent(intent); 441 } 442 } 443 444 private void setupDialer() { 445 final Tab tab = getActionBar().newTab(); 446 // TODO: Temporarily disable tab text labels (in all 4 tabs in this 447 // activity) so that the current tabs will all fit onscreen in 448 // portrait (bug 4520620). (Also note we do setText("") rather 449 // leaving the text null, to work around bug 4521549.) 450 tab.setText(""); // R.string.dialerIconLabel 451 tab.setTabListener(mTabListener); 452 tab.setIcon(R.drawable.ic_tab_dialer); 453 getActionBar().addTab(tab); 454 } 455 456 private void setupCallLog() { 457 final Tab tab = getActionBar().newTab(); 458 tab.setText(""); // R.string.recentCallsIconLabel 459 tab.setIcon(R.drawable.ic_tab_recent); 460 tab.setTabListener(mTabListener); 461 getActionBar().addTab(tab); 462 } 463 464 private void setupFavorites() { 465 final Tab tab = getActionBar().newTab(); 466 tab.setText(""); // R.string.contactsFavoritesLabel 467 tab.setIcon(R.drawable.ic_tab_starred); 468 tab.setTabListener(mTabListener); 469 getActionBar().addTab(tab); 470 } 471 472 /** 473 * Returns true if the intent is due to hitting the green send key while in a call. 474 * 475 * @param intent the intent that launched this activity 476 * @param recentCallsRequest true if the intent is requesting to view recent calls 477 * @return true if the intent is due to hitting the green send key while in a call 478 */ 479 private boolean isSendKeyWhileInCall(final Intent intent, 480 final boolean recentCallsRequest) { 481 // If there is a call in progress go to the call screen 482 if (recentCallsRequest) { 483 final boolean callKey = intent.getBooleanExtra("call_key", false); 484 485 try { 486 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); 487 if (callKey && phone != null && phone.showCallScreen()) { 488 return true; 489 } 490 } catch (RemoteException e) { 491 Log.e(TAG, "Failed to handle send while in call", e); 492 } 493 } 494 495 return false; 496 } 497 498 /** 499 * Sets the current tab based on the intent's request type 500 * 501 * @param intent Intent that contains information about which tab should be selected 502 */ 503 private void setCurrentTab(Intent intent) { 504 // If we got here by hitting send and we're in call forward along to the in-call activity 505 final boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.getType()); 506 if (isSendKeyWhileInCall(intent, recentCallsRequest)) { 507 finish(); 508 return; 509 } 510 511 // Remember the old manually selected tab index so that it can be restored if it is 512 // overwritten by one of the programmatic tab selections 513 final int savedTabIndex = mLastManuallySelectedFragment; 514 515 final int tabIndex; 516 if (DialpadFragment.phoneIsInUse() || isDialIntent(intent)) { 517 tabIndex = TAB_INDEX_DIALER; 518 } else if (recentCallsRequest) { 519 tabIndex = TAB_INDEX_CALL_LOG; 520 } else { 521 tabIndex = mLastManuallySelectedFragment; 522 } 523 mViewPager.setCurrentItem(tabIndex, false /* smoothScroll */); 524 if (mViewPager.getCurrentItem() == tabIndex) { 525 mPageChangeListener.setCurrentPosition(tabIndex); 526 sendFragmentVisibilityChange(tabIndex, true); 527 } else { 528 getActionBar().selectTab(getActionBar().getTabAt(tabIndex)); 529 } 530 531 // Restore to the previous manual selection 532 mLastManuallySelectedFragment = savedTabIndex; 533 } 534 535 @Override 536 public void onNewIntent(Intent newIntent) { 537 setIntent(newIntent); 538 fixIntent(newIntent); 539 setCurrentTab(newIntent); 540 final String action = newIntent.getAction(); 541 if (UI.FILTER_CONTACTS_ACTION.equals(action)) { 542 setupFilterText(newIntent); 543 } 544 if (mInSearchUi || mSearchFragment.isVisible()) { 545 exitSearchUi(); 546 } 547 548 if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { 549 if (mDialpadFragment != null) { 550 mDialpadFragment.configureScreenFromIntent(newIntent); 551 } else { 552 Log.e(TAG, "DialpadFragment isn't ready yet when the tab is already selected."); 553 } 554 } 555 invalidateOptionsMenu(); 556 } 557 558 /** Returns true if the given intent contains a phone number to populate the dialer with */ 559 private boolean isDialIntent(Intent intent) { 560 final String action = intent.getAction(); 561 if (Intent.ACTION_DIAL.equals(action)) { 562 return true; 563 } 564 if (Intent.ACTION_VIEW.equals(action)) { 565 final Uri data = intent.getData(); 566 if (data != null && "tel".equals(data.getScheme())) { 567 return true; 568 } 569 } 570 return false; 571 } 572 573 /** 574 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}. 575 * This text originally came from a FILTER_CONTACTS_ACTION intent received 576 * by this activity. The stored text will then be cleared after after this 577 * method returns. 578 * 579 * @return The stored filter text 580 */ 581 public String getAndClearFilterText() { 582 String filterText = mFilterText; 583 mFilterText = null; 584 return filterText; 585 } 586 587 /** 588 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent. 589 * This is so child activities can check if they are supposed to display a filter. 590 * 591 * @param intent The intent received in {@link #onNewIntent(Intent)} 592 */ 593 private void setupFilterText(Intent intent) { 594 // If the intent was relaunched from history, don't apply the filter text. 595 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { 596 return; 597 } 598 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY); 599 if (filter != null && filter.length() > 0) { 600 mFilterText = filter; 601 } 602 } 603 604 @Override 605 public void onBackPressed() { 606 if (mInSearchUi) { 607 // We should let the user go back to usual screens with tabs. 608 exitSearchUi(); 609 } else if (isTaskRoot()) { 610 // Instead of stopping, simply push this to the back of the stack. 611 // This is only done when running at the top of the stack; 612 // otherwise, we have been launched by someone else so need to 613 // allow the user to go back to the caller. 614 moveTaskToBack(false); 615 } else { 616 super.onBackPressed(); 617 } 618 } 619 620 private DialpadFragment.Listener mDialpadListener = new DialpadFragment.Listener() { 621 @Override 622 public void onSearchButtonPressed() { 623 enterSearchUi(); 624 } 625 }; 626 627 private ContactTileListFragment.Listener mStrequentListener = 628 new ContactTileListFragment.Listener() { 629 @Override 630 public void onContactSelected(Uri contactUri) { 631 PhoneNumberInteraction.startInteractionForPhoneCall( 632 DialtactsActivity.this, contactUri); 633 } 634 }; 635 636 @Override 637 public boolean onCreateOptionsMenu(Menu menu) { 638 MenuInflater inflater = getMenuInflater(); 639 inflater.inflate(R.menu.dialtacts_options, menu); 640 return true; 641 } 642 643 @Override 644 public boolean onPrepareOptionsMenu(Menu menu) { 645 final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar); 646 final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option); 647 Tab tab = getActionBar().getSelectedTab(); 648 if (mInSearchUi) { 649 searchMenuItem.setVisible(false); 650 filterOptionMenuItem.setVisible(true); 651 filterOptionMenuItem.setOnMenuItemClickListener( 652 mFilterOptionsMenuItemClickListener); 653 } else if (tab == null || tab.getPosition() == TAB_INDEX_DIALER) { 654 searchMenuItem.setVisible(false); 655 filterOptionMenuItem.setVisible(false); 656 } else { 657 filterOptionMenuItem.setVisible(false); 658 searchMenuItem.setVisible(true); 659 searchMenuItem.setOnMenuItemClickListener(mSearchMenuItemClickListener); 660 } 661 662 return true; 663 } 664 665 @Override 666 public void startSearch(String initialQuery, boolean selectInitialQuery, 667 Bundle appSearchData, boolean globalSearch) { 668 if (mSearchFragment != null && mSearchFragment.isAdded() && !globalSearch) { 669 enterSearchUi(); 670 } else { 671 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); 672 } 673 } 674 675 /** 676 * Hides every tab and shows search UI for phone lookup. 677 */ 678 private void enterSearchUi() { 679 final ActionBar actionBar = getActionBar(); 680 681 final Tab tab = actionBar.getSelectedTab(); 682 683 // User can search during the call, but we don't want to remember the status. 684 if (tab != null && !DialpadFragment.phoneIsInUse()) { 685 mLastManuallySelectedFragment = tab.getPosition(); 686 } 687 688 // Instantiate or reset SearchView in ActionBar. 689 if (mSearchView == null) { 690 final View searchViewLayout = 691 getLayoutInflater().inflate(R.layout.custom_action_bar, null); 692 mSearchView = (SearchView) searchViewLayout.findViewById(R.id.search_view); 693 mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener); 694 mSearchView.setOnCloseListener(mPhoneSearchCloseListener); 695 // Since we're using a custom layout for showing SearchView instead of letting the 696 // search menu icon do that job, we need to manually configure the View so it looks 697 // "shown via search menu". 698 // - it should be iconified by default 699 // - it should not be iconified at this time 700 // See also comments for onActionViewExpanded()/onActionViewCollapsed() 701 mSearchView.setIconifiedByDefault(true); 702 mSearchView.setQueryHint(getString(R.string.hint_findContacts)); 703 mSearchView.setIconified(false); 704 mSearchView.requestFocus(); 705 // Show soft keyboard when SearchView has a focus. Need to delay the request in order 706 // to let InputMethodManager handle it correctly. 707 mSearchView.addOnAttachStateChangeListener(new OnAttachStateChangeListener() { 708 @Override 709 public void onViewDetachedFromWindow(View v) { 710 } 711 712 @Override 713 public void onViewAttachedToWindow(View v) { 714 if (mSearchView.hasFocus()) { 715 mSearchView.postDelayed(new Runnable() { 716 public void run() { 717 showInputMethod(mSearchView.findFocus()); 718 } 719 }, 0); 720 } 721 } 722 }); 723 actionBar.setCustomView(searchViewLayout, 724 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 725 } else { 726 mSearchView.setQuery(null, true); 727 } 728 729 actionBar.setDisplayShowCustomEnabled(true); 730 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 731 actionBar.setDisplayShowHomeEnabled(true); 732 actionBar.setDisplayHomeAsUpEnabled(true); 733 734 sendFragmentVisibilityChange(mViewPager.getCurrentItem(), false); 735 736 // Show the search fragment and hide everything else. 737 final FragmentTransaction transaction = getFragmentManager().beginTransaction(); 738 transaction.show(mSearchFragment); 739 transaction.commit(); 740 mViewPager.setVisibility(View.GONE); 741 742 // We need to call this and onActionViewCollapsed() manually, since we are using a custom 743 // layout instead of asking the search menu item to take care of SearchView. 744 mSearchView.onActionViewExpanded(); 745 mInSearchUi = true; 746 } 747 748 private void showInputMethod(View view) { 749 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 750 if (imm != null) { 751 imm.showSoftInput(view, 0); 752 } 753 } 754 755 private void hideInputMethod(View view) { 756 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 757 if (imm != null) { 758 imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 759 } 760 } 761 762 /** 763 * Goes back to usual Phone UI with tags. Previously selected Tag and associated Fragment 764 * should be automatically focused again. 765 */ 766 private void exitSearchUi() { 767 final ActionBar actionBar = getActionBar(); 768 769 final FragmentTransaction transaction = getFragmentManager().beginTransaction(); 770 transaction.hide(mSearchFragment); 771 transaction.commit(); 772 773 // We want to hide SearchView and show Tabs. Also focus on previously selected one. 774 actionBar.setDisplayShowCustomEnabled(false); 775 actionBar.setDisplayShowHomeEnabled(false); 776 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 777 778 sendFragmentVisibilityChange(mViewPager.getCurrentItem(), true); 779 780 mViewPager.setVisibility(View.VISIBLE); 781 782 hideInputMethod(getCurrentFocus()); 783 784 // Request to update option menu. 785 invalidateOptionsMenu(); 786 787 // See comments in onActionViewExpanded() 788 mSearchView.onActionViewCollapsed(); 789 mInSearchUi = false; 790 } 791 792 private Fragment getFragmentAt(int position) { 793 switch (position) { 794 case TAB_INDEX_DIALER: 795 return mDialpadFragment; 796 case TAB_INDEX_CALL_LOG: 797 return mCallLogFragment; 798 case TAB_INDEX_FAVORITES: 799 return mStrequentFragment; 800 default: 801 throw new IllegalStateException("Unknown fragment index: " + position); 802 } 803 } 804 805 private void sendFragmentVisibilityChange(int position, boolean visibility) { 806 final Fragment fragment = getFragmentAt(position); 807 if (fragment instanceof ViewPagerVisibilityListener) { 808 ((ViewPagerVisibilityListener) fragment).onVisibilityChanged(visibility); 809 } 810 } 811 812 /** Returns an Intent to launch Call Settings screen */ 813 public static Intent getCallSettingsIntent() { 814 final Intent intent = new Intent(Intent.ACTION_MAIN); 815 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME); 816 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 817 return intent; 818 } 819 820 @Override 821 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 822 if (resultCode != Activity.RESULT_OK) { 823 return; 824 } 825 switch (requestCode) { 826 case SUBACTIVITY_ACCOUNT_FILTER: { 827 ContactListFilter filter = (ContactListFilter) data.getParcelableExtra( 828 AccountFilterActivity.KEY_EXTRA_CONTACT_LIST_FILTER); 829 if (filter == null) { 830 return; 831 } 832 if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) { 833 mContactListFilterController.selectCustomFilter(); 834 } else { 835 mContactListFilterController.setContactListFilter(filter, true); 836 } 837 } 838 break; 839 } 840 } 841} 842