Merge pull request #1164 from Andries-Smit/wear-improve-rotary-input

Wear: improve rotary input
This commit is contained in:
Milos Kozak 2022-01-12 21:16:45 +01:00 committed by GitHub
commit 5ba127fe17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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