Search in sources :

Example 11 with JobManager

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

the class StorageAccountRestoreJob method onRun.

@Override
protected void onRun() throws Exception {
    SignalServiceAccountManager accountManager = ApplicationDependencies.getSignalServiceAccountManager();
    StorageKey storageServiceKey = SignalStore.storageService().getOrCreateStorageKey();
    Log.i(TAG, "Retrieving manifest...");
    Optional<SignalStorageManifest> manifest = accountManager.getStorageManifest(storageServiceKey);
    if (!manifest.isPresent()) {
        Log.w(TAG, "Manifest did not exist or was undecryptable (bad key). Not restoring. Force-pushing.");
        ApplicationDependencies.getJobManager().add(new StorageForcePushJob());
        return;
    }
    Log.i(TAG, "Resetting the local manifest to an empty state so that it will sync later.");
    SignalStore.storageService().setManifest(SignalStorageManifest.EMPTY);
    Optional<StorageId> accountId = manifest.get().getAccountStorageId();
    if (!accountId.isPresent()) {
        Log.w(TAG, "Manifest had no account record! Not restoring.");
        return;
    }
    Log.i(TAG, "Retrieving account record...");
    List<SignalStorageRecord> records = accountManager.readStorageRecords(storageServiceKey, Collections.singletonList(accountId.get()));
    SignalStorageRecord record = records.size() > 0 ? records.get(0) : null;
    if (record == null) {
        Log.w(TAG, "Could not find account record, even though we had an ID! Not restoring.");
        return;
    }
    SignalAccountRecord accountRecord = record.getAccount().orNull();
    if (accountRecord == null) {
        Log.w(TAG, "The storage record didn't actually have an account on it! Not restoring.");
        return;
    }
    Log.i(TAG, "Applying changes locally...");
    SignalDatabase.getRawDatabase().beginTransaction();
    try {
        StorageSyncHelper.applyAccountStorageSyncUpdates(context, Recipient.self(), accountRecord, false);
        SignalDatabase.getRawDatabase().setTransactionSuccessful();
    } finally {
        SignalDatabase.getRawDatabase().endTransaction();
    }
    JobManager jobManager = ApplicationDependencies.getJobManager();
    if (accountRecord.getAvatarUrlPath().isPresent()) {
        Log.i(TAG, "Fetching avatar...");
        Optional<JobTracker.JobState> state = jobManager.runSynchronously(new RetrieveProfileAvatarJob(Recipient.self(), accountRecord.getAvatarUrlPath().get()), LIFESPAN / 2);
        if (state.isPresent()) {
            Log.i(TAG, "Avatar retrieved successfully. " + state.get());
        } else {
            Log.w(TAG, "Avatar retrieval did not complete in time (or otherwise failed).");
        }
    } else {
        Log.i(TAG, "No avatar present. Not fetching.");
    }
    Log.i(TAG, "Refreshing attributes...");
    Optional<JobTracker.JobState> state = jobManager.runSynchronously(new RefreshAttributesJob(), LIFESPAN / 2);
    if (state.isPresent()) {
        Log.i(TAG, "Attributes refreshed successfully. " + state.get());
    } else {
        Log.w(TAG, "Attribute refresh did not complete in time (or otherwise failed).");
    }
}
Also used : SignalAccountRecord(org.whispersystems.signalservice.api.storage.SignalAccountRecord) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) StorageId(org.whispersystems.signalservice.api.storage.StorageId) StorageKey(org.whispersystems.signalservice.api.storage.StorageKey)

Example 12 with JobManager

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

the class MessageContentProcessor method handleSynchronizeStickerPackOperation.

private void handleSynchronizeStickerPackOperation(@NonNull List<StickerPackOperationMessage> stickerPackOperations, long envelopeTimestamp) {
    log(envelopeTimestamp, "Synchronize sticker pack operation.");
    JobManager jobManager = ApplicationDependencies.getJobManager();
    for (StickerPackOperationMessage operation : stickerPackOperations) {
        if (operation.getPackId().isPresent() && operation.getPackKey().isPresent() && operation.getType().isPresent()) {
            String packId = Hex.toStringCondensed(operation.getPackId().get());
            String packKey = Hex.toStringCondensed(operation.getPackKey().get());
            switch(operation.getType().get()) {
                case INSTALL:
                    jobManager.add(StickerPackDownloadJob.forInstall(packId, packKey, false));
                    break;
                case REMOVE:
                    SignalDatabase.stickers().uninstallPack(packId);
                    break;
            }
        } else {
            warn("Received incomplete sticker pack operation sync.");
        }
    }
}
Also used : StickerPackOperationMessage(org.whispersystems.signalservice.api.messages.multidevice.StickerPackOperationMessage) JobManager(org.thoughtcrime.securesms.jobmanager.JobManager)

Example 13 with JobManager

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

the class RestStrategy method execute.

@WorkerThread
@Override
public boolean execute(long timeout) {
    long startTime = System.currentTimeMillis();
    JobManager jobManager = ApplicationDependencies.getJobManager();
    QueueFindingJobListener queueListener = new QueueFindingJobListener();
    try (IncomingMessageProcessor.Processor processor = ApplicationDependencies.getIncomingMessageProcessor().acquire()) {
        jobManager.addListener(job -> job.getParameters().getQueue() != null && job.getParameters().getQueue().startsWith(PushProcessMessageJob.QUEUE_PREFIX), queueListener);
        int jobCount = enqueuePushDecryptJobs(processor, startTime, timeout);
        if (jobCount == 0) {
            Log.d(TAG, "No PushDecryptMessageJobs were enqueued.");
            return true;
        } else {
            Log.d(TAG, jobCount + " PushDecryptMessageJob(s) were enqueued.");
        }
        long timeRemainingMs = blockUntilQueueDrained(PushDecryptMessageJob.QUEUE, TimeUnit.SECONDS.toMillis(10));
        Set<String> processQueues = queueListener.getQueues();
        Log.d(TAG, "Discovered " + processQueues.size() + " queue(s): " + processQueues);
        if (timeRemainingMs > 0) {
            Iterator<String> iter = processQueues.iterator();
            while (iter.hasNext() && timeRemainingMs > 0) {
                timeRemainingMs = blockUntilQueueDrained(iter.next(), timeRemainingMs);
            }
            if (timeRemainingMs <= 0) {
                Log.w(TAG, "Ran out of time while waiting for queues to drain.");
            }
        } else {
            Log.w(TAG, "Ran out of time before we could even wait on individual queues!");
        }
        return true;
    } catch (IOException e) {
        Log.w(TAG, "Failed to retrieve messages. Resetting the SignalServiceMessageReceiver.", e);
        ApplicationDependencies.resetSignalServiceMessageReceiver();
        return false;
    } finally {
        jobManager.removeListener(queueListener);
    }
}
Also used : JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) IOException(java.io.IOException) WorkerThread(androidx.annotation.WorkerThread)

Example 14 with JobManager

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

the class RestStrategy method blockUntilQueueDrained.

private static long blockUntilQueueDrained(@NonNull String queue, long timeoutMs) {
    long startTime = System.currentTimeMillis();
    final JobManager jobManager = ApplicationDependencies.getJobManager();
    final MarkerJob markerJob = new MarkerJob(queue);
    Optional<JobTracker.JobState> jobState = jobManager.runSynchronously(markerJob, timeoutMs);
    if (!jobState.isPresent()) {
        Log.w(TAG, "Timed out waiting for " + queue + " job(s) to finish!");
    }
    long endTime = System.currentTimeMillis();
    long duration = endTime - startTime;
    Log.d(TAG, "Waited " + duration + " ms for the " + queue + " job(s) to finish.");
    return timeoutMs - duration;
}
Also used : JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) MarkerJob(org.thoughtcrime.securesms.jobs.MarkerJob)

Example 15 with JobManager

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

the class MessageRetrievalStrategy method blockUntilQueueDrained.

protected static void blockUntilQueueDrained(@NonNull String tag, @NonNull String queue, long timeoutMs) {
    long startTime = System.currentTimeMillis();
    final JobManager jobManager = ApplicationDependencies.getJobManager();
    final MarkerJob markerJob = new MarkerJob(queue);
    Optional<JobTracker.JobState> jobState = jobManager.runSynchronously(markerJob, timeoutMs);
    if (!jobState.isPresent()) {
        Log.w(tag, "Timed out waiting for " + queue + " job(s) to finish!");
    }
    long endTime = System.currentTimeMillis();
    long duration = endTime - startTime;
    Log.d(tag, "Waited " + duration + " ms for the " + queue + " job(s) to finish.");
}
Also used : JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) MarkerJob(org.thoughtcrime.securesms.jobs.MarkerJob)

Aggregations

JobManager (org.thoughtcrime.securesms.jobmanager.JobManager)58 Job (org.thoughtcrime.securesms.jobmanager.Job)18 IOException (java.io.IOException)14 List (java.util.List)14 NonNull (androidx.annotation.NonNull)10 Log (org.signal.core.util.logging.Log)10 SignalDatabase (org.thoughtcrime.securesms.database.SignalDatabase)10 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)10 Recipient (org.thoughtcrime.securesms.recipients.Recipient)10 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)10 WorkerThread (androidx.annotation.WorkerThread)8 TimeUnit (java.util.concurrent.TimeUnit)8 Data (org.thoughtcrime.securesms.jobmanager.Data)8 NetworkConstraint (org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint)8 Application (android.app.Application)6 ArrayList (java.util.ArrayList)6 Collections (java.util.Collections)6 MessageId (org.thoughtcrime.securesms.database.model.MessageId)6 SmsSendJob (org.thoughtcrime.securesms.jobs.SmsSendJob)6 AttachmentCompressionJob (org.thoughtcrime.securesms.jobs.AttachmentCompressionJob)5