Split security preferences from PrefrencesHelper (#8030)

This commit is contained in:
Andreas 2022-09-18 19:07:48 +02:00 committed by GitHub
parent 877ae041a4
commit b668364afb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 70 additions and 53 deletions

View File

@ -16,6 +16,7 @@ import eu.kanade.data.listOfStringsAdapter
import eu.kanade.domain.source.service.SourcePreferences import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.tachiyomi.core.preference.AndroidPreferenceStore import eu.kanade.tachiyomi.core.preference.AndroidPreferenceStore
import eu.kanade.tachiyomi.core.preference.PreferenceStore import eu.kanade.tachiyomi.core.preference.PreferenceStore
import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.data.cache.ChapterCache import eu.kanade.tachiyomi.data.cache.ChapterCache
import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.DownloadManager
@ -143,6 +144,9 @@ class PreferenceModule(val application: Application) : InjektModule {
addSingletonFactory { addSingletonFactory {
SourcePreferences(get()) SourcePreferences(get())
} }
addSingletonFactory {
SecurityPreferences(get())
}
addSingletonFactory { addSingletonFactory {
PreferencesHelper( PreferencesHelper(
context = application, context = application,

View File

@ -5,6 +5,7 @@ import android.os.Build
import androidx.core.content.edit import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import eu.kanade.domain.source.service.SourcePreferences import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.data.backup.BackupCreatorJob import eu.kanade.tachiyomi.data.backup.BackupCreatorJob
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.preference.MANGA_NON_COMPLETED import eu.kanade.tachiyomi.data.preference.MANGA_NON_COMPLETED
@ -39,6 +40,7 @@ object Migrations {
preferences: PreferencesHelper, preferences: PreferencesHelper,
networkPreferences: NetworkPreferences, networkPreferences: NetworkPreferences,
sourcePreferences: SourcePreferences, sourcePreferences: SourcePreferences,
securityPreferences: SecurityPreferences,
): Boolean { ): Boolean {
val oldVersion = preferences.lastVersionCode().get() val oldVersion = preferences.lastVersionCode().get()
if (oldVersion < BuildConfig.VERSION_CODE) { if (oldVersion < BuildConfig.VERSION_CODE) {
@ -254,7 +256,7 @@ object Migrations {
if (oldVersion < 75) { if (oldVersion < 75) {
val oldSecureScreen = prefs.getBoolean("secure_screen", false) val oldSecureScreen = prefs.getBoolean("secure_screen", false)
if (oldSecureScreen) { if (oldSecureScreen) {
preferences.secureScreen().set(PreferenceValues.SecureScreenMode.ALWAYS) securityPreferences.secureScreen().set(SecurityPreferences.SecureScreenMode.ALWAYS)
} }
if (DeviceUtil.isMiui && preferences.extensionInstaller().get() == PreferenceValues.ExtensionInstaller.PACKAGEINSTALLER) { if (DeviceUtil.isMiui && preferences.extensionInstaller().get() == PreferenceValues.ExtensionInstaller.PACKAGEINSTALLER) {
preferences.extensionInstaller().set(PreferenceValues.ExtensionInstaller.LEGACY) preferences.extensionInstaller().set(PreferenceValues.ExtensionInstaller.LEGACY)

View File

@ -5,9 +5,9 @@ import android.graphics.BitmapFactory
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.storage.getUriCompat
import eu.kanade.tachiyomi.util.system.notificationBuilder import eu.kanade.tachiyomi.util.system.notificationBuilder
import eu.kanade.tachiyomi.util.system.notificationManager import eu.kanade.tachiyomi.util.system.notificationManager
@ -17,7 +17,7 @@ import java.util.concurrent.TimeUnit
class BackupNotifier(private val context: Context) { class BackupNotifier(private val context: Context) {
private val preferences: PreferencesHelper by injectLazy() private val preferences: SecurityPreferences by injectLazy()
private val progressNotificationBuilder = context.notificationBuilder(Notifications.CHANNEL_BACKUP_RESTORE_PROGRESS) { private val progressNotificationBuilder = context.notificationBuilder(Notifications.CHANNEL_BACKUP_RESTORE_PROGRESS) {
setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher)) setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher))

View File

@ -5,11 +5,11 @@ import android.content.Context
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.data.notification.NotificationHandler import eu.kanade.tachiyomi.data.notification.NotificationHandler
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.lang.chop import eu.kanade.tachiyomi.util.lang.chop
import eu.kanade.tachiyomi.util.system.notificationBuilder import eu.kanade.tachiyomi.util.system.notificationBuilder
import eu.kanade.tachiyomi.util.system.notificationManager import eu.kanade.tachiyomi.util.system.notificationManager
@ -23,7 +23,7 @@ import java.util.regex.Pattern
*/ */
internal class DownloadNotifier(private val context: Context) { internal class DownloadNotifier(private val context: Context) {
private val preferences: PreferencesHelper by injectLazy() private val preferences: SecurityPreferences by injectLazy()
private val progressNotificationBuilder by lazy { private val progressNotificationBuilder by lazy {
context.notificationBuilder(Notifications.CHANNEL_DOWNLOADER_PROGRESS) { context.notificationBuilder(Notifications.CHANNEL_DOWNLOADER_PROGRESS) {

View File

@ -16,11 +16,11 @@ import coil.transform.CircleCropTransformation
import eu.kanade.domain.chapter.model.Chapter import eu.kanade.domain.chapter.model.Chapter
import eu.kanade.domain.manga.model.Manga import eu.kanade.domain.manga.model.Manga
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.data.download.Downloader import eu.kanade.tachiyomi.data.download.Downloader
import eu.kanade.tachiyomi.data.notification.NotificationHandler import eu.kanade.tachiyomi.data.notification.NotificationHandler
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.util.lang.chop import eu.kanade.tachiyomi.util.lang.chop
import eu.kanade.tachiyomi.util.lang.launchUI import eu.kanade.tachiyomi.util.lang.launchUI
@ -33,7 +33,7 @@ import java.text.DecimalFormatSymbols
class LibraryUpdateNotifier(private val context: Context) { class LibraryUpdateNotifier(private val context: Context) {
private val preferences: PreferencesHelper by injectLazy() private val preferences: SecurityPreferences by injectLazy()
/** /**
* Pending intent of action that cancels the library update * Pending intent of action that cancels the library update

View File

@ -37,8 +37,6 @@ object PreferenceKeys {
const val librarySortingMode = "library_sorting_mode" const val librarySortingMode = "library_sorting_mode"
const val hideNotificationContent = "hide_notification_content"
const val autoUpdateMetadata = "auto_update_metadata" const val autoUpdateMetadata = "auto_update_metadata"
const val autoUpdateTrackers = "auto_update_trackers" const val autoUpdateTrackers = "auto_update_trackers"

View File

@ -74,10 +74,4 @@ object PreferenceValues {
PACKAGEINSTALLER(R.string.ext_installer_packageinstaller), PACKAGEINSTALLER(R.string.ext_installer_packageinstaller),
SHIZUKU(R.string.ext_installer_shizuku), SHIZUKU(R.string.ext_installer_shizuku),
} }
enum class SecureScreenMode(val titleResId: Int) {
ALWAYS(R.string.lock_always),
INCOGNITO(R.string.pref_incognito_mode),
NEVER(R.string.lock_never),
}
} }

View File

@ -46,20 +46,6 @@ class PreferencesHelper(
fun sideNavIconAlignment() = this.preferenceStore.getInt("pref_side_nav_icon_alignment", 0) fun sideNavIconAlignment() = this.preferenceStore.getInt("pref_side_nav_icon_alignment", 0)
fun useAuthenticator() = this.preferenceStore.getBoolean("use_biometric_lock", false)
fun lockAppAfter() = this.preferenceStore.getInt("lock_app_after", 0)
/**
* For app lock. Will be set when there is a pending timed lock.
* Otherwise this pref should be deleted.
*/
fun lastAppClosed() = this.preferenceStore.getLong("last_app_closed", 0)
fun secureScreen() = this.preferenceStore.getEnum("secure_screen_v2", Values.SecureScreenMode.INCOGNITO)
fun hideNotificationContent() = this.preferenceStore.getBoolean(Keys.hideNotificationContent, false)
fun autoUpdateMetadata() = this.preferenceStore.getBoolean(Keys.autoUpdateMetadata, false) fun autoUpdateMetadata() = this.preferenceStore.getBoolean(Keys.autoUpdateMetadata, false)
fun autoUpdateTrackers() = this.preferenceStore.getBoolean(Keys.autoUpdateTrackers, false) fun autoUpdateTrackers() = this.preferenceStore.getBoolean(Keys.autoUpdateTrackers, false)

View File

@ -46,7 +46,7 @@ import coil.transform.RoundedCornersTransformation
import eu.kanade.data.DatabaseHandler import eu.kanade.data.DatabaseHandler
import eu.kanade.domain.manga.model.MangaCover import eu.kanade.domain.manga.model.MangaCover
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.ui.manga.MangaController
import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchIO
@ -61,7 +61,7 @@ import java.util.Date
class UpdatesGridGlanceWidget : GlanceAppWidget() { class UpdatesGridGlanceWidget : GlanceAppWidget() {
private val app: Application by injectLazy() private val app: Application by injectLazy()
private val preferences: PreferencesHelper by injectLazy() private val preferences: SecurityPreferences by injectLazy()
private val coroutineScope = MainScope() private val coroutineScope = MainScope()

View File

@ -5,7 +5,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import eu.kanade.tachiyomi.data.preference.PreferenceValues import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.ui.security.UnlockActivity import eu.kanade.tachiyomi.ui.security.UnlockActivity
import eu.kanade.tachiyomi.util.system.AuthenticatorUtil import eu.kanade.tachiyomi.util.system.AuthenticatorUtil
@ -24,7 +24,7 @@ interface SecureActivityDelegate {
companion object { companion object {
fun onApplicationCreated() { fun onApplicationCreated() {
val lockDelay = Injekt.get<PreferencesHelper>().lockAppAfter().get() val lockDelay = Injekt.get<SecurityPreferences>().lockAppAfter().get()
if (lockDelay == 0) { if (lockDelay == 0) {
// Restore always active app lock // Restore always active app lock
// Delayed lock will be restored later on activity resume // Delayed lock will be restored later on activity resume
@ -33,7 +33,7 @@ interface SecureActivityDelegate {
} }
fun onApplicationStopped() { fun onApplicationStopped() {
val preferences = Injekt.get<PreferencesHelper>() val preferences = Injekt.get<SecurityPreferences>()
if (!preferences.useAuthenticator().get()) return if (!preferences.useAuthenticator().get()) return
if (lockState != LockState.ACTIVE) { if (lockState != LockState.ACTIVE) {
preferences.lastAppClosed().set(Date().time) preferences.lastAppClosed().set(Date().time)
@ -49,7 +49,7 @@ interface SecureActivityDelegate {
fun unlock() { fun unlock() {
lockState = LockState.INACTIVE lockState = LockState.INACTIVE
Injekt.get<PreferencesHelper>().lastAppClosed().delete() Injekt.get<SecurityPreferences>().lastAppClosed().delete()
} }
} }
} }
@ -67,6 +67,7 @@ class SecureActivityDelegateImpl : SecureActivityDelegate, DefaultLifecycleObser
private lateinit var activity: AppCompatActivity private lateinit var activity: AppCompatActivity
private val preferences: PreferencesHelper by injectLazy() private val preferences: PreferencesHelper by injectLazy()
private val securityPreferences: SecurityPreferences by injectLazy()
override fun registerSecureActivity(activity: AppCompatActivity) { override fun registerSecureActivity(activity: AppCompatActivity) {
this.activity = activity this.activity = activity
@ -82,31 +83,31 @@ class SecureActivityDelegateImpl : SecureActivityDelegate, DefaultLifecycleObser
} }
private fun setSecureScreen() { private fun setSecureScreen() {
val secureScreenFlow = preferences.secureScreen().changes() val secureScreenFlow = securityPreferences.secureScreen().changes()
val incognitoModeFlow = preferences.incognitoMode().changes() val incognitoModeFlow = preferences.incognitoMode().changes()
combine(secureScreenFlow, incognitoModeFlow) { secureScreen, incognitoMode -> combine(secureScreenFlow, incognitoModeFlow) { secureScreen, incognitoMode ->
secureScreen == PreferenceValues.SecureScreenMode.ALWAYS || secureScreen == SecurityPreferences.SecureScreenMode.ALWAYS ||
secureScreen == PreferenceValues.SecureScreenMode.INCOGNITO && incognitoMode secureScreen == SecurityPreferences.SecureScreenMode.INCOGNITO && incognitoMode
} }
.onEach { activity.window.setSecureScreen(it) } .onEach { activity.window.setSecureScreen(it) }
.launchIn(activity.lifecycleScope) .launchIn(activity.lifecycleScope)
} }
private fun setAppLock() { private fun setAppLock() {
if (!preferences.useAuthenticator().get()) return if (!securityPreferences.useAuthenticator().get()) return
if (activity.isAuthenticationSupported()) { if (activity.isAuthenticationSupported()) {
updatePendingLockStatus() updatePendingLockStatus()
if (!isAppLocked()) return if (!isAppLocked()) return
activity.startActivity(Intent(activity, UnlockActivity::class.java)) activity.startActivity(Intent(activity, UnlockActivity::class.java))
activity.overridePendingTransition(0, 0) activity.overridePendingTransition(0, 0)
} else { } else {
preferences.useAuthenticator().set(false) securityPreferences.useAuthenticator().set(false)
} }
} }
private fun updatePendingLockStatus() { private fun updatePendingLockStatus() {
val lastClosedPref = preferences.lastAppClosed() val lastClosedPref = securityPreferences.lastAppClosed()
val lockDelay = 60000 * preferences.lockAppAfter().get() val lockDelay = 60000 * securityPreferences.lockAppAfter().get()
if (lastClosedPref.isSet() && lockDelay > 0) { if (lastClosedPref.isSet() && lockDelay > 0) {
// Restore pending status in case app was killed // Restore pending status in case app was killed
lockState = LockState.PENDING lockState = LockState.PENDING

View File

@ -116,6 +116,7 @@ class MainActivity : BaseActivity() {
preferences = preferences, preferences = preferences,
networkPreferences = Injekt.get(), networkPreferences = Injekt.get(),
sourcePreferences = sourcePreferences, sourcePreferences = sourcePreferences,
securityPreferences = Injekt.get(),
) )
} else { } else {
false false

View File

@ -5,9 +5,8 @@ import androidx.fragment.app.FragmentActivity
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferenceValues import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.util.preference.bindTo import eu.kanade.tachiyomi.util.preference.bindTo
import eu.kanade.tachiyomi.util.preference.defaultValue
import eu.kanade.tachiyomi.util.preference.entriesRes import eu.kanade.tachiyomi.util.preference.entriesRes
import eu.kanade.tachiyomi.util.preference.infoPreference import eu.kanade.tachiyomi.util.preference.infoPreference
import eu.kanade.tachiyomi.util.preference.intListPreference import eu.kanade.tachiyomi.util.preference.intListPreference
@ -19,16 +18,18 @@ import eu.kanade.tachiyomi.util.system.AuthenticatorUtil
import eu.kanade.tachiyomi.util.system.AuthenticatorUtil.isAuthenticationSupported import eu.kanade.tachiyomi.util.system.AuthenticatorUtil.isAuthenticationSupported
import eu.kanade.tachiyomi.util.system.AuthenticatorUtil.startAuthentication import eu.kanade.tachiyomi.util.system.AuthenticatorUtil.startAuthentication
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import uy.kohesive.injekt.injectLazy
class SettingsSecurityController : SettingsController() { class SettingsSecurityController : SettingsController() {
private val securityPreferences: SecurityPreferences by injectLazy()
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply { override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_security titleRes = R.string.pref_category_security
if (context.isAuthenticationSupported()) { if (context.isAuthenticationSupported()) {
switchPreference { switchPreference {
bindTo(preferences.useAuthenticator()) bindTo(securityPreferences.useAuthenticator())
titleRes = R.string.lock_with_biometrics titleRes = R.string.lock_with_biometrics
requireAuthentication( requireAuthentication(
@ -39,7 +40,7 @@ class SettingsSecurityController : SettingsController() {
} }
intListPreference { intListPreference {
bindTo(preferences.lockAppAfter()) bindTo(securityPreferences.lockAppAfter())
titleRes = R.string.lock_when_idle titleRes = R.string.lock_when_idle
val values = arrayOf("0", "1", "2", "5", "10", "-1") val values = arrayOf("0", "1", "2", "5", "10", "-1")
entries = values.mapNotNull { entries = values.mapNotNull {
@ -79,22 +80,21 @@ class SettingsSecurityController : SettingsController() {
false false
} }
visibleIf(preferences.useAuthenticator()) { it } visibleIf(securityPreferences.useAuthenticator()) { it }
} }
} }
switchPreference { switchPreference {
key = Keys.hideNotificationContent bindTo(securityPreferences.hideNotificationContent())
titleRes = R.string.hide_notification_content titleRes = R.string.hide_notification_content
defaultValue = false
} }
listPreference { listPreference {
bindTo(preferences.secureScreen()) bindTo(securityPreferences.secureScreen())
titleRes = R.string.secure_screen titleRes = R.string.secure_screen
summary = "%s" summary = "%s"
entriesRes = PreferenceValues.SecureScreenMode.values().map { it.titleResId }.toTypedArray() entriesRes = SecurityPreferences.SecureScreenMode.values().map { it.titleResId }.toTypedArray()
entryValues = PreferenceValues.SecureScreenMode.values().map { it.name }.toTypedArray() entryValues = SecurityPreferences.SecureScreenMode.values().map { it.name }.toTypedArray()
} }
infoPreference(R.string.secure_screen_summary) infoPreference(R.string.secure_screen_summary)

View File

@ -0,0 +1,31 @@
package eu.kanade.tachiyomi.core.security
import eu.kanade.tachiyomi.core.R
import eu.kanade.tachiyomi.core.preference.PreferenceStore
import eu.kanade.tachiyomi.core.preference.getEnum
class SecurityPreferences(
private val preferenceStore: PreferenceStore
) {
fun useAuthenticator() = this.preferenceStore.getBoolean("use_biometric_lock", false)
fun lockAppAfter() = this.preferenceStore.getInt("lock_app_after", 0)
fun secureScreen() = this.preferenceStore.getEnum("secure_screen_v2", SecureScreenMode.INCOGNITO)
fun hideNotificationContent() = this.preferenceStore.getBoolean("hide_notification_content", false)
/**
* For app lock. Will be set when there is a pending timed lock.
* Otherwise this pref should be deleted.
*/
fun lastAppClosed() = this.preferenceStore.getLong("last_app_closed", 0)
enum class SecureScreenMode(val titleResId: Int) {
ALWAYS(R.string.lock_always),
INCOGNITO(R.string.pref_incognito_mode),
NEVER(R.string.lock_never),
}
}