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 public static final int TYPE_PRIVATE = 4;
37
38 public static String CONVERSATION = "conversationUuid";
39 public static String COUNTERPART = "counterpart";
40 public static String TRUE_COUNTERPART = "trueCounterpart";
41 public static String BODY = "body";
42 public static String TIME_SENT = "timeSent";
43 public static String ENCRYPTION = "encryption";
44 public static String STATUS = "status";
45 public static String TYPE = "type";
46 public static String REMOTE_MSG_ID = "remoteMsgId";
47
48 protected String conversationUuid;
49 protected String counterpart;
50 protected String trueCounterpart;
51 protected String body;
52 protected String encryptedBody;
53 protected long timeSent;
54 protected int encryption;
55 protected int status;
56 protected int type;
57 protected boolean read = true;
58 protected String remoteMsgId = null;
59
60 protected transient Conversation conversation = null;
61
62 protected transient JingleConnection jingleConnection = null;
63
64 private Message() {
65
66 }
67
68 public Message(Conversation conversation, String body, int encryption) {
69 this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),
70 conversation.getContactJid(), null, body, System.currentTimeMillis(), encryption,
71 Message.STATUS_UNSEND,TYPE_TEXT,null);
72 this.conversation = conversation;
73 }
74
75 public Message(Conversation conversation, String counterpart, String body, int encryption, int status) {
76 this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),counterpart, null, body, System.currentTimeMillis(), encryption,status,TYPE_TEXT,null);
77 this.conversation = conversation;
78 }
79
80 public Message(String uuid, String conversationUUid, String counterpart, String trueCounterpart,
81 String body, long timeSent, int encryption, int status, int type, String remoteMsgId) {
82 this.uuid = uuid;
83 this.conversationUuid = conversationUUid;
84 this.counterpart = counterpart;
85 this.trueCounterpart = trueCounterpart;
86 this.body = body;
87 this.timeSent = timeSent;
88 this.encryption = encryption;
89 this.status = status;
90 this.type = type;
91 this.remoteMsgId = remoteMsgId;
92 }
93
94 @Override
95 public ContentValues getContentValues() {
96 ContentValues values = new ContentValues();
97 values.put(UUID, uuid);
98 values.put(CONVERSATION, conversationUuid);
99 values.put(COUNTERPART, counterpart);
100 values.put(TRUE_COUNTERPART,trueCounterpart);
101 values.put(BODY, body);
102 values.put(TIME_SENT, timeSent);
103 values.put(ENCRYPTION, encryption);
104 values.put(STATUS, status);
105 values.put(TYPE, type);
106 values.put(REMOTE_MSG_ID,remoteMsgId);
107 return values;
108 }
109
110 public String getConversationUuid() {
111 return conversationUuid;
112 }
113
114 public Conversation getConversation() {
115 return this.conversation;
116 }
117
118 public String getCounterpart() {
119 return counterpart;
120 }
121
122 public Contact getContact() {
123 if (this.conversation.getMode() == Conversation.MODE_SINGLE) {
124 return this.conversation.getContact();
125 } else {
126 if (this.trueCounterpart == null) {
127 return null;
128 } else {
129 Account account = this.conversation.getAccount();
130 Contact contact = account.getRoster().getContact(this.trueCounterpart);
131 if (contact.showInRoster()) {
132 return contact;
133 } else {
134 return null;
135 }
136 }
137 }
138 }
139
140 public String getBody() {
141 return body;
142 }
143
144 public String getReadableBody(Context context) {
145 if ((encryption == ENCRYPTION_PGP)&&(type == TYPE_TEXT)) {
146 return ""+context.getText(R.string.encrypted_message_received);
147 } else if ((encryption == ENCRYPTION_OTR)&&(type == TYPE_IMAGE)) {
148 return ""+context.getText(R.string.encrypted_image_received);
149 } else if (encryption == ENCRYPTION_DECRYPTION_FAILED) {
150 return ""+context.getText(R.string.decryption_failed);
151 } else if (type == TYPE_IMAGE) {
152 return ""+context.getText(R.string.image_file);
153 } else {
154 return body.trim();
155 }
156 }
157
158 public long getTimeSent() {
159 return timeSent;
160 }
161
162 public int getEncryption() {
163 return encryption;
164 }
165
166 public int getStatus() {
167 return status;
168 }
169
170 public String getRemoteMsgId() {
171 return this.remoteMsgId;
172 }
173
174 public void setRemoteMsgId(String id) {
175 this.remoteMsgId = id;
176 }
177
178 public static Message fromCursor(Cursor cursor) {
179 return new Message(cursor.getString(cursor.getColumnIndex(UUID)),
180 cursor.getString(cursor.getColumnIndex(CONVERSATION)),
181 cursor.getString(cursor.getColumnIndex(COUNTERPART)),
182 cursor.getString(cursor.getColumnIndex(TRUE_COUNTERPART)),
183 cursor.getString(cursor.getColumnIndex(BODY)),
184 cursor.getLong(cursor.getColumnIndex(TIME_SENT)),
185 cursor.getInt(cursor.getColumnIndex(ENCRYPTION)),
186 cursor.getInt(cursor.getColumnIndex(STATUS)),
187 cursor.getInt(cursor.getColumnIndex(TYPE)),
188 cursor.getString(cursor.getColumnIndex(REMOTE_MSG_ID)));
189 }
190
191 public void setConversation(Conversation conv) {
192 this.conversation = conv;
193 }
194
195 public void setStatus(int status) {
196 this.status = status;
197 }
198
199 public boolean isRead() {
200 return this.read;
201 }
202
203 public void markRead() {
204 this.read = true;
205 }
206
207 public void markUnread() {
208 this.read = false;
209 }
210
211 public void setTime(long time) {
212 this.timeSent = time;
213 }
214
215 public void setEncryption(int encryption) {
216 this.encryption = encryption;
217 }
218
219 public void setBody(String body) {
220 this.body = body;
221 }
222
223 public String getEncryptedBody() {
224 return this.encryptedBody;
225 }
226
227 public void setEncryptedBody(String body) {
228 this.encryptedBody = body;
229 }
230
231 public void setType(int type) {
232 this.type = type;
233 }
234
235 public int getType() {
236 return this.type;
237 }
238
239 public void setPresence(String presence) {
240 if (presence == null || presence.isEmpty()) {
241 this.counterpart = this.counterpart.split("/")[0];
242 } else {
243 this.counterpart = this.counterpart.split("/")[0] + "/" + presence;
244 }
245 }
246
247 public void setTrueCounterpart(String trueCounterpart) {
248 this.trueCounterpart = trueCounterpart;
249 }
250
251 public String getPresence() {
252 String[] counterparts = this.counterpart.split("/");
253 if (counterparts.length == 2) {
254 return counterparts[1];
255 } else {
256 return null;
257 }
258 }
259
260 public void setJingleConnection(JingleConnection connection) {
261 this.jingleConnection = connection;
262 }
263
264 public JingleConnection getJingleConnection() {
265 return this.jingleConnection;
266 }
267
268 public static Message createStatusMessage(Conversation conversation) {
269 Message message = new Message();
270 message.setType(Message.TYPE_STATUS);
271 message.setConversation(conversation);
272 return message;
273 }
274
275 public void setCounterpart(String counterpart) {
276 this.counterpart = counterpart;
277 }
278}