SecureActivityDelegate.kt 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package eu.kanade.tachiyomi.ui.security
  2. import android.content.Intent
  3. import android.view.WindowManager
  4. import androidx.fragment.app.FragmentActivity
  5. import androidx.lifecycle.lifecycleScope
  6. import eu.kanade.tachiyomi.data.preference.PreferencesHelper
  7. import eu.kanade.tachiyomi.util.system.BiometricUtil
  8. import kotlinx.coroutines.flow.launchIn
  9. import kotlinx.coroutines.flow.onEach
  10. import uy.kohesive.injekt.injectLazy
  11. import java.util.Date
  12. class SecureActivityDelegate(private val activity: FragmentActivity) {
  13. private val preferences: PreferencesHelper by injectLazy()
  14. fun onCreate() {
  15. preferences.secureScreen().asFlow()
  16. .onEach {
  17. if (it) {
  18. activity.window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
  19. } else {
  20. activity.window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
  21. }
  22. }
  23. .launchIn(activity.lifecycleScope)
  24. }
  25. fun onResume() {
  26. if (preferences.useBiometricLock().get()) {
  27. if (BiometricUtil.isSupported(activity)) {
  28. if (isAppLocked()) {
  29. activity.startActivity(Intent(activity, BiometricUnlockActivity::class.java))
  30. activity.overridePendingTransition(0, 0)
  31. }
  32. } else {
  33. preferences.useBiometricLock().set(false)
  34. }
  35. }
  36. }
  37. private fun isAppLocked(): Boolean {
  38. if (!locked) {
  39. return false
  40. }
  41. return preferences.lockAppAfter().get() <= 0 ||
  42. Date().time >= preferences.lastAppUnlock().get() + 60 * 1000 * preferences.lockAppAfter().get()
  43. }
  44. companion object {
  45. var locked: Boolean = true
  46. }
  47. }