2014-04-21 17:51:03 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
2014-05-13 14:48:39 +00:00
|
|
|
import java.io.IOException;
|
2014-04-21 17:51:03 +00:00
|
|
|
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
|
|
|
import eu.siacs.conversations.persistance.DatabaseBackend;
|
|
|
|
import eu.siacs.conversations.persistance.FileBackend;
|
|
|
|
import android.content.ContentProvider;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.ParcelFileDescriptor;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
public class ImageProvider extends ContentProvider {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ParcelFileDescriptor openFile(Uri uri, String mode)
|
|
|
|
throws FileNotFoundException {
|
2014-05-13 14:48:39 +00:00
|
|
|
ParcelFileDescriptor pfd;
|
2014-04-21 17:51:03 +00:00
|
|
|
FileBackend fileBackend = new FileBackend(getContext());
|
2014-05-13 14:48:39 +00:00
|
|
|
if ("r".equals(mode)) {
|
|
|
|
DatabaseBackend databaseBackend = DatabaseBackend
|
|
|
|
.getInstance(getContext());
|
|
|
|
String uuids = uri.getPath();
|
|
|
|
Log.d("xmppService", "uuids = " + uuids+" mode="+mode);
|
|
|
|
if (uuids == null) {
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
|
|
|
String[] uuidsSplited = uuids.split("/");
|
|
|
|
if (uuidsSplited.length != 3) {
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
|
|
|
String conversationUuid = uuidsSplited[1];
|
2014-06-04 20:35:08 +00:00
|
|
|
String messageUuid = uuidsSplited[2].split("\\.")[0];
|
|
|
|
|
|
|
|
Log.d("xmppService","messageUuid="+messageUuid);
|
2014-05-13 14:48:39 +00:00
|
|
|
|
|
|
|
Conversation conversation = databaseBackend
|
|
|
|
.findConversationByUuid(conversationUuid);
|
|
|
|
if (conversation == null) {
|
|
|
|
throw new FileNotFoundException("conversation " + conversationUuid
|
|
|
|
+ " could not be found");
|
|
|
|
}
|
|
|
|
Message message = databaseBackend.findMessageByUuid(messageUuid);
|
|
|
|
if (message == null) {
|
|
|
|
throw new FileNotFoundException("message " + messageUuid
|
|
|
|
+ " could not be found");
|
|
|
|
}
|
|
|
|
|
|
|
|
Account account = databaseBackend.findAccountByUuid(conversation
|
|
|
|
.getAccountUuid());
|
|
|
|
if (account == null) {
|
|
|
|
throw new FileNotFoundException("account "
|
|
|
|
+ conversation.getAccountUuid() + " cound not be found");
|
|
|
|
}
|
|
|
|
message.setConversation(conversation);
|
|
|
|
conversation.setAccount(account);
|
|
|
|
|
2014-08-08 09:49:23 +00:00
|
|
|
File file = fileBackend.getJingleFileLegacy(message);
|
2014-05-13 14:48:39 +00:00
|
|
|
pfd = ParcelFileDescriptor.open(file,
|
|
|
|
ParcelFileDescriptor.MODE_READ_ONLY);
|
|
|
|
return pfd;
|
|
|
|
} else {
|
2014-04-21 17:51:03 +00:00
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int delete(Uri arg0, String arg1, String[] arg2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getType(Uri arg0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Uri insert(Uri arg0, ContentValues arg1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
|
|
|
|
String arg4) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-13 13:37:11 +00:00
|
|
|
|
2014-08-08 09:49:23 +00:00
|
|
|
public static Uri getProviderUri(Message message) {
|
2014-05-13 13:37:11 +00:00
|
|
|
return Uri
|
|
|
|
.parse("content://eu.siacs.conversations.images/"
|
|
|
|
+ message.getConversationUuid()
|
|
|
|
+ "/"
|
2014-06-04 20:35:08 +00:00
|
|
|
+ message.getUuid()
|
|
|
|
+ ".webp");
|
2014-05-13 13:37:11 +00:00
|
|
|
}
|
2014-05-13 14:48:39 +00:00
|
|
|
}
|