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();
93 <receiver android:name=".IsoDepReceiver"
94 android:label="IsoDepReceiver">
96 <action android:name="android.nfc.action.TECH_DISCOVERED" />
99 <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
100 android:resource="@xml/filter_nfc"
104 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
105 <!-- capture anything using IsoDep -->
107 <tech>android.nfc.tech.IsoDep</tech>
113 private void enableDispatch(int slot) {
114 Intent intent = getIntent();
115 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
116 intent.putExtra("slot", slot);
117 tagIntent = PendingIntent.getActivity(this, 0, intent, 0);
119 new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
120 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
121 if (adapter == null) {
122 Toast.makeText(this, R.string.no_nfc,
123 Toast.LENGTH_LONG).show();
126 if (! adapter.isEnabled()) {
127 Toast.makeText(this, R.string.nfc_disabled,
128 Toast.LENGTH_LONG).show();
131 adapter.enableForegroundDispatch(
132 this, tagIntent, new IntentFilter[] {iso},
133 new String[][] {new String[] {IsoDep.class.getName()}});
136 private void disableDispatch() {
137 if (tagIntent != null) {
141 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
142 if (adapter != null) {
143 adapter.disableForegroundDispatch(this);