From 6c1c3ff87fee48a1c1506ff08068744faac29a6c Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 26 Mar 2020 13:38:20 +0100 Subject: [PATCH] New method to wait 1.5 seconds after screen turns off --- .../keepass/utils/BroadcastAction.kt | 43 +++++++++++++------ app/src/main/res/values/donottranslate.xml | 2 +- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt b/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt index 7e889f86d..31b7709d5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt +++ b/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt @@ -19,12 +19,15 @@ */ package com.kunzisoft.keepass.utils +import android.app.AlarmManager import android.app.NotificationManager +import android.app.PendingIntent import android.content.BroadcastReceiver import android.content.Context +import android.content.Context.ALARM_SERVICE import android.content.Intent import android.content.IntentFilter -import android.os.Handler +import android.os.Build import android.util.Log import com.kunzisoft.keepass.R import com.kunzisoft.keepass.database.element.Database @@ -42,26 +45,34 @@ const val REMOVE_ENTRY_MAGIKEYBOARD_ACTION = "com.kunzisoft.keepass.REMOVE_ENTRY class LockReceiver(var lockAction: () -> Unit) : BroadcastReceiver() { - private val screenOffHandler = Handler() - private var screenOffRunnable: Runnable? = null + var mLockPendingIntent: PendingIntent? = null override fun onReceive(context: Context, intent: Intent) { - - screenOffRunnable?.let { runnable -> - screenOffHandler.removeCallbacks(runnable) - } // If allowed, lock and exit if (!TimeoutHelper.temporarilyDisableTimeout) { intent.action?.let { when (it) { + Intent.ACTION_SCREEN_ON -> { + cancelLockPendingIntent(context) + } Intent.ACTION_SCREEN_OFF -> { if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(context)) { - screenOffRunnable = Runnable { - lockAction.invoke() - } + mLockPendingIntent = PendingIntent.getBroadcast(context, + 4575, + Intent(intent).apply { + action = LOCK_ACTION + }, + 0) // Launch the effective action after a small time - screenOffHandler.postDelayed(screenOffRunnable!!, - context.getString(R.string.timeout_screen_off).toLong()) + val first: Long = System.currentTimeMillis() + context.getString(R.string.timeout_screen_off).toLong() + val alarmManager = context.getSystemService(ALARM_SERVICE) as AlarmManager? + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + alarmManager?.setExact(AlarmManager.RTC_WAKEUP, first, mLockPendingIntent) + } else { + alarmManager?.set(AlarmManager.RTC_WAKEUP, first, mLockPendingIntent) + } + } else { + cancelLockPendingIntent(context) } } LOCK_ACTION, @@ -71,6 +82,14 @@ class LockReceiver(var lockAction: () -> Unit) : BroadcastReceiver() { } } } + + private fun cancelLockPendingIntent(context: Context) { + mLockPendingIntent?.let { + val alarmManager = context.getSystemService(ALARM_SERVICE) as AlarmManager? + alarmManager?.cancel(mLockPendingIntent) + mLockPendingIntent = null + } + } } fun Context.registerLockReceiver(lockReceiver: LockReceiver?, diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index 0f44c87e2..9fb0a4825 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -245,7 +245,7 @@ timeout_backup_key 300000 - 3000 + 1500 5000 10000