OSDN Git Service

Localization: do not use private ICU APIs
[android-x86/packages-apps-Eleven.git] / src / org / lineageos / eleven / locale / LocaleSetManager.java
1 /*
2  * Copyright (C) 2014 The CyanogenMod Project
3  * Copyright (C) 2009 The Android Open Source Project
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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
16  */
17 package org.lineageos.eleven.locale;
18
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;
24
25 import org.lineageos.eleven.provider.PropertiesStore;
26
27 import java.util.Locale;
28
29 public class LocaleSetManager {
30     private static final String TAG = LocaleSetManager.class.getSimpleName();
31
32     private LocaleSet mCurrentLocales;
33     private final Context mContext;
34
35     public LocaleSetManager(final Context context) {
36         mContext = context;
37     }
38
39     /**
40      * @return true if the currently saved locale set needs to be updated
41      */
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());
46         }
47
48         LocaleSet systemLocaleSet = getSystemLocaleSet();
49
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())) {
53             return true;
54         }
55
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));
62             return true;
63         }
64
65
66         return false;
67     }
68
69     /**
70      * Sets up the locale set
71      * @param localeSet value to set it to
72      */
73     public void updateLocaleSet(LocaleSet localeSet) {
74         Log.d(TAG, "Locale Changed from: " + mCurrentLocales + " to " + localeSet);
75         mCurrentLocales = localeSet;
76         LocaleUtils.getInstance().setLocales(mCurrentLocales);
77     }
78
79     /**
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
83      */
84     private static LocaleSet getCombinedLocaleSet(LocaleSet oldLocales, Locale newLocale) {
85         Locale prevLocale = null;
86
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)) {
91                 return oldLocales;
92             }
93         }
94
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();
98     }
99
100     /**
101      * @return the system locale set
102      */
103     public LocaleSet getSystemLocaleSet() {
104         final Locale curLocale = getLocale();
105         return getCombinedLocaleSet(mCurrentLocales, curLocale);
106     }
107
108     /**
109      * @return the stored locale set
110      */
111     public LocaleSet getStoredLocaleSet() {
112         final String providerLocaleString = PropertiesStore.getInstance(mContext)
113                 .getProperty(PropertiesStore.DbProperties.LOCALE);
114
115         if (TextUtils.isEmpty(providerLocaleString)) {
116             return null;
117         }
118
119         return LocaleSet.getLocaleSet(providerLocaleString);
120     }
121
122     @VisibleForTesting
123     protected Locale getLocale() {
124         return Locale.getDefault();
125     }
126 }