package X6FRs_4.IMuAHx_6.XnFJ5_9;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/* compiled from: SelfDestructiveThread.java */
/* loaded from: classes.dex */
public class PGPF6hQ_2 {

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public Handler f526KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public HandlerThread f527KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final String f529KQgRr5e_0;
    public final int PGPF6hQ_2;
    public final int QxJ9d_1;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final Object f528KQgRr5e_0 = new Object();

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public Handler.Callback f525KQgRr5e_0 = new KQgRr5e_0();
    public int KQgRr5e_0 = 0;

    /* compiled from: SelfDestructiveThread.java */
    /* loaded from: classes.dex */
    public class KQgRr5e_0 implements Handler.Callback {
        public KQgRr5e_0() {
        }

        @Override // android.os.Handler.Callback
        public boolean handleMessage(Message message) {
            int i = message.what;
            if (i == 0) {
                PGPF6hQ_2.this.KQgRr5e_0();
                return true;
            } else if (i != 1) {
                return true;
            } else {
                PGPF6hQ_2.this.KQgRr5e_0((Runnable) message.obj);
                return true;
            }
        }
    }

    /* compiled from: SelfDestructiveThread.java */
    /* loaded from: classes.dex */
    public interface Mqv2r_3<T> {
        void KQgRr5e_0(T t);
    }

    /* compiled from: SelfDestructiveThread.java */
    /* renamed from: X6FRs_4.IMuAHx_6.XnFJ5_9.PGPF6hQ_2$PGPF6hQ_2  reason: collision with other inner class name */
    /* loaded from: classes.dex */
    public class RunnableC0047PGPF6hQ_2 implements Runnable {
        public final /* synthetic */ Callable KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final /* synthetic */ AtomicBoolean f530KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final /* synthetic */ AtomicReference f531KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final /* synthetic */ Condition f532KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final /* synthetic */ ReentrantLock f533KQgRr5e_0;

        public RunnableC0047PGPF6hQ_2(PGPF6hQ_2 pGPF6hQ_2, AtomicReference atomicReference, Callable callable, ReentrantLock reentrantLock, AtomicBoolean atomicBoolean, Condition condition) {
            this.f531KQgRr5e_0 = atomicReference;
            this.KQgRr5e_0 = callable;
            this.f533KQgRr5e_0 = reentrantLock;
            this.f530KQgRr5e_0 = atomicBoolean;
            this.f532KQgRr5e_0 = condition;
        }

        @Override // java.lang.Runnable
        public void run() {
            try {
                this.f531KQgRr5e_0.set(this.KQgRr5e_0.call());
            } catch (Exception unused) {
            }
            this.f533KQgRr5e_0.lock();
            try {
                this.f530KQgRr5e_0.set(false);
                this.f532KQgRr5e_0.signal();
            } finally {
                this.f533KQgRr5e_0.unlock();
            }
        }
    }

    /* compiled from: SelfDestructiveThread.java */
    /* loaded from: classes.dex */
    public class QxJ9d_1 implements Runnable {
        public final /* synthetic */ Mqv2r_3 KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final /* synthetic */ Handler f534KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final /* synthetic */ Callable f535KQgRr5e_0;

        /* compiled from: SelfDestructiveThread.java */
        /* loaded from: classes.dex */
        public class KQgRr5e_0 implements Runnable {

            /* renamed from: KQgRr5e_0  reason: collision with other field name */
            public final /* synthetic */ Object f536KQgRr5e_0;

            public KQgRr5e_0(Object obj) {
                this.f536KQgRr5e_0 = obj;
            }

            @Override // java.lang.Runnable
            public void run() {
                QxJ9d_1.this.KQgRr5e_0.KQgRr5e_0(this.f536KQgRr5e_0);
            }
        }

        public QxJ9d_1(PGPF6hQ_2 pGPF6hQ_2, Callable callable, Handler handler, Mqv2r_3 mqv2r_3) {
            this.f535KQgRr5e_0 = callable;
            this.f534KQgRr5e_0 = handler;
            this.KQgRr5e_0 = mqv2r_3;
        }

        @Override // java.lang.Runnable
        public void run() {
            Object obj;
            try {
                obj = this.f535KQgRr5e_0.call();
            } catch (Exception unused) {
                obj = null;
            }
            this.f534KQgRr5e_0.post(new KQgRr5e_0(obj));
        }
    }

    public PGPF6hQ_2(String str, int i, int i2) {
        this.f529KQgRr5e_0 = str;
        this.PGPF6hQ_2 = i;
        this.QxJ9d_1 = i2;
    }

    public <T> void KQgRr5e_0(Callable<T> callable, Mqv2r_3<T> mqv2r_3) {
        QxJ9d_1(new QxJ9d_1(this, callable, new Handler(), mqv2r_3));
    }

    public final void QxJ9d_1(Runnable runnable) {
        synchronized (this.f528KQgRr5e_0) {
            if (this.f527KQgRr5e_0 == null) {
                HandlerThread handlerThread = new HandlerThread(this.f529KQgRr5e_0, this.PGPF6hQ_2);
                this.f527KQgRr5e_0 = handlerThread;
                handlerThread.start();
                this.f526KQgRr5e_0 = new Handler(this.f527KQgRr5e_0.getLooper(), this.f525KQgRr5e_0);
                this.KQgRr5e_0++;
            }
            this.f526KQgRr5e_0.removeMessages(0);
            this.f526KQgRr5e_0.sendMessage(this.f526KQgRr5e_0.obtainMessage(1, runnable));
        }
    }

    public <T> T KQgRr5e_0(Callable<T> callable, int i) throws InterruptedException {
        ReentrantLock reentrantLock = new ReentrantLock();
        Condition newCondition = reentrantLock.newCondition();
        AtomicReference atomicReference = new AtomicReference();
        AtomicBoolean atomicBoolean = new AtomicBoolean(true);
        QxJ9d_1(new RunnableC0047PGPF6hQ_2(this, atomicReference, callable, reentrantLock, atomicBoolean, newCondition));
        reentrantLock.lock();
        try {
            if (!atomicBoolean.get()) {
                return (T) atomicReference.get();
            }
            long nanos = TimeUnit.MILLISECONDS.toNanos(i);
            do {
                try {
                    nanos = newCondition.awaitNanos(nanos);
                } catch (InterruptedException unused) {
                }
                if (!atomicBoolean.get()) {
                    return (T) atomicReference.get();
                }
            } while (nanos > 0);
            throw new InterruptedException("timeout");
        } finally {
            reentrantLock.unlock();
        }
    }

    public void KQgRr5e_0(Runnable runnable) {
        runnable.run();
        synchronized (this.f528KQgRr5e_0) {
            this.f526KQgRr5e_0.removeMessages(0);
            this.f526KQgRr5e_0.sendMessageDelayed(this.f526KQgRr5e_0.obtainMessage(0), this.QxJ9d_1);
        }
    }

    public void KQgRr5e_0() {
        synchronized (this.f528KQgRr5e_0) {
            if (this.f526KQgRr5e_0.hasMessages(1)) {
                return;
            }
            this.f527KQgRr5e_0.quit();
            this.f527KQgRr5e_0 = null;
            this.f526KQgRr5e_0 = null;
        }
    }
}
