package com.applovin.impl; import csdk.gluads.Consts; import java.util.Timer; import java.util.TimerTask; /* loaded from: classes.dex */ public class go { private final com.applovin.impl.sdk.j a; private Timer b; private long c; private long d; private long e; private boolean f; private final Runnable g; private long h; private final Object i = new Object(); private TimerTask b() { return new a(); } private go(com.applovin.impl.sdk.j jVar, Runnable runnable) { this.a = jVar; this.g = runnable; } public long c() { if (this.b == null) { return this.d - this.h; } return this.d - (System.currentTimeMillis() - this.c); } public void d() { synchronized (this.i) { Timer timer = this.b; if (timer != null) { try { timer.cancel(); this.h = Math.max(1L, System.currentTimeMillis() - this.c); } catch (Throwable th) { try { com.applovin.impl.sdk.j jVar = this.a; if (jVar != null) { jVar.J(); if (com.applovin.impl.sdk.n.a()) { this.a.J(); if (com.applovin.impl.sdk.n.a()) { this.a.J().a("Timer", "Encountered error while pausing timer", th); } } } this.b = null; } finally { this.b = null; } } } } } public void e() { synchronized (this.i) { long j = this.h; if (j > 0) { try { long j2 = this.d - j; this.d = j2; if (j2 < 0) { this.d = 0L; } this.b = new Timer(); a(b(), this.d, this.f, this.e); this.c = System.currentTimeMillis(); } catch (Throwable th) { try { com.applovin.impl.sdk.j jVar = this.a; if (jVar != null) { jVar.J(); if (com.applovin.impl.sdk.n.a()) { this.a.J(); if (com.applovin.impl.sdk.n.a()) { this.a.J().a("Timer", "Encountered error while resuming timer", th); } } } this.h = 0L; } finally { this.h = 0L; } } } } } public class a extends TimerTask { public a() { } @Override // java.util.TimerTask, java.lang.Runnable public void run() { try { go.this.g.run(); synchronized (go.this.i) { try { if (go.this.f) { go.this.c = System.currentTimeMillis(); go goVar = go.this; goVar.d = goVar.e; } else { go.this.b = null; } } finally { } } } catch (Throwable th) { try { if (go.this.a != null) { go.this.a.J(); if (com.applovin.impl.sdk.n.a()) { go.this.a.J().a("Timer", "Encountered error while executing timed task", th); } go.this.a.E().a("Timer", "executingTimedTask", th); } synchronized (go.this.i) { try { if (go.this.f) { go.this.c = System.currentTimeMillis(); go goVar2 = go.this; goVar2.d = goVar2.e; } else { go.this.b = null; } } finally { } } } catch (Throwable th2) { synchronized (go.this.i) { try { if (go.this.f) { go.this.c = System.currentTimeMillis(); go goVar3 = go.this; goVar3.d = goVar3.e; } else { go.this.b = null; } throw th2; } finally { } } } } } } public void a() { synchronized (this.i) { Timer timer = this.b; if (timer != null) { try { timer.cancel(); this.b = null; } catch (Throwable th) { try { com.applovin.impl.sdk.j jVar = this.a; if (jVar != null) { jVar.J(); if (com.applovin.impl.sdk.n.a()) { this.a.J(); if (com.applovin.impl.sdk.n.a()) { this.a.J().a("Timer", "Encountered error while cancelling timer", th); } } } this.b = null; } catch (Throwable th2) { this.b = null; this.h = 0L; throw th2; } } this.h = 0L; } } } public static go a(long j, com.applovin.impl.sdk.j jVar, Runnable runnable) { return a(j, false, jVar, runnable); } public static go a(long j, boolean z, com.applovin.impl.sdk.j jVar, Runnable runnable) { if (j < 0) { throw new IllegalArgumentException("Cannot create a scheduled timer. Invalid fire time passed in: " + j + Consts.STRING_PERIOD); } if (runnable != null) { go goVar = new go(jVar, runnable); goVar.c = System.currentTimeMillis(); goVar.d = j; goVar.f = z; goVar.e = j; try { goVar.b = new Timer(); goVar.a(goVar.b(), j, z, goVar.e); } catch (OutOfMemoryError e) { jVar.J(); if (com.applovin.impl.sdk.n.a()) { jVar.J().a("Timer", "Failed to create timer due to OOM error", e); } } return goVar; } throw new IllegalArgumentException("Cannot create a scheduled timer. Runnable is null."); } private void a(TimerTask timerTask, long j, boolean z, long j2) { if (z) { this.b.schedule(timerTask, j, j2); } else { this.b.schedule(timerTask, j); } } }