2014-04-14 08:09:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.keyboard;
|
|
|
|
|
2014-04-14 09:28:08 +00:00
|
|
|
import android.content.SharedPreferences;
|
2014-05-08 03:04:36 +00:00
|
|
|
import android.os.Build.VERSION_CODES;
|
2014-04-14 09:28:08 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2014-06-06 03:11:35 +00:00
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
2014-08-12 14:13:18 +00:00
|
|
|
import com.android.inputmethod.compat.BuildCompatUtils;
|
2014-04-14 08:09:12 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
2014-05-08 03:04:36 +00:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
2014-04-14 08:09:12 +00:00
|
|
|
|
2014-06-06 03:11:35 +00:00
|
|
|
public final class KeyboardTheme implements Comparable<KeyboardTheme> {
|
2014-04-14 09:28:08 +00:00
|
|
|
private static final String TAG = KeyboardTheme.class.getSimpleName();
|
|
|
|
|
2014-05-16 03:34:23 +00:00
|
|
|
static final String KLP_KEYBOARD_THEME_KEY = "pref_keyboard_layout_20110916";
|
|
|
|
static final String LXX_KEYBOARD_THEME_KEY = "pref_keyboard_theme_20140509";
|
2014-05-08 03:04:36 +00:00
|
|
|
|
2014-09-03 01:52:56 +00:00
|
|
|
// These should be aligned with Keyboard.themeId and Keyboard.Case.keyboardTheme
|
|
|
|
// attributes' values in attrs.xml.
|
2014-06-06 03:11:35 +00:00
|
|
|
public static final int THEME_ID_ICS = 0;
|
|
|
|
public static final int THEME_ID_KLP = 2;
|
2014-05-19 02:34:11 +00:00
|
|
|
public static final int THEME_ID_LXX_LIGHT = 3;
|
|
|
|
public static final int THEME_ID_LXX_DARK = 4;
|
2014-06-06 03:11:35 +00:00
|
|
|
public static final int DEFAULT_THEME_ID = THEME_ID_KLP;
|
2014-04-14 09:28:08 +00:00
|
|
|
|
|
|
|
private static final KeyboardTheme[] KEYBOARD_THEMES = {
|
2014-09-03 01:52:56 +00:00
|
|
|
new KeyboardTheme(THEME_ID_ICS, "ICS", R.style.KeyboardTheme_ICS,
|
2014-06-06 03:11:35 +00:00
|
|
|
// This has never been selected because we support ICS or later.
|
2014-05-16 03:34:23 +00:00
|
|
|
VERSION_CODES.BASE),
|
2014-09-03 01:52:56 +00:00
|
|
|
new KeyboardTheme(THEME_ID_KLP, "KLP", R.style.KeyboardTheme_KLP,
|
2014-06-06 03:11:35 +00:00
|
|
|
// Default theme for ICS, JB, and KLP.
|
2014-05-16 03:34:23 +00:00
|
|
|
VERSION_CODES.ICE_CREAM_SANDWICH),
|
2014-09-03 01:52:56 +00:00
|
|
|
new KeyboardTheme(THEME_ID_LXX_LIGHT, "LXXLight", R.style.KeyboardTheme_LXX_Light,
|
2014-06-06 03:11:35 +00:00
|
|
|
// Default theme for LXX.
|
2014-08-12 14:13:18 +00:00
|
|
|
BuildCompatUtils.VERSION_CODES_LXX),
|
2014-09-03 01:52:56 +00:00
|
|
|
new KeyboardTheme(THEME_ID_LXX_DARK, "LXXDark", R.style.KeyboardTheme_LXX_Dark,
|
2014-05-19 02:34:11 +00:00
|
|
|
VERSION_CODES.BASE),
|
2014-04-14 08:09:12 +00:00
|
|
|
};
|
2014-06-06 03:11:35 +00:00
|
|
|
|
2014-05-08 03:04:36 +00:00
|
|
|
static {
|
|
|
|
// Sort {@link #KEYBOARD_THEME} by descending order of {@link #mMinApiVersion}.
|
2014-06-06 03:11:35 +00:00
|
|
|
Arrays.sort(KEYBOARD_THEMES);
|
2014-05-08 03:04:36 +00:00
|
|
|
}
|
2014-04-14 08:09:12 +00:00
|
|
|
|
|
|
|
public final int mThemeId;
|
|
|
|
public final int mStyleId;
|
2014-09-03 01:52:56 +00:00
|
|
|
public final String mThemeName;
|
2014-06-06 03:11:35 +00:00
|
|
|
private final int mMinApiVersion;
|
2014-04-14 08:09:12 +00:00
|
|
|
|
|
|
|
// Note: The themeId should be aligned with "themeId" attribute of Keyboard style
|
2014-05-08 03:04:36 +00:00
|
|
|
// in values/themes-<style>.xml.
|
2014-09-03 01:52:56 +00:00
|
|
|
private KeyboardTheme(final int themeId, final String themeName, final int styleId,
|
|
|
|
final int minApiVersion) {
|
2014-04-14 08:09:12 +00:00
|
|
|
mThemeId = themeId;
|
2014-09-03 01:52:56 +00:00
|
|
|
mThemeName = themeName;
|
2014-04-14 08:09:12 +00:00
|
|
|
mStyleId = styleId;
|
2014-05-08 03:04:36 +00:00
|
|
|
mMinApiVersion = minApiVersion;
|
|
|
|
}
|
|
|
|
|
2014-06-06 03:11:35 +00:00
|
|
|
@Override
|
|
|
|
public int compareTo(final KeyboardTheme rhs) {
|
|
|
|
if (mMinApiVersion > rhs.mMinApiVersion) return -1;
|
|
|
|
if (mMinApiVersion < rhs.mMinApiVersion) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-08 03:04:36 +00:00
|
|
|
@Override
|
|
|
|
public boolean equals(final Object o) {
|
|
|
|
if (o == this) return true;
|
|
|
|
return (o instanceof KeyboardTheme) && ((KeyboardTheme)o).mThemeId == mThemeId;
|
2014-04-14 08:09:12 +00:00
|
|
|
}
|
2014-04-14 09:28:08 +00:00
|
|
|
|
2014-05-08 03:04:36 +00:00
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return mThemeId;
|
|
|
|
}
|
|
|
|
|
2014-06-06 03:11:35 +00:00
|
|
|
@UsedForTesting
|
|
|
|
static KeyboardTheme searchKeyboardThemeById(final int themeId) {
|
2014-04-14 09:28:08 +00:00
|
|
|
// TODO: This search algorithm isn't optimal if there are many themes.
|
|
|
|
for (final KeyboardTheme theme : KEYBOARD_THEMES) {
|
|
|
|
if (theme.mThemeId == themeId) {
|
|
|
|
return theme;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-06 03:11:35 +00:00
|
|
|
@UsedForTesting
|
2014-05-08 03:04:36 +00:00
|
|
|
static KeyboardTheme getDefaultKeyboardTheme(final SharedPreferences prefs,
|
|
|
|
final int sdkVersion) {
|
2014-05-16 03:34:23 +00:00
|
|
|
final String klpThemeIdString = prefs.getString(KLP_KEYBOARD_THEME_KEY, null);
|
|
|
|
if (klpThemeIdString != null) {
|
2014-05-08 03:04:36 +00:00
|
|
|
if (sdkVersion <= VERSION_CODES.KITKAT) {
|
|
|
|
try {
|
2014-05-16 03:34:23 +00:00
|
|
|
final int themeId = Integer.parseInt(klpThemeIdString);
|
2014-05-08 03:04:36 +00:00
|
|
|
final KeyboardTheme theme = searchKeyboardThemeById(themeId);
|
|
|
|
if (theme != null) {
|
|
|
|
return theme;
|
|
|
|
}
|
2014-05-16 03:34:23 +00:00
|
|
|
Log.w(TAG, "Unknown keyboard theme in KLP preference: " + klpThemeIdString);
|
2014-05-08 03:04:36 +00:00
|
|
|
} catch (final NumberFormatException e) {
|
2014-05-16 03:34:23 +00:00
|
|
|
Log.w(TAG, "Illegal keyboard theme in KLP preference: " + klpThemeIdString, e);
|
2014-05-08 03:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-16 03:34:23 +00:00
|
|
|
// Remove old preference.
|
|
|
|
Log.i(TAG, "Remove KLP keyboard theme preference: " + klpThemeIdString);
|
|
|
|
prefs.edit().remove(KLP_KEYBOARD_THEME_KEY).apply();
|
2014-05-08 03:04:36 +00:00
|
|
|
}
|
|
|
|
// TODO: This search algorithm isn't optimal if there are many themes.
|
|
|
|
for (final KeyboardTheme theme : KEYBOARD_THEMES) {
|
|
|
|
if (sdkVersion >= theme.mMinApiVersion) {
|
|
|
|
return theme;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return searchKeyboardThemeById(DEFAULT_THEME_ID);
|
|
|
|
}
|
|
|
|
|
2014-09-03 01:52:56 +00:00
|
|
|
public static String getKeyboardThemeName(final int themeId) {
|
|
|
|
final KeyboardTheme theme = searchKeyboardThemeById(themeId);
|
|
|
|
return theme.mThemeName;
|
|
|
|
}
|
|
|
|
|
2014-05-08 03:04:36 +00:00
|
|
|
public static void saveKeyboardThemeId(final String themeIdString,
|
|
|
|
final SharedPreferences prefs) {
|
2014-08-12 14:13:18 +00:00
|
|
|
saveKeyboardThemeId(themeIdString, prefs, BuildCompatUtils.EFFECTIVE_SDK_INT);
|
2014-05-16 03:34:23 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 03:11:35 +00:00
|
|
|
@UsedForTesting
|
2014-05-16 03:34:23 +00:00
|
|
|
static String getPreferenceKey(final int sdkVersion) {
|
|
|
|
if (sdkVersion <= VERSION_CODES.KITKAT) {
|
|
|
|
return KLP_KEYBOARD_THEME_KEY;
|
|
|
|
}
|
|
|
|
return LXX_KEYBOARD_THEME_KEY;
|
|
|
|
}
|
|
|
|
|
2014-06-06 03:11:35 +00:00
|
|
|
@UsedForTesting
|
|
|
|
static void saveKeyboardThemeId(final String themeIdString,
|
|
|
|
final SharedPreferences prefs, final int sdkVersion) {
|
2014-05-16 03:34:23 +00:00
|
|
|
final String prefKey = getPreferenceKey(sdkVersion);
|
|
|
|
prefs.edit().putString(prefKey, themeIdString).apply();
|
2014-04-14 09:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static KeyboardTheme getKeyboardTheme(final SharedPreferences prefs) {
|
2014-08-12 14:13:18 +00:00
|
|
|
return getKeyboardTheme(prefs, BuildCompatUtils.EFFECTIVE_SDK_INT);
|
2014-05-16 03:34:23 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 03:11:35 +00:00
|
|
|
@UsedForTesting
|
2014-05-16 03:34:23 +00:00
|
|
|
static KeyboardTheme getKeyboardTheme(final SharedPreferences prefs, final int sdkVersion) {
|
|
|
|
final String lxxThemeIdString = prefs.getString(LXX_KEYBOARD_THEME_KEY, null);
|
|
|
|
if (lxxThemeIdString == null) {
|
2014-05-08 03:04:36 +00:00
|
|
|
return getDefaultKeyboardTheme(prefs, sdkVersion);
|
2014-04-14 09:28:08 +00:00
|
|
|
}
|
|
|
|
try {
|
2014-05-16 03:34:23 +00:00
|
|
|
final int themeId = Integer.parseInt(lxxThemeIdString);
|
2014-05-08 03:04:36 +00:00
|
|
|
final KeyboardTheme theme = searchKeyboardThemeById(themeId);
|
2014-04-14 09:28:08 +00:00
|
|
|
if (theme != null) {
|
|
|
|
return theme;
|
|
|
|
}
|
2014-05-16 03:34:23 +00:00
|
|
|
Log.w(TAG, "Unknown keyboard theme in LXX preference: " + lxxThemeIdString);
|
2014-04-14 09:28:08 +00:00
|
|
|
} catch (final NumberFormatException e) {
|
2014-05-16 03:34:23 +00:00
|
|
|
Log.w(TAG, "Illegal keyboard theme in LXX preference: " + lxxThemeIdString, e);
|
2014-04-14 09:28:08 +00:00
|
|
|
}
|
2014-05-16 03:34:23 +00:00
|
|
|
// Remove preference that contains unknown or illegal theme id.
|
|
|
|
prefs.edit().remove(LXX_KEYBOARD_THEME_KEY).apply();
|
2014-05-08 03:04:36 +00:00
|
|
|
return getDefaultKeyboardTheme(prefs, sdkVersion);
|
2014-04-14 09:28:08 +00:00
|
|
|
}
|
2014-04-14 08:09:12 +00:00
|
|
|
}
|