Added way to remove custom images

This commit is contained in:
Michael Schättgen 2018-06-07 00:30:49 +02:00
parent 97eb3490d8
commit f9e716391c
5 changed files with 52 additions and 10 deletions

View file

@ -184,12 +184,17 @@ public class KeyInfo implements Serializable {
public void setImage(byte[] image)
{
_image = Base64.encodeToString(image, Base64.DEFAULT);
if(image != null && !image.equals(""))
{
_image = Base64.encodeToString(image, Base64.DEFAULT);
} else {
_image = null;
}
}
public byte[] getImage()
{
return Base64.decode(_image, Base64.DEFAULT);
return _image != null ? Base64.decode(_image, Base64.DEFAULT) : null;
}
public void setSecret(byte[] secret) {

View file

@ -49,6 +49,8 @@ import me.impy.aegis.ui.views.KeyProfile;
public class EditProfileActivity extends AegisActivity {
private boolean _isNew = false;
private boolean _edited = false;
private boolean _hasCustomImage = false;
private KeyProfile _profile;
private CircleImageView _iconView;
@ -131,8 +133,11 @@ public class EditProfileActivity extends AegisActivity {
@Override
public void afterTextChanged(Editable s) {
TextDrawable drawable = TextDrawableHelper.generate(s.toString(), _iconView);
_iconView.setImageDrawable(drawable);
if(!_hasCustomImage)
{
TextDrawable drawable = TextDrawableHelper.generate(s.toString(), _iconView);
_iconView.setImageDrawable(drawable);
}
}
});
@ -166,7 +171,18 @@ public class EditProfileActivity extends AegisActivity {
private void updateFields() {
DatabaseEntry entry = _profile.getEntry();
_iconView.setImageDrawable(_profile.getDrawable(_iconView));
if (_profile.getEntry().getInfo().getImage() != null)
{
byte[] imageBytes = _profile.getEntry().getInfo().getImage();
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
_iconView.setImageBitmap(image);
_hasCustomImage = true;
}
else
{
_iconView.setImageDrawable(_profile.getDrawable(_iconView));
}
_textName.setText(entry.getName());
_textIssuer.setText(entry.getInfo().getIssuer());
@ -270,6 +286,9 @@ public class EditProfileActivity extends AegisActivity {
finish(true);
});
break;
case R.id.action_default_image:
_iconView.setImageDrawable(_profile.getDrawable(_iconView));
_hasCustomImage = false;
default:
return super.onOptionsItemSelected(item);
}
@ -283,6 +302,10 @@ public class EditProfileActivity extends AegisActivity {
if (_isNew) {
menu.findItem(R.id.action_delete).setVisible(false);
}
if (!_hasCustomImage) {
menu.findItem(R.id.action_default_image).setVisible(false);
}
return true;
}
@ -309,6 +332,7 @@ public class EditProfileActivity extends AegisActivity {
public void onClick(View v) {
_iconView.setImageBitmap(_kropView.getCroppedBitmap());
_kropView.setVisibility(View.GONE);
_hasCustomImage = true;
}
});
}
@ -355,11 +379,18 @@ public class EditProfileActivity extends AegisActivity {
info.setType(type);
info.setAccountName(_textName.getText().toString());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
drawableToBitmap(_iconView.getDrawable()).compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
if(_hasCustomImage)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
drawableToBitmap(_iconView.getDrawable()).compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
info.setImage(bitmapdata);
info.setImage(bitmapdata);
}
else
{
info.setImage(null);
}
} catch (KeyInfoException e) {
onError("The entered info is incorrect: " + e.getMessage());
return false;

View file

@ -65,7 +65,7 @@ public class KeyProfileHolder extends RecyclerView.ViewHolder {
_profileIssuer.setText(" - " + profile.getEntry().getInfo().getIssuer());
}
if (profile.getEntry().getInfo().getImage() != null)
if (profile.getEntry().getInfo().getImage() != null && !profile.getEntry().getInfo().getImage().equals(""))
{
byte[] imageBytes = profile.getEntry().getInfo().getImage();
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);