/**
   * Goes back to usual Phone UI with tags. Previously selected Tag and associated Fragment should
   * be automatically focused again.
   */
  private void exitSearchUi() {
    final ActionBar actionBar = getActionBar();

    // Hide the search fragment, if exists.
    if (mSearchFragment != null) {
      mSearchFragment.setUserVisibleHint(false);

      final FragmentTransaction transaction = getFragmentManager().beginTransaction();
      transaction.hide(mSearchFragment);
      transaction.commitAllowingStateLoss();
    }

    // We want to hide SearchView and show Tabs. Also focus on previously selected one.
    actionBar.setDisplayShowCustomEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    sendFragmentVisibilityChange(mViewPager.getCurrentItem(), true);

    // Before exiting the search screen, reset swipe state.
    mDuringSwipe = false;
    mUserTabClick = false;

    mViewPager.setVisibility(View.VISIBLE);

    hideInputMethod(getCurrentFocus());

    // Request to update option menu.
    invalidateOptionsMenu();

    // See comments in onActionViewExpanded()
    mSearchView.onActionViewCollapsed();
    mInSearchUi = false;
  }
 @Override
 public boolean onQueryTextChange(String newText) {
   // Show search result with non-empty text. Show a bare list otherwise.
   if (mSearchFragment != null) {
     mSearchFragment.setQueryString(newText, true);
   }
   return true;
 }
  @Override
  public void onAttachFragment(Fragment fragment) {
    // This method can be called before onCreate(), at which point we cannot rely on ViewPager.
    // In that case, we will setup the "current position" soon after the ViewPager is ready.
    final int currentPosition = mViewPager != null ? mViewPager.getCurrentItem() : -1;

    if (fragment instanceof DialpadFragment) {
      mDialpadFragment = (DialpadFragment) fragment;
      mDialpadFragment.setListener(mDialpadListener);
      if (currentPosition == TAB_INDEX_DIALER) {
        mDialpadFragment.onVisibilityChanged(true);
      }
    } else if (fragment instanceof CallLogFragment) {
      mCallLogFragment = (CallLogFragment) fragment;
      if (currentPosition == TAB_INDEX_CALL_LOG) {
        mCallLogFragment.onVisibilityChanged(true);
      }
    } else if (fragment instanceof PhoneFavoriteFragment) {
      mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
      mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
      if (mContactListFilterController != null
          && mContactListFilterController.getFilter() != null) {
        mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter());
      }
    } else if (fragment instanceof PhoneNumberPickerFragment) {
      mSearchFragment = (PhoneNumberPickerFragment) fragment;
      mSearchFragment.setOnPhoneNumberPickerActionListener(mPhoneNumberPickerActionListener);
      mSearchFragment.setQuickContactEnabled(true);
      mSearchFragment.setDarkTheme(true);
      mSearchFragment.setPhotoPosition(ContactListItemView.PhotoPosition.LEFT);
      if (mContactListFilterController != null
          && mContactListFilterController.getFilter() != null) {
        mSearchFragment.setFilter(mContactListFilterController.getFilter());
      }
      // Here we assume that we're not on the search mode, so let's hide the fragment.
      //
      // We get here either when the fragment is created (normal case), or after configuration
      // changes.  In the former case, we're not in search mode because we can only
      // enter search mode if the fragment is created.  (see enterSearchUi())
      // In the latter case we're not in search mode either because we don't retain
      // mInSearchUi -- ideally we should but at this point it's not supported.
      mSearchFragment.setUserVisibleHint(false);
      // After configuration changes fragments will forget their "hidden" state, so make
      // sure to hide it.
      if (!mSearchFragment.isHidden()) {
        final FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.hide(mSearchFragment);
        transaction.commitAllowingStateLoss();
      }
    }
  }
  @Override
  public void onStart() {
    super.onStart();
    if (mPhoneFavoriteFragment != null) {
      mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter());
    }
    if (mSearchFragment != null) {
      mSearchFragment.setFilter(mContactListFilterController.getFilter());
    }

    if (mDuringSwipe || mUserTabClick) {
      if (DEBUG) Log.d(TAG, "reset buggy flag state..");
      mDuringSwipe = false;
      mUserTabClick = false;
    }
  }
 @Override
 public void startSearch(
     String initialQuery, boolean selectInitialQuery, Bundle appSearchData, boolean globalSearch) {
   if (mSearchFragment != null && mSearchFragment.isAdded() && !globalSearch) {
     if (mInSearchUi) {
       if (mSearchView.hasFocus()) {
         showInputMethod(mSearchView.findFocus());
       } else {
         mSearchView.requestFocus();
       }
     } else {
       enterSearchUi();
     }
   } else {
     super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
   }
 }
  /** Hides every tab and shows search UI for phone lookup. */
  private void enterSearchUi() {
    if (mSearchFragment == null) {
      // We add the search fragment dynamically in the first onLayoutChange() and
      // mSearchFragment is set sometime later when the fragment transaction is actually
      // executed, which means there's a window when users are able to hit the (physical)
      // search key but mSearchFragment is still null.
      // It's quite hard to handle this case right, so let's just ignore the search key
      // in this case.  Users can just hit it again and it will work this time.
      return;
    }
    if (mSearchView == null) {
      prepareSearchView();
    }

    final ActionBar actionBar = getActionBar();

    final Tab tab = actionBar.getSelectedTab();

    // User can search during the call, but we don't want to remember the status.
    if (tab != null && !DialpadFragment.phoneIsInUse()) {
      mLastManuallySelectedFragment = tab.getPosition();
    }

    mSearchView.setQuery(null, true);

    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);

    sendFragmentVisibilityChange(mViewPager.getCurrentItem(), false);

    // Show the search fragment and hide everything else.
    mSearchFragment.setUserVisibleHint(true);
    final FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.show(mSearchFragment);
    transaction.commitAllowingStateLoss();
    mViewPager.setVisibility(View.GONE);

    // We need to call this and onActionViewCollapsed() manually, since we are using a custom
    // layout instead of asking the search menu item to take care of SearchView.
    mSearchView.onActionViewExpanded();
    mInSearchUi = true;
  }
  @Override
  public void onNewIntent(Intent newIntent) {
    setIntent(newIntent);
    fixIntent(newIntent);
    setCurrentTab(newIntent);
    final String action = newIntent.getAction();
    if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
      setupFilterText(newIntent);
    }
    if (mInSearchUi || (mSearchFragment != null && mSearchFragment.isVisible())) {
      exitSearchUi();
    }

    if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) {
      if (mDialpadFragment != null) {
        mDialpadFragment.configureScreenFromIntent(newIntent);
      } else {
        Log.e(TAG, "DialpadFragment isn't ready yet when the tab is already selected.");
      }
    }
    invalidateOptionsMenu();
  }