@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);
      }
    }
  }
  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();
  }