Merge branch 'develop' of github.com:Kunzisoft/KeePassDX into develop

This commit is contained in:
J-Jamet committed 2021-01-22 18:03:36 +01:00
commit da761614bd
51 files changed
+419 -127

No files matched your search

@@ -34,7 +34,12 @@ class IOActionTask<T>(
mainScope.launch {
withContext(Dispatchers.IO) {
val asyncResult: Deferred<T?> = async {
action.invoke()
try {
action.invoke()
} catch (e: Exception) {
e.printStackTrace()
null
}
}
withContext(Dispatchers.Main) {
afterActionDatabaseListener?.invoke(asyncResult.await())
@@ -162,8 +162,8 @@ class AdvancedUnlockFragment: StylishFragment(), AdvancedUnlockManager.AdvancedU
disconnect()
}
} ?: run {
connect(databaseUri)
this.mAutoOpenPrompt = autoOpenPrompt
connect(databaseUri)
}
} else {
disconnect()
@@ -129,41 +129,49 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
}
fun checkDatabaseInfo() {
mDatabase.fileUri?.let {
val previousDatabaseInfo = mSnapFileDatabaseInfo
val lastFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo(
FileDatabaseInfo(applicationContext, it))
try {
mDatabase.fileUri?.let {
val previousDatabaseInfo = mSnapFileDatabaseInfo
val lastFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo(
FileDatabaseInfo(applicationContext, it))
val oldDatabaseModification = previousDatabaseInfo?.lastModification
val newDatabaseModification = lastFileDatabaseInfo.lastModification
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)
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)
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
}
mSnapFileDatabaseInfo = lastFileDatabaseInfo
}
} catch (e: Exception) {
Log.e(TAG, "Unable to check database info", e)
}
}
fun saveDatabaseInfo() {
mDatabase.fileUri?.let {
mSnapFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo(
FileDatabaseInfo(applicationContext, it))
Log.i(TAG, "Database file saved $mSnapFileDatabaseInfo")
try {
mDatabase.fileUri?.let {
mSnapFileDatabaseInfo = SnapFileDatabaseInfo.fromFileDatabaseInfo(
FileDatabaseInfo(applicationContext, it))
Log.i(TAG, "Database file saved $mSnapFileDatabaseInfo")
}
} catch (e: Exception) {
Log.e(TAG, "Unable to check database info", e)
}
}
@@ -386,7 +386,12 @@ class NestedAppSettingsFragment : NestedSettingsFragment() {
}
if (styleEnabled) {
Stylish.assignStyle(styleIdString)
activity.recreate()
// Relaunch the current activity to redraw theme
activity.apply {
startActivity(intent)
finish()
activity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
}
}
styleEnabled
}
@@ -58,7 +58,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
private var mEncryptionAlgorithmPref: DialogListExplanationPreference? = null
private var mKeyDerivationPref: DialogListExplanationPreference? = null
private var mRoundPref: InputKdfNumberPreference? = null
private var mMemoryPref: InputKdfNumberPreference? = null
private var mMemoryPref: InputKdfSizePreference? = null
private var mParallelismPref: InputKdfNumberPreference? = null
override fun onCreateScreenPreference(screen: Screen, savedInstanceState: Bundle?, rootKey: String?) {
@@ -231,7 +231,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
}
// Memory Usage
mMemoryPref = findPreference<InputKdfNumberPreference>(getString(R.string.memory_usage_key))?.apply {
mMemoryPref = findPreference<InputKdfSizePreference>(getString(R.string.memory_usage_key))?.apply {
summary = mDatabase.memoryUsage.toString()
}
@@ -25,7 +25,7 @@ import androidx.preference.DialogPreference
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine
class InputKdfNumberPreference @JvmOverloads constructor(context: Context,
open class InputKdfNumberPreference @JvmOverloads constructor(context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.dialogPreferenceStyle,
defStyleRes: Int = defStyleAttr)
@@ -0,0 +1,53 @@
/*
* Copyright 2019 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 <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preference
import android.content.Context
import android.util.AttributeSet
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.utils.DataByte
class InputKdfSizePreference @JvmOverloads constructor(context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.dialogPreferenceStyle,
defStyleRes: Int = defStyleAttr)
: InputKdfNumberPreference(context, attrs, defStyleAttr, defStyleRes) {
override fun setSummary(summary: CharSequence) {
if (summary == UNKNOWN_VALUE_STRING) {
super.setSummary("")
} else {
var summaryString = summary
try {
val memorySize = summary.toString().toLong()
summaryString = if (memorySize > 0) {
// To convert bytes to mebibytes
DataByte(memorySize, DataByte.ByteFormat.BYTE)
.toBetterByteFormat().toString(context)
} else {
memorySize.toString()
}
} catch (e: Exception) {
} finally {
super.setSummary(summaryString)
}
}
}
}
@@ -36,7 +36,7 @@ open class InputNumberPreference @JvmOverloads constructor(context: Context,
override fun setSummary(summary: CharSequence) {
if (summary == INFINITE_VALUE_STRING) {
super.setSummary("")
super.setSummary("∞")
} else {
super.setSummary(summary)
}
@@ -0,0 +1,49 @@
/*
* Copyright 2019 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 <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.settings.preference
import android.content.Context
import android.util.AttributeSet
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.utils.DataByte
open class InputSizePreference @JvmOverloads constructor(context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.dialogPreferenceStyle,
defStyleRes: Int = defStyleAttr)
: InputNumberPreference(context, attrs, defStyleAttr, defStyleRes) {
override fun setSummary(summary: CharSequence) {
var summaryString = summary
try {
val memorySize = summary.toString().toLong()
summaryString = if (memorySize >= 0) {
// To convert bytes to mebibytes
DataByte(memorySize, DataByte.ByteFormat.BYTE)
.toBetterByteFormat().toString(context)
} else {
memorySize.toString()
}
} catch (e: Exception) {
} finally {
super.setSummary(summaryString)
}
}
}
@@ -33,6 +33,7 @@ import com.kunzisoft.keepass.R
abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCompat() {
private var inputTextView: EditText? = null
private var textUnitView: TextView? = null
private var textExplanationView: TextView? = null
private var switchElementView: CompoundButton? = null
@@ -47,6 +48,14 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
}
}
fun setInoutText(@StringRes inputTextId: Int) {
inputText = getString(inputTextId)
}
fun showInputText(show: Boolean) {
inputTextView?.visibility = if (show) View.VISIBLE else View.GONE
}
fun setInputTextError(error: CharSequence) {
this.inputTextView?.error = error
}
@@ -55,6 +64,24 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
this.mOnInputTextEditorActionListener = onEditorActionListener
}
var unitText: String?
get() = textUnitView?.text?.toString() ?: ""
set(unitText) {
textUnitView?.apply {
if (unitText != null && unitText.isNotEmpty()) {
text = unitText
visibility = View.VISIBLE
} else {
text = ""
visibility = View.GONE
}
}
}
fun setUnitText(@StringRes unitTextId: Int) {
unitText = getString(unitTextId)
}
var explanationText: String?
get() = textExplanationView?.text?.toString() ?: ""
set(explanationText) {
@@ -69,6 +96,10 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
}
}
fun setExplanationText(@StringRes explanationTextId: Int) {
explanationText = getString(explanationTextId)
}
override fun onBindDialogView(view: View) {
super.onBindDialogView(view)
@@ -93,6 +124,8 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
}
}
}
textUnitView = view.findViewById(R.id.input_text_unit)
textUnitView?.visibility = View.GONE
textExplanationView = view.findViewById(R.id.explanation_text)
textExplanationView?.visibility = View.GONE
switchElementView = view.findViewById(R.id.switch_element)
@@ -113,18 +146,6 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
return false
}
fun setInoutText(@StringRes inputTextId: Int) {
inputText = getString(inputTextId)
}
fun showInputText(show: Boolean) {
inputTextView?.visibility = if (show) View.VISIBLE else View.GONE
}
fun setExplanationText(@StringRes explanationTextId: Int) {
explanationText = getString(explanationTextId)
}
fun setSwitchAction(onCheckedChange: ((isChecked: Boolean)-> Unit)?, defaultChecked: Boolean) {
switchElementView?.visibility = if (onCheckedChange == null) View.GONE else View.VISIBLE
switchElementView?.isChecked = defaultChecked
@@ -22,50 +22,76 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment
import android.os.Bundle
import android.view.View
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.utils.DataByte
class MaxHistorySizePreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() {
private var dataByte = DataByte(2L, DataByte.ByteFormat.MEBIBYTE)
override fun onBindDialogView(view: View) {
super.onBindDialogView(view)
setExplanationText(R.string.max_history_size_summary)
database?.historyMaxSize?.let { maxItemsDatabase ->
inputText = maxItemsDatabase.toString()
dataByte = DataByte(maxItemsDatabase, DataByte.ByteFormat.BYTE)
.toBetterByteFormat()
inputText = dataByte.number.toString()
if (dataByte.number >= 0) {
setUnitText(dataByte.format.stringId)
} else {
unitText = null
}
setSwitchAction({ isChecked ->
inputText = if (!isChecked) {
INFINITE_MAX_HISTORY_SIZE.toString()
} else
DEFAULT_MAX_HISTORY_SIZE.toString()
if (!isChecked) {
dataByte = INFINITE_MAX_HISTORY_SIZE_DATA_BYTE
inputText = INFINITE_MAX_HISTORY_SIZE.toString()
unitText = null
} else {
dataByte = DEFAULT_MAX_HISTORY_SIZE_DATA_BYTE
inputText = dataByte.number.toString()
setUnitText(dataByte.format.stringId)
}
showInputText(isChecked)
}, maxItemsDatabase > INFINITE_MAX_HISTORY_SIZE)
}
}
override fun onDialogClosed(positiveResult: Boolean) {
if (positiveResult) {
database?.let { database ->
var maxHistorySize: Long = try {
val maxHistorySize: Long = try {
inputText.toLong()
} catch (e: NumberFormatException) {
DEFAULT_MAX_HISTORY_SIZE
DEFAULT_MAX_HISTORY_SIZE_DATA_BYTE.toBytes()
}
if (maxHistorySize < INFINITE_MAX_HISTORY_SIZE) {
maxHistorySize = INFINITE_MAX_HISTORY_SIZE
val numberOfBytes = if (maxHistorySize >= 0) {
val dataByteConversion = DataByte(maxHistorySize, dataByte.format)
var bytes = dataByteConversion.toBytes()
if (bytes > Long.MAX_VALUE) {
bytes = Long.MAX_VALUE
}
bytes
} else {
INFINITE_MAX_HISTORY_SIZE
}
val oldMaxHistorySize = database.historyMaxSize
database.historyMaxSize = maxHistorySize
database.historyMaxSize = numberOfBytes
mProgressDatabaseTaskProvider?.startDatabaseSaveMaxHistorySize(oldMaxHistorySize, maxHistorySize, mDatabaseAutoSaveEnable)
mProgressDatabaseTaskProvider?.startDatabaseSaveMaxHistorySize(oldMaxHistorySize, numberOfBytes, mDatabaseAutoSaveEnable)
}
}
}
companion object {
const val DEFAULT_MAX_HISTORY_SIZE = 134217728L
const val INFINITE_MAX_HISTORY_SIZE = -1L
private val INFINITE_MAX_HISTORY_SIZE_DATA_BYTE = DataByte(INFINITE_MAX_HISTORY_SIZE, DataByte.ByteFormat.MEBIBYTE)
private val DEFAULT_MAX_HISTORY_SIZE_DATA_BYTE = DataByte(6L, DataByte.ByteFormat.MEBIBYTE)
fun newInstance(key: String): MaxHistorySizePreferenceDialogFragmentCompat {
val fragment = MaxHistorySizePreferenceDialogFragmentCompat()
val bundle = Bundle(1)
@@ -22,33 +22,46 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment
import android.os.Bundle
import android.view.View
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.utils.DataByte
class MemoryUsagePreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() {
private var dataByte = DataByte(MIN_MEMORY_USAGE, DataByte.ByteFormat.BYTE)
override fun onBindDialogView(view: View) {
super.onBindDialogView(view)
setExplanationText(R.string.memory_usage_explanation)
inputText = database?.memoryUsage?.toString()?: MIN_MEMORY_USAGE.toString()
val memoryBytes = database?.memoryUsage ?: MIN_MEMORY_USAGE
dataByte = DataByte(memoryBytes, DataByte.ByteFormat.BYTE)
.toBetterByteFormat()
inputText = dataByte.number.toString()
setUnitText(dataByte.format.stringId)
}
override fun onDialogClosed(positiveResult: Boolean) {
if (positiveResult) {
database?.let { database ->
var memoryUsage: Long = try {
var newMemoryUsage: Long = try {
inputText.toLong()
} catch (e: NumberFormatException) {
MIN_MEMORY_USAGE
}
if (memoryUsage < MIN_MEMORY_USAGE) {
memoryUsage = MIN_MEMORY_USAGE
if (newMemoryUsage < MIN_MEMORY_USAGE) {
newMemoryUsage = MIN_MEMORY_USAGE
}
// To transform in bytes
dataByte.number = newMemoryUsage
var numberOfBytes = dataByte.toBytes()
if (numberOfBytes > Long.MAX_VALUE) {
numberOfBytes = Long.MAX_VALUE
}
// TODO Max Memory
val oldMemoryUsage = database.memoryUsage
database.memoryUsage = memoryUsage
database.memoryUsage = numberOfBytes
mProgressDatabaseTaskProvider?.startDatabaseSaveMemoryUsage(oldMemoryUsage, memoryUsage, mDatabaseAutoSaveEnable)
mProgressDatabaseTaskProvider?.startDatabaseSaveMemoryUsage(oldMemoryUsage, numberOfBytes, mDatabaseAutoSaveEnable)
}
}
}
@@ -0,0 +1,84 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.utils
import android.content.Context
import androidx.annotation.StringRes
import com.kunzisoft.keepass.R
class DataByte(var number: Long, var format: ByteFormat) {
fun toBetterByteFormat(): DataByte {
return when (this.format) {
ByteFormat.BYTE -> {
when {
this.number % GIBIBYTES == 0L -> {
DataByte((this.number / GIBIBYTES), ByteFormat.GIBIBYTE)
}
this.number % MEBIBYTES == 0L -> {
DataByte((this.number / MEBIBYTES), ByteFormat.MEBIBYTE)
}
this.number % KIBIBYTES == 0L -> {
DataByte((this.number / KIBIBYTES), ByteFormat.KIBIBYTE)
}
else -> {
DataByte(this.number, ByteFormat.BYTE)
}
}
}
else -> {
DataByte(toBytes(), ByteFormat.BYTE).toBetterByteFormat()
}
}
}
/**
* Number of bytes in current DataByte
*/
fun toBytes(): Long {
return when (this.format) {
ByteFormat.BYTE -> this.number
ByteFormat.KIBIBYTE -> this.number * KIBIBYTES
ByteFormat.MEBIBYTE -> this.number * MEBIBYTES
ByteFormat.GIBIBYTE -> this.number * GIBIBYTES
}
}
override fun toString(): String {
return "$number ${format.name}"
}
fun toString(context: Context): String {
return "$number ${context.getString(format.stringId)}"
}
enum class ByteFormat(@StringRes var stringId: Int) {
BYTE(R.string.unit_byte),
KIBIBYTE(R.string.unit_kibibyte),
MEBIBYTE(R.string.unit_mebibyte),
GIBIBYTE(R.string.unit_gibibyte)
}
companion object {
const val KIBIBYTES = 1024L
const val MEBIBYTES = 1048576L
const val GIBIBYTES = 1073741824L
}
}