Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 19688b58 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Refactor KeyboardTheme

This CL introduces two preference keys in addition to the existing one.

- pref_keyboard_theme: The new preference key that is used only in the
  preference settings.

- pref_keyboard_layout_20110916: The previous preference key that
  holds keyboard color scheme settings. This is superseded by the next
  key.

- pref_keyboard_theme_20140509: The new preference key that holds
  keyboard theme settings. This value is forcibly reset to LMP theme
  on a LMP device. On a KitKat and previous device, the settings of
  the pref_keyboard_layout_20110916 is carried on into this
  preference.

This CL must be checked in together with Ifcedeabbec.

Change-Id: Ibe75146e2c1c7eebf964ca86550baf244fedb79a
parent 28ebe2db
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -24,9 +24,6 @@
         at input history to suggest a hopefully helpful suggestions for the next word? -->
    <bool name="config_default_next_word_prediction">true</bool>

    <!-- This configuration must be aligned with {@link KeyboardTheme#DEFAULT_THEME_ID}. -->
    <string name="config_default_keyboard_theme_id" translatable="false">2</string>

    <integer name="config_delay_update_shift_state">100</integer>
    <integer name="config_double_space_period_timeout">1100</integer>

+34 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
**
** Copyright 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.
*/
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <!-- For keyboard color scheme option dialog. -->
    <string-array name="keyboard_theme_names" translatable="false">
        <item>@string/keyboard_color_scheme_white</item>
        <item>@string/keyboard_color_scheme_blue</item>
        <!-- TODO: Make this item as translatable string resource. -->
        <item>Quantum</item>
    </string-array>
    <!-- An element must be a keyboard theme id of {@link KeyboardTheme#THEME_ID_*}. -->
    <string-array name="keyboard_theme_ids" translatable="false">
        <item>2</item>
        <item>0</item>
        <item>3</item>
    </string-array>
</resources>
+2 −3
Original line number Diff line number Diff line
@@ -158,12 +158,11 @@
                android:persistent="true"
                android:defaultValue="false" />
            <ListPreference
                android:key="pref_keyboard_layout_20110916"
                android:key="pref_keyboard_theme"
                android:title="@string/keyboard_color_scheme"
                android:persistent="true"
                android:entryValues="@array/keyboard_theme_ids"
                android:entries="@array/keyboard_theme_names"
                android:defaultValue="@string/config_default_keyboard_theme_id" />
                android:entries="@array/keyboard_theme_names" />
            <PreferenceScreen
                android:fragment="com.android.inputmethod.latin.settings.AdditionalSubtypeSettings"
                android:key="custom_input_styles"
+4 −3
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
     * what user actually typed. */
    private boolean mIsAutoCorrectionActive;

    private KeyboardTheme mKeyboardTheme = KeyboardTheme.getDefaultKeyboardTheme();
    private KeyboardTheme mKeyboardTheme;
    private Context mThemeContext;

    private static final KeyboardSwitcher sInstance = new KeyboardSwitcher();
@@ -101,7 +101,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {

    private boolean updateKeyboardThemeAndContextThemeWrapper(final Context context,
            final KeyboardTheme keyboardTheme) {
        if (mThemeContext == null || mKeyboardTheme.mThemeId != keyboardTheme.mThemeId) {
        if (mThemeContext == null || !keyboardTheme.equals(mKeyboardTheme)) {
            mKeyboardTheme = keyboardTheme;
            mThemeContext = new ContextThemeWrapper(context, keyboardTheme.mStyleId);
            KeyboardLayoutSet.clearKeyboardCache();
@@ -342,7 +342,8 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
            mKeyboardView.closing();
        }

        updateKeyboardThemeAndContextThemeWrapper(mLatinIME, mKeyboardTheme);
        updateKeyboardThemeAndContextThemeWrapper(
                mLatinIME, KeyboardTheme.getKeyboardTheme(mPrefs));
        mCurrentInputView = (InputView)LayoutInflater.from(mThemeContext).inflate(
                R.layout.input_view, null);
        mMainKeyboardFrame = mCurrentInputView.findViewById(R.id.main_keyboard_frame);
+108 −18
Original line number Diff line number Diff line
@@ -17,34 +17,86 @@
package com.android.inputmethod.keyboard;

import android.content.SharedPreferences;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.util.Log;

import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.settings.Settings;

import java.util.Arrays;
import java.util.Comparator;

public final class KeyboardTheme {
    private static final String TAG = KeyboardTheme.class.getSimpleName();

    public static final int THEME_ID_ICS = 0;
    public static final int THEME_ID_KLP = 2;
    private static final int DEFAULT_THEME_ID = THEME_ID_KLP;
    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;

    private static final KeyboardTheme[] KEYBOARD_THEMES = {
        new KeyboardTheme(THEME_ID_ICS, R.style.KeyboardTheme_ICS),
        new KeyboardTheme(THEME_ID_KLP, R.style.KeyboardTheme_KLP),
        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),
    };
    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;
            }
        });
    }

    public final int mThemeId;
    public final int mStyleId;
    final int mMinApiVersion;

    // Note: The themeId should be aligned with "themeId" attribute of Keyboard style
    // in values/style.xml.
    public KeyboardTheme(final int themeId, final int styleId) {
    // in values/themes-<style>.xml.
    private KeyboardTheme(final int themeId, final int styleId, final int minApiVersion) {
        mThemeId = themeId;
        mStyleId = styleId;
        mMinApiVersion = minApiVersion;
    }

    @Override
    public boolean equals(final Object o) {
        if (o == this) return true;
        return (o instanceof KeyboardTheme) && ((KeyboardTheme)o).mThemeId == mThemeId;
    }

    @Override
    public int hashCode() {
        return mThemeId;
    }

    private static KeyboardTheme searchKeyboardTheme(final int themeId) {
    // 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) {
        // TODO: This search algorithm isn't optimal if there are many themes.
        for (final KeyboardTheme theme : KEYBOARD_THEMES) {
            if (theme.mThemeId == themeId) {
@@ -54,18 +106,57 @@ public final class KeyboardTheme {
        return null;
    }

    public static KeyboardTheme getDefaultKeyboardTheme() {
        return searchKeyboardTheme(DEFAULT_THEME_ID);
    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();
    }

    public static KeyboardTheme getKeyboardTheme(final SharedPreferences prefs) {
        final String themeIdString = prefs.getString(Settings.PREF_KEYBOARD_LAYOUT, null);
        final int sdkVersion = getSdkVersion();
        final String themeIdString = prefs.getString(KEYBOARD_THEME_KEY, null);
        if (themeIdString == null) {
            return getDefaultKeyboardTheme();
            return getDefaultKeyboardTheme(prefs, sdkVersion);
        }
        try {
            final int themeId = Integer.parseInt(themeIdString);
            final KeyboardTheme theme = searchKeyboardTheme(themeId);
            final KeyboardTheme theme = searchKeyboardThemeById(themeId);
            if (theme != null) {
                return theme;
            }
@@ -73,9 +164,8 @@ public final class KeyboardTheme {
        } catch (final NumberFormatException e) {
            Log.w(TAG, "Illegal keyboard theme in preference: " + themeIdString);
        }
        // Reset preference to default value.
        final String defaultThemeIdString = Integer.toString(DEFAULT_THEME_ID);
        prefs.edit().putString(Settings.PREF_KEYBOARD_LAYOUT, defaultThemeIdString).apply();
        return getDefaultKeyboardTheme();
        // Remove preference.
        prefs.edit().remove(KEYBOARD_THEME_KEY).apply();
        return getDefaultKeyboardTheme(prefs, sdkVersion);
    }
}
Loading