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