/tags/20100328/src/com/menny/android/anysoftkeyboard/AnyKeyboardView.java

http://softkeyboard.googlecode.com/ · Java · 236 lines · 70 code · 23 blank · 143 comment · 20 complexity · 69fe3297a2b4f00c219f1cbd5afe7882 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008-2009 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.menny.android.anysoftkeyboard;
  17. import android.content.Context;
  18. import android.inputmethodservice.Keyboard;
  19. import android.inputmethodservice.KeyboardView;
  20. import android.inputmethodservice.Keyboard.Key;
  21. import android.util.AttributeSet;
  22. import android.util.Log;
  23. import com.menny.android.anysoftkeyboard.keyboards.AnyKeyboard;
  24. public class AnyKeyboardView extends KeyboardView {
  25. private final static String TAG = "ASK AnyKeyboardView";
  26. static final int KEYCODE_OPTIONS = -100;
  27. //static final int KEYCODE_SHIFT_LONGPRESS = -101;
  28. static final int KEYCODE_SMILEY_LONGPRESS = -102;
  29. private Keyboard mPhoneKeyboard;
  30. public AnyKeyboardView(Context context, AttributeSet attrs) {
  31. super(context, attrs);
  32. initializeStuff();
  33. }
  34. private void initializeStuff() {
  35. setPreviewEnabled(AnySoftKeyboardConfiguration.getInstance().getShowKeyPreview());
  36. setProximityCorrectionEnabled(true);
  37. }
  38. public AnyKeyboardView(Context context, AttributeSet attrs, int defStyle) {
  39. super(context, attrs, defStyle);
  40. initializeStuff();
  41. }
  42. // @Override
  43. // public boolean onTouchEvent(MotionEvent me) {
  44. // Log.d(TAG, "onTouchEvent");
  45. // return super.onTouchEvent(me);
  46. // }
  47. //
  48. // @Override
  49. // public boolean onKeyDown(int keyCode, KeyEvent event) {
  50. // Log.d(TAG, "onKeyDown:"+keyCode);
  51. // return super.onKeyDown(keyCode, event);
  52. // }
  53. //
  54. // @Override
  55. // public boolean onKeyUp(int keyCode, KeyEvent event) {
  56. // Log.d(TAG, "onKeyUp:"+keyCode);
  57. // return super.onKeyUp(keyCode, event);
  58. // }
  59. public void setPhoneKeyboard(Keyboard phoneKeyboard) {
  60. mPhoneKeyboard = phoneKeyboard;
  61. }
  62. @Override
  63. protected boolean onLongPress(Key key) {
  64. if (key.codes[0] == 10) {
  65. getOnKeyboardActionListener().onKey(KEYCODE_OPTIONS, null);
  66. return true;
  67. // } else if (key.codes[0] == Keyboard.KEYCODE_SHIFT) {
  68. // getOnKeyboardActionListener().onKey(KEYCODE_SHIFT_LONGPRESS, null);
  69. // invalidate();
  70. // return true;
  71. } else if (key.codes[0] == AnyKeyboard.KEYCODE_SMILEY) {
  72. getOnKeyboardActionListener().onKey(KEYCODE_SMILEY_LONGPRESS, null);
  73. invalidate();
  74. return true;
  75. } else if (key.codes[0] == '0' && getKeyboard() == mPhoneKeyboard) {
  76. // Long pressing on 0 in phone number keypad gives you a '+'.
  77. getOnKeyboardActionListener().onKey('+', null);
  78. return true;
  79. } else {
  80. return super.onLongPress(key);
  81. }
  82. }
  83. public void simulateLongPress(int keyCode)
  84. {
  85. if (super.getKeyboard() == null)
  86. return;
  87. for(Key key : super.getKeyboard().getKeys())
  88. {
  89. if (key.codes[0] == keyCode)
  90. {
  91. super.onLongPress(key);
  92. return;
  93. }
  94. }
  95. }
  96. @Override
  97. public void setKeyboard(Keyboard keyboard) {
  98. if ((keyboard != null) && (keyboard.getMinWidth() != getWidth()))
  99. {
  100. Log.w(TAG, "NOTE: The SET keyboard has the wrong width! Keyboard width: "+keyboard.getMinWidth()+", device width:"+getWidth());
  101. }
  102. super.setKeyboard(keyboard);
  103. }
  104. /**************************** INSTRUMENTATION *******************************/
  105. // static final boolean DEBUG_AUTO_PLAY = false;
  106. // private static final int MSG_TOUCH_DOWN = 1;
  107. // private static final int MSG_TOUCH_UP = 2;
  108. //
  109. // Handler mHandler2;
  110. //
  111. // private String mStringToPlay;
  112. // private int mStringIndex;
  113. // private boolean mDownDelivered;
  114. // private Key[] mAsciiKeys = new Key[256];
  115. // private boolean mPlaying;
  116. // @Override
  117. // public void setKeyboard(Keyboard k) {
  118. // super.setKeyboard(k);
  119. // if (DEBUG_AUTO_PLAY) {
  120. // findKeys();
  121. // if (mHandler2 == null) {
  122. // mHandler2 = new Handler() {
  123. // @Override
  124. // public void handleMessage(Message msg) {
  125. // removeMessages(MSG_TOUCH_DOWN);
  126. // removeMessages(MSG_TOUCH_UP);
  127. // if (mPlaying == false) return;
  128. //
  129. // switch (msg.what) {
  130. // case MSG_TOUCH_DOWN:
  131. // if (mStringIndex >= mStringToPlay.length()) {
  132. // mPlaying = false;
  133. // return;
  134. // }
  135. // char c = mStringToPlay.charAt(mStringIndex);
  136. // while (c > 255 || mAsciiKeys[(int) c] == null) {
  137. // mStringIndex++;
  138. // if (mStringIndex >= mStringToPlay.length()) {
  139. // mPlaying = false;
  140. // return;
  141. // }
  142. // c = mStringToPlay.charAt(mStringIndex);
  143. // }
  144. // int x = mAsciiKeys[c].x + 10;
  145. // int y = mAsciiKeys[c].y + 26;
  146. // MotionEvent me = MotionEvent.obtain(SystemClock.uptimeMillis(),
  147. // SystemClock.uptimeMillis(),
  148. // MotionEvent.ACTION_DOWN, x, y, 0);
  149. // AnyKeyboardView.this.dispatchTouchEvent(me);
  150. // me.recycle();
  151. // sendEmptyMessageDelayed(MSG_TOUCH_UP, 500); // Deliver up in 500ms if nothing else
  152. // // happens
  153. // mDownDelivered = true;
  154. // break;
  155. // case MSG_TOUCH_UP:
  156. // char cUp = mStringToPlay.charAt(mStringIndex);
  157. // int x2 = mAsciiKeys[cUp].x + 10;
  158. // int y2 = mAsciiKeys[cUp].y + 26;
  159. // mStringIndex++;
  160. //
  161. // MotionEvent me2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
  162. // SystemClock.uptimeMillis(),
  163. // MotionEvent.ACTION_UP, x2, y2, 0);
  164. // AnyKeyboardView.this.dispatchTouchEvent(me2);
  165. // me2.recycle();
  166. // sendEmptyMessageDelayed(MSG_TOUCH_DOWN, 500); // Deliver up in 500ms if nothing else
  167. // // happens
  168. // mDownDelivered = false;
  169. // break;
  170. // }
  171. // }
  172. // };
  173. //
  174. // }
  175. // }
  176. // }
  177. // private void findKeys() {
  178. // List<Key> keys = getKeyboard().getKeys();
  179. // // Get the keys on this keyboard
  180. // for (int i = 0; i < keys.size(); i++) {
  181. // int code = keys.get(i).codes[0];
  182. // if (code >= 0 && code <= 255) {
  183. // mAsciiKeys[code] = keys.get(i);
  184. // }
  185. // }
  186. // }
  187. // void startPlaying(String s) {
  188. // if (!DEBUG_AUTO_PLAY) return;
  189. // if (s == null) return;
  190. // mStringToPlay = s.toLowerCase();
  191. // mPlaying = true;
  192. // mDownDelivered = false;
  193. // mStringIndex = 0;
  194. // mHandler2.sendEmptyMessageDelayed(MSG_TOUCH_DOWN, 10);
  195. // }
  196. // @Override
  197. // public void draw(Canvas c) {
  198. // super.draw(c);
  199. // if (DEBUG_AUTO_PLAY && mPlaying) {
  200. // mHandler2.removeMessages(MSG_TOUCH_DOWN);
  201. // mHandler2.removeMessages(MSG_TOUCH_UP);
  202. // if (mDownDelivered) {
  203. // mHandler2.sendEmptyMessageDelayed(MSG_TOUCH_UP, 20);
  204. // } else {
  205. // mHandler2.sendEmptyMessageDelayed(MSG_TOUCH_DOWN, 20);
  206. // }
  207. // }
  208. // }
  209. public void requestRedraw()
  210. {
  211. super.invalidate();
  212. }
  213. }