return early if cannot init adapter
[YkNeoCR.git] / src / org / average / nfcauthcr / QueryCrToken.java
1 package org.average.ykneocr;
2
3 import java.util.ArrayList;
4
5 import android.os.Bundle;
6 import android.app.Activity;
7 import android.app.AlertDialog;
8 import android.app.PendingIntent;
9 import android.content.DialogInterface;
10 import android.content.Intent;
11 import android.content.IntentFilter;
12 import android.content.SharedPreferences;
13 import android.nfc.NfcAdapter;
14 import android.nfc.Tag;
15 import android.nfc.TagLostException;
16 import android.nfc.tech.IsoDep;
17 import android.preference.PreferenceManager;
18 import android.util.Log;
19 import android.widget.Toast;
20
21 import org.average.ykneocr.NfcCRdispatch;
22
23 public class QueryCrToken extends Activity {
24
25         private final String TAG = getClass().getName();
26
27         private NfcCRdispatch dispatch = new NfcCRdispatch((Activity)this);
28         private SharedPreferences prefs;
29
30         @Override
31         protected void onCreate(Bundle savedInstanceState) {
32                 super.onCreate(savedInstanceState);
33                 Log.v(TAG, "onCreate get prefs");
34                 prefs = PreferenceManager.getDefaultSharedPreferences(this);
35         }
36
37         @Override
38         protected void onResume() {
39                 super.onResume();
40                 Log.v(TAG, "Starting the work");
41
42                 setResult(RESULT_CANCELED);
43                 int slot = prefs.getInt("slot_number", -1);
44                 if (slot != 1 && slot != 2) {
45                         Log.e(TAG, "Slot " + slot + " cannot be used");
46                         Toast.makeText(this, R.string.need_slot,
47                                                 Toast.LENGTH_LONG).show();
48                         finish();
49                 }
50                 new AlertDialog.Builder(this)
51                         .setTitle(R.string.challenging)
52                         .setMessage(R.string.swipe)
53                         .setOnCancelListener(
54                                 new DialogInterface.OnCancelListener() {
55                                 public void onCancel(DialogInterface dialog) {
56                                         Log.v(TAG, "Cancel");
57                                         finish();
58                                 }
59                         })
60                         .create().show();
61
62                 Intent intent = getIntent();
63                 ArrayList<String> challenge =
64                                 intent.getStringArrayListExtra("challenge");
65                 intent.putExtra("yubikey_neo_slot", slot);
66                 if (challenge != null) {
67                         if (!dispatch.onResume(challenge)) {
68                                 Log.e(TAG, "Failed to enable fg dispatch");
69                                 finish();
70                         }
71                 } else {
72                         Log.e(TAG, "Challenge missing in the Intent");
73                         finish();
74                 }
75         }
76
77         @Override
78         protected void onPause() {
79                 super.onPause();
80                 Log.v(TAG, "Finished the work");
81
82                 dispatch.onPause();
83         }
84
85         public void onNewIntent(Intent newintent) {
86                 Log.v(TAG, "NFC Intent arrived");
87
88                 ArrayList<String> response = dispatch.onNewIntent(newintent);
89                 if (response != null) {
90                         Intent masterintent = getIntent();
91                         masterintent.putExtra("response", response);
92                         setResult(RESULT_OK, masterintent);
93                         finish();
94                 }
95         }
96 }