/ime/latinime/src/com/googlecode/eyesfree/inputmethod/latin/LatinKeyboardView.java
http://eyes-free.googlecode.com/ · Java · 327 lines · 269 code · 30 blank · 28 comment · 49 complexity · bccfcdb040517fdfa0983acb1a9312c5 MD5 · raw file
- /*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * 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.googlecode.eyesfree.inputmethod.latin;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.inputmethodservice.Keyboard;
- import android.inputmethodservice.Keyboard.Key;
- import android.os.Handler;
- import android.os.Message;
- import android.os.SystemClock;
- import android.text.TextUtils;
- import android.util.AttributeSet;
- import android.view.KeyEvent;
- import android.view.MotionEvent;
- import java.util.List;
- public class LatinKeyboardView extends LatinKeyboardBaseView {
- public static final int KEYCODE_TAB = 9;
- public static final int KEYCODE_RETURN = 10;
- public static final int KEYCODE_SPACE = 32;
- public static final int KEYCODE_SHIFT = -1;
- public static final int KEYCODE_SYMBOL = -2;
- public static final int KEYCODE_DELETE = -5;
- public static final int KEYCODE_OPTIONS = -100;
- public static final int KEYCODE_OPTIONS_LONGPRESS = -101;
- public static final int KEYCODE_VOICE = -102;
- public static final int KEYCODE_F1 = -103;
- public static final int KEYCODE_NEXT_LANGUAGE = -104;
- public static final int KEYCODE_PREV_LANGUAGE = -105;
- public static final int KEYCODE_BACK = -106;
- public static final int KEYCODE_SEARCH = -107;
- public static final int KEYCODE_HOME = -108;
- public static final int KEYCODE_MENU = -109;
- public static final int KEYCODE_CALL = -110;
- public static final int KEYCODE_ENDCALL = -111;
- public static final int KEYCODE_ALT = -112;
- public static final int KEYCODE_DPAD_UP = -113;
- public static final int KEYCODE_DPAD_LEFT = -114;
- public static final int KEYCODE_DPAD_CENTER = -115;
- public static final int KEYCODE_DPAD_RIGHT = -116;
- public static final int KEYCODE_DPAD_DOWN = -117;
- public static final int KEYCODE_UNKNOWN = Integer.MIN_VALUE;
- /**
- * Returns the key code from {@link KeyEvent} for a given internal key code.
- *
- * @param internalKeyCode The internal key code.
- * @return the key code from {@link KeyEvent}
- */
- static int getKeyCode(int internalKeyCode) {
- switch (internalKeyCode) {
- case KEYCODE_BACK:
- return KeyEvent.KEYCODE_BACK;
- case KEYCODE_SEARCH:
- return KeyEvent.KEYCODE_SEARCH;
- case KEYCODE_MENU:
- return KeyEvent.KEYCODE_MENU;
- case KEYCODE_CALL:
- return KeyEvent.KEYCODE_CALL;
- case KEYCODE_ENDCALL:
- return KeyEvent.KEYCODE_ENDCALL;
- case KEYCODE_ALT:
- return KeyEvent.KEYCODE_ALT_LEFT;
- case KEYCODE_DPAD_UP:
- return KeyEvent.KEYCODE_DPAD_UP;
- case KEYCODE_DPAD_LEFT:
- return KeyEvent.KEYCODE_DPAD_LEFT;
- case KEYCODE_DPAD_CENTER:
- return KeyEvent.KEYCODE_DPAD_CENTER;
- case KEYCODE_DPAD_RIGHT:
- return KeyEvent.KEYCODE_DPAD_RIGHT;
- case KEYCODE_DPAD_DOWN:
- return KeyEvent.KEYCODE_DPAD_DOWN;
- default:
- return KeyEvent.KEYCODE_UNKNOWN;
- }
- }
- private Keyboard mPhoneKeyboard;
- public LatinKeyboardView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
- public LatinKeyboardView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- public void setPhoneKeyboard(Keyboard phoneKeyboard) {
- mPhoneKeyboard = phoneKeyboard;
- }
- @Override
- public void setPreviewEnabled(boolean previewEnabled) {
- if (getKeyboard() == mPhoneKeyboard) {
- // Phone keyboard never shows popup preview (except language switch).
- super.setPreviewEnabled(false);
- } else {
- super.setPreviewEnabled(previewEnabled);
- }
- }
- @Override
- public void setKeyboard(Keyboard k) {
- super.setKeyboard(k);
- setKeyboardLocal(k);
- }
- @Override
- protected boolean onLongPress(Key key) {
- int primaryCode = key.codes[0];
- if (primaryCode == KEYCODE_OPTIONS) {
- return invokeOnKey(KEYCODE_OPTIONS_LONGPRESS);
- } else if (primaryCode == '0' && getKeyboard() == mPhoneKeyboard) {
- // Long pressing on 0 in phone number keypad gives you a '+'.
- return invokeOnKey('+');
- } else {
- return super.onLongPress(key);
- }
- }
- private boolean invokeOnKey(int primaryCode) {
- getOnKeyboardActionListener().onKey(primaryCode, null,
- LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE,
- LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE);
- return true;
- }
- @Override
- protected CharSequence adjustCase(CharSequence label) {
- Keyboard keyboard = getKeyboard();
- if (keyboard.isShifted()
- && keyboard instanceof LatinKeyboard
- && ((LatinKeyboard) keyboard).isAlphaKeyboard()
- && !TextUtils.isEmpty(label) && label.length() < 3
- && Character.isLowerCase(label.charAt(0))) {
- label = label.toString().toUpperCase();
- }
- return label;
- }
- public boolean setShiftLocked(boolean shiftLocked) {
- Keyboard keyboard = getKeyboard();
- if (keyboard instanceof LatinKeyboard) {
- ((LatinKeyboard)keyboard).setShiftLocked(shiftLocked);
- invalidateAllKeys();
- return true;
- }
- return false;
- }
- public boolean isShiftLocked() {
- Keyboard keyboard = getKeyboard();
- if (keyboard instanceof LatinKeyboard) {
- return ((LatinKeyboard)keyboard).isShiftLocked();
- }
- return false;
- }
- @Override
- public boolean onTouchEvent(MotionEvent me) {
- LatinKeyboard keyboard = (LatinKeyboard) getKeyboard();
- if (DEBUG_LINE) {
- mLastX = (int) me.getX();
- mLastY = (int) me.getY();
- invalidate();
- }
- // Reset any bounding box controls in the keyboard
- if (me.getAction() == MotionEvent.ACTION_DOWN) {
- keyboard.keyReleased();
- }
- return super.onTouchEvent(me);
- }
- /**************************** INSTRUMENTATION *******************************/
- static final boolean DEBUG_AUTO_PLAY = false;
- static final boolean DEBUG_LINE = false;
- private static final int MSG_TOUCH_DOWN = 1;
- private static final int MSG_TOUCH_UP = 2;
- Handler mHandler2;
- private String mStringToPlay;
- private int mStringIndex;
- private boolean mDownDelivered;
- private Key[] mAsciiKeys = new Key[256];
- private boolean mPlaying;
- private int mLastX;
- private int mLastY;
- private Paint mPaint;
- private void setKeyboardLocal(Keyboard k) {
- 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;
- switch (msg.what) {
- case MSG_TOUCH_DOWN:
- if (mStringIndex >= mStringToPlay.length()) {
- mPlaying = false;
- return;
- }
- char c = mStringToPlay.charAt(mStringIndex);
- while (c > 255 || mAsciiKeys[c] == null) {
- mStringIndex++;
- if (mStringIndex >= mStringToPlay.length()) {
- mPlaying = false;
- return;
- }
- c = mStringToPlay.charAt(mStringIndex);
- }
- int x = mAsciiKeys[c].x + 10;
- int y = mAsciiKeys[c].y + 26;
- MotionEvent me = MotionEvent.obtain(SystemClock.uptimeMillis(),
- SystemClock.uptimeMillis(),
- 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++;
- MotionEvent me2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
- SystemClock.uptimeMillis(),
- 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];
- if (code >= 0 && code <= 255) {
- mAsciiKeys[code] = keys.get(i);
- }
- }
- }
- 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);
- }
- }
- @Override
- public void draw(Canvas c) {
- 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);
- }
- }
- }
- 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);
- }
- }
- }