1 /*
2  * Copyright (C) 2018 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.car.settings.language;
18 
19 import android.app.ActivityManager;
20 import android.car.drivingstate.CarUxRestrictions;
21 import android.content.Context;
22 import android.os.RemoteException;
23 
24 import androidx.preference.Preference;
25 
26 import com.android.car.settings.common.FragmentController;
27 import com.android.car.settings.common.PreferenceController;
28 import com.android.internal.app.LocaleHelper;
29 
30 import java.util.Locale;
31 
32 /** Updates the language settings entry summary with the currently configured locale. */
33 public class LanguageSettingsEntryPreferenceController extends PreferenceController<Preference> {
34 
LanguageSettingsEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)35     public LanguageSettingsEntryPreferenceController(Context context, String preferenceKey,
36             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
37         super(context, preferenceKey, fragmentController, uxRestrictions);
38     }
39 
40     @Override
updateState(Preference preference)41     protected void updateState(Preference preference) {
42         Locale locale = getConfiguredLocale();
43         preference.setSummary(
44                 LocaleHelper.getDisplayName(locale, locale, /* sentenceCase= */ true));
45     }
46 
47     @Override
getPreferenceType()48     protected Class<Preference> getPreferenceType() {
49         return Preference.class;
50     }
51 
52     /**
53      * Returns the locale from current system configuration, or the default locale if no system
54      * locale is available.
55      */
getConfiguredLocale()56     private static Locale getConfiguredLocale() {
57         try {
58             Locale configLocale =
59                     ActivityManager.getService().getConfiguration().getLocales().get(0);
60             return configLocale != null ? configLocale : Locale.getDefault();
61         } catch (RemoteException e) {
62             return Locale.getDefault();
63         }
64     }
65 }
66