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