--- /dev/null
+package org.average.nfcauthcr;
+
+import java.util.Random;
+
+import android.os.Bundle;
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.preference.PreferenceManager;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
+import android.content.DialogInterface;
+import android.util.Log;
+import android.view.View;
+import android.widget.TextView;
+import android.widget.RadioButton;
+
+import org.average.nfcauthcr.Check;
+
+public class Enroll extends Activity {
+
+ private final String TAG = getClass().getName();
+
+ private static boolean waitingForResult = false;
+ private static SharedPreferences prefs;
+ private static int slot;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState)
+ {
+ super.onCreate(savedInstanceState);
+ Log.v(TAG, "Starting");
+ prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ setContentView(R.layout.main);
+ slot = prefs.getInt("slot_number", -1);
+ Log.v(TAG, "found saved slot value " + slot);
+ RadioButton btn = null;
+ switch (slot) {
+ case 1: btn = (RadioButton)findViewById(R.id.slot_1);
+ break;
+ case 2: btn = (RadioButton)findViewById(R.id.slot_2);
+ break;
+ }
+ if (btn != null) btn.setChecked(true);
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+ Log.v(TAG, "Going inactive, try to stop");
+ if (!waitingForResult) { finish(); }
+ }
+
+ @Override
+ protected void onStop() {
+ super.onStop();
+ Log.v(TAG, "Stop requested");
+ }
+
+ public void onSlotSelectionClicked(View view) {
+ Log.v(TAG, "Radio Button selected");
+ if (! ((RadioButton) view).isChecked()) return;
+ switch(view.getId()) {
+ case R.id.slot_1: slot=1; break;
+ case R.id.slot_2: slot=2; break;
+ }
+ Editor editor = prefs.edit();
+ editor.putInt("slot_number", slot);
+ editor.commit();
+ Log.v(TAG, "stored slot number " + slot);
+ }
+
+ public void onEnrollClicked(View view) {
+ Log.v(TAG, "Enroll clicked");
+ if (slot > 0) {
+ runEnrollment(slot);
+ } else {
+ showEnrollResult("Must specify which slot to use");
+ }
+ }
+
+ public void onActivityResult(int requestCode, int resultCode,
+ Intent intent) {
+ Log.v(TAG, "Got activity result");
+ waitingForResult = false;
+
+ if (resultCode != RESULT_OK) {
+ Log.v(TAG, "Error result code " + resultCode);
+ return;
+ }
+ byte[] challenge = intent.getByteArrayExtra("challenge");
+ Log.v(TAG, "Challenge is \"" + hex(challenge) + "\"");
+ byte[] response = intent.getByteArrayExtra("response");
+ Log.v(TAG, "Response is \"" + hex(response) + "\"");
+ Editor editor = prefs.edit();
+ editor.putString("challenge", hex(challenge));
+ editor.putString("response", hex(response));
+ editor.commit();
+ Log.v(TAG, "stored new challenge and response");
+ }
+
+ private void showEnrollResult(final String msg) {
+ Log.v(TAG, "Show result: \"" + msg + "\"");
+
+ AlertDialog.Builder builder = new AlertDialog.Builder(this);
+ builder.setTitle(R.string.enrollresult);
+ builder.setMessage(msg);
+ builder.setPositiveButton(android.R.string.ok,
+ new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int which) {
+ dialog.dismiss();
+ if (!waitingForResult) { finish(); }
+ }
+ });
+ AlertDialog dialog = builder.create();
+ dialog.show();
+ }
+
+ private void runEnrollment(int slot) {
+ Random rng = new Random();
+ byte[] challenge = new byte[63];
+ rng.nextBytes(challenge);
+ Log.v(TAG, "Random challenge: " + hex(challenge));
+ Log.v(TAG, "Launching challenging activity");
+ Intent crIntent = new Intent(this, NFCAuthCRCheck.class);
+ crIntent.putExtra("slot", slot);
+ crIntent.putExtra("challenge", challenge);
+ this.startActivityForResult(crIntent, 0);
+ waitingForResult = true;
+ }
+
+ private String hex(byte[] a) {
+ StringBuilder sb = new StringBuilder();
+ if (a == null) return "<null>";
+ for (byte b: a) sb.append(String.format("%02x", b&0xff));
+ return sb.toString();
+ }
+}