package me.devsaki.hentoid.widget;

import android.view.KeyEvent;
import android.view.View;

public final class VolumeKeyListener implements View.OnKeyListener {

    private Runnable onVolumeDownKeyListener;

    private Runnable onVolumeUpKeyListener;

    public VolumeKeyListener setOnVolumeDownKeyListener(Runnable onVolumeDownKeyListener) {
        this.onVolumeDownKeyListener = onVolumeDownKeyListener;
        return this;
    }

    public VolumeKeyListener setOnVolumeUpKeyListener(Runnable onVolumeUpKeyListener) {
        this.onVolumeUpKeyListener = onVolumeUpKeyListener;
        return this;
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            onVolumeDownKeyListener.run();
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
            onVolumeUpKeyListener.run();
            return true;
        }
        return false;
    }
}

@@ -86,16 +86,44 @@
 .View;%0A%0A
+import java.util.Calendar;%0A%0A
 public f
@@ -184,16 +184,64 @@
 ener %7B%0A%0A
+    private static int ACTION_DELAY_MS = 1000;%0A%0A
     priv
@@ -320,24 +320,106 @@
 yListener;%0A%0A
+    private long lastKeyDownEventTick = -1;%0A    private boolean isTurbo = false;%0A%0A
     public V
@@ -915,189 +915,1253 @@
     
-onVolumeDownKeyListener.run();%0A            return true;%0A        %7D else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) %7B%0A            onVolumeUpKeyListener.run();%0A            return true;
+if (event.getAction() == KeyEvent.ACTION_DOWN) %7B%0A                long timeNow = Calendar.getInstance().getTimeInMillis();%0A                if (timeNow - lastKeyDownEventTick %3E ACTION_DELAY_MS / (isTurbo ? 2 : 1)) %7B%0A                    if (-1 == lastKeyDownEventTick) isTurbo = true;%0A                    lastKeyDownEventTick = timeNow;%0A                    onVolumeDownKeyListener.run();%0A                %7D%0A                return true;%0A            %7D else if (event.getAction() == KeyEvent.ACTION_UP) %7B%0A                lastKeyDownEventTick = -1;%0A                isTurbo = false;%0A            %7D%0A        %7D else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) %7B%0A            if (event.getAction() == KeyEvent.ACTION_DOWN) %7B%0A                long timeNow = Calendar.getInstance().getTimeInMillis();%0A                if (timeNow - lastKeyDownEventTick %3E ACTION_DELAY_MS / (isTurbo ? 2 : 1)) %7B%0A                    if (-1 == lastKeyDownEventTick) isTurbo = true;%0A                    lastKeyDownEventTick = timeNow;%0A                    onVolumeUpKeyListener.run();%0A                %7D%0A                return true;%0A            %7D else if (event.getAction() == KeyEvent.ACTION_UP) %7B%0A                lastKeyDownEventTick = -1;%0A                isTurbo = false;%0A            %7D
 %0A   

Fix to volume key listening (#295) with all the behaviour implemented in the same class