@TargetApi(11)
  private void registerContextualActionBarForUserList() {
    // TODO: Consider switching to a single choice list when its action mode state starts to
    // automatically survive configuration (e.g., orientation) changes.
    //
    // Since action mode does not currently survive orientation changes in single choice mode, we
    // use multiple choice mode instead while still enforcing that only one item is checked at any
    // time.
    userList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    final AbsListView.MultiChoiceModeListener multiChoiceModeListener =
        new AbsListView.MultiChoiceModeListener() {
          @Override
          public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
          }

          @Override
          public void onDestroyActionMode(ActionMode mode) {
            actionMode = null;
          }

          @Override
          public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            actionMode = mode;
            getMenuInflater().inflate(R.menu.account_list_context, menu);

            // This method is invoked both when the user starts the CAB and when the CAB is
            // recreated
            // after an orientation change. In the former case, no list items are checked. In the
            // latter, one of the items is checked.
            // Unfortunately, the Android framework does not preserve the state of the menu after
            // orientation changes. Thus, we need to update the menu.
            if (userList.getCheckedItemCount() > 0) {
              // Assume only one item can be checked, and blow up otherwise
              int position = getMultiSelectListSingleCheckedItemPosition(userList);
              updateCabForAccount(mode, menu, users[position]);
            }

            return true;
          }

          @Override
          public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            // This function can be triggered twice as user can double click the item while it is on
            // the
            // dismissing animation, we ignore the second click by checking if the item is
            // unselected.

            if (userList.getCheckedItemCount() == 0) {
              return false;
            }
            int position = getMultiSelectListSingleCheckedItemPosition(userList);
            if (onContextItemSelected(item, position)) {
              mode.finish();
              return true;
            }
            return false;
          }

          @Override
          public void onItemCheckedStateChanged(
              ActionMode mode, int position, long id, boolean checked) {
            if (checked) {
              mode.setTitle(users[position].getIndex().getStrippedName());

              // Ensure that only one item is checked, by unchecking all other checked items.
              SparseBooleanArray checkedItemPositions = userList.getCheckedItemPositions();
              for (int i = 0, len = userList.getCount(); i < len; i++) {
                if (i == position) {
                  continue;
                }
                boolean itemChecked = checkedItemPositions.get(i);
                if (itemChecked) {
                  userList.setItemChecked(i, false);
                }
              }

              updateCabForAccount(mode, mode.getMenu(), users[position]);
              userAdapter.notifyDataSetChanged();
            }
          }

          private void updateCabForAccount(ActionMode mode, Menu menu, PinInfo account) {
            mode.setTitle(account.getIndex().getStrippedName());
            updateMenuForAccount(menu, account);
            copyCodeToClipboard(account);
          }

          private void updateMenuForAccount(Menu menu, PinInfo account) {
            MenuItem copyMenuItem = menu.findItem(R.id.copy);
            if (copyMenuItem != null) {
              copyMenuItem.setVisible(false);
            }

            MenuItem renameMenuItem = menu.findItem(R.id.rename);
            if (renameMenuItem != null) {
              renameMenuItem.setVisible(isRenameAccountAvailableSupported(account));
            }

            MenuItem checkKeyValueMenuItem = menu.findItem(R.id.check_code);
            if (checkKeyValueMenuItem != null) {
              checkKeyValueMenuItem.setVisible(isCheckAccountKeyValueSupported(account));
            }
          }

          private void copyCodeToClipboard(PinInfo account) {
            copyStringToClipboard(userList.getContext(), account.getPin());
            Toast.makeText(
                    userList.getContext(), R.string.copied_to_clipboard_toast, Toast.LENGTH_SHORT)
                .show();
          }
        };
    userList.setMultiChoiceModeListener(multiChoiceModeListener);
  }
Called when the activity is first created.
java/com/google/android/apps/authenticator/AuthenticatorActivity.java