1 /*
<lambda>null2 * Copyright 2018 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 @file:JvmName("StatusRunnable")
17
18 package androidx.work.impl.utils
19
20 import androidx.work.WorkInfo
21 import androidx.work.WorkQuery
22 import androidx.work.executeAsync
23 import androidx.work.impl.WorkDatabase
24 import androidx.work.impl.model.WorkSpec.Companion.WORK_INFO_MAPPER
25 import androidx.work.impl.utils.taskexecutor.TaskExecutor
26 import com.google.common.util.concurrent.ListenableFuture
27 import java.util.UUID
28
29 internal fun WorkDatabase.forStringIds(
30 executor: TaskExecutor,
31 ids: List<String>,
32 ): ListenableFuture<List<WorkInfo>> =
33 loadStatusFuture(executor) { db ->
34 WORK_INFO_MAPPER.apply(db.workSpecDao().getWorkStatusPojoForIds(ids))
35 }
36
forUUIDnull37 internal fun WorkDatabase.forUUID(
38 executor: TaskExecutor,
39 id: UUID,
40 ): ListenableFuture<WorkInfo?> =
41 loadStatusFuture(executor) { db ->
42 db.workSpecDao().getWorkStatusPojoForId(id.toString())?.toWorkInfo()
43 }
44
forTagnull45 internal fun WorkDatabase.forTag(
46 executor: TaskExecutor,
47 tag: String
48 ): ListenableFuture<List<WorkInfo>> =
49 loadStatusFuture(executor) { db ->
50 WORK_INFO_MAPPER.apply(db.workSpecDao().getWorkStatusPojoForTag(tag))
51 }
52
forUniqueWorknull53 internal fun WorkDatabase.forUniqueWork(
54 executor: TaskExecutor,
55 name: String,
56 ): ListenableFuture<List<WorkInfo>> =
57 loadStatusFuture(executor) { db ->
58 WORK_INFO_MAPPER.apply(db.workSpecDao().getWorkStatusPojoForName(name))
59 }
60
forWorkQuerySpecnull61 internal fun WorkDatabase.forWorkQuerySpec(
62 executor: TaskExecutor,
63 querySpec: WorkQuery
64 ): ListenableFuture<List<WorkInfo>> =
65 loadStatusFuture(executor) { db ->
66 WORK_INFO_MAPPER.apply(db.rawWorkInfoDao().getWorkInfoPojos(querySpec.toRawQuery()))
67 }
68
69 // it should be rewritten via SuspendToFutureAdapter.launchFuture once it is stable.
loadStatusFuturenull70 private fun <T> WorkDatabase.loadStatusFuture(
71 executor: TaskExecutor,
72 block: (WorkDatabase) -> T
73 ): ListenableFuture<T> =
74 executor.serialTaskExecutor.executeAsync("loadStatusFuture") { block(this@loadStatusFuture) }
75