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