diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/MainCredentialActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/MainCredentialActivity.kt index 2d6312e36..f9724d840 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/MainCredentialActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/MainCredentialActivity.kt @@ -59,6 +59,7 @@ import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.MainCredential import com.kunzisoft.keepass.database.exception.DuplicateUuidDatabaseException import com.kunzisoft.keepass.database.exception.FileNotFoundDatabaseException +import com.kunzisoft.keepass.database.exception.getLocalizedMessage import com.kunzisoft.keepass.education.PasswordActivityEducation import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.model.* diff --git a/app/src/main/java/com/kunzisoft/keepass/database/exception/LocalizedDatabaseException.kt b/app/src/main/java/com/kunzisoft/keepass/database/exception/LocalizedDatabaseException.kt new file mode 100644 index 000000000..fb53782d3 --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/database/exception/LocalizedDatabaseException.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2021 Jeremy Jamet / Kunzisoft. + * + * This file is part of KeePassDX. + * + * KeePassDX is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * KeePassDX is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with KeePassDX. If not, see . + */ +package com.kunzisoft.keepass.database.exception + +import android.content.res.Resources +import com.kunzisoft.keepass.R + +fun DatabaseException.getLocalizedMessage(resources: Resources): String = parameters?.let { + when (this) { + is FileNotFoundDatabaseException -> resources.getString(R.string.file_not_found_content) + is CorruptedDatabaseException -> resources.getString(R.string.corrupted_file) + is InvalidAlgorithmDatabaseException -> resources.getString(R.string.invalid_algorithm) + is UnknownDatabaseLocationException -> resources.getString(R.string.error_location_unknown) + is HardwareKeyDatabaseException -> resources.getString(R.string.error_hardware_key_unsupported) + is EmptyKeyDatabaseException -> resources.getString(R.string.error_empty_key) + is SignatureDatabaseException -> resources.getString(R.string.invalid_db_sig) + is VersionDatabaseException -> resources.getString(R.string.unsupported_db_version) + is InvalidCredentialsDatabaseException -> resources.getString(R.string.invalid_credentials) + is KDFMemoryDatabaseException -> resources.getString(R.string.error_load_database_KDF_memory) + is NoMemoryDatabaseException -> resources.getString(R.string.error_out_of_memory) + is DuplicateUuidDatabaseException -> resources.getString(R.string.invalid_db_same_uuid) + is XMLMalformedDatabaseException -> resources.getString(R.string.error_XML_malformed) + is MergeDatabaseKDBException -> resources.getString(R.string.error_unable_merge_database_kdb) + is MoveEntryDatabaseException -> resources.getString(R.string.error_move_entry_here) + is MoveGroupDatabaseException -> resources.getString(R.string.error_move_group_here) + is CopyEntryDatabaseException -> resources.getString(R.string.error_copy_entry_here) + is CopyGroupDatabaseException -> resources.getString(R.string.error_copy_group_here) + is DatabaseInputException -> resources.getString(R.string.error_load_database) + is DatabaseOutputException -> resources.getString(R.string.error_save_database) + else -> (mThrowable as? DatabaseException)?.getLocalizedMessage(resources) + } +} ?: resources.getString(R.string.error_load_database) + diff --git a/app/src/main/java/com/kunzisoft/keepass/view/ViewUtil.kt b/app/src/main/java/com/kunzisoft/keepass/view/ViewUtil.kt index 44182c9ee..ca329a96e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/ViewUtil.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/ViewUtil.kt @@ -54,6 +54,7 @@ import android.graphics.drawable.Drawable import android.view.ViewGroup import android.widget.LinearLayout import com.google.android.material.appbar.CollapsingToolbarLayout +import com.kunzisoft.keepass.database.exception.getLocalizedMessage /** @@ -226,8 +227,8 @@ fun View.updateLockPaddingLeft() { fun Context.showActionErrorIfNeeded(result: ActionRunnable.Result) { if (!result.isSuccess) { - result.exception?.errorId?.let { errorId -> - Toast.makeText(this, errorId, Toast.LENGTH_LONG).show() + result.exception?.let { exception -> + Toast.makeText(this, exception.getLocalizedMessage(resources), Toast.LENGTH_LONG).show() } ?: result.message?.let { message -> Toast.makeText(this, message, Toast.LENGTH_LONG).show() } @@ -236,8 +237,8 @@ fun Context.showActionErrorIfNeeded(result: ActionRunnable.Result) { fun CoordinatorLayout.showActionErrorIfNeeded(result: ActionRunnable.Result) { if (!result.isSuccess) { - result.exception?.errorId?.let { errorId -> - Snackbar.make(this, errorId, Snackbar.LENGTH_LONG).asError().show() + result.exception?.let { exception -> + Snackbar.make(this, exception.getLocalizedMessage(resources), Snackbar.LENGTH_LONG).asError().show() } ?: result.message?.let { message -> Snackbar.make(this, message, Snackbar.LENGTH_LONG).asError().show() } @@ -288,4 +289,4 @@ fun CollapsingToolbarLayout.changeTitleColor(color: Int) { setCollapsedTitleTextColor(color) setExpandedTitleColor(color) invalidate() -} \ No newline at end of file +} diff --git a/database/src/main/java/com/kunzisoft/keepass/database/exception/DatabaseException.kt b/database/src/main/java/com/kunzisoft/keepass/database/exception/DatabaseException.kt index 6f14f690a..e5962fc53 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/exception/DatabaseException.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/exception/DatabaseException.kt @@ -19,9 +19,6 @@ */ package com.kunzisoft.keepass.database.exception -import android.content.res.Resources -import androidx.annotation.StringRes -import com.kunzisoft.keepass.database.R import com.kunzisoft.keepass.database.element.node.NodeId import com.kunzisoft.keepass.database.element.node.Type import java.io.PrintStream @@ -30,7 +27,6 @@ import java.io.PrintWriter abstract class DatabaseException : Exception { var innerMessage: String? = null - abstract var errorId: Int var parameters: (Array)? = null var mThrowable: Throwable? = null @@ -46,27 +42,12 @@ abstract class DatabaseException : Exception { } }.toString() } + constructor(throwable: Throwable) { mThrowable = throwable innerMessage = throwable.localizedMessage } - fun getLocalizedMessage(resources: Resources): String { - val throwable = mThrowable - if (throwable is DatabaseException) - errorId = throwable.errorId - val localMessage = parameters?.let { - resources.getString(errorId, *it) - } ?: resources.getString(errorId) - return StringBuilder().apply { - append(localMessage) - if (innerMessage != null) { - append(" ") - append(innerMessage) - } - }.toString() - } - override fun printStackTrace() { mThrowable?.printStackTrace() super.printStackTrace() @@ -83,116 +64,62 @@ abstract class DatabaseException : Exception { } } -class FileNotFoundDatabaseException : DatabaseInputException() { - @StringRes - override var errorId: Int = R.string.file_not_found_content -} +class FileNotFoundDatabaseException : DatabaseInputException() -class CorruptedDatabaseException : DatabaseInputException() { - @StringRes - override var errorId: Int = R.string.corrupted_file -} +class CorruptedDatabaseException : DatabaseInputException() class InvalidAlgorithmDatabaseException : DatabaseInputException { - @StringRes - override var errorId: Int = R.string.invalid_algorithm constructor() : super() constructor(exception: Throwable) : super(exception) } -class UnknownDatabaseLocationException : DatabaseException() { - @StringRes - override var errorId: Int = R.string.error_location_unknown -} +class UnknownDatabaseLocationException : DatabaseException() -class HardwareKeyDatabaseException : DatabaseException() { - @StringRes - override var errorId: Int = R.string.error_hardware_key_unsupported -} +class HardwareKeyDatabaseException : DatabaseException() -class EmptyKeyDatabaseException : DatabaseException() { - @StringRes - override var errorId: Int = R.string.error_empty_key -} +class EmptyKeyDatabaseException : DatabaseException() -class SignatureDatabaseException : DatabaseInputException() { - @StringRes - override var errorId: Int = R.string.invalid_db_sig -} +class SignatureDatabaseException : DatabaseInputException() -class VersionDatabaseException : DatabaseInputException() { - @StringRes - override var errorId: Int = R.string.unsupported_db_version -} +class VersionDatabaseException : DatabaseInputException() class InvalidCredentialsDatabaseException : DatabaseInputException { - @StringRes - override var errorId: Int = R.string.invalid_credentials constructor() : super() constructor(string: String) : super(string) } -class KDFMemoryDatabaseException(exception: Throwable) : DatabaseInputException(exception) { - @StringRes - override var errorId: Int = R.string.error_load_database_KDF_memory -} +class KDFMemoryDatabaseException(exception: Throwable) : DatabaseInputException(exception) -class NoMemoryDatabaseException(exception: Throwable) : DatabaseInputException(exception) { - @StringRes - override var errorId: Int = R.string.error_out_of_memory -} +class NoMemoryDatabaseException(exception: Throwable) : DatabaseInputException(exception) class DuplicateUuidDatabaseException(type: Type, uuid: NodeId<*>) : DatabaseInputException() { - @StringRes - override var errorId: Int = R.string.invalid_db_same_uuid init { parameters = arrayOf(type.name, uuid.toString()) } } class XMLMalformedDatabaseException : DatabaseInputException { - @StringRes - override var errorId: Int = R.string.error_XML_malformed constructor() : super() constructor(string: String) : super(string) } -class MergeDatabaseKDBException : DatabaseInputException() { - @StringRes - override var errorId: Int = R.string.error_unable_merge_database_kdb -} +class MergeDatabaseKDBException : DatabaseInputException() -class MoveEntryDatabaseException : DatabaseException() { - @StringRes - override var errorId: Int = R.string.error_move_entry_here -} +class MoveEntryDatabaseException : DatabaseException() -class MoveGroupDatabaseException : DatabaseException() { - @StringRes - override var errorId: Int = R.string.error_move_group_here -} +class MoveGroupDatabaseException : DatabaseException() -class CopyEntryDatabaseException : DatabaseException() { - @StringRes - override var errorId: Int = R.string.error_copy_entry_here -} +class CopyEntryDatabaseException : DatabaseException() -class CopyGroupDatabaseException : DatabaseException() { - @StringRes - override var errorId: Int = R.string.error_copy_group_here -} +class CopyGroupDatabaseException : DatabaseException() open class DatabaseInputException : DatabaseException { - @StringRes - override var errorId: Int = R.string.error_load_database constructor() : super() constructor(string: String) : super(string) constructor(throwable: Throwable) : super(throwable) } open class DatabaseOutputException : DatabaseException { - @StringRes - override var errorId: Int = R.string.error_save_database constructor(string: String) : super(string) constructor(string: String, e: Exception) : super(string, e) constructor(e: Exception) : super(e) diff --git a/database/src/main/res/values/strings.xml b/database/src/main/res/values/strings.xml index 623aac650..084d44889 100644 --- a/database/src/main/res/values/strings.xml +++ b/database/src/main/res/values/strings.xml @@ -4,27 +4,6 @@ Gzip Recycle bin Templates - - Could not find file. Try reopening it from your file browser. - Corrupted file. - Wrong algorithm. - Database location is unknown, database action cannot be performed. - Hardware key is not supported. - "Key cannot be empty." - Could not recognize the database format. - Unsupported database version. - Could not read credentials. - Could not load the key. Try to lower the KDF \"Memory Usage\". - No memory to load your entire database. - %1$s with the same UUID %2$s already exists. - XML malformed. - Unable to merge from a database V1. - You cannot move an entry here. - You cannot move a group here. - You cannot copy an entry here. - You cannot copy a group here. - Could not load the database. - Could not save the database. Retrieving database key… Decrypting database content…