mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-21 01:21:34 -08:00
Refactor database runnable and prevent activity to be closed during saving
This commit is contained in:
1 parent
2208c67b62
commit
620b1b4f02
9 files changed
+73
-68
No files matched your search
@@ -327,7 +327,7 @@ public class GroupActivity extends LockingActivity
|
||||
|
||||
private void openGroup(PwGroup group, boolean isASearch) {
|
||||
// Check TimeoutHelper
|
||||
TimeoutHelper.INSTANCE.lockOrResetTimeout(this, () -> {
|
||||
TimeoutHelper.INSTANCE.checkTimeAndLockIfTimeoutOrResetTimeout(this, () -> {
|
||||
// Open a group in a new fragment
|
||||
ListNodesFragment newListNodeFragment = ListNodesFragment.newInstance(group, getReadOnly(), isASearch);
|
||||
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
||||
|
||||
@@ -143,13 +143,14 @@ abstract class LockingActivity : StylishActivity() {
|
||||
inner class LockReceiver : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val action = intent.action
|
||||
if (action != null) {
|
||||
when (action) {
|
||||
Intent.ACTION_SCREEN_OFF -> if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(this@LockingActivity)) {
|
||||
lockAndExit()
|
||||
if (!TimeoutHelper.temporarilyDisableTimeout) {
|
||||
intent.action?.let {
|
||||
when (it) {
|
||||
Intent.ACTION_SCREEN_OFF -> if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(this@LockingActivity)) {
|
||||
lockAndExit()
|
||||
}
|
||||
LOCK_ACTION -> lockAndExit()
|
||||
}
|
||||
LOCK_ACTION -> lockAndExit()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -166,7 +167,7 @@ abstract class LockingActivity : StylishActivity() {
|
||||
views.forEach {
|
||||
it.setOnFocusChangeListener { _, hasFocus ->
|
||||
if (hasFocus) {
|
||||
TimeoutHelper.lockOrResetTimeout(this)
|
||||
TimeoutHelper.checkTimeAndLockIfTimeoutOrResetTimeout(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -174,7 +175,7 @@ abstract class LockingActivity : StylishActivity() {
|
||||
|
||||
override fun onBackPressed() {
|
||||
if (timeoutEnable) {
|
||||
TimeoutHelper.lockOrResetTimeout(this) {
|
||||
TimeoutHelper.checkTimeAndLockIfTimeoutOrResetTimeout(this) {
|
||||
super.onBackPressed()
|
||||
}
|
||||
} else {
|
||||
|
||||
-47
@@ -1,47 +0,0 @@
|
||||
package com.kunzisoft.keepass.database.action;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.kunzisoft.keepass.database.Database;
|
||||
|
||||
public abstract class ActionWithSaveDatabaseRunnable extends RunnableOnFinish {
|
||||
|
||||
protected Context mContext;
|
||||
protected boolean mDontSave;
|
||||
protected Database mDatabase;
|
||||
|
||||
public ActionWithSaveDatabaseRunnable(Context context, Database database, OnFinishRunnable finish, boolean dontSave) {
|
||||
super(finish);
|
||||
|
||||
this.mDatabase = database;
|
||||
this.mContext = context;
|
||||
this.mDontSave = dontSave;
|
||||
this.mFinish = new AfterActionRunnable(finish);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// Commit to disk
|
||||
SaveDatabaseRunnable save = new SaveDatabaseRunnable(mContext, mDatabase, mFinish, mDontSave);
|
||||
save.run();
|
||||
}
|
||||
|
||||
public void runWithoutSaveDatabase() {
|
||||
mFinish.run();
|
||||
}
|
||||
|
||||
abstract protected void onFinish(boolean success, String message);
|
||||
|
||||
private class AfterActionRunnable extends OnFinishRunnable {
|
||||
|
||||
AfterActionRunnable(OnFinishRunnable finish) {
|
||||
super(finish);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
onFinish(mSuccess, mMessage);
|
||||
super.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -30,7 +30,7 @@ import com.kunzisoft.keepass.utils.UriUtil;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class AssignPasswordInDatabaseRunnable extends ActionWithSaveDatabaseRunnable {
|
||||
public class AssignPasswordInDatabaseRunnable extends SaveDatabaseRunnable {
|
||||
|
||||
private String mPassword;
|
||||
private Uri mKeyfile;
|
||||
@@ -63,7 +63,8 @@ public class AssignPasswordInDatabaseRunnable extends ActionWithSaveDatabaseRunn
|
||||
} catch (InvalidKeyFileException|IOException e) {
|
||||
erase(mBackupKey);
|
||||
finish(false, e.getMessage());
|
||||
super.runWithoutSaveDatabase();
|
||||
// run without save database
|
||||
mFinish.run();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,13 +23,14 @@ import android.content.Context;
|
||||
|
||||
import com.kunzisoft.keepass.database.Database;
|
||||
import com.kunzisoft.keepass.database.exception.PwDbOutputException;
|
||||
import com.kunzisoft.keepass.timeout.TimeoutHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SaveDatabaseRunnable extends RunnableOnFinish {
|
||||
|
||||
private Context mContext;
|
||||
private Database mDatabase;
|
||||
protected Context mContext;
|
||||
protected Database mDatabase;
|
||||
private boolean mDontSave;
|
||||
|
||||
public SaveDatabaseRunnable(Context context, Database database, OnFinishRunnable finish, boolean dontSave) {
|
||||
@@ -38,6 +39,7 @@ public class SaveDatabaseRunnable extends RunnableOnFinish {
|
||||
this.mContext = context;
|
||||
this.mDatabase = database;
|
||||
this.mDontSave = dontSave;
|
||||
this.mFinish = new AfterActionRunnable(finish);
|
||||
}
|
||||
|
||||
public SaveDatabaseRunnable(Context ctx, Database db, OnFinishRunnable finish) {
|
||||
@@ -46,8 +48,8 @@ public class SaveDatabaseRunnable extends RunnableOnFinish {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
if ( ! mDontSave ) {
|
||||
TimeoutHelper.INSTANCE.temporarilyDisableTimeout();
|
||||
if (!mDontSave) {
|
||||
try {
|
||||
mDatabase.saveData(mContext);
|
||||
} catch (IOException e) {
|
||||
@@ -59,8 +61,27 @@ public class SaveDatabaseRunnable extends RunnableOnFinish {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
finish(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method called when the database finish to save data
|
||||
* @param success 'true' if save is ok, 'false' elsewhere
|
||||
* @param message
|
||||
*/
|
||||
protected void onFinish(boolean success, String message) {}
|
||||
|
||||
private class AfterActionRunnable extends OnFinishRunnable {
|
||||
|
||||
AfterActionRunnable(OnFinishRunnable finish) {
|
||||
super(finish);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
TimeoutHelper.INSTANCE.releaseTemporarilyDisableTimeoutAndLockIfTimeout(mContext);
|
||||
onFinish(mSuccess, mMessage);
|
||||
super.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -4,9 +4,9 @@ import android.content.Context;
|
||||
|
||||
import com.kunzisoft.keepass.database.Database;
|
||||
import com.kunzisoft.keepass.database.PwNode;
|
||||
import com.kunzisoft.keepass.database.action.ActionWithSaveDatabaseRunnable;
|
||||
import com.kunzisoft.keepass.database.action.SaveDatabaseRunnable;
|
||||
|
||||
abstract class ActionNodeDatabaseRunnable extends ActionWithSaveDatabaseRunnable {
|
||||
abstract class ActionNodeDatabaseRunnable extends SaveDatabaseRunnable {
|
||||
|
||||
private AfterActionNodeOnFinish callbackRunnable;
|
||||
|
||||
|
||||
@@ -117,7 +117,7 @@ open class SettingsActivity : LockingActivity(), MainPreferenceFragment.Callback
|
||||
|
||||
override fun onNestedPreferenceSelected(key: NestedSettingsFragment.Screen) {
|
||||
if (timeoutEnable)
|
||||
TimeoutHelper.lockOrResetTimeout(this) {
|
||||
TimeoutHelper.checkTimeAndLockIfTimeoutOrResetTimeout(this) {
|
||||
replaceFragment(key)
|
||||
}
|
||||
else
|
||||
|
||||
+1
-1
@@ -47,7 +47,7 @@ public abstract class DatabaseSavePreferenceDialogFragmentCompat extends InputP
|
||||
assert getActivity() != null;
|
||||
|
||||
if (database != null && afterSaveDatabase != null) {
|
||||
SaveDatabaseRunnable saveDatabaseRunnable = new SaveDatabaseRunnable(getContext(),
|
||||
SaveDatabaseRunnable saveDatabaseRunnable = new SaveDatabaseRunnable(getActivity(),
|
||||
database,
|
||||
afterSaveDatabase);
|
||||
saveDatabaseRunnable.setUpdateProgressTaskStatus(
|
||||
|
||||
@@ -40,6 +40,9 @@ object TimeoutHelper {
|
||||
|
||||
private const val TAG = "TimeoutHelper"
|
||||
|
||||
var temporarilyDisableTimeout = false
|
||||
private set
|
||||
|
||||
private fun getLockPendingIntent(context: Context): PendingIntent {
|
||||
return PendingIntent.getBroadcast(context,
|
||||
REQUEST_ID,
|
||||
@@ -79,9 +82,14 @@ object TimeoutHelper {
|
||||
|
||||
/**
|
||||
* Check the time previously record with recordTime and do the [timeoutAction] if timeout
|
||||
* if temporarilyDisableTimeout() is called, the function as no effect until releaseTemporarilyDisableTimeoutAndCheckTime() is called
|
||||
* return 'false' if timeout, 'true' if in time
|
||||
*/
|
||||
fun checkTime(context: Context, timeoutAction: (() -> Unit)? = null): Boolean {
|
||||
// No effect if temporarily disable
|
||||
if (temporarilyDisableTimeout)
|
||||
return true
|
||||
|
||||
// Cancel the lock PendingIntent
|
||||
if (App.getDB().loaded) {
|
||||
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
@@ -133,10 +141,31 @@ object TimeoutHelper {
|
||||
}
|
||||
}
|
||||
|
||||
fun lockOrResetTimeout(activity: Activity, action: (() -> Unit)? = null) {
|
||||
/**
|
||||
* Check the time previously record then, if timeout lock the activity, else reset the timer
|
||||
*/
|
||||
fun checkTimeAndLockIfTimeoutOrResetTimeout(activity: Activity, action: (() -> Unit)? = null) {
|
||||
if (checkTimeAndLockIfTimeout(activity)) {
|
||||
recordTime(activity)
|
||||
action?.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Temporarily disable timeout, checkTime() function always return true
|
||||
*/
|
||||
fun temporarilyDisableTimeout() {
|
||||
temporarilyDisableTimeout = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the temporarily disable timeout and directly call checkTime()
|
||||
*/
|
||||
fun releaseTemporarilyDisableTimeoutAndLockIfTimeout(context: Context): Boolean {
|
||||
temporarilyDisableTimeout = false
|
||||
return if (context is LockingActivity)
|
||||
checkTimeAndLockIfTimeout(context)
|
||||
else
|
||||
checkTime(context)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user