1package de.gultsch.chat.entities;
2
3import android.content.ContentValues;
4import android.database.Cursor;
5
6public class Message extends AbstractEntity {
7
8 private static final long serialVersionUID = 7222081895167103025L;
9
10 public static final String TABLENAME = "messages";
11
12 public static final int STATUS_RECIEVED = 0;
13 public static final int STATUS_UNSEND = 1;
14 public static final int STATUS_SEND = 2;
15
16 public static final int ENCRYPTION_NONE = 0;
17 public static final int ENCRYPTION_PGP = 1;
18 public static final int ENCRYPTION_OTR = 2;
19
20 public static String CONVERSATION = "conversationUuid";
21 public static String COUNTERPART = "counterpart";
22 public static String BODY = "body";
23 public static String TIME_SENT = "timeSent";
24 public static String ENCRYPTION = "encryption";
25 public static String STATUS = "status";
26
27 protected String conversationUuid;
28 protected String counterpart;
29 protected String body;
30 protected long timeSent;
31 protected int encryption;
32 protected int status;
33 protected boolean read = true;
34
35 protected transient Conversation conversation = null;
36
37 public Message(Conversation conversation, String body, int encryption) {
38 this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),
39 conversation.getContactJid(), body, System.currentTimeMillis(), encryption,
40 Message.STATUS_UNSEND);
41 this.conversation = conversation;
42 }
43
44 public Message(Conversation conversation, String counterpart, String body, int encryption, int status) {
45 this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),counterpart, body, System.currentTimeMillis(), encryption,status);
46 this.conversation = conversation;
47 }
48
49 public Message(String uuid, String conversationUUid, String counterpart,
50 String body, long timeSent, int encryption, int status) {
51 this.uuid = uuid;
52 this.conversationUuid = conversationUUid;
53 this.counterpart = counterpart;
54 this.body = body;
55 this.timeSent = timeSent;
56 this.encryption = encryption;
57 this.status = status;
58 }
59
60 @Override
61 public ContentValues getContentValues() {
62 ContentValues values = new ContentValues();
63 values.put(UUID, uuid);
64 values.put(CONVERSATION, conversationUuid);
65 values.put(COUNTERPART, counterpart);
66 values.put(BODY, body);
67 values.put(TIME_SENT, timeSent);
68 values.put(ENCRYPTION, encryption);
69 values.put(STATUS, status);
70 return values;
71 }
72
73 public String getConversationUuid() {
74 return conversationUuid;
75 }
76
77 public Conversation getConversation() {
78 return this.conversation;
79 }
80
81 public String getCounterpart() {
82 return counterpart;
83 }
84
85 public String getBody() {
86 return body;
87 }
88
89 public long getTimeSent() {
90 return timeSent;
91 }
92
93 public int getEncryption() {
94 return encryption;
95 }
96
97 public int getStatus() {
98 return status;
99 }
100
101 public static Message fromCursor(Cursor cursor) {
102 return new Message(cursor.getString(cursor.getColumnIndex(UUID)),
103 cursor.getString(cursor.getColumnIndex(CONVERSATION)),
104 cursor.getString(cursor.getColumnIndex(COUNTERPART)),
105 cursor.getString(cursor.getColumnIndex(BODY)),
106 cursor.getLong(cursor.getColumnIndex(TIME_SENT)),
107 cursor.getInt(cursor.getColumnIndex(ENCRYPTION)),
108 cursor.getInt(cursor.getColumnIndex(STATUS)));
109 }
110
111 public void setConversation(Conversation conv) {
112 this.conversation = conv;
113 }
114
115 public void setStatus(int status) {
116 this.status = status;
117 }
118
119 public boolean isRead() {
120 return this.read;
121 }
122
123 public void markRead() {
124 this.read = true;
125 }
126
127 public void markUnread() {
128 this.read = false;
129 }
130
131 public void setTime(long time) {
132 this.timeSent = time;
133 }
134
135}