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