2014-04-05 21:06:10 +02:00
|
|
|
package eu.siacs.conversations.persistance;
|
|
|
|
|
2014-04-19 01:14:30 +02:00
|
|
|
import java.io.File;
|
2014-05-13 16:48:39 +02:00
|
|
|
import java.io.FileInputStream;
|
2014-04-05 21:06:10 +02:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2014-06-30 12:01:43 +02:00
|
|
|
import android.graphics.Matrix;
|
|
|
|
import android.media.ExifInterface;
|
2014-04-05 21:06:10 +02:00
|
|
|
import android.net.Uri;
|
2014-04-19 01:14:30 +02:00
|
|
|
import android.util.Log;
|
2014-04-07 20:05:45 +02:00
|
|
|
import android.util.LruCache;
|
2014-05-14 18:32:58 +02:00
|
|
|
import eu.siacs.conversations.R;
|
2014-04-05 21:06:10 +02:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2014-04-06 15:34:08 +02:00
|
|
|
import eu.siacs.conversations.entities.Message;
|
2014-04-13 11:32:45 +02:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.JingleFile;
|
2014-04-05 21:06:10 +02:00
|
|
|
|
|
|
|
public class FileBackend {
|
2014-04-07 20:05:45 +02:00
|
|
|
|
2014-04-05 21:06:10 +02:00
|
|
|
private static int IMAGE_SIZE = 1920;
|
2014-04-07 20:05:45 +02:00
|
|
|
|
2014-04-05 21:06:10 +02:00
|
|
|
private Context context;
|
2014-04-07 20:05:45 +02:00
|
|
|
private LruCache<String, Bitmap> thumbnailCache;
|
|
|
|
|
2014-04-05 21:06:10 +02:00
|
|
|
public FileBackend(Context context) {
|
|
|
|
this.context = context;
|
2014-04-07 20:05:45 +02: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 21:06:10 +02:00
|
|
|
}
|
2014-05-14 18:32:58 +02:00
|
|
|
|
2014-04-25 16:24:56 +02:00
|
|
|
public LruCache<String, Bitmap> getThumbnailCache() {
|
|
|
|
return thumbnailCache;
|
|
|
|
}
|
2014-05-14 18:32:58 +02:00
|
|
|
|
2014-04-13 18:09:40 +02:00
|
|
|
public JingleFile getJingleFile(Message message) {
|
2014-05-06 21:34:30 +02:00
|
|
|
return getJingleFile(message, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public JingleFile getJingleFile(Message message, boolean decrypted) {
|
2014-04-06 15:34:08 +02:00
|
|
|
Conversation conversation = message.getConversation();
|
2014-04-07 20:05:45 +02:00
|
|
|
String prefix = context.getFilesDir().getAbsolutePath();
|
|
|
|
String path = prefix + "/" + conversation.getAccount().getJid() + "/"
|
|
|
|
+ conversation.getContactJid();
|
2014-05-06 21:34:30 +02:00
|
|
|
String filename;
|
2014-05-14 18:32:58 +02:00
|
|
|
if ((decrypted) || (message.getEncryption() == Message.ENCRYPTION_NONE)) {
|
2014-05-06 21:34:30 +02:00
|
|
|
filename = message.getUuid() + ".webp";
|
|
|
|
} else {
|
2014-06-20 17:30:19 +02:00
|
|
|
if (message.getEncryption() == Message.ENCRYPTION_OTR) {
|
|
|
|
filename = message.getUuid() + ".webp";
|
|
|
|
} else {
|
|
|
|
filename = message.getUuid() + ".webp.pgp";
|
|
|
|
}
|
2014-05-06 21:34:30 +02:00
|
|
|
}
|
2014-04-13 11:32:45 +02:00
|
|
|
return new JingleFile(path + "/" + filename);
|
2014-04-06 15:34:08 +02:00
|
|
|
}
|
2014-04-19 01:14:30 +02:00
|
|
|
|
2014-04-25 16:24:56 +02:00
|
|
|
public Bitmap resize(Bitmap originalBitmap, int size) {
|
2014-04-07 20:05:45 +02:00
|
|
|
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));
|
|
|
|
}
|
2014-04-19 01:14:30 +02:00
|
|
|
Bitmap scalledBitmap = Bitmap.createScaledBitmap(originalBitmap,
|
|
|
|
scalledW, scalledH, true);
|
2014-04-07 20:05:45 +02:00
|
|
|
return scalledBitmap;
|
|
|
|
} else {
|
|
|
|
return originalBitmap;
|
|
|
|
}
|
|
|
|
}
|
2014-06-30 12:01:43 +02:00
|
|
|
|
|
|
|
public Bitmap rotate(Bitmap bitmap, int degree) {
|
|
|
|
int w = bitmap.getWidth();
|
|
|
|
int h = bitmap.getHeight();
|
|
|
|
Matrix mtx = new Matrix();
|
|
|
|
mtx.postRotate(degree);
|
|
|
|
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
|
2014-05-21 00:39:45 +02:00
|
|
|
}
|
2014-04-07 20:05:45 +02:00
|
|
|
|
2014-06-30 12:01:43 +02:00
|
|
|
public JingleFile copyImageToPrivateStorage(Message message, Uri image)
|
2014-05-14 18:32:58 +02:00
|
|
|
throws ImageCopyException {
|
2014-06-30 12:01:43 +02:00
|
|
|
return this.copyImageToPrivateStorage(message, image, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private JingleFile copyImageToPrivateStorage(Message message, Uri image,
|
|
|
|
int sampleSize) throws ImageCopyException {
|
2014-04-05 21:06:10 +02:00
|
|
|
try {
|
2014-05-13 16:48:39 +02:00
|
|
|
InputStream is;
|
2014-05-14 18:32:58 +02:00
|
|
|
if (image != null) {
|
|
|
|
is = context.getContentResolver().openInputStream(image);
|
2014-05-13 16:48:39 +02:00
|
|
|
} else {
|
|
|
|
is = new FileInputStream(getIncomingFile());
|
2014-06-30 12:01:43 +02:00
|
|
|
image = getIncomingUri();
|
2014-05-13 16:48:39 +02:00
|
|
|
}
|
2014-04-13 18:09:40 +02:00
|
|
|
JingleFile file = getJingleFile(message);
|
2014-04-05 21:06:10 +02:00
|
|
|
file.getParentFile().mkdirs();
|
|
|
|
file.createNewFile();
|
2014-05-20 22:52:57 +02:00
|
|
|
Bitmap originalBitmap;
|
2014-05-21 00:39:45 +02:00
|
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
|
|
int inSampleSize = (int) Math.pow(2, sampleSize);
|
2014-06-30 12:01:43 +02:00
|
|
|
Log.d("xmppService", "reading bitmap with sample size "
|
|
|
|
+ inSampleSize);
|
2014-05-21 00:39:45 +02:00
|
|
|
options.inSampleSize = inSampleSize;
|
|
|
|
originalBitmap = BitmapFactory.decodeStream(is, null, options);
|
|
|
|
is.close();
|
2014-05-14 18:32:58 +02:00
|
|
|
if (originalBitmap == null) {
|
|
|
|
throw new ImageCopyException(R.string.error_not_an_image_file);
|
|
|
|
}
|
|
|
|
if (image == null) {
|
2014-05-13 16:48:39 +02:00
|
|
|
getIncomingFile().delete();
|
|
|
|
}
|
2014-04-07 20:05:45 +02:00
|
|
|
Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE);
|
2014-06-30 12:01:43 +02:00
|
|
|
originalBitmap = null;
|
|
|
|
ExifInterface exif = new ExifInterface(image.toString());
|
|
|
|
if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
|
|
|
|
.equalsIgnoreCase("6")) {
|
|
|
|
scalledBitmap = rotate(scalledBitmap, 90);
|
|
|
|
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
|
|
|
|
.equalsIgnoreCase("8")) {
|
|
|
|
scalledBitmap = rotate(scalledBitmap, 270);
|
|
|
|
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
|
|
|
|
.equalsIgnoreCase("3")) {
|
|
|
|
scalledBitmap = rotate(scalledBitmap, 180);
|
|
|
|
}
|
2014-05-20 22:52:57 +02:00
|
|
|
OutputStream os = new FileOutputStream(file);
|
2014-04-19 01:14:30 +02:00
|
|
|
boolean success = scalledBitmap.compress(
|
|
|
|
Bitmap.CompressFormat.WEBP, 75, os);
|
2014-04-05 21:06:10 +02:00
|
|
|
if (!success) {
|
2014-05-14 18:32:58 +02:00
|
|
|
throw new ImageCopyException(R.string.error_compressing_image);
|
2014-04-05 21:06:10 +02:00
|
|
|
}
|
2014-04-25 16:24:56 +02:00
|
|
|
os.flush();
|
2014-04-05 21:06:10 +02:00
|
|
|
os.close();
|
2014-04-25 16:24:56 +02:00
|
|
|
long size = file.getSize();
|
|
|
|
int width = scalledBitmap.getWidth();
|
|
|
|
int height = scalledBitmap.getHeight();
|
2014-05-14 18:32:58 +02:00
|
|
|
message.setBody("" + size + "," + width + "," + height);
|
2014-04-05 21:06:10 +02:00
|
|
|
return file;
|
|
|
|
} catch (FileNotFoundException e) {
|
2014-05-14 18:32:58 +02:00
|
|
|
throw new ImageCopyException(R.string.error_file_not_found);
|
2014-04-05 21:06:10 +02:00
|
|
|
} catch (IOException e) {
|
2014-05-14 18:32:58 +02:00
|
|
|
throw new ImageCopyException(R.string.error_io_exception);
|
2014-05-12 14:59:46 +02:00
|
|
|
} catch (SecurityException e) {
|
2014-05-14 18:32:58 +02:00
|
|
|
throw new ImageCopyException(
|
|
|
|
R.string.error_security_exception_during_image_copy);
|
2014-05-21 00:39:45 +02:00
|
|
|
} catch (OutOfMemoryError e) {
|
2014-05-21 20:27:53 +02:00
|
|
|
++sampleSize;
|
2014-06-30 12:01:43 +02:00
|
|
|
if (sampleSize <= 3) {
|
2014-05-21 20:27:53 +02:00
|
|
|
return copyImageToPrivateStorage(message, image, sampleSize);
|
|
|
|
} else {
|
|
|
|
throw new ImageCopyException(R.string.error_out_of_memory);
|
|
|
|
}
|
2014-04-05 21:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-07 20:05:45 +02:00
|
|
|
|
2014-04-06 15:34:08 +02:00
|
|
|
public Bitmap getImageFromMessage(Message message) {
|
2014-04-19 01:14:30 +02:00
|
|
|
return BitmapFactory.decodeFile(getJingleFile(message)
|
|
|
|
.getAbsolutePath());
|
2014-04-07 20:05:45 +02:00
|
|
|
}
|
|
|
|
|
2014-04-25 23:06:20 +02:00
|
|
|
public Bitmap getThumbnail(Message message, int size, boolean cacheOnly)
|
2014-04-19 01:14:30 +02:00
|
|
|
throws FileNotFoundException {
|
2014-04-07 20:05:45 +02:00
|
|
|
Bitmap thumbnail = thumbnailCache.get(message.getUuid());
|
2014-05-14 18:32:58 +02:00
|
|
|
if ((thumbnail == null) && (!cacheOnly)) {
|
2014-04-13 18:09:40 +02:00
|
|
|
Bitmap fullsize = BitmapFactory.decodeFile(getJingleFile(message)
|
2014-04-07 20:05:45 +02:00
|
|
|
.getAbsolutePath());
|
2014-04-19 01:14:30 +02:00
|
|
|
if (fullsize == null) {
|
2014-04-18 11:57:28 +02:00
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
2014-04-07 20:05:45 +02:00
|
|
|
thumbnail = resize(fullsize, size);
|
|
|
|
this.thumbnailCache.put(message.getUuid(), thumbnail);
|
|
|
|
}
|
|
|
|
return thumbnail;
|
2014-04-06 15:34:08 +02:00
|
|
|
}
|
2014-04-19 01:14:30 +02:00
|
|
|
|
|
|
|
public void removeFiles(Conversation conversation) {
|
|
|
|
String prefix = context.getFilesDir().getAbsolutePath();
|
|
|
|
String path = prefix + "/" + conversation.getAccount().getJid() + "/"
|
|
|
|
+ conversation.getContactJid();
|
|
|
|
File file = new File(path);
|
|
|
|
try {
|
|
|
|
this.deleteFile(file);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.d("xmppService",
|
|
|
|
"error deleting file: " + file.getAbsolutePath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void deleteFile(File f) throws IOException {
|
|
|
|
if (f.isDirectory()) {
|
|
|
|
for (File c : f.listFiles())
|
|
|
|
deleteFile(c);
|
|
|
|
}
|
|
|
|
f.delete();
|
|
|
|
}
|
2014-05-13 16:48:39 +02:00
|
|
|
|
|
|
|
public File getIncomingFile() {
|
2014-05-14 18:32:58 +02:00
|
|
|
return new File(context.getFilesDir().getAbsolutePath() + "/incoming");
|
|
|
|
}
|
2014-06-30 12:01:43 +02:00
|
|
|
|
|
|
|
public Uri getIncomingUri() {
|
|
|
|
return Uri.parse(context.getFilesDir().getAbsolutePath() + "/incoming");
|
|
|
|
}
|
2014-05-14 18:32:58 +02:00
|
|
|
|
|
|
|
public class ImageCopyException extends Exception {
|
|
|
|
private static final long serialVersionUID = -1010013599132881427L;
|
|
|
|
private int resId;
|
|
|
|
|
|
|
|
public ImageCopyException(int resId) {
|
|
|
|
this.resId = resId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getResId() {
|
|
|
|
return resId;
|
|
|
|
}
|
2014-05-13 16:48:39 +02:00
|
|
|
}
|
2014-04-05 21:06:10 +02:00
|
|
|
}
|