]> www.average.org Git - YkNeoCR.git/blobdiff - src/org/average/nfcauthcr/NfcCRdispatch.java
reorg, separate roles
[YkNeoCR.git] / src / org / average / nfcauthcr / NfcCRdispatch.java
diff --git a/src/org/average/nfcauthcr/NfcCRdispatch.java b/src/org/average/nfcauthcr/NfcCRdispatch.java
new file mode 100644 (file)
index 0000000..bfacfa9
--- /dev/null
@@ -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);
+               }
+       }
+}