下面列出了android.view.inputmethod.InputMethodManager#getCurrentInputMethodSubtype ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean switchToNextInputSubtypeInThisIme(final IBinder token,
final boolean onlyCurrentIme) {
final InputMethodManager imm = mImmWrapper.mImm;
final InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype();
final List<InputMethodSubtype> enabledSubtypes = getMyEnabledInputMethodSubtypeList(
true /* allowsImplicitlySelectedSubtypes */);
final int currentIndex = getSubtypeIndexInList(currentSubtype, enabledSubtypes);
if (currentIndex == INDEX_NOT_FOUND) {
Log.w(TAG, "Can't find current subtype in enabled subtypes: subtype="
+ SubtypeLocaleUtils.getSubtypeNameForLogging(currentSubtype));
return false;
}
final int nextIndex = (currentIndex + 1) % enabledSubtypes.size();
if (nextIndex <= currentIndex && !onlyCurrentIme) {
// The current subtype is the last or only enabled one and it needs to switch to
// next IME.
return false;
}
final InputMethodSubtype nextSubtype = enabledSubtypes.get(nextIndex);
setInputMethodAndSubtype(token, nextSubtype);
return true;
}
private boolean switchToNextInputSubtypeInThisIme(final IBinder token,
final boolean onlyCurrentIme) {
final InputMethodManager imm = mImmWrapper.mImm;
final InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype();
final List<InputMethodSubtype> enabledSubtypes = getMyEnabledInputMethodSubtypeList(
true /* allowsImplicitlySelectedSubtypes */);
final int currentIndex = getSubtypeIndexInList(currentSubtype, enabledSubtypes);
if (currentIndex == INDEX_NOT_FOUND) {
Log.w(TAG, "Can't find current subtype in enabled subtypes: subtype="
+ SubtypeLocaleUtils.getSubtypeNameForLogging(currentSubtype));
return false;
}
final int nextIndex = (currentIndex + 1) % enabledSubtypes.size();
if (nextIndex <= currentIndex && !onlyCurrentIme) {
// The current subtype is the last or only enabled one and it needs to switch to
// next IME.
return false;
}
final InputMethodSubtype nextSubtype = enabledSubtypes.get(nextIndex);
setInputMethodAndSubtype(token, nextSubtype);
return true;
}
private boolean switchToNextInputSubtypeInThisIme(final IBinder token,
final boolean onlyCurrentIme) {
final InputMethodManager imm = mImmWrapper.mImm;
final InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype();
final List<InputMethodSubtype> enabledSubtypes = getMyEnabledInputMethodSubtypeList(
true /* allowsImplicitlySelectedSubtypes */);
final int currentIndex = getSubtypeIndexInList(currentSubtype, enabledSubtypes);
if (currentIndex == INDEX_NOT_FOUND) {
Log.w(TAG, "Can't find current subtype in enabled subtypes: subtype="
+ SubtypeLocaleUtils.getSubtypeNameForLogging(currentSubtype));
return false;
}
final int nextIndex = (currentIndex + 1) % enabledSubtypes.size();
if (nextIndex <= currentIndex && !onlyCurrentIme) {
// The current subtype is the last or only enabled one and it needs to switch to
// next IME.
return false;
}
final InputMethodSubtype nextSubtype = enabledSubtypes.get(nextIndex);
setInputMethodAndSubtype(token, nextSubtype);
return true;
}
private void recordKeyboardLocaleUma() {
InputMethodManager imm =
(InputMethodManager) mAppContext.getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> ims = imm.getEnabledInputMethodList();
ArrayList<String> uniqueLanguages = new ArrayList<String>();
for (InputMethodInfo method : ims) {
List<InputMethodSubtype> submethods =
imm.getEnabledInputMethodSubtypeList(method, true);
for (InputMethodSubtype submethod : submethods) {
if (submethod.getMode().equals("keyboard")) {
String language = submethod.getLocale().split("_")[0];
if (!uniqueLanguages.contains(language)) {
uniqueLanguages.add(language);
}
}
}
}
RecordHistogram.recordCountHistogram("InputMethod.ActiveCount", uniqueLanguages.size());
InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype();
Locale systemLocale = Locale.getDefault();
if (currentSubtype != null && currentSubtype.getLocale() != null && systemLocale != null) {
String keyboardLanguage = currentSubtype.getLocale().split("_")[0];
boolean match = systemLocale.getLanguage().equalsIgnoreCase(keyboardLanguage);
RecordHistogram.recordBooleanHistogram("InputMethod.MatchesSystemLanguage", match);
}
}
private void recordKeyboardLocaleUma() {
InputMethodManager imm =
(InputMethodManager) mAppContext.getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> ims = imm.getEnabledInputMethodList();
ArrayList<String> uniqueLanguages = new ArrayList<>();
for (InputMethodInfo method : ims) {
List<InputMethodSubtype> submethods =
imm.getEnabledInputMethodSubtypeList(method, true);
for (InputMethodSubtype submethod : submethods) {
if (submethod.getMode().equals("keyboard")) {
String language = submethod.getLocale().split("_")[0];
if (!uniqueLanguages.contains(language)) {
uniqueLanguages.add(language);
}
}
}
}
RecordHistogram.recordCountHistogram("InputMethod.ActiveCount", uniqueLanguages.size());
InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype();
Locale systemLocale = Locale.getDefault();
if (currentSubtype != null && currentSubtype.getLocale() != null && systemLocale != null) {
String keyboardLanguage = currentSubtype.getLocale().split("_")[0];
boolean match = systemLocale.getLanguage().equalsIgnoreCase(keyboardLanguage);
RecordHistogram.recordBooleanHistogram("InputMethod.MatchesSystemLanguage", match);
}
}
/**
* Returns the locale of the IME. This only works for the old versions of Gboard. The new version
* wraps the content description and text in the locale of the IME and returns an empty string
* when InputMethodSubtype is queried for the locale.
*/
private static @Nullable Locale getKeyboardLocale(Context context) {
Locale locale = null;
InputMethodManager imm =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
if (ims == null) {
return locale;
}
String localeString = ims.getLocale();
locale = LocaleUtils.parseLocaleString(localeString);
return locale;
}
@SuppressWarnings("deprecation") // InputMethodSubtype.getLocale() deprecated in API 24
private void recordKeyboardLocaleUma(Context context) {
InputMethodManager imm =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> ims = imm.getEnabledInputMethodList();
ArrayList<String> uniqueLanguages = new ArrayList<>();
for (InputMethodInfo method : ims) {
List<InputMethodSubtype> submethods =
imm.getEnabledInputMethodSubtypeList(method, true);
for (InputMethodSubtype submethod : submethods) {
if (submethod.getMode().equals("keyboard")) {
String language = submethod.getLocale().split("_")[0];
if (!uniqueLanguages.contains(language)) {
uniqueLanguages.add(language);
}
}
}
}
RecordHistogram.recordCountHistogram("InputMethod.ActiveCount", uniqueLanguages.size());
InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype();
Locale systemLocale = Locale.getDefault();
if (currentSubtype != null && currentSubtype.getLocale() != null && systemLocale != null) {
String keyboardLanguage = currentSubtype.getLocale().split("_")[0];
boolean match = systemLocale.getLanguage().equalsIgnoreCase(keyboardLanguage);
RecordHistogram.recordBooleanHistogram("InputMethod.MatchesSystemLanguage", match);
}
}
@Override
public SpellCheckerSubtype getCurrentSpellCheckerSubtype(
String locale, boolean allowImplicitlySelectedSubtype) {
final int subtypeHashCode;
final SpellCheckerInfo sci;
final Locale systemLocale;
final int userId = UserHandle.getCallingUserId();
synchronized (mLock) {
final TextServicesData tsd = getDataFromCallingUserIdLocked(userId);
if (tsd == null) return null;
subtypeHashCode =
tsd.getSelectedSpellCheckerSubtype(SpellCheckerSubtype.SUBTYPE_ID_NONE);
if (DBG) {
Slog.w(TAG, "getCurrentSpellCheckerSubtype: " + subtypeHashCode);
}
sci = tsd.getCurrentSpellChecker();
systemLocale = mContext.getResources().getConfiguration().locale;
}
if (sci == null || sci.getSubtypeCount() == 0) {
if (DBG) {
Slog.w(TAG, "Subtype not found.");
}
return null;
}
if (subtypeHashCode == SpellCheckerSubtype.SUBTYPE_ID_NONE
&& !allowImplicitlySelectedSubtype) {
return null;
}
String candidateLocale = null;
if (subtypeHashCode == 0) {
// Spell checker language settings == "auto"
final InputMethodManager imm = mContext.getSystemService(InputMethodManager.class);
if (imm != null) {
final InputMethodSubtype currentInputMethodSubtype =
imm.getCurrentInputMethodSubtype();
if (currentInputMethodSubtype != null) {
final String localeString = currentInputMethodSubtype.getLocale();
if (!TextUtils.isEmpty(localeString)) {
// 1. Use keyboard locale if available in the spell checker
candidateLocale = localeString;
}
}
}
if (candidateLocale == null) {
// 2. Use System locale if available in the spell checker
candidateLocale = systemLocale.toString();
}
}
SpellCheckerSubtype candidate = null;
for (int i = 0; i < sci.getSubtypeCount(); ++i) {
final SpellCheckerSubtype scs = sci.getSubtypeAt(i);
if (subtypeHashCode == 0) {
final String scsLocale = scs.getLocale();
if (candidateLocale.equals(scsLocale)) {
return scs;
} else if (candidate == null) {
if (candidateLocale.length() >= 2 && scsLocale.length() >= 2
&& candidateLocale.startsWith(scsLocale)) {
// Fall back to the applicable language
candidate = scs;
}
}
} else if (scs.hashCode() == subtypeHashCode) {
if (DBG) {
Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale
+ ", " + scs.getLocale());
}
// 3. Use the user specified spell check language
return scs;
}
}
// 4. Fall back to the applicable language and return it if not null
// 5. Simply just return it even if it's null which means we could find no suitable
// spell check languages
return candidate;
}
/** Handle an event that indicates a key is held on the soft keyboard. */
private void processKeyboardKeyEvent(AccessibilityEvent event, EventId eventId) {
final CharSequence text = AccessibilityEventUtils.getEventTextOrDescription(event);
if (TextUtils.isEmpty(text)) {
return;
}
String localeString = null;
// For new version of Gboard, contentDescription of the node is wrapped in the locale of the
// IME.
if (text instanceof Spannable) {
Spannable spannable = (Spannable) text;
LocaleSpan[] spans = spannable.getSpans(0, text.length(), LocaleSpan.class);
for (LocaleSpan span : spans) {
// Quit the loop when a LocaleSpan is detected. We expect just one LocaleSpan.
localeString = span.getLocale().toString();
break;
}
}
// Old version of Gboard does not provide content description wrapped in the locale of the IME
// so we try using InputMethodManager.
if (localeString == null) {
InputMethodManager inputMethodManager =
(InputMethodManager) service.getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype inputMethod = inputMethodManager.getCurrentInputMethodSubtype();
if (inputMethod != null) {
String localeStringFromIme = inputMethod.getLocale();
if (!localeStringFromIme.isEmpty()) {
localeString = localeStringFromIme;
}
}
}
// Use system locale as the fallback option.
if (localeString == null) {
localeString = Locale.getDefault().toString();
}
CharSequence phoneticLetter = getPhoneticLetter(localeString, text.toString());
if (phoneticLetter != null) {
postPhoneticLetterRunnable(phoneticLetter, eventId);
}
}
public static String[] getCurrentKeyboardLanguage() {
try {
InputMethodManager inputManager = (InputMethodManager) ApplicationLoader.applicationContext.getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype inputMethodSubtype = inputManager.getCurrentInputMethodSubtype();
String locale = null;
if (inputMethodSubtype != null) {
if (Build.VERSION.SDK_INT >= 24) {
locale = inputMethodSubtype.getLanguageTag();
}
if (TextUtils.isEmpty(locale)) {
locale = inputMethodSubtype.getLocale();
}
} else {
inputMethodSubtype = inputManager.getLastInputMethodSubtype();
if (inputMethodSubtype != null) {
if (Build.VERSION.SDK_INT >= 24) {
locale = inputMethodSubtype.getLanguageTag();
}
if (TextUtils.isEmpty(locale)) {
locale = inputMethodSubtype.getLocale();
}
}
}
if (TextUtils.isEmpty(locale)) {
locale = LocaleController.getSystemLocaleStringIso639();
String locale2;
LocaleController.LocaleInfo localeInfo = LocaleController.getInstance().getCurrentLocaleInfo();
locale2 = localeInfo.getBaseLangCode();
if (TextUtils.isEmpty(locale2)) {
locale2 = localeInfo.getLangCode();
}
if (locale.contains(locale2) || locale2.contains(locale)) {
if (!locale.contains("en")) {
locale2 = "en";
} else {
locale2 = null;
}
}
if (!TextUtils.isEmpty(locale2)) {
return new String[]{locale.replace('_', '-'), locale2};
} else {
return new String[]{locale.replace('_', '-')};
}
} else {
return new String[]{locale.replace('_', '-')};
}
} catch (Exception ignore) {
}
return new String[]{"en"};
}
public static String[] getCurrentKeyboardLanguage() {
try {
InputMethodManager inputManager = (InputMethodManager) ApplicationLoader.applicationContext.getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype inputMethodSubtype = inputManager.getCurrentInputMethodSubtype();
String locale = null;
if (inputMethodSubtype != null) {
if (Build.VERSION.SDK_INT >= 24) {
locale = inputMethodSubtype.getLanguageTag();
}
if (TextUtils.isEmpty(locale)) {
locale = inputMethodSubtype.getLocale();
}
} else {
inputMethodSubtype = inputManager.getLastInputMethodSubtype();
if (inputMethodSubtype != null) {
if (Build.VERSION.SDK_INT >= 24) {
locale = inputMethodSubtype.getLanguageTag();
}
if (TextUtils.isEmpty(locale)) {
locale = inputMethodSubtype.getLocale();
}
}
}
if (TextUtils.isEmpty(locale)) {
locale = LocaleController.getSystemLocaleStringIso639();
String locale2;
LocaleController.LocaleInfo localeInfo = LocaleController.getInstance().getCurrentLocaleInfo();
locale2 = localeInfo.getBaseLangCode();
if (TextUtils.isEmpty(locale2)) {
locale2 = localeInfo.getLangCode();
}
if (locale.contains(locale2) || locale2.contains(locale)) {
if (!locale.contains("en")) {
locale2 = "en";
} else {
locale2 = null;
}
}
if (!TextUtils.isEmpty(locale2)) {
return new String[]{locale.replace('_', '-'), locale2};
} else {
return new String[]{locale.replace('_', '-')};
}
} else {
return new String[]{locale.replace('_', '-')};
}
} catch (Exception ignore) {
}
return new String[]{"en"};
}