Fix database creation index

This commit is contained in:
J-Jamet committed 2019-07-25 14:50:51 +02:00
1 parent 2ce3fc3bd6
commit 2d640dbe62
10 files changed
+46 -44

No files matched your search

@@ -49,6 +49,7 @@ import com.kunzisoft.keepass.activities.helpers.EntrySelectionHelper
import com.kunzisoft.keepass.activities.helpers.KeyFileHelper
import com.kunzisoft.keepass.activities.stylish.StylishActivity
import com.kunzisoft.keepass.adapters.FileDatabaseHistoryAdapter
import com.kunzisoft.keepass.app.App
import com.kunzisoft.keepass.autofill.AutofillHelper
import com.kunzisoft.keepass.database.action.*
import com.kunzisoft.keepass.education.FileDatabaseSelectActivityEducation
@@ -407,7 +408,7 @@ class FileDatabaseSelectActivity : StylishActivity(),
// Create the new database and start prof
ProgressDialogThread(this@FileDatabaseSelectActivity,
{
CreateDatabaseRunnable(databaseFilename) { database ->
CreateDatabaseRunnable(databaseFilename, App.currentDatabase) { database ->
UriUtil.parseUriFile(databaseFilename)?.let { databaseUri ->
// TODO store database created
@@ -973,7 +973,7 @@ class GroupActivity : LockingActivity(),
// Else lock if needed
else {
if (PreferencesUtil.isLockDatabaseWhenBackButtonOnRootClicked(this)) {
App.currentDatabase.closeAndClear(applicationContext)
App.currentDatabase.closeAndClear(applicationContext.filesDir)
super.onBackPressed()
} else {
moveTaskToBack(true)
@@ -590,7 +590,7 @@ class PasswordActivity : StylishActivity(),
private fun loadDatabase(password: String?, keyFile: Uri?) {
// Clear before we load
val database = App.currentDatabase
database.closeAndClear(applicationContext)
database.closeAndClear(applicationContext.filesDir)
mDatabaseFileUri?.let { databaseUri ->
// Show the progress dialog and load the database
@@ -768,7 +768,7 @@ class PasswordActivity : StylishActivity(),
when (resultCode) {
LockingActivity.RESULT_EXIT_LOCK, Activity.RESULT_CANCELED -> {
setEmptyViews()
App.currentDatabase.closeAndClear(applicationContext)
App.currentDatabase.closeAndClear(applicationContext.filesDir)
}
}
}
@@ -190,7 +190,7 @@ fun Activity.lock() {
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).apply {
cancelAll()
}
App.currentDatabase.closeAndClear(applicationContext)
App.currentDatabase.closeAndClear(applicationContext.filesDir)
setResult(LockingActivity.RESULT_EXIT_LOCK)
finish()
}
@@ -37,7 +37,7 @@ class App : MultiDexApplication() {
}
override fun onTerminate() {
currentDatabase.closeAndClear(applicationContext)
currentDatabase.closeAndClear(applicationContext.filesDir)
super.onTerminate()
}
}
@@ -19,19 +19,19 @@
*/
package com.kunzisoft.keepass.database.action
import com.kunzisoft.keepass.app.App
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.tasks.ActionRunnable
class CreateDatabaseRunnable(private val mFilename: String,
private val mDatabase: Database,
val onDatabaseCreate: (database: Database) -> ActionRunnable?)
: ActionRunnable() {
override fun run() {
try {
// Create new database record
Database(mFilename).apply {
App.currentDatabase = this
mDatabase.apply {
createData(mFilename)
// Set Database state
loaded = true
// Commit changes
@@ -40,6 +40,8 @@ class CreateDatabaseRunnable(private val mFilename: String,
finishRun(true)
} catch (e: Exception) {
mDatabase.closeAndClear()
finishRun(false, e.message)
}
}
@@ -122,7 +122,11 @@ class LoadDatabaseRunnable(private val mWeakContext: WeakReference<Context>,
FileDatabaseHistory.getInstance(mWeakContext).addDatabaseUri(uri, keyFileUri)
}
override fun onFinishRun(result: Result) {}
override fun onFinishRun(result: Result) {
if (!result.isSuccess) {
mDatabase.closeAndClear(mWeakContext.get()?.filesDir)
}
}
companion object {
private val TAG = LoadDatabaseRunnable::class.java.name
@@ -178,26 +178,6 @@ class Database {
return null
}
constructor()
constructor(databasePath: String) {
// TODO Test with kdb extension
if (isKDBExtension(databasePath)) {
setDatabaseV3(PwDatabaseV3())
} else {
val databaseV4 = PwDatabaseV4()
val groupV4 = databaseV4.createGroup()
groupV4.title = dbNameFromPath(databasePath)
groupV4.icon = databaseV4.iconFactory.folderIcon
databaseV4.rootGroup = groupV4
setDatabaseV4(databaseV4)
}
UriUtil.parseUriFile(databasePath)?.let { uri ->
setUri(uri)
}
}
private fun setDatabaseV3(pwDatabaseV3: PwDatabaseV3) {
this.pwDatabaseV3 = pwDatabaseV3
this.pwDatabaseV4 = null
@@ -208,14 +188,6 @@ class Database {
this.pwDatabaseV4 = pwDatabaseV4
}
private fun isKDBExtension(filename: String?): Boolean {
if (filename == null) {
return false
}
val extIdx = filename.lastIndexOf(".")
return if (extIdx == -1) false else filename.substring(extIdx).equals(".kdb", ignoreCase = true)
}
private fun dbNameFromPath(dbPath: String): String {
val filename = URLUtil.guessFileName(dbPath, null, null)
if (filename == null || filename.isEmpty()) {
@@ -227,8 +199,12 @@ class Database {
} else filename.substring(0, lastExtDot)
}
fun setUri(mUri: Uri) {
this.mUri = mUri
fun createData(databasePath: String) {
// Always create a new database with the last version
setDatabaseV4(PwDatabaseV4(dbNameFromPath(databasePath)))
UriUtil.parseUriFile(databasePath)?.let { uri ->
this.mUri = uri
}
}
@Throws(IOException::class, InvalidDBException::class)
@@ -425,17 +401,19 @@ class Database {
}
// TODO Clear database when lock broadcast is receive in backstage
fun closeAndClear(context: Context) {
fun closeAndClear(filesDirectory: File? = null) {
drawFactory.clearCache()
// Delete the cache of the database if present
pwDatabaseV3?.clearCache()
pwDatabaseV4?.clearCache()
// In all cases, delete all the files in the temp dir
try {
FileUtils.cleanDirectory(context.filesDir)
FileUtils.cleanDirectory(filesDirectory)
} catch (e: IOException) {
Log.e(TAG, "Unable to clear the directory cache.", e)
}
pwDatabaseV3 = null
pwDatabaseV4 = null
mUri = null
loaded = false
@@ -275,6 +275,11 @@ abstract class PwDatabase<Group : PwGroup<*, Group, Entry>, Entry : PwEntry<Grou
return entryIndexes.size
}
open fun clearCache() {
this.groupIndexes.clear()
this.entryIndexes.clear()
}
/*
* -------------------------------------
* Node Manipulation
@@ -43,7 +43,7 @@ import java.util.*
import javax.xml.parsers.DocumentBuilderFactory
class PwDatabaseV4 : PwDatabase<PwGroupV4, PwEntryV4>() {
class PwDatabaseV4 : PwDatabase<PwGroupV4, PwEntryV4> {
var hmacKey: ByteArray? = null
private set
@@ -93,6 +93,17 @@ class PwDatabaseV4 : PwDatabase<PwGroupV4, PwEntryV4>() {
var localizedAppName = "KeePassDX" // TODO resource
constructor()
constructor(databaseName: String) {
val groupV4 = createGroup().apply {
title = databaseName
icon = iconFactory.folderIcon
}
rootGroup = groupV4
addGroupIndex(groupV4)
}
override val version: String
get() = "KeePass 2"
@@ -408,7 +419,8 @@ class PwDatabaseV4 : PwDatabase<PwGroupV4, PwEntryV4>() {
return super.validatePasswordEncoding(key)
}
fun clearCache() {
override fun clearCache() {
super.clearCache()
binPool.clear()
}