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