package z3;

import android.content.Context;
import android.webkit.WebResourceResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

/* JADX INFO: loaded from: classes.dex */
public final class v implements w {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final String[] f37047b = {"app_webview/", "databases/", "lib/", "shared_prefs/", "code_cache/"};

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final File f37048a;

    public v(Context context, File file) {
        try {
            this.f37048a = new File(a4.o.a(file));
            if (a(context)) {
                return;
            }
            throw new IllegalArgumentException("The given directory \"" + file + "\" doesn't exist under an allowed app internal storage directory");
        } catch (IOException e10) {
            throw new IllegalArgumentException("Failed to resolve the canonical path for the given directory: " + file.getPath(), e10);
        }
    }

    public final boolean a(Context context) throws IOException {
        String strA = a4.o.a(this.f37048a);
        String strA2 = a4.o.a(context.getCacheDir());
        String strA3 = a4.o.a(context.getDataDir());
        if ((!strA.startsWith(strA2) && !strA.startsWith(strA3)) || strA.equals(strA2) || strA.equals(strA3)) {
            return false;
        }
        String[] strArr = f37047b;
        for (int i10 = 0; i10 < 5; i10++) {
            if (strA.startsWith(strA3 + strArr[i10])) {
                return false;
            }
        }
        return true;
    }

    @Override // z3.w
    public final WebResourceResponse handle(String str) {
        File canonicalFileIfChild;
        File file = this.f37048a;
        try {
            canonicalFileIfChild = a4.o.getCanonicalFileIfChild(file, str);
        } catch (IOException unused) {
        }
        if (canonicalFileIfChild == null) {
            String.format("The requested file: %s is outside the mounted directory: %s", str, file);
            return new WebResourceResponse(null, null, null);
        }
        InputStream fileInputStream = new FileInputStream(canonicalFileIfChild);
        if (canonicalFileIfChild.getPath().endsWith(".svgz")) {
            fileInputStream = new GZIPInputStream(fileInputStream);
        }
        return new WebResourceResponse(a4.o.b(str), null, fileInputStream);
    }
}
