Java Code Examples for android.view.inputmethod.InputMethodManager#getCurrentInputMethodSubtype()

The following examples show how to use android.view.inputmethod.InputMethodManager#getCurrentInputMethodSubtype() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RichInputMethodManager.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: RichInputMethodManager.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: RichInputMethodManager.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: DeferredStartupHandler.java    From delion with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 5
Source File: DeferredStartupHandler.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 6
Source File: NodeVariables.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: ProcessInitializationHandler.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 8
Source File: TextServicesManagerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 9
Source File: ProcessorPhoneticLetters.java    From talkback with Apache License 2.0 4 votes vote down vote up
/** 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);
  }
}
 
Example 10
Source File: AndroidUtilities.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
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"};
}
 
Example 11
Source File: AndroidUtilities.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
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"};
}