package com.epson.mobilephone.common.wifidirect;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;

import org.opencv.videoio.Videoio;

import java.util.Arrays;

import epson.print.MyPrinter;

public class NfcTagUtils {
    private static final String FIXEDTAG_EPS_V1 = "EPS_V1";
    private static final String FIXEDTAG_EPS_V2 = "EPS_V2";
    private static final String ID_CL = "CL";
    private static final String ID_EPSON = "EPSON";
    private static final String ID_MACADDRESS = "MAC_ADDRESS";
    private static final String MIME_TEXTPLAIN = "text/plain";
    private static final String PREFIX_FIXEDTAG = "EPS_V";
    private static final String PREFIX_TAGWITHNTERFACE = "EPS_I";
    private static final String TAG = "NfcTagUtils";
    private static final String TYPE_ANDROID_COM_PKG = "android.com:pkg";
    public static final int VIBE_TIME = 100;

    public static class EpsonNfcConnectInfo implements Parcelable {
        public static final Parcelable.Creator<EpsonNfcConnectInfo> CREATOR = new Parcelable.Creator<EpsonNfcConnectInfo>() {
            public EpsonNfcConnectInfo createFromParcel(Parcel parcel) {
                return new EpsonNfcConnectInfo(parcel);
            }

            public EpsonNfcConnectInfo[] newArray(int i) {
                return new EpsonNfcConnectInfo[i];
            }
        };
        public String aarPackageName;
        public String initialpassword;
        public String ipAddressV4;
        public String ipAddressV4SimpleAP;
        public String macAdress;
        public String macAdressP2P;
        public String password;
        public int printerStatus;
        public String ssid;
        public String tagVersion;

        public int describeContents() {
            return 0;
        }

        public EpsonNfcConnectInfo() {
        }

        private EpsonNfcConnectInfo(Parcel parcel) {
            macAdress = parcel.readString();
            macAdressP2P = parcel.readString();
            ssid = parcel.readString();
            password = parcel.readString();
            initialpassword = parcel.readString();
            aarPackageName = parcel.readString();
            tagVersion = parcel.readString();
            ipAddressV4 = parcel.readString();
            ipAddressV4SimpleAP = parcel.readString();
            printerStatus = parcel.readInt();
        }

        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeString(macAdress);
            parcel.writeString(macAdressP2P);
            parcel.writeString(ssid);
            parcel.writeString(password);
            parcel.writeString(initialpassword);
            parcel.writeString(aarPackageName);
            parcel.writeString(tagVersion);
            parcel.writeString(ipAddressV4);
            parcel.writeString(ipAddressV4SimpleAP);
            parcel.writeInt(printerStatus);
        }

        public boolean isTagWithInterface() {
            String str = tagVersion;
            return str != null && str.startsWith(NfcTagUtils.PREFIX_TAGWITHNTERFACE);
        }

        public boolean hasOwnAAR(Context context) {
            return context.getPackageName().equals(aarPackageName);
        }
    }

    public static boolean isNeedChangePrinter(Context context, String str, EpsonNfcConnectInfo epsonNfcConnectInfo) {
        String macAddressFromPrinterId = NfcMacAddrUtils.getMacAddressFromPrinterId(str);
        if (macAddressFromPrinterId == null) {
            return true;
        }
        if (macAddressFromPrinterId.equals(epsonNfcConnectInfo.macAdress)) {
            MyPrinter curPrinter = MyPrinter.getCurPrinter(context);
            if (3 != curPrinter.getLocation() || curPrinter.getIp().equals(epsonNfcConnectInfo.ipAddressV4)) {
                return false;
            }
            return true;
        }
        String connectInfo = WiFiDirectManager.getConnectInfo(context, WiFiDirectManager.DEVICE_TYPE_PRINTER);
        if (connectInfo != null && connectInfo.equals(epsonNfcConnectInfo.ssid)) {
            String str2 = epsonNfcConnectInfo.ssid;
            if (!isExistSimpleApDisabled(context, str2) && !isExistSimpleApNormal(context, str2)) {
                return true;
            }
            return false;
        }
        return true;
    }

    @SuppressLint({"NewApi"})
    public static void enableForegroundDispatch(Activity activity, IntentFilter[] intentFilterArr, String[][] strArr) {
        NfcAdapter defaultAdapter;
        if (Build.VERSION.SDK_INT >= 14 && (defaultAdapter = NfcAdapter.getDefaultAdapter(activity)) != null) {
            defaultAdapter.enableForegroundDispatch(activity, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Videoio.CAP_INTELPERC_DEPTH_GENERATOR), 0), intentFilterArr, strArr);
        }
    }

    @SuppressLint({"NewApi"})
    public static void disableForegroundDispatch(Activity activity) {
        NfcAdapter defaultAdapter;
        if (Build.VERSION.SDK_INT >= 14 && (defaultAdapter = NfcAdapter.getDefaultAdapter(activity)) != null) {
            defaultAdapter.disableForegroundDispatch(activity);
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:5:0x0010, code lost:
        r4 = (android.nfc.NdefMessage) r4[0];
     */
    @android.annotation.SuppressLint({"NewApi"})
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static com.epson.mobilephone.common.wifidirect.NfcTagUtils.EpsonNfcConnectInfo parseNECTag(android.content.Context r3, android.content.Intent r4) {
        /*
            int r0 = android.os.Build.VERSION.SDK_INT
            r1 = 0
            r2 = 14
            if (r0 < r2) goto L_0x0062
            java.lang.String r0 = "android.nfc.extra.NDEF_MESSAGES"
            android.os.Parcelable[] r4 = r4.getParcelableArrayExtra(r0)
            if (r4 != 0) goto L_0x0010
            return r1
        L_0x0010:
            r0 = 0
            r4 = r4[r0]
            android.nfc.NdefMessage r4 = (android.nfc.NdefMessage) r4
            java.lang.String r0 = "EPSON"
            byte[] r0 = r0.getBytes()
            android.nfc.NdefRecord r0 = findNdefMessage(r0, r4)
            if (r0 != 0) goto L_0x0022
            return r1
        L_0x0022:
            java.lang.String r2 = new java.lang.String
            byte[] r0 = r0.getPayload()
            r2.<init>(r0)
            java.lang.String r0 = "EPS_V"
            boolean r0 = r2.startsWith(r0)
            if (r0 == 0) goto L_0x0038
            com.epson.mobilephone.common.wifidirect.NfcTagUtils$EpsonNfcConnectInfo r0 = parseNECTagFixed(r2, r4)
            goto L_0x0044
        L_0x0038:
            java.lang.String r0 = "EPS_I"
            boolean r0 = r2.startsWith(r0)
            if (r0 == 0) goto L_0x0061
            com.epson.mobilephone.common.wifidirect.NfcTagUtils$EpsonNfcConnectInfo r0 = parseNECTagWithInterface(r2, r4)
        L_0x0044:
            if (r0 != 0) goto L_0x0047
            return r1
        L_0x0047:
            java.lang.String r4 = getAARPackageName(r4)
            r0.aarPackageName = r4
            java.lang.String r4 = "vibrator"
            java.lang.Object r3 = r3.getSystemService(r4)
            android.os.Vibrator r3 = (android.os.Vibrator) r3
            boolean r4 = r3.hasVibrator()
            if (r4 == 0) goto L_0x0060
            r1 = 100
            r3.vibrate(r1)
        L_0x0060:
            return r0
        L_0x0061:
            return r1
        L_0x0062:
            return r1
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.wifidirect.NfcTagUtils.parseNECTag(android.content.Context, android.content.Intent):com.epson.mobilephone.common.wifidirect.NfcTagUtils$EpsonNfcConnectInfo");
    }

    /* JADX WARNING: Removed duplicated region for block: B:19:0x0083  */
    /* JADX WARNING: Removed duplicated region for block: B:20:0x008c  */
    /* JADX WARNING: Removed duplicated region for block: B:23:0x009a  */
    /* JADX WARNING: Removed duplicated region for block: B:24:0x00a5  */
    @android.annotation.SuppressLint({"NewApi"})
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static com.epson.mobilephone.common.wifidirect.NfcTagUtils.EpsonNfcConnectInfo parseNECTagFixed(java.lang.String r5, android.nfc.NdefMessage r6) {
        /*
            java.lang.String r0 = "MAC_ADDRESS"
            byte[] r0 = r0.getBytes()
            android.nfc.NdefRecord r0 = findNdefMessage(r0, r6)
            r1 = 0
            if (r0 != 0) goto L_0x000e
            return r1
        L_0x000e:
            short r2 = r0.getTnf()
            r3 = 2
            if (r3 != r2) goto L_0x00bb
            java.lang.String r2 = "text/plain"
            byte[] r2 = r2.getBytes()
            byte[] r4 = r0.getType()
            boolean r2 = java.util.Arrays.equals(r2, r4)
            if (r2 != 0) goto L_0x0027
            goto L_0x00bb
        L_0x0027:
            java.lang.String r2 = new java.lang.String
            byte[] r0 = r0.getPayload()
            r2.<init>(r0)
            java.lang.String r0 = "CL"
            byte[] r0 = r0.getBytes()
            android.nfc.NdefRecord r6 = findNdefMessage(r0, r6)
            r0 = 1
            if (r6 == 0) goto L_0x0065
            short r4 = r6.getTnf()
            if (r3 != r4) goto L_0x0065
            java.lang.String r3 = "text/plain"
            byte[] r3 = r3.getBytes()
            byte[] r4 = r6.getType()
            boolean r3 = java.util.Arrays.equals(r3, r4)
            if (r3 != r0) goto L_0x0065
            java.lang.String r3 = new java.lang.String     // Catch:{ NumberFormatException -> 0x0065 }
            byte[] r6 = r6.getPayload()     // Catch:{ NumberFormatException -> 0x0065 }
            r3.<init>(r6)     // Catch:{ NumberFormatException -> 0x0065 }
            java.lang.Integer r6 = java.lang.Integer.valueOf(r3)     // Catch:{ NumberFormatException -> 0x0065 }
            int r6 = r6.intValue()     // Catch:{ NumberFormatException -> 0x0065 }
            goto L_0x0066
        L_0x0065:
            r6 = 0
        L_0x0066:
            com.epson.mobilephone.common.wifidirect.NfcTagUtils$EpsonNfcConnectInfo r3 = new com.epson.mobilephone.common.wifidirect.NfcTagUtils$EpsonNfcConnectInfo
            r3.<init>()
            r3.tagVersion = r5
            java.util.Locale r4 = java.util.Locale.US
            java.lang.String r4 = r2.toUpperCase(r4)
            r3.macAdress = r4
            java.lang.String r4 = com.epson.mobilephone.common.wifidirect.NfcMacAddrUtils.getMacAddressP2P(r2)
            r3.macAdressP2P = r4
            java.lang.String r4 = com.epson.mobilephone.common.wifidirect.NfcMacAddrUtils.getSSIDFromMacAddress(r2)
            r3.ssid = r4
            if (r6 < r0) goto L_0x008c
            char[] r6 = com.epson.mobilephone.common.wifidirect.NfcMacAddrUtils.acCharacterTblPassphraseCL1
            java.lang.String r6 = com.epson.mobilephone.common.wifidirect.NfcMacAddrUtils.getPassFormMacAddress(r2, r6)
            r3.password = r6
            goto L_0x0094
        L_0x008c:
            char[] r6 = com.epson.mobilephone.common.wifidirect.NfcMacAddrUtils.acCharacterTblPassphrase
            java.lang.String r6 = com.epson.mobilephone.common.wifidirect.NfcMacAddrUtils.getPassFormMacAddress(r2, r6)
            r3.password = r6
        L_0x0094:
            boolean r6 = IsCraigDempsey(r2)
            if (r6 != r0) goto L_0x00a5
            java.lang.String r5 = "000048D400E6"
            char[] r6 = com.epson.mobilephone.common.wifidirect.NfcMacAddrUtils.acCharacterTblPassphraseCL1
            java.lang.String r5 = com.epson.mobilephone.common.wifidirect.NfcMacAddrUtils.getPassFormMacAddress(r5, r6)
            r3.initialpassword = r5
            goto L_0x00ba
        L_0x00a5:
            java.lang.String r6 = "EPS_V1"
            boolean r5 = r5.equals(r6)
            if (r5 != r0) goto L_0x00b8
            java.lang.String r5 = "000048D400E6"
            char[] r6 = com.epson.mobilephone.common.wifidirect.NfcMacAddrUtils.acCharacterTblPassphrase
            java.lang.String r5 = com.epson.mobilephone.common.wifidirect.NfcMacAddrUtils.getPassFormMacAddress(r5, r6)
            r3.initialpassword = r5
            goto L_0x00ba
        L_0x00b8:
            r3.initialpassword = r1
        L_0x00ba:
            return r3
        L_0x00bb:
            return r1
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.wifidirect.NfcTagUtils.parseNECTagFixed(java.lang.String, android.nfc.NdefMessage):com.epson.mobilephone.common.wifidirect.NfcTagUtils$EpsonNfcConnectInfo");
    }

    @SuppressLint({"NewApi"})
    public static EpsonNfcConnectInfo parseNECTagWithInterface(String str, NdefMessage ndefMessage) {
        NdefRecord[] records = ndefMessage.getRecords();
        if (records.length <= 1) {
            return null;
        }
        byte[] payload = records[1].getPayload();
        NfcTagParser nfcTagParser = new NfcTagParser();
        if (!nfcTagParser.parseTag(payload)) {
            return null;
        }
        EpsonNfcConnectInfo epsonNfcConnectInfo = new EpsonNfcConnectInfo();
        epsonNfcConnectInfo.tagVersion = str;
        epsonNfcConnectInfo.macAdress = nfcTagParser.getMacAddress();
        if (epsonNfcConnectInfo.macAdress != null) {
            epsonNfcConnectInfo.macAdressP2P = NfcMacAddrUtils.getMacAddressP2P(epsonNfcConnectInfo.macAdress);
        }
        epsonNfcConnectInfo.ssid = nfcTagParser.getSSID();
        epsonNfcConnectInfo.password = nfcTagParser.getPassWord();
        epsonNfcConnectInfo.ipAddressV4 = nfcTagParser.getIPAddressV4();
        epsonNfcConnectInfo.ipAddressV4SimpleAP = nfcTagParser.getIPAddressV4SimpleAP();
        epsonNfcConnectInfo.printerStatus = nfcTagParser.getPrinterStatus();
        return epsonNfcConnectInfo;
    }

    @SuppressLint({"NewApi"})
    public static NdefRecord findNdefMessage(byte[] bArr, NdefMessage ndefMessage) {
        for (NdefRecord ndefRecord : ndefMessage.getRecords()) {
            if (Arrays.equals(bArr, ndefRecord.getId())) {
                return ndefRecord;
            }
        }
        return null;
    }

    @SuppressLint({"NewApi"})
    public static String getAARPackageName(NdefMessage ndefMessage) {
        for (NdefRecord ndefRecord : ndefMessage.getRecords()) {
            if (4 == ndefRecord.getTnf() && Arrays.equals(TYPE_ANDROID_COM_PKG.getBytes(), ndefRecord.getType())) {
                return new String(ndefRecord.getPayload());
            }
        }
        return null;
    }

    public static boolean IsCraigDempsey(String str) {
        if (str == null || str.length() != 12) {
            return false;
        }
        byte[] macAdressBytes = NfcMacAddrUtils.getMacAdressBytes(str);
        if (!str.startsWith("9CAED3")) {
            return false;
        }
        long j = (((long) macAdressBytes[5]) & 255) + ((((long) macAdressBytes[4]) & 255) << 8) + ((255 & ((long) macAdressBytes[3])) << 16);
        if (11960320 > j || 13836287 < j) {
            return false;
        }
        return true;
    }

    public static boolean isExistSimpleApDisabled(Context context, String str) {
        EPLog.d(TAG, "isExistSimpleApDisabled()");
        return WiFiUtils.getInstance(context).getExistSimpleApDisabled(str) != -1;
    }

    public static boolean isExistSimpleApNormal(Context context, String str) {
        EPLog.d(TAG, "isExistSimpleApNormal()");
        return WiFiUtils.getInstance(context).getNetworkId(str) != -1;
    }
}
