]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/NfcCRdispatch.java
reorg, separate roles
[YkNeoCR.git] / src / org / average / nfcauthcr / NfcCRdispatch.java
1 package org.average.nfcauthcr;
2
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;
13
14 import org.average.nfcauthcr.YkNeo;
15 import org.average.nfcauthcr.CRException;
16
17 public class NfcCRdispatch {
18
19         private final String TAG = getClass().getName();
20
21         private Activity activity = null;
22         private PendingIntent tagIntent = null;
23         private byte[] challenge;
24
25         NfcCRdispatch(Activity activity) {
26                 Log.v(TAG, "new NfcCRdispatch, activity=" + activity);
27                 this.activity = activity;
28         }
29
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);
35                 try {
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();
42                         return null;
43                 }
44         }
45
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);
51                 IntentFilter iso =
52                         new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
53                 String[] techs =
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();
59                         return;
60                 }
61                 if (!adapter.isEnabled()) {
62                         Toast.makeText(activity, R.string.nfc_disabled,
63                                                 Toast.LENGTH_LONG).show();
64                         return;
65                 }
66                 adapter.enableForegroundDispatch(activity, tagIntent,
67                                         new IntentFilter[] {iso},
68                                         new String[][] {techs});
69         }
70
71         public void onPause() {
72                 if (tagIntent != null) {
73                         tagIntent.cancel();
74                         tagIntent = null;
75                 }
76                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
77                 if (adapter != null) {
78                         adapter.disableForegroundDispatch(activity);
79                 }
80         }
81 }