1 /*
2  * Copyright (C) 2018 Google Inc.
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.car.settingslib.language;
18 
19 import android.app.ActivityManager;
20 import android.content.Context;
21 import android.os.RemoteException;
22 
23 import androidx.annotation.Nullable;
24 
25 import com.android.internal.app.LocaleHelper;
26 import com.android.internal.app.LocaleStore;
27 import com.android.internal.app.SuggestedLocaleAdapter;
28 
29 import java.util.Locale;
30 import java.util.Set;
31 
32 /**
33  * Utility class that supports the language/locale picker flows.
34  */
35 public final class LanguagePickerUtils {
36     /**
37      * Creates an instance of {@link SuggestedLocaleAdapter} with a locale
38      * {@link LocaleStore.LocaleInfo} that is scoped to a parent locale if a parent locale is
39      * provided.
40      */
createSuggestedLocaleAdapter( Context context, Set<LocaleStore.LocaleInfo> localeInfoSet, @Nullable LocaleStore.LocaleInfo parent)41     public static SuggestedLocaleAdapter createSuggestedLocaleAdapter(
42             Context context,
43             Set<LocaleStore.LocaleInfo> localeInfoSet,
44             @Nullable LocaleStore.LocaleInfo parent) {
45         boolean countryMode = (parent != null);
46         Locale displayLocale = countryMode ? parent.getLocale() : Locale.getDefault();
47         SuggestedLocaleAdapter adapter = new SuggestedLocaleAdapter(localeInfoSet, countryMode);
48         LocaleHelper.LocaleInfoComparator comp =
49                 new LocaleHelper.LocaleInfoComparator(displayLocale, countryMode);
50         adapter.sort(comp);
51         adapter.setDisplayLocale(context, displayLocale);
52         return adapter;
53     }
54 
55     /**
56      * Returns the locale from current system configuration, or the default locale if no system
57      * locale is available.
58      */
getConfiguredLocale()59     public static Locale getConfiguredLocale() {
60         try {
61             Locale configLocale =
62                     ActivityManager.getService().getConfiguration().getLocales().get(0);
63             return configLocale != null ? configLocale : Locale.getDefault();
64         } catch (RemoteException e) {
65             return Locale.getDefault();
66         }
67     }
68 
LanguagePickerUtils()69     private LanguagePickerUtils() {}
70 }
71