Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
| 17 | package com.android.internal.widget; |
| 18 | |
| 19 | import android.content.Context; |
Jim Miller | 281a80d | 2010-02-18 19:54:22 -0800 | [diff] [blame] | 20 | import android.content.res.Resources; |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 21 | import android.inputmethodservice.Keyboard; |
| 22 | import android.inputmethodservice.KeyboardView; |
| 23 | import android.inputmethodservice.KeyboardView.OnKeyboardActionListener; |
| 24 | import android.os.Handler; |
| 25 | import android.os.SystemClock; |
Jim Miller | 281a80d | 2010-02-18 19:54:22 -0800 | [diff] [blame] | 26 | import android.os.Vibrator; |
Jim Miller | 280b602 | 2010-02-09 19:05:02 -0800 | [diff] [blame] | 27 | import android.provider.Settings; |
Jim Miller | 281a80d | 2010-02-18 19:54:22 -0800 | [diff] [blame] | 28 | import android.util.Log; |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 29 | import android.view.KeyCharacterMap; |
| 30 | import android.view.KeyEvent; |
| 31 | import android.view.View; |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 32 | import android.view.ViewAncestor; |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 33 | import com.android.internal.R; |
| 34 | |
| 35 | public class PasswordEntryKeyboardHelper implements OnKeyboardActionListener { |
| 36 | |
| 37 | public static final int KEYBOARD_MODE_ALPHA = 0; |
| 38 | public static final int KEYBOARD_MODE_NUMERIC = 1; |
| 39 | private static final int KEYBOARD_STATE_NORMAL = 0; |
| 40 | private static final int KEYBOARD_STATE_SHIFTED = 1; |
| 41 | private static final int KEYBOARD_STATE_CAPSLOCK = 2; |
Jim Miller | 281a80d | 2010-02-18 19:54:22 -0800 | [diff] [blame] | 42 | private static final String TAG = "PasswordEntryKeyboardHelper"; |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 43 | private int mKeyboardMode = KEYBOARD_MODE_ALPHA; |
| 44 | private int mKeyboardState = KEYBOARD_STATE_NORMAL; |
| 45 | private PasswordEntryKeyboard mQwertyKeyboard; |
| 46 | private PasswordEntryKeyboard mQwertyKeyboardShifted; |
| 47 | private PasswordEntryKeyboard mSymbolsKeyboard; |
| 48 | private PasswordEntryKeyboard mSymbolsKeyboardShifted; |
| 49 | private PasswordEntryKeyboard mNumericKeyboard; |
| 50 | private Context mContext; |
| 51 | private View mTargetView; |
| 52 | private KeyboardView mKeyboardView; |
Jim Miller | 281a80d | 2010-02-18 19:54:22 -0800 | [diff] [blame] | 53 | private long[] mVibratePattern; |
| 54 | private Vibrator mVibrator; |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 55 | |
| 56 | public PasswordEntryKeyboardHelper(Context context, KeyboardView keyboardView, View targetView) { |
Jae Yong Sung | 8171b51 | 2010-08-05 10:44:27 -0700 | [diff] [blame] | 57 | this(context, keyboardView, targetView, true); |
| 58 | } |
| 59 | |
| 60 | public PasswordEntryKeyboardHelper(Context context, KeyboardView keyboardView, View targetView, |
| 61 | boolean useFullScreenWidth) { |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 62 | mContext = context; |
| 63 | mTargetView = targetView; |
| 64 | mKeyboardView = keyboardView; |
Jae Yong Sung | 8171b51 | 2010-08-05 10:44:27 -0700 | [diff] [blame] | 65 | if (useFullScreenWidth || mKeyboardView.getLayoutParams().width == -1) { |
| 66 | createKeyboards(); |
| 67 | } else { |
| 68 | createKeyboardsWithSpecificSize(mKeyboardView.getLayoutParams().width, |
| 69 | mKeyboardView.getLayoutParams().height); |
| 70 | } |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 71 | mKeyboardView.setOnKeyboardActionListener(this); |
Jim Miller | 281a80d | 2010-02-18 19:54:22 -0800 | [diff] [blame] | 72 | mVibrator = new Vibrator(); |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | public boolean isAlpha() { |
| 76 | return mKeyboardMode == KEYBOARD_MODE_ALPHA; |
| 77 | } |
| 78 | |
Jae Yong Sung | 8171b51 | 2010-08-05 10:44:27 -0700 | [diff] [blame] | 79 | private void createKeyboardsWithSpecificSize(int viewWidth, int viewHeight) { |
| 80 | mNumericKeyboard = new PasswordEntryKeyboard(mContext, R.xml.password_kbd_numeric, |
| 81 | viewWidth, viewHeight); |
| 82 | mQwertyKeyboard = new PasswordEntryKeyboard(mContext, |
| 83 | R.xml.password_kbd_qwerty, R.id.mode_normal, viewWidth, viewHeight); |
| 84 | mQwertyKeyboard.enableShiftLock(); |
| 85 | |
| 86 | mQwertyKeyboardShifted = new PasswordEntryKeyboard(mContext, |
| 87 | R.xml.password_kbd_qwerty_shifted, |
| 88 | R.id.mode_normal, viewWidth, viewHeight); |
| 89 | mQwertyKeyboardShifted.enableShiftLock(); |
| 90 | mQwertyKeyboardShifted.setShifted(true); // always shifted. |
| 91 | |
| 92 | mSymbolsKeyboard = new PasswordEntryKeyboard(mContext, R.xml.password_kbd_symbols, |
| 93 | viewWidth, viewHeight); |
| 94 | mSymbolsKeyboard.enableShiftLock(); |
| 95 | |
| 96 | mSymbolsKeyboardShifted = new PasswordEntryKeyboard(mContext, |
| 97 | R.xml.password_kbd_symbols_shift, viewWidth, viewHeight); |
| 98 | mSymbolsKeyboardShifted.enableShiftLock(); |
| 99 | mSymbolsKeyboardShifted.setShifted(true); // always shifted |
| 100 | } |
| 101 | |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 102 | private void createKeyboards() { |
| 103 | mNumericKeyboard = new PasswordEntryKeyboard(mContext, R.xml.password_kbd_numeric); |
| 104 | mQwertyKeyboard = new PasswordEntryKeyboard(mContext, |
| 105 | R.xml.password_kbd_qwerty, R.id.mode_normal); |
| 106 | mQwertyKeyboard.enableShiftLock(); |
| 107 | |
| 108 | mQwertyKeyboardShifted = new PasswordEntryKeyboard(mContext, |
| 109 | R.xml.password_kbd_qwerty_shifted, |
| 110 | R.id.mode_normal); |
| 111 | mQwertyKeyboardShifted.enableShiftLock(); |
| 112 | mQwertyKeyboardShifted.setShifted(true); // always shifted. |
| 113 | |
| 114 | mSymbolsKeyboard = new PasswordEntryKeyboard(mContext, R.xml.password_kbd_symbols); |
| 115 | mSymbolsKeyboard.enableShiftLock(); |
| 116 | |
| 117 | mSymbolsKeyboardShifted = new PasswordEntryKeyboard(mContext, |
| 118 | R.xml.password_kbd_symbols_shift); |
| 119 | mSymbolsKeyboardShifted.enableShiftLock(); |
| 120 | mSymbolsKeyboardShifted.setShifted(true); // always shifted |
| 121 | } |
| 122 | |
| 123 | public void setKeyboardMode(int mode) { |
| 124 | switch (mode) { |
| 125 | case KEYBOARD_MODE_ALPHA: |
| 126 | mKeyboardView.setKeyboard(mQwertyKeyboard); |
| 127 | mKeyboardState = KEYBOARD_STATE_NORMAL; |
Jim Miller | 280b602 | 2010-02-09 19:05:02 -0800 | [diff] [blame] | 128 | final boolean visiblePassword = Settings.System.getInt( |
| 129 | mContext.getContentResolver(), |
| 130 | Settings.System.TEXT_SHOW_PASSWORD, 1) != 0; |
Jim Miller | 7dd879a | 2011-01-23 16:18:01 -0800 | [diff] [blame] | 131 | final boolean enablePreview = false; // TODO: grab from configuration |
| 132 | mKeyboardView.setPreviewEnabled(visiblePassword && enablePreview); |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 133 | break; |
| 134 | case KEYBOARD_MODE_NUMERIC: |
| 135 | mKeyboardView.setKeyboard(mNumericKeyboard); |
| 136 | mKeyboardState = KEYBOARD_STATE_NORMAL; |
Jim Miller | 280b602 | 2010-02-09 19:05:02 -0800 | [diff] [blame] | 137 | mKeyboardView.setPreviewEnabled(false); // never show popup for numeric keypad |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 138 | break; |
| 139 | } |
| 140 | mKeyboardMode = mode; |
| 141 | } |
| 142 | |
Joe Onorato | 1653775 | 2010-03-17 22:19:51 -0700 | [diff] [blame] | 143 | private void sendKeyEventsToTarget(int character) { |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 144 | Handler handler = mTargetView.getHandler(); |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 145 | KeyEvent[] events = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD).getEvents( |
Joe Onorato | 1653775 | 2010-03-17 22:19:51 -0700 | [diff] [blame] | 146 | new char[] { (char) character }); |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 147 | if (events != null) { |
Joe Onorato | 1653775 | 2010-03-17 22:19:51 -0700 | [diff] [blame] | 148 | final int N = events.length; |
| 149 | for (int i=0; i<N; i++) { |
| 150 | KeyEvent event = events[i]; |
| 151 | event = KeyEvent.changeFlags(event, event.getFlags() |
| 152 | | KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE); |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 153 | handler.sendMessage(handler.obtainMessage(ViewAncestor.DISPATCH_KEY, event)); |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public void sendDownUpKeyEvents(int keyEventCode) { |
| 159 | long eventTime = SystemClock.uptimeMillis(); |
| 160 | Handler handler = mTargetView.getHandler(); |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 161 | handler.sendMessage(handler.obtainMessage(ViewAncestor.DISPATCH_KEY_FROM_IME, |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 162 | new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, keyEventCode, 0, 0, |
| 163 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 164 | KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE))); |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 165 | handler.sendMessage(handler.obtainMessage(ViewAncestor.DISPATCH_KEY_FROM_IME, |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 166 | new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP, keyEventCode, 0, 0, |
| 167 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 168 | KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE))); |
| 169 | } |
| 170 | |
| 171 | public void onKey(int primaryCode, int[] keyCodes) { |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 172 | if (primaryCode == Keyboard.KEYCODE_DELETE) { |
| 173 | handleBackspace(); |
| 174 | } else if (primaryCode == Keyboard.KEYCODE_SHIFT) { |
| 175 | handleShift(); |
| 176 | } else if (primaryCode == Keyboard.KEYCODE_CANCEL) { |
| 177 | handleClose(); |
| 178 | return; |
| 179 | } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE && mKeyboardView != null) { |
| 180 | handleModeChange(); |
| 181 | } else { |
| 182 | handleCharacter(primaryCode, keyCodes); |
| 183 | // Switch back to old keyboard if we're not in capslock mode |
| 184 | if (mKeyboardState == KEYBOARD_STATE_SHIFTED) { |
| 185 | // skip to the unlocked state |
| 186 | mKeyboardState = KEYBOARD_STATE_CAPSLOCK; |
| 187 | handleShift(); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Jim Miller | 281a80d | 2010-02-18 19:54:22 -0800 | [diff] [blame] | 192 | /** |
| 193 | * Sets and enables vibrate pattern. If id is 0 (or can't be loaded), vibrate is disabled. |
| 194 | * @param id resource id for array containing vibrate pattern. |
| 195 | */ |
| 196 | public void setVibratePattern(int id) { |
| 197 | int[] tmpArray = null; |
| 198 | try { |
| 199 | tmpArray = mContext.getResources().getIntArray(id); |
| 200 | } catch (Resources.NotFoundException e) { |
| 201 | if (id != 0) { |
| 202 | Log.e(TAG, "Vibrate pattern missing", e); |
| 203 | } |
| 204 | } |
| 205 | if (tmpArray == null) { |
| 206 | mVibratePattern = null; |
| 207 | return; |
| 208 | } |
| 209 | mVibratePattern = new long[tmpArray.length]; |
| 210 | for (int i = 0; i < tmpArray.length; i++) { |
| 211 | mVibratePattern[i] = tmpArray[i]; |
| 212 | } |
| 213 | } |
| 214 | |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 215 | private void handleModeChange() { |
| 216 | final Keyboard current = mKeyboardView.getKeyboard(); |
| 217 | Keyboard next = null; |
| 218 | if (current == mQwertyKeyboard || current == mQwertyKeyboardShifted) { |
| 219 | next = mSymbolsKeyboard; |
| 220 | } else if (current == mSymbolsKeyboard || current == mSymbolsKeyboardShifted) { |
| 221 | next = mQwertyKeyboard; |
| 222 | } |
| 223 | if (next != null) { |
| 224 | mKeyboardView.setKeyboard(next); |
| 225 | mKeyboardState = KEYBOARD_STATE_NORMAL; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | private void handleBackspace() { |
| 230 | sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL); |
| 231 | } |
| 232 | |
| 233 | private void handleShift() { |
| 234 | if (mKeyboardView == null) { |
| 235 | return; |
| 236 | } |
| 237 | Keyboard current = mKeyboardView.getKeyboard(); |
| 238 | PasswordEntryKeyboard next = null; |
| 239 | final boolean isAlphaMode = current == mQwertyKeyboard |
| 240 | || current == mQwertyKeyboardShifted; |
| 241 | if (mKeyboardState == KEYBOARD_STATE_NORMAL) { |
| 242 | mKeyboardState = isAlphaMode ? KEYBOARD_STATE_SHIFTED : KEYBOARD_STATE_CAPSLOCK; |
| 243 | next = isAlphaMode ? mQwertyKeyboardShifted : mSymbolsKeyboardShifted; |
| 244 | } else if (mKeyboardState == KEYBOARD_STATE_SHIFTED) { |
| 245 | mKeyboardState = KEYBOARD_STATE_CAPSLOCK; |
| 246 | next = isAlphaMode ? mQwertyKeyboardShifted : mSymbolsKeyboardShifted; |
| 247 | } else if (mKeyboardState == KEYBOARD_STATE_CAPSLOCK) { |
| 248 | mKeyboardState = KEYBOARD_STATE_NORMAL; |
| 249 | next = isAlphaMode ? mQwertyKeyboard : mSymbolsKeyboard; |
| 250 | } |
| 251 | if (next != null) { |
| 252 | if (next != current) { |
| 253 | mKeyboardView.setKeyboard(next); |
| 254 | } |
| 255 | next.setShiftLocked(mKeyboardState == KEYBOARD_STATE_CAPSLOCK); |
| 256 | mKeyboardView.setShifted(mKeyboardState != KEYBOARD_STATE_NORMAL); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | private void handleCharacter(int primaryCode, int[] keyCodes) { |
| 261 | // Maybe turn off shift if not in capslock mode. |
| 262 | if (mKeyboardView.isShifted() && primaryCode != ' ' && primaryCode != '\n') { |
| 263 | primaryCode = Character.toUpperCase(primaryCode); |
| 264 | } |
| 265 | sendKeyEventsToTarget(primaryCode); |
| 266 | } |
| 267 | |
| 268 | private void handleClose() { |
| 269 | |
| 270 | } |
| 271 | |
| 272 | public void onPress(int primaryCode) { |
Jim Miller | 281a80d | 2010-02-18 19:54:22 -0800 | [diff] [blame] | 273 | if (mVibratePattern != null) { |
| 274 | mVibrator.vibrate(mVibratePattern, -1); |
| 275 | } |
Jim Miller | 0b31970 | 2010-02-05 18:51:59 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | public void onRelease(int primaryCode) { |
| 279 | |
| 280 | } |
| 281 | |
| 282 | public void onText(CharSequence text) { |
| 283 | |
| 284 | } |
| 285 | |
| 286 | public void swipeDown() { |
| 287 | |
| 288 | } |
| 289 | |
| 290 | public void swipeLeft() { |
| 291 | |
| 292 | } |
| 293 | |
| 294 | public void swipeRight() { |
| 295 | |
| 296 | } |
| 297 | |
| 298 | public void swipeUp() { |
| 299 | |
| 300 | } |
| 301 | }; |