package s.coroutines.android;

import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.CancellationException;
import kotlin.Metadata;
import kotlin.Unit;
import kotlin.coroutines.CoroutineContext;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.Lambda;
import kotlin.jvm.internal.o;
import kotlin.ranges.j;
import s.coroutines.CancellableContinuation;
import s.coroutines.Delay;
import s.coroutines.Dispatchers;
import s.coroutines.DisposableHandle;
import s.coroutines.Job;
import s.coroutines.MainCoroutineDispatcher;
import s.coroutines.NonDisposableHandle;

/* JADX INFO: renamed from: s.a.a2.b, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000^\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0002\b\u0006\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0000\u0018\u00002\u00020\u00012\u00020\u0002B\u001b\b\u0016\u0012\u0006\u0010\u0003\u001a\u00020\u0004\u0012\n\b\u0002\u0010\u0005\u001a\u0004\u0018\u00010\u0006¢\u0006\u0002\u0010\u0007B!\b\u0002\u0012\u0006\u0010\u0003\u001a\u00020\u0004\u0012\b\u0010\u0005\u001a\u0004\u0018\u00010\u0006\u0012\u0006\u0010\b\u001a\u00020\t¢\u0006\u0002\u0010\nJ\u001c\u0010\u000f\u001a\u00020\u00102\u0006\u0010\u0011\u001a\u00020\u00122\n\u0010\u0013\u001a\u00060\u0014j\u0002`\u0015H\u0002J\u001c\u0010\u0016\u001a\u00020\u00102\u0006\u0010\u0011\u001a\u00020\u00122\n\u0010\u0013\u001a\u00060\u0014j\u0002`\u0015H\u0016J\u0013\u0010\u0017\u001a\u00020\t2\b\u0010\u0018\u001a\u0004\u0018\u00010\u0019H\u0096\u0002J\b\u0010\u001a\u001a\u00020\u001bH\u0016J$\u0010\u001c\u001a\u00020\u001d2\u0006\u0010\u001e\u001a\u00020\u001f2\n\u0010\u0013\u001a\u00060\u0014j\u0002`\u00152\u0006\u0010\u0011\u001a\u00020\u0012H\u0016J\u0010\u0010 \u001a\u00020\t2\u0006\u0010\u0011\u001a\u00020\u0012H\u0016J\u001e\u0010!\u001a\u00020\u00102\u0006\u0010\u001e\u001a\u00020\u001f2\f\u0010\"\u001a\b\u0012\u0004\u0012\u00020\u00100#H\u0016J\b\u0010$\u001a\u00020\u0006H\u0016R\u0010\u0010\u000b\u001a\u0004\u0018\u00010\u0000X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0003\u001a\u00020\u0004X\u0082\u0004¢\u0006\u0002\n\u0000R\u0014\u0010\f\u001a\u00020\u0000X\u0096\u0004¢\u0006\b\n\u0000\u001a\u0004\b\r\u0010\u000eR\u000e\u0010\b\u001a\u00020\tX\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u0005\u001a\u0004\u0018\u00010\u0006X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006%"}, d2 = {"Lkotlinx/coroutines/android/HandlerContext;", "Lkotlinx/coroutines/android/HandlerDispatcher;", "Lkotlinx/coroutines/Delay;", "handler", "Landroid/os/Handler;", "name", "", "(Landroid/os/Handler;Ljava/lang/String;)V", "invokeImmediately", "", "(Landroid/os/Handler;Ljava/lang/String;Z)V", "_immediate", "immediate", "getImmediate", "()Lkotlinx/coroutines/android/HandlerContext;", "cancelOnRejection", "", "context", "Lkotlin/coroutines/CoroutineContext;", "block", "Ljava/lang/Runnable;", "Lkotlinx/coroutines/Runnable;", "dispatch", "equals", "other", "", "hashCode", "", "invokeOnTimeout", "Lkotlinx/coroutines/DisposableHandle;", "timeMillis", "", "isDispatchNeeded", "scheduleResumeAfterDelay", "continuation", "Lkotlinx/coroutines/CancellableContinuation;", "toString", "kotlinx-coroutines-android"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class HandlerContext extends HandlerDispatcher implements Delay {
    private volatile HandlerContext _immediate;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final Handler f9318m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final String f9319n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final boolean f9320o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final HandlerContext f9321p;

    /* JADX INFO: renamed from: s.a.a2.b$a */
    @Metadata(d1 = {"\u0000\n\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0002\u0010\u0000\u001a\u00020\u0001H\n¢\u0006\u0002\b\u0002¨\u0006\u0003"}, d2 = {"<anonymous>", "", "run", "kotlinx/coroutines/RunnableKt$Runnable$1"}, k = 3, mv = {1, 6, 0}, xi = 48)
    public static final class a implements Runnable {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final /* synthetic */ CancellableContinuation f9322f;

        /* JADX INFO: renamed from: m, reason: collision with root package name */
        public final /* synthetic */ HandlerContext f9323m;

        public a(CancellableContinuation cancellableContinuation, HandlerContext handlerContext) {
            this.f9322f = cancellableContinuation;
            this.f9323m = handlerContext;
        }

        @Override // java.lang.Runnable
        public final void run() {
            this.f9322f.e(this.f9323m, Unit.a);
        }
    }

    /* JADX INFO: renamed from: s.a.a2.b$b */
    @Metadata(d1 = {"\u0000\u000e\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0003\n\u0000\u0010\u0000\u001a\u00020\u00012\b\u0010\u0002\u001a\u0004\u0018\u00010\u0003H\n¢\u0006\u0002\b\u0004"}, d2 = {"<anonymous>", "", "it", "", "invoke"}, k = 3, mv = {1, 6, 0}, xi = 48)
    public static final class b extends Lambda implements Function1<Throwable, Unit> {

        /* JADX INFO: renamed from: m, reason: collision with root package name */
        public final /* synthetic */ Runnable f9325m;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public b(Runnable runnable) {
            super(1);
            this.f9325m = runnable;
        }

        @Override // kotlin.jvm.functions.Function1
        public Unit invoke(Throwable th) {
            HandlerContext.this.f9318m.removeCallbacks(this.f9325m);
            return Unit.a;
        }
    }

    public HandlerContext(Handler handler, String str, boolean z2) {
        super(null);
        this.f9318m = handler;
        this.f9319n = str;
        this.f9320o = z2;
        this._immediate = z2 ? this : null;
        HandlerContext handlerContext = this._immediate;
        if (handlerContext == null) {
            handlerContext = new HandlerContext(handler, str, true);
            this._immediate = handlerContext;
        }
        this.f9321p = handlerContext;
    }

    @Override // s.coroutines.CoroutineDispatcher
    public boolean A(CoroutineContext coroutineContext) {
        return (this.f9320o && o.a(Looper.myLooper(), this.f9318m.getLooper())) ? false : true;
    }

    @Override // s.coroutines.MainCoroutineDispatcher
    public MainCoroutineDispatcher B() {
        return this.f9321p;
    }

    public final void D(CoroutineContext coroutineContext, Runnable runnable) {
        CancellationException cancellationException = new CancellationException("The task was rejected, the handler underlying the dispatcher '" + this + "' was closed");
        int i2 = Job.f9422l;
        Job job = (Job) coroutineContext.get(Job.a.f9423f);
        if (job != null) {
            job.s(cancellationException);
        }
        Dispatchers.f9455b.z(coroutineContext, runnable);
    }

    @Override // s.coroutines.Delay
    public void e(long j2, CancellableContinuation<? super Unit> cancellableContinuation) {
        a aVar = new a(cancellableContinuation, this);
        if (this.f9318m.postDelayed(aVar, j.a(j2, 4611686018427387903L))) {
            cancellableContinuation.b(new b(aVar));
        } else {
            D(cancellableContinuation.getContext(), aVar);
        }
    }

    public boolean equals(Object other) {
        return (other instanceof HandlerContext) && ((HandlerContext) other).f9318m == this.f9318m;
    }

    public int hashCode() {
        return System.identityHashCode(this.f9318m);
    }

    @Override // s.coroutines.android.HandlerDispatcher, s.coroutines.Delay
    public DisposableHandle l(long j2, final Runnable runnable, CoroutineContext coroutineContext) {
        if (this.f9318m.postDelayed(runnable, j.a(j2, 4611686018427387903L))) {
            return new DisposableHandle() { // from class: s.a.a2.a
                @Override // s.coroutines.DisposableHandle
                public final void b() {
                    HandlerContext handlerContext = this.f9316f;
                    handlerContext.f9318m.removeCallbacks(runnable);
                }
            };
        }
        D(coroutineContext, runnable);
        return NonDisposableHandle.f9458f;
    }

    @Override // s.coroutines.MainCoroutineDispatcher, s.coroutines.CoroutineDispatcher
    public String toString() {
        String strC = C();
        if (strC != null) {
            return strC;
        }
        String string = this.f9319n;
        if (string == null) {
            string = this.f9318m.toString();
        }
        return this.f9320o ? g.a.a.a.a.t(string, ".immediate") : string;
    }

    @Override // s.coroutines.CoroutineDispatcher
    public void z(CoroutineContext coroutineContext, Runnable runnable) {
        if (this.f9318m.post(runnable)) {
            return;
        }
        D(coroutineContext, runnable);
    }
}
