]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/Check.java
36f3b1d8fe17cdb342a25ae85eb90ea56be7f30a
[YkNeoCR.git] / src / org / average / nfcauthcr / Check.java
1 package org.average.nfcauthcr;
2
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;
15
16 import org.average.nfcauthcr.YkNeo;
17 import org.average.nfcauthcr.CRException;
18
19 public class Check extends Activity {
20
21         private final String TAG = getClass().getName();
22
23         private AlertDialog swipeDialog;
24         private PendingIntent tagIntent;
25
26         @Override
27         protected void onResume() {
28                 super.onResume();
29                 Log.v(TAG, "Starting the work");
30
31                 Intent intent = getIntent();
32                 setResult(RESULT_CANCELED);
33                 if (swipeDialog != null) {
34                         swipeDialog.dismiss();
35                         swipeDialog = null;
36                 }
37                 int slot = intent.getIntExtra("slot", -1);
38                 if (slot > 0) {
39                         swipeDialog = makeDialog();
40                         swipeDialog.show();
41                         enableDispatch(slot);
42                 }
43         }
44
45         @Override
46         protected void onPause() {
47                 super.onPause();
48                 Log.v(TAG, "Finished the work");
49
50                 if(swipeDialog != null) {
51                         swipeDialog.dismiss();
52                         swipeDialog = null;
53                 }
54                 disableDispatch();
55         }
56
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);
65                 try {
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();
75                 }
76                 finish();
77         }
78
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) {
86                                 finish();
87                         }
88                 });
89                 return builder.create();
90         }
91
92 /*
93 <receiver android:name=".IsoDepReceiver"
94           android:label="IsoDepReceiver">
95      <intent-filter>
96          <action android:name="android.nfc.action.TECH_DISCOVERED" />
97      </intent-filter>
98
99      <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
100          android:resource="@xml/filter_nfc"
101      />
102  </receiver>
103
104 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
105      <!-- capture anything using IsoDep -->
106      <tech-list>
107          <tech>android.nfc.tech.IsoDep</tech>
108      </tech-list>
109  </resources>
110
111 */
112
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);
118                 IntentFilter iso =
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();
124                         finish();
125                 }
126                 if (! adapter.isEnabled()) {
127                         Toast.makeText(this, R.string.nfc_disabled,
128                                                 Toast.LENGTH_LONG).show();
129                         finish();
130                 }
131                 adapter.enableForegroundDispatch(
132                         this, tagIntent, new IntentFilter[] {iso},
133                         new String[][] {new String[] {IsoDep.class.getName()}});
134         }
135
136         private void disableDispatch() {
137                 if (tagIntent != null) {
138                         tagIntent.cancel();
139                         tagIntent = null;
140                 }
141                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
142                 if (adapter != null) {
143                         adapter.disableForegroundDispatch(this);
144                 }
145         }
146 }