Skip to content

Commit a86f5e4

Browse files
committed
Add an option for the implicitly selected subtype
Bug: 5057886 Change-Id: Iddde4724891501b4f18cade6a3d2c64b6124e58a
1 parent a2f69c9 commit a86f5e4

File tree

6 files changed

+77
-18
lines changed

6 files changed

+77
-18
lines changed

api/current.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ package android {
709709
field public static final int overScrollFooter = 16843459; // 0x10102c3
710710
field public static final int overScrollHeader = 16843458; // 0x10102c2
711711
field public static final int overScrollMode = 16843457; // 0x10102c1
712+
field public static final int overridesImplicitlyEnabledSubtype = 16843696; // 0x10103b0
712713
field public static final int packageNames = 16843649; // 0x1010381
713714
field public static final int padding = 16842965; // 0x10100d5
714715
field public static final int paddingBottom = 16842969; // 0x10100d9
@@ -24531,6 +24532,7 @@ package android.view.inputmethod {
2453124532
}
2453224533

2453324534
public final class InputMethodSubtype implements android.os.Parcelable {
24535+
ctor public InputMethodSubtype(int, int, java.lang.String, java.lang.String, java.lang.String, boolean, boolean);
2453424536
method public boolean containsExtraValueKey(java.lang.String);
2453524537
method public int describeContents();
2453624538
method public java.lang.CharSequence getDisplayName(android.content.Context, java.lang.String, android.content.pm.ApplicationInfo);
@@ -24541,6 +24543,7 @@ package android.view.inputmethod {
2454124543
method public java.lang.String getMode();
2454224544
method public int getNameResId();
2454324545
method public boolean isAuxiliary();
24546+
method public boolean overridesImplicitlyEnabledSubtype();
2454424547
method public void writeToParcel(android.os.Parcel, int);
2454524548
field public static final android.os.Parcelable.Creator CREATOR;
2454624549
}

core/java/android/view/inputmethod/InputMethodInfo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ public InputMethodInfo(Context context, ResolveInfo service,
164164
a.getString(com.android.internal.R.styleable
165165
.InputMethod_Subtype_imeSubtypeExtraValue),
166166
a.getBoolean(com.android.internal.R.styleable
167-
.InputMethod_Subtype_isAuxiliary, false));
167+
.InputMethod_Subtype_isAuxiliary, false),
168+
a.getBoolean(com.android.internal.R.styleable
169+
.InputMethod_Subtype_overridesImplicitlyEnabledSubtype, false));
168170
mSubtypes.add(subtype);
169171
}
170172
}

core/java/android/view/inputmethod/InputMethodSubtype.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public final class InputMethodSubtype implements Parcelable {
4242
private static final String EXTRA_VALUE_KEY_VALUE_SEPARATOR = "=";
4343

4444
private final boolean mIsAuxiliary;
45+
private final boolean mOverridesImplicitlyEnabledSubtype;
4546
private final int mSubtypeHashCode;
4647
private final int mSubtypeIconResId;
4748
private final int mSubtypeNameResId;
@@ -57,11 +58,12 @@ public final class InputMethodSubtype implements Parcelable {
5758
* @param locale The locale supported by the subtype
5859
* @param mode The mode supported by the subtype
5960
* @param extraValue The extra value of the subtype
61+
* @param isAuxiliary true when this subtype is one shot subtype.
6062
* @hide
6163
*/
62-
public InputMethodSubtype(
63-
int nameId, int iconId, String locale, String mode, String extraValue) {
64-
this(nameId, iconId, locale, mode, extraValue, false);
64+
public InputMethodSubtype(int nameId, int iconId, String locale, String mode, String extraValue,
65+
boolean isAuxiliary) {
66+
this(nameId, iconId, locale, mode, extraValue, false, false);
6567
}
6668

6769
/**
@@ -72,18 +74,21 @@ public InputMethodSubtype(
7274
* @param mode The mode supported by the subtype
7375
* @param extraValue The extra value of the subtype
7476
* @param isAuxiliary true when this subtype is one shot subtype.
75-
* @hide
77+
* @param overridesImplicitlyEnabledSubtype true when this subtype should be selected by default
78+
* if no other subtypes are selected explicitly. Note that a subtype with this parameter being
79+
* true will not be shown in the subtypes list.
7680
*/
7781
public InputMethodSubtype(int nameId, int iconId, String locale, String mode, String extraValue,
78-
boolean isAuxiliary) {
82+
boolean isAuxiliary, boolean overridesImplicitlyEnabledSubtype) {
7983
mSubtypeNameResId = nameId;
8084
mSubtypeIconResId = iconId;
8185
mSubtypeLocale = locale != null ? locale : "";
8286
mSubtypeMode = mode != null ? mode : "";
8387
mSubtypeExtraValue = extraValue != null ? extraValue : "";
8488
mIsAuxiliary = isAuxiliary;
89+
mOverridesImplicitlyEnabledSubtype = overridesImplicitlyEnabledSubtype;
8590
mSubtypeHashCode = hashCodeInternal(mSubtypeLocale, mSubtypeMode, mSubtypeExtraValue,
86-
mIsAuxiliary);
91+
mIsAuxiliary, mOverridesImplicitlyEnabledSubtype);
8792
}
8893

8994
InputMethodSubtype(Parcel source) {
@@ -97,8 +102,9 @@ public InputMethodSubtype(int nameId, int iconId, String locale, String mode, St
97102
s = source.readString();
98103
mSubtypeExtraValue = s != null ? s : "";
99104
mIsAuxiliary = (source.readInt() == 1);
105+
mOverridesImplicitlyEnabledSubtype = (source.readInt() == 1);
100106
mSubtypeHashCode = hashCodeInternal(mSubtypeLocale, mSubtypeMode, mSubtypeExtraValue,
101-
mIsAuxiliary);
107+
mIsAuxiliary, mOverridesImplicitlyEnabledSubtype);
102108
}
103109

104110
/**
@@ -145,6 +151,14 @@ public boolean isAuxiliary() {
145151
return mIsAuxiliary;
146152
}
147153

154+
/**
155+
* @return true when this subtype is selected by default if no other subtypes are selected
156+
* explicitly. Note that a subtype that returns true will not be shown in the subtypes list.
157+
*/
158+
public boolean overridesImplicitlyEnabledSubtype() {
159+
return mOverridesImplicitlyEnabledSubtype;
160+
}
161+
148162
/**
149163
* @param context Context will be used for getting Locale and PackageManager.
150164
* @param packageName The package name of the IME
@@ -244,6 +258,7 @@ public void writeToParcel(Parcel dest, int parcelableFlags) {
244258
dest.writeString(mSubtypeMode);
245259
dest.writeString(mSubtypeExtraValue);
246260
dest.writeInt(mIsAuxiliary ? 1 : 0);
261+
dest.writeInt(mOverridesImplicitlyEnabledSubtype ? 1 : 0);
247262
}
248263

249264
public static final Parcelable.Creator<InputMethodSubtype> CREATOR
@@ -276,8 +291,9 @@ private static Locale constructLocaleFromString(String localeStr) {
276291
}
277292

278293
private static int hashCodeInternal(String locale, String mode, String extraValue,
279-
boolean isAuxiliary) {
280-
return Arrays.hashCode(new Object[] {locale, mode, extraValue, isAuxiliary});
294+
boolean isAuxiliary, boolean overridesImplicitlyEnabledSubtype) {
295+
return Arrays.hashCode(new Object[] {locale, mode, extraValue, isAuxiliary,
296+
overridesImplicitlyEnabledSubtype});
281297
}
282298

283299
/**

core/res/res/values/attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,10 @@
22372237
InputMethodManager#switchToLastInputMethod will ignore auxiliary subtypes when it
22382238
chooses a target subtype. -->
22392239
<attr name="isAuxiliary" format="boolean" />
2240+
<!-- Set true when this subtype should be selected by default if no other subtypes are
2241+
selected explicitly. Note that a subtype with this parameter being true will
2242+
not be shown in the subtypes list. -->
2243+
<attr name="overridesImplicitlyEnabledSubtype" format="boolean" />
22402244
<!-- The extra value of the subtype. This string can be any string and will be passed to
22412245
the IME when the framework calls the IME with the subtype. -->
22422246
<attr name="imeSubtypeExtraValue" format="string" />

core/res/res/values/public.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,4 +2010,5 @@
20102010
<public type="attr" name="targetDescriptions" />
20112011
<public type="attr" name="directionDescriptions" />
20122012

2013+
<public type="attr" name="overridesImplicitlyEnabledSubtype" />
20132014
</resources>

services/java/com/android/server/InputMethodManagerService.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMeth
655655
List<InputMethodSubtype> enabledSubtypes =
656656
mSettings.getEnabledInputMethodSubtypeListLocked(imi);
657657
if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
658-
enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
658+
enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
659659
}
660660
return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
661661
}
@@ -1903,6 +1903,20 @@ private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
19031903
return subtypes;
19041904
}
19051905

1906+
1907+
private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
1908+
InputMethodInfo imi, String mode) {
1909+
ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1910+
final int subtypeCount = imi.getSubtypeCount();
1911+
for (int i = 0; i < subtypeCount; ++i) {
1912+
final InputMethodSubtype subtype = imi.getSubtypeAt(i);
1913+
if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
1914+
subtypes.add(subtype);
1915+
}
1916+
}
1917+
return subtypes;
1918+
}
1919+
19061920
private boolean chooseNewDefaultIMELocked() {
19071921
List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
19081922
if (enabled != null && enabled.size() > 0) {
@@ -2357,14 +2371,28 @@ private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
23572371
return NOT_A_SUBTYPE_ID;
23582372
}
23592373

2360-
private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2361-
Resources res, List<InputMethodSubtype> subtypes) {
2374+
private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2375+
Resources res, InputMethodInfo imi) {
2376+
final List<InputMethodSubtype> subtypes = getSubtypes(imi);
23622377
final String systemLocale = res.getConfiguration().locale.toString();
23632378
if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
23642379
HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
23652380
new HashMap<String, InputMethodSubtype>();
23662381
final int N = subtypes.size();
23672382
boolean containsKeyboardSubtype = false;
2383+
for (int i = 0; i < N; ++i) {
2384+
// scan overriding implicitly enabled subtypes.
2385+
InputMethodSubtype subtype = subtypes.get(i);
2386+
if (subtype.overridesImplicitlyEnabledSubtype()) {
2387+
final String mode = subtype.getMode();
2388+
if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2389+
applicableModeAndSubtypesMap.put(mode, subtype);
2390+
}
2391+
}
2392+
}
2393+
if (applicableModeAndSubtypesMap.size() > 0) {
2394+
return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2395+
}
23682396
for (int i = 0; i < N; ++i) {
23692397
InputMethodSubtype subtype = subtypes.get(i);
23702398
final String locale = subtype.getLocale();
@@ -2489,16 +2517,21 @@ private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
24892517
subtype = findLastResortApplicableSubtypeLocked(
24902518
mRes, enabledSubtypes, mode, null, true);
24912519
}
2520+
final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2521+
getOverridingImplicitlyEnabledSubtypes(imi, mode);
2522+
final ArrayList<InputMethodSubtype> subtypesForSearch =
2523+
overridingImplicitlyEnabledSubtypes.isEmpty()
2524+
? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
24922525
// 4. Search by the current subtype's locale from all subtypes.
24932526
if (subtype == null && mCurrentSubtype != null) {
24942527
subtype = findLastResortApplicableSubtypeLocked(
2495-
mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
2528+
mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
24962529
}
24972530
// 5. Search by the system locale from all subtypes.
24982531
// 6. Search the first enabled subtype matched with mode from all subtypes.
24992532
if (subtype == null) {
25002533
subtype = findLastResortApplicableSubtypeLocked(
2501-
mRes, getSubtypes(imi), mode, null, true);
2534+
mRes, subtypesForSearch, mode, null, true);
25022535
}
25032536
if (subtype != null) {
25042537
if (imiId.equals(mCurMethodId)) {
@@ -2945,12 +2978,12 @@ private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair
29452978
if (explicitlyEnabledSubtypes.size() == 0) {
29462979
// If there are no explicitly enabled subtypes, applicable subtypes are
29472980
// enabled implicitly.
2948-
InputMethodInfo ime = mMethodMap.get(imeId);
2981+
InputMethodInfo imi = mMethodMap.get(imeId);
29492982
// If IME is enabled and no subtypes are enabled, applicable subtypes
29502983
// are enabled implicitly, so needs to treat them to be enabled.
2951-
if (ime != null && ime.getSubtypeCount() > 0) {
2984+
if (imi != null && imi.getSubtypeCount() > 0) {
29522985
List<InputMethodSubtype> implicitlySelectedSubtypes =
2953-
getApplicableSubtypesLocked(mRes, getSubtypes(ime));
2986+
getImplicitlyApplicableSubtypesLocked(mRes, imi);
29542987
if (implicitlySelectedSubtypes != null) {
29552988
final int N = implicitlySelectedSubtypes.size();
29562989
for (int i = 0; i < N; ++i) {

0 commit comments

Comments
 (0)