Fix lock timeout

This commit is contained in:
J-Jamet
2019-07-26 11:22:23 +02:00
parent 9bbfeaba72
commit 63b80634c2
7 changed files with 59 additions and 46 deletions

View File

@@ -597,7 +597,7 @@ class PasswordActivity : StylishActivity(),
ProgressDialogThread(this,
{ progressTaskUpdater ->
LoadDatabaseRunnable(
WeakReference(this@PasswordActivity.applicationContext),
WeakReference(this@PasswordActivity),
database,
databaseUri,
password,

View File

@@ -71,13 +71,12 @@ abstract class LockingActivity : StylishActivity() {
}
if (timeoutEnable) {
if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(this)) {
lockReceiver = LockReceiver()
val intentFilter = IntentFilter()
intentFilter.addAction(Intent.ACTION_SCREEN_OFF)
intentFilter.addAction(LOCK_ACTION)
registerReceiver(lockReceiver, IntentFilter(intentFilter))
lockReceiver = LockReceiver()
val intentFilter = IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_OFF)
addAction(LOCK_ACTION)
}
registerReceiver(lockReceiver, IntentFilter(intentFilter))
}
exitLock = false

View File

@@ -6,6 +6,7 @@ import android.support.v4.app.FragmentActivity
import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.tasks.ProgressTaskDialogFragment
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
import com.kunzisoft.keepass.timeout.TimeoutHelper
open class ProgressDialogThread(private val activity: FragmentActivity,
private val actionRunnable: (ProgressTaskUpdater?)-> ActionRunnable,
@@ -24,6 +25,7 @@ open class ProgressDialogThread(private val activity: FragmentActivity,
actionRunnableAsyncTask = ActionRunnableAsyncTask(progressTaskDialogFragment,
{
activity.runOnUiThread {
TimeoutHelper.temporarilyDisableTimeout()
// Show the dialog
ProgressTaskDialogFragment.start(activity, progressTaskDialogFragment)
}
@@ -32,6 +34,7 @@ open class ProgressDialogThread(private val activity: FragmentActivity,
actionFinishInUIThread?.onFinishRun(result)
// Remove the progress task
ProgressTaskDialogFragment.stop(activity)
TimeoutHelper.releaseTemporarilyDisableTimeoutAndLockIfTimeout(activity)
}
})
}

View File

@@ -20,12 +20,9 @@
package com.kunzisoft.keepass.database.action
import android.content.Context
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.exception.PwDbOutputException
import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.timeout.TimeoutHelper
import java.io.IOException
abstract class SaveDatabaseRunnable(protected var context: Context,
@@ -33,10 +30,6 @@ abstract class SaveDatabaseRunnable(protected var context: Context,
private val save: Boolean,
nestedAction: ActionRunnable? = null) : ActionRunnable(nestedAction) {
init {
TimeoutHelper.temporarilyDisableTimeout()
}
// TODO Service to prevent background thread kill
override fun run() {
if (save) {
@@ -54,7 +47,6 @@ abstract class SaveDatabaseRunnable(protected var context: Context,
override fun onFinishRun(result: Result) {
// Need to call super.onFinishRun(result) in child class
TimeoutHelper.releaseTemporarilyDisableTimeoutAndLockIfTimeout(context)
}
}

View File

@@ -101,7 +101,7 @@ class KeyboardEntryNotificationService : Service() {
TimeoutHelper.DEFAULT_TIMEOUT
}
if (notificationTimeoutMilliSecs != TimeoutHelper.TIMEOUT_NEVER) {
if (notificationTimeoutMilliSecs != TimeoutHelper.NEVER) {
stopTask(cleanNotificationTimer)
cleanNotificationTimer = Thread {
val maxPos = 100

View File

@@ -24,6 +24,7 @@ import android.content.SharedPreferences
import android.preference.PreferenceManager
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.SortNodeEnum
import com.kunzisoft.keepass.timeout.TimeoutHelper
import java.util.*
object PreferencesUtil {
@@ -86,6 +87,38 @@ object PreferencesUtil {
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 {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
return prefs.getBoolean(context.getString(R.string.lock_database_screen_off_key),

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
* Copyright 2019 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
@@ -24,17 +24,17 @@ import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.preference.PreferenceManager
import android.os.Build
import android.util.Log
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.lock.LockingActivity
import com.kunzisoft.keepass.activities.lock.lock
import com.kunzisoft.keepass.app.App
import com.kunzisoft.keepass.settings.PreferencesUtil
object TimeoutHelper {
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
@@ -54,28 +54,22 @@ object TimeoutHelper {
* Record the current time to check it later with checkTime
*/
fun recordTime(context: Context) {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
// Record timeout time in case timeout service is killed
val time = System.currentTimeMillis()
val edit = prefs.edit()
edit.putLong(context.getString(R.string.timeout_backup_key), time)
edit.apply()
PreferencesUtil.saveCurrentTime(context)
if (App.currentDatabase.loaded) {
val timeout = try {
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
}
val timeout = PreferencesUtil.getAppTimeout(context)
// No timeout don't start timeout service
if (timeout != TIMEOUT_NEVER) {
if (timeout != NEVER) {
val triggerTime = System.currentTimeMillis() + timeout
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
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()
// Retrieve the timeout programmatically backup
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val timeoutBackup = prefs.getLong(context.getString(R.string.timeout_backup_key),
TIMEOUT_NEVER)
val timeoutBackup = PreferencesUtil.getTimeSaved(context)
// The timeout never started
if (timeoutBackup == TIMEOUT_NEVER) {
if (timeoutBackup == NEVER) {
return true
}
// Retrieve the app timeout in settings
val appTimeout = try {
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
}
val appTimeout = PreferencesUtil.getAppTimeout((context))
// We are set to never timeout
if (appTimeout == TIMEOUT_NEVER) {
if (appTimeout == NEVER) {
return true
}