From dac12be53e18962c80fd471421adac29b5c92f06 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Fri, 14 Nov 2014 00:28:39 +0100 Subject: copy non local files to private storage first --- .../conversations/persistance/FileBackend.java | 32 ++++++++++++++- .../services/XmppConnectionService.java | 45 ++++++++++++++-------- .../conversations/ui/ConversationActivity.java | 23 +++++++++-- 3 files changed, 77 insertions(+), 23 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 2d054b5e..908cbefc 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -29,6 +29,8 @@ import android.provider.MediaStore; import android.util.Base64; import android.util.Base64OutputStream; import android.util.Log; +import android.webkit.MimeTypeMap; + import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.DownloadableFile; @@ -118,8 +120,8 @@ public class FileBackend { public String getOriginalPath(Uri uri) { String path = null; if (uri.getScheme().equals("file")) { - path = uri.getPath(); - } else { + return uri.getPath(); + } else if (uri.toString().startsWith("content://media/")) { String[] projection = {MediaStore.MediaColumns.DATA}; Cursor metaCursor = mXmppConnectionService.getContentResolver().query(uri, projection, null, null, null); @@ -136,6 +138,32 @@ public class FileBackend { return path; } + public DownloadableFile copyFileToPrivateStorage(Message message, Uri uri) throws FileCopyException { + try { + Log.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage"); + String mime = mXmppConnectionService.getContentResolver().getType(uri); + String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime); + message.setRelativeFilePath(message.getUuid() + "." + extension); + DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message); + OutputStream os = new FileOutputStream(file); + InputStream is = mXmppConnectionService.getContentResolver().openInputStream(uri); + byte[] buffer = new byte[1024]; + int length; + while ((length = is.read(buffer)) > 0) { + os.write(buffer, 0, length); + } + os.flush(); + os.close(); + is.close(); + Log.d(Config.LOGTAG, "output file name " + mXmppConnectionService.getFileBackend().getFile(message)); + return file; + } catch (FileNotFoundException e) { + throw new FileCopyException(R.string.error_file_not_found); + } catch (IOException e) { + throw new FileCopyException(R.string.error_io_exception); + } + } + public DownloadableFile copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException { return this.copyImageToPrivateStorage(message, image, 0); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index abcc92be..babcce26 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -295,29 +295,41 @@ public class XmppConnectionService extends Service { return this.mAvatarService; } - public Message attachFileToConversation(Conversation conversation, final Uri uri) { + public void attachFileToConversation(Conversation conversation, final Uri uri, final UiCallback callback) { + final Message message; + if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) { + message = new Message(conversation, "", + Message.ENCRYPTION_DECRYPTED); + } else { + message = new Message(conversation, "", + conversation.getNextEncryption(forceEncryption())); + } + message.setCounterpart(conversation.getNextCounterpart()); + message.setType(Message.TYPE_FILE); + message.setStatus(Message.STATUS_OFFERED); String path = getFileBackend().getOriginalPath(uri); if (path!=null) { - Log.d(Config.LOGTAG,"file path : "+path); - Message message; - if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) { - message = new Message(conversation, "", - Message.ENCRYPTION_DECRYPTED); - } else { - message = new Message(conversation, "", - conversation.getNextEncryption(forceEncryption())); - } - message.setCounterpart(conversation.getNextCounterpart()); - message.setType(Message.TYPE_FILE); - message.setStatus(Message.STATUS_OFFERED); message.setRelativeFilePath(path); getFileBackend().updateFileParams(message); - return message; + callback.success(message); + } else { + new Thread(new Runnable() { + @Override + public void run() { + try { + getFileBackend().copyFileToPrivateStorage(message, uri); + getFileBackend().updateFileParams(message); + callback.success(message); + } catch (FileBackend.FileCopyException e) { + callback.error(e.getResId(),message); + } + } + }).start(); + } - return null; } - public Message attachImageToConversation(final Conversation conversation, + public void attachImageToConversation(final Conversation conversation, final Uri uri, final UiCallback callback) { final Message message; if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) { @@ -346,7 +358,6 @@ public class XmppConnectionService extends Service { } } }).start(); - return message; } public Conversation find(Bookmark bookmark) { diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 8c2f9dab..9859d424 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -315,7 +315,9 @@ public class ConversationActivity extends XmppActivity implements startActivityForResult(chooser, REQUEST_ATTACH_IMAGE_DIALOG); } else if (attachmentChoice == ATTACHMENT_CHOICE_CHOOSE_FILE) { Intent attachFileIntent = new Intent(); - attachFileIntent.setType("file/*"); + //attachFileIntent.setType("file/*"); + attachFileIntent.setType("*/*"); + attachFileIntent.addCategory(Intent.CATEGORY_OPENABLE); attachFileIntent.setAction(Intent.ACTION_GET_CONTENT); Intent chooser = Intent.createChooser(attachFileIntent, getString(R.string.attach_file)); @@ -780,9 +782,22 @@ public class ConversationActivity extends XmppActivity implements } private void attachFileToConversation(Conversation conversation, Uri uri) { - Log.d(Config.LOGTAG, "attachFileToConversation"); - Message message = xmppConnectionService.attachFileToConversation(conversation,uri); - xmppConnectionService.sendMessage(message); + xmppConnectionService.attachFileToConversation(conversation,uri, new UiCallback() { + @Override + public void success(Message message) { + xmppConnectionService.sendMessage(message); + } + + @Override + public void error(int errorCode, Message message) { + + } + + @Override + public void userInputRequried(PendingIntent pi, Message message) { + + } + }); } private void attachImageToConversation(Conversation conversation, Uri uri) { -- cgit v1.2.3