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("/", 2)[0];
250		} else {
251			this.counterpart = this.counterpart.split("/", 2)[0] + "/"
252					+ presence;
253		}
254	}
255
256	public void setTrueCounterpart(String trueCounterpart) {
257		this.trueCounterpart = trueCounterpart;
258	}
259
260	public String getPresence() {
261		String[] counterparts = this.counterpart.split("/", 2);
262		if (counterparts.length == 2) {
263			return counterparts[1];
264		} else {
265			if (this.counterpart.contains("/")) {
266				return "";
267			} else {
268				return null;
269			}
270		}
271	}
272
273	public void setDownloadable(Downloadable downloadable) {
274		this.downloadable = downloadable;
275	}
276
277	public Downloadable getDownloadable() {
278		return this.downloadable;
279	}
280
281	public static Message createStatusMessage(Conversation conversation) {
282		Message message = new Message();
283		message.setType(Message.TYPE_STATUS);
284		message.setConversation(conversation);
285		return message;
286	}
287
288	public void setCounterpart(String counterpart) {
289		this.counterpart = counterpart;
290	}
291
292	public boolean equals(Message message) {
293		if ((this.remoteMsgId != null) && (this.body != null)
294				&& (this.counterpart != null)) {
295			return this.remoteMsgId.equals(message.getRemoteMsgId())
296					&& this.body.equals(message.getBody())
297					&& this.counterpart.equals(message.getCounterpart());
298		} else {
299			return false;
300		}
301	}
302
303	public Message next() {
304		int index = this.conversation.getMessages().indexOf(this);
305		if (index < 0 || index >= this.conversation.getMessages().size() - 1) {
306			return null;
307		} else {
308			return this.conversation.getMessages().get(index + 1);
309		}
310	}
311
312	public Message prev() {
313		int index = this.conversation.getMessages().indexOf(this);
314		if (index <= 0 || index > this.conversation.getMessages().size()) {
315			return null;
316		} else {
317			return this.conversation.getMessages().get(index - 1);
318		}
319	}
320
321	public boolean mergable(Message message) {
322		if (message == null) {
323			return false;
324		}
325		return (message.getType() == Message.TYPE_TEXT
326				&& message.getEncryption() != Message.ENCRYPTION_PGP
327				&& this.getType() == message.getType()
328				&& this.getEncryption() == message.getEncryption()
329				&& this.getCounterpart().equals(message.getCounterpart())
330				&& (message.getTimeSent() - this.getTimeSent()) <= (Config.MESSAGE_MERGE_WINDOW * 1000) && ((this
331				.getStatus() == message.getStatus() || ((this.getStatus() == Message.STATUS_SEND || this
332				.getStatus() == Message.STATUS_SEND_RECEIVED) && (message
333				.getStatus() == Message.STATUS_UNSEND
334				|| message.getStatus() == Message.STATUS_SEND || message
335					.getStatus() == Message.STATUS_SEND_DISPLAYED)))));
336	}
337
338	public String getMergedBody() {
339		Message next = this.next();
340		if (this.mergable(next)) {
341			return body.trim() + '\n' + next.getMergedBody();
342		}
343		return body.trim();
344	}
345
346	public int getMergedStatus() {
347		Message next = this.next();
348		if (this.mergable(next)) {
349			return next.getMergedStatus();
350		} else {
351			return getStatus();
352		}
353	}
354
355	public long getMergedTimeSent() {
356		Message next = this.next();
357		if (this.mergable(next)) {
358			return next.getMergedTimeSent();
359		} else {
360			return getTimeSent();
361		}
362	}
363
364	public boolean wasMergedIntoPrevious() {
365		Message prev = this.prev();
366		if (prev == null) {
367			return false;
368		} else {
369			return prev.mergable(this);
370		}
371	}
372}