1 package org.average.nfcauthcr;
3 import java.io.IOException;
4 import java.util.ArrayList;
6 import android.app.Activity;
7 import android.app.PendingIntent;
8 import android.content.Intent;
9 import android.content.IntentFilter;
10 import android.nfc.NfcAdapter;
11 import android.nfc.Tag;
12 import android.nfc.TagLostException;
13 import android.nfc.tech.IsoDep;
14 import android.util.Log;
15 import android.widget.Toast;
17 import org.average.nfcauthcr.YkNeo;
18 import org.average.nfcauthcr.CRException;
20 public class NfcCRdispatch {
22 private final String TAG = getClass().getName();
24 private Activity activity = null;
25 private PendingIntent tagIntent = null;
26 private ArrayList<String> challenge;
28 NfcCRdispatch(Activity activity) {
29 Log.v(TAG, "new NfcCRdispatch, activity=" + activity);
30 this.activity = activity;
33 public ArrayList<String> onNewIntent(Intent intent) {
34 Log.v(TAG, "NFC Intent arrived");
35 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
36 if (tag == null) return null;
37 IsoDep isoTag = IsoDep.get(tag);
39 int slot = intent.getIntExtra("yubikey_neo_slot", -1);
41 return YkNeo.doChalResp(isoTag, slot, challenge);
42 } catch (TagLostException e) {
43 Log.v(TAG, e.getMessage());
44 Toast.makeText(activity, e.getMessage(),
45 Toast.LENGTH_LONG).show();
46 } catch (IOException e) {
47 Log.v(TAG, e.getMessage());
48 Toast.makeText(activity, e.getMessage(),
49 Toast.LENGTH_LONG).show();
50 } catch (CRException e) {
51 Log.v(TAG, e.getMessage());
52 Toast.makeText(activity, e.getMessage(),
53 Toast.LENGTH_LONG).show();
58 public void onResume(ArrayList<String> challenge) {
59 this.challenge = challenge;
60 Intent intent = activity.getIntent();
61 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
62 tagIntent = PendingIntent.getActivity(activity, 0, intent, 0);
64 new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
66 new String[] {IsoDep.class.getName()};
67 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
68 if (adapter == null) {
69 Toast.makeText(activity, R.string.no_nfc,
70 Toast.LENGTH_LONG).show();
73 if (!adapter.isEnabled()) {
74 Toast.makeText(activity, R.string.nfc_disabled,
75 Toast.LENGTH_LONG).show();
78 adapter.enableForegroundDispatch(activity, tagIntent,
79 new IntentFilter[] {iso},
80 new String[][] {techs});
83 public void onPause() {
84 if (tagIntent != null) {
88 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
89 if (adapter != null) {
90 adapter.disableForegroundDispatch(activity);