package com.ifpdos.factorytest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Environment;
import android.os.Message;
import android.os.PowerManager;
import android.os.SystemProperties;
import android.util.Log;
import android.view.View;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;
import com.seewo.vcommons.constants.SeewoConstants;
import com.seewo.vcommons.helper.StorageHelper;
import com.seewo.vcommons.utils.ShellCommandUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.util.Iterator;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class Common {
    public static final String AUDIO_OUT_FILE = "audio_out.wav";
    public static final String DebugMenu_Class = "com.ifpdos.debugmenu.MainActivity";
    public static final String DebugMenu_PACKAGE = "com.ifpdos.debugmenu";
    public static final String DebugMenu_Receiver = "com.ifpdos.debugmenu.upgrade.UpgradeReceiver";
    public static final String EXT_SD_PATH = "/storage/sdcard1/";
    static final String FACTORY_AGENT_PASSWORD = "8f9174c17fe14c72288c3b4739132d34";
    static final String FACTORY_RESET_PASSWORD = "9d311b2e7e1a3c8b543ef6af1f762837";
    static final String FACTORY_TEST_PASSWORD = "a39ebcae960bbd4a8ae0edb386fd33af";
    public static final String FILE_PATH_KEY = "file_path";
    public static final String GRAY_SCALE_TEST_FILE = "gray_scale_test.bmp";
    public static final String HALL_SWITCH_ENABLE_PATH = "/sys/bus/platform/drivers/switch-hall/enable";
    public static final String HALL_SWITCH_PATH = "/sys/class/switch/hall/state";
    public static final String IN_SD_PATH = "/storage/sdcard0/";
    private static final String IR_SWITCH_PATH = "/sys/bus/platform/drivers/ir-switch/ir_switch_threshold";
    public static final String KEY_IS_AUDIO_OUT = "key_audio_out";
    public static String LCD_VCOM_DUTY_PATH = "/sys/devices/platform/leds-mt65xx/leds/lcd-backlight/vcom_pwm_duty";
    public static final String MACHINE_SHOCK_FILE = "machine_shock.mp3";
    public static final String PLAYBACK_VOLUME_KEY = "playback_volume";
    public static final String SD_PATH = "/mnt/sdcard/";
    public static final String SOUND_PRESSURE_FILE = "0dB_pink noise.mp3";
    public static final String SPEAKER_TEST_FILE = "speaker.wav";
    public static String SYS_TP_FW_VERSION = "/sys/tp_fw/version";
    public static final String TAG = "Common";
    public static final String UDI_CORE_PACKAGE = "com.cvte.udi.service.core";
    public static final String USB_PATH = "/storage/usbotg/";
    public static final String VIDEO_PLAYER_CLASS = "com.android.gallery3d/.app.MovieActivity";
    public static final String DFT_CONFIG_DIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + File.separator + "dft_config";
    static String mFactoryTestPassward = "";

    static boolean isNotHexNumber(byte b) {
        if (b < 48 || b > 57) {
            return b < 97 || b > 102;
        }
        return false;
    }

    public static void disableIrSleep() {
        if (new File(IR_SWITCH_PATH).exists()) {
            textFileWriter(IR_SWITCH_PATH, "0");
        }
    }

    public static Uri getTestVideo() {
        Uri randomFile = FileFinder.getRandomFile("/storage/sdcard1/factory_test/", ".mp4");
        if (randomFile == null) {
            randomFile = FileFinder.getRandomFile("/data/factory_test/", ".mp4");
        }
        if (randomFile == null) {
            File file = new File("/system/media/audio/test/test003");
            if (file.exists()) {
                randomFile = Uri.fromFile(file);
            }
        }
        if (randomFile != null) {
            return randomFile;
        }
        File file2 = new File("/etc/factory_test/video.mp4");
        return file2.exists() ? Uri.fromFile(file2) : randomFile;
    }

    public static Uri getTestAudio() {
        File file = new File("/etc/factory_test/audio.mp3");
        if (file.exists()) {
            return Uri.fromFile(file);
        }
        return null;
    }

    public static Uri getMachineShockAudio() {
        File file = new File(new File(DFT_CONFIG_DIR), MACHINE_SHOCK_FILE);
        if (file.exists()) {
            return Uri.fromFile(file);
        }
        return null;
    }

    public static String getMachineShockAudioPath() {
        File file = new File(new File(DFT_CONFIG_DIR), MACHINE_SHOCK_FILE);
        if (file.exists()) {
            return file.getAbsolutePath();
        }
        return null;
    }

    public static String getSoundPressureAudioPath() {
        File file = new File(new File(DFT_CONFIG_DIR), SOUND_PRESSURE_FILE);
        if (file.exists()) {
            return file.getAbsolutePath();
        }
        return null;
    }

    public static String getGrayScaleTestFile() {
        File file = new File(new File(DFT_CONFIG_DIR), GRAY_SCALE_TEST_FILE);
        if (file.exists()) {
            return file.getAbsolutePath();
        }
        Log.d(TAG, "getGrayScaleTestFile: not found gray scale test file");
        return null;
    }

    public static Uri getSpeakerTestAudio() {
        File file = new File(new File(DFT_CONFIG_DIR), SPEAKER_TEST_FILE);
        if (file.exists()) {
            return Uri.fromFile(file);
        }
        return null;
    }

    public static String getSpeakerTestAudioPath() {
        File file = new File(new File(DFT_CONFIG_DIR), SPEAKER_TEST_FILE);
        if (file.exists()) {
            return file.getAbsolutePath();
        }
        return null;
    }

    public static Uri getAudioOutAudio() {
        File file = new File(new File(DFT_CONFIG_DIR), AUDIO_OUT_FILE);
        if (file.exists()) {
            return Uri.fromFile(file);
        }
        return null;
    }

    public static String getAudioOutAudioPath() {
        File file = new File(new File(DFT_CONFIG_DIR), AUDIO_OUT_FILE);
        if (file.exists()) {
            return file.getAbsolutePath();
        }
        return null;
    }

    public static String getWirelessMicAudioPath() {
        List<StorageHelper.StorageItem> usbItems = StorageHelper.getInstance().getUsbItems();
        if (usbItems == null || usbItems.isEmpty()) {
            Log.e(TAG, "usbItems null or empty");
            return null;
        }
        for (StorageHelper.StorageItem storageItem : usbItems) {
            String strTrim = storageItem.mLabel.trim();
            Log.d(TAG, "name: " + strTrim);
            Log.d(TAG, "length:" + strTrim.length());
            if ("PTT".equals(strTrim)) {
                File file = new File(storageItem.mPath + File.separator + "mictest.wav");
                Log.d(TAG, "file path: " + file.getAbsolutePath());
                if (file.exists() && file.isFile()) {
                    return file.getAbsolutePath();
                }
            }
        }
        return null;
    }

    public static Uri getNoiseTestAudio() {
        File file = new File("/etc/factory_test/noise.mp3");
        if (file.exists()) {
            return Uri.fromFile(file);
        }
        return null;
    }

    public static Uri findTestAudioOnUSB(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);
                if (file.exists() && file.isFile()) {
                    return Uri.fromFile(file);
                }
            }
        }
        return null;
    }

    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);
                if (file.exists() && file.isFile()) {
                    return file;
                }
            }
        }
        return null;
    }

    public static String textFileReader(String str) {
        try {
            FileReader fileReader = new FileReader(str);
            char[] cArr = new char[10240];
            int i = fileReader.read(cArr, 0, 10239);
            fileReader.close();
            cArr[i] = 0;
            return new String(cArr, 0, i);
        } catch (Exception e) {
            Log.e(TAG, "textFileReader error:" + e);
            return "";
        }
    }

    public static boolean textFileWriter(String str, String str2) {
        try {
            FileWriter fileWriter = new FileWriter(str);
            fileWriter.write(str2, 0, str2.length());
            fileWriter.flush();
            fileWriter.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static int getFontHeight(float f) {
        Paint paint = new Paint();
        paint.setTextSize(f);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        return ((int) Math.ceil(fontMetrics.descent - fontMetrics.top)) + 2;
    }

    public static boolean checkPackageExists(Context context, String str) {
        if (str != null && !"".equals(str)) {
            try {
                context.getPackageManager().getApplicationInfo(str, 8192);
                return true;
            } catch (Exception e) {
                Log.e(TAG, "e=" + e);
            }
        }
        return false;
    }

    public static void showSystemUiIsolated(boolean z, View view) {
        view.setSystemUiVisibility(z ? 0 : 4102);
    }

    public static void showSystemUi(boolean z, View view) {
        view.setSystemUiVisibility(z ? 0 : 4102);
    }

    public static String runCmd(String str) {
        try {
            Process processExec = Runtime.getRuntime().exec(ShellCommandUtils.COMMAND_SH);
            OutputStream outputStream = processExec.getOutputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(processExec.getInputStream()));
            outputStream.write((str + ShellCommandUtils.COMMAND_LINE_END).getBytes());
            outputStream.flush();
            outputStream.write(ShellCommandUtils.COMMAND_EXIT.getBytes());
            outputStream.flush();
            processExec.waitFor();
            String str2 = "";
            while (true) {
                try {
                    String line = bufferedReader.readLine();
                    if (line != null) {
                        str2 = str2 + line + ShellCommandUtils.COMMAND_LINE_END;
                    } else {
                        outputStream.close();
                        bufferedReader.close();
                        return str2;
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Fails to get ret:" + e);
                    e.printStackTrace();
                    return "";
                }
            }
        } catch (Exception e2) {
            Log.e(TAG, "Fails to sh:" + e2);
            e2.printStackTrace();
            return "";
        }
    }

    public static String bytesToHexString(byte[] bArr) {
        if (bArr == null) {
            return "";
        }
        char[] cArr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        char[] cArr2 = new char[bArr.length * 2];
        for (int i = 0; i < bArr.length; i++) {
            byte b = bArr[i];
            int i2 = i * 2;
            cArr2[i2] = cArr[(b >>> 4) & 15];
            cArr2[i2 + 1] = cArr[b & 15];
        }
        return new String(cArr2);
    }

    public static String makeMd5(File file) {
        if (!file.exists()) {
            return "File Not Found";
        }
        if (file.length() == 0) {
            return "d41d8cd98f00b204e9800998ecf8427e";
        }
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            if (messageDigest == null) {
                return "";
            }
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bArr = new byte[1048576];
            while (true) {
                int i = fileInputStream.read(bArr);
                if (i != -1) {
                    messageDigest.update(bArr, 0, i);
                } else {
                    fileInputStream.close();
                    return bytesToHexString(messageDigest.digest());
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            return "";
        }
    }

    public static String makeMd5(byte[] bArr) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            if (messageDigest == null) {
                return "d41d8cd98f00b204e9800998ecf8427e";
            }
            messageDigest.update(bArr, 0, bArr.length);
            return bytesToHexString(messageDigest.digest());
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            return "d41d8cd98f00b204e9800998ecf8427e";
        }
    }

    static boolean checkFactoryResetPassword(Activity activity) {
        if (FACTORY_RESET_PASSWORD.equals(makeMd5(("checkFactoryResetPassword" + makeMd5((activity.getIntent().getStringExtra("FactoryResetPassward") + "checkFactoryResetPassword").getBytes())).getBytes()))) {
            return true;
        }
        activity.finish();
        return false;
    }

    static boolean checkFactoryTestPassword(Activity activity) {
        if (FACTORY_TEST_PASSWORD.equals(makeMd5(("checkFactoryTestPassword" + makeMd5((activity.getIntent().getStringExtra("FactoryTestPassward") + "checkFactoryTestPassword").getBytes())).getBytes()))) {
            return true;
        }
        activity.finish();
        return false;
    }

    static boolean checkFactoryAgentPassword(Activity activity) {
        if (FACTORY_AGENT_PASSWORD.equals(makeMd5(("checkFactoryAgentPassword" + makeMd5((activity.getIntent().getStringExtra("c6ad4d9c") + "checkFactoryAgentPassword").getBytes())).getBytes()))) {
            return true;
        }
        activity.finish();
        return false;
    }

    static boolean writeImeiByAt(String str, int i, Message message) {
        Phone defaultPhone = PhoneFactory.getDefaultPhone();
        Log.d(TAG, "p:" + defaultPhone);
        if (defaultPhone == null) {
            return false;
        }
        String[] strArr = new String[2];
        strArr[0] = "AT+EGMR=1,";
        if (i == 0) {
            strArr[0] = strArr[0] + SeewoConstants.KEY_THIRD_BUTTON_IN_PC;
        } else {
            if (i >= 4) {
                return false;
            }
            strArr[0] = strArr[0] + (i + 9);
        }
        strArr[0] = strArr[0] + ",\"" + str + "\"\r\n";
        strArr[1] = "+EGMR";
        defaultPhone.invokeOemRilRequestStrings(strArr, message);
        return true;
    }

    static boolean writeGsmSnByAt(String str, Message message) {
        Phone defaultPhone = PhoneFactory.getDefaultPhone();
        Log.d(TAG, "p: " + defaultPhone);
        if (defaultPhone == null) {
            return false;
        }
        defaultPhone.invokeOemRilRequestStrings(new String[]{"AT+EGMR=1,5,\"" + str + "\"\r\n", "+EGMR"}, message);
        return true;
    }

    public static void reboot(Context context) {
        try {
            ((PowerManager) context.getSystemService("power")).reboot("");
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

    static boolean isValidMac(String str) {
        if (str == null || str.length() != 17) {
            return false;
        }
        byte[] bytes = str.toLowerCase().getBytes();
        return (bytes[2] != 58 || bytes[5] != 58 || bytes[8] != 58 || bytes[11] != 58 || bytes[14] != 58 || isNotHexNumber(bytes[0]) || isNotHexNumber(bytes[1]) || isNotHexNumber(bytes[3]) || isNotHexNumber(bytes[4]) || isNotHexNumber(bytes[6]) || isNotHexNumber(bytes[7]) || isNotHexNumber(bytes[9]) || isNotHexNumber(bytes[10]) || isNotHexNumber(bytes[12]) || isNotHexNumber(bytes[13]) || isNotHexNumber(bytes[15]) || isNotHexNumber(bytes[16])) ? false : true;
    }

    public static byte[] macToBytes(String str) {
        String[] strArrSplit;
        if (!isValidMac(str) || (strArrSplit = str.split(":")) == null || strArrSplit.length != 6) {
            return null;
        }
        byte[] bArr = new byte[6];
        for (int i = 0; i < 6; i++) {
            bArr[i] = (byte) Integer.parseInt(strArrSplit[i], 16);
        }
        return bArr;
    }

    public static String getNtrigTpFwVersion() {
        return SystemProperties.get("ro.sys.ntrig.tp.fw.version", "null");
    }

    public static String getTpFwVersion() {
        return SystemProperties.get("ro.sys.tp.fw.version", "0x00");
    }

    public static boolean isHasBattery() {
        return SystemProperties.get("ro.sys.battery", SeewoConstants.VALUE_LOCK_TOUCH_OFF).equals(SeewoConstants.VALUE_LOCK_TOUCH_OFF);
    }
}
