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