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