d.java 源代码


package com.bykv.vk.openvk.component.video.a.b;

import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import com.bykv.vk.openvk.component.video.a.b.b;
import com.bykv.vk.openvk.component.video.a.b.i;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class d {

    private static volatile d f8128e;

    private volatile int f8129a = 163840;

    private final SparseArray<Map<String, com.bykv.vk.openvk.component.video.a.b.b>> f8130b;

    private final b<Runnable> f8131c;

    private final ExecutorService f8132d;

    private volatile com.bykv.vk.openvk.component.video.a.b.b.c f8133f;

    private volatile com.bykv.vk.openvk.component.video.a.b.a.c f8134g;

    private volatile com.bykv.vk.openvk.component.video.a.b.a.b f8135h;

    private final HashSet<a> f8136i;

    private final b.InterfaceC0140b f8137j;

    private volatile c f8138k;

    private volatile c f8139l;

    private volatile String f8140m;

    private volatile boolean f8141n;

    public static final class a {

        final boolean f8150a;

        final boolean f8151b;

        final int f8152c;

        final String f8153d;

        final Map<String, String> f8154e;

        final String[] f8155f;

        a(boolean z6, boolean z7, int i6, String str, Map<String, String> map, String[] strArr) {
            this.f8150a = z6;
            this.f8151b = z7;
            this.f8152c = i6;
            this.f8153d = str;
            this.f8154e = map;
            this.f8155f = strArr;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || a.class != obj.getClass()) {
                return false;
            }
            a aVar = (a) obj;
            if (this.f8150a != aVar.f8150a || this.f8151b != aVar.f8151b || this.f8152c != aVar.f8152c) {
                return false;
            }
            return this.f8153d.equals(aVar.f8153d);
        }

        public int hashCode() {
            return ((((((this.f8150a ? 1 : 0) * 31) + (this.f8151b ? 1 : 0)) * 31) + this.f8152c) * 31) + this.f8153d.hashCode();
        }
    }

    public static final class b<T> extends LinkedBlockingDeque<T> {

        private ThreadPoolExecutor f8156a;

        private b() {
        }

        public void a(ThreadPoolExecutor threadPoolExecutor) {
            synchronized (this) {
                if (this.f8156a == null) {
                    if (threadPoolExecutor != null) {
                        this.f8156a = threadPoolExecutor;
                    } else {
                        throw new NullPointerException("executor argument can't be null!");
                    }
                } else {
                    throw new IllegalStateException("You can only call setExecutor() once!");
                }
            }
        }

        @Override
        public boolean offer(T t6) {
            synchronized (this) {
                int poolSize = this.f8156a.getPoolSize();
                int activeCount = this.f8156a.getActiveCount();
                int maximumPoolSize = this.f8156a.getMaximumPoolSize();
                if (activeCount >= poolSize && poolSize < maximumPoolSize) {
                    if (e.f8159c) {
                        Log.i("TAG_PROXY_TT", "create new preloader thread");
                    }
                    return false;
                }
                return offerFirst(t6);
            }
        }
    }

    private d() {
        SparseArray<Map<String, com.bykv.vk.openvk.component.video.a.b.b>> sparseArray = new SparseArray<>(2);
        this.f8130b = sparseArray;
        this.f8136i = new HashSet<>();
        this.f8137j = new b.InterfaceC0140b() {
            @Override
            public void a(com.bykv.vk.openvk.component.video.a.b.b bVar) {
                int f6 = bVar.f();
                synchronized (d.this.f8130b) {
                    Map map = (Map) d.this.f8130b.get(f6);
                    if (map != null) {
                        map.remove(bVar.f8065h);
                    }
                }
                if (e.f8159c) {
                    Log.d("TAG_PROXY_Preloader", "afterExecute, key: " + bVar.f8065h);
                }
            }
        };
        b<Runnable> bVar = new b<>();
        this.f8131c = bVar;
        ExecutorService a7 = a(bVar);
        this.f8132d = a7;
        bVar.a((ThreadPoolExecutor) a7);
        sparseArray.put(0, new HashMap());
        sparseArray.put(1, new HashMap());
    }

    public static d c() {
        if (f8128e == null) {
            synchronized (d.class) {
                if (f8128e == null) {
                    f8128e = new d();
                }
            }
        }
        return f8128e;
    }

    public void d() {
        com.bykv.vk.openvk.component.video.a.c.a.a(new com.bytedance.sdk.component.f.g("cancelAll") {
            @Override
            public void run() {
                ArrayList<com.bykv.vk.openvk.component.video.a.b.b> arrayList = new ArrayList();
                synchronized (d.this.f8130b) {
                    int size = d.this.f8130b.size();
                    for (int i6 = 0; i6 < size; i6++) {
                        Map map = (Map) d.this.f8130b.get(d.this.f8130b.keyAt(i6));
                        if (map != null) {
                            arrayList.addAll(map.values());
                            map.clear();
                        }
                    }
                    d.this.f8131c.clear();
                }
                for (com.bykv.vk.openvk.component.video.a.b.b bVar : arrayList) {
                    bVar.a();
                    if (e.f8159c) {
                        Log.w("TAG_PROXY_Preloader", "PreloadTask: " + bVar + ", canceled!!!");
                    }
                }
            }
        });
    }

    public void a(com.bykv.vk.openvk.component.video.a.b.a.c cVar) {
        this.f8134g = cVar;
    }

    public c b() {
        return this.f8139l;
    }

    public void a(com.bykv.vk.openvk.component.video.a.b.b.c cVar) {
        this.f8133f = cVar;
    }

    public void a(int i6) {
        if (i6 > 0) {
            this.f8129a = i6;
        }
        if (e.f8159c) {
            Log.i("TAG_PROXY_Preloader", "MaxPreloadSize: " + i6);
        }
    }

    public synchronized void a(long j6, long j7, long j8) {
    }

    public c a() {
        return this.f8138k;
    }

    public void a(boolean z6, String str) {
        com.bykv.vk.openvk.component.video.a.b.b remove;
        this.f8140m = str;
        this.f8141n = z6;
        if (e.f8159c) {
            Log.i("TAG_PROXY_Preloader", "setCurrentPlayKey, " + str);
        }
        HashSet hashSet = null;
        HashSet hashSet2 = null;
        if (str == null) {
            synchronized (this.f8136i) {
                if (!this.f8136i.isEmpty()) {
                    hashSet2 = new HashSet(this.f8136i);
                    this.f8136i.clear();
                }
            }
            if (hashSet2 != null) {
                Iterator it = hashSet2.iterator();
                while (it.hasNext()) {
                    a aVar = (a) it.next();
                    a(aVar.f8150a, aVar.f8151b, aVar.f8152c, aVar.f8153d, aVar.f8154e, aVar.f8155f);
                    if (e.f8159c) {
                        Log.i("TAG_PROXY_Preloader", "setCurrentPlayKey, resume preload: " + aVar.f8153d);
                    }
                }
                return;
            }
            return;
        }
        int i6 = e.f8164h;
        if (i6 != 3 && i6 != 2) {
            if (i6 == 1) {
                synchronized (this.f8130b) {
                    Map<String, com.bykv.vk.openvk.component.video.a.b.b> map = this.f8130b.get(com.bykv.vk.openvk.component.video.a.b.b.b.a(z6));
                    remove = map != null ? map.remove(str) : null;
                }
                if (remove != null) {
                    remove.a();
                    return;
                }
                return;
            }
            return;
        }
        synchronized (this.f8130b) {
            int size = this.f8130b.size();
            for (int i7 = 0; i7 < size; i7++) {
                SparseArray<Map<String, com.bykv.vk.openvk.component.video.a.b.b>> sparseArray = this.f8130b;
                Map<String, com.bykv.vk.openvk.component.video.a.b.b> map2 = sparseArray.get(sparseArray.keyAt(i7));
                if (map2 != null) {
                    Collection<com.bykv.vk.openvk.component.video.a.b.b> values = map2.values();
                    if (values != null && !values.isEmpty()) {
                        if (hashSet == null) {
                            hashSet = new HashSet();
                        }
                        hashSet.addAll(values);
                    }
                    map2.clear();
                }
            }
        }
        if (hashSet == null || hashSet.isEmpty()) {
            return;
        }
        Iterator it2 = hashSet.iterator();
        while (it2.hasNext()) {
            com.bykv.vk.openvk.component.video.a.b.b bVar = (com.bykv.vk.openvk.component.video.a.b.b) it2.next();
            bVar.a();
            if (e.f8159c) {
                Log.i("TAG_PROXY_Preloader", "setCurrentPlayKey, cancel preload: " + bVar.f8064g);
            }
        }
        if (i6 == 3) {
            synchronized (this.f8136i) {
                Iterator it3 = hashSet.iterator();
                while (it3.hasNext()) {
                    a aVar2 = (a) ((com.bykv.vk.openvk.component.video.a.b.b) it3.next()).f8096n;
                    if (aVar2 != null) {
                        this.f8136i.add(aVar2);
                    }
                }
            }
        }
    }

    public void a(boolean z6, boolean z7, int i6, String str, String... strArr) {
        a(z6, z7, i6, str, null, strArr);
    }

    public void a(boolean z6, boolean z7, int i6, String str, Map<String, String> map, String... strArr) {
        SparseArray<Map<String, com.bykv.vk.openvk.component.video.a.b.b>> sparseArray;
        ArrayList arrayList;
        boolean z8 = e.f8159c;
        if (z8) {
            Log.d("TAG_PROXY_Preloader", "preload start !!!!");
        }
        com.bykv.vk.openvk.component.video.a.b.a.a aVar = z6 ? this.f8135h : this.f8134g;
        com.bykv.vk.openvk.component.video.a.b.b.c cVar = this.f8133f;
        if (aVar == null || cVar == null) {
            if (z8) {
                Log.e("TAG_PROXY_Preloader", "cache or videoProxyDB null in Preloader!!!");
                return;
            }
            return;
        }
        if (TextUtils.isEmpty(str) || strArr == null || strArr.length <= 0) {
            return;
        }
        int i7 = i6 <= 0 ? this.f8129a : i6;
        String a7 = z7 ? str : com.bykv.vk.openvk.component.video.api.e.b.a(str);
        File d6 = aVar.d(a7);
        if (d6 != null && d6.length() >= i7) {
            if (z8) {
                Log.i("TAG_PROXY_Preloader", "no need preload, file size: " + d6.length() + ", need preload size: " + i7);
                return;
            }
            return;
        }
        if (f.a().a(com.bykv.vk.openvk.component.video.a.b.b.b.a(z6), a7)) {
            if (z8) {
                Log.w("TAG_PROXY_Preloader", "has running proxy task, skip preload for key: " + str);
                return;
            }
            return;
        }
        SparseArray<Map<String, com.bykv.vk.openvk.component.video.a.b.b>> sparseArray2 = this.f8130b;
        synchronized (sparseArray2) {
            try {
                Map<String, com.bykv.vk.openvk.component.video.a.b.b> map2 = this.f8130b.get(z6 ? 1 : 0);
                if (!map2.containsKey(a7)) {
                    int i8 = i7;
                    sparseArray = sparseArray2;
                    try {
                        a aVar2 = new a(z6, z7, i7, str, map, strArr);
                        String str2 = this.f8140m;
                        if (str2 != null) {
                            int i9 = e.f8164h;
                            if (i9 == 3) {
                                synchronized (this.f8136i) {
                                    this.f8136i.add(aVar2);
                                }
                                if (z8) {
                                    Log.w("TAG_PROXY_Preloader", "cancel preload: " + str + ", add to pending queue");
                                }
                                return;
                            }
                            if (i9 == 2) {
                                if (z8) {
                                    Log.w("TAG_PROXY_Preloader", "cancel preload: " + str);
                                }
                                return;
                            }
                            if (i9 == 1 && this.f8141n == z6 && str2.equals(a7)) {
                                if (z8) {
                                    Log.w("TAG_PROXY_Preloader", "cancel preload: " + str + ", it is playing");
                                }
                                return;
                            }
                        }
                        List<i.b> a8 = com.bykv.vk.openvk.component.video.a.c.a.a(com.bykv.vk.openvk.component.video.a.c.a.a(map));
                        if (a8 != null) {
                            arrayList = new ArrayList(a8.size());
                            int size = a8.size();
                            for (int i10 = 0; i10 < size; i10++) {
                                i.b bVar = a8.get(i10);
                                if (bVar != null) {
                                    arrayList.add(new i.b(bVar.f8239a, bVar.f8240b));
                                }
                            }
                        } else {
                            arrayList = null;
                        }
                        com.bykv.vk.openvk.component.video.a.b.b a9 = new b.a().a(aVar).a(cVar).a(str).b(a7).a(new l(com.bykv.vk.openvk.component.video.a.c.a.a(strArr))).a((List<i.b>) arrayList).a(i8).a(this.f8137j).a(aVar2).a();
                        map2.put(a7, a9);
                        this.f8132d.execute(a9);
                    } catch (Throwable th) {
                        th = th;
                        throw th;
                    }
                }
            } catch (Throwable th2) {
                th = th2;
                sparseArray = sparseArray2;
            }
        }
    }

    public void a(String str) {
        a(false, false, str);
    }

    public void a(final boolean z6, final boolean z7, final String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        com.bykv.vk.openvk.component.video.a.c.a.a(new com.bytedance.sdk.component.f.g("cancel b b S") {
            @Override
            public void run() {
                com.bykv.vk.openvk.component.video.a.b.b bVar;
                String a7;
                synchronized (d.this.f8130b) {
                    Map map = (Map) d.this.f8130b.get(com.bykv.vk.openvk.component.video.a.b.b.b.a(z6));
                    if (map != null) {
                        if (z7) {
                            a7 = str;
                        } else {
                            a7 = com.bykv.vk.openvk.component.video.api.e.b.a(str);
                        }
                        bVar = (com.bykv.vk.openvk.component.video.a.b.b) map.remove(a7);
                    } else {
                        bVar = null;
                    }
                }
                if (bVar != null) {
                    bVar.a();
                }
            }
        });
    }

    private static ExecutorService a(final b<Runnable> bVar) {
        int a7 = com.bykv.vk.openvk.component.video.a.c.a.a();
        return new ThreadPoolExecutor(0, a7 < 1 ? 1 : a7 > 4 ? 4 : a7, 60L, TimeUnit.SECONDS, bVar, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable runnable) {
                Thread thread = new Thread(runnable) {
                    @Override
                    public void run() {
                        try {
                            Process.setThreadPriority(10);
                        } catch (Throwable th) {
                            th.printStackTrace();
                        }
                        super.run();
                    }
                };
                thread.setName("tt_pangle_thread_video_preload_" + thread.getId());
                thread.setDaemon(true);
                if (e.f8159c) {
                    Log.i("TAG_PROXY_Preloader", "new preload thead: " + thread.getName());
                }
                return thread;
            }
        }, new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable runnable, ThreadPoolExecutor threadPoolExecutor) {
                try {
                    b.this.offerFirst(runnable);
                    if (e.f8159c) {
                        Log.i("TAG_PROXY_TT", "task rejected in preloader, put first!!!");
                    }
                } catch (Throwable th) {
                    th.printStackTrace();
                }
            }
        });
    }
}