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.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.os.Build;
22 
23 import com.android.car.settings.common.FragmentController;
24 import com.android.internal.app.LocaleStore;
25 
26 import java.util.Locale;
27 import java.util.Set;
28 
29 /** Business logic for showing and acting on languages in the language settings screen. */
30 public class LanguagePickerPreferenceController extends LanguageBasePreferenceController {
31 
LanguagePickerPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)32     public LanguagePickerPreferenceController(Context context, String preferenceKey,
33             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
34         super(context, preferenceKey, fragmentController, uxRestrictions);
35     }
36 
37     @Override
defineLocaleProvider()38     protected LocalePreferenceProvider defineLocaleProvider() {
39         Set<LocaleStore.LocaleInfo> localeInfoSet = LocaleStore.getLevelLocales(
40                 getContext(),
41                 getExclusionSet(),
42                 /* parent= */ null,
43                 /* translatedOnly= */ true);
44         maybeAddPseudoLocale(localeInfoSet);
45 
46         return LocalePreferenceProvider.newInstance(getContext(), localeInfoSet,
47                 /* parentLocale= */ null);
48     }
49 
50     @Override
handleLocaleWithChildren(LocaleStore.LocaleInfo parentLocaleInfo)51     protected void handleLocaleWithChildren(LocaleStore.LocaleInfo parentLocaleInfo) {
52         ChildLocalePickerFragment fragment = ChildLocalePickerFragment.newInstance(
53                 parentLocaleInfo);
54         fragment.registerChildLocaleSelectedListener(
55                 localeInfo -> getFragmentController().goBack());
56         getFragmentController().launchFragment(fragment);
57     }
58 
59     /**
60      * Add a pseudo locale in debug build for testing RTL.
61      *
62      * @param localeInfos the set of {@link LocaleStore.LocaleInfo} to which the locale is added.
63      */
maybeAddPseudoLocale(Set<LocaleStore.LocaleInfo> localeInfos)64     private void maybeAddPseudoLocale(Set<LocaleStore.LocaleInfo> localeInfos) {
65         if (Build.IS_USERDEBUG) {
66             // The ar-XB pseudo-locale is RTL.
67             localeInfos.add(LocaleStore.getLocaleInfo(new Locale("ar", "XB")));
68         }
69     }
70 }
71