private void addAttachment(int type) {
   Log.w("ComposeMessageActivity", "Selected: " + type);
   switch (type) {
     case AttachmentTypeSelectorAdapter.ADD_IMAGE:
       AttachmentManager.selectImage(this, PICK_IMAGE);
       break;
     case AttachmentTypeSelectorAdapter.ADD_VIDEO:
       AttachmentManager.selectVideo(this, PICK_VIDEO);
       break;
     case AttachmentTypeSelectorAdapter.ADD_SOUND:
       AttachmentManager.selectAudio(this, PICK_AUDIO);
       break;
   }
 }
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    if (isEncryptedConversation && isSingleConversation()) {
      boolean isPushDestination = DirectoryHelper.isPushDestination(this, getRecipients());
      Recipient primaryRecipient =
          getRecipients() == null ? null : getRecipients().getPrimaryRecipient();
      boolean hasSession = Session.hasSession(this, masterSecret, primaryRecipient);

      getMenuInflater().inflate(R.menu.conversation_button_context, menu);

      if (attachmentManager.isAttachmentPresent()) {
        menu.removeItem(R.id.menu_context_send_encrypted_sms);
        menu.removeItem(R.id.menu_context_send_unencrypted_sms);
      } else {
        menu.removeItem(R.id.menu_context_send_encrypted_mms);
        menu.removeItem(R.id.menu_context_send_unencrypted_mms);
      }

      if (!isPushDestination) {
        menu.removeItem(R.id.menu_context_send_push);
      }

      if (!hasSession) {
        menu.removeItem(R.id.menu_context_send_encrypted_mms);
        menu.removeItem(R.id.menu_context_send_encrypted_sms);
      }
    }
  }
  private long sendMediaMessage(boolean forcePlaintext, boolean forceSms)
      throws InvalidMessageException, MmsException {
    SlideDeck slideDeck;

    if (attachmentManager.isAttachmentPresent()) slideDeck = attachmentManager.getSlideDeck();
    else slideDeck = new SlideDeck();

    OutgoingMediaMessage outgoingMessage =
        new OutgoingMediaMessage(this, recipients, slideDeck, getMessage(), distributionType);

    if (isEncryptedConversation && !forcePlaintext) {
      outgoingMessage = new OutgoingSecureMediaMessage(outgoingMessage);
    }

    return MessageSender.send(this, masterSecret, outgoingMessage, threadId, forceSms);
  }
 private void addAttachmentImage(Uri imageUri) {
   try {
     attachmentManager.setImage(imageUri);
   } catch (IOException e) {
     Log.w(TAG, e);
     attachmentManager.clear();
     Toast.makeText(
             this,
             R.string.ConversationActivity_sorry_there_was_an_error_setting_your_attachment,
             Toast.LENGTH_LONG)
         .show();
   } catch (BitmapDecodingException e) {
     Log.w(TAG, e);
     attachmentManager.clear();
     Toast.makeText(
             this,
             R.string.ConversationActivity_sorry_there_was_an_error_setting_your_attachment,
             Toast.LENGTH_LONG)
         .show();
   }
 }
  private String getMessage() throws InvalidMessageException {
    String rawText = composeText.getText().toString();

    if (rawText.length() < 1 && !attachmentManager.isAttachmentPresent())
      throw new InvalidMessageException(
          getString(R.string.ConversationActivity_message_is_empty_exclamation));

    if (!isEncryptedConversation && Tag.isTaggable(rawText))
      rawText = Tag.getTaggedMessage(rawText);

    return rawText;
  }
 private void addAttachmentAudio(Uri audioUri) {
   try {
     attachmentManager.setAudio(audioUri);
   } catch (IOException e) {
     attachmentManager.clear();
     Toast.makeText(
             this,
             R.string.ConversationActivity_sorry_there_was_an_error_setting_your_attachment,
             Toast.LENGTH_LONG)
         .show();
     Log.w("ComposeMessageActivity", e);
   } catch (MediaTooLargeException e) {
     attachmentManager.clear();
     Toast.makeText(
             this,
             R.string
                 .ConversationActivity_sorry_the_selected_audio_exceeds_message_size_restrictions,
             Toast.LENGTH_LONG)
         .show();
     Log.w("ComposeMessageActivity", e);
   }
 }
  private void initializeSecurity() {
    TypedArray drawables = obtainStyledAttributes(SEND_ATTRIBUTES);
    Recipient primaryRecipient =
        getRecipients() == null ? null : getRecipients().getPrimaryRecipient();
    boolean isPushDestination = DirectoryHelper.isPushDestination(this, getRecipients());
    boolean isSecureDestination =
        isSingleConversation() && Session.hasSession(this, masterSecret, primaryRecipient);

    if (isPushDestination || isSecureDestination) {
      this.isEncryptedConversation = true;
      this.characterCalculator = new EncryptedCharacterCalculator();
    } else {
      this.isEncryptedConversation = false;
      this.characterCalculator = new CharacterCalculator();
    }

    if (isPushDestination) {
      sendButton.setImageDrawable(drawables.getDrawable(0));
      setComposeTextHint(getString(R.string.conversation_activity__type_message_push));
    } else if (isSecureDestination) {
      sendButton.setImageDrawable(drawables.getDrawable(1));
      setComposeTextHint(
          attachmentManager.isAttachmentPresent()
              ? getString(R.string.conversation_activity__type_message_mms_secure)
              : getString(R.string.conversation_activity__type_message_sms_secure));
    } else {
      sendButton.setImageDrawable(drawables.getDrawable(2));
      setComposeTextHint(
          (attachmentManager.isAttachmentPresent() || !recipients.isSingleRecipient())
              ? getString(R.string.conversation_activity__type_message_mms_insecure)
              : getString(R.string.conversation_activity__type_message_sms_insecure));
    }

    drawables.recycle();

    calculateCharactersRemaining();
  }
  private List<Draft> getDraftsForCurrentState() {
    List<Draft> drafts = new LinkedList<Draft>();

    if (!Util.isEmpty(composeText)) {
      drafts.add(new Draft(Draft.TEXT, composeText.getText().toString()));
    }

    for (Slide slide : attachmentManager.getSlideDeck().getSlides()) {
      if (slide.hasImage()) drafts.add(new Draft(Draft.IMAGE, slide.getUri().toString()));
      else if (slide.hasAudio()) drafts.add(new Draft(Draft.AUDIO, slide.getUri().toString()));
      else if (slide.hasVideo()) drafts.add(new Draft(Draft.VIDEO, slide.getUri().toString()));
    }

    return drafts;
  }
  private void sendComplete(Recipients recipients, long threadId, boolean refreshFragment) {
    attachmentManager.clear();
    composeText.setText("");

    this.recipients = recipients;
    this.threadId = threadId;

    ConversationFragment fragment =
        (ConversationFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_content);
    if (refreshFragment) {
      fragment.reload(recipients, threadId);

      initializeTitleBar();
      initializeSecurity();
    }
    fragment.scrollToBottom();
  }
  private void sendMessage(boolean forcePlaintext, boolean forceSms) {
    try {
      Recipients recipients = getRecipients();

      if (recipients == null) throw new RecipientFormattingException("Badly formatted");

      long allocatedThreadId;

      if ((!recipients.isSingleRecipient() || recipients.isEmailRecipient()) && !isMmsEnabled) {
        handleManualMmsRequired();
        return;
      } else if (attachmentManager.isAttachmentPresent()
          || !recipients.isSingleRecipient()
          || recipients.isGroupRecipient()
          || recipients.isEmailRecipient()) {
        allocatedThreadId = sendMediaMessage(forcePlaintext, forceSms);
      } else {
        allocatedThreadId = sendTextMessage(forcePlaintext, forceSms);
      }

      sendComplete(recipients, allocatedThreadId, allocatedThreadId != this.threadId);
    } catch (RecipientFormattingException ex) {
      Toast.makeText(
              ConversationActivity.this,
              R.string
                  .ConversationActivity_recipient_is_not_a_valid_sms_or_email_address_exclamation,
              Toast.LENGTH_LONG)
          .show();
      Log.w(TAG, ex);
    } catch (InvalidMessageException ex) {
      Toast.makeText(
              ConversationActivity.this,
              R.string.ConversationActivity_message_is_empty_exclamation,
              Toast.LENGTH_SHORT)
          .show();
      Log.w(TAG, ex);
    } catch (MmsException e) {
      Log.w(TAG, e);
    }
  }