1 package org.average.nfcauthcr;
3 import java.util.Random;
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;
18 import org.average.nfcauthcr.Check;
20 public class Enroll extends Activity {
22 private final String TAG = getClass().getName();
24 private static boolean waitingForResult = false;
25 private static SharedPreferences prefs;
26 private static int slot;
29 protected void onCreate(Bundle savedInstanceState)
31 super.onCreate(savedInstanceState);
32 Log.v(TAG, "Starting");
33 prefs = PreferenceManager.getDefaultSharedPreferences(this);
37 protected void 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;
44 case 1: btn = (RadioButton)findViewById(R.id.slot_1);
46 case 2: btn = (RadioButton)findViewById(R.id.slot_2);
49 if (btn != null) btn.setChecked(true);
53 protected void onPause() {
55 Log.v(TAG, "Going inactive, try to stop");
56 if (!waitingForResult) { finish(); }
60 protected void onStop() {
62 Log.v(TAG, "Stop requested");
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;
72 Editor editor = prefs.edit();
73 editor.putInt("slot_number", slot);
75 Log.v(TAG, "stored slot number " + slot);
78 public void onEnrollClicked(View view) {
79 Log.v(TAG, "Enroll clicked");
83 showEnrollResult(R.string.need_slot);
87 public void onActivityResult(int requestCode, int resultCode,
89 Log.v(TAG, "Got activity result");
90 waitingForResult = false;
92 if (resultCode != RESULT_OK) {
93 Log.v(TAG, "Error result code " + resultCode);
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));
104 showEnrollResult(R.string.enroll_success);
105 Log.v(TAG, "stored new challenge and response");
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) {
116 if (!waitingForResult) { finish(); }
119 AlertDialog dialog = builder.create();
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;
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();