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;
17 import org.average.nfcauthcr.CRException;
19 public class Check extends Activity {
21 private final String TAG = getClass().getName();
23 private AlertDialog swipeDialog;
24 private PendingIntent tagIntent;
27 protected void onResume() {
29 Log.v(TAG, "Starting the work");
31 Intent intent = getIntent();
32 setResult(RESULT_CANCELED);
33 if (swipeDialog != null) {
34 swipeDialog.dismiss();
37 int slot = intent.getIntExtra("slot", -1);
39 swipeDialog = makeDialog();
46 protected void onPause() {
48 Log.v(TAG, "Finished the work");
50 if(swipeDialog != null) {
51 swipeDialog.dismiss();
57 public void onNewIntent(Intent intent) {
58 Log.v(TAG, "NFC Intent arrived");
59 int slot = intent.getIntExtra("slot", -1);
60 byte[] challenge = intent.getByteArrayExtra("challenge");
61 if (slot <= 0) return;
62 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
63 if (tag == null) return;
64 IsoDep isoTag = IsoDep.get(tag);
66 byte[] response = YkNeo.doChallengeYubiKey(
67 isoTag, slot, challenge);
68 Intent data = getIntent();
69 data.putExtra("response", response);
70 setResult(RESULT_OK, data);
71 } catch (CRException e) {
72 Log.v(TAG, e.getMessage());
73 Toast.makeText(this, e.getMessage(),
74 Toast.LENGTH_LONG).show();
79 private AlertDialog makeDialog() {
80 AlertDialog.Builder builder = new AlertDialog.Builder(this);
81 builder.setTitle(R.string.challenging);
82 builder.setMessage(R.string.swipe);
83 builder.setOnCancelListener(
84 new DialogInterface.OnCancelListener() {
85 public void onCancel(DialogInterface dialog) {
89 return builder.create();
92 private void enableDispatch(int slot) {
93 Intent intent = getIntent();
94 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
95 intent.putExtra("slot", slot);
96 tagIntent = PendingIntent.getActivity(this, 0, intent, 0);
98 new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
99 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
100 if (adapter == null) {
101 Toast.makeText(this, R.string.no_nfc,
102 Toast.LENGTH_LONG).show();
105 if (! adapter.isEnabled()) {
106 Toast.makeText(this, R.string.nfc_disabled,
107 Toast.LENGTH_LONG).show();
110 adapter.enableForegroundDispatch(
111 this, tagIntent, new IntentFilter[] {iso},
112 new String[][] {new String[] {IsoDep.class.getName()}});
115 private void disableDispatch() {
116 if (tagIntent != null) {
120 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
121 if (adapter != null) {
122 adapter.disableForegroundDispatch(this);