copy non local files to private storage first

This commit is contained in:
iNPUTmice 2014-11-14 00:28:39 +01:00
parent 02cbda68a7
commit dac12be53e
3 changed files with 77 additions and 23 deletions

View file

@ -29,6 +29,8 @@ import android.provider.MediaStore;
import android.util.Base64; import android.util.Base64;
import android.util.Base64OutputStream; import android.util.Base64OutputStream;
import android.util.Log; import android.util.Log;
import android.webkit.MimeTypeMap;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.DownloadableFile;
@ -118,8 +120,8 @@ public class FileBackend {
public String getOriginalPath(Uri uri) { public String getOriginalPath(Uri uri) {
String path = null; String path = null;
if (uri.getScheme().equals("file")) { if (uri.getScheme().equals("file")) {
path = uri.getPath(); return uri.getPath();
} else { } else if (uri.toString().startsWith("content://media/")) {
String[] projection = {MediaStore.MediaColumns.DATA}; String[] projection = {MediaStore.MediaColumns.DATA};
Cursor metaCursor = mXmppConnectionService.getContentResolver().query(uri, Cursor metaCursor = mXmppConnectionService.getContentResolver().query(uri,
projection, null, null, null); projection, null, null, null);
@ -136,6 +138,32 @@ public class FileBackend {
return path; 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) public DownloadableFile copyImageToPrivateStorage(Message message, Uri image)
throws FileCopyException { throws FileCopyException {
return this.copyImageToPrivateStorage(message, image, 0); return this.copyImageToPrivateStorage(message, image, 0);

View file

@ -295,29 +295,41 @@ public class XmppConnectionService extends Service {
return this.mAvatarService; return this.mAvatarService;
} }
public Message attachFileToConversation(Conversation conversation, final Uri uri) { public void attachFileToConversation(Conversation conversation, final Uri uri, final UiCallback<Message> 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); String path = getFileBackend().getOriginalPath(uri);
if (path!=null) { 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); message.setRelativeFilePath(path);
getFileBackend().updateFileParams(message); 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<Message> callback) { final Uri uri, final UiCallback<Message> callback) {
final Message message; final Message message;
if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) { if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
@ -346,7 +358,6 @@ public class XmppConnectionService extends Service {
} }
} }
}).start(); }).start();
return message;
} }
public Conversation find(Bookmark bookmark) { public Conversation find(Bookmark bookmark) {

View file

@ -315,7 +315,9 @@ public class ConversationActivity extends XmppActivity implements
startActivityForResult(chooser, REQUEST_ATTACH_IMAGE_DIALOG); startActivityForResult(chooser, REQUEST_ATTACH_IMAGE_DIALOG);
} else if (attachmentChoice == ATTACHMENT_CHOICE_CHOOSE_FILE) { } else if (attachmentChoice == ATTACHMENT_CHOICE_CHOOSE_FILE) {
Intent attachFileIntent = new Intent(); Intent attachFileIntent = new Intent();
attachFileIntent.setType("file/*"); //attachFileIntent.setType("file/*");
attachFileIntent.setType("*/*");
attachFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
attachFileIntent.setAction(Intent.ACTION_GET_CONTENT); attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent chooser = Intent.createChooser(attachFileIntent, Intent chooser = Intent.createChooser(attachFileIntent,
getString(R.string.attach_file)); getString(R.string.attach_file));
@ -780,9 +782,22 @@ public class ConversationActivity extends XmppActivity implements
} }
private void attachFileToConversation(Conversation conversation, Uri uri) { private void attachFileToConversation(Conversation conversation, Uri uri) {
Log.d(Config.LOGTAG, "attachFileToConversation"); xmppConnectionService.attachFileToConversation(conversation,uri, new UiCallback<Message>() {
Message message = xmppConnectionService.attachFileToConversation(conversation,uri); @Override
xmppConnectionService.sendMessage(message); 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) { private void attachImageToConversation(Conversation conversation, Uri uri) {