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.app.tvsettings.TvSettingsEnums;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.os.RemoteException;
25 import android.util.ArrayMap;
26 import android.util.Log;
27 
28 import androidx.annotation.Keep;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.internal.app.LocalePicker;
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     // Pseudo locales used for internal purposes only should not be shown in the
50     // language picker.
51     private static final String PSEUDO_LOCALE_EN_XC = "en-XC";
52 
53     private static final String LANGUAGE_RADIO_GROUP = "language";
54 
55     private final Map<String, LocalePicker.LocaleInfo> mLocaleInfoMap = new ArrayMap<>();
56 
57     // Adjust this value to keep things relatively responsive without janking animations
58     private static final int LANGUAGE_SET_DELAY_MS = 500;
59     private final Handler mDelayHandler = new Handler();
60     private Locale mNewLocale;
61     private final Runnable mSetLanguageRunnable = new Runnable() {
62         @Override
63         public void run() {
64             LocalePicker.updateLocale(mNewLocale);
65         }
66     };
67 
newInstance()68     public static LanguageFragment newInstance() {
69         return new LanguageFragment();
70     }
71 
72     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)73     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
74         final Context themedContext = getPreferenceManager().getContext();
75         final PreferenceScreen screen =
76                 getPreferenceManager().createPreferenceScreen(themedContext);
77         screen.setTitle(R.string.system_language);
78 
79         Locale currentLocale = null;
80         try {
81             currentLocale = ActivityManager.getService().getConfiguration()
82                     .getLocales().get(0);
83         } catch (RemoteException e) {
84             Log.e(TAG, "Could not retrieve locale", e);
85         }
86 
87         final List<LocalePicker.LocaleInfo> localeInfoList =
88                 LocalePicker.getAllAssetLocales(themedContext,
89                         DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(getContext()));
90 
91         Preference activePref = null;
92         for (final LocalePicker.LocaleInfo localeInfo : localeInfoList) {
93             final String languageTag = localeInfo.getLocale().toLanguageTag();
94             if (PSEUDO_LOCALE_EN_XC.equals(languageTag)) {
95                 continue;
96             }
97             mLocaleInfoMap.put(languageTag, localeInfo);
98 
99             final RadioPreference radioPreference = new RadioPreference(themedContext);
100             radioPreference.setKey(languageTag);
101             radioPreference.setPersistent(false);
102             radioPreference.setTitle(localeInfo.getLabel());
103             radioPreference.setRadioGroup(LANGUAGE_RADIO_GROUP);
104             radioPreference.setLayoutResource(R.layout.preference_reversed_widget);
105 
106             if (localeInfo.getLocale().equals(currentLocale)) {
107                 radioPreference.setChecked(true);
108                 activePref = radioPreference;
109             }
110 
111             screen.addPreference(radioPreference);
112         }
113 
114         if (activePref != null && savedInstanceState == null) {
115             scrollToPreference(activePref);
116         }
117 
118         setPreferenceScreen(screen);
119     }
120 
121     @Override
onPreferenceTreeClick(Preference preference)122     public boolean onPreferenceTreeClick(Preference preference) {
123         if (preference instanceof RadioPreference) {
124             final RadioPreference radioPreference = (RadioPreference) preference;
125             radioPreference.clearOtherRadioPreferences(getPreferenceScreen());
126             if (radioPreference.isChecked()) {
127                 mNewLocale = mLocaleInfoMap.get(radioPreference.getKey()).getLocale();
128                 mDelayHandler.removeCallbacks(mSetLanguageRunnable);
129                 mDelayHandler.postDelayed(mSetLanguageRunnable, LANGUAGE_SET_DELAY_MS);
130             } else {
131                 radioPreference.setChecked(true);
132             }
133         }
134         return super.onPreferenceTreeClick(preference);
135     }
136 
137     @Override
getPageId()138     protected int getPageId() {
139         return TvSettingsEnums.SYSTEM_LANGUAGE;
140     }
141 }
142