package d.f.a.r2;

import android.os.Environment;
import android.util.Log;
import com.google.zxing.common.StringUtils;
import d.i.f.d.b;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/* JADX INFO: compiled from: FileUtils.java */
/* JADX INFO: loaded from: classes.dex */
public class u2 {
    public static final String a = Environment.getExternalStorageDirectory().getAbsolutePath();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final String f3338b;

    static {
        f3338b = (d.i.c.a.r() || d.i.c.a.e() == d.i.f.g.a.MT9666) ? "boot0.jpg" : "boot0.bmp";
        System.getProperty("line.separator");
    }

    public static String a(String str) {
        File file = new File(f(str));
        String str2 = "";
        if (file.isDirectory()) {
            Log.d("TestFile", "The File doesn't not exist.");
        } else {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
                while (true) {
                    String line = bufferedReader.readLine();
                    if (line == null) {
                        break;
                    }
                    str2 = str2 + line + "\n";
                }
                fileInputStream.close();
            } catch (FileNotFoundException unused) {
                Log.d("TestFile", "The File doesn't not exist.");
            } catch (IOException e2) {
                Log.d("TestFile", e2.getMessage());
            }
        }
        return str2;
    }

    public static boolean b(File file) {
        Log.e("FileUtils", "checkTargetFile: " + file);
        if (file.exists()) {
            Log.e("FileUtils", "checkTargetFile: 文件已存在，删除文件");
            if (!file.delete()) {
                Log.e("FileUtils", "delete file error");
                return true;
            }
        }
        boolean zCreateNewFile = false;
        try {
            Log.e("FileUtils", "checkTargetFile: 创建新的文件前");
            zCreateNewFile = file.createNewFile();
            Log.e("FileUtils", "checkTargetFile: 创建新的文件成功");
        } catch (IOException e2) {
            e2.getStackTrace();
            Log.e("FileUtils", "checkTargetFile: " + e2.getMessage());
        }
        return !zCreateNewFile;
    }

    public static boolean c(String str, String str2) {
        StringBuilder sb = new StringBuilder();
        sb.append("cp ");
        sb.append(str);
        sb.append(" ");
        sb.append(str2);
        return d.i.c.a.b(sb.toString()).a == 0;
    }

    public static File d(String str) {
        return e(str, true);
    }

    public static File e(String str, boolean z) {
        if (str == null) {
            return null;
        }
        Log.i("FileUtils", "findFileOrDirOnUsb fileName: " + str + ", isOnlyFindFile = " + z);
        List<b.m> listC = d.i.f.d.b.a().c();
        if (!listC.isEmpty()) {
            Iterator<b.m> it = listC.iterator();
            while (it.hasNext()) {
                File file = new File(d.b.a.a.a.w(new StringBuilder(), it.next().a, "/", str));
                boolean z2 = !z || file.isFile();
                Log.e("FileUtils", "findFileOrDirOnUsb filePath: " + file + ",isFileMatch = " + z2);
                if (file.exists() && z2) {
                    return file;
                }
            }
        }
        return null;
    }

    public static String f(String str) {
        File fileD = d(str);
        return fileD == null ? "" : fileD.getAbsolutePath();
    }

    public static File g(String str) {
        if (!o()) {
            return null;
        }
        File file = new File(i().a);
        if (file.isDirectory()) {
            return new File(file, str);
        }
        return null;
    }

    public static String h(File file) {
        if (!file.isFile()) {
            return null;
        }
        byte[] bArr = new byte[1024];
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            FileInputStream fileInputStream = new FileInputStream(file);
            while (true) {
                int i2 = fileInputStream.read(bArr, 0, 1024);
                if (i2 == -1) {
                    break;
                }
                messageDigest.update(bArr, 0, i2);
            }
            fileInputStream.close();
            byte[] bArrDigest = messageDigest.digest();
            StringBuilder sb = new StringBuilder("");
            if (bArrDigest == null || bArrDigest.length <= 0) {
                return null;
            }
            for (byte b2 : bArrDigest) {
                String hexString = Integer.toHexString(b2 & 255);
                if (hexString.length() < 2) {
                    sb.append(0);
                }
                sb.append(hexString);
            }
            return sb.toString();
        } catch (Exception e2) {
            e2.printStackTrace();
            return null;
        }
    }

    public static b.m i() {
        List<b.m> listC = d.i.f.d.b.a().c();
        if (listC.isEmpty()) {
            return null;
        }
        return listC.get(0);
    }

    public static String j() {
        return k(a);
    }

    public static String k(String str) {
        File file = new File(str);
        if (!file.exists()) {
            return "";
        }
        if (file.listFiles() == null) {
            StringBuilder sbE = d.b.a.a.a.E("chmod ", "777", " ");
            sbE.append(file.getAbsolutePath());
            d.i.c.a.b(sbE.toString());
        }
        File[] fileArrListFiles = file.listFiles();
        if (fileArrListFiles != null) {
            for (File file2 : fileArrListFiles) {
                String name = file2.getName();
                if (name.toLowerCase().matches("test.mp4|test.avi|test.wmv|test.flv|test.rmvb")) {
                    return d.b.a.a.a.q(str, "/", name);
                }
            }
        }
        return "";
    }

    public static String l() {
        Iterator<b.m> it = d.i.f.d.b.a().c().iterator();
        while (it.hasNext()) {
            String strK = k(it.next().a);
            if (!(strK == null || strK.trim().length() == 0)) {
                return strK;
            }
        }
        return null;
    }

    public static boolean m(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            int[] iArr = {fileInputStream.read(), fileInputStream.read()};
            fileInputStream.close();
            Log.e("FileUtils", "isBmpFile: b[0] == " + iArr[0]);
            Log.e("FileUtils", "isBmpFile: b[1] == " + iArr[1]);
            if (iArr[0] == 66) {
                return iArr[1] == 77;
            }
            return false;
        } catch (Exception e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public static boolean n(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.skip(fileInputStream.available() - 2);
            int[] iArr = {fileInputStream.read(), fileInputStream.read(), fileInputStream.read(), fileInputStream.read()};
            Log.e("FileUtils", "isBmpFile: b[0] == " + iArr[0]);
            Log.e("FileUtils", "isBmpFile: b[1] == " + iArr[1]);
            Log.e("FileUtils", "isBmpFile: b[2] == " + iArr[2]);
            Log.e("FileUtils", "isBmpFile: b[3] == " + iArr[3]);
            fileInputStream.close();
            if (iArr[0] == 255 && iArr[1] == 216 && iArr[2] == 255) {
                return iArr[3] == 217;
            }
            return false;
        } catch (Exception e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public static boolean o() {
        return d.i.f.d.b.a().c().size() > 0;
    }

    public static String p(String str) {
        File file = null;
        if (str != null) {
            Log.e("FileUtils", "pattern: " + str);
            List<b.m> listC = d.i.f.d.b.a().c();
            if (!listC.isEmpty()) {
                Iterator<b.m> it = listC.iterator();
                loop0: while (true) {
                    if (!it.hasNext()) {
                        break;
                    }
                    File[] fileArrListFiles = new File(it.next().a + "/" + d.f.a.p2.t0.f3048b).listFiles();
                    if (fileArrListFiles != null) {
                        for (File file2 : fileArrListFiles) {
                            if (Pattern.matches(str, file2.getName())) {
                                Log.e("FileUtils", "matchFileOnUSB: " + file2);
                                file = file2;
                                break loop0;
                            }
                        }
                    }
                }
            }
        }
        return file == null ? "" : file.getAbsolutePath();
    }

    public static String q(String str, String str2) {
        File file = null;
        if (str2 != null) {
            Log.e("FileUtils", "pattern: " + str2);
            List<b.m> listC = d.i.f.d.b.a().c();
            if (!listC.isEmpty()) {
                Iterator<b.m> it = listC.iterator();
                loop0: while (true) {
                    if (!it.hasNext()) {
                        break;
                    }
                    File[] fileArrListFiles = new File(it.next().a + "/" + str + "/").listFiles();
                    if (fileArrListFiles != null) {
                        for (File file2 : fileArrListFiles) {
                            if (Pattern.matches(str2, file2.getName())) {
                                Log.e("FileUtils", "matchFileOnUSB: " + file2);
                                file = file2;
                                break loop0;
                            }
                        }
                    }
                }
            }
        }
        return file == null ? "" : file.getAbsolutePath();
    }

    public static d.i.a.a.a r(String str) {
        d.i.a.a.a aVar;
        try {
            Log.e("FileUtils", "readStandardInfoFile: USB" + o());
            if (o()) {
                File fileD = d(str);
                if (fileD != null) {
                    aVar = new d.i.a.a.a();
                    aVar.a(fileD);
                } else {
                    Log.e("FileUtils", "readStandardInfoFile: 发现了USB但找不到文件");
                    aVar = null;
                }
            } else {
                aVar = null;
            }
            if (aVar != null) {
                return aVar;
            }
            File file = new File(d.i.f.d.b.a().b().a + "/" + str);
            if (!file.exists() || !file.isFile()) {
                file = null;
            }
            if (file == null || !file.exists()) {
                return aVar;
            }
            d.i.a.a.a aVar2 = new d.i.a.a.a();
            aVar2.a(file);
            return aVar2;
        } catch (IllegalArgumentException e2) {
            d.i.a.a.d.a("FileUtils", "get json file fail", e2);
            return null;
        }
    }

    public static void s(File file, String str) throws IOException {
        ZipFile zipFile = new ZipFile(file);
        Enumeration<? extends ZipEntry> enumerationEntries = zipFile.entries();
        byte[] bArr = new byte[1024];
        while (enumerationEntries.hasMoreElements()) {
            ZipEntry zipEntryNextElement = enumerationEntries.nextElement();
            if (zipEntryNextElement.isDirectory()) {
                StringBuilder sbB = d.b.a.a.a.B("ze.getName() = ");
                sbB.append(zipEntryNextElement.getName());
                Log.d("FileUtils", sbB.toString());
                String str2 = new String((str + zipEntryNextElement.getName()).getBytes("8859_1"), StringUtils.GB2312);
                Log.d("FileUtils", "str = " + str2);
                new File(str2).mkdir();
            } else {
                StringBuilder sbB2 = d.b.a.a.a.B("ze.getName() = ");
                sbB2.append(zipEntryNextElement.getName());
                Log.d("FileUtils", sbB2.toString());
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(str, zipEntryNextElement.getName())));
                BufferedInputStream bufferedInputStream = new BufferedInputStream(zipFile.getInputStream(zipEntryNextElement));
                while (true) {
                    int i2 = bufferedInputStream.read(bArr, 0, 1024);
                    if (i2 == -1) {
                        break;
                    } else {
                        bufferedOutputStream.write(bArr, 0, i2);
                    }
                }
                bufferedInputStream.close();
                bufferedOutputStream.close();
            }
        }
        zipFile.close();
    }

    public static void t(String str, String str2) {
        File file = new File(str);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e2) {
                StringBuilder sbB = d.b.a.a.a.B("writeCSV createNewFile=");
                sbB.append(e2.getMessage());
                Log.i("FileUtils", sbB.toString());
                throw new RuntimeException(e2);
            }
        }
        d.b.a.a.a.O("writeCSV value=", str2, "FileUtils");
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
            try {
                bufferedWriter.write(str2);
                bufferedWriter.newLine();
                bufferedWriter.flush();
                bufferedWriter.close();
            } finally {
            }
        } catch (IOException e3) {
            StringBuilder sbB2 = d.b.a.a.a.B("writeCSV=");
            sbB2.append(e3.getMessage());
            Log.i("FileUtils", sbB2.toString());
        }
    }
}
