From fb8aafb69f252efbf9e58bdb80180b3afb59b866 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Tue, 8 Jun 2021 09:41:12 +0700 Subject: [PATCH] Enable secure screen when incognito mode is enabled (#5339) * Enable secure screen when incognito mode is enabled * Fix incognito banner not showing up after configuration changes --- .../ui/base/activity/BaseThemedActivity.kt | 13 +++++++++++++ .../eu/kanade/tachiyomi/ui/main/MainActivity.kt | 1 + .../tachiyomi/ui/security/SecureActivityDelegate.kt | 10 ++-------- .../kanade/tachiyomi/util/view/WindowExtensions.kt | 9 +++++++++ 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt index 209402c5e..79b0012f4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt @@ -4,11 +4,17 @@ import android.content.res.Configuration.UI_MODE_NIGHT_MASK import android.content.res.Configuration.UI_MODE_NIGHT_YES import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.lifecycleScope import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferenceValues.DarkThemeVariant import eu.kanade.tachiyomi.data.preference.PreferenceValues.LightThemeVariant import eu.kanade.tachiyomi.data.preference.PreferenceValues.ThemeMode import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.asImmediateFlow +import eu.kanade.tachiyomi.util.view.setSecureScreen +import kotlinx.coroutines.flow.launchIn +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy abstract class BaseThemedActivity : AppCompatActivity() { @@ -38,6 +44,13 @@ abstract class BaseThemedActivity : AppCompatActivity() { } } setTheme(themeId) + + Injekt.get().incognitoMode() + .asImmediateFlow { + window.setSecureScreen(it) + } + .launchIn(lifecycleScope) + super.onCreate(savedInstanceState) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index f4cac0a9c..1e1bd8e8a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -237,6 +237,7 @@ class MainActivity : BaseViewBindingActivity() { .asImmediateFlow { binding.downloadedOnly.isVisible = it } .launchIn(lifecycleScope) + binding.incognitoMode.isVisible = preferences.incognitoMode().get() preferences.incognitoMode().asFlow() .drop(1) .onEach { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/security/SecureActivityDelegate.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/security/SecureActivityDelegate.kt index d879188ee..59f5fc6b5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/security/SecureActivityDelegate.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/security/SecureActivityDelegate.kt @@ -1,11 +1,11 @@ package eu.kanade.tachiyomi.ui.security import android.content.Intent -import android.view.WindowManager import androidx.fragment.app.FragmentActivity import androidx.lifecycle.lifecycleScope import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.system.AuthenticatorUtil +import eu.kanade.tachiyomi.util.view.setSecureScreen import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import uy.kohesive.injekt.injectLazy @@ -17,13 +17,7 @@ class SecureActivityDelegate(private val activity: FragmentActivity) { fun onCreate() { preferences.secureScreen().asFlow() - .onEach { - if (it) { - activity.window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE) - } else { - activity.window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) - } - } + .onEach { activity.window.setSecureScreen(it || preferences.incognitoMode().get()) } .launchIn(activity.lifecycleScope) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt index cd3c29c24..cefe188d3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt @@ -5,6 +5,7 @@ import android.graphics.Color import android.os.Build import android.view.View import android.view.Window +import android.view.WindowManager import eu.kanade.tachiyomi.util.system.InternalResourceHelper import eu.kanade.tachiyomi.util.system.getResourceColor @@ -42,3 +43,11 @@ fun Window.setNavigationBarTransparentCompat(context: Context) { context.getResourceColor(android.R.attr.navigationBarColor, 0.7F) } } + +fun Window.setSecureScreen(enabled: Boolean) { + if (enabled) { + setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE) + } else { + clearFlags(WindowManager.LayoutParams.FLAG_SECURE) + } +}