Message.java

  1package eu.siacs.conversations.entities;
  2
  3import android.content.ContentValues;
  4import android.database.Cursor;
  5
  6public class Message extends AbstractEntity {
  7
  8	private static final long serialVersionUID = 7222081895167103025L;
  9	
 10	public static final String TABLENAME = "messages";
 11
 12	public static final int STATUS_RECIEVED = 0;
 13	public static final int STATUS_UNSEND = 1;
 14	public static final int STATUS_SEND = 2;
 15	public static final int STATUS_ERROR = 3;
 16
 17	public static final int ENCRYPTION_NONE = 0;
 18	public static final int ENCRYPTION_PGP = 1;
 19	public static final int ENCRYPTION_OTR = 2;
 20	public static final int ENCRYPTION_DECRYPTED = 3;
 21	
 22	public static final int TYPE_TEXT = 0;
 23	public static final int TYPE_IMAGE = 1;
 24
 25	public static String CONVERSATION = "conversationUuid";
 26	public static String COUNTERPART = "counterpart";
 27	public static String BODY = "body";
 28	public static String TIME_SENT = "timeSent";
 29	public static String ENCRYPTION = "encryption";
 30	public static String STATUS = "status";
 31	public static String TYPE = "type";
 32
 33	protected String conversationUuid;
 34	protected String counterpart;
 35	protected String body;
 36	protected String encryptedBody;
 37	protected long timeSent;
 38	protected int encryption;
 39	protected int status;
 40	protected int type;
 41	protected boolean read = true;
 42
 43	protected transient Conversation conversation = null;
 44
 45	public Message(Conversation conversation, String body, int encryption) {
 46		this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),
 47				conversation.getContactJid(), body, System.currentTimeMillis(), encryption,
 48				Message.STATUS_UNSEND,TYPE_TEXT);
 49		this.conversation = conversation;
 50	}
 51	
 52	public Message(Conversation conversation, String counterpart, String body, int encryption, int status) {
 53		this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),counterpart, body, System.currentTimeMillis(), encryption,status,TYPE_TEXT);
 54		this.conversation = conversation;
 55	}
 56	
 57	public Message(String uuid, String conversationUUid, String counterpart,
 58			String body, long timeSent, int encryption, int status, int type) {
 59		this.uuid = uuid;
 60		this.conversationUuid = conversationUUid;
 61		this.counterpart = counterpart;
 62		this.body = body;
 63		this.timeSent = timeSent;
 64		this.encryption = encryption;
 65		this.status = status;
 66		this.type = type;
 67	}
 68
 69	@Override
 70	public ContentValues getContentValues() {
 71		ContentValues values = new ContentValues();
 72		values.put(UUID, uuid);
 73		values.put(CONVERSATION, conversationUuid);
 74		values.put(COUNTERPART, counterpart);
 75		values.put(BODY, body);
 76		values.put(TIME_SENT, timeSent);
 77		values.put(ENCRYPTION, encryption);
 78		values.put(STATUS, status);
 79		values.put(TYPE, type);
 80		return values;
 81	}
 82
 83	public String getConversationUuid() {
 84		return conversationUuid;
 85	}
 86	
 87	public Conversation getConversation() {
 88		return this.conversation;
 89	}
 90
 91	public String getCounterpart() {
 92		return counterpart;
 93	}
 94
 95	public String getBody() {
 96		return body;
 97	}
 98
 99	public long getTimeSent() {
100		return timeSent;
101	}
102
103	public int getEncryption() {
104		return encryption;
105	}
106
107	public int getStatus() {
108		return status;
109	}
110
111	public static Message fromCursor(Cursor cursor) {
112		return new Message(cursor.getString(cursor.getColumnIndex(UUID)),
113				cursor.getString(cursor.getColumnIndex(CONVERSATION)),
114				cursor.getString(cursor.getColumnIndex(COUNTERPART)),
115				cursor.getString(cursor.getColumnIndex(BODY)),
116				cursor.getLong(cursor.getColumnIndex(TIME_SENT)),
117				cursor.getInt(cursor.getColumnIndex(ENCRYPTION)),
118				cursor.getInt(cursor.getColumnIndex(STATUS)),
119				cursor.getInt(cursor.getColumnIndex(TYPE)));
120	}
121
122	public void setConversation(Conversation conv) {
123		this.conversation = conv;
124	}
125
126	public void setStatus(int status) {
127		this.status = status;
128	}
129
130	public boolean isRead() {
131		return this.read;
132	}
133	
134	public void markRead() {
135		this.read = true;
136	}
137	
138	public void markUnread() {
139		this.read = false;
140	}
141
142	public void setTime(long time) {
143		this.timeSent = time;
144	}
145
146	public void setEncryption(int encryption) {
147		this.encryption = encryption;
148	}
149
150	public void setBody(String body) {
151		this.body = body;
152	}
153
154	public String getEncryptedBody() {
155		return this.encryptedBody;
156	}
157	
158	public void setEncryptedBody(String body) {
159		this.encryptedBody = body;
160	}
161
162	public void setType(int type) {
163		this.type = type;
164	}
165	
166	public int getType() {
167		return this.type;
168	}
169
170	public void setPresence(String presence) {
171		this.counterpart = this.counterpart.split("/")[0] + "/" + presence;
172	}
173}