aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/services/ImageProvider.java
blob: 80cfbd95cfb728518061c3100516405e3d47344e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package eu.siacs.conversations.services;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

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 {
		ParcelFileDescriptor pfd;
		FileBackend fileBackend = new FileBackend(getContext());
		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];
			String messageUuid = uuidsSplited[2].split("\\.")[0];
			
			Log.d("xmppService","messageUuid="+messageUuid);
	
			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);
	
			File file = fileBackend.getJingleFile(message);
			pfd = ParcelFileDescriptor.open(file,
					ParcelFileDescriptor.MODE_READ_ONLY);
			return pfd;
		} else if ("w".equals(mode)){
			File file = fileBackend.getIncomingFile();
			try {
				file.createNewFile();
			} catch (IOException e) {
				throw new FileNotFoundException();
			}
			pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
			return pfd;
		} else {
			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;
	}
	
	public static Uri getContentUri(Message message) {
		return Uri
				.parse("content://eu.siacs.conversations.images/"
						+ message.getConversationUuid()
						+ "/"
						+ message.getUuid()
						+ ".webp");
	}

	public static Uri getIncomingContentUri() {
		return Uri.parse("content://eu.siacs.conversations.images/incoming");
	}
}