private void handleStartSecureSession() {
    if (getRecipients() == null) {
      Toast.makeText(
              this, getString(R.string.ConversationActivity_invalid_recipient), Toast.LENGTH_LONG)
          .show();
      return;
    }

    final Recipient recipient = getRecipients().getPrimaryRecipient();
    String recipientName =
        (recipient.getName() == null ? recipient.getNumber() : recipient.getName());
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.ConversationActivity_initiate_secure_session_question);
    builder.setIcon(Dialogs.resolveIcon(this, R.attr.dialog_info_icon));
    builder.setCancelable(true);
    builder.setMessage(
        String.format(
            getString(R.string.ConversationActivity_initiate_secure_session_with_s_question),
            recipientName));
    builder.setPositiveButton(
        R.string.yes,
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            KeyExchangeInitiator.initiate(ConversationActivity.this, masterSecret, recipient, true);
          }
        });

    builder.setNegativeButton(R.string.no, null);
    builder.show();
  }
  private void handleDeleteAllSelected() {
    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
    alert.setIcon(Dialogs.resolveIcon(getActivity(), R.attr.dialog_alert_icon));
    alert.setTitle(R.string.ConversationListFragment_delete_threads_question);
    alert.setMessage(
        R.string
            .ConversationListFragment_are_you_sure_you_wish_to_delete_all_selected_conversation_threads);
    alert.setCancelable(true);

    alert.setPositiveButton(
        R.string.delete,
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            final Set<Long> selectedConversations =
                ((ConversationListAdapter) getListAdapter()).getBatchSelections();

            if (!selectedConversations.isEmpty()) {
              new AsyncTask<Void, Void, Void>() {
                private ProgressDialog dialog;

                @Override
                protected void onPreExecute() {
                  dialog =
                      ProgressDialog.show(
                          getActivity(),
                          getSherlockActivity()
                              .getString(R.string.ConversationListFragment_deleting),
                          getSherlockActivity()
                              .getString(
                                  R.string.ConversationListFragment_deleting_selected_threads),
                          true,
                          false);
                }

                @Override
                protected Void doInBackground(Void... params) {
                  DatabaseFactory.getThreadDatabase(getActivity())
                      .deleteConversations(selectedConversations);
                  MessageNotifier.updateNotification(getActivity(), masterSecret);
                  return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                  dialog.dismiss();
                  if (actionMode != null) {
                    actionMode.finish();
                    actionMode = null;
                  }
                }
              }.execute();
            }
          }
        });

    alert.setNegativeButton(android.R.string.cancel, null);
    alert.show();
  }
  private void handleLeavePushGroup() {
    if (getRecipients() == null) {
      Toast.makeText(
              this, getString(R.string.ConversationActivity_invalid_recipient), Toast.LENGTH_LONG)
          .show();
      return;
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.ConversationActivity_leave_group));
    builder.setIcon(Dialogs.resolveIcon(this, R.attr.dialog_info_icon));
    builder.setCancelable(true);
    builder.setMessage(
        getString(R.string.ConversationActivity_are_you_sure_you_want_to_leave_this_group));
    builder.setPositiveButton(
        R.string.yes,
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            try {
              Context self = ConversationActivity.this;
              byte[] groupId =
                  GroupUtil.getDecodedId(getRecipients().getPrimaryRecipient().getNumber());
              DatabaseFactory.getGroupDatabase(self).setActive(groupId, false);

              GroupContext context =
                  GroupContext.newBuilder()
                      .setId(ByteString.copyFrom(groupId))
                      .setType(GroupContext.Type.QUIT)
                      .build();

              OutgoingGroupMediaMessage outgoingMessage =
                  new OutgoingGroupMediaMessage(self, getRecipients(), context, null);
              MessageSender.send(self, masterSecret, outgoingMessage, threadId, false);
              DatabaseFactory.getGroupDatabase(self)
                  .remove(groupId, TextSecurePreferences.getLocalNumber(self));
              initializeEnabledCheck();
            } catch (IOException e) {
              Log.w(TAG, e);
              Toast.makeText(
                      ConversationActivity.this, "Error leaving group....", Toast.LENGTH_LONG)
                  .show();
            } catch (MmsException e) {
              Log.w(TAG, e);
              Toast.makeText(ConversationActivity.this, "Error leaving group...", Toast.LENGTH_LONG)
                  .show();
            }
          }
        });

    builder.setNegativeButton(R.string.no, null);
    builder.show();
  }
  private void handleDial(Recipient recipient) {
    try {
      if (recipient == null) return;

      Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + recipient.getNumber()));
      startActivity(dialIntent);
    } catch (ActivityNotFoundException anfe) {
      Log.w(TAG, anfe);
      Dialogs.showAlertDialog(
          this,
          getString(R.string.ConversationActivity_calls_not_supported),
          getString(
              R.string.ConversationActivity_this_device_does_not_appear_to_support_dial_actions));
    }
  }
  private void handleAbortSecureSession() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.ConversationActivity_abort_secure_session_confirmation);
    builder.setIcon(Dialogs.resolveIcon(this, R.attr.dialog_alert_icon));
    builder.setCancelable(true);
    builder.setMessage(
        R.string
            .ConversationActivity_are_you_sure_that_you_want_to_abort_this_secure_session_question);
    builder.setPositiveButton(
        R.string.yes,
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            if (isSingleConversation()) {
              ConversationActivity self = ConversationActivity.this;
              Recipient recipient = getRecipients().getPrimaryRecipient();

              if (SessionRecordV2.hasSession(
                  self,
                  masterSecret,
                  recipient.getRecipientId(),
                  RecipientDevice.DEFAULT_DEVICE_ID)) {
                OutgoingEndSessionMessage endSessionMessage =
                    new OutgoingEndSessionMessage(
                        new OutgoingTextMessage(getRecipients(), "TERMINATE"));

                long allocatedThreadId =
                    MessageSender.send(self, masterSecret, endSessionMessage, threadId, false);

                sendComplete(recipients, allocatedThreadId, allocatedThreadId != self.threadId);
              } else {
                Session.abortSessionFor(self, recipient);
                initializeSecurity();
                initializeTitleBar();
              }
            }
          }
        });
    builder.setNegativeButton(R.string.no, null);
    builder.show();
  }
  private void handleDeleteThread() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.ConversationActivity_delete_thread_confirmation);
    builder.setIcon(Dialogs.resolveIcon(this, R.attr.dialog_alert_icon));
    builder.setCancelable(true);
    builder.setMessage(
        R.string
            .ConversationActivity_are_you_sure_that_you_want_to_permanently_delete_this_conversation_question);
    builder.setPositiveButton(
        R.string.yes,
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            if (threadId > 0) {
              DatabaseFactory.getThreadDatabase(ConversationActivity.this)
                  .deleteConversation(threadId);
              finish();
            }
          }
        });

    builder.setNegativeButton(R.string.no, null);
    builder.show();
  }