From bf15ee43da6ee0bebb5189f5719c971705f55a5d Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sat, 28 Jul 2018 14:00:49 +0200 Subject: [PATCH] Populate custom fields in search entry --- .../adapters/SearchEntryCursorAdapter.java | 2 +- .../kunzisoft/keepass/database/Database.java | 10 +-- .../keepass/database/cursor/EntryCursor.java | 78 ++++++++++++------- .../database/cursor/ExtraFieldCursor.java | 43 ++++++++++ 4 files changed, 99 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/com/kunzisoft/keepass/database/cursor/ExtraFieldCursor.java diff --git a/app/src/main/java/com/kunzisoft/keepass/adapters/SearchEntryCursorAdapter.java b/app/src/main/java/com/kunzisoft/keepass/adapters/SearchEntryCursorAdapter.java index 533e76e81..949d8c2f5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/adapters/SearchEntryCursorAdapter.java +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/SearchEntryCursorAdapter.java @@ -117,7 +117,7 @@ public class SearchEntryCursorAdapter extends CursorAdapter { cursor.move(position)) { pwEntry = database.createEntry(); - database.populateEntry(pwEntry, cursor); + database.populateEntry(pwEntry, (EntryCursor) cursor); } return pwEntry; } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/Database.java b/app/src/main/java/com/kunzisoft/keepass/database/Database.java index 1e11483fe..68bd6a7a9 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/Database.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/Database.java @@ -210,9 +210,9 @@ public class Database { try { switch (getPwDatabase().getVersion()) { case V3: - EntryCursor.addEntry(cursor, i, (PwEntryV3) entry); + cursor.addEntry((PwEntryV3) entry); case V4: - EntryCursor.addEntry(cursor, i, (PwEntryV4) entry); + cursor.addEntry((PwEntryV4) entry); } } catch (Exception e) { Log.e(TAG, "This version of PwGroup can't be populated", e); @@ -222,16 +222,16 @@ public class Database { return cursor; } - public void populateEntry(PwEntry pwEntry, Cursor cursor) { + public void populateEntry(PwEntry pwEntry, EntryCursor cursor) { // TODO invert field reference manager pwEntry.startToManageFieldReferences(getPwDatabase()); PwIconFactory iconFactory = getPwDatabase().getIconFactory(); try { switch (getPwDatabase().getVersion()) { case V3: - EntryCursor.populateEntry(cursor, (PwEntryV3) pwEntry, iconFactory); + cursor.populateEntry((PwEntryV3) pwEntry, iconFactory); case V4: - EntryCursor.populateEntry(cursor, (PwEntryV4) pwEntry, iconFactory); + cursor.populateEntry((PwEntryV4) pwEntry, iconFactory); } } catch (Exception e) { Log.e(TAG, "This version of PwGroup can't be populated", e); diff --git a/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursor.java b/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursor.java index 9f3981bc8..693cd042d 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursor.java +++ b/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursor.java @@ -1,6 +1,5 @@ package com.kunzisoft.keepass.database.cursor; -import android.database.Cursor; import android.database.MatrixCursor; import android.provider.BaseColumns; @@ -16,18 +15,20 @@ import java.util.UUID; public class EntryCursor extends MatrixCursor { + private long entryId; public static final String _ID = BaseColumns._ID; - public static final String COLUMN_INDEX_UUID_MOST_SIGNIFICANT_BITS = "UUIDMostSignificantBits"; - public static final String COLUMN_INDEX_UUID_LEAST_SIGNIFICANT_BITS = "UUIDLeastSignificantBits"; + public static final String COLUMN_INDEX_UUID_MOST_SIGNIFICANT_BITS = "UUID_most_significant_bits"; + public static final String COLUMN_INDEX_UUID_LEAST_SIGNIFICANT_BITS = "UUID_least_significant_bits"; public static final String COLUMN_INDEX_TITLE = "title"; - public static final String COLUMN_INDEX_ICON_STANDARD = "iconStandard"; - public static final String COLUMN_INDEX_ICON_CUSTOM_UUID_MOST_SIGNIFICANT_BITS = "iconCustomUUIDMostSignificantBits"; - public static final String COLUMN_INDEX_ICON_CUSTOM_UUID_LEAST_SIGNIFICANT_BITS = "iconCustomUUIDLeastSignificantBits"; + public static final String COLUMN_INDEX_ICON_STANDARD = "icon_standard"; + public static final String COLUMN_INDEX_ICON_CUSTOM_UUID_MOST_SIGNIFICANT_BITS = "icon_custom_UUID_most_significant_bits"; + public static final String COLUMN_INDEX_ICON_CUSTOM_UUID_LEAST_SIGNIFICANT_BITS = "icon_custom_UUID_least_significant_bits"; public static final String COLUMN_INDEX_USERNAME = "username"; public static final String COLUMN_INDEX_PASSWORD = "password"; public static final String COLUMN_INDEX_URL = "URL"; public static final String COLUMN_INDEX_NOTES = "notes"; - // TODO custom elements + + private ExtraFieldCursor extraFieldCursor; public EntryCursor() { super(new String[]{ _ID, @@ -41,10 +42,12 @@ public class EntryCursor extends MatrixCursor { COLUMN_INDEX_PASSWORD, COLUMN_INDEX_URL, COLUMN_INDEX_NOTES}); + entryId = 0; + extraFieldCursor = new ExtraFieldCursor(); } - public static void addEntry(MatrixCursor cursor, int id, PwEntryV3 entry) { - cursor.addRow(new Object[] {id, + public void addEntry(PwEntryV3 entry) { + addRow(new Object[] {entryId, entry.getUUID().getMostSignificantBits(), entry.getUUID().getLeastSignificantBits(), entry.getTitle(), @@ -54,12 +57,12 @@ public class EntryCursor extends MatrixCursor { entry.getUsername(), entry.getPassword(), entry.getUrl(), - entry.getNotes(), - }); + entry.getNotes()}); + entryId++; } - public static void addEntry(MatrixCursor cursor, int id, PwEntryV4 entry) { - cursor.addRow(new Object[] {id, + public void addEntry(PwEntryV4 entry) { + addRow(new Object[] {entryId, entry.getUUID().getMostSignificantBits(), entry.getUUID().getLeastSignificantBits(), entry.getTitle(), @@ -70,34 +73,53 @@ public class EntryCursor extends MatrixCursor { entry.getPassword(), entry.getUrl(), entry.getNotes()}); + + entry.getFields().doActionToAllCustomProtectedField((key, value) -> { + extraFieldCursor.addExtraField(entryId, key, value); + }); + + entryId++; } - private static void populateEntryBaseVersion(Cursor cursor, PwEntry pwEntry, PwIconFactory iconFactory) { + private void populateEntryBaseVersion(PwEntry pwEntry, PwIconFactory iconFactory) { pwEntry.setUUID( - new UUID(cursor.getLong(cursor.getColumnIndex(EntryCursor.COLUMN_INDEX_UUID_MOST_SIGNIFICANT_BITS)), - cursor.getLong(cursor.getColumnIndex(EntryCursor.COLUMN_INDEX_UUID_LEAST_SIGNIFICANT_BITS)))); - pwEntry.setTitle(cursor.getString(cursor.getColumnIndex(EntryCursor.COLUMN_INDEX_TITLE))); + new UUID(getLong(getColumnIndex(EntryCursor.COLUMN_INDEX_UUID_MOST_SIGNIFICANT_BITS)), + getLong(getColumnIndex(EntryCursor.COLUMN_INDEX_UUID_LEAST_SIGNIFICANT_BITS)))); + pwEntry.setTitle(getString(getColumnIndex(EntryCursor.COLUMN_INDEX_TITLE))); - PwIconStandard iconStandard = iconFactory.getIcon(cursor.getInt(cursor.getColumnIndex(EntryCursor.COLUMN_INDEX_ICON_STANDARD))); + PwIconStandard iconStandard = iconFactory.getIcon(getInt(getColumnIndex(EntryCursor.COLUMN_INDEX_ICON_STANDARD))); pwEntry.setIcon(iconStandard); - pwEntry.setUsername(cursor.getString(cursor.getColumnIndex(EntryCursor.COLUMN_INDEX_USERNAME))); - pwEntry.setPassword(cursor.getString(cursor.getColumnIndex(EntryCursor.COLUMN_INDEX_PASSWORD))); - pwEntry.setUrl(cursor.getString(cursor.getColumnIndex(EntryCursor.COLUMN_INDEX_URL))); - pwEntry.setNotes(cursor.getString(cursor.getColumnIndex(EntryCursor.COLUMN_INDEX_NOTES))); + pwEntry.setUsername(getString(getColumnIndex(EntryCursor.COLUMN_INDEX_USERNAME))); + pwEntry.setPassword(getString(getColumnIndex(EntryCursor.COLUMN_INDEX_PASSWORD))); + pwEntry.setUrl(getString(getColumnIndex(EntryCursor.COLUMN_INDEX_URL))); + pwEntry.setNotes(getString(getColumnIndex(EntryCursor.COLUMN_INDEX_NOTES))); } - public static void populateEntry(Cursor cursor, PwEntryV3 pwEntry, PwIconFactory iconFactory) { - populateEntryBaseVersion(cursor, pwEntry, iconFactory); + public void populateEntry(PwEntryV3 pwEntry, PwIconFactory iconFactory) { + populateEntryBaseVersion(pwEntry, iconFactory); } - public static void populateEntry(Cursor cursor, PwEntryV4 pwEntry, PwIconFactory iconFactory) { - populateEntryBaseVersion(cursor, pwEntry, iconFactory); + public void populateEntry(PwEntryV4 pwEntry, PwIconFactory iconFactory) { + populateEntryBaseVersion(pwEntry, iconFactory); + // Retrieve custom icon PwIconCustom iconCustom = iconFactory.getIcon( - new UUID(cursor.getLong(cursor.getColumnIndex(EntryCursor.COLUMN_INDEX_ICON_CUSTOM_UUID_MOST_SIGNIFICANT_BITS)), - cursor.getLong(cursor.getColumnIndex(EntryCursor.COLUMN_INDEX_ICON_CUSTOM_UUID_LEAST_SIGNIFICANT_BITS)))); + new UUID(getLong(getColumnIndex(EntryCursor.COLUMN_INDEX_ICON_CUSTOM_UUID_MOST_SIGNIFICANT_BITS)), + getLong(getColumnIndex(EntryCursor.COLUMN_INDEX_ICON_CUSTOM_UUID_LEAST_SIGNIFICANT_BITS)))); pwEntry.setCustomIcon(iconCustom); + + // Retrieve extra fields + if (extraFieldCursor.moveToFirst()) { + while (!extraFieldCursor.isAfterLast()) { + // Add a new extra field only if entryId is the one we want + if (extraFieldCursor.getLong(extraFieldCursor.getColumnIndex(ExtraFieldCursor.FOREIGN_KEY_ENTRY_ID)) + == getLong(getColumnIndex(EntryCursor._ID))) { + extraFieldCursor.populateExtraFieldInEntry(pwEntry); + } + extraFieldCursor.moveToNext(); + } + } } } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/cursor/ExtraFieldCursor.java b/app/src/main/java/com/kunzisoft/keepass/database/cursor/ExtraFieldCursor.java new file mode 100644 index 000000000..af618ebb7 --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/database/cursor/ExtraFieldCursor.java @@ -0,0 +1,43 @@ +package com.kunzisoft.keepass.database.cursor; + +import android.database.MatrixCursor; +import android.provider.BaseColumns; + +import com.kunzisoft.keepass.database.PwEntryV4; +import com.kunzisoft.keepass.database.security.ProtectedString; + +public class ExtraFieldCursor extends MatrixCursor { + + private long fieldId; + public static final String _ID = BaseColumns._ID; + public static final String FOREIGN_KEY_ENTRY_ID = "entry_id"; + public static final String COLUMN_LABEL = "label"; + public static final String COLUMN_PROTECTION = "protection"; + public static final String COLUMN_VALUE = "value"; + + public ExtraFieldCursor() { + super(new String[]{ _ID, + FOREIGN_KEY_ENTRY_ID, + COLUMN_LABEL, + COLUMN_PROTECTION, + COLUMN_VALUE}); + fieldId = 0; + } + + public synchronized void addExtraField(long entryId, String label, ProtectedString value) { + addRow(new Object[] {fieldId, + entryId, + label, + (value.isProtected()) ? 1 : 0, + value.toString()}); + fieldId++; + } + + public void populateExtraFieldInEntry(PwEntryV4 pwEntry) { + + pwEntry.addExtraField(getString(getColumnIndex(ExtraFieldCursor.COLUMN_LABEL)), + new ProtectedString((getInt(getColumnIndex(ExtraFieldCursor.COLUMN_PROTECTION)) > 0), + getString(getColumnIndex(ExtraFieldCursor.COLUMN_VALUE)))); + + } +}