2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2010-03-26 22:07:10 +00:00
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2009-03-13 22:11:42 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Canvas;
|
2010-02-05 22:07:04 +00:00
|
|
|
import android.graphics.Paint;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.inputmethodservice.Keyboard;
|
|
|
|
import android.inputmethodservice.Keyboard.Key;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Message;
|
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.MotionEvent;
|
2010-08-30 12:09:44 +00:00
|
|
|
|
|
|
|
import java.util.List;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
public class LatinKeyboardView extends LatinKeyboardBaseView {
|
2009-03-13 22:11:42 +00:00
|
|
|
|
|
|
|
static final int KEYCODE_OPTIONS = -100;
|
2010-09-02 19:25:31 +00:00
|
|
|
static final int KEYCODE_OPTIONS_LONGPRESS = -101;
|
2010-01-16 20:21:23 +00:00
|
|
|
static final int KEYCODE_VOICE = -102;
|
|
|
|
static final int KEYCODE_F1 = -103;
|
2010-01-22 21:57:20 +00:00
|
|
|
static final int KEYCODE_NEXT_LANGUAGE = -104;
|
2010-01-30 04:09:49 +00:00
|
|
|
static final int KEYCODE_PREV_LANGUAGE = -105;
|
2010-01-22 21:57:20 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
private Keyboard mPhoneKeyboard;
|
|
|
|
|
2010-03-30 23:49:15 +00:00
|
|
|
/** Whether we've started dropping move events because we found a big jump */
|
|
|
|
private boolean mDroppingEvents;
|
2010-09-03 08:54:37 +00:00
|
|
|
/**
|
2010-08-30 12:09:44 +00:00
|
|
|
* Whether multi-touch disambiguation needs to be disabled if a real multi-touch event has
|
|
|
|
* occured
|
2010-03-30 23:49:15 +00:00
|
|
|
*/
|
|
|
|
private boolean mDisableDisambiguation;
|
|
|
|
/** The distance threshold at which we start treating the touch session as a multi-touch */
|
|
|
|
private int mJumpThresholdSquare = Integer.MAX_VALUE;
|
2010-04-06 13:27:17 +00:00
|
|
|
/** The y coordinate of the last row */
|
|
|
|
private int mLastRowY;
|
2010-01-30 04:09:49 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
public LatinKeyboardView(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public LatinKeyboardView(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
}
|
2009-05-07 00:38:00 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
public void setPhoneKeyboard(Keyboard phoneKeyboard) {
|
|
|
|
mPhoneKeyboard = phoneKeyboard;
|
|
|
|
}
|
2009-05-07 00:38:00 +00:00
|
|
|
|
2010-03-30 23:49:15 +00:00
|
|
|
@Override
|
|
|
|
public void setKeyboard(Keyboard k) {
|
|
|
|
super.setKeyboard(k);
|
|
|
|
// One-seventh of the keyboard width seems like a reasonable threshold
|
|
|
|
mJumpThresholdSquare = k.getMinWidth() / 7;
|
|
|
|
mJumpThresholdSquare *= mJumpThresholdSquare;
|
2010-04-06 13:27:17 +00:00
|
|
|
// Assuming there are 4 rows, this is the coordinate of the last row
|
|
|
|
mLastRowY = (k.getHeight() * 3) / 4;
|
2010-03-30 23:49:15 +00:00
|
|
|
setKeyboardLocal(k);
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
@Override
|
|
|
|
protected boolean onLongPress(Key key) {
|
2010-09-03 08:54:37 +00:00
|
|
|
int primaryCode = key.codes[0];
|
|
|
|
if (primaryCode == KEYCODE_OPTIONS) {
|
2010-09-02 19:25:31 +00:00
|
|
|
getOnKeyboardActionListener().onKey(KEYCODE_OPTIONS_LONGPRESS, null,
|
|
|
|
LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE,
|
|
|
|
LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE);
|
|
|
|
return true;
|
2010-09-03 08:54:37 +00:00
|
|
|
} else if (primaryCode == '0' && getKeyboard() == mPhoneKeyboard) {
|
2009-03-13 22:11:42 +00:00
|
|
|
// Long pressing on 0 in phone number keypad gives you a '+'.
|
2010-08-25 12:27:13 +00:00
|
|
|
getOnKeyboardActionListener().onKey(
|
2010-09-03 08:54:37 +00:00
|
|
|
'+', null,
|
|
|
|
LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE,
|
|
|
|
LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE);
|
|
|
|
return true;
|
2009-03-13 22:11:42 +00:00
|
|
|
} else {
|
|
|
|
return super.onLongPress(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
@Override
|
|
|
|
protected CharSequence adjustCase(CharSequence label) {
|
|
|
|
Keyboard keyboard = getKeyboard();
|
|
|
|
if (keyboard.isShifted()
|
|
|
|
&& keyboard instanceof LatinKeyboard
|
|
|
|
&& ((LatinKeyboard) keyboard).isAlphaKeyboard()
|
|
|
|
&& label != null && label.length() < 3
|
|
|
|
&& Character.isLowerCase(label.charAt(0))) {
|
|
|
|
label = label.toString().toUpperCase();
|
|
|
|
}
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setShiftLocked(boolean shiftLocked) {
|
|
|
|
Keyboard keyboard = getKeyboard();
|
2010-09-06 05:50:50 +00:00
|
|
|
if (keyboard instanceof LatinKeyboard) {
|
2010-08-20 05:35:02 +00:00
|
|
|
((LatinKeyboard)keyboard).setShiftLocked(shiftLocked);
|
|
|
|
invalidateAllKeys();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-30 23:49:15 +00:00
|
|
|
/**
|
|
|
|
* This function checks to see if we need to handle any sudden jumps in the pointer location
|
|
|
|
* that could be due to a multi-touch being treated as a move by the firmware or hardware.
|
|
|
|
* Once a sudden jump is detected, all subsequent move events are discarded
|
|
|
|
* until an UP is received.<P>
|
2010-09-03 08:54:37 +00:00
|
|
|
* When a sudden jump is detected, an UP event is simulated at the last position and when
|
2010-03-30 23:49:15 +00:00
|
|
|
* the sudden moves subside, a DOWN event is simulated for the second key.
|
|
|
|
* @param me the motion event
|
2010-09-03 08:54:37 +00:00
|
|
|
* @return true if the event was consumed, so that it doesn't continue to be handled by
|
2010-03-30 23:49:15 +00:00
|
|
|
* KeyboardView.
|
|
|
|
*/
|
|
|
|
private boolean handleSuddenJump(MotionEvent me) {
|
|
|
|
final int action = me.getAction();
|
|
|
|
final int x = (int) me.getX();
|
|
|
|
final int y = (int) me.getY();
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
// Real multi-touch event? Stop looking for sudden jumps
|
|
|
|
if (me.getPointerCount() > 1) {
|
|
|
|
mDisableDisambiguation = true;
|
|
|
|
}
|
|
|
|
if (mDisableDisambiguation) {
|
|
|
|
// If UP, reset the multi-touch flag
|
|
|
|
if (action == MotionEvent.ACTION_UP) mDisableDisambiguation = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
// Reset the "session"
|
|
|
|
mDroppingEvents = false;
|
|
|
|
mDisableDisambiguation = false;
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
// Is this a big jump?
|
|
|
|
final int distanceSquare = (mLastX - x) * (mLastX - x) + (mLastY - y) * (mLastY - y);
|
2010-04-06 13:27:17 +00:00
|
|
|
// Check the distance and also if the move is not entirely within the bottom row
|
|
|
|
// If it's only in the bottom row, it might be an intentional slide gesture
|
|
|
|
// for language switching
|
|
|
|
if (distanceSquare > mJumpThresholdSquare
|
|
|
|
&& (mLastY < mLastRowY || y < mLastRowY)) {
|
2010-03-30 23:49:15 +00:00
|
|
|
// If we're not yet dropping events, start dropping and send an UP event
|
|
|
|
if (!mDroppingEvents) {
|
|
|
|
mDroppingEvents = true;
|
|
|
|
// Send an up event
|
|
|
|
MotionEvent translated = MotionEvent.obtain(me.getEventTime(), me.getEventTime(),
|
|
|
|
MotionEvent.ACTION_UP,
|
|
|
|
mLastX, mLastY, me.getMetaState());
|
|
|
|
super.onTouchEvent(translated);
|
|
|
|
translated.recycle();
|
|
|
|
}
|
|
|
|
result = true;
|
|
|
|
} else if (mDroppingEvents) {
|
|
|
|
// If moves are small and we're already dropping events, continue dropping
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
if (mDroppingEvents) {
|
|
|
|
// Send a down event first, as we dropped a bunch of sudden jumps and assume that
|
|
|
|
// the user is releasing the touch on the second key.
|
|
|
|
MotionEvent translated = MotionEvent.obtain(me.getEventTime(), me.getEventTime(),
|
|
|
|
MotionEvent.ACTION_DOWN,
|
|
|
|
x, y, me.getMetaState());
|
|
|
|
super.onTouchEvent(translated);
|
|
|
|
translated.recycle();
|
|
|
|
mDroppingEvents = false;
|
|
|
|
// Let the up event get processed as well, result = false
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Track the previous coordinate
|
|
|
|
mLastX = x;
|
|
|
|
mLastY = y;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-22 21:51:39 +00:00
|
|
|
@Override
|
|
|
|
public boolean onTouchEvent(MotionEvent me) {
|
2010-01-30 04:09:49 +00:00
|
|
|
LatinKeyboard keyboard = (LatinKeyboard) getKeyboard();
|
2010-02-05 22:07:04 +00:00
|
|
|
if (DEBUG_LINE) {
|
|
|
|
mLastX = (int) me.getX();
|
|
|
|
mLastY = (int) me.getY();
|
|
|
|
invalidate();
|
|
|
|
}
|
2010-04-15 21:44:22 +00:00
|
|
|
|
2010-08-30 12:09:44 +00:00
|
|
|
// If there was a sudden jump, return without processing the actual motion event.
|
|
|
|
if (handleSuddenJump(me))
|
|
|
|
return true;
|
|
|
|
|
2010-01-30 04:09:49 +00:00
|
|
|
// Reset any bounding box controls in the keyboard
|
|
|
|
if (me.getAction() == MotionEvent.ACTION_DOWN) {
|
|
|
|
keyboard.keyReleased();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (me.getAction() == MotionEvent.ACTION_UP) {
|
|
|
|
int languageDirection = keyboard.getLanguageChangeDirection();
|
|
|
|
if (languageDirection != 0) {
|
|
|
|
getOnKeyboardActionListener().onKey(
|
|
|
|
languageDirection == 1 ? KEYCODE_NEXT_LANGUAGE : KEYCODE_PREV_LANGUAGE,
|
2010-08-25 12:27:13 +00:00
|
|
|
null, mLastX, mLastY);
|
2010-01-30 04:09:49 +00:00
|
|
|
me.setAction(MotionEvent.ACTION_CANCEL);
|
|
|
|
keyboard.keyReleased();
|
|
|
|
return super.onTouchEvent(me);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-30 12:09:44 +00:00
|
|
|
return super.onTouchEvent(me);
|
2010-03-20 00:31:54 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**************************** INSTRUMENTATION *******************************/
|
|
|
|
|
|
|
|
static final boolean DEBUG_AUTO_PLAY = false;
|
2010-02-05 22:07:04 +00:00
|
|
|
static final boolean DEBUG_LINE = false;
|
2009-03-13 22:11:42 +00:00
|
|
|
private static final int MSG_TOUCH_DOWN = 1;
|
|
|
|
private static final int MSG_TOUCH_UP = 2;
|
2010-09-03 08:54:37 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
Handler mHandler2;
|
2010-09-03 08:54:37 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
private String mStringToPlay;
|
|
|
|
private int mStringIndex;
|
|
|
|
private boolean mDownDelivered;
|
|
|
|
private Key[] mAsciiKeys = new Key[256];
|
|
|
|
private boolean mPlaying;
|
2010-02-05 22:07:04 +00:00
|
|
|
private int mLastX;
|
|
|
|
private int mLastY;
|
|
|
|
private Paint mPaint;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-03-30 23:49:15 +00:00
|
|
|
private void setKeyboardLocal(Keyboard k) {
|
2009-03-13 22:11:42 +00:00
|
|
|
if (DEBUG_AUTO_PLAY) {
|
|
|
|
findKeys();
|
|
|
|
if (mHandler2 == null) {
|
|
|
|
mHandler2 = new Handler() {
|
|
|
|
@Override
|
|
|
|
public void handleMessage(Message msg) {
|
|
|
|
removeMessages(MSG_TOUCH_DOWN);
|
|
|
|
removeMessages(MSG_TOUCH_UP);
|
|
|
|
if (mPlaying == false) return;
|
2010-09-03 08:54:37 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
switch (msg.what) {
|
|
|
|
case MSG_TOUCH_DOWN:
|
|
|
|
if (mStringIndex >= mStringToPlay.length()) {
|
|
|
|
mPlaying = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char c = mStringToPlay.charAt(mStringIndex);
|
2010-09-03 08:54:37 +00:00
|
|
|
while (c > 255 || mAsciiKeys[c] == null) {
|
2009-03-13 22:11:42 +00:00
|
|
|
mStringIndex++;
|
|
|
|
if (mStringIndex >= mStringToPlay.length()) {
|
|
|
|
mPlaying = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
c = mStringToPlay.charAt(mStringIndex);
|
|
|
|
}
|
|
|
|
int x = mAsciiKeys[c].x + 10;
|
|
|
|
int y = mAsciiKeys[c].y + 26;
|
2010-09-03 08:54:37 +00:00
|
|
|
MotionEvent me = MotionEvent.obtain(SystemClock.uptimeMillis(),
|
|
|
|
SystemClock.uptimeMillis(),
|
2009-03-13 22:11:42 +00:00
|
|
|
MotionEvent.ACTION_DOWN, x, y, 0);
|
|
|
|
LatinKeyboardView.this.dispatchTouchEvent(me);
|
|
|
|
me.recycle();
|
|
|
|
sendEmptyMessageDelayed(MSG_TOUCH_UP, 500); // Deliver up in 500ms if nothing else
|
|
|
|
// happens
|
|
|
|
mDownDelivered = true;
|
|
|
|
break;
|
|
|
|
case MSG_TOUCH_UP:
|
|
|
|
char cUp = mStringToPlay.charAt(mStringIndex);
|
|
|
|
int x2 = mAsciiKeys[cUp].x + 10;
|
|
|
|
int y2 = mAsciiKeys[cUp].y + 26;
|
|
|
|
mStringIndex++;
|
2010-09-03 08:54:37 +00:00
|
|
|
|
|
|
|
MotionEvent me2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
|
|
|
|
SystemClock.uptimeMillis(),
|
2009-03-13 22:11:42 +00:00
|
|
|
MotionEvent.ACTION_UP, x2, y2, 0);
|
|
|
|
LatinKeyboardView.this.dispatchTouchEvent(me2);
|
|
|
|
me2.recycle();
|
|
|
|
sendEmptyMessageDelayed(MSG_TOUCH_DOWN, 500); // Deliver up in 500ms if nothing else
|
|
|
|
// happens
|
|
|
|
mDownDelivered = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void findKeys() {
|
|
|
|
List<Key> keys = getKeyboard().getKeys();
|
|
|
|
// Get the keys on this keyboard
|
|
|
|
for (int i = 0; i < keys.size(); i++) {
|
|
|
|
int code = keys.get(i).codes[0];
|
2010-09-03 08:54:37 +00:00
|
|
|
if (code >= 0 && code <= 255) {
|
2009-03-13 22:11:42 +00:00
|
|
|
mAsciiKeys[code] = keys.get(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
|
|
|
|
public void startPlaying(String s) {
|
|
|
|
if (DEBUG_AUTO_PLAY) {
|
|
|
|
if (s == null) return;
|
|
|
|
mStringToPlay = s.toLowerCase();
|
|
|
|
mPlaying = true;
|
|
|
|
mDownDelivered = false;
|
|
|
|
mStringIndex = 0;
|
|
|
|
mHandler2.sendEmptyMessageDelayed(MSG_TOUCH_DOWN, 10);
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void draw(Canvas c) {
|
2010-08-20 05:35:02 +00:00
|
|
|
LatinIMEUtil.GCUtils.getInstance().reset();
|
|
|
|
boolean tryGC = true;
|
|
|
|
for (int i = 0; i < LatinIMEUtil.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) {
|
|
|
|
try {
|
|
|
|
super.draw(c);
|
|
|
|
tryGC = false;
|
|
|
|
} catch (OutOfMemoryError e) {
|
|
|
|
tryGC = LatinIMEUtil.GCUtils.getInstance().tryGCOrWait("LatinKeyboardView", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (DEBUG_AUTO_PLAY) {
|
|
|
|
if (mPlaying) {
|
|
|
|
mHandler2.removeMessages(MSG_TOUCH_DOWN);
|
|
|
|
mHandler2.removeMessages(MSG_TOUCH_UP);
|
|
|
|
if (mDownDelivered) {
|
|
|
|
mHandler2.sendEmptyMessageDelayed(MSG_TOUCH_UP, 20);
|
|
|
|
} else {
|
|
|
|
mHandler2.sendEmptyMessageDelayed(MSG_TOUCH_DOWN, 20);
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-05 22:07:04 +00:00
|
|
|
if (DEBUG_LINE) {
|
|
|
|
if (mPaint == null) {
|
|
|
|
mPaint = new Paint();
|
|
|
|
mPaint.setColor(0x80FFFFFF);
|
|
|
|
mPaint.setAntiAlias(false);
|
|
|
|
}
|
|
|
|
c.drawLine(mLastX, 0, mLastX, getHeight(), mPaint);
|
|
|
|
c.drawLine(0, mLastY, getWidth(), mLastY, mPaint);
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
}
|