package com.apm.insight.runtime;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import androidx.annotation.Nullable;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/* JADX INFO: loaded from: classes.dex */
public final class p {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final HandlerThread f5523a;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private volatile Handler f5526d;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final Queue<c> f5524b = new ConcurrentLinkedQueue();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final Queue<Message> f5525c = new ConcurrentLinkedQueue();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final Object f5527e = new Object();

    public class a implements Runnable {
        public a() {
        }

        @Override // java.lang.Runnable
        public final void run() {
            while (!p.this.f5525c.isEmpty()) {
                if (p.this.f5526d != null) {
                    try {
                        p.this.f5526d.sendMessageAtFrontOfQueue((Message) p.this.f5525c.poll());
                    } catch (Throwable unused) {
                    }
                }
            }
            while (!p.this.f5524b.isEmpty()) {
                c cVar = (c) p.this.f5524b.poll();
                if (p.this.f5526d != null) {
                    try {
                        p.this.f5526d.sendMessageAtTime(cVar.f5532a, cVar.f5533b);
                    } catch (Throwable unused2) {
                    }
                }
            }
        }
    }

    public class b extends HandlerThread {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private volatile int f5529a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private volatile boolean f5530b;

        public b(String str) {
            super(str);
            this.f5529a = 0;
            this.f5530b = false;
        }

        @Override // android.os.HandlerThread
        public final void onLooperPrepared() {
            super.onLooperPrepared();
            synchronized (p.this.f5527e) {
                p.this.f5526d = new Handler();
            }
            p.this.f5526d.post(p.this.new a());
            while (true) {
                try {
                    Looper.loop();
                } catch (Throwable th) {
                    try {
                        com.apm.insight.b.f.a(com.apm.insight.e.g()).a().c();
                        if (this.f5529a < 5) {
                            com.apm.insight.c.a();
                            j.a(th, "NPTH_CATCH");
                        } else if (!this.f5530b) {
                            this.f5530b = true;
                            com.apm.insight.c.a();
                            j.a(new RuntimeException(), "NPTH_ERR_MAX");
                        }
                        this.f5529a++;
                    } catch (Throwable unused) {
                    }
                }
            }
        }
    }

    public static class c {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        Message f5532a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        long f5533b;

        public c(Message message, long j10) {
            this.f5532a = message;
            this.f5533b = j10;
        }
    }

    static {
        new Object() { // from class: com.apm.insight.runtime.p.1
        };
        new Object() { // from class: com.apm.insight.runtime.p.2
        };
    }

    public p(String str) {
        this.f5523a = new b(str);
    }

    @Nullable
    public final Handler a() {
        return this.f5526d;
    }

    public final HandlerThread c() {
        return this.f5523a;
    }

    public final void b() {
        this.f5523a.start();
    }

    private Message b(Runnable runnable) {
        return Message.obtain(this.f5526d, runnable);
    }

    private boolean b(Message message, long j10) {
        if (this.f5526d == null) {
            synchronized (this.f5527e) {
                if (this.f5526d == null) {
                    this.f5524b.add(new c(message, j10));
                    return true;
                }
            }
        }
        try {
            return this.f5526d.sendMessageAtTime(message, j10);
        } catch (Throwable unused) {
            return true;
        }
    }

    public final boolean a(Runnable runnable) {
        return a(b(runnable), 0L);
    }

    public final boolean a(Runnable runnable, long j10) {
        return a(b(runnable), j10);
    }

    private boolean a(Message message, long j10) {
        if (j10 < 0) {
            j10 = 0;
        }
        return b(message, SystemClock.uptimeMillis() + j10);
    }
}
