Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2014 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.inputmethod.latin.settings;
     18 
     19 import android.content.Context;
     20 import android.content.SharedPreferences;
     21 import android.content.res.Resources;
     22 import android.os.Bundle;
     23 import android.preference.Preference;
     24 import android.preference.PreferenceScreen;
     25 
     26 import com.android.inputmethod.keyboard.KeyboardTheme;
     27 import com.android.inputmethod.latin.R;
     28 import com.android.inputmethod.latin.settings.RadioButtonPreference.OnRadioButtonClickedListener;
     29 
     30 /**
     31  * "Keyboard theme" settings sub screen.
     32  */
     33 public final class ThemeSettingsFragment extends SubScreenFragment
     34         implements OnRadioButtonClickedListener {
     35     private String mSelectedThemeId;
     36 
     37     static class KeyboardThemePreference extends RadioButtonPreference {
     38         final String mThemeId;
     39 
     40         KeyboardThemePreference(final Context context, final String name, final String id) {
     41             super(context);
     42             setTitle(name);
     43             mThemeId = id;
     44         }
     45     }
     46 
     47     static void updateKeyboardThemeSummary(final Preference pref) {
     48         final Resources res = pref.getContext().getResources();
     49         final SharedPreferences prefs = pref.getSharedPreferences();
     50         final KeyboardTheme keyboardTheme = KeyboardTheme.getKeyboardTheme(prefs);
     51         final String keyboardThemeId = String.valueOf(keyboardTheme.mThemeId);
     52         final String[] keyboardThemeNames = res.getStringArray(R.array.keyboard_theme_names);
     53         final String[] keyboardThemeIds = res.getStringArray(R.array.keyboard_theme_ids);
     54         for (int index = 0; index < keyboardThemeNames.length; index++) {
     55             if (keyboardThemeId.equals(keyboardThemeIds[index])) {
     56                 pref.setSummary(keyboardThemeNames[index]);
     57                 return;
     58             }
     59         }
     60     }
     61 
     62     @Override
     63     public void onCreate(final Bundle icicle) {
     64         super.onCreate(icicle);
     65         addPreferencesFromResource(R.xml.prefs_screen_theme);
     66         final PreferenceScreen screen = getPreferenceScreen();
     67         final Resources res = getResources();
     68         final String[] keyboardThemeNames = res.getStringArray(R.array.keyboard_theme_names);
     69         final String[] keyboardThemeIds = res.getStringArray(R.array.keyboard_theme_ids);
     70         for (int index = 0; index < keyboardThemeNames.length; index++) {
     71             final KeyboardThemePreference pref = new KeyboardThemePreference(
     72                     getActivity(), keyboardThemeNames[index], keyboardThemeIds[index]);
     73             screen.addPreference(pref);
     74             pref.setOnRadioButtonClickedListener(this);
     75         }
     76         final SharedPreferences prefs = getSharedPreferences();
     77         final KeyboardTheme keyboardTheme = KeyboardTheme.getKeyboardTheme(prefs);
     78         mSelectedThemeId = String.valueOf(keyboardTheme.mThemeId);
     79     }
     80 
     81     @Override
     82     public void onRadioButtonClicked(final RadioButtonPreference preference) {
     83         if (preference instanceof KeyboardThemePreference) {
     84             final KeyboardThemePreference pref = (KeyboardThemePreference)preference;
     85             mSelectedThemeId = pref.mThemeId;
     86             updateSelected();
     87         }
     88     }
     89 
     90     @Override
     91     public void onResume() {
     92         super.onResume();
     93         updateSelected();
     94     }
     95 
     96     @Override
     97     public void onPause() {
     98         super.onPause();
     99         KeyboardTheme.saveKeyboardThemeId(mSelectedThemeId, getSharedPreferences());
    100     }
    101 
    102     private void updateSelected() {
    103         final PreferenceScreen screen = getPreferenceScreen();
    104         final int count = screen.getPreferenceCount();
    105         for (int index = 0; index < count; index++) {
    106             final Preference preference = screen.getPreference(index);
    107             if (preference instanceof KeyboardThemePreference) {
    108                 final KeyboardThemePreference pref = (KeyboardThemePreference)preference;
    109                 final boolean selected = mSelectedThemeId.equals(pref.mThemeId);
    110                 pref.setSelected(selected);
    111             }
    112         }
    113     }
    114 }
    115