Make sample IME aware of language switching

This CL demonstrates how the new language switching functionality
should work by using the SoftKeyboard sample.

BUG: 15267645
Change-Id: I18ab25a0784979fe6028b97a22ff02bfd502d506
This commit is contained in:
Yohei Yukawa
2014-05-27 17:19:34 +09:00
parent 6f42309d3b
commit 88838a30af
9 changed files with 120 additions and 19 deletions

View File

@@ -16,14 +16,17 @@
package com.example.android.softkeyboard;
import android.app.Dialog;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.IBinder;
import android.text.InputType;
import android.text.method.MetaKeyKeyListener;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.CompletionInfo;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
@@ -114,10 +117,17 @@ public class SoftKeyboard extends InputMethodService
mInputView = (LatinKeyboardView) getLayoutInflater().inflate(
R.layout.input, null);
mInputView.setOnKeyboardActionListener(this);
mInputView.setKeyboard(mQwertyKeyboard);
setLatinKeyboard(mQwertyKeyboard);
return mInputView;
}
private void setLatinKeyboard(LatinKeyboard nextKeyboard) {
final boolean shouldSupportLanguageSwitchKey =
mInputMethodManager.shouldOfferSwitchingToNextInputMethod(getToken());
nextKeyboard.setLanguageSwitchKeyVisibility(shouldSupportLanguageSwitchKey);
mInputView.setKeyboard(nextKeyboard);
}
/**
* Called by the framework when your view for showing candidates needs to
* be generated, like {@link #onCreateInputView}.
@@ -247,7 +257,7 @@ public class SoftKeyboard extends InputMethodService
@Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
super.onStartInputView(attribute, restarting);
// Apply the selected keyboard to the input view.
mInputView.setKeyboard(mCurKeyboard);
setLatinKeyboard(mCurKeyboard);
mInputView.closing();
final InputMethodSubtype subtype = mInputMethodManager.getCurrentInputMethodSubtype();
mInputView.setSubtypeOnSpaceKey(subtype);
@@ -509,19 +519,19 @@ public class SoftKeyboard extends InputMethodService
} else if (primaryCode == Keyboard.KEYCODE_CANCEL) {
handleClose();
return;
} else if (primaryCode == LatinKeyboardView.KEYCODE_LANGUAGE_SWITCH) {
handleLanguageSwitch();
return;
} else if (primaryCode == LatinKeyboardView.KEYCODE_OPTIONS) {
// Show a menu or somethin'
} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE
&& mInputView != null) {
Keyboard current = mInputView.getKeyboard();
if (current == mSymbolsKeyboard || current == mSymbolsShiftedKeyboard) {
current = mQwertyKeyboard;
setLatinKeyboard(mQwertyKeyboard);
} else {
current = mSymbolsKeyboard;
}
mInputView.setKeyboard(current);
if (current == mSymbolsKeyboard) {
current.setShifted(false);
setLatinKeyboard(mSymbolsKeyboard);
mSymbolsKeyboard.setShifted(false);
}
} else {
handleCharacter(primaryCode, keyCodes);
@@ -597,11 +607,11 @@ public class SoftKeyboard extends InputMethodService
mInputView.setShifted(mCapsLock || !mInputView.isShifted());
} else if (currentKeyboard == mSymbolsKeyboard) {
mSymbolsKeyboard.setShifted(true);
mInputView.setKeyboard(mSymbolsShiftedKeyboard);
setLatinKeyboard(mSymbolsShiftedKeyboard);
mSymbolsShiftedKeyboard.setShifted(true);
} else if (currentKeyboard == mSymbolsShiftedKeyboard) {
mSymbolsShiftedKeyboard.setShifted(false);
mInputView.setKeyboard(mSymbolsKeyboard);
setLatinKeyboard(mSymbolsKeyboard);
mSymbolsKeyboard.setShifted(false);
}
}
@@ -629,6 +639,22 @@ public class SoftKeyboard extends InputMethodService
mInputView.closing();
}
private IBinder getToken() {
final Dialog dialog = getWindow();
if (dialog == null) {
return null;
}
final Window window = dialog.getWindow();
if (window == null) {
return null;
}
return window.getAttributes().token;
}
private void handleLanguageSwitch() {
mInputMethodManager.switchToNextInputMethod(getToken(), false /* onlyCurrentIme */);
}
private void checkToggleCapsLock() {
long now = System.currentTimeMillis();
if (mLastShiftTime + 800 > now) {