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