package com.ifpdos.factorytest;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.os.storage.DiskInfo;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ifpdos.factorytest.storage.UsbTestHelper;
import com.seewo.vcommons.helper.StorageHelper;
import com.seewo.vcommons.utils.ShellCommandUtils;
import com.seewo.vcommons.utils.reflect.StorageManagerInsider;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/* JADX INFO: loaded from: classes.dex */
public class UsbSpeedTestActivity extends Activity {
    private static final String TAG = "UsbSpeedTest";
    private static final float standardReadRate = 30.0f;
    private static final float standardWriteRate = 30.0f;
    private Button btnRead;
    private Button btnUsbNum;
    private Button btnWrite;
    public Handler mHandler;
    private Handler mWriteHandler;
    private int count = 0;
    private int MESSAGE_NOT_GET_VALUE = 0;
    private int MESSAGE_UPDATE_LAYOUT_READ_DATA = 1;
    private int MESSAGE_TEST_USB_NUM = 2;
    private int diskSize = 0;
    private int usbDriveCount = 0;
    private int diskSizeFinish = 0;
    private HandlerThread mWriteThread = new HandlerThread("write");
    final ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

    public enum FileUnit {
        KB,
        MB,
        GB
    }

    @Override // android.app.Activity
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_usb_speed_test);
        ((Button) findViewById(R.id.usb_speed_test_pass)).setOnClickListener(new View.OnClickListener() { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity$$ExternalSyntheticLambda0
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                this.f$0.lambda$onCreate$0(view);
            }
        });
        ((Button) findViewById(R.id.usb_speed_test_fail)).setOnClickListener(new View.OnClickListener() { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity$$ExternalSyntheticLambda1
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                this.f$0.lambda$onCreate$1(view);
            }
        });
        Button button = (Button) findViewById(R.id.usb_speed_test_read);
        this.btnRead = button;
        button.setOnClickListener(new View.OnClickListener() { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity$$ExternalSyntheticLambda2
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                this.f$0.lambda$onCreate$2(view);
            }
        });
        Button button2 = (Button) findViewById(R.id.usb_speed_test_write);
        this.btnWrite = button2;
        button2.setOnClickListener(new View.OnClickListener() { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity$$ExternalSyntheticLambda3
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                this.f$0.lambda$onCreate$3(view);
            }
        });
        Button button3 = (Button) findViewById(R.id.usb_num_test);
        this.btnUsbNum = button3;
        button3.setOnClickListener(new View.OnClickListener() { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity$$ExternalSyntheticLambda4
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                this.f$0.lambda$onCreate$4(view);
            }
        });
        this.mHandler = new Handler(getMainLooper()) { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity.1
            @Override // android.os.Handler
            public void handleMessage(Message message) {
                super.handleMessage(message);
                if (message.what == UsbSpeedTestActivity.this.MESSAGE_UPDATE_LAYOUT_READ_DATA) {
                    String string = message.getData().getString("value");
                    Log.i(UsbSpeedTestActivity.TAG, "请求结果:" + string);
                    UsbSpeedTestActivity.this.updateLayoutRead(string);
                } else if (message.what == UsbSpeedTestActivity.this.MESSAGE_NOT_GET_VALUE) {
                    Log.d(UsbSpeedTestActivity.TAG, "do not get value");
                } else if (message.what == UsbSpeedTestActivity.this.MESSAGE_TEST_USB_NUM) {
                    UsbSpeedTestActivity.this.updateUsbInfo();
                    Message message2 = new Message();
                    message2.what = UsbSpeedTestActivity.this.MESSAGE_TEST_USB_NUM;
                    UsbSpeedTestActivity.this.mHandler.sendMessageDelayed(message2, 1000L);
                }
            }
        };
        Message message = new Message();
        message.what = this.MESSAGE_TEST_USB_NUM;
        this.mHandler.sendMessage(message);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onCreate$0(View view) {
        setResult(1);
        finish();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onCreate$1(View view) {
        setResult(2);
        finish();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onCreate$2(View view) {
        this.mHandler.removeMessages(this.MESSAGE_TEST_USB_NUM);
        startReadSpeedTest();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onCreate$3(View view) {
        this.mHandler.removeMessages(this.MESSAGE_TEST_USB_NUM);
        if (this.mWriteHandler == null) {
            this.mWriteThread.start();
            this.mWriteHandler = new Handler(this.mWriteThread.getLooper());
        }
        this.mWriteHandler.post(new Runnable() { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity$$ExternalSyntheticLambda8
            @Override // java.lang.Runnable
            public final void run() {
                this.f$0.startWriteSpeedTest();
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onCreate$4(View view) {
        Message message = new Message();
        message.what = this.MESSAGE_TEST_USB_NUM;
        this.mHandler.sendMessage(message);
        this.btnUsbNum.setBackgroundResource(R.drawable.test_pass_background);
        this.btnRead.setBackgroundResource(R.drawable.usb_speed_btn);
        this.btnWrite.setBackgroundResource(R.drawable.usb_speed_btn);
    }

    @Override // android.app.Activity
    protected void onDestroy() {
        super.onDestroy();
        if (this.mWriteHandler != null) {
            this.mWriteThread.quit();
            this.mWriteHandler.removeCallbacksAndMessages(null);
        }
        this.cachedThreadPool.shutdown();
    }

    public void startWriteSpeedTest() {
        if (this.count != 0) {
            return;
        }
        Log.d(TAG, "doWriteTest");
        Handler handler = new Handler(getMainLooper()) { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity.2
            @Override // android.os.Handler
            public void handleMessage(Message message) {
                super.handleMessage(message);
                if (message.what == 1) {
                    String string = message.getData().getString("value");
                    Log.i(UsbSpeedTestActivity.TAG, "请求结果:" + string);
                    UsbSpeedTestActivity.this.updateLayoutWrite(string);
                } else if (message.what == 0) {
                    Log.d(UsbSpeedTestActivity.TAG, "do not get value");
                }
            }
        };
        handler.post(new Runnable() { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity$$ExternalSyntheticLambda5
            @Override // java.lang.Runnable
            public final void run() {
                this.f$0.lambda$startWriteSpeedTest$5();
            }
        });
        List<StorageHelper.StorageItem> usbItemWithoutTFCard = getUsbItemWithoutTFCard();
        if (!usbItemWithoutTFCard.isEmpty()) {
            int size = usbItemWithoutTFCard.size();
            this.diskSize = size;
            this.diskSizeFinish = size;
            final String string = getString(R.string.usb_speed_write_test_title, new Object[]{Integer.valueOf(this.diskSize)});
            handler.post(new Runnable() { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity$$ExternalSyntheticLambda6
                @Override // java.lang.Runnable
                public final void run() {
                    this.f$0.lambda$startWriteSpeedTest$6(string);
                }
            });
            this.count = 1;
            for (StorageHelper.StorageItem storageItem : usbItemWithoutTFCard) {
                Log.d(TAG, "usbPath=" + storageItem.mPath + ShellCommandUtils.COMMAND_LINE_END);
                String str = storageItem.mPath;
                String str2 = storageItem.mLabel;
                Log.d(TAG, "mUsbRateTestRunnable");
                String str3 = str2 + "|" + isUsbRateOk(str, false);
                Message message = new Message();
                Bundle bundle = new Bundle();
                bundle.putString("value", str3);
                message.setData(bundle);
                message.what = 1;
                handler.sendMessage(message);
            }
            return;
        }
        final String string2 = getString(R.string.usb_speed_write_test_title);
        this.count = 0;
        handler.post(new Runnable() { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity$$ExternalSyntheticLambda7
            @Override // java.lang.Runnable
            public final void run() {
                this.f$0.lambda$startWriteSpeedTest$7(string2);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$startWriteSpeedTest$5() {
        this.btnWrite.setBackgroundResource(R.drawable.test_pass_background);
        this.btnRead.setBackgroundResource(R.drawable.usb_speed_btn);
        this.btnUsbNum.setBackgroundResource(R.drawable.usb_speed_btn);
        this.btnRead.setClickable(false);
        this.btnWrite.setClickable(false);
        this.btnUsbNum.setClickable(false);
        ((Button) findViewById(R.id.usb_speed_test_pass)).setEnabled(false);
        ((TextView) findViewById(R.id.usbSpeedTextTitle)).setText(" ");
        ((LinearLayout) findViewById(R.id.usbSpeedResultlinearLayout)).removeAllViews();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$startWriteSpeedTest$6(String str) {
        ((TextView) findViewById(R.id.usbSpeedTextTitle)).setText(str);
        ((TextView) findViewById(R.id.usbSpeedTextTitle)).setTextColor(Color.parseColor("#ffffff"));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$startWriteSpeedTest$7(String str) {
        ((TextView) findViewById(R.id.usbSpeedTextTitle)).setText(str);
        ((TextView) findViewById(R.id.usbSpeedTextTitle)).setTextColor(Color.parseColor("#D6001C"));
        this.btnRead.setClickable(true);
        this.btnWrite.setClickable(true);
        this.btnUsbNum.setClickable(true);
        ((Button) findViewById(R.id.usb_speed_test_pass)).setEnabled(true);
    }

    public void startReadSpeedTest() {
        if (this.count != 0) {
            return;
        }
        this.btnRead.setBackgroundResource(R.drawable.test_pass_background);
        this.btnWrite.setBackgroundResource(R.drawable.usb_speed_btn);
        this.btnUsbNum.setBackgroundResource(R.drawable.usb_speed_btn);
        this.btnRead.setClickable(false);
        this.btnWrite.setClickable(false);
        this.btnUsbNum.setClickable(false);
        ((Button) findViewById(R.id.usb_speed_test_pass)).setEnabled(false);
        ((TextView) findViewById(R.id.usbSpeedTextTitle)).setText(" ");
        ((LinearLayout) findViewById(R.id.usbSpeedResultlinearLayout)).removeAllViews();
        Log.d(TAG, "doReadTest");
        List<StorageHelper.StorageItem> usbItemWithoutTFCard = getUsbItemWithoutTFCard();
        if (!usbItemWithoutTFCard.isEmpty()) {
            int size = usbItemWithoutTFCard.size();
            this.diskSize = size;
            this.diskSizeFinish = size;
            ((TextView) findViewById(R.id.usbSpeedTextTitle)).setText(getString(R.string.usb_speed_read_test_title, new Object[]{Integer.valueOf(this.diskSize)}));
            ((TextView) findViewById(R.id.usbSpeedTextTitle)).setTextColor(Color.parseColor("#ffffff"));
            this.count = 1;
            for (StorageHelper.StorageItem storageItem : usbItemWithoutTFCard) {
                Log.d(TAG, "usbPath=" + storageItem.mPath + ShellCommandUtils.COMMAND_LINE_END);
                this.cachedThreadPool.execute(new Runnable(storageItem) { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity.3
                    private Handler handlers;
                    private String label;
                    private String path;
                    final /* synthetic */ StorageHelper.StorageItem val$item;

                    {
                        this.val$item = storageItem;
                        this.path = storageItem.mPath;
                        this.label = storageItem.mLabel;
                        this.handlers = UsbSpeedTestActivity.this.mHandler;
                    }

                    @Override // java.lang.Runnable
                    public void run() {
                        Log.d(UsbSpeedTestActivity.TAG, "mUsbRateTestRunnable");
                        String str = this.label + "|" + UsbSpeedTestActivity.this.isUsbRateOk(this.path, true);
                        Message message = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putString("value", str);
                        message.setData(bundle);
                        message.what = 1;
                        this.handlers.sendMessage(message);
                    }
                });
            }
            return;
        }
        ((TextView) findViewById(R.id.usbSpeedTextTitle)).setText(getString(R.string.usb_null_tips));
        ((TextView) findViewById(R.id.usbSpeedTextTitle)).setTextColor(Color.parseColor("#D6001C"));
        this.count = 0;
        this.btnRead.setClickable(true);
        this.btnWrite.setClickable(true);
        this.btnUsbNum.setClickable(true);
        ((Button) findViewById(R.id.usb_speed_test_pass)).setEnabled(true);
    }

    public void updateLayoutRead(String str) {
        this.diskSizeFinish--;
        String str2 = str.split("\\|")[0];
        String str3 = str.split("\\|")[1];
        if (str3.contains("error")) {
            String str4 = getString(R.string.usb_path) + str2 + "  " + str3;
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.usbSpeedResultlinearLayout);
            TextView textView = new TextView(this);
            textView.setText(str4);
            textView.setTextSize(30.0f);
            textView.setTextColor(Color.parseColor("#D6001C"));
            linearLayout.addView(textView);
        } else {
            String string = getString(R.string.usb_path_read_speed, new Object[]{str2, str3});
            Log.i(TAG, "U盘:" + str2);
            Log.i(TAG, "读出速率:" + str3);
            boolean z = Float.parseFloat(str3) > 30.0f;
            LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.usbSpeedResultlinearLayout);
            TextView textView2 = new TextView(this);
            textView2.setText(string);
            textView2.setTextSize(30.0f);
            if (!z) {
                textView2.setTextColor(Color.parseColor("#D6001C"));
            } else {
                textView2.setTextColor(Color.parseColor("#00BB31"));
            }
            linearLayout2.addView(textView2);
        }
        if (this.diskSizeFinish <= 0) {
            ((TextView) findViewById(R.id.usbSpeedTextTitle)).setText(getString(R.string.usb_speed_read_test_result_title, new Object[]{Integer.valueOf(this.diskSize)}));
            ((TextView) findViewById(R.id.usbSpeedTextTitle)).setTextColor(Color.parseColor("#00BB31"));
            this.count = 0;
            this.btnRead.setClickable(true);
            this.btnWrite.setClickable(true);
            this.btnUsbNum.setClickable(true);
            ((Button) findViewById(R.id.usb_speed_test_pass)).setEnabled(true);
        }
    }

    public void updateLayoutWrite(String str) {
        this.diskSizeFinish--;
        String str2 = str.split("\\|")[0];
        String str3 = str.split("\\|")[1];
        if (str3.contains("error")) {
            String str4 = getString(R.string.usb_path) + str2 + "  " + str3;
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.usbSpeedResultlinearLayout);
            TextView textView = new TextView(this);
            textView.setText(str4);
            textView.setTextSize(30.0f);
            textView.setTextColor(Color.parseColor("#D6001C"));
            linearLayout.addView(textView);
        } else {
            String string = getString(R.string.usb_path_write_speed, new Object[]{str2, str3});
            Log.i(TAG, "U盘:" + str2);
            Log.i(TAG, "写入速率:" + str3);
            boolean z = Float.parseFloat(str3) > 30.0f;
            LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.usbSpeedResultlinearLayout);
            TextView textView2 = new TextView(this);
            textView2.setText(string);
            textView2.setTextSize(30.0f);
            if (!z) {
                textView2.setTextColor(Color.parseColor("#D6001C"));
            } else {
                textView2.setTextColor(Color.parseColor("#00BB31"));
            }
            linearLayout2.addView(textView2);
        }
        if (this.diskSizeFinish <= 0) {
            ((TextView) findViewById(R.id.usbSpeedTextTitle)).setText(getString(R.string.usb_speed_write_test_result_title, new Object[]{Integer.valueOf(this.diskSize)}));
            ((TextView) findViewById(R.id.usbSpeedTextTitle)).setTextColor(Color.parseColor("#00BB31"));
            this.count = 0;
            this.btnRead.setClickable(true);
            this.btnWrite.setClickable(true);
            this.btnUsbNum.setClickable(true);
            ((Button) findViewById(R.id.usb_speed_test_pass)).setEnabled(true);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void updateUsbInfo() {
        final String string;
        StringBuilder sb = new StringBuilder();
        List<StorageHelper.StorageItem> usbItemWithoutTFCard = getUsbItemWithoutTFCard();
        this.usbDriveCount = 0;
        if (!usbItemWithoutTFCard.isEmpty()) {
            for (StorageHelper.StorageItem storageItem : usbItemWithoutTFCard) {
                this.usbDriveCount++;
                sb.append(getString(R.string.usb_path_name) + storageItem.mLabel + ShellCommandUtils.COMMAND_LINE_END);
                Log.d(TAG, "item.mLabel  = " + storageItem.mLabel);
            }
        }
        if (sb.length() == 0) {
            string = getString(R.string.no_usb_connected);
        } else {
            string = getString(R.string.usb_count_detected) + this.usbDriveCount;
        }
        final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.usbSpeedResultlinearLayout);
        final TextView textView = new TextView(this);
        textView.setText(sb.toString());
        textView.setTextSize(30.0f);
        textView.setAutoSizeTextTypeUniformWithConfiguration(1, 30, 1, 2);
        textView.setTextColor(Color.parseColor("#00BB31"));
        runOnUiThread(new Runnable() { // from class: com.ifpdos.factorytest.UsbSpeedTestActivity.4
            @Override // java.lang.Runnable
            public void run() {
                ((TextView) UsbSpeedTestActivity.this.findViewById(R.id.usbSpeedTextTitle)).setText(string);
                linearLayout.removeAllViews();
                linearLayout.addView(textView);
            }
        });
    }

    String isUsbRateOk(String str, boolean z) {
        if (z) {
            String fromUsbTest = UsbTestHelper.readFromUsbTest(str);
            Log.d(TAG, " readcmd=" + fromUsbTest + ShellCommandUtils.COMMAND_LINE_END);
            return fromUsbTest;
        }
        String strWriteToUsbTest = UsbTestHelper.writeToUsbTest(str);
        Log.d(TAG, " writecmd=" + strWriteToUsbTest + ShellCommandUtils.COMMAND_LINE_END);
        return strWriteToUsbTest;
    }

    public List<StorageHelper.StorageItem> getTFCardItems() {
        String str;
        ArrayList arrayList = new ArrayList();
        List<VolumeInfo> volumes = StorageManagerInsider.getVolumes((StorageManager) getSystemService("storage"));
        if (volumes != null && !volumes.isEmpty()) {
            int i = 0;
            for (VolumeInfo volumeInfo : volumes) {
                DiskInfo disk = volumeInfo.getDisk();
                if (volumeInfo.isMountedReadable() && disk != null) {
                    StorageHelper.StorageItem storageItem = new StorageHelper.StorageItem();
                    storageItem.mPath = volumeInfo.path;
                    storageItem.mInternalPath = volumeInfo.internalPath;
                    if (disk.isSd()) {
                        if (TextUtils.isEmpty(volumeInfo.fsLabel)) {
                            i++;
                            str = "SD" + i;
                        } else {
                            str = volumeInfo.fsLabel;
                        }
                        storageItem.mLabel = str;
                        storageItem.mSavePath = volumeInfo.path;
                        storageItem.mAliasPath = str;
                        arrayList.add(storageItem);
                    }
                }
            }
        }
        return arrayList;
    }

    private List<StorageHelper.StorageItem> getUsbItemWithoutTFCard() {
        List<StorageHelper.StorageItem> usbItems = StorageHelper.getInstance().getUsbItems();
        List<StorageHelper.StorageItem> tFCardItems = getTFCardItems();
        Log.d(TAG, "usbNumber=" + usbItems.size());
        Log.d(TAG, "TFCardNumber=" + tFCardItems.size());
        if (!tFCardItems.isEmpty() && !usbItems.isEmpty()) {
            for (int i = 0; i < tFCardItems.size(); i++) {
                int i2 = 0;
                while (i2 < usbItems.size()) {
                    if (tFCardItems.get(i).mPath.equals(usbItems.get(i2).mPath)) {
                        usbItems.remove(i2);
                        i2--;
                    }
                    i2++;
                }
            }
        }
        return usbItems;
    }
}
