package com.google.common.util.concurrent; import com.google.common.base.Preconditions; import com.google.common.base.Supplier; import com.google.common.util.concurrent.Service; import java.util.Objects; import java.util.concurrent.Callable; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Logger; import javax.annotation.CheckForNull; @ElementTypesAreNonnullByDefault /* loaded from: classes3.dex */ public abstract class AbstractScheduledService implements Service { private static final Logger logger = Logger.getLogger(AbstractScheduledService.class.getName()); private final AbstractService delegate = new ServiceDelegate(); /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes3.dex */ public interface Cancellable { void cancel(boolean z); boolean isCancelled(); } protected abstract void runOneIteration() throws Exception; protected abstract Scheduler scheduler(); protected void shutDown() throws Exception { } protected void startUp() throws Exception { } /* loaded from: classes3.dex */ public static abstract class Scheduler { abstract Cancellable schedule(AbstractService abstractService, ScheduledExecutorService scheduledExecutorService, Runnable runnable); public static Scheduler newFixedDelaySchedule(final long j, final long j2, final TimeUnit timeUnit) { Preconditions.checkNotNull(timeUnit); Preconditions.checkArgument(j2 > 0, "delay must be > 0, found %s", j2); return new Scheduler() { // from class: com.google.common.util.concurrent.AbstractScheduledService.Scheduler.1 /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ { super(); } @Override // com.google.common.util.concurrent.AbstractScheduledService.Scheduler public Cancellable schedule(AbstractService abstractService, ScheduledExecutorService scheduledExecutorService, Runnable runnable) { return new FutureAsCancellable(scheduledExecutorService.scheduleWithFixedDelay(runnable, j, j2, timeUnit)); } }; } public static Scheduler newFixedRateSchedule(final long j, final long j2, final TimeUnit timeUnit) { Preconditions.checkNotNull(timeUnit); Preconditions.checkArgument(j2 > 0, "period must be > 0, found %s", j2); return new Scheduler() { // from class: com.google.common.util.concurrent.AbstractScheduledService.Scheduler.2 /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ { super(); } @Override // com.google.common.util.concurrent.AbstractScheduledService.Scheduler public Cancellable schedule(AbstractService abstractService, ScheduledExecutorService scheduledExecutorService, Runnable runnable) { return new FutureAsCancellable(scheduledExecutorService.scheduleAtFixedRate(runnable, j, j2, timeUnit)); } }; } private Scheduler() { } } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes3.dex */ public final class ServiceDelegate extends AbstractService { @CheckForNull private volatile ScheduledExecutorService executorService; private final ReentrantLock lock; @CheckForNull private volatile Cancellable runningTask; private final Runnable task; private ServiceDelegate() { this.lock = new ReentrantLock(); this.task = new Task(); } /* loaded from: classes3.dex */ class Task implements Runnable { Task() { } @Override // java.lang.Runnable public void run() { ServiceDelegate.this.lock.lock(); try { } finally { try { } finally { } } if (((Cancellable) Objects.requireNonNull(ServiceDelegate.this.runningTask)).isCancelled()) { return; } AbstractScheduledService.this.runOneIteration(); } } @Override // com.google.common.util.concurrent.AbstractService protected final void doStart() { this.executorService = MoreExecutors.renamingDecorator(AbstractScheduledService.this.executor(), new Supplier() { // from class: com.google.common.util.concurrent.AbstractScheduledService.ServiceDelegate.1 @Override // com.google.common.base.Supplier public String get() { String serviceName = AbstractScheduledService.this.serviceName(); String valueOf = String.valueOf(ServiceDelegate.this.state()); return new StringBuilder(String.valueOf(serviceName).length() + 1 + String.valueOf(valueOf).length()).append(serviceName).append(" ").append(valueOf).toString(); } }); this.executorService.execute(new Runnable() { // from class: com.google.common.util.concurrent.AbstractScheduledService.ServiceDelegate.2 @Override // java.lang.Runnable public void run() { ServiceDelegate.this.lock.lock(); try { AbstractScheduledService.this.startUp(); ServiceDelegate serviceDelegate = ServiceDelegate.this; serviceDelegate.runningTask = AbstractScheduledService.this.scheduler().schedule(AbstractScheduledService.this.delegate, ServiceDelegate.this.executorService, ServiceDelegate.this.task); ServiceDelegate.this.notifyStarted(); } finally { try { } finally { } } } }); } @Override // com.google.common.util.concurrent.AbstractService protected final void doStop() { Objects.requireNonNull(this.runningTask); Objects.requireNonNull(this.executorService); this.runningTask.cancel(false); this.executorService.execute(new Runnable() { // from class: com.google.common.util.concurrent.AbstractScheduledService.ServiceDelegate.3 @Override // java.lang.Runnable public void run() { try { ServiceDelegate.this.lock.lock(); try { if (ServiceDelegate.this.state() != Service.State.STOPPING) { return; } AbstractScheduledService.this.shutDown(); ServiceDelegate.this.lock.unlock(); ServiceDelegate.this.notifyStopped(); } finally { ServiceDelegate.this.lock.unlock(); } } catch (Throwable th) { ServiceDelegate.this.notifyFailed(th); } } }); } @Override // com.google.common.util.concurrent.AbstractService public String toString() { return AbstractScheduledService.this.toString(); } } protected AbstractScheduledService() { } protected ScheduledExecutorService executor() { final ScheduledExecutorService newSingleThreadScheduledExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { // from class: com.google.common.util.concurrent.AbstractScheduledService.1ThreadFactoryImpl @Override // java.util.concurrent.ThreadFactory public Thread newThread(Runnable runnable) { return MoreExecutors.newThread(AbstractScheduledService.this.serviceName(), runnable); } }); addListener(new Service.Listener(this) { // from class: com.google.common.util.concurrent.AbstractScheduledService.1 @Override // com.google.common.util.concurrent.Service.Listener public void terminated(Service.State state) { newSingleThreadScheduledExecutor.shutdown(); } @Override // com.google.common.util.concurrent.Service.Listener public void failed(Service.State state, Throwable th) { newSingleThreadScheduledExecutor.shutdown(); } }, MoreExecutors.directExecutor()); return newSingleThreadScheduledExecutor; } protected String serviceName() { return getClass().getSimpleName(); } public String toString() { String serviceName = serviceName(); String valueOf = String.valueOf(state()); return new StringBuilder(String.valueOf(serviceName).length() + 3 + String.valueOf(valueOf).length()).append(serviceName).append(" [").append(valueOf).append("]").toString(); } @Override // com.google.common.util.concurrent.Service public final boolean isRunning() { return this.delegate.isRunning(); } @Override // com.google.common.util.concurrent.Service public final Service.State state() { return this.delegate.state(); } @Override // com.google.common.util.concurrent.Service public final void addListener(Service.Listener listener, Executor executor) { this.delegate.addListener(listener, executor); } @Override // com.google.common.util.concurrent.Service public final Throwable failureCause() { return this.delegate.failureCause(); } @Override // com.google.common.util.concurrent.Service public final Service startAsync() { this.delegate.startAsync(); return this; } @Override // com.google.common.util.concurrent.Service public final Service stopAsync() { this.delegate.stopAsync(); return this; } @Override // com.google.common.util.concurrent.Service public final void awaitRunning() { this.delegate.awaitRunning(); } @Override // com.google.common.util.concurrent.Service public final void awaitRunning(long j, TimeUnit timeUnit) throws TimeoutException { this.delegate.awaitRunning(j, timeUnit); } @Override // com.google.common.util.concurrent.Service public final void awaitTerminated() { this.delegate.awaitTerminated(); } @Override // com.google.common.util.concurrent.Service public final void awaitTerminated(long j, TimeUnit timeUnit) throws TimeoutException { this.delegate.awaitTerminated(j, timeUnit); } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes3.dex */ public static final class FutureAsCancellable implements Cancellable { private final Future delegate; FutureAsCancellable(Future future) { this.delegate = future; } @Override // com.google.common.util.concurrent.AbstractScheduledService.Cancellable public void cancel(boolean z) { this.delegate.cancel(z); } @Override // com.google.common.util.concurrent.AbstractScheduledService.Cancellable public boolean isCancelled() { return this.delegate.isCancelled(); } } /* loaded from: classes3.dex */ public static abstract class CustomScheduler extends Scheduler { protected abstract Schedule getNextSchedule() throws Exception; public CustomScheduler() { super(); } /* loaded from: classes3.dex */ private final class ReschedulableCallable implements Callable { @CheckForNull private SupplantableFuture cancellationDelegate; private final ScheduledExecutorService executor; private final ReentrantLock lock = new ReentrantLock(); private final AbstractService service; private final Runnable wrappedRunnable; ReschedulableCallable(AbstractService abstractService, ScheduledExecutorService scheduledExecutorService, Runnable runnable) { this.wrappedRunnable = runnable; this.executor = scheduledExecutorService; this.service = abstractService; } @Override // java.util.concurrent.Callable @CheckForNull public Void call() throws Exception { this.wrappedRunnable.run(); reschedule(); return null; } public Cancellable reschedule() { Cancellable futureAsCancellable; try { Schedule nextSchedule = CustomScheduler.this.getNextSchedule(); this.lock.lock(); try { futureAsCancellable = initializeOrUpdateCancellationDelegate(nextSchedule); this.lock.unlock(); th = null; } catch (Throwable th) { th = th; try { futureAsCancellable = new FutureAsCancellable(Futures.immediateCancelledFuture()); } finally { this.lock.unlock(); } } if (th != null) { this.service.notifyFailed(th); } return futureAsCancellable; } catch (Throwable th2) { this.service.notifyFailed(th2); return new FutureAsCancellable(Futures.immediateCancelledFuture()); } } private Cancellable initializeOrUpdateCancellationDelegate(Schedule schedule) { SupplantableFuture supplantableFuture = this.cancellationDelegate; if (supplantableFuture == null) { SupplantableFuture supplantableFuture2 = new SupplantableFuture(this.lock, submitToExecutor(schedule)); this.cancellationDelegate = supplantableFuture2; return supplantableFuture2; } if (!supplantableFuture.currentFuture.isCancelled()) { this.cancellationDelegate.currentFuture = submitToExecutor(schedule); } return this.cancellationDelegate; } private ScheduledFuture submitToExecutor(Schedule schedule) { return this.executor.schedule(this, schedule.delay, schedule.unit); } } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes3.dex */ public static final class SupplantableFuture implements Cancellable { private Future currentFuture; private final ReentrantLock lock; SupplantableFuture(ReentrantLock reentrantLock, Future future) { this.lock = reentrantLock; this.currentFuture = future; } @Override // com.google.common.util.concurrent.AbstractScheduledService.Cancellable public void cancel(boolean z) { this.lock.lock(); try { this.currentFuture.cancel(z); } finally { this.lock.unlock(); } } @Override // com.google.common.util.concurrent.AbstractScheduledService.Cancellable public boolean isCancelled() { this.lock.lock(); try { return this.currentFuture.isCancelled(); } finally { this.lock.unlock(); } } } @Override // com.google.common.util.concurrent.AbstractScheduledService.Scheduler final Cancellable schedule(AbstractService abstractService, ScheduledExecutorService scheduledExecutorService, Runnable runnable) { return new ReschedulableCallable(abstractService, scheduledExecutorService, runnable).reschedule(); } /* JADX INFO: Access modifiers changed from: protected */ /* loaded from: classes3.dex */ public static final class Schedule { private final long delay; private final TimeUnit unit; public Schedule(long j, TimeUnit timeUnit) { this.delay = j; this.unit = (TimeUnit) Preconditions.checkNotNull(timeUnit); } } } }