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.TagEvent;
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;
27 private AlertDialog swipeDialog;
30 protected void onCreate(Bundle savedInstanceState)
32 super.onCreate(savedInstanceState);
33 Log.v(TAG, "Starting");
34 prefs = PreferenceManager.getDefaultSharedPreferences(this);
38 protected void onResume() {
40 setContentView(R.layout.main);
41 slot = prefs.getInt("slot_number", -1);
42 Log.v(TAG, "found saved slot value " + slot);
43 RadioButton btn = null;
45 case 1: btn = (RadioButton)findViewById(R.id.slot_1);
47 case 2: btn = (RadioButton)findViewById(R.id.slot_2);
50 if (btn != null) btn.setChecked(true);
54 protected void onPause() {
56 Log.v(TAG, "Going inactive, try to stop");
57 if (!waitingForResult) { finish(); }
61 protected void onStop() {
63 Log.v(TAG, "Stop requested");
66 public void onSlotSelectionClicked(View view) {
67 Log.v(TAG, "Radio Button selected");
68 if (! ((RadioButton) view).isChecked()) return;
69 switch(view.getId()) {
70 case R.id.slot_1: slot=1; break;
71 case R.id.slot_2: slot=2; break;
73 Editor editor = prefs.edit();
74 editor.putInt("slot_number", slot);
76 Log.v(TAG, "stored slot number " + slot);
79 public void onEnrollClicked(View view) {
80 Log.v(TAG, "Enroll clicked");
84 showEnrollResult(R.string.need_slot);
88 public void onActivityResult(int requestCode, int resultCode,
90 Log.v(TAG, "Got activity result");
91 waitingForResult = false;
92 if(swipeDialog != null) {
93 swipeDialog.dismiss();
97 if (resultCode != RESULT_OK) {
98 Log.v(TAG, "Error result code " + resultCode);
101 byte[] challenge = intent.getByteArrayExtra("challenge");
102 Log.v(TAG, "Challenge is \"" + hex(challenge) + "\"");
103 byte[] response = intent.getByteArrayExtra("response");
104 Log.v(TAG, "Response is \"" + hex(response) + "\"");
105 Editor editor = prefs.edit();
106 editor.putString("challenge", hex(challenge));
107 editor.putString("response", hex(response));
109 showEnrollResult(R.string.enroll_success);
110 Log.v(TAG, "stored new challenge and response");
113 private void showEnrollResult(int messageid) {
114 AlertDialog.Builder builder = new AlertDialog.Builder(this);
115 builder.setTitle(R.string.enrollresult);
116 builder.setMessage(messageid);
117 builder.setPositiveButton(android.R.string.ok,
118 new DialogInterface.OnClickListener() {
119 public void onClick(DialogInterface dialog, int which) {
121 if (!waitingForResult) { finish(); }
124 AlertDialog dialog = builder.create();
128 private void runEnrollment(int slot) {
129 Random rng = new Random();
130 byte[] challenge = new byte[63];
131 rng.nextBytes(challenge);
132 Log.v(TAG, "Random challenge: " + hex(challenge));
133 Intent crIntent = new Intent(this, TagEvent.class);
134 crIntent.putExtra("slot", slot);
135 crIntent.putExtra("challenge", challenge);
136 this.startActivityForResult(crIntent, 0);
137 waitingForResult = true;
138 if (swipeDialog != null) swipeDialog.dismiss();
139 swipeDialog = makeDialog();
143 private AlertDialog makeDialog() {
144 AlertDialog.Builder builder = new AlertDialog.Builder(this);
145 builder.setTitle(R.string.challenging);
146 builder.setMessage(R.string.swipe);
147 builder.setOnCancelListener(
148 new DialogInterface.OnCancelListener() {
149 public void onCancel(DialogInterface dialog) {
150 Log.v(TAG, "unbind service FIXME");
153 return builder.create();
156 private String hex(byte[] a) {
157 StringBuilder sb = new StringBuilder();
158 if (a == null) return "<null>";
159 for (byte b: a) sb.append(String.format("%02x", b&0xff));
160 return sb.toString();