Always on the valid system default ime
Bug: 6422390 Change-Id: I9af4065e4b9f9332f3b0db168dea5546727d5951
This commit is contained in:
@@ -22,6 +22,7 @@ import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Resources;
|
||||
import android.preference.CheckBoxPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceScreen;
|
||||
@@ -36,6 +37,7 @@ import android.view.inputmethod.InputMethodSubtype;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
public class InputMethodAndSubtypeUtil {
|
||||
@@ -46,6 +48,7 @@ public class InputMethodAndSubtypeUtil {
|
||||
private static final char INPUT_METHOD_SEPARATER = ':';
|
||||
private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
|
||||
private static final int NOT_A_SUBTYPE_ID = -1;
|
||||
private static final Locale ENGLISH_LOCALE = new Locale("en");
|
||||
|
||||
private static final TextUtils.SimpleStringSplitter sStringInputMethodSplitter
|
||||
= new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
|
||||
@@ -183,7 +186,7 @@ public class InputMethodAndSubtypeUtil {
|
||||
getEnabledInputMethodsAndSubtypeList(resolver);
|
||||
HashSet<String> disabledSystemIMEs = getDisabledSystemIMEs(resolver);
|
||||
|
||||
final boolean onlyOneIME = inputMethodInfos.size() == 1;
|
||||
final int imiCount = inputMethodInfos.size();
|
||||
boolean needsToResetSelectedSubtype = false;
|
||||
for (InputMethodInfo imi : inputMethodInfos) {
|
||||
final String imiId = imi.getId();
|
||||
@@ -195,9 +198,9 @@ public class InputMethodAndSubtypeUtil {
|
||||
((CheckBoxPreference) pref).isChecked()
|
||||
: enabledIMEAndSubtypesMap.containsKey(imiId);
|
||||
final boolean isCurrentInputMethod = imiId.equals(currentInputMethodId);
|
||||
final boolean auxIme = isAuxiliaryIme(imi);
|
||||
final boolean systemIme = isSystemIme(imi);
|
||||
if (((onlyOneIME || (systemIme && !auxIme)) && !hasHardKeyboard) || isImeChecked) {
|
||||
if ((!hasHardKeyboard && isAlwaysCheckedIme(imi, context.getActivity(), imiCount))
|
||||
|| isImeChecked) {
|
||||
if (!enabledIMEAndSubtypesMap.containsKey(imiId)) {
|
||||
// imiId has just been enabled
|
||||
enabledIMEAndSubtypesMap.put(imiId, new HashSet<String>());
|
||||
@@ -373,4 +376,47 @@ public class InputMethodAndSubtypeUtil {
|
||||
public static boolean isAuxiliaryIme(InputMethodInfo imi) {
|
||||
return imi.isAuxiliaryIme();
|
||||
}
|
||||
|
||||
public static boolean isAlwaysCheckedIme(InputMethodInfo imi, Context context, int imiCount) {
|
||||
if (imiCount <= 1) {
|
||||
return true;
|
||||
}
|
||||
if (!isSystemIme(imi)) {
|
||||
return false;
|
||||
}
|
||||
if (isAuxiliaryIme(imi)) {
|
||||
return false;
|
||||
}
|
||||
if (isValidDefaultIme(imi, context)) {
|
||||
return true;
|
||||
}
|
||||
return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
|
||||
}
|
||||
|
||||
private static boolean isValidDefaultIme(InputMethodInfo imi, Context context) {
|
||||
if (imi.getIsDefaultResourceId() != 0) {
|
||||
try {
|
||||
Resources res = context.createPackageContext(
|
||||
imi.getPackageName(), 0).getResources();
|
||||
if (res.getBoolean(imi.getIsDefaultResourceId())
|
||||
&& containsSubtypeOf(imi, context.getResources().getConfiguration().
|
||||
locale.getLanguage())) {
|
||||
return true;
|
||||
}
|
||||
} catch (PackageManager.NameNotFoundException ex) {
|
||||
} catch (Resources.NotFoundException ex) {
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
|
||||
final int N = imi.getSubtypeCount();
|
||||
for (int i = 0; i < N; ++i) {
|
||||
if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -51,7 +51,7 @@ public class InputMethodPreference extends CheckBoxPreference
|
||||
private final InputMethodInfo mImi;
|
||||
private final InputMethodManager mImm;
|
||||
private final Intent mSettingsIntent;
|
||||
private final boolean mIsSystemIme;
|
||||
private final boolean mAlwaysChecked;
|
||||
|
||||
private AlertDialog mDialog = null;
|
||||
private ImageView mInputMethodSettingsButton;
|
||||
@@ -68,7 +68,7 @@ public class InputMethodPreference extends CheckBoxPreference
|
||||
if (isChecked()) {
|
||||
setChecked(false);
|
||||
} else {
|
||||
if (mIsSystemIme) {
|
||||
if (mAlwaysChecked) {
|
||||
setChecked(true);
|
||||
} else {
|
||||
showSecurityWarnDialog(mImi, InputMethodPreference.this);
|
||||
@@ -87,9 +87,9 @@ public class InputMethodPreference extends CheckBoxPreference
|
||||
mImm = imm;
|
||||
mImi = imi;
|
||||
updateSummary();
|
||||
mIsSystemIme = InputMethodAndSubtypeUtil.isSystemIme(imi);
|
||||
final boolean isAuxIme = InputMethodAndSubtypeUtil.isAuxiliaryIme(imi);
|
||||
if (imiCount <= 1 || (mIsSystemIme && !isAuxIme)) {
|
||||
mAlwaysChecked = InputMethodAndSubtypeUtil.isAlwaysCheckedIme(
|
||||
imi, fragment.getActivity(), imiCount);
|
||||
if (mAlwaysChecked) {
|
||||
setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user