Skip to content
This repository was archived by the owner on Sep 13, 2024. It is now read-only.

Commit 258a751

Browse files
Merge pull request #18 from coderbunker/issue#5-otp_new
TOTP done
2 parents 6f00903 + 26a7aa7 commit 258a751

File tree

11 files changed

+480
-109
lines changed

11 files changed

+480
-109
lines changed

.idea/caches/build_file_checksums.ser

-1 Bytes
Binary file not shown.

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ android {
2020

2121
dependencies {
2222
implementation fileTree(dir: 'libs', include: ['*.jar'])
23-
implementation 'com.android.support:appcompat-v7:27.0.0'
23+
implementation 'com.android.support:appcompat-v7:27.1.1'
2424
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
2525
implementation 'com.android.support:support-v4:27.1.1'
26+
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
2627
testImplementation 'junit:junit:4.12'
2728
androidTestImplementation 'com.android.support.test:runner:1.0.2'
2829
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

app/src/main/AndroidManifest.xml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@
88

99

1010
<application
11-
android:allowBackup="true"
11+
android:allowBackup="false"
1212
android:icon="@mipmap/ic_launcher"
1313
android:label="@string/app_name"
14+
android:launchMode="singleTask"
1415
android:roundIcon="@mipmap/ic_launcher_round"
15-
android:supportsRtl="true"
16-
android:theme="@style/AppTheme">
17-
<activity android:name=".MainActivity">
18-
<intent-filter>
19-
<action android:name="android.intent.action.MAIN" />
16+
android:stateNotNeeded="true"
17+
android:supportsRtl="true">
18+
<activity
19+
android:name=".MainActivity"
20+
android:screenOrientation="landscape">
2021

21-
<category android:name="android.intent.category.LAUNCHER" />
22-
</intent-filter>
2322
</activity>
2423
<activity android:name=".KioskActivity">
2524
<intent-filter>
25+
<action android:name="android.intent.action.MAIN" />
26+
27+
<category android:name="android.intent.category.LAUNCHER" />
2628
<category android:name="android.intent.category.HOME" />
2729
<category android:name="android.intent.category.DEFAULT" />
2830
</intent-filter>
2931
</activity>
3032

31-
<activity android:name=".SettingsActivity"></activity>
33+
<activity
34+
android:name=".SettingsActivity"
35+
android:screenOrientation="landscape" />
3236
</application>
3337

3438
</manifest>

app/src/main/java/com/coderbunker/kioskapp/KioskActivity.java

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.app.Dialog;
55
import android.content.Context;
66
import android.content.DialogInterface;
7+
import android.content.Intent;
8+
import android.content.SharedPreferences;
79
import android.content.pm.ActivityInfo;
810
import android.os.Bundle;
911
import android.view.KeyEvent;
@@ -13,6 +15,9 @@
1315
import android.view.WindowManager;
1416
import android.webkit.WebView;
1517
import android.widget.Button;
18+
import android.widget.Toast;
19+
20+
import com.coderbunker.kioskapp.lib.TOTP;
1621

1722
import java.util.ArrayList;
1823
import java.util.Arrays;
@@ -24,9 +29,9 @@
2429
public 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);

app/src/main/java/com/coderbunker/kioskapp/KioskWebviewClient.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
import android.webkit.SslErrorHandler;
55
import android.webkit.WebView;
66
import android.webkit.WebViewClient;
7+
import android.widget.Toast;
78

89
class KioskWebviewClient extends WebViewClient {
910

10-
private String URL = "https://naibaben.github.io/";
11-
1211
@Override
1312
public boolean shouldOverrideUrlLoading(WebView view, String url) {
14-
15-
if(url.contains(url)) {
13+
if (url.contains(url)) {
1614
view.loadUrl(url);
1715
}
1816
return true;

app/src/main/java/com/coderbunker/kioskapp/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ protected void onCreate(Bundle savedInstanceState) {
1616
super.onCreate(savedInstanceState);
1717
setContentView(R.layout.activity_main);
1818

19-
kioskmode = (Button) findViewById(R.id.button_kioskmode);
20-
settings = (Button) findViewById(R.id.button_settings);
19+
kioskmode = findViewById(R.id.button_kioskmode);
20+
settings = findViewById(R.id.button_settings);
2121

2222
kioskmode.setOnClickListener(new View.OnClickListener() {
2323
@Override

0 commit comments

Comments
 (0)