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.
This commit is contained in:
Jules Aguillon 2022-11-11 15:39:28 +01:00
parent f1ce6abe5a
commit ef34303c7e
3 changed files with 22 additions and 7 deletions

View File

@ -8,7 +8,7 @@
</intent-filter>
<meta-data android:name="android.view.im" android:resource="@xml/method"/>
</service>
<activity android:name="juloo.keyboard2.SettingsActivity" android:icon="@drawable/ic_launcher" android:label="@string/settings_activity_label" android:theme="@style/android:Theme.DeviceDefault" android:exported="true">
<activity android:name="juloo.keyboard2.SettingsActivity" android:icon="@drawable/ic_launcher" android:label="@string/settings_activity_label" android:theme="@style/android:Theme.DeviceDefault" android:exported="true" android:directBootAware="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>

View File

@ -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<String> set = new HashSet<String>();
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<String> lastUseSet = prefs.getStringSet(LAST_USE_PREF, null);
_lastUsed = new HashMap<Emoji, Integer>();
SharedPreferences prefs;
// Storage might not be available (eg. the device is locked), avoid
// crashing.
try { prefs = emojiSharedPreferences(); }
catch (Exception _e) { return; }
Set<String> 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]);

View File

@ -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
{