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