make errro handling via exception
[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                         return;
105                 }
106                 if (! adapter.isEnabled()) {
107                         Toast.makeText(this, R.string.nfc_disabled,
108                                                 Toast.LENGTH_LONG).show();
109                         finish();
110                         return;
111                 }
112                 adapter.enableForegroundDispatch(
113                         this, tagIntent, new IntentFilter[] {iso},
114                         new String[][] {new String[] {IsoDep.class.getName()}});
115         }
116
117         private void disableDispatch() {
118                 if (tagIntent != null) {
119                         tagIntent.cancel();
120                         tagIntent = null;
121                 }
122                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
123                 if (adapter != null) {
124                         adapter.disableForegroundDispatch(this);
125                 }
126         }
127 }