diff options
author | Christian Schneppe <kriztan@users.noreply.github.com> | 2018-09-27 18:42:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-27 18:42:25 +0200 |
commit | 4f4a52f068f3b3956aca5934531a48d1f90d7974 (patch) | |
tree | da24e0f840730a75f2b3dab69a3b5f12025391be /src/main/java/de/pixart/messenger/persistance/FileBackend.java | |
parent | 1816092e68584b5794303578b6af6d621ffc58d4 (diff) | |
parent | f4ac1f68feac3e3b19ab5932ede9d79dd14bff27 (diff) |
Merge branch 'master' into bubble-into-avatar
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/persistance/FileBackend.java | 124 |
1 files changed, 90 insertions, 34 deletions
diff --git a/src/main/java/de/pixart/messenger/persistance/FileBackend.java b/src/main/java/de/pixart/messenger/persistance/FileBackend.java index 0c3028feb..822fa6318 100644 --- a/src/main/java/de/pixart/messenger/persistance/FileBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/FileBackend.java @@ -46,6 +46,7 @@ import java.security.DigestOutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; @@ -56,6 +57,7 @@ import de.pixart.messenger.R; import de.pixart.messenger.entities.DownloadableFile; import de.pixart.messenger.entities.Message; import de.pixart.messenger.services.XmppConnectionService; +import de.pixart.messenger.ui.util.Attachment; import de.pixart.messenger.utils.CryptoHelper; import de.pixart.messenger.utils.ExifHelper; import de.pixart.messenger.utils.FileUtils; @@ -80,8 +82,8 @@ public class FileBackend { } private void createNoMedia() { - final File nomedia_files = new File(getConversationsDirectory("Files", true) + ".nomedia"); - final File nomedia_audios = new File(getConversationsDirectory("Audios", true) + ".nomedia"); + final File nomedia_files = new File(getConversationsDirectory("Files") + ".nomedia"); + final File nomedia_audios = new File(getConversationsDirectory("Audios") + ".nomedia"); if (!nomedia_files.exists()) { try { nomedia_files.createNewFile(); @@ -99,8 +101,8 @@ public class FileBackend { } public void updateMediaScanner(File file) { - if (file.getAbsolutePath().startsWith(getConversationsDirectory("Images", true)) - || file.getAbsolutePath().startsWith(getConversationsDirectory("Videos", true))) { + if (file.getAbsolutePath().startsWith(getConversationsDirectory("Images")) + || file.getAbsolutePath().startsWith(getConversationsDirectory("Videos"))) { Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); intent.setData(Uri.fromFile(file)); mXmppConnectionService.sendBroadcast(intent); @@ -129,13 +131,13 @@ public class FileBackend { file = new DownloadableFile(path); } else { if (mime != null && mime.startsWith("image")) { - file = new DownloadableFile(getConversationsDirectory("Images", true) + path); + file = new DownloadableFile(getConversationsDirectory("Images") + path); } else if (mime != null && mime.startsWith("video")) { - file = new DownloadableFile(getConversationsDirectory("Videos", true) + path); + file = new DownloadableFile(getConversationsDirectory("Videos") + path); } else if (mime != null && mime.startsWith("audio")) { - file = new DownloadableFile(getConversationsDirectory("Audios", true) + path); + file = new DownloadableFile(getConversationsDirectory("Audios") + path); } else { - file = new DownloadableFile(getConversationsDirectory("Files", true) + path); + file = new DownloadableFile(getConversationsDirectory("Files") + path); } } return file; @@ -151,7 +153,7 @@ public class FileBackend { } final DownloadableFile file = getFileForPath(path, message.getMimeType()); if (encrypted) { - return new DownloadableFile(getConversationsDirectory("Files", true) + file.getName() + ".pgp"); + return new DownloadableFile(getConversationsDirectory("Files") + file.getName() + ".pgp"); } else { return file; } @@ -172,16 +174,19 @@ public class FileBackend { } } - public static boolean allFilesUnderSize(Context context, List<Uri> uris, long max) { + public static boolean allFilesUnderSize(Context context, List<Attachment> attachments, long max) { if (max <= 0) { Log.d(Config.LOGTAG, "server did not report max file size for http upload"); return true; //exception to be compatible with HTTP Upload < v0.2 } - for (Uri uri : uris) { - String mime = context.getContentResolver().getType(uri); + for (Attachment attachment : attachments) { + if (attachment.getType() != Attachment.Type.FILE) { + continue; + } + String mime = attachment.getMime(); if (mime != null && mime.startsWith("video/")) { try { - Dimensions dimensions = FileBackend.getVideoDimensions(context, uri); + Dimensions dimensions = FileBackend.getVideoDimensions(context, attachment.getUri()); if (dimensions.getMin() >= 720) { Log.d(Config.LOGTAG, "do not consider video file with min width larger or equal than 720 for size check"); continue; @@ -190,7 +195,7 @@ public class FileBackend { //ignore and fall through } } - if (FileBackend.getFileSize(context, uri) > max) { + if (FileBackend.getFileSize(context, attachment.getUri()) > max) { Log.d(Config.LOGTAG, "not all files are under " + max + " bytes. suggesting falling back to jingle"); return false; } @@ -198,31 +203,43 @@ public class FileBackend { return true; } - public static String getDirectoryName(final String type, final boolean isMedia) { - String media = ""; - if (isMedia) { - media = "Media/Pix-Art Messenger "; + public List<Attachment> convertToAttachments(List<DatabaseBackend.FilePath> relativeFilePaths) { + List<Attachment> attachments = new ArrayList<>(); + for (DatabaseBackend.FilePath relativeFilePath : relativeFilePaths) { + final String mime = MimeUtils.guessMimeTypeFromExtension(MimeUtils.extractRelevantExtension(relativeFilePath.path)); + Log.d(Config.LOGTAG, "mime=" + mime); + File file = getFileForPath(relativeFilePath.path, mime); + if (file.exists()) { + attachments.add(Attachment.of(relativeFilePath.uuid, file, mime)); + } else { + Log.d(Config.LOGTAG, "file " + file.getAbsolutePath() + " doesn't exist"); + } } - if (type == "null" || type == null) { - return "/Pix-Art Messenger/"; + return attachments; + } + + public static String getConversationsDirectory(final String type) { + if (type.equalsIgnoreCase("null") || type == null) { + return getAppMediaDirectory() + "Pix-Art Messenger" + "/"; } else { - return "/Pix-Art Messenger" + "/" + media + type + "/"; + return getAppMediaDirectory() + "Pix-Art Messenger" + " " + type + "/"; } } - public static String getConversationsDirectory(final String type, final boolean isMedia) { - String DirName = null; - if (type != "null" || type != null) { - DirName = type; - } - String path = Environment.getExternalStorageDirectory().getAbsolutePath() + getDirectoryName(DirName, isMedia); - File createFolders = new File(path); - if (!createFolders.exists()) { - Log.d(Config.LOGTAG, "creating directory " + createFolders); - createFolders.mkdirs(); - } - return path; + public static String getAppMediaDirectory() { + return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Pix-Art Messenger" + "/Media/"; + } + public static String getBackupDirectory() { + return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Pix-Art Messenger" + "/Database/"; + } + + public static String getAppLogsDirectory() { + return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Pix-Art Messenger" + "/Chats/"; + } + + public static String getAppUpdateDirectory() { + return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Pix-Art Messenger" + "/Update/"; } private Bitmap resize(final Bitmap originalBitmap, int size) throws IOException { @@ -294,7 +311,7 @@ public class FileBackend { Log.d(Config.LOGTAG, "File path = null"); return false; } - if (path.contains(getDirectoryName("null", true))) { + if (path.contains(getConversationsDirectory("null"))) { Log.d(Config.LOGTAG, "File " + path + " is in our directory"); return true; } @@ -554,6 +571,21 @@ public class FileBackend { return paint; } + private Bitmap cropCenterSquareVideo(Uri uri, int size) { + MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever(); + Bitmap frame; + try { + metadataRetriever.setDataSource(mXmppConnectionService, uri); + frame = metadataRetriever.getFrameAtTime(0); + metadataRetriever.release(); + return cropCenterSquare(frame, size); + } catch (Exception e) { + frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); + frame.eraseColor(0xff000000); + return frame; + } + } + private Bitmap getVideoPreview(File file, int size) throws IOException { MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever(); Bitmap frame; @@ -1041,6 +1073,30 @@ public class FileBackend { return getVideoDimensions(metadataRetriever); } + public Bitmap getPreviewForUri(Attachment attachment, int size, boolean cacheOnly) { + final String key = "attachment_" + attachment.getUuid().toString() + "_" + String.valueOf(size); + final LruCache<String, Bitmap> cache = mXmppConnectionService.getBitmapCache(); + Bitmap bitmap = cache.get(key); + if (bitmap != null || cacheOnly) { + return bitmap; + } + if (attachment.getMime() != null && attachment.getMime().startsWith("video/")) { + bitmap = cropCenterSquareVideo(attachment.getUri(), size); + drawOverlay(bitmap, R.drawable.play_video, 0.75f); + } else { + bitmap = cropCenterSquare(attachment.getUri(), size); + if ("image/gif".equals(attachment.getMime())) { + Bitmap withGifOverlay = bitmap.copy(Bitmap.Config.ARGB_8888, true); + drawOverlay(withGifOverlay, R.drawable.play_gif, 1.0f); + bitmap.recycle(); + bitmap = withGifOverlay; + } + } + if (bitmap != null) { + cache.put(key, bitmap); + } + return bitmap; + } private static Dimensions getVideoDimensions(Context context, Uri uri) throws NotAVideoFile { MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); |