satok | 03b2ea1 | 2011-08-03 17:36:14 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view.textservice; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.Arrays; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * This class is used to specify meta information of a subtype contained in a spell checker. |
| 30 | * Subtype can describe locale (e.g. en_US, fr_FR...) used for settings. |
| 31 | */ |
| 32 | public final class SpellCheckerSubtype implements Parcelable { |
| 33 | |
| 34 | private final int mSubtypeHashCode; |
| 35 | private final int mSubtypeNameResId; |
| 36 | private final String mSubtypeLocale; |
| 37 | private final String mSubtypeExtraValue; |
| 38 | |
| 39 | /** |
| 40 | * Constructor |
| 41 | * @param nameId The name of the subtype |
| 42 | * @param locale The locale supported by the subtype |
| 43 | * @param extraValue The extra value of the subtype |
| 44 | */ |
| 45 | public SpellCheckerSubtype(int nameId, String locale, String extraValue) { |
| 46 | mSubtypeNameResId = nameId; |
| 47 | mSubtypeLocale = locale != null ? locale : ""; |
| 48 | mSubtypeExtraValue = extraValue != null ? extraValue : ""; |
| 49 | mSubtypeHashCode = hashCodeInternal(mSubtypeLocale, mSubtypeExtraValue); |
| 50 | } |
| 51 | |
| 52 | SpellCheckerSubtype(Parcel source) { |
| 53 | String s; |
| 54 | mSubtypeNameResId = source.readInt(); |
| 55 | s = source.readString(); |
| 56 | mSubtypeLocale = s != null ? s : ""; |
| 57 | s = source.readString(); |
| 58 | mSubtypeExtraValue = s != null ? s : ""; |
| 59 | mSubtypeHashCode = hashCodeInternal(mSubtypeLocale, mSubtypeExtraValue); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @return the name of the subtype |
| 64 | */ |
| 65 | public int getNameResId() { |
| 66 | return mSubtypeNameResId; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return the locale of the subtype |
| 71 | */ |
| 72 | public String getLocale() { |
| 73 | return mSubtypeLocale; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @return the extra value of the subtype |
| 78 | */ |
| 79 | public String getExtraValue() { |
| 80 | return mSubtypeExtraValue; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public int hashCode() { |
| 85 | return mSubtypeHashCode; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean equals(Object o) { |
| 90 | if (o instanceof SpellCheckerSubtype) { |
| 91 | SpellCheckerSubtype subtype = (SpellCheckerSubtype) o; |
| 92 | return (subtype.hashCode() == hashCode()) |
| 93 | && (subtype.getNameResId() == getNameResId()) |
| 94 | && (subtype.getLocale().equals(getLocale())) |
| 95 | && (subtype.getExtraValue().equals(getExtraValue())); |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public int describeContents() { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void writeToParcel(Parcel dest, int parcelableFlags) { |
| 107 | dest.writeInt(mSubtypeNameResId); |
| 108 | dest.writeString(mSubtypeLocale); |
| 109 | dest.writeString(mSubtypeExtraValue); |
| 110 | } |
| 111 | |
| 112 | public static final Parcelable.Creator<SpellCheckerSubtype> CREATOR |
| 113 | = new Parcelable.Creator<SpellCheckerSubtype>() { |
| 114 | @Override |
| 115 | public SpellCheckerSubtype createFromParcel(Parcel source) { |
| 116 | return new SpellCheckerSubtype(source); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public SpellCheckerSubtype[] newArray(int size) { |
| 121 | return new SpellCheckerSubtype[size]; |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | private static int hashCodeInternal(String locale, String extraValue) { |
| 126 | return Arrays.hashCode(new Object[] {locale, extraValue}); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Sort the list of subtypes |
| 131 | * @param context Context will be used for getting localized strings |
| 132 | * @param flags Flags for the sort order |
| 133 | * @param sci SpellCheckerInfo of which subtypes are subject to be sorted |
| 134 | * @param subtypeList List which will be sorted |
| 135 | * @return Sorted list of subtypes |
| 136 | * @hide |
| 137 | */ |
| 138 | public static List<SpellCheckerSubtype> sort(Context context, int flags, SpellCheckerInfo sci, |
| 139 | List<SpellCheckerSubtype> subtypeList) { |
| 140 | if (sci == null) return subtypeList; |
| 141 | final HashSet<SpellCheckerSubtype> subtypesSet = new HashSet<SpellCheckerSubtype>( |
| 142 | subtypeList); |
| 143 | final ArrayList<SpellCheckerSubtype> sortedList = new ArrayList<SpellCheckerSubtype>(); |
| 144 | int N = sci.getSubtypeCount(); |
| 145 | for (int i = 0; i < N; ++i) { |
| 146 | SpellCheckerSubtype subtype = sci.getSubtypeAt(i); |
| 147 | if (subtypesSet.contains(subtype)) { |
| 148 | sortedList.add(subtype); |
| 149 | subtypesSet.remove(subtype); |
| 150 | } |
| 151 | } |
| 152 | // If subtypes in subtypesSet remain, that means these subtypes are not |
| 153 | // contained in sci, so the remaining subtypes will be appended. |
| 154 | for (SpellCheckerSubtype subtype: subtypesSet) { |
| 155 | sortedList.add(subtype); |
| 156 | } |
| 157 | return sortedList; |
| 158 | } |
| 159 | } |