68a8d0e9762e1a04d71a7e948c7d7f56cd5eb749
[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                         dispatch.onResume(challenge);
68                 } else {
69                         Log.e(TAG, "Challenge missing in the Intent");
70                         finish();
71                 }
72         }
73
74         @Override
75         protected void onPause() {
76                 super.onPause();
77                 Log.v(TAG, "Finished the work");
78
79                 dispatch.onPause();
80         }
81
82         public void onNewIntent(Intent newintent) {
83                 Log.v(TAG, "NFC Intent arrived");
84
85                 ArrayList<String> response = dispatch.onNewIntent(newintent);
86                 if (response != null) {
87                         Intent masterintent = getIntent();
88                         masterintent.putExtra("response", response);
89                         setResult(RESULT_OK, masterintent);
90                         finish();
91                 }
92         }
93 }