/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.work.impl.utils;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.work.Logger;
import androidx.work.WorkInfo;
import androidx.work.impl.Processor;
import androidx.work.impl.WorkDatabase;
import androidx.work.impl.WorkManagerImpl;
import androidx.work.impl.model.WorkSpecDao;

/**
 * A {@link Runnable} that can stop work and set the {@link WorkInfo.State} to
 * {@link WorkInfo.State#ENQUEUED} if it's in {@link WorkInfo.State#RUNNING}.
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class StopWorkRunnable implements Runnable {

    private static final String TAG = Logger.tagWithPrefix("StopWorkRunnable");

    private final WorkManagerImpl mWorkManagerImpl;
    private final String mWorkSpecId;
    private final boolean mStopInForeground;

    public StopWorkRunnable(
            @NonNull WorkManagerImpl workManagerImpl,
            @NonNull String workSpecId,
            boolean stopInForeground) {
        mWorkManagerImpl = workManagerImpl;
        mWorkSpecId = workSpecId;
        mStopInForeground = stopInForeground;
    }

    @Override
    public void run() {
        WorkDatabase workDatabase = mWorkManagerImpl.getWorkDatabase();
        Processor processor = mWorkManagerImpl.getProcessor();
        WorkSpecDao workSpecDao = workDatabase.workSpecDao();
        workDatabase.beginTransaction();
        try {
            boolean isForegroundWork = processor.isEnqueuedInForeground(mWorkSpecId);
            boolean isStopped;
            if (mStopInForeground) {
                isStopped = mWorkManagerImpl
                        .getProcessor()
                        .stopForegroundWork(mWorkSpecId);
            } else {
                if (!isForegroundWork
                        && workSpecDao.getState(mWorkSpecId) == WorkInfo.State.RUNNING) {
                    workSpecDao.setState(WorkInfo.State.ENQUEUED, mWorkSpecId);
                }
                // This call is safe to make for foreground work because Processor ignores requests
                // to stop for foreground work.
                isStopped = mWorkManagerImpl
                        .getProcessor()
                        .stopWork(mWorkSpecId);
            }

            Logger.get().debug(
                    TAG,
                    "StopWorkRunnable for " + mWorkSpecId + "; Processor.stopWork = " + isStopped);

            workDatabase.setTransactionSuccessful();
        } finally {
            workDatabase.endTransaction();
        }
    }
}

@@ -774,344 +774,124 @@
 ork.
-WorkInfo;%0Aimport androidx.work.impl.Processor;%0Aimport androidx.work.impl.WorkDatabase;%0Aimport androidx.work.impl.WorkManagerImpl;%0Aimport androidx.work.impl.model.WorkSpecDao;%0A%0A/**%0A * A %7B@link Runnable%7D that can stop work and set the %7B@link WorkInfo.State%7D to%0A * %7B@link WorkInfo.State#ENQUEUED%7D if it's in %7B@link WorkInfo.State#RUNNING%7D.
+impl.WorkManagerImpl;%0A%0A/**%0A * A %7B@link Runnable%7D that requests %7B@link androidx.work.impl.Processor%7D to stop the work
 %0A * 
@@ -1503,25 +1503,24 @@
 ound;%0A    %7D%0A
-%0A
     @Overrid
@@ -1549,350 +1549,8 @@
 ) %7B%0A
-        WorkDatabase workDatabase = mWorkManagerImpl.getWorkDatabase();%0A        Processor processor = mWorkManagerImpl.getProcessor();%0A        WorkSpecDao workSpecDao = workDatabase.workSpecDao();%0A        workDatabase.beginTransaction();%0A        try %7B%0A            boolean isForegroundWork = processor.isEnqueuedInForeground(mWorkSpecId);%0A    
     
@@ -1572,20 +1572,16 @@
 topped;%0A
-    
         
@@ -1609,36 +1609,32 @@
 ) %7B%0A            
- 
-   
 isStopped = mWor
@@ -1658,36 +1658,32 @@
                 
-  
-  
 .getProcessor()%0A
@@ -1674,36 +1674,32 @@
 .getProcessor()%0A
-    
                 
@@ -1744,20 +1744,16 @@
         
-    
 %7D else %7B
@@ -1757,238 +1757,8 @@
 e %7B%0A
-                if (!isForegroundWork%0A                        && workSpecDao.getState(mWorkSpecId) == WorkInfo.State.RUNNING) %7B%0A                    workSpecDao.setState(WorkInfo.State.ENQUEUED, mWorkSpecId);%0A                %7D%0A    
     
@@ -1861,20 +1861,16 @@
         
-    
 // to st
@@ -1897,36 +1897,32 @@
 rk.%0A            
-   
- 
 isStopped = mWor
@@ -1946,36 +1946,32 @@
                 
-    
 .getProcessor()%0A
@@ -1986,28 +1986,24 @@
             
- 
-   
 .stopWork(mW
@@ -2026,19 +2026,11 @@
     
-  
-  
 %7D%0A%0A
-    
     
@@ -2073,21 +2073,13 @@
     
-    
 TAG,%0A
-    
     
@@ -2175,134 +2175,8 @@
 );%0A%0A
-            workDatabase.setTransactionSuccessful();%0A        %7D finally %7B%0A            workDatabase.endTransaction();%0A        %7D%0A
     

Stop calling setStatus in StopWorkRunnable