Harmony
Harmony copied to clipboard
[CRITICAL BUG] Harmony crashes when trying to load Shared Prefs before user encrypted storage is available
I need to be able to load the shared prefs before the user unlocks the device for the first time. To do this, all you usually need to do is use the "device protected" storage context when creating the sharedPrefs:
try {
return EncryptedHarmony.getSharedPreferences(
context.createDeviceProtectedStorageContext(), // Use device protected storage context so that it works before unlocking
PrefKeys.SHARED_PREFERENCES_FILE_NAME,
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
} catch (GeneralSecurityException e) {
Utils.logError(TAG, "GeneralSecurityException: " + e);
} catch (IOException e) {
Utils.logError(TAG, "IOException: " + e);
}
return null;
Moving it to device encrypted storage after being created in the regular context does not seem to work either (may be a separate issue):
final Context deviceContext = context.createDeviceProtectedStorageContext();
// This will come back as true if already moved (prefs wasn't found in the regular context)
if (deviceContext.moveSharedPreferencesFrom(context, PrefKeys.SHARED_PREFERENCES_FILE_NAME)) {
Utils.logDebug(TAG, "Migrated shared preferences to device protected storage, or was already there.");
} else {
Utils.logWarn(TAG, "Failed to migrate shared preferences.");
}
Stack trace of crash below:
FATAL EXCEPTION: main
Process: com.teqtic.lockmeout:NotificationHidingService, PID: 4024
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:590)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
Caused by: java.util.concurrent.ExecutionException: java.io.IOException: Unable to create harmony prefs directories
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:191)
at com.frybits.harmony.HarmonyImpl.awaitForLoad(Harmony.kt:266)
at com.frybits.harmony.HarmonyImpl.edit(Harmony.kt:242)
at androidx.security.crypto.SecureHarmonyPreferencesImpl.edit(SecureHarmonyPreferences.kt:194)
at com.teqtic.lockmeout.services.NotificationHidingService.onListenerConnected(NotificationHidingService.java:245)
at android.service.notification.NotificationListenerService$MyHandler.handleMessage(NotificationListenerService.java:2418)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loopOnce(Looper.java:232)
at android.os.Looper.loop(Looper.java:317)
at android.app.ActivityThread.main(ActivityThread.java:8592)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
Caused by: java.io.IOException: Unable to create harmony prefs directories
at com.frybits.harmony.HarmonyImpl.checkForRequiredFiles(Harmony.kt:273)
at com.frybits.harmony.HarmonyImpl.initialLoad(Harmony.kt:298)
at com.frybits.harmony.HarmonyImpl.isLoadedTask$lambda$4(Harmony.kt:171)
at com.frybits.harmony.HarmonyImpl.$r8$lambda$w7RcXw4Qa0Sdpj8Zeizyl--EULA(Unknown Source:0)
at com.frybits.harmony.HarmonyImpl$$ExternalSyntheticLambda6.call(Unknown Source:2)
at java.util.concurrent.FutureTask.run(FutureTask.java:264)
at android.os.Handler.handleCallback(Handler.java:959)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loopOnce(Looper.java:232)
at android.os.Looper.loop(Looper.java:317)
at android.os.HandlerThread.run(HandlerThread.java:85)