]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/NFCAuthCRCheck.java
6637c00164221415b39f0a8c99624c23ef6341bd
[YkNeoCR.git] / src / org / average / nfcauthcr / NFCAuthCRCheck.java
1 package org.average.nfcauthcr;
2
3 import java.io.IOException;
4
5 import android.os.Bundle;
6 import android.app.Activity;
7 import android.app.AlertDialog;
8 import android.app.PendingIntent;
9 import android.content.DialogInterface;
10 import android.content.Intent;
11 import android.content.IntentFilter;
12 import android.nfc.NfcAdapter;
13 import android.nfc.Tag;
14 import android.nfc.TagLostException;
15 import android.nfc.tech.IsoDep;
16 import android.util.Log;
17 import android.widget.Toast;
18
19 public class NFCAuthCRCheck extends Activity {
20
21         private final String TAG = getClass().getName();
22
23         private static final byte[] selectCommand =
24                 {0x00, (byte) 0xA4, 0x04, 0x00, 0x07, (byte) 0xA0,
25                  0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00};
26
27         private AlertDialog swipeDialog;
28         private PendingIntent tagIntent;
29
30         @Override
31         protected void onResume() {
32                 super.onResume();
33                 Log.v(TAG, "Starting the work");
34
35                 Intent intent = getIntent();
36                 Bundle extras = intent.getExtras();
37                 setResult(RESULT_CANCELED);
38                 if(swipeDialog != null) {
39                         swipeDialog.dismiss();
40                         swipeDialog = null;
41                 }
42                 if(extras != null) {
43                         int slot = extras.getInt("slot");
44                         if (slot > 0) {
45                                 swipeDialog = makeDialog();
46                                 swipeDialog.show();
47                                 enableDispatch(slot);
48                         }
49                 }
50         }
51
52         @Override
53         protected void onPause() {
54                 super.onPause();
55                 Log.v(TAG, "Finished the work");
56
57                 if(swipeDialog != null) {
58                         swipeDialog.dismiss();
59                         swipeDialog = null;
60                 }
61                 disableDispatch();
62         }
63
64         public void onNewIntent(Intent intent) {
65                 Log.v(TAG, "NFC Intent arrived");
66                 int slot = intent.getIntExtra("slot", -1);
67                 if (slot <= 0) return;
68                 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
69                 if (tag == null) return;
70                 IsoDep isoTag = IsoDep.get(tag);
71                 try {
72                         isoTag.connect();
73                         byte[] resp = isoTag.transceive(selectCommand);
74                         int length = resp.length;
75                         if (resp[length - 2] == (byte)0x90 &&
76                             resp[length - 1] == 0x00) {
77                                 doChallengeYubiKey(isoTag, slot);
78                         } else {
79                                 Toast.makeText(this, R.string.tag_error,
80                                                 Toast.LENGTH_LONG).show();
81                         }
82                 } catch (TagLostException e) {
83                         Toast.makeText(this,
84                                 R.string.tag_lost, Toast.LENGTH_LONG).show();
85                 } catch (IOException e) {
86                         Toast.makeText(this,
87                                 getText(R.string.tag_error) +
88                                 e.getMessage(),
89                                 Toast.LENGTH_LONG).show();
90                 }
91                 finish();
92         }
93
94         private AlertDialog makeDialog() {
95                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
96                 builder.setTitle(R.string.challenging);
97                 builder.setMessage(R.string.swipe);
98                 builder.setOnCancelListener(
99                                 new DialogInterface.OnCancelListener() {
100                         public void onCancel(DialogInterface dialog) {
101                                 finish();
102                         }
103                 });
104                 return builder.create();
105         }
106
107         private void enableDispatch(int slot) {
108                 Intent intent = getIntent();
109                 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
110                 intent.putExtra("slot", slot);
111                 tagIntent = PendingIntent.getActivity(this, 0, intent, 0);
112                 IntentFilter iso =
113                         new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
114                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
115                 if (adapter == null) {
116                         Toast.makeText(this, R.string.no_nfc,
117                                                 Toast.LENGTH_LONG).show();
118                         finish();
119                         return;
120                 }
121                 if (! adapter.isEnabled()) {
122                         Toast.makeText(this, R.string.nfc_disabled,
123                                                 Toast.LENGTH_LONG).show();
124                         finish();
125                         return;
126                 }
127                 adapter.enableForegroundDispatch(
128                         this, tagIntent, new IntentFilter[] {iso},
129                         new String[][] {new String[] {IsoDep.class.getName()}});
130         }
131
132         private void disableDispatch() {
133                 if (tagIntent != null) {
134                         tagIntent.cancel();
135                         tagIntent = null;
136                 }
137                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
138                 if (adapter != null) {
139                         adapter.disableForegroundDispatch(this);
140                 }
141         }
142
143         private void doChallengeYubiKey(IsoDep isoTag, int slot)
144                                                 throws IOException {
145                 Intent data = getIntent();
146                 data.putExtra("response","<FIXME>real data here");
147                 setResult(RESULT_OK, data);
148         }
149 }