44import android .app .Dialog ;
55import android .content .Context ;
66import android .content .DialogInterface ;
7+ import android .content .Intent ;
8+ import android .content .SharedPreferences ;
79import android .content .pm .ActivityInfo ;
810import android .os .Bundle ;
911import android .view .KeyEvent ;
1315import android .view .WindowManager ;
1416import android .webkit .WebView ;
1517import android .widget .Button ;
18+ import android .widget .Toast ;
19+
20+ import com .coderbunker .kioskapp .lib .TOTP ;
1621
1722import java .util .ArrayList ;
1823import java .util .Arrays ;
2429public class KioskActivity extends Activity {
2530
2631 private final Context context = this ;
27- private WebView webView ;
32+ private WebView webView ;
2833 private static String password = "1234" ;
29- private static String URL = "https://naibaben.github.io/ " ;
34+ private static String URL = "" ;
3035
3136 private final List blockedKeys = new ArrayList (Arrays .asList (KeyEvent .KEYCODE_VOLUME_DOWN ,
3237 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 {
3540 private boolean locked = false ;
3641 private Dialog dialog ;
3742
38- private Button b1 , b2 , b3 , b4 ;
43+ private Button b1 , b2 , b3 , b4 , b5 , b6 ;
3944 private Button n0 , n1 , n2 , n3 , n4 , n5 , n6 , n7 , n8 , n9 ;
4045 private ArrayList <Button > numbers ;
4146
4247 private int cptPwd ;
4348
4449 private Timer timerLock , timerNav ;
4550
51+ private SharedPreferences prefs ;
52+
4653 @ Override
4754 public void onBackPressed () {
48- //Do nothing...
55+ //Do nothing...
4956 }
5057
5158 @ Override
@@ -65,19 +72,25 @@ protected void onCreate(Bundle savedInstanceState) {
6572
6673 setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE );
6774
75+ prefs = this .getSharedPreferences (
76+ "com.coderbunker.kioskapp" , Context .MODE_PRIVATE );
77+
78+ URL = prefs .getString ("url" , "https://naibaben.github.io/" );
79+
6880 //Get the webView and load the URL
6981 webView = findViewById (R .id .webview );
7082 webView .setWebViewClient (new KioskWebviewClient ());
7183 webView .getSettings ().setJavaScriptEnabled (true );
7284 webView .loadUrl (URL );
85+ Toast .makeText (this , "Loading " + URL , Toast .LENGTH_SHORT ).show ();
7386
7487 webView .setOnTouchListener (new View .OnTouchListener () {
7588
7689 @ Override
7790 public boolean onTouch (View view , MotionEvent motionEvent ) {
7891 hideSystemUI ();
7992
80- if (!dialogPrompted && locked ) {
93+ if (!dialogPrompted && locked ) {
8194 askPassword ();
8295 return true ;
8396 } else
@@ -133,26 +146,18 @@ private void hideNavBar(View view) {
133146 }
134147
135148
136-
137-
138149 @ Override
139150 public void onWindowFocusChanged (boolean hasFocus ) {
140151 super .onWindowFocusChanged (hasFocus );
141152 if (hasFocus ) {
142153 hideSystemUI ();
143- }else {
154+ } else {
144155 hideSystemUI ();
145156 }
146157 }
147158
148159
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 ) {
156161 URL = newURL ;
157162 }
158163
@@ -171,33 +176,54 @@ public void enterNumber(String number) {
171176 case 3 :
172177 b4 .setText (number );
173178 break ;
179+ case 4 :
180+ b5 .setText (number );
181+ break ;
182+ case 5 :
183+ b6 .setText (number );
184+ break ;
174185 }
175186
176- if (cptPwd == 3 ) {
187+ if (cptPwd == 5 ) {
177188 cptPwd = 0 ;
178189 checkPwd ();
179190 } else
180191 cptPwd ++;
181192
182193 }
183194
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+ }
188201
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 ();
189207 } 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+ }
192220 }
193221
194222 cptPwd = 0 ;
195223 }
196224
197225 @ Override
198226 public boolean dispatchKeyEvent (KeyEvent event ) {
199-
200-
201227 if (blockedKeys .contains (event .getKeyCode ())) {
202228 return true ;
203229 } else {
@@ -207,14 +233,15 @@ public boolean dispatchKeyEvent(KeyEvent event) {
207233
208234 @ Override
209235 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 ;
212240 }
213241 return super .onKeyDown (keyCode , event );
214242 }
215243
216- private void askPassword ()
217- {
244+ private void askPassword () {
218245 dialogPrompted = true ;
219246
220247 dialog = new Dialog (webView .getContext ());
@@ -235,6 +262,9 @@ public void onDismiss(DialogInterface dialogInterface) {
235262 b2 = dialog .findViewById (R .id .b2 );
236263 b3 = dialog .findViewById (R .id .b3 );
237264 b4 = dialog .findViewById (R .id .b4 );
265+ b5 = dialog .findViewById (R .id .b5 );
266+ b6 = dialog .findViewById (R .id .b6 );
267+
238268
239269 n0 = dialog .findViewById (R .id .number0 );
240270 n1 = dialog .findViewById (R .id .number1 );
0 commit comments