OSDN Git Service

Eleven: remove guava dependency
[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.text.TextUtils;
22 import android.util.Log;
23
24 import org.lineageos.eleven.provider.PropertiesStore;
25
26 import java.util.Locale;
27
28 import libcore.icu.ICU;
29
30 public class LocaleSetManager {
31     private static final String TAG = LocaleSetManager.class.getSimpleName();
32
33     private LocaleSet mCurrentLocales;
34     private final Context mContext;
35
36     public LocaleSetManager(final Context context) {
37         mContext = context;
38     }
39
40     /**
41      * @return true if the currently saved locale set needs to be updated
42      */
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());
47         }
48
49         LocaleSet systemLocaleSet = getSystemLocaleSet();
50
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())) {
54             return true;
55         }
56
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());
63             return true;
64         }
65
66
67         return false;
68     }
69
70     /**
71      * Sets up the locale set
72      * @param localeSet value to set it to
73      */
74     public void updateLocaleSet(LocaleSet localeSet) {
75         Log.d(TAG, "Locale Changed from: " + mCurrentLocales + " to " + localeSet);
76         mCurrentLocales = localeSet;
77         LocaleUtils.getInstance().setLocales(mCurrentLocales);
78     }
79
80     /**
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
84      */
85     private static LocaleSet getCombinedLocaleSet(LocaleSet oldLocales, Locale newLocale) {
86         Locale prevLocale = null;
87
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)) {
92                 return oldLocales;
93             }
94         }
95
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();
99     }
100
101     /**
102      * @return the system locale set
103      */
104     public LocaleSet getSystemLocaleSet() {
105         final Locale curLocale = getLocale();
106         return getCombinedLocaleSet(mCurrentLocales, curLocale);
107     }
108
109     /**
110      * @return the stored locale set
111      */
112     public LocaleSet getStoredLocaleSet() {
113         final String providerLocaleString = PropertiesStore.getInstance(mContext)
114                 .getProperty(PropertiesStore.DbProperties.LOCALE);
115
116         if (TextUtils.isEmpty(providerLocaleString)) {
117             return null;
118         }
119
120         return LocaleSet.getLocaleSet(providerLocaleString);
121     }
122
123     @VisibleForTesting
124     protected Locale getLocale() {
125         return Locale.getDefault();
126     }
127 }