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