package com.ifpdos.factorytest.storage;

import android.util.Log;
import com.cvte.vman.VmanMiddleWare;
import com.ifpdos.factorytest.udi.UsbInfoBody;
import com.ifpdos.factorytest.udi.UsbTestApi;
import com.ifpdos.factorytest.udi.UsbTestResultBody;
import com.ifpdos.factorytest.udi.UsbTestTypeEnum;
import com.seewo.sdk.SDKSystemHelper;
import com.seewo.vcommons.helper.StorageHelper;
import com.seewo.vcommons.utils.ShellCommandUtils;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/* JADX INFO: loaded from: classes.dex */
public class UsbTestHelper {
    private static final String TAG = "UstTestHelper";
    private static final ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    private static final String strEnd = "M/s";
    private static final String strStart = "s, ";

    public static UsbTestResultBody startUsbNumResult() {
        UsbTestResultBody usbTestResultBody = new UsbTestResultBody();
        ArrayList arrayList = new ArrayList();
        List<StorageHelper.StorageItem> usbItems = StorageHelper.getInstance().getUsbItems();
        if (!usbItems.isEmpty()) {
            for (StorageHelper.StorageItem storageItem : usbItems) {
                arrayList.add(new UsbInfoBody(storageItem.mLabel, storageItem.mPath, null));
                Log.d(TAG, "startUsbNumResult:item.mLabel:" + storageItem.mLabel);
            }
        }
        usbTestResultBody.setResult(arrayList);
        UsbTestApi.INSTANCE.notify(UsbTestTypeEnum.NUM, usbTestResultBody);
        return usbTestResultBody;
    }

    public static void startUsbSpeedTest(final boolean z) {
        final List<StorageHelper.StorageItem> usbItems = StorageHelper.getInstance().getUsbItems();
        final UsbTestResultBody usbTestResultBody = new UsbTestResultBody();
        final ArrayList arrayList = new ArrayList();
        cachedThreadPool.execute(new Runnable() { // from class: com.ifpdos.factorytest.storage.UsbTestHelper.1
            @Override // java.lang.Runnable
            public void run() {
                String strWriteToUsbTest;
                for (StorageHelper.StorageItem storageItem : usbItems) {
                    if (z) {
                        strWriteToUsbTest = UsbTestHelper.readFromUsbTest(storageItem.mPath);
                    } else {
                        strWriteToUsbTest = UsbTestHelper.writeToUsbTest(storageItem.mPath);
                    }
                    UsbInfoBody usbInfoBody = new UsbInfoBody(storageItem.mLabel, storageItem.mPath, strWriteToUsbTest);
                    Log.d(UsbTestHelper.TAG, "isReadTest:" + z + " speed: " + strWriteToUsbTest + " item.mPath:" + storageItem.mPath);
                    arrayList.add(usbInfoBody);
                }
                usbTestResultBody.setResult(arrayList);
                if (z) {
                    UsbTestApi.INSTANCE.notify(UsbTestTypeEnum.READ, usbTestResultBody);
                } else {
                    UsbTestApi.INSTANCE.notify(UsbTestTypeEnum.WRITE, usbTestResultBody);
                }
            }
        });
    }

    public static void dropSystemCaches() {
        try {
            SDKSystemHelper.I.executeShellCommand("echo 3 > /proc/sys/vm/drop_caches");
            VmanMiddleWare.getInstance().getSystemCtrl().doDropCaches();
        } catch (Throwable unused) {
            Log.w(TAG, "invoke vman doDropCaches error");
        }
    }

    public static String writeToUsbTest(String str) {
        dropSystemCaches();
        String str2 = "dd if=/dev/zero of=" + str + "/1 bs=1024000 count=200";
        try {
            long jNanoTime = System.nanoTime();
            execCmd(str2, null);
            execCmd("sync", null);
            long jNanoTime2 = System.nanoTime() - jNanoTime;
            Log.d(TAG, "resumeTime: " + jNanoTime2);
            float f = 200.0f / ((jNanoTime2 * 1.0f) / 1.0E9f);
            Log.d(TAG, "write rate: " + f);
            File file = new File(str + "/1");
            if (file.exists()) {
                file.delete();
            }
            return String.valueOf(f);
        } catch (Exception e) {
            Log.d(TAG, "写操作复制单个文件操作出错" + e.toString());
            return "error:writeToUsbTest";
        }
    }

    public static boolean isTestFileExist() {
        List<StorageHelper.StorageItem> usbItems = StorageHelper.getInstance().getUsbItems();
        if (usbItems.isEmpty()) {
            return false;
        }
        Iterator<StorageHelper.StorageItem> it = usbItems.iterator();
        while (it.hasNext()) {
            if (!new File(it.next().mPath, "usb_test_file.mp4").exists()) {
                return false;
            }
        }
        return true;
    }

    public static String readFromUsbTest(String str) {
        try {
            dropSystemCaches();
            String str2 = "dd of=/dev/null if=" + str + "/usb_test_file.mp4 conv=sync";
            Log.d(TAG, "readFromUsbTest execute shell: " + str2);
            Thread.sleep(1000L);
            dropSystemCaches();
            String strExecCmd = execCmd(str2, null);
            Log.d(TAG, "readResult=" + strExecCmd + ShellCommandUtils.COMMAND_LINE_END);
            String strSubstring = strExecCmd.substring(strExecCmd.indexOf(strStart), strExecCmd.indexOf(strEnd)).substring(strStart.length());
            Log.d(TAG, "strReadRate=" + strSubstring + ShellCommandUtils.COMMAND_LINE_END);
            return strSubstring;
        } catch (Exception e) {
            Log.d(TAG, "读取单个文件操作出错", e);
            return "error:not found usb_test_file.mp4";
        }
    }

    public static String execCmd(String str, File file) throws Exception {
        Process processExec;
        BufferedReader bufferedReader;
        StringBuilder sb = new StringBuilder();
        BufferedReader bufferedReader2 = null;
        try {
            processExec = Runtime.getRuntime().exec(str, (String[]) null, file);
            try {
                processExec.waitFor();
                BufferedReader bufferedReader3 = new BufferedReader(new InputStreamReader(processExec.getInputStream(), "UTF-8"));
                try {
                    bufferedReader = new BufferedReader(new InputStreamReader(processExec.getErrorStream(), "UTF-8"));
                    while (true) {
                        try {
                            String line = bufferedReader3.readLine();
                            if (line == null) {
                                break;
                            }
                            sb.append(line).append('\n');
                        } catch (Throwable th) {
                            th = th;
                            bufferedReader2 = bufferedReader3;
                            closeStream(bufferedReader2);
                            closeStream(bufferedReader);
                            if (processExec != null) {
                                processExec.destroy();
                            }
                            throw th;
                        }
                    }
                    while (true) {
                        String line2 = bufferedReader.readLine();
                        if (line2 == null) {
                            break;
                        }
                        sb.append(line2).append('\n');
                    }
                    closeStream(bufferedReader3);
                    closeStream(bufferedReader);
                    if (processExec != null) {
                        processExec.destroy();
                    }
                    return sb.toString();
                } catch (Throwable th2) {
                    th = th2;
                    bufferedReader = null;
                }
            } catch (Throwable th3) {
                th = th3;
                bufferedReader = null;
            }
        } catch (Throwable th4) {
            th = th4;
            processExec = null;
            bufferedReader = null;
        }
    }

    private static void closeStream(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Exception unused) {
            }
        }
    }
}
