d5342f018fce227d7f2e91ba85f332a46911c1bb
[YkNeoCR.git] / src / org / average / nfcauthcr / NfcCRdispatch.java
1 package org.average.ykneocr;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5
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;
16
17 import org.average.ykneocr.YkNeo;
18 import org.average.ykneocr.CRException;
19
20 public class NfcCRdispatch {
21
22         private final String TAG = getClass().getName();
23
24         private Activity activity = null;
25         private PendingIntent tagIntent = null;
26         private ArrayList<String> challenge;
27
28         NfcCRdispatch(Activity activity) {
29                 Log.v(TAG, "new NfcCRdispatch, activity=" + activity);
30                 this.activity = activity;
31         }
32
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);
38                 try {
39                         int slot = intent.getIntExtra("yubikey_neo_slot", -1);
40                         isoTag.connect();
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();
54                 }
55                 return null;
56         }
57
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);
63                 IntentFilter iso =
64                         new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
65                 String[] techs =
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();
71                         return;
72                 }
73                 if (!adapter.isEnabled()) {
74                         Toast.makeText(activity, R.string.nfc_disabled,
75                                                 Toast.LENGTH_LONG).show();
76                         return;
77                 }
78                 adapter.enableForegroundDispatch(activity, tagIntent,
79                                         new IntentFilter[] {iso},
80                                         new String[][] {techs});
81         }
82
83         public void onPause() {
84                 if (tagIntent != null) {
85                         tagIntent.cancel();
86                         tagIntent = null;
87                 }
88                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
89                 if (adapter != null) {
90                         adapter.disableForegroundDispatch(activity);
91                 }
92         }
93 }