From 0a6eb6630612cda0ce6a9ec896d81281efd9631f Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Thu, 24 Jan 2019 20:51:43 +0100 Subject: put images into MessageStyle notifications --- .../messenger/crypto/PgpDecryptionService.java | 8 +++- .../messenger/http/HttpDownloadConnection.java | 10 +++-- .../pixart/messenger/persistance/FileBackend.java | 47 +++++++++++++++++++++- .../messenger/services/NotificationService.java | 25 +++++++++--- .../messenger/xmpp/jingle/JingleConnection.java | 4 +- 5 files changed, 79 insertions(+), 15 deletions(-) (limited to 'src/main/java/de') diff --git a/src/main/java/de/pixart/messenger/crypto/PgpDecryptionService.java b/src/main/java/de/pixart/messenger/crypto/PgpDecryptionService.java index 1c1a995a9..8f01c6982 100644 --- a/src/main/java/de/pixart/messenger/crypto/PgpDecryptionService.java +++ b/src/main/java/de/pixart/messenger/crypto/PgpDecryptionService.java @@ -135,6 +135,7 @@ public class PgpDecryptionService { } private void executeApi(Message message) { + boolean skipNotificationPush = false; synchronized (message) { Intent params = userInteractionResult != null ? userInteractionResult : new Intent(); params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); @@ -209,8 +210,9 @@ public class PgpDecryptionService { mXmppConnectionService.getFileBackend().updateFileParams(message, url); message.setEncryption(Message.ENCRYPTION_DECRYPTED); inputFile.delete(); - mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile); mXmppConnectionService.updateMessage(message); + skipNotificationPush = true; + mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile, () -> notifyIfPending(message)); break; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: synchronized (PgpDecryptionService.this) { @@ -231,7 +233,9 @@ public class PgpDecryptionService { } } } - notifyIfPending(message); + if (!skipNotificationPush) { + notifyIfPending(message); + } } private synchronized void notifyIfPending(Message message) { diff --git a/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java b/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java index cf867d796..b20e57e3f 100644 --- a/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java +++ b/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java @@ -146,7 +146,6 @@ public class HttpDownloadConnection implements Transferable { } private void finish() { - mXmppConnectionService.getFileBackend().updateMediaScanner(file); message.setTransferable(null); mHttpConnectionManager.finishConnection(this); boolean notify = acceptedAutomatically && !message.isRead(); @@ -154,9 +153,12 @@ public class HttpDownloadConnection implements Transferable { notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify); } mHttpConnectionManager.updateConversationUi(true); - if (notify) { - mXmppConnectionService.getNotificationService().push(message); - } + final boolean notifyAfterScan = notify; + mXmppConnectionService.getFileBackend().updateMediaScanner(file, () -> { + if (notifyAfterScan) { + mXmppConnectionService.getNotificationService().push(message); + } + }); } private void changeStatus(int status) { diff --git a/src/main/java/de/pixart/messenger/persistance/FileBackend.java b/src/main/java/de/pixart/messenger/persistance/FileBackend.java index 705e5489b..952bb7369 100644 --- a/src/main/java/de/pixart/messenger/persistance/FileBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/FileBackend.java @@ -15,6 +15,7 @@ import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.RectF; import android.media.MediaMetadataRetriever; +import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Build; import android.os.Environment; @@ -101,15 +102,57 @@ public class FileBackend { } } + public static Uri getMediaUri(Context context, File file) { + final String filePath = file.getAbsolutePath(); + final Cursor cursor = context.getContentResolver().query( + MediaStore.Images.Media.EXTERNAL_CONTENT_URI, + new String[]{MediaStore.Images.Media._ID}, + MediaStore.Images.Media.DATA + "=? ", + new String[]{filePath}, null); + if (cursor != null && cursor.moveToFirst()) { + final int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID)); + cursor.close(); + return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id)); + } else { + return null; + } + } + public void updateMediaScanner(File file) { + updateMediaScanner(file, null); + } + + public void updateMediaScanner(File file, final Runnable callback) { if (file.getAbsolutePath().startsWith(getConversationsDirectory("Images")) || file.getAbsolutePath().startsWith(getConversationsDirectory("Videos"))) { - Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); + MediaScannerConnection.scanFile(mXmppConnectionService, new String[]{file.getAbsolutePath()}, null, new MediaScannerConnection.MediaScannerConnectionClient() { + @Override + public void onMediaScannerConnected() { + + } + + @Override + public void onScanCompleted(String path, Uri uri) { + if (callback != null && file.getAbsolutePath().equals(path)) { + callback.run(); + } else { + Log.d(Config.LOGTAG, "media scanner scanned wrong file"); + if (callback != null) { + callback.run(); + } + } + } + }); + return; + /*Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); intent.setData(Uri.fromFile(file)); - mXmppConnectionService.sendBroadcast(intent); + mXmppConnectionService.sendBroadcast(intent);*/ } else { createNoMedia(); } + if (callback != null) { + callback.run(); + } } public boolean deleteFile(File file) { diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index 5e165567f..095abdee1 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -531,7 +531,7 @@ public class NotificationService { } else { Message message; //TODO starting with Android 9 we might want to put images in MessageStyle - if ((message = getImage(messages)) != null) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P && (message = getImage(messages)) != null) { modifyForImage(mBuilder, mUnreadBuilder, message, messages); } else { modifyForTextOnly(mBuilder, mUnreadBuilder, messages); @@ -667,7 +667,16 @@ public class NotificationService { } for (Message message : messages) { final Person sender = message.getStatus() == Message.STATUS_RECEIVED ? getPerson(message) : null; - messagingStyle.addMessage(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && isImageMessage(message)) { + final Uri dataUri = FileBackend.getMediaUri(mXmppConnectionService, mXmppConnectionService.getFileBackend().getFile(message)); + NotificationCompat.MessagingStyle.Message imageMessage = new NotificationCompat.MessagingStyle.Message(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender); + if (dataUri != null) { + imageMessage.setData(message.getMimeType(), dataUri); + } + messagingStyle.addMessage(imageMessage); + } else { + messagingStyle.addMessage(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender); + } } messagingStyle.setGroupConversation(multiple); builder.setStyle(messagingStyle); @@ -714,16 +723,20 @@ public class NotificationService { if (message.getStatus() != Message.STATUS_RECEIVED) { return null; } - if (message.getType() != Message.TYPE_TEXT - && message.getTransferable() == null - && message.getEncryption() != Message.ENCRYPTION_PGP - && message.getFileParams().height > 0) { + if (isImageMessage(message)) { image = message; } } return image; } + private static boolean isImageMessage(Message message) { + return message.getType() != Message.TYPE_TEXT + && message.getTransferable() == null + && message.getEncryption() != Message.ENCRYPTION_PGP + && message.getFileParams().height > 0; + } + private Message getFirstDownloadableMessage(final Iterable messages) { for (final Message message : messages) { if (message.getTransferable() != null || (message.getType() == Message.TYPE_TEXT && message.treatAsDownloadable())) { diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java index 2f3ec09ea..85fdc536f 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java @@ -117,8 +117,10 @@ public class JingleConnection implements Transferable { if (message.getEncryption() == Message.ENCRYPTION_PGP) { account.getPgpDecryptionService().decrypt(message, true); } else { - JingleConnection.this.mXmppConnectionService.getNotificationService().push(message); + mXmppConnectionService.getFileBackend().updateMediaScanner(file, () -> JingleConnection.this.mXmppConnectionService.getNotificationService().push(message)); } + Log.d(Config.LOGTAG, "successfully transmitted file:" + file.getAbsolutePath() + " (" + CryptoHelper.bytesToHex(file.getSha1Sum()) + ")"); + return; } } else { if (ftVersion == Content.Version.FT_5) { //older Conversations will break when receiving a session-info -- cgit v1.2.3