package kotlinx.coroutines.android; import android.os.Handler; import android.os.Looper; import java.util.concurrent.CancellationException; import kotlin.Unit; import kotlin.coroutines.CoroutineContext; import kotlin.jvm.functions.Function1; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import kotlin.ranges.RangesKt___RangesKt; import kotlinx.coroutines.CancellableContinuation; import kotlinx.coroutines.Delay; import kotlinx.coroutines.Dispatchers; import kotlinx.coroutines.DisposableHandle; import kotlinx.coroutines.JobKt; import kotlinx.coroutines.NonDisposableHandle; /* loaded from: classes5.dex */ public final class HandlerContext extends HandlerDispatcher implements Delay { private volatile HandlerContext _immediate; public final Handler handler; public final HandlerContext immediate; public final boolean invokeImmediately; public final String name; @Override // kotlinx.coroutines.MainCoroutineDispatcher public HandlerContext getImmediate() { return this.immediate; } @Override // kotlinx.coroutines.Delay /* renamed from: scheduleResumeAfterDelay */ public void mo4149scheduleResumeAfterDelay(long j, final CancellableContinuation cancellableContinuation) { long coerceAtMost; final Runnable runnable = new Runnable() { // from class: kotlinx.coroutines.android.HandlerContext$scheduleResumeAfterDelay$$inlined$Runnable$1 @Override // java.lang.Runnable public final void run() { CancellableContinuation.this.resumeUndispatched(this, Unit.INSTANCE); } }; Handler handler = this.handler; coerceAtMost = RangesKt___RangesKt.coerceAtMost(j, 4611686018427387903L); if (handler.postDelayed(runnable, coerceAtMost)) { cancellableContinuation.invokeOnCancellation(new Function1() { // from class: kotlinx.coroutines.android.HandlerContext$scheduleResumeAfterDelay$1 /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ { super(1); } @Override // kotlin.jvm.functions.Function1 public /* bridge */ /* synthetic */ Object invoke(Object obj) { invoke((Throwable) obj); return Unit.INSTANCE; } public final void invoke(Throwable th) { Handler handler2; handler2 = HandlerContext.this.handler; handler2.removeCallbacks(runnable); } }); } else { cancelOnRejection(cancellableContinuation.getContext(), runnable); } } public HandlerContext(Handler handler, String str, boolean z) { super(null); this.handler = handler; this.name = str; this.invokeImmediately = z; this._immediate = z ? this : null; HandlerContext handlerContext = this._immediate; if (handlerContext == null) { handlerContext = new HandlerContext(handler, str, true); this._immediate = handlerContext; } this.immediate = handlerContext; } public /* synthetic */ HandlerContext(Handler handler, String str, int i, DefaultConstructorMarker defaultConstructorMarker) { this(handler, (i & 2) != 0 ? null : str); } public HandlerContext(Handler handler, String str) { this(handler, str, false); } @Override // kotlinx.coroutines.CoroutineDispatcher public boolean isDispatchNeeded(CoroutineContext coroutineContext) { return (this.invokeImmediately && Intrinsics.areEqual(Looper.myLooper(), this.handler.getLooper())) ? false : true; } @Override // kotlinx.coroutines.CoroutineDispatcher /* renamed from: dispatch */ public void mo4148dispatch(CoroutineContext coroutineContext, Runnable runnable) { if (this.handler.post(runnable)) { return; } cancelOnRejection(coroutineContext, runnable); } @Override // kotlinx.coroutines.Delay public DisposableHandle invokeOnTimeout(long j, final Runnable runnable, CoroutineContext coroutineContext) { long coerceAtMost; Handler handler = this.handler; coerceAtMost = RangesKt___RangesKt.coerceAtMost(j, 4611686018427387903L); if (handler.postDelayed(runnable, coerceAtMost)) { return new DisposableHandle() { // from class: kotlinx.coroutines.android.HandlerContext$$ExternalSyntheticLambda0 @Override // kotlinx.coroutines.DisposableHandle public final void dispose() { HandlerContext.invokeOnTimeout$lambda$3(HandlerContext.this, runnable); } }; } cancelOnRejection(coroutineContext, runnable); return NonDisposableHandle.INSTANCE; } public static final void invokeOnTimeout$lambda$3(HandlerContext handlerContext, Runnable runnable) { handlerContext.handler.removeCallbacks(runnable); } public final void cancelOnRejection(CoroutineContext coroutineContext, Runnable runnable) { JobKt.cancel(coroutineContext, new CancellationException("The task was rejected, the handler underlying the dispatcher '" + this + "' was closed")); Dispatchers.getIO().mo4148dispatch(coroutineContext, runnable); } @Override // kotlinx.coroutines.CoroutineDispatcher public String toString() { String stringInternalImpl = toStringInternalImpl(); if (stringInternalImpl != null) { return stringInternalImpl; } String str = this.name; if (str == null) { str = this.handler.toString(); } if (!this.invokeImmediately) { return str; } return str + ".immediate"; } public boolean equals(Object obj) { return (obj instanceof HandlerContext) && ((HandlerContext) obj).handler == this.handler; } public int hashCode() { return System.identityHashCode(this.handler); } }