1 /*
2  * Copyright (C) 2022 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.tv.settings.system.locale;
18 
19 import android.app.ActivityManager;
20 import android.content.Context;
21 import android.os.RemoteException;
22 import android.util.Log;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.lifecycle.ViewModel;
26 
27 import com.android.internal.app.LocaleStore;
28 
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.Map;
37 import java.util.Set;
38 
39 import com.android.tv.settings.R;
40 
41 /**
42  * ViewModel to provide data for locale selection.
43  */
44 public class LocaleDataViewModel extends ViewModel {
45     static final boolean TRANSLATED_ONLY = false;
46     private static final String TAG = "LocaleDataViewModel";
47     @VisibleForTesting
48     final Map<LocaleStore.LocaleInfo, List<LocaleStore.LocaleInfo>> mLocaleMap =
49             new HashMap<>();
50     Set<LocaleStore.LocaleInfo> mLocaleInfos;
51     Set<String> mUnsupportedLocales;
52 
getCurrentLocale()53     public static Locale getCurrentLocale() {
54         try {
55             return ActivityManager.getService().getConfiguration()
56                     .getLocales().get(0);
57         } catch (RemoteException e) {
58             Log.e(TAG, "Could not retrieve locale", e);
59             return null;
60         }
61     }
62 
getLocaleInfos( Context context)63     public synchronized Set<LocaleStore.LocaleInfo> getLocaleInfos(
64             Context context) {
65         if (mLocaleInfos == null) {
66             mLocaleInfos = LocaleStore.getLevelLocales(context,
67                     /* ignorables= */ Collections.emptySet(),
68                     /* parent= */ null,
69                     TRANSLATED_ONLY);
70         }
71         return mLocaleInfos;
72     }
73 
addLocaleInfoList(LocaleStore.LocaleInfo localeInfo, Context context)74     public synchronized void addLocaleInfoList(LocaleStore.LocaleInfo localeInfo, Context context) {
75         if (mLocaleMap.containsKey(localeInfo)) {
76             return;
77         }
78 
79         if (mUnsupportedLocales == null) {
80             String[] unsupportedLocales = context.getResources().getStringArray(
81                     R.array.config_unsupported_locales);
82             mUnsupportedLocales = new HashSet<>(Arrays.asList(unsupportedLocales));
83         }
84 
85         ArrayList<LocaleStore.LocaleInfo> localeInfoWithCountryList = new ArrayList<>();
86         for (LocaleStore.LocaleInfo locale : LocaleStore.getLevelLocales(
87                 context, Collections.emptySet(), localeInfo, TRANSLATED_ONLY)) {
88             if (!mUnsupportedLocales.contains(locale.getId())) {
89                 localeInfoWithCountryList.add(locale);
90             }
91         }
92 
93         mLocaleMap.put(localeInfo, localeInfoWithCountryList);
94     }
95 
96     public synchronized List<LocaleStore.LocaleInfo>
getLocaleInfoList(LocaleStore.LocaleInfo localeInfo)97     getLocaleInfoList(LocaleStore.LocaleInfo localeInfo) {
98         return mLocaleMap.get(localeInfo);
99     }
100 }