1package eu.siacs.conversations.entities;
2
3import eu.siacs.conversations.R;
4import eu.siacs.conversations.xmpp.jingle.JingleConnection;
5import android.content.ContentValues;
6import android.content.Context;
7import android.database.Cursor;
8
9public class Message extends AbstractEntity {
10
11 public static final String TABLENAME = "messages";
12
13 public static final int STATUS_RECEPTION_FAILED = -3;
14 public static final int STATUS_RECEIVED_OFFER = -2;
15 public static final int STATUS_RECIEVING = -1;
16 public static final int STATUS_RECIEVED = 0;
17 public static final int STATUS_UNSEND = 1;
18 public static final int STATUS_SEND = 2;
19 public static final int STATUS_SEND_FAILED = 3;
20 public static final int STATUS_SEND_REJECTED = 4;
21 public static final int STATUS_WAITING = 5;
22 public static final int STATUS_OFFERED = 6;
23 public static final int STATUS_SEND_RECEIVED = 7;
24 public static final int STATUS_SEND_DISPLAYED = 8;
25
26 public static final int ENCRYPTION_NONE = 0;
27 public static final int ENCRYPTION_PGP = 1;
28 public static final int ENCRYPTION_OTR = 2;
29 public static final int ENCRYPTION_DECRYPTED = 3;
30 public static final int ENCRYPTION_DECRYPTION_FAILED = 4;
31
32 public static final int TYPE_TEXT = 0;
33 public static final int TYPE_IMAGE = 1;
34 public static final int TYPE_AUDIO = 2;
35 public static final int TYPE_STATUS = 3;
36
37 public static String CONVERSATION = "conversationUuid";
38 public static String COUNTERPART = "counterpart";
39 public static String TRUE_COUNTERPART = "trueCounterpart";
40 public static String BODY = "body";
41 public static String TIME_SENT = "timeSent";
42 public static String ENCRYPTION = "encryption";
43 public static String STATUS = "status";
44 public static String TYPE = "type";
45
46 protected String conversationUuid;
47 protected String counterpart;
48 protected String trueCounterpart;
49 protected String body;
50 protected String encryptedBody;
51 protected long timeSent;
52 protected int encryption;
53 protected int status;
54 protected int type;
55 protected boolean read = true;
56
57 protected transient Conversation conversation = null;
58
59 protected transient JingleConnection jingleConnection = null;
60
61 private Message() {
62
63 }
64
65 public Message(Conversation conversation, String body, int encryption) {
66 this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),
67 conversation.getContactJid(), null, body, System.currentTimeMillis(), encryption,
68 Message.STATUS_UNSEND,TYPE_TEXT);
69 this.conversation = conversation;
70 }
71
72 public Message(Conversation conversation, String counterpart, String body, int encryption, int status) {
73 this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),counterpart, null, body, System.currentTimeMillis(), encryption,status,TYPE_TEXT);
74 this.conversation = conversation;
75 }
76
77 public Message(String uuid, String conversationUUid, String counterpart, String trueCounterpart,
78 String body, long timeSent, int encryption, int status, int type) {
79 this.uuid = uuid;
80 this.conversationUuid = conversationUUid;
81 this.counterpart = counterpart;
82 this.trueCounterpart = trueCounterpart;
83 this.body = body;
84 this.timeSent = timeSent;
85 this.encryption = encryption;
86 this.status = status;
87 this.type = type;
88 }
89
90 @Override
91 public ContentValues getContentValues() {
92 ContentValues values = new ContentValues();
93 values.put(UUID, uuid);
94 values.put(CONVERSATION, conversationUuid);
95 values.put(COUNTERPART, counterpart);
96 values.put(TRUE_COUNTERPART,trueCounterpart);
97 values.put(BODY, body);
98 values.put(TIME_SENT, timeSent);
99 values.put(ENCRYPTION, encryption);
100 values.put(STATUS, status);
101 values.put(TYPE, type);
102 return values;
103 }
104
105 public String getConversationUuid() {
106 return conversationUuid;
107 }
108
109 public Conversation getConversation() {
110 return this.conversation;
111 }
112
113 public String getCounterpart() {
114 return counterpart;
115 }
116
117 public Contact getContact() {
118 if (this.conversation.getMode() == Conversation.MODE_SINGLE) {
119 return this.conversation.getContact();
120 } else {
121 if (this.trueCounterpart == null) {
122 return null;
123 } else {
124 Account account = this.conversation.getAccount();
125 Contact contact = account.getRoster().getContact(this.trueCounterpart);
126 if (contact.showInRoster()) {
127 return contact;
128 } else {
129 return null;
130 }
131 }
132 }
133 }
134
135 public String getBody() {
136 return body;
137 }
138
139 public String getReadableBody(Context context) {
140 if ((encryption == ENCRYPTION_PGP)&&(type == TYPE_TEXT)) {
141 return ""+context.getText(R.string.encrypted_message_received);
142 } else if ((encryption == ENCRYPTION_OTR)&&(type == TYPE_IMAGE)) {
143 return ""+context.getText(R.string.encrypted_image_received);
144 } else if (encryption == ENCRYPTION_DECRYPTION_FAILED) {
145 return ""+context.getText(R.string.decryption_failed);
146 } else if (type == TYPE_IMAGE) {
147 return ""+context.getText(R.string.image_file);
148 } else {
149 return body.trim();
150 }
151 }
152
153 public long getTimeSent() {
154 return timeSent;
155 }
156
157 public int getEncryption() {
158 return encryption;
159 }
160
161 public int getStatus() {
162 return status;
163 }
164
165 public static Message fromCursor(Cursor cursor) {
166 return new Message(cursor.getString(cursor.getColumnIndex(UUID)),
167 cursor.getString(cursor.getColumnIndex(CONVERSATION)),
168 cursor.getString(cursor.getColumnIndex(COUNTERPART)),
169 cursor.getString(cursor.getColumnIndex(TRUE_COUNTERPART)),
170 cursor.getString(cursor.getColumnIndex(BODY)),
171 cursor.getLong(cursor.getColumnIndex(TIME_SENT)),
172 cursor.getInt(cursor.getColumnIndex(ENCRYPTION)),
173 cursor.getInt(cursor.getColumnIndex(STATUS)),
174 cursor.getInt(cursor.getColumnIndex(TYPE)));
175 }
176
177 public void setConversation(Conversation conv) {
178 this.conversation = conv;
179 }
180
181 public void setStatus(int status) {
182 this.status = status;
183 }
184
185 public boolean isRead() {
186 return this.read;
187 }
188
189 public void markRead() {
190 this.read = true;
191 }
192
193 public void markUnread() {
194 this.read = false;
195 }
196
197 public void setTime(long time) {
198 this.timeSent = time;
199 }
200
201 public void setEncryption(int encryption) {
202 this.encryption = encryption;
203 }
204
205 public void setBody(String body) {
206 this.body = body;
207 }
208
209 public String getEncryptedBody() {
210 return this.encryptedBody;
211 }
212
213 public void setEncryptedBody(String body) {
214 this.encryptedBody = body;
215 }
216
217 public void setType(int type) {
218 this.type = type;
219 }
220
221 public int getType() {
222 return this.type;
223 }
224
225 public void setPresence(String presence) {
226 if (presence == null) {
227 this.counterpart = this.counterpart.split("/")[0];
228 } else {
229 this.counterpart = this.counterpart.split("/")[0] + "/" + presence;
230 }
231 }
232
233 public void setTrueCounterpart(String trueCounterpart) {
234 this.trueCounterpart = trueCounterpart;
235 }
236
237 public String getPresence() {
238 String[] counterparts = this.counterpart.split("/");
239 if (counterparts.length == 2) {
240 return counterparts[1];
241 } else {
242 return null;
243 }
244 }
245
246 public void setJingleConnection(JingleConnection connection) {
247 this.jingleConnection = connection;
248 }
249
250 public JingleConnection getJingleConnection() {
251 return this.jingleConnection;
252 }
253
254 public static Message createStatusMessage(Conversation conversation) {
255 Message message = new Message();
256 message.setType(Message.TYPE_STATUS);
257 message.setConversation(conversation);
258 return message;
259 }
260}