1package eu.siacs.conversations.services;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5
6import eu.siacs.conversations.entities.Account;
7import eu.siacs.conversations.entities.Conversation;
8import eu.siacs.conversations.entities.Message;
9import eu.siacs.conversations.persistance.DatabaseBackend;
10import eu.siacs.conversations.persistance.FileBackend;
11
12import android.content.ContentProvider;
13import android.content.ContentValues;
14import android.database.Cursor;
15import android.net.Uri;
16import android.os.ParcelFileDescriptor;
17import android.util.Log;
18
19public class ImageProvider extends ContentProvider {
20
21 @Override
22 public ParcelFileDescriptor openFile(Uri uri, String mode)
23 throws FileNotFoundException {
24 DatabaseBackend databaseBackend = DatabaseBackend
25 .getInstance(getContext());
26 FileBackend fileBackend = new FileBackend(getContext());
27 String uuids = uri.getPath();
28 Log.d("xmppService", "uuids = " + uuids);
29 if (uuids == null) {
30 throw new FileNotFoundException();
31 }
32 String[] uuidsSplited = uuids.split("/");
33 if (uuidsSplited.length != 3) {
34 throw new FileNotFoundException();
35 }
36 String conversationUuid = uuidsSplited[1];
37 String messageUuid = uuidsSplited[2];
38
39 Conversation conversation = databaseBackend
40 .findConversationByUuid(conversationUuid);
41 if (conversation == null) {
42 throw new FileNotFoundException("conversation " + conversationUuid
43 + " could not be found");
44 }
45 Message message = databaseBackend.findMessageByUuid(messageUuid);
46 if (message == null) {
47 throw new FileNotFoundException("message " + messageUuid
48 + " could not be found");
49 }
50
51 Account account = databaseBackend.findAccountByUuid(conversation
52 .getAccountUuid());
53 if (account == null) {
54 throw new FileNotFoundException("account "
55 + conversation.getAccountUuid() + " cound not be found");
56 }
57 message.setConversation(conversation);
58 conversation.setAccount(account);
59
60 File file = fileBackend.getJingleFile(message);
61 ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file,
62 ParcelFileDescriptor.MODE_READ_ONLY);
63 return pfd;
64 }
65
66 @Override
67 public int delete(Uri arg0, String arg1, String[] arg2) {
68 return 0;
69 }
70
71 @Override
72 public String getType(Uri arg0) {
73 return null;
74 }
75
76 @Override
77 public Uri insert(Uri arg0, ContentValues arg1) {
78 return null;
79 }
80
81 @Override
82 public boolean onCreate() {
83 return false;
84 }
85
86 @Override
87 public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
88 String arg4) {
89 return null;
90 }
91
92 @Override
93 public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
94 return 0;
95 }
96
97 public static Uri getContentUri(Message message) {
98 return Uri
99 .parse("content://eu.siacs.conversations.images/"
100 + message.getConversationUuid()
101 + "/"
102 + message.getUuid());
103 }
104
105}