Fix serialization of entry icons and some other stuff

This commit is contained in:
Alexander Bakker 2018-06-07 14:33:33 +02:00
parent 8419e1f35d
commit 606d6e77e9
7 changed files with 31 additions and 15 deletions

View file

@ -37,8 +37,8 @@ public class DatabaseEntry implements Serializable {
obj.put("uuid", _uuid.toString()); obj.put("uuid", _uuid.toString());
obj.put("name", _name); obj.put("name", _name);
obj.put("issuer", _issuer); obj.put("issuer", _issuer);
obj.put("info", _info.toJson());
obj.put("icon", _icon == null ? JSONObject.NULL : Base64.encode(_icon)); obj.put("icon", _icon == null ? JSONObject.NULL : Base64.encode(_icon));
obj.put("info", _info.toJson());
} catch (JSONException e) { } catch (JSONException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -55,12 +55,13 @@ public class DatabaseEntry implements Serializable {
} }
_name = obj.getString("name"); _name = obj.getString("name");
_issuer = obj.getString("issuer"); _issuer = obj.getString("issuer");
_info = OtpInfo.parseJson(obj.getString("type"), obj.getJSONObject("info"));
String icon = obj.optString("icon", null); Object icon = obj.get("icon");
if (icon != null) { if (icon != JSONObject.NULL) {
_icon = Base64.decode(icon); _icon = Base64.decode((String) icon);
} }
_info = OtpInfo.parseJson(obj.getString("type"), obj.getJSONObject("info"));
} }
public UUID getUUID() { public UUID getUUID() {
@ -98,4 +99,8 @@ public class DatabaseEntry implements Serializable {
public void setIcon(byte[] icon) { public void setIcon(byte[] icon) {
_icon = icon; _icon = icon;
} }
public boolean hasIcon() {
return _icon != null;
}
} }

View file

@ -18,10 +18,11 @@ public class TextDrawableHelper {
text = fallback; text = fallback;
} }
ColorGenerator generator = ColorGenerator.MATERIAL; int color = ColorGenerator.MATERIAL.getColor(text);
int color = generator.getColor(text);
return TextDrawable.builder().beginConfig() return TextDrawable.builder().beginConfig()
.width(view.getWidth()) .width(view.getWidth())
.height(view.getHeight()).endConfig().buildRect(text.substring(0, 1).toUpperCase(), color); .height(view.getHeight())
.endConfig()
.buildRect(text.substring(0, 1).toUpperCase(), color);
} }
} }

View file

@ -115,10 +115,10 @@ public class EditEntryActivity extends AegisActivity {
// fill the fields with values if possible // fill the fields with values if possible
if (_entry != null) { if (_entry != null) {
if (_entry.getIcon() != null) { if (_entry.hasIcon()) {
byte[] imageBytes = _entry.getIcon(); byte[] imageBytes = _entry.getIcon();
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
_iconView.setImageBitmap(image); _iconView.setImageBitmap(bitmap);
_hasCustomImage = true; _hasCustomImage = true;
} else { } else {
TextDrawable drawable = TextDrawableHelper.generate(_entry.getIssuer(), _entry.getName(), _iconView); TextDrawable drawable = TextDrawableHelper.generate(_entry.getIssuer(), _entry.getName(), _iconView);

View file

@ -411,9 +411,7 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene
private void loadEntries() { private void loadEntries() {
updateLockIcon(); updateLockIcon();
for (DatabaseEntry entry : _db.getEntries()) { _entryListView.addEntries(_db.getEntries());
_entryListView.addEntry(entry);
}
} }
private void updateLockIcon() { private void updateLockIcon() {

View file

@ -43,6 +43,11 @@ public class EntryAdapter extends RecyclerView.Adapter<EntryHolder> implements I
} }
} }
public void addEntries(List<DatabaseEntry> entries) {
_entries.addAll(entries);
notifyDataSetChanged();
}
public void removeEntry(DatabaseEntry entry) { public void removeEntry(DatabaseEntry entry) {
entry = getEntryByUUID(entry.getUUID()); entry = getEntryByUUID(entry.getUUID());
int position = _entries.indexOf(entry); int position = _entries.indexOf(entry);

View file

@ -76,7 +76,7 @@ public class EntryHolder extends RecyclerView.ViewHolder {
_profileIssuer.setText(" - " + entry.getIssuer()); _profileIssuer.setText(" - " + entry.getIssuer());
} }
if (entry.getIcon() != null) { if (_entry.hasIcon()) {
byte[] imageBytes = entry.getIcon(); byte[] imageBytes = entry.getIcon();
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
_profileDrawable.setImageBitmap(image); _profileDrawable.setImageBitmap(image);

View file

@ -10,6 +10,8 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import java.util.List;
import me.impy.aegis.R; import me.impy.aegis.R;
import me.impy.aegis.db.DatabaseEntry; import me.impy.aegis.db.DatabaseEntry;
import me.impy.aegis.helpers.SimpleItemTouchHelperCallback; import me.impy.aegis.helpers.SimpleItemTouchHelperCallback;
@ -135,6 +137,11 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
checkPeriodUniformity(); checkPeriodUniformity();
} }
public void addEntries(List<DatabaseEntry> entries) {
_adapter.addEntries(entries);
checkPeriodUniformity();
}
public void removeEntry(DatabaseEntry entry) { public void removeEntry(DatabaseEntry entry) {
_adapter.removeEntry(entry); _adapter.removeEntry(entry);
checkPeriodUniformity(); checkPeriodUniformity();