Search in sources :

Example 11 with IncomingTextMessage

use of org.thoughtcrime.securesms.sms.IncomingTextMessage in project Signal-Android by signalapp.

the class GroupMessageProcessor method storeMessage.

@Nullable
private static Long storeMessage(@NonNull Context context, @NonNull SignalServiceEnvelope envelope, @NonNull SignalServiceGroup group, @NonNull GroupContext storage, boolean outgoing) {
    if (group.getAvatar().isPresent()) {
        ApplicationContext.getInstance(context).getJobManager().add(new AvatarDownloadJob(context, group.getGroupId()));
    }
    try {
        if (outgoing) {
            MmsDatabase mmsDatabase = DatabaseFactory.getMmsDatabase(context);
            Address addres = Address.fromExternal(context, GroupUtil.getEncodedId(group.getGroupId(), false));
            Recipient recipient = Recipient.from(context, addres, false);
            OutgoingGroupMediaMessage outgoingMessage = new OutgoingGroupMediaMessage(recipient, storage, null, envelope.getTimestamp(), 0);
            long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);
            long messageId = mmsDatabase.insertMessageOutbox(outgoingMessage, threadId, false, null);
            mmsDatabase.markAsSent(messageId, true);
            return threadId;
        } else {
            SmsDatabase smsDatabase = DatabaseFactory.getSmsDatabase(context);
            String body = Base64.encodeBytes(storage.toByteArray());
            IncomingTextMessage incoming = new IncomingTextMessage(Address.fromExternal(context, envelope.getSource()), envelope.getSourceDevice(), envelope.getTimestamp(), body, Optional.of(group), 0);
            IncomingGroupMessage groupMessage = new IncomingGroupMessage(incoming, storage, body);
            Optional<InsertResult> insertResult = smsDatabase.insertMessageInbox(groupMessage);
            if (insertResult.isPresent()) {
                MessageNotifier.updateNotification(context, insertResult.get().getThreadId());
                return insertResult.get().getThreadId();
            } else {
                return null;
            }
        }
    } catch (MmsException e) {
        Log.w(TAG, e);
    }
    return null;
}
Also used : IncomingGroupMessage(org.thoughtcrime.securesms.sms.IncomingGroupMessage) InsertResult(org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult) Address(org.thoughtcrime.securesms.database.Address) AvatarDownloadJob(org.thoughtcrime.securesms.jobs.AvatarDownloadJob) Recipient(org.thoughtcrime.securesms.recipients.Recipient) ByteString(com.google.protobuf.ByteString) MmsException(org.thoughtcrime.securesms.mms.MmsException) OutgoingGroupMediaMessage(org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) SmsDatabase(org.thoughtcrime.securesms.database.SmsDatabase) MmsDatabase(org.thoughtcrime.securesms.database.MmsDatabase) Nullable(android.support.annotation.Nullable)

Example 12 with IncomingTextMessage

use of org.thoughtcrime.securesms.sms.IncomingTextMessage in project Signal-Android by signalapp.

the class PushDecryptJob method insertPlaceholder.

private Optional<InsertResult> insertPlaceholder(@NonNull SignalServiceEnvelope envelope) {
    SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
    IncomingTextMessage textMessage = new IncomingTextMessage(Address.fromExternal(context, envelope.getSource()), envelope.getSourceDevice(), envelope.getTimestamp(), "", Optional.absent(), 0);
    textMessage = new IncomingEncryptedMessage(textMessage, "");
    return database.insertMessageInbox(textMessage);
}
Also used : IncomingEncryptedMessage(org.thoughtcrime.securesms.sms.IncomingEncryptedMessage) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) SmsDatabase(org.thoughtcrime.securesms.database.SmsDatabase)

Example 13 with IncomingTextMessage

use of org.thoughtcrime.securesms.sms.IncomingTextMessage in project Signal-Android by signalapp.

the class PushDecryptJob method handleTextMessage.

private void handleTextMessage(@NonNull SignalServiceEnvelope envelope, @NonNull SignalServiceDataMessage message, @NonNull Optional<Long> smsMessageId) throws MmsException {
    SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
    String body = message.getBody().isPresent() ? message.getBody().get() : "";
    Recipient recipient = getMessageDestination(envelope, message);
    if (message.getExpiresInSeconds() != recipient.getExpireMessages()) {
        handleExpirationUpdate(envelope, message, Optional.absent());
    }
    Long threadId;
    if (smsMessageId.isPresent() && !message.getGroupInfo().isPresent()) {
        threadId = database.updateBundleMessageBody(smsMessageId.get(), body).second;
    } else {
        IncomingTextMessage textMessage = new IncomingTextMessage(Address.fromExternal(context, envelope.getSource()), envelope.getSourceDevice(), message.getTimestamp(), body, message.getGroupInfo(), message.getExpiresInSeconds() * 1000L);
        textMessage = new IncomingEncryptedMessage(textMessage, body);
        Optional<InsertResult> insertResult = database.insertMessageInbox(textMessage);
        if (insertResult.isPresent())
            threadId = insertResult.get().getThreadId();
        else
            threadId = null;
        if (smsMessageId.isPresent())
            database.deleteMessage(smsMessageId.get());
    }
    if (threadId != null) {
        MessageNotifier.updateNotification(context, threadId);
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult) IncomingEncryptedMessage(org.thoughtcrime.securesms.sms.IncomingEncryptedMessage) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) SmsDatabase(org.thoughtcrime.securesms.database.SmsDatabase) Recipient(org.thoughtcrime.securesms.recipients.Recipient)

Example 14 with IncomingTextMessage

use of org.thoughtcrime.securesms.sms.IncomingTextMessage in project Signal-Android by signalapp.

the class PushDecryptJob method handleUntrustedIdentityMessage.

private void handleUntrustedIdentityMessage(@NonNull SignalServiceEnvelope envelope, @NonNull Optional<Long> smsMessageId) {
    try {
        SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
        Address sourceAddress = Address.fromExternal(context, envelope.getSource());
        byte[] serialized = envelope.hasLegacyMessage() ? envelope.getLegacyMessage() : envelope.getContent();
        PreKeySignalMessage whisperMessage = new PreKeySignalMessage(serialized);
        IdentityKey identityKey = whisperMessage.getIdentityKey();
        String encoded = Base64.encodeBytes(serialized);
        IncomingTextMessage textMessage = new IncomingTextMessage(sourceAddress, envelope.getSourceDevice(), envelope.getTimestamp(), encoded, Optional.absent(), 0);
        if (!smsMessageId.isPresent()) {
            IncomingPreKeyBundleMessage bundleMessage = new IncomingPreKeyBundleMessage(textMessage, encoded, envelope.hasLegacyMessage());
            Optional<InsertResult> insertResult = database.insertMessageInbox(bundleMessage);
            if (insertResult.isPresent()) {
                database.setMismatchedIdentity(insertResult.get().getMessageId(), sourceAddress, identityKey);
                MessageNotifier.updateNotification(context, insertResult.get().getThreadId());
            }
        } else {
            database.updateMessageBody(smsMessageId.get(), encoded);
            database.markAsPreKeyBundle(smsMessageId.get());
            database.setMismatchedIdentity(smsMessageId.get(), sourceAddress, identityKey);
        }
    } catch (InvalidMessageException | InvalidVersionException e) {
        throw new AssertionError(e);
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) IdentityKey(org.whispersystems.libsignal.IdentityKey) Address(org.thoughtcrime.securesms.database.Address) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) IncomingPreKeyBundleMessage(org.thoughtcrime.securesms.sms.IncomingPreKeyBundleMessage) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) SmsDatabase(org.thoughtcrime.securesms.database.SmsDatabase)

Aggregations

IncomingTextMessage (org.thoughtcrime.securesms.sms.IncomingTextMessage)14 InsertResult (org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult)11 SmsDatabase (org.thoughtcrime.securesms.database.SmsDatabase)7 EncryptingSmsDatabase (org.thoughtcrime.securesms.database.EncryptingSmsDatabase)6 Recipients (org.thoughtcrime.securesms.recipients.Recipients)4 IncomingEncryptedMessage (org.thoughtcrime.securesms.sms.IncomingEncryptedMessage)4 Recipient (org.thoughtcrime.securesms.recipients.Recipient)3 Nullable (android.support.annotation.Nullable)2 ByteString (com.google.protobuf.ByteString)2 TextSecureSessionStore (org.thoughtcrime.securesms.crypto.storage.TextSecureSessionStore)2 Address (org.thoughtcrime.securesms.database.Address)2 MmsDatabase (org.thoughtcrime.securesms.database.MmsDatabase)2 AvatarDownloadJob (org.thoughtcrime.securesms.jobs.AvatarDownloadJob)2 OutgoingGroupMediaMessage (org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage)2 IncomingEndSessionMessage (org.thoughtcrime.securesms.sms.IncomingEndSessionMessage)2 IncomingGroupMessage (org.thoughtcrime.securesms.sms.IncomingGroupMessage)2 IncomingPreKeyBundleMessage (org.thoughtcrime.securesms.sms.IncomingPreKeyBundleMessage)2 IdentityKey (org.whispersystems.libsignal.IdentityKey)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 InvalidVersionException (org.whispersystems.libsignal.InvalidVersionException)2