I want to make an alert after the user's eyelid is closed for more than 3 seconds but I have a problem with my code. How can I make the boolean right_eye and boolean left_eye trigger the alert after 3 seconds it closed? Here is some code that I wrote.
public void eyeTracking(FirebaseVisionFace face){
boolean right_eye = face.getRightEyeOpenProbability() < 0.5;
boolean left_eye = face.getLeftEyeOpenProbability() < 0.5;
Handler mHandler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
}
};
if (right_eye && left_eye) {
begin = System.currentTimeMillis();
Log.d(TAG, "eyeTracking: " + sleep);
mHandler.postDelayed(runnable, 3000);
sleep = true;
}
else {
sleep = false;
}
if(sleep){
stop = System.currentTimeMillis();
Log.d(TAG, "eyeTracking: " + begin + "stop" + stop);
if(begin - stop >= 3000) {
alertBox();
}
}
}
when i try log my begin and stop variable it returns same value does i do it wrong?
You should not set your begin variable every time you detect eye is closed. Try this bellow code on your project. Hope this will help you:
long begin=0;
public void eyeTracking(FirebaseVisionFace face){
boolean right_eye = face.getRightEyeOpenProbability() < 0.5;
boolean left_eye = face.getLeftEyeOpenProbability() < 0.5;
if (right_eye && left_eye) {
//if your begin variable is reset
if(begin==0){
begin = System.currentTimeMillis();
}
Log.d(TAG, "eyeTracking: " + sleep);
sleep = true;
}
else {
//reset your begin variable
begin=0;
sleep = false;
}
Log.d(TAG, "Eyes closed time: "+System.currentTimeMillis()-begin);
if(sleep && System.currentTimeMillis()-begin>3000){
Log.d(TAG, "Show alert");
alertBox();
}
}