package com.ifpdos.factorytest;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.NfcA;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.ifpdos.factorytest.udi.NFCData;
import com.ifpdos.factorytest.udi.NFCNotifyApi;
import com.ifpdos.factorytest.udi.OnlyNotifyApiBinder;
import com.ifpdos.factorytest.udi.ProtocolDefineKt;
import com.ifpdos.factorytest.util.StringUtil;
import com.ifpdos.udi.sdk.service.sdk.UdiServiceSdk;
import com.ifpdos.udi.sdk.service.sdk.converter.serializable.SerializableConverter;
import com.seewo.libnfc.HandleNfc;
import com.seewo.libnfc.NfcCallback;
import com.seewo.libnfc.customer.NfcData;
import com.seewo.libnfc.customer.ProwiseData;
import com.seewo.libnfc.model.NfcCard;
import com.seewo.libnfc.tools.HexTools;
import com.seewo.vcommons.constants.SeewoIntent;

/* JADX INFO: loaded from: classes.dex */
public class NfcTestActivity extends Activity implements NfcCallback {
    private static final int ANDROID_VERSION_S = 31;
    private static final int CHECK_DELAY = 3000;
    private static final int FLAG_MUTABLE = 33554432;
    private static int PEDNDING_INTENT_FLAGS = 0;
    private static final String SERVICE_NAME = "com.ifpdos.midfactorytest.udi";
    private static final String TAG = "NFCTools";
    private Runnable mCheckReadRunnable = new Runnable() { // from class: com.ifpdos.factorytest.NfcTestActivity.1
        @Override // java.lang.Runnable
        public void run() {
            if (NfcTestActivity.this.mInIOMode) {
                NfcTestActivity.this.mInIOMode = false;
                NfcTestActivity nfcTestActivity = NfcTestActivity.this;
                nfcTestActivity.setText(nfcTestActivity.getString(R.string.read_fail), StringUtil.RED);
            }
        }
    };
    private HandleNfc mHandleNfc;
    private Handler mHandler;
    private boolean mInIOMode;
    private IntentFilter[] mIntentFilter;
    private boolean mIsFromUdi;
    private boolean mIsWriteMode;
    private TextView mMessageTextView;
    private NfcAdapter mNfcAdapter;
    protected NfcCard mNfcCard;
    private EditText mPasswordEditText;
    private PendingIntent mPendingIntent;
    private String[][] mTechLists;
    private EditText mUsernameEditText;

    @Override // android.app.Activity
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_android_nfc_test);
        initUdi();
        initNfc();
        initView();
        this.mHandler = new Handler();
    }

    private void initUdi() {
        boolean booleanExtra = getIntent().getBooleanExtra(ProtocolDefineKt.KEY_FROM_UDI, false);
        this.mIsFromUdi = booleanExtra;
        if (booleanExtra) {
            new UdiServiceSdk.Builder().setConverter(new SerializableConverter()).build(this).registerApis(SERVICE_NAME, OnlyNotifyApiBinder.INSTANCE.getApiList());
        }
    }

    private void initView() {
        this.mUsernameEditText = (EditText) findViewById(R.id.et_username);
        this.mPasswordEditText = (EditText) findViewById(R.id.et_password);
        this.mMessageTextView = (TextView) findViewById(R.id.message_tv);
    }

    private void initNfc() {
        this.mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (Build.VERSION.SDK_INT >= 31) {
            PEDNDING_INTENT_FLAGS = FLAG_MUTABLE;
        } else {
            PEDNDING_INTENT_FLAGS = 0;
        }
        this.mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(SeewoIntent.DRAWER_STATE_RIGHT), PEDNDING_INTENT_FLAGS);
        IntentFilter intentFilter = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
        intentFilter.addCategory("*/*");
        this.mIntentFilter = new IntentFilter[]{intentFilter};
        this.mTechLists = new String[][]{new String[]{MifareClassic.class.getName()}, new String[]{NfcA.class.getName()}};
        this.mHandleNfc = new HandleNfc(this);
    }

    public void onClickRead(View view) {
        if (this.mInIOMode) {
            return;
        }
        this.mIsWriteMode = false;
        this.mInIOMode = true;
        if (!this.mHandleNfc.readDefault()) {
            setText(getString(R.string.read_fail), StringUtil.RED);
            this.mInIOMode = false;
        } else {
            this.mHandler.postDelayed(this.mCheckReadRunnable, 3000L);
            setText(getString(R.string.reading));
        }
    }

    public void onClickWrite(View view) {
        if (this.mInIOMode || this.mUsernameEditText.getText().toString().isEmpty() || this.mPasswordEditText.getText().toString().isEmpty()) {
            return;
        }
        this.mInIOMode = true;
        this.mIsWriteMode = true;
        setText(getString(R.string.writing));
        this.mHandleNfc.write(new ProwiseData(this.mUsernameEditText.getText().toString(), this.mPasswordEditText.getText().toString()));
    }

    @Override // android.app.Activity
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        this.mHandleNfc.setIntent(intent);
    }

    @Override // android.app.Activity
    protected void onResume() {
        super.onResume();
        NfcAdapter nfcAdapter = this.mNfcAdapter;
        if (nfcAdapter != null) {
            nfcAdapter.enableForegroundDispatch(this, this.mPendingIntent, this.mIntentFilter, this.mTechLists);
        }
    }

    @Override // android.app.Activity
    protected void onPause() {
        super.onPause();
        NfcAdapter nfcAdapter = this.mNfcAdapter;
        if (nfcAdapter != null) {
            nfcAdapter.disableForegroundDispatch(this);
        }
    }

    @Override // com.seewo.libnfc.NfcCallback
    public void onCardGet(NfcCard nfcCard) {
        Log.e(TAG, "onCardGet ,WriteMode " + this.mIsWriteMode);
        if (nfcCard == null) {
            if (this.mIsWriteMode) {
                setText(getString(R.string.write_fail));
            }
        } else {
            this.mNfcCard = nfcCard;
            if (this.mIsWriteMode) {
                setText(getString(R.string.writing));
                this.mHandleNfc.write(new ProwiseData(this.mUsernameEditText.getText().toString(), this.mPasswordEditText.getText().toString()));
            } else {
                onClickRead(null);
            }
        }
    }

    @Override // com.seewo.libnfc.NfcCallback
    public void onDataReaded(NfcData nfcData) {
        this.mHandler.removeCallbacks(this.mCheckReadRunnable);
        Log.e(TAG, "onDataRead");
        this.mInIOMode = false;
        if (nfcData != null && nfcData.getmRawData().length != 0) {
            ProwiseData prowiseData = new ProwiseData(nfcData.getmRawData(), HexTools.toHex(this.mNfcCard.getUid()));
            if (TextUtils.isEmpty(prowiseData.getUserKey())) {
                Log.e(TAG, "UserName or Password empty");
                setText(getString(R.string.read_fail), StringUtil.RED);
                return;
            } else {
                if (this.mIsFromUdi) {
                    NFCNotifyApi.INSTANCE.notify(new NFCData(prowiseData.getUserName(), prowiseData.getUserKey()));
                }
                setText("User: " + prowiseData.getUserName() + "<br/>password: " + prowiseData.getUserKey(), StringUtil.GREEN);
                return;
            }
        }
        Log.e(TAG, "NfcData empty");
        setText(getString(R.string.read_fail), StringUtil.RED);
    }

    @Override // com.seewo.libnfc.NfcCallback
    public void onDataWrited(boolean z) {
        Log.e(TAG, "onDataWrited " + z);
        this.mInIOMode = false;
        this.mIsWriteMode = false;
        if (z) {
            setText(getString(R.string.write_succeed), StringUtil.GREEN);
        } else {
            setText(getString(R.string.write_fail), StringUtil.RED);
        }
    }

    private void setText(final String str) {
        this.mHandler.post(new Runnable() { // from class: com.ifpdos.factorytest.NfcTestActivity.2
            @Override // java.lang.Runnable
            public void run() {
                NfcTestActivity.this.mMessageTextView.setText(str);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void setText(final String str, final String str2) {
        this.mHandler.post(new Runnable() { // from class: com.ifpdos.factorytest.NfcTestActivity.3
            @Override // java.lang.Runnable
            public void run() {
                NfcTestActivity.this.mMessageTextView.setText(Html.fromHtml(StringUtil.setSpannableColor(str, str2)));
            }
        });
    }
}
