4
4
import android .app .Dialog ;
5
5
import android .content .Context ;
6
6
import android .content .DialogInterface ;
7
+ import android .content .Intent ;
8
+ import android .content .SharedPreferences ;
7
9
import android .content .pm .ActivityInfo ;
8
10
import android .os .Bundle ;
9
11
import android .view .KeyEvent ;
13
15
import android .view .WindowManager ;
14
16
import android .webkit .WebView ;
15
17
import android .widget .Button ;
18
+ import android .widget .Toast ;
19
+
20
+ import com .coderbunker .kioskapp .lib .TOTP ;
16
21
17
22
import java .util .ArrayList ;
18
23
import java .util .Arrays ;
24
29
public class KioskActivity extends Activity {
25
30
26
31
private final Context context = this ;
27
- private WebView webView ;
32
+ private WebView webView ;
28
33
private static String password = "1234" ;
29
- private static String URL = "https://naibaben.github.io/ " ;
34
+ private static String URL = "" ;
30
35
31
36
private final List blockedKeys = new ArrayList (Arrays .asList (KeyEvent .KEYCODE_VOLUME_DOWN ,
32
37
KeyEvent .KEYCODE_VOLUME_UP , KeyEvent .KEYCODE_BACK , KeyEvent .KEYCODE_HOME , KeyEvent .KEYCODE_POWER , KeyEvent .KEYCODE_APP_SWITCH ));
@@ -35,17 +40,19 @@ public class KioskActivity extends Activity {
35
40
private boolean locked = false ;
36
41
private Dialog dialog ;
37
42
38
- private Button b1 , b2 , b3 , b4 ;
43
+ private Button b1 , b2 , b3 , b4 , b5 , b6 ;
39
44
private Button n0 , n1 , n2 , n3 , n4 , n5 , n6 , n7 , n8 , n9 ;
40
45
private ArrayList <Button > numbers ;
41
46
42
47
private int cptPwd ;
43
48
44
49
private Timer timerLock , timerNav ;
45
50
51
+ private SharedPreferences prefs ;
52
+
46
53
@ Override
47
54
public void onBackPressed () {
48
- //Do nothing...
55
+ //Do nothing...
49
56
}
50
57
51
58
@ Override
@@ -65,19 +72,25 @@ protected void onCreate(Bundle savedInstanceState) {
65
72
66
73
setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE );
67
74
75
+ prefs = this .getSharedPreferences (
76
+ "com.coderbunker.kioskapp" , Context .MODE_PRIVATE );
77
+
78
+ URL = prefs .getString ("url" , "https://naibaben.github.io/" );
79
+
68
80
//Get the webView and load the URL
69
81
webView = findViewById (R .id .webview );
70
82
webView .setWebViewClient (new KioskWebviewClient ());
71
83
webView .getSettings ().setJavaScriptEnabled (true );
72
84
webView .loadUrl (URL );
85
+ Toast .makeText (this , "Loading " + URL , Toast .LENGTH_SHORT ).show ();
73
86
74
87
webView .setOnTouchListener (new View .OnTouchListener () {
75
88
76
89
@ Override
77
90
public boolean onTouch (View view , MotionEvent motionEvent ) {
78
91
hideSystemUI ();
79
92
80
- if (!dialogPrompted && locked ) {
93
+ if (!dialogPrompted && locked ) {
81
94
askPassword ();
82
95
return true ;
83
96
} else
@@ -133,26 +146,18 @@ private void hideNavBar(View view) {
133
146
}
134
147
135
148
136
-
137
-
138
149
@ Override
139
150
public void onWindowFocusChanged (boolean hasFocus ) {
140
151
super .onWindowFocusChanged (hasFocus );
141
152
if (hasFocus ) {
142
153
hideSystemUI ();
143
- }else {
154
+ } else {
144
155
hideSystemUI ();
145
156
}
146
157
}
147
158
148
159
149
- public static void setPassword (String newPwd )
150
- {
151
- password = newPwd ;
152
- }
153
-
154
- public static void setURL (String newURL )
155
- {
160
+ public static void setURL (String newURL ) {
156
161
URL = newURL ;
157
162
}
158
163
@@ -171,33 +176,54 @@ public void enterNumber(String number) {
171
176
case 3 :
172
177
b4 .setText (number );
173
178
break ;
179
+ case 4 :
180
+ b5 .setText (number );
181
+ break ;
182
+ case 5 :
183
+ b6 .setText (number );
184
+ break ;
174
185
}
175
186
176
- if (cptPwd == 3 ) {
187
+ if (cptPwd == 5 ) {
177
188
cptPwd = 0 ;
178
189
checkPwd ();
179
190
} else
180
191
cptPwd ++;
181
192
182
193
}
183
194
184
- private void checkPwd () {
185
- String pwd = b1 .getText ().toString () + b2 .getText ().toString () + b3 .getText ().toString () + b4 .getText ().toString ();
186
- if (password .equals (pwd )) {
187
- finish ();
195
+ private void launchHome () {
196
+ Intent intent = new Intent (KioskActivity .this , MainActivity .class );
197
+ intent .addFlags (Intent .FLAG_ACTIVITY_NO_HISTORY | Intent .FLAG_ACTIVITY_CLEAR_TOP );
198
+ startActivity (intent );
199
+ finish ();
200
+ }
188
201
202
+ private void checkPwd () {
203
+ String otp = prefs .getString ("otp" , null );
204
+ if (otp == null ) {
205
+ Toast .makeText (context , "Please go to the settings and create a password" , Toast .LENGTH_SHORT ).show ();
206
+ launchHome ();
189
207
} else {
190
- dialog .dismiss ();
191
- dialogPrompted = false ;
208
+ String pwd = b1 .getText ().toString () + b2 .getText ().toString () + b3 .getText ().toString () + b4 .getText ().toString () + b5 .getText ().toString () + b6 .getText ().toString ();
209
+ String generated_number = TOTP .generateCurrentNumber (otp , System .currentTimeMillis ());
210
+ String previous_generated_number = TOTP .generateCurrentNumber (otp , System .currentTimeMillis () - 30000 );
211
+
212
+ if (pwd .equals (generated_number ) || pwd .equals (previous_generated_number )) {
213
+ Toast .makeText (context , "PIN correct" , Toast .LENGTH_SHORT ).show ();
214
+ launchHome ();
215
+ } else {
216
+ dialog .dismiss ();
217
+ dialogPrompted = false ;
218
+ Toast .makeText (context , "Wrong PIN" , Toast .LENGTH_SHORT ).show ();
219
+ }
192
220
}
193
221
194
222
cptPwd = 0 ;
195
223
}
196
224
197
225
@ Override
198
226
public boolean dispatchKeyEvent (KeyEvent event ) {
199
-
200
-
201
227
if (blockedKeys .contains (event .getKeyCode ())) {
202
228
return true ;
203
229
} else {
@@ -207,14 +233,15 @@ public boolean dispatchKeyEvent(KeyEvent event) {
207
233
208
234
@ Override
209
235
public boolean onKeyDown (int keyCode , KeyEvent event ) {
210
- if (blockedKeys .contains (event .getKeyCode ())){
211
- return true ;
236
+ Toast .makeText (context , "Clicked" , Toast .LENGTH_SHORT ).show ();
237
+ if (blockedKeys .contains (event .getKeyCode ())) {
238
+ Toast .makeText (context , "Blocked" , Toast .LENGTH_SHORT ).show ();
239
+ return false ;
212
240
}
213
241
return super .onKeyDown (keyCode , event );
214
242
}
215
243
216
- private void askPassword ()
217
- {
244
+ private void askPassword () {
218
245
dialogPrompted = true ;
219
246
220
247
dialog = new Dialog (webView .getContext ());
@@ -235,6 +262,9 @@ public void onDismiss(DialogInterface dialogInterface) {
235
262
b2 = dialog .findViewById (R .id .b2 );
236
263
b3 = dialog .findViewById (R .id .b3 );
237
264
b4 = dialog .findViewById (R .id .b4 );
265
+ b5 = dialog .findViewById (R .id .b5 );
266
+ b6 = dialog .findViewById (R .id .b6 );
267
+
238
268
239
269
n0 = dialog .findViewById (R .id .number0 );
240
270
n1 = dialog .findViewById (R .id .number1 );
0 commit comments