From 7265d9bd6d80c5bedaa6de2b80f6619a301a07c8 Mon Sep 17 00:00:00 2001 From: satok Date: Mon, 14 Feb 2011 15:47:30 +0900 Subject: [PATCH] Add sort method to InputMethodSubtype Bug: 3364167 Change-Id: I94ad55b8eab49dc9b233dcb4e74429863b406fb4 --- .../view/inputmethod/InputMethodSubtype.java | 35 +++++++++++++++++++ .../server/InputMethodManagerService.java | 9 +++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/core/java/android/view/inputmethod/InputMethodSubtype.java b/core/java/android/view/inputmethod/InputMethodSubtype.java index ba425a6ae7611..0a9386d8a81ef 100644 --- a/core/java/android/view/inputmethod/InputMethodSubtype.java +++ b/core/java/android/view/inputmethod/InputMethodSubtype.java @@ -16,10 +16,14 @@ package android.view.inputmethod; +import android.content.Context; import android.os.Parcel; import android.os.Parcelable; +import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; +import java.util.List; /** * This class is used to specify meta information of a subtype contained in an input method. @@ -148,4 +152,35 @@ public final class InputMethodSubtype implements Parcelable { String mode, String extraValue) { return Arrays.hashCode(new Object[] {nameResId, iconResId, locale, mode, extraValue}); } + + /** + * Sort the list of InputMethodSubtype + * @param context Context will be used for getting localized strings from IME + * @param flags Flags for the sort order + * @param imi InputMethodInfo of which subtypes are subject to be sorted + * @param subtypeList List of InputMethodSubtype which will be sorted + * @return Sorted list of subtypes + * @hide + */ + public static List sort(Context context, int flags, InputMethodInfo imi, + List subtypeList) { + if (imi == null) return subtypeList; + final HashSet inputSubtypesSet = new HashSet( + subtypeList); + final ArrayList sortedList = new ArrayList(); + int N = imi.getSubtypeCount(); + for (int i = 0; i < N; ++i) { + InputMethodSubtype subtype = imi.getSubtypeAt(i); + if (inputSubtypesSet.contains(subtype)) { + sortedList.add(subtype); + inputSubtypesSet.remove(subtype); + } + } + // If subtypes in inputSubtypesSet remain, that means these subtypes are not + // contained in imi, so the remaining subtypes will be appended. + for (InputMethodSubtype subtype: inputSubtypesSet) { + sortedList.add(subtype); + } + return sortedList; + } } diff --git a/services/java/com/android/server/InputMethodManagerService.java b/services/java/com/android/server/InputMethodManagerService.java index bc196836e4b66..d25b9c8148db4 100644 --- a/services/java/com/android/server/InputMethodManagerService.java +++ b/services/java/com/android/server/InputMethodManagerService.java @@ -596,13 +596,12 @@ public class InputMethodManagerService extends IInputMethodManager.Stub if (imi == null && mCurMethodId != null) { imi = mMethodMap.get(mCurMethodId); } - final List enabledSubtypes = + List enabledSubtypes = mSettings.getEnabledInputMethodSubtypeListLocked(imi); - if (!allowsImplicitlySelectedSubtypes || enabledSubtypes.size() > 0) { - return enabledSubtypes; - } else { - return getApplicableSubtypesLocked(mRes, getSubtypes(imi)); + if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) { + enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi)); } + return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes); } public List getEnabledInputMethodSubtypeList(InputMethodInfo imi,