Unexpected-Keyboard/srcs/juloo.keyboard2/Keyboard2View.java

405 lines
11 KiB
Java
Raw Normal View History

2015-07-30 20:14:55 +02:00
package juloo.keyboard2;
import android.content.Context;
2015-08-08 16:47:22 +02:00
import android.content.SharedPreferences;
2015-07-30 20:14:55 +02:00
import android.graphics.Canvas;
2015-08-01 21:36:40 +02:00
import android.graphics.RectF;
2015-07-30 20:14:55 +02:00
import android.graphics.Paint;
2015-08-08 16:47:22 +02:00
import android.preference.PreferenceManager;
2015-07-30 20:14:55 +02:00
import android.util.AttributeSet;
import android.util.DisplayMetrics;
2015-08-05 01:30:56 +02:00
import android.os.Handler;
import android.os.Message;
2015-08-03 00:01:04 +02:00
import android.os.Vibrator;
2015-07-30 20:14:55 +02:00
import android.view.MotionEvent;
import android.view.View;
2015-08-01 23:54:38 +02:00
import java.util.LinkedList;
2015-07-30 20:14:55 +02:00
public class Keyboard2View extends View
2015-08-05 01:30:56 +02:00
implements View.OnTouchListener, Handler.Callback
2015-07-30 20:14:55 +02:00
{
2015-08-08 15:26:23 +02:00
private static final float KEY_PER_ROW = 10;
2015-08-03 15:11:11 +02:00
2015-08-08 15:26:23 +02:00
private static final long VIBRATE_MIN_INTERVAL = 100;
2015-07-30 20:14:55 +02:00
private Keyboard2 _ime;
private KeyboardData _keyboard;
2015-07-30 20:14:55 +02:00
2015-08-01 23:54:38 +02:00
private LinkedList<KeyDown> _downKeys = new LinkedList<KeyDown>();
private int _flags = 0;
2015-08-01 16:33:30 +02:00
2015-08-03 00:01:04 +02:00
private Vibrator _vibratorService;
private long _lastVibration = 0;
2015-08-05 01:30:56 +02:00
private Handler _handler;
private static int _currentWhat = 0;
private float _horizontalMargin;
2015-08-08 15:26:23 +02:00
private float _marginTop;
2015-07-30 20:14:55 +02:00
private float _keyWidth;
2015-08-08 16:47:22 +02:00
private float _keyHeight;
2015-07-30 20:14:55 +02:00
private float _keyPadding;
private float _keyBgPadding;
2015-08-01 21:36:40 +02:00
private float _keyRound;
2015-07-30 20:14:55 +02:00
2015-08-08 23:07:38 +02:00
private float _subValueDist = 10f;
2015-08-08 16:47:22 +02:00
private boolean _vibrateEnabled = true;
private long _vibrateDuration = 20;
private long _longPressTimeout = 600;
private long _longPressInterval = 65;
private float _marginBottom;
2015-08-01 23:54:38 +02:00
private Paint _keyBgPaint = new Paint();
private Paint _keyDownBgPaint = new Paint();
private Paint _keyLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
2015-08-02 20:21:53 +02:00
private Paint _keyLabelLockedPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
2015-08-01 23:54:38 +02:00
private Paint _keySubLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
2015-07-30 20:14:55 +02:00
public Keyboard2View(Context context, AttributeSet attrs)
{
super(context, attrs);
2015-08-03 00:01:04 +02:00
_vibratorService = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
2015-08-05 01:30:56 +02:00
_handler = new Handler(this);
_horizontalMargin = getResources().getDimension(R.dimen.horizontal_margin);
2015-08-08 15:26:23 +02:00
_marginTop = getResources().getDimension(R.dimen.margin_top);
_marginBottom = getResources().getDimension(R.dimen.margin_bottom);
2015-07-31 23:52:47 +02:00
_keyHeight = getResources().getDimension(R.dimen.key_height);
_keyPadding = getResources().getDimension(R.dimen.key_padding);
_keyBgPadding = getResources().getDimension(R.dimen.key_bg_padding);
2015-08-01 21:36:40 +02:00
_keyRound = getResources().getDimension(R.dimen.key_round);
2015-07-30 20:14:55 +02:00
_keyBgPaint.setColor(getResources().getColor(R.color.key_bg));
_keyDownBgPaint.setColor(getResources().getColor(R.color.key_down_bg));
_keyLabelPaint.setColor(getResources().getColor(R.color.key_label));
2015-07-31 23:52:47 +02:00
_keyLabelPaint.setTextSize(getResources().getDimension(R.dimen.label_text_size));
2015-07-30 20:14:55 +02:00
_keyLabelPaint.setTextAlign(Paint.Align.CENTER);
2015-08-02 20:21:53 +02:00
_keyLabelLockedPaint.setColor(getResources().getColor(R.color.key_label_locked));
_keyLabelLockedPaint.setTextSize(getResources().getDimension(R.dimen.label_text_size));
_keyLabelLockedPaint.setTextAlign(Paint.Align.CENTER);
2015-07-31 23:17:07 +02:00
_keySubLabelPaint.setColor(getResources().getColor(R.color.key_sub_label));
2015-07-31 23:52:47 +02:00
_keySubLabelPaint.setTextSize(getResources().getDimension(R.dimen.sublabel_text_size));
2015-07-30 20:14:55 +02:00
setOnTouchListener(this);
}
2015-08-08 16:47:22 +02:00
public void reset_prefs(Keyboard2 ime)
2015-07-30 20:14:55 +02:00
{
2015-08-08 16:47:22 +02:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ime);
_ime = ime;
2015-08-08 23:07:38 +02:00
_subValueDist = prefs.getFloat("sub_value_dist", _subValueDist);
2015-08-08 16:47:22 +02:00
_vibrateEnabled = prefs.getBoolean("vibrate_enabled", _vibrateEnabled);
2015-08-08 23:07:38 +02:00
_vibrateDuration = prefs.getInt("vibrate_duration", (int)_vibrateDuration);
_longPressTimeout = prefs.getInt("longpress_timeout", (int)_longPressTimeout);
_longPressInterval = prefs.getInt("longpress_interval", (int)_longPressInterval);
2015-08-08 16:47:22 +02:00
_marginBottom = prefs.getFloat("margin_bottom", _marginBottom);
String keyboardLayout = prefs.getString("keyboard_layout", null);
int xmlRes = 0;
2015-08-08 16:47:22 +02:00
if (keyboardLayout != null)
xmlRes = ime.getResources().getIdentifier(keyboardLayout, "xml", ime.getPackageName());
if (xmlRes == 0)
xmlRes = R.xml.azerty;
_keyboard = new KeyboardData(ime.getResources().getXml(xmlRes));
reset();
2015-08-08 16:47:22 +02:00
}
public void reset()
{
_flags = 0;
_downKeys.clear();
requestLayout();
invalidate();
2015-07-30 20:14:55 +02:00
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
float x;
float y;
2015-07-30 22:30:29 +02:00
float keyW;
int p;
2015-07-30 20:14:55 +02:00
2015-07-31 01:14:35 +02:00
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();
2015-07-31 01:14:35 +02:00
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 ;
2015-07-31 01:14:35 +02:00
default:
return (false);
}
return (true);
}
private KeyDown getKeyDown(int pointerId)
{
for (KeyDown k : _downKeys)
{
if (k.pointerId == pointerId)
return (k);
}
return (null);
}
2015-08-02 19:56:23 +02:00
private KeyDown getKeyDown(KeyboardData.Key key)
{
for (KeyDown k : _downKeys)
{
if (k.key == key)
2015-08-02 19:56:23 +02:00
return (k);
}
2015-08-02 19:56:23 +02:00
return (null);
}
private void onTouchMove(float moveX, float moveY, int pointerId)
{
2015-08-03 15:11:11 +02:00
KeyDown key = getKeyDown(pointerId);
KeyValue newValue;
2015-08-03 15:11:11 +02:00
if (key != null)
2015-08-03 00:01:04 +02:00
{
2015-08-03 15:11:11 +02:00
moveX -= key.downX;
moveY -= key.downY;
2015-08-08 16:47:22 +02:00
if ((Math.abs(moveX) + Math.abs(moveY)) < _subValueDist)
2015-08-03 15:11:11 +02:00
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)
{
2015-08-05 01:30:56 +02:00
if (key.timeoutWhat != -1)
2015-08-03 15:11:11 +02:00
{
2015-08-05 01:30:56 +02:00
_handler.removeMessages(key.timeoutWhat);
2015-08-18 01:25:47 +02:00
if ((newValue.getFlags() & KeyValue.FLAG_NOCHAR) == 0)
_handler.sendEmptyMessageDelayed(key.timeoutWhat, _longPressTimeout);
2015-08-03 15:11:11 +02:00
}
2015-08-05 01:30:56 +02:00
key.value = newValue;
key.flags = newValue.getFlags();
updateFlags();
2015-08-18 01:25:47 +02:00
invalidate();
2015-08-05 01:30:56 +02:00
vibrate();
2015-08-03 15:11:11 +02:00
}
2015-08-03 00:01:04 +02:00
}
}
private void onTouchDown(float touchX, float touchY, int pointerId)
2015-07-31 01:14:35 +02:00
{
float x;
float y;
float keyW;
2015-08-08 15:26:23 +02:00
y = _marginTop - _keyHeight;
for (KeyboardData.Row row : _keyboard.getRows())
2015-07-30 20:14:55 +02:00
{
y += _keyHeight;
if (touchY < y || touchY >= (y + _keyHeight))
2015-07-31 01:14:35 +02:00
continue ;
x = (KEY_PER_ROW * _keyWidth - row.getWidth(_keyWidth)) / 2 + _horizontalMargin;
for (KeyboardData.Key key : row)
2015-07-30 20:14:55 +02:00
{
keyW = _keyWidth * key.width;
if (touchX >= x && touchX < (x + keyW))
2015-07-30 22:30:29 +02:00
{
2015-08-02 19:56:23 +02:00
KeyDown down = getKeyDown(key);
if (down != null)
{
2015-08-02 20:21:53 +02:00
if ((down.flags & KeyValue.FLAG_LOCK) != 0)
{
down.flags ^= KeyValue.FLAG_LOCK;
down.flags |= KeyValue.FLAG_LOCKED;
}
else if (down.pointerId == -1)
2015-08-02 19:56:23 +02:00
down.pointerId = pointerId;
}
else
2015-08-05 01:30:56 +02:00
{
int what = _currentWhat++;
if (key.key0 != null && (key.key0.getFlags() & KeyValue.FLAG_NOCHAR) == 0)
_handler.sendEmptyMessageDelayed(what, _longPressTimeout);
2015-08-05 01:30:56 +02:00
_downKeys.add(new KeyDown(pointerId, key, touchX, touchY, what));
}
2015-08-03 00:01:04 +02:00
vibrate();
2015-08-01 23:54:38 +02:00
updateFlags();
invalidate();
return ;
2015-07-30 22:30:29 +02:00
}
x += keyW;
2015-07-30 20:14:55 +02:00
}
}
2015-07-31 01:14:35 +02:00
}
private void onTouchUp(int pointerId)
{
KeyDown k = getKeyDown(pointerId);
2015-08-01 16:33:30 +02:00
if (k != null)
2015-08-01 16:33:30 +02:00
{
2015-08-05 01:30:56 +02:00
if (k.timeoutWhat != -1)
{
_handler.removeMessages(k.timeoutWhat);
k.timeoutWhat = -1;
}
2015-08-01 23:54:38 +02:00
if ((k.flags & KeyValue.FLAG_KEEP_ON) != 0)
{
k.flags ^= KeyValue.FLAG_KEEP_ON;
k.pointerId = -1;
return ;
}
for (int i = 0; i < _downKeys.size(); i++)
{
KeyDown downKey = _downKeys.get(i);
2015-08-02 20:21:53 +02:00
if (downKey.pointerId == -1 && (downKey.flags & KeyValue.FLAG_LOCKED) == 0)
2015-08-01 23:54:38 +02:00
_downKeys.remove(i--);
else if ((downKey.flags & KeyValue.FLAG_KEEP_ON) != 0)
downKey.flags ^= KeyValue.FLAG_KEEP_ON;
}
2015-08-02 20:21:53 +02:00
if (k.value != null && (k.flags & (KeyValue.FLAG_LOCKED | KeyValue.FLAG_NOCHAR)) == 0)
2015-08-01 23:54:38 +02:00
_ime.handleKeyUp(k.value, _flags);
_downKeys.remove(k);
2015-08-01 23:54:38 +02:00
updateFlags();
invalidate();
return ;
2015-07-31 01:14:35 +02:00
}
2015-07-30 20:14:55 +02:00
}
2015-08-01 23:54:38 +02:00
private void updateFlags()
{
_flags = 0;
for (KeyDown k : _downKeys)
_flags |= k.flags;
}
2015-08-03 00:01:04 +02:00
private void vibrate()
{
2015-08-08 16:47:22 +02:00
if (!_vibrateEnabled)
2015-08-08 15:26:23 +02:00
return ;
long now = System.currentTimeMillis();
2015-08-03 00:01:04 +02:00
if ((now - _lastVibration) > VIBRATE_MIN_INTERVAL)
{
_lastVibration = now;
try
{
2015-08-08 16:47:22 +02:00
_vibratorService.vibrate(_vibrateDuration);
2015-08-03 00:01:04 +02:00
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
2015-08-05 01:30:56 +02:00
@Override
public boolean handleMessage(Message msg)
{
long now = System.currentTimeMillis();
for (KeyDown key : _downKeys)
{
if (key.timeoutWhat == msg.what)
{
2015-08-08 16:47:22 +02:00
_handler.sendEmptyMessageDelayed(msg.what, _longPressInterval);
2015-08-05 01:30:56 +02:00
_ime.handleKeyUp(key.value, _flags);
vibrate();
return (true);
}
}
return (false);
}
2015-07-30 20:14:55 +02:00
@Override
public void onMeasure(int wSpec, int hSpec)
{
2015-08-02 23:36:22 +02:00
DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
2015-07-30 20:14:55 +02:00
int height;
if (_keyboard.getRows() == null)
2015-07-30 20:14:55 +02:00
height = 0;
else
height = (int)(_keyHeight * ((float)_keyboard.getRows().size())
2015-08-08 15:26:23 +02:00
+ _marginTop + _marginBottom);
2015-08-02 23:36:22 +02:00
setMeasuredDimension(dm.widthPixels, height);
_keyWidth = (getWidth() - (_horizontalMargin * 2)) / KEY_PER_ROW;
2015-07-30 20:14:55 +02:00
}
@Override
protected void onDraw(Canvas canvas)
{
float x;
float y;
2015-08-08 15:26:23 +02:00
y = _marginTop;
for (KeyboardData.Row row : _keyboard.getRows())
2015-07-30 20:14:55 +02:00
{
2015-08-02 22:59:25 +02:00
x = (KEY_PER_ROW * _keyWidth - row.getWidth(_keyWidth)) / 2f + _horizontalMargin;
for (KeyboardData.Key k : row)
2015-07-30 20:14:55 +02:00
{
float keyW = _keyWidth * k.width;
2015-08-02 20:21:53 +02:00
KeyDown keyDown = getKeyDown(k);
if (keyDown != null)
canvas.drawRect(x + _keyBgPadding, y + _keyBgPadding,
x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding, _keyDownBgPaint);
2015-07-30 22:30:29 +02:00
else
2015-08-01 21:36:40 +02:00
canvas.drawRoundRect(new RectF(x + _keyBgPadding, y + _keyBgPadding,
x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding), _keyRound, _keyRound, _keyBgPaint);
2015-07-30 20:14:55 +02:00
if (k.key0 != null)
2015-08-18 01:25:47 +02:00
canvas.drawText(k.key0.getSymbol(_flags), keyW / 2f + x,
2015-08-02 22:59:25 +02:00
(_keyHeight + _keyLabelPaint.getTextSize()) / 2f + y,
2015-08-02 20:21:53 +02:00
(keyDown != null && (keyDown.flags & KeyValue.FLAG_LOCKED) != 0)
? _keyLabelLockedPaint : _keyLabelPaint);
2015-08-02 22:59:25 +02:00
float subPadding = _keyBgPadding + _keyPadding;
2015-08-03 15:11:11 +02:00
_keySubLabelPaint.setTextAlign(Paint.Align.LEFT);
2015-07-30 20:14:55 +02:00
if (k.key1 != null)
2015-08-18 01:25:47 +02:00
canvas.drawText(k.key1.getSymbol(_flags), x + subPadding,
2015-08-03 15:11:11 +02:00
y + subPadding - _keySubLabelPaint.ascent(), _keySubLabelPaint);
2015-07-30 20:14:55 +02:00
if (k.key3 != null)
2015-08-18 01:25:47 +02:00
canvas.drawText(k.key3.getSymbol(_flags), x + subPadding,
2015-08-03 15:11:11 +02:00
y + _keyHeight - subPadding - _keySubLabelPaint.descent(), _keySubLabelPaint);
_keySubLabelPaint.setTextAlign(Paint.Align.RIGHT);
if (k.key2 != null)
2015-08-18 01:25:47 +02:00
canvas.drawText(k.key2.getSymbol(_flags), x + keyW - subPadding,
2015-08-03 15:11:11 +02:00
y + subPadding - _keySubLabelPaint.ascent(), _keySubLabelPaint);
2015-07-30 20:14:55 +02:00
if (k.key4 != null)
2015-08-18 01:25:47 +02:00
canvas.drawText(k.key4.getSymbol(_flags), x + keyW - subPadding,
2015-08-03 15:11:11 +02:00
y + _keyHeight - subPadding - _keySubLabelPaint.descent(), _keySubLabelPaint);
x += keyW;
2015-07-30 20:14:55 +02:00
}
y += _keyHeight;
2015-07-30 20:14:55 +02:00
}
}
private class KeyDown
{
public int pointerId;
public KeyValue value;
public KeyboardData.Key key;
public float downX;
public float downY;
2015-08-01 23:54:38 +02:00
public int flags;
2015-08-05 01:30:56 +02:00
public int timeoutWhat;
2015-08-05 01:30:56 +02:00
public KeyDown(int pointerId, KeyboardData.Key key, float x, float y, int what)
{
this.pointerId = pointerId;
2015-08-05 01:30:56 +02:00
value = key.key0;
this.key = key;
2015-08-01 21:36:40 +02:00
downX = x;
downY = y;
2015-08-05 01:30:56 +02:00
flags = (value == null) ? 0 : value.getFlags();
timeoutWhat = what;
}
}
2015-07-30 20:14:55 +02:00
}