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);
154		} else if ((encryption == ENCRYPTION_OTR) && (type == TYPE_IMAGE)) {
155			return "" + context.getText(R.string.encrypted_image_received);
156		} else if (encryption == ENCRYPTION_DECRYPTION_FAILED) {
157			return "" + context.getText(R.string.decryption_failed);
158		} else if (type == TYPE_IMAGE) {
159			return "" + context.getText(R.string.image_file);
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 || presence.isEmpty()) {
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			return null;
264		}
265	}
266
267	public void setJingleConnection(JingleConnection connection) {
268		this.jingleConnection = connection;
269	}
270
271	public JingleConnection getJingleConnection() {
272		return this.jingleConnection;
273	}
274
275	public static Message createStatusMessage(Conversation conversation) {
276		Message message = new Message();
277		message.setType(Message.TYPE_STATUS);
278		message.setConversation(conversation);
279		return message;
280	}
281
282	public void setCounterpart(String counterpart) {
283		this.counterpart = counterpart;
284	}
285
286	public boolean equals(Message message) {
287		if ((this.remoteMsgId != null) && (this.body != null)
288				&& (this.counterpart != null)) {
289			return this.remoteMsgId.equals(message.getRemoteMsgId())
290					&& this.body.equals(message.getBody())
291					&& this.counterpart.equals(message.getCounterpart());
292		} else {
293			return false;
294		}
295	}
296
297	public Message next() {
298		int index = this.conversation.getMessages().indexOf(this);
299		if (index < 0 || index >= this.conversation.getMessages().size() - 1) {
300			return null;
301		} else {
302			return this.conversation.getMessages().get(index + 1);
303		}
304	}
305
306	public Message prev() {
307		int index = this.conversation.getMessages().indexOf(this);
308		if (index <= 0 || index > this.conversation.getMessages().size()) {
309			return null;
310		} else {
311			return this.conversation.getMessages().get(index - 1);
312		}
313	}
314
315	public boolean mergable(Message message) {
316		if (message == null) {
317			return false;
318		}
319		return (message.getType() == Message.TYPE_TEXT
320				&& message.getEncryption() != Message.ENCRYPTION_PGP
321				&& this.getType() == message.getType()
322				&& this.getEncryption() == message.getEncryption()
323				&& this.getCounterpart().equals(message.getCounterpart())
324				&& (message.getTimeSent() - this.getTimeSent()) <= (Config.MESSAGE_MERGE_WINDOW * 1000) && ((this
325				.getStatus() == message.getStatus()) || ((this.getStatus() == Message.STATUS_SEND || this
326				.getStatus() == Message.STATUS_SEND_RECEIVED) && (message
327				.getStatus() == Message.STATUS_UNSEND
328				|| message.getStatus() == Message.STATUS_SEND || message
329					.getStatus() == Message.STATUS_SEND_DISPLAYED))));
330	}
331
332	public String getMergedBody() {
333		Message next = this.next();
334		if (this.mergable(next)) {
335			return body.trim() + '\n' + next.getMergedBody();
336		}
337		return body.trim();
338	}
339	
340	public int getMergedStatus() {
341		Message next = this.next();
342		if (this.mergable(next)) {
343			return next.getMergedStatus();
344		} else {
345			return getStatus();
346		}
347	}
348	
349	public long getMergedTimeSent() {
350		Message next = this.next();
351		if (this.mergable(next)) {
352			return next.getMergedTimeSent();
353		} else {
354			return getTimeSent();
355		}
356	}
357
358	public boolean wasMergedIntoPrevious() {
359		Message prev = this.prev();
360		if (prev == null) {
361			return false;
362		} else {
363			return prev.mergable(this);
364		}
365	}
366}