• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.tv.settings.system;
18 
19 import android.app.ActivityManager;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.os.Handler;
23 import android.os.RemoteException;
24 import android.util.ArrayMap;
25 import android.util.Log;
26 
27 import androidx.annotation.Keep;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.internal.app.LocalePicker;
32 import com.android.internal.logging.nano.MetricsProto;
33 import com.android.settingslib.development.DevelopmentSettingsEnabler;
34 import com.android.tv.settings.R;
35 import com.android.tv.settings.RadioPreference;
36 import com.android.tv.settings.SettingsPreferenceFragment;
37 
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.Map;
41 
42 /**
43  * The language settings screen in TV Settings.
44  */
45 @Keep
46 public class LanguageFragment extends SettingsPreferenceFragment {
47     private static final String TAG = "LanguageFragment";
48 
49     private static final String LANGUAGE_RADIO_GROUP = "language";
50 
51     private final Map<String, LocalePicker.LocaleInfo> mLocaleInfoMap = new ArrayMap<>();
52 
53     // Adjust this value to keep things relatively responsive without janking animations
54     private static final int LANGUAGE_SET_DELAY_MS = 500;
55     private final Handler mDelayHandler = new Handler();
56     private Locale mNewLocale;
57     private final Runnable mSetLanguageRunnable = new Runnable() {
58         @Override
59         public void run() {
60             LocalePicker.updateLocale(mNewLocale);
61         }
62     };
63 
newInstance()64     public static LanguageFragment newInstance() {
65         return new LanguageFragment();
66     }
67 
68     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)69     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
70         final Context themedContext = getPreferenceManager().getContext();
71         final PreferenceScreen screen =
72                 getPreferenceManager().createPreferenceScreen(themedContext);
73         screen.setTitle(R.string.system_language);
74 
75         Locale currentLocale = null;
76         try {
77             currentLocale = ActivityManager.getService().getConfiguration()
78                     .getLocales().get(0);
79         } catch (RemoteException e) {
80             Log.e(TAG, "Could not retrieve locale", e);
81         }
82 
83         final List<LocalePicker.LocaleInfo> localeInfoList =
84                 LocalePicker.getAllAssetLocales(themedContext,
85                         DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(getContext()));
86 
87         Preference activePref = null;
88         for (final LocalePicker.LocaleInfo localeInfo : localeInfoList) {
89             final String languageTag = localeInfo.getLocale().toLanguageTag();
90             mLocaleInfoMap.put(languageTag, localeInfo);
91 
92             final RadioPreference radioPreference = new RadioPreference(themedContext);
93             radioPreference.setKey(languageTag);
94             radioPreference.setPersistent(false);
95             radioPreference.setTitle(localeInfo.getLabel());
96             radioPreference.setRadioGroup(LANGUAGE_RADIO_GROUP);
97             radioPreference.setLayoutResource(R.layout.preference_reversed_widget);
98 
99             if (localeInfo.getLocale().equals(currentLocale)) {
100                 radioPreference.setChecked(true);
101                 activePref = radioPreference;
102             }
103 
104             screen.addPreference(radioPreference);
105         }
106 
107         if (activePref != null && savedInstanceState == null) {
108             scrollToPreference(activePref);
109         }
110 
111         setPreferenceScreen(screen);
112     }
113 
114     @Override
onPreferenceTreeClick(Preference preference)115     public boolean onPreferenceTreeClick(Preference preference) {
116         if (preference instanceof RadioPreference) {
117             final RadioPreference radioPreference = (RadioPreference) preference;
118             radioPreference.clearOtherRadioPreferences(getPreferenceScreen());
119             if (radioPreference.isChecked()) {
120                 mNewLocale = mLocaleInfoMap.get(radioPreference.getKey()).getLocale();
121                 mDelayHandler.removeCallbacks(mSetLanguageRunnable);
122                 mDelayHandler.postDelayed(mSetLanguageRunnable, LANGUAGE_SET_DELAY_MS);
123             } else {
124                 radioPreference.setChecked(true);
125             }
126         }
127         return super.onPreferenceTreeClick(preference);
128     }
129 
130     @Override
getMetricsCategory()131     public int getMetricsCategory() {
132         return MetricsProto.MetricsEvent.SETTINGS_LANGUAGE_CATEGORY;
133     }
134 }
135