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