0

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?

  • _"my begin and stop variable it returns same value"_ They are assigned a value basically at the same time: first `stop` is assigned in the `if (right_eye && left_eye) {...}` branch and right after that `begin` is assigned in the `if(sleep){...}` branch. So, something's wrong in your application logic. – Markus Kauppinen Mar 12 '19 at 08:39
  • yeah even when i delayed it still the same return value log. Can i know if i call the System.currentTimeMillis() in wrong place? I have edited the question to show you – shafiq ruslan Mar 12 '19 at 09:06
  • Currently the `TimerTask` has no effect on anything. It just runs the empty `run()` method after a few seconds, but still everything after it is run immediately. – Markus Kauppinen Mar 12 '19 at 09:10
  • so does it means my begin variable get System.currentTimeMillis before the delayed? – shafiq ruslan Mar 12 '19 at 09:27
  • Yep. You'll need to look at e.g. [How to call a method after a delay in Android](https://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android). But it would of course be useful to read about threads, runnables and handlers so that you actually know what you are doing. – Markus Kauppinen Mar 12 '19 at 10:06
  • yep i know i also have do handler.postDelayed(). but the log is still return the same for both variable. Can you explain me why my log return the same? – shafiq ruslan Mar 12 '19 at 10:12

1 Answers1

1

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();
    }
}
Huy Le
  • 34
  • 3
  • thanks for the solution. but i think you have misunderstood my question. I want to popup the alert if the eyelid is closed more than 3 seconds. Your solution is still alert can popup if the eyelid is close in less three seconds. – shafiq ruslan Mar 12 '19 at 07:31
  • Well, in that case i suggest you move your attribute include sleep,begin out of your function like this : `boolean sleep; long beginToSleep=0;` And your function need to check like this : `if (right_eye && left_eye) { if(beginToSleep == 0) { beginToSleep= System.currentTimeMillis(); } sleep = true; Log.d(TAG, "eyeTracking: " + sleep + stop); if(System.currentTimeMills()-beginToSleep>5000) { //make alert }} else { beginToSleep=0; sleep = false; }}` – Huy Le Mar 12 '19 at 07:55
  • Sorry but your code is not working. it's weird when i try log my begin and stop variable, it returns the same value. – shafiq ruslan Mar 12 '19 at 08:30
  • if you put your var in the function, it will be reset every time when the function is called. So you have to move them – Huy Le Mar 12 '19 at 09:33
  • how to do it? sorry i'm still beginner in this – shafiq ruslan Mar 12 '19 at 09:45
  • even i defined the var as global variable it still same return value – shafiq ruslan Mar 12 '19 at 09:56
  • could you update your code ? i saw that your begin and stop variable still not set as global variable – Huy Le Mar 12 '19 at 10:18
  • hi @shafiqruslan i have updated my post. Let's try this code – Huy Le Mar 12 '19 at 13:21
  • Such a careless me with the logic. Thank you bro it worked. – shafiq ruslan Mar 12 '19 at 17:14