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];
40
41 Conversation conversation = databaseBackend
42 .findConversationByUuid(conversationUuid);
43 if (conversation == null) {
44 throw new FileNotFoundException("conversation " + conversationUuid
45 + " could not be found");
46 }
47 Message message = databaseBackend.findMessageByUuid(messageUuid);
48 if (message == null) {
49 throw new FileNotFoundException("message " + messageUuid
50 + " could not be found");
51 }
52
53 Account account = databaseBackend.findAccountByUuid(conversation
54 .getAccountUuid());
55 if (account == null) {
56 throw new FileNotFoundException("account "
57 + conversation.getAccountUuid() + " cound not be found");
58 }
59 message.setConversation(conversation);
60 conversation.setAccount(account);
61
62 File file = fileBackend.getJingleFile(message);
63 pfd = ParcelFileDescriptor.open(file,
64 ParcelFileDescriptor.MODE_READ_ONLY);
65 return pfd;
66 } else if ("w".equals(mode)){
67 File file = fileBackend.getIncomingFile();
68 try {
69 file.createNewFile();
70 } catch (IOException e) {
71 throw new FileNotFoundException();
72 }
73 pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
74 return pfd;
75 } else {
76 throw new FileNotFoundException();
77 }
78 }
79
80 @Override
81 public int delete(Uri arg0, String arg1, String[] arg2) {
82 return 0;
83 }
84
85 @Override
86 public String getType(Uri arg0) {
87 return null;
88 }
89
90 @Override
91 public Uri insert(Uri arg0, ContentValues arg1) {
92 return null;
93 }
94
95 @Override
96 public boolean onCreate() {
97 return false;
98 }
99
100 @Override
101 public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
102 String arg4) {
103 return null;
104 }
105
106 @Override
107 public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
108 return 0;
109 }
110
111 public static Uri getContentUri(Message message) {
112 return Uri
113 .parse("content://eu.siacs.conversations.images/"
114 + message.getConversationUuid()
115 + "/"
116 + message.getUuid());
117 }
118
119 public static Uri getIncomingContentUri() {
120 return Uri.parse("content://eu.siacs.conversations.images/incoming");
121 }
122}