Search in sources :

Example 11 with SmsDatabase

use of org.thoughtcrime.securesms.database.SmsDatabase in project Signal-Android by WhisperSystems.

the class MasterSecretDecryptJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) {
    EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context);
    SmsDatabase.Reader smsReader = smsDatabase.getDecryptInProgressMessages(masterSecret);
    SmsMessageRecord smsRecord;
    while ((smsRecord = smsReader.getNext()) != null) {
        try {
            String body = getAsymmetricDecryptedBody(masterSecret, smsRecord.getBody().getBody());
            smsDatabase.updateMessageBody(new MasterSecretUnion(masterSecret), smsRecord.getId(), body);
        } catch (InvalidMessageException e) {
            Log.w(TAG, e);
        }
    }
    MmsDatabase mmsDatabase = DatabaseFactory.getMmsDatabase(context);
    MmsDatabase.Reader mmsReader = mmsDatabase.getDecryptInProgressMessages(masterSecret);
    MessageRecord mmsRecord;
    while ((mmsRecord = mmsReader.getNext()) != null) {
        try {
            String body = getAsymmetricDecryptedBody(masterSecret, mmsRecord.getBody().getBody());
            mmsDatabase.updateMessageBody(new MasterSecretUnion(masterSecret), mmsRecord.getId(), body);
        } catch (InvalidMessageException e) {
            Log.w(TAG, e);
        }
    }
    smsReader.close();
    mmsReader.close();
    MessageNotifier.updateNotification(context, masterSecret);
}
Also used : MasterSecretUnion(org.thoughtcrime.securesms.crypto.MasterSecretUnion) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) SmsMessageRecord(org.thoughtcrime.securesms.database.model.SmsMessageRecord) EncryptingSmsDatabase(org.thoughtcrime.securesms.database.EncryptingSmsDatabase) EncryptingSmsDatabase(org.thoughtcrime.securesms.database.EncryptingSmsDatabase) SmsDatabase(org.thoughtcrime.securesms.database.SmsDatabase) MessageRecord(org.thoughtcrime.securesms.database.model.MessageRecord) SmsMessageRecord(org.thoughtcrime.securesms.database.model.SmsMessageRecord) MmsDatabase(org.thoughtcrime.securesms.database.MmsDatabase)

Example 12 with SmsDatabase

use of org.thoughtcrime.securesms.database.SmsDatabase 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 13 with SmsDatabase

use of org.thoughtcrime.securesms.database.SmsDatabase in project Signal-Android by signalapp.

the class MessageSender method send.

public static long send(final Context context, final OutgoingTextMessage message, final long threadId, final boolean forceSms, final SmsDatabase.InsertListener insertListener) {
    SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
    Recipient recipient = message.getRecipient();
    boolean keyExchange = message.isKeyExchange();
    long allocatedThreadId;
    if (threadId == -1) {
        allocatedThreadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);
    } else {
        allocatedThreadId = threadId;
    }
    long messageId = database.insertMessageOutbox(allocatedThreadId, message, forceSms, System.currentTimeMillis(), insertListener);
    sendTextMessage(context, recipient, forceSms, keyExchange, messageId, message.getExpiresIn());
    return allocatedThreadId;
}
Also used : SmsDatabase(org.thoughtcrime.securesms.database.SmsDatabase) Recipient(org.thoughtcrime.securesms.recipients.Recipient)

Example 14 with SmsDatabase

use of org.thoughtcrime.securesms.database.SmsDatabase 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 15 with SmsDatabase

use of org.thoughtcrime.securesms.database.SmsDatabase 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)

Aggregations

SmsDatabase (org.thoughtcrime.securesms.database.SmsDatabase)20 InsertResult (org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult)8 IncomingTextMessage (org.thoughtcrime.securesms.sms.IncomingTextMessage)6 MmsDatabase (org.thoughtcrime.securesms.database.MmsDatabase)5 Recipient (org.thoughtcrime.securesms.recipients.Recipient)5 Nullable (android.support.annotation.Nullable)3 EncryptingSmsDatabase (org.thoughtcrime.securesms.database.EncryptingSmsDatabase)3 MmsSmsDatabase (org.thoughtcrime.securesms.database.MmsSmsDatabase)3 Intent (android.content.Intent)2 TextSecureSessionStore (org.thoughtcrime.securesms.crypto.storage.TextSecureSessionStore)2 Address (org.thoughtcrime.securesms.database.Address)2 SmsMessageRecord (org.thoughtcrime.securesms.database.model.SmsMessageRecord)2 IncomingEncryptedMessage (org.thoughtcrime.securesms.sms.IncomingEncryptedMessage)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 SessionStore (org.whispersystems.libsignal.state.SessionStore)2 SuppressLint (android.annotation.SuppressLint)1 PendingIntent (android.app.PendingIntent)1 AlertDialog (android.support.v7.app.AlertDialog)1 ByteString (com.google.protobuf.ByteString)1 MasterSecretUnion (org.thoughtcrime.securesms.crypto.MasterSecretUnion)1