]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/Check.java
move swipeDialog to activity
[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 PendingIntent tagIntent;
24
25         @Override
26         protected void onResume() {
27                 super.onResume();
28                 Log.v(TAG, "Starting the work");
29
30                 Intent intent = getIntent();
31                 setResult(RESULT_CANCELED);
32                 int slot = intent.getIntExtra("slot", -1);
33                 if (slot > 0) {
34                         enableDispatch(slot);
35                 }
36         }
37
38         @Override
39         protected void onPause() {
40                 super.onPause();
41                 Log.v(TAG, "Finished the work");
42
43                 disableDispatch();
44         }
45
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);
54                 try {
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();
64                 }
65                 finish();
66         }
67
68 /*
69 <receiver android:name=".IsoDepReceiver"
70           android:label="IsoDepReceiver">
71      <intent-filter>
72          <action android:name="android.nfc.action.TECH_DISCOVERED" />
73      </intent-filter>
74
75      <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
76          android:resource="@xml/filter_nfc"
77      />
78  </receiver>
79
80 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
81      <!-- capture anything using IsoDep -->
82      <tech-list>
83          <tech>android.nfc.tech.IsoDep</tech>
84      </tech-list>
85  </resources>
86
87 */
88
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);
94                 IntentFilter iso =
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();
100                         finish();
101                 }
102                 if (! adapter.isEnabled()) {
103                         Toast.makeText(this, R.string.nfc_disabled,
104                                                 Toast.LENGTH_LONG).show();
105                         finish();
106                 }
107                 adapter.enableForegroundDispatch(
108                         this, tagIntent, new IntentFilter[] {iso},
109                         new String[][] {new String[] {IsoDep.class.getName()}});
110         }
111
112         private void disableDispatch() {
113                 if (tagIntent != null) {
114                         tagIntent.cancel();
115                         tagIntent = null;
116                 }
117                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
118                 if (adapter != null) {
119                         adapter.disableForegroundDispatch(this);
120                 }
121         }
122 }