KeyboardTheme.java revision 5380acfd04342b35289599f0bda3eaae1ddac642
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 17package com.android.inputmethod.keyboard; 18 19import android.content.SharedPreferences; 20import android.os.Build; 21import android.os.Build.VERSION_CODES; 22import android.util.Log; 23 24import com.android.inputmethod.annotations.UsedForTesting; 25import com.android.inputmethod.latin.R; 26 27import java.util.Arrays; 28 29public final class KeyboardTheme implements Comparable<KeyboardTheme> { 30 private static final String TAG = KeyboardTheme.class.getSimpleName(); 31 32 static final String KLP_KEYBOARD_THEME_KEY = "pref_keyboard_layout_20110916"; 33 static final String LXX_KEYBOARD_THEME_KEY = "pref_keyboard_theme_20140509"; 34 35 public static final int THEME_ID_ICS = 0; 36 public static final int THEME_ID_KLP = 2; 37 public static final int THEME_ID_LXX_LIGHT = 3; 38 public static final int THEME_ID_LXX_DARK = 4; 39 public static final int DEFAULT_THEME_ID = THEME_ID_KLP; 40 41 // TODO: Update this constant once the *next* version becomes available. 42 public static final int VERSION_CODES_LXX = 21; 43 44 private static final KeyboardTheme[] KEYBOARD_THEMES = { 45 new KeyboardTheme(THEME_ID_ICS, R.style.KeyboardTheme_ICS, 46 // This has never been selected because we support ICS or later. 47 VERSION_CODES.BASE), 48 new KeyboardTheme(THEME_ID_KLP, R.style.KeyboardTheme_KLP, 49 // Default theme for ICS, JB, and KLP. 50 VERSION_CODES.ICE_CREAM_SANDWICH), 51 new KeyboardTheme(THEME_ID_LXX_LIGHT, R.style.KeyboardTheme_LXX_Light, 52 // Default theme for LXX. 53 VERSION_CODES_LXX), 54 new KeyboardTheme(THEME_ID_LXX_DARK, R.style.KeyboardTheme_LXX_Dark, 55 VERSION_CODES.BASE), 56 }; 57 58 static { 59 // Sort {@link #KEYBOARD_THEME} by descending order of {@link #mMinApiVersion}. 60 Arrays.sort(KEYBOARD_THEMES); 61 } 62 63 public final int mThemeId; 64 public final int mStyleId; 65 private final int mMinApiVersion; 66 67 // Note: The themeId should be aligned with "themeId" attribute of Keyboard style 68 // in values/themes-<style>.xml. 69 private KeyboardTheme(final int themeId, final int styleId, final int minApiVersion) { 70 mThemeId = themeId; 71 mStyleId = styleId; 72 mMinApiVersion = minApiVersion; 73 } 74 75 @Override 76 public int compareTo(final KeyboardTheme rhs) { 77 if (mMinApiVersion > rhs.mMinApiVersion) return -1; 78 if (mMinApiVersion < rhs.mMinApiVersion) return 1; 79 return 0; 80 } 81 82 @Override 83 public boolean equals(final Object o) { 84 if (o == this) return true; 85 return (o instanceof KeyboardTheme) && ((KeyboardTheme)o).mThemeId == mThemeId; 86 } 87 88 @Override 89 public int hashCode() { 90 return mThemeId; 91 } 92 93 @UsedForTesting 94 static KeyboardTheme searchKeyboardThemeById(final int themeId) { 95 // TODO: This search algorithm isn't optimal if there are many themes. 96 for (final KeyboardTheme theme : KEYBOARD_THEMES) { 97 if (theme.mThemeId == themeId) { 98 return theme; 99 } 100 } 101 return null; 102 } 103 104 private static int getSdkVersion() { 105 return Build.VERSION.SDK_INT; 106 } 107 108 @UsedForTesting 109 static KeyboardTheme getDefaultKeyboardTheme(final SharedPreferences prefs, 110 final int sdkVersion) { 111 final String klpThemeIdString = prefs.getString(KLP_KEYBOARD_THEME_KEY, null); 112 if (klpThemeIdString != null) { 113 if (sdkVersion <= VERSION_CODES.KITKAT) { 114 try { 115 final int themeId = Integer.parseInt(klpThemeIdString); 116 final KeyboardTheme theme = searchKeyboardThemeById(themeId); 117 if (theme != null) { 118 return theme; 119 } 120 Log.w(TAG, "Unknown keyboard theme in KLP preference: " + klpThemeIdString); 121 } catch (final NumberFormatException e) { 122 Log.w(TAG, "Illegal keyboard theme in KLP preference: " + klpThemeIdString, e); 123 } 124 } 125 // Remove old preference. 126 Log.i(TAG, "Remove KLP keyboard theme preference: " + klpThemeIdString); 127 prefs.edit().remove(KLP_KEYBOARD_THEME_KEY).apply(); 128 } 129 // TODO: This search algorithm isn't optimal if there are many themes. 130 for (final KeyboardTheme theme : KEYBOARD_THEMES) { 131 if (sdkVersion >= theme.mMinApiVersion) { 132 return theme; 133 } 134 } 135 return searchKeyboardThemeById(DEFAULT_THEME_ID); 136 } 137 138 public static void saveKeyboardThemeId(final String themeIdString, 139 final SharedPreferences prefs) { 140 saveKeyboardThemeId(themeIdString, prefs, getSdkVersion()); 141 } 142 143 @UsedForTesting 144 static String getPreferenceKey(final int sdkVersion) { 145 if (sdkVersion <= VERSION_CODES.KITKAT) { 146 return KLP_KEYBOARD_THEME_KEY; 147 } 148 return LXX_KEYBOARD_THEME_KEY; 149 } 150 151 @UsedForTesting 152 static void saveKeyboardThemeId(final String themeIdString, 153 final SharedPreferences prefs, final int sdkVersion) { 154 final String prefKey = getPreferenceKey(sdkVersion); 155 prefs.edit().putString(prefKey, themeIdString).apply(); 156 } 157 158 public static KeyboardTheme getKeyboardTheme(final SharedPreferences prefs) { 159 return getKeyboardTheme(prefs, getSdkVersion()); 160 } 161 162 @UsedForTesting 163 static KeyboardTheme getKeyboardTheme(final SharedPreferences prefs, final int sdkVersion) { 164 final String lxxThemeIdString = prefs.getString(LXX_KEYBOARD_THEME_KEY, null); 165 if (lxxThemeIdString == null) { 166 return getDefaultKeyboardTheme(prefs, sdkVersion); 167 } 168 try { 169 final int themeId = Integer.parseInt(lxxThemeIdString); 170 final KeyboardTheme theme = searchKeyboardThemeById(themeId); 171 if (theme != null) { 172 return theme; 173 } 174 Log.w(TAG, "Unknown keyboard theme in LXX preference: " + lxxThemeIdString); 175 } catch (final NumberFormatException e) { 176 Log.w(TAG, "Illegal keyboard theme in LXX preference: " + lxxThemeIdString, e); 177 } 178 // Remove preference that contains unknown or illegal theme id. 179 prefs.edit().remove(LXX_KEYBOARD_THEME_KEY).apply(); 180 return getDefaultKeyboardTheme(prefs, sdkVersion); 181 } 182} 183