From 6be2148951d5f59738dbcc13aa6980eb16991f95 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 9 May 2019 23:41:26 +0200 Subject: [PATCH] Fix opening bugs --- .../com/kunzisoft/keepass/tests/TestUtil.java | 10 --- .../keepass/activities/EntryEditActivity.java | 4 +- .../keepass/adapters/NodeAdapter.java | 2 +- .../AssignPasswordInDatabaseRunnable.kt | 8 +-- .../database/action/CreateDatabaseRunnable.kt | 14 ++-- .../keepass/database/element/Database.kt | 70 ++++++++----------- .../database/element/EntryVersioned.kt | 8 +-- .../database/element/GroupVersioned.kt | 33 +++++---- .../keepass/database/element/PwDatabase.java | 16 +++-- .../database/element/PwDatabaseV3.java | 8 ++- .../database/element/PwDatabaseV4.java | 10 ++- .../keepass/database/element/PwEntryV3.java | 6 -- .../keepass/database/element/PwEntryV4.java | 19 +++-- .../keepass/database/element/PwGroupV4.java | 10 +-- .../keepass/database/load/Importer.java | 7 ++ .../keepass/database/load/ImporterV3.java | 18 +++-- .../keepass/database/load/ImporterV4.java | 11 ++- .../com/kunzisoft/keepass/utils/UriUtil.java | 15 ---- .../com/kunzisoft/keepass/utils/UriUtils.kt | 21 ++++++ 19 files changed, 146 insertions(+), 144 deletions(-) create mode 100644 app/src/main/java/com/kunzisoft/keepass/utils/UriUtils.kt diff --git a/app/src/androidTest/java/com/kunzisoft/keepass/tests/TestUtil.java b/app/src/androidTest/java/com/kunzisoft/keepass/tests/TestUtil.java index c7dd69151..bdec9c505 100644 --- a/app/src/androidTest/java/com/kunzisoft/keepass/tests/TestUtil.java +++ b/app/src/androidTest/java/com/kunzisoft/keepass/tests/TestUtil.java @@ -54,16 +54,6 @@ public class TestUtil { } - public static InputStream getKeyFileInputStream(Context ctx, String keyfile) throws FileNotFoundException { - InputStream keyIs = null; - if (!EmptyUtils.isNullOrEmpty(keyfile)) { - Uri uri = UriUtil.parseDefaultFile(keyfile); - keyIs = UriUtil.getUriInputStream(ctx, uri); - } - - return keyIs; - } - public static String getSdPath(String filename) { File file = new File(sdcard, filename); return file.getAbsolutePath(); diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.java b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.java index 58fbf3d60..9ed63f747 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.java +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.java @@ -188,8 +188,8 @@ public class EntryEditActivity extends LockingHideActivity fillData(); } - // Close the activity if entry to edit can't be retrieve - if (mEntry == null) { + // Close the activity if entry to edit or parent to add entry can't be retrieve + if (mEntry == null || mParent == null) { finish(); return; } diff --git a/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java b/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java index 3dcdd0a43..ccc348600 100644 --- a/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java @@ -139,7 +139,7 @@ public class NodeAdapter extends RecyclerView.Adapter { assignPreferences(); // TODO verify sort try { - this.nodeSortedList.addAll(group.getChildEntriesWithoutMetaStream()); + this.nodeSortedList.addAll(group.getChildrenWithoutMetaStream()); } catch (Exception e) { Log.e(TAG, "Can't add node elements to the list", e); Toast.makeText(context, "Can't add node elements to the list : " + e.getMessage(), Toast.LENGTH_LONG).show(); diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/AssignPasswordInDatabaseRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/AssignPasswordInDatabaseRunnable.kt index 2d294b8cd..90f0d3326 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/AssignPasswordInDatabaseRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/AssignPasswordInDatabaseRunnable.kt @@ -25,6 +25,7 @@ import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.exception.InvalidKeyFileException import com.kunzisoft.keepass.tasks.ActionRunnable import com.kunzisoft.keepass.utils.UriUtil +import com.kunzisoft.keepass.utils.getUriInputStream import java.io.IOException class AssignPasswordInDatabaseRunnable @JvmOverloads constructor( @@ -57,10 +58,9 @@ class AssignPasswordInDatabaseRunnable @JvmOverloads constructor( mBackupKey = ByteArray(database.masterKey.size) System.arraycopy(database.masterKey, 0, mBackupKey!!, 0, mBackupKey!!.size) - val uriInputStream = UriUtil.getUriInputStream(context, mKeyFile) - mMasterPassword?.let { - database.retrieveMasterKey(it, uriInputStream) - } + val uriInputStream = getUriInputStream(context.contentResolver, mKeyFile) + database.retrieveMasterKey(mMasterPassword, uriInputStream) + // To save the database super.run() finishRun(true) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/CreateDatabaseRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/CreateDatabaseRunnable.kt index c4d32c3cc..3ab478d89 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/CreateDatabaseRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/CreateDatabaseRunnable.kt @@ -22,7 +22,6 @@ 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 -import com.kunzisoft.keepass.utils.UriUtil class CreateDatabaseRunnable(private val mFilename: String, val onDatabaseCreate: (database: Database) -> ActionRunnable) @@ -35,14 +34,11 @@ class CreateDatabaseRunnable(private val mFilename: String, // Create new database record database = Database(mFilename) App.setDB(database) - - // Set Database state - database?.setUri(UriUtil.parseDefaultFile(mFilename)) - database?.loaded = true - - // Commit changes - database?.let { - onDatabaseCreate(it).run() + database?.apply { + // Set Database state + loaded = true + // Commit changes + onDatabaseCreate(this).run() } finishRun(true) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt index bf349fc54..ed8586d83 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt @@ -41,6 +41,7 @@ import com.kunzisoft.keepass.stream.LEDataInputStream import com.kunzisoft.keepass.tasks.ProgressTaskUpdater import com.kunzisoft.keepass.utils.EmptyUtils import com.kunzisoft.keepass.utils.UriUtil +import com.kunzisoft.keepass.utils.getUriInputStream import org.apache.commons.io.FileUtils import java.io.* import java.util.* @@ -176,20 +177,22 @@ class Database { GroupVersioned(pwDatabaseV4!!.recycleBin!!) } else null - constructor() {} + constructor() constructor(databasePath: String) { // TODO Test with kdb extension if (isKDBExtension(databasePath)) { setDatabaseV3(PwDatabaseV3()) } else { - val groupV4 = PwGroupV4() - setDatabaseV4(PwDatabaseV4()) - + val databaseV4 = PwDatabaseV4() + val groupV4 = databaseV4.createGroup() groupV4.title = dbNameFromPath(databasePath) - groupV4.setIconStandard(pwDatabaseV4!!.getIconFactory().folderIcon) - this.pwDatabaseV4!!.rootGroup = groupV4 + groupV4.setIconStandard(databaseV4.getIconFactory().folderIcon) + databaseV4.rootGroup = groupV4 + setDatabaseV4(databaseV4) } + + setUri(UriUtil.parseDefaultFile(databasePath)) } private fun setDatabaseV3(pwDatabaseV3: PwDatabaseV3) { @@ -236,9 +239,9 @@ class Database { } // Pass Uris as InputStreams - val inputStream: InputStream + val inputStream: InputStream? try { - inputStream = UriUtil.getUriInputStream(ctx, uri) + inputStream = getUriInputStream(ctx.contentResolver, uri) } catch (e: Exception) { Log.e("KPD", "Database::loadData", e) throw ContentFileNotFoundException.getInstance(uri) @@ -248,7 +251,7 @@ class Database { var keyFileInputStream: InputStream? = null keyfile?.let { try { - keyFileInputStream = UriUtil.getUriInputStream(ctx, keyfile) + keyFileInputStream = getUriInputStream(ctx.contentResolver, keyfile) } catch (e: Exception) { Log.e("KPD", "Database::loadData", e) throw ContentFileNotFoundException.getInstance(keyfile) @@ -271,18 +274,21 @@ class Database { // Return to the start bufferedInputStream.reset() + when { // Header of database V3 - PwDbHeaderV3.matchesHeader(sig1, sig2) -> setDatabaseV3(ImporterV3().openDatabase(bufferedInputStream, - password, - keyFileInputStream, - progressTaskUpdater)) + PwDbHeaderV3.matchesHeader(sig1, sig2) -> setDatabaseV3(ImporterV3() + .openDatabaseAndBuildIndex(bufferedInputStream, + password, + keyFileInputStream, + progressTaskUpdater)) // Header of database V4 - PwDbHeaderV4.matchesHeader(sig1, sig2) -> setDatabaseV4(ImporterV4(ctx.filesDir).openDatabase(bufferedInputStream, - password, - keyFileInputStream, - progressTaskUpdater)) + PwDbHeaderV4.matchesHeader(sig1, sig2) -> setDatabaseV4(ImporterV4(ctx.filesDir) + .openDatabaseAndBuildIndex(bufferedInputStream, + password, + keyFileInputStream, + progressTaskUpdater)) // Header not recognized else -> throw InvalidDBSignatureException() @@ -495,24 +501,14 @@ class Database { } @Throws(InvalidKeyFileException::class, IOException::class) - fun retrieveMasterKey(key: String, keyInputStream: InputStream) { + fun retrieveMasterKey(key: String?, keyInputStream: InputStream?) { pwDatabaseV3?.retrieveMasterKey(key, keyInputStream) pwDatabaseV4?.retrieveMasterKey(key, keyInputStream) } fun createEntry(): EntryVersioned? { - - pwDatabaseV3?.let { database -> - return EntryVersioned(PwEntryV3()).apply { - database.newEntryId()?.let { - nodeId = it - } - } - } - - - pwDatabaseV4?.let { database -> - return EntryVersioned(PwEntryV4()).apply { + pwDatabaseV3 ?: pwDatabaseV4?.let { database -> + return EntryVersioned(database.createEntry()).apply { database.newEntryId()?.let { nodeId = it } @@ -523,18 +519,8 @@ class Database { } fun createGroup(): GroupVersioned? { - - pwDatabaseV3?.let { database -> - return GroupVersioned(PwGroupV3()).apply { - database.newGroupId()?.let { - setNodeId(it) - } - } - } - - - pwDatabaseV4?.let { database -> - return GroupVersioned(PwGroupV4()).apply { + pwDatabaseV3 ?: pwDatabaseV4?.let { database -> + return GroupVersioned(database.createGroup()).apply { database.newGroupId()?.let { setNodeId(it) } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt index eb7fc77c9..b49dae1b9 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt @@ -25,11 +25,11 @@ class EntryVersioned : NodeVersioned, PwEntryInterface { */ constructor(entry: EntryVersioned) { if (entry.pwEntryV3 != null) { - if (this.pwEntryV3 != null) + if (this.pwEntryV3 == null) this.pwEntryV3 = PwEntryV3() } if (entry.pwEntryV4 != null) { - if (this.pwEntryV4 != null) + if (this.pwEntryV4 == null) this.pwEntryV4 = PwEntryV4() } updateWith(entry) @@ -37,14 +37,14 @@ class EntryVersioned : NodeVersioned, PwEntryInterface { constructor(entry: PwEntryV3) { this.pwEntryV4 = null - if (this.pwEntryV3 != null) + if (this.pwEntryV3 == null) this.pwEntryV3 = PwEntryV3() this.pwEntryV3?.updateWith(entry) } constructor(entry: PwEntryV4) { this.pwEntryV3 = null - if (this.pwEntryV4 != null) + if (this.pwEntryV4 == null) this.pwEntryV4 = PwEntryV4() this.pwEntryV4?.updateWith(entry) } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/GroupVersioned.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/GroupVersioned.kt index a4e7a3194..c932b8773 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/GroupVersioned.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/GroupVersioned.kt @@ -25,8 +25,8 @@ class GroupVersioned : NodeVersioned, PwGroupInterface { - return ArrayList() // TODO if needed + val children = ArrayList() + + pwGroupV3?:pwGroupV4?.getChildGroups()?.forEach { + children.add(GroupVersioned(it)) + } + + return children } override fun getChildEntries(): MutableList { val children = ArrayList() - pwGroupV3?.getChildEntries()?.forEach { - children.add(EntryVersioned(it)) - } - - pwGroupV4?.getChildEntries()?.forEach { + pwGroupV3?:pwGroupV4?.getChildEntries()?.forEach { children.add(EntryVersioned(it)) } @@ -188,17 +190,20 @@ class GroupVersioned : NodeVersioned, PwGroupInterface? { - pwGroupV3?.let { - return getChildEntries().filter { !it.isMetaStream } - } + fun getChildrenWithoutMetaStream(): List { + val children = ArrayList() + children.addAll(getChildGroups()) + + pwGroupV3?.let { + children.addAll(getChildEntries().filter { !it.isMetaStream }) + } pwGroupV4?.let { // No MetasStream in V4 - return getChildEntries() + children.addAll(getChildEntries()) } - return null + return children } override fun addChildGroup(group: GroupVersioned) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabase.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabase.java index b236523b0..07028a7a1 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabase.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabase.java @@ -37,14 +37,14 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.UUID; -public abstract class PwDatabase { +public abstract class PwDatabase, Entry extends PwEntry> { public static final UUID UUID_ZERO = new UUID(0,0); // Algorithm used to encrypt the database protected PwEncryptionAlgorithm algorithm; - protected byte masterKey[] = new byte[32]; + protected byte[] masterKey = new byte[32]; protected byte[] finalKey; protected PwIconFactory iconFactory = new PwIconFactory(); @@ -70,15 +70,15 @@ public abstract class PwDatabase { return finalKey; } - protected abstract byte[] getMasterKey(String key, InputStream keyInputStream) + protected abstract byte[] getMasterKey(@Nullable String key, @Nullable InputStream keyInputStream) throws InvalidKeyFileException, IOException; - public void retrieveMasterKey(String key, InputStream keyInputStream) + public void retrieveMasterKey(@Nullable String key, @Nullable InputStream keyInputStream) throws InvalidKeyFileException, IOException { - masterKey = getMasterKey(key, keyInputStream); - } + masterKey = getMasterKey(key, keyInputStream); + } - protected byte[] getCompositeKey(String key, InputStream keyInputStream) + protected byte[] getCompositeKey(@Nullable String key, @Nullable InputStream keyInputStream) throws InvalidKeyFileException, IOException { assert(key != null && keyInputStream != null); @@ -239,6 +239,8 @@ public abstract class PwDatabase { public abstract Group getRootGroup(); + public abstract Entry createEntry(); + /* * ------------------------------------- * Index Manipulation diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV3.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV3.java index 07bcaa97e..f1bfbbcfe 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV3.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV3.java @@ -24,6 +24,7 @@ import com.kunzisoft.keepass.crypto.finalkey.FinalKeyFactory; import com.kunzisoft.keepass.database.exception.InvalidKeyFileException; import com.kunzisoft.keepass.stream.NullOutputStream; +import javax.annotation.Nullable; import java.io.IOException; import java.io.InputStream; import java.security.DigestOutputStream; @@ -146,7 +147,7 @@ public class PwDatabaseV3 extends PwDatabase { } @Override - public byte[] getMasterKey(String key, InputStream keyInputStream) + public byte[] getMasterKey(@Nullable String key, @Nullable InputStream keyInputStream) throws InvalidKeyFileException, IOException { if (key != null && keyInputStream != null) { @@ -227,6 +228,11 @@ public class PwDatabaseV3 extends PwDatabase { return rootGroup; } + @Override + public PwEntryV3 createEntry() { + return new PwEntryV3(); + } + // TODO: This could still be refactored cleaner public void copyEncrypted(byte[] buf, int offset, int size) { // No-op diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV4.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV4.java index 5a88c944b..f05bfd5da 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV4.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwDatabaseV4.java @@ -367,7 +367,7 @@ public class PwDatabaseV4 extends PwDatabase { } @Override - public byte[] getMasterKey(String key, InputStream keyInputStream) + public byte[] getMasterKey(@Nullable String key, @Nullable InputStream keyInputStream) throws InvalidKeyFileException, IOException { byte[] fKey = new byte[]{}; @@ -523,6 +523,12 @@ public class PwDatabaseV4 extends PwDatabase { return rootGroup; } + + @Override + public PwEntryV4 createEntry() { + return new PwEntryV4(); + } + @Override public boolean isBackup(PwGroupV4 group) { if (!recycleBinEnabled) { @@ -540,7 +546,7 @@ public class PwDatabaseV4 extends PwDatabase { if (getRecycleBin() == null) { // Create recycle bin - PwGroupV4 recycleBin = new PwGroupV4(); + PwGroupV4 recycleBin = createGroup(); recycleBin.setTitle(RECYCLEBIN_NAME); recycleBin.setIconStandard(iconFactory.getTrashIcon()); recycleBin.setEnableAutoType(false); diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV3.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV3.java index f5b3c0195..97d5f1737 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV3.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV3.java @@ -187,12 +187,6 @@ public class PwEntryV3 extends PwEntry { return Type.ENTRY; } - public void setNewParent(int groupId) { - PwGroupV3 pwGroupV3 = new PwGroupV3(); - pwGroupV3.setNodeId(new PwNodeIdInt(groupId)); - setParent(pwGroupV3); - } - @Override public String getUsername() { return username; diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.java index 4f90fd83e..df369158e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwEntryV4.java @@ -173,6 +173,7 @@ public class PwEntryV4 extends PwEntry implements ITimeLog return newEntry; } + @NonNull @Override public Type getType() { return Type.ENTRY; @@ -202,47 +203,50 @@ public class PwEntryV4 extends PwEntry implements ITimeLog return spr.compile(text, this, db); } + @NonNull @Override public String getUsername() { return decodeRefKey(mDecodeRef, STR_USERNAME); } + @NonNull @Override public String getTitle() { return decodeRefKey(mDecodeRef, STR_TITLE); } + @NonNull @Override public String getPassword() { return decodeRefKey(mDecodeRef, STR_PASSWORD); } @Override - public void setTitle(String title) { + public void setTitle(@NonNull String title) { boolean protect = (mDatabase != null) && mDatabase.getMemoryProtection().protectTitle; setProtectedString(STR_TITLE, title, protect); } @Override - public void setUsername(String user) { + public void setUsername(@NonNull String user) { boolean protect = (mDatabase != null) && mDatabase.getMemoryProtection().protectUserName; setProtectedString(STR_USERNAME, user, protect); } @Override - public void setPassword(String pass) { + public void setPassword(@NonNull String pass) { boolean protect = (mDatabase != null) && mDatabase.getMemoryProtection().protectPassword; setProtectedString(STR_PASSWORD, pass, protect); } @Override - public void setUrl(String url) { + public void setUrl(@NonNull String url) { boolean protect = (mDatabase != null) && mDatabase.getMemoryProtection().protectUrl; setProtectedString(STR_URL, url, protect); } @Override - public void setNotes(String notes) { + public void setNotes(@NonNull String notes) { boolean protect = (mDatabase != null) && mDatabase.getMemoryProtection().protectNotes; setProtectedString(STR_NOTES, notes, protect); } @@ -271,17 +275,20 @@ public class PwEntryV4 extends PwEntry implements ITimeLog usageCount = count; } + @NonNull @Override public String getNotes() { return decodeRefKey(mDecodeRef, STR_NOTES); } + @NonNull @Override public String getUrl() { return decodeRefKey(mDecodeRef, STR_URL); } - @Override + @NonNull + @Override public PwIcon getIcon() { if (customIcon == null || customIcon.isUnknown()) { return super.getIcon(); diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV4.java b/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV4.java index 14b87d057..cc490ec1e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV4.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/PwGroupV4.java @@ -20,6 +20,7 @@ package com.kunzisoft.keepass.database.element; import android.os.Parcel; +import android.support.annotation.NonNull; import com.kunzisoft.keepass.database.ITimeLogger; import java.util.HashMap; @@ -74,7 +75,7 @@ public class PwGroupV4 extends PwGroup implements IT } @Override - public void writeToParcel(Parcel dest, int flags) { + public void writeToParcel(@NonNull Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeParcelable(customIcon, flags); dest.writeLong(usageCount); @@ -118,7 +119,6 @@ public class PwGroupV4 extends PwGroup implements IT lastTopVisibleEntry = source.lastTopVisibleEntry; } - @SuppressWarnings("unchecked") @Override public PwGroupV4 clone() { // Attributes in parent @@ -143,7 +143,8 @@ public class PwGroupV4 extends PwGroup implements IT return newGroup; } - @Override + @NonNull + @Override public Type getType() { return Type.GROUP; } @@ -184,7 +185,8 @@ public class PwGroupV4 extends PwGroup implements IT expires = exp; } - @Override + @NonNull + @Override public PwIcon getIcon() { if (customIcon == null || customIcon.isUnknown()) { // TODO Encapsulate with PwEntryV4 return super.getIcon(); diff --git a/app/src/main/java/com/kunzisoft/keepass/database/load/Importer.java b/app/src/main/java/com/kunzisoft/keepass/database/load/Importer.java index 1b60c2e88..ff1d1bbdb 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/load/Importer.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/load/Importer.java @@ -41,4 +41,11 @@ public abstract class Importer { public abstract PwDb openDatabase(InputStream inStream, String password, InputStream keyInputStream, ProgressTaskUpdater updater) throws IOException, InvalidDBException; + public PwDb openDatabaseAndBuildIndex(InputStream inStream, String password, InputStream keyInputStream, ProgressTaskUpdater updater) + throws IOException, InvalidDBException { + PwDb database = openDatabase(inStream, password, keyInputStream, updater); + database.populateNodesIndexes(); + return database; + } + } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3.java b/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3.java index b8ad4b184..d80b17350 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV3.java @@ -186,13 +186,13 @@ public class ImporterV3 extends Importer { } // New manual root because V3 contains multiple root groups (here available with getRootGroups()) - PwGroupV3 newRoot = new PwGroupV3(); + PwGroupV3 newRoot = databaseToOpen.createGroup(); newRoot.setLevel(-1); databaseToOpen.setRootGroup(newRoot); // Import all groups int pos = PwDbHeaderV3.BUF_SIZE; - PwGroupV3 newGrp = new PwGroupV3(); + PwGroupV3 newGrp = databaseToOpen.createGroup(); for( int i = 0; i < hdr.numGroups; ) { int fieldType = LEDataInputStream.readUShort( filebuf, pos ); pos += 2; @@ -202,7 +202,7 @@ public class ImporterV3 extends Importer { if( fieldType == 0xFFFF ) { // End-Group record. Save group and count it. databaseToOpen.addGroupIndex(newGrp); - newGrp = new PwGroupV3(); + newGrp = databaseToOpen.createGroup(); i++; } else { @@ -212,7 +212,7 @@ public class ImporterV3 extends Importer { } // Import all entries - PwEntryV3 newEnt = new PwEntryV3(); + PwEntryV3 newEnt = databaseToOpen.createEntry(); for( int i = 0; i < hdr.numEntries; ) { int fieldType = LEDataInputStream.readUShort( filebuf, pos ); int fieldSize = LEDataInputStream.readInt( filebuf, pos + 2 ); @@ -220,7 +220,7 @@ public class ImporterV3 extends Importer { if( fieldType == 0xFFFF ) { // End-Group record. Save group and count it. databaseToOpen.addEntryIndex(newEnt); - newEnt = new PwEntryV3(); + newEnt = databaseToOpen.createEntry(); i++; } else { @@ -228,8 +228,6 @@ public class ImporterV3 extends Importer { } pos += 2 + 4 + fieldSize; } - - databaseToOpen.populateNodesIndexes(); return databaseToOpen; } @@ -276,8 +274,6 @@ public class ImporterV3 extends Importer { } } - - private void readEntryField(PwDatabaseV3 db, PwEntryV3 ent, byte[] buf, int offset) throws UnsupportedEncodingException { int fieldType = LEDataInputStream.readUShort(buf, offset); offset += 2; @@ -292,7 +288,9 @@ public class ImporterV3 extends Importer { ent.setNodeId(new PwNodeIdUUID(Types.bytestoUUID(buf, offset))); break; case 0x0002 : - ent.setNewParent(LEDataInputStream.readInt(buf, offset)); + PwGroupV3 pwGroupV3 = databaseToOpen.createGroup(); + pwGroupV3.setNodeId(new PwNodeIdInt(LEDataInputStream.readInt(buf, offset))); + ent.setParent(pwGroupV3); break; case 0x0003 : int iconId = LEDataInputStream.readInt(buf, offset); diff --git a/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV4.java b/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV4.java index 43245cfc8..d571bbc07 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV4.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/load/ImporterV4.java @@ -175,8 +175,6 @@ public class ImporterV4 extends Importer { ReadXmlStreamed(isXml); - mDatabase.populateNodesIndexes(); - return mDatabase; } @@ -512,9 +510,8 @@ public class ImporterV4 extends Importer { if ( ctxGroups.size() != 0 ) throw new IOException("Group list should be empty."); - PwGroupV4 rootGroup = new PwGroupV4(); - mDatabase.setRootGroup(rootGroup); - ctxGroups.push(rootGroup); + mDatabase.setRootGroup(mDatabase.createGroup()); + ctxGroups.push(mDatabase.getRootGroup()); ctxGroup = ctxGroups.peek(); return SwitchContext(ctx, KdbContext.Group, xpp); @@ -551,7 +548,7 @@ public class ImporterV4 extends Importer { } else if ( name.equalsIgnoreCase(PwDatabaseV4XML.ElemCustomData) ) { return SwitchContext(ctx, KdbContext.GroupCustomData, xpp); } else if ( name.equalsIgnoreCase(PwDatabaseV4XML.ElemGroup) ) { - ctxGroup = new PwGroupV4(); + ctxGroup = mDatabase.createGroup(); PwGroupV4 groupPeek = ctxGroups.peek(); groupPeek.addChildGroup(ctxGroup); ctxGroup.setParent(groupPeek); @@ -559,7 +556,7 @@ public class ImporterV4 extends Importer { return SwitchContext(ctx, KdbContext.Group, xpp); } else if ( name.equalsIgnoreCase(PwDatabaseV4XML.ElemEntry) ) { - ctxEntry = new PwEntryV4(); + ctxEntry = mDatabase.createEntry(); ctxGroup.addChildEntry(ctxEntry); ctxEntry.setParent(ctxGroup); diff --git a/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.java b/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.java index 475bcc178..91b4838a8 100644 --- a/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.java +++ b/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.java @@ -61,21 +61,6 @@ public class UriUtil { return left.equals(uriRight); } - public static InputStream getUriInputStream(Context ctx, Uri uri) throws FileNotFoundException { - if (uri == null) return null; - - String scheme = uri.getScheme(); - if (EmptyUtils.isNullOrEmpty(scheme) || scheme.equals("file")) { - return new FileInputStream(uri.getPath()); - } - else if (scheme.equals("content")) { - return ctx.getContentResolver().openInputStream(uri); - } - else { - return null; - } - } - /** * Many android apps respond with non-writeable content URIs that correspond to files. * This will attempt to translate the content URIs to file URIs when possible/appropriate diff --git a/app/src/main/java/com/kunzisoft/keepass/utils/UriUtils.kt b/app/src/main/java/com/kunzisoft/keepass/utils/UriUtils.kt new file mode 100644 index 000000000..0f0df99d3 --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/utils/UriUtils.kt @@ -0,0 +1,21 @@ +package com.kunzisoft.keepass.utils + +import android.content.ContentResolver +import android.net.Uri +import java.io.FileInputStream +import java.io.FileNotFoundException +import java.io.InputStream + +@Throws(FileNotFoundException::class) +fun getUriInputStream(contentResolver: ContentResolver, uri: Uri?): InputStream? { + if (uri == null) return null + + val scheme = uri.scheme + return if (EmptyUtils.isNullOrEmpty(scheme) || scheme == "file") { + FileInputStream(uri.path!!) + } else if (scheme == "content") { + contentResolver.openInputStream(uri) + } else { + null + } +} \ No newline at end of file