use of android.view.inputmethod.InputMethodSubtype in project android_frameworks_base by AOSPA.
the class InputMethodUtils method getImplicitlyApplicableSubtypesLockedImpl.
private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLockedImpl(Resources res, InputMethodInfo imi) {
final List<InputMethodSubtype> subtypes = InputMethodUtils.getSubtypes(imi);
final LocaleList systemLocales = res.getConfiguration().getLocales();
final String systemLocale = systemLocales.get(0).toString();
if (TextUtils.isEmpty(systemLocale))
return new ArrayList<>();
final int numSubtypes = subtypes.size();
// Handle overridesImplicitlyEnabledSubtype mechanism.
final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap = new HashMap<>();
for (int i = 0; i < numSubtypes; ++i) {
// scan overriding implicitly enabled subtypes.
final InputMethodSubtype subtype = subtypes.get(i);
if (subtype.overridesImplicitlyEnabledSubtype()) {
final String mode = subtype.getMode();
if (!applicableModeAndSubtypesMap.containsKey(mode)) {
applicableModeAndSubtypesMap.put(mode, subtype);
}
}
}
if (applicableModeAndSubtypesMap.size() > 0) {
return new ArrayList<>(applicableModeAndSubtypesMap.values());
}
final HashMap<String, ArrayList<InputMethodSubtype>> nonKeyboardSubtypesMap = new HashMap<>();
final ArrayList<InputMethodSubtype> keyboardSubtypes = new ArrayList<>();
for (int i = 0; i < numSubtypes; ++i) {
final InputMethodSubtype subtype = subtypes.get(i);
final String mode = subtype.getMode();
if (SUBTYPE_MODE_KEYBOARD.equals(mode)) {
keyboardSubtypes.add(subtype);
} else {
if (!nonKeyboardSubtypesMap.containsKey(mode)) {
nonKeyboardSubtypesMap.put(mode, new ArrayList<>());
}
nonKeyboardSubtypesMap.get(mode).add(subtype);
}
}
final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<>();
LocaleUtils.filterByLanguage(keyboardSubtypes, sSubtypeToLocale, systemLocales, applicableSubtypes);
if (!applicableSubtypes.isEmpty()) {
boolean hasAsciiCapableKeyboard = false;
final int numApplicationSubtypes = applicableSubtypes.size();
for (int i = 0; i < numApplicationSubtypes; ++i) {
final InputMethodSubtype subtype = applicableSubtypes.get(i);
if (subtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
hasAsciiCapableKeyboard = true;
break;
}
}
if (!hasAsciiCapableKeyboard) {
final int numKeyboardSubtypes = keyboardSubtypes.size();
for (int i = 0; i < numKeyboardSubtypes; ++i) {
final InputMethodSubtype subtype = keyboardSubtypes.get(i);
final String mode = subtype.getMode();
if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
applicableSubtypes.add(subtype);
}
}
}
}
if (applicableSubtypes.isEmpty()) {
InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
if (lastResortKeyboardSubtype != null) {
applicableSubtypes.add(lastResortKeyboardSubtype);
}
}
// For each non-keyboard mode, extract subtypes with system locales.
for (final ArrayList<InputMethodSubtype> subtypeList : nonKeyboardSubtypesMap.values()) {
LocaleUtils.filterByLanguage(subtypeList, sSubtypeToLocale, systemLocales, applicableSubtypes);
}
return applicableSubtypes;
}
use of android.view.inputmethod.InputMethodSubtype in project android_frameworks_base by AOSPA.
the class InputMethodUtils method getOverridingImplicitlyEnabledSubtypes.
public static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(InputMethodInfo imi, String mode) {
ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
final int subtypeCount = imi.getSubtypeCount();
for (int i = 0; i < subtypeCount; ++i) {
final InputMethodSubtype subtype = imi.getSubtypeAt(i);
if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
subtypes.add(subtype);
}
}
return subtypes;
}
use of android.view.inputmethod.InputMethodSubtype in project android_frameworks_base by AOSPA.
the class InputMethodUtils method findLastResortApplicableSubtypeLocked.
/**
* If there are no selected subtypes, tries finding the most applicable one according to the
* given locale.
* @param subtypes this function will search the most applicable subtype in subtypes
* @param mode subtypes will be filtered by mode
* @param locale subtypes will be filtered by locale
* @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
* it will return the first subtype matched with mode
* @return the most applicable subtypeId
*/
public static InputMethodSubtype findLastResortApplicableSubtypeLocked(Resources res, List<InputMethodSubtype> subtypes, String mode, String locale, boolean canIgnoreLocaleAsLastResort) {
if (subtypes == null || subtypes.size() == 0) {
return null;
}
if (TextUtils.isEmpty(locale)) {
locale = res.getConfiguration().locale.toString();
}
final String language = getLanguageFromLocaleString(locale);
boolean partialMatchFound = false;
InputMethodSubtype applicableSubtype = null;
InputMethodSubtype firstMatchedModeSubtype = null;
final int N = subtypes.size();
for (int i = 0; i < N; ++i) {
InputMethodSubtype subtype = subtypes.get(i);
final String subtypeLocale = subtype.getLocale();
final String subtypeLanguage = getLanguageFromLocaleString(subtypeLocale);
// and all subtypes with all modes can be candidates.
if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
if (firstMatchedModeSubtype == null) {
firstMatchedModeSubtype = subtype;
}
if (locale.equals(subtypeLocale)) {
// Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
applicableSubtype = subtype;
break;
} else if (!partialMatchFound && language.equals(subtypeLanguage)) {
// Partial match (e.g. system locale is "en_US" and subtype locale is "en")
applicableSubtype = subtype;
partialMatchFound = true;
}
}
}
if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
return firstMatchedModeSubtype;
}
// subtype.
if (DEBUG) {
if (applicableSubtype != null) {
Slog.d(TAG, "Applicable InputMethodSubtype was found: " + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
}
}
return applicableSubtype;
}
use of android.view.inputmethod.InputMethodSubtype in project android_frameworks_base by AOSPA.
the class InputMethodManagerService method showInputMethodMenu.
private void showInputMethodMenu(boolean showAuxSubtypes) {
if (DEBUG)
Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
final Context context = mContext;
final boolean isScreenLocked = isScreenLocked();
final String lastInputMethodId = mSettings.getSelectedInputMethod();
int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
if (DEBUG)
Slog.v(TAG, "Current IME: " + lastInputMethodId);
synchronized (mMethodMap) {
final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis = mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(mContext);
if (immis == null || immis.size() == 0) {
return;
}
hideInputMethodMenuLocked();
final List<ImeSubtypeListItem> imList = mSwitchingController.getSortedInputMethodAndSubtypeListLocked(showAuxSubtypes, isScreenLocked);
if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
if (currentSubtype != null) {
final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
}
}
final int N = imList.size();
mIms = new InputMethodInfo[N];
mSubtypeIds = new int[N];
int checkedItem = 0;
for (int i = 0; i < N; ++i) {
final ImeSubtypeListItem item = imList.get(i);
mIms[i] = item.mImi;
mSubtypeIds[i] = item.mSubtypeId;
if (mIms[i].getId().equals(lastInputMethodId)) {
int subtypeId = mSubtypeIds[i];
if ((subtypeId == NOT_A_SUBTYPE_ID) || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0) || (subtypeId == lastInputMethodSubtypeId)) {
checkedItem = i;
}
}
}
final Context settingsContext = new ContextThemeWrapper(context, com.android.internal.R.style.Theme_DeviceDefault_Settings);
mDialogBuilder = new AlertDialog.Builder(settingsContext);
mDialogBuilder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
hideInputMethodMenu();
}
});
final Context dialogContext = mDialogBuilder.getContext();
final TypedArray a = dialogContext.obtainStyledAttributes(null, com.android.internal.R.styleable.DialogPreference, com.android.internal.R.attr.alertDialogStyle, 0);
final Drawable dialogIcon = a.getDrawable(com.android.internal.R.styleable.DialogPreference_dialogIcon);
a.recycle();
mDialogBuilder.setIcon(dialogIcon);
final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
final View tv = inflater.inflate(com.android.internal.R.layout.input_method_switch_dialog_title, null);
mDialogBuilder.setCustomTitle(tv);
// Setup layout for a toggle switch of the hardware keyboard
mSwitchingDialogTitleView = tv;
mSwitchingDialogTitleView.findViewById(com.android.internal.R.id.hard_keyboard_section).setVisibility(mWindowManagerInternal.isHardKeyboardAvailable() ? View.VISIBLE : View.GONE);
final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(com.android.internal.R.id.hard_keyboard_switch);
hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSettings.setShowImeWithHardKeyboard(isChecked);
// Ensure that the input method dialog is dismissed when changing
// the hardware keyboard state.
hideInputMethodMenu();
}
});
final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext, com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
final OnClickListener choiceListener = new OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
synchronized (mMethodMap) {
if (mIms == null || mIms.length <= which || mSubtypeIds == null || mSubtypeIds.length <= which) {
return;
}
final InputMethodInfo im = mIms[which];
int subtypeId = mSubtypeIds[which];
adapter.mCheckedItem = which;
adapter.notifyDataSetChanged();
hideInputMethodMenu();
if (im != null) {
if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
subtypeId = NOT_A_SUBTYPE_ID;
}
setInputMethodLocked(im.getId(), subtypeId);
}
}
}
};
mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
mSwitchingDialog = mDialogBuilder.create();
mSwitchingDialog.setCanceledOnTouchOutside(true);
mSwitchingDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
mSwitchingDialog.getWindow().getAttributes().privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
mSwitchingDialog.show();
}
}
use of android.view.inputmethod.InputMethodSubtype in project android_frameworks_base by AOSPA.
the class InputMethodManagerService method switchToLastInputMethod.
@Override
public boolean switchToLastInputMethod(IBinder token) {
if (!calledFromValidUser()) {
return false;
}
synchronized (mMethodMap) {
final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
final InputMethodInfo lastImi;
if (lastIme != null) {
lastImi = mMethodMap.get(lastIme.first);
} else {
lastImi = null;
}
String targetLastImiId = null;
int subtypeId = NOT_A_SUBTYPE_ID;
if (lastIme != null && lastImi != null) {
final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
final int lastSubtypeHash = Integer.parseInt(lastIme.second);
final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID : mCurrentSubtype.hashCode();
// defined, there is no need to switch to the last IME.
if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
targetLastImiId = lastIme.first;
subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
}
}
if (TextUtils.isEmpty(targetLastImiId) && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
// This is a safety net. If the currentSubtype can't be added to the history
// and the framework couldn't find the last ime, we will make the last ime be
// the most applicable enabled keyboard subtype of the system imes.
final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
if (enabled != null) {
final int N = enabled.size();
final String locale = mCurrentSubtype == null ? mRes.getConfiguration().locale.toString() : mCurrentSubtype.getLocale();
for (int i = 0; i < N; ++i) {
final InputMethodInfo imi = enabled.get(i);
if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
InputMethodSubtype keyboardSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes, InputMethodUtils.getSubtypes(imi), InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
if (keyboardSubtype != null) {
targetLastImiId = imi.getId();
subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, keyboardSubtype.hashCode());
if (keyboardSubtype.getLocale().equals(locale)) {
break;
}
}
}
}
}
}
if (!TextUtils.isEmpty(targetLastImiId)) {
if (DEBUG) {
Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second + ", from: " + mCurMethodId + ", " + subtypeId);
}
setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
return true;
} else {
return false;
}
}
}
Aggregations