mirror of
https://github.com/moparisthebest/Conversations
synced 2024-11-28 03:32:15 -05:00
rotate avatars
Conflicts: src/eu/siacs/conversations/persistance/FileBackend.java
This commit is contained in:
parent
d42ea45b1c
commit
c62937016b
@ -44,8 +44,9 @@ public class FileBackend {
|
|||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
private LruCache<String, Bitmap> thumbnailCache;
|
private LruCache<String, Bitmap> thumbnailCache;
|
||||||
|
|
||||||
private SimpleDateFormat imageDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS",Locale.US);
|
private SimpleDateFormat imageDateFormat = new SimpleDateFormat(
|
||||||
|
"yyyyMMdd_HHmmssSSS", Locale.US);
|
||||||
|
|
||||||
public FileBackend(Context context) {
|
public FileBackend(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
@ -85,14 +86,15 @@ public class FileBackend {
|
|||||||
}
|
}
|
||||||
return new JingleFile(path + "/" + filename);
|
return new JingleFile(path + "/" + filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JingleFile getJingleFile(Message message) {
|
public JingleFile getJingleFile(Message message) {
|
||||||
return getJingleFile(message, true);
|
return getJingleFile(message, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JingleFile getJingleFile(Message message, boolean decrypted) {
|
public JingleFile getJingleFile(Message message, boolean decrypted) {
|
||||||
StringBuilder filename = new StringBuilder();
|
StringBuilder filename = new StringBuilder();
|
||||||
filename.append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
|
filename.append(Environment.getExternalStoragePublicDirectory(
|
||||||
|
Environment.DIRECTORY_PICTURES).getAbsolutePath());
|
||||||
filename.append("/Conversations/");
|
filename.append("/Conversations/");
|
||||||
filename.append(message.getUuid());
|
filename.append(message.getUuid());
|
||||||
if ((decrypted) || (message.getEncryption() == Message.ENCRYPTION_NONE)) {
|
if ((decrypted) || (message.getEncryption() == Message.ENCRYPTION_NONE)) {
|
||||||
@ -106,7 +108,7 @@ public class FileBackend {
|
|||||||
}
|
}
|
||||||
return new JingleFile(filename.toString());
|
return new JingleFile(filename.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap resize(Bitmap originalBitmap, int size) {
|
public Bitmap resize(Bitmap originalBitmap, int size) {
|
||||||
int w = originalBitmap.getWidth();
|
int w = originalBitmap.getWidth();
|
||||||
int h = originalBitmap.getHeight();
|
int h = originalBitmap.getHeight();
|
||||||
@ -144,7 +146,8 @@ public class FileBackend {
|
|||||||
private JingleFile copyImageToPrivateStorage(Message message, Uri image,
|
private JingleFile copyImageToPrivateStorage(Message message, Uri image,
|
||||||
int sampleSize) throws ImageCopyException {
|
int sampleSize) throws ImageCopyException {
|
||||||
try {
|
try {
|
||||||
InputStream is = context.getContentResolver().openInputStream(image);
|
InputStream is = context.getContentResolver()
|
||||||
|
.openInputStream(image);
|
||||||
JingleFile file = getJingleFile(message);
|
JingleFile file = getJingleFile(message);
|
||||||
file.getParentFile().mkdirs();
|
file.getParentFile().mkdirs();
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
@ -194,17 +197,23 @@ public class FileBackend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getRotation(Uri image) {
|
private int getRotation(Uri image) {
|
||||||
if ("content".equals(image.getScheme())) {
|
if ("content".equals(image.getScheme())) {
|
||||||
Cursor cursor = context.getContentResolver().query(image,
|
try {
|
||||||
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
|
Cursor cursor = context
|
||||||
|
.getContentResolver()
|
||||||
if (cursor.getCount() != 1) {
|
.query(image,
|
||||||
return -1;
|
new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
|
||||||
}
|
null, null, null);
|
||||||
cursor.moveToFirst();
|
if (cursor.getCount() != 1) {
|
||||||
return cursor.getInt(0);
|
return -1;
|
||||||
|
}
|
||||||
|
cursor.moveToFirst();
|
||||||
|
return cursor.getInt(0);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ExifInterface exif;
|
ExifInterface exif;
|
||||||
try {
|
try {
|
||||||
@ -273,28 +282,32 @@ public class FileBackend {
|
|||||||
|
|
||||||
public Uri getTakePhotoUri() {
|
public Uri getTakePhotoUri() {
|
||||||
StringBuilder pathBuilder = new StringBuilder();
|
StringBuilder pathBuilder = new StringBuilder();
|
||||||
pathBuilder.append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
|
pathBuilder.append(Environment
|
||||||
|
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
|
||||||
pathBuilder.append('/');
|
pathBuilder.append('/');
|
||||||
pathBuilder.append("Camera");
|
pathBuilder.append("Camera");
|
||||||
pathBuilder.append('/');
|
pathBuilder.append('/');
|
||||||
pathBuilder.append("IMG_"+this.imageDateFormat.format(new Date())+".jpg");
|
pathBuilder.append("IMG_" + this.imageDateFormat.format(new Date())
|
||||||
Uri uri = Uri.parse("file://"+pathBuilder.toString());
|
+ ".jpg");
|
||||||
|
Uri uri = Uri.parse("file://" + pathBuilder.toString());
|
||||||
File file = new File(uri.toString());
|
File file = new File(uri.toString());
|
||||||
file.getParentFile().mkdirs();
|
file.getParentFile().mkdirs();
|
||||||
return uri;
|
return uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
|
public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
|
||||||
try {
|
try {
|
||||||
Avatar avatar = new Avatar();
|
Avatar avatar = new Avatar();
|
||||||
Bitmap bm = cropCenterSquare(image, size);
|
Bitmap bm = cropCenterSquare(image, size);
|
||||||
if (bm==null) {
|
if (bm == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
Base64OutputStream mBase64OutputSttream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
|
Base64OutputStream mBase64OutputSttream = new Base64OutputStream(
|
||||||
|
mByteArrayOutputStream, Base64.DEFAULT);
|
||||||
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
||||||
DigestOutputStream mDigestOutputStream = new DigestOutputStream(mBase64OutputSttream, digest);
|
DigestOutputStream mDigestOutputStream = new DigestOutputStream(
|
||||||
|
mBase64OutputSttream, digest);
|
||||||
if (!bm.compress(format, 75, mDigestOutputStream)) {
|
if (!bm.compress(format, 75, mDigestOutputStream)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -309,25 +322,26 @@ public class FileBackend {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAvatarCached(Avatar avatar) {
|
public boolean isAvatarCached(Avatar avatar) {
|
||||||
File file = new File(getAvatarPath(context, avatar.getFilename()));
|
File file = new File(getAvatarPath(context, avatar.getFilename()));
|
||||||
return file.exists();
|
return file.exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean save(Avatar avatar) {
|
public boolean save(Avatar avatar) {
|
||||||
if (isAvatarCached(avatar)) {
|
if (isAvatarCached(avatar)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
String filename = getAvatarPath(context, avatar.getFilename());
|
String filename = getAvatarPath(context, avatar.getFilename());
|
||||||
File file = new File(filename+".tmp");
|
File file = new File(filename + ".tmp");
|
||||||
file.getParentFile().mkdirs();
|
file.getParentFile().mkdirs();
|
||||||
try {
|
try {
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
FileOutputStream mFileOutputStream = new FileOutputStream(file);
|
FileOutputStream mFileOutputStream = new FileOutputStream(file);
|
||||||
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
||||||
digest.reset();
|
digest.reset();
|
||||||
DigestOutputStream mDigestOutputStream = new DigestOutputStream(mFileOutputStream, digest);
|
DigestOutputStream mDigestOutputStream = new DigestOutputStream(
|
||||||
|
mFileOutputStream, digest);
|
||||||
mDigestOutputStream.write(avatar.getImageAsBytes());
|
mDigestOutputStream.write(avatar.getImageAsBytes());
|
||||||
mDigestOutputStream.flush();
|
mDigestOutputStream.flush();
|
||||||
mDigestOutputStream.close();
|
mDigestOutputStream.close();
|
||||||
@ -337,7 +351,7 @@ public class FileBackend {
|
|||||||
file.renameTo(new File(filename));
|
file.renameTo(new File(filename));
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
Log.d("xmppService","sha1sum mismatch for "+avatar.owner);
|
Log.d("xmppService", "sha1sum mismatch for " + avatar.owner);
|
||||||
file.delete();
|
file.delete();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -349,9 +363,9 @@ public class FileBackend {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getAvatarPath(Context context, String avatar) {
|
public static String getAvatarPath(Context context, String avatar) {
|
||||||
return context.getFilesDir().getAbsolutePath() + "/avatars/"+avatar;
|
return context.getFilesDir().getAbsolutePath() + "/avatars/" + avatar;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap cropCenterSquare(Uri image, int size) {
|
public Bitmap cropCenterSquare(Uri image, int size) {
|
||||||
@ -361,16 +375,20 @@ public class FileBackend {
|
|||||||
InputStream is = context.getContentResolver()
|
InputStream is = context.getContentResolver()
|
||||||
.openInputStream(image);
|
.openInputStream(image);
|
||||||
Bitmap input = BitmapFactory.decodeStream(is, null, options);
|
Bitmap input = BitmapFactory.decodeStream(is, null, options);
|
||||||
if (input==null) {
|
if (input == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
|
int rotation = getRotation(image);
|
||||||
|
if (rotation > 0) {
|
||||||
|
input = rotate(input, rotation);
|
||||||
|
}
|
||||||
return cropCenterSquare(input, size);
|
return cropCenterSquare(input, size);
|
||||||
}
|
}
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Bitmap cropCenterSquare(Bitmap input, int size) {
|
public static Bitmap cropCenterSquare(Bitmap input, int size) {
|
||||||
int w = input.getWidth();
|
int w = input.getWidth();
|
||||||
int h = input.getHeight();
|
int h = input.getHeight();
|
||||||
@ -381,8 +399,7 @@ public class FileBackend {
|
|||||||
float outHeight = scale * h;
|
float outHeight = scale * h;
|
||||||
float left = (size - outWidth) / 2;
|
float left = (size - outWidth) / 2;
|
||||||
float top = (size - outHeight) / 2;
|
float top = (size - outHeight) / 2;
|
||||||
RectF target = new RectF(left, top, left + outWidth, top
|
RectF target = new RectF(left, top, left + outWidth, top + outHeight);
|
||||||
+ outHeight);
|
|
||||||
|
|
||||||
Bitmap output = Bitmap.createBitmap(size, size, input.getConfig());
|
Bitmap output = Bitmap.createBitmap(size, size, input.getConfig());
|
||||||
Canvas canvas = new Canvas(output);
|
Canvas canvas = new Canvas(output);
|
||||||
@ -412,11 +429,11 @@ public class FileBackend {
|
|||||||
return inSampleSize;
|
return inSampleSize;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Uri getJingleFileUri(Message message) {
|
public Uri getJingleFileUri(Message message) {
|
||||||
File file = getJingleFile(message);
|
File file = getJingleFile(message);
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
return Uri.parse("file://"+file.getAbsolutePath());
|
return Uri.parse("file://" + file.getAbsolutePath());
|
||||||
} else {
|
} else {
|
||||||
return ImageProvider.getProviderUri(message);
|
return ImageProvider.getProviderUri(message);
|
||||||
}
|
}
|
||||||
@ -436,8 +453,9 @@ public class FileBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Bitmap getAvatar(String avatar, int size, Context context) {
|
public static Bitmap getAvatar(String avatar, int size, Context context) {
|
||||||
Bitmap bm = BitmapFactory.decodeFile(FileBackend.getAvatarPath(context, avatar));
|
Bitmap bm = BitmapFactory.decodeFile(FileBackend.getAvatarPath(context,
|
||||||
if (bm==null) {
|
avatar));
|
||||||
|
if (bm == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return cropCenterSquare(bm, UIHelper.getRealPx(size, context));
|
return cropCenterSquare(bm, UIHelper.getRealPx(size, context));
|
||||||
|
Loading…
Reference in New Issue
Block a user