@Override
  protected void onCreate(Bundle savedState) {
    super.onCreate(savedState);

    ScContactsDatabaseHelper db = ScContactsDatabaseHelper.getInstance(getBaseContext());
    boolean dbReady = db.isRegisteredWithKeyManager() && db.isReady();
    if (!dbReady) {
      final String msg = getString(R.string.app_name) + ": " + getString(R.string.locked);
      Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
      finish();
      return;
    }

    if (savedState != null) {
      mActionCode = savedState.getInt(KEY_ACTION_CODE);
    }

    // Extract relevant information from the intent
    mRequest = mIntentResolver.resolveIntent(getIntent());
    if (!mRequest.isValid()) {
      setResult(RESULT_CANCELED);
      finish();
      return;
    }

    Intent redirect = mRequest.getRedirectIntent();
    if (redirect != null) {
      // Need to start a different activity
      startActivity(redirect);
      finish();
      return;
    }

    configureActivityTitle();

    setContentView(R.layout.contact_picker);
    setSupportActionBar((Toolbar) findViewById(R.id.main_toolbar));

    if (mActionCode != mRequest.getActionCode()) {
      mActionCode = mRequest.getActionCode();
      configureListFragment();
    }

    prepareSearchViewAndActionBar();

    mCreateNewContactButton = findViewById(R.id.new_contact);
    if (mCreateNewContactButton != null) {
      if (shouldShowCreateNewContactButton()) {
        mCreateNewContactButton.setVisibility(View.VISIBLE);
        mCreateNewContactButton.setOnClickListener(this);
      } else {
        mCreateNewContactButton.setVisibility(View.GONE);
      }
    }
  }
  /** 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 configureActivityTitle() {
    if (!TextUtils.isEmpty(mRequest.getActivityTitle())) {
      setTitle(mRequest.getActivityTitle());
      return;
    }

    int actionCode = mRequest.getActionCode();
    switch (actionCode) {
      case ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT:
        {
          setTitle(R.string.contactPickerActivityTitle);
          break;
        }

      case ContactsRequest.ACTION_PICK_CONTACT:
        {
          setTitle(R.string.contactPickerActivityTitle);
          break;
        }

      case ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT:
        {
          setTitle(R.string.contactPickerActivityTitle);
          break;
        }

      case ContactsRequest.ACTION_CREATE_SHORTCUT_CONTACT:
        {
          setTitle(R.string.shortcutActivityTitle);
          break;
        }

      case ContactsRequest.ACTION_PICK_PHONE:
        {
          setTitle(R.string.contactPickerActivityTitle);
          break;
        }

      case ContactsRequest.ACTION_PICK_EMAIL:
        {
          setTitle(R.string.contactPickerActivityTitle);
          break;
        }

      case ContactsRequest.ACTION_CREATE_SHORTCUT_CALL:
        {
          setTitle(R.string.callShortcutActivityTitle);
          break;
        }

      case ContactsRequest.ACTION_CREATE_SHORTCUT_SMS:
        {
          setTitle(R.string.messageShortcutActivityTitle);
          break;
        }

      case ContactsRequest.ACTION_PICK_POSTAL:
        {
          setTitle(R.string.contactPickerActivityTitle);
          break;
        }
    }
  }
  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();
  }
 private boolean shouldShowCreateNewContactButton() {
   return (mActionCode == ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT
       || (mActionCode == ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT
           && !mRequest.isSearchMode()));
 }