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