package androidx.work.impl; import android.content.Context; import android.os.PowerManager; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RestrictTo; import androidx.core.content.ContextCompat; import androidx.work.Configuration; import androidx.work.ForegroundInfo; import androidx.work.Logger; import androidx.work.WorkerParameters; import androidx.work.impl.WorkerWrapper; import androidx.work.impl.foreground.ForegroundProcessor; import androidx.work.impl.foreground.SystemForegroundDispatcher; import androidx.work.impl.model.WorkGenerationalId; import androidx.work.impl.model.WorkSpec; import androidx.work.impl.utils.WakeLocks; import androidx.work.impl.utils.taskexecutor.TaskExecutor; import com.google.common.util.concurrent.ListenableFuture; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP}) /* loaded from: classes.dex */ public class Processor implements ForegroundProcessor { private static final String FOREGROUND_WAKELOCK_TAG = "ProcessorForegroundLck"; private static final String TAG = Logger.tagWithPrefix("Processor"); private Context mAppContext; private Configuration mConfiguration; private WorkDatabase mWorkDatabase; private TaskExecutor mWorkTaskExecutor; private Map mEnqueuedWorkMap = new HashMap(); private Map mForegroundWorkMap = new HashMap(); private Set mCancelledIds = new HashSet(); private final List mOuterListeners = new ArrayList(); @Nullable private PowerManager.WakeLock mForegroundLock = null; private final Object mLock = new Object(); private Map> mWorkRuns = new HashMap(); public Processor(@NonNull Context context, @NonNull Configuration configuration, @NonNull TaskExecutor taskExecutor, @NonNull WorkDatabase workDatabase) { this.mAppContext = context; this.mConfiguration = configuration; this.mWorkTaskExecutor = taskExecutor; this.mWorkDatabase = workDatabase; } public boolean startWork(@NonNull StartStopToken startStopToken) { return startWork(startStopToken, null); } public boolean startWork(@NonNull StartStopToken startStopToken, @Nullable WorkerParameters.RuntimeExtras runtimeExtras) { WorkGenerationalId id = startStopToken.getId(); final String workSpecId = id.getWorkSpecId(); final ArrayList arrayList = new ArrayList(); WorkSpec workSpec = (WorkSpec) this.mWorkDatabase.runInTransaction(new Callable() { // from class: androidx.work.impl.Processor$$ExternalSyntheticLambda0 @Override // java.util.concurrent.Callable public final Object call() { WorkSpec lambda$startWork$0; lambda$startWork$0 = Processor.this.lambda$startWork$0(arrayList, workSpecId); return lambda$startWork$0; } }); if (workSpec == null) { Logger.get().warning(TAG, "Didn't find WorkSpec for id " + id); runOnExecuted(id, false); return false; } synchronized (this.mLock) { try { if (isEnqueued(workSpecId)) { Set set = this.mWorkRuns.get(workSpecId); if (set.iterator().next().getId().getGeneration() == id.getGeneration()) { set.add(startStopToken); Logger.get().debug(TAG, "Work " + id + " is already enqueued for processing"); } else { runOnExecuted(id, false); } return false; } if (workSpec.getGeneration() != id.getGeneration()) { runOnExecuted(id, false); return false; } final WorkerWrapper build = new WorkerWrapper.Builder(this.mAppContext, this.mConfiguration, this.mWorkTaskExecutor, this, this.mWorkDatabase, workSpec, arrayList).withRuntimeExtras(runtimeExtras).build(); final ListenableFuture launch = build.launch(); launch.addListener(new Runnable() { // from class: androidx.work.impl.Processor$$ExternalSyntheticLambda1 @Override // java.lang.Runnable public final void run() { Processor.this.lambda$startWork$1(launch, build); } }, this.mWorkTaskExecutor.getMainThreadExecutor()); this.mEnqueuedWorkMap.put(workSpecId, build); HashSet hashSet = new HashSet(); hashSet.add(startStopToken); this.mWorkRuns.put(workSpecId, hashSet); Logger.get().debug(TAG, getClass().getSimpleName() + ": processing " + id); return true; } catch (Throwable th) { throw th; } } } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ WorkSpec lambda$startWork$0(ArrayList arrayList, String str) throws Exception { arrayList.addAll(this.mWorkDatabase.workTagDao().getTagsForWorkSpecId(str)); return this.mWorkDatabase.workSpecDao().getWorkSpec(str); } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ void lambda$startWork$1(ListenableFuture listenableFuture, WorkerWrapper workerWrapper) { boolean z; try { z = ((Boolean) listenableFuture.get()).booleanValue(); } catch (InterruptedException | ExecutionException unused) { z = true; } onExecuted(workerWrapper, z); } @Override // androidx.work.impl.foreground.ForegroundProcessor public void startForeground(@NonNull String str, @NonNull ForegroundInfo foregroundInfo) { synchronized (this.mLock) { try { Logger.get().info(TAG, "Moving WorkSpec (" + str + ") to the foreground"); WorkerWrapper remove = this.mEnqueuedWorkMap.remove(str); if (remove != null) { if (this.mForegroundLock == null) { PowerManager.WakeLock newWakeLock = WakeLocks.newWakeLock(this.mAppContext, FOREGROUND_WAKELOCK_TAG); this.mForegroundLock = newWakeLock; newWakeLock.acquire(); } this.mForegroundWorkMap.put(str, remove); ContextCompat.startForegroundService(this.mAppContext, SystemForegroundDispatcher.createStartForegroundIntent(this.mAppContext, remove.getWorkGenerationalId(), foregroundInfo)); } } catch (Throwable th) { throw th; } } } public boolean stopForegroundWork(@NonNull StartStopToken startStopToken, int i) { WorkerWrapper cleanUpWorkerUnsafe; String workSpecId = startStopToken.getId().getWorkSpecId(); synchronized (this.mLock) { cleanUpWorkerUnsafe = cleanUpWorkerUnsafe(workSpecId); } return interrupt(workSpecId, cleanUpWorkerUnsafe, i); } public boolean stopWork(@NonNull StartStopToken startStopToken, int i) { String workSpecId = startStopToken.getId().getWorkSpecId(); synchronized (this.mLock) { try { if (this.mForegroundWorkMap.get(workSpecId) != null) { Logger.get().debug(TAG, "Ignored stopWork. WorkerWrapper " + workSpecId + " is in foreground"); return false; } Set set = this.mWorkRuns.get(workSpecId); if (set != null && set.contains(startStopToken)) { return interrupt(workSpecId, cleanUpWorkerUnsafe(workSpecId), i); } return false; } finally { } } } public boolean stopAndCancelWork(@NonNull String str, int i) { WorkerWrapper cleanUpWorkerUnsafe; synchronized (this.mLock) { Logger.get().debug(TAG, "Processor cancelling " + str); this.mCancelledIds.add(str); cleanUpWorkerUnsafe = cleanUpWorkerUnsafe(str); } return interrupt(str, cleanUpWorkerUnsafe, i); } public boolean isCancelled(@NonNull String str) { boolean contains; synchronized (this.mLock) { contains = this.mCancelledIds.contains(str); } return contains; } public boolean hasWork() { boolean z; synchronized (this.mLock) { try { z = (this.mEnqueuedWorkMap.isEmpty() && this.mForegroundWorkMap.isEmpty()) ? false : true; } finally { } } return z; } public boolean isEnqueued(@NonNull String str) { boolean z; synchronized (this.mLock) { z = getWorkerWrapperUnsafe(str) != null; } return z; } public void addExecutionListener(@NonNull ExecutionListener executionListener) { synchronized (this.mLock) { this.mOuterListeners.add(executionListener); } } public void removeExecutionListener(@NonNull ExecutionListener executionListener) { synchronized (this.mLock) { this.mOuterListeners.remove(executionListener); } } private void onExecuted(@NonNull WorkerWrapper workerWrapper, boolean z) { synchronized (this.mLock) { try { WorkGenerationalId workGenerationalId = workerWrapper.getWorkGenerationalId(); String workSpecId = workGenerationalId.getWorkSpecId(); if (getWorkerWrapperUnsafe(workSpecId) == workerWrapper) { cleanUpWorkerUnsafe(workSpecId); } Logger.get().debug(TAG, getClass().getSimpleName() + " " + workSpecId + " executed; reschedule = " + z); Iterator it = this.mOuterListeners.iterator(); while (it.hasNext()) { it.next().onExecuted(workGenerationalId, z); } } catch (Throwable th) { throw th; } } } @Nullable private WorkerWrapper getWorkerWrapperUnsafe(@NonNull String str) { WorkerWrapper workerWrapper = this.mForegroundWorkMap.get(str); return workerWrapper == null ? this.mEnqueuedWorkMap.get(str) : workerWrapper; } @Nullable public WorkSpec getRunningWorkSpec(@NonNull String str) { synchronized (this.mLock) { try { WorkerWrapper workerWrapperUnsafe = getWorkerWrapperUnsafe(str); if (workerWrapperUnsafe == null) { return null; } return workerWrapperUnsafe.getWorkSpec(); } catch (Throwable th) { throw th; } } } private void runOnExecuted(@NonNull final WorkGenerationalId workGenerationalId, final boolean z) { this.mWorkTaskExecutor.getMainThreadExecutor().execute(new Runnable() { // from class: androidx.work.impl.Processor$$ExternalSyntheticLambda2 @Override // java.lang.Runnable public final void run() { Processor.this.lambda$runOnExecuted$2(workGenerationalId, z); } }); } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ void lambda$runOnExecuted$2(WorkGenerationalId workGenerationalId, boolean z) { synchronized (this.mLock) { try { Iterator it = this.mOuterListeners.iterator(); while (it.hasNext()) { it.next().onExecuted(workGenerationalId, z); } } catch (Throwable th) { throw th; } } } private void stopForegroundService() { synchronized (this.mLock) { try { if (!(!this.mForegroundWorkMap.isEmpty())) { try { this.mAppContext.startService(SystemForegroundDispatcher.createStopForegroundIntent(this.mAppContext)); } catch (Throwable th) { Logger.get().error(TAG, "Unable to stop foreground service", th); } PowerManager.WakeLock wakeLock = this.mForegroundLock; if (wakeLock != null) { wakeLock.release(); this.mForegroundLock = null; } } } catch (Throwable th2) { throw th2; } } } @Nullable private WorkerWrapper cleanUpWorkerUnsafe(@NonNull String str) { WorkerWrapper remove = this.mForegroundWorkMap.remove(str); boolean z = remove != null; if (!z) { remove = this.mEnqueuedWorkMap.remove(str); } this.mWorkRuns.remove(str); if (z) { stopForegroundService(); } return remove; } private static boolean interrupt(@NonNull String str, @Nullable WorkerWrapper workerWrapper, int i) { if (workerWrapper != null) { workerWrapper.interrupt(i); Logger.get().debug(TAG, "WorkerWrapper interrupted for " + str); return true; } Logger.get().debug(TAG, "WorkerWrapper could not be found for " + str); return false; } }