1 /*
<lambda>null2 * Copyright 2023 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
18
19 import android.content.Context
20 import androidx.work.Configuration
21 import androidx.work.R
22 import androidx.work.impl.background.greedy.GreedyScheduler
23 import androidx.work.impl.constraints.trackers.Trackers
24 import androidx.work.impl.utils.taskexecutor.TaskExecutor
25 import androidx.work.impl.utils.taskexecutor.WorkManagerTaskExecutor
26 import kotlinx.coroutines.CoroutineScope
27 import kotlinx.coroutines.Job
28 import kotlinx.coroutines.cancelAndJoin
29 import kotlinx.coroutines.runBlocking
30
31 @JvmName("createWorkManager")
32 @JvmOverloads
33 fun WorkManagerImpl(
34 context: Context,
35 configuration: Configuration,
36 workTaskExecutor: TaskExecutor = WorkManagerTaskExecutor(configuration.taskExecutor),
37 workDatabase: WorkDatabase =
38 WorkDatabase.create(
39 context.applicationContext,
40 workTaskExecutor.serialTaskExecutor,
41 configuration.clock,
42 context.resources.getBoolean(R.bool.workmanager_test_configuration)
43 ),
44 trackers: Trackers = Trackers(context.applicationContext, workTaskExecutor),
45 processor: Processor =
46 Processor(context.applicationContext, configuration, workTaskExecutor, workDatabase),
47 schedulersCreator: SchedulersCreator = ::createSchedulers
48 ): WorkManagerImpl {
49 val schedulers =
50 schedulersCreator(
51 context,
52 configuration,
53 workTaskExecutor,
54 workDatabase,
55 trackers,
56 processor
57 )
58 return WorkManagerImpl(
59 context.applicationContext,
60 configuration,
61 workTaskExecutor,
62 workDatabase,
63 schedulers,
64 processor,
65 trackers
66 )
67 }
68
69 @JvmName("createTestWorkManager")
TestWorkManagerImplnull70 fun TestWorkManagerImpl(
71 context: Context,
72 configuration: Configuration,
73 workTaskExecutor: TaskExecutor,
74 ) =
75 WorkManagerImpl(
76 context,
77 configuration,
78 workTaskExecutor,
79 WorkDatabase.create(context, workTaskExecutor.serialTaskExecutor, configuration.clock, true)
80 )
81
82 typealias SchedulersCreator =
83 (
84 context: Context,
85 configuration: Configuration,
86 workTaskExecutor: TaskExecutor,
87 workDatabase: WorkDatabase,
88 trackers: Trackers,
89 processor: Processor
90 ) -> List<Scheduler>
91
92 fun schedulers(vararg schedulers: Scheduler): SchedulersCreator = { _, _, _, _, _, _ ->
93 schedulers.toList()
94 }
95
createSchedulersnull96 private fun createSchedulers(
97 context: Context,
98 configuration: Configuration,
99 workTaskExecutor: TaskExecutor,
100 workDatabase: WorkDatabase,
101 trackers: Trackers,
102 processor: Processor,
103 ): List<Scheduler> =
104 listOf(
105 Schedulers.createBestAvailableBackgroundScheduler(context, workDatabase, configuration),
106 GreedyScheduler(
107 context,
108 configuration,
109 trackers,
110 processor,
111 WorkLauncherImpl(processor, workTaskExecutor),
112 workTaskExecutor
113 ),
114 )
115
116 @JvmName("createWorkManagerScope")
117 internal fun WorkManagerScope(taskExecutor: TaskExecutor) =
118 CoroutineScope(taskExecutor.taskCoroutineDispatcher)
119
120 fun WorkManagerImpl.close() {
121 runBlocking { workManagerScope.coroutineContext[Job]!!.cancelAndJoin() }
122 workDatabase.close()
123 }
124