1 package org.average.nfcauthcr;
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;
16 import org.average.nfcauthcr.YkNeo;
18 public class Check extends Activity {
20 private final String TAG = getClass().getName();
22 private AlertDialog swipeDialog;
23 private PendingIntent tagIntent;
26 protected void onResume() {
28 Log.v(TAG, "Starting the work");
30 Intent intent = getIntent();
31 setResult(RESULT_CANCELED);
32 if (swipeDialog != null) {
33 swipeDialog.dismiss();
36 int slot = intent.getIntExtra("slot", -1);
38 swipeDialog = makeDialog();
45 protected void onPause() {
47 Log.v(TAG, "Finished the work");
49 if(swipeDialog != null) {
50 swipeDialog.dismiss();
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 = YkNeo.doChallengeYubiKey(
65 isoTag, slot, challenge);
66 if (response != null) {
67 Intent data = getIntent();
68 data.putExtra("response", response);
69 setResult(RESULT_OK, data);
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) {
84 return builder.create();
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);
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();
101 if (! adapter.isEnabled()) {
102 Toast.makeText(this, R.string.nfc_disabled,
103 Toast.LENGTH_LONG).show();
107 adapter.enableForegroundDispatch(
108 this, tagIntent, new IntentFilter[] {iso},
109 new String[][] {new String[] {IsoDep.class.getName()}});
112 private void disableDispatch() {
113 if (tagIntent != null) {
117 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
118 if (adapter != null) {
119 adapter.disableForegroundDispatch(this);