package com.seewo.vcommons.mode;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import androidx.core.view.MotionEventCompat;
import androidx.core.view.ViewCompat;
import com.ifpdos.mcusdk.McuCommandList;
import com.seewo.vcommons.cutomer.CustomerUtils;
import com.seewo.vcommons.data.PreferencesUtils;
import com.seewo.vcommons.data.PropertiesUtils;
import com.seewo.vcommons.helper.StorageHelper;
import com.seewo.vcommons.helper.VStatusHelper;
import com.seewo.vcommons.utils.BoardTypeUtils;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/* JADX INFO: loaded from: classes.dex */
public final class FakePowerOffMode {
    public static final String ACTION_ENABLE_CHANGED = "com.seewo.intent.action.FAKE_POWER_OFF_ENABLE_STATE_CHANGED";
    public static final String ACTION_ENABLE_SWITCH_CHANGED = "com.seewo.intent.action.FAKE_POWER_OFF_ENABLE_SWITCH_STATE_CHANGED";
    public static final String ACTION_REQUEST_ENTER_FAKE_STANDBY_STATE = "com.seewo.intent.action.FAKE_STANDBY_REQ_ENTER";
    public static final String ACTION_SET_LEAVE_STANDBY_TIMER = "com.seewo.intent.action.FAKE_STANDBY_SET_TIMER";
    public static final String ARG_BOOL_IS_CANCEL_COUNT_DOWN = "arg_is_cancel_count_down";
    public static final String ARG_LONG_COUNT_DOWN_MS = "arg_count_down_ms";
    private static final String FAKE_POWER_OFF_MODE_ENABLE = "persist.sys.fpo.enable";
    private static final String PROP_KEY_ENABLED = "persist.sys.etc.fpo_enable";
    private static final String PROP_KEY_ENABLE_SWITCH = "persist.sys.etc.fpo_enable_sw";
    private static final int SHOW = 1;
    private static final String SWITCH_AES_ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String SWITCH_AES_ALGORITHM_SHORT = "AES";
    private static final String SWITCH_AES_FILE_NAME = "FakePowerOff";
    private static final long SWITCH_AES_MAX_LENGTH = 2048;
    private static final String TAG = "FakePowerOffMode";
    private static byte[] SWITCH_AES_KEY = {-34, -83, -66, McuCommandList.MAIN_SEND_MCU_VERSION_NEW, 25, -112, -107, -122, 30, 90, -51, 54, 112, -59, 50, -39};
    private static byte[] SWITCH_AES_IV = {125, 18, 59, -43, -34, -83, -66, McuCommandList.MAIN_SEND_MCU_VERSION_NEW, -65, 54, 78, 9, 2, 37, -107, -122};

    private FakePowerOffMode() {
    }

    private static void sendBroadcast(Context context, String str) {
        if (context != null) {
            context.sendBroadcast(new Intent(str));
        }
    }

    public static void requestEnterFakeStandbyState(Context context) {
        sendBroadcast(context, ACTION_REQUEST_ENTER_FAKE_STANDBY_STATE);
    }

    public static void requestLeaveFakeStandbyState(Context context) {
        if (context != null) {
            VStatusHelper.setBackLightState(context, true);
        }
    }

    public static void requestLeaveFakeStandbyStateDelayed(Context context, long j) {
        if (context == null || !isFeatureIncluded()) {
            return;
        }
        Intent intent = new Intent(ACTION_SET_LEAVE_STANDBY_TIMER);
        if (j < 0) {
            intent.putExtra(ARG_BOOL_IS_CANCEL_COUNT_DOWN, true);
        } else {
            intent.putExtra(ARG_LONG_COUNT_DOWN_MS, TimeUnit.SECONDS.toMillis(j));
        }
        context.sendBroadcast(intent);
    }

    public static void setEnableAllowed(boolean z) {
        PropertiesUtils.setProperty(PROP_KEY_ENABLE_SWITCH, String.valueOf(z));
    }

    public static boolean isEnableAllowed() {
        return PropertiesUtils.getProperty(PROP_KEY_ENABLE_SWITCH, false);
    }

    public static boolean isEnableStateChangeable() {
        return isFeatureIncluded() && (isEnabled() || isEnableAllowed());
    }

    public static boolean isFeatureIncluded() {
        if (isShowFakePowerOffMode() || CustomerUtils.isViewSonicCustomized() || CustomerUtils.isSaharaCustomized() || CustomerUtils.isOptomaCustomized() || CustomerUtils.isSeewoDualCustomized()) {
            return true;
        }
        return CustomerUtils.isSeewoCustomized() && BoardTypeUtils.is972Machine();
    }

    public static boolean isShowFakePowerOffMode() {
        return PropertiesUtils.getProperty(FAKE_POWER_OFF_MODE_ENABLE, 0) == 1;
    }

    public static boolean isInFakePowerOffMode(Context context) {
        return isFeatureIncluded() && PreferencesUtils.getFakeStandby(context);
    }

    public static void setEnabled(Context context, boolean z) {
        if (isEnableStateChangeable()) {
            PropertiesUtils.setProperty(PROP_KEY_ENABLED, String.valueOf(z));
            if (!z) {
                setPowerOffChannel(context, null);
            }
            sendBroadcast(context, ACTION_ENABLE_CHANGED);
        }
    }

    public static boolean isEnabled() {
        return isFeatureIncluded() && PropertiesUtils.getProperty(PROP_KEY_ENABLED, false);
    }

    public static void setPowerOffChannel(Context context, Integer num) {
        if (isEnabled()) {
            PreferencesUtils.setFakePowerOffChannel(context, num);
        }
    }

    public static Integer getPowerOffChannel(Context context) {
        return PreferencesUtils.getFakePowerOffChannel(context);
    }

    private static void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                Log.e(TAG, "closeQuietly", e);
            }
        }
    }

    private static byte[] fileToBytes(File file) throws Throwable {
        FileInputStream fileInputStream;
        long length;
        FileInputStream fileInputStream2 = null;
        try {
            length = file.exists() ? file.length() : 0L;
        } catch (Exception e) {
            e = e;
            fileInputStream = null;
        } catch (Throwable th) {
            th = th;
            closeQuietly(fileInputStream2);
            throw th;
        }
        if (length <= 0 || length > SWITCH_AES_MAX_LENGTH) {
            closeQuietly(null);
            return null;
        }
        byte[] bArr = new byte[(int) length];
        fileInputStream = new FileInputStream(file);
        try {
            try {
                fileInputStream.read(bArr);
                closeQuietly(fileInputStream);
                return bArr;
            } catch (Exception e2) {
                e = e2;
                Log.e(TAG, "getSwitchFileBytes", e);
                closeQuietly(fileInputStream);
                return null;
            }
        } catch (Throwable th2) {
            th = th2;
            fileInputStream2 = fileInputStream;
            closeQuietly(fileInputStream2);
            throw th;
        }
        Log.e(TAG, "getSwitchFileBytes", e);
        closeQuietly(fileInputStream);
        return null;
    }

    private static byte[] doAES(byte[] bArr, int i) {
        try {
            Cipher cipher = Cipher.getInstance(SWITCH_AES_ALGORITHM);
            cipher.init(i, new SecretKeySpec(SWITCH_AES_KEY, SWITCH_AES_ALGORITHM_SHORT), new IvParameterSpec(SWITCH_AES_IV));
            return cipher.doFinal(bArr);
        } catch (Exception e) {
            Log.d(TAG, "doAES", e);
            return null;
        }
    }

    public static void checkEnableSwitchSync(Context context, String str) {
        if (context == null || str == null || !isFeatureIncluded()) {
            return;
        }
        Log.d(TAG, "machineSN: " + str);
        byte[] bArrDoAES = doAES(str.getBytes(), 1);
        boolean z = false;
        for (StorageHelper.StorageItem storageItem : StorageHelper.getInstance().getUsbItems()) {
            z = checkFileKeyVersion1(storageItem.mPath, bArrDoAES) || checkFileKeyVersion2(storageItem.mPath, bArrDoAES);
            if (z) {
                break;
            }
        }
        Log.d(TAG, "Key check result: " + z);
        if (z != isEnableAllowed()) {
            PropertiesUtils.setProperty(PROP_KEY_ENABLE_SWITCH, String.valueOf(z));
            sendBroadcast(context, ACTION_ENABLE_SWITCH_CHANGED);
        }
    }

    private static boolean checkFileKeyVersion1(String str, byte[] bArr) {
        File keyFile = getKeyFile(str, 1);
        if (keyFile.exists()) {
            return Arrays.equals(fileToBytes(keyFile), bArr);
        }
        return false;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r4v0 */
    /* JADX WARN: Type inference failed for: r4v1 */
    /* JADX WARN: Type inference failed for: r4v13 */
    /* JADX WARN: Type inference failed for: r4v14 */
    /* JADX WARN: Type inference failed for: r4v15 */
    /* JADX WARN: Type inference failed for: r4v16 */
    /* JADX WARN: Type inference failed for: r4v2, types: [java.io.Closeable] */
    /* JADX WARN: Type inference failed for: r4v4 */
    /* JADX WARN: Type inference failed for: r4v5 */
    /* JADX WARN: Type inference failed for: r4v9 */
    private static boolean checkFileKeyVersion2(String str, byte[] bArr) throws Throwable {
        FileInputStream fileInputStream;
        File keyFile = getKeyFile(str, 2);
        if (!keyFile.exists()) {
            return false;
        }
        byte[] bArr2 = new byte[2048];
        ?? r4 = 0;
        FileInputStream fileInputStream2 = null;
        try {
            try {
                fileInputStream = new FileInputStream(keyFile);
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception e) {
            e = e;
        }
        try {
            fileInputStream.read(bArr2, 0, 4);
            int iBytesToInt = bytesToInt(bArr2, 0);
            String str2 = TAG;
            Log.d(str2, "Read Key File Version :" + iBytesToInt);
            fileInputStream.read(bArr2, 0, 4);
            int iBytesToInt2 = bytesToInt(bArr2, 0);
            StringBuilder sb = new StringBuilder("Read Key num : ");
            Log.d(str2, sb.append(iBytesToInt2).toString());
            int i = 0;
            ?? r42 = sb;
            while (i < iBytesToInt2) {
                fileInputStream.read(bArr2, 0, 4);
                int iBytesToInt3 = bytesToInt(bArr2, 0);
                fileInputStream.read(bArr2, 0, iBytesToInt3);
                boolean zByteEquals = byteEquals(bArr, bArr2, iBytesToInt3);
                if (zByteEquals) {
                    closeQuietly(fileInputStream);
                    return true;
                }
                i++;
                r42 = zByteEquals;
            }
            closeQuietly(fileInputStream);
            r4 = r42;
        } catch (Exception e2) {
            e = e2;
            fileInputStream2 = fileInputStream;
            Log.e(TAG, "checkFileKeyVersion2", e);
            closeQuietly(fileInputStream2);
            r4 = fileInputStream2;
        } catch (Throwable th2) {
            th = th2;
            r4 = fileInputStream;
            closeQuietly(r4);
            throw th;
        }
        return false;
    }

    private static boolean byteEquals(byte[] bArr, byte[] bArr2, int i) {
        if (i <= 0) {
            return false;
        }
        if (bArr == bArr2) {
            return true;
        }
        if (bArr == null || bArr2 == null || bArr.length < i || bArr2.length < i) {
            return false;
        }
        for (int i2 = 0; i2 < i; i2++) {
            if (bArr[i2] != bArr2[i2]) {
                return false;
            }
        }
        return true;
    }

    private static int bytesToInt(byte[] bArr, int i) {
        return ((bArr[i + 3] << 24) & ViewCompat.MEASURED_STATE_MASK) | (bArr[i] & 255) | ((bArr[i + 1] << 8) & MotionEventCompat.ACTION_POINTER_INDEX_MASK) | ((bArr[i + 2] << 16) & 16711680);
    }

    private static File getKeyFile(String str, int i) {
        return new File(str, SWITCH_AES_FILE_NAME + (i == 1 ? "" : Integer.toString(i)));
    }
}
