mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-05-14 05:52:52 +00:00
Added (de)serialization for the custom images
This commit is contained in:
parent
d8bfe2e947
commit
11b195aab3
2 changed files with 51 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
package me.impy.aegis.crypto;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.util.Base64;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
|
@ -17,6 +18,7 @@ public class KeyInfo implements Serializable {
|
|||
private String _algorithm = "SHA1";
|
||||
private int _digits = 6;
|
||||
private int _period = 30;
|
||||
private String _image;
|
||||
|
||||
public String getURL() {
|
||||
Uri.Builder builder = new Uri.Builder();
|
||||
|
@ -27,6 +29,9 @@ public class KeyInfo implements Serializable {
|
|||
builder.appendQueryParameter("period", Integer.toString(_period));
|
||||
builder.appendQueryParameter("algorithm", _algorithm);
|
||||
builder.appendQueryParameter("secret", new String(Base32.encode(_secret)));
|
||||
if (_image != null && !_image.equals("")) {
|
||||
builder.appendQueryParameter("image", _image);
|
||||
}
|
||||
if (_type.equals("hotp")) {
|
||||
builder.appendQueryParameter("counter", Long.toString(_counter));
|
||||
}
|
||||
|
@ -103,6 +108,10 @@ public class KeyInfo implements Serializable {
|
|||
if (digits != null) {
|
||||
info.setDigits(Integer.parseInt(digits));
|
||||
}
|
||||
String image = url.getQueryParameter("image");
|
||||
if (image != null) {
|
||||
info.setImage(Base64.decode(image, Base64.DEFAULT));
|
||||
}
|
||||
|
||||
// 'counter' is required if the type is 'hotp'
|
||||
String counter = url.getQueryParameter("counter");
|
||||
|
@ -173,6 +182,16 @@ public class KeyInfo implements Serializable {
|
|||
setSecret(secret);
|
||||
}
|
||||
|
||||
public void setImage(byte[] image)
|
||||
{
|
||||
_image = Base64.encodeToString(image, Base64.DEFAULT);
|
||||
}
|
||||
|
||||
public byte[] getImage()
|
||||
{
|
||||
return Base64.decode(_image, Base64.DEFAULT);
|
||||
}
|
||||
|
||||
public void setSecret(byte[] secret) {
|
||||
_secret = secret;
|
||||
}
|
||||
|
|
|
@ -3,7 +3,10 @@ package me.impy.aegis.ui;
|
|||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.ArrayRes;
|
||||
import android.support.v7.app.ActionBar;
|
||||
|
@ -29,6 +32,8 @@ import com.esafirm.imagepicker.features.ImagePicker;
|
|||
import com.esafirm.imagepicker.features.ReturnMode;
|
||||
import com.esafirm.imagepicker.model.Image;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import de.hdodenhof.circleimageview.CircleImageView;
|
||||
import me.impy.aegis.R;
|
||||
import me.impy.aegis.crypto.KeyInfo;
|
||||
|
@ -349,6 +354,12 @@ public class EditProfileActivity extends AegisActivity {
|
|||
info.setAlgorithm(algo);
|
||||
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();
|
||||
|
||||
info.setImage(bitmapdata);
|
||||
} catch (KeyInfoException e) {
|
||||
onError("The entered info is incorrect: " + e.getMessage());
|
||||
return false;
|
||||
|
@ -419,4 +430,25 @@ public class EditProfileActivity extends AegisActivity {
|
|||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static Bitmap drawableToBitmap(Drawable drawable) {
|
||||
if (drawable instanceof BitmapDrawable) {
|
||||
return ((BitmapDrawable) drawable).getBitmap();
|
||||
}
|
||||
|
||||
final int width = !drawable.getBounds().isEmpty() ? drawable
|
||||
.getBounds().width() : drawable.getIntrinsicWidth();
|
||||
|
||||
final int height = !drawable.getBounds().isEmpty() ? drawable
|
||||
.getBounds().height() : drawable.getIntrinsicHeight();
|
||||
|
||||
final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width,
|
||||
height <= 0 ? 1 : height, Bitmap.Config.ARGB_8888);
|
||||
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
drawable.draw(canvas);
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue