mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-04-20 14:19:08 +00:00
add a ktx shortcut for default shared preferences
This commit is contained in:
parent
125a483591
commit
b31d6d0051
6 changed files with 16 additions and 15 deletions
|
@ -16,6 +16,7 @@ import helium314.keyboard.latin.utils.getCustomLayoutFile
|
|||
import helium314.keyboard.latin.utils.getCustomLayoutFiles
|
||||
import helium314.keyboard.latin.utils.onCustomLayoutFileListChanged
|
||||
import helium314.keyboard.latin.utils.prefs
|
||||
import helium314.keyboard.latin.utils.protectedPrefs
|
||||
import helium314.keyboard.latin.utils.upgradeToolbarPrefs
|
||||
import java.io.File
|
||||
|
||||
|
@ -233,8 +234,8 @@ private fun upgradesWhenComingFromOldAppName(context: Context) {
|
|||
// move pinned clips to credential protected storage if device is not locked (should never happen)
|
||||
if (!prefs.contains(Settings.PREF_PINNED_CLIPS)) return
|
||||
try {
|
||||
val defaultPrefs = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
defaultPrefs.edit { putString(Settings.PREF_PINNED_CLIPS, prefs.getString(Settings.PREF_PINNED_CLIPS, "")) }
|
||||
val defaultProtectedPrefs = context.protectedPrefs()
|
||||
defaultProtectedPrefs.edit { putString(Settings.PREF_PINNED_CLIPS, prefs.getString(Settings.PREF_PINNED_CLIPS, "")) }
|
||||
prefs.edit { remove(Settings.PREF_PINNED_CLIPS) }
|
||||
} catch (_: IllegalStateException) {
|
||||
// SharedPreferences in credential encrypted storage are not available until after user is unlocked
|
||||
|
|
|
@ -26,7 +26,6 @@ import androidx.annotation.NonNull;
|
|||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import helium314.keyboard.keyboard.KeyboardActionListener;
|
||||
import helium314.keyboard.keyboard.KeyboardTheme;
|
||||
|
@ -587,7 +586,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
|
|||
|
||||
public static String readPinnedClipString(final Context context) {
|
||||
try {
|
||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
final SharedPreferences prefs = KtxKt.protectedPrefs(context);
|
||||
return prefs.getString(PREF_PINNED_CLIPS, "");
|
||||
} catch (final IllegalStateException e) {
|
||||
// SharedPreferences in credential encrypted storage are not available until after user is unlocked
|
||||
|
@ -597,7 +596,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
|
|||
|
||||
public static void writePinnedClipString(final Context context, final String clips) {
|
||||
try {
|
||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
final SharedPreferences prefs = KtxKt.protectedPrefs(context);
|
||||
prefs.edit().putString(PREF_PINNED_CLIPS, clips).apply();
|
||||
} catch (final IllegalStateException e) {
|
||||
// SharedPreferences in credential encrypted storage are not available until after user is unlocked
|
||||
|
|
|
@ -11,8 +11,6 @@ import android.app.Application;
|
|||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import helium314.keyboard.latin.App;
|
||||
import helium314.keyboard.latin.BuildConfig;
|
||||
import helium314.keyboard.latin.settings.Settings;
|
||||
|
@ -64,7 +62,7 @@ public final class JniUtils {
|
|||
// we want the default preferences, because storing the checksum in device protected storage is discouraged
|
||||
// see https://developer.android.com/reference/android/content/Context#createDeviceProtectedStorageContext()
|
||||
// if device is locked, this will throw an IllegalStateException
|
||||
wantedChecksum = PreferenceManager.getDefaultSharedPreferences(app).getString(Settings.PREF_LIBRARY_CHECKSUM, wantedChecksum);
|
||||
wantedChecksum = KtxKt.protectedPrefs(app).getString(Settings.PREF_LIBRARY_CHECKSUM, wantedChecksum);
|
||||
}
|
||||
final FileInputStream libStream = new FileInputStream(userSuppliedLibrary);
|
||||
final String checksum = ChecksumCalculator.INSTANCE.checksum(libStream);
|
||||
|
|
|
@ -82,4 +82,9 @@ fun Activity.switchTo(fragment: androidx.fragment.app.Fragment) {
|
|||
}
|
||||
}
|
||||
|
||||
/** SharedPreferences from deviceProtectedContext, which are accessible even without unlocking.
|
||||
* They should not be used to store sensitive data! */
|
||||
fun Context.prefs(): SharedPreferences = DeviceProtectedUtils.getSharedPreferences(this)
|
||||
|
||||
/** The "default" preferences that are only accessible after the device has been unlocked. */
|
||||
fun Context.protectedPrefs(): SharedPreferences = getSharedPreferences("${packageName}_preferences", Context.MODE_PRIVATE)
|
||||
|
|
|
@ -10,12 +10,10 @@ import androidx.compose.material3.Text
|
|||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.preference.PreferenceManager
|
||||
import helium314.keyboard.dictionarypack.DictionaryPackConstants
|
||||
import helium314.keyboard.keyboard.internal.keyboard_parser.LAYOUT_NUMBER
|
||||
import helium314.keyboard.keyboard.internal.keyboard_parser.LAYOUT_NUMPAD
|
||||
|
@ -39,6 +37,7 @@ import helium314.keyboard.latin.utils.Log
|
|||
import helium314.keyboard.latin.utils.getActivity
|
||||
import helium314.keyboard.latin.utils.onCustomLayoutFileListChanged
|
||||
import helium314.keyboard.latin.utils.prefs
|
||||
import helium314.keyboard.latin.utils.protectedPrefs
|
||||
import helium314.keyboard.latin.utils.reloadEnabledSubtypes
|
||||
import helium314.keyboard.latin.utils.updateAdditionalSubtypes
|
||||
import helium314.keyboard.settings.Setting
|
||||
|
@ -114,7 +113,7 @@ fun BackupRestorePreference(setting: Setting) {
|
|||
settingsToJsonStream(prefs.all, zipStream)
|
||||
zipStream.closeEntry()
|
||||
zipStream.putNextEntry(ZipEntry(PROTECTED_PREFS_FILE_NAME))
|
||||
settingsToJsonStream(PreferenceManager.getDefaultSharedPreferences(ctx).all, zipStream)
|
||||
settingsToJsonStream(ctx.protectedPrefs().all, zipStream)
|
||||
zipStream.closeEntry()
|
||||
zipStream.close()
|
||||
}
|
||||
|
@ -157,7 +156,7 @@ fun BackupRestorePreference(setting: Setting) {
|
|||
readJsonLinesToSettings(prefLines, prefs)
|
||||
} else if (entry.name == PROTECTED_PREFS_FILE_NAME) {
|
||||
val prefLines = String(zip.readBytes()).split("\n")
|
||||
val protectedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx)
|
||||
val protectedPrefs = ctx.protectedPrefs()
|
||||
protectedPrefs.edit().clear().apply()
|
||||
readJsonLinesToSettings(prefLines, protectedPrefs)
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ import androidx.compose.material3.Text
|
|||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
|
@ -21,7 +20,7 @@ import helium314.keyboard.latin.common.FileUtils
|
|||
import helium314.keyboard.latin.settings.Settings
|
||||
import helium314.keyboard.latin.utils.ChecksumCalculator
|
||||
import helium314.keyboard.latin.utils.JniUtils
|
||||
import helium314.keyboard.latin.utils.prefs
|
||||
import helium314.keyboard.latin.utils.protectedPrefs
|
||||
import helium314.keyboard.settings.Setting
|
||||
import helium314.keyboard.settings.dialogs.ConfirmationDialog
|
||||
import java.io.File
|
||||
|
@ -33,7 +32,7 @@ import java.io.IOException
|
|||
fun LoadGestureLibPreference(setting: Setting) {
|
||||
var showDialog by rememberSaveable { mutableStateOf(false) }
|
||||
val ctx = LocalContext.current
|
||||
val prefs = ctx.prefs()
|
||||
val prefs = ctx.protectedPrefs()
|
||||
val abi = Build.SUPPORTED_ABIS[0]
|
||||
val libFile = File(ctx.filesDir.absolutePath + File.separator + JniUtils.JNI_LIB_IMPORT_FILE_NAME)
|
||||
fun renameToLibFileAndRestart(file: File, checksum: String) {
|
||||
|
|
Loading…
Add table
Reference in a new issue