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.
This commit is contained in:
Jules Aguillon 2022-11-11 14:27:02 +01:00
parent 29fbb27a8a
commit f1ce6abe5a
5 changed files with 148 additions and 47 deletions

View File

@ -5,7 +5,6 @@ import android.content.res.Resources;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.KeyEvent; import android.view.KeyEvent;
@ -15,6 +14,8 @@ import java.util.HashSet;
final class Config final class Config
{ {
private final SharedPreferences _prefs;
// From resources // From resources
public final float marginTop; public final float marginTop;
public final float keyPadding; public final float keyPadding;
@ -53,9 +54,9 @@ final class Config
public final IKeyEventHandler handler; 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 // static values
marginTop = res.getDimension(R.dimen.margin_top); marginTop = res.getDimension(R.dimen.margin_top);
keyPadding = res.getDimension(R.dimen.key_padding); keyPadding = res.getDimension(R.dimen.key_padding);
@ -76,7 +77,7 @@ final class Config
characterSize = 1.f; characterSize = 1.f;
accents = 1; accents = 1;
// from prefs // from prefs
refresh(context); refresh(res);
// initialized later // initialized later
shouldOfferSwitchingToNextInputMethod = false; shouldOfferSwitchingToNextInputMethod = false;
shouldOfferSwitchingToProgramming = false; shouldOfferSwitchingToProgramming = false;
@ -90,10 +91,8 @@ final class Config
/* /*
** Reload prefs ** 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(); DisplayMetrics dm = res.getDisplayMetrics();
// The height of the keyboard is relative to the height of the screen. // 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. // 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 // Scale some dimensions depending on orientation
float horizontalIntervalScale = 1.f; float horizontalIntervalScale = 1.f;
float characterSizeScale = 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); show_numpad = "always".equals(show_numpad_s);
if (res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) // Landscape mode if (res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) // Landscape mode
{ {
if ("landscape".equals(show_numpad_s)) if ("landscape".equals(show_numpad_s))
show_numpad = true; show_numpad = true;
keyboardHeightPercent = prefs.getInt("keyboard_height_landscape", 50); keyboardHeightPercent = _prefs.getInt("keyboard_height_landscape", 50);
horizontalIntervalScale = 2.f; horizontalIntervalScale = 2.f;
characterSizeScale = 1.25f; characterSizeScale = 1.25f;
} }
else 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); 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); 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 // The swipe distance is defined relatively to the "exact physical pixels
// per inch of the screen", which isn't affected by the scaling settings. // 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. // Take the mean of both dimensions as an approximation of the diagonal.
float physical_scaling = (dm.widthPixels + dm.heightPixels) / (dm.xdpi + dm.ydpi); float physical_scaling = (dm.widthPixels + dm.heightPixels) / (dm.xdpi + dm.ydpi);
swipe_dist_px = Float.valueOf(prefs.getString("swipe_dist", "15")) * physical_scaling;; swipe_dist_px = Float.valueOf(_prefs.getString("swipe_dist", "15")) * physical_scaling;;
vibrateEnabled = prefs.getBoolean("vibrate_enabled", vibrateEnabled); vibrateEnabled = _prefs.getBoolean("vibrate_enabled", vibrateEnabled);
longPressTimeout = prefs.getInt("longpress_timeout", (int)longPressTimeout); longPressTimeout = _prefs.getInt("longpress_timeout", (int)longPressTimeout);
longPressInterval = prefs.getInt("longpress_interval", (int)longPressInterval); longPressInterval = _prefs.getInt("longpress_interval", (int)longPressInterval);
marginBottom = getDipPref(dm, prefs, "margin_bottom", marginBottom); marginBottom = getDipPref(dm, _prefs, "margin_bottom", marginBottom);
keyVerticalInterval = getDipPref(dm, prefs, "key_vertical_space", keyVerticalInterval); keyVerticalInterval = getDipPref(dm, _prefs, "key_vertical_space", keyVerticalInterval);
keyHorizontalInterval = keyHorizontalInterval =
getDipPref(dm, prefs, "key_horizontal_space", keyHorizontalInterval) getDipPref(dm, _prefs, "key_horizontal_space", keyHorizontalInterval)
* horizontalIntervalScale; * horizontalIntervalScale;
// Do not substract keyVerticalInterval from keyHeight because this is done // Do not substract keyVerticalInterval from keyHeight because this is done
// during rendered. // during rendered.
keyHeight = dm.heightPixels * keyboardHeightPercent / 100 / 4; keyHeight = dm.heightPixels * keyboardHeightPercent / 100 / 4;
horizontalMargin = horizontalMargin =
getDipPref(dm, prefs, "horizontal_margin", horizontalMargin) getDipPref(dm, _prefs, "horizontal_margin", horizontalMargin)
+ res.getDimension(R.dimen.extra_horizontal_margin); + res.getDimension(R.dimen.extra_horizontal_margin);
preciseRepeat = prefs.getBoolean("precise_repeat", preciseRepeat); preciseRepeat = _prefs.getBoolean("precise_repeat", preciseRepeat);
double_tap_lock_shift = prefs.getBoolean("lock_double_tap", false); double_tap_lock_shift = _prefs.getBoolean("lock_double_tap", false);
characterSize = characterSize =
prefs.getFloat("character_size", characterSize) _prefs.getFloat("character_size", characterSize)
* characterSizeScale; * characterSizeScale;
accents = Integer.valueOf(prefs.getString("accents", "1")); accents = Integer.valueOf(_prefs.getString("accents", "1"));
theme = getThemeId(res, prefs.getString("theme", "")); theme = getThemeId(res, _prefs.getString("theme", ""));
autocapitalisation = prefs.getBoolean("autocapitalisation", true); autocapitalisation = _prefs.getBoolean("autocapitalisation", true);
extra_keys_param = ExtraKeyCheckBoxPreference.get_extra_keys(prefs); extra_keys_param = ExtraKeyCheckBoxPreference.get_extra_keys(_prefs);
} }
/** Update the layout according to the configuration. /** Update the layout according to the configuration.
@ -277,9 +276,10 @@ final class Config
private static Config _globalConfig = null; 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() public static Config globalConfig()

View File

@ -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<String, ?> 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<String>)v);
}
e.commit();
}
}

View File

@ -3,7 +3,6 @@ package juloo.keyboard2;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
@ -84,9 +83,8 @@ public class EmojiGridView extends GridView
private void saveLastUsed() private void saveLastUsed()
{ {
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(getContext()).edit(); SharedPreferences.Editor edit = emojiSharedPreferences().edit();
HashSet<String> set = new HashSet<String>(); HashSet<String> set = new HashSet<String>();
for (Emoji emoji : _lastUsed.keySet()) for (Emoji emoji : _lastUsed.keySet())
set.add(String.valueOf(_lastUsed.get(emoji)) + "-" + emoji.name()); set.add(String.valueOf(_lastUsed.get(emoji)) + "-" + emoji.name());
edit.putStringSet(LAST_USE_PREF, set); edit.putStringSet(LAST_USE_PREF, set);
@ -95,9 +93,8 @@ public class EmojiGridView extends GridView
private void loadLastUsed() private void loadLastUsed()
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences prefs = emojiSharedPreferences();
Set<String> lastUseSet = prefs.getStringSet(LAST_USE_PREF, null); Set<String> lastUseSet = prefs.getStringSet(LAST_USE_PREF, null);
_lastUsed = new HashMap<Emoji, Integer>(); _lastUsed = new HashMap<Emoji, Integer>();
if (lastUseSet != null) if (lastUseSet != null)
for (String emojiData : lastUseSet) 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 private static class EmojiView extends TextView
{ {
public EmojiView(Context context) public EmojiView(Context context)

View File

@ -6,23 +6,22 @@ import android.content.SharedPreferences;
import android.inputmethodservice.InputMethodService; import android.inputmethodservice.InputMethodService;
import android.os.Build.VERSION; import android.os.Build.VERSION;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceManager;
import android.text.InputType; import android.text.InputType;
import android.util.Log;
import android.util.LogPrinter;
import android.view.ContextThemeWrapper; 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.EditorInfo;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype; 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.Arrays;
import java.util.List;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
public class Keyboard2 extends InputMethodService public class Keyboard2 extends InputMethodService
@ -49,11 +48,10 @@ public class Keyboard2 extends InputMethodService
public void onCreate() public void onCreate()
{ {
super.onCreate(); super.onCreate();
PreferenceManager.setDefaultValues(this, R.xml.settings, false); SharedPreferences prefs = DirectBootAwarePreferences.get_shared_preferences(this);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); prefs.registerOnSharedPreferenceChangeListener(this);
Config.initGlobalConfig(this, new KeyEventHandler(this.new Receiver())); Config.initGlobalConfig(prefs, getResources(), new KeyEventHandler(this.new Receiver()));
_config = Config.globalConfig(); _config = Config.globalConfig();
_config.refresh(this);
_keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard); _keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard);
_keyboardView.reset(); _keyboardView.reset();
_debug_logs = getResources().getBoolean(R.bool.debug_logs); _debug_logs = getResources().getBoolean(R.bool.debug_logs);
@ -205,7 +203,7 @@ public class Keyboard2 extends InputMethodService
private void refreshConfig() private void refreshConfig()
{ {
int prev_theme = _config.theme; int prev_theme = _config.theme;
_config.refresh(this); _config.refresh(getResources());
refreshSubtypeImm(); refreshSubtypeImm();
// Refreshing the theme config requires re-creating the views // Refreshing the theme config requires re-creating the views
if (prev_theme != _config.theme) if (prev_theme != _config.theme)

View File

@ -1,9 +1,11 @@
package juloo.keyboard2; package juloo.keyboard2;
import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
public class SettingsActivity extends PreferenceActivity public class SettingsActivity extends PreferenceActivity
{ {
@ -13,6 +15,8 @@ public class SettingsActivity extends PreferenceActivity
detectSystemTheme(); detectSystemTheme();
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings); addPreferencesFromResource(R.xml.settings);
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this.new OnPreferencesChange());
} }
/** The default theme is [Theme.DeviceDefault], which is dark. Detect if the /** 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); 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);
}
}
} }