diff --git a/app/src/main/java/me/impy/aegis/db/Database.java b/app/src/main/java/me/impy/aegis/db/Database.java index d119ce16..becf2c9e 100644 --- a/app/src/main/java/me/impy/aegis/db/Database.java +++ b/app/src/main/java/me/impy/aegis/db/Database.java @@ -36,13 +36,25 @@ public class Database { throw new Exception("Unsupported version"); } - _counter = obj.getLong("counter"); + // if no counter is present, ignore and reset the id of all entries + boolean ignoreID = false; + if (!obj.has("counter")) { + ignoreID = true; + } else { + _counter = obj.getLong("counter"); + } JSONArray array = obj.getJSONArray("entries"); for (int i = 0; i < array.length(); i++) { - DatabaseEntry e = new DatabaseEntry(null); - e.deserialize(array.getJSONObject(i)); - _entries.add(e); + DatabaseEntry entry = new DatabaseEntry(null); + entry.deserialize(array.getJSONObject(i), ignoreID); + + // if the id was ignored, make sure it receives a new one + if (ignoreID) { + addKey(entry); + } else { + _entries.add(entry); + } } } diff --git a/app/src/main/java/me/impy/aegis/db/DatabaseEntry.java b/app/src/main/java/me/impy/aegis/db/DatabaseEntry.java index ce6dc2f8..be92b328 100644 --- a/app/src/main/java/me/impy/aegis/db/DatabaseEntry.java +++ b/app/src/main/java/me/impy/aegis/db/DatabaseEntry.java @@ -25,8 +25,10 @@ public class DatabaseEntry implements Serializable { return obj; } - public void deserialize(JSONObject obj) throws Exception { - _id = obj.getLong("id"); + public void deserialize(JSONObject obj, boolean ignoreID) throws Exception { + if (!ignoreID) { + _id = obj.getLong("id"); + } _name = obj.getString("name"); _info = KeyInfo.fromURL(obj.getString("url")); }