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;
|
|
|
|
import android.os.Build.VERSION_CODES;
|
2014-04-14 09:28:08 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
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;
|
|
|
|
import java.util.Comparator;
|
2014-04-14 08:09:12 +00:00
|
|
|
|
|
|
|
public final class KeyboardTheme {
|
2014-04-14 09:28:08 +00:00
|
|
|
private static final String TAG = KeyboardTheme.class.getSimpleName();
|
|
|
|
|
2014-05-08 03:04:36 +00:00
|
|
|
static final String KITKAT_KEYBOARD_THEME_KEY = "pref_keyboard_layout_20110916";
|
|
|
|
static final String KEYBOARD_THEME_KEY = "pref_keyboard_theme_20140509";
|
|
|
|
|
|
|
|
static final int THEME_ID_ICS = 0;
|
|
|
|
static final int THEME_ID_KLP = 2;
|
|
|
|
static final int THEME_ID_LMP = 3;
|
|
|
|
static final int DEFAULT_THEME_ID = THEME_ID_KLP;
|
2014-04-14 09:28:08 +00:00
|
|
|
|
|
|
|
private static final KeyboardTheme[] KEYBOARD_THEMES = {
|
2014-05-08 03:04:36 +00:00
|
|
|
new KeyboardTheme(THEME_ID_ICS, R.style.KeyboardTheme_ICS,
|
|
|
|
VERSION_CODES.ICE_CREAM_SANDWICH),
|
|
|
|
new KeyboardTheme(THEME_ID_KLP, R.style.KeyboardTheme_KLP,
|
|
|
|
VERSION_CODES.KITKAT),
|
|
|
|
// TODO: Update to LMP style.
|
|
|
|
new KeyboardTheme(THEME_ID_LMP, R.style.KeyboardTheme_KLP,
|
|
|
|
// TODO: Update this constant once the *next* version becomes available.
|
|
|
|
VERSION_CODES.CUR_DEVELOPMENT),
|
2014-04-14 08:09:12 +00:00
|
|
|
};
|
2014-05-08 03:04:36 +00:00
|
|
|
static {
|
|
|
|
// Sort {@link #KEYBOARD_THEME} by descending order of {@link #mMinApiVersion}.
|
|
|
|
Arrays.sort(KEYBOARD_THEMES, new Comparator<KeyboardTheme>() {
|
|
|
|
@Override
|
|
|
|
public int compare(final KeyboardTheme lhs, final KeyboardTheme rhs) {
|
|
|
|
if (lhs.mMinApiVersion > rhs.mMinApiVersion) return -1;
|
|
|
|
if (lhs.mMinApiVersion < rhs.mMinApiVersion) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-04-14 08:09:12 +00:00
|
|
|
|
|
|
|
public final int mThemeId;
|
|
|
|
public final int mStyleId;
|
2014-05-08 03:04:36 +00:00
|
|
|
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.
|
|
|
|
private KeyboardTheme(final int themeId, final int styleId, final int minApiVersion) {
|
2014-04-14 08:09:12 +00:00
|
|
|
mThemeId = themeId;
|
|
|
|
mStyleId = styleId;
|
2014-05-08 03:04:36 +00:00
|
|
|
mMinApiVersion = minApiVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This method should be removed when {@link LatinImeLogger} is removed.
|
|
|
|
public int getCompatibleThemeIdForLogging() {
|
|
|
|
switch (mThemeId) {
|
|
|
|
case THEME_ID_ICS:
|
|
|
|
return 5;
|
|
|
|
case THEME_ID_KLP:
|
|
|
|
return 9;
|
|
|
|
case THEME_ID_LMP:
|
|
|
|
return 10;
|
|
|
|
default: // Invalid theme
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private 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-05-08 03:04:36 +00:00
|
|
|
private static int getSdkVersion() {
|
|
|
|
final int sdkVersion = Build.VERSION.SDK_INT;
|
|
|
|
// TODO: Consider to remove this check once the *next* version becomes available.
|
|
|
|
if (sdkVersion == VERSION_CODES.KITKAT && Build.VERSION.CODENAME.startsWith("L")) {
|
|
|
|
return VERSION_CODES.CUR_DEVELOPMENT;
|
|
|
|
}
|
|
|
|
return sdkVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyboardTheme getDefaultKeyboardTheme(final SharedPreferences prefs,
|
|
|
|
final int sdkVersion) {
|
|
|
|
final String obsoleteIdString = prefs.getString(KITKAT_KEYBOARD_THEME_KEY, null);
|
|
|
|
if (obsoleteIdString != null) {
|
|
|
|
// Remove old preference.
|
|
|
|
prefs.edit().remove(KITKAT_KEYBOARD_THEME_KEY).apply();
|
|
|
|
if (sdkVersion <= VERSION_CODES.KITKAT) {
|
|
|
|
try {
|
|
|
|
final int themeId = Integer.parseInt(obsoleteIdString);
|
|
|
|
final KeyboardTheme theme = searchKeyboardThemeById(themeId);
|
|
|
|
if (theme != null) {
|
|
|
|
return theme;
|
|
|
|
}
|
|
|
|
Log.w(TAG, "Unknown keyboard theme in preference: " + obsoleteIdString);
|
|
|
|
} catch (final NumberFormatException e) {
|
|
|
|
Log.w(TAG, "Illegal keyboard theme in preference: " + obsoleteIdString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void saveKeyboardThemeId(final String themeIdString,
|
|
|
|
final SharedPreferences prefs) {
|
|
|
|
prefs.edit().putString(KEYBOARD_THEME_KEY, themeIdString).apply();
|
2014-04-14 09:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static KeyboardTheme getKeyboardTheme(final SharedPreferences prefs) {
|
2014-05-08 03:04:36 +00:00
|
|
|
final int sdkVersion = getSdkVersion();
|
|
|
|
final String themeIdString = prefs.getString(KEYBOARD_THEME_KEY, null);
|
2014-04-14 09:28:08 +00:00
|
|
|
if (themeIdString == null) {
|
2014-05-08 03:04:36 +00:00
|
|
|
return getDefaultKeyboardTheme(prefs, sdkVersion);
|
2014-04-14 09:28:08 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
final int themeId = Integer.parseInt(themeIdString);
|
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;
|
|
|
|
}
|
|
|
|
Log.w(TAG, "Unknown keyboard theme in preference: " + themeIdString);
|
|
|
|
} catch (final NumberFormatException e) {
|
|
|
|
Log.w(TAG, "Illegal keyboard theme in preference: " + themeIdString);
|
|
|
|
}
|
2014-05-08 03:04:36 +00:00
|
|
|
// Remove preference.
|
|
|
|
prefs.edit().remove(KEYBOARD_THEME_KEY).apply();
|
|
|
|
return getDefaultKeyboardTheme(prefs, sdkVersion);
|
2014-04-14 09:28:08 +00:00
|
|
|
}
|
2014-04-14 08:09:12 +00:00
|
|
|
}
|