1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
package eu.siacs.conversations.persistance;
import java.io.File;
import java.io.FileInputStream;
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;
import android.net.Uri;
import android.util.Log;
import android.util.LruCache;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.xmpp.jingle.JingleFile;
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;
}
};
}
public LruCache<String, Bitmap> getThumbnailCache() {
return thumbnailCache;
}
public JingleFile getJingleFile(Message message) {
return getJingleFile(message, true);
}
public JingleFile getJingleFile(Message message, boolean decrypted) {
Conversation conversation = message.getConversation();
String prefix = context.getFilesDir().getAbsolutePath();
String path = prefix + "/" + conversation.getAccount().getJid() + "/"
+ conversation.getContactJid();
String filename;
if ((decrypted) || (message.getEncryption() == Message.ENCRYPTION_NONE)) {
filename = message.getUuid() + ".webp";
} else {
filename = message.getUuid() + ".webp.pgp";
}
return new JingleFile(path + "/" + filename);
}
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));
}
Bitmap scalledBitmap = Bitmap.createScaledBitmap(originalBitmap,
scalledW, scalledH, true);
return scalledBitmap;
} else {
return originalBitmap;
}
}
public JingleFile copyImageToPrivateStorage(Message message, Uri image)
throws ImageCopyException {
try {
InputStream is;
if (image != null) {
is = context.getContentResolver().openInputStream(image);
} else {
is = new FileInputStream(getIncomingFile());
}
JingleFile file = getJingleFile(message);
file.getParentFile().mkdirs();
file.createNewFile();
Bitmap originalBitmap;
try {
originalBitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (OutOfMemoryError e) {
is.close();
Log.d("xmppService","catched out of memory. try again");
if (image != null) {
is = context.getContentResolver().openInputStream(image);
} else {
is = new FileInputStream(getIncomingFile());
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
originalBitmap = BitmapFactory.decodeStream(is, null, options);
}
if (originalBitmap == null) {
throw new ImageCopyException(R.string.error_not_an_image_file);
}
if (image == null) {
getIncomingFile().delete();
}
Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE);
OutputStream os = new FileOutputStream(file);
boolean success = scalledBitmap.compress(
Bitmap.CompressFormat.WEBP, 75, os);
if (!success) {
throw new ImageCopyException(R.string.error_compressing_image);
}
os.flush();
os.close();
long size = file.getSize();
int width = scalledBitmap.getWidth();
int height = scalledBitmap.getHeight();
message.setBody("" + size + "," + width + "," + height);
return file;
} catch (FileNotFoundException e) {
throw new ImageCopyException(R.string.error_file_not_found);
} catch (IOException e) {
throw new ImageCopyException(R.string.error_io_exception);
} catch (SecurityException e) {
throw new ImageCopyException(
R.string.error_security_exception_during_image_copy);
}
}
public Bitmap getImageFromMessage(Message message) {
return BitmapFactory.decodeFile(getJingleFile(message)
.getAbsolutePath());
}
public Bitmap getThumbnail(Message message, int size, boolean cacheOnly)
throws FileNotFoundException {
Bitmap thumbnail = thumbnailCache.get(message.getUuid());
if ((thumbnail == null) && (!cacheOnly)) {
Bitmap fullsize = BitmapFactory.decodeFile(getJingleFile(message)
.getAbsolutePath());
if (fullsize == null) {
throw new FileNotFoundException();
}
thumbnail = resize(fullsize, size);
this.thumbnailCache.put(message.getUuid(), thumbnail);
}
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();
}
public File getIncomingFile() {
return new File(context.getFilesDir().getAbsolutePath() + "/incoming");
}
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;
}
}
}
|