begin converting to multiple ops per request
[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.RadioButton;
16
17 import org.average.nfcauthcr.QueryCrToken;
18
19 public class Enroll extends Activity {
20
21         private final String TAG = getClass().getName();
22
23         private static boolean waitingForResult = false;
24         private static SharedPreferences prefs;
25         private static int slot;
26
27         @Override
28         protected void onCreate(Bundle savedInstanceState)
29         {
30                 super.onCreate(savedInstanceState);
31                 Log.v(TAG, "Starting");
32                 prefs = PreferenceManager.getDefaultSharedPreferences(this);
33         }
34
35         @Override
36         protected void onResume() {
37                 super.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;
42                 switch (slot) {
43                 case 1: btn = (RadioButton)findViewById(R.id.slot_1);
44                         break;
45                 case 2: btn = (RadioButton)findViewById(R.id.slot_2);
46                         break;
47                 }
48                 if (btn != null) btn.setChecked(true);
49         }
50
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;
57                 }
58                 Editor editor = prefs.edit();
59                 editor.putInt("slot_number", slot);
60                 editor.commit();
61                 Log.v(TAG, "stored slot number " + slot);
62         }
63
64         public void onEnrollClicked(View view) {
65                 Log.v(TAG, "Enroll clicked");
66                 if (slot > 0) {
67                         runEnrollment(slot);
68                 } else {
69                         showEnrollResult(R.string.need_slot);
70                 }
71         }
72
73         public void onActivityResult(int requestCode, int resultCode,
74                                         Intent intent) {
75                 Log.v(TAG, "Got activity result");
76                 waitingForResult = false;
77
78                 if (resultCode != RESULT_OK) {
79                         Log.v(TAG, "Error result code " + resultCode);
80                         return;
81                 }
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));
89                 editor.commit();
90                 showEnrollResult(R.string.enroll_success);
91                 Log.v(TAG, "stored new challenge and response");
92         }
93
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) {
101                                 dialog.dismiss();
102                                 if (!waitingForResult) { finish(); }
103                         }
104                 });
105                 AlertDialog dialog = builder.create();
106                 dialog.show();
107         }
108
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);
118         }
119
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();
125         }
126 }