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)
153					.toString();
154		} else if ((encryption == ENCRYPTION_OTR) && (type == TYPE_IMAGE)) {
155			return context.getText(R.string.encrypted_image_received)
156					.toString();
157		} else if (encryption == ENCRYPTION_DECRYPTION_FAILED) {
158			return context.getText(R.string.decryption_failed).toString();
159		} else if (type == TYPE_IMAGE) {
160			return context.getText(R.string.image_file).toString();
161		} else {
162			return body.trim();
163		}
164	}
165
166	public long getTimeSent() {
167		return timeSent;
168	}
169
170	public int getEncryption() {
171		return encryption;
172	}
173
174	public int getStatus() {
175		return status;
176	}
177
178	public String getRemoteMsgId() {
179		return this.remoteMsgId;
180	}
181
182	public void setRemoteMsgId(String id) {
183		this.remoteMsgId = id;
184	}
185
186	public static Message fromCursor(Cursor cursor) {
187		return new Message(cursor.getString(cursor.getColumnIndex(UUID)),
188				cursor.getString(cursor.getColumnIndex(CONVERSATION)),
189				cursor.getString(cursor.getColumnIndex(COUNTERPART)),
190				cursor.getString(cursor.getColumnIndex(TRUE_COUNTERPART)),
191				cursor.getString(cursor.getColumnIndex(BODY)),
192				cursor.getLong(cursor.getColumnIndex(TIME_SENT)),
193				cursor.getInt(cursor.getColumnIndex(ENCRYPTION)),
194				cursor.getInt(cursor.getColumnIndex(STATUS)),
195				cursor.getInt(cursor.getColumnIndex(TYPE)),
196				cursor.getString(cursor.getColumnIndex(REMOTE_MSG_ID)));
197	}
198
199	public void setConversation(Conversation conv) {
200		this.conversation = conv;
201	}
202
203	public void setStatus(int status) {
204		this.status = status;
205	}
206
207	public boolean isRead() {
208		return this.read;
209	}
210
211	public void markRead() {
212		this.read = true;
213	}
214
215	public void markUnread() {
216		this.read = false;
217	}
218
219	public void setTime(long time) {
220		this.timeSent = time;
221	}
222
223	public void setEncryption(int encryption) {
224		this.encryption = encryption;
225	}
226
227	public void setBody(String body) {
228		this.body = body;
229	}
230
231	public String getEncryptedBody() {
232		return this.encryptedBody;
233	}
234
235	public void setEncryptedBody(String body) {
236		this.encryptedBody = body;
237	}
238
239	public void setType(int type) {
240		this.type = type;
241	}
242
243	public int getType() {
244		return this.type;
245	}
246
247	public void setPresence(String presence) {
248		if (presence == null) {
249			this.counterpart = this.counterpart.split("/")[0];
250		} else {
251			this.counterpart = this.counterpart.split("/")[0] + "/" + presence;
252		}
253	}
254
255	public void setTrueCounterpart(String trueCounterpart) {
256		this.trueCounterpart = trueCounterpart;
257	}
258
259	public String getPresence() {
260		String[] counterparts = this.counterpart.split("/");
261		if (counterparts.length == 2) {
262			return counterparts[1];
263		} else {
264			if (this.counterpart.contains("/")) {
265				return "";
266			} else {
267				return null;
268			}
269		}
270	}
271
272	public void setDownloadable(Downloadable downloadable) {
273		this.downloadable = downloadable;
274	}
275
276	public Downloadable getDownloadable() {
277		return this.downloadable;
278	}
279
280	public static Message createStatusMessage(Conversation conversation) {
281		Message message = new Message();
282		message.setType(Message.TYPE_STATUS);
283		message.setConversation(conversation);
284		return message;
285	}
286
287	public void setCounterpart(String counterpart) {
288		this.counterpart = counterpart;
289	}
290
291	public boolean equals(Message message) {
292		if ((this.remoteMsgId != null) && (this.body != null)
293				&& (this.counterpart != null)) {
294			return this.remoteMsgId.equals(message.getRemoteMsgId())
295					&& this.body.equals(message.getBody())
296					&& this.counterpart.equals(message.getCounterpart());
297		} else {
298			return false;
299		}
300	}
301
302	public Message next() {
303		int index = this.conversation.getMessages().indexOf(this);
304		if (index < 0 || index >= this.conversation.getMessages().size() - 1) {
305			return null;
306		} else {
307			return this.conversation.getMessages().get(index + 1);
308		}
309	}
310
311	public Message prev() {
312		int index = this.conversation.getMessages().indexOf(this);
313		if (index <= 0 || index > this.conversation.getMessages().size()) {
314			return null;
315		} else {
316			return this.conversation.getMessages().get(index - 1);
317		}
318	}
319
320	public boolean mergable(Message message) {
321		if (message == null) {
322			return false;
323		}
324		return (message.getType() == Message.TYPE_TEXT
325				&& message.getEncryption() != Message.ENCRYPTION_PGP
326				&& this.getType() == message.getType()
327				&& this.getEncryption() == message.getEncryption()
328				&& this.getCounterpart().equals(message.getCounterpart())
329				&& (message.getTimeSent() - this.getTimeSent()) <= (Config.MESSAGE_MERGE_WINDOW * 1000)
330				&& ((this.getStatus() == message.getStatus())
331				|| (this.getStatus() == Message.STATUS_SEND && (message.getStatus() == Message.STATUS_UNSEND
332				|| message.getStatus() == Message.STATUS_SEND))
333				|| (this.getStatus() == Message.STATUS_SEND_RECEIVED
334				&& message.getStatus() == Message.STATUS_SEND_DISPLAYED)));
335	}
336
337	public String getMergedBody() {
338		Message next = this.next();
339		if (this.mergable(next)) {
340			return body.trim() + '\n' + next.getMergedBody();
341		}
342		return body.trim();
343	}
344
345	public int getMergedStatus() {
346		Message next = this.next();
347		if (this.mergable(next)) {
348			return next.getMergedStatus();
349		} else {
350			return getStatus();
351		}
352	}
353
354	public long getMergedTimeSent() {
355		Message next = this.next();
356		if (this.mergable(next)) {
357			return next.getMergedTimeSent();
358		} else {
359			return getTimeSent();
360		}
361	}
362
363	public boolean wasMergedIntoPrevious() {
364		Message prev = this.prev();
365		if (prev == null) {
366			return false;
367		} else {
368			return prev.mergable(this);
369		}
370	}
371}