2 * Copyright (C) 2014 The CyanogenMod Project
3 * Copyright (C) 2009 The Android Open Source Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License
17 package org.lineageos.eleven.locale;
19 import android.content.Context;
20 import android.support.annotation.VisibleForTesting;
21 import android.text.TextUtils;
22 import android.util.Log;
24 import org.lineageos.eleven.provider.PropertiesStore;
26 import java.util.Locale;
28 import libcore.icu.ICU;
30 public class LocaleSetManager {
31 private static final String TAG = LocaleSetManager.class.getSimpleName();
33 private LocaleSet mCurrentLocales;
34 private final Context mContext;
36 public LocaleSetManager(final Context context) {
41 * @return true if the currently saved locale set needs to be updated
43 public boolean localeSetNeedsUpdate() {
44 // if we haven't loaded our current locale, try to retrieve it from the db
45 if (mCurrentLocales == null) {
46 updateLocaleSet(getStoredLocaleSet());
49 LocaleSet systemLocaleSet = getSystemLocaleSet();
51 // if we don't have a stored locale or it is different, return true
52 if (mCurrentLocales == null ||
53 !mCurrentLocales.toString().equals(systemLocaleSet.toString())) {
57 // if our icu version has changed, return true
58 final String storedICUversion = PropertiesStore.getInstance(mContext)
59 .getProperty(PropertiesStore.DbProperties.ICU_VERSION);
60 if (!ICU.getIcuVersion().equals(storedICUversion)) {
61 Log.d(TAG, "ICU version has changed from: " + storedICUversion + " to "
62 + ICU.getIcuVersion());
71 * Sets up the locale set
72 * @param localeSet value to set it to
74 public void updateLocaleSet(LocaleSet localeSet) {
75 Log.d(TAG, "Locale Changed from: " + mCurrentLocales + " to " + localeSet);
76 mCurrentLocales = localeSet;
77 LocaleUtils.getInstance().setLocales(mCurrentLocales);
81 * This takes an old and new locale set and creates a combined locale set. If they share a
82 * primary then the old one is returned
83 * @return the combined locale set
85 private static LocaleSet getCombinedLocaleSet(LocaleSet oldLocales, Locale newLocale) {
86 Locale prevLocale = null;
88 if (oldLocales != null) {
89 prevLocale = oldLocales.getPrimaryLocale();
90 // If primary locale is unchanged then no change to locale set.
91 if (newLocale.equals(prevLocale)) {
96 // Otherwise, construct a new locale set based on the new locale
97 // and the previous primary locale.
98 return new LocaleSet(newLocale, prevLocale).normalize();
102 * @return the system locale set
104 public LocaleSet getSystemLocaleSet() {
105 final Locale curLocale = getLocale();
106 return getCombinedLocaleSet(mCurrentLocales, curLocale);
110 * @return the stored locale set
112 public LocaleSet getStoredLocaleSet() {
113 final String providerLocaleString = PropertiesStore.getInstance(mContext)
114 .getProperty(PropertiesStore.DbProperties.LOCALE);
116 if (TextUtils.isEmpty(providerLocaleString)) {
120 return LocaleSet.getLocaleSet(providerLocaleString);
124 protected Locale getLocale() {
125 return Locale.getDefault();