Message.java

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