Search in sources :

Example 21 with Job

use of org.thoughtcrime.securesms.jobmanager.Job in project Signal-Android by signalapp.

the class GroupsV2CapabilityChecker method refreshCapabilitiesIfNecessary.

/**
 * @param resolved A collection of resolved recipients.
 * @return True if a recipient needed to be refreshed, otherwise false.
 */
@WorkerThread
public static boolean refreshCapabilitiesIfNecessary(@NonNull Collection<Recipient> resolved) throws IOException {
    Set<RecipientId> needsRefresh = Stream.of(resolved).filter(r -> r.getGroupsV2Capability() != Recipient.Capability.SUPPORTED).map(Recipient::getId).collect(Collectors.toSet());
    if (needsRefresh.size() > 0) {
        Log.d(TAG, "[refreshCapabilitiesIfNecessary] Need to refresh " + needsRefresh.size() + " recipients.");
        List<Job> jobs = RetrieveProfileJob.forRecipients(needsRefresh);
        JobManager jobManager = ApplicationDependencies.getJobManager();
        for (Job job : jobs) {
            if (!jobManager.runSynchronously(job, TimeUnit.SECONDS.toMillis(10)).isPresent()) {
                throw new IOException("Recipient capability was not retrieved in time");
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) IOException(java.io.IOException) RetrieveProfileJob(org.thoughtcrime.securesms.jobs.RetrieveProfileJob) Job(org.thoughtcrime.securesms.jobmanager.Job) WorkerThread(androidx.annotation.WorkerThread)

Example 22 with Job

use of org.thoughtcrime.securesms.jobmanager.Job in project Signal-Android by signalapp.

the class MessageSender method preUploadPushAttachment.

/**
 * @return A result if the attachment was enqueued, or null if it failed to enqueue or shouldn't
 *         be enqueued (like in the case of a local self-send).
 */
@Nullable
public static PreUploadResult preUploadPushAttachment(@NonNull Context context, @NonNull Attachment attachment, @Nullable Recipient recipient) {
    if (isLocalSelfSend(context, recipient, false)) {
        return null;
    }
    Log.i(TAG, "Pre-uploading attachment for " + (recipient != null ? recipient.getId() : "null"));
    try {
        AttachmentDatabase attachmentDatabase = SignalDatabase.attachments();
        DatabaseAttachment databaseAttachment = attachmentDatabase.insertAttachmentForPreUpload(attachment);
        Job compressionJob = AttachmentCompressionJob.fromAttachment(databaseAttachment, false, -1);
        Job resumableUploadSpecJob = new ResumableUploadSpecJob();
        Job uploadJob = new AttachmentUploadJob(databaseAttachment.getAttachmentId());
        ApplicationDependencies.getJobManager().startChain(compressionJob).then(resumableUploadSpecJob).then(uploadJob).enqueue();
        return new PreUploadResult(databaseAttachment.getAttachmentId(), Arrays.asList(compressionJob.getId(), resumableUploadSpecJob.getId(), uploadJob.getId()));
    } catch (MmsException e) {
        Log.w(TAG, "preUploadPushAttachment() - Failed to upload!", e);
        return null;
    }
}
Also used : MmsException(org.thoughtcrime.securesms.mms.MmsException) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) ResumableUploadSpecJob(org.thoughtcrime.securesms.jobs.ResumableUploadSpecJob) AttachmentMarkUploadedJob(org.thoughtcrime.securesms.jobs.AttachmentMarkUploadedJob) ReactionSendJob(org.thoughtcrime.securesms.jobs.ReactionSendJob) PushMediaSendJob(org.thoughtcrime.securesms.jobs.PushMediaSendJob) Job(org.thoughtcrime.securesms.jobmanager.Job) ResumableUploadSpecJob(org.thoughtcrime.securesms.jobs.ResumableUploadSpecJob) PushGroupSendJob(org.thoughtcrime.securesms.jobs.PushGroupSendJob) AttachmentCompressionJob(org.thoughtcrime.securesms.jobs.AttachmentCompressionJob) ProfileKeySendJob(org.thoughtcrime.securesms.jobs.ProfileKeySendJob) RemoteDeleteSendJob(org.thoughtcrime.securesms.jobs.RemoteDeleteSendJob) MmsSendJob(org.thoughtcrime.securesms.jobs.MmsSendJob) AttachmentUploadJob(org.thoughtcrime.securesms.jobs.AttachmentUploadJob) PushTextSendJob(org.thoughtcrime.securesms.jobs.PushTextSendJob) AttachmentCopyJob(org.thoughtcrime.securesms.jobs.AttachmentCopyJob) SmsSendJob(org.thoughtcrime.securesms.jobs.SmsSendJob) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) AttachmentUploadJob(org.thoughtcrime.securesms.jobs.AttachmentUploadJob) Nullable(androidx.annotation.Nullable)

Example 23 with Job

use of org.thoughtcrime.securesms.jobmanager.Job in project Signal-Android by signalapp.

the class MessageDecryptionUtil method decrypt.

/**
 * Takes a {@link SignalServiceEnvelope} and returns a {@link DecryptionResult}, which has either
 * a plaintext {@link SignalServiceContent} or information about an error that happened.
 *
 * Excluding the data updated in our protocol stores that results from decrypting a message, this
 * method is side-effect free, preferring to return the decryption results to be handled by the
 * caller.
 */
@NonNull
public static DecryptionResult decrypt(@NonNull Context context, @NonNull SignalServiceEnvelope envelope) {
    SignalServiceAccountDataStore protocolStore = ApplicationDependencies.getProtocolStore().aci();
    SignalServiceAddress localAddress = new SignalServiceAddress(Recipient.self().requireServiceId(), Recipient.self().requireE164());
    SignalServiceCipher cipher = new SignalServiceCipher(localAddress, SignalStore.account().getDeviceId(), protocolStore, ReentrantSessionLock.INSTANCE, UnidentifiedAccessUtil.getCertificateValidator());
    List<Job> jobs = new LinkedList<>();
    if (envelope.isPreKeySignalMessage()) {
        jobs.add(new RefreshPreKeysJob());
    }
    try {
        try {
            return DecryptionResult.forSuccess(cipher.decrypt(envelope), jobs);
        } catch (ProtocolInvalidVersionException e) {
            Log.w(TAG, String.valueOf(envelope.getTimestamp()), e);
            return DecryptionResult.forError(MessageState.INVALID_VERSION, toExceptionMetadata(e), jobs);
        } catch (ProtocolInvalidKeyIdException | ProtocolInvalidKeyException | ProtocolUntrustedIdentityException | ProtocolNoSessionException | ProtocolInvalidMessageException e) {
            Log.w(TAG, String.valueOf(envelope.getTimestamp()), e);
            Recipient sender = Recipient.external(context, e.getSender());
            if (sender.supportsMessageRetries() && Recipient.self().supportsMessageRetries() && FeatureFlags.retryReceipts()) {
                jobs.add(handleRetry(context, sender, envelope, e));
                postInternalErrorNotification(context);
            } else {
                jobs.add(new AutomaticSessionResetJob(sender.getId(), e.getSenderDevice(), envelope.getTimestamp()));
            }
            return DecryptionResult.forNoop(jobs);
        } catch (ProtocolLegacyMessageException e) {
            Log.w(TAG, "[" + envelope.getTimestamp() + "] " + envelope.getSourceIdentifier() + ":" + envelope.getSourceDevice(), e);
            return DecryptionResult.forError(MessageState.LEGACY_MESSAGE, toExceptionMetadata(e), jobs);
        } catch (ProtocolDuplicateMessageException e) {
            Log.w(TAG, "[" + envelope.getTimestamp() + "] " + envelope.getSourceIdentifier() + ":" + envelope.getSourceDevice(), e);
            return DecryptionResult.forError(MessageState.DUPLICATE_MESSAGE, toExceptionMetadata(e), jobs);
        } catch (InvalidMetadataVersionException | InvalidMetadataMessageException | InvalidMessageStructureException e) {
            Log.w(TAG, "[" + envelope.getTimestamp() + "] " + envelope.getSourceIdentifier() + ":" + envelope.getSourceDevice(), e);
            return DecryptionResult.forNoop(jobs);
        } catch (SelfSendException e) {
            Log.i(TAG, "Dropping UD message from self.");
            return DecryptionResult.forNoop(jobs);
        } catch (UnsupportedDataMessageException e) {
            Log.w(TAG, "[" + envelope.getTimestamp() + "] " + envelope.getSourceIdentifier() + ":" + envelope.getSourceDevice(), e);
            return DecryptionResult.forError(MessageState.UNSUPPORTED_DATA_MESSAGE, toExceptionMetadata(e), jobs);
        }
    } catch (NoSenderException e) {
        Log.w(TAG, "Invalid message, but no sender info!");
        return DecryptionResult.forNoop(jobs);
    }
}
Also used : ProtocolInvalidMessageException(org.signal.libsignal.metadata.ProtocolInvalidMessageException) ProtocolUntrustedIdentityException(org.signal.libsignal.metadata.ProtocolUntrustedIdentityException) ProtocolInvalidVersionException(org.signal.libsignal.metadata.ProtocolInvalidVersionException) InvalidMessageStructureException(org.whispersystems.signalservice.api.InvalidMessageStructureException) SelfSendException(org.signal.libsignal.metadata.SelfSendException) ProtocolInvalidKeyIdException(org.signal.libsignal.metadata.ProtocolInvalidKeyIdException) ProtocolDuplicateMessageException(org.signal.libsignal.metadata.ProtocolDuplicateMessageException) UnsupportedDataMessageException(org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) ProtocolLegacyMessageException(org.signal.libsignal.metadata.ProtocolLegacyMessageException) SignalServiceAccountDataStore(org.whispersystems.signalservice.api.SignalServiceAccountDataStore) AutomaticSessionResetJob(org.thoughtcrime.securesms.jobs.AutomaticSessionResetJob) RefreshPreKeysJob(org.thoughtcrime.securesms.jobs.RefreshPreKeysJob) SendRetryReceiptJob(org.thoughtcrime.securesms.jobs.SendRetryReceiptJob) Job(org.thoughtcrime.securesms.jobmanager.Job) RefreshPreKeysJob(org.thoughtcrime.securesms.jobs.RefreshPreKeysJob) ProtocolNoSessionException(org.signal.libsignal.metadata.ProtocolNoSessionException) SignalServiceCipher(org.whispersystems.signalservice.api.crypto.SignalServiceCipher) Recipient(org.thoughtcrime.securesms.recipients.Recipient) AutomaticSessionResetJob(org.thoughtcrime.securesms.jobs.AutomaticSessionResetJob) LinkedList(java.util.LinkedList) InvalidMetadataMessageException(org.signal.libsignal.metadata.InvalidMetadataMessageException) ProtocolInvalidKeyException(org.signal.libsignal.metadata.ProtocolInvalidKeyException) InvalidMetadataVersionException(org.signal.libsignal.metadata.InvalidMetadataVersionException) NonNull(androidx.annotation.NonNull)

Example 24 with Job

use of org.thoughtcrime.securesms.jobmanager.Job in project Signal-Android by signalapp.

the class RetrieveProfileJob method forRecipients.

/**
 * Works for any RecipientId, whether it's an individual, group, or yourself.
 *
 * @return A list of length 2 or less. Two iff you are in the recipients.
 */
@WorkerThread
@NonNull
public static List<Job> forRecipients(@NonNull Set<RecipientId> recipientIds) {
    Context context = ApplicationDependencies.getApplication();
    Set<RecipientId> combined = new HashSet<>(recipientIds.size());
    boolean includeSelf = false;
    for (RecipientId recipientId : recipientIds) {
        Recipient recipient = Recipient.resolved(recipientId);
        if (recipient.isSelf()) {
            includeSelf = true;
        } else if (recipient.isGroup()) {
            List<Recipient> recipients = SignalDatabase.groups().getGroupMembers(recipient.requireGroupId(), GroupDatabase.MemberSet.FULL_MEMBERS_EXCLUDING_SELF);
            combined.addAll(Stream.of(recipients).map(Recipient::getId).toList());
        } else {
            combined.add(recipientId);
        }
    }
    List<Job> jobs = new ArrayList<>(2);
    if (includeSelf) {
        jobs.add(new RefreshOwnProfileJob());
    }
    if (combined.size() > 0) {
        jobs.add(new RetrieveProfileJob(combined));
    }
    return jobs;
}
Also used : Context(android.content.Context) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) ArrayList(java.util.ArrayList) Recipient(org.thoughtcrime.securesms.recipients.Recipient) List(java.util.List) ArrayList(java.util.ArrayList) Job(org.thoughtcrime.securesms.jobmanager.Job) HashSet(java.util.HashSet) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Example 25 with Job

use of org.thoughtcrime.securesms.jobmanager.Job in project Signal-Android by signalapp.

the class PaymentSendJob method enqueuePayment.

/**
 * @param totalFee Total quoted totalFee to the user. This is expected to cover all defrags and the actual transaction.
 */
@AnyThread
@NonNull
public static UUID enqueuePayment(@Nullable RecipientId recipientId, @NonNull MobileCoinPublicAddress publicAddress, @NonNull String note, @NonNull Money amount, @NonNull Money totalFee) {
    UUID uuid = UUID.randomUUID();
    long timestamp = System.currentTimeMillis();
    Job sendJob = new PaymentSendJob(new Parameters.Builder().setQueue(QUEUE).setMaxAttempts(1).build(), uuid, timestamp, recipientId, Objects.requireNonNull(publicAddress), note, amount, totalFee);
    JobManager.Chain chain = ApplicationDependencies.getJobManager().startChain(sendJob).then(new PaymentTransactionCheckJob(uuid, QUEUE)).then(new MultiDeviceOutgoingPaymentSyncJob(uuid));
    if (recipientId != null) {
        chain.then(new PaymentNotificationSendJob(recipientId, uuid, recipientId.toQueueKey(true)));
    }
    chain.then(PaymentLedgerUpdateJob.updateLedgerToReflectPayment(uuid)).enqueue();
    return uuid;
}
Also used : JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) UUID(java.util.UUID) Job(org.thoughtcrime.securesms.jobmanager.Job) NonNull(androidx.annotation.NonNull) AnyThread(androidx.annotation.AnyThread)

Aggregations

Job (org.thoughtcrime.securesms.jobmanager.Job)26 JobManager (org.thoughtcrime.securesms.jobmanager.JobManager)16 NonNull (androidx.annotation.NonNull)14 Recipient (org.thoughtcrime.securesms.recipients.Recipient)12 WorkerThread (androidx.annotation.WorkerThread)10 List (java.util.List)10 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)10 Context (android.content.Context)8 Stream (com.annimon.stream.Stream)8 IOException (java.io.IOException)8 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)8 AttachmentCompressionJob (org.thoughtcrime.securesms.jobs.AttachmentCompressionJob)8 AttachmentCopyJob (org.thoughtcrime.securesms.jobs.AttachmentCopyJob)8 AttachmentMarkUploadedJob (org.thoughtcrime.securesms.jobs.AttachmentMarkUploadedJob)8 AttachmentUploadJob (org.thoughtcrime.securesms.jobs.AttachmentUploadJob)8 MmsSendJob (org.thoughtcrime.securesms.jobs.MmsSendJob)8 ProfileKeySendJob (org.thoughtcrime.securesms.jobs.ProfileKeySendJob)8 PushGroupSendJob (org.thoughtcrime.securesms.jobs.PushGroupSendJob)8 PushMediaSendJob (org.thoughtcrime.securesms.jobs.PushMediaSendJob)8 PushTextSendJob (org.thoughtcrime.securesms.jobs.PushTextSendJob)8