From d54243489000c0c7b065ff94409e5de62140913d Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Fri, 14 Nov 2014 12:31:57 +0100 Subject: some mime and pgp fixes for file transfer --- .../eu/siacs/conversations/crypto/PgpEngine.java | 21 +------- .../conversations/entities/DownloadableFile.java | 14 ++--- .../conversations/persistance/FileBackend.java | 2 +- .../conversations/ui/ConversationActivity.java | 24 +++++---- .../conversations/ui/adapter/MessageAdapter.java | 61 ++++++++++------------ .../xmpp/jingle/JingleConnection.java | 21 +++++++- src/main/res/values/strings.xml | 1 + 7 files changed, 71 insertions(+), 73 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java b/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java index 3f84daf6..83d9b7b2 100644 --- a/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java +++ b/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java @@ -3,7 +3,6 @@ package eu.siacs.conversations.crypto; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -24,7 +23,6 @@ import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.ui.UiCallback; import android.app.PendingIntent; import android.content.Intent; -import android.graphics.BitmapFactory; import android.net.Uri; public class PgpEngine { @@ -98,24 +96,7 @@ public class PgpEngine { OpenPgpApi.RESULT_CODE_ERROR)) { case OpenPgpApi.RESULT_CODE_SUCCESS: URL url = message.getImageParams().url; - BitmapFactory.Options options = new BitmapFactory.Options(); - options.inJustDecodeBounds = true; - BitmapFactory.decodeFile( - outputFile.getAbsolutePath(), options); - int imageHeight = options.outHeight; - int imageWidth = options.outWidth; - if (url == null) { - message.setBody(Long.toString(outputFile - .getSize()) - + '|' - + imageWidth - + '|' - + imageHeight); - } else { - message.setBody(url.toString() + "|" - + Long.toString(outputFile.getSize()) - + '|' + imageWidth + '|' + imageHeight); - } + mXmppConnectionService.getFileBackend().updateFileParams(message,url); message.setEncryption(Message.ENCRYPTION_DECRYPTED); PgpEngine.this.mXmppConnectionService .updateMessage(message); diff --git a/src/main/java/eu/siacs/conversations/entities/DownloadableFile.java b/src/main/java/eu/siacs/conversations/entities/DownloadableFile.java index 6aff643a..25f33907 100644 --- a/src/main/java/eu/siacs/conversations/entities/DownloadableFile.java +++ b/src/main/java/eu/siacs/conversations/entities/DownloadableFile.java @@ -55,13 +55,15 @@ public class DownloadableFile extends File { } public String getMimeType() { - if (mime==null) { - mime = URLConnection.guessContentTypeFromName(this.getAbsolutePath()); - if (mime == null) { - mime = ""; - } + String path = this.getAbsolutePath(); + String mime = URLConnection.guessContentTypeFromName(path); + if (mime != null) { + return mime; + } else if (mime == null && path.endsWith(".webp")) { + return "image/webp"; + } else { + return ""; } - return mime; } public void setExpectedSize(long size) { diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 3d771572..9683d38d 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -62,7 +62,7 @@ public class FileBackend { if (path != null && !path.isEmpty()) { String[] parts = path.split("\\."); extension = "."+parts[parts.length - 1]; - } else if (message.getType() == Message.TYPE_IMAGE) { + } else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_TEXT) { extension = ".webp"; } else { extension = ""; diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 9859d424..7e28cc6c 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -15,7 +15,6 @@ import android.os.SystemClock; import android.provider.MediaStore; import android.support.v4.widget.SlidingPaneLayout; import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener; -import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; @@ -32,7 +31,6 @@ import android.widget.Toast; import java.util.ArrayList; import java.util.List; -import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; @@ -80,7 +78,7 @@ public class ConversationActivity extends XmppActivity implements private ArrayAdapter listAdapter; - private Toast prepareImageToast; + private Toast prepareFileToast; public List getConversationList() { @@ -782,15 +780,19 @@ public class ConversationActivity extends XmppActivity implements } private void attachFileToConversation(Conversation conversation, Uri uri) { + prepareFileToast = Toast.makeText(getApplicationContext(), + getText(R.string.preparing_file), Toast.LENGTH_LONG); + prepareFileToast.show(); xmppConnectionService.attachFileToConversation(conversation,uri, new UiCallback() { @Override public void success(Message message) { + hidePrepareFileToast(); xmppConnectionService.sendMessage(message); } @Override public void error(int errorCode, Message message) { - + displayErrorDialog(errorCode); } @Override @@ -801,16 +803,16 @@ public class ConversationActivity extends XmppActivity implements } private void attachImageToConversation(Conversation conversation, Uri uri) { - prepareImageToast = Toast.makeText(getApplicationContext(), + prepareFileToast = Toast.makeText(getApplicationContext(), getText(R.string.preparing_image), Toast.LENGTH_LONG); - prepareImageToast.show(); + prepareFileToast.show(); xmppConnectionService.attachImageToConversation(conversation, uri, new UiCallback() { @Override public void userInputRequried(PendingIntent pi, Message object) { - hidePrepareImageToast(); + hidePrepareFileToast(); ConversationActivity.this.runIntent(pi, ConversationActivity.REQUEST_SEND_PGP_IMAGE); } @@ -822,19 +824,19 @@ public class ConversationActivity extends XmppActivity implements @Override public void error(int error, Message message) { - hidePrepareImageToast(); + hidePrepareFileToast(); displayErrorDialog(error); } }); } - private void hidePrepareImageToast() { - if (prepareImageToast != null) { + private void hidePrepareFileToast() { + if (prepareFileToast != null) { runOnUiThread(new Runnable() { @Override public void run() { - prepareImageToast.cancel(); + prepareFileToast.cancel(); } }); } diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index 81eccacc..71795e98 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -281,15 +281,16 @@ public class MessageAdapter extends ArrayAdapter { } private void displayOpenableMessage(ViewHolder viewHolder,final Message message) { + final DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message); viewHolder.image.setVisibility(View.GONE); viewHolder.messageBody.setVisibility(View.GONE); viewHolder.download_button.setVisibility(View.VISIBLE); - viewHolder.download_button.setText(activity.getString(R.string.open_file,activity.xmppConnectionService.getFileBackend().getFile(message).getMimeType())); + viewHolder.download_button.setText(activity.getString(R.string.open_file,file.getMimeType())); viewHolder.download_button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - openDonwloadable(message); + openDonwloadable(file); } }); viewHolder.download_button.setOnLongClickListener(openContextMenu); @@ -506,43 +507,35 @@ public class MessageAdapter extends ArrayAdapter { } else if (d.getStatus() == Downloadable.STATUS_FAILED) { displayInfoMessage(viewHolder, activity.getString(R.string.image_transmission_failed)); } - } else if (item.getType() == Message.TYPE_IMAGE) { - if (item.getEncryption() == Message.ENCRYPTION_PGP) { - displayInfoMessage(viewHolder,activity.getString(R.string.encrypted_message)); - } else if (item.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) { - displayDecryptionFailed(viewHolder); - } else { - displayImageMessage(viewHolder, item); - } - } else if (item.getType() == Message.TYPE_FILE) { + } else if (item.getType() == Message.TYPE_IMAGE && item.getEncryption() != Message.ENCRYPTION_PGP && item.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) { + displayImageMessage(viewHolder, item); + } else if (item.getType() == Message.TYPE_FILE && item.getEncryption() != Message.ENCRYPTION_PGP && item.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) { if (item.getImageParams().width > 0) { displayImageMessage(viewHolder,item); } else { displayOpenableMessage(viewHolder, item); } - } else { - if (item.getEncryption() == Message.ENCRYPTION_PGP) { - if (activity.hasPgp()) { - displayInfoMessage(viewHolder,activity.getString(R.string.encrypted_message)); - } else { - displayInfoMessage(viewHolder, - activity.getString(R.string.install_openkeychain)); - if (viewHolder != null) { - viewHolder.message_box - .setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - activity.showInstallPgpDialog(); - } - }); - } - } - } else if (item.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) { - displayDecryptionFailed(viewHolder); + } else if (item.getEncryption() == Message.ENCRYPTION_PGP) { + if (activity.hasPgp()) { + displayInfoMessage(viewHolder,activity.getString(R.string.encrypted_message)); } else { - displayTextMessage(viewHolder, item); + displayInfoMessage(viewHolder, + activity.getString(R.string.install_openkeychain)); + if (viewHolder != null) { + viewHolder.message_box + .setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + activity.showInstallPgpDialog(); + } + }); + } } + } else if (item.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) { + displayDecryptionFailed(viewHolder); + } else { + displayTextMessage(viewHolder, item); } displayStatus(viewHolder, item); @@ -560,8 +553,8 @@ public class MessageAdapter extends ArrayAdapter { } } - public void openDonwloadable(Message message) { - DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message); + public void openDonwloadable(DownloadableFile file) { + Log.d(Config.LOGTAG,"file "+file.getAbsolutePath()); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), file.getMimeType()); getContext().startActivity(intent); diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index 793c6c54..0c79d101 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -1,5 +1,6 @@ package eu.siacs.conversations.xmpp.jingle; +import java.net.URLConnection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -97,6 +98,9 @@ public class JingleConnection implements Downloadable { Message.STATUS_RECEIVED); } else { message.setDownloadable(null); + if (message.getEncryption() != Message.ENCRYPTION_PGP) { + file.delete(); + } } Log.d(Config.LOGTAG, "sucessfully transmitted file:" + file.getAbsolutePath()); @@ -922,6 +926,21 @@ public class JingleConnection implements Downloadable { @Override public String getMimeType() { - return this.file.getMimeType(); + if (this.message.getType() == Message.TYPE_FILE) { + String mime = null; + String path = this.message.getRelativeFilePath(); + if (path != null && !this.message.getRelativeFilePath().isEmpty()) { + mime = URLConnection.guessContentTypeFromName(this.message.getRelativeFilePath()); + if (mime!=null) { + return mime; + } else { + return ""; + } + } else { + return ""; + } + } else { + return "image/webp"; + } } } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 6ad79b26..287829f5 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -316,4 +316,5 @@ Download %s file Open %s file sending (%1$d%% completed) + Preparing file for transmission -- cgit v1.2.3