Message.java

  1package eu.siacs.conversations.entities;
  2
  3import eu.siacs.conversations.R;
  4import eu.siacs.conversations.xmpp.jingle.JingleConnection;
  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_RECIEVING = -1;
 16	public static final int STATUS_RECIEVED = 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
 47	protected String conversationUuid;
 48	protected String counterpart;
 49	protected String trueCounterpart;
 50	protected String body;
 51	protected String encryptedBody;
 52	protected long timeSent;
 53	protected int encryption;
 54	protected int status;
 55	protected int type;
 56	protected boolean read = true;
 57
 58	protected transient Conversation conversation = null;
 59	
 60	protected transient JingleConnection jingleConnection = null;
 61	
 62	private Message() {
 63		
 64	}
 65
 66	public Message(Conversation conversation, String body, int encryption) {
 67		this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),
 68				conversation.getContactJid(), null, body, System.currentTimeMillis(), encryption,
 69				Message.STATUS_UNSEND,TYPE_TEXT);
 70		this.conversation = conversation;
 71	}
 72	
 73	public Message(Conversation conversation, String counterpart, String body, int encryption, int status) {
 74		this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),counterpart, null, body, System.currentTimeMillis(), encryption,status,TYPE_TEXT);
 75		this.conversation = conversation;
 76	}
 77	
 78	public Message(String uuid, String conversationUUid, String counterpart, String trueCounterpart,
 79			String body, long timeSent, int encryption, int status, int type) {
 80		this.uuid = uuid;
 81		this.conversationUuid = conversationUUid;
 82		this.counterpart = counterpart;
 83		this.trueCounterpart = trueCounterpart;
 84		this.body = body;
 85		this.timeSent = timeSent;
 86		this.encryption = encryption;
 87		this.status = status;
 88		this.type = type;
 89	}
 90
 91	@Override
 92	public ContentValues getContentValues() {
 93		ContentValues values = new ContentValues();
 94		values.put(UUID, uuid);
 95		values.put(CONVERSATION, conversationUuid);
 96		values.put(COUNTERPART, counterpart);
 97		values.put(TRUE_COUNTERPART,trueCounterpart);
 98		values.put(BODY, body);
 99		values.put(TIME_SENT, timeSent);
100		values.put(ENCRYPTION, encryption);
101		values.put(STATUS, status);
102		values.put(TYPE, type);
103		return values;
104	}
105
106	public String getConversationUuid() {
107		return conversationUuid;
108	}
109	
110	public Conversation getConversation() {
111		return this.conversation;
112	}
113
114	public String getCounterpart() {
115		return counterpart;
116	}
117	
118	public Contact getContact() {
119		if (this.conversation.getMode() == Conversation.MODE_SINGLE) {
120			return this.conversation.getContact();
121		} else {
122			if (this.trueCounterpart == null) {
123				return null;
124			} else {
125				Account account = this.conversation.getAccount();
126				Contact contact = account.getRoster().getContact(this.trueCounterpart);
127				if (contact.showInRoster()) {
128					return contact;
129				} else {
130					return null;
131				}
132			}
133		}
134	}
135
136	public String getBody() {
137		return body;
138	}
139	
140	public String getReadableBody(Context context) {
141		if ((encryption == ENCRYPTION_PGP)&&(type == TYPE_TEXT)) {
142			return ""+context.getText(R.string.encrypted_message_received);
143		} else if ((encryption == ENCRYPTION_OTR)&&(type == TYPE_IMAGE)) {
144			return ""+context.getText(R.string.encrypted_image_received);
145		} else if (encryption == ENCRYPTION_DECRYPTION_FAILED) {
146			return ""+context.getText(R.string.decryption_failed);
147		} else if (type == TYPE_IMAGE) {
148			return ""+context.getText(R.string.image_file);
149		} else {
150			return body.trim();
151		}
152	}
153
154	public long getTimeSent() {
155		return timeSent;
156	}
157
158	public int getEncryption() {
159		return encryption;
160	}
161
162	public int getStatus() {
163		return status;
164	}
165
166	public static Message fromCursor(Cursor cursor) {
167		return new Message(cursor.getString(cursor.getColumnIndex(UUID)),
168				cursor.getString(cursor.getColumnIndex(CONVERSATION)),
169				cursor.getString(cursor.getColumnIndex(COUNTERPART)),
170				cursor.getString(cursor.getColumnIndex(TRUE_COUNTERPART)),
171				cursor.getString(cursor.getColumnIndex(BODY)),
172				cursor.getLong(cursor.getColumnIndex(TIME_SENT)),
173				cursor.getInt(cursor.getColumnIndex(ENCRYPTION)),
174				cursor.getInt(cursor.getColumnIndex(STATUS)),
175				cursor.getInt(cursor.getColumnIndex(TYPE)));
176	}
177
178	public void setConversation(Conversation conv) {
179		this.conversation = conv;
180	}
181
182	public void setStatus(int status) {
183		this.status = status;
184	}
185
186	public boolean isRead() {
187		return this.read;
188	}
189	
190	public void markRead() {
191		this.read = true;
192	}
193	
194	public void markUnread() {
195		this.read = false;
196	}
197
198	public void setTime(long time) {
199		this.timeSent = time;
200	}
201
202	public void setEncryption(int encryption) {
203		this.encryption = encryption;
204	}
205
206	public void setBody(String body) {
207		this.body = body;
208	}
209
210	public String getEncryptedBody() {
211		return this.encryptedBody;
212	}
213	
214	public void setEncryptedBody(String body) {
215		this.encryptedBody = body;
216	}
217
218	public void setType(int type) {
219		this.type = type;
220	}
221	
222	public int getType() {
223		return this.type;
224	}
225
226	public void setPresence(String presence) {
227		if (presence == null || presence.isEmpty()) {
228			this.counterpart = this.counterpart.split("/")[0];
229		} else {
230			this.counterpart = this.counterpart.split("/")[0] + "/" + presence;
231		}
232	}
233	
234	public void setTrueCounterpart(String trueCounterpart) {
235		this.trueCounterpart = trueCounterpart;
236	}
237	
238	public String getPresence() {
239		String[] counterparts = this.counterpart.split("/");
240		if (counterparts.length == 2) {
241			return counterparts[1];
242		} else {
243			return null;
244		}
245	}
246	
247	public void setJingleConnection(JingleConnection connection) {
248		this.jingleConnection = connection;
249	}
250	
251	public JingleConnection getJingleConnection() {
252		return this.jingleConnection;
253	}
254	
255	public static Message createStatusMessage(Conversation conversation) {
256		Message message = new Message();
257		message.setType(Message.TYPE_STATUS);
258		message.setConversation(conversation);
259		return message;
260	}
261
262	public void setCounterpart(String counterpart) {
263		this.counterpart = counterpart;
264	}
265}