Search in sources :

Example 26 with ThreadDatabase

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

the class MessageContentProcessor method handleSynchronizeMessageRequestResponse.

private void handleSynchronizeMessageRequestResponse(@NonNull MessageRequestResponseMessage response, long envelopeTimestamp) throws BadGroupIdException {
    log(envelopeTimestamp, "Synchronize message request response.");
    RecipientDatabase recipientDatabase = SignalDatabase.recipients();
    ThreadDatabase threadDatabase = SignalDatabase.threads();
    Recipient recipient;
    if (response.getPerson().isPresent()) {
        recipient = Recipient.externalPush(response.getPerson().get());
    } else if (response.getGroupId().isPresent()) {
        GroupId groupId = GroupId.v1(response.getGroupId().get());
        recipient = Recipient.externalPossiblyMigratedGroup(context, groupId);
    } else {
        warn("Message request response was missing a thread recipient! Skipping.");
        return;
    }
    long threadId = threadDatabase.getOrCreateThreadIdFor(recipient);
    switch(response.getType()) {
        case ACCEPT:
            recipientDatabase.setProfileSharing(recipient.getId(), true);
            recipientDatabase.setBlocked(recipient.getId(), false);
            break;
        case DELETE:
            recipientDatabase.setProfileSharing(recipient.getId(), false);
            if (threadId > 0)
                threadDatabase.deleteConversation(threadId);
            break;
        case BLOCK:
            recipientDatabase.setBlocked(recipient.getId(), true);
            recipientDatabase.setProfileSharing(recipient.getId(), false);
            break;
        case BLOCK_AND_DELETE:
            recipientDatabase.setBlocked(recipient.getId(), true);
            recipientDatabase.setProfileSharing(recipient.getId(), false);
            if (threadId > 0)
                threadDatabase.deleteConversation(threadId);
            break;
        default:
            warn("Got an unknown response type! Skipping");
            break;
    }
}
Also used : RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) Recipient(org.thoughtcrime.securesms.recipients.Recipient) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) GroupId(org.thoughtcrime.securesms.groups.GroupId)

Example 27 with ThreadDatabase

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

the class UserNotificationMigrationJob method performMigration.

@Override
void performMigration() {
    if (!SignalStore.account().isRegistered() || SignalStore.account().getE164() == null || SignalStore.account().getAci() == null) {
        Log.w(TAG, "Not registered! Skipping.");
        return;
    }
    if (!SignalStore.settings().isNotifyWhenContactJoinsSignal()) {
        Log.w(TAG, "New contact notifications disabled! Skipping.");
        return;
    }
    if (TextSecurePreferences.getFirstInstallVersion(context) < 759) {
        Log.w(TAG, "Install is older than v5.0.8. Skipping.");
        return;
    }
    ThreadDatabase threadDatabase = SignalDatabase.threads();
    int threadCount = threadDatabase.getUnarchivedConversationListCount() + threadDatabase.getArchivedConversationListCount();
    if (threadCount >= 3) {
        Log.w(TAG, "Already have 3 or more threads. Skipping.");
        return;
    }
    List<RecipientId> registered = SignalDatabase.recipients().getRegistered();
    List<RecipientId> systemContacts = SignalDatabase.recipients().getSystemContacts();
    Set<RecipientId> registeredSystemContacts = SetUtil.intersection(registered, systemContacts);
    Set<RecipientId> threadRecipients = threadDatabase.getAllThreadRecipients();
    if (threadRecipients.containsAll(registeredSystemContacts)) {
        Log.w(TAG, "Threads already exist for all relevant contacts. Skipping.");
        return;
    }
    String message = context.getResources().getQuantityString(R.plurals.UserNotificationMigrationJob_d_contacts_are_on_signal, registeredSystemContacts.size(), registeredSystemContacts.size());
    Intent mainActivityIntent = new Intent(context, MainActivity.class);
    Intent newConversationIntent = new Intent(context, NewConversationActivity.class);
    PendingIntent pendingIntent = TaskStackBuilder.create(context).addNextIntent(mainActivityIntent).addNextIntent(newConversationIntent).getPendingIntent(0, 0);
    Notification notification = new NotificationCompat.Builder(context, NotificationChannels.getMessagesChannel(context)).setSmallIcon(R.drawable.ic_notification).setContentText(message).setContentIntent(pendingIntent).build();
    try {
        NotificationManagerCompat.from(context).notify(NotificationIds.USER_NOTIFICATION_MIGRATION, notification);
    } catch (Throwable t) {
        Log.w(TAG, "Failed to notify!", t);
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) TaskStackBuilder(androidx.core.app.TaskStackBuilder) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) Notification(android.app.Notification)

Example 28 with ThreadDatabase

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

the class ReviewCardRepository method delete.

void delete(@NonNull ReviewCard reviewCard, @NonNull Runnable onActionCompleteListener) {
    if (recipientId == null) {
        throw new UnsupportedOperationException();
    }
    SignalExecutors.BOUNDED.execute(() -> {
        Recipient resolved = Recipient.resolved(recipientId);
        if (resolved.isGroup())
            throw new AssertionError();
        if (TextSecurePreferences.isMultiDevice(context)) {
            ApplicationDependencies.getJobManager().add(MultiDeviceMessageRequestResponseJob.forDelete(recipientId));
        }
        ThreadDatabase threadDatabase = SignalDatabase.threads();
        long threadId = Objects.requireNonNull(threadDatabase.getThreadIdFor(recipientId));
        threadDatabase.deleteConversation(threadId);
        onActionCompleteListener.run();
    });
}
Also used : Recipient(org.thoughtcrime.securesms.recipients.Recipient) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase)

Example 29 with ThreadDatabase

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

the class LiveRecipientCache method warmUp.

@AnyThread
public void warmUp() {
    if (warmedUp.getAndSet(true)) {
        return;
    }
    Stopwatch stopwatch = new Stopwatch("recipient-warm-up");
    SignalExecutors.BOUNDED.execute(() -> {
        ThreadDatabase threadDatabase = SignalDatabase.threads();
        List<Recipient> recipients = new ArrayList<>();
        try (ThreadDatabase.Reader reader = threadDatabase.readerFor(threadDatabase.getRecentConversationList(THREAD_CACHE_WARM_MAX, false, false))) {
            int i = 0;
            ThreadRecord record = null;
            while ((record = reader.getNext()) != null && i < THREAD_CACHE_WARM_MAX) {
                recipients.add(record.getRecipient());
                i++;
            }
        }
        Log.d(TAG, "Warming up " + recipients.size() + " thread recipients.");
        addToCache(recipients);
        stopwatch.split("thread");
        if (SignalStore.registrationValues().isRegistrationComplete() && SignalStore.account().getAci() != null) {
            try (Cursor cursor = SignalDatabase.recipients().getNonGroupContacts(false)) {
                int count = 0;
                while (cursor != null && cursor.moveToNext() && count < CONTACT_CACHE_WARM_MAX) {
                    RecipientId id = RecipientId.from(CursorUtil.requireLong(cursor, RecipientDatabase.ID));
                    Recipient.resolved(id);
                    count++;
                }
                Log.d(TAG, "Warmed up " + count + " contact recipient.");
                stopwatch.split("contact");
            }
        }
        stopwatch.stop(TAG);
    });
}
Also used : Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) ArrayList(java.util.ArrayList) ThreadRecord(org.thoughtcrime.securesms.database.model.ThreadRecord) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) Cursor(android.database.Cursor) SuppressLint(android.annotation.SuppressLint) AnyThread(androidx.annotation.AnyThread)

Aggregations

ThreadDatabase (org.thoughtcrime.securesms.database.ThreadDatabase)29 Recipient (org.thoughtcrime.securesms.recipients.Recipient)14 ThreadRecord (org.thoughtcrime.securesms.database.model.ThreadRecord)7 ArrayList (java.util.ArrayList)6 SuppressLint (android.annotation.SuppressLint)5 Cursor (android.database.Cursor)5 WorkerThread (androidx.annotation.WorkerThread)5 Context (android.content.Context)4 MmsException (org.thoughtcrime.securesms.mms.MmsException)4 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)4 Recipients (org.thoughtcrime.securesms.recipients.Recipients)4 Bundle (android.os.Bundle)3 NonNull (androidx.annotation.NonNull)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 DraftDatabase (org.thoughtcrime.securesms.database.DraftDatabase)3 Drafts (org.thoughtcrime.securesms.database.DraftDatabase.Drafts)3 MessageDatabase (org.thoughtcrime.securesms.database.MessageDatabase)3 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)3 ComponentName (android.content.ComponentName)2