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.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.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Logger; /* loaded from: classes2.dex */ public abstract class AbstractScheduledService implements Service { private static final Logger logger = Logger.getLogger(AbstractScheduledService.class.getName()); private final AbstractService delegate = new ServiceDelegate(); protected abstract void runOneIteration() throws Exception; protected abstract Scheduler scheduler(); protected void shutDown() throws Exception { } protected void startUp() throws Exception { } /* loaded from: classes2.dex */ public static abstract class Scheduler { abstract Future schedule(AbstractService abstractService, ScheduledExecutorService scheduledExecutorService, Runnable runnable); public static Scheduler newFixedDelaySchedule(long j, long j2, TimeUnit timeUnit) { Preconditions.checkNotNull(timeUnit); Preconditions.checkArgument(j2 > 0, "delay must be > 0, found %s", j2); return new Scheduler(j, j2, timeUnit) { // from class: com.google.common.util.concurrent.AbstractScheduledService.Scheduler.1 final long val$delay; final long val$initialDelay; final TimeUnit val$unit; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ { super(); this.val$initialDelay = j; this.val$delay = j2; this.val$unit = timeUnit; } @Override // com.google.common.util.concurrent.AbstractScheduledService.Scheduler public Future schedule(AbstractService abstractService, ScheduledExecutorService scheduledExecutorService, Runnable runnable) { return scheduledExecutorService.scheduleWithFixedDelay(runnable, this.val$initialDelay, this.val$delay, this.val$unit); } }; } public static Scheduler newFixedRateSchedule(long j, long j2, TimeUnit timeUnit) { Preconditions.checkNotNull(timeUnit); Preconditions.checkArgument(j2 > 0, "period must be > 0, found %s", j2); return new Scheduler(j, j2, timeUnit) { // from class: com.google.common.util.concurrent.AbstractScheduledService.Scheduler.2 final long val$initialDelay; final long val$period; final TimeUnit val$unit; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ { super(); this.val$initialDelay = j; this.val$period = j2; this.val$unit = timeUnit; } @Override // com.google.common.util.concurrent.AbstractScheduledService.Scheduler public Future schedule(AbstractService abstractService, ScheduledExecutorService scheduledExecutorService, Runnable runnable) { return scheduledExecutorService.scheduleAtFixedRate(runnable, this.val$initialDelay, this.val$period, this.val$unit); } }; } private Scheduler() { } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public final class ServiceDelegate extends AbstractService { private volatile ScheduledExecutorService executorService; private final ReentrantLock lock; private volatile Future runningTask; private final Runnable task; final AbstractScheduledService this$0; private ServiceDelegate(AbstractScheduledService abstractScheduledService) { this.this$0 = abstractScheduledService; this.lock = new ReentrantLock(); this.task = new Task(this); } /* loaded from: classes2.dex */ class Task implements Runnable { final ServiceDelegate this$1; Task(ServiceDelegate serviceDelegate) { this.this$1 = serviceDelegate; } @Override // java.lang.Runnable public void run() { ServiceDelegate serviceDelegate; this.this$1.lock.lock(); try { } finally { try { try { } catch (Throwable th) { } } catch (Exception e) { } serviceDelegate = this.this$1; serviceDelegate.lock.unlock(); } if (this.this$1.runningTask.isCancelled()) { serviceDelegate = this.this$1; serviceDelegate.lock.unlock(); } else { this.this$1.this$0.runOneIteration(); serviceDelegate = this.this$1; serviceDelegate.lock.unlock(); } } } @Override // com.google.common.util.concurrent.AbstractService protected final void doStart() { this.executorService = MoreExecutors.renamingDecorator(this.this$0.executor(), new Supplier(this) { // from class: com.google.common.util.concurrent.AbstractScheduledService.ServiceDelegate.1 final ServiceDelegate this$1; { this.this$1 = this; } @Override // com.google.common.base.Supplier public String get() { String serviceName = this.this$1.this$0.serviceName(); String valueOf = String.valueOf(this.this$1.state()); StringBuilder sb = new StringBuilder(String.valueOf(serviceName).length() + 1 + String.valueOf(valueOf).length()); sb.append(serviceName); sb.append(" "); sb.append(valueOf); return sb.toString(); } }); this.executorService.execute(new Runnable(this) { // from class: com.google.common.util.concurrent.AbstractScheduledService.ServiceDelegate.2 final ServiceDelegate this$1; { this.this$1 = this; } @Override // java.lang.Runnable public void run() { this.this$1.lock.lock(); try { this.this$1.this$0.startUp(); ServiceDelegate serviceDelegate = this.this$1; serviceDelegate.runningTask = serviceDelegate.this$0.scheduler().schedule(this.this$1.this$0.delegate, this.this$1.executorService, this.this$1.task); this.this$1.notifyStarted(); } finally { try { } finally { } } } }); } @Override // com.google.common.util.concurrent.AbstractService protected final void doStop() { this.runningTask.cancel(false); this.executorService.execute(new Runnable(this) { // from class: com.google.common.util.concurrent.AbstractScheduledService.ServiceDelegate.3 final ServiceDelegate this$1; { this.this$1 = this; } @Override // java.lang.Runnable public void run() { try { this.this$1.lock.lock(); try { if (this.this$1.state() != Service.State.STOPPING) { return; } this.this$1.this$0.shutDown(); this.this$1.lock.unlock(); this.this$1.notifyStopped(); } finally { this.this$1.lock.unlock(); } } catch (Throwable th) { this.this$1.notifyFailed(th); } } }); } @Override // com.google.common.util.concurrent.AbstractService public final String toString() { return this.this$0.toString(); } } protected AbstractScheduledService() { } protected ScheduledExecutorService executor() { ScheduledExecutorService newSingleThreadScheduledExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory(this) { // from class: com.google.common.util.concurrent.AbstractScheduledService.1ThreadFactoryImpl final AbstractScheduledService this$0; { this.this$0 = this; } @Override // java.util.concurrent.ThreadFactory public Thread newThread(Runnable runnable) { return MoreExecutors.newThread(this.this$0.serviceName(), runnable); } }); addListener(new Service.Listener(this, newSingleThreadScheduledExecutor) { // from class: com.google.common.util.concurrent.AbstractScheduledService.1 final ScheduledExecutorService val$executor; { this.val$executor = newSingleThreadScheduledExecutor; } @Override // com.google.common.util.concurrent.Service.Listener public void terminated(Service.State state) { this.val$executor.shutdown(); } @Override // com.google.common.util.concurrent.Service.Listener public void failed(Service.State state, Throwable th) { this.val$executor.shutdown(); } }, MoreExecutors.directExecutor()); return newSingleThreadScheduledExecutor; } protected String serviceName() { return getClass().getSimpleName(); } public String toString() { String serviceName = serviceName(); String valueOf = String.valueOf(state()); StringBuilder sb = new StringBuilder(String.valueOf(serviceName).length() + 3 + String.valueOf(valueOf).length()); sb.append(serviceName); sb.append(" ["); sb.append(valueOf); sb.append("]"); return sb.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); } /* loaded from: classes2.dex */ public static abstract class CustomScheduler extends Scheduler { protected abstract Schedule getNextSchedule() throws Exception; public CustomScheduler() { super(); } /* loaded from: classes2.dex */ class ReschedulableCallable extends ForwardingFuture implements Callable { private Future currentFuture; private final ScheduledExecutorService executor; private final ReentrantLock lock = new ReentrantLock(); private final AbstractService service; final CustomScheduler this$0; private final Runnable wrappedRunnable; ReschedulableCallable(CustomScheduler customScheduler, AbstractService abstractService, ScheduledExecutorService scheduledExecutorService, Runnable runnable) { this.this$0 = customScheduler; this.wrappedRunnable = runnable; this.executor = scheduledExecutorService; this.service = abstractService; } @Override // java.util.concurrent.Callable public Void call() throws Exception { this.wrappedRunnable.run(); reschedule(); return null; } public void reschedule() { try { Schedule nextSchedule = this.this$0.getNextSchedule(); this.lock.lock(); try { Future future = this.currentFuture; if (future == null || !future.isCancelled()) { this.currentFuture = this.executor.schedule(this, nextSchedule.delay, nextSchedule.unit); } this.lock.unlock(); th = null; } catch (Throwable th) { th = th; this.lock.unlock(); } if (th != null) { this.service.notifyFailed(th); } } catch (Throwable th2) { this.service.notifyFailed(th2); } } @Override // com.google.common.util.concurrent.ForwardingFuture, java.util.concurrent.Future public boolean cancel(boolean z) { this.lock.lock(); try { return this.currentFuture.cancel(z); } finally { this.lock.unlock(); } } @Override // com.google.common.util.concurrent.ForwardingFuture, java.util.concurrent.Future public boolean isCancelled() { this.lock.lock(); try { return this.currentFuture.isCancelled(); } finally { this.lock.unlock(); } } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.util.concurrent.ForwardingFuture, com.google.common.collect.ForwardingObject public Future delegate() { throw new UnsupportedOperationException("Only cancel and isCancelled is supported by this future"); } } @Override // com.google.common.util.concurrent.AbstractScheduledService.Scheduler final Future schedule(AbstractService abstractService, ScheduledExecutorService scheduledExecutorService, Runnable runnable) { ReschedulableCallable reschedulableCallable = new ReschedulableCallable(this, abstractService, scheduledExecutorService, runnable); reschedulableCallable.reschedule(); return reschedulableCallable; } /* JADX INFO: Access modifiers changed from: protected */ /* loaded from: classes2.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); } } } }