2014-04-05 19:06:10 +00:00
|
|
|
package eu.siacs.conversations.persistance;
|
|
|
|
|
2014-04-12 08:02:48 +00:00
|
|
|
import java.io.BufferedInputStream;
|
2014-04-05 19:06:10 +00:00
|
|
|
import java.io.File;
|
2014-04-12 08:02:48 +00:00
|
|
|
import java.io.FileInputStream;
|
2014-04-05 19:06:10 +00:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2014-04-12 08:02:48 +00:00
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2014-04-05 19:06:10 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Log;
|
2014-04-07 18:05:45 +00:00
|
|
|
import android.util.LruCache;
|
2014-04-05 19:06:10 +00:00
|
|
|
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2014-04-06 13:34:08 +00:00
|
|
|
import eu.siacs.conversations.entities.Message;
|
2014-04-12 08:02:48 +00:00
|
|
|
import eu.siacs.conversations.utils.CryptoHelper;
|
2014-04-05 19:06:10 +00:00
|
|
|
|
|
|
|
public class FileBackend {
|
2014-04-07 18:05:45 +00:00
|
|
|
|
2014-04-05 19:06:10 +00:00
|
|
|
private static int IMAGE_SIZE = 1920;
|
2014-04-07 18:05:45 +00:00
|
|
|
|
2014-04-05 19:06:10 +00:00
|
|
|
private Context context;
|
2014-04-07 18:05:45 +00:00
|
|
|
private LruCache<String, Bitmap> thumbnailCache;
|
|
|
|
|
2014-04-05 19:06:10 +00:00
|
|
|
public FileBackend(Context context) {
|
|
|
|
this.context = context;
|
2014-04-07 18:05:45 +00:00
|
|
|
|
|
|
|
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
|
|
|
|
int cacheSize = maxMemory / 8;
|
|
|
|
thumbnailCache = new LruCache<String, Bitmap>(cacheSize) {
|
|
|
|
@Override
|
|
|
|
protected int sizeOf(String key, Bitmap bitmap) {
|
|
|
|
return bitmap.getByteCount() / 1024;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-05 19:06:10 +00:00
|
|
|
}
|
2014-04-07 18:05:45 +00:00
|
|
|
|
|
|
|
public File getImageFile(Message message) {
|
2014-04-06 13:34:08 +00:00
|
|
|
Conversation conversation = message.getConversation();
|
2014-04-07 18:05:45 +00:00
|
|
|
String prefix = context.getFilesDir().getAbsolutePath();
|
|
|
|
String path = prefix + "/" + conversation.getAccount().getJid() + "/"
|
|
|
|
+ conversation.getContactJid();
|
2014-04-06 13:34:08 +00:00
|
|
|
String filename = message.getUuid() + ".webp";
|
2014-04-07 18:05:45 +00:00
|
|
|
return new File(path + "/" + filename);
|
2014-04-06 13:34:08 +00:00
|
|
|
}
|
2014-04-05 19:06:10 +00:00
|
|
|
|
2014-04-07 18:05:45 +00:00
|
|
|
private Bitmap resize(Bitmap originalBitmap, int size) {
|
|
|
|
int w = originalBitmap.getWidth();
|
|
|
|
int h = originalBitmap.getHeight();
|
|
|
|
if (Math.max(w, h) > size) {
|
|
|
|
int scalledW;
|
|
|
|
int scalledH;
|
|
|
|
if (w <= h) {
|
|
|
|
scalledW = (int) (w / ((double) h / size));
|
|
|
|
scalledH = size;
|
|
|
|
} else {
|
|
|
|
scalledW = size;
|
|
|
|
scalledH = (int) (h / ((double) w / size));
|
|
|
|
}
|
|
|
|
Bitmap scalledBitmap = Bitmap.createScaledBitmap(
|
|
|
|
originalBitmap, scalledW, scalledH, true);
|
|
|
|
return scalledBitmap;
|
|
|
|
} else {
|
|
|
|
return originalBitmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-06 13:34:08 +00:00
|
|
|
public File copyImageToPrivateStorage(Message message, Uri image) {
|
2014-04-05 19:06:10 +00:00
|
|
|
try {
|
2014-04-07 18:05:45 +00:00
|
|
|
InputStream is = context.getContentResolver()
|
|
|
|
.openInputStream(image);
|
2014-04-06 13:34:08 +00:00
|
|
|
File file = getImageFile(message);
|
2014-04-05 19:06:10 +00:00
|
|
|
file.getParentFile().mkdirs();
|
|
|
|
file.createNewFile();
|
|
|
|
OutputStream os = new FileOutputStream(file);
|
|
|
|
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
|
|
|
|
is.close();
|
2014-04-07 18:05:45 +00:00
|
|
|
Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE);
|
|
|
|
boolean success = scalledBitmap.compress(Bitmap.CompressFormat.WEBP,75,os);
|
2014-04-05 19:06:10 +00:00
|
|
|
if (!success) {
|
2014-04-07 18:05:45 +00:00
|
|
|
Log.d("xmppService", "couldnt compress");
|
2014-04-05 19:06:10 +00:00
|
|
|
}
|
|
|
|
os.close();
|
2014-04-12 08:02:48 +00:00
|
|
|
message.setBody(this.createSha1(file));
|
2014-04-05 19:06:10 +00:00
|
|
|
return file;
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2014-04-07 18:05:45 +00:00
|
|
|
|
2014-04-05 19:06:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
2014-04-07 18:05:45 +00:00
|
|
|
|
2014-04-06 13:34:08 +00:00
|
|
|
public Bitmap getImageFromMessage(Message message) {
|
2014-04-07 18:05:45 +00:00
|
|
|
return BitmapFactory
|
|
|
|
.decodeFile(getImageFile(message).getAbsolutePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bitmap getThumbnailFromMessage(Message message, int size) {
|
|
|
|
Bitmap thumbnail = thumbnailCache.get(message.getUuid());
|
|
|
|
if (thumbnail==null) {
|
|
|
|
Log.d("xmppService","creating new thumbnail" + message.getUuid());
|
|
|
|
Bitmap fullsize = BitmapFactory.decodeFile(getImageFile(message)
|
|
|
|
.getAbsolutePath());
|
|
|
|
thumbnail = resize(fullsize, size);
|
|
|
|
this.thumbnailCache.put(message.getUuid(), thumbnail);
|
|
|
|
}
|
|
|
|
return thumbnail;
|
2014-04-06 13:34:08 +00:00
|
|
|
}
|
2014-04-12 08:02:48 +00:00
|
|
|
|
|
|
|
private String createSha1(final File file) {
|
|
|
|
InputStream fis = null;
|
|
|
|
try {
|
|
|
|
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
|
|
|
fis = new FileInputStream(file);
|
|
|
|
int n = 0;
|
|
|
|
byte[] buffer = new byte[8192];
|
|
|
|
while (n != -1) {
|
|
|
|
n = fis.read(buffer);
|
|
|
|
if (n > 0) {
|
|
|
|
digest.update(buffer, 0, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fis.close();
|
|
|
|
return CryptoHelper.bytesToHex(digest.digest());
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
return null;
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
if (fis!=null) {
|
|
|
|
try {
|
|
|
|
fis.close();
|
|
|
|
return null;
|
|
|
|
} catch (IOException e1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
if (fis!=null) {
|
|
|
|
try {
|
|
|
|
fis.close();
|
|
|
|
return null;
|
|
|
|
} catch (IOException e1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-04-05 19:06:10 +00:00
|
|
|
}
|