Conversations/src/eu/siacs/conversations/persistance/FileBackend.java

178 lines
5.1 KiB
Java
Raw Normal View History

2014-04-05 15:06:10 -04:00
package eu.siacs.conversations.persistance;
2014-04-18 19:14:30 -04:00
import java.io.File;
2014-05-13 10:48:39 -04:00
import java.io.FileInputStream;
2014-04-05 15:06:10 -04:00
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
2014-04-25 10:24:56 -04:00
import java.lang.ref.WeakReference;
2014-04-05 15:06:10 -04:00
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
2014-04-18 19:14:30 -04:00
import android.util.Log;
import android.util.LruCache;
2014-04-25 10:24:56 -04:00
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import eu.siacs.conversations.entities.Account;
2014-04-05 15:06:10 -04:00
import eu.siacs.conversations.entities.Conversation;
2014-04-06 09:34:08 -04:00
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xmpp.jingle.JingleFile;
2014-04-05 15:06:10 -04:00
public class FileBackend {
2014-04-05 15:06:10 -04:00
private static int IMAGE_SIZE = 1920;
2014-04-05 15:06:10 -04:00
private Context context;
private LruCache<String, Bitmap> thumbnailCache;
2014-04-05 15:06:10 -04:00
public FileBackend(Context context) {
this.context = context;
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 15:06:10 -04:00
}
2014-04-25 10:24:56 -04:00
public LruCache<String, Bitmap> getThumbnailCache() {
return thumbnailCache;
}
2014-05-06 15:34:30 -04:00
public JingleFile getJingleFile(Message message) {
2014-05-06 15:34:30 -04:00
return getJingleFile(message, true);
}
public JingleFile getJingleFile(Message message, boolean decrypted) {
2014-04-06 09:34:08 -04:00
Conversation conversation = message.getConversation();
String prefix = context.getFilesDir().getAbsolutePath();
String path = prefix + "/" + conversation.getAccount().getJid() + "/"
+ conversation.getContactJid();
2014-05-06 15:34:30 -04:00
String filename;
if ((decrypted)||(message.getEncryption() == Message.ENCRYPTION_NONE)) {
filename = message.getUuid() + ".webp";
} else {
filename = message.getUuid() + ".webp.pgp";
}
return new JingleFile(path + "/" + filename);
2014-04-06 09:34:08 -04:00
}
2014-04-18 19:14:30 -04:00
2014-04-25 10:24:56 -04:00
public 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));
}
2014-04-18 19:14:30 -04:00
Bitmap scalledBitmap = Bitmap.createScaledBitmap(originalBitmap,
scalledW, scalledH, true);
return scalledBitmap;
} else {
return originalBitmap;
}
}
public JingleFile copyImageToPrivateStorage(Message message, Uri image) {
2014-04-05 15:06:10 -04:00
try {
2014-05-13 10:48:39 -04:00
InputStream is;
if (image!=null) {
Log.d("xmppService","copying file: "+image.toString()+ " to internal storage");
is = context.getContentResolver()
.openInputStream(image);
2014-05-13 10:48:39 -04:00
} else {
Log.d("xmppService","copying file from incoming to internal storage");
is = new FileInputStream(getIncomingFile());
}
JingleFile file = getJingleFile(message);
2014-04-05 15:06:10 -04:00
file.getParentFile().mkdirs();
file.createNewFile();
OutputStream os = new FileOutputStream(file);
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
is.close();
2014-05-13 10:48:39 -04:00
if (image==null) {
Log.d("xmppService","delete incoming file");
getIncomingFile().delete();
}
Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE);
2014-04-18 19:14:30 -04:00
boolean success = scalledBitmap.compress(
Bitmap.CompressFormat.WEBP, 75, os);
2014-04-05 15:06:10 -04:00
if (!success) {
return null;
2014-04-05 15:06:10 -04:00
}
2014-04-25 10:24:56 -04:00
os.flush();
2014-04-05 15:06:10 -04:00
os.close();
2014-04-25 10:24:56 -04:00
long size = file.getSize();
int width = scalledBitmap.getWidth();
int height = scalledBitmap.getHeight();
message.setBody(""+size+","+width+","+height);
2014-04-05 15:06:10 -04:00
return file;
} catch (FileNotFoundException e) {
return null;
2014-04-05 15:06:10 -04:00
} catch (IOException e) {
return null;
} catch (SecurityException e) {
return null;
2014-04-05 15:06:10 -04:00
}
}
2014-04-06 09:34:08 -04:00
public Bitmap getImageFromMessage(Message message) {
2014-04-18 19:14:30 -04:00
return BitmapFactory.decodeFile(getJingleFile(message)
.getAbsolutePath());
}
public Bitmap getThumbnail(Message message, int size, boolean cacheOnly)
2014-04-18 19:14:30 -04:00
throws FileNotFoundException {
Bitmap thumbnail = thumbnailCache.get(message.getUuid());
if ((thumbnail == null)&&(!cacheOnly)) {
Bitmap fullsize = BitmapFactory.decodeFile(getJingleFile(message)
.getAbsolutePath());
2014-04-18 19:14:30 -04:00
if (fullsize == null) {
throw new FileNotFoundException();
}
thumbnail = resize(fullsize, size);
this.thumbnailCache.put(message.getUuid(), thumbnail);
}
return thumbnail;
2014-04-06 09:34:08 -04:00
}
2014-04-18 19:14:30 -04: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 10:48:39 -04:00
public File getIncomingFile() {
return new File(context.getFilesDir().getAbsolutePath()+"/incoming");
}
2014-04-05 15:06:10 -04:00
}