• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.regionalpreferences;
18 
19 import android.content.Context;
20 import android.os.LocaleList;
21 
22 import com.android.internal.app.LocaleStore;
23 import com.android.settings.core.BasePreferenceController;
24 import com.android.settings.localepicker.LocaleFeatureProviderImpl;
25 
26 import java.util.HashSet;
27 import java.util.Locale;
28 import java.util.Set;
29 
30 /** A controller for the entry of Numbering System's page */
31 public class NumberingSystemController extends BasePreferenceController {
32     private static final String TAG = NumberingSystemController.class.getSimpleName();
33 
34     private LocaleList mLocaleList;
NumberingSystemController(Context context, String preferenceKey)35     public NumberingSystemController(Context context, String preferenceKey) {
36         super(context, preferenceKey);
37         // Initialize the supported languages to LocaleInfos
38         LocaleStore.fillCache(context);
39         mLocaleList = getNumberingSystemLocale();
40     }
41 
42     /**
43      * @return {@link AvailabilityStatus} for the Setting. This status is used to determine if the
44      * Setting should be shown or disabled in Settings. Further, it can be used to produce
45      * appropriate error / warning Slice in the case of unavailability.
46      * </p>
47      * The status is used for the convenience methods: {@link #isAvailable()}, {@link
48      * #isSupported()}
49      * </p>
50      * The inherited class doesn't need to check work profile if android:forWork="true" is set in
51      * preference xml.
52      */
53     @Override
getAvailabilityStatus()54     public int getAvailabilityStatus() {
55         return mLocaleList.isEmpty() ? CONDITIONALLY_UNAVAILABLE : AVAILABLE;
56     }
57 
getNumberingSystemLocale()58     private static LocaleList getNumberingSystemLocale() {
59         LocaleList localeList = LocaleList.getDefault();
60         Set<Locale> localesHasNumberingSystems = new HashSet<>();
61         for (int i = 0; i < localeList.size(); i++) {
62             Locale locale = localeList.get(i);
63             LocaleStore.LocaleInfo localeInfo = LocaleStore.getLocaleInfo(locale);
64             if (localeInfo.hasNumberingSystems()) {
65                 localesHasNumberingSystems.add(locale);
66             }
67         }
68         return convertToLocaleList(localesHasNumberingSystems);
69     }
70 
convertToLocaleList(Set<Locale> locales)71     private static LocaleList convertToLocaleList(Set<Locale> locales) {
72         if (locales.isEmpty()) {
73             return LocaleList.getEmptyLocaleList();
74         }
75         return new LocaleList(locales.stream().toArray(Locale[]::new));
76     }
77 
78     @Override
getSummary()79     public CharSequence getSummary() {
80         return new LocaleFeatureProviderImpl().getLocaleNames(getNumberingSystemLocale());
81     }
82 }
83