diff --git a/app/src/main/java/com/kunzisoft/keepass/crypto/keyDerivation/KdfEngine.kt b/app/src/main/java/com/kunzisoft/keepass/crypto/keyDerivation/KdfEngine.kt index b2ec56f42..29e7124f5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/crypto/keyDerivation/KdfEngine.kt +++ b/app/src/main/java/com/kunzisoft/keepass/crypto/keyDerivation/KdfEngine.kt @@ -22,9 +22,11 @@ package com.kunzisoft.keepass.crypto.keyDerivation import com.kunzisoft.keepass.database.ObjectNameResource import java.io.IOException +import java.io.Serializable import java.util.UUID -abstract class KdfEngine : ObjectNameResource { +// TODO Parcelable +abstract class KdfEngine : ObjectNameResource, Serializable { var uuid: UUID? = null diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/ProgressDialogThread.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/ProgressDialogThread.kt index 4c5ed8cf6..7bac595c2 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/ProgressDialogThread.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/ProgressDialogThread.kt @@ -9,6 +9,7 @@ import android.os.Bundle import android.os.IBinder import androidx.fragment.app.FragmentActivity import com.kunzisoft.keepass.app.database.CipherDatabaseEntity +import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine import com.kunzisoft.keepass.database.element.* import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_ASSIGN_PASSWORD_TASK @@ -19,7 +20,18 @@ import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Compa import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_DELETE_NODES_TASK import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_LOAD_TASK import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_MOVE_NODES_TASK -import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_COLOR_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_COMPRESSION_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_DEFAULT_USERNAME_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_DESCRIPTION_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_ENCRYPTION_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_ITERATIONS_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_KEY_DERIVATION_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_MAX_HISTORY_ITEMS_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_MAX_HISTORY_SIZE_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_MEMORY_USAGE_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_NAME_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_PARALLELISM_TASK import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_UPDATE_ENTRY_TASK import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_UPDATE_GROUP_TASK import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.getBundleFromListNodes @@ -172,6 +184,12 @@ class ProgressDialogThread(private val activity: FragmentActivity, } } + /* + ---- + Main methods + ---- + */ + fun startDatabaseCreate(databaseUri: Uri, masterPasswordChecked: Boolean, masterPassword: String?, @@ -187,10 +205,6 @@ class ProgressDialogThread(private val activity: FragmentActivity, , ACTION_DATABASE_CREATE_TASK) } - fun startDatabaseSave() { - start(null, ACTION_DATABASE_SAVE_TASK) - } - fun startDatabaseLoad(databaseUri: Uri, masterPassword: String?, keyFile: Uri?, @@ -222,6 +236,12 @@ class ProgressDialogThread(private val activity: FragmentActivity, , ACTION_DATABASE_ASSIGN_PASSWORD_TASK) } + /* + ---- + Nodes Actions + ---- + */ + fun startDatabaseCreateGroup(newGroup: GroupVersioned, parent: GroupVersioned, save: Boolean) { @@ -313,4 +333,124 @@ class ProgressDialogThread(private val activity: FragmentActivity, save: Boolean) { startDatabaseActionListNodes(ACTION_DATABASE_DELETE_NODES_TASK, nodesToDelete, null, save) } + + /* + ----------------- + Main Settings + ----------------- + */ + + fun startDatabaseSaveName(oldName: String, + newName: String) { + start(Bundle().apply { + putString(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldName) + putString(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newName) + } + , ACTION_DATABASE_SAVE_NAME_TASK) + } + + fun startDatabaseSaveDescription(oldDescription: String, + newDescription: String) { + start(Bundle().apply { + putString(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldDescription) + putString(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newDescription) + } + , ACTION_DATABASE_SAVE_DESCRIPTION_TASK) + } + + fun startDatabaseSaveDefaultUsername(oldDefaultUsername: String, + newDefaultUsername: String) { + start(Bundle().apply { + putString(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldDefaultUsername) + putString(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newDefaultUsername) + } + , ACTION_DATABASE_SAVE_DEFAULT_USERNAME_TASK) + } + + fun startDatabaseSaveColor(oldColor: String, + newColor: String) { + start(Bundle().apply { + putString(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldColor) + putString(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newColor) + } + , ACTION_DATABASE_SAVE_COLOR_TASK) + } + + fun startDatabaseSaveCompression(oldCompression: PwCompressionAlgorithm, + newCompression: PwCompressionAlgorithm) { + start(Bundle().apply { + putSerializable(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldCompression) + putSerializable(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newCompression) + } + , ACTION_DATABASE_SAVE_COMPRESSION_TASK) + } + + fun startDatabaseSaveMaxHistoryItems(oldMaxHistoryItems: Int, + newMaxHistoryItems: Int) { + start(Bundle().apply { + putInt(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldMaxHistoryItems) + putInt(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newMaxHistoryItems) + } + , ACTION_DATABASE_SAVE_MAX_HISTORY_ITEMS_TASK) + } + + fun startDatabaseSaveMaxHistorySize(oldMaxHistorySize: Long, + newMaxHistorySize: Long) { + start(Bundle().apply { + putLong(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldMaxHistorySize) + putLong(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newMaxHistorySize) + } + , ACTION_DATABASE_SAVE_MAX_HISTORY_SIZE_TASK) + } + + /* + ------------------- + Security Settings + ------------------- + */ + + fun startDatabaseSaveEncryption(oldEncryption: PwEncryptionAlgorithm, + newEncryption: PwEncryptionAlgorithm) { + start(Bundle().apply { + putSerializable(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldEncryption) + putSerializable(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newEncryption) + } + , ACTION_DATABASE_SAVE_ENCRYPTION_TASK) + } + + fun startDatabaseSaveKeyDerivation(oldKeyDerivation: KdfEngine, + newKeyDerivation: KdfEngine) { + start(Bundle().apply { + putSerializable(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldKeyDerivation) + putSerializable(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newKeyDerivation) + } + , ACTION_DATABASE_SAVE_KEY_DERIVATION_TASK) + } + + fun startDatabaseSaveIterations(oldIterations: Long, + newIterations: Long) { + start(Bundle().apply { + putLong(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldIterations) + putLong(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newIterations) + } + , ACTION_DATABASE_SAVE_ITERATIONS_TASK) + } + + fun startDatabaseSaveMemoryUsage(oldMemoryUsage: Long, + newMemoryUsage: Long) { + start(Bundle().apply { + putLong(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldMemoryUsage) + putLong(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newMemoryUsage) + } + , ACTION_DATABASE_SAVE_MEMORY_USAGE_TASK) + } + + fun startDatabaseSaveParallelism(oldParallelism: Int, + newParallelism: Int) { + start(Bundle().apply { + putInt(DatabaseTaskNotificationService.OLD_ELEMENT_KEY, oldParallelism) + putInt(DatabaseTaskNotificationService.NEW_ELEMENT_KEY, newParallelism) + } + , ACTION_DATABASE_SAVE_PARALLELISM_TASK) + } } \ No newline at end of file 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 c3ac2cdbd..e5f6efe51 100644 --- a/app/src/main/java/com/kunzisoft/keepass/notifications/DatabaseTaskNotificationService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/notifications/DatabaseTaskNotificationService.kt @@ -74,17 +74,8 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat val titleId: Int = when (intentAction) { ACTION_DATABASE_CREATE_TASK -> R.string.creating_database - ACTION_DATABASE_SAVE_TASK, - ACTION_DATABASE_ASSIGN_PASSWORD_TASK, - ACTION_DATABASE_CREATE_GROUP_TASK, - ACTION_DATABASE_UPDATE_GROUP_TASK, - ACTION_DATABASE_CREATE_ENTRY_TASK, - ACTION_DATABASE_UPDATE_ENTRY_TASK, - ACTION_DATABASE_COPY_NODES_TASK, - ACTION_DATABASE_MOVE_NODES_TASK, - ACTION_DATABASE_DELETE_NODES_TASK -> R.string.saving_database ACTION_DATABASE_LOAD_TASK -> R.string.loading_database - else -> R.string.loading_database + else -> R.string.saving_database } val messageId: Int? = when (intentAction) { ACTION_DATABASE_LOAD_TASK -> null @@ -98,7 +89,6 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat val actionRunnable: ActionRunnable? = when (intentAction) { ACTION_DATABASE_CREATE_TASK -> buildDatabaseCreateActionTask(intent) - ACTION_DATABASE_SAVE_TASK -> buildDatabaseSaveActionTask() ACTION_DATABASE_LOAD_TASK -> buildDatabaseLoadActionTask(intent) ACTION_DATABASE_ASSIGN_PASSWORD_TASK -> buildDatabaseAssignPasswordActionTask(intent) ACTION_DATABASE_CREATE_GROUP_TASK -> buildDatabaseCreateGroupActionTask(intent) @@ -108,58 +98,54 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat ACTION_DATABASE_COPY_NODES_TASK -> buildDatabaseCopyNodesActionTask(intent) ACTION_DATABASE_MOVE_NODES_TASK -> buildDatabaseMoveNodesActionTask(intent) ACTION_DATABASE_DELETE_NODES_TASK -> buildDatabaseDeleteNodesActionTask(intent) + ACTION_DATABASE_SAVE_NAME_TASK, + ACTION_DATABASE_SAVE_DESCRIPTION_TASK, + ACTION_DATABASE_SAVE_DEFAULT_USERNAME_TASK, + ACTION_DATABASE_SAVE_COLOR_TASK, + ACTION_DATABASE_SAVE_COMPRESSION_TASK, + ACTION_DATABASE_SAVE_MAX_HISTORY_ITEMS_TASK, + ACTION_DATABASE_SAVE_MAX_HISTORY_SIZE_TASK, + ACTION_DATABASE_SAVE_ENCRYPTION_TASK, + ACTION_DATABASE_SAVE_KEY_DERIVATION_TASK, + ACTION_DATABASE_SAVE_MEMORY_USAGE_TASK, + ACTION_DATABASE_SAVE_PARALLELISM_TASK, + ACTION_DATABASE_SAVE_ITERATIONS_TASK -> buildDatabaseSaveElementActionTask(intent) else -> null } - when (intentAction) { - ACTION_DATABASE_CREATE_TASK, - ACTION_DATABASE_SAVE_TASK, - ACTION_DATABASE_LOAD_TASK, - ACTION_DATABASE_ASSIGN_PASSWORD_TASK, - ACTION_DATABASE_CREATE_GROUP_TASK, - ACTION_DATABASE_UPDATE_GROUP_TASK, - ACTION_DATABASE_CREATE_ENTRY_TASK, - ACTION_DATABASE_UPDATE_ENTRY_TASK, - ACTION_DATABASE_COPY_NODES_TASK, - ACTION_DATABASE_MOVE_NODES_TASK, - ACTION_DATABASE_DELETE_NODES_TASK -> { + actionRunnable?.let { actionRunnableNotNull -> + // Assign elements for updates + mTitleId = titleId + mMessageId = messageId + mWarningId = warningId - // Assign elements for updates - mTitleId = titleId - mMessageId = messageId - mWarningId = warningId + // Create the notification + newNotification(intent.getIntExtra(DATABASE_TASK_TITLE_KEY, titleId)) - // Create the notification - newNotification(intent.getIntExtra(DATABASE_TASK_TITLE_KEY, titleId)) + // Build and launch the action + actionRunnableAsyncTask = ActionRunnableAsyncTask(this, + { + sendBroadcast(Intent(DATABASE_START_TASK_ACTION).apply { + putExtra(DATABASE_TASK_TITLE_KEY, titleId) + putExtra(DATABASE_TASK_MESSAGE_KEY, messageId) + putExtra(DATABASE_TASK_WARNING_KEY, warningId) + }) - // Build and launch the action - actionRunnableAsyncTask = ActionRunnableAsyncTask(this, - { - sendBroadcast(Intent(DATABASE_START_TASK_ACTION).apply { - putExtra(DATABASE_TASK_TITLE_KEY, titleId) - putExtra(DATABASE_TASK_MESSAGE_KEY, messageId) - putExtra(DATABASE_TASK_WARNING_KEY, warningId) - }) - - mActionTaskListeners.forEach { actionTaskListener -> - actionTaskListener.onStartAction(titleId, messageId, warningId) - } - - }, { result -> - mActionTaskListeners.forEach { actionTaskListener -> - actionTaskListener.onStopAction(intentAction, result) - } - - sendBroadcast(Intent(DATABASE_STOP_TASK_ACTION)) - - stopSelf() + mActionTaskListeners.forEach { actionTaskListener -> + actionTaskListener.onStartAction(titleId, messageId, warningId) } - ) - actionRunnable?.let { actionRunnableNotNull -> - actionRunnableAsyncTask?.execute({ actionRunnableNotNull }) + + }, { result -> + mActionTaskListeners.forEach { actionTaskListener -> + actionTaskListener.onStopAction(intentAction!!, result) + } + + sendBroadcast(Intent(DATABASE_STOP_TASK_ACTION)) + + stopSelf() } - } - else -> {} + ) + actionRunnableAsyncTask?.execute({ actionRunnableNotNull }) } return START_REDELIVER_INTENT @@ -221,12 +207,6 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat } } - private fun buildDatabaseSaveActionTask(): ActionRunnable? { - return SaveDatabaseActionRunnable(this, - Database.getInstance(), - true) - } - private fun buildDatabaseLoadActionTask(intent: Intent): ActionRunnable? { if (intent.hasExtra(DATABASE_URI_KEY) @@ -288,6 +268,16 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat } } + private inner class AfterActionNodeRunnable : AfterActionNodeFinishRunnable() { + override fun onActionNodeFinish(actionNodeValues: ActionNodeValues) { + // TODO Encapsulate + val bundle = actionNodeValues.result.data ?: Bundle() + bundle.putBundle(OLD_NODES_KEY, getBundleFromListNodes(actionNodeValues.oldNodes)) + bundle.putBundle(NEW_NODES_KEY, getBundleFromListNodes(actionNodeValues.newNodes)) + actionNodeValues.result.data = bundle + } + } + private fun buildDatabaseCreateGroupActionTask(intent: Intent): ActionRunnable? { return if (intent.hasExtra(GROUP_KEY) && intent.hasExtra(PARENT_ID_KEY) @@ -366,16 +356,6 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat } } - private inner class AfterActionNodeRunnable : AfterActionNodeFinishRunnable() { - override fun onActionNodeFinish(actionNodeValues: ActionNodeValues) { - // TODO Encapsulate - val bundle = actionNodeValues.result.data ?: Bundle() - bundle.putBundle(OLD_NODES_KEY, getBundleFromListNodes(actionNodeValues.oldNodes)) - bundle.putBundle(NEW_NODES_KEY, getBundleFromListNodes(actionNodeValues.newNodes)) - actionNodeValues.result.data = bundle - } - } - private fun buildDatabaseCopyNodesActionTask(intent: Intent): ActionRunnable? { return if (intent.hasExtra(GROUPS_ID_KEY) && intent.hasExtra(ENTRIES_ID_KEY) @@ -435,6 +415,17 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat } } + private fun buildDatabaseSaveElementActionTask(intent: Intent): ActionRunnable? { + return SaveDatabaseActionRunnable(this, + Database.getInstance(), + true, + object: ActionRunnable() { + override fun onFinishRun(result: Result) { + result.data = intent.extras + } + }) + } + private class ActionRunnableAsyncTask(private val progressTaskUpdater: ProgressTaskUpdater, private val onPreExecute: () -> Unit, private val onPostExecute: (result: ActionRunnable.Result) -> Unit) @@ -471,7 +462,6 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat const val DATABASE_TASK_WARNING_KEY = "DATABASE_TASK_WARNING_KEY" const val ACTION_DATABASE_CREATE_TASK = "ACTION_DATABASE_CREATE_TASK" - const val ACTION_DATABASE_SAVE_TASK = "ACTION_DATABASE_SAVE_TASK" const val ACTION_DATABASE_LOAD_TASK = "ACTION_DATABASE_LOAD_TASK" const val ACTION_DATABASE_ASSIGN_PASSWORD_TASK = "ACTION_DATABASE_ASSIGN_PASSWORD_TASK" const val ACTION_DATABASE_CREATE_GROUP_TASK = "ACTION_DATABASE_CREATE_GROUP_TASK" @@ -481,6 +471,18 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat const val ACTION_DATABASE_COPY_NODES_TASK = "ACTION_DATABASE_COPY_NODES_TASK" const val ACTION_DATABASE_MOVE_NODES_TASK = "ACTION_DATABASE_MOVE_NODES_TASK" const val ACTION_DATABASE_DELETE_NODES_TASK = "ACTION_DATABASE_DELETE_NODES_TASK" + const val ACTION_DATABASE_SAVE_NAME_TASK = "ACTION_DATABASE_SAVE_NAME_TASK" + const val ACTION_DATABASE_SAVE_DESCRIPTION_TASK = "ACTION_DATABASE_SAVE_DESCRIPTION_TASK" + const val ACTION_DATABASE_SAVE_DEFAULT_USERNAME_TASK = "ACTION_DATABASE_SAVE_DEFAULT_USERNAME_TASK" + const val ACTION_DATABASE_SAVE_COLOR_TASK = "ACTION_DATABASE_SAVE_COLOR_TASK" + const val ACTION_DATABASE_SAVE_COMPRESSION_TASK = "ACTION_DATABASE_SAVE_COMPRESSION_TASK" + const val ACTION_DATABASE_SAVE_MAX_HISTORY_ITEMS_TASK = "ACTION_DATABASE_SAVE_MAX_HISTORY_ITEMS_TASK" + const val ACTION_DATABASE_SAVE_MAX_HISTORY_SIZE_TASK = "ACTION_DATABASE_SAVE_MAX_HISTORY_SIZE_TASK" + const val ACTION_DATABASE_SAVE_ENCRYPTION_TASK = "ACTION_DATABASE_SAVE_ENCRYPTION_TASK" + const val ACTION_DATABASE_SAVE_KEY_DERIVATION_TASK = "ACTION_DATABASE_SAVE_KEY_DERIVATION_TASK" + const val ACTION_DATABASE_SAVE_MEMORY_USAGE_TASK = "ACTION_DATABASE_SAVE_MEMORY_USAGE_TASK" + const val ACTION_DATABASE_SAVE_PARALLELISM_TASK = "ACTION_DATABASE_SAVE_PARALLELISM_TASK" + const val ACTION_DATABASE_SAVE_ITERATIONS_TASK = "ACTION_DATABASE_SAVE_ITERATIONS_TASK" const val DATABASE_URI_KEY = "DATABASE_URI_KEY" const val MASTER_PASSWORD_CHECKED_KEY = "MASTER_PASSWORD_CHECKED_KEY" @@ -500,6 +502,8 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat const val SAVE_DATABASE_KEY = "SAVE_DATABASE_KEY" const val OLD_NODES_KEY = "OLD_NODES_KEY" const val NEW_NODES_KEY = "NEW_NODES_KEY" + 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 fun getListNodesFromBundle(database: Database, bundle: Bundle): List { val nodesAction = ArrayList() diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/NestedSettingsFragment.kt b/app/src/main/java/com/kunzisoft/keepass/settings/NestedSettingsFragment.kt index 04390d4e5..ff3197948 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/NestedSettingsFragment.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/NestedSettingsFragment.kt @@ -47,13 +47,30 @@ import com.kunzisoft.keepass.activities.stylish.Stylish import com.kunzisoft.keepass.app.database.CipherDatabaseAction import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction import com.kunzisoft.keepass.biometric.BiometricUnlockDatabaseHelper +import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.PwCompressionAlgorithm +import com.kunzisoft.keepass.database.element.PwEncryptionAlgorithm import com.kunzisoft.keepass.education.Education import com.kunzisoft.keepass.icons.IconPackChooser +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_COLOR_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_COMPRESSION_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_DEFAULT_USERNAME_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_DESCRIPTION_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_ENCRYPTION_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_ITERATIONS_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_KEY_DERIVATION_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_MAX_HISTORY_ITEMS_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_MAX_HISTORY_SIZE_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_MEMORY_USAGE_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_NAME_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_PARALLELISM_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.NEW_ELEMENT_KEY +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.OLD_ELEMENT_KEY import com.kunzisoft.keepass.settings.preference.* import com.kunzisoft.keepass.settings.preference.DialogColorPreference.Companion.DISABLE_COLOR import com.kunzisoft.keepass.settings.preferencedialogfragment.* +import com.kunzisoft.keepass.tasks.ActionRunnable class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceClickListener { @@ -62,7 +79,15 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen private var mCount = 0 + private var dbNamePref: InputTextPreference? = null + private var dbDescriptionPref: InputTextPreference? = null + private var dbDefaultUsername: InputTextPreference? = null private var dbCustomColorPref: DialogColorPreference? = null + private var dbDataCompressionPref: Preference? = null + private var dbMaxHistoryItemsPref: InputNumberPreference? = null + private var dbMaxHistorySizePref: InputNumberPreference? = null + private var mEncryptionAlgorithmPref: DialogListExplanationPreference? = null + private var mKeyDerivationPref: DialogListExplanationPreference? = null private var mRoundPref: InputKdfNumberPreference? = null private var mMemoryPref: InputKdfNumberPreference? = null private var mParallelismPref: InputKdfNumberPreference? = null @@ -349,7 +374,7 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen val dbGeneralPrefCategory: PreferenceCategory? = findPreference(getString(R.string.database_category_general_key)) // Database name - val dbNamePref: InputTextPreference? = findPreference(getString(R.string.database_name_key)) + dbNamePref = findPreference(getString(R.string.database_name_key)) if (mDatabase.allowName) { dbNamePref?.summary = mDatabase.name } else { @@ -357,7 +382,7 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen } // Database description - val dbDescriptionPref: InputTextPreference? = findPreference(getString(R.string.database_description_key)) + dbDescriptionPref = findPreference(getString(R.string.database_description_key)) if (mDatabase.allowDescription) { dbDescriptionPref?.summary = mDatabase.description } else { @@ -365,7 +390,7 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen } // Database default username - val dbDefaultUsername: InputTextPreference? = findPreference(getString(R.string.database_default_username_key)) + dbDefaultUsername = findPreference(getString(R.string.database_default_username_key)) if (mDatabase.allowDefaultUsername) { dbDefaultUsername?.summary = mDatabase.defaultUsername } else { @@ -397,9 +422,9 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen val dbCompressionPrefCategory: PreferenceCategory? = findPreference(getString(R.string.database_category_compression_key)) // Database compression - val databaseDataCompressionPref = findPreference(getString(R.string.database_data_compression_key)) + dbDataCompressionPref = findPreference(getString(R.string.database_data_compression_key)) if (mDatabase.allowDataCompression) { - databaseDataCompressionPref?.summary = (mDatabase.compressionAlgorithm + dbDataCompressionPref?.summary = (mDatabase.compressionAlgorithm ?: PwCompressionAlgorithm.None).getName(resources) } else { dbCompressionPrefCategory?.isVisible = false @@ -421,12 +446,14 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen ?.isVisible = mDatabase.manageHistory == true // Max history items - findPreference(getString(R.string.max_history_items_key)) - ?.summary = mDatabase.historyMaxItems.toString() + dbMaxHistoryItemsPref = findPreference(getString(R.string.max_history_items_key))?.apply { + summary = mDatabase.historyMaxItems.toString() + } // Max history size - findPreference(getString(R.string.max_history_size_key)) - ?.summary = mDatabase.historyMaxSize.toString() + dbMaxHistorySizePref = findPreference(getString(R.string.max_history_size_key))?.apply { + summary = mDatabase.historyMaxSize.toString() + } } else { Log.e(javaClass.name, "Database isn't ready") @@ -438,24 +465,29 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen if (mDatabase.loaded) { // Encryption Algorithm - findPreference(getString(R.string.encryption_algorithm_key)) - ?.summary = mDatabase.getEncryptionAlgorithmName(resources) + mEncryptionAlgorithmPref = findPreference(getString(R.string.encryption_algorithm_key))?.apply { + summary = mDatabase.getEncryptionAlgorithmName(resources) + } // Key derivation function - findPreference(getString(R.string.key_derivation_function_key)) - ?.summary = mDatabase.getKeyDerivationName(resources) + mKeyDerivationPref = findPreference(getString(R.string.key_derivation_function_key))?.apply { + summary = mDatabase.getKeyDerivationName(resources) + } // Round encryption - mRoundPref = findPreference(getString(R.string.transform_rounds_key)) - mRoundPref?.summary = mDatabase.numberKeyEncryptionRounds.toString() + mRoundPref = findPreference(getString(R.string.transform_rounds_key))?.apply { + summary = mDatabase.numberKeyEncryptionRounds.toString() + } // Memory Usage - mMemoryPref = findPreference(getString(R.string.memory_usage_key)) - mMemoryPref?.summary = mDatabase.memoryUsage.toString() + mMemoryPref = findPreference(getString(R.string.memory_usage_key))?.apply { + summary = mDatabase.memoryUsage.toString() + } // Parallelism - mParallelismPref = findPreference(getString(R.string.parallelism_key)) - mParallelismPref?.summary = mDatabase.parallelism.toString() + mParallelismPref = findPreference(getString(R.string.parallelism_key))?.apply { + summary = mDatabase.parallelism.toString() + } } else { Log.e(javaClass.name, "Database isn't ready") } @@ -551,6 +583,178 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen return view } + fun onProgressDialogThreadResult(actionTask: String, + result: ActionRunnable.Result) { + result.data?.let { data -> + if (data.containsKey(OLD_ELEMENT_KEY) + && data.containsKey(NEW_ELEMENT_KEY)) { + when (actionTask) { + /* + -------- + Main preferences + -------- + */ + ACTION_DATABASE_SAVE_NAME_TASK -> { + val oldName = data.getString(OLD_ELEMENT_KEY)!! + val newName = data.getString(NEW_ELEMENT_KEY)!! + val nameToShow = + if (result.isSuccess) { + newName + } else { + mDatabase.name = oldName + oldName + } + dbNamePref?.summary = nameToShow + } + ACTION_DATABASE_SAVE_DESCRIPTION_TASK -> { + val oldDescription = data.getString(OLD_ELEMENT_KEY)!! + val newDescription = data.getString(NEW_ELEMENT_KEY)!! + val descriptionToShow = + if (result.isSuccess) { + newDescription + } else { + mDatabase.description = oldDescription + oldDescription + } + dbDescriptionPref?.summary = descriptionToShow + } + ACTION_DATABASE_SAVE_DEFAULT_USERNAME_TASK -> { + val oldDefaultUsername = data.getString(OLD_ELEMENT_KEY)!! + val newDefaultUsername = data.getString(NEW_ELEMENT_KEY)!! + val defaultUsernameToShow = + if (result.isSuccess) { + newDefaultUsername + } else { + mDatabase.defaultUsername = oldDefaultUsername + oldDefaultUsername + } + dbDefaultUsername?.summary = defaultUsernameToShow + } + ACTION_DATABASE_SAVE_COLOR_TASK -> { + val oldColor = data.getString(OLD_ELEMENT_KEY)!! + val newColor = data.getString(NEW_ELEMENT_KEY)!! + + val defaultColorToShow = + if (result.isSuccess) { + newColor + } else { + mDatabase.customColor = oldColor + oldColor + } + dbCustomColorPref?.summary = defaultColorToShow + } + ACTION_DATABASE_SAVE_COMPRESSION_TASK -> { + val oldCompression = data.getSerializable(OLD_ELEMENT_KEY) as PwCompressionAlgorithm + val newCompression = data.getSerializable(NEW_ELEMENT_KEY) as PwCompressionAlgorithm + val algorithmToShow = + if (result.isSuccess) { + newCompression + } else { + mDatabase.compressionAlgorithm = oldCompression + oldCompression + } + dbDataCompressionPref?.summary = algorithmToShow.getName(resources) + } + ACTION_DATABASE_SAVE_MAX_HISTORY_ITEMS_TASK -> { + val oldMaxHistoryItems = data.getInt(OLD_ELEMENT_KEY) + val newMaxHistoryItems = data.getInt(NEW_ELEMENT_KEY) + val maxHistoryItemsToShow = + if (result.isSuccess) { + newMaxHistoryItems + } else { + mDatabase.historyMaxItems = oldMaxHistoryItems + oldMaxHistoryItems + } + dbMaxHistoryItemsPref?.summary = maxHistoryItemsToShow.toString() + } + ACTION_DATABASE_SAVE_MAX_HISTORY_SIZE_TASK -> { + val oldMaxHistorySize = data.getLong(OLD_ELEMENT_KEY) + val newMaxHistorySize = data.getLong(NEW_ELEMENT_KEY) + val maxHistorySizeToShow = + if (result.isSuccess) { + newMaxHistorySize + } else { + mDatabase.historyMaxSize = oldMaxHistorySize + oldMaxHistorySize + } + dbMaxHistorySizePref?.summary = maxHistorySizeToShow.toString() + } + + /* + -------- + Security + -------- + */ + ACTION_DATABASE_SAVE_ENCRYPTION_TASK -> { + val oldEncryption = data.getSerializable(OLD_ELEMENT_KEY) as PwEncryptionAlgorithm + val newEncryption = data.getSerializable(NEW_ELEMENT_KEY) as PwEncryptionAlgorithm + val algorithmToShow = + if (result.isSuccess) { + newEncryption + } else { + mDatabase.encryptionAlgorithm = oldEncryption + oldEncryption + } + mEncryptionAlgorithmPref?.summary = algorithmToShow.getName(resources) + } + ACTION_DATABASE_SAVE_KEY_DERIVATION_TASK -> { + val oldKeyDerivationEngine = data.getSerializable(OLD_ELEMENT_KEY) as KdfEngine + val newKeyDerivationEngine = data.getSerializable(NEW_ELEMENT_KEY) as KdfEngine + val kdfEngineToShow = + if (result.isSuccess) { + newKeyDerivationEngine + } else { + mDatabase.kdfEngine = oldKeyDerivationEngine + oldKeyDerivationEngine + } + mKeyDerivationPref?.summary = kdfEngineToShow.getName(resources) + + mRoundPref?.summary = kdfEngineToShow.defaultKeyRounds.toString() + // Disable memory and parallelism if not available + mMemoryPref?.summary = kdfEngineToShow.defaultMemoryUsage.toString() + mParallelismPref?.summary = kdfEngineToShow.defaultParallelism.toString() + } + ACTION_DATABASE_SAVE_ITERATIONS_TASK -> { + val oldIterations = data.getLong(OLD_ELEMENT_KEY) + val newIterations = data.getLong(NEW_ELEMENT_KEY) + val roundsToShow = + if (result.isSuccess) { + newIterations + } else { + mDatabase.numberKeyEncryptionRounds = oldIterations + oldIterations + } + mRoundPref?.summary = roundsToShow.toString() + } + ACTION_DATABASE_SAVE_MEMORY_USAGE_TASK -> { + val oldMemoryUsage = data.getLong(OLD_ELEMENT_KEY) + val newMemoryUsage = data.getLong(NEW_ELEMENT_KEY) + val memoryToShow = + if (result.isSuccess) { + newMemoryUsage + } else { + mDatabase.memoryUsage = oldMemoryUsage + oldMemoryUsage + } + mMemoryPref?.summary = memoryToShow.toString() + } + ACTION_DATABASE_SAVE_PARALLELISM_TASK -> { + val oldParallelism = data.getInt(OLD_ELEMENT_KEY) + val newParallelism = data.getInt(NEW_ELEMENT_KEY) + val parallelismToShow = + if (result.isSuccess) { + newParallelism + } else { + mDatabase.parallelism = oldParallelism + oldParallelism + } + mParallelismPref?.summary = parallelismToShow.toString() + } + } + } + } + } + override fun onDisplayPreferenceDialog(preference: Preference?) { var otherDialogFragment = false @@ -559,6 +763,7 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen preference?.let { preference -> var dialogFragment: DialogFragment? = null when { + // Main Preferences preference.key == getString(R.string.database_name_key) -> { dialogFragment = DatabaseNamePreferenceDialogFragmentCompat.newInstance(preference.key) } @@ -582,6 +787,8 @@ class NestedSettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferen preference.key == getString(R.string.max_history_size_key) -> { dialogFragment = MaxHistorySizePreferenceDialogFragmentCompat.newInstance(preference.key) } + + // Security preference.key == getString(R.string.encryption_algorithm_key) -> { dialogFragment = DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat.newInstance(preference.key) } diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/SettingsActivity.kt b/app/src/main/java/com/kunzisoft/keepass/settings/SettingsActivity.kt index ea0067edc..eb29b9312 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/SettingsActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/SettingsActivity.kt @@ -26,7 +26,6 @@ import android.content.Intent import android.net.Uri import android.os.Bundle import android.view.MenuItem -import android.widget.Toast import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment import com.kunzisoft.keepass.R @@ -36,8 +35,6 @@ import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper import com.kunzisoft.keepass.activities.lock.LockingActivity import com.kunzisoft.keepass.database.action.ProgressDialogThread import com.kunzisoft.keepass.database.element.Database -import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_ASSIGN_PASSWORD_TASK -import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_SAVE_TASK import com.kunzisoft.keepass.timeout.TimeoutHelper open class SettingsActivity @@ -94,18 +91,10 @@ open class SettingsActivity backupManager = BackupManager(this) progressDialogThread = ProgressDialogThread(this) { actionTask, result -> - when (actionTask) { - ACTION_DATABASE_ASSIGN_PASSWORD_TASK -> { - // Nothing here - } - ACTION_DATABASE_SAVE_TASK -> { - if (!result.isSuccess) { - // TODO Log.e(TAG, result.message) - Toast.makeText(this, result.message, Toast.LENGTH_SHORT).show() - } - // TODO actionAfterSaveDatabase?.onFinishRun(result) - } - } + // Call result in fragment + (supportFragmentManager + .findFragmentByTag(TAG_NESTED) as NestedSettingsFragment?) + ?.onProgressDialogThreadResult(actionTask, result) } } @@ -135,7 +124,10 @@ open class SettingsActivity super.onStop() } - override fun onAssignKeyDialogPositiveClick(masterPasswordChecked: Boolean, masterPassword: String?, keyFileChecked: Boolean, keyFile: Uri?) { + override fun onAssignKeyDialogPositiveClick(masterPasswordChecked: Boolean, + masterPassword: String?, + keyFileChecked: Boolean, + keyFile: Uri?) { Database.getInstance().let { database -> // Show the progress dialog now or after dialog confirmation if (database.validatePasswordEncoding(masterPassword, keyFileChecked)) { @@ -161,7 +153,10 @@ open class SettingsActivity } } - override fun onAssignKeyDialogNegativeClick(masterPasswordChecked: Boolean, masterPassword: String?, keyFileChecked: Boolean, keyFile: Uri?) { + override fun onAssignKeyDialogNegativeClick(masterPasswordChecked: Boolean, + masterPassword: String?, + keyFileChecked: Boolean, + keyFile: Uri?) { } diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseColorPreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseColorPreferenceDialogFragmentCompat.kt index 1c737f114..2cd54591c 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseColorPreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseColorPreferenceDialogFragmentCompat.kt @@ -36,7 +36,6 @@ import com.kunzisoft.androidclearchroma.colormode.ColorMode import com.kunzisoft.androidclearchroma.fragment.ChromaColorFragment import com.kunzisoft.androidclearchroma.fragment.ChromaColorFragment.* import com.kunzisoft.keepass.R -import com.kunzisoft.keepass.tasks.ActionRunnable import java.lang.Exception class DatabaseColorPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() { @@ -88,16 +87,15 @@ class DatabaseColorPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialog } val oldColor = database.customColor database.customColor = newColor - - actionInUIThreadAfterSaveDatabase = AfterColorSave(newColor, oldColor) + progressDialogThread?.startDatabaseSaveColor(oldColor, newColor) } - super.onDialogClosed(true) + onDialogClosed(true) dismiss() } alertDialogBuilder.setNegativeButton(android.R.string.cancel) { _, _ -> - super.onDialogClosed(false) + onDialogClosed(false) dismiss() } @@ -112,6 +110,10 @@ class DatabaseColorPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialog return dialog } + override fun onDialogClosed(positiveResult: Boolean) { + // Nothing here + } + /** * Set new dimensions to dialog * @param ad dialog @@ -134,22 +136,6 @@ class DatabaseColorPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialog return rootView } - private inner class AfterColorSave(private val mNewColor: String, - private val mOldColor: String) - : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val defaultColorToShow = - if (result.isSuccess) { - mNewColor - } else { - database?.customColor = mOldColor - mOldColor - } - preference.summary = defaultColorToShow - } - } - companion object { private const val TAG_FRAGMENT_COLORS = "TAG_FRAGMENT_COLORS" diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDataCompressionPreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDataCompressionPreferenceDialogFragmentCompat.kt index fbee376de..f20873115 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDataCompressionPreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDataCompressionPreferenceDialogFragmentCompat.kt @@ -26,7 +26,6 @@ import androidx.recyclerview.widget.RecyclerView import com.kunzisoft.keepass.R import com.kunzisoft.keepass.database.element.PwCompressionAlgorithm import com.kunzisoft.keepass.settings.preferencedialogfragment.adapter.ListRadioItemAdapter -import com.kunzisoft.keepass.tasks.ActionRunnable class DatabaseDataCompressionPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat(), @@ -60,39 +59,21 @@ class DatabaseDataCompressionPreferenceDialogFragmentCompat if (positiveResult) { database?.let { database -> if (compressionSelected != null) { - val newAlgorithm = compressionSelected - val oldAlgorithm = database.compressionAlgorithm - database.compressionAlgorithm = newAlgorithm + val newCompression = compressionSelected + val oldCompression = database.compressionAlgorithm + database.compressionAlgorithm = newCompression - if (oldAlgorithm != null && newAlgorithm != null) - actionInUIThreadAfterSaveDatabase = AfterDescriptionSave(newAlgorithm, oldAlgorithm) + if (oldCompression != null && newCompression != null) + progressDialogThread?.startDatabaseSaveCompression(oldCompression, newCompression) } } } - - super.onDialogClosed(positiveResult) } override fun onItemSelected(item: PwCompressionAlgorithm) { this.compressionSelected = item } - private inner class AfterDescriptionSave(private val mNewAlgorithm: PwCompressionAlgorithm, - private val mOldAlgorithm: PwCompressionAlgorithm) - : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val algorithmToShow = - if (result.isSuccess) { - mNewAlgorithm - } else { - database?.compressionAlgorithm = mOldAlgorithm - mOldAlgorithm - } - preference.summary = algorithmToShow.getName(settingsResources) - } - } - companion object { fun newInstance(key: String): DatabaseDataCompressionPreferenceDialogFragmentCompat { diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDefaultUsernamePreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDefaultUsernamePreferenceDialogFragmentCompat.kt index 6c7cb52ff..2430616b4 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDefaultUsernamePreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDefaultUsernamePreferenceDialogFragmentCompat.kt @@ -21,7 +21,6 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment import android.os.Bundle import android.view.View -import com.kunzisoft.keepass.tasks.ActionRunnable class DatabaseDefaultUsernamePreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() { @@ -38,27 +37,9 @@ class DatabaseDefaultUsernamePreferenceDialogFragmentCompat : DatabaseSavePrefer val oldDefaultUsername = database.defaultUsername database.defaultUsername = newDefaultUsername - actionInUIThreadAfterSaveDatabase = AfterDefaultUsernameSave(newDefaultUsername, oldDefaultUsername) + progressDialogThread?.startDatabaseSaveDefaultUsername(oldDefaultUsername, newDefaultUsername) } } - - super.onDialogClosed(positiveResult) - } - - private inner class AfterDefaultUsernameSave(private val mNewDefaultUsername: String, - private val mOldDefaultUsername: String) - : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val defaultUsernameToShow = - if (result.isSuccess) { - mNewDefaultUsername - } else { - database?.defaultUsername = mOldDefaultUsername - mOldDefaultUsername - } - preference.summary = defaultUsernameToShow - } } companion object { diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDescriptionPreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDescriptionPreferenceDialogFragmentCompat.kt index e08bdb851..199f42373 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDescriptionPreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseDescriptionPreferenceDialogFragmentCompat.kt @@ -21,7 +21,6 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment import android.os.Bundle import android.view.View -import com.kunzisoft.keepass.tasks.ActionRunnable class DatabaseDescriptionPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() { @@ -38,27 +37,9 @@ class DatabaseDescriptionPreferenceDialogFragmentCompat : DatabaseSavePreference val oldDescription = database.description database.description = newDescription - actionInUIThreadAfterSaveDatabase = AfterDescriptionSave(newDescription, oldDescription) + progressDialogThread?.startDatabaseSaveDescription(oldDescription, newDescription) } } - - super.onDialogClosed(positiveResult) - } - - private inner class AfterDescriptionSave(private val mNewDescription: String, - private val mOldDescription: String) - : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val descriptionToShow = - if (result.isSuccess) { - mNewDescription - } else { - database?.description = mOldDescription - mOldDescription - } - preference.summary = descriptionToShow - } } companion object { diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat.kt index 38aef811e..61c1f3edb 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat.kt @@ -26,7 +26,6 @@ import android.view.View import com.kunzisoft.keepass.R import com.kunzisoft.keepass.database.element.PwEncryptionAlgorithm import com.kunzisoft.keepass.settings.preferencedialogfragment.adapter.ListRadioItemAdapter -import com.kunzisoft.keepass.tasks.ActionRunnable class DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat(), @@ -66,35 +65,17 @@ class DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat database.encryptionAlgorithm = newAlgorithm if (oldAlgorithm != null && newAlgorithm != null) - actionInUIThreadAfterSaveDatabase = AfterDescriptionSave(newAlgorithm, oldAlgorithm) + progressDialogThread?.startDatabaseSaveEncryption(oldAlgorithm, newAlgorithm) } } } } - - super.onDialogClosed(positiveResult) } override fun onItemSelected(item: PwEncryptionAlgorithm) { this.algorithmSelected = item } - private inner class AfterDescriptionSave(private val mNewAlgorithm: PwEncryptionAlgorithm, - private val mOldAlgorithm: PwEncryptionAlgorithm) - : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val algorithmToShow = - if (result.isSuccess) { - mNewAlgorithm - } else { - database?.encryptionAlgorithm = mOldAlgorithm - mOldAlgorithm - } - preference.summary = algorithmToShow.getName(settingsResources) - } - } - companion object { fun newInstance(key: String): DatabaseEncryptionAlgorithmPreferenceDialogFragmentCompat { diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseKeyDerivationPreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseKeyDerivationPreferenceDialogFragmentCompat.kt index 6b05c14a7..dc82d55cb 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseKeyDerivationPreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseKeyDerivationPreferenceDialogFragmentCompat.kt @@ -27,7 +27,6 @@ import android.view.View import com.kunzisoft.keepass.R import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine import com.kunzisoft.keepass.settings.preferencedialogfragment.adapter.ListRadioItemAdapter -import com.kunzisoft.keepass.tasks.ActionRunnable class DatabaseKeyDerivationPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat(), @@ -67,13 +66,11 @@ class DatabaseKeyDerivationPreferenceDialogFragmentCompat val oldKdfEngine = database.kdfEngine if (newKdfEngine != null && oldKdfEngine != null) { database.kdfEngine = newKdfEngine - actionInUIThreadAfterSaveDatabase = AfterDescriptionSave(newKdfEngine, oldKdfEngine) + progressDialogThread?.startDatabaseSaveKeyDerivation(oldKdfEngine, newKdfEngine) } } } } - - super.onDialogClosed(positiveResult) } fun setRoundPreference(preference: Preference?) { @@ -92,27 +89,6 @@ class DatabaseKeyDerivationPreferenceDialogFragmentCompat kdfEngineSelected = item } - private inner class AfterDescriptionSave(private val mNewKdfEngine: KdfEngine, - private val mOldKdfEngine: KdfEngine) - : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val kdfEngineToShow = - if (result.isSuccess) { - mNewKdfEngine - } else { - database?.kdfEngine = mOldKdfEngine - mOldKdfEngine - } - preference.summary = kdfEngineToShow.getName(settingsResources) - - roundPreference?.summary = kdfEngineToShow.defaultKeyRounds.toString() - // Disable memory and parallelism if not available - memoryPreference?.summary = kdfEngineToShow.defaultMemoryUsage.toString() - parallelismPreference?.summary = kdfEngineToShow.defaultParallelism.toString() - } - } - companion object { fun newInstance(key: String): DatabaseKeyDerivationPreferenceDialogFragmentCompat { diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseNamePreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseNamePreferenceDialogFragmentCompat.kt index 8b4372037..6eb74e17a 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseNamePreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseNamePreferenceDialogFragmentCompat.kt @@ -21,7 +21,6 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment import android.os.Bundle import android.view.View -import com.kunzisoft.keepass.tasks.ActionRunnable class DatabaseNamePreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() { @@ -38,27 +37,9 @@ class DatabaseNamePreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogF val oldName = database.name database.name = newName - actionInUIThreadAfterSaveDatabase = AfterNameSave(newName, oldName) + progressDialogThread?.startDatabaseSaveName(oldName, newName) } } - - super.onDialogClosed(positiveResult) - } - - private inner class AfterNameSave(private val mNewName: String, - private val mOldName: String) - : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val nameToShow = - if (result.isSuccess) { - mNewName - } else { - database?.name = mOldName - mOldName - } - preference.summary = nameToShow - } } companion object { diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseSavePreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseSavePreferenceDialogFragmentCompat.kt index 9b0ee00b1..2d4182679 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseSavePreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseSavePreferenceDialogFragmentCompat.kt @@ -19,20 +19,17 @@ */ package com.kunzisoft.keepass.settings.preferencedialogfragment -import android.content.res.Resources +import android.content.Context import android.os.Bundle -import android.view.View +import com.kunzisoft.keepass.database.action.ProgressDialogThread import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.settings.SettingsActivity -import com.kunzisoft.keepass.tasks.ActionRunnable abstract class DatabaseSavePreferenceDialogFragmentCompat : InputPreferenceDialogFragmentCompat() { protected var database: Database? = null - var actionInUIThreadAfterSaveDatabase: ActionRunnable? = null - - protected lateinit var settingsResources: Resources + protected var progressDialogThread: ProgressDialogThread? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -40,33 +37,15 @@ abstract class DatabaseSavePreferenceDialogFragmentCompat : InputPreferenceDialo this.database = Database.getInstance() } - override fun onBindDialogView(view: View) { - super.onBindDialogView(view) - - activity?.resources?.let { settingsResources = it } - } - - override fun onDialogClosed(positiveResult: Boolean) { - if (positiveResult) { - registerActionFinish() - (activity as SettingsActivity?)?.progressDialogThread?.startDatabaseSave() + override fun onAttach(context: Context) { + super.onAttach(context) + // Attach dialog thread to start action + if (context is SettingsActivity) { + progressDialogThread = context.progressDialogThread } } - fun registerActionFinish() { - // TODO remove receiver - // Register a database task receiver to stop loading dialog when service finish the task - /* - when (intent?.action) { - DATABASE_STOP_TASK_ACTION -> { - actionInUIThreadAfterSaveDatabase?.onFinishRun(result) - } - } - */ - } - companion object { - private const val TAG = "DbSavePrefDialog" } } diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MaxHistoryItemsPreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MaxHistoryItemsPreferenceDialogFragmentCompat.kt index bc3f7e454..f9f45731e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MaxHistoryItemsPreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MaxHistoryItemsPreferenceDialogFragmentCompat.kt @@ -22,7 +22,6 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment import android.os.Bundle import android.view.View import com.kunzisoft.keepass.R -import com.kunzisoft.keepass.tasks.ActionRunnable class MaxHistoryItemsPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() { @@ -57,27 +56,9 @@ class MaxHistoryItemsPreferenceDialogFragmentCompat : DatabaseSavePreferenceDial val oldMaxHistoryItems = database.historyMaxItems database.historyMaxItems = maxHistoryItems - actionInUIThreadAfterSaveDatabase = AfterMaxHistoryItemsSave(maxHistoryItems, oldMaxHistoryItems) + progressDialogThread?.startDatabaseSaveMaxHistoryItems(oldMaxHistoryItems, maxHistoryItems) } } - - super.onDialogClosed(positiveResult) - } - - private inner class AfterMaxHistoryItemsSave(private val mNewMaxHistoryItems: Int, - private val mOldMaxHistoryItems: Int) - : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val maxHistoryItemsToShow = - if (result.isSuccess) { - mNewMaxHistoryItems - } else { - database?.historyMaxItems = mOldMaxHistoryItems - mOldMaxHistoryItems - } - preference.summary = maxHistoryItemsToShow.toString() - } } companion object { diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MaxHistorySizePreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MaxHistorySizePreferenceDialogFragmentCompat.kt index da592012b..ca709a2b5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MaxHistorySizePreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MaxHistorySizePreferenceDialogFragmentCompat.kt @@ -22,7 +22,6 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment import android.os.Bundle import android.view.View import com.kunzisoft.keepass.R -import com.kunzisoft.keepass.tasks.ActionRunnable class MaxHistorySizePreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() { @@ -57,27 +56,9 @@ class MaxHistorySizePreferenceDialogFragmentCompat : DatabaseSavePreferenceDialo val oldMaxHistorySize = database.historyMaxSize database.historyMaxSize = maxHistorySize - actionInUIThreadAfterSaveDatabase = AfterMaxHistorySizeSave(maxHistorySize, oldMaxHistorySize) + progressDialogThread?.startDatabaseSaveMaxHistorySize(oldMaxHistorySize, maxHistorySize) } } - - super.onDialogClosed(positiveResult) - } - - private inner class AfterMaxHistorySizeSave(private val mNewMaxHistorySize: Long, - private val mOldMaxHistorySize: Long) - : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val maxHistorySizeToShow = - if (result.isSuccess) { - mNewMaxHistorySize - } else { - database?.historyMaxSize = mOldMaxHistorySize - mOldMaxHistorySize - } - preference.summary = maxHistorySizeToShow.toString() - } } companion object { diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MemoryUsagePreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MemoryUsagePreferenceDialogFragmentCompat.kt index 25463f269..c2f6e41a0 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MemoryUsagePreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/MemoryUsagePreferenceDialogFragmentCompat.kt @@ -22,7 +22,6 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment import android.os.Bundle import android.view.View import com.kunzisoft.keepass.R -import com.kunzisoft.keepass.tasks.ActionRunnable class MemoryUsagePreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() { @@ -49,27 +48,9 @@ class MemoryUsagePreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFr val oldMemoryUsage = database.memoryUsage database.memoryUsage = memoryUsage - actionInUIThreadAfterSaveDatabase = AfterMemorySave(memoryUsage, oldMemoryUsage) + progressDialogThread?.startDatabaseSaveMemoryUsage(oldMemoryUsage, memoryUsage) } } - - super.onDialogClosed(positiveResult) - } - - private inner class AfterMemorySave(private val mNewMemory: Long, - private val mOldMemory: Long) - : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val memoryToShow = - if (result.isSuccess) { - mNewMemory - } else { - database?.memoryUsage = mOldMemory - mOldMemory - } - preference.summary = memoryToShow.toString() - } } companion object { diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/ParallelismPreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/ParallelismPreferenceDialogFragmentCompat.kt index e876203ec..2d675a54b 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/ParallelismPreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/ParallelismPreferenceDialogFragmentCompat.kt @@ -22,7 +22,6 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment import android.os.Bundle import android.view.View import com.kunzisoft.keepass.R -import com.kunzisoft.keepass.tasks.ActionRunnable class ParallelismPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() { @@ -49,27 +48,9 @@ class ParallelismPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFr val oldParallelism = database.parallelism database.parallelism = parallelism - actionInUIThreadAfterSaveDatabase = AfterParallelismSave(parallelism, oldParallelism) + progressDialogThread?.startDatabaseSaveParallelism(oldParallelism, parallelism) } } - - super.onDialogClosed(positiveResult) - } - - private inner class AfterParallelismSave(private val mNewParallelism: Int, - private val mOldParallelism: Int) - : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val parallelismToShow = - if (result.isSuccess) { - mNewParallelism - } else { - database?.parallelism = mOldParallelism - mOldParallelism - } - preference.summary = parallelismToShow.toString() - } } companion object { diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/RoundsPreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/RoundsPreferenceDialogFragmentCompat.kt index f4aa89e42..7faa0d370 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/RoundsPreferenceDialogFragmentCompat.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/RoundsPreferenceDialogFragmentCompat.kt @@ -23,7 +23,6 @@ import android.os.Bundle import android.view.View import android.widget.Toast import com.kunzisoft.keepass.R -import com.kunzisoft.keepass.tasks.ActionRunnable class RoundsPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() { @@ -55,26 +54,9 @@ class RoundsPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmen database.numberKeyEncryptionRounds = Long.MAX_VALUE } - actionInUIThreadAfterSaveDatabase = AfterRoundSave(rounds, oldRounds) + progressDialogThread?.startDatabaseSaveIterations(oldRounds, rounds) } } - - super.onDialogClosed(positiveResult) - } - - private inner class AfterRoundSave(private val mNewRounds: Long, - private val mOldRounds: Long) : ActionRunnable() { - - override fun onFinishRun(result: Result) { - val roundsToShow = - if (result.isSuccess) { - mNewRounds - } else { - database?.numberKeyEncryptionRounds = mOldRounds - mOldRounds - } - preference.summary = roundsToShow.toString() - } } companion object {