inject SharedPreferences (#4441)
(this one is for @charlag)
Calling `PreferenceManager.getDefaultSharedPreferences()` will read the
preference file from disk every time. This PR makes `SharedPreferences`
a singleton so they will only be created once at appstart (with a few
exceptions where it is hard to inject, e.g. in the `openLink` helper)
which should help getting our ANRs down.
```
StrictMode policy violation; ~duration=285 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1666)
at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:8054)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
at java.io.File.exists(File.java:813)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:790)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:781)
at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:737)
at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:962)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:583)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:221)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:221)
at androidx.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:119)
at com.keylesspalace.tusky.BaseActivity.onCreate(BaseActivity.java:96)
...
```
This commit is contained in:
parent
78b1fcb9c6
commit
d554d71958
27 changed files with 159 additions and 135 deletions
|
|
@ -52,7 +52,7 @@ class TuskyApplication : Application(), Configuration.Provider {
|
|||
lateinit var localeManager: LocaleManager
|
||||
|
||||
@Inject
|
||||
lateinit var sharedPreferences: SharedPreferences
|
||||
lateinit var preferences: SharedPreferences
|
||||
|
||||
override fun onCreate() {
|
||||
// Uncomment me to get StrictMode violation logs
|
||||
|
|
@ -70,7 +70,7 @@ class TuskyApplication : Application(), Configuration.Provider {
|
|||
Security.insertProviderAt(Conscrypt.newProvider(), 1)
|
||||
|
||||
// Migrate shared preference keys and defaults from version to version.
|
||||
val oldVersion = sharedPreferences.getInt(
|
||||
val oldVersion = preferences.getInt(
|
||||
PrefKeys.SCHEMA_VERSION,
|
||||
NEW_INSTALL_SCHEMA_VERSION
|
||||
)
|
||||
|
|
@ -84,7 +84,7 @@ class TuskyApplication : Application(), Configuration.Provider {
|
|||
EmojiPackHelper.init(this, DefaultEmojiPackList.get(this), allowPackImports = false)
|
||||
|
||||
// init night mode
|
||||
val theme = sharedPreferences.getString(APP_THEME, AppTheme.DEFAULT.value)
|
||||
val theme = preferences.getString(APP_THEME, AppTheme.DEFAULT.value)
|
||||
setAppNightMode(theme)
|
||||
|
||||
localeManager.setLocale()
|
||||
|
|
@ -109,7 +109,7 @@ class TuskyApplication : Application(), Configuration.Provider {
|
|||
|
||||
private fun upgradeSharedPreferences(oldVersion: Int, newVersion: Int) {
|
||||
Log.d(TAG, "Upgrading shared preferences: $oldVersion -> $newVersion")
|
||||
val editor = sharedPreferences.edit()
|
||||
val editor = preferences.edit()
|
||||
|
||||
if (oldVersion < 2023022701) {
|
||||
// These preferences are (now) handled in AccountPreferenceHandler. Remove them from shared for clarity.
|
||||
|
|
@ -123,7 +123,7 @@ class TuskyApplication : Application(), Configuration.Provider {
|
|||
// Default value for appTheme is now THEME_SYSTEM. If the user is upgrading and
|
||||
// didn't have an explicit preference set use the previous default, so the
|
||||
// theme does not unexpectedly change.
|
||||
if (!sharedPreferences.contains(APP_THEME)) {
|
||||
if (!preferences.contains(APP_THEME)) {
|
||||
editor.putString(APP_THEME, AppTheme.NIGHT.value)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue