package androidx.lifecycle;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import androidx.lifecycle.m0;
import androidx.lifecycle.n;

/* loaded from: classes.dex */
public final class j0 implements u {

    /* renamed from: x, reason: collision with root package name */
    public static final b f4814x = new b(null);

    /* renamed from: y, reason: collision with root package name */
    private static final j0 f4815y = new j0();

    /* renamed from: p, reason: collision with root package name */
    private int f4816p;

    /* renamed from: q, reason: collision with root package name */
    private int f4817q;

    /* renamed from: t, reason: collision with root package name */
    private Handler f4820t;

    /* renamed from: r, reason: collision with root package name */
    private boolean f4818r = true;

    /* renamed from: s, reason: collision with root package name */
    private boolean f4819s = true;

    /* renamed from: u, reason: collision with root package name */
    private final w f4821u = new w(this);

    /* renamed from: v, reason: collision with root package name */
    private final Runnable f4822v = new Runnable() { // from class: androidx.lifecycle.i0
        @Override // java.lang.Runnable
        public final void run() {
            j0.h(j0.this);
        }
    };

    /* renamed from: w, reason: collision with root package name */
    private final m0.a f4823w = new d();

    /* loaded from: classes.dex */
    public static final class a {

        /* renamed from: a, reason: collision with root package name */
        public static final a f4824a = new a();

        private a() {
        }

        public static final void a(Activity activity, Application.ActivityLifecycleCallbacks callback) {
            kotlin.jvm.internal.t.g(activity, "activity");
            kotlin.jvm.internal.t.g(callback, "callback");
            activity.registerActivityLifecycleCallbacks(callback);
        }
    }

    /* loaded from: classes.dex */
    public static final class b {
        private b() {
        }

        public /* synthetic */ b(kotlin.jvm.internal.k kVar) {
            this();
        }

        public final u a() {
            return j0.f4815y;
        }

        public final void b(Context context) {
            kotlin.jvm.internal.t.g(context, "context");
            j0.f4815y.g(context);
        }
    }

    /* loaded from: classes.dex */
    public static final class c extends j {

        /* loaded from: classes.dex */
        public static final class a extends j {
            final /* synthetic */ j0 this$0;

            a(j0 j0Var) {
                this.this$0 = j0Var;
            }

            @Override // android.app.Application.ActivityLifecycleCallbacks
            public void onActivityPostResumed(Activity activity) {
                kotlin.jvm.internal.t.g(activity, "activity");
                this.this$0.d();
            }

            @Override // android.app.Application.ActivityLifecycleCallbacks
            public void onActivityPostStarted(Activity activity) {
                kotlin.jvm.internal.t.g(activity, "activity");
                this.this$0.e();
            }
        }

        c() {
        }

        @Override // androidx.lifecycle.j, android.app.Application.ActivityLifecycleCallbacks
        public void onActivityCreated(Activity activity, Bundle bundle) {
            kotlin.jvm.internal.t.g(activity, "activity");
        }

        @Override // androidx.lifecycle.j, android.app.Application.ActivityLifecycleCallbacks
        public void onActivityPaused(Activity activity) {
            kotlin.jvm.internal.t.g(activity, "activity");
            j0.this.c();
        }

        @Override // android.app.Application.ActivityLifecycleCallbacks
        public void onActivityPreCreated(Activity activity, Bundle bundle) {
            kotlin.jvm.internal.t.g(activity, "activity");
            a.a(activity, new a(j0.this));
        }

        @Override // androidx.lifecycle.j, android.app.Application.ActivityLifecycleCallbacks
        public void onActivityStopped(Activity activity) {
            kotlin.jvm.internal.t.g(activity, "activity");
            j0.this.f();
        }
    }

    /* loaded from: classes.dex */
    public static final class d implements m0.a {
        d() {
        }

        @Override // androidx.lifecycle.m0.a
        public void a() {
        }

        @Override // androidx.lifecycle.m0.a
        public void onResume() {
            j0.this.d();
        }

        @Override // androidx.lifecycle.m0.a
        public void onStart() {
            j0.this.e();
        }
    }

    private j0() {
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void h(j0 this$0) {
        kotlin.jvm.internal.t.g(this$0, "this$0");
        this$0.i();
        this$0.j();
    }

    public static final u k() {
        return f4814x.a();
    }

    public final void c() {
        int i10 = this.f4817q - 1;
        this.f4817q = i10;
        if (i10 == 0) {
            Handler handler = this.f4820t;
            kotlin.jvm.internal.t.d(handler);
            handler.postDelayed(this.f4822v, 700L);
        }
    }

    public final void d() {
        int i10 = this.f4817q + 1;
        this.f4817q = i10;
        if (i10 == 1) {
            if (this.f4818r) {
                this.f4821u.i(n.a.ON_RESUME);
                this.f4818r = false;
            } else {
                Handler handler = this.f4820t;
                kotlin.jvm.internal.t.d(handler);
                handler.removeCallbacks(this.f4822v);
            }
        }
    }

    public final void e() {
        int i10 = this.f4816p + 1;
        this.f4816p = i10;
        if (i10 == 1 && this.f4819s) {
            this.f4821u.i(n.a.ON_START);
            this.f4819s = false;
        }
    }

    public final void f() {
        this.f4816p--;
        j();
    }

    public final void g(Context context) {
        kotlin.jvm.internal.t.g(context, "context");
        this.f4820t = new Handler();
        this.f4821u.i(n.a.ON_CREATE);
        Context applicationContext = context.getApplicationContext();
        kotlin.jvm.internal.t.e(applicationContext, "null cannot be cast to non-null type android.app.Application");
        ((Application) applicationContext).registerActivityLifecycleCallbacks(new c());
    }

    @Override // androidx.lifecycle.u
    public n getLifecycle() {
        return this.f4821u;
    }

    public final void i() {
        if (this.f4817q == 0) {
            this.f4818r = true;
            this.f4821u.i(n.a.ON_PAUSE);
        }
    }

    public final void j() {
        if (this.f4816p == 0 && this.f4818r) {
            this.f4821u.i(n.a.ON_STOP);
            this.f4819s = true;
        }
    }
}
