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:
Konrad Pozniak 2024-05-24 08:05:09 +02:00 committed by GitHub
commit d554d71958
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 159 additions and 135 deletions

View file

@ -15,9 +15,8 @@
package com.keylesspalace.tusky.db
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import androidx.preference.PreferenceManager
import com.keylesspalace.tusky.db.dao.AccountDao
import com.keylesspalace.tusky.db.entity.AccountEntity
import com.keylesspalace.tusky.entity.Account
@ -35,7 +34,10 @@ import javax.inject.Singleton
private const val TAG = "AccountManager"
@Singleton
class AccountManager @Inject constructor(db: AppDatabase) {
class AccountManager @Inject constructor(
db: AppDatabase,
private val preferences: SharedPreferences
) {
@Volatile
var activeAccount: AccountEntity? = null
@ -236,9 +238,8 @@ class AccountManager @Inject constructor(db: AppDatabase) {
/**
* @return true if the name of the currently-selected account should be displayed in UIs
*/
fun shouldDisplaySelfUsername(context: Context): Boolean {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val showUsernamePreference = sharedPreferences.getString(
fun shouldDisplaySelfUsername(): Boolean {
val showUsernamePreference = preferences.getString(
PrefKeys.SHOW_SELF_USERNAME,
"disambiguate"
)