Use a singleton for default shared pref
This commit is contained in:
parent
f1d902b9ad
commit
fa381cc06d
@ -22,7 +22,7 @@ import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.content.SharedPreferences
|
||||
import androidx.core.content.edit
|
||||
import androidx.preference.PreferenceManager
|
||||
import im.vector.app.core.di.DefaultSharedPreferences
|
||||
import im.vector.app.core.utils.lsFiles
|
||||
import timber.log.Timber
|
||||
|
||||
@ -44,7 +44,7 @@ class DebugReceiver : BroadcastReceiver() {
|
||||
}
|
||||
|
||||
private fun dumpPreferences(context: Context) {
|
||||
logPrefs("DefaultSharedPreferences", PreferenceManager.getDefaultSharedPreferences(context))
|
||||
logPrefs("DefaultSharedPreferences", DefaultSharedPreferences.getInstance(context))
|
||||
}
|
||||
|
||||
private fun logPrefs(name: String, sharedPreferences: SharedPreferences?) {
|
||||
@ -58,7 +58,7 @@ class DebugReceiver : BroadcastReceiver() {
|
||||
}
|
||||
|
||||
private fun alterScalarToken(context: Context) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||
DefaultSharedPreferences.getInstance(context).edit {
|
||||
// putString("SCALAR_TOKEN_PREFERENCE_KEY" + Matrix.getInstance(context).defaultSession.myUserId, "bad_token")
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ package im.vector.app.push.fcm
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import androidx.preference.PreferenceManager
|
||||
import android.widget.Toast
|
||||
import androidx.core.content.edit
|
||||
import com.google.android.gms.common.ConnectionResult
|
||||
@ -27,6 +26,7 @@ import com.google.android.gms.common.GoogleApiAvailability
|
||||
import com.google.firebase.iid.FirebaseInstanceId
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.di.ActiveSessionHolder
|
||||
import im.vector.app.core.di.DefaultSharedPreferences
|
||||
import im.vector.app.core.pushers.PushersManager
|
||||
import im.vector.app.features.settings.VectorPreferences
|
||||
import timber.log.Timber
|
||||
@ -46,7 +46,7 @@ object FcmHelper {
|
||||
* @return the FCM token or null if not received from FCM
|
||||
*/
|
||||
fun getFcmToken(context: Context): String? {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context).getString(PREFS_KEY_FCM_TOKEN, null)
|
||||
return DefaultSharedPreferences.getInstance(context).getString(PREFS_KEY_FCM_TOKEN, null)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,7 +58,7 @@ object FcmHelper {
|
||||
*/
|
||||
fun storeFcmToken(context: Context,
|
||||
token: String?) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||
DefaultSharedPreferences.getInstance(context).edit {
|
||||
putString(PREFS_KEY_FCM_TOKEN, token)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 2020 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package im.vector.app.core.di
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import androidx.preference.PreferenceManager
|
||||
|
||||
object DefaultSharedPreferences {
|
||||
|
||||
@Volatile private var INSTANCE: SharedPreferences? = null
|
||||
|
||||
fun getInstance(context: Context): SharedPreferences =
|
||||
INSTANCE ?: synchronized(this) {
|
||||
INSTANCE ?: PreferenceManager.getDefaultSharedPreferences(context).also { INSTANCE = it }
|
||||
}
|
||||
}
|
@ -23,11 +23,11 @@ import android.widget.TextView
|
||||
import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import androidx.core.content.edit
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.preference.PreferenceManager
|
||||
import butterknife.BindView
|
||||
import butterknife.ButterKnife
|
||||
import butterknife.OnClick
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.di.DefaultSharedPreferences
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
@ -57,7 +57,7 @@ class KeysBackupBanner @JvmOverloads constructor(
|
||||
|
||||
init {
|
||||
setupView()
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||
DefaultSharedPreferences.getInstance(context).edit {
|
||||
putBoolean(BANNER_SETUP_DO_NOT_SHOW_AGAIN, false)
|
||||
putString(BANNER_RECOVER_DO_NOT_SHOW_FOR_VERSION, "")
|
||||
}
|
||||
@ -105,17 +105,17 @@ class KeysBackupBanner @JvmOverloads constructor(
|
||||
state.let {
|
||||
when (it) {
|
||||
is State.Setup -> {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||
DefaultSharedPreferences.getInstance(context).edit {
|
||||
putBoolean(BANNER_SETUP_DO_NOT_SHOW_AGAIN, true)
|
||||
}
|
||||
}
|
||||
is State.Recover -> {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||
DefaultSharedPreferences.getInstance(context).edit {
|
||||
putString(BANNER_RECOVER_DO_NOT_SHOW_FOR_VERSION, it.version)
|
||||
}
|
||||
}
|
||||
is State.Update -> {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||
DefaultSharedPreferences.getInstance(context).edit {
|
||||
putString(BANNER_UPDATE_DO_NOT_SHOW_FOR_VERSION, it.version)
|
||||
}
|
||||
}
|
||||
@ -150,7 +150,7 @@ class KeysBackupBanner @JvmOverloads constructor(
|
||||
|
||||
private fun renderSetup(nbOfKeys: Int) {
|
||||
if (nbOfKeys == 0
|
||||
|| PreferenceManager.getDefaultSharedPreferences(context).getBoolean(BANNER_SETUP_DO_NOT_SHOW_AGAIN, false)) {
|
||||
|| DefaultSharedPreferences.getInstance(context).getBoolean(BANNER_SETUP_DO_NOT_SHOW_AGAIN, false)) {
|
||||
// Do not display the setup banner if there is no keys to backup, or if the user has already closed it
|
||||
isVisible = false
|
||||
} else {
|
||||
@ -164,7 +164,7 @@ class KeysBackupBanner @JvmOverloads constructor(
|
||||
}
|
||||
|
||||
private fun renderRecover(version: String) {
|
||||
if (version == PreferenceManager.getDefaultSharedPreferences(context).getString(BANNER_RECOVER_DO_NOT_SHOW_FOR_VERSION, null)) {
|
||||
if (version == DefaultSharedPreferences.getInstance(context).getString(BANNER_RECOVER_DO_NOT_SHOW_FOR_VERSION, null)) {
|
||||
isVisible = false
|
||||
} else {
|
||||
isVisible = true
|
||||
@ -177,7 +177,7 @@ class KeysBackupBanner @JvmOverloads constructor(
|
||||
}
|
||||
|
||||
private fun renderUpdate(version: String) {
|
||||
if (version == PreferenceManager.getDefaultSharedPreferences(context).getString(BANNER_UPDATE_DO_NOT_SHOW_FOR_VERSION, null)) {
|
||||
if (version == DefaultSharedPreferences.getInstance(context).getString(BANNER_UPDATE_DO_NOT_SHOW_FOR_VERSION, null)) {
|
||||
isVisible = false
|
||||
} else {
|
||||
isVisible = true
|
||||
@ -258,7 +258,7 @@ class KeysBackupBanner @JvmOverloads constructor(
|
||||
* Inform the banner that a Recover has been done for this version, so do not show the Recover banner for this version
|
||||
*/
|
||||
fun onRecoverDoneForVersion(context: Context, version: String) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||
DefaultSharedPreferences.getInstance(context).edit {
|
||||
putString(BANNER_RECOVER_DO_NOT_SHOW_FOR_VERSION, version)
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ import android.media.Ringtone
|
||||
import android.media.RingtoneManager
|
||||
import android.net.Uri
|
||||
import androidx.core.content.edit
|
||||
import androidx.preference.PreferenceManager
|
||||
import im.vector.app.core.di.DefaultSharedPreferences
|
||||
import im.vector.app.features.settings.VectorPreferences
|
||||
|
||||
/**
|
||||
@ -40,7 +40,7 @@ import im.vector.app.features.settings.VectorPreferences
|
||||
* @see Ringtone
|
||||
*/
|
||||
fun getCallRingtoneUri(context: Context): Uri? {
|
||||
val callRingtone: String? = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
val callRingtone: String? = DefaultSharedPreferences.getInstance(context)
|
||||
.getString(VectorPreferences.SETTINGS_CALL_RINGTONE_URI_PREFERENCE_KEY, null)
|
||||
|
||||
callRingtone?.let {
|
||||
@ -94,7 +94,7 @@ fun getCallRingtoneName(context: Context): String? {
|
||||
* @see Ringtone
|
||||
*/
|
||||
fun setCallRingtoneUri(context: Context, ringtoneUri: Uri) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
DefaultSharedPreferences.getInstance(context)
|
||||
.edit {
|
||||
putString(VectorPreferences.SETTINGS_CALL_RINGTONE_URI_PREFERENCE_KEY, ringtoneUri.toString())
|
||||
}
|
||||
@ -104,14 +104,14 @@ fun setCallRingtoneUri(context: Context, ringtoneUri: Uri) {
|
||||
* Set using Riot default ringtone
|
||||
*/
|
||||
fun useRiotDefaultRingtone(context: Context): Boolean {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(VectorPreferences.SETTINGS_CALL_RINGTONE_USE_RIOT_PREFERENCE_KEY, true)
|
||||
return DefaultSharedPreferences.getInstance(context).getBoolean(VectorPreferences.SETTINGS_CALL_RINGTONE_USE_RIOT_PREFERENCE_KEY, true)
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask if default Riot ringtone has to be used
|
||||
*/
|
||||
fun setUseRiotDefaultRingtone(context: Context, useRiotDefault: Boolean) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
DefaultSharedPreferences.getInstance(context)
|
||||
.edit {
|
||||
putBoolean(VectorPreferences.SETTINGS_CALL_RINGTONE_USE_RIOT_PREFERENCE_KEY, useRiotDefault)
|
||||
}
|
||||
|
@ -20,8 +20,8 @@ import android.app.Activity
|
||||
import android.content.Context
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.content.edit
|
||||
import androidx.preference.PreferenceManager
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.di.DefaultSharedPreferences
|
||||
import im.vector.app.core.utils.openUrlInChromeCustomTab
|
||||
import im.vector.app.features.settings.VectorSettingsUrls
|
||||
|
||||
@ -31,7 +31,7 @@ private const val CURRENT_DISCLAIMER_VALUE = 2
|
||||
private const val SHARED_PREF_KEY = "LAST_DISCLAIMER_VERSION_VALUE"
|
||||
|
||||
fun showDisclaimerDialog(activity: Activity) {
|
||||
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity)
|
||||
val sharedPrefs = DefaultSharedPreferences.getInstance(activity)
|
||||
|
||||
if (sharedPrefs.getInt(SHARED_PREF_KEY, 0) < CURRENT_DISCLAIMER_VALUE) {
|
||||
sharedPrefs.edit {
|
||||
@ -52,7 +52,7 @@ fun showDisclaimerDialog(activity: Activity) {
|
||||
}
|
||||
|
||||
fun doNotShowDisclaimerDialog(context: Context) {
|
||||
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
val sharedPrefs = DefaultSharedPreferences.getInstance(context)
|
||||
|
||||
sharedPrefs.edit {
|
||||
putInt(SHARED_PREF_KEY, CURRENT_DISCLAIMER_VALUE)
|
||||
|
@ -18,8 +18,8 @@ package im.vector.app.features.homeserver
|
||||
|
||||
import android.content.Context
|
||||
import androidx.core.content.edit
|
||||
import androidx.preference.PreferenceManager
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.di.DefaultSharedPreferences
|
||||
|
||||
/**
|
||||
* Object to store and retrieve home and identity server urls
|
||||
@ -38,7 +38,7 @@ object ServerUrlsRepository {
|
||||
* Save home and identity sever urls received by the Referrer receiver
|
||||
*/
|
||||
fun setDefaultUrlsFromReferrer(context: Context, homeServerUrl: String, identityServerUrl: String) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
DefaultSharedPreferences.getInstance(context)
|
||||
.edit {
|
||||
if (homeServerUrl.isNotEmpty()) {
|
||||
putString(DEFAULT_REFERRER_HOME_SERVER_URL_PREF, homeServerUrl)
|
||||
@ -54,7 +54,7 @@ object ServerUrlsRepository {
|
||||
* Save home and identity sever urls entered by the user. May be custom or default value
|
||||
*/
|
||||
fun saveServerUrls(context: Context, homeServerUrl: String, identityServerUrl: String) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
DefaultSharedPreferences.getInstance(context)
|
||||
.edit {
|
||||
putString(HOME_SERVER_URL_PREF, homeServerUrl)
|
||||
putString(IDENTITY_SERVER_URL_PREF, identityServerUrl)
|
||||
@ -65,7 +65,7 @@ object ServerUrlsRepository {
|
||||
* Return last used home server url, or the default one from referrer or the default one from resources
|
||||
*/
|
||||
fun getLastHomeServerUrl(context: Context): String {
|
||||
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
val prefs = DefaultSharedPreferences.getInstance(context)
|
||||
|
||||
return prefs.getString(HOME_SERVER_URL_PREF,
|
||||
prefs.getString(DEFAULT_REFERRER_HOME_SERVER_URL_PREF,
|
||||
|
@ -19,7 +19,7 @@ package im.vector.app.features.rageshake
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import androidx.core.content.edit
|
||||
import androidx.preference.PreferenceManager
|
||||
import im.vector.app.core.di.DefaultSharedPreferences
|
||||
import im.vector.app.core.resources.VersionCodeProvider
|
||||
import im.vector.app.features.version.VersionProvider
|
||||
import org.matrix.android.sdk.api.Matrix
|
||||
@ -61,7 +61,7 @@ class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter
|
||||
*/
|
||||
override fun uncaughtException(thread: Thread, throwable: Throwable) {
|
||||
Timber.v("Uncaught exception: $throwable")
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||
DefaultSharedPreferences.getInstance(context).edit {
|
||||
putBoolean(PREFS_CRASH_KEY, true)
|
||||
}
|
||||
val b = StringBuilder()
|
||||
@ -115,7 +115,7 @@ class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter
|
||||
* @return true if the application crashed
|
||||
*/
|
||||
fun didAppCrash(context: Context): Boolean {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context)
|
||||
return DefaultSharedPreferences.getInstance(context)
|
||||
.getBoolean(PREFS_CRASH_KEY, false)
|
||||
}
|
||||
|
||||
@ -123,7 +123,7 @@ class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter
|
||||
* Clear the crash status
|
||||
*/
|
||||
fun clearAppCrashStatus(context: Context) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||
DefaultSharedPreferences.getInstance(context).edit {
|
||||
remove(PREFS_CRASH_KEY)
|
||||
}
|
||||
}
|
||||
|
@ -19,8 +19,8 @@ package im.vector.app.features.settings
|
||||
import android.content.Context
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.core.content.edit
|
||||
import androidx.preference.PreferenceManager
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.di.DefaultSharedPreferences
|
||||
|
||||
/**
|
||||
* Object to manage the Font Scale choice of the user
|
||||
@ -56,7 +56,7 @@ object FontScale {
|
||||
* @return the font scale value
|
||||
*/
|
||||
fun getFontScaleValue(context: Context): FontScaleValue {
|
||||
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
val preferences = DefaultSharedPreferences.getInstance(context)
|
||||
|
||||
return if (APPLICATION_FONT_SCALE_KEY !in preferences) {
|
||||
val fontScale = context.resources.configuration.fontScale
|
||||
@ -81,7 +81,7 @@ object FontScale {
|
||||
* @param fontScaleValue the font scale value to store
|
||||
*/
|
||||
private fun saveFontScaleValue(context: Context, fontScaleValue: FontScaleValue) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
DefaultSharedPreferences.getInstance(context)
|
||||
.edit { putString(APPLICATION_FONT_SCALE_KEY, fontScaleValue.preferenceValue) }
|
||||
}
|
||||
}
|
||||
|
@ -19,9 +19,9 @@ package im.vector.app.features.settings
|
||||
import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import androidx.core.content.edit
|
||||
import androidx.preference.PreferenceManager
|
||||
import im.vector.app.BuildConfig
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.di.DefaultSharedPreferences
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
@ -59,7 +59,7 @@ object VectorLocale {
|
||||
*/
|
||||
fun init(context: Context) {
|
||||
this.context = context
|
||||
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
val preferences = DefaultSharedPreferences.getInstance(context)
|
||||
|
||||
if (preferences.contains(APPLICATION_LOCALE_LANGUAGE_KEY)) {
|
||||
applicationLocale = Locale(preferences.getString(APPLICATION_LOCALE_LANGUAGE_KEY, "")!!,
|
||||
@ -85,7 +85,7 @@ object VectorLocale {
|
||||
fun saveApplicationLocale(locale: Locale) {
|
||||
applicationLocale = locale
|
||||
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||
DefaultSharedPreferences.getInstance(context).edit {
|
||||
val language = locale.language
|
||||
if (language.isEmpty()) {
|
||||
remove(APPLICATION_LOCALE_LANGUAGE_KEY)
|
||||
|
@ -22,10 +22,10 @@ import android.media.RingtoneManager
|
||||
import android.net.Uri
|
||||
import android.provider.MediaStore
|
||||
import androidx.core.content.edit
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.squareup.seismic.ShakeDetector
|
||||
import im.vector.app.BuildConfig
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.di.DefaultSharedPreferences
|
||||
import im.vector.app.features.homeserver.ServerUrlsRepository
|
||||
import im.vector.app.features.themes.ThemeUtils
|
||||
import org.matrix.android.sdk.api.extensions.tryThis
|
||||
@ -227,7 +227,7 @@ class VectorPreferences @Inject constructor(private val context: Context) {
|
||||
)
|
||||
}
|
||||
|
||||
private val defaultPrefs = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
private val defaultPrefs = DefaultSharedPreferences.getInstance(context)
|
||||
|
||||
/**
|
||||
* Clear the preferences.
|
||||
|
@ -25,8 +25,8 @@ import androidx.annotation.AttrRes
|
||||
import androidx.annotation.ColorInt
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.graphics.drawable.DrawableCompat
|
||||
import androidx.preference.PreferenceManager
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.di.DefaultSharedPreferences
|
||||
import timber.log.Timber
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
|
||||
@ -73,8 +73,7 @@ object ThemeUtils {
|
||||
fun getApplicationTheme(context: Context): String {
|
||||
val currentTheme = this.currentTheme.get()
|
||||
return if (currentTheme == null) {
|
||||
val themeFromPref = PreferenceManager
|
||||
.getDefaultSharedPreferences(context)
|
||||
val themeFromPref = DefaultSharedPreferences.getInstance(context)
|
||||
.getString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE) ?: THEME_LIGHT_VALUE
|
||||
this.currentTheme.set(themeFromPref)
|
||||
themeFromPref
|
||||
|
Loading…
x
Reference in New Issue
Block a user