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.content.Context;
20 import android.os.Bundle;
21 
22 import androidx.annotation.XmlRes;
23 
24 import com.android.car.settings.R;
25 import com.android.car.settings.common.SettingsFragment;
26 import com.android.internal.app.LocaleStore;
27 
28 import java.util.Locale;
29 
30 /** Secondary screen for language selection, when a language has multiple locales. */
31 public class ChildLocalePickerFragment extends SettingsFragment {
32 
33     /**
34      * Creates a ChildLocalePickerFragment with the parent locale info included in the arguments.
35      */
newInstance(LocaleStore.LocaleInfo parentLocaleInfo)36     public static ChildLocalePickerFragment newInstance(LocaleStore.LocaleInfo parentLocaleInfo) {
37         Bundle bundle = new Bundle();
38         bundle.putSerializable(LocaleUtil.LOCALE_BUNDLE_KEY, parentLocaleInfo.getLocale());
39         ChildLocalePickerFragment fragment = new ChildLocalePickerFragment();
40         fragment.setArguments(bundle);
41         return fragment;
42     }
43 
44     private LanguageBasePreferenceController.LocaleSelectedListener mLocaleSelectedListener;
45     private LocaleStore.LocaleInfo mParentLocaleInfo;
46 
47     /**
48      * Allows the creator of ChildLocalePickerFragment to include a listener for when the child
49      * locale is selected.
50      */
registerChildLocaleSelectedListener( LanguageBasePreferenceController.LocaleSelectedListener listener)51     public void registerChildLocaleSelectedListener(
52             LanguageBasePreferenceController.LocaleSelectedListener listener) {
53         mLocaleSelectedListener = listener;
54     }
55 
56     @Override
onAttach(Context context)57     public void onAttach(Context context) {
58         super.onAttach(context);
59         Locale locale = (Locale) getArguments().getSerializable(LocaleUtil.LOCALE_BUNDLE_KEY);
60         mParentLocaleInfo = LocaleStore.getLocaleInfo(locale);
61         use(ChildLocalePickerPreferenceController.class,
62                 R.string.pk_child_locale_picker).setParentLocaleInfo(mParentLocaleInfo);
63         use(ChildLocalePickerPreferenceController.class,
64                 R.string.pk_child_locale_picker).setLocaleSelectedListener(
65                 mLocaleSelectedListener);
66     }
67 
68     @Override
onActivityCreated(Bundle savedInstanceState)69     public void onActivityCreated(Bundle savedInstanceState) {
70         super.onActivityCreated(savedInstanceState);
71 
72         getToolbar().setTitle(mParentLocaleInfo.getFullNameNative());
73     }
74 
75     @Override
76     @XmlRes
getPreferenceScreenResId()77     protected int getPreferenceScreenResId() {
78         return R.xml.child_locale_picker_fragment;
79     }
80 }
81