X-Git-Url: http://www.average.org/gitweb/?p=YkNeoCR.git;a=blobdiff_plain;f=src%2Forg%2Faverage%2Fnfcauthcr%2FNfcCRdispatch.java;fp=src%2Forg%2Faverage%2Fnfcauthcr%2FNfcCRdispatch.java;h=bfacfa988a257a214ee0c09fcec052069e7762bf;hp=0000000000000000000000000000000000000000;hb=08b05b67a65e78e80fb0fffec8855a69a72ebff9;hpb=9194bb54ed7bfd3ccb2ee93151fb3b1467cdae35 diff --git a/src/org/average/nfcauthcr/NfcCRdispatch.java b/src/org/average/nfcauthcr/NfcCRdispatch.java new file mode 100644 index 0000000..bfacfa9 --- /dev/null +++ b/src/org/average/nfcauthcr/NfcCRdispatch.java @@ -0,0 +1,81 @@ +package org.average.nfcauthcr; + +import android.app.Activity; +import android.app.PendingIntent; +import android.content.Intent; +import android.content.IntentFilter; +import android.nfc.NfcAdapter; +import android.nfc.Tag; +import android.nfc.TagLostException; +import android.nfc.tech.IsoDep; +import android.util.Log; +import android.widget.Toast; + +import org.average.nfcauthcr.YkNeo; +import org.average.nfcauthcr.CRException; + +public class NfcCRdispatch { + + private final String TAG = getClass().getName(); + + private Activity activity = null; + private PendingIntent tagIntent = null; + private byte[] challenge; + + NfcCRdispatch(Activity activity) { + Log.v(TAG, "new NfcCRdispatch, activity=" + activity); + this.activity = activity; + } + + public byte[] onNewIntent(Intent intent) { + Log.v(TAG, "NFC Intent arrived"); + Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); + if (tag == null) return null; + IsoDep isoTag = IsoDep.get(tag); + try { + int slot = intent.getIntExtra("yubikey_neo_slot", -1); + return YkNeo.doChalResp(isoTag, slot, challenge); + } catch (CRException e) { + Log.v(TAG, e.getMessage()); + Toast.makeText(activity, e.getMessage(), + Toast.LENGTH_LONG).show(); + return null; + } + } + + public void onResume(byte[] challenge) { + this.challenge = challenge; + Intent intent = activity.getIntent(); + intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + tagIntent = PendingIntent.getActivity(activity, 0, intent, 0); + IntentFilter iso = + new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); + String[] techs = + new String[] {IsoDep.class.getName()}; + NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity); + if (adapter == null) { + Toast.makeText(activity, R.string.no_nfc, + Toast.LENGTH_LONG).show(); + return; + } + if (!adapter.isEnabled()) { + Toast.makeText(activity, R.string.nfc_disabled, + Toast.LENGTH_LONG).show(); + return; + } + adapter.enableForegroundDispatch(activity, tagIntent, + new IntentFilter[] {iso}, + new String[][] {techs}); + } + + public void onPause() { + if (tagIntent != null) { + tagIntent.cancel(); + tagIntent = null; + } + NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity); + if (adapter != null) { + adapter.disableForegroundDispatch(activity); + } + } +}