1package eu.siacs.conversations.persistance;
2
3import android.annotation.TargetApi;
4import android.content.ContentResolver;
5import android.content.Context;
6import android.content.Intent;
7import android.database.Cursor;
8import android.graphics.Bitmap;
9import android.graphics.BitmapFactory;
10import android.graphics.Canvas;
11import android.graphics.Color;
12import android.graphics.Matrix;
13import android.graphics.Paint;
14import android.graphics.RectF;
15import android.media.MediaMetadataRetriever;
16import android.net.Uri;
17import android.os.Build;
18import android.os.Environment;
19import android.os.ParcelFileDescriptor;
20import android.provider.MediaStore;
21import android.provider.OpenableColumns;
22import android.support.v4.content.FileProvider;
23import android.system.Os;
24import android.system.StructStat;
25import android.util.Base64;
26import android.util.Base64OutputStream;
27import android.util.Log;
28import android.util.LruCache;
29import android.webkit.MimeTypeMap;
30
31import java.io.ByteArrayOutputStream;
32import java.io.Closeable;
33import java.io.File;
34import java.io.FileDescriptor;
35import java.io.FileInputStream;
36import java.io.FileNotFoundException;
37import java.io.FileOutputStream;
38import java.io.IOException;
39import java.io.InputStream;
40import java.io.OutputStream;
41import java.net.Socket;
42import java.net.URL;
43import java.security.DigestOutputStream;
44import java.security.MessageDigest;
45import java.security.NoSuchAlgorithmException;
46import java.text.SimpleDateFormat;
47import java.util.Date;
48import java.util.List;
49import java.util.Locale;
50
51import eu.siacs.conversations.Config;
52import eu.siacs.conversations.R;
53import eu.siacs.conversations.entities.DownloadableFile;
54import eu.siacs.conversations.entities.Message;
55import eu.siacs.conversations.services.XmppConnectionService;
56import eu.siacs.conversations.utils.CryptoHelper;
57import eu.siacs.conversations.utils.ExifHelper;
58import eu.siacs.conversations.utils.FileUtils;
59import eu.siacs.conversations.utils.FileWriterException;
60import eu.siacs.conversations.utils.MimeUtils;
61import eu.siacs.conversations.xmpp.pep.Avatar;
62
63public class FileBackend {
64
65 private static final Object THUMBNAIL_LOCK = new Object();
66
67 private static final SimpleDateFormat IMAGE_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
68
69 public static final String FILE_PROVIDER = ".files";
70
71 private XmppConnectionService mXmppConnectionService;
72
73 public FileBackend(XmppConnectionService service) {
74 this.mXmppConnectionService = service;
75 }
76
77 private void createNoMedia() {
78 final File nomedia = new File(getConversationsDirectory("Files") + ".nomedia");
79 if (!nomedia.exists()) {
80 try {
81 nomedia.createNewFile();
82 } catch (Exception e) {
83 Log.d(Config.LOGTAG, "could not create nomedia file");
84 }
85 }
86 }
87
88 public void updateMediaScanner(File file) {
89 String path = file.getAbsolutePath();
90 if (!path.startsWith(getConversationsDirectory("Files"))) {
91 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
92 intent.setData(Uri.fromFile(file));
93 mXmppConnectionService.sendBroadcast(intent);
94 } else {
95 createNoMedia();
96 }
97 }
98
99 public boolean deleteFile(Message message) {
100 File file = getFile(message);
101 if (file.delete()) {
102 updateMediaScanner(file);
103 return true;
104 } else {
105 return false;
106 }
107 }
108
109 public DownloadableFile getFile(Message message) {
110 return getFile(message, true);
111 }
112
113 public DownloadableFile getFileForPath(String path, String mime) {
114 final DownloadableFile file;
115 if (path.startsWith("/")) {
116 file = new DownloadableFile(path);
117 } else {
118 if (mime != null && mime.startsWith("image/")) {
119 file = new DownloadableFile(getConversationsDirectory("Images") + path);
120 } else if (mime != null && mime.startsWith("video/")) {
121 file = new DownloadableFile(getConversationsDirectory("Videos") + path);
122 } else {
123 file = new DownloadableFile(getConversationsDirectory("Files") + path);
124 }
125 }
126 return file;
127 }
128
129 public DownloadableFile getFile(Message message, boolean decrypted) {
130 final boolean encrypted = !decrypted
131 && (message.getEncryption() == Message.ENCRYPTION_PGP
132 || message.getEncryption() == Message.ENCRYPTION_DECRYPTED);
133 String path = message.getRelativeFilePath();
134 if (path == null) {
135 path = message.getUuid();
136 }
137 final DownloadableFile file = getFileForPath(path, message.getMimeType());
138 if (encrypted) {
139 return new DownloadableFile(getConversationsDirectory("Files") + file.getName() + ".pgp");
140 } else {
141 return file;
142 }
143 }
144
145 public static long getFileSize(Context context, Uri uri) {
146 try {
147 final Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
148 if (cursor != null && cursor.moveToFirst()) {
149 long size = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
150 cursor.close();
151 return size;
152 } else {
153 return -1;
154 }
155 } catch (Exception e) {
156 return -1;
157 }
158 }
159
160 public static boolean allFilesUnderSize(Context context, List<Uri> uris, long max) {
161 if (max <= 0) {
162 Log.d(Config.LOGTAG, "server did not report max file size for http upload");
163 return true; //exception to be compatible with HTTP Upload < v0.2
164 }
165 for (Uri uri : uris) {
166 String mime = context.getContentResolver().getType(uri);
167 if (mime != null && mime.startsWith("video/")) {
168 try {
169 Dimensions dimensions = FileBackend.getVideoDimensions(context, uri);
170 if (dimensions.getMin() > 720) {
171 Log.d(Config.LOGTAG, "do not consider video file with min width larger than 720 for size check");
172 continue;
173 }
174 } catch (NotAVideoFile notAVideoFile) {
175 //ignore and fall through
176 }
177 }
178 if (FileBackend.getFileSize(context, uri) > max) {
179 Log.d(Config.LOGTAG, "not all files are under " + max + " bytes. suggesting falling back to jingle");
180 return false;
181 }
182 }
183 return true;
184 }
185
186 public String getConversationsDirectory(final String type) {
187 if (Config.ONLY_INTERNAL_STORAGE) {
188 return mXmppConnectionService.getFilesDir().getAbsolutePath() + "/" + type + "/";
189 } else {
190 return Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations " + type + "/";
191 }
192 }
193
194 public static String getConversationsLogsDirectory() {
195 return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Conversations/";
196 }
197
198 public Bitmap resize(Bitmap originalBitmap, int size) {
199 int w = originalBitmap.getWidth();
200 int h = originalBitmap.getHeight();
201 if (Math.max(w, h) > size) {
202 int scalledW;
203 int scalledH;
204 if (w <= h) {
205 scalledW = (int) (w / ((double) h / size));
206 scalledH = size;
207 } else {
208 scalledW = size;
209 scalledH = (int) (h / ((double) w / size));
210 }
211 Bitmap result = Bitmap.createScaledBitmap(originalBitmap, scalledW, scalledH, true);
212 if (originalBitmap != null && !originalBitmap.isRecycled()) {
213 originalBitmap.recycle();
214 }
215 return result;
216 } else {
217 return originalBitmap;
218 }
219 }
220
221 public static Bitmap rotate(Bitmap bitmap, int degree) {
222 if (degree == 0) {
223 return bitmap;
224 }
225 int w = bitmap.getWidth();
226 int h = bitmap.getHeight();
227 Matrix mtx = new Matrix();
228 mtx.postRotate(degree);
229 Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
230 if (bitmap != null && !bitmap.isRecycled()) {
231 bitmap.recycle();
232 }
233 return result;
234 }
235
236 public boolean useImageAsIs(Uri uri) {
237 String path = getOriginalPath(uri);
238 if (path == null) {
239 return false;
240 }
241 File file = new File(path);
242 long size = file.length();
243 if (size == 0 || size >= mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize)) {
244 return false;
245 }
246 BitmapFactory.Options options = new BitmapFactory.Options();
247 options.inJustDecodeBounds = true;
248 try {
249 BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(uri), null, options);
250 if (options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) {
251 return false;
252 }
253 return (options.outWidth <= Config.IMAGE_SIZE && options.outHeight <= Config.IMAGE_SIZE && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase()));
254 } catch (FileNotFoundException e) {
255 return false;
256 }
257 }
258
259 public String getOriginalPath(Uri uri) {
260 return FileUtils.getPath(mXmppConnectionService, uri);
261 }
262
263 public void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
264 Log.d(Config.LOGTAG, "copy file (" + uri.toString() + ") to private storage " + file.getAbsolutePath());
265 file.getParentFile().mkdirs();
266 OutputStream os = null;
267 InputStream is = null;
268 try {
269 file.createNewFile();
270 os = new FileOutputStream(file);
271 is = mXmppConnectionService.getContentResolver().openInputStream(uri);
272 byte[] buffer = new byte[1024];
273 int length;
274 while ((length = is.read(buffer)) > 0) {
275 try {
276 os.write(buffer, 0, length);
277 } catch (IOException e) {
278 throw new FileWriterException();
279 }
280 }
281 try {
282 os.flush();
283 } catch (IOException e) {
284 throw new FileWriterException();
285 }
286 } catch (FileNotFoundException e) {
287 throw new FileCopyException(R.string.error_file_not_found);
288 } catch (FileWriterException e) {
289 throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
290 } catch (IOException e) {
291 e.printStackTrace();
292 throw new FileCopyException(R.string.error_io_exception);
293 } finally {
294 close(os);
295 close(is);
296 }
297 }
298
299 public void copyFileToPrivateStorage(Message message, Uri uri) throws FileCopyException {
300 String mime = MimeUtils.guessMimeTypeFromUri(mXmppConnectionService, uri);
301 Log.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage (mime=" + mime + ")");
302 String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime);
303 if (extension == null) {
304 extension = getExtensionFromUri(uri);
305 }
306 message.setRelativeFilePath(message.getUuid() + "." + extension);
307 copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri);
308 }
309
310 private String getExtensionFromUri(Uri uri) {
311 String[] projection = {MediaStore.MediaColumns.DATA};
312 String filename = null;
313 Cursor cursor = mXmppConnectionService.getContentResolver().query(uri, projection, null, null, null);
314 if (cursor != null) {
315 try {
316 if (cursor.moveToFirst()) {
317 filename = cursor.getString(0);
318 }
319 } catch (Exception e) {
320 filename = null;
321 } finally {
322 cursor.close();
323 }
324 }
325 int pos = filename == null ? -1 : filename.lastIndexOf('.');
326 return pos > 0 ? filename.substring(pos + 1) : null;
327 }
328
329 private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException {
330 file.getParentFile().mkdirs();
331 InputStream is = null;
332 OutputStream os = null;
333 try {
334 if (!file.exists() && !file.createNewFile()) {
335 throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
336 }
337 is = mXmppConnectionService.getContentResolver().openInputStream(image);
338 if (is == null) {
339 throw new FileCopyException(R.string.error_not_an_image_file);
340 }
341 Bitmap originalBitmap;
342 BitmapFactory.Options options = new BitmapFactory.Options();
343 int inSampleSize = (int) Math.pow(2, sampleSize);
344 Log.d(Config.LOGTAG, "reading bitmap with sample size " + inSampleSize);
345 options.inSampleSize = inSampleSize;
346 originalBitmap = BitmapFactory.decodeStream(is, null, options);
347 is.close();
348 if (originalBitmap == null) {
349 throw new FileCopyException(R.string.error_not_an_image_file);
350 }
351 Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
352 int rotation = getRotation(image);
353 scaledBitmap = rotate(scaledBitmap, rotation);
354 boolean targetSizeReached = false;
355 int quality = Config.IMAGE_QUALITY;
356 final int imageMaxSize = mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize);
357 while (!targetSizeReached) {
358 os = new FileOutputStream(file);
359 boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, quality, os);
360 if (!success) {
361 throw new FileCopyException(R.string.error_compressing_image);
362 }
363 os.flush();
364 targetSizeReached = file.length() <= imageMaxSize || quality <= 50;
365 quality -= 5;
366 }
367 scaledBitmap.recycle();
368 } catch (FileNotFoundException e) {
369 throw new FileCopyException(R.string.error_file_not_found);
370 } catch (IOException e) {
371 e.printStackTrace();
372 throw new FileCopyException(R.string.error_io_exception);
373 } catch (SecurityException e) {
374 throw new FileCopyException(R.string.error_security_exception_during_image_copy);
375 } catch (OutOfMemoryError e) {
376 ++sampleSize;
377 if (sampleSize <= 3) {
378 copyImageToPrivateStorage(file, image, sampleSize);
379 } else {
380 throw new FileCopyException(R.string.error_out_of_memory);
381 }
382 } finally {
383 close(os);
384 close(is);
385 }
386 }
387
388 public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException {
389 Log.d(Config.LOGTAG, "copy image (" + image.toString() + ") to private storage " + file.getAbsolutePath());
390 copyImageToPrivateStorage(file, image, 0);
391 }
392
393 public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException {
394 switch (Config.IMAGE_FORMAT) {
395 case JPEG:
396 message.setRelativeFilePath(message.getUuid() + ".jpg");
397 break;
398 case PNG:
399 message.setRelativeFilePath(message.getUuid() + ".png");
400 break;
401 case WEBP:
402 message.setRelativeFilePath(message.getUuid() + ".webp");
403 break;
404 }
405 copyImageToPrivateStorage(getFile(message), image);
406 updateFileParams(message);
407 }
408
409 private int getRotation(File file) {
410 return getRotation(Uri.parse("file://" + file.getAbsolutePath()));
411 }
412
413 private int getRotation(Uri image) {
414 InputStream is = null;
415 try {
416 is = mXmppConnectionService.getContentResolver().openInputStream(image);
417 return ExifHelper.getOrientation(is);
418 } catch (FileNotFoundException e) {
419 return 0;
420 } finally {
421 close(is);
422 }
423 }
424
425 public Bitmap getThumbnail(Message message, int size, boolean cacheOnly) throws FileNotFoundException {
426 final String uuid = message.getUuid();
427 final LruCache<String, Bitmap> cache = mXmppConnectionService.getBitmapCache();
428 Bitmap thumbnail = cache.get(uuid);
429 if ((thumbnail == null) && (!cacheOnly)) {
430 synchronized (THUMBNAIL_LOCK) {
431 thumbnail = cache.get(uuid);
432 if (thumbnail != null) {
433 return thumbnail;
434 }
435 DownloadableFile file = getFile(message);
436 final String mime = file.getMimeType();
437 if (mime.startsWith("video/")) {
438 thumbnail = getVideoPreview(file, size);
439 } else {
440 Bitmap fullsize = getFullsizeImagePreview(file, size);
441 if (fullsize == null) {
442 throw new FileNotFoundException();
443 }
444 thumbnail = resize(fullsize, size);
445 thumbnail = rotate(thumbnail, getRotation(file));
446 if (mime.equals("image/gif")) {
447 Bitmap withGifOverlay = thumbnail.copy(Bitmap.Config.ARGB_8888, true);
448 drawOverlay(withGifOverlay, R.drawable.play_gif, 1.0f);
449 thumbnail.recycle();
450 thumbnail = withGifOverlay;
451 }
452 }
453 this.mXmppConnectionService.getBitmapCache().put(uuid, thumbnail);
454 }
455 }
456 return thumbnail;
457 }
458
459 private Bitmap getFullsizeImagePreview(File file, int size) {
460 BitmapFactory.Options options = new BitmapFactory.Options();
461 options.inSampleSize = calcSampleSize(file, size);
462 try {
463 return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
464 } catch (OutOfMemoryError e) {
465 options.inSampleSize *= 2;
466 return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
467 }
468 }
469
470 private void drawOverlay(Bitmap bitmap, int resource, float factor) {
471 Bitmap overlay = BitmapFactory.decodeResource(mXmppConnectionService.getResources(), resource);
472 Canvas canvas = new Canvas(bitmap);
473 float targetSize = Math.min(canvas.getWidth(), canvas.getHeight()) * factor;
474 Log.d(Config.LOGTAG, "target size overlay: " + targetSize + " overlay bitmap size was " + overlay.getHeight());
475 float left = (canvas.getWidth() - targetSize) / 2.0f;
476 float top = (canvas.getHeight() - targetSize) / 2.0f;
477 RectF dst = new RectF(left, top, left + targetSize - 1, top + targetSize - 1);
478 canvas.drawBitmap(overlay, null, dst, createAntiAliasingPaint());
479 }
480
481 private static Paint createAntiAliasingPaint() {
482 Paint paint = new Paint();
483 paint.setAntiAlias(true);
484 paint.setFilterBitmap(true);
485 paint.setDither(true);
486 return paint;
487 }
488
489 private Bitmap getVideoPreview(File file, int size) {
490 MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
491 Bitmap frame;
492 try {
493 metadataRetriever.setDataSource(file.getAbsolutePath());
494 frame = metadataRetriever.getFrameAtTime(0);
495 metadataRetriever.release();
496 frame = resize(frame, size);
497 } catch (RuntimeException e) {
498 frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
499 frame.eraseColor(0xff000000);
500 }
501 drawOverlay(frame, R.drawable.play_video, 0.75f);
502 return frame;
503 }
504
505 private static String getTakePhotoPath() {
506 return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/";
507 }
508
509 public Uri getTakePhotoUri() {
510 File file;
511 if (Config.ONLY_INTERNAL_STORAGE) {
512 file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath(), "Camera/IMG_" + this.IMAGE_DATE_FORMAT.format(new Date()) + ".jpg");
513 } else {
514 file = new File(getTakePhotoPath() + "IMG_" + this.IMAGE_DATE_FORMAT.format(new Date()) + ".jpg");
515 }
516 file.getParentFile().mkdirs();
517 return getUriForFile(mXmppConnectionService, file);
518 }
519
520 public static Uri getUriForFile(Context context, File file) {
521 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
522 try {
523 String packageId = context.getPackageName();
524 return FileProvider.getUriForFile(context, packageId + FILE_PROVIDER, file);
525 } catch (IllegalArgumentException e) {
526 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
527 throw new SecurityException(e);
528 } else {
529 return Uri.fromFile(file);
530 }
531 }
532 } else {
533 return Uri.fromFile(file);
534 }
535 }
536
537 public static Uri getIndexableTakePhotoUri(Uri original) {
538 if (Config.ONLY_INTERNAL_STORAGE || "file".equals(original.getScheme())) {
539 return original;
540 } else {
541 List<String> segments = original.getPathSegments();
542 return Uri.parse("file://" + getTakePhotoPath() + segments.get(segments.size() - 1));
543 }
544 }
545
546 public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
547 Bitmap bm = cropCenterSquare(image, size);
548 if (bm == null) {
549 return null;
550 }
551 if (hasAlpha(bm)) {
552 Log.d(Config.LOGTAG,"alpha in avatar detected; uploading as PNG");
553 bm.recycle();
554 bm = cropCenterSquare(image, 96);
555 return getPepAvatar(bm, Bitmap.CompressFormat.PNG, 100);
556 }
557 return getPepAvatar(bm, format, 100);
558 }
559
560 private static boolean hasAlpha(final Bitmap bitmap) {
561 for(int x = 0; x < bitmap.getWidth(); ++x) {
562 for(int y = 0; y < bitmap.getWidth(); ++y) {
563 if (Color.alpha(bitmap.getPixel(x,y)) < 255) {
564 return true;
565 }
566 }
567 }
568 return false;
569 }
570
571 private Avatar getPepAvatar(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
572 try {
573 ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
574 Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
575 MessageDigest digest = MessageDigest.getInstance("SHA-1");
576 DigestOutputStream mDigestOutputStream = new DigestOutputStream(mBase64OutputStream, digest);
577 if (!bitmap.compress(format, quality, mDigestOutputStream)) {
578 return null;
579 }
580 mDigestOutputStream.flush();
581 mDigestOutputStream.close();
582 long chars = mByteArrayOutputStream.size();
583 if (format != Bitmap.CompressFormat.PNG && quality >= 50 && chars >= Config.AVATAR_CHAR_LIMIT) {
584 int q = quality - 2;
585 Log.d(Config.LOGTAG, "avatar char length was " + chars + " reducing quality to " + q);
586 return getPepAvatar(bitmap, format, q);
587 }
588 Log.d(Config.LOGTAG, "settled on char length " + chars + " with quality=" + quality);
589 final Avatar avatar = new Avatar();
590 avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
591 avatar.image = new String(mByteArrayOutputStream.toByteArray());
592 if (format.equals(Bitmap.CompressFormat.WEBP)) {
593 avatar.type = "image/webp";
594 } else if (format.equals(Bitmap.CompressFormat.JPEG)) {
595 avatar.type = "image/jpeg";
596 } else if (format.equals(Bitmap.CompressFormat.PNG)) {
597 avatar.type = "image/png";
598 }
599 avatar.width = bitmap.getWidth();
600 avatar.height = bitmap.getHeight();
601 return avatar;
602 } catch (Exception e) {
603 return null;
604 }
605 }
606
607 public Avatar getStoredPepAvatar(String hash) {
608 if (hash == null) {
609 return null;
610 }
611 Avatar avatar = new Avatar();
612 File file = new File(getAvatarPath(hash));
613 FileInputStream is = null;
614 try {
615 avatar.size = file.length();
616 BitmapFactory.Options options = new BitmapFactory.Options();
617 options.inJustDecodeBounds = true;
618 BitmapFactory.decodeFile(file.getAbsolutePath(), options);
619 is = new FileInputStream(file);
620 ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
621 Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
622 MessageDigest digest = MessageDigest.getInstance("SHA-1");
623 DigestOutputStream os = new DigestOutputStream(mBase64OutputStream, digest);
624 byte[] buffer = new byte[4096];
625 int length;
626 while ((length = is.read(buffer)) > 0) {
627 os.write(buffer, 0, length);
628 }
629 os.flush();
630 os.close();
631 avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
632 avatar.image = new String(mByteArrayOutputStream.toByteArray());
633 avatar.height = options.outHeight;
634 avatar.width = options.outWidth;
635 avatar.type = options.outMimeType;
636 return avatar;
637 } catch (IOException e) {
638 return null;
639 } catch (NoSuchAlgorithmException e) {
640 return null;
641 } finally {
642 close(is);
643 }
644 }
645
646 public boolean isAvatarCached(Avatar avatar) {
647 File file = new File(getAvatarPath(avatar.getFilename()));
648 return file.exists();
649 }
650
651 public boolean save(Avatar avatar) {
652 File file;
653 if (isAvatarCached(avatar)) {
654 file = new File(getAvatarPath(avatar.getFilename()));
655 avatar.size = file.length();
656 } else {
657 String filename = getAvatarPath(avatar.getFilename());
658 file = new File(filename + ".tmp");
659 file.getParentFile().mkdirs();
660 OutputStream os = null;
661 try {
662 file.createNewFile();
663 os = new FileOutputStream(file);
664 MessageDigest digest = MessageDigest.getInstance("SHA-1");
665 digest.reset();
666 DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
667 final byte[] bytes = avatar.getImageAsBytes();
668 mDigestOutputStream.write(bytes);
669 mDigestOutputStream.flush();
670 mDigestOutputStream.close();
671 String sha1sum = CryptoHelper.bytesToHex(digest.digest());
672 if (sha1sum.equals(avatar.sha1sum)) {
673 file.renameTo(new File(filename));
674 } else {
675 Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
676 file.delete();
677 return false;
678 }
679 avatar.size = bytes.length;
680 } catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
681 return false;
682 } finally {
683 close(os);
684 }
685 }
686 return true;
687 }
688
689 public String getAvatarPath(String avatar) {
690 return mXmppConnectionService.getFilesDir().getAbsolutePath() + "/avatars/" + avatar;
691 }
692
693 public Uri getAvatarUri(String avatar) {
694 return Uri.parse("file:" + getAvatarPath(avatar));
695 }
696
697 public Bitmap cropCenterSquare(Uri image, int size) {
698 if (image == null) {
699 return null;
700 }
701 InputStream is = null;
702 try {
703 BitmapFactory.Options options = new BitmapFactory.Options();
704 options.inSampleSize = calcSampleSize(image, size);
705 is = mXmppConnectionService.getContentResolver().openInputStream(image);
706 if (is == null) {
707 return null;
708 }
709 Bitmap input = BitmapFactory.decodeStream(is, null, options);
710 if (input == null) {
711 return null;
712 } else {
713 input = rotate(input, getRotation(image));
714 return cropCenterSquare(input, size);
715 }
716 } catch (SecurityException e) {
717 return null; // happens for example on Android 6.0 if contacts permissions get revoked
718 } catch (FileNotFoundException e) {
719 return null;
720 } finally {
721 close(is);
722 }
723 }
724
725 public Bitmap cropCenter(Uri image, int newHeight, int newWidth) {
726 if (image == null) {
727 return null;
728 }
729 InputStream is = null;
730 try {
731 BitmapFactory.Options options = new BitmapFactory.Options();
732 options.inSampleSize = calcSampleSize(image, Math.max(newHeight, newWidth));
733 is = mXmppConnectionService.getContentResolver().openInputStream(image);
734 if (is == null) {
735 return null;
736 }
737 Bitmap source = BitmapFactory.decodeStream(is, null, options);
738 if (source == null) {
739 return null;
740 }
741 int sourceWidth = source.getWidth();
742 int sourceHeight = source.getHeight();
743 float xScale = (float) newWidth / sourceWidth;
744 float yScale = (float) newHeight / sourceHeight;
745 float scale = Math.max(xScale, yScale);
746 float scaledWidth = scale * sourceWidth;
747 float scaledHeight = scale * sourceHeight;
748 float left = (newWidth - scaledWidth) / 2;
749 float top = (newHeight - scaledHeight) / 2;
750
751 RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
752 Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
753 Canvas canvas = new Canvas(dest);
754 canvas.drawBitmap(source, null, targetRect, createAntiAliasingPaint());
755 if (source.isRecycled()) {
756 source.recycle();
757 }
758 return dest;
759 } catch (SecurityException e) {
760 return null; //android 6.0 with revoked permissions for example
761 } catch (FileNotFoundException e) {
762 return null;
763 } finally {
764 close(is);
765 }
766 }
767
768 public Bitmap cropCenterSquare(Bitmap input, int size) {
769 int w = input.getWidth();
770 int h = input.getHeight();
771
772 float scale = Math.max((float) size / h, (float) size / w);
773
774 float outWidth = scale * w;
775 float outHeight = scale * h;
776 float left = (size - outWidth) / 2;
777 float top = (size - outHeight) / 2;
778 RectF target = new RectF(left, top, left + outWidth, top + outHeight);
779
780 Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
781 Canvas canvas = new Canvas(output);
782 canvas.drawBitmap(input, null, target, createAntiAliasingPaint());
783 if (!input.isRecycled()) {
784 input.recycle();
785 }
786 return output;
787 }
788
789 private int calcSampleSize(Uri image, int size) throws FileNotFoundException, SecurityException {
790 BitmapFactory.Options options = new BitmapFactory.Options();
791 options.inJustDecodeBounds = true;
792 BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(image), null, options);
793 return calcSampleSize(options, size);
794 }
795
796 private static int calcSampleSize(File image, int size) {
797 BitmapFactory.Options options = new BitmapFactory.Options();
798 options.inJustDecodeBounds = true;
799 BitmapFactory.decodeFile(image.getAbsolutePath(), options);
800 return calcSampleSize(options, size);
801 }
802
803 public static int calcSampleSize(BitmapFactory.Options options, int size) {
804 int height = options.outHeight;
805 int width = options.outWidth;
806 int inSampleSize = 1;
807
808 if (height > size || width > size) {
809 int halfHeight = height / 2;
810 int halfWidth = width / 2;
811
812 while ((halfHeight / inSampleSize) > size
813 && (halfWidth / inSampleSize) > size) {
814 inSampleSize *= 2;
815 }
816 }
817 return inSampleSize;
818 }
819
820 public void updateFileParams(Message message) {
821 updateFileParams(message, null);
822 }
823
824 public void updateFileParams(Message message, URL url) {
825 DownloadableFile file = getFile(message);
826 final String mime = file.getMimeType();
827 boolean image = message.getType() == Message.TYPE_IMAGE || (mime != null && mime.startsWith("image/"));
828 boolean video = mime != null && mime.startsWith("video/");
829 boolean audio = mime != null && mime.startsWith("audio/");
830 final StringBuilder body = new StringBuilder();
831 if (url != null) {
832 body.append(url.toString());
833 }
834 body.append('|').append(file.getSize());
835 if (image || video) {
836 try {
837 Dimensions dimensions = image ? getImageDimensions(file) : getVideoDimensions(file);
838 body.append('|').append(dimensions.width).append('|').append(dimensions.height);
839 } catch (NotAVideoFile notAVideoFile) {
840 Log.d(Config.LOGTAG, "file with mime type " + file.getMimeType() + " was not a video file");
841 //fall threw
842 }
843 } else if (audio) {
844 body.append("|0|0|").append(getMediaRuntime(file));
845 }
846 message.setBody(body.toString());
847 }
848
849 public int getMediaRuntime(Uri uri) {
850 try {
851 MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
852 mediaMetadataRetriever.setDataSource(mXmppConnectionService, uri);
853 return Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
854 } catch (RuntimeException e) {
855 return 0;
856 }
857 }
858
859 private int getMediaRuntime(File file) {
860 try {
861 MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
862 mediaMetadataRetriever.setDataSource(file.toString());
863 return Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
864 } catch (RuntimeException e) {
865 return 0;
866 }
867 }
868
869 private Dimensions getImageDimensions(File file) {
870 BitmapFactory.Options options = new BitmapFactory.Options();
871 options.inJustDecodeBounds = true;
872 BitmapFactory.decodeFile(file.getAbsolutePath(), options);
873 int rotation = getRotation(file);
874 boolean rotated = rotation == 90 || rotation == 270;
875 int imageHeight = rotated ? options.outWidth : options.outHeight;
876 int imageWidth = rotated ? options.outHeight : options.outWidth;
877 return new Dimensions(imageHeight, imageWidth);
878 }
879
880 private Dimensions getVideoDimensions(File file) throws NotAVideoFile {
881 MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
882 try {
883 metadataRetriever.setDataSource(file.getAbsolutePath());
884 } catch (RuntimeException e) {
885 throw new NotAVideoFile(e);
886 }
887 return getVideoDimensions(metadataRetriever);
888 }
889
890 private static Dimensions getVideoDimensions(Context context, Uri uri) throws NotAVideoFile {
891 MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
892 try {
893 mediaMetadataRetriever.setDataSource(context, uri);
894 } catch (RuntimeException e) {
895 throw new NotAVideoFile(e);
896 }
897 return getVideoDimensions(mediaMetadataRetriever);
898 }
899
900 private static Dimensions getVideoDimensions(MediaMetadataRetriever metadataRetriever) throws NotAVideoFile {
901 String hasVideo = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
902 if (hasVideo == null) {
903 throw new NotAVideoFile();
904 }
905 int rotation = extractRotationFromMediaRetriever(metadataRetriever);
906 boolean rotated = rotation == 90 || rotation == 270;
907 int height;
908 try {
909 String h = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
910 height = Integer.parseInt(h);
911 } catch (Exception e) {
912 height = -1;
913 }
914 int width;
915 try {
916 String w = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
917 width = Integer.parseInt(w);
918 } catch (Exception e) {
919 width = -1;
920 }
921 metadataRetriever.release();
922 Log.d(Config.LOGTAG, "extracted video dims " + width + "x" + height);
923 return rotated ? new Dimensions(width, height) : new Dimensions(height, width);
924 }
925
926 private static int extractRotationFromMediaRetriever(MediaMetadataRetriever metadataRetriever) {
927 int rotation;
928 if (Build.VERSION.SDK_INT >= 17) {
929 String r = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
930 try {
931 rotation = Integer.parseInt(r);
932 } catch (Exception e) {
933 rotation = 0;
934 }
935 } else {
936 rotation = 0;
937 }
938 return rotation;
939 }
940
941 private static class Dimensions {
942 public final int width;
943 public final int height;
944
945 public Dimensions(int height, int width) {
946 this.width = width;
947 this.height = height;
948 }
949
950 public int getMin() {
951 return Math.min(width, height);
952 }
953 }
954
955 private static class NotAVideoFile extends Exception {
956 public NotAVideoFile(Throwable t) {
957 super(t);
958 }
959
960 public NotAVideoFile() {
961 super();
962 }
963 }
964
965 public class FileCopyException extends Exception {
966 private static final long serialVersionUID = -1010013599132881427L;
967 private int resId;
968
969 public FileCopyException(int resId) {
970 this.resId = resId;
971 }
972
973 public int getResId() {
974 return resId;
975 }
976 }
977
978 public Bitmap getAvatar(String avatar, int size) {
979 if (avatar == null) {
980 return null;
981 }
982 Bitmap bm = cropCenter(getAvatarUri(avatar), size, size);
983 if (bm == null) {
984 return null;
985 }
986 return bm;
987 }
988
989 public boolean isFileAvailable(Message message) {
990 return getFile(message).exists();
991 }
992
993 public static void close(Closeable stream) {
994 if (stream != null) {
995 try {
996 stream.close();
997 } catch (IOException e) {
998 }
999 }
1000 }
1001
1002 public static void close(Socket socket) {
1003 if (socket != null) {
1004 try {
1005 socket.close();
1006 } catch (IOException e) {
1007 }
1008 }
1009 }
1010
1011
1012 public static boolean weOwnFile(Context context, Uri uri) {
1013 if (uri == null || !ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
1014 return false;
1015 } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
1016 return fileIsInFilesDir(context, uri);
1017 } else {
1018 return weOwnFileLollipop(uri);
1019 }
1020 }
1021
1022
1023 /**
1024 * This is more than hacky but probably way better than doing nothing
1025 * Further 'optimizations' might contain to get the parents of CacheDir and NoBackupDir
1026 * and check against those as well
1027 */
1028 private static boolean fileIsInFilesDir(Context context, Uri uri) {
1029 try {
1030 final String haystack = context.getFilesDir().getParentFile().getCanonicalPath();
1031 final String needle = new File(uri.getPath()).getCanonicalPath();
1032 return needle.startsWith(haystack);
1033 } catch (IOException e) {
1034 return false;
1035 }
1036 }
1037
1038 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
1039 private static boolean weOwnFileLollipop(Uri uri) {
1040 try {
1041 File file = new File(uri.getPath());
1042 FileDescriptor fd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY).getFileDescriptor();
1043 StructStat st = Os.fstat(fd);
1044 return st.st_uid == android.os.Process.myUid();
1045 } catch (FileNotFoundException e) {
1046 return false;
1047 } catch (Exception e) {
1048 return true;
1049 }
1050 }
1051}