1package eu.siacs.conversations.persistance;
2
3import java.io.FileNotFoundException;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.OutputStream;
8
9import android.content.Context;
10import android.graphics.Bitmap;
11import android.graphics.BitmapFactory;
12import android.net.Uri;
13import android.util.LruCache;
14
15import eu.siacs.conversations.entities.Conversation;
16import eu.siacs.conversations.entities.Message;
17import eu.siacs.conversations.xmpp.jingle.JingleFile;
18
19public class FileBackend {
20
21 private static int IMAGE_SIZE = 1920;
22
23 private Context context;
24 private LruCache<String, Bitmap> thumbnailCache;
25
26 public FileBackend(Context context) {
27 this.context = context;
28
29 int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
30 int cacheSize = maxMemory / 8;
31 thumbnailCache = new LruCache<String, Bitmap>(cacheSize) {
32 @Override
33 protected int sizeOf(String key, Bitmap bitmap) {
34 return bitmap.getByteCount() / 1024;
35 }
36 };
37
38 }
39
40 public JingleFile getJingleFile(Message message) {
41 Conversation conversation = message.getConversation();
42 String prefix = context.getFilesDir().getAbsolutePath();
43 String path = prefix + "/" + conversation.getAccount().getJid() + "/"
44 + conversation.getContactJid();
45 String filename = message.getUuid() + ".webp";
46 return new JingleFile(path + "/" + filename);
47 }
48
49 private Bitmap resize(Bitmap originalBitmap, int size) {
50 int w = originalBitmap.getWidth();
51 int h = originalBitmap.getHeight();
52 if (Math.max(w, h) > size) {
53 int scalledW;
54 int scalledH;
55 if (w <= h) {
56 scalledW = (int) (w / ((double) h / size));
57 scalledH = size;
58 } else {
59 scalledW = size;
60 scalledH = (int) (h / ((double) w / size));
61 }
62 Bitmap scalledBitmap = Bitmap.createScaledBitmap(
63 originalBitmap, scalledW, scalledH, true);
64 return scalledBitmap;
65 } else {
66 return originalBitmap;
67 }
68 }
69
70 public JingleFile copyImageToPrivateStorage(Message message, Uri image) {
71 try {
72 InputStream is = context.getContentResolver()
73 .openInputStream(image);
74 JingleFile file = getJingleFile(message);
75 file.getParentFile().mkdirs();
76 file.createNewFile();
77 OutputStream os = new FileOutputStream(file);
78 Bitmap originalBitmap = BitmapFactory.decodeStream(is);
79 is.close();
80 Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE);
81 boolean success = scalledBitmap.compress(Bitmap.CompressFormat.WEBP,75,os);
82 if (!success) {
83 //Log.d("xmppService", "couldnt compress");
84 }
85 os.close();
86 return file;
87 } catch (FileNotFoundException e) {
88 // TODO Auto-generated catch block
89 e.printStackTrace();
90 } catch (IOException e) {
91 // TODO Auto-generated catch block
92 e.printStackTrace();
93 }
94
95 return null;
96 }
97
98 public Bitmap getImageFromMessage(Message message) {
99 return BitmapFactory
100 .decodeFile(getJingleFile(message).getAbsolutePath());
101 }
102
103 public Bitmap getThumbnailFromMessage(Message message, int size) throws FileNotFoundException {
104 Bitmap thumbnail = thumbnailCache.get(message.getUuid());
105 if (thumbnail==null) {
106 Bitmap fullsize = BitmapFactory.decodeFile(getJingleFile(message)
107 .getAbsolutePath());
108 if (fullsize==null) {
109 throw new FileNotFoundException();
110 }
111 thumbnail = resize(fullsize, size);
112 this.thumbnailCache.put(message.getUuid(), thumbnail);
113 }
114 return thumbnail;
115 }
116}