package a4;

import android.content.Context;
import android.util.TypedValue;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media3.common.MimeTypes;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;

/* JADX INFO: loaded from: classes.dex */
public final class o {

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

    public o(Context context) {
        this.f347a = context;
    }

    public static String a(File file) throws IOException {
        String canonicalPath = file.getCanonicalPath();
        return !canonicalPath.endsWith("/") ? canonicalPath.concat("/") : canonicalPath;
    }

    public static String b(String str) {
        String str2 = null;
        if (str != null) {
            String strGuessContentTypeFromName = URLConnection.guessContentTypeFromName(str);
            if (strGuessContentTypeFromName == null) {
                int iLastIndexOf = str.lastIndexOf(46);
                if (iLastIndexOf != -1) {
                    String lowerCase = str.substring(iLastIndexOf + 1).toLowerCase();
                    lowerCase.getClass();
                    switch (lowerCase) {
                        case "gz":
                        case "tgz":
                            str2 = "application/gzip";
                            break;
                        case "js":
                        case "mjs":
                            str2 = "text/javascript";
                            break;
                        case "bmp":
                            str2 = MimeTypes.IMAGE_BMP;
                            break;
                        case "css":
                            str2 = "text/css";
                            break;
                        case "gif":
                            str2 = "image/gif";
                            break;
                        case "htm":
                        case "html":
                        case "shtm":
                        case "ehtml":
                        case "shtml":
                            str2 = "text/html";
                            break;
                        case "ico":
                            str2 = "image/x-icon";
                            break;
                        case "jpg":
                        case "pjp":
                        case "jfif":
                        case "jpeg":
                        case "pjpeg":
                            str2 = MimeTypes.IMAGE_JPEG;
                            break;
                        case "m4a":
                            str2 = "audio/x-m4a";
                            break;
                        case "m4v":
                        case "mp4":
                            str2 = MimeTypes.VIDEO_MP4;
                            break;
                        case "mht":
                        case "mhtml":
                            str2 = "multipart/related";
                            break;
                        case "mp3":
                            str2 = MimeTypes.AUDIO_MPEG;
                            break;
                        case "mpg":
                        case "mpeg":
                            str2 = MimeTypes.VIDEO_MPEG;
                            break;
                        case "oga":
                        case "ogg":
                        case "opus":
                            str2 = MimeTypes.AUDIO_OGG;
                            break;
                        case "ogm":
                        case "ogv":
                            str2 = MimeTypes.VIDEO_OGG;
                            break;
                        case "pdf":
                            str2 = "application/pdf";
                            break;
                        case "png":
                            str2 = MimeTypes.IMAGE_PNG;
                            break;
                        case "svg":
                        case "svgz":
                            str2 = "image/svg+xml";
                            break;
                        case "tif":
                        case "tiff":
                            str2 = "image/tiff";
                            break;
                        case "wav":
                            str2 = MimeTypes.AUDIO_WAV;
                            break;
                        case "xht":
                        case "xhtm":
                        case "xhtml":
                            str2 = "application/xhtml+xml";
                            break;
                        case "xml":
                            str2 = "text/xml";
                            break;
                        case "zip":
                            str2 = "application/zip";
                            break;
                        case "apng":
                            str2 = "image/apng";
                            break;
                        case "flac":
                            str2 = MimeTypes.AUDIO_FLAC;
                            break;
                        case "json":
                            str2 = "application/json";
                            break;
                        case "wasm":
                            str2 = "application/wasm";
                            break;
                        case "webm":
                            str2 = MimeTypes.VIDEO_WEBM;
                            break;
                        case "webp":
                            str2 = MimeTypes.IMAGE_WEBP;
                            break;
                        case "woff":
                            str2 = "application/font-woff";
                            break;
                    }
                }
            } else {
                str2 = strGuessContentTypeFromName;
            }
        }
        return str2 == null ? "text/plain" : str2;
    }

    @Nullable
    public static File getCanonicalFileIfChild(@NonNull File file, @NonNull String str) throws IOException {
        String strA = a(file);
        String canonicalPath = new File(file, str).getCanonicalPath();
        if (canonicalPath.startsWith(strA)) {
            return new File(canonicalPath);
        }
        return null;
    }

    public final InputStream c(String str) throws IOException {
        if (str.length() > 1 && str.charAt(0) == '/') {
            str = str.substring(1);
        }
        String[] strArrSplit = str.split("/", -1);
        if (strArrSplit.length != 2) {
            throw new IllegalArgumentException("Incorrect resource path: ".concat(str));
        }
        String str2 = strArrSplit[0];
        String strSubstring = strArrSplit[1];
        int iLastIndexOf = strSubstring.lastIndexOf(46);
        if (iLastIndexOf != -1) {
            strSubstring = strSubstring.substring(0, iLastIndexOf);
        }
        Context context = this.f347a;
        int identifier = context.getResources().getIdentifier(strSubstring, str2, context.getPackageName());
        TypedValue typedValue = new TypedValue();
        context.getResources().getValue(identifier, typedValue, true);
        int i10 = typedValue.type;
        if (i10 != 3) {
            throw new IOException(String.format("Expected %s resource to be of TYPE_STRING but was %d", str, Integer.valueOf(i10)));
        }
        InputStream inputStreamOpenRawResource = context.getResources().openRawResource(identifier);
        return str.endsWith(".svgz") ? new GZIPInputStream(inputStreamOpenRawResource) : inputStreamOpenRawResource;
    }
}
