2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2010-03-26 22:07:10 +00:00
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2010-12-02 09:46:21 +00:00
|
|
|
*
|
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
|
2010-12-02 09:46:21 +00:00
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2010-12-02 09:46:21 +00:00
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
package com.android.inputmethod.keyboard;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-12-09 12:06:26 +00:00
|
|
|
import com.android.inputmethod.latin.Utils;
|
2010-11-24 08:32:23 +00:00
|
|
|
import com.android.inputmethod.voice.VoiceIMEConnector;
|
2010-10-01 10:40:44 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Canvas;
|
2010-09-20 09:10:54 +00:00
|
|
|
import android.text.TextUtils;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.MotionEvent;
|
2010-08-30 12:09:44 +00:00
|
|
|
|
2010-12-02 11:54:32 +00:00
|
|
|
// TODO: We should remove this class
|
2010-12-02 09:46:21 +00:00
|
|
|
public class LatinKeyboardView extends KeyboardView {
|
2010-01-22 21:57:20 +00:00
|
|
|
|
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-12-10 06:24:28 +00:00
|
|
|
private int mLastX;
|
|
|
|
private int mLastY;
|
2010-01-30 04:09:49 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
public LatinKeyboardView(Context context, AttributeSet attrs) {
|
2010-09-30 08:57:23 +00:00
|
|
|
this(context, attrs, 0);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public LatinKeyboardView(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
}
|
2009-05-07 00:38:00 +00:00
|
|
|
|
2010-09-21 07:55:18 +00:00
|
|
|
@Override
|
|
|
|
public void setPreviewEnabled(boolean previewEnabled) {
|
2010-11-24 01:55:22 +00:00
|
|
|
LatinKeyboard latinKeyboard = getLatinKeyboard();
|
|
|
|
if (latinKeyboard != null
|
|
|
|
&& (latinKeyboard.isPhoneKeyboard() || latinKeyboard.isNumberKeyboard())) {
|
|
|
|
// Phone and number keyboard never shows popup preview (except language switch).
|
2010-09-21 07:55:18 +00:00
|
|
|
super.setPreviewEnabled(false);
|
|
|
|
} else {
|
|
|
|
super.setPreviewEnabled(previewEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-11 10:02:21 +00:00
|
|
|
public void setLatinKeyboard(LatinKeyboard newKeyboard) {
|
|
|
|
final LatinKeyboard oldKeyboard = getLatinKeyboard();
|
|
|
|
if (oldKeyboard != null) {
|
|
|
|
// Reset old keyboard state before switching to new keyboard.
|
|
|
|
oldKeyboard.keyReleased();
|
|
|
|
}
|
|
|
|
super.setKeyboard(newKeyboard);
|
2010-03-30 23:49:15 +00:00
|
|
|
// One-seventh of the keyboard width seems like a reasonable threshold
|
2011-01-11 10:02:21 +00:00
|
|
|
mJumpThresholdSquare = newKeyboard.getMinWidth() / 7;
|
2010-03-30 23:49:15 +00:00
|
|
|
mJumpThresholdSquare *= mJumpThresholdSquare;
|
2010-04-06 13:27:17 +00:00
|
|
|
// Assuming there are 4 rows, this is the coordinate of the last row
|
2011-01-11 10:02:21 +00:00
|
|
|
mLastRowY = (newKeyboard.getHeight() * 3) / 4;
|
2010-03-30 23:49:15 +00:00
|
|
|
}
|
|
|
|
|
2010-10-25 09:04:01 +00:00
|
|
|
public LatinKeyboard getLatinKeyboard() {
|
2010-12-02 09:46:21 +00:00
|
|
|
Keyboard keyboard = getKeyboard();
|
2010-10-25 09:04:01 +00:00
|
|
|
if (keyboard instanceof LatinKeyboard) {
|
|
|
|
return (LatinKeyboard)keyboard;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 11:33:54 +00:00
|
|
|
public void setSpacebarTextFadeFactor(float fadeFactor, LatinKeyboard oldKeyboard) {
|
|
|
|
final LatinKeyboard currentKeyboard = getLatinKeyboard();
|
|
|
|
// We should not set text fade factor to the keyboard which does not display the language on
|
|
|
|
// its spacebar.
|
|
|
|
if (currentKeyboard != null && currentKeyboard == oldKeyboard)
|
|
|
|
currentKeyboard.setSpacebarTextFadeFactor(fadeFactor, this);
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
@Override
|
|
|
|
protected boolean onLongPress(Key key) {
|
2010-12-20 11:30:26 +00:00
|
|
|
int primaryCode = key.mCode;
|
2010-12-06 03:12:27 +00:00
|
|
|
if (primaryCode == Keyboard.CODE_SETTINGS) {
|
|
|
|
return invokeOnKey(Keyboard.CODE_SETTINGS_LONGPRESS);
|
2010-11-24 01:55:22 +00:00
|
|
|
} else if (primaryCode == '0' && getLatinKeyboard().isPhoneKeyboard()) {
|
2009-03-13 22:11:42 +00:00
|
|
|
// Long pressing on 0 in phone number keypad gives you a '+'.
|
2010-09-30 08:57:23 +00:00
|
|
|
return invokeOnKey('+');
|
2009-03-13 22:11:42 +00:00
|
|
|
} else {
|
|
|
|
return super.onLongPress(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-30 08:57:23 +00:00
|
|
|
private boolean invokeOnKey(int primaryCode) {
|
2010-12-20 07:13:57 +00:00
|
|
|
getOnKeyboardActionListener().onCodeInput(primaryCode, null,
|
2010-12-30 16:35:24 +00:00
|
|
|
KeyboardActionListener.NOT_A_TOUCH_COORDINATE,
|
|
|
|
KeyboardActionListener.NOT_A_TOUCH_COORDINATE);
|
2010-09-30 08:57:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
@Override
|
|
|
|
protected CharSequence adjustCase(CharSequence label) {
|
2010-10-25 09:04:01 +00:00
|
|
|
LatinKeyboard keyboard = getLatinKeyboard();
|
|
|
|
if (keyboard.isAlphaKeyboard()
|
2010-11-13 08:16:34 +00:00
|
|
|
&& keyboard.isShiftedOrShiftLocked()
|
2010-09-20 09:10:54 +00:00
|
|
|
&& !TextUtils.isEmpty(label) && label.length() < 3
|
2010-08-20 05:35:02 +00:00
|
|
|
&& Character.isLowerCase(label.charAt(0))) {
|
2010-12-10 06:24:28 +00:00
|
|
|
return label.toString().toUpperCase();
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
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-10-25 09:04:01 +00:00
|
|
|
LatinKeyboard keyboard = getLatinKeyboard();
|
2011-01-19 08:44:51 +00:00
|
|
|
if (keyboard == null) return true;
|
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.
|
2011-01-19 08:44:51 +00:00
|
|
|
if (handleSuddenJump(me)) return true;
|
2010-08-30 12:09:44 +00:00
|
|
|
|
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) {
|
2010-12-20 07:13:57 +00:00
|
|
|
getOnKeyboardActionListener().onCodeInput(
|
2010-12-02 09:46:21 +00:00
|
|
|
languageDirection == 1
|
2010-12-02 11:54:32 +00:00
|
|
|
? Keyboard.CODE_NEXT_LANGUAGE : Keyboard.CODE_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
|
|
|
@Override
|
|
|
|
public void draw(Canvas c) {
|
2010-12-09 12:06:26 +00:00
|
|
|
Utils.GCUtils.getInstance().reset();
|
2010-08-20 05:35:02 +00:00
|
|
|
boolean tryGC = true;
|
2010-12-09 12:06:26 +00:00
|
|
|
for (int i = 0; i < Utils.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) {
|
2010-08-20 05:35:02 +00:00
|
|
|
try {
|
|
|
|
super.draw(c);
|
|
|
|
tryGC = false;
|
|
|
|
} catch (OutOfMemoryError e) {
|
2010-12-09 12:06:26 +00:00
|
|
|
tryGC = Utils.GCUtils.getInstance().tryGCOrWait("LatinKeyboardView", e);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-11-24 08:32:23 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onAttachedToWindow() {
|
|
|
|
// Token is available from here.
|
|
|
|
VoiceIMEConnector.getInstance().onAttachedToWindow();
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|