diff options
author | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-04-07 20:05:45 +0200 |
---|---|---|
committer | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-04-07 20:05:45 +0200 |
commit | 3f403fb8a976f6cc7d135cf1eb6dd6f0789c312a (patch) | |
tree | 69fb1e80e9380b337b775910798870b04c5e4037 /src/eu/siacs/conversations/persistance/FileBackend.java | |
parent | c30bf75a5d77fab21480e04ce18b46d6b2826295 (diff) |
jingle connection and manager. able to trigger dialog in gajim
Diffstat (limited to 'src/eu/siacs/conversations/persistance/FileBackend.java')
-rw-r--r-- | src/eu/siacs/conversations/persistance/FileBackend.java | 97 |
1 files changed, 63 insertions, 34 deletions
diff --git a/src/eu/siacs/conversations/persistance/FileBackend.java b/src/eu/siacs/conversations/persistance/FileBackend.java index 9ae40645..122898b9 100644 --- a/src/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/eu/siacs/conversations/persistance/FileBackend.java @@ -6,65 +6,82 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.math.BigInteger; 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; import eu.siacs.conversations.entities.Message; - public class FileBackend { - + private static int IMAGE_SIZE = 1920; - + private Context context; - + private LruCache<String, Bitmap> thumbnailCache; + 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; + } + }; + } - - private File getImageFile(Message message) { + + public File getImageFile(Message message) { Conversation conversation = message.getConversation(); - String prefix = context.getFilesDir().getAbsolutePath(); - String path = prefix+"/"+conversation.getAccount().getJid()+"/"+conversation.getContactJid(); + String prefix = context.getFilesDir().getAbsolutePath(); + String path = prefix + "/" + conversation.getAccount().getJid() + "/" + + conversation.getContactJid(); String filename = message.getUuid() + ".webp"; - return new File(path+"/"+filename); + return new File(path + "/" + filename); } + 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; + } + } + public File copyImageToPrivateStorage(Message message, Uri image) { try { - InputStream is = context.getContentResolver().openInputStream(image); + InputStream is = context.getContentResolver() + .openInputStream(image); File file = getImageFile(message); file.getParentFile().mkdirs(); file.createNewFile(); OutputStream os = new FileOutputStream(file); Bitmap originalBitmap = BitmapFactory.decodeStream(is); is.close(); - int w = originalBitmap.getWidth(); - int h = originalBitmap.getHeight(); - boolean success; - if (Math.max(w, h) > IMAGE_SIZE) { - int scalledW; - int scalledH; - if (w<=h) { - scalledW = (int) (w / ((double) h/IMAGE_SIZE)); - scalledH = IMAGE_SIZE; - } else { - scalledW = IMAGE_SIZE; - scalledH = (int) (h / ((double) w/IMAGE_SIZE)); - } - Bitmap scalledBitmap = Bitmap.createScaledBitmap(originalBitmap, scalledW,scalledH, true); - success = scalledBitmap.compress(Bitmap.CompressFormat.WEBP, 75, os); - } else { - success = originalBitmap.compress(Bitmap.CompressFormat.WEBP, 75, os); - } + Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE); + 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; @@ -75,12 +92,24 @@ public class FileBackend { // TODO Auto-generated catch block e.printStackTrace(); } - + return null; } - - + public Bitmap getImageFromMessage(Message message) { - return BitmapFactory.decodeFile(getImageFile(message).getAbsolutePath()); + 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; } } |