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
|
|
@ -21,7 +21,6 @@ import android.os.Build
|
|||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.core.os.LocaleListCompat
|
||||
import androidx.preference.PreferenceDataStore
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.keylesspalace.tusky.R
|
||||
import com.keylesspalace.tusky.settings.PrefKeys
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
|
|
@ -33,10 +32,11 @@ class LocaleManager @Inject constructor(
|
|||
@ApplicationContext val context: Context
|
||||
) : PreferenceDataStore() {
|
||||
|
||||
private var prefs: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
@Inject
|
||||
lateinit var preferences: SharedPreferences
|
||||
|
||||
fun setLocale() {
|
||||
val language = prefs.getNonNullString(PrefKeys.LANGUAGE, DEFAULT)
|
||||
val language = preferences.getNonNullString(PrefKeys.LANGUAGE, DEFAULT)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
if (language != HANDLED_BY_SYSTEM) {
|
||||
|
|
@ -44,7 +44,7 @@ class LocaleManager @Inject constructor(
|
|||
// hand over the old setting to the system and save a dummy value in Shared Preferences
|
||||
applyLanguageToApp(language)
|
||||
|
||||
prefs.edit()
|
||||
preferences.edit()
|
||||
.putString(PrefKeys.LANGUAGE, HANDLED_BY_SYSTEM)
|
||||
.apply()
|
||||
}
|
||||
|
|
@ -58,7 +58,7 @@ class LocaleManager @Inject constructor(
|
|||
// if we are on Android < 13 we have to save the selected language so we can apply it at appstart
|
||||
// on Android 13+ the system handles it for us
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
|
||||
prefs.edit()
|
||||
preferences.edit()
|
||||
.putString(PrefKeys.LANGUAGE, value)
|
||||
.apply()
|
||||
}
|
||||
|
|
@ -84,7 +84,7 @@ class LocaleManager @Inject constructor(
|
|||
}
|
||||
}
|
||||
} else {
|
||||
prefs.getNonNullString(PrefKeys.LANGUAGE, DEFAULT)
|
||||
preferences.getNonNullString(PrefKeys.LANGUAGE, DEFAULT)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
package com.keylesspalace.tusky.util
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.coroutineScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.keylesspalace.tusky.settings.PrefKeys
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
import kotlinx.coroutines.delay
|
||||
|
|
@ -19,8 +19,7 @@ private val UPDATE_INTERVAL = 1.minutes
|
|||
* if setting absoluteTimeView is false.
|
||||
* Start updates when the Fragment becomes visible and stop when it is hidden.
|
||||
*/
|
||||
fun Fragment.updateRelativeTimePeriodically(callback: Runnable) {
|
||||
val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
fun Fragment.updateRelativeTimePeriodically(preferences: SharedPreferences, callback: Runnable) {
|
||||
val lifecycle = viewLifecycleOwner.lifecycle
|
||||
lifecycle.coroutineScope.launch {
|
||||
// This child coroutine will launch each time the Fragment moves to the STARTED state
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue