/** Creates the fragment based on the current request. */
  public void configureListFragment() {
    switch (mActionCode) {
      case ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT:
        {
          ContactPickerFragment fragment = new ContactPickerFragment();
          fragment.setEditMode(true);
          fragment.setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_NONE);
          mListFragment = fragment;
          break;
        }

      case ContactsRequest.ACTION_PICK_CONTACT:
        {
          ContactPickerFragment fragment = new ContactPickerFragment();
          fragment.setIncludeProfile(mRequest.shouldIncludeProfile());
          mListFragment = fragment;
          break;
        }

      case ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT:
        {
          mListFragment = new ContactPickerFragment();
          break;
        }

      case ContactsRequest.ACTION_CREATE_SHORTCUT_CONTACT:
        {
          ContactPickerFragment fragment = new ContactPickerFragment();
          fragment.setShortcutRequested(true);
          mListFragment = fragment;
          break;
        }

      case ContactsRequest.ACTION_PICK_PHONE:
        {
          PhoneNumberPickerFragment fragment = new PhoneNumberPickerFragment();
          fragment.setUseCallableUri(true);
          mListFragment = fragment;
          break;
        }

      case ContactsRequest.ACTION_PICK_EMAIL:
        {
          mListFragment = new EmailAddressPickerFragment();
          break;
        }

      case ContactsRequest.ACTION_CREATE_SHORTCUT_CALL:
        {
          PhoneNumberPickerFragment fragment = new PhoneNumberPickerFragment();
          fragment.setShortcutAction(Intent.ACTION_CALL);

          mListFragment = fragment;
          break;
        }

      case ContactsRequest.ACTION_CREATE_SHORTCUT_SMS:
        {
          PhoneNumberPickerFragment fragment = new PhoneNumberPickerFragment();
          fragment.setShortcutAction(Intent.ACTION_SENDTO);
          mListFragment = fragment;
          break;
        }

        //            case ContactsRequest.ACTION_PICK_POSTAL: {
        //                PostalAddressPickerFragment fragment = new PostalAddressPickerFragment();
        //                mListFragment = fragment;
        //                break;
        //            }

      default:
        throw new IllegalStateException("Invalid action code: " + mActionCode);
    }

    mListFragment.setLegacyCompatibilityMode(mRequest.isLegacyCompatibilityMode());
    mListFragment.setDirectoryResultLimit(DEFAULT_DIRECTORY_RESULT_LIMIT);

    getFragmentManager()
        .beginTransaction()
        .replace(R.id.list_container, mListFragment)
        .commitAllowingStateLoss();
  }
  private void prepareSearchViewAndActionBar() {
    // Postal address pickers (and legacy pickers) don't support search, so just show
    // "HomeAsUp" button and title.
    if (mRequest.getActionCode() == ContactsRequest.ACTION_PICK_POSTAL
        || mRequest.isLegacyCompatibilityMode()) {
      findViewById(R.id.search_view).setVisibility(View.GONE);
      final ActionBar actionBar = getSupportActionBar();
      if (actionBar != null) {
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
      }
      return;
    }

    // If ActionBar is available, show SearchView on it. If not, show SearchView inside the
    // Activity's layout.
    final ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
      final View searchViewOnLayout = findViewById(R.id.search_view);
      if (searchViewOnLayout != null) {
        searchViewOnLayout.setVisibility(View.GONE);
      }

      @SuppressLint("InflateParams")
      final View searchViewContainer =
          LayoutInflater.from(actionBar.getThemedContext())
              .inflate(R.layout.custom_action_bar, null);
      mSearchView = (SearchView) searchViewContainer.findViewById(R.id.search_view);

      // In order to make the SearchView look like "shown via search menu", we need to
      // manually setup its state.
      mSearchView.setIconifiedByDefault(true);
      mSearchView.setQueryHint(getString(R.string.hint_findContacts));
      mSearchView.setIconified(false);

      mSearchView.setOnQueryTextListener(this);
      mSearchView.setOnCloseListener(this);
      mSearchView.setOnQueryTextFocusChangeListener(this);

      actionBar.setCustomView(
          searchViewContainer,
          new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      actionBar.setDisplayShowCustomEnabled(true);
      actionBar.setDisplayShowHomeEnabled(true);
      actionBar.setDisplayHomeAsUpEnabled(true);
    } else {
      mSearchView = (SearchView) findViewById(R.id.search_view);
      mSearchView.setQueryHint(getString(R.string.hint_findContacts));
      mSearchView.setOnQueryTextListener(this);

      // This is a hack to prevent the search view from grabbing focus
      // at this point.  If search view were visible, it would always grabs focus
      // because it is the first focusable widget in the window.
      mSearchView.setVisibility(View.INVISIBLE);
      mSearchView.postDelayed(
          new Runnable() {
            @Override
            public void run() {
              mSearchView.setVisibility(View.VISIBLE);
            }
          },
          FOCUS_DELAY);
    }

    // Clear focus and suppress keyboard show-up.
    mSearchView.clearFocus();
  }