Java Code Examples for androidx.work.WorkManager#getInstance()

The following examples show how to use androidx.work.WorkManager#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComposeRepository.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
public void sendEmail(IdentifiableIdentity identity, ComposeViewModel.Draft draft, final Collection<String> inReplyTo) {
    final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SendEmailWorker.class)
            .setConstraints(CONNECTED_CONSTRAINT)
            .setInputData(SendEmailWorker.data(
                    requireAccount().id,
                    identity.getId(),
                    inReplyTo,
                    draft.getTo(),
                    draft.getCc(),
                    draft.getSubject(),
                    draft.getBody()
            ))
            .build();
    final WorkManager workManager = WorkManager.getInstance(application);
    workManager.enqueue(workRequest);
}
 
Example 2
Source File: ComposeRepository.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
public boolean discard(EditableEmail editableEmail) {
    final OneTimeWorkRequest discardDraft = new OneTimeWorkRequest.Builder(DiscardDraftWorker.class)
            .setConstraints(CONNECTED_CONSTRAINT)
            .setInputData(DiscardDraftWorker.data(requireAccount().id, editableEmail.id))
            .build();
    final WorkManager workManager = WorkManager.getInstance(application);
    final boolean isOnlyEmailInThread = editableEmail.isOnlyEmailInThread();
    if (isOnlyEmailInThread) {
        insertQueryItemOverwrite(editableEmail.threadId);
    }
    workManager.enqueue(discardDraft);
    return isOnlyEmailInThread;
}
 
Example 3
Source File: LttrsRepository.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
private void markImportantNow(final Collection<String> threadIds) {
    requireDatabase().overwriteDao().insertMailboxOverwrites(
            MailboxOverwriteEntity.of(threadIds, Role.IMPORTANT, true)
    );
    deleteQueryItemOverwrite(threadIds, Role.IMPORTANT);
    final WorkManager workManager = WorkManager.getInstance(application);
    for(final String threadId : threadIds) {
        final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(MarkImportantWorker.class)
                .setConstraints(CONNECTED_CONSTRAINT)
                .setInputData(MarkImportantWorker.data(requireAccount().id, threadId))
                .addTag(AbstractMuaWorker.TAG_EMAIL_MODIFICATION)
                .build();
        workManager.enqueueUniqueWork(
                MarkImportantWorker.uniqueName(threadId),
                ExistingWorkPolicy.REPLACE,
                workRequest
        );
    }
}
 
Example 4
Source File: LttrsRepository.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
private void markNotImportant(final Collection<String> threadIds, final IdentifiableMailboxWithRole mailbox) {
    Preconditions.checkArgument(mailbox.getRole() == Role.IMPORTANT);
    insertQueryItemOverwrite(threadIds, mailbox);
    requireDatabase().overwriteDao().insertMailboxOverwrites(
            MailboxOverwriteEntity.of(threadIds, Role.IMPORTANT, false)
    );
    final WorkManager workManager = WorkManager.getInstance(application);
    for(final String threadId : threadIds) {
        final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(RemoveFromMailboxWorker.class)
                .setConstraints(CONNECTED_CONSTRAINT)
                .setInputData(RemoveFromMailboxWorker.data(requireAccount().id, threadId, mailbox))
                .addTag(AbstractMuaWorker.TAG_EMAIL_MODIFICATION)
                .setInitialDelay(INITIAL_DELAY_DURATION, INITIAL_DELAY_TIME_UNIT)
                .build();
        workManager.enqueueUniqueWork(
                MarkImportantWorker.uniqueName(threadId),
                ExistingWorkPolicy.REPLACE,
                workRequest
        );
    }
}
 
Example 5
Source File: ThreadViewModel.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
public void waitForEdit(UUID uuid) {
    final WorkManager workManager = WorkManager.getInstance(getApplication());
    LiveData<WorkInfo> liveData = workManager.getWorkInfoByIdLiveData(uuid);
    liveData.observeForever(new Observer<WorkInfo>() {
        @Override
        public void onChanged(WorkInfo workInfo) {
            if (workInfo.getState() == WorkInfo.State.SUCCEEDED) {
                final Data data = workInfo.getOutputData();
                final String threadId = data.getString("threadId");
                if (threadId != null && !ThreadViewModel.this.threadId.equals(threadId)) {
                    LOGGER.info("redirecting to thread {}", threadId);
                    threadViewRedirect.postValue(new Event<>(threadId));
                }
                liveData.removeObserver(this);
            } else if (workInfo.getState() == WorkInfo.State.FAILED) {
                liveData.removeObserver(this);
            }
        }
    });
}
 
Example 6
Source File: MessagesAdapter.java    From tindroid with Apache License 2.0 6 votes vote down vote up
private boolean cancelUpload(long msgId) {
    final String uniqueID = Long.toString(msgId);

    WorkManager wm = WorkManager.getInstance(mActivity);
    WorkInfo.State state = null;
    try {
        List<WorkInfo> lwi = wm.getWorkInfosForUniqueWork(uniqueID).get();
        if (!lwi.isEmpty()) {
            WorkInfo wi = lwi.get(0);
            state = wi.getState();
        }
    } catch (ExecutionException | InterruptedException ignored) {
    }

    if (state == null || !state.isFinished()) {
        wm.cancelUniqueWork(Long.toString(msgId));
        return true;
    }
    return state == WorkInfo.State.CANCELLED;
}
 
Example 7
Source File: JobProxyWorkManager.java    From android-job with Apache License 2.0 6 votes vote down vote up
private WorkManager getWorkManager() {
    // don't cache the instance, it could change under the hood, e.g. during tests
    WorkManager workManager;
    try {
        workManager = WorkManager.getInstance(mContext);
    } catch (Throwable t) {
        workManager = null;
    }
    if (workManager == null) {
        try {
            WorkManager.initialize(mContext, new Configuration.Builder().build());
            workManager = WorkManager.getInstance(mContext);
        } catch (Throwable ignored) {
        }
        CAT.w("WorkManager getInstance() returned null, now: %s", workManager);
    }

    return workManager;
}
 
Example 8
Source File: CandyBarArtWorker.java    From candybar with Apache License 2.0 5 votes vote down vote up
public static void enqueueLoad(Context context) {
    WorkManager manager = WorkManager.getInstance(context);
    Constraints constraints = new Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .build();
    WorkRequest request = new OneTimeWorkRequest.Builder(CandyBarArtWorker.class)
            .setConstraints(constraints)
            .build();
    manager.enqueue(request);
}
 
Example 9
Source File: ComposeRepository.java    From lttrs-android with Apache License 2.0 5 votes vote down vote up
public void submitEmail(IdentityWithNameAndEmail identity, EditableEmail editableEmail) {
    final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SubmitEmailWorker.class)
            .setConstraints(CONNECTED_CONSTRAINT)
            .setInputData(SubmitEmailWorker.data(
                    requireAccount().id,
                    identity.getId(),
                    editableEmail.id
            ))
            .build();
    final WorkManager workManager = WorkManager.getInstance(application);
    workManager.enqueue(workRequest);
}
 
Example 10
Source File: ComposeRepository.java    From lttrs-android with Apache License 2.0 5 votes vote down vote up
public UUID saveDraft(final IdentifiableIdentity identity,
                      final ComposeViewModel.Draft draft,
                      final Collection<String> inReplyTo,
                      final EditableEmail discard) {
    final OneTimeWorkRequest saveDraftRequest = new OneTimeWorkRequest.Builder(SaveDraftWorker.class)
            .setConstraints(CONNECTED_CONSTRAINT)
            .setInputData(SendEmailWorker.data(
                    requireAccount().id,
                    identity.getId(),
                    inReplyTo,
                    draft.getTo(),
                    draft.getCc(),
                    draft.getSubject(),
                    draft.getBody()
            ))
            .build();
    final WorkManager workManager = WorkManager.getInstance(application);
    WorkContinuation continuation = workManager.beginWith(saveDraftRequest);
    if (discard != null) {
        final OneTimeWorkRequest discardPreviousDraft = new OneTimeWorkRequest.Builder(DiscardDraftWorker.class)
                .setConstraints(CONNECTED_CONSTRAINT)
                .setInputData(DiscardDraftWorker.data(requireAccount().id, discard.id))
                .build();
        continuation = continuation.then(discardPreviousDraft);
    }
    continuation.enqueue();
    return saveDraftRequest.getId();
}
 
Example 11
Source File: AbstractQueryViewModel.java    From lttrs-android with Apache License 2.0 5 votes vote down vote up
AbstractQueryViewModel(@NonNull Application application, ListenableFuture<AccountWithCredentials> account) {
    super(application);

    final WorkManager workManager = WorkManager.getInstance(application);

    this.queryRepository = new QueryRepository(application, account);
    this.important = this.queryRepository.getImportant();
    this.emailModificationWorkInfo = Transformations.map(workManager.getWorkInfosByTagLiveData(AbstractMuaWorker.TAG_EMAIL_MODIFICATION), WorkInfoUtil::allDone);
}
 
Example 12
Source File: GndApplicationModule.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@Provides
@Singleton
static WorkManager workManager() {
  return WorkManager.getInstance();
}