Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 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.app; |
| 18 | |
| 19 | import android.content.Context; |
Igor Viarheichyk | 025402c | 2017-10-06 12:46:49 -0700 | [diff] [blame] | 20 | import android.os.LocaleList; |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 21 | import android.provider.Settings; |
| 22 | import android.telephony.TelephonyManager; |
| 23 | |
| 24 | import java.util.HashMap; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.IllformedLocaleException; |
| 27 | import java.util.Locale; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | public class LocaleStore { |
| 31 | private static final HashMap<String, LocaleInfo> sLocaleCache = new HashMap<>(); |
| 32 | private static boolean sFullyInitialized = false; |
| 33 | |
| 34 | public static class LocaleInfo { |
Mihai Nita | 86235d4 | 2016-04-01 11:50:59 -0700 | [diff] [blame] | 35 | private static final int SUGGESTION_TYPE_NONE = 0; |
| 36 | private static final int SUGGESTION_TYPE_SIM = 1 << 0; |
| 37 | private static final int SUGGESTION_TYPE_CFG = 1 << 1; |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 38 | |
| 39 | private final Locale mLocale; |
| 40 | private final Locale mParent; |
| 41 | private final String mId; |
| 42 | private boolean mIsTranslated; |
| 43 | private boolean mIsPseudo; |
| 44 | private boolean mIsChecked; // Used by the LocaleListEditor to mark entries for deletion |
| 45 | // Combination of flags for various reasons to show a locale as a suggestion. |
| 46 | // Can be SIM, location, etc. |
| 47 | private int mSuggestionFlags; |
| 48 | |
| 49 | private String mFullNameNative; |
| 50 | private String mFullCountryNameNative; |
| 51 | private String mLangScriptKey; |
| 52 | |
| 53 | private LocaleInfo(Locale locale) { |
| 54 | this.mLocale = locale; |
| 55 | this.mId = locale.toLanguageTag(); |
| 56 | this.mParent = getParent(locale); |
| 57 | this.mIsChecked = false; |
| 58 | this.mSuggestionFlags = SUGGESTION_TYPE_NONE; |
| 59 | this.mIsTranslated = false; |
| 60 | this.mIsPseudo = false; |
| 61 | } |
| 62 | |
| 63 | private LocaleInfo(String localeId) { |
| 64 | this(Locale.forLanguageTag(localeId)); |
| 65 | } |
| 66 | |
| 67 | private static Locale getParent(Locale locale) { |
| 68 | if (locale.getCountry().isEmpty()) { |
| 69 | return null; |
| 70 | } |
| 71 | return new Locale.Builder() |
Igor Viarheichyk | 025402c | 2017-10-06 12:46:49 -0700 | [diff] [blame] | 72 | .setLocale(locale) |
| 73 | .setRegion("") |
| 74 | .setExtension(Locale.UNICODE_LOCALE_EXTENSION, "") |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 75 | .build(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public String toString() { |
| 80 | return mId; |
| 81 | } |
| 82 | |
| 83 | public Locale getLocale() { |
| 84 | return mLocale; |
| 85 | } |
| 86 | |
| 87 | public Locale getParent() { |
| 88 | return mParent; |
| 89 | } |
| 90 | |
| 91 | public String getId() { |
| 92 | return mId; |
| 93 | } |
| 94 | |
| 95 | public boolean isTranslated() { |
| 96 | return mIsTranslated; |
| 97 | } |
| 98 | |
| 99 | public void setTranslated(boolean isTranslated) { |
| 100 | mIsTranslated = isTranslated; |
| 101 | } |
| 102 | |
| 103 | /* package */ boolean isSuggested() { |
| 104 | if (!mIsTranslated) { // Never suggest an untranslated locale |
| 105 | return false; |
| 106 | } |
| 107 | return mSuggestionFlags != SUGGESTION_TYPE_NONE; |
| 108 | } |
| 109 | |
| 110 | private boolean isSuggestionOfType(int suggestionMask) { |
Mihai Nita | c67b64f | 2016-02-05 14:27:55 -0800 | [diff] [blame] | 111 | if (!mIsTranslated) { // Never suggest an untranslated locale |
| 112 | return false; |
| 113 | } |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 114 | return (mSuggestionFlags & suggestionMask) == suggestionMask; |
| 115 | } |
| 116 | |
| 117 | public String getFullNameNative() { |
| 118 | if (mFullNameNative == null) { |
| 119 | mFullNameNative = |
| 120 | LocaleHelper.getDisplayName(mLocale, mLocale, true /* sentence case */); |
| 121 | } |
| 122 | return mFullNameNative; |
| 123 | } |
| 124 | |
| 125 | String getFullCountryNameNative() { |
| 126 | if (mFullCountryNameNative == null) { |
| 127 | mFullCountryNameNative = LocaleHelper.getDisplayCountry(mLocale, mLocale); |
| 128 | } |
| 129 | return mFullCountryNameNative; |
| 130 | } |
| 131 | |
Mihai Nita | 43af636 | 2016-04-19 09:09:07 -0700 | [diff] [blame] | 132 | String getFullCountryNameInUiLanguage() { |
| 133 | // We don't cache the UI name because the default locale keeps changing |
| 134 | return LocaleHelper.getDisplayCountry(mLocale); |
| 135 | } |
| 136 | |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 137 | /** Returns the name of the locale in the language of the UI. |
| 138 | * It is used for search, but never shown. |
| 139 | * For instance German will show as "Deutsch" in the list, but we will also search for |
| 140 | * "allemand" if the system UI is in French. |
| 141 | */ |
| 142 | public String getFullNameInUiLanguage() { |
Mihai Nita | 43af636 | 2016-04-19 09:09:07 -0700 | [diff] [blame] | 143 | // We don't cache the UI name because the default locale keeps changing |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 144 | return LocaleHelper.getDisplayName(mLocale, true /* sentence case */); |
| 145 | } |
| 146 | |
| 147 | private String getLangScriptKey() { |
| 148 | if (mLangScriptKey == null) { |
| 149 | Locale parentWithScript = getParent(LocaleHelper.addLikelySubtags(mLocale)); |
| 150 | mLangScriptKey = |
| 151 | (parentWithScript == null) |
| 152 | ? mLocale.toLanguageTag() |
| 153 | : parentWithScript.toLanguageTag(); |
| 154 | } |
| 155 | return mLangScriptKey; |
| 156 | } |
| 157 | |
Mihai Nita | f1f39cf | 2016-02-29 14:42:12 -0800 | [diff] [blame] | 158 | String getLabel(boolean countryMode) { |
| 159 | if (countryMode) { |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 160 | return getFullCountryNameNative(); |
Mihai Nita | f1f39cf | 2016-02-29 14:42:12 -0800 | [diff] [blame] | 161 | } else { |
| 162 | return getFullNameNative(); |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Mihai Nita | 43af636 | 2016-04-19 09:09:07 -0700 | [diff] [blame] | 166 | String getContentDescription(boolean countryMode) { |
| 167 | if (countryMode) { |
| 168 | return getFullCountryNameInUiLanguage(); |
| 169 | } else { |
| 170 | return getFullNameInUiLanguage(); |
| 171 | } |
| 172 | } |
| 173 | |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 174 | public boolean getChecked() { |
| 175 | return mIsChecked; |
| 176 | } |
| 177 | |
| 178 | public void setChecked(boolean checked) { |
| 179 | mIsChecked = checked; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private static Set<String> getSimCountries(Context context) { |
| 184 | Set<String> result = new HashSet<>(); |
| 185 | |
| 186 | TelephonyManager tm = TelephonyManager.from(context); |
| 187 | |
| 188 | if (tm != null) { |
| 189 | String iso = tm.getSimCountryIso().toUpperCase(Locale.US); |
| 190 | if (!iso.isEmpty()) { |
| 191 | result.add(iso); |
| 192 | } |
| 193 | |
| 194 | iso = tm.getNetworkCountryIso().toUpperCase(Locale.US); |
| 195 | if (!iso.isEmpty()) { |
| 196 | result.add(iso); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return result; |
| 201 | } |
| 202 | |
Mihai Nita | 137b96e | 2016-01-25 11:31:15 -0800 | [diff] [blame] | 203 | /* |
| 204 | * This method is added for SetupWizard, to force an update of the suggested locales |
| 205 | * when the SIM is initialized. |
| 206 | * |
| 207 | * <p>When the device is freshly started, it sometimes gets to the language selection |
| 208 | * before the SIM is properly initialized. |
| 209 | * So at the time the cache is filled, the info from the SIM might not be available. |
| 210 | * The SetupWizard has a SimLocaleMonitor class to detect onSubscriptionsChanged events. |
| 211 | * SetupWizard will call this function when that happens.</p> |
| 212 | * |
| 213 | * <p>TODO: decide if it is worth moving such kind of monitoring in this shared code. |
| 214 | * The user might change the SIM or might cross border and connect to a network |
| 215 | * in a different country, without restarting the Settings application or the phone.</p> |
| 216 | */ |
| 217 | public static void updateSimCountries(Context context) { |
| 218 | Set<String> simCountries = getSimCountries(context); |
| 219 | |
| 220 | for (LocaleInfo li : sLocaleCache.values()) { |
| 221 | // This method sets the suggestion flags for the (new) SIM locales, but it does not |
| 222 | // try to clean up the old flags. After all, if the user replaces a German SIM |
| 223 | // with a French one, it is still possible that they are speaking German. |
| 224 | // So both French and German are reasonable suggestions. |
| 225 | if (simCountries.contains(li.getLocale().getCountry())) { |
| 226 | li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
Mihai Nita | c67b64f | 2016-02-05 14:27:55 -0800 | [diff] [blame] | 231 | /* |
| 232 | * Show all the languages supported for a country in the suggested list. |
| 233 | * This is also handy for devices without SIM (tablets). |
| 234 | */ |
| 235 | private static void addSuggestedLocalesForRegion(Locale locale) { |
| 236 | if (locale == null) { |
| 237 | return; |
| 238 | } |
| 239 | final String country = locale.getCountry(); |
| 240 | if (country.isEmpty()) { |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | for (LocaleInfo li : sLocaleCache.values()) { |
| 245 | if (country.equals(li.getLocale().getCountry())) { |
| 246 | // We don't need to differentiate between manual and SIM suggestions |
| 247 | li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 252 | public static void fillCache(Context context) { |
| 253 | if (sFullyInitialized) { |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | Set<String> simCountries = getSimCountries(context); |
| 258 | |
Igor Viarheichyk | 025402c | 2017-10-06 12:46:49 -0700 | [diff] [blame] | 259 | final boolean isInDeveloperMode = Settings.Global.getInt(context.getContentResolver(), |
| 260 | Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0; |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 261 | for (String localeId : LocalePicker.getSupportedLocales(context)) { |
| 262 | if (localeId.isEmpty()) { |
| 263 | throw new IllformedLocaleException("Bad locale entry in locale_config.xml"); |
| 264 | } |
| 265 | LocaleInfo li = new LocaleInfo(localeId); |
Igor Viarheichyk | 025402c | 2017-10-06 12:46:49 -0700 | [diff] [blame] | 266 | |
| 267 | if (LocaleList.isPseudoLocale(li.getLocale())) { |
| 268 | if (isInDeveloperMode) { |
| 269 | li.setTranslated(true); |
| 270 | li.mIsPseudo = true; |
| 271 | li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM; |
| 272 | } else { |
| 273 | // Do not display pseudolocales unless in development mode. |
| 274 | continue; |
| 275 | } |
| 276 | } |
| 277 | |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 278 | if (simCountries.contains(li.getLocale().getCountry())) { |
| 279 | li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM; |
| 280 | } |
| 281 | sLocaleCache.put(li.getId(), li); |
| 282 | final Locale parent = li.getParent(); |
| 283 | if (parent != null) { |
| 284 | String parentId = parent.toLanguageTag(); |
| 285 | if (!sLocaleCache.containsKey(parentId)) { |
| 286 | sLocaleCache.put(parentId, new LocaleInfo(parent)); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 291 | // TODO: See if we can reuse what LocaleList.matchScore does |
| 292 | final HashSet<String> localizedLocales = new HashSet<>(); |
| 293 | for (String localeId : LocalePicker.getSystemAssetLocales()) { |
| 294 | LocaleInfo li = new LocaleInfo(localeId); |
Mihai Nita | 86235d4 | 2016-04-01 11:50:59 -0700 | [diff] [blame] | 295 | final String country = li.getLocale().getCountry(); |
| 296 | // All this is to figure out if we should suggest a country |
| 297 | if (!country.isEmpty()) { |
| 298 | LocaleInfo cachedLocale = null; |
| 299 | if (sLocaleCache.containsKey(li.getId())) { // the simple case, e.g. fr-CH |
| 300 | cachedLocale = sLocaleCache.get(li.getId()); |
| 301 | } else { // e.g. zh-TW localized, zh-Hant-TW in cache |
| 302 | final String langScriptCtry = li.getLangScriptKey() + "-" + country; |
| 303 | if (sLocaleCache.containsKey(langScriptCtry)) { |
| 304 | cachedLocale = sLocaleCache.get(langScriptCtry); |
| 305 | } |
| 306 | } |
| 307 | if (cachedLocale != null) { |
| 308 | cachedLocale.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_CFG; |
| 309 | } |
| 310 | } |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 311 | localizedLocales.add(li.getLangScriptKey()); |
| 312 | } |
| 313 | |
| 314 | for (LocaleInfo li : sLocaleCache.values()) { |
| 315 | li.setTranslated(localizedLocales.contains(li.getLangScriptKey())); |
| 316 | } |
| 317 | |
Mihai Nita | c67b64f | 2016-02-05 14:27:55 -0800 | [diff] [blame] | 318 | addSuggestedLocalesForRegion(Locale.getDefault()); |
| 319 | |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 320 | sFullyInitialized = true; |
| 321 | } |
| 322 | |
| 323 | private static int getLevel(Set<String> ignorables, LocaleInfo li, boolean translatedOnly) { |
| 324 | if (ignorables.contains(li.getId())) return 0; |
| 325 | if (li.mIsPseudo) return 2; |
| 326 | if (translatedOnly && !li.isTranslated()) return 0; |
| 327 | if (li.getParent() != null) return 2; |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Returns a list of locales for language or region selection. |
| 333 | * If the parent is null, then it is the language list. |
Mihai Nita | 137b96e | 2016-01-25 11:31:15 -0800 | [diff] [blame] | 334 | * If it is not null, then the list will contain all the locales that belong to that parent. |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 335 | * Example: if the parent is "ar", then the region list will contain all Arabic locales. |
| 336 | * (this is not language based, but language-script, so that it works for zh-Hant and so on. |
| 337 | */ |
Mihai Nita | 137b96e | 2016-01-25 11:31:15 -0800 | [diff] [blame] | 338 | public static Set<LocaleInfo> getLevelLocales(Context context, Set<String> ignorables, |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 339 | LocaleInfo parent, boolean translatedOnly) { |
| 340 | fillCache(context); |
| 341 | String parentId = parent == null ? null : parent.getId(); |
| 342 | |
| 343 | HashSet<LocaleInfo> result = new HashSet<>(); |
| 344 | for (LocaleStore.LocaleInfo li : sLocaleCache.values()) { |
| 345 | int level = getLevel(ignorables, li, translatedOnly); |
| 346 | if (level == 2) { |
| 347 | if (parent != null) { // region selection |
| 348 | if (parentId.equals(li.getParent().toLanguageTag())) { |
Mihai Nita | f1f39cf | 2016-02-29 14:42:12 -0800 | [diff] [blame] | 349 | result.add(li); |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 350 | } |
| 351 | } else { // language selection |
| 352 | if (li.isSuggestionOfType(LocaleInfo.SUGGESTION_TYPE_SIM)) { |
| 353 | result.add(li); |
| 354 | } else { |
| 355 | result.add(getLocaleInfo(li.getParent())); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | return result; |
| 361 | } |
| 362 | |
| 363 | public static LocaleInfo getLocaleInfo(Locale locale) { |
| 364 | String id = locale.toLanguageTag(); |
| 365 | LocaleInfo result; |
| 366 | if (!sLocaleCache.containsKey(id)) { |
| 367 | result = new LocaleInfo(locale); |
| 368 | sLocaleCache.put(id, result); |
| 369 | } else { |
| 370 | result = sLocaleCache.get(id); |
| 371 | } |
| 372 | return result; |
| 373 | } |
| 374 | } |