| /* |
| * Copyright (C) 2011 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package android.view.textservice; |
| |
| 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 a spell checker. |
| * Subtype can describe locale (e.g. en_US, fr_FR...) used for settings. |
| */ |
| public final class SpellCheckerSubtype implements Parcelable { |
| |
| private final int mSubtypeHashCode; |
| private final int mSubtypeNameResId; |
| private final String mSubtypeLocale; |
| private final String mSubtypeExtraValue; |
| |
| /** |
| * Constructor |
| * @param nameId The name of the subtype |
| * @param locale The locale supported by the subtype |
| * @param extraValue The extra value of the subtype |
| */ |
| public SpellCheckerSubtype(int nameId, String locale, String extraValue) { |
| mSubtypeNameResId = nameId; |
| mSubtypeLocale = locale != null ? locale : ""; |
| mSubtypeExtraValue = extraValue != null ? extraValue : ""; |
| mSubtypeHashCode = hashCodeInternal(mSubtypeLocale, mSubtypeExtraValue); |
| } |
| |
| SpellCheckerSubtype(Parcel source) { |
| String s; |
| mSubtypeNameResId = source.readInt(); |
| s = source.readString(); |
| mSubtypeLocale = s != null ? s : ""; |
| s = source.readString(); |
| mSubtypeExtraValue = s != null ? s : ""; |
| mSubtypeHashCode = hashCodeInternal(mSubtypeLocale, mSubtypeExtraValue); |
| } |
| |
| /** |
| * @return the name of the subtype |
| */ |
| public int getNameResId() { |
| return mSubtypeNameResId; |
| } |
| |
| /** |
| * @return the locale of the subtype |
| */ |
| public String getLocale() { |
| return mSubtypeLocale; |
| } |
| |
| /** |
| * @return the extra value of the subtype |
| */ |
| public String getExtraValue() { |
| return mSubtypeExtraValue; |
| } |
| |
| @Override |
| public int hashCode() { |
| return mSubtypeHashCode; |
| } |
| |
| @Override |
| public boolean equals(Object o) { |
| if (o instanceof SpellCheckerSubtype) { |
| SpellCheckerSubtype subtype = (SpellCheckerSubtype) o; |
| return (subtype.hashCode() == hashCode()) |
| && (subtype.getNameResId() == getNameResId()) |
| && (subtype.getLocale().equals(getLocale())) |
| && (subtype.getExtraValue().equals(getExtraValue())); |
| } |
| return false; |
| } |
| |
| @Override |
| public int describeContents() { |
| return 0; |
| } |
| |
| @Override |
| public void writeToParcel(Parcel dest, int parcelableFlags) { |
| dest.writeInt(mSubtypeNameResId); |
| dest.writeString(mSubtypeLocale); |
| dest.writeString(mSubtypeExtraValue); |
| } |
| |
| public static final Parcelable.Creator<SpellCheckerSubtype> CREATOR |
| = new Parcelable.Creator<SpellCheckerSubtype>() { |
| @Override |
| public SpellCheckerSubtype createFromParcel(Parcel source) { |
| return new SpellCheckerSubtype(source); |
| } |
| |
| @Override |
| public SpellCheckerSubtype[] newArray(int size) { |
| return new SpellCheckerSubtype[size]; |
| } |
| }; |
| |
| private static int hashCodeInternal(String locale, String extraValue) { |
| return Arrays.hashCode(new Object[] {locale, extraValue}); |
| } |
| |
| /** |
| * Sort the list of subtypes |
| * @param context Context will be used for getting localized strings |
| * @param flags Flags for the sort order |
| * @param sci SpellCheckerInfo of which subtypes are subject to be sorted |
| * @param subtypeList List which will be sorted |
| * @return Sorted list of subtypes |
| * @hide |
| */ |
| public static List<SpellCheckerSubtype> sort(Context context, int flags, SpellCheckerInfo sci, |
| List<SpellCheckerSubtype> subtypeList) { |
| if (sci == null) return subtypeList; |
| final HashSet<SpellCheckerSubtype> subtypesSet = new HashSet<SpellCheckerSubtype>( |
| subtypeList); |
| final ArrayList<SpellCheckerSubtype> sortedList = new ArrayList<SpellCheckerSubtype>(); |
| int N = sci.getSubtypeCount(); |
| for (int i = 0; i < N; ++i) { |
| SpellCheckerSubtype subtype = sci.getSubtypeAt(i); |
| if (subtypesSet.contains(subtype)) { |
| sortedList.add(subtype); |
| subtypesSet.remove(subtype); |
| } |
| } |
| // If subtypes in subtypesSet remain, that means these subtypes are not |
| // contained in sci, so the remaining subtypes will be appended. |
| for (SpellCheckerSubtype subtype: subtypesSet) { |
| sortedList.add(subtype); |
| } |
| return sortedList; |
| } |
| } |