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