From ef34303c7eaa781ed4655016538aac47f0aef002 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Fri, 11 Nov 2022 15:39:28 +0100 Subject: [PATCH] Avoid crashing in direct-boot mode The settings activity can't open in direct-boot mode. The emoji pane opens without the "last used" data. --- AndroidManifest.xml | 2 +- srcs/juloo.keyboard2/EmojiGridView.java | 13 +++++++++---- srcs/juloo.keyboard2/SettingsActivity.java | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 5b2a120..e840c65 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -8,7 +8,7 @@ - + diff --git a/srcs/juloo.keyboard2/EmojiGridView.java b/srcs/juloo.keyboard2/EmojiGridView.java index 581200c..749ce3e 100644 --- a/srcs/juloo.keyboard2/EmojiGridView.java +++ b/srcs/juloo.keyboard2/EmojiGridView.java @@ -83,7 +83,9 @@ public class EmojiGridView extends GridView private void saveLastUsed() { - SharedPreferences.Editor edit = emojiSharedPreferences().edit(); + SharedPreferences.Editor edit; + try { edit = emojiSharedPreferences().edit(); } + catch (Exception _e) { return; } HashSet set = new HashSet(); for (Emoji emoji : _lastUsed.keySet()) set.add(String.valueOf(_lastUsed.get(emoji)) + "-" + emoji.name()); @@ -93,15 +95,18 @@ public class EmojiGridView extends GridView private void loadLastUsed() { - SharedPreferences prefs = emojiSharedPreferences(); - Set lastUseSet = prefs.getStringSet(LAST_USE_PREF, null); _lastUsed = new HashMap(); + SharedPreferences prefs; + // Storage might not be available (eg. the device is locked), avoid + // crashing. + try { prefs = emojiSharedPreferences(); } + catch (Exception _e) { return; } + Set lastUseSet = prefs.getStringSet(LAST_USE_PREF, null); if (lastUseSet != null) for (String emojiData : lastUseSet) { String[] data = emojiData.split("-", 2); Emoji emoji; - if (data.length != 2) continue ; emoji = Emoji.getEmojiByName(data[1]); diff --git a/srcs/juloo.keyboard2/SettingsActivity.java b/srcs/juloo.keyboard2/SettingsActivity.java index c6454a7..04fe501 100644 --- a/srcs/juloo.keyboard2/SettingsActivity.java +++ b/srcs/juloo.keyboard2/SettingsActivity.java @@ -14,9 +14,13 @@ public class SettingsActivity extends PreferenceActivity { detectSystemTheme(); super.onCreate(savedInstanceState); + SharedPreferences prefs; + // The preferences can't be read when in direct-boot mode. Avoid crashing + // and don't allow changing the settings. + try { prefs = PreferenceManager.getDefaultSharedPreferences(this); } + catch (Exception _e) { fallbackEncrypted(); return; } addPreferencesFromResource(R.xml.settings); - PreferenceManager.getDefaultSharedPreferences(this) - .registerOnSharedPreferenceChangeListener(this.new OnPreferencesChange()); + prefs.registerOnSharedPreferenceChangeListener(this.new OnPreferencesChange()); } /** The default theme is [Theme.DeviceDefault], which is dark. Detect if the @@ -31,6 +35,12 @@ public class SettingsActivity extends PreferenceActivity } } + void fallbackEncrypted() + { + // Can't communicate with the user here. + finish(); + } + /** See DirectBootAwarePreferences. */ class OnPreferencesChange implements SharedPreferences.OnSharedPreferenceChangeListener {