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