diff options
| author | Jules Aguillon | 2021-12-19 19:44:27 +0100 |
|---|---|---|
| committer | Jules Aguillon | 2021-12-19 19:44:27 +0100 |
| commit | 1cfecbdf48edb65cc0be8b017c8f389f91212a32 (patch) | |
| tree | c15f27b20e63aaf959cbf7d91701b3cfff01b098 /srcs/juloo.keyboard2/Keyboard2View.java | |
| parent | 988d8db7e8a6f539a8e6f40c66df7b87571a88e9 (diff) | |
| download | unexpected-keyboard-1cfecbdf48edb65cc0be8b017c8f389f91212a32.tar.gz unexpected-keyboard-1cfecbdf48edb65cc0be8b017c8f389f91212a32.zip | |
Auto-format Java and XML files
Use xmllint.
Re-indent Java files using spaces.
Diffstat (limited to 'srcs/juloo.keyboard2/Keyboard2View.java')
| -rw-r--r-- | srcs/juloo.keyboard2/Keyboard2View.java | 596 |
1 files changed, 298 insertions, 298 deletions
diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index 5cbdfd0..c4f8462 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -17,32 +17,32 @@ import android.widget.PopupWindow; import java.util.ArrayList; public class Keyboard2View extends View - implements View.OnTouchListener, Handler.Callback + implements View.OnTouchListener, Handler.Callback { - private static final long VIBRATE_MIN_INTERVAL = 100; + private static final long VIBRATE_MIN_INTERVAL = 100; - private KeyboardData _keyboard; + private KeyboardData _keyboard; - private ArrayList<KeyDown> _downKeys = new ArrayList<KeyDown>(); + private ArrayList<KeyDown> _downKeys = new ArrayList<KeyDown>(); - private int _flags = 0; + private int _flags = 0; - private Vibrator _vibratorService; - private long _lastVibration = 0; + private Vibrator _vibratorService; + private long _lastVibration = 0; - private Handler _handler; - private static int _currentWhat = 0; + private Handler _handler; + private static int _currentWhat = 0; - private Config _config; + private Config _config; - private float _keyWidth; + private float _keyWidth; - private Paint _keyBgPaint = new Paint(); - private Paint _keyDownBgPaint = new Paint(); - private Paint _keyLabelPaint; - private Paint _keySubLabelPaint; - private Paint _specialKeyLabelPaint; - private Paint _specialKeySubLabelPaint; + private Paint _keyBgPaint = new Paint(); + private Paint _keyDownBgPaint = new Paint(); + private Paint _keyLabelPaint; + private Paint _keySubLabelPaint; + private Paint _specialKeyLabelPaint; + private Paint _specialKeySubLabelPaint; private int _lockedColor; private int _activatedColor; private int _labelColor; @@ -50,49 +50,49 @@ public class Keyboard2View extends View private float _labelTextSize; private float _sublabelTextSize; - private static RectF _tmpRect = new RectF(); + private static RectF _tmpRect = new RectF(); - public Keyboard2View(Context context, AttributeSet attrs) - { - super(context, attrs); - _vibratorService = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); - _handler = new Handler(this); + public Keyboard2View(Context context, AttributeSet attrs) + { + super(context, attrs); + _vibratorService = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); + _handler = new Handler(this); refreshConfig(((Keyboard2)context).getConfig(), null); - setOnTouchListener(this); - } + setOnTouchListener(this); + } /* Internally calls [reset()]. */ public void refreshConfig(Config config, KeyboardData kw) { Resources res = getResources(); - _config = config; + _config = config; _lockedColor = res.getColor(R.color.key_label_locked); _activatedColor = res.getColor(R.color.key_label_activated); _labelColor = res.getColor(R.color.key_label); _subLabelColor = res.getColor(R.color.key_sub_label); _labelTextSize = res.getDimension(R.dimen.label_text_size) * config.characterSize; _sublabelTextSize = res.getDimension(R.dimen.sublabel_text_size) * config.characterSize; - _keyBgPaint.setColor(res.getColor(R.color.key_bg)); - _keyDownBgPaint.setColor(res.getColor(R.color.key_down_bg)); - _keyLabelPaint = initLabelPaint(Paint.Align.CENTER, null); - _keySubLabelPaint = initLabelPaint(Paint.Align.LEFT, null); - Typeface specialKeysFont = ((Keyboard2)getContext()).getSpecialKeyFont(); - _specialKeyLabelPaint = initLabelPaint(Paint.Align.CENTER, specialKeysFont); - _specialKeySubLabelPaint = initLabelPaint(Paint.Align.LEFT, specialKeysFont); + _keyBgPaint.setColor(res.getColor(R.color.key_bg)); + _keyDownBgPaint.setColor(res.getColor(R.color.key_down_bg)); + _keyLabelPaint = initLabelPaint(Paint.Align.CENTER, null); + _keySubLabelPaint = initLabelPaint(Paint.Align.LEFT, null); + Typeface specialKeysFont = ((Keyboard2)getContext()).getSpecialKeyFont(); + _specialKeyLabelPaint = initLabelPaint(Paint.Align.CENTER, specialKeysFont); + _specialKeySubLabelPaint = initLabelPaint(Paint.Align.LEFT, specialKeysFont); if (kw != null) setKeyboard(kw); // handle layout options then calls reset(). } - private Paint initLabelPaint(Paint.Align align, Typeface font) - { + private Paint initLabelPaint(Paint.Align align, Typeface font) + { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setTextAlign(align); if (font != null) paint.setTypeface(font); - return (paint); - } + return (paint); + } - public void setKeyboard(KeyboardData kw) + public void setKeyboard(KeyboardData kw) { if (!_config.shouldOfferSwitchingToNextInputMethod) kw = kw.removeKeys(new KeyboardData.RemoveKeysByEvent(KeyValue.EVENT_CHANGE_METHOD)); @@ -102,155 +102,155 @@ public class Keyboard2View extends View reset(); } - public void reset() - { - _flags = 0; - _downKeys.clear(); - requestLayout(); - invalidate(); - } - - @Override - public boolean onTouch(View v, MotionEvent event) - { - float x; - float y; - float keyW; - int p; - - switch (event.getActionMasked()) - { - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_POINTER_UP: - onTouchUp(event.getPointerId(event.getActionIndex())); - break ; - case MotionEvent.ACTION_DOWN: - case MotionEvent.ACTION_POINTER_DOWN: - p = event.getActionIndex(); - onTouchDown(event.getX(p), event.getY(p), event.getPointerId(p)); - break ; - case MotionEvent.ACTION_MOVE: - for (p = 0; p < event.getPointerCount(); p++) - onTouchMove(event.getX(p), event.getY(p), event.getPointerId(p)); - break ; - default: - return (false); - } - return (true); - } - - private KeyDown getKeyDown(int pointerId) - { - for (KeyDown k : _downKeys) - { - if (k.pointerId == pointerId) - return (k); - } - return (null); - } - - private KeyDown getKeyDown(KeyboardData.Key key) - { - for (KeyDown k : _downKeys) - { - if (k.key == key) - return (k); - } - return (null); - } - - private KeyDown getKeyDown(KeyValue kv) - { - for (KeyDown k : _downKeys) - { - if (k.value == kv) - return (k); - } - return (null); - } - - private void onTouchMove(float moveX, float moveY, int pointerId) - { - KeyDown key = getKeyDown(pointerId); - KeyValue newValue; - - if (key != null) - { - moveX -= key.downX; - moveY -= key.downY; + public void reset() + { + _flags = 0; + _downKeys.clear(); + requestLayout(); + invalidate(); + } + + @Override + public boolean onTouch(View v, MotionEvent event) + { + float x; + float y; + float keyW; + int p; + + switch (event.getActionMasked()) + { + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_POINTER_UP: + onTouchUp(event.getPointerId(event.getActionIndex())); + break ; + case MotionEvent.ACTION_DOWN: + case MotionEvent.ACTION_POINTER_DOWN: + p = event.getActionIndex(); + onTouchDown(event.getX(p), event.getY(p), event.getPointerId(p)); + break ; + case MotionEvent.ACTION_MOVE: + for (p = 0; p < event.getPointerCount(); p++) + onTouchMove(event.getX(p), event.getY(p), event.getPointerId(p)); + break ; + default: + return (false); + } + return (true); + } + + private KeyDown getKeyDown(int pointerId) + { + for (KeyDown k : _downKeys) + { + if (k.pointerId == pointerId) + return (k); + } + return (null); + } + + private KeyDown getKeyDown(KeyboardData.Key key) + { + for (KeyDown k : _downKeys) + { + if (k.key == key) + return (k); + } + return (null); + } + + private KeyDown getKeyDown(KeyValue kv) + { + for (KeyDown k : _downKeys) + { + if (k.value == kv) + return (k); + } + return (null); + } + + private void onTouchMove(float moveX, float moveY, int pointerId) + { + KeyDown key = getKeyDown(pointerId); + KeyValue newValue; + + if (key != null) + { + moveX -= key.downX; + moveY -= key.downY; float absDist = Math.abs(moveX) + Math.abs(moveY); key.ptrDist = absDist; - if (absDist < _config.subValueDist) - newValue = key.key.key0; - else if (moveX < 0) - newValue = (moveY < 0) ? key.key.key1 : key.key.key3; - else if (moveY < 0) - newValue = key.key.key2; - else - newValue = key.key.key4; - if (newValue != null && newValue != key.value) - { - if (key.timeoutWhat != -1) - { - _handler.removeMessages(key.timeoutWhat); - if ((newValue.flags & KeyValue.FLAG_NOREPEAT) == 0) - _handler.sendEmptyMessageDelayed(key.timeoutWhat, _config.longPressTimeout); - } - key.value = newValue; - key.flags = newValue.flags; - updateFlags(); - invalidate(); - handleKeyDown(newValue); - } - } - } - - private void onTouchDown(float touchX, float touchY, int pointerId) - { - float y = _config.marginTop - _config.keyHeight; - for (KeyboardData.Row row : _keyboard.rows) - { - y += _config.keyHeight; - if (touchY < y || touchY >= (y + _config.keyHeight)) - continue ; + if (absDist < _config.subValueDist) + newValue = key.key.key0; + else if (moveX < 0) + newValue = (moveY < 0) ? key.key.key1 : key.key.key3; + else if (moveY < 0) + newValue = key.key.key2; + else + newValue = key.key.key4; + if (newValue != null && newValue != key.value) + { + if (key.timeoutWhat != -1) + { + _handler.removeMessages(key.timeoutWhat); + if ((newValue.flags & KeyValue.FLAG_NOREPEAT) == 0) + _handler.sendEmptyMessageDelayed(key.timeoutWhat, _config.longPressTimeout); + } + key.value = newValue; + key.flags = newValue.flags; + updateFlags(); + invalidate(); + handleKeyDown(newValue); + } + } + } + + private void onTouchDown(float touchX, float touchY, int pointerId) + { + float y = _config.marginTop - _config.keyHeight; + for (KeyboardData.Row row : _keyboard.rows) + { + y += _config.keyHeight; + if (touchY < y || touchY >= (y + _config.keyHeight)) + continue ; float x = _config.horizontalMargin; - for (KeyboardData.Key key : row.keys) - { + for (KeyboardData.Key key : row.keys) + { x += key.shift * _keyWidth; - float keyW = _keyWidth * key.width; - if (touchX >= x && touchX < (x + keyW)) - { + float keyW = _keyWidth * key.width; + if (touchX >= x && touchX < (x + keyW)) + { int what = _currentWhat++; if (key.key0 != null && (key.key0.flags & KeyValue.FLAG_NOREPEAT) == 0) _handler.sendEmptyMessageDelayed(what, _config.longPressTimeout); _downKeys.add(new KeyDown(pointerId, key, touchX, touchY, what)); - handleKeyDown(key.key0); - updateFlags(); - invalidate(); - return ; - } - x += keyW; - } - } - } + handleKeyDown(key.key0); + updateFlags(); + invalidate(); + return ; + } + x += keyW; + } + } + } // Whether a key is already activated (key down but pointer up) private KeyDown getActivatedKey(KeyValue kv) { - for (KeyDown k : _downKeys) - { - if (k.value == kv && k.pointerId == -1) - return (k); - } - return (null); + for (KeyDown k : _downKeys) + { + if (k.value == kv && k.pointerId == -1) + return (k); + } + return (null); } - private void onTouchUp(int pointerId) - { - KeyDown k = getKeyDown(pointerId); + private void onTouchUp(int pointerId) + { + KeyDown k = getKeyDown(pointerId); - if (k != null) - { + if (k != null) + { // Stop key repeat if (k.timeoutWhat != -1) { @@ -295,55 +295,55 @@ public class Keyboard2View extends View } updateFlags(); invalidate(); - } - } - - private void handleKeyUp(KeyDown key) - { - if (key.value != null && (key.flags & (KeyValue.FLAG_LOCKED | KeyValue.FLAG_NOCHAR)) == 0) - ((Keyboard2)getContext()).handleKeyUp(key.value, _flags); - } - - private void handleKeyDown(KeyValue key) - { - if (key == null) - return ; - vibrate(); - } - - private void updateFlags() - { - _flags = 0; - for (KeyDown k : _downKeys) - _flags |= k.flags; - } - - private void vibrate() - { - if (!_config.vibrateEnabled) - return ; - long now = System.currentTimeMillis(); - if ((now - _lastVibration) > VIBRATE_MIN_INTERVAL) - { - _lastVibration = now; - try - { - _vibratorService.vibrate(_config.vibrateDuration); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - } - - @Override - public boolean handleMessage(Message msg) - { - for (KeyDown key : _downKeys) - { - if (key.timeoutWhat == msg.what) - { + } + } + + private void handleKeyUp(KeyDown key) + { + if (key.value != null && (key.flags & (KeyValue.FLAG_LOCKED | KeyValue.FLAG_NOCHAR)) == 0) + ((Keyboard2)getContext()).handleKeyUp(key.value, _flags); + } + + private void handleKeyDown(KeyValue key) + { + if (key == null) + return ; + vibrate(); + } + + private void updateFlags() + { + _flags = 0; + for (KeyDown k : _downKeys) + _flags |= k.flags; + } + + private void vibrate() + { + if (!_config.vibrateEnabled) + return ; + long now = System.currentTimeMillis(); + if ((now - _lastVibration) > VIBRATE_MIN_INTERVAL) + { + _lastVibration = now; + try + { + _vibratorService.vibrate(_config.vibrateDuration); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + } + + @Override + public boolean handleMessage(Message msg) + { + for (KeyDown key : _downKeys) + { + if (key.timeoutWhat == msg.what) + { long nextInterval = _config.longPressInterval; if (_config.preciseRepeat && (key.flags & KeyValue.FLAG_PRECISE_REPEAT) != 0) { @@ -351,17 +351,17 @@ public class Keyboard2View extends View float accel = Math.min(4.f, Math.max(0.3f, key.ptrDist / (_config.subValueDist * 15.f))); nextInterval = (long)((float)nextInterval / accel); } - _handler.sendEmptyMessageDelayed(msg.what, nextInterval); - ((Keyboard2)getContext()).handleKeyUp(key.value, _flags); - return (true); - } - } - return (false); - } - - @Override - public void onMeasure(int wSpec, int hSpec) - { + _handler.sendEmptyMessageDelayed(msg.what, nextInterval); + ((Keyboard2)getContext()).handleKeyUp(key.value, _flags); + return (true); + } + } + return (false); + } + + @Override + public void onMeasure(int wSpec, int hSpec) + { DisplayMetrics dm = getContext().getResources().getDisplayMetrics(); int width = dm.widthPixels; int height = @@ -370,49 +370,49 @@ public class Keyboard2View extends View + _config.marginTop + _config.marginBottom); setMeasuredDimension(width, height); _keyWidth = (width - (_config.horizontalMargin * 2)) / _keyboard.keysWidth; - } - - @Override - protected void onDraw(Canvas canvas) - { - float y = _config.marginTop; - for (KeyboardData.Row row : _keyboard.rows) - { + } + + @Override + protected void onDraw(Canvas canvas) + { + float y = _config.marginTop; + for (KeyboardData.Row row : _keyboard.rows) + { y += row.shift * _config.keyHeight; float x = _config.horizontalMargin; float keyH = row.height * _config.keyHeight; - for (KeyboardData.Key k : row.keys) - { + for (KeyboardData.Key k : row.keys) + { x += k.shift * _keyWidth + _config.keyHorizontalInterval; - float keyW = _keyWidth * k.width - _config.keyHorizontalInterval; - KeyDown keyDown = getKeyDown(k); - _tmpRect.set(x, y, x + keyW, y + keyH); - if (keyDown != null) - canvas.drawRect(_tmpRect, _keyDownBgPaint); - else - canvas.drawRoundRect(_tmpRect, _config.keyRound, _config.keyRound, _keyBgPaint); - if (k.key0 != null) - drawLabel(canvas, k.key0, keyW / 2f + x, (keyH + _labelTextSize) / 2f + y, keyDown); - float subPadding = _config.keyPadding; - if (k.key1 != null) - drawSubLabel(canvas, k.key1, x + subPadding, y + subPadding, false, true, keyDown); - if (k.key3 != null) - drawSubLabel(canvas, k.key3, x + subPadding, y + keyH - subPadding, false, false, keyDown); - if (k.key2 != null) - drawSubLabel(canvas, k.key2, x + keyW - subPadding, y + subPadding, true, true, keyDown); - if (k.key4 != null) - drawSubLabel(canvas, k.key4, x + keyW - subPadding, y + keyH - subPadding, true, false, keyDown); - x += keyW; - } - y += keyH + _config.keyVerticalInterval; - } - } - - @Override - public void onDetachedFromWindow() - { - super.onDetachedFromWindow(); - } + float keyW = _keyWidth * k.width - _config.keyHorizontalInterval; + KeyDown keyDown = getKeyDown(k); + _tmpRect.set(x, y, x + keyW, y + keyH); + if (keyDown != null) + canvas.drawRect(_tmpRect, _keyDownBgPaint); + else + canvas.drawRoundRect(_tmpRect, _config.keyRound, _config.keyRound, _keyBgPaint); + if (k.key0 != null) + drawLabel(canvas, k.key0, keyW / 2f + x, (keyH + _labelTextSize) / 2f + y, keyDown); + float subPadding = _config.keyPadding; + if (k.key1 != null) + drawSubLabel(canvas, k.key1, x + subPadding, y + subPadding, false, true, keyDown); + if (k.key3 != null) + drawSubLabel(canvas, k.key3, x + subPadding, y + keyH - subPadding, false, false, keyDown); + if (k.key2 != null) + drawSubLabel(canvas, k.key2, x + keyW - subPadding, y + subPadding, true, true, keyDown); + if (k.key4 != null) + drawSubLabel(canvas, k.key4, x + keyW - subPadding, y + keyH - subPadding, true, false, keyDown); + x += keyW; + } + y += keyH + _config.keyVerticalInterval; + } + } + + @Override + public void onDetachedFromWindow() + { + super.onDetachedFromWindow(); + } private int labelColor(KeyValue k, KeyDown hasKeyDown, int defaultColor) { @@ -430,17 +430,17 @@ public class Keyboard2View extends View return defaultColor; } - private void drawLabel(Canvas canvas, KeyValue k, float x, float y, KeyDown keyDown) - { + private void drawLabel(Canvas canvas, KeyValue k, float x, float y, KeyDown keyDown) + { k = KeyModifier.handleFlags(k, _flags); Paint p = ((k.flags & KeyValue.FLAG_KEY_FONT) != 0) ? _specialKeyLabelPaint : _keyLabelPaint; p.setColor(labelColor(k, keyDown, _labelColor)); p.setTextSize(_labelTextSize * scaleTextSize(k)); canvas.drawText(k.symbol, x, y, p); - } + } - private void drawSubLabel(Canvas canvas, KeyValue k, float x, float y, boolean right, boolean up, KeyDown keyDown) - { + private void drawSubLabel(Canvas canvas, KeyValue k, float x, float y, boolean right, boolean up, KeyDown keyDown) + { k = KeyModifier.handleFlags(k, _flags); Paint p = ((k.flags & KeyValue.FLAG_KEY_FONT) != 0) ? _specialKeySubLabelPaint : _keySubLabelPaint; p.setColor(labelColor(k, keyDown, _subLabelColor)); @@ -448,36 +448,36 @@ public class Keyboard2View extends View p.setTextSize(_sublabelTextSize * scaleTextSize(k)); y -= up ? p.ascent() : p.descent(); canvas.drawText(k.symbol, x, y, p); - } + } private float scaleTextSize(KeyValue k) { return (k.symbol.length() < 2) ? 1.f : 0.8f; } - private static class KeyDown - { + private static class KeyDown + { /* -1 if pointer is up. */ - public int pointerId; - public KeyValue value; - public KeyboardData.Key key; - public float downX; - public float downY; + public int pointerId; + public KeyValue value; + public KeyboardData.Key key; + public float downX; + public float downY; /* Manhattan distance of the pointer to the center of the key */ public float ptrDist; - public int flags; - public int timeoutWhat; - - public KeyDown(int pointerId, KeyboardData.Key key, float x, float y, int what) - { - this.pointerId = pointerId; - value = key.key0; - this.key = key; - downX = x; - downY = y; + public int flags; + public int timeoutWhat; + + public KeyDown(int pointerId, KeyboardData.Key key, float x, float y, int what) + { + this.pointerId = pointerId; + value = key.key0; + this.key = key; + downX = x; + downY = y; ptrDist = 0.f; - flags = (value == null) ? 0 : value.flags; - timeoutWhat = what; - } - } + flags = (value == null) ? 0 : value.flags; + timeoutWhat = what; + } + } } |
