Improve key repeat

This commit is contained in:
juloo 2015-08-05 01:30:56 +02:00
parent bee4334626
commit 117e4a3d4f

View File

@ -6,23 +6,25 @@ import android.graphics.RectF;
import android.graphics.Paint; import android.graphics.Paint;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator; import android.os.Vibrator;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import java.util.LinkedList; import java.util.LinkedList;
public class Keyboard2View extends View public class Keyboard2View extends View
implements View.OnTouchListener implements View.OnTouchListener, Handler.Callback
{ {
private static final float KEY_PER_ROW = 10; private static final float KEY_PER_ROW = 10;
private static final float SUB_VALUE_DIST = 7f; private static final float SUB_VALUE_DIST = 7f;
private static final long VIBRATE_LONG = 25; private static final long VIBRATE_DURATION = 20;
private static final long VIBRATE_MIN_INTERVAL = 100; private static final long VIBRATE_MIN_INTERVAL = 100;
private static final long LONGPRESS_TIMEOUT = 800; private static final long LONGPRESS_TIMEOUT = 600;
private static final long LONGPRESS_INTERVAL = 90; private static final long LONGPRESS_INTERVAL = 65;
private Keyboard2 _ime; private Keyboard2 _ime;
private KeyboardData _keyboard; private KeyboardData _keyboard;
@ -34,6 +36,9 @@ public class Keyboard2View extends View
private Vibrator _vibratorService; private Vibrator _vibratorService;
private long _lastVibration = 0; private long _lastVibration = 0;
private Handler _handler;
private static int _currentWhat = 0;
private float _verticalMargin; private float _verticalMargin;
private float _horizontalMargin; private float _horizontalMargin;
private float _keyWidth; private float _keyWidth;
@ -52,6 +57,7 @@ public class Keyboard2View extends View
{ {
super(context, attrs); super(context, attrs);
_vibratorService = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); _vibratorService = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
_handler = new Handler(this);
_verticalMargin = getResources().getDimension(R.dimen.vertical_margin); _verticalMargin = getResources().getDimension(R.dimen.vertical_margin);
_horizontalMargin = getResources().getDimension(R.dimen.horizontal_margin); _horizontalMargin = getResources().getDimension(R.dimen.horizontal_margin);
_keyHeight = getResources().getDimension(R.dimen.key_height); _keyHeight = getResources().getDimension(R.dimen.key_height);
@ -147,21 +153,16 @@ public class Keyboard2View extends View
newValue = key.key.key4; newValue = key.key.key4;
if (newValue != null && newValue != key.value) if (newValue != null && newValue != key.value)
{ {
key.setValue(newValue); if (key.timeoutWhat != -1)
{
_handler.removeMessages(key.timeoutWhat);
_handler.sendEmptyMessageDelayed(key.timeoutWhat, LONGPRESS_TIMEOUT);
}
key.value = newValue;
key.flags = newValue.getFlags();
updateFlags(); updateFlags();
vibrate(); vibrate();
} }
else if (key.value != null && (key.flags & KeyValue.FLAG_NOCHAR) == 0)
{
long now = System.currentTimeMillis();
if (now >= key.nextPress)
{
key.nextPress = now + LONGPRESS_INTERVAL;
_ime.handleKeyUp(key.value, _flags);
vibrate();
}
}
} }
} }
@ -195,7 +196,11 @@ public class Keyboard2View extends View
down.pointerId = pointerId; down.pointerId = pointerId;
} }
else else
_downKeys.add(new KeyDown(pointerId, key, touchX, touchY)); {
int what = _currentWhat++;
_handler.sendEmptyMessageDelayed(what, LONGPRESS_TIMEOUT);
_downKeys.add(new KeyDown(pointerId, key, touchX, touchY, what));
}
vibrate(); vibrate();
updateFlags(); updateFlags();
invalidate(); invalidate();
@ -212,6 +217,11 @@ public class Keyboard2View extends View
if (k != null) if (k != null)
{ {
if (k.timeoutWhat != -1)
{
_handler.removeMessages(k.timeoutWhat);
k.timeoutWhat = -1;
}
if ((k.flags & KeyValue.FLAG_KEEP_ON) != 0) if ((k.flags & KeyValue.FLAG_KEEP_ON) != 0)
{ {
k.flags ^= KeyValue.FLAG_KEEP_ON; k.flags ^= KeyValue.FLAG_KEEP_ON;
@ -251,7 +261,7 @@ public class Keyboard2View extends View
_lastVibration = now; _lastVibration = now;
try try
{ {
_vibratorService.vibrate(VIBRATE_LONG); _vibratorService.vibrate(VIBRATE_DURATION);
} }
catch (Exception e) catch (Exception e)
{ {
@ -260,6 +270,25 @@ public class Keyboard2View extends View
} }
} }
@Override
public boolean handleMessage(Message msg)
{
long now = System.currentTimeMillis();
for (KeyDown key : _downKeys)
{
if (key.timeoutWhat == msg.what)
{
_handler.sendEmptyMessageDelayed(msg.what, LONGPRESS_INTERVAL);
_ime.handleKeyUp(key.value, _flags);
vibrate();
return (true);
}
}
Keyboard2.log("problem: cannot handle this message");
return (false);
}
@Override @Override
public void onMeasure(int wSpec, int hSpec) public void onMeasure(int wSpec, int hSpec)
{ {
@ -330,22 +359,17 @@ public class Keyboard2View extends View
public float downX; public float downX;
public float downY; public float downY;
public int flags; public int flags;
public long nextPress; public int timeoutWhat;
public KeyDown(int pointerId, KeyboardData.Key key, float x, float y) public KeyDown(int pointerId, KeyboardData.Key key, float x, float y, int what)
{ {
this.pointerId = pointerId; this.pointerId = pointerId;
value = key.key0;
this.key = key; this.key = key;
downX = x; downX = x;
downY = y; downY = y;
setValue(key.key0); flags = (value == null) ? 0 : value.getFlags();
} timeoutWhat = what;
public void setValue(KeyValue v)
{
value = v;
flags = (value == null) ? 0 : v.getFlags();
nextPress = System.currentTimeMillis() + LONGPRESS_TIMEOUT;
} }
} }
} }