From f1ce6abe5a37c9ca57d885ecb9a3218969bc7fdf Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Fri, 11 Nov 2022 14:27:02 +0100 Subject: [PATCH] Direct-boot aware preferences Store preferences in device protected storage, which is available before the device is unlocked. The keyboard was crashing when trying to access the encrypted preferences. The emoji pane uses a separate preferences file, the old data is lost. The SettingsActivity can't easily use the new preferences storage. Instead, it continues to use the "default" preferences store, which is copied back to the protected storage when needed. --- srcs/juloo.keyboard2/Config.java | 58 ++++++------- .../DirectBootAwarePreferences.java | 86 +++++++++++++++++++ srcs/juloo.keyboard2/EmojiGridView.java | 12 +-- srcs/juloo.keyboard2/Keyboard2.java | 24 +++--- srcs/juloo.keyboard2/SettingsActivity.java | 15 ++++ 5 files changed, 148 insertions(+), 47 deletions(-) create mode 100644 srcs/juloo.keyboard2/DirectBootAwarePreferences.java diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 17acb9f..76415b7 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -5,7 +5,6 @@ import android.content.res.Resources; import android.content.res.Configuration; import android.content.SharedPreferences; import android.os.Build; -import android.preference.PreferenceManager; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.KeyEvent; @@ -15,6 +14,8 @@ import java.util.HashSet; final class Config { + private final SharedPreferences _prefs; + // From resources public final float marginTop; public final float keyPadding; @@ -53,9 +54,9 @@ final class Config public final IKeyEventHandler handler; - private Config(Context context, IKeyEventHandler h) + private Config(SharedPreferences prefs, Resources res, IKeyEventHandler h) { - Resources res = context.getResources(); + _prefs = prefs; // static values marginTop = res.getDimension(R.dimen.margin_top); keyPadding = res.getDimension(R.dimen.key_padding); @@ -76,7 +77,7 @@ final class Config characterSize = 1.f; accents = 1; // from prefs - refresh(context); + refresh(res); // initialized later shouldOfferSwitchingToNextInputMethod = false; shouldOfferSwitchingToProgramming = false; @@ -90,10 +91,8 @@ final class Config /* ** Reload prefs */ - public void refresh(Context context) + public void refresh(Resources res) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - Resources res = context.getResources(); DisplayMetrics dm = res.getDisplayMetrics(); // The height of the keyboard is relative to the height of the screen. // This is the height of the keyboard if it have 4 rows. @@ -101,52 +100,52 @@ final class Config // Scale some dimensions depending on orientation float horizontalIntervalScale = 1.f; float characterSizeScale = 1.f; - String show_numpad_s = prefs.getString("show_numpad", "never"); + String show_numpad_s = _prefs.getString("show_numpad", "never"); show_numpad = "always".equals(show_numpad_s); if (res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) // Landscape mode { if ("landscape".equals(show_numpad_s)) show_numpad = true; - keyboardHeightPercent = prefs.getInt("keyboard_height_landscape", 50); + keyboardHeightPercent = _prefs.getInt("keyboard_height_landscape", 50); horizontalIntervalScale = 2.f; characterSizeScale = 1.25f; } else { - keyboardHeightPercent = prefs.getInt("keyboard_height", 35); + keyboardHeightPercent = _prefs.getInt("keyboard_height", 35); } - String layout_s = prefs.getString("layout", "system"); + String layout_s = _prefs.getString("layout", "system"); layout = layout_s.equals("system") ? -1 : layoutId_of_string(layout_s); - String prog_layout_s = prefs.getString("programming_layout", "none"); + String prog_layout_s = _prefs.getString("programming_layout", "none"); programming_layout = prog_layout_s.equals("none") ? -1 : layoutId_of_string(prog_layout_s); // The swipe distance is defined relatively to the "exact physical pixels // per inch of the screen", which isn't affected by the scaling settings. // Take the mean of both dimensions as an approximation of the diagonal. float physical_scaling = (dm.widthPixels + dm.heightPixels) / (dm.xdpi + dm.ydpi); - swipe_dist_px = Float.valueOf(prefs.getString("swipe_dist", "15")) * physical_scaling;; - vibrateEnabled = prefs.getBoolean("vibrate_enabled", vibrateEnabled); - longPressTimeout = prefs.getInt("longpress_timeout", (int)longPressTimeout); - longPressInterval = prefs.getInt("longpress_interval", (int)longPressInterval); - marginBottom = getDipPref(dm, prefs, "margin_bottom", marginBottom); - keyVerticalInterval = getDipPref(dm, prefs, "key_vertical_space", keyVerticalInterval); + swipe_dist_px = Float.valueOf(_prefs.getString("swipe_dist", "15")) * physical_scaling;; + vibrateEnabled = _prefs.getBoolean("vibrate_enabled", vibrateEnabled); + longPressTimeout = _prefs.getInt("longpress_timeout", (int)longPressTimeout); + longPressInterval = _prefs.getInt("longpress_interval", (int)longPressInterval); + marginBottom = getDipPref(dm, _prefs, "margin_bottom", marginBottom); + keyVerticalInterval = getDipPref(dm, _prefs, "key_vertical_space", keyVerticalInterval); keyHorizontalInterval = - getDipPref(dm, prefs, "key_horizontal_space", keyHorizontalInterval) + getDipPref(dm, _prefs, "key_horizontal_space", keyHorizontalInterval) * horizontalIntervalScale; // Do not substract keyVerticalInterval from keyHeight because this is done // during rendered. keyHeight = dm.heightPixels * keyboardHeightPercent / 100 / 4; horizontalMargin = - getDipPref(dm, prefs, "horizontal_margin", horizontalMargin) + getDipPref(dm, _prefs, "horizontal_margin", horizontalMargin) + res.getDimension(R.dimen.extra_horizontal_margin); - preciseRepeat = prefs.getBoolean("precise_repeat", preciseRepeat); - double_tap_lock_shift = prefs.getBoolean("lock_double_tap", false); + preciseRepeat = _prefs.getBoolean("precise_repeat", preciseRepeat); + double_tap_lock_shift = _prefs.getBoolean("lock_double_tap", false); characterSize = - prefs.getFloat("character_size", characterSize) + _prefs.getFloat("character_size", characterSize) * characterSizeScale; - accents = Integer.valueOf(prefs.getString("accents", "1")); - theme = getThemeId(res, prefs.getString("theme", "")); - autocapitalisation = prefs.getBoolean("autocapitalisation", true); - extra_keys_param = ExtraKeyCheckBoxPreference.get_extra_keys(prefs); + accents = Integer.valueOf(_prefs.getString("accents", "1")); + theme = getThemeId(res, _prefs.getString("theme", "")); + autocapitalisation = _prefs.getBoolean("autocapitalisation", true); + extra_keys_param = ExtraKeyCheckBoxPreference.get_extra_keys(_prefs); } /** Update the layout according to the configuration. @@ -277,9 +276,10 @@ final class Config private static Config _globalConfig = null; - public static void initGlobalConfig(Context context, IKeyEventHandler handler) + public static void initGlobalConfig(SharedPreferences prefs, Resources res, + IKeyEventHandler handler) { - _globalConfig = new Config(context, handler); + _globalConfig = new Config(prefs, res, handler); } public static Config globalConfig() diff --git a/srcs/juloo.keyboard2/DirectBootAwarePreferences.java b/srcs/juloo.keyboard2/DirectBootAwarePreferences.java new file mode 100644 index 0000000..372e159 --- /dev/null +++ b/srcs/juloo.keyboard2/DirectBootAwarePreferences.java @@ -0,0 +1,86 @@ +package juloo.keyboard2; + +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Build.VERSION; +import android.preference.PreferenceManager; +import java.util.Map; +import java.util.Set; + +public final class DirectBootAwarePreferences +{ + /* On API >= 24, preferences are read from the device protected storage. This + * storage is less protected than the default, no personnal or sensitive + * information is stored there (only the keyboard settings). This storage is + * accessible during boot and allow the keyboard to read its settings and + * allow typing the storage password. */ + public static SharedPreferences get_shared_preferences(Context context) + { + if (VERSION.SDK_INT < 24) + return PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences prefs = get_protected_prefs(context); + check_need_migration(context, prefs); + return prefs; + } + + /* Copy shared preferences to device protected storage. Not using + * [Context.moveSharedPreferencesFrom] because the settings activity still + * use [PreferenceActivity], which can't work on a non-default shared + * preference file. */ + public static void copy_preferences_to_protected_storage(Context context, + SharedPreferences src) + { + if (VERSION.SDK_INT >= 24) + copy_shared_preferences(src, get_protected_prefs(context)); + } + + static SharedPreferences get_protected_prefs(Context context) + { + String pref_name = + PreferenceManager.getDefaultSharedPreferencesName(context); + return context.createDeviceProtectedStorageContext() + .getSharedPreferences(pref_name, Context.MODE_PRIVATE); + } + + static void check_need_migration(Context app_context, + SharedPreferences protected_prefs) + { + if (!protected_prefs.getBoolean("need_migration", true)) + return; + SharedPreferences prefs; + try + { + prefs = PreferenceManager.getDefaultSharedPreferences(app_context); + } + catch (Exception e) + { + // Device is locked, migrate later. + return; + } + prefs.edit().putBoolean("need_migration", false).commit(); + copy_shared_preferences(prefs, protected_prefs); + } + + static void copy_shared_preferences(SharedPreferences src, SharedPreferences dst) + { + SharedPreferences.Editor e = dst.edit(); + Map entries = src.getAll(); + for (String k : entries.keySet()) + { + Object v = entries.get(k); + if (v instanceof Boolean) + e.putBoolean(k, (Boolean)v); + else if (v instanceof Float) + e.putFloat(k, (Float)v); + else if (v instanceof Integer) + e.putInt(k, (Integer)v); + else if (v instanceof Long) + e.putLong(k, (Long)v); + else if (v instanceof String) + e.putString(k, (String)v); + else if (v instanceof Set) + e.putStringSet(k, (Set)v); + } + e.commit(); + } +} diff --git a/srcs/juloo.keyboard2/EmojiGridView.java b/srcs/juloo.keyboard2/EmojiGridView.java index 224099c..581200c 100644 --- a/srcs/juloo.keyboard2/EmojiGridView.java +++ b/srcs/juloo.keyboard2/EmojiGridView.java @@ -3,7 +3,6 @@ package juloo.keyboard2; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Typeface; -import android.preference.PreferenceManager; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; @@ -84,9 +83,8 @@ public class EmojiGridView extends GridView private void saveLastUsed() { - SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(getContext()).edit(); + SharedPreferences.Editor edit = emojiSharedPreferences().edit(); HashSet set = new HashSet(); - for (Emoji emoji : _lastUsed.keySet()) set.add(String.valueOf(_lastUsed.get(emoji)) + "-" + emoji.name()); edit.putStringSet(LAST_USE_PREF, set); @@ -95,9 +93,8 @@ public class EmojiGridView extends GridView private void loadLastUsed() { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences prefs = emojiSharedPreferences(); Set lastUseSet = prefs.getStringSet(LAST_USE_PREF, null); - _lastUsed = new HashMap(); if (lastUseSet != null) for (String emojiData : lastUseSet) @@ -114,6 +111,11 @@ public class EmojiGridView extends GridView } } + SharedPreferences emojiSharedPreferences() + { + return getContext().getSharedPreferences("emoji_last_use", Context.MODE_PRIVATE); + } + private static class EmojiView extends TextView { public EmojiView(Context context) diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index 192e54d..7bf9812 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -6,23 +6,22 @@ import android.content.SharedPreferences; import android.inputmethodservice.InputMethodService; import android.os.Build.VERSION; import android.os.IBinder; -import android.preference.PreferenceManager; import android.text.InputType; +import android.util.Log; +import android.util.LogPrinter; import android.view.ContextThemeWrapper; +import android.view.KeyEvent; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewParent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodSubtype; -import android.view.KeyEvent; -import android.view.View; -import android.view.ViewGroup; -import android.view.ViewParent; -import android.util.Log; -import android.util.LogPrinter; import java.util.Arrays; -import java.util.List; import java.util.HashSet; +import java.util.List; import java.util.Set; public class Keyboard2 extends InputMethodService @@ -49,11 +48,10 @@ public class Keyboard2 extends InputMethodService public void onCreate() { super.onCreate(); - PreferenceManager.setDefaultValues(this, R.xml.settings, false); - PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); - Config.initGlobalConfig(this, new KeyEventHandler(this.new Receiver())); + SharedPreferences prefs = DirectBootAwarePreferences.get_shared_preferences(this); + prefs.registerOnSharedPreferenceChangeListener(this); + Config.initGlobalConfig(prefs, getResources(), new KeyEventHandler(this.new Receiver())); _config = Config.globalConfig(); - _config.refresh(this); _keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard); _keyboardView.reset(); _debug_logs = getResources().getBoolean(R.bool.debug_logs); @@ -205,7 +203,7 @@ public class Keyboard2 extends InputMethodService private void refreshConfig() { int prev_theme = _config.theme; - _config.refresh(this); + _config.refresh(getResources()); refreshSubtypeImm(); // Refreshing the theme config requires re-creating the views if (prev_theme != _config.theme) diff --git a/srcs/juloo.keyboard2/SettingsActivity.java b/srcs/juloo.keyboard2/SettingsActivity.java index 98cd590..c6454a7 100644 --- a/srcs/juloo.keyboard2/SettingsActivity.java +++ b/srcs/juloo.keyboard2/SettingsActivity.java @@ -1,9 +1,11 @@ package juloo.keyboard2; +import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceActivity; +import android.preference.PreferenceManager; public class SettingsActivity extends PreferenceActivity { @@ -13,6 +15,8 @@ public class SettingsActivity extends PreferenceActivity detectSystemTheme(); super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings); + PreferenceManager.getDefaultSharedPreferences(this) + .registerOnSharedPreferenceChangeListener(this.new OnPreferencesChange()); } /** The default theme is [Theme.DeviceDefault], which is dark. Detect if the @@ -26,4 +30,15 @@ public class SettingsActivity extends PreferenceActivity setTheme(android.R.style.Theme_DeviceDefault_Light); } } + + /** See DirectBootAwarePreferences. */ + class OnPreferencesChange implements SharedPreferences.OnSharedPreferenceChangeListener + { + @Override + public void onSharedPreferenceChanged(SharedPreferences prefs, String _key) + { + DirectBootAwarePreferences + .copy_preferences_to_protected_storage(SettingsActivity.this, prefs); + } + } }