Message.java

  1package eu.siacs.conversations.entities;
  2
  3import eu.siacs.conversations.Config;
  4import eu.siacs.conversations.R;
  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_RECEIVING = -1;
 16	public static final int STATUS_RECEIVED = 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 Downloadable downloadable = 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
 71						.currentTimeMillis(), encryption,
 72				Message.STATUS_UNSEND, TYPE_TEXT, null);
 73		this.conversation = conversation;
 74	}
 75
 76	public Message(Conversation conversation, String counterpart, String body,
 77			int encryption, int status) {
 78		this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),
 79				counterpart, null, body, System.currentTimeMillis(),
 80				encryption, status, TYPE_TEXT, null);
 81		this.conversation = conversation;
 82	}
 83
 84	public Message(String uuid, String conversationUUid, String counterpart,
 85			String trueCounterpart, String body, long timeSent, int encryption,
 86			int status, int type, String remoteMsgId) {
 87		this.uuid = uuid;
 88		this.conversationUuid = conversationUUid;
 89		this.counterpart = counterpart;
 90		this.trueCounterpart = trueCounterpart;
 91		this.body = body;
 92		this.timeSent = timeSent;
 93		this.encryption = encryption;
 94		this.status = status;
 95		this.type = type;
 96		this.remoteMsgId = remoteMsgId;
 97	}
 98
 99	@Override
100	public ContentValues getContentValues() {
101		ContentValues values = new ContentValues();
102		values.put(UUID, uuid);
103		values.put(CONVERSATION, conversationUuid);
104		values.put(COUNTERPART, counterpart);
105		values.put(TRUE_COUNTERPART, trueCounterpart);
106		values.put(BODY, body);
107		values.put(TIME_SENT, timeSent);
108		values.put(ENCRYPTION, encryption);
109		values.put(STATUS, status);
110		values.put(TYPE, type);
111		values.put(REMOTE_MSG_ID, remoteMsgId);
112		return values;
113	}
114
115	public String getConversationUuid() {
116		return conversationUuid;
117	}
118
119	public Conversation getConversation() {
120		return this.conversation;
121	}
122
123	public String getCounterpart() {
124		return counterpart;
125	}
126
127	public Contact getContact() {
128		if (this.conversation.getMode() == Conversation.MODE_SINGLE) {
129			return this.conversation.getContact();
130		} else {
131			if (this.trueCounterpart == null) {
132				return null;
133			} else {
134				Account account = this.conversation.getAccount();
135				Contact contact = account.getRoster().getContact(
136						this.trueCounterpart);
137				if (contact.showInRoster()) {
138					return contact;
139				} else {
140					return null;
141				}
142			}
143		}
144	}
145
146	public String getBody() {
147		return body;
148	}
149
150	public String getReadableBody(Context context) {
151		if ((encryption == ENCRYPTION_PGP) && (type == TYPE_TEXT)) {
152			return context.getText(R.string.encrypted_message_received).toString();
153		} else if ((encryption == ENCRYPTION_OTR) && (type == TYPE_IMAGE)) {
154			return context.getText(R.string.encrypted_image_received).toString();
155		} else if (encryption == ENCRYPTION_DECRYPTION_FAILED) {
156			return context.getText(R.string.decryption_failed).toString();
157		} else if (type == TYPE_IMAGE) {
158			return context.getText(R.string.image_file).toString();
159		} else {
160			return body.trim();
161		}
162	}
163
164	public long getTimeSent() {
165		return timeSent;
166	}
167
168	public int getEncryption() {
169		return encryption;
170	}
171
172	public int getStatus() {
173		return status;
174	}
175
176	public String getRemoteMsgId() {
177		return this.remoteMsgId;
178	}
179
180	public void setRemoteMsgId(String id) {
181		this.remoteMsgId = id;
182	}
183
184	public static Message fromCursor(Cursor cursor) {
185		return new Message(cursor.getString(cursor.getColumnIndex(UUID)),
186				cursor.getString(cursor.getColumnIndex(CONVERSATION)),
187				cursor.getString(cursor.getColumnIndex(COUNTERPART)),
188				cursor.getString(cursor.getColumnIndex(TRUE_COUNTERPART)),
189				cursor.getString(cursor.getColumnIndex(BODY)),
190				cursor.getLong(cursor.getColumnIndex(TIME_SENT)),
191				cursor.getInt(cursor.getColumnIndex(ENCRYPTION)),
192				cursor.getInt(cursor.getColumnIndex(STATUS)),
193				cursor.getInt(cursor.getColumnIndex(TYPE)),
194				cursor.getString(cursor.getColumnIndex(REMOTE_MSG_ID)));
195	}
196
197	public void setConversation(Conversation conv) {
198		this.conversation = conv;
199	}
200
201	public void setStatus(int status) {
202		this.status = status;
203	}
204
205	public boolean isRead() {
206		return this.read;
207	}
208
209	public void markRead() {
210		this.read = true;
211	}
212
213	public void markUnread() {
214		this.read = false;
215	}
216
217	public void setTime(long time) {
218		this.timeSent = time;
219	}
220
221	public void setEncryption(int encryption) {
222		this.encryption = encryption;
223	}
224
225	public void setBody(String body) {
226		this.body = body;
227	}
228
229	public String getEncryptedBody() {
230		return this.encryptedBody;
231	}
232
233	public void setEncryptedBody(String body) {
234		this.encryptedBody = body;
235	}
236
237	public void setType(int type) {
238		this.type = type;
239	}
240
241	public int getType() {
242		return this.type;
243	}
244
245	public void setPresence(String presence) {
246		if (presence == null) {
247			this.counterpart = this.counterpart.split("/")[0];
248		} else {
249			this.counterpart = this.counterpart.split("/")[0] + "/" + presence;
250		}
251	}
252
253	public void setTrueCounterpart(String trueCounterpart) {
254		this.trueCounterpart = trueCounterpart;
255	}
256
257	public String getPresence() {
258		String[] counterparts = this.counterpart.split("/");
259		if (counterparts.length == 2) {
260			return counterparts[1];
261		} else {
262			if (this.counterpart.contains("/")) {
263				return "";
264			} else {
265				return null;
266			}
267		}
268	}
269
270	public void setDownloadable(Downloadable downloadable) {
271		this.downloadable = downloadable;
272	}
273
274	public Downloadable getDownloadable() {
275		return this.downloadable;
276	}
277
278	public static Message createStatusMessage(Conversation conversation) {
279		Message message = new Message();
280		message.setType(Message.TYPE_STATUS);
281		message.setConversation(conversation);
282		return message;
283	}
284
285	public void setCounterpart(String counterpart) {
286		this.counterpart = counterpart;
287	}
288
289	public boolean equals(Message message) {
290		if ((this.remoteMsgId != null) && (this.body != null)
291				&& (this.counterpart != null)) {
292			return this.remoteMsgId.equals(message.getRemoteMsgId())
293					&& this.body.equals(message.getBody())
294					&& this.counterpart.equals(message.getCounterpart());
295		} else {
296			return false;
297		}
298	}
299
300	public Message next() {
301		int index = this.conversation.getMessages().indexOf(this);
302		if (index < 0 || index >= this.conversation.getMessages().size() - 1) {
303			return null;
304		} else {
305			return this.conversation.getMessages().get(index + 1);
306		}
307	}
308
309	public Message prev() {
310		int index = this.conversation.getMessages().indexOf(this);
311		if (index <= 0 || index > this.conversation.getMessages().size()) {
312			return null;
313		} else {
314			return this.conversation.getMessages().get(index - 1);
315		}
316	}
317
318	public boolean mergable(Message message) {
319		if (message == null) {
320			return false;
321		}
322		return (message.getType() == Message.TYPE_TEXT
323				&& message.getEncryption() != Message.ENCRYPTION_PGP
324				&& this.getType() == message.getType()
325				&& this.getEncryption() == message.getEncryption()
326				&& this.getCounterpart().equals(message.getCounterpart())
327				&& (message.getTimeSent() - this.getTimeSent()) <= (Config.MESSAGE_MERGE_WINDOW * 1000) && ((this
328				.getStatus() == message.getStatus()) || ((this.getStatus() == Message.STATUS_SEND || this
329				.getStatus() == Message.STATUS_SEND_RECEIVED) && (message
330				.getStatus() == Message.STATUS_UNSEND
331				|| message.getStatus() == Message.STATUS_SEND || message
332					.getStatus() == Message.STATUS_SEND_DISPLAYED))));
333	}
334
335	public String getMergedBody() {
336		Message next = this.next();
337		if (this.mergable(next)) {
338			return body.trim() + '\n' + next.getMergedBody();
339		}
340		return body.trim();
341	}
342	
343	public int getMergedStatus() {
344		Message next = this.next();
345		if (this.mergable(next)) {
346			return next.getMergedStatus();
347		} else {
348			return getStatus();
349		}
350	}
351	
352	public long getMergedTimeSent() {
353		Message next = this.next();
354		if (this.mergable(next)) {
355			return next.getMergedTimeSent();
356		} else {
357			return getTimeSent();
358		}
359	}
360
361	public boolean wasMergedIntoPrevious() {
362		Message prev = this.prev();
363		if (prev == null) {
364			return false;
365		} else {
366			return prev.mergable(this);
367		}
368	}
369}