]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/NFCAuthCREnroll.java
bb6b4f0357118db644ce4e7aace211823aa5be34
[YkNeoCR.git] / src / org / average / nfcauthcr / NFCAuthCREnroll.java
1 package org.average.nfcauthcr;
2
3 import android.os.Bundle;
4 import android.app.Activity;
5 import android.app.AlertDialog;
6 import android.preference.PreferenceManager;
7 import android.content.Intent;
8 import android.content.SharedPreferences;
9 import android.content.SharedPreferences.Editor;
10 import android.content.DialogInterface;
11 import android.util.Log;
12 import android.view.View;
13 import android.widget.TextView;
14 import android.widget.RadioButton;
15
16 import org.average.nfcauthcr.NFCAuthCRCheck;
17
18 public class NFCAuthCREnroll extends Activity {
19
20         private final String TAG = getClass().getName();
21
22         private static boolean waitingForResult = false;
23         private static SharedPreferences prefs;
24         private static int slot;
25
26         @Override
27         protected void onCreate(Bundle savedInstanceState)
28         {
29                 super.onCreate(savedInstanceState);
30                 Log.v(TAG, "Starting");
31                 prefs = PreferenceManager.getDefaultSharedPreferences(this);
32         }
33
34         @Override
35         protected void onResume() {
36                 super.onResume();
37                 setContentView(R.layout.main);
38                 slot = prefs.getInt("slot_number", -1);
39                 Log.v(TAG, "found saved slot value " + slot);
40                 RadioButton btn = null;
41                 switch (slot) {
42                 case 1: btn = (RadioButton)findViewById(R.id.slot_1);
43                         break;
44                 case 2: btn = (RadioButton)findViewById(R.id.slot_2);
45                         break;
46                 }
47                 if (btn != null) btn.setChecked(true);
48         }
49
50         @Override
51         protected void onPause() {
52                 super.onPause();
53                 Log.v(TAG, "Going inactive, try to stop");
54                 if (!waitingForResult) { finish(); }
55         }
56
57         @Override
58         protected void onStop() {
59                 super.onStop();
60                 Log.v(TAG, "Stop requested");
61         }
62
63         public void onSlotSelectionClicked(View view) {
64                 Log.v(TAG, "Radio Button selected");
65                 if (! ((RadioButton) view).isChecked()) return;
66                 switch(view.getId()) {
67                 case R.id.slot_1: slot=1; break;
68                 case R.id.slot_2: slot=2; break;
69                 }
70                 Editor editor = prefs.edit();
71                 editor.putInt("slot_number", slot);
72                 editor.commit();
73                 Log.v(TAG, "stored slot number " + slot);
74         }
75
76         public void onEnrollClicked(View view) {
77                 Log.v(TAG, "Enroll clicked");
78                 if (slot > 0) {
79                         runChallenge(slot);
80                 } else {
81                         showEnrollResult("Must specify which slot to use");
82                 }
83         }
84
85         public void onActivityResult(int requestCode, int resultCode,
86                                         Intent intent) {
87                 Log.v(TAG, "Got activity result");
88                 waitingForResult = false;
89                 if (resultCode == RESULT_OK) {
90                         String res = intent.getStringExtra("response");
91                         Log.v(TAG, "Response is \"" + res + "\"");
92                 } else {
93                         Log.v(TAG, "Error result code " + resultCode);
94                 }
95         }
96
97         private void showEnrollResult(final String msg) {
98                 Log.v(TAG, "Show result: \"" + msg + "\"");
99
100                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
101                 builder.setTitle(R.string.enrollresult);
102                 builder.setMessage(msg);
103                 builder.setPositiveButton(android.R.string.ok,
104                                 new DialogInterface.OnClickListener() {
105                         public void onClick(DialogInterface dialog, int which) {
106                                 dialog.dismiss();
107                                 if (!waitingForResult) { finish(); }
108                         }
109                 });
110                 AlertDialog dialog = builder.create();
111                 dialog.show();
112         }
113
114         private void runChallenge(int slot) {
115                 Log.v(TAG, "Launching challenging activity");
116                 Intent crIntent = new Intent(this, NFCAuthCRCheck.class);
117                 crIntent.putExtra("slot", slot);
118                 this.startActivityForResult(crIntent, 0);
119                 waitingForResult = true;
120         }
121 }