1 package org.average.nfcauthcr;
3 import java.io.IOException;
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;
19 public class NFCAuthCRCheck extends Activity {
21 private final String TAG = getClass().getName();
23 private static final byte[] selectCommand =
24 {0x00, (byte) 0xA4, 0x04, 0x00, 0x07, (byte) 0xA0,
25 0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00};
27 private AlertDialog swipeDialog;
28 private PendingIntent tagIntent;
31 protected void onResume() {
33 Log.v(TAG, "Starting the work");
35 Intent intent = getIntent();
36 Bundle extras = intent.getExtras();
37 setResult(RESULT_CANCELED);
38 if(swipeDialog != null) {
39 swipeDialog.dismiss();
43 int slot = extras.getInt("slot");
45 swipeDialog = makeDialog();
53 protected void onPause() {
55 Log.v(TAG, "Finished the work");
57 if(swipeDialog != null) {
58 swipeDialog.dismiss();
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);
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);
79 Toast.makeText(this, R.string.tag_error,
80 Toast.LENGTH_LONG).show();
82 } catch (TagLostException e) {
84 R.string.tag_lost, Toast.LENGTH_LONG).show();
85 } catch (IOException e) {
87 getText(R.string.tag_error) +
89 Toast.LENGTH_LONG).show();
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) {
104 return builder.create();
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);
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();
121 if (! adapter.isEnabled()) {
122 Toast.makeText(this, R.string.nfc_disabled,
123 Toast.LENGTH_LONG).show();
127 adapter.enableForegroundDispatch(
128 this, tagIntent, new IntentFilter[] {iso},
129 new String[][] {new String[] {IsoDep.class.getName()}});
132 private void disableDispatch() {
133 if (tagIntent != null) {
137 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
138 if (adapter != null) {
139 adapter.disableForegroundDispatch(this);
143 private void doChallengeYubiKey(IsoDep isoTag, int slot)
145 Intent data = getIntent();
146 data.putExtra("response","<FIXME>real data here");
147 setResult(RESULT_OK, data);