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.os.Build;
22 import android.text.TextUtils;
23 import android.util.Log;
25 import org.lineageos.eleven.provider.PropertiesStore;
27 import java.util.Locale;
29 public class LocaleSetManager {
30 private static final String TAG = LocaleSetManager.class.getSimpleName();
32 private LocaleSet mCurrentLocales;
33 private final Context mContext;
35 public LocaleSetManager(final Context context) {
40 * @return true if the currently saved locale set needs to be updated
42 public boolean localeSetNeedsUpdate() {
43 // if we haven't loaded our current locale, try to retrieve it from the db
44 if (mCurrentLocales == null) {
45 updateLocaleSet(getStoredLocaleSet());
48 LocaleSet systemLocaleSet = getSystemLocaleSet();
50 // if we don't have a stored locale or it is different, return true
51 if (mCurrentLocales == null ||
52 !mCurrentLocales.toString().equals(systemLocaleSet.toString())) {
56 // if our icu version has changed, return true
57 final String storedICUversion = PropertiesStore.getInstance(mContext)
58 .getProperty(PropertiesStore.DbProperties.ICU_VERSION);
59 if (!String.valueOf(Build.VERSION.SDK_INT).equals(storedICUversion)) {
60 Log.d(TAG, "ICU version has changed from: " + storedICUversion + " to "
61 + String.valueOf(Build.VERSION.SDK_INT));
70 * Sets up the locale set
71 * @param localeSet value to set it to
73 public void updateLocaleSet(LocaleSet localeSet) {
74 Log.d(TAG, "Locale Changed from: " + mCurrentLocales + " to " + localeSet);
75 mCurrentLocales = localeSet;
76 LocaleUtils.getInstance().setLocales(mCurrentLocales);
80 * This takes an old and new locale set and creates a combined locale set. If they share a
81 * primary then the old one is returned
82 * @return the combined locale set
84 private static LocaleSet getCombinedLocaleSet(LocaleSet oldLocales, Locale newLocale) {
85 Locale prevLocale = null;
87 if (oldLocales != null) {
88 prevLocale = oldLocales.getPrimaryLocale();
89 // If primary locale is unchanged then no change to locale set.
90 if (newLocale.equals(prevLocale)) {
95 // Otherwise, construct a new locale set based on the new locale
96 // and the previous primary locale.
97 return new LocaleSet(newLocale, prevLocale).normalize();
101 * @return the system locale set
103 public LocaleSet getSystemLocaleSet() {
104 final Locale curLocale = getLocale();
105 return getCombinedLocaleSet(mCurrentLocales, curLocale);
109 * @return the stored locale set
111 public LocaleSet getStoredLocaleSet() {
112 final String providerLocaleString = PropertiesStore.getInstance(mContext)
113 .getProperty(PropertiesStore.DbProperties.LOCALE);
115 if (TextUtils.isEmpty(providerLocaleString)) {
119 return LocaleSet.getLocaleSet(providerLocaleString);
123 protected Locale getLocale() {
124 return Locale.getDefault();