1 /*
2  * Copyright (C) 2016 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 com.android.internal.inputmethod;
18 
19 import com.android.internal.annotations.VisibleForTesting;
20 
21 import android.annotation.IntRange;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.icu.util.ULocale;
25 import android.os.LocaleList;
26 
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Locale;
32 
33 public final class LocaleUtils {
34 
35     @VisibleForTesting
36     public interface LocaleExtractor<T> {
37         @Nullable
get(@ullable T source)38         Locale get(@Nullable T source);
39     }
40 
41     /**
42      * Calculates a matching score for the single desired locale.
43      *
44      * @see LocaleUtils#calculateMatchingScore(ULocale, LocaleList, byte[])
45      *
46      * @param supported The locale supported by IME subtype.
47      * @param desired The locale preferred by user.
48      * @return A score based on the locale matching for the default subtype enabling.
49      */
50     @IntRange(from=1, to=3)
calculateMatchingSubScore(@onNull final ULocale supported, @NonNull final ULocale desired)51     private static byte calculateMatchingSubScore(@NonNull final ULocale supported,
52             @NonNull final ULocale desired) {
53         // Assuming supported/desired is fully expanded.
54         if (supported.equals(desired)) {
55             return 3;  // Exact match.
56         }
57 
58         // Skip language matching since it was already done in calculateMatchingScore.
59 
60         final String supportedScript = supported.getScript();
61         if (supportedScript.isEmpty() || !supportedScript.equals(desired.getScript())) {
62             // TODO: Need subscript matching. For example, Hanb should match with Bopo.
63             return 1;
64         }
65 
66         final String supportedCountry = supported.getCountry();
67         if (supportedCountry.isEmpty() || !supportedCountry.equals(desired.getCountry())) {
68             return 2;
69         }
70 
71         // Ignore others e.g. variants, extensions.
72         return 3;
73     }
74 
75     /**
76      * Calculates a matching score for the desired locale list.
77      *
78      * <p>The supported locale gets a matching score of 3 if all language, script and country of the
79      * supported locale matches with the desired locale.  The supported locale gets a matching
80      * score of 2 if the language and script of the supported locale matches with the desired
81      * locale. The supported locale gets a matching score of 1 if only language of the supported
82      * locale matches with the desired locale.  The supported locale gets a matching score of 0 if
83      * the language of the supported locale doesn't match with the desired locale.</p>
84      *
85      * @param supported The locale supported by IME subtyle.
86      * @param desired The locale list preferred by user. Typically system locale list.
87      * @param out The output buffer to be stored the individual score for the desired language list.
88      * The length of {@code out} must be same as the length of {@code desired} language list.
89      * @return {@code false} if supported locale doesn't match with any desired locale list.
90      * Otherwise {@code true}.
91      */
calculateMatchingScore(@onNull final ULocale supported, @NonNull final LocaleList desired, @NonNull byte[] out)92     private static boolean calculateMatchingScore(@NonNull final ULocale supported,
93             @NonNull final LocaleList desired, @NonNull byte[] out) {
94         if (desired.isEmpty()) {
95             return false;
96         }
97 
98         boolean allZeros = true;
99         final int N = desired.size();
100         for (int i = 0; i < N; ++i) {
101             final Locale locale = desired.get(i);
102 
103             if (!locale.getLanguage().equals(supported.getLanguage())) {
104                 // TODO: cache the result of addLikelySubtags if it is slow.
105                 out[i] = 0;
106             } else {
107                 out[i] = calculateMatchingSubScore(
108                         supported, ULocale.addLikelySubtags(ULocale.forLocale(locale)));
109                 if (allZeros && out[i] != 0) {
110                     allZeros = false;
111                 }
112             }
113         }
114         return !allZeros;
115     }
116 
117     private static final class ScoreEntry implements Comparable<ScoreEntry> {
118         public int mIndex = -1;
119         @NonNull public final byte[] mScore;  // matching score of the i-th system languages.
120 
ScoreEntry(@onNull byte[] score, int index)121         ScoreEntry(@NonNull byte[] score, int index) {
122             mScore = new byte[score.length];
123             set(score, index);
124         }
125 
set(@onNull byte[] score, int index)126         private void set(@NonNull byte[] score, int index) {
127             for (int i = 0; i < mScore.length; ++i) {
128                 mScore[i] = score[i];
129             }
130             mIndex = index;
131         }
132 
133         /**
134          * Update score and index if the given score is better than this.
135          */
updateIfBetter(@onNull byte[] score, int index)136         public void updateIfBetter(@NonNull byte[] score, int index) {
137             if (compare(mScore, score) == -1) {  // mScore < score
138                 set(score, index);
139             }
140         }
141 
142         /**
143          * Provides comaprison for bytes[].
144          *
145          * <p> Comparison does as follows. If the first value of {@code left} is larger than the
146          * first value of {@code right}, {@code left} is large than {@code right}.  If the first
147          * value of {@code left} is less than the first value of {@code right}, {@code left} is less
148          * than {@code right}. If the first value of {@code left} and the first value of
149          * {@code right} is equal, do the same comparison to the next value. Finally if all values
150          * in {@code left} and {@code right} are equal, {@code left} and {@code right} is equal.</p>
151          *
152          * @param left The length must be equal to {@code right}.
153          * @param right The length must be equal to {@code left}.
154          * @return 1 if {@code left} is larger than {@code right}. -1 if {@code left} is less than
155          * {@code right}. 0 if {@code left} and {@code right} is equal.
156          */
157         @IntRange(from=-1, to=1)
compare(@onNull byte[] left, @NonNull byte[] right)158         private static int compare(@NonNull byte[] left, @NonNull byte[] right) {
159             for (int i = 0; i < left.length; ++i) {
160                 if (left[i] > right[i]) {
161                     return 1;
162                 } else if (left[i] < right[i]) {
163                     return -1;
164                 }
165             }
166             return 0;
167         }
168 
169         @Override
compareTo(final ScoreEntry other)170         public int compareTo(final ScoreEntry other) {
171             return -1 * compare(mScore, other.mScore);  // Order by descending order.
172         }
173     }
174 
175     /**
176      * Filters the given items based on language preferences.
177      *
178      * <p>For each language found in {@code preferredLanguages}, this method tries to copy at most
179      * one best-match item from {@code source} to {@code dest}.  For example, if
180      * {@code "en-GB", "ja", "en-AU", "fr-CA", "en-IN"} is specified to {@code preferredLanguages},
181      * this method tries to copy at most one English locale, at most one Japanese, and at most one
182      * French locale from {@code source} to {@code dest}.  Here the best matching English locale
183      * will be searched from {@code source} based on matching score. For the score design, see
184      * {@link LocaleUtils#calculateMatchingScore(ULocale, LocaleList, byte[])}</p>
185      *
186      * @param sources Source items to be filtered.
187      * @param extractor Type converter from the source items to {@link Locale} object.
188      * @param preferredLanguages Ordered list of locales with which the input items will be
189      * filtered.
190      * @param dest Destination into which the filtered items will be added.
191      * @param <T> Type of the data items.
192      */
193     @VisibleForTesting
filterByLanguage( @onNull List<T> sources, @NonNull LocaleExtractor<T> extractor, @NonNull LocaleList preferredLanguages, @NonNull ArrayList<T> dest)194     public static <T> void filterByLanguage(
195             @NonNull List<T> sources,
196             @NonNull LocaleExtractor<T> extractor,
197             @NonNull LocaleList preferredLanguages,
198             @NonNull ArrayList<T> dest) {
199         final HashMap<String, ScoreEntry> scoreboard = new HashMap<>();
200         final byte[] score = new byte[preferredLanguages.size()];
201 
202         final int sourceSize = sources.size();
203         for (int i = 0; i < sourceSize; ++i) {
204             final Locale locale = extractor.get(sources.get(i));
205             if (locale == null ||
206                     !calculateMatchingScore(ULocale.addLikelySubtags(ULocale.forLocale(locale)),
207                             preferredLanguages, score)) {
208                 continue;
209             }
210 
211             final String lang = locale.getLanguage();
212             final ScoreEntry bestScore = scoreboard.get(lang);
213             if (bestScore == null) {
214                 scoreboard.put(lang, new ScoreEntry(score, i));
215             } else {
216                 bestScore.updateIfBetter(score, i);
217             }
218         }
219 
220         final ScoreEntry[] result = scoreboard.values().toArray(new ScoreEntry[scoreboard.size()]);
221         Arrays.sort(result);
222         for (final ScoreEntry entry : result) {
223             dest.add(sources.get(entry.mIndex));
224         }
225     }
226 }
227