From 61d2107e9c9e81bcb580b04bd662b0baf96ef6f0 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 22 Feb 2020 13:30:36 -0500 Subject: [PATCH] Add secure screen setting --- app/src/main/java/eu/kanade/tachiyomi/App.kt | 4 ++-- .../tachiyomi/data/preference/PreferenceKeys.kt | 2 ++ .../tachiyomi/data/preference/PreferencesHelper.kt | 2 ++ .../tachiyomi/ui/base/activity/BaseActivity.kt | 7 +++++-- .../tachiyomi/ui/base/activity/BaseRxActivity.kt | 14 ++++++++++++++ .../kanade/tachiyomi/ui/reader/ReaderActivity.kt | 6 ------ .../ui/security/BiometricUnlockActivity.kt | 2 +- ...UnlockDelegate.kt => SecureActivityDelegate.kt} | 14 +++++++++++++- .../ui/setting/SettingsGeneralController.kt | 12 +++++++++--- app/src/main/res/values/strings.xml | 1 + 10 files changed, 49 insertions(+), 15 deletions(-) rename app/src/main/java/eu/kanade/tachiyomi/ui/security/{BiometricUnlockDelegate.kt => SecureActivityDelegate.kt} (71%) diff --git a/app/src/main/java/eu/kanade/tachiyomi/App.kt b/app/src/main/java/eu/kanade/tachiyomi/App.kt index fd68315bb4..cffddba7e0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/App.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/App.kt @@ -15,7 +15,7 @@ import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.updater.UpdaterJob -import eu.kanade.tachiyomi.ui.security.BiometricUnlockDelegate +import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate import eu.kanade.tachiyomi.util.system.LocaleHelper import org.acra.ACRA import org.acra.annotation.ReportsCrashes @@ -64,7 +64,7 @@ open class App : Application(), LifecycleObserver { fun onAppBackgrounded() { val preferences: PreferencesHelper by injectLazy() if (preferences.lockAppAfter().getOrDefault() >= 0) { - BiometricUnlockDelegate.locked = true + SecureActivityDelegate.locked = true } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index d714dbd4cd..48b5be4065 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -111,6 +111,8 @@ object PreferenceKeys { const val lastAppUnlock = "last_app_unlock" + const val secureScreen = "secure_screen" + const val downloadNew = "download_new" const val downloadNewCategories = "download_new_categories" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index d4500176c8..6dd69a2c86 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -58,6 +58,8 @@ class PreferencesHelper(val context: Context) { fun lastAppUnlock() = rxPrefs.getLong(Keys.lastAppUnlock, 0) + fun secureScreen() = rxPrefs.getBoolean(Keys.secureScreen, false) + fun clear() = prefs.edit().clear().apply() fun themeMode() = rxPrefs.getString(Keys.themeMode, Values.THEME_MODE_SYSTEM) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt index 2935982f10..e284d94b7c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt @@ -8,7 +8,7 @@ import androidx.appcompat.app.AppCompatDelegate import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault -import eu.kanade.tachiyomi.ui.security.BiometricUnlockDelegate +import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate import eu.kanade.tachiyomi.util.system.LocaleHelper import uy.kohesive.injekt.injectLazy import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values @@ -45,11 +45,14 @@ abstract class BaseActivity : AppCompatActivity() { }) super.onCreate(savedInstanceState) + + SecureActivityDelegate.onCreate(this) } override fun onResume() { super.onResume() - BiometricUnlockDelegate.onResume(this) + + SecureActivityDelegate.onResume(this) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt index 7d120f5cb1..5105ac0484 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt @@ -1,6 +1,8 @@ package eu.kanade.tachiyomi.ui.base.activity +import android.os.Bundle import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter +import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate import eu.kanade.tachiyomi.util.system.LocaleHelper import nucleus.view.NucleusAppCompatActivity @@ -11,4 +13,16 @@ abstract class BaseRxActivity

> : NucleusAppCompatActivity

() { initializeMenu() } - override fun onResume() { - super.onResume() - BiometricUnlockDelegate.onResume(this) - } - /** * Called when the activity is destroyed. Cleans up the viewer, configuration and any view. */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricUnlockActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricUnlockActivity.kt index fdfaad2b81..654628211a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricUnlockActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricUnlockActivity.kt @@ -28,7 +28,7 @@ class BiometricUnlockActivity : AppCompatActivity() { override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { super.onAuthenticationSucceeded(result) - BiometricUnlockDelegate.locked = false + SecureActivityDelegate.locked = false preferences.lastAppUnlock().set(Date().time) finish() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricUnlockDelegate.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/security/SecureActivityDelegate.kt similarity index 71% rename from app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricUnlockDelegate.kt rename to app/src/main/java/eu/kanade/tachiyomi/ui/security/SecureActivityDelegate.kt index ab684fa649..d9310c0864 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricUnlockDelegate.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/security/SecureActivityDelegate.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.security import android.content.Intent +import android.view.WindowManager import androidx.biometric.BiometricManager import androidx.fragment.app.FragmentActivity import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -8,12 +9,23 @@ import eu.kanade.tachiyomi.data.preference.getOrDefault import uy.kohesive.injekt.injectLazy import java.util.Date -object BiometricUnlockDelegate { +object SecureActivityDelegate { private val preferences by injectLazy() var locked: Boolean = true + fun onCreate(activity: FragmentActivity) { + preferences.secureScreen().asObservable() + .subscribe { + if (it) { + activity.window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE) + } else { + activity.window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) + } + } + } + fun onResume(activity: FragmentActivity) { val lockApp = preferences.useBiometricLock().getOrDefault() if (lockApp && BiometricManager.from(activity).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt index b82ead818c..b768def6df 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt @@ -117,10 +117,10 @@ class SettingsGeneralController : SettingsController() { summary = "%s" } - if (BiometricManager.from(context).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { - preferenceCategory { - titleRes = R.string.pref_category_security + preferenceCategory { + titleRes = R.string.pref_category_security + if (BiometricManager.from(context).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { switchPreference { key = Keys.useBiometricLock titleRes = R.string.lock_with_biometrics @@ -145,6 +145,12 @@ class SettingsGeneralController : SettingsController() { .subscribeUntilDestroy { isVisible = it } } } + + switchPreference { + key = Keys.secureScreen + titleRes = R.string.secure_screen + defaultValue = false + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 49a9e922e8..637b1bf8a1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -141,6 +141,7 @@ After 1 minute After %1$s minutes + Secure screen Display