]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/NFCAuthCREnroll.java
got enrollment work
[YkNeoCR.git] / src / org / average / nfcauthcr / NFCAuthCREnroll.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.TextView;
16 import android.widget.RadioButton;
17
18 import org.average.nfcauthcr.NFCAuthCRCheck;
19
20 public class NFCAuthCREnroll extends Activity {
21
22         private final String TAG = getClass().getName();
23
24         private static boolean waitingForResult = false;
25         private static SharedPreferences prefs;
26         private static int slot;
27
28         @Override
29         protected void onCreate(Bundle savedInstanceState)
30         {
31                 super.onCreate(savedInstanceState);
32                 Log.v(TAG, "Starting");
33                 prefs = PreferenceManager.getDefaultSharedPreferences(this);
34         }
35
36         @Override
37         protected void onResume() {
38                 super.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;
43                 switch (slot) {
44                 case 1: btn = (RadioButton)findViewById(R.id.slot_1);
45                         break;
46                 case 2: btn = (RadioButton)findViewById(R.id.slot_2);
47                         break;
48                 }
49                 if (btn != null) btn.setChecked(true);
50         }
51
52         @Override
53         protected void onPause() {
54                 super.onPause();
55                 Log.v(TAG, "Going inactive, try to stop");
56                 if (!waitingForResult) { finish(); }
57         }
58
59         @Override
60         protected void onStop() {
61                 super.onStop();
62                 Log.v(TAG, "Stop requested");
63         }
64
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;
71                 }
72                 Editor editor = prefs.edit();
73                 editor.putInt("slot_number", slot);
74                 editor.commit();
75                 Log.v(TAG, "stored slot number " + slot);
76         }
77
78         public void onEnrollClicked(View view) {
79                 Log.v(TAG, "Enroll clicked");
80                 if (slot > 0) {
81                         runEnrollment(slot);
82                 } else {
83                         showEnrollResult("Must specify which slot to use");
84                 }
85         }
86
87         public void onActivityResult(int requestCode, int resultCode,
88                                         Intent intent) {
89                 Log.v(TAG, "Got activity result");
90                 waitingForResult = false;
91
92                 if (resultCode != RESULT_OK) {
93                         Log.v(TAG, "Error result code " + resultCode);
94                         return;
95                 }
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));
103                 editor.commit();
104                 Log.v(TAG, "stored new challenge and response");
105         }
106
107         private void showEnrollResult(final String msg) {
108                 Log.v(TAG, "Show result: \"" + msg + "\"");
109
110                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
111                 builder.setTitle(R.string.enrollresult);
112                 builder.setMessage(msg);
113                 builder.setPositiveButton(android.R.string.ok,
114                                 new DialogInterface.OnClickListener() {
115                         public void onClick(DialogInterface dialog, int which) {
116                                 dialog.dismiss();
117                                 if (!waitingForResult) { finish(); }
118                         }
119                 });
120                 AlertDialog dialog = builder.create();
121                 dialog.show();
122         }
123
124         private void runEnrollment(int slot) {
125                 Random rng = new Random();
126                 byte[] challenge = new byte[63];
127                 rng.nextBytes(challenge);
128                 Log.v(TAG, "Random challenge: " + hex(challenge));
129                 Log.v(TAG, "Launching challenging activity");
130                 Intent crIntent = new Intent(this, NFCAuthCRCheck.class);
131                 crIntent.putExtra("slot", slot);
132                 crIntent.putExtra("challenge", challenge);
133                 this.startActivityForResult(crIntent, 0);
134                 waitingForResult = true;
135         }
136
137         private String hex(byte[] a) {
138                 StringBuilder sb = new StringBuilder();
139                 if (a == null) return "<null>";
140                 for (byte b: a) sb.append(String.format("%02x", b&0xff));
141                 return sb.toString();
142         }
143 }