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