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