导航菜单

页面标题

页面副标题

凤凰之城 v1.0.3 - DataLoaderHelper.java 源代码

正在查看: 凤凰之城 v1.0.3 应用的 DataLoaderHelper.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.bykv.vk.component.ttvideo;

import android.content.Context;
import android.text.TextUtils;
import com.alimm.tanx.ui.image.glide.load.Key;
import com.bykv.vk.component.ttvideo.log.VideoEventEngineUploader;
import com.bykv.vk.component.ttvideo.mediakit.medialoader.AVMDLDataLoader;
import com.bykv.vk.component.ttvideo.mediakit.medialoader.AVMDLDataLoaderConfigure;
import com.bykv.vk.component.ttvideo.mediakit.medialoader.AVMDLDataLoaderListener;
import com.bykv.vk.component.ttvideo.mediakit.medialoader.AVMDLDataLoaderNotifyInfo;
import com.bykv.vk.component.ttvideo.mediakit.medialoader.AVMDLStartCompleteListener;
import com.bykv.vk.component.ttvideo.medialoader.MediaLoaderWrapper;
import com.bykv.vk.component.ttvideo.utils.Error;
import com.bykv.vk.component.ttvideo.utils.TTVideoEngineLog;
import com.bytedance.sdk.component.utils.v;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantLock;

public class DataLoaderHelper implements AVMDLDataLoaderListener, AVMDLStartCompleteListener {
    public static final int DATALOADER_KEY_ENABLE_ALOG = 512;
    public static final int DATALOADER_KEY_INT_MAXCACHESIZE = 1;
    public static final int DATALOADER_KEY_INT_OPENTIMEOUT = 3;
    public static final int DATALOADER_KEY_INT_PARALLEL_NUM = 11;
    public static final int DATALOADER_KEY_INT_RWTIMEOUT = 2;
    public static final int DATALOADER_KEY_INT_TRYCOUNT = 4;
    public static final int DATALOADER_KEY_SET_MDL_PROTOCOL_ENABLE = 1005;
    public static final int DATALOADER_KEY_STRING_CACHEDIR = 0;
    public static final int IsClose = 1;
    public static final int IsPlayTask = 1;
    public static final int IsPreloadTask = 2;
    public static final int IsStart = 0;
    public static final int IsUnknown = 0;
    public static final String MDL_PREFIX = "mdl://";

    private static final DataLoaderHelper f4556p = new DataLoaderHelper();

    private static int f4557q = 0;

    private static int f4558r = 0;

    private static int f4559s = 0;

    private final d f4561b;

    private final d f4562c;

    private final d f4563d;

    private final d f4564e;

    private AVMDLDataLoader f4568i;

    private VideoEventEngineUploader f4570k;

    private boolean f4572m;

    private final ConcurrentHashMap<String, c> f4565f = new ConcurrentHashMap<>();

    private final HashMap<String, ArrayList<TTVideoEngine>> f4566g = new HashMap<>();

    private final ReentrantLock f4567h = new ReentrantLock();

    private boolean f4571l = true;

    private int f4573n = 0;

    private final ReentrantLock f4574o = new ReentrantLock();

    private volatile int f4560a = 1;

    private AVMDLDataLoaderConfigure f4569j = AVMDLDataLoaderConfigure.getDefaultonfigure();

    public class DataLoaderCacheInfo {
        public long mMediaSize = 0;
        public long mCacheSizeFromZero = 0;
        public String mLocalFilePath = null;

        public DataLoaderCacheInfo(DataLoaderHelper dataLoaderHelper) {
        }
    }

    public static class DataLoaderTaskProgressInfo {
        public String mKey = null;
        public String mVideoId = null;
        public String mLocalFilePath = null;
        public long mMediaSize = 0;
        public long mCacheSizeFromZero = 0;
        public int mTaskType = 0;
    }

    public class b {

        public String f4575a = null;

        public long f4576b = 0;

        public PreloaderURLItem f4577c = null;

        public boolean f4578d = false;

        public List<a> f4579e = new CopyOnWriteArrayList();

        public class a {

            public String f4580a = null;

            public String f4581b = null;

            public String[] f4582c = null;

            public String f4583d = null;

            public long f4584e = 0;

            public long f4585f = 0;

            public long f4586g = 0;

            public long f4587h = 0;

            public long f4588i = 0;

            public a(b bVar) {
            }
        }

        public b(DataLoaderHelper dataLoaderHelper) {
        }

        public a a(String str) {
            if (TextUtils.isEmpty(str)) {
                return null;
            }
            a b2 = b(str);
            if (b2 != null) {
                return b2;
            }
            a aVar = new a(this);
            aVar.f4580a = str;
            this.f4579e.add(aVar);
            return aVar;
        }

        public IPreLoaderItemCallBackListener a() {
            PreloaderURLItem preloaderURLItem = this.f4577c;
            if (preloaderURLItem == null || preloaderURLItem.getCallBackListener() == null) {
                return null;
            }
            return this.f4577c.getCallBackListener();
        }

        public void a(int i2) {
            if (a() != null) {
                DataLoaderHelper.c();
                int unused = DataLoaderHelper.f4559s;
                PreLoaderItemCallBackInfo preLoaderItemCallBackInfo = new PreLoaderItemCallBackInfo(3);
                preLoaderItemCallBackInfo.preloadError = new Error(Error.DataLoaderPreload, i2);
                a().preloadItemInfo(preLoaderItemCallBackInfo);
            }
        }

        public void a(String str, long j2) {
            if (this.f4579e.size() == 0) {
                a aVar = new a(this);
                aVar.f4580a = str;
                aVar.f4586g = j2;
                this.f4579e.add(aVar);
            } else {
                TTVideoEngineLog.e("DataLoaderHelper", "[preload] DataLoaderTaskItem setup fail");
            }
            this.f4576b = j2;
        }

        public a b(String str) {
            if (this.f4579e.size() > 0) {
                for (int i2 = 0; i2 < this.f4579e.size(); i2++) {
                    a aVar = this.f4579e.get(i2);
                    if (aVar.f4580a.equals(str)) {
                        return aVar;
                    }
                }
            }
            return null;
        }
    }

    public class c {

        public String f4589a;

        public int f4590b = 0;

        public List<a> f4591c = null;

        public class a {

            public String f4596e;

            public String f4592a = null;

            public long f4593b = 0;

            public long f4594c = 0;

            public List<C0055a> f4595d = null;

            public Error f4597f = null;

            public class C0055a {

                public long f4598a = 0;

                public long f4599b = 0;

                public C0055a(a aVar) {
                }
            }

            public a(c cVar) {
            }

            public void a(long j2) {
                if (this.f4595d == null) {
                    this.f4595d = new ArrayList();
                }
                if (this.f4595d.size() == 0) {
                    this.f4595d.add(new C0055a(this));
                }
                this.f4595d.get(0).f4598a = 0L;
                this.f4595d.get(0).f4599b = j2;
            }

            public boolean a() {
                long j2 = this.f4594c;
                long min = j2 > 0 ? Math.min(j2, this.f4593b) : this.f4593b;
                List<C0055a> list = this.f4595d;
                C0055a c0055a = list != null ? list.get(list.size() - 1) : null;
                if (this.f4597f == null) {
                    return c0055a != null && this.f4593b > 0 && c0055a.f4598a + c0055a.f4599b >= min;
                }
                return true;
            }

            public long b() {
                List<C0055a> list = this.f4595d;
                if (list == null || list.size() <= 0) {
                    return 0L;
                }
                return this.f4595d.get(r0.size() - 1).f4599b;
            }
        }

        public c(DataLoaderHelper dataLoaderHelper) {
        }

        private a a(String str) {
            List<a> list;
            if (!TextUtils.isEmpty(str) && (list = this.f4591c) != null) {
                for (a aVar : list) {
                    if (aVar.f4592a.equals(str)) {
                        return aVar;
                    }
                }
            }
            return null;
        }

        public long a() {
            long j2 = 0;
            if (this.f4591c != null) {
                for (int i2 = 0; i2 < this.f4591c.size(); i2++) {
                    j2 += this.f4591c.get(i2).b();
                }
            }
            return j2;
        }

        public void a(b bVar) {
            if (this.f4591c == null) {
                this.f4591c = new ArrayList();
            }
            for (b.a aVar : bVar.f4579e) {
                boolean z2 = true;
                a a2 = a(aVar.f4580a);
                if (a2 == null) {
                    a2 = new a(this);
                    z2 = false;
                }
                a2.f4592a = aVar.f4580a;
                a2.f4593b = aVar.f4587h;
                a2.a(aVar.f4588i);
                a2.f4594c = aVar.f4586g;
                a2.f4596e = aVar.f4583d;
                if (!z2) {
                    this.f4591c.add(a2);
                }
            }
        }

        public void a(String str, Error error) {
            a a2 = a(str);
            if (a2 != null) {
                a2.f4597f = error;
            }
        }

        public boolean b() {
            boolean z2;
            int i2;
            if (this.f4591c != null) {
                z2 = true;
                i2 = 0;
                for (int i3 = 0; i3 < this.f4591c.size(); i3++) {
                    a aVar = this.f4591c.get(i3);
                    if (aVar == null || aVar.a()) {
                        i2++;
                    } else {
                        z2 = false;
                    }
                }
            } else {
                z2 = true;
                i2 = 0;
            }
            return z2 || (i2 >= 2 && this.f4590b == 1);
        }

        public boolean c() {
            if (this.f4590b != 2) {
                return false;
            }
            if (this.f4591c != null) {
                for (int i2 = 0; i2 < this.f4591c.size(); i2++) {
                    a aVar = this.f4591c.get(i2);
                    if (aVar != null && !aVar.a()) {
                        return false;
                    }
                }
            }
            return true;
        }
    }

    public class d {

        private final ReentrantLock f4600a;

        private final ArrayList<b> f4601b;

        private long f4602c;

        private d(DataLoaderHelper dataLoaderHelper) {
            this.f4600a = new ReentrantLock();
            this.f4601b = new ArrayList<>();
            this.f4602c = 0L;
        }

        private Boolean e() {
            if (this.f4602c >= 1) {
                return Boolean.valueOf(((long) this.f4601b.size()) >= this.f4602c);
            }
            return Boolean.FALSE;
        }

        public b a() {
            this.f4600a.lock();
            if (this.f4601b.size() == 0) {
                this.f4600a.unlock();
                return null;
            }
            b bVar = this.f4601b.get(r0.size() - 1);
            this.f4600a.unlock();
            return bVar;
        }

        public void a(long j2) {
            this.f4602c = j2;
        }

        public boolean a(b bVar) {
            Boolean bool;
            if (bVar == null || bVar.f4579e.size() == 0) {
                return false;
            }
            this.f4600a.lock();
            if (e().booleanValue()) {
                bool = Boolean.FALSE;
            } else {
                this.f4601b.add(bVar);
                bool = Boolean.TRUE;
            }
            this.f4600a.unlock();
            return bool.booleanValue();
        }

        public boolean a(String str) {
            int i2 = 0;
            if (TextUtils.isEmpty(str)) {
                return false;
            }
            Boolean bool = Boolean.FALSE;
            this.f4600a.lock();
            while (true) {
                if (i2 >= this.f4601b.size()) {
                    break;
                }
                if (this.f4601b.get(i2).b(str) != null) {
                    bool = Boolean.TRUE;
                    break;
                }
                i2++;
            }
            this.f4600a.unlock();
            return bool.booleanValue();
        }

        public long b() {
            this.f4600a.lock();
            long size = this.f4601b.size();
            this.f4600a.unlock();
            return size;
        }

        public b b(b bVar) {
            b bVar2;
            if (TextUtils.isEmpty(bVar.f4575a)) {
                bVar2 = null;
            } else {
                bVar2 = d(bVar.f4575a);
                if (bVar2 != null) {
                    return bVar2;
                }
            }
            Iterator<b.a> it = bVar.f4579e.iterator();
            while (it.hasNext() && (bVar2 = c(it.next().f4580a)) == null) {
            }
            return bVar2;
        }

        public b b(String str) {
            b bVar = null;
            if (TextUtils.isEmpty(str)) {
                return null;
            }
            this.f4600a.lock();
            int i2 = 0;
            while (true) {
                if (i2 >= this.f4601b.size()) {
                    break;
                }
                b bVar2 = this.f4601b.get(i2);
                if (bVar2.b(str) != null) {
                    bVar = bVar2;
                    break;
                }
                i2++;
            }
            this.f4600a.unlock();
            return bVar;
        }

        public b c() {
            this.f4600a.lock();
            if (this.f4601b.size() == 0) {
                this.f4600a.unlock();
                return null;
            }
            b bVar = this.f4601b.get(r0.size() - 1);
            this.f4601b.remove(bVar);
            this.f4600a.unlock();
            return bVar;
        }

        public b c(String str) {
            b bVar = null;
            if (TextUtils.isEmpty(str)) {
                return null;
            }
            this.f4600a.lock();
            int i2 = 0;
            while (true) {
                if (i2 >= this.f4601b.size()) {
                    break;
                }
                b bVar2 = this.f4601b.get(i2);
                if (bVar2.b(str) != null) {
                    bVar = bVar2;
                    break;
                }
                i2++;
            }
            if (bVar != null) {
                this.f4601b.remove(bVar);
            }
            this.f4600a.unlock();
            return bVar;
        }

        public b d(String str) {
            b bVar = null;
            if (TextUtils.isEmpty(str)) {
                return null;
            }
            this.f4600a.lock();
            int i2 = 0;
            while (true) {
                if (i2 >= this.f4601b.size()) {
                    break;
                }
                b bVar2 = this.f4601b.get(i2);
                if (!TextUtils.isEmpty(bVar2.f4575a) && bVar2.f4575a.equals(str)) {
                    bVar = bVar2;
                    break;
                }
                i2++;
            }
            if (bVar != null) {
                this.f4601b.remove(bVar);
            }
            this.f4600a.unlock();
            return bVar;
        }

        public ArrayList<b> d() {
            try {
                this.f4600a.lock();
                return new ArrayList<>(this.f4601b);
            } finally {
                this.f4600a.unlock();
            }
        }
    }

    private DataLoaderHelper() {
        this.f4561b = new d();
        this.f4562c = new d();
        this.f4563d = new d();
        this.f4564e = new d();
    }

    private String a(String str, String str2, long j2, long j3, String[] strArr, String str3) {
        String str4;
        if (TextUtils.isEmpty(str) || strArr == null || strArr.length <= 0) {
            return null;
        }
        String[] strArr2 = (String[]) strArr.clone();
        if (j3 <= 0) {
            j3 = 0;
        }
        for (String str5 : strArr2) {
            if (!c(str5)) {
                return null;
            }
        }
        String[] a2 = a(strArr2);
        String encodeUrl = encodeUrl(str);
        if (TextUtils.isEmpty(encodeUrl)) {
            return null;
        }
        TTVideoEngineLog.i("DataLoaderHelper", "preloadProxyQuery: key = " + str + ", rawKey = " + str2);
        if (!TextUtils.isEmpty(str2)) {
            str = str2;
        }
        String encodeUrl2 = encodeUrl(str);
        if (TextUtils.isEmpty(encodeUrl2)) {
            encodeUrl2 = "videoId";
        }
        StringBuffer stringBuffer = new StringBuffer("?rk=");
        stringBuffer.append(encodeUrl2);
        stringBuffer.append("&k=");
        stringBuffer.append(encodeUrl);
        if (j3 > 0) {
            stringBuffer.append("&s=");
            stringBuffer.append(j2 + j3);
        }
        if (!TextUtils.isEmpty(str3)) {
            try {
                str4 = URLEncoder.encode(str3, Key.STRING_CHARSET_NAME);
            } catch (UnsupportedEncodingException e2) {
                v.b(e2);
                str4 = null;
            }
            if (!TextUtils.isEmpty(str4)) {
                stringBuffer.append("&p=");
                stringBuffer.append(str4);
            }
        }
        StringBuffer stringBuffer2 = new StringBuffer();
        for (int i2 = 0; i2 < a2.length; i2++) {
            String encodeUrlByQuery = encodeUrlByQuery(a2[i2]);
            StringBuffer stringBuffer3 = new StringBuffer();
            if (!TextUtils.isEmpty(encodeUrlByQuery)) {
                stringBuffer3.append("&u");
                stringBuffer3.append(i2);
                stringBuffer3.append("=");
                stringBuffer3.append(encodeUrlByQuery);
            }
            if (stringBuffer.length() + stringBuffer2.length() + stringBuffer3.length() + 32 > 3096) {
                break;
            }
            stringBuffer2.append(stringBuffer3);
        }
        if (stringBuffer2.length() <= 0) {
            return null;
        }
        stringBuffer.append(stringBuffer2);
        return stringBuffer.toString();
    }

    private ArrayList<TTVideoEngine> a(String str) {
        ArrayList<TTVideoEngine> arrayList = null;
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            this.f4567h.lock();
            ArrayList<TTVideoEngine> arrayList2 = this.f4566g.get(str);
            if (arrayList2 != null) {
                arrayList = new ArrayList<>(arrayList2);
                TTVideoEngineLog.i("DataLoaderHelper", "get engine ref,rawKey = " + str + " engine ref count: " + arrayList.size());
            }
            return arrayList;
        } finally {
            this.f4567h.unlock();
        }
    }

    private void a() {
        if (this.f4568i == null) {
            TTVideoEngineLog.e("DataLoaderHelper", "[preload] need load mdl first.");
            return;
        }
        b a2 = this.f4561b.a();
        if (a2 != null && this.f4562c.a(a2)) {
            this.f4561b.c();
            if (a2.f4577c != null) {
                b(a2);
            }
        }
    }

    private void a(b bVar) {
        boolean z2;
        ArrayList<TTVideoEngine> a2;
        ArrayList<b> d2 = this.f4564e.d();
        int i2 = 0;
        while (true) {
            if (i2 >= d2.size()) {
                z2 = true;
                break;
            }
            b bVar2 = d2.get(i2);
            if (!TextUtils.isEmpty(bVar2.f4575a) && !TextUtils.isEmpty(bVar.f4575a) && bVar2.f4575a.equals(bVar.f4575a)) {
                z2 = false;
                break;
            }
            i2++;
        }
        if (!z2 || (a2 = a(bVar.f4575a)) == null || a2.size() == 0) {
            return;
        }
        for (int i3 = 0; i3 < a2.size(); i3++) {
            a2.get(i3).a();
        }
    }

    private void a(b bVar, b.a aVar) {
        if (bVar == null) {
            TTVideoEngineLog.e("DataLoaderHelper", "[preload] start mdl preload task fail. item is null");
            return;
        }
        if (aVar == null) {
            TTVideoEngineLog.e("DataLoaderHelper", "[preload] start mdl preload task fail. track is null");
            notifyPreloadError(bVar.f4577c, -100004);
            return;
        }
        TTVideoEngineLog.i("DataLoaderHelper", "[preload] start mdl preload task.  key = " + aVar.f4580a);
        String str = aVar.f4580a;
        String[] strArr = aVar.f4582c;
        if (strArr != null) {
            String a2 = a(str, bVar.f4575a, aVar.f4585f, 0L, strArr, aVar.f4583d);
            aVar.f4581b = a2;
            if (!TextUtils.isEmpty(a2)) {
                this.f4563d.a(bVar);
                if (aVar.f4585f > 0) {
                    long j2 = aVar.f4584e;
                    if (j2 > 0) {
                        this.f4568i.preloadResource(aVar.f4581b, (int) j2);
                    }
                    this.f4568i.preloadResource(aVar.f4581b, (int) aVar.f4585f, (int) aVar.f4586g);
                } else {
                    this.f4568i.preloadResource(aVar.f4581b, (int) aVar.f4586g);
                }
                TTVideoEngineLog.i("DataLoaderHelper", String.format("[preload] exect preload task ,key is %s; videoId = %s", aVar.f4580a, bVar.f4575a));
                return;
            }
        }
        bVar.a(-100001);
    }

    private void a(AVMDLDataLoaderNotifyInfo aVMDLDataLoaderNotifyInfo, boolean z2, boolean z3) {
        b b2;
        d dVar;
        if (aVMDLDataLoaderNotifyInfo == null || TextUtils.isEmpty(aVMDLDataLoaderNotifyInfo.logInfo)) {
            return;
        }
        String[] split = aVMDLDataLoaderNotifyInfo.logInfo.split(",");
        if (split.length < 4) {
            return;
        }
        boolean z4 = false;
        try {
            long longValue = Long.valueOf(split[0]).longValue();
            long longValue2 = Long.valueOf(split[1]).longValue();
            if (longValue2 <= 0 && !z3) {
                TTVideoEngineLog.i("DataLoaderHelper", "[preload] mediaSize <= 0 return " + aVMDLDataLoaderNotifyInfo.logInfo);
                return;
            }
            String str = split[2];
            String str2 = split[3];
            if (z2 || z3) {
                b2 = this.f4563d.b(str);
                if (b2 == null) {
                    TTVideoEngineLog.i("DataLoaderHelper", "[preload] preload temItem == null return " + aVMDLDataLoaderNotifyInfo.logInfo);
                    return;
                }
                dVar = this.f4563d;
                z4 = true;
            } else {
                b2 = this.f4564e.b(str);
                if (b2 == null) {
                    TTVideoEngineLog.i("DataLoaderHelper", "[preload] play temItem == null return " + aVMDLDataLoaderNotifyInfo.logInfo);
                    return;
                }
                dVar = this.f4564e;
            }
            b.a b3 = b2.b(str);
            if (b3 != null) {
                b3.f4587h = longValue2;
                b3.f4588i = longValue;
                b3.f4583d = str2;
            }
            String str3 = TextUtils.isEmpty(b2.f4575a) ? str : b2.f4575a;
            c loadProgressByRawKey = getLoadProgressByRawKey(str3);
            if (loadProgressByRawKey == null) {
                loadProgressByRawKey = new c(this);
            }
            loadProgressByRawKey.f4589a = b2.f4575a;
            loadProgressByRawKey.f4590b = z2 ? 2 : 1;
            loadProgressByRawKey.a(b2);
            putByRawKey(str3, loadProgressByRawKey);
            if (loadProgressByRawKey.b() || z3) {
                if (!z4) {
                    dVar.c(str);
                }
                removeLoadProgressByRawKey(str3);
                if (!z2) {
                    a(b2);
                }
                if (b2.a() == null || !z3) {
                    return;
                }
                f4559s++;
                dVar.c(str);
                DataLoaderTaskProgressInfo dataLoaderTaskProgressInfo = new DataLoaderTaskProgressInfo();
                dataLoaderTaskProgressInfo.mCacheSizeFromZero = longValue;
                dataLoaderTaskProgressInfo.mMediaSize = longValue2;
                dataLoaderTaskProgressInfo.mKey = str;
                dataLoaderTaskProgressInfo.mLocalFilePath = str2;
                dataLoaderTaskProgressInfo.mVideoId = b2.f4575a;
                dataLoaderTaskProgressInfo.mTaskType = loadProgressByRawKey.f4590b;
                PreLoaderItemCallBackInfo preLoaderItemCallBackInfo = new PreLoaderItemCallBackInfo(2);
                preLoaderItemCallBackInfo.preloadDataInfo = dataLoaderTaskProgressInfo;
                preLoaderItemCallBackInfo.loadProgress = loadProgressByRawKey;
                b2.a().preloadItemInfo(preLoaderItemCallBackInfo);
                TTVideoEngineLog.i("DataLoaderHelper", "[preload] notify end cache size = " + loadProgressByRawKey.a());
            }
        } catch (Exception unused) {
        }
    }

    private void a(String str, String str2, long j2, PreloaderURLItem preloaderURLItem) {
        if (TextUtils.isEmpty(str)) {
            TTVideoEngineLog.e("DataLoaderHelper", "[preload] key invalid.");
            notifyPreloadError(preloaderURLItem, -100001);
            return;
        }
        if (this.f4568i == null || this.f4560a != 0) {
            TTVideoEngineLog.e("DataLoaderHelper", "[preload] need load mdl first.");
            notifyPreloadError(preloaderURLItem, -100002);
        } else {
            if (this.f4562c.a(str) || this.f4561b.a(str)) {
                notifyPreloadError(preloaderURLItem, -100003);
                return;
            }
            b bVar = new b(this);
            bVar.a(str, j2);
            bVar.f4575a = str2;
            bVar.f4577c = preloaderURLItem;
            this.f4561b.a(bVar);
            a();
        }
    }

    private boolean a(d dVar) {
        boolean z2 = false;
        if (dVar.b() > 0) {
            while (true) {
                b c2 = dVar.c();
                if (c2 == null) {
                    break;
                }
                z2 = true;
                c(c2);
            }
        }
        return z2;
    }

    private String[] a(String[] strArr) {
        if (strArr == null || strArr.length <= 1) {
            return strArr;
        }
        ArrayList arrayList = new ArrayList();
        arrayList.add(strArr[0]);
        for (int i2 = 1; i2 < strArr.length; i2++) {
            String str = strArr[i2];
            Boolean bool = Boolean.TRUE;
            int size = arrayList.size() - 1;
            while (true) {
                if (size < 0) {
                    break;
                }
                if (((String) arrayList.get(size)).equals(str)) {
                    bool = Boolean.FALSE;
                    break;
                }
                size--;
            }
            if (bool.booleanValue()) {
                arrayList.add(str);
            }
        }
        String[] strArr2 = new String[arrayList.size()];
        arrayList.toArray(strArr2);
        return strArr2;
    }

    private DataLoaderCacheInfo b(String str) {
        DataLoaderCacheInfo dataLoaderCacheInfo = null;
        if (str == null) {
            return null;
        }
        String[] split = str.split(",");
        if (split.length >= 4) {
            dataLoaderCacheInfo = new DataLoaderCacheInfo(this);
            dataLoaderCacheInfo.mLocalFilePath = split[3];
            if (!TextUtils.isEmpty(split[0])) {
                dataLoaderCacheInfo.mCacheSizeFromZero = Long.parseLong(split[0]);
            }
            if (!TextUtils.isEmpty(split[1])) {
                dataLoaderCacheInfo.mMediaSize = Long.parseLong(split[1]);
            }
        }
        return dataLoaderCacheInfo;
    }

    private void b(b bVar) {
        if (bVar == null) {
            a();
            return;
        }
        if (this.f4568i == null) {
            TTVideoEngineLog.e("DataLoaderHelper", "[preload] need load mdl first.");
            return;
        }
        this.f4562c.b(bVar);
        long j2 = bVar.f4576b;
        PreloaderURLItem preloaderURLItem = bVar.f4577c;
        if (preloaderURLItem != null && preloaderURLItem.getUrls() != null && bVar.f4577c.getUrls().length > 0) {
            b.a aVar = bVar.f4579e.get(0);
            aVar.f4582c = bVar.f4577c.getUrls();
            aVar.f4586g = bVar.f4576b;
            aVar.f4583d = bVar.f4577c.getFilePath();
            a(bVar, aVar);
        }
        if (bVar.a() != null) {
            PreLoaderItemCallBackInfo preLoaderItemCallBackInfo = new PreLoaderItemCallBackInfo(6);
            preLoaderItemCallBackInfo.preloadType = 0;
            preLoaderItemCallBackInfo.videoPreloadSize = j2;
            preLoaderItemCallBackInfo.audioPreloadSize = 0L;
            bVar.a().preloadItemInfo(preLoaderItemCallBackInfo);
            TTVideoEngineLog.i("DataLoaderHelper", "[preload] notify info. type = 0");
        }
        a();
    }

    public static int c() {
        int i2 = f4559s;
        f4559s = i2 + 1;
        return i2;
    }

    private void c(b bVar) {
        if (bVar == null || bVar.f4578d) {
            return;
        }
        bVar.f4578d = true;
        if (bVar.a() != null) {
            f4559s++;
            bVar.a().preloadItemInfo(new PreLoaderItemCallBackInfo(5));
        }
    }

    private boolean c(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        String lowerCase = str.toLowerCase();
        return (lowerCase.startsWith("http://127.0.0.1") || lowerCase.startsWith("http://localhost") || lowerCase.startsWith("mdl://") || lowerCase.startsWith("file://") || lowerCase.startsWith("/") || lowerCase.endsWith(".mpd") || lowerCase.contains(".mpd?")) ? false : true;
    }

    private boolean d() {
        if (this.f4568i != null) {
            return true;
        }
        if (AVMDLDataLoader.init(false, false) != 0) {
            TTVideoEngineLog.e("DataLoaderHelper", "library has not been loaded");
            return false;
        }
        try {
            AVMDLDataLoader aVMDLDataLoader = AVMDLDataLoader.getInstance();
            this.f4568i = aVMDLDataLoader;
            aVMDLDataLoader.setConfigure(this.f4569j);
            this.f4568i.setListener(this);
            this.f4568i.setStartCompleteListener(this);
            return true;
        } catch (Exception unused) {
            return false;
        }
    }

    public static String encodeUrl(String str) {
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        try {
            return URLEncoder.encode(str, Key.STRING_CHARSET_NAME);
        } catch (UnsupportedEncodingException unused) {
            return null;
        }
    }

    public static String encodeUrlByQuery(String str) {
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        try {
            String queryComponentEncode = AVMDLDataLoader.queryComponentEncode(str);
            return TextUtils.isEmpty(queryComponentEncode) ? URLEncoder.encode(str, Key.STRING_CHARSET_NAME) : queryComponentEncode;
        } catch (UnsupportedEncodingException unused) {
            return null;
        }
    }

    public static DataLoaderHelper getDataLoader() {
        return f4556p;
    }

    public void _addEngine(TTVideoEngine tTVideoEngine, String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        try {
            this.f4567h.lock();
            ArrayList<TTVideoEngine> arrayList = this.f4566g.get(str);
            if (arrayList == null) {
                arrayList = new ArrayList<>();
                this.f4566g.put(str, arrayList);
            }
            if (!arrayList.contains(tTVideoEngine)) {
                arrayList.add(tTVideoEngine);
                TTVideoEngineLog.i("DataLoaderHelper", "add engine ref,rawKey = " + str + " engine ref count: " + arrayList.size());
            }
        } finally {
            this.f4567h.unlock();
        }
    }

    public String _proxyUrl(String str, String str2, long j2, String[] strArr, String str3) {
        if (this.f4560a != 0 || TextUtils.isEmpty(str) || strArr == null || strArr.length <= 0) {
            return null;
        }
        String[] strArr2 = (String[]) strArr.clone();
        for (String str4 : strArr2) {
            if (!c(str4)) {
                return str4;
            }
        }
        if (this.f4568i == null) {
            TTVideoEngineLog.e("DataLoaderHelper", "start mdl first");
            return null;
        }
        this.f4574o.lock();
        try {
            String localAddr = this.f4568i.getLocalAddr();
            if (TextUtils.isEmpty(localAddr)) {
                TTVideoEngineLog.e("DataLoaderHelper", "local host error");
                return null;
            }
            String a2 = a(str, str2, 0L, j2, strArr2, str3);
            if (TextUtils.isEmpty(a2)) {
                return null;
            }
            StringBuffer stringBuffer = new StringBuffer();
            int i2 = this.f4573n;
            this.f4573n = i2 + 1;
            StringBuffer stringBuffer2 = new StringBuffer();
            TTVideoEngineLog.i("DataLoaderHelper", "proxy url, mInvalidMdlProcotol: " + this.f4571l);
            if (this.f4571l || !this.f4572m) {
                stringBuffer.append(MediaLoaderWrapper.HTTP_PROTO_PREFIX);
                stringBuffer.append(localAddr);
                stringBuffer.append("/");
                stringBuffer.append(i2);
            } else {
                stringBuffer.append("mdl://id");
                stringBuffer.append(i2);
                stringBuffer.append("/");
            }
            stringBuffer.append(a2);
            stringBuffer2.append(a2);
            String auth = this.f4568i.getAuth(a2);
            if (!TextUtils.isEmpty(auth)) {
                stringBuffer.append("&ah=");
                stringBuffer.append(auth);
                stringBuffer.append("&ah=");
                stringBuffer.append(auth);
            }
            String stringBuffer3 = stringBuffer.toString();
            TTVideoEngineLog.i("DataLoaderHelper", "_proxyUrl: ".concat(String.valueOf(stringBuffer3)));
            this.f4574o.unlock();
            b d2 = this.f4564e.d(str2);
            if (d2 == null) {
                d2 = new b(this);
            }
            b.a a3 = d2.a(str);
            if (a3 != null) {
                a3.f4580a = str;
                d2.f4575a = str2;
                a3.f4581b = stringBuffer3;
                a3.f4582c = strArr2;
                a3.f4583d = str3;
                this.f4564e.a(d2);
            }
            return stringBuffer3;
        } finally {
            this.f4574o.unlock();
        }
    }

    public void _removeEngine(TTVideoEngine tTVideoEngine, String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        try {
            this.f4567h.lock();
            ArrayList<TTVideoEngine> arrayList = this.f4566g.get(str);
            if (arrayList != null) {
                arrayList.remove(tTVideoEngine);
                TTVideoEngineLog.i("DataLoaderHelper", "remove engine ref,rawKey = " + str + " engine ref count: " + arrayList.size());
                if (arrayList.size() == 0) {
                    this.f4566g.remove(str);
                }
            }
        } finally {
            this.f4567h.unlock();
        }
    }

    public void _removePlayTask(String str) {
        this.f4564e.c(str);
    }

    public void addTask(PreloaderURLItem preloaderURLItem) {
        f4557q++;
        if (preloaderURLItem == null) {
            TTVideoEngineLog.e("DataLoaderHelper", "[preload] url item invalid");
            return;
        }
        this.f4574o.lock();
        try {
            a(preloaderURLItem.getKey(), preloaderURLItem.getVideoId(), preloaderURLItem.getPreloadSize(), preloaderURLItem);
        } finally {
            this.f4574o.unlock();
        }
    }

    public void cancelAllTasks() {
        if (this.f4568i == null || this.f4560a != 0) {
            TTVideoEngineLog.e("DataLoaderHelper", "need start mdl first");
            return;
        }
        this.f4574o.lock();
        try {
            cancelAllTasksInternal();
            this.f4574o.unlock();
        } catch (Throwable unused) {
            this.f4574o.unlock();
        }
    }

    public void cancelAllTasksInternal() {
        a(this.f4562c);
        a(this.f4561b);
        if (a(this.f4563d)) {
            this.f4568i.cancelAll();
        }
    }

    public void cancelTask(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        if (this.f4568i == null || this.f4560a != 0) {
            TTVideoEngineLog.e("DataLoaderHelper", "need start mdl first");
            return;
        }
        this.f4574o.lock();
        try {
            b c2 = this.f4561b.c(str);
            if (c2 == null && (c2 = this.f4562c.c(str)) == null) {
                c2 = this.f4563d.c(str);
                if (c2 != null) {
                    this.f4568i.cancel(str);
                }
                TTVideoEngineLog.i("DataLoaderHelper", "[preload] cancel preload task. key = ".concat(String.valueOf(str)));
                this.f4574o.unlock();
            }
            c(c2);
            TTVideoEngineLog.i("DataLoaderHelper", "[preload] cancel preload task. key = ".concat(String.valueOf(str)));
            this.f4574o.unlock();
        } catch (Throwable unused) {
            this.f4574o.unlock();
        }
    }

    public void clearAllCaches() {
        if (this.f4560a != 0) {
            return;
        }
        this.f4574o.lock();
        try {
            AVMDLDataLoader aVMDLDataLoader = this.f4568i;
            if (aVMDLDataLoader == null) {
                TTVideoEngineLog.e("DataLoaderHelper", "mInnerDataLoader == null");
            } else {
                aVMDLDataLoader.clearAllCaches();
            }
        } finally {
            try {
            } finally {
            }
        }
    }

    public void close() {
        this.f4574o.lock();
        try {
            if (this.f4560a == 1) {
                TTVideoEngineLog.e("DataLoaderHelper", "DataLoader not started, not need close");
            } else {
                this.f4568i.close();
                cancelAllTasksInternal();
                this.f4560a = 1;
            }
        } finally {
            this.f4574o.unlock();
        }
    }

    public DataLoaderCacheInfo getCacheInfoByFilePath(String str, String str2) {
        this.f4574o.lock();
        try {
            AVMDLDataLoader aVMDLDataLoader = this.f4568i;
            if (aVMDLDataLoader != null) {
                DataLoaderCacheInfo b2 = b(aVMDLDataLoader.getStringCacheInfo(str, str2));
                this.f4574o.unlock();
                return b2;
            }
        } catch (Throwable unused) {
        }
        this.f4574o.unlock();
        return null;
    }

    public long getCacheSize(String str) {
        long j2 = 0;
        if (this.f4560a != 0) {
            return 0L;
        }
        this.f4574o.lock();
        try {
            AVMDLDataLoader aVMDLDataLoader = this.f4568i;
            if (aVMDLDataLoader != null) {
                long cacheSize = aVMDLDataLoader.getCacheSize(str);
                if (cacheSize > 0) {
                    j2 = cacheSize;
                }
            }
        } finally {
            try {
                return j2;
            } finally {
            }
        }
        return j2;
    }

    public long getCacheSizeByFilePath(String str, String str2) {
        this.f4574o.lock();
        long j2 = 0;
        try {
            DataLoaderCacheInfo cacheInfoByFilePath = getCacheInfoByFilePath(str, str2);
            if (cacheInfoByFilePath != null) {
                j2 = cacheInfoByFilePath.mCacheSizeFromZero;
            }
        } catch (Throwable unused) {
        }
        this.f4574o.unlock();
        return j2;
    }

    @Override
    public String getCheckSumInfo(String str) {
        return null;
    }

    @Override
    public long getInt64Value(int i2, long j2) {
        return 0L;
    }

    public c getLoadProgressByRawKey(String str) {
        if (!TextUtils.isEmpty(str)) {
            return this.f4565f.get(str);
        }
        TTVideoEngineLog.e("DataLoaderHelper", "[preload] get, param is invalid key is null");
        return null;
    }

    @Override
    public String getStringValue(int i2, long j2, String str) {
        return null;
    }

    public void notifyPreloadError(PreloaderURLItem preloaderURLItem, int i2) {
        IPreLoaderItemCallBackListener callBackListener = preloaderURLItem.getCallBackListener();
        if (callBackListener != null) {
            f4559s++;
            PreLoaderItemCallBackInfo preLoaderItemCallBackInfo = new PreLoaderItemCallBackInfo(3);
            preLoaderItemCallBackInfo.preloadError = new Error(Error.DataLoaderPreload, i2);
            callBackListener.preloadItemInfo(preLoaderItemCallBackInfo);
        }
    }

    @Override
    public void onNotify(AVMDLDataLoaderNotifyInfo aVMDLDataLoaderNotifyInfo) {
        String str;
        b b2;
        if (aVMDLDataLoaderNotifyInfo == null) {
            return;
        }
        int i2 = aVMDLDataLoaderNotifyInfo.what;
        if (i2 != 0 && i2 != 15) {
            if (i2 != 20) {
                if (i2 == 3 || i2 == 4) {
                    if (i2 == 4) {
                        f4558r++;
                    }
                    if (aVMDLDataLoaderNotifyInfo.logInfo != null) {
                        long j2 = aVMDLDataLoaderNotifyInfo.parameter;
                        if (j2 != 3) {
                            a(aVMDLDataLoaderNotifyInfo, j2 == 2, i2 == 4);
                            return;
                        }
                        return;
                    }
                    return;
                }
                if (i2 == 7) {
                    f4558r++;
                    if (TextUtils.isEmpty(aVMDLDataLoaderNotifyInfo.logInfo)) {
                        return;
                    }
                    String[] split = aVMDLDataLoaderNotifyInfo.logInfo.split(",");
                    if (split.length < 4) {
                        return;
                    }
                    String str2 = split[2];
                    b b3 = this.f4563d.b(str2);
                    this.f4563d.c(str2);
                    c(b3);
                    return;
                }
                if (i2 != 8) {
                    if (i2 != 10 && i2 != 11) {
                        return;
                    }
                }
            }
            if (i2 == 8) {
                f4558r++;
            }
            String str3 = aVMDLDataLoaderNotifyInfo.logInfo;
            if ((str3 == null || aVMDLDataLoaderNotifyInfo.parameter != 3) && str3 != null) {
                TTVideoEngineLog.i("DataLoaderHelper", " task fail log = " + aVMDLDataLoaderNotifyInfo.logInfo + ", code = " + aVMDLDataLoaderNotifyInfo.code + ", task type = " + aVMDLDataLoaderNotifyInfo.parameter);
                String[] split2 = aVMDLDataLoaderNotifyInfo.logInfo.split(",");
                if (split2.length > 0 && (b2 = this.f4563d.b((str = split2[0]))) != null) {
                    d dVar = this.f4563d;
                    Error error = new Error(Error.DataLoaderPreload, (int) aVMDLDataLoaderNotifyInfo.code, aVMDLDataLoaderNotifyInfo.logInfo);
                    String str4 = TextUtils.isEmpty(b2.f4575a) ? str : b2.f4575a;
                    c loadProgressByRawKey = getLoadProgressByRawKey(str4);
                    if (loadProgressByRawKey == null) {
                        loadProgressByRawKey = new c(this);
                    }
                    loadProgressByRawKey.f4589a = b2.f4575a;
                    loadProgressByRawKey.f4590b = (int) aVMDLDataLoaderNotifyInfo.parameter;
                    loadProgressByRawKey.a(b2);
                    loadProgressByRawKey.a(str, error);
                    putByRawKey(str4, loadProgressByRawKey);
                    if (!loadProgressByRawKey.b() && !loadProgressByRawKey.c()) {
                        loadProgressByRawKey.b();
                        loadProgressByRawKey.c();
                        return;
                    }
                    dVar.c(str);
                    TTVideoEngineLog.i("DataLoaderHelper", "pop all task item. videoId = " + b2.f4575a + " key = " + str);
                    removeLoadProgressByRawKey(str4);
                    if (b2.a() == null || aVMDLDataLoaderNotifyInfo.parameter != 2) {
                        return;
                    }
                    PreLoaderItemCallBackInfo preLoaderItemCallBackInfo = new PreLoaderItemCallBackInfo(3);
                    preLoaderItemCallBackInfo.preloadError = error;
                    preLoaderItemCallBackInfo.loadProgress = loadProgressByRawKey;
                    preLoaderItemCallBackInfo.preloadDataInfo = null;
                    f4559s++;
                    b2.a().preloadItemInfo(preLoaderItemCallBackInfo);
                    return;
                }
                return;
            }
            return;
        }
        VideoEventEngineUploader videoEventEngineUploader = this.f4570k;
        if (videoEventEngineUploader != null) {
            videoEventEngineUploader.onEvent(aVMDLDataLoaderNotifyInfo.logType, aVMDLDataLoaderNotifyInfo.jsonLog);
        }
    }

    @Override
    public void onStartComplete() {
    }

    public void putByRawKey(String str, c cVar) {
        if (TextUtils.isEmpty(str) || cVar == null) {
            TTVideoEngineLog.e("DataLoaderHelper", "[preload] param is invalid");
        } else {
            this.f4565f.put(str, cVar);
        }
    }

    public void removeCacheFile(String str, boolean z2) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        this.f4574o.lock();
        try {
            if (this.f4568i == null) {
                TTVideoEngineLog.e("DataLoaderHelper", "mInnerDataLoader == null");
            } else {
                TTVideoEngineLog.i("DataLoaderHelper", "remove mdl file. key ".concat(String.valueOf(str)));
                if (z2) {
                    this.f4568i.forceRemoveFileCache(str);
                } else {
                    this.f4568i.removeFileCache(str);
                }
            }
        } catch (Throwable unused) {
        }
        this.f4574o.unlock();
    }

    public void removeLoadProgressByRawKey(String str) {
        if (TextUtils.isEmpty(str)) {
            TTVideoEngineLog.e("DataLoaderHelper", "[preload] remove, param is invalid key is null");
        } else {
            this.f4565f.remove(str);
        }
    }

    public void setCacheInfoLists(String[] strArr, long[] jArr) {
        Arrays.toString(strArr);
        Arrays.toString(jArr);
        this.f4574o.lock();
        try {
            this.f4569j.setCacheInfoList(strArr, jArr);
        } finally {
            this.f4574o.unlock();
        }
    }

    public void setContext(Context context) {
        this.f4574o.lock();
        this.f4574o.unlock();
    }

    public synchronized void setEngineUploader(VideoEventEngineUploader videoEventEngineUploader) {
        this.f4570k = videoEventEngineUploader;
    }

    public void setIntValue(int i2, int i3) {
        this.f4574o.lock();
        try {
            try {
            } catch (Exception e2) {
                v.b(e2);
            }
            if (i2 == 11) {
                this.f4569j.mPreloadParallelNum = i3;
                AVMDLDataLoader aVMDLDataLoader = this.f4568i;
                if (aVMDLDataLoader != null) {
                    aVMDLDataLoader.setIntValue(102, i3);
                }
            } else if (i2 != 512) {
                boolean z2 = true;
                if (i2 == 1005) {
                    if (i3 != 1) {
                        z2 = false;
                    }
                    this.f4572m = z2;
                    TTVideoEngineLog.i("DataLoaderHelper", "config DATALOADER_KEY_SET_MDL_PROTOCOL_ENABLE: " + this.f4572m);
                } else if (i2 == 1) {
                    this.f4569j.mMaxCacheSize = i3;
                } else if (i2 == 2) {
                    this.f4569j.mRWTimeOut = i3;
                } else {
                    if (i2 != 3) {
                        if (i2 == 4) {
                            this.f4569j.mTryCount = i3;
                        }
                    }
                    this.f4569j.mOpenTimeOut = i3;
                }
            } else {
                this.f4569j.mAlogEnable = i3;
                AVMDLDataLoader aVMDLDataLoader2 = this.f4568i;
                if (aVMDLDataLoader2 != null) {
                    aVMDLDataLoader2.setIntValue(AVMDLDataLoader.KeyIsAlogEnable, i3);
                }
            }
        } finally {
            this.f4574o.unlock();
        }
    }

    public void setStringValue(int i2, String str) {
        this.f4574o.lock();
        if (i2 == 0) {
            try {
                this.f4569j.mCacheDir = str;
            } catch (Exception unused) {
            } catch (Throwable th) {
                this.f4574o.unlock();
                throw th;
            }
        }
        this.f4574o.unlock();
    }

    public void start() {
        this.f4574o.lock();
        try {
            if (this.f4560a == 0) {
                TTVideoEngineLog.e("DataLoaderHelper", "DataLoader has started not need start");
            } else {
                if (!d()) {
                    throw new Exception("init data loader fail");
                }
                this.f4562c.a(4L);
                if (this.f4569j == null) {
                    this.f4569j = AVMDLDataLoaderConfigure.getDefaultonfigure();
                }
                if (this.f4570k != null) {
                    this.f4569j.mEnableReportTaskLog = 1;
                }
                this.f4568i.setConfigure(this.f4569j);
                if (this.f4568i.start() < 0) {
                    throw new Exception("start data loader fail");
                }
                if (this.f4568i.getLongValue(AVMDLDataLoader.KeyIsGetMdlProtocolHandle) == -1) {
                    this.f4571l = true;
                }
                this.f4560a = 0;
            }
        } finally {
            this.f4574o.unlock();
        }
    }
}