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.RadioButton;
17 import org.average.nfcauthcr.QueryCrToken;
19 public class Enroll extends Activity {
21 private final String TAG = getClass().getName();
23 private static boolean waitingForResult = false;
24 private static SharedPreferences prefs;
25 private static int slot;
28 protected void onCreate(Bundle savedInstanceState)
30 super.onCreate(savedInstanceState);
31 Log.v(TAG, "Starting");
32 prefs = PreferenceManager.getDefaultSharedPreferences(this);
36 protected void onResume() {
38 setContentView(R.layout.main);
39 slot = prefs.getInt("slot_number", -1);
40 Log.v(TAG, "found saved slot value " + slot);
41 RadioButton btn = null;
43 case 1: btn = (RadioButton)findViewById(R.id.slot_1);
45 case 2: btn = (RadioButton)findViewById(R.id.slot_2);
48 if (btn != null) btn.setChecked(true);
51 public void onSlotSelectionClicked(View view) {
52 Log.v(TAG, "Radio Button selected");
53 if (! ((RadioButton) view).isChecked()) return;
54 switch(view.getId()) {
55 case R.id.slot_1: slot=1; break;
56 case R.id.slot_2: slot=2; break;
58 Editor editor = prefs.edit();
59 editor.putInt("slot_number", slot);
61 Log.v(TAG, "stored slot number " + slot);
64 public void onEnrollClicked(View view) {
65 Log.v(TAG, "Enroll clicked");
69 showEnrollResult(R.string.need_slot);
73 public void onActivityResult(int requestCode, int resultCode,
75 Log.v(TAG, "Got activity result");
76 waitingForResult = false;
78 if (resultCode != RESULT_OK) {
79 Log.v(TAG, "Error result code " + resultCode);
82 byte[] challenge = intent.getByteArrayExtra("challenge");
83 Log.v(TAG, "Challenge is \"" + hex(challenge) + "\"");
84 byte[] response = intent.getByteArrayExtra("response");
85 Log.v(TAG, "Response is \"" + hex(response) + "\"");
86 Editor editor = prefs.edit();
87 editor.putString("challenge", hex(challenge));
88 editor.putString("response", hex(response));
90 showEnrollResult(R.string.enroll_success);
91 Log.v(TAG, "stored new challenge and response");
94 private void showEnrollResult(int messageid) {
95 AlertDialog.Builder builder = new AlertDialog.Builder(this);
96 builder.setTitle(R.string.enrollresult);
97 builder.setMessage(messageid);
98 builder.setPositiveButton(android.R.string.ok,
99 new DialogInterface.OnClickListener() {
100 public void onClick(DialogInterface dialog, int which) {
102 if (!waitingForResult) { finish(); }
105 AlertDialog dialog = builder.create();
109 private void runEnrollment(int slot) {
110 Random rng = new Random();
111 byte[] challenge = new byte[63];
112 rng.nextBytes(challenge);
113 Log.v(TAG, "Random challenge: " + hex(challenge));
114 Intent crIntent = new Intent(this, QueryCrToken.class);
115 crIntent.putExtra("challenge", challenge);
116 waitingForResult = true;
117 this.startActivityForResult(crIntent, 0);
120 private String hex(byte[] a) {
121 StringBuilder sb = new StringBuilder();
122 if (a == null) return "<null>";
123 for (byte b: a) sb.append(String.format("%02x", b&0xff));
124 return sb.toString();