private Pair<Long, Long> insertSyncMediaMessage(
      MasterSecret masterSecret, TextSecureEnvelope envelope, TextSecureMessage message)
      throws MmsException {
    MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
    TextSecureSyncContext syncContext = message.getSyncContext().get();
    Recipients recipients = getSyncMessageDestination(message);
    OutgoingMediaMessage mediaMessage =
        new OutgoingMediaMessage(
            context,
            masterSecret,
            recipients,
            message.getAttachments().get(),
            message.getBody().orNull());

    mediaMessage = new OutgoingSecureMediaMessage(mediaMessage);

    long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
    long messageId =
        database.insertMessageOutbox(
            masterSecret, mediaMessage, threadId, false, syncContext.getTimestamp());

    database.markAsSent(messageId, "push".getBytes(), 0);
    database.markAsPush(messageId);

    return new Pair<>(messageId, threadId);
  }
  private Pair<Long, Long> insertSyncTextMessage(
      MasterSecret masterSecret,
      TextSecureEnvelope envelope,
      TextSecureMessage message,
      Optional<Long> smsMessageId) {
    EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
    Recipients recipients = getSyncMessageDestination(message);
    String body = message.getBody().or("");
    OutgoingTextMessage outgoingTextMessage = new OutgoingTextMessage(recipients, body);

    long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
    long messageId =
        database.insertMessageOutbox(
            masterSecret,
            threadId,
            outgoingTextMessage,
            false,
            message.getSyncContext().get().getTimestamp());

    database.markAsSent(messageId);
    database.markAsPush(messageId);
    database.markAsSecure(messageId);

    if (smsMessageId.isPresent()) {
      database.deleteMessage(smsMessageId.get());
    }

    return new Pair<>(messageId, threadId);
  }
 private Recipients getSyncMessageDestination(TextSecureMessage message) {
   if (message.getGroupInfo().isPresent()) {
     return RecipientFactory.getRecipientsFromString(
         context, GroupUtil.getEncodedId(message.getGroupInfo().get().getGroupId()), false);
   } else {
     return RecipientFactory.getRecipientsFromString(
         context, message.getSyncContext().get().getDestination(), false);
   }
 }
  private void handleTextMessage(
      MasterSecret masterSecret,
      TextSecureEnvelope envelope,
      TextSecureMessage message,
      Optional<Long> smsMessageId) {
    Pair<Long, Long> messageAndThreadId;

    if (message.getSyncContext().isPresent()) {
      messageAndThreadId = insertSyncTextMessage(masterSecret, envelope, message, smsMessageId);
    } else {
      messageAndThreadId = insertStandardTextMessage(masterSecret, envelope, message, smsMessageId);
    }

    MessageNotifier.updateNotification(context, masterSecret, messageAndThreadId.second);
  }
  private void handleMediaMessage(
      MasterSecret masterSecret,
      TextSecureEnvelope envelope,
      TextSecureMessage message,
      Optional<Long> smsMessageId)
      throws MmsException {
    Pair<Long, Long> messageAndThreadId;

    if (message.getSyncContext().isPresent()) {
      messageAndThreadId = insertSyncMediaMessage(masterSecret, envelope, message);
    } else {
      messageAndThreadId = insertStandardMediaMessage(masterSecret, envelope, message);
    }

    ApplicationContext.getInstance(context)
        .getJobManager()
        .add(new AttachmentDownloadJob(context, messageAndThreadId.first));

    if (smsMessageId.isPresent()) {
      DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get());
    }

    MessageNotifier.updateNotification(context, masterSecret, messageAndThreadId.second);
  }