1package eu.siacs.conversations.services;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6
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("xmppService", "uuids = " + uuids+" mode="+mode);
31 if (uuids == null) {
32 throw new FileNotFoundException();
33 }
34 String[] uuidsSplited = uuids.split("/");
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("xmppService","messageUuid="+messageUuid);
42
43 Conversation conversation = databaseBackend
44 .findConversationByUuid(conversationUuid);
45 if (conversation == null) {
46 throw new FileNotFoundException("conversation " + conversationUuid
47 + " 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 if ("w".equals(mode)){
69 File file = fileBackend.getIncomingFile();
70 try {
71 file.createNewFile();
72 } catch (IOException e) {
73 throw new FileNotFoundException();
74 }
75 pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
76 return pfd;
77 } else {
78 throw new FileNotFoundException();
79 }
80 }
81
82 @Override
83 public int delete(Uri arg0, String arg1, String[] arg2) {
84 return 0;
85 }
86
87 @Override
88 public String getType(Uri arg0) {
89 return null;
90 }
91
92 @Override
93 public Uri insert(Uri arg0, ContentValues arg1) {
94 return null;
95 }
96
97 @Override
98 public boolean onCreate() {
99 return false;
100 }
101
102 @Override
103 public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
104 String arg4) {
105 return null;
106 }
107
108 @Override
109 public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
110 return 0;
111 }
112
113 public static Uri getProviderUri(Message message) {
114 return Uri
115 .parse("content://eu.siacs.conversations.images/"
116 + message.getConversationUuid()
117 + "/"
118 + message.getUuid()
119 + ".webp");
120 }
121
122 public static Uri getIncomingContentUri() {
123 return Uri.parse("content://eu.siacs.conversations.images/incoming");
124 }
125}