package com.seewo.vcommons.mode;

import a.b.c.a.a;
import android.content.Context;
import android.content.Intent;
import android.os.SystemProperties;
import android.util.Log;
import com.seewo.libmcuservice.constant.IMcuConstants;
import com.seewo.vcommons.constants.SeewoBoardKeys;
import com.seewo.vcommons.cutomer.CustomerUtils;
import com.seewo.vcommons.data.PreferencesUtils;
import com.seewo.vcommons.helper.StorageHelper;
import com.seewo.vcommons.helper.VStatusHelper;
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 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 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, -17, SeewoBoardKeys.TOUCH_KEY_BOARD_V, -112, -107, -122, SeewoBoardKeys.TOUCH_KEY_BOARD_1, 90, -51, SeewoBoardKeys.TOUCH_KEY_BOARD_COMMA, 112, -59, 50, -39};
    private static byte[] SWITCH_AES_IV = {125, SeewoBoardKeys.TOUCH_KEY_BOARD_O, 59, -43, -34, -83, -66, -17, -65, SeewoBoardKeys.TOUCH_KEY_BOARD_COMMA, SeewoBoardKeys.TOUCH_KEY_BOARD_PAGE_DOWN, 9, 2, SeewoBoardKeys.TOUCH_KEY_BOARD_8, -107, -122};

    private FakePowerOffMode() {
    }

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

    private static int bytesToInt(byte[] bArr, int i2) {
        return ((bArr[i2 + 3] << SeewoBoardKeys.TOUCH_KEY_BOARD_U) & (-16777216)) | (bArr[i2] & IMcuConstants.DEFAULT_VALUE) | ((bArr[i2 + 1] << 8) & 65280) | ((bArr[i2 + 2] << SeewoBoardKeys.TOUCH_KEY_BOARD_M) & 16711680);
    }

    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()) {
            SystemProperties.set(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: r0v13 */
    /* JADX WARN: Type inference failed for: r0v14 */
    /* JADX WARN: Type inference failed for: r0v2 */
    /* JADX WARN: Type inference failed for: r0v3 */
    /* JADX WARN: Type inference failed for: r0v4, types: [java.io.Closeable] */
    /* JADX WARN: Type inference failed for: r0v6 */
    /* JADX WARN: Type inference failed for: r0v7 */
    private static boolean checkFileKeyVersion2(String str, byte[] bArr) throws Throwable {
        FileInputStream fileInputStream;
        File keyFile = getKeyFile(str, 2);
        if (!keyFile.exists()) {
            return false;
        }
        ?? r0 = 0;
        FileInputStream fileInputStream2 = null;
        byte[] bArr2 = new byte[2048];
        try {
            try {
                fileInputStream = new FileInputStream(keyFile);
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception e2) {
            e = e2;
        }
        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);
            Log.d(str2, "Read Key num : " + iBytesToInt2);
            for (int i2 = 0; i2 < iBytesToInt2; i2++) {
                fileInputStream.read(bArr2, 0, 4);
                int iBytesToInt3 = bytesToInt(bArr2, 0);
                fileInputStream.read(bArr2, 0, iBytesToInt3);
                if (byteEquals(bArr, bArr2, iBytesToInt3)) {
                    closeQuietly(fileInputStream);
                    return true;
                }
            }
            closeQuietly(fileInputStream);
            r0 = iBytesToInt2;
        } catch (Exception e3) {
            e = e3;
            fileInputStream2 = fileInputStream;
            Log.e(TAG, "checkFileKeyVersion2", e);
            closeQuietly(fileInputStream2);
            r0 = fileInputStream2;
        } catch (Throwable th2) {
            th = th2;
            r0 = fileInputStream;
            closeQuietly(r0);
            throw th;
        }
        return false;
    }

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

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

    private static byte[] fileToBytes(File file) throws Throwable {
        FileInputStream fileInputStream;
        long length;
        FileInputStream fileInputStream2 = null;
        try {
            length = file.exists() ? file.length() : 0L;
        } catch (Exception e2) {
            e = e2;
            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 e3) {
                e = e3;
                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 File getKeyFile(String str, int i2) {
        return new File(str, a.p(SWITCH_AES_FILE_NAME, i2 == 1 ? "" : Integer.toString(i2)));
    }

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

    public static boolean isEnableAllowed() {
        return SystemProperties.getBoolean(PROP_KEY_ENABLE_SWITCH, false);
    }

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

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

    public static boolean isFeatureIncluded() {
        return CustomerUtils.isViewSonicCustomized() || CustomerUtils.isSaharaCustomized() || CustomerUtils.isOptomaCustomized();
    }

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

    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 j2) {
        if (context == null || !isFeatureIncluded()) {
            return;
        }
        Intent intent = new Intent(ACTION_SET_LEAVE_STANDBY_TIMER);
        if (j2 < 0) {
            intent.putExtra(ARG_BOOL_IS_CANCEL_COUNT_DOWN, true);
        } else {
            intent.putExtra(ARG_LONG_COUNT_DOWN_MS, TimeUnit.SECONDS.toMillis(j2));
        }
        context.sendBroadcast(intent);
    }

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

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

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

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