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
diff --git a/java/res/values/cm_strings.xml b/java/res/values/cm_strings.xml
index 513dbd5..fd14ce9 100644
--- a/java/res/values/cm_strings.xml
+++ b/java/res/values/cm_strings.xml
@@ -26,4 +26,6 @@
 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_hu_ZZ">Hungarian (QWERTY)</string>
+
+    <string name="keyboard_theme_follow_system">Follow system settings</string>
 </resources>
diff --git a/java/res/values/keyboard-themes.xml b/java/res/values/keyboard-themes.xml
index 26b2582..2826f5f 100644
--- a/java/res/values/keyboard-themes.xml
+++ b/java/res/values/keyboard-themes.xml
@@ -22,6 +22,7 @@
     <string-array name="keyboard_theme_names" translatable="false">
         <item>@string/keyboard_theme_material_light</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_blue</item>
     </string-array>
@@ -30,6 +31,7 @@
     <integer-array name="keyboard_theme_ids" translatable="false">
         <item>3</item>
         <item>4</item>
+        <item>5</item>
         <item>2</item>
         <item>0</item>
     </integer-array>
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java b/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java
index 006d086..2796c2e 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java
@@ -17,6 +17,7 @@
 package com.android.inputmethod.keyboard;
 
 import android.content.Context;
+import android.content.res.Configuration;
 import android.content.SharedPreferences;
 import android.os.Build;
 import android.os.Build.VERSION_CODES;
@@ -41,7 +42,8 @@
     public static final int THEME_ID_KLP = 2;
     public static final int THEME_ID_LXX_LIGHT = 3;
     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;
 
@@ -59,6 +61,8 @@
         new KeyboardTheme(THEME_ID_LXX_DARK, "LXXDark", R.style.KeyboardTheme_LXX_Dark,
                 // This has never been selected as default theme.
                 VERSION_CODES.BASE),
+        new KeyboardTheme(THEME_ID_AUTO_DARK, "AutoDark", 0,
+                Build.VERSION_CODES.Q),
     };
 
     static {
@@ -167,9 +171,7 @@
     }
 
     public static KeyboardTheme getKeyboardTheme(final Context context) {
-        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
-        final KeyboardTheme[] availableThemeArray = getAvailableThemeArray(context);
-        return getKeyboardTheme(prefs, BuildCompatUtils.EFFECTIVE_SDK_INT, availableThemeArray);
+        return getKeyboardTheme(context, BuildCompatUtils.EFFECTIVE_SDK_INT);
     }
 
     /* package private for testing */
@@ -192,24 +194,43 @@
     }
 
     /* package private for testing */
-    static KeyboardTheme getKeyboardTheme(final SharedPreferences prefs, final int sdkVersion,
-            final KeyboardTheme[] availableThemeArray) {
-        final String lxxThemeIdString = prefs.getString(LXX_KEYBOARD_THEME_KEY, null);
-        if (lxxThemeIdString == null) {
-            return getDefaultKeyboardTheme(prefs, sdkVersion, availableThemeArray);
-        }
-        try {
-            final int themeId = Integer.parseInt(lxxThemeIdString);
-            final KeyboardTheme theme = searchKeyboardThemeById(themeId, availableThemeArray);
-            if (theme != null) {
-                return theme;
+    static KeyboardTheme getKeyboardTheme(final Context context, final int sdkVersion) {
+        int themeId = getSelectedKeyboardThemeId(context);
+        if (THEME_ID_AUTO_DARK == themeId) {
+            Configuration cfg = context.getResources().getConfiguration();
+            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;
             }
-            Log.w(TAG, "Unknown keyboard theme in LXX preference: " + lxxThemeIdString);
-        } catch (final NumberFormatException e) {
-            Log.w(TAG, "Illegal keyboard theme in LXX preference: " + lxxThemeIdString, e);
+        }
+        final KeyboardTheme[] availableThemeArray = getAvailableThemeArray(context);
+        final KeyboardTheme theme = searchKeyboardThemeById(themeId, availableThemeArray);
+        if (theme != null) {
+            return theme;
+        }
+        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) {
+                Log.w(TAG, "Illegal keyboard theme in LXX preference: " + lxxThemeIdString, e);
+            }
         }
         // Remove preference that contains unknown or illegal theme id.
         prefs.edit().remove(LXX_KEYBOARD_THEME_KEY).apply();
-        return getDefaultKeyboardTheme(prefs, sdkVersion, availableThemeArray);
+        return getDefaultKeyboardTheme(prefs, BuildCompatUtils.EFFECTIVE_SDK_INT,
+                availableThemeArray).mThemeId;
     }
 }
diff --git a/java/src/com/android/inputmethod/latin/settings/ThemeSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/ThemeSettingsFragment.java
index 29289ae..437209b 100644
--- a/java/src/com/android/inputmethod/latin/settings/ThemeSettingsFragment.java
+++ b/java/src/com/android/inputmethod/latin/settings/ThemeSettingsFragment.java
@@ -46,11 +46,11 @@
     static void updateKeyboardThemeSummary(final Preference pref) {
         final Context context = pref.getContext();
         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 int[] keyboardThemeIds = res.getIntArray(R.array.keyboard_theme_ids);
         for (int index = 0; index < keyboardThemeNames.length; index++) {
-            if (keyboardTheme.mThemeId == keyboardThemeIds[index]) {
+            if (keyboardThemeId == keyboardThemeIds[index]) {
                 pref.setSummary(keyboardThemeNames[index]);
                 return;
             }
@@ -72,8 +72,7 @@
             screen.addPreference(pref);
             pref.setOnRadioButtonClickedListener(this);
         }
-        final KeyboardTheme keyboardTheme = KeyboardTheme.getKeyboardTheme(context);
-        mSelectedThemeId = keyboardTheme.mThemeId;
+        mSelectedThemeId = KeyboardTheme.getSelectedKeyboardThemeId(context);
     }
 
     @Override