]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/Enroll.java
8d90488dbc41e34e665aea4e9c84a76b71b93211
[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.TextView;
16 import android.widget.RadioButton;
17
18 import org.average.nfcauthcr.Check;
19
20 public class Enroll 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         private AlertDialog swipeDialog;
28
29         @Override
30         protected void onCreate(Bundle savedInstanceState)
31         {
32                 super.onCreate(savedInstanceState);
33                 Log.v(TAG, "Starting");
34                 prefs = PreferenceManager.getDefaultSharedPreferences(this);
35         }
36
37         @Override
38         protected void onResume() {
39                 super.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;
44                 switch (slot) {
45                 case 1: btn = (RadioButton)findViewById(R.id.slot_1);
46                         break;
47                 case 2: btn = (RadioButton)findViewById(R.id.slot_2);
48                         break;
49                 }
50                 if (btn != null) btn.setChecked(true);
51         }
52
53         @Override
54         protected void onPause() {
55                 super.onPause();
56                 Log.v(TAG, "Going inactive, try to stop");
57                 if (!waitingForResult) { finish(); }
58         }
59
60         @Override
61         protected void onStop() {
62                 super.onStop();
63                 Log.v(TAG, "Stop requested");
64         }
65
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;
72                 }
73                 Editor editor = prefs.edit();
74                 editor.putInt("slot_number", slot);
75                 editor.commit();
76                 Log.v(TAG, "stored slot number " + slot);
77         }
78
79         public void onEnrollClicked(View view) {
80                 Log.v(TAG, "Enroll clicked");
81                 if (slot > 0) {
82                         runEnrollment(slot);
83                 } else {
84                         showEnrollResult(R.string.need_slot);
85                 }
86         }
87
88         public void onActivityResult(int requestCode, int resultCode,
89                                         Intent intent) {
90                 Log.v(TAG, "Got activity result");
91                 waitingForResult = false;
92                 if(swipeDialog != null) {
93                         swipeDialog.dismiss();
94                         swipeDialog = null;
95                 }
96
97                 if (resultCode != RESULT_OK) {
98                         Log.v(TAG, "Error result code " + resultCode);
99                         return;
100                 }
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));
108                 editor.commit();
109                 showEnrollResult(R.string.enroll_success);
110                 Log.v(TAG, "stored new challenge and response");
111         }
112
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) {
120                                 dialog.dismiss();
121                                 if (!waitingForResult) { finish(); }
122                         }
123                 });
124                 AlertDialog dialog = builder.create();
125                 dialog.show();
126         }
127
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, Check.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();
140                 swipeDialog.show();
141         }
142
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");
151                         }
152                 });
153                 return builder.create();
154         }
155
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();
161         }
162 }