1package eu.siacs.conversations.persistance;
2
3import android.annotation.TargetApi;
4import android.content.ContentResolver;
5import android.content.Context;
6import android.database.Cursor;
7import android.graphics.Bitmap;
8import android.graphics.BitmapFactory;
9import android.graphics.Canvas;
10import android.graphics.Color;
11import android.graphics.Matrix;
12import android.graphics.Paint;
13import android.graphics.RectF;
14import android.graphics.pdf.PdfRenderer;
15import android.media.MediaMetadataRetriever;
16import android.media.MediaScannerConnection;
17import android.net.Uri;
18import android.os.Build;
19import android.os.Environment;
20import android.os.ParcelFileDescriptor;
21import android.provider.MediaStore;
22import android.provider.OpenableColumns;
23import android.system.Os;
24import android.system.StructStat;
25import android.util.Base64;
26import android.util.Base64OutputStream;
27import android.util.DisplayMetrics;
28import android.util.Log;
29import android.util.LruCache;
30
31import androidx.annotation.RequiresApi;
32import androidx.annotation.StringRes;
33import androidx.core.content.FileProvider;
34import androidx.exifinterface.media.ExifInterface;
35
36import com.google.common.io.ByteStreams;
37
38import java.io.ByteArrayOutputStream;
39import java.io.Closeable;
40import java.io.File;
41import java.io.FileDescriptor;
42import java.io.FileInputStream;
43import java.io.FileNotFoundException;
44import java.io.FileOutputStream;
45import java.io.IOException;
46import java.io.InputStream;
47import java.io.OutputStream;
48import java.net.ServerSocket;
49import java.net.Socket;
50import java.security.DigestOutputStream;
51import java.security.MessageDigest;
52import java.security.NoSuchAlgorithmException;
53import java.text.SimpleDateFormat;
54import java.util.ArrayList;
55import java.util.Date;
56import java.util.List;
57import java.util.Locale;
58import java.util.UUID;
59
60import eu.siacs.conversations.Config;
61import eu.siacs.conversations.R;
62import eu.siacs.conversations.entities.DownloadableFile;
63import eu.siacs.conversations.entities.Message;
64import eu.siacs.conversations.services.AttachFileToConversationRunnable;
65import eu.siacs.conversations.services.XmppConnectionService;
66import eu.siacs.conversations.ui.RecordingActivity;
67import eu.siacs.conversations.ui.util.Attachment;
68import eu.siacs.conversations.utils.Compatibility;
69import eu.siacs.conversations.utils.CryptoHelper;
70import eu.siacs.conversations.utils.FileUtils;
71import eu.siacs.conversations.utils.FileWriterException;
72import eu.siacs.conversations.utils.MimeUtils;
73import eu.siacs.conversations.xmpp.pep.Avatar;
74
75public class FileBackend {
76
77 private static final Object THUMBNAIL_LOCK = new Object();
78
79 private static final SimpleDateFormat IMAGE_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
80
81 private static final String FILE_PROVIDER = ".files";
82 private static final float IGNORE_PADDING = 0.15f;
83 private final XmppConnectionService mXmppConnectionService;
84
85 public FileBackend(XmppConnectionService service) {
86 this.mXmppConnectionService = service;
87 }
88
89 private static boolean isInDirectoryThatShouldNotBeScanned(Context context, File file) {
90 return isInDirectoryThatShouldNotBeScanned(context, file.getAbsolutePath());
91 }
92
93 public static boolean isInDirectoryThatShouldNotBeScanned(Context context, String path) {
94 for (String type : new String[]{RecordingActivity.STORAGE_DIRECTORY_TYPE_NAME, "Files"}) {
95 if (path.startsWith(getConversationsDirectory(context, type))) {
96 return true;
97 }
98 }
99 return false;
100 }
101
102 public static long getFileSize(Context context, Uri uri) {
103 try {
104 final Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
105 if (cursor != null && cursor.moveToFirst()) {
106 long size = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
107 cursor.close();
108 return size;
109 } else {
110 return -1;
111 }
112 } catch (Exception e) {
113 return -1;
114 }
115 }
116
117 public static boolean allFilesUnderSize(Context context, List<Attachment> attachments, long max) {
118 final boolean compressVideo = !AttachFileToConversationRunnable.getVideoCompression(context).equals("uncompressed");
119 if (max <= 0) {
120 Log.d(Config.LOGTAG, "server did not report max file size for http upload");
121 return true; //exception to be compatible with HTTP Upload < v0.2
122 }
123 for (Attachment attachment : attachments) {
124 if (attachment.getType() != Attachment.Type.FILE) {
125 continue;
126 }
127 String mime = attachment.getMime();
128 if (mime != null && mime.startsWith("video/") && compressVideo) {
129 try {
130 Dimensions dimensions = FileBackend.getVideoDimensions(context, attachment.getUri());
131 if (dimensions.getMin() > 720) {
132 Log.d(Config.LOGTAG, "do not consider video file with min width larger than 720 for size check");
133 continue;
134 }
135 } catch (NotAVideoFile notAVideoFile) {
136 //ignore and fall through
137 }
138 }
139 if (FileBackend.getFileSize(context, attachment.getUri()) > max) {
140 Log.d(Config.LOGTAG, "not all files are under " + max + " bytes. suggesting falling back to jingle");
141 return false;
142 }
143 }
144 return true;
145 }
146
147 public static String getConversationsDirectory(Context context, final String type) {
148 if (Config.ONLY_INTERNAL_STORAGE) {
149 return context.getFilesDir().getAbsolutePath() + "/" + type + "/";
150 } else {
151 return getAppMediaDirectory(context) + context.getString(R.string.app_name) + " " + type + "/";
152 }
153 }
154
155 public static String getAppMediaDirectory(Context context) {
156 return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + context.getString(R.string.app_name) + "/Media/";
157 }
158
159 public static String getBackupDirectory(Context context) {
160 return getBackupDirectory(context.getString(R.string.app_name));
161 }
162
163 public static String getBackupDirectory(String app) {
164 return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + app + "/Backup/";
165 }
166
167 private static Bitmap rotate(final Bitmap bitmap, final int degree) {
168 if (degree == 0) {
169 return bitmap;
170 }
171 final int w = bitmap.getWidth();
172 final int h = bitmap.getHeight();
173 final Matrix matrix = new Matrix();
174 matrix.postRotate(degree);
175 final Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
176 if (!bitmap.isRecycled()) {
177 bitmap.recycle();
178 }
179 return result;
180 }
181
182 public static boolean isPathBlacklisted(String path) {
183 final String androidDataPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/";
184 return path.startsWith(androidDataPath);
185 }
186
187 private static Paint createAntiAliasingPaint() {
188 Paint paint = new Paint();
189 paint.setAntiAlias(true);
190 paint.setFilterBitmap(true);
191 paint.setDither(true);
192 return paint;
193 }
194
195 private static String getTakePhotoPath() {
196 return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/";
197 }
198
199 public static Uri getUriForUri(Context context, Uri uri) {
200 if ("file".equals(uri.getScheme())) {
201 return getUriForFile(context, new File(uri.getPath()));
202 } else {
203 return uri;
204 }
205 }
206
207 public static Uri getUriForFile(Context context, File file) {
208 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
209 try {
210 return FileProvider.getUriForFile(context, getAuthority(context), file);
211 } catch (IllegalArgumentException e) {
212 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
213 throw new SecurityException(e);
214 } else {
215 return Uri.fromFile(file);
216 }
217 }
218 } else {
219 return Uri.fromFile(file);
220 }
221 }
222
223 public static String getAuthority(Context context) {
224 return context.getPackageName() + FILE_PROVIDER;
225 }
226
227 private static boolean hasAlpha(final Bitmap bitmap) {
228 final int w = bitmap.getWidth();
229 final int h = bitmap.getHeight();
230 final int yStep = Math.max(1, w / 100);
231 final int xStep = Math.max(1, h / 100);
232 for (int x = 0; x < w; x += xStep) {
233 for (int y = 0; y < h; y += yStep) {
234 if (Color.alpha(bitmap.getPixel(x, y)) < 255) {
235 return true;
236 }
237 }
238 }
239 return false;
240 }
241
242 private static int calcSampleSize(File image, int size) {
243 BitmapFactory.Options options = new BitmapFactory.Options();
244 options.inJustDecodeBounds = true;
245 BitmapFactory.decodeFile(image.getAbsolutePath(), options);
246 return calcSampleSize(options, size);
247 }
248
249
250 private static int calcSampleSize(BitmapFactory.Options options, int size) {
251 int height = options.outHeight;
252 int width = options.outWidth;
253 int inSampleSize = 1;
254
255 if (height > size || width > size) {
256 int halfHeight = height / 2;
257 int halfWidth = width / 2;
258
259 while ((halfHeight / inSampleSize) > size
260 && (halfWidth / inSampleSize) > size) {
261 inSampleSize *= 2;
262 }
263 }
264 return inSampleSize;
265 }
266
267 private static Dimensions getVideoDimensions(Context context, Uri uri) throws NotAVideoFile {
268 MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
269 try {
270 mediaMetadataRetriever.setDataSource(context, uri);
271 } catch (RuntimeException e) {
272 throw new NotAVideoFile(e);
273 }
274 return getVideoDimensions(mediaMetadataRetriever);
275 }
276
277 private static Dimensions getVideoDimensionsOfFrame(MediaMetadataRetriever mediaMetadataRetriever) {
278 Bitmap bitmap = null;
279 try {
280 bitmap = mediaMetadataRetriever.getFrameAtTime();
281 return new Dimensions(bitmap.getHeight(), bitmap.getWidth());
282 } catch (Exception e) {
283 return null;
284 } finally {
285 if (bitmap != null) {
286 bitmap.recycle();
287 }
288 }
289 }
290
291 private static Dimensions getVideoDimensions(MediaMetadataRetriever metadataRetriever) throws NotAVideoFile {
292 String hasVideo = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
293 if (hasVideo == null) {
294 throw new NotAVideoFile();
295 }
296 Dimensions dimensions = getVideoDimensionsOfFrame(metadataRetriever);
297 if (dimensions != null) {
298 return dimensions;
299 }
300 final int rotation = extractRotationFromMediaRetriever(metadataRetriever);
301 boolean rotated = rotation == 90 || rotation == 270;
302 int height;
303 try {
304 String h = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
305 height = Integer.parseInt(h);
306 } catch (Exception e) {
307 height = -1;
308 }
309 int width;
310 try {
311 String w = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
312 width = Integer.parseInt(w);
313 } catch (Exception e) {
314 width = -1;
315 }
316 metadataRetriever.release();
317 Log.d(Config.LOGTAG, "extracted video dims " + width + "x" + height);
318 return rotated ? new Dimensions(width, height) : new Dimensions(height, width);
319 }
320
321 private static int extractRotationFromMediaRetriever(MediaMetadataRetriever metadataRetriever) {
322 String r = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
323 try {
324 return Integer.parseInt(r);
325 } catch (Exception e) {
326 return 0;
327 }
328 }
329
330 public static void close(final Closeable stream) {
331 if (stream != null) {
332 try {
333 stream.close();
334 } catch (Exception e) {
335 Log.d(Config.LOGTAG, "unable to close stream", e);
336 }
337 }
338 }
339
340 public static void close(final Socket socket) {
341 if (socket != null) {
342 try {
343 socket.close();
344 } catch (IOException e) {
345 Log.d(Config.LOGTAG, "unable to close socket", e);
346 }
347 }
348 }
349
350 public static void close(final ServerSocket socket) {
351 if (socket != null) {
352 try {
353 socket.close();
354 } catch (IOException e) {
355 Log.d(Config.LOGTAG, "unable to close server socket", e);
356 }
357 }
358 }
359
360 public static boolean weOwnFile(Context context, Uri uri) {
361 if (uri == null || !ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
362 return false;
363 } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
364 return fileIsInFilesDir(context, uri);
365 } else {
366 return weOwnFileLollipop(uri);
367 }
368 }
369
370 /**
371 * This is more than hacky but probably way better than doing nothing
372 * Further 'optimizations' might contain to get the parents of CacheDir and NoBackupDir
373 * and check against those as well
374 */
375 private static boolean fileIsInFilesDir(Context context, Uri uri) {
376 try {
377 final String haystack = context.getFilesDir().getParentFile().getCanonicalPath();
378 final String needle = new File(uri.getPath()).getCanonicalPath();
379 return needle.startsWith(haystack);
380 } catch (IOException e) {
381 return false;
382 }
383 }
384
385 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
386 private static boolean weOwnFileLollipop(Uri uri) {
387 try {
388 File file = new File(uri.getPath());
389 FileDescriptor fd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY).getFileDescriptor();
390 StructStat st = Os.fstat(fd);
391 return st.st_uid == android.os.Process.myUid();
392 } catch (FileNotFoundException e) {
393 return false;
394 } catch (Exception e) {
395 return true;
396 }
397 }
398
399 public static Uri getMediaUri(Context context, File file) {
400 final String filePath = file.getAbsolutePath();
401 final Cursor cursor;
402 try {
403 cursor = context.getContentResolver().query(
404 MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
405 new String[]{MediaStore.Images.Media._ID},
406 MediaStore.Images.Media.DATA + "=? ",
407 new String[]{filePath}, null);
408 } catch (SecurityException e) {
409 return null;
410 }
411 if (cursor != null && cursor.moveToFirst()) {
412 final int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
413 cursor.close();
414 return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id));
415 } else {
416 return null;
417 }
418 }
419
420 public static void updateFileParams(Message message, String url, long size) {
421 final StringBuilder body = new StringBuilder();
422 body.append(url).append('|').append(size);
423 message.setBody(body.toString());
424 }
425
426 public Bitmap getPreviewForUri(Attachment attachment, int size, boolean cacheOnly) {
427 final String key = "attachment_" + attachment.getUuid().toString() + "_" + size;
428 final LruCache<String, Bitmap> cache = mXmppConnectionService.getBitmapCache();
429 Bitmap bitmap = cache.get(key);
430 if (bitmap != null || cacheOnly) {
431 return bitmap;
432 }
433 final String mime = attachment.getMime();
434 if ("application/pdf".equals(mime) && Compatibility.runsTwentyOne()) {
435 bitmap = cropCenterSquarePdf(attachment.getUri(), size);
436 drawOverlay(bitmap, paintOverlayBlackPdf(bitmap) ? R.drawable.open_pdf_black : R.drawable.open_pdf_white, 0.75f);
437 } else if (mime != null && mime.startsWith("video/")) {
438 bitmap = cropCenterSquareVideo(attachment.getUri(), size);
439 drawOverlay(bitmap, paintOverlayBlack(bitmap) ? R.drawable.play_video_black : R.drawable.play_video_white, 0.75f);
440 } else {
441 bitmap = cropCenterSquare(attachment.getUri(), size);
442 if (bitmap != null && "image/gif".equals(mime)) {
443 Bitmap withGifOverlay = bitmap.copy(Bitmap.Config.ARGB_8888, true);
444 drawOverlay(withGifOverlay, paintOverlayBlack(withGifOverlay) ? R.drawable.play_gif_black : R.drawable.play_gif_white, 1.0f);
445 bitmap.recycle();
446 bitmap = withGifOverlay;
447 }
448 }
449 if (bitmap != null) {
450 cache.put(key, bitmap);
451 }
452 return bitmap;
453 }
454
455 private void createNoMedia(File diretory) {
456 final File noMedia = new File(diretory, ".nomedia");
457 if (!noMedia.exists()) {
458 try {
459 if (!noMedia.createNewFile()) {
460 Log.d(Config.LOGTAG, "created nomedia file " + noMedia.getAbsolutePath());
461 }
462 } catch (Exception e) {
463 Log.d(Config.LOGTAG, "could not create nomedia file");
464 }
465 }
466 }
467
468 public void updateMediaScanner(File file) {
469 updateMediaScanner(file, null);
470 }
471
472 public void updateMediaScanner(File file, final Runnable callback) {
473 if (!isInDirectoryThatShouldNotBeScanned(mXmppConnectionService, file)) {
474 MediaScannerConnection.scanFile(mXmppConnectionService, new String[]{file.getAbsolutePath()}, null, new MediaScannerConnection.MediaScannerConnectionClient() {
475 @Override
476 public void onMediaScannerConnected() {
477
478 }
479
480 @Override
481 public void onScanCompleted(String path, Uri uri) {
482 if (callback != null && file.getAbsolutePath().equals(path)) {
483 callback.run();
484 } else {
485 Log.d(Config.LOGTAG, "media scanner scanned wrong file");
486 if (callback != null) {
487 callback.run();
488 }
489 }
490 }
491 });
492 return;
493 /*Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
494 intent.setData(Uri.fromFile(file));
495 mXmppConnectionService.sendBroadcast(intent);*/
496 } else if (file.getAbsolutePath().startsWith(getAppMediaDirectory(mXmppConnectionService))) {
497 createNoMedia(file.getParentFile());
498 }
499 if (callback != null) {
500 callback.run();
501 }
502 }
503
504 public boolean deleteFile(Message message) {
505 File file = getFile(message);
506 if (file.delete()) {
507 updateMediaScanner(file);
508 return true;
509 } else {
510 return false;
511 }
512 }
513
514 public DownloadableFile getFile(Message message) {
515 return getFile(message, true);
516 }
517
518
519 public DownloadableFile getFileForPath(String path) {
520 return getFileForPath(path, MimeUtils.guessMimeTypeFromExtension(MimeUtils.extractRelevantExtension(path)));
521 }
522
523 public DownloadableFile getFileForPath(String path, String mime) {
524 final DownloadableFile file;
525 if (path.startsWith("/")) {
526 file = new DownloadableFile(path);
527 } else {
528 if (mime != null && mime.startsWith("image/")) {
529 file = new DownloadableFile(getConversationsDirectory("Images") + path);
530 } else if (mime != null && mime.startsWith("video/")) {
531 file = new DownloadableFile(getConversationsDirectory("Videos") + path);
532 } else {
533 file = new DownloadableFile(getConversationsDirectory("Files") + path);
534 }
535 }
536 return file;
537 }
538
539 public boolean isInternalFile(final File file) {
540 final File internalFile = getFileForPath(file.getName());
541 return file.getAbsolutePath().equals(internalFile.getAbsolutePath());
542 }
543
544 public DownloadableFile getFile(Message message, boolean decrypted) {
545 final boolean encrypted = !decrypted
546 && (message.getEncryption() == Message.ENCRYPTION_PGP
547 || message.getEncryption() == Message.ENCRYPTION_DECRYPTED);
548 String path = message.getRelativeFilePath();
549 if (path == null) {
550 path = message.getUuid();
551 }
552 final DownloadableFile file = getFileForPath(path, message.getMimeType());
553 if (encrypted) {
554 return new DownloadableFile(getConversationsDirectory("Files") + file.getName() + ".pgp");
555 } else {
556 return file;
557 }
558 }
559
560 public List<Attachment> convertToAttachments(List<DatabaseBackend.FilePath> relativeFilePaths) {
561 List<Attachment> attachments = new ArrayList<>();
562 for (DatabaseBackend.FilePath relativeFilePath : relativeFilePaths) {
563 final String mime = MimeUtils.guessMimeTypeFromExtension(MimeUtils.extractRelevantExtension(relativeFilePath.path));
564 final File file = getFileForPath(relativeFilePath.path, mime);
565 attachments.add(Attachment.of(relativeFilePath.uuid, file, mime));
566 }
567 return attachments;
568 }
569
570 private String getConversationsDirectory(final String type) {
571 return getConversationsDirectory(mXmppConnectionService, type);
572 }
573
574 private Bitmap resize(final Bitmap originalBitmap, int size) throws IOException {
575 int w = originalBitmap.getWidth();
576 int h = originalBitmap.getHeight();
577 if (w <= 0 || h <= 0) {
578 throw new IOException("Decoded bitmap reported bounds smaller 0");
579 } else if (Math.max(w, h) > size) {
580 int scalledW;
581 int scalledH;
582 if (w <= h) {
583 scalledW = Math.max((int) (w / ((double) h / size)), 1);
584 scalledH = size;
585 } else {
586 scalledW = size;
587 scalledH = Math.max((int) (h / ((double) w / size)), 1);
588 }
589 final Bitmap result = Bitmap.createScaledBitmap(originalBitmap, scalledW, scalledH, true);
590 if (!originalBitmap.isRecycled()) {
591 originalBitmap.recycle();
592 }
593 return result;
594 } else {
595 return originalBitmap;
596 }
597 }
598
599 public boolean useImageAsIs(final Uri uri) {
600 final String path = getOriginalPath(uri);
601 if (path == null || isPathBlacklisted(path)) {
602 return false;
603 }
604 final File file = new File(path);
605 long size = file.length();
606 if (size == 0 || size >= mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize)) {
607 return false;
608 }
609 BitmapFactory.Options options = new BitmapFactory.Options();
610 options.inJustDecodeBounds = true;
611 try {
612 final InputStream inputStream = mXmppConnectionService.getContentResolver().openInputStream(uri);
613 BitmapFactory.decodeStream(inputStream, null, options);
614 close(inputStream);
615 if (options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) {
616 return false;
617 }
618 return (options.outWidth <= Config.IMAGE_SIZE && options.outHeight <= Config.IMAGE_SIZE && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase()));
619 } catch (FileNotFoundException e) {
620 Log.d(Config.LOGTAG, "unable to get image dimensions", e);
621 return false;
622 }
623 }
624
625 public String getOriginalPath(Uri uri) {
626 return FileUtils.getPath(mXmppConnectionService, uri);
627 }
628
629 private void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
630 Log.d(Config.LOGTAG, "copy file (" + uri.toString() + ") to private storage " + file.getAbsolutePath());
631 file.getParentFile().mkdirs();
632 try {
633 file.createNewFile();
634 } catch (IOException e) {
635 throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
636 }
637 try (final OutputStream os = new FileOutputStream(file);
638 final InputStream is = mXmppConnectionService.getContentResolver().openInputStream(uri)) {
639 if (is == null) {
640 throw new FileCopyException(R.string.error_file_not_found);
641 }
642 try {
643 ByteStreams.copy(is, os);
644 } catch (IOException e) {
645 throw new FileWriterException();
646 }
647 try {
648 os.flush();
649 } catch (IOException e) {
650 throw new FileWriterException();
651 }
652 } catch (final FileNotFoundException e) {
653 cleanup(file);
654 throw new FileCopyException(R.string.error_file_not_found);
655 } catch (final FileWriterException e) {
656 cleanup(file);
657 throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
658 } catch (final SecurityException e) {
659 cleanup(file);
660 throw new FileCopyException(R.string.error_security_exception);
661 } catch (final IOException e) {
662 cleanup(file);
663 throw new FileCopyException(R.string.error_io_exception);
664 }
665 }
666
667 public void copyFileToPrivateStorage(Message message, Uri uri, String type) throws FileCopyException {
668 String mime = MimeUtils.guessMimeTypeFromUriAndMime(mXmppConnectionService, uri, type);
669 Log.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage (mime=" + mime + ")");
670 String extension = MimeUtils.guessExtensionFromMimeType(mime);
671 if (extension == null) {
672 Log.d(Config.LOGTAG, "extension from mime type was null");
673 extension = getExtensionFromUri(uri);
674 }
675 if ("ogg".equals(extension) && type != null && type.startsWith("audio/")) {
676 extension = "oga";
677 }
678 message.setRelativeFilePath(message.getUuid() + "." + extension);
679 copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri);
680 }
681
682 private String getExtensionFromUri(Uri uri) {
683 String[] projection = {MediaStore.MediaColumns.DATA};
684 String filename = null;
685 Cursor cursor;
686 try {
687 cursor = mXmppConnectionService.getContentResolver().query(uri, projection, null, null, null);
688 } catch (IllegalArgumentException e) {
689 cursor = null;
690 }
691 if (cursor != null) {
692 try {
693 if (cursor.moveToFirst()) {
694 filename = cursor.getString(0);
695 }
696 } catch (Exception e) {
697 filename = null;
698 } finally {
699 cursor.close();
700 }
701 }
702 if (filename == null) {
703 final List<String> segments = uri.getPathSegments();
704 if (segments.size() > 0) {
705 filename = segments.get(segments.size() - 1);
706 }
707 }
708 int pos = filename == null ? -1 : filename.lastIndexOf('.');
709 return pos > 0 ? filename.substring(pos + 1) : null;
710 }
711
712 private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException, ImageCompressionException {
713 final File parent = file.getParentFile();
714 if (parent != null && parent.mkdirs()) {
715 Log.d(Config.LOGTAG, "created parent directory");
716 }
717 InputStream is = null;
718 OutputStream os = null;
719 try {
720 if (!file.exists() && !file.createNewFile()) {
721 throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
722 }
723 is = mXmppConnectionService.getContentResolver().openInputStream(image);
724 if (is == null) {
725 throw new FileCopyException(R.string.error_not_an_image_file);
726 }
727 final Bitmap originalBitmap;
728 final BitmapFactory.Options options = new BitmapFactory.Options();
729 final int inSampleSize = (int) Math.pow(2, sampleSize);
730 Log.d(Config.LOGTAG, "reading bitmap with sample size " + inSampleSize);
731 options.inSampleSize = inSampleSize;
732 originalBitmap = BitmapFactory.decodeStream(is, null, options);
733 is.close();
734 if (originalBitmap == null) {
735 throw new ImageCompressionException("Source file was not an image");
736 }
737 if (!"image/jpeg".equals(options.outMimeType) && hasAlpha(originalBitmap)) {
738 originalBitmap.recycle();
739 throw new ImageCompressionException("Source file had alpha channel");
740 }
741 Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
742 final int rotation = getRotation(image);
743 scaledBitmap = rotate(scaledBitmap, rotation);
744 boolean targetSizeReached = false;
745 int quality = Config.IMAGE_QUALITY;
746 final int imageMaxSize = mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize);
747 while (!targetSizeReached) {
748 os = new FileOutputStream(file);
749 Log.d(Config.LOGTAG, "compressing image with quality " + quality);
750 boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, quality, os);
751 if (!success) {
752 throw new FileCopyException(R.string.error_compressing_image);
753 }
754 os.flush();
755 final long fileSize = file.length();
756 Log.d(Config.LOGTAG, "achieved file size of " + fileSize);
757 targetSizeReached = fileSize <= imageMaxSize || quality <= 50;
758 quality -= 5;
759 }
760 scaledBitmap.recycle();
761 } catch (final FileNotFoundException e) {
762 cleanup(file);
763 throw new FileCopyException(R.string.error_file_not_found);
764 } catch (final IOException e) {
765 cleanup(file);
766 throw new FileCopyException(R.string.error_io_exception);
767 } catch (SecurityException e) {
768 cleanup(file);
769 throw new FileCopyException(R.string.error_security_exception_during_image_copy);
770 } catch (final OutOfMemoryError e) {
771 ++sampleSize;
772 if (sampleSize <= 3) {
773 copyImageToPrivateStorage(file, image, sampleSize);
774 } else {
775 throw new FileCopyException(R.string.error_out_of_memory);
776 }
777 } finally {
778 close(os);
779 close(is);
780 }
781 }
782
783 private static void cleanup(final File file) {
784 try {
785 file.delete();
786 } catch (Exception e) {
787
788 }
789 }
790
791 public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException, ImageCompressionException {
792 Log.d(Config.LOGTAG, "copy image (" + image.toString() + ") to private storage " + file.getAbsolutePath());
793 copyImageToPrivateStorage(file, image, 0);
794 }
795
796 public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException, ImageCompressionException {
797 switch (Config.IMAGE_FORMAT) {
798 case JPEG:
799 message.setRelativeFilePath(message.getUuid() + ".jpg");
800 break;
801 case PNG:
802 message.setRelativeFilePath(message.getUuid() + ".png");
803 break;
804 case WEBP:
805 message.setRelativeFilePath(message.getUuid() + ".webp");
806 break;
807 }
808 copyImageToPrivateStorage(getFile(message), image);
809 updateFileParams(message);
810 }
811
812 public boolean unusualBounds(final Uri image) {
813 try {
814 final BitmapFactory.Options options = new BitmapFactory.Options();
815 options.inJustDecodeBounds = true;
816 final InputStream inputStream = mXmppConnectionService.getContentResolver().openInputStream(image);
817 BitmapFactory.decodeStream(inputStream, null, options);
818 close(inputStream);
819 float ratio = (float) options.outHeight / options.outWidth;
820 return ratio > (21.0f / 9.0f) || ratio < (9.0f / 21.0f);
821 } catch (final Exception e) {
822 Log.w(Config.LOGTAG, "unable to detect image bounds", e);
823 return false;
824 }
825 }
826
827 private int getRotation(final File file) {
828 try (final InputStream inputStream = new FileInputStream(file)) {
829 return getRotation(inputStream);
830 } catch (Exception e) {
831 return 0;
832 }
833 }
834
835 private int getRotation(final Uri image) {
836 try (final InputStream is = mXmppConnectionService.getContentResolver().openInputStream(image)) {
837 return is == null ? 0 : getRotation(is);
838 } catch (final Exception e) {
839 return 0;
840 }
841 }
842
843 private static int getRotation(final InputStream inputStream) throws IOException {
844 final ExifInterface exif = new ExifInterface(inputStream);
845 final int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
846 switch (orientation) {
847 case ExifInterface.ORIENTATION_ROTATE_180:
848 return 180;
849 case ExifInterface.ORIENTATION_ROTATE_90:
850 return 90;
851 case ExifInterface.ORIENTATION_ROTATE_270:
852 return 270;
853 default:
854 return 0;
855 }
856 }
857
858 public Bitmap getThumbnail(Message message, int size, boolean cacheOnly) throws IOException {
859 final String uuid = message.getUuid();
860 final LruCache<String, Bitmap> cache = mXmppConnectionService.getBitmapCache();
861 Bitmap thumbnail = cache.get(uuid);
862 if ((thumbnail == null) && (!cacheOnly)) {
863 synchronized (THUMBNAIL_LOCK) {
864 thumbnail = cache.get(uuid);
865 if (thumbnail != null) {
866 return thumbnail;
867 }
868 DownloadableFile file = getFile(message);
869 final String mime = file.getMimeType();
870 if ("application/pdf".equals(mime) && Compatibility.runsTwentyOne()) {
871 thumbnail = getPdfDocumentPreview(file, size);
872 } else if (mime.startsWith("video/")) {
873 thumbnail = getVideoPreview(file, size);
874 } else {
875 final Bitmap fullSize = getFullSizeImagePreview(file, size);
876 if (fullSize == null) {
877 throw new FileNotFoundException();
878 }
879 thumbnail = resize(fullSize, size);
880 thumbnail = rotate(thumbnail, getRotation(file));
881 if (mime.equals("image/gif")) {
882 Bitmap withGifOverlay = thumbnail.copy(Bitmap.Config.ARGB_8888, true);
883 drawOverlay(withGifOverlay, paintOverlayBlack(withGifOverlay) ? R.drawable.play_gif_black : R.drawable.play_gif_white, 1.0f);
884 thumbnail.recycle();
885 thumbnail = withGifOverlay;
886 }
887 }
888 cache.put(uuid, thumbnail);
889 }
890 }
891 return thumbnail;
892 }
893
894 private Bitmap getFullSizeImagePreview(File file, int size) {
895 BitmapFactory.Options options = new BitmapFactory.Options();
896 options.inSampleSize = calcSampleSize(file, size);
897 try {
898 return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
899 } catch (OutOfMemoryError e) {
900 options.inSampleSize *= 2;
901 return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
902 }
903 }
904
905 private void drawOverlay(Bitmap bitmap, int resource, float factor) {
906 Bitmap overlay = BitmapFactory.decodeResource(mXmppConnectionService.getResources(), resource);
907 Canvas canvas = new Canvas(bitmap);
908 float targetSize = Math.min(canvas.getWidth(), canvas.getHeight()) * factor;
909 Log.d(Config.LOGTAG, "target size overlay: " + targetSize + " overlay bitmap size was " + overlay.getHeight());
910 float left = (canvas.getWidth() - targetSize) / 2.0f;
911 float top = (canvas.getHeight() - targetSize) / 2.0f;
912 RectF dst = new RectF(left, top, left + targetSize - 1, top + targetSize - 1);
913 canvas.drawBitmap(overlay, null, dst, createAntiAliasingPaint());
914 }
915
916 /**
917 * https://stackoverflow.com/a/3943023/210897
918 */
919 private boolean paintOverlayBlack(final Bitmap bitmap) {
920 final int h = bitmap.getHeight();
921 final int w = bitmap.getWidth();
922 int record = 0;
923 for (int y = Math.round(h * IGNORE_PADDING); y < h - Math.round(h * IGNORE_PADDING); ++y) {
924 for (int x = Math.round(w * IGNORE_PADDING); x < w - Math.round(w * IGNORE_PADDING); ++x) {
925 int pixel = bitmap.getPixel(x, y);
926 if ((Color.red(pixel) * 0.299 + Color.green(pixel) * 0.587 + Color.blue(pixel) * 0.114) > 186) {
927 --record;
928 } else {
929 ++record;
930 }
931 }
932 }
933 return record < 0;
934 }
935
936 private boolean paintOverlayBlackPdf(final Bitmap bitmap) {
937 final int h = bitmap.getHeight();
938 final int w = bitmap.getWidth();
939 int white = 0;
940 for (int y = 0; y < h; ++y) {
941 for (int x = 0; x < w; ++x) {
942 int pixel = bitmap.getPixel(x, y);
943 if ((Color.red(pixel) * 0.299 + Color.green(pixel) * 0.587 + Color.blue(pixel) * 0.114) > 186) {
944 white++;
945 }
946 }
947 }
948 return white > (h * w * 0.4f);
949 }
950
951 private Bitmap cropCenterSquareVideo(Uri uri, int size) {
952 MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
953 Bitmap frame;
954 try {
955 metadataRetriever.setDataSource(mXmppConnectionService, uri);
956 frame = metadataRetriever.getFrameAtTime(0);
957 metadataRetriever.release();
958 return cropCenterSquare(frame, size);
959 } catch (Exception e) {
960 frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
961 frame.eraseColor(0xff000000);
962 return frame;
963 }
964 }
965
966 private Bitmap getVideoPreview(final File file, final int size) {
967 final MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
968 Bitmap frame;
969 try {
970 metadataRetriever.setDataSource(file.getAbsolutePath());
971 frame = metadataRetriever.getFrameAtTime(0);
972 metadataRetriever.release();
973 frame = resize(frame, size);
974 } catch (IOException | RuntimeException e) {
975 frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
976 frame.eraseColor(0xff000000);
977 }
978 drawOverlay(frame, paintOverlayBlack(frame) ? R.drawable.play_video_black : R.drawable.play_video_white, 0.75f);
979 return frame;
980 }
981
982 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
983 private Bitmap getPdfDocumentPreview(final File file, final int size) {
984 try {
985 final ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
986 final Bitmap rendered = renderPdfDocument(fileDescriptor, size, true);
987 drawOverlay(rendered, paintOverlayBlackPdf(rendered) ? R.drawable.open_pdf_black : R.drawable.open_pdf_white, 0.75f);
988 return rendered;
989 } catch (final IOException | SecurityException e) {
990 Log.d(Config.LOGTAG, "unable to render PDF document preview", e);
991 final Bitmap placeholder = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
992 placeholder.eraseColor(0xff000000);
993 return placeholder;
994 }
995 }
996
997
998 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
999 private Bitmap cropCenterSquarePdf(final Uri uri, final int size) {
1000 try {
1001 ParcelFileDescriptor fileDescriptor = mXmppConnectionService.getContentResolver().openFileDescriptor(uri, "r");
1002 final Bitmap bitmap = renderPdfDocument(fileDescriptor, size, false);
1003 return cropCenterSquare(bitmap, size);
1004 } catch (Exception e) {
1005 final Bitmap placeholder = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1006 placeholder.eraseColor(0xff000000);
1007 return placeholder;
1008 }
1009 }
1010
1011 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
1012 private Bitmap renderPdfDocument(ParcelFileDescriptor fileDescriptor, int targetSize, boolean fit) throws IOException {
1013 final PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
1014 final PdfRenderer.Page page = pdfRenderer.openPage(0);
1015 final Dimensions dimensions = scalePdfDimensions(new Dimensions(page.getHeight(), page.getWidth()), targetSize, fit);
1016 final Bitmap rendered = Bitmap.createBitmap(dimensions.width, dimensions.height, Bitmap.Config.ARGB_8888);
1017 rendered.eraseColor(0xffffffff);
1018 page.render(rendered, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
1019 page.close();
1020 pdfRenderer.close();
1021 fileDescriptor.close();
1022 return rendered;
1023 }
1024
1025 public Uri getTakePhotoUri() {
1026 File file;
1027 if (Config.ONLY_INTERNAL_STORAGE) {
1028 file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath(), "Camera/IMG_" + IMAGE_DATE_FORMAT.format(new Date()) + ".jpg");
1029 } else {
1030 file = new File(getTakePhotoPath() + "IMG_" + IMAGE_DATE_FORMAT.format(new Date()) + ".jpg");
1031 }
1032 file.getParentFile().mkdirs();
1033 return getUriForFile(mXmppConnectionService, file);
1034 }
1035
1036 public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
1037
1038 final Avatar uncompressAvatar = getUncompressedAvatar(image);
1039 if (uncompressAvatar != null && uncompressAvatar.image.length() <= Config.AVATAR_CHAR_LIMIT) {
1040 return uncompressAvatar;
1041 }
1042 if (uncompressAvatar != null) {
1043 Log.d(Config.LOGTAG, "uncompressed avatar exceeded char limit by " + (uncompressAvatar.image.length() - Config.AVATAR_CHAR_LIMIT));
1044 }
1045
1046 Bitmap bm = cropCenterSquare(image, size);
1047 if (bm == null) {
1048 return null;
1049 }
1050 if (hasAlpha(bm)) {
1051 Log.d(Config.LOGTAG, "alpha in avatar detected; uploading as PNG");
1052 bm.recycle();
1053 bm = cropCenterSquare(image, 96);
1054 return getPepAvatar(bm, Bitmap.CompressFormat.PNG, 100);
1055 }
1056 return getPepAvatar(bm, format, 100);
1057 }
1058
1059 private Avatar getUncompressedAvatar(Uri uri) {
1060 Bitmap bitmap = null;
1061 try {
1062 bitmap = BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(uri));
1063 return getPepAvatar(bitmap, Bitmap.CompressFormat.PNG, 100);
1064 } catch (Exception e) {
1065 return null;
1066 } finally {
1067 if (bitmap != null) {
1068 bitmap.recycle();
1069 }
1070 }
1071 }
1072
1073 private Avatar getPepAvatar(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
1074 try {
1075 ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
1076 Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
1077 MessageDigest digest = MessageDigest.getInstance("SHA-1");
1078 DigestOutputStream mDigestOutputStream = new DigestOutputStream(mBase64OutputStream, digest);
1079 if (!bitmap.compress(format, quality, mDigestOutputStream)) {
1080 return null;
1081 }
1082 mDigestOutputStream.flush();
1083 mDigestOutputStream.close();
1084 long chars = mByteArrayOutputStream.size();
1085 if (format != Bitmap.CompressFormat.PNG && quality >= 50 && chars >= Config.AVATAR_CHAR_LIMIT) {
1086 int q = quality - 2;
1087 Log.d(Config.LOGTAG, "avatar char length was " + chars + " reducing quality to " + q);
1088 return getPepAvatar(bitmap, format, q);
1089 }
1090 Log.d(Config.LOGTAG, "settled on char length " + chars + " with quality=" + quality);
1091 final Avatar avatar = new Avatar();
1092 avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
1093 avatar.image = new String(mByteArrayOutputStream.toByteArray());
1094 if (format.equals(Bitmap.CompressFormat.WEBP)) {
1095 avatar.type = "image/webp";
1096 } else if (format.equals(Bitmap.CompressFormat.JPEG)) {
1097 avatar.type = "image/jpeg";
1098 } else if (format.equals(Bitmap.CompressFormat.PNG)) {
1099 avatar.type = "image/png";
1100 }
1101 avatar.width = bitmap.getWidth();
1102 avatar.height = bitmap.getHeight();
1103 return avatar;
1104 } catch (OutOfMemoryError e) {
1105 Log.d(Config.LOGTAG, "unable to convert avatar to base64 due to low memory");
1106 return null;
1107 } catch (Exception e) {
1108 return null;
1109 }
1110 }
1111
1112 public Avatar getStoredPepAvatar(String hash) {
1113 if (hash == null) {
1114 return null;
1115 }
1116 Avatar avatar = new Avatar();
1117 final File file = getAvatarFile(hash);
1118 FileInputStream is = null;
1119 try {
1120 avatar.size = file.length();
1121 BitmapFactory.Options options = new BitmapFactory.Options();
1122 options.inJustDecodeBounds = true;
1123 BitmapFactory.decodeFile(file.getAbsolutePath(), options);
1124 is = new FileInputStream(file);
1125 ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
1126 Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
1127 MessageDigest digest = MessageDigest.getInstance("SHA-1");
1128 DigestOutputStream os = new DigestOutputStream(mBase64OutputStream, digest);
1129 byte[] buffer = new byte[4096];
1130 int length;
1131 while ((length = is.read(buffer)) > 0) {
1132 os.write(buffer, 0, length);
1133 }
1134 os.flush();
1135 os.close();
1136 avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
1137 avatar.image = new String(mByteArrayOutputStream.toByteArray());
1138 avatar.height = options.outHeight;
1139 avatar.width = options.outWidth;
1140 avatar.type = options.outMimeType;
1141 return avatar;
1142 } catch (NoSuchAlgorithmException | IOException e) {
1143 return null;
1144 } finally {
1145 close(is);
1146 }
1147 }
1148
1149 public boolean isAvatarCached(Avatar avatar) {
1150 final File file = getAvatarFile(avatar.getFilename());
1151 return file.exists();
1152 }
1153
1154 public boolean save(final Avatar avatar) {
1155 File file;
1156 if (isAvatarCached(avatar)) {
1157 file = getAvatarFile(avatar.getFilename());
1158 avatar.size = file.length();
1159 } else {
1160 file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString());
1161 if (file.getParentFile().mkdirs()) {
1162 Log.d(Config.LOGTAG, "created cache directory");
1163 }
1164 OutputStream os = null;
1165 try {
1166 if (!file.createNewFile()) {
1167 Log.d(Config.LOGTAG, "unable to create temporary file " + file.getAbsolutePath());
1168 }
1169 os = new FileOutputStream(file);
1170 MessageDigest digest = MessageDigest.getInstance("SHA-1");
1171 digest.reset();
1172 DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
1173 final byte[] bytes = avatar.getImageAsBytes();
1174 mDigestOutputStream.write(bytes);
1175 mDigestOutputStream.flush();
1176 mDigestOutputStream.close();
1177 String sha1sum = CryptoHelper.bytesToHex(digest.digest());
1178 if (sha1sum.equals(avatar.sha1sum)) {
1179 final File outputFile = getAvatarFile(avatar.getFilename());
1180 if (outputFile.getParentFile().mkdirs()) {
1181 Log.d(Config.LOGTAG, "created avatar directory");
1182 }
1183 final File avatarFile = getAvatarFile(avatar.getFilename());
1184 if (!file.renameTo(avatarFile)) {
1185 Log.d(Config.LOGTAG, "unable to rename " + file.getAbsolutePath() + " to " + outputFile);
1186 return false;
1187 }
1188 } else {
1189 Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
1190 if (!file.delete()) {
1191 Log.d(Config.LOGTAG, "unable to delete temporary file");
1192 }
1193 return false;
1194 }
1195 avatar.size = bytes.length;
1196 } catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
1197 return false;
1198 } finally {
1199 close(os);
1200 }
1201 }
1202 return true;
1203 }
1204
1205 public void deleteHistoricAvatarPath() {
1206 delete(getHistoricAvatarPath());
1207 }
1208
1209 private void delete(final File file) {
1210 if (file.isDirectory()) {
1211 final File[] files = file.listFiles();
1212 if (files != null) {
1213 for (final File f : files) {
1214 delete(f);
1215 }
1216 }
1217 }
1218 if (file.delete()) {
1219 Log.d(Config.LOGTAG, "deleted " + file.getAbsolutePath());
1220 }
1221 }
1222
1223 private File getHistoricAvatarPath() {
1224 return new File(mXmppConnectionService.getFilesDir(), "/avatars/");
1225 }
1226
1227 private File getAvatarFile(String avatar) {
1228 return new File(mXmppConnectionService.getCacheDir(), "/avatars/" + avatar);
1229 }
1230
1231 public Uri getAvatarUri(String avatar) {
1232 return Uri.fromFile(getAvatarFile(avatar));
1233 }
1234
1235 public Bitmap cropCenterSquare(Uri image, int size) {
1236 if (image == null) {
1237 return null;
1238 }
1239 InputStream is = null;
1240 try {
1241 BitmapFactory.Options options = new BitmapFactory.Options();
1242 options.inSampleSize = calcSampleSize(image, size);
1243 is = mXmppConnectionService.getContentResolver().openInputStream(image);
1244 if (is == null) {
1245 return null;
1246 }
1247 Bitmap input = BitmapFactory.decodeStream(is, null, options);
1248 if (input == null) {
1249 return null;
1250 } else {
1251 input = rotate(input, getRotation(image));
1252 return cropCenterSquare(input, size);
1253 }
1254 } catch (FileNotFoundException | SecurityException e) {
1255 Log.d(Config.LOGTAG, "unable to open file " + image.toString(), e);
1256 return null;
1257 } finally {
1258 close(is);
1259 }
1260 }
1261
1262 public Bitmap cropCenter(Uri image, int newHeight, int newWidth) {
1263 if (image == null) {
1264 return null;
1265 }
1266 InputStream is = null;
1267 try {
1268 BitmapFactory.Options options = new BitmapFactory.Options();
1269 options.inSampleSize = calcSampleSize(image, Math.max(newHeight, newWidth));
1270 is = mXmppConnectionService.getContentResolver().openInputStream(image);
1271 if (is == null) {
1272 return null;
1273 }
1274 Bitmap source = BitmapFactory.decodeStream(is, null, options);
1275 if (source == null) {
1276 return null;
1277 }
1278 int sourceWidth = source.getWidth();
1279 int sourceHeight = source.getHeight();
1280 float xScale = (float) newWidth / sourceWidth;
1281 float yScale = (float) newHeight / sourceHeight;
1282 float scale = Math.max(xScale, yScale);
1283 float scaledWidth = scale * sourceWidth;
1284 float scaledHeight = scale * sourceHeight;
1285 float left = (newWidth - scaledWidth) / 2;
1286 float top = (newHeight - scaledHeight) / 2;
1287
1288 RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
1289 Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
1290 Canvas canvas = new Canvas(dest);
1291 canvas.drawBitmap(source, null, targetRect, createAntiAliasingPaint());
1292 if (source.isRecycled()) {
1293 source.recycle();
1294 }
1295 return dest;
1296 } catch (SecurityException e) {
1297 return null; //android 6.0 with revoked permissions for example
1298 } catch (FileNotFoundException e) {
1299 return null;
1300 } finally {
1301 close(is);
1302 }
1303 }
1304
1305 public Bitmap cropCenterSquare(Bitmap input, int size) {
1306 int w = input.getWidth();
1307 int h = input.getHeight();
1308
1309 float scale = Math.max((float) size / h, (float) size / w);
1310
1311 float outWidth = scale * w;
1312 float outHeight = scale * h;
1313 float left = (size - outWidth) / 2;
1314 float top = (size - outHeight) / 2;
1315 RectF target = new RectF(left, top, left + outWidth, top + outHeight);
1316
1317 Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
1318 Canvas canvas = new Canvas(output);
1319 canvas.drawBitmap(input, null, target, createAntiAliasingPaint());
1320 if (!input.isRecycled()) {
1321 input.recycle();
1322 }
1323 return output;
1324 }
1325
1326 private int calcSampleSize(Uri image, int size) throws FileNotFoundException, SecurityException {
1327 final BitmapFactory.Options options = new BitmapFactory.Options();
1328 options.inJustDecodeBounds = true;
1329 final InputStream inputStream = mXmppConnectionService.getContentResolver().openInputStream(image);
1330 BitmapFactory.decodeStream(inputStream, null, options);
1331 close(inputStream);
1332 return calcSampleSize(options, size);
1333 }
1334
1335 public void updateFileParams(Message message) {
1336 updateFileParams(message, null);
1337 }
1338
1339 public void updateFileParams(Message message, String url) {
1340 DownloadableFile file = getFile(message);
1341 final String mime = file.getMimeType();
1342 final boolean privateMessage = message.isPrivateMessage();
1343 final boolean image = message.getType() == Message.TYPE_IMAGE || (mime != null && mime.startsWith("image/"));
1344 final boolean video = mime != null && mime.startsWith("video/");
1345 final boolean audio = mime != null && mime.startsWith("audio/");
1346 final boolean pdf = "application/pdf".equals(mime);
1347 final StringBuilder body = new StringBuilder();
1348 if (url != null) {
1349 body.append(url);
1350 }
1351 body.append('|').append(file.getSize());
1352 if (image || video || (pdf && Compatibility.runsTwentyOne())) {
1353 try {
1354 final Dimensions dimensions;
1355 if (video) {
1356 dimensions = getVideoDimensions(file);
1357 } else if (pdf && Compatibility.runsTwentyOne()) {
1358 dimensions = getPdfDocumentDimensions(file);
1359 } else {
1360 dimensions = getImageDimensions(file);
1361 }
1362 if (dimensions.valid()) {
1363 body.append('|').append(dimensions.width).append('|').append(dimensions.height);
1364 }
1365 } catch (NotAVideoFile notAVideoFile) {
1366 Log.d(Config.LOGTAG, "file with mime type " + file.getMimeType() + " was not a video file");
1367 //fall threw
1368 }
1369 } else if (audio) {
1370 body.append("|0|0|").append(getMediaRuntime(file));
1371 }
1372 message.setBody(body.toString());
1373 message.setDeleted(false);
1374 message.setType(privateMessage ? Message.TYPE_PRIVATE_FILE : (image ? Message.TYPE_IMAGE : Message.TYPE_FILE));
1375 }
1376
1377 private int getMediaRuntime(File file) {
1378 try {
1379 MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
1380 mediaMetadataRetriever.setDataSource(file.toString());
1381 return Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
1382 } catch (RuntimeException e) {
1383 return 0;
1384 }
1385 }
1386
1387 private Dimensions getImageDimensions(File file) {
1388 BitmapFactory.Options options = new BitmapFactory.Options();
1389 options.inJustDecodeBounds = true;
1390 BitmapFactory.decodeFile(file.getAbsolutePath(), options);
1391 int rotation = getRotation(file);
1392 boolean rotated = rotation == 90 || rotation == 270;
1393 int imageHeight = rotated ? options.outWidth : options.outHeight;
1394 int imageWidth = rotated ? options.outHeight : options.outWidth;
1395 return new Dimensions(imageHeight, imageWidth);
1396 }
1397
1398 private Dimensions getVideoDimensions(File file) throws NotAVideoFile {
1399 MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
1400 try {
1401 metadataRetriever.setDataSource(file.getAbsolutePath());
1402 } catch (RuntimeException e) {
1403 throw new NotAVideoFile(e);
1404 }
1405 return getVideoDimensions(metadataRetriever);
1406 }
1407
1408 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
1409 private Dimensions getPdfDocumentDimensions(final File file) {
1410 final ParcelFileDescriptor fileDescriptor;
1411 try {
1412 fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
1413 if (fileDescriptor == null) {
1414 return new Dimensions(0, 0);
1415 }
1416 } catch (FileNotFoundException e) {
1417 return new Dimensions(0, 0);
1418 }
1419 try {
1420 final PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
1421 final PdfRenderer.Page page = pdfRenderer.openPage(0);
1422 final int height = page.getHeight();
1423 final int width = page.getWidth();
1424 page.close();
1425 pdfRenderer.close();
1426 return scalePdfDimensions(new Dimensions(height, width));
1427 } catch (IOException | SecurityException e) {
1428 Log.d(Config.LOGTAG, "unable to get dimensions for pdf document", e);
1429 return new Dimensions(0, 0);
1430 }
1431 }
1432
1433 private Dimensions scalePdfDimensions(Dimensions in) {
1434 final DisplayMetrics displayMetrics = mXmppConnectionService.getResources().getDisplayMetrics();
1435 final int target = (int) (displayMetrics.density * 288);
1436 return scalePdfDimensions(in, target, true);
1437 }
1438
1439 private static Dimensions scalePdfDimensions(final Dimensions in, final int target, final boolean fit) {
1440 final int w, h;
1441 if (fit == (in.width <= in.height)) {
1442 w = Math.max((int) (in.width / ((double) in.height / target)), 1);
1443 h = target;
1444 } else {
1445 w = target;
1446 h = Math.max((int) (in.height / ((double) in.width / target)), 1);
1447 }
1448 return new Dimensions(h, w);
1449 }
1450
1451 public Bitmap getAvatar(String avatar, int size) {
1452 if (avatar == null) {
1453 return null;
1454 }
1455 Bitmap bm = cropCenter(getAvatarUri(avatar), size, size);
1456 return bm;
1457 }
1458
1459 private static class Dimensions {
1460 public final int width;
1461 public final int height;
1462
1463 Dimensions(int height, int width) {
1464 this.width = width;
1465 this.height = height;
1466 }
1467
1468 public int getMin() {
1469 return Math.min(width, height);
1470 }
1471
1472 public boolean valid() {
1473 return width > 0 && height > 0;
1474 }
1475 }
1476
1477 private static class NotAVideoFile extends Exception {
1478 public NotAVideoFile(Throwable t) {
1479 super(t);
1480 }
1481
1482 public NotAVideoFile() {
1483 super();
1484 }
1485 }
1486
1487 public static class ImageCompressionException extends Exception {
1488
1489 ImageCompressionException(String message) {
1490 super(message);
1491 }
1492 }
1493
1494
1495 public static class FileCopyException extends Exception {
1496 private final int resId;
1497
1498 private FileCopyException(@StringRes int resId) {
1499 this.resId = resId;
1500 }
1501
1502 public @StringRes
1503 int getResId() {
1504 return resId;
1505 }
1506 }
1507}