package com.vungle.ads.internal.task; import android.os.Handler; import android.os.Looper; import android.os.SystemClock; import androidx.annotation.VisibleForTesting; import com.vungle.ads.internal.util.Logger; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.Executor; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; /* loaded from: classes4.dex */ public final class VungleJobRunner implements JobRunner { private final JobCreator creator; private final Executor executor; private long nextCheck; private final List pendingJobs; private final Runnable pendingRunnable; private final ThreadPriorityHelper threadPriorityHelper; public static final Companion Companion = new Companion(null); private static final Handler handler = new Handler(Looper.getMainLooper()); private static final String TAG = VungleJobRunner.class.getSimpleName(); public VungleJobRunner(JobCreator creator, Executor executor, ThreadPriorityHelper threadPriorityHelper) { Intrinsics.checkNotNullParameter(creator, "creator"); Intrinsics.checkNotNullParameter(executor, "executor"); this.creator = creator; this.executor = executor; this.threadPriorityHelper = threadPriorityHelper; this.nextCheck = Long.MAX_VALUE; this.pendingJobs = new CopyOnWriteArrayList(); this.pendingRunnable = new PendingRunnable(new WeakReference(this)); } public static final class PendingJob { private JobInfo info; private final long uptimeMillis; public final JobInfo getInfo() { return this.info; } public final long getUptimeMillis() { return this.uptimeMillis; } public final void setInfo(JobInfo jobInfo) { this.info = jobInfo; } public PendingJob(long j, JobInfo jobInfo) { this.uptimeMillis = j; this.info = jobInfo; } } public static final class PendingRunnable implements Runnable { private WeakReference runner; public final WeakReference getRunner() { return this.runner; } public final void setRunner(WeakReference weakReference) { Intrinsics.checkNotNullParameter(weakReference, ""); this.runner = weakReference; } public PendingRunnable(WeakReference runner) { Intrinsics.checkNotNullParameter(runner, "runner"); this.runner = runner; } @Override // java.lang.Runnable public void run() { VungleJobRunner vungleJobRunner = this.runner.get(); if (vungleJobRunner != null) { vungleJobRunner.executePendingJobs(); } } } @Override // com.vungle.ads.internal.task.JobRunner public synchronized void execute(JobInfo jobInfo) { try { Intrinsics.checkNotNullParameter(jobInfo, "jobInfo"); JobInfo copy = jobInfo.copy(); if (copy != null) { String jobTag = copy.getJobTag(); long delay = copy.getDelay(); copy.setDelay(0L); if (copy.getUpdateCurrent()) { for (PendingJob pendingJob : this.pendingJobs) { JobInfo info = pendingJob.getInfo(); if (Intrinsics.areEqual(info != null ? info.getJobTag() : null, jobTag)) { Logger.Companion companion = Logger.Companion; String TAG2 = TAG; Intrinsics.checkNotNullExpressionValue(TAG2, "TAG"); companion.d(TAG2, "replacing pending job with new " + jobTag); this.pendingJobs.remove(pendingJob); } } } this.pendingJobs.add(new PendingJob(SystemClock.uptimeMillis() + delay, copy)); executePendingJobs(); } } catch (Throwable th) { throw th; } } @VisibleForTesting public final int getPendingJobSize$vungle_ads_release() { return this.pendingJobs.size(); } @Override // com.vungle.ads.internal.task.JobRunner public synchronized void cancelPendingJob(String tag) { try { Intrinsics.checkNotNullParameter(tag, "tag"); ArrayList arrayList = new ArrayList(); for (PendingJob pendingJob : this.pendingJobs) { JobInfo info = pendingJob.getInfo(); if (Intrinsics.areEqual(info != null ? info.getJobTag() : null, tag)) { arrayList.add(pendingJob); } } this.pendingJobs.removeAll(arrayList); } catch (Throwable th) { throw th; } } /* JADX INFO: Access modifiers changed from: private */ public final synchronized void executePendingJobs() { try { long uptimeMillis = SystemClock.uptimeMillis(); long j = Long.MAX_VALUE; for (PendingJob pendingJob : this.pendingJobs) { if (uptimeMillis >= pendingJob.getUptimeMillis()) { this.pendingJobs.remove(pendingJob); JobInfo info = pendingJob.getInfo(); if (info != null) { this.executor.execute(new JobRunnable(info, this.creator, this, this.threadPriorityHelper)); } } else { j = Math.min(j, pendingJob.getUptimeMillis()); } } if (j != Long.MAX_VALUE && j != this.nextCheck) { Handler handler2 = handler; handler2.removeCallbacks(this.pendingRunnable); handler2.postAtTime(this.pendingRunnable, TAG, j); } this.nextCheck = j; } catch (Throwable th) { throw th; } } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } } }