package com.ifpdos.factorytest.util;

import android.os.Environment;
import android.os.FileUtils;
import android.util.Log;
import com.seewo.vcommons.helper.StorageHelper;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Iterator;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class FileUtil {
    private static final String AUDIO_FILE = "machine_shock.mp3";
    private static final String CONFIG_CONTENT = "dft_config";
    private static final String NETWORK_CONFIG_FILE = "dft_config/network.json";
    private static final String NEW_WIFI_CONFIG_FILE = "dft_config/wifi.json";
    private static final String TAG = "FileUtil";
    private static final String WIFI_CONFIG_FILE = "wifi.json";

    public static File findFileOnUSB(String str) {
        if (str == null) {
            return null;
        }
        List<StorageHelper.StorageItem> usbItems = StorageHelper.getInstance().getUsbItems();
        if (!usbItems.isEmpty()) {
            Iterator<StorageHelper.StorageItem> it = usbItems.iterator();
            while (it.hasNext()) {
                File file = new File(it.next().mPath + "/" + str);
                Log.d(TAG, "try find targetFile:" + file.getAbsolutePath());
                if (file.exists()) {
                    Log.d(TAG, "find file on U disk:" + file.getAbsolutePath());
                    return file;
                }
            }
        }
        return null;
    }

    public static synchronized void copyDftConfig() {
        Log.d(TAG, "copyDftConfig");
        File fileFindFileOnUSB = findFileOnUSB(CONFIG_CONTENT);
        if (fileFindFileOnUSB != null) {
            Log.e(TAG, "onReceive: 开始拷贝dft_config目录");
            File externalStoragePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            if (!externalStoragePublicDirectory.exists()) {
                Log.d(TAG, "copyDftConfig dir is not exists" + externalStoragePublicDirectory.getAbsolutePath());
                return;
            } else {
                copyDir(fileFindFileOnUSB.getPath(), externalStoragePublicDirectory.getPath() + File.separator + CONFIG_CONTENT);
                Log.d(TAG, "finish copyDftConfig");
                return;
            }
        }
        File fileFindFileOnUSB2 = findFileOnUSB(WIFI_CONFIG_FILE);
        if (fileFindFileOnUSB2 != null) {
            Log.e(TAG, "onReceive: 开始拷贝WIFI配置文件");
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), CONFIG_CONTENT);
            if (!file.exists()) {
                file.mkdir();
            }
            try {
                try {
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(fileFindFileOnUSB2));
                    try {
                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(file, WIFI_CONFIG_FILE)));
                        try {
                            FileUtils.copy(bufferedInputStream, bufferedOutputStream);
                            bufferedOutputStream.flush();
                            bufferedOutputStream.close();
                            bufferedInputStream.close();
                        } finally {
                        }
                    } catch (Throwable th) {
                        try {
                            bufferedInputStream.close();
                        } catch (Throwable th2) {
                            th.addSuppressed(th2);
                        }
                        throw th;
                    }
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
            } catch (IOException e2) {
                throw new RuntimeException(e2);
            }
        }
        Log.d(TAG, "finish copyDftConfig");
    }

    public static void copyDir(String str, String str2) {
        String[] list = new File(str).list();
        if (!new File(str2).exists()) {
            new File(str2).mkdir();
        }
        for (String str3 : list) {
            File file = new File(str + File.separator + str3);
            File file2 = new File(str2 + File.separator + str3);
            if (file.isDirectory()) {
                copyDir(file.getPath(), file2.getPath());
            }
            if (file.isFile()) {
                try {
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                    try {
                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file2));
                        try {
                            FileUtils.copy(bufferedInputStream, bufferedOutputStream);
                            bufferedOutputStream.flush();
                            bufferedOutputStream.close();
                            bufferedInputStream.close();
                        } finally {
                        }
                    } finally {
                    }
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                } catch (IOException e2) {
                    throw new RuntimeException(e2);
                }
            }
        }
    }

    public static String calculateMD5(File file) {
        int i;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bArr = new byte[4096];
            while (true) {
                int i2 = fileInputStream.read(bArr);
                if (i2 == -1) {
                    break;
                }
                messageDigest.update(bArr, 0, i2);
            }
            fileInputStream.close();
            byte[] bArrDigest = messageDigest.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : bArrDigest) {
                String hexString = Integer.toHexString(b & 255);
                if (hexString.length() == 1) {
                    sb.append('0');
                }
                sb.append(hexString);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
