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