package com.seewo.libnfc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.MifareUltralight;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcelable;
import android.util.Log;
import com.alibaba.fastjson.asm.Opcodes;
import com.seewo.libnfc.customer.BenQData;
import com.seewo.libnfc.customer.NfcData;
import com.seewo.libnfc.customer.ProwiseData;
import com.seewo.libnfc.customer.SaharaData;
import com.seewo.libnfc.mifare.MifareCallback;
import com.seewo.libnfc.mifare.MifareFactory;
import com.seewo.libnfc.mifare.MifareHelper;
import com.seewo.libnfc.model.NfcCard;
import com.seewo.libnfc.tools.HexTools;
import com.seewo.libnfc.tools.NfcAdapterInsider;

/* JADX INFO: loaded from: classes.dex */
public class HandleNfc implements NfcBase {
    private static final long ENABLE_DELAY = 5000;
    public static final String TAG = "LibNFC";
    private static boolean sIsRebootNFC = false;
    private Context mContext;
    private IntentFilter mIntentFilter;
    private MifareHelper mMifareHelper;
    private NfcAdapter mNfcAdapter;
    private NfcCallback mNfcCallback;
    private NfcCard mNfcCard;
    private Parcelable mParcelable;
    private Tag mTag;
    private byte[] encryptData = {0};
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private MifareCallback mMifareCallback = new MifareCallback() { // from class: com.seewo.libnfc.HandleNfc.1
        @Override // com.seewo.libnfc.mifare.MifareCallback
        public void onWrite(boolean z) {
            Log.v("TouchMenu", "MifareCallback onWrite:" + z);
            if (HandleNfc.this.mNfcCallback != null) {
                HandleNfc.this.mNfcCallback.onDataWrited(z);
            }
        }

        @Override // com.seewo.libnfc.mifare.MifareCallback
        public void onDataRead(NfcData nfcData) {
            if (HandleNfc.this.mNfcCallback != null) {
                HandleNfc.this.mNfcCallback.onDataReaded(nfcData);
            }
        }
    };
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() { // from class: com.seewo.libnfc.HandleNfc.2
        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            if ("android.nfc.action.ADAPTER_STATE_CHANGED".equals(intent.getAction())) {
                HandleNfc.this.handleNfcStateChanged(intent.getIntExtra("android.nfc.extra.ADAPTER_STATE", 1));
            }
        }
    };
    private Runnable mEnableNFCRunnable = new Runnable() { // from class: com.seewo.libnfc.HandleNfc.3
        @Override // java.lang.Runnable
        public void run() {
            HandleNfc.this.enableNFC();
            boolean unused = HandleNfc.sIsRebootNFC = false;
        }
    };

    public HandleNfc(NfcCallback nfcCallback) {
        this.mNfcCallback = nfcCallback;
    }

    public void setIntent(Intent intent) {
        resolveIntent(intent);
    }

    public void setContext(Context context) {
        this.mContext = context;
        if (this.mNfcAdapter == null) {
            this.mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
        }
    }

    private void startReceive() {
        if (this.mContext == null || this.mIntentFilter != null) {
            return;
        }
        IntentFilter intentFilter = new IntentFilter("android.nfc.action.ADAPTER_STATE_CHANGED");
        this.mIntentFilter = intentFilter;
        this.mContext.registerReceiver(this.mReceiver, intentFilter);
    }

    private void stopReceive() {
        Log.v(TAG, "stopReceive!enable");
        enableNFC();
        Context context = this.mContext;
        if (context != null && this.mIntentFilter != null) {
            context.unregisterReceiver(this.mReceiver);
            this.mIntentFilter = null;
        }
        this.mHandler.removeCallbacks(this.mEnableNFCRunnable);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void enableNFC() {
        NfcAdapter nfcAdapter = this.mNfcAdapter;
        if (nfcAdapter == null || nfcAdapter.isEnabled()) {
            return;
        }
        NfcAdapterInsider.enable(this.mNfcAdapter);
    }

    private void resolveIntent(Intent intent) {
        String action = intent.getAction();
        if ("android.nfc.action.TAG_DISCOVERED".equals(action) || "android.nfc.action.TECH_DISCOVERED".equals(action) || "android.nfc.action.NDEF_DISCOVERED".equals(action)) {
            resolveParcelable(intent.getParcelableExtra("android.nfc.extra.TAG"));
            NfcCallback nfcCallback = this.mNfcCallback;
            if (nfcCallback != null) {
                nfcCallback.onCardGet(this.mNfcCard);
            }
        }
    }

    public void rebootNFC() {
        if (sIsRebootNFC) {
            return;
        }
        startReceive();
        if (this.mNfcAdapter != null) {
            Log.v(TAG, "disable!");
            sIsRebootNFC = true;
            NfcAdapterInsider.disable(this.mNfcAdapter);
            this.mHandler.postDelayed(this.mEnableNFCRunnable, ENABLE_DELAY);
        }
    }

    private void resolveParcelable(Parcelable parcelable) {
        this.mParcelable = parcelable;
        NfcCard card = getCard(parcelable);
        this.mNfcCard = card;
        if (card.getType() == null) {
            Log.v(TAG, "Unknow NFC card!");
            if (sIsRebootNFC) {
                return;
            }
            startReceive();
            if (this.mNfcAdapter != null) {
                Log.v(TAG, "disable!");
                sIsRebootNFC = true;
                NfcAdapterInsider.disable(this.mNfcAdapter);
                this.mHandler.postDelayed(this.mEnableNFCRunnable, ENABLE_DELAY);
                return;
            }
            return;
        }
        MifareHelper modual = MifareFactory.getModual(this.mNfcCard.getType(), this.mTag);
        this.mMifareHelper = modual;
        if (modual == null) {
            return;
        }
        modual.setResultCallback(this.mMifareCallback);
    }

    private NfcCard getCard(Parcelable parcelable) {
        Tag tag = (Tag) parcelable;
        this.mTag = tag;
        byte[] id = tag.getId();
        String[] techList = tag.getTechList();
        int length = techList.length;
        int i = 0;
        int i2 = 0;
        while (true) {
            NfcCard.MifareTypes mifareTypes = null;
            if (i2 < length) {
                String str = techList[i2];
                if (str.equals(MifareClassic.class.getName())) {
                    MifareClassic mifareClassic = MifareClassic.get(tag);
                    int type = mifareClassic.getType();
                    if (type == 0) {
                        mifareTypes = NfcCard.MifareTypes.Mifare_Classic;
                    } else if (type == 1) {
                        mifareTypes = NfcCard.MifareTypes.Mifare_Plus_SL1;
                    } else if (type == 2) {
                        mifareTypes = NfcCard.MifareTypes.Mifare_Pro;
                    }
                    return new NfcCard(id, mifareClassic.getBlockCount() * 16, mifareTypes);
                }
                if (str.equals(MifareUltralight.class.getName())) {
                    int type2 = MifareUltralight.get(tag).getType();
                    if (type2 == 1) {
                        mifareTypes = NfcCard.MifareTypes.Mifare_Utralight;
                        i = 512;
                    } else if (type2 == 2) {
                        mifareTypes = NfcCard.MifareTypes.Mifare_Utralight_C;
                        i = Opcodes.CHECKCAST;
                    }
                    return new NfcCard(id, i, mifareTypes);
                }
                str.equals(IsoDep.class.getName());
                i2++;
            } else {
                return new NfcCard(id, 0, null);
            }
        }
    }

    public boolean readDefault() {
        MifareHelper mifareHelper;
        if (this.mNfcCard == null || (mifareHelper = this.mMifareHelper) == null) {
            NfcCallback nfcCallback = this.mNfcCallback;
            if (nfcCallback == null) {
                return false;
            }
            nfcCallback.onCardGet(null);
            return false;
        }
        mifareHelper.readData();
        return true;
    }

    @Override // com.seewo.libnfc.NfcBase
    public boolean write(NfcData nfcData) {
        if (this.mNfcCard == null || this.mMifareHelper == null) {
            NfcCallback nfcCallback = this.mNfcCallback;
            if (nfcCallback == null) {
                return false;
            }
            nfcCallback.onCardGet(null);
            return false;
        }
        int i = AnonymousClass4.$SwitchMap$com$seewo$libnfc$customer$NfcData$Customers[nfcData.getCustomer().ordinal()];
        if (i == 1) {
            this.encryptData = ((ProwiseData) nfcData).getCipherData(HexTools.toHex(this.mNfcCard.getUid()));
        } else if (i == 2) {
            this.encryptData = ((BenQData) nfcData).getCipherData(null);
        } else if (i == 3) {
            this.encryptData = ((SaharaData) nfcData).getCipherData(HexTools.toHex(this.mNfcCard.getUid()));
        }
        Log.e("xxxxx", "encryptData:" + this.encryptData + ";Data:" + nfcData.toString());
        this.mMifareHelper.writeData(this.encryptData, nfcData.getCustomer());
        return true;
    }

    /* JADX INFO: renamed from: com.seewo.libnfc.HandleNfc$4, reason: invalid class name */
    static /* synthetic */ class AnonymousClass4 {
        static final /* synthetic */ int[] $SwitchMap$com$seewo$libnfc$customer$NfcData$Customers;

        static {
            int[] iArr = new int[NfcData.Customers.values().length];
            $SwitchMap$com$seewo$libnfc$customer$NfcData$Customers = iArr;
            try {
                iArr[NfcData.Customers.PROWISE.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$com$seewo$libnfc$customer$NfcData$Customers[NfcData.Customers.BENQ.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$com$seewo$libnfc$customer$NfcData$Customers[NfcData.Customers.SAHARA.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void handleNfcStateChanged(int i) {
        if (i == 1) {
            Log.v(TAG, "STATE_OFF:enableNFC");
            enableNFC();
        } else {
            if (i != 3) {
                return;
            }
            Log.v(TAG, "STATE_ON!");
            stopReceive();
            sIsRebootNFC = false;
        }
    }

    public void setParcelable(Parcelable parcelable) {
        resolveParcelable(parcelable);
    }

    public Parcelable getParcelable() {
        return this.mParcelable;
    }
}
