put images into MessageStyle notifications

This commit is contained in:
Daniel Gultsch 2019-01-09 12:47:09 +01:00
parent c4539b8583
commit 96e3546904
5 changed files with 80 additions and 15 deletions

View file

@ -135,6 +135,7 @@ public class PgpDecryptionService {
} }
private void executeApi(Message message) { private void executeApi(Message message) {
boolean skipNotificationPush = false;
synchronized (message) { synchronized (message) {
Intent params = userInteractionResult != null ? userInteractionResult : new Intent(); Intent params = userInteractionResult != null ? userInteractionResult : new Intent();
params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
@ -209,8 +210,9 @@ public class PgpDecryptionService {
mXmppConnectionService.getFileBackend().updateFileParams(message, url); mXmppConnectionService.getFileBackend().updateFileParams(message, url);
message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setEncryption(Message.ENCRYPTION_DECRYPTED);
inputFile.delete(); inputFile.delete();
mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile);
mXmppConnectionService.updateMessage(message); mXmppConnectionService.updateMessage(message);
skipNotificationPush = true;
mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile, () -> notifyIfPending(message));
break; break;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
synchronized (PgpDecryptionService.this) { synchronized (PgpDecryptionService.this) {
@ -231,8 +233,10 @@ public class PgpDecryptionService {
} }
} }
} }
if (!skipNotificationPush) {
notifyIfPending(message); notifyIfPending(message);
} }
}
private synchronized void notifyIfPending(Message message) { private synchronized void notifyIfPending(Message message) {
if (pendingNotifications.remove(message)) { if (pendingNotifications.remove(message)) {

View file

@ -140,7 +140,6 @@ public class HttpDownloadConnection implements Transferable {
} }
private void finish() { private void finish() {
mXmppConnectionService.getFileBackend().updateMediaScanner(file);
message.setTransferable(null); message.setTransferable(null);
mHttpConnectionManager.finishConnection(this); mHttpConnectionManager.finishConnection(this);
boolean notify = acceptedAutomatically && !message.isRead(); boolean notify = acceptedAutomatically && !message.isRead();
@ -148,9 +147,12 @@ public class HttpDownloadConnection implements Transferable {
notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify); notify = message.getConversation().getAccount().getPgpDecryptionService().decrypt(message, notify);
} }
mHttpConnectionManager.updateConversationUi(true); mHttpConnectionManager.updateConversationUi(true);
if (notify) { final boolean notifyAfterScan = notify;
mXmppConnectionService.getFileBackend().updateMediaScanner(file, () -> {
if (notifyAfterScan) {
mXmppConnectionService.getNotificationService().push(message); mXmppConnectionService.getNotificationService().push(message);
} }
});
} }
private void changeStatus(int status) { private void changeStatus(int status) {

View file

@ -13,6 +13,7 @@ import android.graphics.Matrix;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.RectF; import android.graphics.RectF;
import android.media.MediaMetadataRetriever; import android.media.MediaMetadataRetriever;
import android.media.MediaScannerConnection;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
@ -406,14 +407,56 @@ 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) { public void updateMediaScanner(File file) {
updateMediaScanner(file, null);
}
public void updateMediaScanner(File file, final Runnable callback) {
if (!isInDirectoryThatShouldNotBeScanned(mXmppConnectionService, file)) { if (!isInDirectoryThatShouldNotBeScanned(mXmppConnectionService, file)) {
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)); intent.setData(Uri.fromFile(file));
mXmppConnectionService.sendBroadcast(intent); mXmppConnectionService.sendBroadcast(intent);*/
} else if (file.getAbsolutePath().startsWith(getAppMediaDirectory(mXmppConnectionService))) { } else if (file.getAbsolutePath().startsWith(getAppMediaDirectory(mXmppConnectionService))) {
createNoMedia(file.getParentFile()); createNoMedia(file.getParentFile());
} }
if (callback != null) {
callback.run();
}
} }
public boolean deleteFile(Message message) { public boolean deleteFile(Message message) {

View file

@ -519,7 +519,7 @@ public class NotificationService {
} else { } else {
Message message; Message message;
//TODO starting with Android 9 we might want to put images in MessageStyle //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); modifyForImage(mBuilder, mUnreadBuilder, message, messages);
} else { } else {
modifyForTextOnly(mBuilder, mUnreadBuilder, messages); modifyForTextOnly(mBuilder, mUnreadBuilder, messages);
@ -656,8 +656,17 @@ public class NotificationService {
} }
for (Message message : messages) { for (Message message : messages) {
final Person sender = message.getStatus() == Message.STATUS_RECEIVED ? getPerson(message) : null; final Person sender = message.getStatus() == Message.STATUS_RECEIVED ? getPerson(message) : null;
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.addMessage(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender);
} }
}
messagingStyle.setGroupConversation(multiple); messagingStyle.setGroupConversation(multiple);
builder.setStyle(messagingStyle); builder.setStyle(messagingStyle);
} else { } else {
@ -703,16 +712,20 @@ public class NotificationService {
if (message.getStatus() != Message.STATUS_RECEIVED) { if (message.getStatus() != Message.STATUS_RECEIVED) {
return null; return null;
} }
if (message.getType() != Message.TYPE_TEXT if (isImageMessage(message)) {
&& message.getTransferable() == null
&& message.getEncryption() != Message.ENCRYPTION_PGP
&& message.getFileParams().height > 0) {
image = message; image = message;
} }
} }
return image; 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<Message> messages) { private Message getFirstDownloadableMessage(final Iterable<Message> messages) {
for (final Message message : messages) { for (final Message message : messages) {
if (message.getTransferable() != null || (message.getType() == Message.TYPE_TEXT && message.treatAsDownloadable())) { if (message.getTransferable() != null || (message.getType() == Message.TYPE_TEXT && message.treatAsDownloadable())) {

View file

@ -122,8 +122,11 @@ public class JingleConnection implements Transferable {
if (message.getEncryption() == Message.ENCRYPTION_PGP) { if (message.getEncryption() == Message.ENCRYPTION_PGP) {
account.getPgpDecryptionService().decrypt(message, true); account.getPgpDecryptionService().decrypt(message, true);
} else { } 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 { } else {
if (ftVersion == Content.Version.FT_5) { //older Conversations will break when receiving a session-info if (ftVersion == Content.Version.FT_5) { //older Conversations will break when receiving a session-info