Wear: make rotarty input plus minus dynamic

This commit is contained in:
Andries Smit 2022-01-12 17:17:43 +01:00
parent 9027c53c85
commit 90e9350f6b

View file

@ -3,19 +3,20 @@ package info.nightscout.androidaps.interaction.utils;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.wearable.input.RotaryEncoder;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.view.InputDeviceCompat;
import androidx.core.view.MotionEventCompat;
import java.text.NumberFormat;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Created by mike on 28.06.2016.
*/
@ -35,6 +36,12 @@ public class PlusMinusEditText implements View.OnKeyListener,
boolean allowZero = false;
boolean roundRobin;
private int mChangeCounter = 0;
private long mLastChange = 0;
private final static int THRESHOLD_COUNTER = 5;
private final static int THRESHOLD_COUNTER_LONG = 10;
private final static int THRESHOLD_TIME = 100;
private final Handler mHandler;
private ScheduledExecutorService mUpdater;
@ -121,6 +128,7 @@ public class PlusMinusEditText implements View.OnKeyListener,
public void setStep(Double step) {
this.step = step;
}
private void inc(int multiplier) {
value += step * multiplier;
if (value > maxValue) {
@ -211,13 +219,21 @@ public class PlusMinusEditText implements View.OnKeyListener,
@Override
public boolean onGenericMotion(View v, MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_SCROLL && RotaryEncoder.isFromRotaryEncoder(ev)) {
float delta = -RotaryEncoder.getRotaryAxisValue(ev);
if (ev.getAction() == MotionEvent.ACTION_SCROLL && ev.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)) {
long now = System.currentTimeMillis();
if (now - mLastChange > THRESHOLD_TIME) mChangeCounter = 0;
int dynamicMultiplier = mChangeCounter < THRESHOLD_COUNTER ? 1 :
mChangeCounter < THRESHOLD_COUNTER_LONG ? 2 : 4;
float delta = -ev.getAxisValue(MotionEventCompat.AXIS_SCROLL);
if (delta > 0) {
inc(1);
inc(dynamicMultiplier);
} else {
dec(1);
dec(dynamicMultiplier);
}
mLastChange = System.currentTimeMillis();
mChangeCounter++;
return true;
}
return false;