package s;

import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import b0.o;
import java.io.File;
import java.io.FilenameFilter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/* JADX INFO: renamed from: s.a, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public abstract class AbstractC0708a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final String[] f6374a = {".ass", ".idx", ".sami", ".smi", ".srt", ".ssa", ".sub", ".sup", ".ttml", ".txt", ".webvtt"};

    /* JADX INFO: renamed from: s.a$a, reason: collision with other inner class name */
    class C0093a implements FilenameFilter {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final /* synthetic */ String f6375a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ List f6376b;

        C0093a(String str, List list) {
            this.f6375a = str;
            this.f6376b = list;
        }

        @Override // java.io.FilenameFilter
        public boolean accept(File file, String str) {
            String lowerCase = str.toLowerCase();
            return lowerCase != null && lowerCase.startsWith(this.f6375a) && lowerCase.lastIndexOf(".") >= 0 && this.f6376b.contains(lowerCase.substring(lowerCase.lastIndexOf(".")));
        }
    }

    public static String a(String str) {
        String lowerCase = str.toLowerCase();
        if (lowerCase.endsWith(".smi") || lowerCase.endsWith(".sami")) {
            return "text/ext/smi";
        }
        if (lowerCase.endsWith(".srt")) {
            return "text/ext/srt";
        }
        if (lowerCase.endsWith(".ssa") || lowerCase.endsWith(".ass")) {
            return "text/ext/ssa";
        }
        if (lowerCase.endsWith(".sub")) {
            return "text/ext/sub";
        }
        if (lowerCase.endsWith(".sup")) {
            return "text/ext/pgs";
        }
        if (lowerCase.endsWith(".ttml")) {
            return "application/ttml+xml";
        }
        if (lowerCase.endsWith(".txt")) {
            return "text/ext/txt";
        }
        if (lowerCase.endsWith(".webvtt")) {
            return "text/vtt";
        }
        if (lowerCase.endsWith(".idx")) {
            return "text/ext/vobsub";
        }
        return null;
    }

    public static boolean b(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        return "text/ext".equals(str.substring(0, str.lastIndexOf("/")));
    }

    public static String[] c(Uri uri) {
        String strDecode;
        if ("file".equals(uri.getScheme())) {
            strDecode = uri.getPath();
        } else {
            try {
                strDecode = URLDecoder.decode(uri.toString(), "utf-8");
            } catch (UnsupportedEncodingException e2) {
                e2.printStackTrace();
                strDecode = null;
            }
        }
        if (strDecode == null) {
            return null;
        }
        o.d("DemoUtil", "[scanExternalSubtitle] Uri path = " + strDecode, 16);
        File file = new File(strDecode);
        File parentFile = file.getParentFile();
        String lowerCase = file.getName().substring(0, file.getName().lastIndexOf(".")).toLowerCase();
        ArrayList arrayList = new ArrayList();
        if (parentFile.exists()) {
            o.d("DemoUtil", "[scanExternalSubtitle] Parent file exists: " + parentFile, 16);
            File[] fileArrListFiles = parentFile.listFiles(new C0093a(lowerCase, Arrays.asList(f6374a)));
            int length = fileArrListFiles.length;
            for (int i2 = 0; i2 < length; i2++) {
                File file2 = fileArrListFiles[i2];
                if (file2.isFile()) {
                    Log.d("DemoUtil", "[scanExternalSubtitle] subtitleFile = " + file2.getName());
                    if (file2.getName().toLowerCase().endsWith(".sub")) {
                        String str = parentFile + "/" + file2.getName().substring(0, file2.getName().lastIndexOf(".")) + ".idx";
                        File file3 = new File(str);
                        o.d("DemoUtil", "[scanExternalSubtitle] Does the corresonding " + str + " exist?", 16);
                        if (file3.exists()) {
                            o.d("DemoUtil", "[scanExternalSubtitle] Yes. Add " + Uri.fromFile(file3).toString(), 16);
                            arrayList.add(Uri.fromFile(file3).toString());
                        } else {
                            o.d("DemoUtil", "[scanExternalSubtitle] No. Add " + Uri.fromFile(file2).toString(), 16);
                            arrayList.add(Uri.fromFile(file2).toString());
                        }
                    } else if (!file2.getName().toLowerCase().endsWith(".idx")) {
                        o.d("DemoUtil", "[scanExternalSubtitle] Add " + Uri.fromFile(file2).toString(), 16);
                        arrayList.add(Uri.fromFile(file2).toString());
                    }
                }
            }
        }
        return (String[]) arrayList.toArray(new String[arrayList.size()]);
    }
}
