Search in sources :

Example 26 with InsertResult

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

the class SmsReceiveJob method storeMessage.

private Optional<InsertResult> storeMessage(IncomingTextMessage message) throws MigrationPendingException {
    MessageDatabase database = SignalDatabase.sms();
    database.ensureMigration();
    if (TextSecurePreferences.getNeedsSqlCipherMigration(context)) {
        throw new MigrationPendingException();
    }
    if (message.isSecureMessage()) {
        IncomingTextMessage placeholder = new IncomingTextMessage(message, "");
        Optional<InsertResult> insertResult = database.insertMessageInbox(placeholder);
        database.markAsLegacyVersion(insertResult.get().getMessageId());
        return insertResult;
    } else {
        return database.insertMessageInbox(message);
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage)

Example 27 with InsertResult

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

the class MessageContentProcessor method handleTextMessage.

@Nullable
private MessageId handleTextMessage(@NonNull SignalServiceContent content, @NonNull SignalServiceDataMessage message, @NonNull Optional<Long> smsMessageId, @NonNull Optional<GroupId> groupId, @NonNull Recipient senderRecipient, @NonNull Recipient threadRecipient, long receivedTime) throws StorageFailedException {
    log(message.getTimestamp(), "Text message.");
    MessageDatabase database = SignalDatabase.sms();
    String body = message.getBody().isPresent() ? message.getBody().get() : "";
    if (message.getExpiresInSeconds() != threadRecipient.getExpiresInSeconds()) {
        handleExpirationUpdate(content, message, Optional.absent(), groupId, senderRecipient, threadRecipient, receivedTime);
    }
    Optional<InsertResult> insertResult;
    if (smsMessageId.isPresent() && !message.getGroupContext().isPresent()) {
        insertResult = Optional.of(database.updateBundleMessageBody(smsMessageId.get(), body));
    } else {
        notifyTypingStoppedFromIncomingMessage(senderRecipient, threadRecipient, content.getSenderDevice());
        IncomingTextMessage textMessage = new IncomingTextMessage(senderRecipient.getId(), content.getSenderDevice(), message.getTimestamp(), content.getServerReceivedTimestamp(), receivedTime, body, groupId, TimeUnit.SECONDS.toMillis(message.getExpiresInSeconds()), content.isNeedsReceipt(), content.getServerUuid());
        textMessage = new IncomingEncryptedMessage(textMessage, body);
        insertResult = database.insertMessageInbox(textMessage);
        if (smsMessageId.isPresent())
            database.deleteMessage(smsMessageId.get());
    }
    if (insertResult.isPresent()) {
        ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
        return new MessageId(insertResult.get().getMessageId(), false);
    } else {
        return null;
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) IncomingEncryptedMessage(org.thoughtcrime.securesms.sms.IncomingEncryptedMessage) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) MessageId(org.thoughtcrime.securesms.database.model.MessageId) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId) Nullable(androidx.annotation.Nullable)

Example 28 with InsertResult

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

the class MessageContentProcessor method handleEndSessionMessage.

@Nullable
private MessageId handleEndSessionMessage(@NonNull SignalServiceContent content, @NonNull Optional<Long> smsMessageId, @NonNull Recipient senderRecipient) {
    log(content.getTimestamp(), "End session message.");
    MessageDatabase smsDatabase = SignalDatabase.sms();
    IncomingTextMessage incomingTextMessage = new IncomingTextMessage(senderRecipient.getId(), content.getSenderDevice(), content.getTimestamp(), content.getServerReceivedTimestamp(), System.currentTimeMillis(), "", Optional.absent(), 0, content.isNeedsReceipt(), content.getServerUuid());
    Optional<InsertResult> insertResult;
    if (!smsMessageId.isPresent()) {
        IncomingEndSessionMessage incomingEndSessionMessage = new IncomingEndSessionMessage(incomingTextMessage);
        insertResult = smsDatabase.insertMessageInbox(incomingEndSessionMessage);
    } else {
        smsDatabase.markAsEndSession(smsMessageId.get());
        insertResult = Optional.of(new InsertResult(smsMessageId.get(), smsDatabase.getThreadIdForMessage(smsMessageId.get())));
    }
    if (insertResult.isPresent()) {
        ApplicationDependencies.getProtocolStore().aci().deleteAllSessions(content.getSender().getIdentifier());
        SecurityEvent.broadcastSecurityUpdateEvent(context);
        ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
        return new MessageId(insertResult.get().getMessageId(), true);
    } else {
        return null;
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) IncomingEndSessionMessage(org.thoughtcrime.securesms.sms.IncomingEndSessionMessage) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) MessageId(org.thoughtcrime.securesms.database.model.MessageId) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId) Nullable(androidx.annotation.Nullable)

Example 29 with InsertResult

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

the class DirectoryHelper method notifyNewUsers.

private static void notifyNewUsers(@NonNull Context context, @NonNull Collection<RecipientId> newUsers) {
    if (!SignalStore.settings().isNotifyWhenContactJoinsSignal())
        return;
    for (RecipientId newUser : newUsers) {
        Recipient recipient = Recipient.resolved(newUser);
        if (!recipient.isSelf() && recipient.hasAUserSetDisplayName(context) && !hasSession(recipient.getId())) {
            IncomingJoinedMessage message = new IncomingJoinedMessage(recipient.getId());
            Optional<InsertResult> insertResult = SignalDatabase.sms().insertMessageInbox(message);
            if (insertResult.isPresent()) {
                int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
                if (hour >= 9 && hour < 23) {
                    ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId(), true);
                } else {
                    Log.i(TAG, "Not notifying of a new user due to the time of day. (Hour: " + hour + ")");
                }
            }
        }
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IncomingJoinedMessage(org.thoughtcrime.securesms.sms.IncomingJoinedMessage)

Example 30 with InsertResult

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

the class MmsDownloadJob method storeRetrievedMms.

private void storeRetrievedMms(String contentLocation, long messageId, long threadId, RetrieveConf retrieved, int subscriptionId, @Nullable RecipientId notificationFrom) throws MmsException {
    MessageDatabase database = SignalDatabase.mms();
    Optional<GroupId> group = Optional.absent();
    Set<RecipientId> members = new HashSet<>();
    String body = null;
    List<Attachment> attachments = new LinkedList<>();
    List<Contact> sharedContacts = new LinkedList<>();
    RecipientId from = null;
    if (retrieved.getFrom() != null) {
        from = Recipient.external(context, Util.toIsoString(retrieved.getFrom().getTextString())).getId();
    } else if (notificationFrom != null) {
        from = notificationFrom;
    }
    if (retrieved.getTo() != null) {
        for (EncodedStringValue toValue : retrieved.getTo()) {
            members.add(Recipient.external(context, Util.toIsoString(toValue.getTextString())).getId());
        }
    }
    if (retrieved.getCc() != null) {
        for (EncodedStringValue ccValue : retrieved.getCc()) {
            members.add(Recipient.external(context, Util.toIsoString(ccValue.getTextString())).getId());
        }
    }
    if (from != null) {
        members.add(from);
    }
    members.add(Recipient.self().getId());
    if (retrieved.getBody() != null) {
        body = PartParser.getMessageText(retrieved.getBody());
        PduBody media = PartParser.getSupportedMediaParts(retrieved.getBody());
        for (int i = 0; i < media.getPartsNum(); i++) {
            PduPart part = media.getPart(i);
            if (part.getData() != null) {
                if (Util.toIsoString(part.getContentType()).toLowerCase().equals(MediaUtil.VCARD)) {
                    sharedContacts.addAll(VCardUtil.parseContacts(new String(part.getData())));
                } else {
                    Uri uri = BlobProvider.getInstance().forData(part.getData()).createForSingleUseInMemory();
                    String name = null;
                    if (part.getName() != null)
                        name = Util.toIsoString(part.getName());
                    attachments.add(new UriAttachment(uri, Util.toIsoString(part.getContentType()), AttachmentDatabase.TRANSFER_PROGRESS_DONE, part.getData().length, name, false, false, false, false, null, null, null, null, null));
                }
            }
        }
    }
    if (members.size() > 2) {
        List<RecipientId> recipients = new ArrayList<>(members);
        group = Optional.of(SignalDatabase.groups().getOrCreateMmsGroupForMembers(recipients));
    }
    IncomingMediaMessage message = new IncomingMediaMessage(from, group, body, TimeUnit.SECONDS.toMillis(retrieved.getDate()), -1, System.currentTimeMillis(), attachments, subscriptionId, 0, false, false, false, Optional.of(sharedContacts));
    Optional<InsertResult> insertResult = database.insertMessageInbox(message, contentLocation, threadId);
    if (insertResult.isPresent()) {
        database.deleteMessage(messageId);
        ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) PduBody(com.google.android.mms.pdu_alt.PduBody) IncomingMediaMessage(org.thoughtcrime.securesms.mms.IncomingMediaMessage) ArrayList(java.util.ArrayList) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) Uri(android.net.Uri) LinkedList(java.util.LinkedList) GroupId(org.thoughtcrime.securesms.groups.GroupId) Contact(org.thoughtcrime.securesms.contactshare.Contact) PduPart(com.google.android.mms.pdu_alt.PduPart) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) HashSet(java.util.HashSet)

Aggregations

InsertResult (org.thoughtcrime.securesms.database.MessageDatabase.InsertResult)30 MessageDatabase (org.thoughtcrime.securesms.database.MessageDatabase)26 IncomingTextMessage (org.thoughtcrime.securesms.sms.IncomingTextMessage)12 Nullable (androidx.annotation.Nullable)10 SyncMessageId (org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)8 MessageId (org.thoughtcrime.securesms.database.model.MessageId)8 IncomingMediaMessage (org.thoughtcrime.securesms.mms.IncomingMediaMessage)6 MmsException (org.thoughtcrime.securesms.mms.MmsException)6 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)6 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 Attachment (org.thoughtcrime.securesms.attachments.Attachment)4 UriAttachment (org.thoughtcrime.securesms.attachments.UriAttachment)4 Recipient (org.thoughtcrime.securesms.recipients.Recipient)4 SuppressLint (android.annotation.SuppressLint)2 Notification (android.app.Notification)2 NotificationManager (android.app.NotificationManager)2 Intent (android.content.Intent)2 Uri (android.net.Uri)2 EncodedStringValue (com.google.android.mms.pdu_alt.EncodedStringValue)2