diff --git a/app/src/main/java/com/kunzisoft/keepass/model/SnapFileDatabaseInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/SnapFileDatabaseInfo.kt index 4ef9acb3e..6d6bbbbd9 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/SnapFileDatabaseInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/SnapFileDatabaseInfo.kt @@ -42,6 +42,26 @@ data class SnapFileDatabaseInfo(var fileUri: Uri?, return 0 } + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is SnapFileDatabaseInfo) return false + + if (fileUri != other.fileUri) return false + if (exists != other.exists) return false + if (lastModification != other.lastModification) return false + if (size != other.size) return false + + return true + } + + override fun hashCode(): Int { + var result = fileUri?.hashCode() ?: 0 + result = 31 * result + exists.hashCode() + result = 31 * result + (lastModification?.hashCode() ?: 0) + result = 31 * result + (size?.hashCode() ?: 0) + return result + } + companion object CREATOR : Parcelable.Creator { override fun createFromParcel(parcel: Parcel): SnapFileDatabaseInfo { return SnapFileDatabaseInfo(parcel) diff --git a/app/src/main/java/com/kunzisoft/keepass/notifications/DatabaseTaskNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/notifications/DatabaseTaskNotificationService.kt index 3535fba62..b63b5ae54 100644 --- a/app/src/main/java/com/kunzisoft/keepass/notifications/DatabaseTaskNotificationService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/notifications/DatabaseTaskNotificationService.kt @@ -66,7 +66,6 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress private var mAllowFinishAction = AtomicBoolean() private var mActionRunning = false - private var mSnapFileDatabaseInfo: SnapFileDatabaseInfo? = null private var mDatabaseInfoListeners = LinkedList() private var mIconId: Int = R.drawable.notification_ic_database_load @@ -134,15 +133,28 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress val previousDatabaseInfo = mSnapFileDatabaseInfo val lastFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo( FileDatabaseInfo(applicationContext, it)) - if (previousDatabaseInfo != null) { - if (previousDatabaseInfo != lastFileDatabaseInfo) { - Log.i(TAG, "Database file modified " + - "$previousDatabaseInfo != $lastFileDatabaseInfo ") - // Call listener to indicate a change in database info + + val oldDatabaseModification = previousDatabaseInfo?.lastModification + val newDatabaseModification = lastFileDatabaseInfo.lastModification + + val conditionExists = previousDatabaseInfo != null + && previousDatabaseInfo.exists != lastFileDatabaseInfo.exists + // To prevent dialog opening too often + val conditionLastModification = (oldDatabaseModification != null && newDatabaseModification != null + && oldDatabaseModification < newDatabaseModification + && mLastLocalSaveTime + 5000 < newDatabaseModification) + + if (conditionExists || conditionLastModification) { + // Show the dialog only if it's real new info and not a delay after a save + Log.i(TAG, "Database file modified " + + "$previousDatabaseInfo != $lastFileDatabaseInfo ") + // Call listener to indicate a change in database info + if (previousDatabaseInfo != null) { mDatabaseInfoListeners.forEach { listener -> listener.onDatabaseInfoChanged(previousDatabaseInfo, lastFileDatabaseInfo) } } + mSnapFileDatabaseInfo = lastFileDatabaseInfo } } } @@ -151,6 +163,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress mDatabase.fileUri?.let { mSnapFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo( FileDatabaseInfo(applicationContext, it)) + Log.i(TAG, "Database file saved $mSnapFileDatabaseInfo") } } @@ -235,9 +248,21 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress actionTaskListener.onStopAction(intentAction!!, result) } } finally { + // Save the database info before performing action + if (intentAction == ACTION_DATABASE_LOAD_TASK) { + saveDatabaseInfo() + } + // Save the database info after performing save action + if (intentAction == ACTION_DATABASE_SAVE + || intent?.getBooleanExtra(SAVE_DATABASE_KEY, false) == true) { + mDatabase.fileUri?.let { + val newSnapFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo( + FileDatabaseInfo(applicationContext, it)) + mLastLocalSaveTime = System.currentTimeMillis() + mSnapFileDatabaseInfo = newSnapFileDatabaseInfo + } + } removeIntentData(intent) - // Save the database info after performing action - saveDatabaseInfo() TimeoutHelper.releaseTemporarilyDisableTimeout() if (TimeoutHelper.checkTimeAndLockIfTimeout(this@DatabaseTaskNotificationService)) { if (!mDatabase.loaded) { @@ -884,6 +909,9 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress const val OLD_ELEMENT_KEY = "OLD_ELEMENT_KEY" // Warning type of this thing change every time const val NEW_ELEMENT_KEY = "NEW_ELEMENT_KEY" // Warning type of this thing change every time + private var mSnapFileDatabaseInfo: SnapFileDatabaseInfo? = null + private var mLastLocalSaveTime: Long = 0 + fun getListNodesFromBundle(database: Database, bundle: Bundle): List { val nodesAction = ArrayList() bundle.getParcelableArrayList>(GROUPS_ID_KEY)?.forEach {