package com.bytedance.sdk.component.wu.ro;

import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;
import androidx.media3.exoplayer.dash.DashMediaSource;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public class wsy extends ThreadPoolExecutor {
    private final String fm;
    private boolean jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private int f6709lb;
    private int ro;
    private int yz;

    public static class fm {
        private String fm = "cache";
        private int ro = 4;

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        private int f6711lb = 100;
        private int yz = 0;
        private long jnr = DashMediaSource.DEFAULT_FALLBACK_TARGET_LIVE_OFFSET_MS;
        private boolean ajl = false;
        private TimeUnit wsy = TimeUnit.MILLISECONDS;
        private boolean wu = false;

        /* JADX INFO: renamed from: ef, reason: collision with root package name */
        private BlockingQueue<Runnable> f6710ef = new PriorityBlockingQueue();
        private ThreadFactory vt = null;

        public fm fm(int i10) {
            this.ro = i10;
            return this;
        }

        public fm jnr(int i10) {
            return this;
        }

        public fm lb(int i10) {
            this.yz = i10;
            return this;
        }

        public fm ro(int i10) {
            this.f6711lb = i10;
            return this;
        }

        public fm yz(int i10) {
            return this;
        }

        public fm fm(long j10) {
            this.jnr = j10;
            return this;
        }

        public fm ro(boolean z10) {
            this.wu = z10;
            return this;
        }

        public fm fm(String str) {
            this.fm = str;
            return this;
        }

        public fm fm(BlockingQueue<Runnable> blockingQueue) {
            this.f6710ef = blockingQueue;
            return this;
        }

        public fm fm(boolean z10) {
            this.ajl = z10;
            return this;
        }

        public wsy fm() {
            if (this.vt == null) {
                this.vt = new jnr(this.fm);
            }
            if (this.ro < 0) {
                this.ro = 8;
            }
            if (this.ro == 0) {
                this.f6710ef = new SynchronousQueue();
            }
            if (this.f6710ef == null) {
                this.f6710ef = new LinkedBlockingQueue();
            }
            if (this.f6711lb > 100) {
                this.f6711lb = 100;
            }
            int i10 = this.f6711lb;
            int i11 = this.ro;
            if (i10 < i11) {
                this.f6711lb = i11;
            }
            return new wsy(this);
        }
    }

    private void lb() {
        try {
            if (this.ro != 0 && getCorePoolSize() < this.f6709lb) {
                int size = getQueue().size();
                if (getActiveCount() < this.ro || size < this.yz) {
                    return;
                }
                setCorePoolSize(this.f6709lb);
            }
        } catch (Exception e10) {
            e10.getMessage();
        }
    }

    private void yz() {
        try {
            if (this.ro != 0 && getCorePoolSize() > this.ro && getQueue().size() == 0) {
                setCorePoolSize(this.ro);
            }
        } catch (Exception e10) {
            e10.getMessage();
        }
    }

    @Override // java.util.concurrent.ThreadPoolExecutor
    public void afterExecute(Runnable runnable, Throwable th) {
        boolean z10 = runnable instanceof lb;
        if (z10) {
            ((lb) runnable).setAfterTimestamp(SystemClock.elapsedRealtime());
        }
        super.afterExecute(runnable, th);
        if (z10) {
            lb lbVar = (lb) runnable;
            lbVar.getName();
            lbVar.getPriority();
            lbVar.getSubmitTimestamp();
            lbVar.getBeforeTimestamp();
            lbVar.getAfterTimestamp();
        }
        yz();
    }

    @Override // java.util.concurrent.ThreadPoolExecutor
    public void beforeExecute(Thread thread, Runnable runnable) {
        if (runnable instanceof lb) {
            ((lb) runnable).setBeforeTimestamp(SystemClock.elapsedRealtime());
        }
        super.beforeExecute(thread, runnable);
    }

    @Override // java.util.concurrent.ThreadPoolExecutor, java.util.concurrent.Executor
    public void execute(Runnable runnable) {
        com.bytedance.sdk.component.wu.ro.fm fmVarFm;
        if (!(runnable instanceof lb)) {
            runnable = new lb("unknown", runnable) { // from class: com.bytedance.sdk.component.wu.ro.wsy.2
                @Override // java.lang.Runnable
                public void run() {
                    Runnable target = getTarget();
                    if (target != null) {
                        target.run();
                    }
                }
            };
        }
        if (!"cache".equals(this.fm)) {
            String name = Thread.currentThread().getName();
            if (!TextUtils.isEmpty(name) && name.startsWith(jnr.fm(this.fm)) && (fmVarFm = yz.fm()) != null) {
                fmVarFm.fm(this, (lb) runnable);
            }
        }
        ((lb) runnable).setSubmitTimestamp(SystemClock.elapsedRealtime());
        try {
            super.execute(runnable);
            lb();
        } catch (Throwable th) {
            fm(runnable, th);
        }
    }

    public int fm() {
        return this.f6709lb;
    }

    public boolean ro() {
        return this.jnr;
    }

    @Override // java.util.concurrent.ThreadPoolExecutor, java.util.concurrent.ExecutorService
    public void shutdown() {
        if ("aidl".equals(this.fm)) {
            return;
        }
        super.shutdown();
    }

    @Override // java.util.concurrent.ThreadPoolExecutor, java.util.concurrent.ExecutorService
    public List<Runnable> shutdownNow() {
        return "aidl".equals(this.fm) ? Collections.emptyList() : super.shutdownNow();
    }

    @Override // java.util.concurrent.AbstractExecutorService, java.util.concurrent.ExecutorService
    public Future<?> submit(Runnable runnable) {
        int priority;
        String name;
        runnable.getClass();
        final RunnableFuture runnableFutureNewTaskFor = newTaskFor(runnable, null);
        if (runnable instanceof lb) {
            lb lbVar = (lb) runnable;
            priority = lbVar.getPriority();
            name = lbVar.getName();
        } else {
            priority = 6;
            name = "";
        }
        if (priority == 0 || TextUtils.isEmpty(name)) {
            new RuntimeException();
        }
        execute(new lb(name, priority) { // from class: com.bytedance.sdk.component.wu.ro.wsy.1
            @Override // java.lang.Runnable
            public void run() {
                runnableFutureNewTaskFor.run();
            }
        });
        return runnableFutureNewTaskFor;
    }

    private wsy(fm fmVar) {
        super(fmVar.ro, Integer.MAX_VALUE, fmVar.jnr, fmVar.wsy, (BlockingQueue<Runnable>) fmVar.f6710ef, fmVar.vt);
        this.jnr = false;
        String unused = fmVar.fm;
        int unused2 = fmVar.ro;
        int unused3 = fmVar.f6711lb;
        long unused4 = fmVar.jnr;
        int unused5 = fmVar.yz;
        boolean unused6 = fmVar.wu;
        BlockingQueue unused7 = fmVar.f6710ef;
        this.fm = fmVar.fm;
        this.ro = fmVar.ro;
        this.f6709lb = fmVar.f6711lb;
        this.yz = fmVar.yz;
        allowCoreThreadTimeOut(fmVar.ajl);
        this.jnr = fmVar.wu;
    }

    public void fm(fm fmVar) {
        try {
            if (fmVar.ro >= 0 && this.ro != fmVar.ro) {
                int i10 = fmVar.ro;
                this.ro = i10;
                setCorePoolSize(i10);
            }
            this.f6709lb = fmVar.f6711lb;
            this.yz = fmVar.yz;
            allowCoreThreadTimeOut(fmVar.ajl);
            this.jnr = fmVar.wu;
        } catch (Throwable th) {
            th.getMessage();
        }
        String unused = fmVar.fm;
        int unused2 = fmVar.ro;
        int unused3 = fmVar.f6711lb;
        long unused4 = fmVar.jnr;
        int unused5 = fmVar.yz;
        boolean unused6 = fmVar.wu;
        BlockingQueue unused7 = fmVar.f6710ef;
    }

    private void fm(Runnable runnable, Throwable th) {
        try {
            if (Looper.myLooper() == Looper.getMainLooper()) {
                Handler handlerRo = yz.ro();
                if (handlerRo != null) {
                    handlerRo.post(runnable);
                    return;
                }
                return;
            }
            runnable.run();
        } catch (Throwable unused) {
        }
    }
}
