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