]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/Check.java
cleanup workflow
[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         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);
97                 IntentFilter iso =
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();
103                         finish();
104                 }
105                 if (! adapter.isEnabled()) {
106                         Toast.makeText(this, R.string.nfc_disabled,
107                                                 Toast.LENGTH_LONG).show();
108                         finish();
109                 }
110                 adapter.enableForegroundDispatch(
111                         this, tagIntent, new IntentFilter[] {iso},
112                         new String[][] {new String[] {IsoDep.class.getName()}});
113         }
114
115         private void disableDispatch() {
116                 if (tagIntent != null) {
117                         tagIntent.cancel();
118                         tagIntent = null;
119                 }
120                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
121                 if (adapter != null) {
122                         adapter.disableForegroundDispatch(this);
123                 }
124         }
125 }