Search in sources :

Example 1 with IncomingGroupMessage

use of org.thoughtcrime.securesms.sms.IncomingGroupMessage in project Signal-Android by WhisperSystems.

the class GroupMessageProcessor method storeMessage.

@Nullable
private static Long storeMessage(@NonNull Context context, @NonNull MasterSecretUnion masterSecret, @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);
            Recipients recipients = RecipientFactory.getRecipientsFromString(context, GroupUtil.getEncodedId(group.getGroupId()), false);
            OutgoingGroupMediaMessage outgoingMessage = new OutgoingGroupMediaMessage(recipients, storage, null, envelope.getTimestamp(), 0);
            long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
            long messageId = mmsDatabase.insertMessageOutbox(masterSecret, outgoingMessage, threadId, false);
            mmsDatabase.markAsSent(messageId, true);
            return threadId;
        } else {
            EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context);
            String body = Base64.encodeBytes(storage.toByteArray());
            IncomingTextMessage incoming = new IncomingTextMessage(envelope.getSource(), envelope.getSourceDevice(), envelope.getTimestamp(), body, Optional.of(group), 0);
            IncomingGroupMessage groupMessage = new IncomingGroupMessage(incoming, storage, body);
            Optional<InsertResult> insertResult = smsDatabase.insertMessageInbox(masterSecret, groupMessage);
            if (insertResult.isPresent()) {
                MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), 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) MmsException(ws.com.google.android.mms.MmsException) OutgoingGroupMediaMessage(org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage) Recipients(org.thoughtcrime.securesms.recipients.Recipients) AvatarDownloadJob(org.thoughtcrime.securesms.jobs.AvatarDownloadJob) EncryptingSmsDatabase(org.thoughtcrime.securesms.database.EncryptingSmsDatabase) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) ByteString(com.google.protobuf.ByteString) MmsDatabase(org.thoughtcrime.securesms.database.MmsDatabase) Nullable(android.support.annotation.Nullable)

Example 2 with IncomingGroupMessage

use of org.thoughtcrime.securesms.sms.IncomingGroupMessage 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 3 with IncomingGroupMessage

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

the class SmsDatabase method insertMessageInbox.

protected Optional<InsertResult> insertMessageInbox(IncomingTextMessage message, long type) {
    if (message.isJoined()) {
        type = (type & (Types.TOTAL_MASK - Types.BASE_TYPE_MASK)) | Types.JOINED_TYPE;
    } else if (message.isPreKeyBundle()) {
        type |= Types.KEY_EXCHANGE_BIT | Types.KEY_EXCHANGE_BUNDLE_BIT;
    } else if (message.isSecureMessage()) {
        type |= Types.SECURE_MESSAGE_BIT;
    } else if (message.isGroup()) {
        type |= Types.SECURE_MESSAGE_BIT;
        if (((IncomingGroupMessage) message).isUpdate())
            type |= Types.GROUP_UPDATE_BIT;
        else if (((IncomingGroupMessage) message).isQuit())
            type |= Types.GROUP_QUIT_BIT;
    } else if (message.isEndSession()) {
        type |= Types.SECURE_MESSAGE_BIT;
        type |= Types.END_SESSION_BIT;
    }
    if (message.isPush())
        type |= Types.PUSH_MESSAGE_BIT;
    if (message.isIdentityUpdate())
        type |= Types.KEY_EXCHANGE_IDENTITY_UPDATE_BIT;
    if (message.isContentPreKeyBundle())
        type |= Types.KEY_EXCHANGE_CONTENT_FORMAT;
    if (message.isIdentityVerified())
        type |= Types.KEY_EXCHANGE_IDENTITY_VERIFIED_BIT;
    else if (message.isIdentityDefault())
        type |= Types.KEY_EXCHANGE_IDENTITY_DEFAULT_BIT;
    Recipient recipient = Recipient.from(context, message.getSender(), true);
    Recipient groupRecipient;
    if (message.getGroupId() == null) {
        groupRecipient = null;
    } else {
        groupRecipient = Recipient.from(context, message.getGroupId(), true);
    }
    boolean unread = (org.thoughtcrime.securesms.util.Util.isDefaultSmsProvider(context) || message.isSecureMessage() || message.isGroup() || message.isPreKeyBundle()) && !message.isIdentityUpdate() && !message.isIdentityDefault() && !message.isIdentityVerified();
    long threadId;
    if (groupRecipient == null)
        threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);
    else
        threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(groupRecipient);
    ContentValues values = new ContentValues(6);
    values.put(ADDRESS, message.getSender().serialize());
    values.put(ADDRESS_DEVICE_ID, message.getSenderDeviceId());
    values.put(DATE_RECEIVED, System.currentTimeMillis());
    values.put(DATE_SENT, message.getSentTimestampMillis());
    values.put(PROTOCOL, message.getProtocol());
    values.put(READ, unread ? 0 : 1);
    values.put(SUBSCRIPTION_ID, message.getSubscriptionId());
    values.put(EXPIRES_IN, message.getExpiresIn());
    if (!TextUtils.isEmpty(message.getPseudoSubject()))
        values.put(SUBJECT, message.getPseudoSubject());
    values.put(REPLY_PATH_PRESENT, message.isReplyPathPresent());
    values.put(SERVICE_CENTER, message.getServiceCenterAddress());
    values.put(BODY, message.getMessageBody());
    values.put(TYPE, type);
    values.put(THREAD_ID, threadId);
    if (message.isPush() && isDuplicate(message, threadId)) {
        Log.w(TAG, "Duplicate message (" + message.getSentTimestampMillis() + "), ignoring...");
        return Optional.absent();
    } else {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        long messageId = db.insert(TABLE_NAME, null, values);
        if (unread) {
            DatabaseFactory.getThreadDatabase(context).incrementUnread(threadId, 1);
        }
        if (!message.isIdentityUpdate() && !message.isIdentityVerified() && !message.isIdentityDefault()) {
            DatabaseFactory.getThreadDatabase(context).update(threadId, true);
        }
        if (message.getSubscriptionId() != -1) {
            DatabaseFactory.getRecipientDatabase(context).setDefaultSubscriptionId(recipient, message.getSubscriptionId());
        }
        notifyConversationListeners(threadId);
        if (!message.isIdentityUpdate() && !message.isIdentityVerified() && !message.isIdentityDefault()) {
            jobManager.add(new TrimThreadJob(context, threadId));
        }
        return Optional.of(new InsertResult(messageId, threadId));
    }
}
Also used : IncomingGroupMessage(org.thoughtcrime.securesms.sms.IncomingGroupMessage) ContentValues(android.content.ContentValues) TrimThreadJob(org.thoughtcrime.securesms.jobs.TrimThreadJob) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) Recipient(org.thoughtcrime.securesms.recipients.Recipient)

Aggregations

IncomingGroupMessage (org.thoughtcrime.securesms.sms.IncomingGroupMessage)3 Nullable (android.support.annotation.Nullable)2 ByteString (com.google.protobuf.ByteString)2 InsertResult (org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult)2 MmsDatabase (org.thoughtcrime.securesms.database.MmsDatabase)2 AvatarDownloadJob (org.thoughtcrime.securesms.jobs.AvatarDownloadJob)2 OutgoingGroupMediaMessage (org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage)2 Recipient (org.thoughtcrime.securesms.recipients.Recipient)2 IncomingTextMessage (org.thoughtcrime.securesms.sms.IncomingTextMessage)2 ContentValues (android.content.ContentValues)1 SQLiteDatabase (net.sqlcipher.database.SQLiteDatabase)1 Address (org.thoughtcrime.securesms.database.Address)1 EncryptingSmsDatabase (org.thoughtcrime.securesms.database.EncryptingSmsDatabase)1 SmsDatabase (org.thoughtcrime.securesms.database.SmsDatabase)1 TrimThreadJob (org.thoughtcrime.securesms.jobs.TrimThreadJob)1 MmsException (org.thoughtcrime.securesms.mms.MmsException)1 Recipients (org.thoughtcrime.securesms.recipients.Recipients)1 MmsException (ws.com.google.android.mms.MmsException)1