Message.java

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