1 /* 2 * 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 androidx.work.StopReason 20 import androidx.work.WorkInfo 21 import androidx.work.WorkerParameters 22 import androidx.work.WorkerParameters.RuntimeExtras 23 import androidx.work.impl.model.WorkSpec 24 import androidx.work.impl.utils.StopWorkRunnable 25 import androidx.work.impl.utils.taskexecutor.TaskExecutor 26 27 interface WorkLauncher { 28 startWorknull29 fun startWork(workSpecId: StartStopToken) { 30 startWork(workSpecId, null) 31 } 32 33 /** 34 * @param workSpecId The [WorkSpec] id to start 35 * @param runtimeExtras The [WorkerParameters.RuntimeExtras] associated with this work 36 */ startWorknull37 fun startWork(workSpecId: StartStopToken, runtimeExtras: RuntimeExtras?) 38 39 /** @param workSpecId The [WorkSpec] id to stop */ 40 fun stopWork(workSpecId: StartStopToken) { 41 stopWork(workSpecId, WorkInfo.STOP_REASON_UNKNOWN) 42 } 43 stopWorknull44 fun stopWork(workSpecId: StartStopToken, @StopReason reason: Int) 45 46 fun stopWorkWithReason(workSpecId: StartStopToken, @StopReason reason: Int) = 47 stopWork(workSpecId, reason) 48 } 49 50 class WorkLauncherImpl( 51 val processor: Processor, 52 val workTaskExecutor: TaskExecutor, 53 ) : WorkLauncher { 54 override fun startWork(workSpecId: StartStopToken, runtimeExtras: RuntimeExtras?) { 55 workTaskExecutor.executeOnTaskThread { processor.startWork(workSpecId, runtimeExtras) } 56 } 57 58 override fun stopWork(workSpecId: StartStopToken, @StopReason reason: Int) { 59 workTaskExecutor.executeOnTaskThread(StopWorkRunnable(processor, workSpecId, false, reason)) 60 } 61 } 62