package com.lynx.tasm.behavior.ui.canvas;

import X.Ke8;
import X.Ke9;
import X.KeA;
import X.KeK;
import android.content.ContextWrapper;
import android.net.Uri;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.he.lynx.loader.Loader;
import com.he.lynx.loader.Resolver;
import com.lynx.tasm.base.LLog;
import com.lynx.tasm.core.JSProxy;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class LynxHeliumResourceLoader implements Loader {
    public ContextWrapper mContext;
    public LynxHelium$PermissionHandler mPermissionHandler;

    static {
        Covode.recordClassIndex(46676);
    }

    public byte[] loadSync(String str) {
        return realLoad(str, (Resolver) null);
    }

    public static byte[] mergeArray(byte[]... bArr) {
        int i = 0;
        for (byte[] length : bArr) {
            i += length.length;
        }
        byte[] bArr2 = new byte[i];
        int i2 = 0;
        for (int i3 = 0; i3 < bArr.length; i3++) {
            int length2 = bArr[i3].length;
            if (length2 != 0) {
                System.arraycopy(bArr[i3], 0, bArr2, i2, length2);
                i2 += length2;
            }
        }
        return bArr2;
    }

    private String redirectUrl(String str) {
        String str2 = null;
        if (str == null) {
            return null;
        }
        LLog.LIZ(4, "HeliumResLoader", "redirectUrl from: ".concat(String.valueOf(str)));
        int indexOf = str.indexOf("?_lynxId=");
        if (indexOf > 0) {
            str2 = str.substring(str.lastIndexOf("?_lynxId=") + 9);
            str = str.substring(0, indexOf);
        }
        if (str2 != null) {
            try {
                return JSProxy.LIZ(Long.parseLong(str2), str);
            } catch (Exception e) {
                LLog.LIZLLL("redirectUrl", "exception: " + e.toString());
            }
        }
        LLog.LIZIZ("HeliumResLoader", "redirectUrl to: ".concat(String.valueOf(str)));
        return str;
    }

    public Uri loadMedia(String str) {
        String redirectUrl = redirectUrl(str);
        LLog.LIZ(4, "HeliumResLoader", "loadMedia ".concat(String.valueOf(redirectUrl)));
        if (redirectUrl.startsWith("http://") || redirectUrl.startsWith("https://")) {
            return Uri.parse(redirectUrl);
        }
        if (redirectUrl.startsWith("file://")) {
            if (!redirectUrl.startsWith("file:///")) {
                redirectUrl = "file:///" + redirectUrl.substring(7);
            }
            LynxHelium$PermissionHandler lynxHelium$PermissionHandler = this.mPermissionHandler;
            if (lynxHelium$PermissionHandler != null) {
                String[] strArr = {"android.permission.READ_EXTERNAL_STORAGE"};
                if (!lynxHelium$PermissionHandler.verifyHasPermission(strArr)) {
                    this.mPermissionHandler.requestPermissions(strArr, new LynxHelium$OnPermissionListener() {
                        static {
                            Covode.recordClassIndex(46680);
                        }

                        public void onPermissionDenied() {
                        }

                        public void onPermissionGranted() {
                        }
                    });
                }
            }
            return Uri.parse(redirectUrl);
        }
        LLog.LIZLLL("HeliumResLoader", "loadMedia do not support:".concat(String.valueOf(redirectUrl)));
        return Uri.parse(redirectUrl);
    }

    public LynxHeliumResourceLoader(ContextWrapper contextWrapper, LynxHelium$PermissionHandler lynxHelium$PermissionHandler) {
        this.mContext = contextWrapper;
        this.mPermissionHandler = lynxHelium$PermissionHandler;
    }

    public void load(final String str, final Resolver resolver) {
        new Thread(new Runnable() {
            static {
                Covode.recordClassIndex(46677);
            }

            public void run() {
                LynxHeliumResourceLoader.this.realLoad(str, resolver);
            }
        }).start();
    }

    public void loadUrl(final String str, final Resolver resolver) {
        new Thread(new Runnable() {
            static {
                Covode.recordClassIndex(46678);
            }

            public void run() {
                LynxHeliumResourceLoader.this.realLoad(str, resolver);
            }
        }).start();
    }

    private void requestResource(final String str, final Resolver resolver) {
        KeK.LIZ().LIZ(new KeA(str), new Ke8() {
            static {
                Covode.recordClassIndex(46681);
            }

            public void onFailed(Ke9 ke9) {
                String str = ke9.LIZIZ;
                LLog.LIZ(6, "HeliumResLoader", "requestResource error ".concat(String.valueOf(str)));
                resolver.reject(new IOException(str));
            }

            public void onSuccess(Ke9 ke9) {
                LLog.LIZIZ("HeliumResLoader", "requestResource success " + str);
                try {
                    LynxHeliumResourceLoader.this.loadFromStream(ke9.LIZLLL, resolver);
                } catch (Exception e) {
                    ke9.LIZIZ = e.toString();
                }
            }
        });
    }

    public byte[] loadFromStream(InputStream inputStream, Resolver resolver) {
        MethodCollector.i(6230);
        int available = inputStream.available();
        if (available <= 0) {
            available = 1024;
        }
        byte[] bArr = new byte[available];
        byte[] bArr2 = new byte[0];
        while (true) {
            int read = inputStream.read(bArr);
            if (read == -1) {
                break;
            }
            byte[] bArr3 = new byte[read];
            System.arraycopy(bArr, 0, bArr3, 0, read);
            bArr2 = mergeArray(bArr2, bArr3);
        }
        if (resolver != null) {
            resolver.resolve(bArr2, 0, bArr2.length);
        }
        inputStream.close();
        MethodCollector.o(6230);
        return bArr2;
    }

    private byte[] loadFromFile(String str, final Resolver resolver) {
        final File file;
        MethodCollector.i(6234);
        if (str.startsWith("/")) {
            file = new File(str);
        } else {
            file = new File("/".concat(String.valueOf(str)));
        }
        LynxHelium$PermissionHandler lynxHelium$PermissionHandler = this.mPermissionHandler;
        if (lynxHelium$PermissionHandler == null) {
            byte[] loadFromStream = loadFromStream(new FileInputStream(file), resolver);
            MethodCollector.o(6234);
            return loadFromStream;
        }
        String[] strArr = {"android.permission.READ_EXTERNAL_STORAGE"};
        if (lynxHelium$PermissionHandler.verifyHasPermission(strArr)) {
            byte[] loadFromStream2 = loadFromStream(new FileInputStream(file), resolver);
            MethodCollector.o(6234);
            return loadFromStream2;
        }
        this.mPermissionHandler.requestPermissions(strArr, new LynxHelium$OnPermissionListener() {
            static {
                Covode.recordClassIndex(46679);
            }

            public void onPermissionDenied() {
                LLog.LIZ(6, "HeliumResLoader", "Read local file permission denied");
                Resolver resolver = resolver;
                if (resolver != null) {
                    resolver.reject(new IOException("Read local file permission denied"));
                }
            }

            public void onPermissionGranted() {
                MethodCollector.i(6607);
                if (resolver != null) {
                    try {
                        LynxHeliumResourceLoader.this.loadFromStream(new FileInputStream(file), resolver);
                        MethodCollector.o(6607);
                        return;
                    } catch (Exception unused) {
                        resolver.reject(new IOException("loadFromStream error"));
                    }
                }
                MethodCollector.o(6607);
            }
        });
        if (resolver == null) {
            LLog.LIZLLL("HeliumResLoader", "WARNING: PermissionProvider not set!!!");
            byte[] loadFromStream3 = loadFromStream(new FileInputStream(file), (Resolver) null);
            MethodCollector.o(6234);
            return loadFromStream3;
        }
        MethodCollector.o(6234);
        return null;
    }

    /* JADX WARNING: Removed duplicated region for block: B:38:0x0160  */
    /* JADX WARNING: Removed duplicated region for block: B:40:0x0164  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public byte[] realLoad(java.lang.String r9, com.he.lynx.loader.Resolver r10) {
        /*
            r8 = this;
            java.lang.String r1 = "load schema invalid!"
            java.lang.String r2 = "assets:///"
            java.lang.String r4 = "load "
            java.lang.String r3 = "HeliumResLoader"
            r7 = 6227(0x1853, float:8.726E-42)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r7)
            r6 = 0
            java.lang.String r9 = r8.redirectUrl(r9)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            boolean r0 = r9.startsWith(r2)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            if (r0 != 0) goto L_0x0020
            java.lang.String r0 = "asset:///"
            boolean r0 = r9.startsWith(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            if (r0 == 0) goto L_0x0057
        L_0x0020:
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r1.<init>(r4)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r1.append(r9)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = "from assert"
            r1.append(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = r1.toString()     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            com.lynx.tasm.base.LLog.LIZIZ(r3, r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            boolean r0 = r9.startsWith(r2)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            if (r0 == 0) goto L_0x003d
            r0 = 10
            goto L_0x003f
        L_0x003d:
            r0 = 9
        L_0x003f:
            java.lang.String r1 = r9.substring(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            android.content.ContextWrapper r0 = r8.mContext     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            android.content.res.Resources r0 = r0.getResources()     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            android.content.res.AssetManager r0 = r0.getAssets()     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.io.InputStream r0 = r0.open(r1)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            byte[] r0 = r8.loadFromStream(r0, r10)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            goto L_0x015e
        L_0x0057:
            java.lang.String r0 = "res:///"
            boolean r0 = r9.startsWith(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r2 = 7
            if (r0 == 0) goto L_0x009a
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r1.<init>(r4)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r1.append(r9)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = "from resource"
            r1.append(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = r1.toString()     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            com.lynx.tasm.base.LLog.LIZIZ(r3, r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r5 = r9.substring(r2)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            android.content.ContextWrapper r0 = r8.mContext     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            android.content.res.Resources r2 = r0.getResources()     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r1 = "drawable"
            android.content.ContextWrapper r0 = r8.mContext     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = r0.getPackageCodePath()     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            int r1 = r2.getIdentifier(r5, r1, r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            android.content.ContextWrapper r0 = r8.mContext     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            android.content.res.Resources r0 = r0.getResources()     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.io.InputStream r0 = r0.openRawResource(r1)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            byte[] r0 = r8.loadFromStream(r0, r10)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            goto L_0x015e
        L_0x009a:
            java.lang.String r0 = "file://"
            boolean r0 = r9.startsWith(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            if (r0 == 0) goto L_0x00c0
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r1.<init>(r4)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r1.append(r9)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = "from file path"
            r1.append(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = r1.toString()     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            com.lynx.tasm.base.LLog.LIZIZ(r3, r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = r9.substring(r2)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            byte[] r0 = r8.loadFromFile(r0, r10)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            goto L_0x015e
        L_0x00c0:
            java.lang.String r0 = "http://"
            boolean r0 = r9.startsWith(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            if (r0 != 0) goto L_0x00e7
            java.lang.String r0 = "https://"
            boolean r0 = r9.startsWith(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            if (r0 == 0) goto L_0x00d1
            goto L_0x00e7
        L_0x00d1:
            java.lang.String r0 = java.lang.String.valueOf(r9)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = r1.concat(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            com.lynx.tasm.base.LLog.LIZLLL(r3, r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            if (r10 == 0) goto L_0x015d
            java.io.IOException r0 = new java.io.IOException     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r0.<init>(r1)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r10.reject(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            goto L_0x015d
        L_0x00e7:
            if (r10 == 0) goto L_0x0101
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r1.<init>(r4)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r1.append(r9)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = "from web"
            r1.append(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = r1.toString()     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            com.lynx.tasm.base.LLog.LIZIZ(r3, r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r8.requestResource(r9, r10)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            goto L_0x015d
        L_0x0101:
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r1.<init>(r4)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            r1.append(r9)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = "from web sync is invalid"
            r1.append(r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            java.lang.String r0 = r1.toString()     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            com.lynx.tasm.base.LLog.LIZLLL(r3, r0)     // Catch:{ Exception -> 0x0116, OutOfMemoryError -> 0x013a }
            goto L_0x015d
        L_0x0116:
            r2 = move-exception
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            r1.<init>(r4)
            r1.append(r9)
            java.lang.String r0 = "exception"
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            com.lynx.tasm.base.LLog.LIZLLL(r3, r0)
            if (r10 == 0) goto L_0x015d
            java.io.IOException r1 = new java.io.IOException
            java.lang.String r0 = r2.getMessage()
            r1.<init>(r0)
            r10.reject(r1)
            goto L_0x015d
        L_0x013a:
            r2 = move-exception
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            r1.<init>(r4)
            r1.append(r9)
            java.lang.String r0 = " out of memory"
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            com.lynx.tasm.base.LLog.LIZLLL(r3, r0)
            if (r10 == 0) goto L_0x015d
            java.io.IOException r1 = new java.io.IOException
            java.lang.String r0 = r2.getMessage()
            r1.<init>(r0)
            r10.reject(r1)
        L_0x015d:
            r0 = r6
        L_0x015e:
            if (r10 == 0) goto L_0x0164
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r7)
            return r6
        L_0x0164:
            if (r0 == 0) goto L_0x016a
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r7)
            return r0
        L_0x016a:
            r0 = 0
            byte[] r0 = new byte[r0]
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r7)
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.lynx.tasm.behavior.ui.canvas.LynxHeliumResourceLoader.realLoad(java.lang.String, com.he.lynx.loader.Resolver):byte[]");
    }
}
