]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/NFCAuthCRCheck.java
got enrollment work
[YkNeoCR.git] / src / org / average / nfcauthcr / NFCAuthCRCheck.java
1 package org.average.nfcauthcr;
2
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.app.PendingIntent;
6 import android.content.DialogInterface;
7 import android.content.Intent;
8 import android.content.IntentFilter;
9 import android.nfc.NfcAdapter;
10 import android.nfc.Tag;
11 import android.nfc.TagLostException;
12 import android.nfc.tech.IsoDep;
13 import android.util.Log;
14 import android.widget.Toast;
15
16 import org.average.nfcauthcr.NFCAuthCRYubiNeo;
17
18 public class NFCAuthCRCheck extends Activity {
19
20         private final String TAG = getClass().getName();
21
22         private AlertDialog swipeDialog;
23         private PendingIntent tagIntent;
24
25         @Override
26         protected void onResume() {
27                 super.onResume();
28                 Log.v(TAG, "Starting the work");
29
30                 Intent intent = getIntent();
31                 setResult(RESULT_CANCELED);
32                 if (swipeDialog != null) {
33                         swipeDialog.dismiss();
34                         swipeDialog = null;
35                 }
36                 int slot = intent.getIntExtra("slot", -1);
37                 if (slot > 0) {
38                         swipeDialog = makeDialog();
39                         swipeDialog.show();
40                         enableDispatch(slot);
41                 }
42         }
43
44         @Override
45         protected void onPause() {
46                 super.onPause();
47                 Log.v(TAG, "Finished the work");
48
49                 if(swipeDialog != null) {
50                         swipeDialog.dismiss();
51                         swipeDialog = null;
52                 }
53                 disableDispatch();
54         }
55
56         public void onNewIntent(Intent intent) {
57                 Log.v(TAG, "NFC Intent arrived");
58                 int slot = intent.getIntExtra("slot", -1);
59                 byte[] challenge = intent.getByteArrayExtra("challenge");
60                 if (slot <= 0) return;
61                 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
62                 if (tag == null) return;
63                 IsoDep isoTag = IsoDep.get(tag);
64                 byte[] response = NFCAuthCRYubiNeo.doChallengeYubiKey(
65                                                 isoTag, slot, challenge);
66                 if (response != null) {
67                         Intent data = getIntent();
68                         data.putExtra("response", response);
69                         setResult(RESULT_OK, data);
70                 }
71                 finish();
72         }
73
74         private AlertDialog makeDialog() {
75                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
76                 builder.setTitle(R.string.challenging);
77                 builder.setMessage(R.string.swipe);
78                 builder.setOnCancelListener(
79                                 new DialogInterface.OnCancelListener() {
80                         public void onCancel(DialogInterface dialog) {
81                                 finish();
82                         }
83                 });
84                 return builder.create();
85         }
86
87         private void enableDispatch(int slot) {
88                 Intent intent = getIntent();
89                 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
90                 intent.putExtra("slot", slot);
91                 tagIntent = PendingIntent.getActivity(this, 0, intent, 0);
92                 IntentFilter iso =
93                         new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
94                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
95                 if (adapter == null) {
96                         Toast.makeText(this, R.string.no_nfc,
97                                                 Toast.LENGTH_LONG).show();
98                         finish();
99                         return;
100                 }
101                 if (! adapter.isEnabled()) {
102                         Toast.makeText(this, R.string.nfc_disabled,
103                                                 Toast.LENGTH_LONG).show();
104                         finish();
105                         return;
106                 }
107                 adapter.enableForegroundDispatch(
108                         this, tagIntent, new IntentFilter[] {iso},
109                         new String[][] {new String[] {IsoDep.class.getName()}});
110         }
111
112         private void disableDispatch() {
113                 if (tagIntent != null) {
114                         tagIntent.cancel();
115                         tagIntent = null;
116                 }
117                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
118                 if (adapter != null) {
119                         adapter.disableForegroundDispatch(this);
120                 }
121         }
122 }