1 package org.average.nfcauthcr;
3 import android.app.Activity;
4 import android.app.PendingIntent;
5 import android.content.Intent;
6 import android.content.IntentFilter;
7 import android.nfc.NfcAdapter;
8 import android.nfc.Tag;
9 import android.nfc.TagLostException;
10 import android.nfc.tech.IsoDep;
11 import android.util.Log;
12 import android.widget.Toast;
14 import org.average.nfcauthcr.YkNeo;
15 import org.average.nfcauthcr.CRException;
17 public class NfcCRdispatch {
19 private final String TAG = getClass().getName();
21 private Activity activity = null;
22 private PendingIntent tagIntent = null;
23 private byte[] challenge;
25 NfcCRdispatch(Activity activity) {
26 Log.v(TAG, "new NfcCRdispatch, activity=" + activity);
27 this.activity = activity;
30 public byte[] onNewIntent(Intent intent) {
31 Log.v(TAG, "NFC Intent arrived");
32 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
33 if (tag == null) return null;
34 IsoDep isoTag = IsoDep.get(tag);
36 int slot = intent.getIntExtra("yubikey_neo_slot", -1);
37 return YkNeo.doChalResp(isoTag, slot, challenge);
38 } catch (CRException e) {
39 Log.v(TAG, e.getMessage());
40 Toast.makeText(activity, e.getMessage(),
41 Toast.LENGTH_LONG).show();
46 public void onResume(byte[] challenge) {
47 this.challenge = challenge;
48 Intent intent = activity.getIntent();
49 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
50 tagIntent = PendingIntent.getActivity(activity, 0, intent, 0);
52 new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
54 new String[] {IsoDep.class.getName()};
55 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
56 if (adapter == null) {
57 Toast.makeText(activity, R.string.no_nfc,
58 Toast.LENGTH_LONG).show();
61 if (!adapter.isEnabled()) {
62 Toast.makeText(activity, R.string.nfc_disabled,
63 Toast.LENGTH_LONG).show();
66 adapter.enableForegroundDispatch(activity, tagIntent,
67 new IntentFilter[] {iso},
68 new String[][] {techs});
71 public void onPause() {
72 if (tagIntent != null) {
76 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
77 if (adapter != null) {
78 adapter.disableForegroundDispatch(activity);