Rahul Ravikumar | 05ee9f8 | 2019-07-15 10:31:14 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package androidx.work.impl.utils; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | |
| 21 | import androidx.annotation.NonNull; |
| 22 | import androidx.annotation.RestrictTo; |
| 23 | import androidx.work.Data; |
| 24 | import androidx.work.Logger; |
| 25 | import androidx.work.ProgressUpdater; |
| 26 | import androidx.work.WorkInfo.State; |
| 27 | import androidx.work.impl.WorkDatabase; |
| 28 | import androidx.work.impl.model.WorkProgress; |
| 29 | import androidx.work.impl.utils.futures.SettableFuture; |
| 30 | import androidx.work.impl.utils.taskexecutor.TaskExecutor; |
| 31 | |
| 32 | import com.google.common.util.concurrent.ListenableFuture; |
| 33 | |
| 34 | import java.util.UUID; |
| 35 | |
| 36 | /** |
| 37 | * Persists {@link androidx.work.ListenableWorker} progress in a {@link WorkDatabase}. |
| 38 | * |
| 39 | * @hide |
| 40 | */ |
| 41 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) |
| 42 | public class WorkProgressUpdater implements ProgressUpdater { |
| 43 | // Synthetic access |
| 44 | static final String TAG = Logger.tagWithPrefix("WorkProgressUpdater"); |
| 45 | |
| 46 | // Synthetic access |
| 47 | final WorkDatabase mWorkDatabase; |
| 48 | // Synthetic access |
| 49 | final TaskExecutor mTaskExecutor; |
| 50 | |
| 51 | public WorkProgressUpdater( |
| 52 | @NonNull WorkDatabase workDatabase, |
| 53 | @NonNull TaskExecutor taskExecutor) { |
| 54 | mWorkDatabase = workDatabase; |
| 55 | mTaskExecutor = taskExecutor; |
| 56 | } |
| 57 | |
| 58 | @NonNull |
| 59 | @Override |
| 60 | public ListenableFuture<Void> updateProgress( |
| 61 | @NonNull final Context context, |
| 62 | @NonNull final UUID id, |
| 63 | @NonNull final Data data) { |
| 64 | final SettableFuture<Void> future = SettableFuture.create(); |
| 65 | mTaskExecutor.executeOnBackgroundThread(new Runnable() { |
| 66 | @Override |
| 67 | public void run() { |
| 68 | String workSpecId = id.toString(); |
| 69 | Logger.get().info(TAG, String.format("Updating progress for %s (%s)", id, data)); |
| 70 | mWorkDatabase.beginTransaction(); |
| 71 | try { |
| 72 | State state = mWorkDatabase.workSpecDao().getState(workSpecId); |
| 73 | if (state == null) { |
| 74 | Logger.get().warning(TAG, |
| 75 | String.format( |
| 76 | "Ignoring setProgress(...). WorkSpec (%s) does not exist.", |
| 77 | workSpecId)); |
| 78 | } else if (state.isFinished()) { |
| 79 | Logger.get().warning(TAG, |
| 80 | String.format( |
| 81 | "Ignoring setProgress(...). WorkSpec (%s) has finished " |
| 82 | + "execution.", |
| 83 | workSpecId)); |
| 84 | } else { |
| 85 | WorkProgress progress = new WorkProgress(workSpecId, data); |
| 86 | mWorkDatabase.workProgressDao().insert(progress); |
| 87 | } |
| 88 | future.set(null); |
| 89 | mWorkDatabase.setTransactionSuccessful(); |
| 90 | } catch (Throwable throwable) { |
| 91 | Logger.get().error(TAG, "Error updating Worker progress", throwable); |
| 92 | future.setException(throwable); |
| 93 | } finally { |
| 94 | mWorkDatabase.endTransaction(); |
| 95 | } |
| 96 | } |
| 97 | }); |
| 98 | return future; |
| 99 | } |
| 100 | } |