mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Fix lock timeout
This commit is contained in:
@@ -597,7 +597,7 @@ class PasswordActivity : StylishActivity(),
|
|||||||
ProgressDialogThread(this,
|
ProgressDialogThread(this,
|
||||||
{ progressTaskUpdater ->
|
{ progressTaskUpdater ->
|
||||||
LoadDatabaseRunnable(
|
LoadDatabaseRunnable(
|
||||||
WeakReference(this@PasswordActivity.applicationContext),
|
WeakReference(this@PasswordActivity),
|
||||||
database,
|
database,
|
||||||
databaseUri,
|
databaseUri,
|
||||||
password,
|
password,
|
||||||
|
|||||||
@@ -71,13 +71,12 @@ abstract class LockingActivity : StylishActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (timeoutEnable) {
|
if (timeoutEnable) {
|
||||||
if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(this)) {
|
lockReceiver = LockReceiver()
|
||||||
lockReceiver = LockReceiver()
|
val intentFilter = IntentFilter().apply {
|
||||||
val intentFilter = IntentFilter()
|
addAction(Intent.ACTION_SCREEN_OFF)
|
||||||
intentFilter.addAction(Intent.ACTION_SCREEN_OFF)
|
addAction(LOCK_ACTION)
|
||||||
intentFilter.addAction(LOCK_ACTION)
|
|
||||||
registerReceiver(lockReceiver, IntentFilter(intentFilter))
|
|
||||||
}
|
}
|
||||||
|
registerReceiver(lockReceiver, IntentFilter(intentFilter))
|
||||||
}
|
}
|
||||||
|
|
||||||
exitLock = false
|
exitLock = false
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import android.support.v4.app.FragmentActivity
|
|||||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||||
import com.kunzisoft.keepass.tasks.ProgressTaskDialogFragment
|
import com.kunzisoft.keepass.tasks.ProgressTaskDialogFragment
|
||||||
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
|
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
|
||||||
|
import com.kunzisoft.keepass.timeout.TimeoutHelper
|
||||||
|
|
||||||
open class ProgressDialogThread(private val activity: FragmentActivity,
|
open class ProgressDialogThread(private val activity: FragmentActivity,
|
||||||
private val actionRunnable: (ProgressTaskUpdater?)-> ActionRunnable,
|
private val actionRunnable: (ProgressTaskUpdater?)-> ActionRunnable,
|
||||||
@@ -24,6 +25,7 @@ open class ProgressDialogThread(private val activity: FragmentActivity,
|
|||||||
actionRunnableAsyncTask = ActionRunnableAsyncTask(progressTaskDialogFragment,
|
actionRunnableAsyncTask = ActionRunnableAsyncTask(progressTaskDialogFragment,
|
||||||
{
|
{
|
||||||
activity.runOnUiThread {
|
activity.runOnUiThread {
|
||||||
|
TimeoutHelper.temporarilyDisableTimeout()
|
||||||
// Show the dialog
|
// Show the dialog
|
||||||
ProgressTaskDialogFragment.start(activity, progressTaskDialogFragment)
|
ProgressTaskDialogFragment.start(activity, progressTaskDialogFragment)
|
||||||
}
|
}
|
||||||
@@ -32,6 +34,7 @@ open class ProgressDialogThread(private val activity: FragmentActivity,
|
|||||||
actionFinishInUIThread?.onFinishRun(result)
|
actionFinishInUIThread?.onFinishRun(result)
|
||||||
// Remove the progress task
|
// Remove the progress task
|
||||||
ProgressTaskDialogFragment.stop(activity)
|
ProgressTaskDialogFragment.stop(activity)
|
||||||
|
TimeoutHelper.releaseTemporarilyDisableTimeoutAndLockIfTimeout(activity)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,12 +20,9 @@
|
|||||||
package com.kunzisoft.keepass.database.action
|
package com.kunzisoft.keepass.database.action
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
|
||||||
import com.kunzisoft.keepass.database.element.Database
|
import com.kunzisoft.keepass.database.element.Database
|
||||||
import com.kunzisoft.keepass.database.exception.PwDbOutputException
|
import com.kunzisoft.keepass.database.exception.PwDbOutputException
|
||||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||||
import com.kunzisoft.keepass.timeout.TimeoutHelper
|
|
||||||
|
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
abstract class SaveDatabaseRunnable(protected var context: Context,
|
abstract class SaveDatabaseRunnable(protected var context: Context,
|
||||||
@@ -33,10 +30,6 @@ abstract class SaveDatabaseRunnable(protected var context: Context,
|
|||||||
private val save: Boolean,
|
private val save: Boolean,
|
||||||
nestedAction: ActionRunnable? = null) : ActionRunnable(nestedAction) {
|
nestedAction: ActionRunnable? = null) : ActionRunnable(nestedAction) {
|
||||||
|
|
||||||
init {
|
|
||||||
TimeoutHelper.temporarilyDisableTimeout()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO Service to prevent background thread kill
|
// TODO Service to prevent background thread kill
|
||||||
override fun run() {
|
override fun run() {
|
||||||
if (save) {
|
if (save) {
|
||||||
@@ -54,7 +47,6 @@ abstract class SaveDatabaseRunnable(protected var context: Context,
|
|||||||
|
|
||||||
override fun onFinishRun(result: Result) {
|
override fun onFinishRun(result: Result) {
|
||||||
// Need to call super.onFinishRun(result) in child class
|
// Need to call super.onFinishRun(result) in child class
|
||||||
TimeoutHelper.releaseTemporarilyDisableTimeoutAndLockIfTimeout(context)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ class KeyboardEntryNotificationService : Service() {
|
|||||||
TimeoutHelper.DEFAULT_TIMEOUT
|
TimeoutHelper.DEFAULT_TIMEOUT
|
||||||
}
|
}
|
||||||
|
|
||||||
if (notificationTimeoutMilliSecs != TimeoutHelper.TIMEOUT_NEVER) {
|
if (notificationTimeoutMilliSecs != TimeoutHelper.NEVER) {
|
||||||
stopTask(cleanNotificationTimer)
|
stopTask(cleanNotificationTimer)
|
||||||
cleanNotificationTimer = Thread {
|
cleanNotificationTimer = Thread {
|
||||||
val maxPos = 100
|
val maxPos = 100
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import android.content.SharedPreferences
|
|||||||
import android.preference.PreferenceManager
|
import android.preference.PreferenceManager
|
||||||
import com.kunzisoft.keepass.R
|
import com.kunzisoft.keepass.R
|
||||||
import com.kunzisoft.keepass.database.SortNodeEnum
|
import com.kunzisoft.keepass.database.SortNodeEnum
|
||||||
|
import com.kunzisoft.keepass.timeout.TimeoutHelper
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
object PreferencesUtil {
|
object PreferencesUtil {
|
||||||
@@ -86,6 +87,38 @@ object PreferencesUtil {
|
|||||||
context.resources.getBoolean(R.bool.clipboard_notifications_default))
|
context.resources.getBoolean(R.bool.clipboard_notifications_default))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save current time, can be retrieve with `getTimeSaved()`
|
||||||
|
*/
|
||||||
|
fun saveCurrentTime(context: Context) {
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(context).edit().apply {
|
||||||
|
putLong(context.getString(R.string.timeout_backup_key), System.currentTimeMillis())
|
||||||
|
apply()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Time previously saved in milliseconds (commonly used to compare with current time and check timeout)
|
||||||
|
*/
|
||||||
|
fun getTimeSaved(context: Context): Long {
|
||||||
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
|
return prefs.getLong(context.getString(R.string.timeout_backup_key),
|
||||||
|
TimeoutHelper.NEVER)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* App timeout selected in milliseconds
|
||||||
|
*/
|
||||||
|
fun getAppTimeout(context: Context): Long {
|
||||||
|
return try {
|
||||||
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
|
java.lang.Long.parseLong(prefs.getString(context.getString(R.string.app_timeout_key),
|
||||||
|
context.getString(R.string.clipboard_timeout_default)))
|
||||||
|
} catch (e: NumberFormatException) {
|
||||||
|
TimeoutHelper.DEFAULT_TIMEOUT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun isLockDatabaseWhenScreenShutOffEnable(context: Context): Boolean {
|
fun isLockDatabaseWhenScreenShutOffEnable(context: Context): Boolean {
|
||||||
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
return prefs.getBoolean(context.getString(R.string.lock_database_screen_off_key),
|
return prefs.getBoolean(context.getString(R.string.lock_database_screen_off_key),
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2018 Jeremy Jamet / Kunzisoft.
|
* Copyright 2019 Jeremy Jamet / Kunzisoft.
|
||||||
*
|
*
|
||||||
* This file is part of KeePass DX.
|
* This file is part of KeePass DX.
|
||||||
*
|
*
|
||||||
@@ -24,17 +24,17 @@ import android.app.AlarmManager
|
|||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.preference.PreferenceManager
|
import android.os.Build
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.kunzisoft.keepass.R
|
|
||||||
import com.kunzisoft.keepass.activities.lock.LockingActivity
|
import com.kunzisoft.keepass.activities.lock.LockingActivity
|
||||||
import com.kunzisoft.keepass.activities.lock.lock
|
import com.kunzisoft.keepass.activities.lock.lock
|
||||||
import com.kunzisoft.keepass.app.App
|
import com.kunzisoft.keepass.app.App
|
||||||
|
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||||
|
|
||||||
object TimeoutHelper {
|
object TimeoutHelper {
|
||||||
|
|
||||||
const val DEFAULT_TIMEOUT = (5 * 60 * 1000).toLong() // 5 minutes
|
const val DEFAULT_TIMEOUT = (5 * 60 * 1000).toLong() // 5 minutes
|
||||||
const val TIMEOUT_NEVER: Long = -1 // Infinite
|
const val NEVER: Long = -1 // Infinite
|
||||||
|
|
||||||
private const val REQUEST_ID = 140
|
private const val REQUEST_ID = 140
|
||||||
|
|
||||||
@@ -54,28 +54,22 @@ object TimeoutHelper {
|
|||||||
* Record the current time to check it later with checkTime
|
* Record the current time to check it later with checkTime
|
||||||
*/
|
*/
|
||||||
fun recordTime(context: Context) {
|
fun recordTime(context: Context) {
|
||||||
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
|
||||||
|
|
||||||
// Record timeout time in case timeout service is killed
|
// Record timeout time in case timeout service is killed
|
||||||
val time = System.currentTimeMillis()
|
PreferencesUtil.saveCurrentTime(context)
|
||||||
val edit = prefs.edit()
|
|
||||||
edit.putLong(context.getString(R.string.timeout_backup_key), time)
|
|
||||||
edit.apply()
|
|
||||||
|
|
||||||
if (App.currentDatabase.loaded) {
|
if (App.currentDatabase.loaded) {
|
||||||
val timeout = try {
|
val timeout = PreferencesUtil.getAppTimeout(context)
|
||||||
java.lang.Long.parseLong(prefs.getString(context.getString(R.string.app_timeout_key),
|
|
||||||
context.getString(R.string.clipboard_timeout_default)))
|
|
||||||
} catch (e: NumberFormatException) {
|
|
||||||
DEFAULT_TIMEOUT
|
|
||||||
}
|
|
||||||
|
|
||||||
// No timeout don't start timeout service
|
// No timeout don't start timeout service
|
||||||
if (timeout != TIMEOUT_NEVER) {
|
if (timeout != NEVER) {
|
||||||
val triggerTime = System.currentTimeMillis() + timeout
|
val triggerTime = System.currentTimeMillis() + timeout
|
||||||
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||||
Log.d(TAG, "TimeoutHelper start")
|
Log.d(TAG, "TimeoutHelper start")
|
||||||
am.set(AlarmManager.RTC, triggerTime, getLockPendingIntent(context))
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||||
|
am.setExact(AlarmManager.RTC, triggerTime, getLockPendingIntent(context))
|
||||||
|
} else {
|
||||||
|
am.set(AlarmManager.RTC, triggerTime, getLockPendingIntent(context))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -101,24 +95,16 @@ object TimeoutHelper {
|
|||||||
val currentTime = System.currentTimeMillis()
|
val currentTime = System.currentTimeMillis()
|
||||||
|
|
||||||
// Retrieve the timeout programmatically backup
|
// Retrieve the timeout programmatically backup
|
||||||
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
val timeoutBackup = PreferencesUtil.getTimeSaved(context)
|
||||||
val timeoutBackup = prefs.getLong(context.getString(R.string.timeout_backup_key),
|
|
||||||
TIMEOUT_NEVER)
|
|
||||||
// The timeout never started
|
// The timeout never started
|
||||||
if (timeoutBackup == TIMEOUT_NEVER) {
|
if (timeoutBackup == NEVER) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the app timeout in settings
|
// Retrieve the app timeout in settings
|
||||||
val appTimeout = try {
|
val appTimeout = PreferencesUtil.getAppTimeout((context))
|
||||||
java.lang.Long.parseLong(prefs.getString(context.getString(R.string.app_timeout_key),
|
|
||||||
context.getString(R.string.clipboard_timeout_default)))
|
|
||||||
} catch (e: NumberFormatException) {
|
|
||||||
DEFAULT_TIMEOUT
|
|
||||||
}
|
|
||||||
|
|
||||||
// We are set to never timeout
|
// We are set to never timeout
|
||||||
if (appTimeout == TIMEOUT_NEVER) {
|
if (appTimeout == NEVER) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user