aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/persistance/FileBackend.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/eu/siacs/conversations/persistance/FileBackend.java')
-rw-r--r--src/eu/siacs/conversations/persistance/FileBackend.java47
1 files changed, 36 insertions, 11 deletions
diff --git a/src/eu/siacs/conversations/persistance/FileBackend.java b/src/eu/siacs/conversations/persistance/FileBackend.java
index 7433946b..307a9e2c 100644
--- a/src/eu/siacs/conversations/persistance/FileBackend.java
+++ b/src/eu/siacs/conversations/persistance/FileBackend.java
@@ -1,5 +1,6 @@
package eu.siacs.conversations.persistance;
+import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -10,6 +11,7 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
+import android.util.Log;
import android.util.LruCache;
import eu.siacs.conversations.entities.Conversation;
@@ -25,7 +27,7 @@ public class FileBackend {
public FileBackend(Context context) {
this.context = context;
-
+
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
int cacheSize = maxMemory / 8;
thumbnailCache = new LruCache<String, Bitmap>(cacheSize) {
@@ -45,7 +47,7 @@ public class FileBackend {
String filename = message.getUuid() + ".webp";
return new JingleFile(path + "/" + filename);
}
-
+
private Bitmap resize(Bitmap originalBitmap, int size) {
int w = originalBitmap.getWidth();
int h = originalBitmap.getHeight();
@@ -59,8 +61,8 @@ public class FileBackend {
scalledW = size;
scalledH = (int) (h / ((double) w / size));
}
- Bitmap scalledBitmap = Bitmap.createScaledBitmap(
- originalBitmap, scalledW, scalledH, true);
+ Bitmap scalledBitmap = Bitmap.createScaledBitmap(originalBitmap,
+ scalledW, scalledH, true);
return scalledBitmap;
} else {
return originalBitmap;
@@ -78,9 +80,10 @@ public class FileBackend {
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
is.close();
Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE);
- boolean success = scalledBitmap.compress(Bitmap.CompressFormat.WEBP,75,os);
+ boolean success = scalledBitmap.compress(
+ Bitmap.CompressFormat.WEBP, 75, os);
if (!success) {
- //Log.d("xmppService", "couldnt compress");
+ // Log.d("xmppService", "couldnt compress");
}
os.close();
return file;
@@ -96,16 +99,17 @@ public class FileBackend {
}
public Bitmap getImageFromMessage(Message message) {
- return BitmapFactory
- .decodeFile(getJingleFile(message).getAbsolutePath());
+ return BitmapFactory.decodeFile(getJingleFile(message)
+ .getAbsolutePath());
}
- public Bitmap getThumbnailFromMessage(Message message, int size) throws FileNotFoundException {
+ public Bitmap getThumbnailFromMessage(Message message, int size)
+ throws FileNotFoundException {
Bitmap thumbnail = thumbnailCache.get(message.getUuid());
- if (thumbnail==null) {
+ if (thumbnail == null) {
Bitmap fullsize = BitmapFactory.decodeFile(getJingleFile(message)
.getAbsolutePath());
- if (fullsize==null) {
+ if (fullsize == null) {
throw new FileNotFoundException();
}
thumbnail = resize(fullsize, size);
@@ -113,4 +117,25 @@ public class FileBackend {
}
return thumbnail;
}
+
+ 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();
+ }
}