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

Commit

Permalink
Merge pull request #29 from coderbunker/issue#27-eye_tracking
Browse files Browse the repository at this point in the history
Issue#27 eye tracking
  • Loading branch information
julianschmuckli authored Aug 15, 2018
2 parents fafeb0e + 0ef19c9 commit 5475771
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
/build
/captures
.externalNativeBuild
.idea/caches/build_file_checksums.ser
.idea/caches/build_file_checksums.ser
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
implementation 'gun0912.ted:tedpermission:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<application
android:allowBackup="false"
Expand Down
109 changes: 70 additions & 39 deletions app/src/main/java/com/coderbunker/kioskapp/KioskActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coderbunker.kioskapp;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
Expand All @@ -11,12 +12,15 @@
import android.hardware.Camera;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
Expand All @@ -28,7 +32,11 @@
import com.coderbunker.kioskapp.facerecognition.CameraPreview;
import com.coderbunker.kioskapp.facerecognition.FaceDetectionListener;
import com.coderbunker.kioskapp.lib.HOTP;
import com.coderbunker.kioskapp.lib.SaveAndLoad;
import com.coderbunker.kioskapp.lib.TOTP;
import com.coderbunker.kioskapp.lib.URLRequest;
import com.gun0912.tedpermission.PermissionListener;
import com.gun0912.tedpermission.TedPermission;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -45,7 +53,7 @@ public class KioskActivity extends Activity implements Observer {
private WebView webView;
private TextView face_detection_score, face_counter_view;
private static String password = "1234";
private static String URL = "";
private static String url = "";

private final List blockedKeys = new ArrayList(Arrays.asList(KeyEvent.KEYCODE_VOLUME_DOWN,
KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_HOME, KeyEvent.KEYCODE_POWER, KeyEvent.KEYCODE_APP_SWITCH));
Expand All @@ -68,6 +76,8 @@ public class KioskActivity extends Activity implements Observer {
private Camera mCamera;
private CameraPreview mCameraPreview;

private boolean enableCaching = false;

@Override
public void onBackPressed() {
//Do nothing...
Expand All @@ -93,7 +103,7 @@ protected void onCreate(Bundle savedInstanceState) {
prefs = this.getSharedPreferences(
"com.coderbunker.kioskapp", Context.MODE_PRIVATE);

URL = prefs.getString("url", "https://coderbunker.github.io/kiosk-web/");
url = prefs.getString("url", "https://coderbunker.github.io/kiosk-web/");
String otp = prefs.getString("otp", null);

if (otp == null) {
Expand Down Expand Up @@ -127,6 +137,26 @@ public void run() {
timerLock.schedule(lock, 5000);
}

@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
System.out.println("Test: " + request.getUrl().toString());

if (enableCaching) {
if (request.getUrl().toString().contains(".mp4") || request.getUrl().toString().contains(".wav")) {
String[] url_parts = request.getUrl().toString().split("/");
String file_name = url_parts[url_parts.length - 1];

if (SaveAndLoad.readFromFile(file_name, KioskActivity.this).equals("")) {
URLRequest.startDownload(request.getUrl().toString(), file_name);
}
return new WebResourceResponse(SaveAndLoad.getMimeType(request.getUrl().toString()), "UTF-8", SaveAndLoad.readFromFileAndReturnInputStream(file_name, KioskActivity.this));

}
}
return super.shouldInterceptRequest(view, request);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains(url)) {
Expand All @@ -141,14 +171,15 @@ public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError e
handler.proceed(); //Ignore SSL certificate error
}
});

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setAppCacheMaxSize(5000 * 1000 * 1000);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.getSettings().setAppCacheMaxSize(200 * 1024 * 1024);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
webView.loadUrl(URL);
webView.loadUrl(url);

Toast.makeText(this, "Loading " + URL, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Loading " + url, Toast.LENGTH_SHORT).show();

//Touch events for password
webView.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -207,22 +238,41 @@ public void run() {


if (checkCameraHardware(this)) {
mCamera = getCameraInstance();
if (mCamera != null) {

FaceDetectionListener faceDetectionListener = new FaceDetectionListener();
faceDetectionListener.addObserver(this);
mCamera.setFaceDetectionListener(faceDetectionListener);
PermissionListener permissionlistener = new PermissionListener() {
@Override
public void onPermissionGranted() {
try {
mCamera.unlock();
} catch (Exception e) {

mCameraPreview = new CameraPreview(this, mCamera);
}
mCamera = getCameraInstance();
if (mCamera != null) {
FaceDetectionListener faceDetectionListener = new FaceDetectionListener();
faceDetectionListener.addObserver(KioskActivity.this);
mCamera.setFaceDetectionListener(faceDetectionListener);

FrameLayout preview = findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);
mCameraPreview = new CameraPreview(context, mCamera);

mCamera.startPreview();
} else {
FrameLayout preview = findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);

}
mCamera.startPreview();
Toast.makeText(context, "Face recognition started", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Due a camera issue the face recognition can not be started.", Toast.LENGTH_LONG).show();
}
}

@Override
public void onPermissionDenied(ArrayList<String> deniedPermissions) {
Toast.makeText(context, "Face recognition not active due denied permissions.", Toast.LENGTH_SHORT).show();
}

};

TedPermission.with(context).setPermissionListener(permissionlistener).setPermissions(Manifest.permission.CAMERA).check();
}


Expand Down Expand Up @@ -465,31 +515,12 @@ protected void onPause() {
super.onPause();
}

private long last_detected = 0;
private long face_current_counter = 0;
private long face_counter = 0;

@Override
public void update(Observable o, Object arg) {
if (o instanceof FaceDetectionListener) {
Camera.Face face = ((Camera.Face) arg);

face_detection_score.setText("Score:" + face.score);

if (face.score >= 85) {
face_current_counter++;
} else {
face_current_counter = 0;
}

if (face_current_counter >= 5 && last_detected < System.currentTimeMillis() + 45000) {
face_counter++;
last_detected = System.currentTimeMillis();
face_current_counter = -5000;
}

face_counter_view.setText("Viewers: " + face_counter);

Camera.Face[] faces = ((Camera.Face[]) arg);
//face_detection_score.setText("Faces:" + faces.length);
face_counter_view.setText("Current faces: " + faces.length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ public void startFaceDetection() {
// start face detection only *after* preview has started
if (params.getMaxNumDetectedFaces() > 0) {
// camera supports face detection, so can start it:
mCamera.startFaceDetection();
try {
mCamera.startFaceDetection();
} catch (RuntimeException ex) {
System.out.println("Face recognition not started");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@ public class FaceDetectionListener extends Observable implements Camera.FaceDete

@Override
public void onFaceDetection(Camera.Face[] faces, Camera camera) {
if (faces.length > 0) {
for (Camera.Face face : faces) {
try {
/*System.out.println("--------------------------");
System.out.println(face.score);*/
setChanged();
notifyObservers(face);
} catch (Exception e) {
e.printStackTrace();
}
}
}
setChanged();
notifyObservers(faces);
}

}
88 changes: 88 additions & 0 deletions app/src/main/java/com/coderbunker/kioskapp/lib/SaveAndLoad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.coderbunker.kioskapp.lib;

import android.content.Context;
import android.util.Log;
import android.webkit.MimeTypeMap;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class SaveAndLoad {
//Source: https://stackoverflow.com/questions/14376807/how-to-read-write-string-from-a-file-in-android

public static void writeToFile(String filename, String data, Context ctx) {
try {
FileOutputStream fou = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fou);
try {
osw.write(data);
osw.flush();
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public static String readFromFile(String filename, Context context) {

String ret = "";

try {
InputStream inputStream = context.openFileInput(filename);

if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();

while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}

inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}

return ret;
}

public static InputStream readFromFileAndReturnInputStream(String filename, Context context) {

String ret = "";

try {
InputStream inputStream = context.openFileInput(filename);

return inputStream;
} catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}

return null;
}

public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
}
Loading

0 comments on commit 5475771

Please sign in to comment.