KeyboardSwitcher.java revision d01ae897d38d4e788e4f089e2b1d6d74655847c6
1/* 2 * Copyright (C) 2008 The Android Open Source Project 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 17package com.android.inputmethod.keyboard; 18 19import com.android.inputmethod.latin.LatinIME; 20import com.android.inputmethod.latin.Settings; 21import com.android.inputmethod.latin.Utils; 22import com.android.inputmethod.latin.LatinImeLogger; 23import com.android.inputmethod.latin.R; 24import com.android.inputmethod.latin.SubtypeSwitcher; 25 26import android.content.Context; 27import android.content.SharedPreferences; 28import android.content.res.Resources; 29import android.util.Log; 30import android.view.InflateException; 31import android.view.inputmethod.InputMethodManager; 32 33import java.lang.ref.SoftReference; 34import java.util.HashMap; 35import java.util.Locale; 36 37public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceChangeListener { 38 private static final String TAG = "KeyboardSwitcher"; 39 private static final boolean DEBUG = false; 40 public static final boolean DEBUG_STATE = false; 41 42 private static String sConfigDefaultKeyboardThemeId; 43 public static final String PREF_KEYBOARD_LAYOUT = "pref_keyboard_layout_20100902"; 44 private static final int[] KEYBOARD_THEMES = { 45 R.layout.input_basic, 46 R.layout.input_basic_highcontrast, 47 R.layout.input_stone_normal, 48 R.layout.input_stone_bold, 49 R.layout.input_gingerbread, 50 R.layout.input_honeycomb, 51 }; 52 53 private SubtypeSwitcher mSubtypeSwitcher; 54 private SharedPreferences mPrefs; 55 56 private LatinKeyboardView mInputView; 57 private LatinIME mInputMethodService; 58 59 private ShiftKeyState mShiftKeyState = new ShiftKeyState("Shift"); 60 private ModifierKeyState mSymbolKeyState = new ModifierKeyState("Symbol"); 61 62 private KeyboardId mSymbolsId; 63 private KeyboardId mSymbolsShiftedId; 64 65 private KeyboardId mCurrentId; 66 private final HashMap<KeyboardId, SoftReference<LatinKeyboard>> mKeyboardCache = 67 new HashMap<KeyboardId, SoftReference<LatinKeyboard>>(); 68 69 private int mMode = KeyboardId.MODE_TEXT; /* default value */ 70 private int mImeOptions; 71 private boolean mIsSymbols; 72 /** mIsAutoCorrectionActive indicates that auto corrected word will be input instead of 73 * what user actually typed. */ 74 private boolean mIsAutoCorrectionActive; 75 private boolean mVoiceKeyEnabled; 76 private boolean mVoiceButtonOnPrimary; 77 78 private static final int AUTO_MODE_SWITCH_STATE_ALPHA = 0; 79 private static final int AUTO_MODE_SWITCH_STATE_SYMBOL_BEGIN = 1; 80 private static final int AUTO_MODE_SWITCH_STATE_SYMBOL = 2; 81 // The following states are used only on the distinct multi-touch panel devices. 82 private static final int AUTO_MODE_SWITCH_STATE_MOMENTARY = 3; 83 private static final int AUTO_MODE_SWITCH_STATE_CHORDING = 4; 84 private int mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_ALPHA; 85 86 // Indicates whether or not we have the settings key 87 private boolean mHasSettingsKey; 88 private static final int SETTINGS_KEY_MODE_AUTO = R.string.settings_key_mode_auto; 89 private static final int SETTINGS_KEY_MODE_ALWAYS_SHOW = 90 R.string.settings_key_mode_always_show; 91 // NOTE: No need to have SETTINGS_KEY_MODE_ALWAYS_HIDE here because it's not being referred to 92 // in the source code now. 93 // Default is SETTINGS_KEY_MODE_AUTO. 94 private static final int DEFAULT_SETTINGS_KEY_MODE = SETTINGS_KEY_MODE_AUTO; 95 96 private int mLayoutId; 97 98 private static final KeyboardSwitcher sInstance = new KeyboardSwitcher(); 99 100 public static KeyboardSwitcher getInstance() { 101 return sInstance; 102 } 103 104 private KeyboardSwitcher() { 105 // Intentional empty constructor for singleton. 106 } 107 108 public static void init(LatinIME ims, SharedPreferences prefs) { 109 sInstance.mInputMethodService = ims; 110 sInstance.mPrefs = prefs; 111 sInstance.mSubtypeSwitcher = SubtypeSwitcher.getInstance(); 112 113 try { 114 sConfigDefaultKeyboardThemeId = ims.getString( 115 R.string.config_default_keyboard_theme_id); 116 sInstance.mLayoutId = Integer.valueOf( 117 prefs.getString(PREF_KEYBOARD_LAYOUT, sConfigDefaultKeyboardThemeId)); 118 } catch (NumberFormatException e) { 119 sConfigDefaultKeyboardThemeId = "0"; 120 sInstance.mLayoutId = 0; 121 } 122 prefs.registerOnSharedPreferenceChangeListener(sInstance); 123 } 124 125 private void makeSymbolsKeyboardIds() { 126 final Locale locale = mSubtypeSwitcher.getInputLocale(); 127 final Resources res = mInputMethodService.getResources(); 128 final int orientation = res.getConfiguration().orientation; 129 final int mode = mMode; 130 final int colorScheme = getColorScheme(); 131 final boolean hasSettingsKey = mHasSettingsKey; 132 final boolean voiceKeyEnabled = mVoiceKeyEnabled; 133 final boolean hasVoiceKey = voiceKeyEnabled && !mVoiceButtonOnPrimary; 134 final int imeOptions = mImeOptions; 135 // Note: This comment is only applied for phone number keyboard layout. 136 // On non-xlarge device, "@integer/key_switch_alpha_symbol" key code is used to switch 137 // between "phone keyboard" and "phone symbols keyboard". But on xlarge device, 138 // "@integer/key_shift" key code is used for that purpose in order to properly display 139 // "more" and "locked more" key labels. To achieve these behavior, we should initialize 140 // mSymbolsId and mSymbolsShiftedId to "phone keyboard" and "phone symbols keyboard" 141 // respectively here for xlarge device's layout switching. 142 int xmlId = mode == KeyboardId.MODE_PHONE ? R.xml.kbd_phone : R.xml.kbd_symbols; 143 mSymbolsId = new KeyboardId( 144 res.getResourceEntryName(xmlId), xmlId, locale, orientation, mode, colorScheme, 145 hasSettingsKey, voiceKeyEnabled, hasVoiceKey, imeOptions, true); 146 xmlId = mode == KeyboardId.MODE_PHONE ? R.xml.kbd_phone_symbols : R.xml.kbd_symbols_shift; 147 mSymbolsShiftedId = new KeyboardId( 148 res.getResourceEntryName(xmlId), xmlId, locale, orientation, mode, colorScheme, 149 hasSettingsKey, voiceKeyEnabled, hasVoiceKey, imeOptions, true); 150 } 151 152 private boolean hasVoiceKey(boolean isSymbols) { 153 return mVoiceKeyEnabled && (isSymbols != mVoiceButtonOnPrimary); 154 } 155 156 public void loadKeyboard(int mode, int imeOptions, boolean voiceKeyEnabled, 157 boolean voiceButtonOnPrimary) { 158 mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_ALPHA; 159 try { 160 if (mInputView == null) return; 161 final Keyboard oldKeyboard = mInputView.getKeyboard(); 162 loadKeyboardInternal(mode, imeOptions, voiceKeyEnabled, voiceButtonOnPrimary, false); 163 final Keyboard newKeyboard = mInputView.getKeyboard(); 164 if (newKeyboard.isAlphaKeyboard()) { 165 final boolean localeChanged = (oldKeyboard == null) 166 || !newKeyboard.mId.mLocale.equals(oldKeyboard.mId.mLocale); 167 mInputMethodService.mHandler.startDisplayLanguageOnSpacebar(localeChanged); 168 } 169 } catch (RuntimeException e) { 170 Log.w(TAG, e); 171 LatinImeLogger.logOnException(mode + "," + imeOptions, e); 172 } 173 } 174 175 private void loadKeyboardInternal(int mode, int imeOptions, boolean voiceButtonEnabled, 176 boolean voiceButtonOnPrimary, boolean isSymbols) { 177 if (mInputView == null) return; 178 179 mMode = mode; 180 mImeOptions = imeOptions; 181 mVoiceKeyEnabled = voiceButtonEnabled; 182 mVoiceButtonOnPrimary = voiceButtonOnPrimary; 183 mIsSymbols = isSymbols; 184 // Update the settings key state because number of enabled IMEs could have been changed 185 mHasSettingsKey = getSettingsKeyMode(mPrefs, mInputMethodService); 186 final KeyboardId id = getKeyboardId(mode, imeOptions, isSymbols); 187 188 final Keyboard oldKeyboard = mInputView.getKeyboard(); 189 if (oldKeyboard != null && oldKeyboard.mId.equals(id)) 190 return; 191 192 makeSymbolsKeyboardIds(); 193 mCurrentId = id; 194 mInputView.setPreviewEnabled(mInputMethodService.getPopupOn()); 195 mInputView.setKeyboard(getKeyboard(id)); 196 } 197 198 private LatinKeyboard getKeyboard(KeyboardId id) { 199 final SoftReference<LatinKeyboard> ref = mKeyboardCache.get(id); 200 LatinKeyboard keyboard = (ref == null) ? null : ref.get(); 201 if (keyboard == null) { 202 final Locale savedLocale = mSubtypeSwitcher.changeSystemLocale( 203 mSubtypeSwitcher.getInputLocale()); 204 205 keyboard = new LatinKeyboard(mInputMethodService, id); 206 207 if (id.mEnableShiftLock) { 208 keyboard.enableShiftLock(); 209 } 210 211 mKeyboardCache.put(id, new SoftReference<LatinKeyboard>(keyboard)); 212 if (DEBUG) 213 Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": " 214 + ((ref == null) ? "LOAD" : "GCed") + " id=" + id); 215 216 mSubtypeSwitcher.changeSystemLocale(savedLocale); 217 } else if (DEBUG) { 218 Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": HIT id=" + id); 219 } 220 221 keyboard.onAutoCorrectionStateChanged(mIsAutoCorrectionActive); 222 keyboard.setShifted(false); 223 // If the cached keyboard had been switched to another keyboard while the language was 224 // displayed on its spacebar, it might have had arbitrary text fade factor. In such case, 225 // we should reset the text fade factor. 226 keyboard.setSpacebarTextFadeFactor(0.0f, null); 227 return keyboard; 228 } 229 230 private KeyboardId getKeyboardId(int mode, int imeOptions, boolean isSymbols) { 231 final boolean hasVoiceKey = hasVoiceKey(isSymbols); 232 final int charColorId = getColorScheme(); 233 final int xmlId; 234 final boolean enableShiftLock; 235 236 if (isSymbols) { 237 if (mode == KeyboardId.MODE_PHONE) { 238 xmlId = R.xml.kbd_phone_symbols; 239 } else if (mode == KeyboardId.MODE_NUMBER) { 240 // Note: MODE_NUMBER keyboard layout has no "switch alpha symbol" key. 241 xmlId = R.xml.kbd_number; 242 } else { 243 xmlId = R.xml.kbd_symbols; 244 } 245 enableShiftLock = false; 246 } else { 247 if (mode == KeyboardId.MODE_PHONE) { 248 xmlId = R.xml.kbd_phone; 249 enableShiftLock = false; 250 } else if (mode == KeyboardId.MODE_NUMBER) { 251 xmlId = R.xml.kbd_number; 252 enableShiftLock = false; 253 } else { 254 xmlId = R.xml.kbd_qwerty; 255 enableShiftLock = true; 256 } 257 } 258 final Resources res = mInputMethodService.getResources(); 259 final int orientation = res.getConfiguration().orientation; 260 final Locale locale = mSubtypeSwitcher.getInputLocale(); 261 return new KeyboardId( 262 res.getResourceEntryName(xmlId), xmlId, locale, orientation, mode, charColorId, 263 mHasSettingsKey, mVoiceKeyEnabled, hasVoiceKey, imeOptions, enableShiftLock); 264 } 265 266 public int getKeyboardMode() { 267 return mMode; 268 } 269 270 public boolean isAlphabetMode() { 271 return mCurrentId != null && mCurrentId.isAlphabetKeyboard(); 272 } 273 274 public boolean isInputViewShown() { 275 return mInputView != null && mInputView.isShown(); 276 } 277 278 public boolean isKeyboardAvailable() { 279 if (mInputView != null) 280 return mInputView.getLatinKeyboard() != null; 281 return false; 282 } 283 284 private LatinKeyboard getLatinKeyboard() { 285 if (mInputView != null) 286 return mInputView.getLatinKeyboard(); 287 return null; 288 } 289 290 public void setPreferredLetters(int[] frequencies) { 291 LatinKeyboard latinKeyboard = getLatinKeyboard(); 292 if (latinKeyboard != null) 293 latinKeyboard.setPreferredLetters(frequencies); 294 } 295 296 public void keyReleased() { 297 LatinKeyboard latinKeyboard = getLatinKeyboard(); 298 if (latinKeyboard != null) 299 latinKeyboard.keyReleased(); 300 } 301 302 public boolean isShiftedOrShiftLocked() { 303 LatinKeyboard latinKeyboard = getLatinKeyboard(); 304 if (latinKeyboard != null) 305 return latinKeyboard.isShiftedOrShiftLocked(); 306 return false; 307 } 308 309 public boolean isShiftLocked() { 310 LatinKeyboard latinKeyboard = getLatinKeyboard(); 311 if (latinKeyboard != null) 312 return latinKeyboard.isShiftLocked(); 313 return false; 314 } 315 316 public boolean isAutomaticTemporaryUpperCase() { 317 LatinKeyboard latinKeyboard = getLatinKeyboard(); 318 if (latinKeyboard != null) 319 return latinKeyboard.isAutomaticTemporaryUpperCase(); 320 return false; 321 } 322 323 public boolean isManualTemporaryUpperCase() { 324 LatinKeyboard latinKeyboard = getLatinKeyboard(); 325 if (latinKeyboard != null) 326 return latinKeyboard.isManualTemporaryUpperCase(); 327 return false; 328 } 329 330 private boolean isManualTemporaryUpperCaseFromAuto() { 331 LatinKeyboard latinKeyboard = getLatinKeyboard(); 332 if (latinKeyboard != null) 333 return latinKeyboard.isManualTemporaryUpperCaseFromAuto(); 334 return false; 335 } 336 337 private void setManualTemporaryUpperCase(boolean shifted) { 338 LatinKeyboard latinKeyboard = getLatinKeyboard(); 339 if (latinKeyboard != null) { 340 // On non-distinct multi touch panel device, we should also turn off the shift locked 341 // state when shift key is pressed to go to normal mode. 342 // On the other hand, on distinct multi touch panel device, turning off the shift locked 343 // state with shift key pressing is handled by onReleaseShift(). 344 if (!hasDistinctMultitouch() && !shifted && latinKeyboard.isShiftLocked()) { 345 latinKeyboard.setShiftLocked(false); 346 } 347 if (latinKeyboard.setShifted(shifted)) { 348 mInputView.invalidateAllKeys(); 349 } 350 } 351 } 352 353 private void setShiftLocked(boolean shiftLocked) { 354 LatinKeyboard latinKeyboard = getLatinKeyboard(); 355 if (latinKeyboard != null && latinKeyboard.setShiftLocked(shiftLocked)) { 356 mInputView.invalidateAllKeys(); 357 } 358 } 359 360 /** 361 * Toggle keyboard shift state triggered by user touch event. 362 */ 363 public void toggleShift() { 364 mInputMethodService.mHandler.cancelUpdateShiftState(); 365 if (DEBUG_STATE) 366 Log.d(TAG, "toggleShift:" 367 + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() 368 + " shiftKeyState=" + mShiftKeyState); 369 if (isAlphabetMode()) { 370 setManualTemporaryUpperCase(!isShiftedOrShiftLocked()); 371 } else { 372 toggleShiftInSymbol(); 373 } 374 } 375 376 public void toggleCapsLock() { 377 mInputMethodService.mHandler.cancelUpdateShiftState(); 378 if (DEBUG_STATE) 379 Log.d(TAG, "toggleCapsLock:" 380 + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() 381 + " shiftKeyState=" + mShiftKeyState); 382 if (isAlphabetMode()) { 383 if (isShiftLocked()) { 384 // Shift key is long pressed while caps lock state, we will toggle back to normal 385 // state. And mark as if shift key is released. 386 setShiftLocked(false); 387 mShiftKeyState.onRelease(); 388 } else { 389 setShiftLocked(true); 390 } 391 } 392 } 393 394 private void setAutomaticTemporaryUpperCase() { 395 LatinKeyboard latinKeyboard = getLatinKeyboard(); 396 if (latinKeyboard != null) { 397 latinKeyboard.setAutomaticTemporaryUpperCase(); 398 mInputView.invalidateAllKeys(); 399 } 400 } 401 402 /** 403 * Update keyboard shift state triggered by connected EditText status change. 404 */ 405 public void updateShiftState() { 406 final ShiftKeyState shiftKeyState = mShiftKeyState; 407 if (DEBUG_STATE) 408 Log.d(TAG, "updateShiftState:" 409 + " autoCaps=" + mInputMethodService.getCurrentAutoCapsState() 410 + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() 411 + " shiftKeyState=" + shiftKeyState); 412 if (isAlphabetMode()) { 413 if (!isShiftLocked() && !shiftKeyState.isIgnoring()) { 414 if (shiftKeyState.isReleasing() && mInputMethodService.getCurrentAutoCapsState()) { 415 // Only when shift key is releasing, automatic temporary upper case will be set. 416 setAutomaticTemporaryUpperCase(); 417 } else { 418 setManualTemporaryUpperCase(shiftKeyState.isMomentary()); 419 } 420 } 421 } else { 422 // In symbol keyboard mode, we should clear shift key state because only alphabet 423 // keyboard has shift key. 424 shiftKeyState.onRelease(); 425 } 426 } 427 428 public void changeKeyboardMode() { 429 if (DEBUG_STATE) 430 Log.d(TAG, "changeKeyboardMode:" 431 + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() 432 + " shiftKeyState=" + mShiftKeyState); 433 toggleKeyboardMode(); 434 if (isShiftLocked() && isAlphabetMode()) 435 setShiftLocked(true); 436 updateShiftState(); 437 } 438 439 public void onPressShift() { 440 if (!isKeyboardAvailable()) 441 return; 442 ShiftKeyState shiftKeyState = mShiftKeyState; 443 if (DEBUG_STATE) 444 Log.d(TAG, "onPressShift:" 445 + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() 446 + " shiftKeyState=" + shiftKeyState); 447 if (isAlphabetMode()) { 448 if (isShiftLocked()) { 449 // Shift key is pressed while caps lock state, we will treat this state as shifted 450 // caps lock state and mark as if shift key pressed while normal state. 451 shiftKeyState.onPress(); 452 setManualTemporaryUpperCase(true); 453 } else if (isAutomaticTemporaryUpperCase()) { 454 // Shift key is pressed while automatic temporary upper case, we have to move to 455 // manual temporary upper case. 456 shiftKeyState.onPress(); 457 setManualTemporaryUpperCase(true); 458 } else if (isShiftedOrShiftLocked()) { 459 // In manual upper case state, we just record shift key has been pressing while 460 // shifted state. 461 shiftKeyState.onPressOnShifted(); 462 } else { 463 // In base layout, chording or manual temporary upper case mode is started. 464 shiftKeyState.onPress(); 465 toggleShift(); 466 } 467 } else { 468 // In symbol mode, just toggle symbol and symbol more keyboard. 469 shiftKeyState.onPress(); 470 toggleShift(); 471 } 472 } 473 474 public void onReleaseShift() { 475 if (!isKeyboardAvailable()) 476 return; 477 ShiftKeyState shiftKeyState = mShiftKeyState; 478 if (DEBUG_STATE) 479 Log.d(TAG, "onReleaseShift:" 480 + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() 481 + " shiftKeyState=" + shiftKeyState); 482 if (isAlphabetMode()) { 483 if (shiftKeyState.isMomentary()) { 484 // After chording input while normal state. 485 toggleShift(); 486 } else if (isShiftLocked() && !shiftKeyState.isIgnoring()) { 487 // Shift has been pressed without chording while caps lock state. 488 toggleCapsLock(); 489 } else if (isShiftedOrShiftLocked() && shiftKeyState.isPressingOnShifted()) { 490 // Shift has been pressed without chording while shifted state. 491 toggleShift(); 492 } else if (isManualTemporaryUpperCaseFromAuto() && shiftKeyState.isPressing()) { 493 // Shift has been pressed without chording while manual temporary upper case 494 // transited from automatic temporary upper case. 495 toggleShift(); 496 } 497 } 498 shiftKeyState.onRelease(); 499 } 500 501 public void onPressSymbol() { 502 if (DEBUG_STATE) 503 Log.d(TAG, "onPressSymbol:" 504 + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() 505 + " symbolKeyState=" + mSymbolKeyState); 506 changeKeyboardMode(); 507 mSymbolKeyState.onPress(); 508 mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_MOMENTARY; 509 } 510 511 public void onReleaseSymbol() { 512 if (DEBUG_STATE) 513 Log.d(TAG, "onReleaseSymbol:" 514 + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() 515 + " symbolKeyState=" + mSymbolKeyState); 516 // Snap back to the previous keyboard mode if the user chords the mode change key and 517 // other key, then released the mode change key. 518 if (mAutoModeSwitchState == AUTO_MODE_SWITCH_STATE_CHORDING) 519 changeKeyboardMode(); 520 mSymbolKeyState.onRelease(); 521 } 522 523 public void onOtherKeyPressed() { 524 if (DEBUG_STATE) 525 Log.d(TAG, "onOtherKeyPressed:" 526 + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() 527 + " shiftKeyState=" + mShiftKeyState 528 + " symbolKeyState=" + mSymbolKeyState); 529 mShiftKeyState.onOtherKeyPressed(); 530 mSymbolKeyState.onOtherKeyPressed(); 531 } 532 533 public void onCancelInput() { 534 // Snap back to the previous keyboard mode if the user cancels sliding input. 535 if (mAutoModeSwitchState == AUTO_MODE_SWITCH_STATE_MOMENTARY && getPointerCount() == 1) 536 changeKeyboardMode(); 537 } 538 539 private void toggleShiftInSymbol() { 540 if (isAlphabetMode()) 541 return; 542 final LatinKeyboard keyboard; 543 if (mCurrentId.equals(mSymbolsId) || !mCurrentId.equals(mSymbolsShiftedId)) { 544 mCurrentId = mSymbolsShiftedId; 545 keyboard = getKeyboard(mCurrentId); 546 // Symbol shifted keyboard has an ALT key that has a caps lock style indicator. To 547 // enable the indicator, we need to call enableShiftLock() and setShiftLocked(true). 548 // Thus we can keep the ALT key's Key.on value true while LatinKey.onRelease() is 549 // called. 550 keyboard.setShiftLocked(true); 551 } else { 552 mCurrentId = mSymbolsId; 553 keyboard = getKeyboard(mCurrentId); 554 // Symbol keyboard has an ALT key that has a caps lock style indicator. To disable the 555 // indicator, we need to call enableShiftLock() and setShiftLocked(false). 556 keyboard.setShifted(false); 557 } 558 mInputView.setKeyboard(keyboard); 559 } 560 561 public boolean isInMomentaryAutoModeSwitchState() { 562 return mAutoModeSwitchState == AUTO_MODE_SWITCH_STATE_MOMENTARY; 563 } 564 565 public boolean isVibrateAndSoundFeedbackRequired() { 566 return mInputView == null || !mInputView.isInSlidingKeyInput(); 567 } 568 569 private int getPointerCount() { 570 return mInputView == null ? 0 : mInputView.getPointerCount(); 571 } 572 573 private void toggleKeyboardMode() { 574 loadKeyboardInternal(mMode, mImeOptions, mVoiceKeyEnabled, mVoiceButtonOnPrimary, 575 !mIsSymbols); 576 if (mIsSymbols) { 577 mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_SYMBOL_BEGIN; 578 } else { 579 mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_ALPHA; 580 } 581 } 582 583 public boolean hasDistinctMultitouch() { 584 return mInputView != null && mInputView.hasDistinctMultitouch(); 585 } 586 587 /** 588 * Updates state machine to figure out when to automatically snap back to the previous mode. 589 */ 590 public void onKey(int key) { 591 if (DEBUG_STATE) 592 Log.d(TAG, "onKey: code=" + key + " autoModeSwitchState=" + mAutoModeSwitchState 593 + " pointers=" + getPointerCount()); 594 switch (mAutoModeSwitchState) { 595 case AUTO_MODE_SWITCH_STATE_MOMENTARY: 596 // Only distinct multi touch devices can be in this state. 597 // On non-distinct multi touch devices, mode change key is handled by 598 // {@link LatinIME#onCodeInput}, not by {@link LatinIME#onPress} and 599 // {@link LatinIME#onRelease}. So, on such devices, {@link #mAutoModeSwitchState} starts 600 // from {@link #AUTO_MODE_SWITCH_STATE_SYMBOL_BEGIN}, or 601 // {@link #AUTO_MODE_SWITCH_STATE_ALPHA}, not from 602 // {@link #AUTO_MODE_SWITCH_STATE_MOMENTARY}. 603 if (key == Keyboard.CODE_SWITCH_ALPHA_SYMBOL) { 604 // Detected only the mode change key has been pressed, and then released. 605 if (mIsSymbols) { 606 mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_SYMBOL_BEGIN; 607 } else { 608 mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_ALPHA; 609 } 610 } else if (getPointerCount() == 1) { 611 // Snap back to the previous keyboard mode if the user pressed the mode change key 612 // and slid to other key, then released the finger. 613 // If the user cancels the sliding input, snapping back to the previous keyboard 614 // mode is handled by {@link #onCancelInput}. 615 changeKeyboardMode(); 616 } else { 617 // Chording input is being started. The keyboard mode will be snapped back to the 618 // previous mode in {@link onReleaseSymbol} when the mode change key is released. 619 mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_CHORDING; 620 } 621 break; 622 case AUTO_MODE_SWITCH_STATE_SYMBOL_BEGIN: 623 if (key != Keyboard.CODE_SPACE && key != Keyboard.CODE_ENTER && key >= 0) { 624 mAutoModeSwitchState = AUTO_MODE_SWITCH_STATE_SYMBOL; 625 } 626 break; 627 case AUTO_MODE_SWITCH_STATE_SYMBOL: 628 // Snap back to alpha keyboard mode if user types one or more non-space/enter 629 // characters followed by a space/enter. 630 if (key == Keyboard.CODE_ENTER || key == Keyboard.CODE_SPACE) { 631 changeKeyboardMode(); 632 } 633 break; 634 } 635 } 636 637 public LatinKeyboardView getInputView() { 638 return mInputView; 639 } 640 641 public LatinKeyboardView onCreateInputView() { 642 createInputViewInternal(mLayoutId, true); 643 return mInputView; 644 } 645 646 private void createInputViewInternal(int newLayout, boolean forceReset) { 647 int layoutId = newLayout; 648 if (mLayoutId != layoutId || mInputView == null || forceReset) { 649 if (mInputView != null) { 650 mInputView.closing(); 651 } 652 if (KEYBOARD_THEMES.length <= layoutId) { 653 layoutId = Integer.valueOf(sConfigDefaultKeyboardThemeId); 654 } 655 656 Utils.GCUtils.getInstance().reset(); 657 boolean tryGC = true; 658 for (int i = 0; i < Utils.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) { 659 try { 660 mInputView = (LatinKeyboardView) mInputMethodService.getLayoutInflater( 661 ).inflate(KEYBOARD_THEMES[layoutId], null); 662 tryGC = false; 663 } catch (OutOfMemoryError e) { 664 Log.w(TAG, "load keyboard failed: " + e); 665 tryGC = Utils.GCUtils.getInstance().tryGCOrWait( 666 mLayoutId + "," + layoutId, e); 667 } catch (InflateException e) { 668 Log.w(TAG, "load keyboard failed: " + e); 669 tryGC = Utils.GCUtils.getInstance().tryGCOrWait( 670 mLayoutId + "," + layoutId, e); 671 } 672 } 673 mInputView.setOnKeyboardActionListener(mInputMethodService); 674 mLayoutId = layoutId; 675 } 676 } 677 678 private void postSetInputView() { 679 mInputMethodService.mHandler.post(new Runnable() { 680 @Override 681 public void run() { 682 if (mInputView != null) { 683 mInputMethodService.setInputView(mInputView); 684 } 685 mInputMethodService.updateInputViewShown(); 686 } 687 }); 688 } 689 690 @Override 691 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 692 if (PREF_KEYBOARD_LAYOUT.equals(key)) { 693 final int layoutId = Integer.valueOf( 694 sharedPreferences.getString(key, sConfigDefaultKeyboardThemeId)); 695 createInputViewInternal(layoutId, false); 696 postSetInputView(); 697 } else if (Settings.PREF_SETTINGS_KEY.equals(key)) { 698 mHasSettingsKey = getSettingsKeyMode(sharedPreferences, mInputMethodService); 699 createInputViewInternal(mLayoutId, true); 700 postSetInputView(); 701 } 702 } 703 704 private int getColorScheme() { 705 return (mInputView != null) 706 ? mInputView.getColorScheme() : KeyboardView.COLOR_SCHEME_WHITE; 707 } 708 709 public void onAutoCorrectionStateChanged(boolean isAutoCorrection) { 710 if (isAutoCorrection != mIsAutoCorrectionActive) { 711 LatinKeyboardView keyboardView = getInputView(); 712 mIsAutoCorrectionActive = isAutoCorrection; 713 keyboardView.invalidateKey(((LatinKeyboard) keyboardView.getKeyboard()) 714 .onAutoCorrectionStateChanged(isAutoCorrection)); 715 } 716 } 717 718 private static boolean getSettingsKeyMode(SharedPreferences prefs, Context context) { 719 Resources resources = context.getResources(); 720 final boolean showSettingsKeyOption = resources.getBoolean( 721 R.bool.config_enable_show_settings_key_option); 722 if (showSettingsKeyOption) { 723 final String settingsKeyMode = prefs.getString(Settings.PREF_SETTINGS_KEY, 724 resources.getString(DEFAULT_SETTINGS_KEY_MODE)); 725 // We show the settings key when 1) SETTINGS_KEY_MODE_ALWAYS_SHOW or 726 // 2) SETTINGS_KEY_MODE_AUTO and there are two or more enabled IMEs on the system 727 if (settingsKeyMode.equals(resources.getString(SETTINGS_KEY_MODE_ALWAYS_SHOW)) 728 || (settingsKeyMode.equals(resources.getString(SETTINGS_KEY_MODE_AUTO)) 729 && Utils.hasMultipleEnabledIMEsOrSubtypes( 730 ((InputMethodManager) context.getSystemService( 731 Context.INPUT_METHOD_SERVICE))))) { 732 return true; 733 } 734 } 735 return false; 736 } 737} 738