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