122 lines
4.4 KiB
Java
122 lines
4.4 KiB
Java
package androidx.work.impl.utils;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.RestrictTo;
|
|
import androidx.annotation.VisibleForTesting;
|
|
import androidx.work.Logger;
|
|
import c.d.b.a.a;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.ThreadFactory;
|
|
import java.util.concurrent.TimeUnit;
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
public class WorkTimer {
|
|
private static final String TAG = Logger.tagWithPrefix("WorkTimer");
|
|
private final ThreadFactory mBackgroundThreadFactory;
|
|
private final ScheduledExecutorService mExecutorService;
|
|
public final Map<String, TimeLimitExceededListener> mListeners = new HashMap();
|
|
public final Object mLock = new Object();
|
|
public final Map<String, WorkTimerRunnable> mTimerMap = new HashMap();
|
|
|
|
/* renamed from: androidx.work.impl.utils.WorkTimer$1 reason: invalid class name */
|
|
public class AnonymousClass1 implements ThreadFactory {
|
|
private int mThreadsCreated = 0;
|
|
|
|
public AnonymousClass1() {
|
|
}
|
|
|
|
@Override // java.util.concurrent.ThreadFactory
|
|
public Thread newThread(@NonNull Runnable runnable) {
|
|
Thread newThread = Executors.defaultThreadFactory().newThread(runnable);
|
|
StringBuilder K = a.K("WorkManager-WorkTimer-thread-");
|
|
K.append(this.mThreadsCreated);
|
|
newThread.setName(K.toString());
|
|
this.mThreadsCreated++;
|
|
return newThread;
|
|
}
|
|
}
|
|
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
public interface TimeLimitExceededListener {
|
|
void onTimeLimitExceeded(@NonNull String str);
|
|
}
|
|
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
public static class WorkTimerRunnable implements Runnable {
|
|
public static final String TAG = "WrkTimerRunnable";
|
|
private final String mWorkSpecId;
|
|
private final WorkTimer mWorkTimer;
|
|
|
|
public WorkTimerRunnable(@NonNull WorkTimer workTimer, @NonNull String str) {
|
|
this.mWorkTimer = workTimer;
|
|
this.mWorkSpecId = str;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
synchronized (this.mWorkTimer.mLock) {
|
|
if (this.mWorkTimer.mTimerMap.remove(this.mWorkSpecId) != null) {
|
|
TimeLimitExceededListener remove = this.mWorkTimer.mListeners.remove(this.mWorkSpecId);
|
|
if (remove != null) {
|
|
remove.onTimeLimitExceeded(this.mWorkSpecId);
|
|
}
|
|
} else {
|
|
Logger.get().debug(TAG, String.format("Timer with %s is already marked as complete.", this.mWorkSpecId), new Throwable[0]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public WorkTimer() {
|
|
AnonymousClass1 r0 = new AnonymousClass1();
|
|
this.mBackgroundThreadFactory = r0;
|
|
this.mExecutorService = Executors.newSingleThreadScheduledExecutor(r0);
|
|
}
|
|
|
|
@NonNull
|
|
@VisibleForTesting
|
|
public ScheduledExecutorService getExecutorService() {
|
|
return this.mExecutorService;
|
|
}
|
|
|
|
@NonNull
|
|
@VisibleForTesting
|
|
public synchronized Map<String, TimeLimitExceededListener> getListeners() {
|
|
return this.mListeners;
|
|
}
|
|
|
|
@NonNull
|
|
@VisibleForTesting
|
|
public synchronized Map<String, WorkTimerRunnable> getTimerMap() {
|
|
return this.mTimerMap;
|
|
}
|
|
|
|
public void onDestroy() {
|
|
if (!this.mExecutorService.isShutdown()) {
|
|
this.mExecutorService.shutdownNow();
|
|
}
|
|
}
|
|
|
|
public void startTimer(@NonNull String str, long j, @NonNull TimeLimitExceededListener timeLimitExceededListener) {
|
|
synchronized (this.mLock) {
|
|
Logger.get().debug(TAG, String.format("Starting timer for %s", str), new Throwable[0]);
|
|
stopTimer(str);
|
|
WorkTimerRunnable workTimerRunnable = new WorkTimerRunnable(this, str);
|
|
this.mTimerMap.put(str, workTimerRunnable);
|
|
this.mListeners.put(str, timeLimitExceededListener);
|
|
this.mExecutorService.schedule(workTimerRunnable, j, TimeUnit.MILLISECONDS);
|
|
}
|
|
}
|
|
|
|
public void stopTimer(@NonNull String str) {
|
|
synchronized (this.mLock) {
|
|
if (this.mTimerMap.remove(str) != null) {
|
|
Logger.get().debug(TAG, String.format("Stopping timer for %s", str), new Throwable[0]);
|
|
this.mListeners.remove(str);
|
|
}
|
|
}
|
|
}
|
|
}
|