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

Unverified Commit 214d2894 authored by Michael W's avatar Michael W Committed by Michael Bestas
Browse files

LatinIME: Add setting for theme to follow system setting

* Add a setting to follow the system design (light/dark)
* Make it default for >= Q
* Rework the handling to differentiate between selected theme
  (by id) and actual theme we use

Change-Id: I380e3540b2437102685aa632c5b0a2f2dff11e1b
parent 56191b3f
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -26,4 +26,6 @@ language among those that use the Latin alphabet. This keyboard is laid out in t
disposition rather than other common dispositions for Latin languages. [CHAR LIMIT=25] -->
disposition rather than other common dispositions for Latin languages. [CHAR LIMIT=25] -->
    <string name="subtype_no_language_bepo">Alphabet (Bépo)</string>
    <string name="subtype_no_language_bepo">Alphabet (Bépo)</string>
    <string name="subtype_hu_ZZ">Hungarian (QWERTY)</string>
    <string name="subtype_hu_ZZ">Hungarian (QWERTY)</string>

    <string name="keyboard_theme_follow_system">Follow system settings</string>
</resources>
</resources>
+2 −0
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@
    <string-array name="keyboard_theme_names" translatable="false">
    <string-array name="keyboard_theme_names" translatable="false">
        <item>@string/keyboard_theme_material_light</item>
        <item>@string/keyboard_theme_material_light</item>
        <item>@string/keyboard_theme_material_dark</item>
        <item>@string/keyboard_theme_material_dark</item>
        <item>@string/keyboard_theme_follow_system</item>
        <item>@string/keyboard_theme_holo_white</item>
        <item>@string/keyboard_theme_holo_white</item>
        <item>@string/keyboard_theme_holo_blue</item>
        <item>@string/keyboard_theme_holo_blue</item>
    </string-array>
    </string-array>
@@ -30,6 +31,7 @@
    <integer-array name="keyboard_theme_ids" translatable="false">
    <integer-array name="keyboard_theme_ids" translatable="false">
        <item>3</item>
        <item>3</item>
        <item>4</item>
        <item>4</item>
        <item>5</item>
        <item>2</item>
        <item>2</item>
        <item>0</item>
        <item>0</item>
    </integer-array>
    </integer-array>
+39 −18
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.inputmethod.keyboard;
package com.android.inputmethod.keyboard;


import android.content.Context;
import android.content.Context;
import android.content.res.Configuration;
import android.content.SharedPreferences;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Build.VERSION_CODES;
@@ -41,7 +42,8 @@ public final class KeyboardTheme implements Comparable<KeyboardTheme> {
    public static final int THEME_ID_KLP = 2;
    public static final int THEME_ID_KLP = 2;
    public static final int THEME_ID_LXX_LIGHT = 3;
    public static final int THEME_ID_LXX_LIGHT = 3;
    public static final int THEME_ID_LXX_DARK = 4;
    public static final int THEME_ID_LXX_DARK = 4;
    public static final int DEFAULT_THEME_ID = THEME_ID_KLP;
    public static final int THEME_ID_AUTO_DARK = 5;
    public static final int DEFAULT_THEME_ID = THEME_ID_AUTO_DARK;


    private static KeyboardTheme[] AVAILABLE_KEYBOARD_THEMES;
    private static KeyboardTheme[] AVAILABLE_KEYBOARD_THEMES;


@@ -59,6 +61,8 @@ public final class KeyboardTheme implements Comparable<KeyboardTheme> {
        new KeyboardTheme(THEME_ID_LXX_DARK, "LXXDark", R.style.KeyboardTheme_LXX_Dark,
        new KeyboardTheme(THEME_ID_LXX_DARK, "LXXDark", R.style.KeyboardTheme_LXX_Dark,
                // This has never been selected as default theme.
                // This has never been selected as default theme.
                VERSION_CODES.BASE),
                VERSION_CODES.BASE),
        new KeyboardTheme(THEME_ID_AUTO_DARK, "AutoDark", 0,
                Build.VERSION_CODES.Q),
    };
    };


    static {
    static {
@@ -167,9 +171,7 @@ public final class KeyboardTheme implements Comparable<KeyboardTheme> {
    }
    }


    public static KeyboardTheme getKeyboardTheme(final Context context) {
    public static KeyboardTheme getKeyboardTheme(final Context context) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return getKeyboardTheme(context, BuildCompatUtils.EFFECTIVE_SDK_INT);
        final KeyboardTheme[] availableThemeArray = getAvailableThemeArray(context);
        return getKeyboardTheme(prefs, BuildCompatUtils.EFFECTIVE_SDK_INT, availableThemeArray);
    }
    }


    /* package private for testing */
    /* package private for testing */
@@ -192,24 +194,43 @@ public final class KeyboardTheme implements Comparable<KeyboardTheme> {
    }
    }


    /* package private for testing */
    /* package private for testing */
    static KeyboardTheme getKeyboardTheme(final SharedPreferences prefs, final int sdkVersion,
    static KeyboardTheme getKeyboardTheme(final Context context, final int sdkVersion) {
            final KeyboardTheme[] availableThemeArray) {
        int themeId = getSelectedKeyboardThemeId(context);
        final String lxxThemeIdString = prefs.getString(LXX_KEYBOARD_THEME_KEY, null);
        if (THEME_ID_AUTO_DARK == themeId) {
        if (lxxThemeIdString == null) {
            Configuration cfg = context.getResources().getConfiguration();
            return getDefaultKeyboardTheme(prefs, sdkVersion, availableThemeArray);
            int nightMode = cfg.uiMode & Configuration.UI_MODE_NIGHT_MASK;
            if (nightMode == Configuration.UI_MODE_NIGHT_YES) {
                themeId = THEME_ID_LXX_DARK;
            } else {
                themeId = THEME_ID_LXX_LIGHT;
            }
            }
        try {
        }
            final int themeId = Integer.parseInt(lxxThemeIdString);
        final KeyboardTheme[] availableThemeArray = getAvailableThemeArray(context);
        final KeyboardTheme theme = searchKeyboardThemeById(themeId, availableThemeArray);
        final KeyboardTheme theme = searchKeyboardThemeById(themeId, availableThemeArray);
        if (theme != null) {
        if (theme != null) {
            return theme;
            return theme;
        }
        }
            Log.w(TAG, "Unknown keyboard theme in LXX preference: " + lxxThemeIdString);
        Log.w(TAG, "Unknown keyboard theme in LXX preference: " + themeId);
        // Remove preference that contains unknown or illegal theme id.
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().remove(LXX_KEYBOARD_THEME_KEY).apply();
        return getDefaultKeyboardTheme(prefs, sdkVersion, availableThemeArray);
    }

    public static int getSelectedKeyboardThemeId(final Context context) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        final KeyboardTheme[] availableThemeArray = getAvailableThemeArray(context);
        final String lxxThemeIdString = prefs.getString(LXX_KEYBOARD_THEME_KEY, null);
        if (lxxThemeIdString != null) {
            try {
                return Integer.parseInt(lxxThemeIdString);
            } catch (final NumberFormatException e) {
            } catch (final NumberFormatException e) {
                Log.w(TAG, "Illegal keyboard theme in LXX preference: " + lxxThemeIdString, e);
                Log.w(TAG, "Illegal keyboard theme in LXX preference: " + lxxThemeIdString, e);
            }
            }
        }
        // Remove preference that contains unknown or illegal theme id.
        // Remove preference that contains unknown or illegal theme id.
        prefs.edit().remove(LXX_KEYBOARD_THEME_KEY).apply();
        prefs.edit().remove(LXX_KEYBOARD_THEME_KEY).apply();
        return getDefaultKeyboardTheme(prefs, sdkVersion, availableThemeArray);
        return getDefaultKeyboardTheme(prefs, BuildCompatUtils.EFFECTIVE_SDK_INT,
                availableThemeArray).mThemeId;
    }
    }
}
}
+3 −4
Original line number Original line Diff line number Diff line
@@ -46,11 +46,11 @@ public final class ThemeSettingsFragment extends SubScreenFragment
    static void updateKeyboardThemeSummary(final Preference pref) {
    static void updateKeyboardThemeSummary(final Preference pref) {
        final Context context = pref.getContext();
        final Context context = pref.getContext();
        final Resources res = context.getResources();
        final Resources res = context.getResources();
        final KeyboardTheme keyboardTheme = KeyboardTheme.getKeyboardTheme(context);
        final int keyboardThemeId = KeyboardTheme.getSelectedKeyboardThemeId(context);
        final String[] keyboardThemeNames = res.getStringArray(R.array.keyboard_theme_names);
        final String[] keyboardThemeNames = res.getStringArray(R.array.keyboard_theme_names);
        final int[] keyboardThemeIds = res.getIntArray(R.array.keyboard_theme_ids);
        final int[] keyboardThemeIds = res.getIntArray(R.array.keyboard_theme_ids);
        for (int index = 0; index < keyboardThemeNames.length; index++) {
        for (int index = 0; index < keyboardThemeNames.length; index++) {
            if (keyboardTheme.mThemeId == keyboardThemeIds[index]) {
            if (keyboardThemeId == keyboardThemeIds[index]) {
                pref.setSummary(keyboardThemeNames[index]);
                pref.setSummary(keyboardThemeNames[index]);
                return;
                return;
            }
            }
@@ -72,8 +72,7 @@ public final class ThemeSettingsFragment extends SubScreenFragment
            screen.addPreference(pref);
            screen.addPreference(pref);
            pref.setOnRadioButtonClickedListener(this);
            pref.setOnRadioButtonClickedListener(this);
        }
        }
        final KeyboardTheme keyboardTheme = KeyboardTheme.getKeyboardTheme(context);
        mSelectedThemeId = KeyboardTheme.getSelectedKeyboardThemeId(context);
        mSelectedThemeId = keyboardTheme.mThemeId;
    }
    }


    @Override
    @Override