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 PendingIntent tagIntent;
26 protected void onResume() {
28 Log.v(TAG, "Starting the work");
30 Intent intent = getIntent();
31 setResult(RESULT_CANCELED);
32 int slot = intent.getIntExtra("slot", -1);
39 protected void onPause() {
41 Log.v(TAG, "Finished the work");
46 public void onNewIntent(Intent intent) {
47 Log.v(TAG, "NFC Intent arrived");
48 int slot = intent.getIntExtra("slot", -1);
49 byte[] challenge = intent.getByteArrayExtra("challenge");
50 if (slot <= 0) return;
51 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
52 if (tag == null) return;
53 IsoDep isoTag = IsoDep.get(tag);
55 byte[] response = YkNeo.doChallengeYubiKey(
56 isoTag, slot, challenge);
57 Intent data = getIntent();
58 data.putExtra("response", response);
59 setResult(RESULT_OK, data);
60 } catch (CRException e) {
61 Log.v(TAG, e.getMessage());
62 Toast.makeText(this, e.getMessage(),
63 Toast.LENGTH_LONG).show();
69 <receiver android:name=".IsoDepReceiver"
70 android:label="IsoDepReceiver">
72 <action android:name="android.nfc.action.TECH_DISCOVERED" />
75 <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
76 android:resource="@xml/filter_nfc"
80 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
81 <!-- capture anything using IsoDep -->
83 <tech>android.nfc.tech.IsoDep</tech>
89 private void enableDispatch(int slot) {
90 Intent intent = getIntent();
91 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
92 intent.putExtra("slot", slot);
93 tagIntent = PendingIntent.getActivity(this, 0, intent, 0);
95 new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
96 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
97 if (adapter == null) {
98 Toast.makeText(this, R.string.no_nfc,
99 Toast.LENGTH_LONG).show();
102 if (! adapter.isEnabled()) {
103 Toast.makeText(this, R.string.nfc_disabled,
104 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);