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

View file

@ -18,10 +18,11 @@ public class TextDrawableHelper {
text = fallback;
}
ColorGenerator generator = ColorGenerator.MATERIAL;
int color = generator.getColor(text);
int color = ColorGenerator.MATERIAL.getColor(text);
return TextDrawable.builder().beginConfig()
.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
if (_entry != null) {
if (_entry.getIcon() != null) {
if (_entry.hasIcon()) {
byte[] imageBytes = _entry.getIcon();
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
_iconView.setImageBitmap(image);
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
_iconView.setImageBitmap(bitmap);
_hasCustomImage = true;
} else {
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() {
updateLockIcon();
for (DatabaseEntry entry : _db.getEntries()) {
_entryListView.addEntry(entry);
}
_entryListView.addEntries(_db.getEntries());
}
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) {
entry = getEntryByUUID(entry.getUUID());
int position = _entries.indexOf(entry);

View file

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

View file

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