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