1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.inputmethod.latin; 18 19 import android.content.res.Configuration; 20 import android.content.res.Resources; 21 import android.text.TextUtils; 22 23 import java.util.HashMap; 24 import java.util.Locale; 25 26 /** 27 * A class to help with handling Locales in string form. 28 * 29 * This file has the same meaning and features (and shares all of its code) with 30 * the one in the dictionary pack. They need to be kept synchronized; for any 31 * update/bugfix to this file, consider also updating/fixing the version in the 32 * dictionary pack. 33 */ 34 public class LocaleUtils { LocaleUtils()35 private LocaleUtils() { 36 // Intentional empty constructor for utility class. 37 } 38 39 // Locale match level constants. 40 // A higher level of match is guaranteed to have a higher numerical value. 41 // Some room is left within constants to add match cases that may arise necessary 42 // in the future, for example differentiating between the case where the countries 43 // are both present and different, and the case where one of the locales does not 44 // specify the countries. This difference is not needed now. 45 46 // Nothing matches. 47 public static final int LOCALE_NO_MATCH = 0; 48 // The languages matches, but the country are different. Or, the reference locale requires a 49 // country and the tested locale does not have one. 50 public static final int LOCALE_LANGUAGE_MATCH_COUNTRY_DIFFER = 3; 51 // The languages and country match, but the variants are different. Or, the reference locale 52 // requires a variant and the tested locale does not have one. 53 public static final int LOCALE_LANGUAGE_AND_COUNTRY_MATCH_VARIANT_DIFFER = 6; 54 // The required locale is null or empty so it will accept anything, and the tested locale 55 // is non-null and non-empty. 56 public static final int LOCALE_ANY_MATCH = 10; 57 // The language matches, and the tested locale specifies a country but the reference locale 58 // does not require one. 59 public static final int LOCALE_LANGUAGE_MATCH = 15; 60 // The language and the country match, and the tested locale specifies a variant but the 61 // reference locale does not require one. 62 public static final int LOCALE_LANGUAGE_AND_COUNTRY_MATCH = 20; 63 // The compared locales are fully identical. This is the best match level. 64 public static final int LOCALE_FULL_MATCH = 30; 65 66 // The level at which a match is "normally" considered a locale match with standard algorithms. 67 // Don't use this directly, use #isMatch to test. 68 private static final int LOCALE_MATCH = LOCALE_ANY_MATCH; 69 70 // Make this match the maximum match level. If this evolves to have more than 2 digits 71 // when written in base 10, also adjust the getMatchLevelSortedString method. 72 private static final int MATCH_LEVEL_MAX = 30; 73 74 /** 75 * Return how well a tested locale matches a reference locale. 76 * 77 * This will check the tested locale against the reference locale and return a measure of how 78 * a well it matches the reference. The general idea is that the tested locale has to match 79 * every specified part of the required locale. A full match occur when they are equal, a 80 * partial match when the tested locale agrees with the reference locale but is more specific, 81 * and a difference when the tested locale does not comply with all requirements from the 82 * reference locale. 83 * In more detail, if the reference locale specifies at least a language and the testedLocale 84 * does not specify one, or specifies a different one, LOCALE_NO_MATCH is returned. If the 85 * reference locale is empty or null, it will match anything - in the form of LOCALE_FULL_MATCH 86 * if the tested locale is empty or null, and LOCALE_ANY_MATCH otherwise. If the reference and 87 * tested locale agree on the language, but not on the country, 88 * LOCALE_LANGUAGE_MATCH_COUNTRY_DIFFER is returned if the reference locale specifies a country, 89 * and LOCALE_LANGUAGE_MATCH otherwise. 90 * If they agree on both the language and the country, but not on the variant, 91 * LOCALE_LANGUAGE_AND_COUNTRY_MATCH_VARIANT_DIFFER is returned if the reference locale 92 * specifies a variant, and LOCALE_LANGUAGE_AND_COUNTRY_MATCH otherwise. If everything matches, 93 * LOCALE_FULL_MATCH is returned. 94 * Examples: 95 * en <=> en_US => LOCALE_LANGUAGE_MATCH 96 * en_US <=> en => LOCALE_LANGUAGE_MATCH_COUNTRY_DIFFER 97 * en_US_POSIX <=> en_US_Android => LOCALE_LANGUAGE_AND_COUNTRY_MATCH_VARIANT_DIFFER 98 * en_US <=> en_US_Android => LOCALE_LANGUAGE_AND_COUNTRY_MATCH 99 * sp_US <=> en_US => LOCALE_NO_MATCH 100 * de <=> de => LOCALE_FULL_MATCH 101 * en_US <=> en_US => LOCALE_FULL_MATCH 102 * "" <=> en_US => LOCALE_ANY_MATCH 103 * 104 * @param referenceLocale the reference locale to test against. 105 * @param testedLocale the locale to test. 106 * @return a constant that measures how well the tested locale matches the reference locale. 107 */ getMatchLevel(String referenceLocale, String testedLocale)108 public static int getMatchLevel(String referenceLocale, String testedLocale) { 109 if (TextUtils.isEmpty(referenceLocale)) { 110 return TextUtils.isEmpty(testedLocale) ? LOCALE_FULL_MATCH : LOCALE_ANY_MATCH; 111 } 112 if (null == testedLocale) return LOCALE_NO_MATCH; 113 String[] referenceParams = referenceLocale.split("_", 3); 114 String[] testedParams = testedLocale.split("_", 3); 115 // By spec of String#split, [0] cannot be null and length cannot be 0. 116 if (!referenceParams[0].equals(testedParams[0])) return LOCALE_NO_MATCH; 117 switch (referenceParams.length) { 118 case 1: 119 return 1 == testedParams.length ? LOCALE_FULL_MATCH : LOCALE_LANGUAGE_MATCH; 120 case 2: 121 if (1 == testedParams.length) return LOCALE_LANGUAGE_MATCH_COUNTRY_DIFFER; 122 if (!referenceParams[1].equals(testedParams[1])) 123 return LOCALE_LANGUAGE_MATCH_COUNTRY_DIFFER; 124 if (3 == testedParams.length) return LOCALE_LANGUAGE_AND_COUNTRY_MATCH; 125 return LOCALE_FULL_MATCH; 126 case 3: 127 if (1 == testedParams.length) return LOCALE_LANGUAGE_MATCH_COUNTRY_DIFFER; 128 if (!referenceParams[1].equals(testedParams[1])) 129 return LOCALE_LANGUAGE_MATCH_COUNTRY_DIFFER; 130 if (2 == testedParams.length) return LOCALE_LANGUAGE_AND_COUNTRY_MATCH_VARIANT_DIFFER; 131 if (!referenceParams[2].equals(testedParams[2])) 132 return LOCALE_LANGUAGE_AND_COUNTRY_MATCH_VARIANT_DIFFER; 133 return LOCALE_FULL_MATCH; 134 } 135 // It should be impossible to come here 136 return LOCALE_NO_MATCH; 137 } 138 139 /** 140 * Return a string that represents this match level, with better matches first. 141 * 142 * The strings are sorted in lexicographic order: a better match will always be less than 143 * a worse match when compared together. 144 */ getMatchLevelSortedString(int matchLevel)145 public static String getMatchLevelSortedString(int matchLevel) { 146 // This works because the match levels are 0~99 (actually 0~30) 147 // Ideally this should use a number of digits equals to the 1og10 of the greater matchLevel 148 return String.format("%02d", MATCH_LEVEL_MAX - matchLevel); 149 } 150 151 /** 152 * Find out whether a match level should be considered a match. 153 * 154 * This method takes a match level as returned by the #getMatchLevel method, and returns whether 155 * it should be considered a match in the usual sense with standard Locale functions. 156 * 157 * @param level the match level, as returned by getMatchLevel. 158 * @return whether this is a match or not. 159 */ isMatch(int level)160 public static boolean isMatch(int level) { 161 return LOCALE_MATCH <= level; 162 } 163 164 static final Object sLockForRunInLocale = new Object(); 165 166 public abstract static class RunInLocale<T> { job(Resources res)167 protected abstract T job(Resources res); 168 169 /** 170 * Execute {@link #job(Resources)} method in specified system locale exclusively. 171 * 172 * @param res the resources to use. Pass current resources. 173 * @param newLocale the locale to change to 174 * @return the value returned from {@link #job(Resources)}. 175 */ runInLocale(final Resources res, final Locale newLocale)176 public T runInLocale(final Resources res, final Locale newLocale) { 177 synchronized (sLockForRunInLocale) { 178 final Configuration conf = res.getConfiguration(); 179 final Locale oldLocale = conf.locale; 180 try { 181 if (newLocale != null && !newLocale.equals(oldLocale)) { 182 conf.locale = newLocale; 183 res.updateConfiguration(conf, null); 184 } 185 return job(res); 186 } finally { 187 if (newLocale != null && !newLocale.equals(oldLocale)) { 188 conf.locale = oldLocale; 189 res.updateConfiguration(conf, null); 190 } 191 } 192 } 193 } 194 } 195 196 private static final HashMap<String, Locale> sLocaleCache = new HashMap<String, Locale>(); 197 198 /** 199 * Creates a locale from a string specification. 200 */ constructLocaleFromString(final String localeStr)201 public static Locale constructLocaleFromString(final String localeStr) { 202 if (localeStr == null) 203 return null; 204 synchronized (sLocaleCache) { 205 if (sLocaleCache.containsKey(localeStr)) 206 return sLocaleCache.get(localeStr); 207 Locale retval = null; 208 String[] localeParams = localeStr.split("_", 3); 209 if (localeParams.length == 1) { 210 retval = new Locale(localeParams[0]); 211 } else if (localeParams.length == 2) { 212 retval = new Locale(localeParams[0], localeParams[1]); 213 } else if (localeParams.length == 3) { 214 retval = new Locale(localeParams[0], localeParams[1], localeParams[2]); 215 } 216 if (retval != null) { 217 sLocaleCache.put(localeStr, retval); 218 } 219 return retval; 220 } 221 } 222 } 223