Show encryption status in the chat message list.
authorDa Risk <da_risk@beem-project.com>
Wed, 24 Aug 2011 23:15:26 +0200
changeset 920 b659da60d81e
parent 919 7552d41aebec
child 921 319ce14e4826
Show encryption status in the chat message list.
res/drawable/logo_encryption.png
res/menu/chat.xml
src/com/beem/project/beem/service/ChatAdapter.java
src/com/beem/project/beem/service/Message.java
src/com/beem/project/beem/ui/Chat.java
Binary file res/drawable/logo_encryption.png has changed
--- a/res/menu/chat.xml	Tue Aug 23 14:14:22 2011 +0200
+++ b/res/menu/chat.xml	Wed Aug 24 23:15:26 2011 +0200
@@ -8,7 +8,7 @@
 	<item android:id="@+id/chat_menu_close_chat" android:visible="true"
 		android:title="@string/chat_menu_close_chat" android:icon="@drawable/ic_menu_end_conversation" />
 	<item android:id="@+id/chat_menu_otr_submenu" android:visible="true"
-		android:title="@string/chat_menu_otr_submenu">
+		android:title="@string/chat_menu_otr_submenu" android:icon="@drawable/logo_encryption">
 		<menu>
 			<item android:id="@+id/chat_menu_start_otr_session"
 				android:visible="true" android:title="@string/chat_menu_start_otr_session" />
--- a/src/com/beem/project/beem/service/ChatAdapter.java	Tue Aug 23 14:14:22 2011 +0200
+++ b/src/com/beem/project/beem/service/ChatAdapter.java	Wed Aug 24 23:15:26 2011 +0200
@@ -113,7 +113,13 @@
      */
     @Override
     public void sendMessage(com.beem.project.beem.service.Message message) throws RemoteException {
-	sendMessage(message, true, true);
+	com.beem.project.beem.service.Message encrypted = otrEncryptMessage(message);
+	if (encrypted != null) {
+	    transferMessage(encrypted);
+	} else {
+	    transferMessage(message);
+	}
+	addMessage(message);
     }
 
     /**
@@ -121,25 +127,12 @@
      * @param message the message to send
      * @param log do we want to log (in memory and history) the message?
      */
-    private void sendMessage(com.beem.project.beem.service.Message message, boolean log, boolean otr) {
+    private void transferMessage(com.beem.project.beem.service.Message message) {
 	org.jivesoftware.smack.packet.Message send = new org.jivesoftware.smack.packet.Message();
 	String msgBody = message.getBody();
-	String otrBody = null;
 	send.setTo(message.getTo());
 	Log.w(TAG, "message to " + message.getTo());
-
-	if (otr && mOtrSessionId != null) {
-
-	    try {
-		otrBody = BeemOtrManager.getInstance().getOtrManager().transformSending(mOtrSessionId, msgBody);
-		send.setBody(otrBody);
-	    } catch (OtrException e) {
-		e.printStackTrace();
-	    }
-	} else {
-	    send.setBody(msgBody);
-	    otrBody = msgBody;
-	}
+	send.setBody(msgBody);
 
 	send.setThread(message.getThread());
 	send.setSubject(message.getSubject());
@@ -148,14 +141,9 @@
 	// send.set
 	try {
 	    mAdaptee.sendMessage(send);
-	    if (log && otrBody != null)
-		mMessages.add(message);
 	} catch (XMPPException e) {
 	    e.printStackTrace();
 	}
-	String state = Environment.getExternalStorageState();
-	if (log && mIsHistory && Environment.MEDIA_MOUNTED.equals(state))
-	    saveHistory(message, mAccountUser);
     }
 
     /**
@@ -165,7 +153,7 @@
     public void injectMessage(String msg) {
 	Message msgToSend = new Message(mParticipant.getJIDWithRes(), Message.MSG_TYPE_CHAT);
 	msgToSend.setBody(msg);
-	sendMessage(msgToSend, false, false);
+	transferMessage(msgToSend);
     }
 
     /**
@@ -239,14 +227,12 @@
      * Add a message in the chat history.
      * @param msg the message to add
      */
-    void addMessage(Message msg) {
+    private void addMessage(Message msg) {
 	if (mMessages.size() == HISTORY_MAX_SIZE)
 	    mMessages.remove(0);
 	mMessages.add(msg);
 	if (!"".equals(msg.getBody()) && msg.getBody() != null) {
-	    String state = Environment.getExternalStorageState();
-	    if (mIsHistory && Environment.MEDIA_MOUNTED.equals(state))
-		saveHistory(msg, msg.getFrom());
+	    logMessage(msg);
 	}
     }
 
@@ -322,6 +308,28 @@
 	return mHistoryPath;
     }
 
+    private void logMessage(com.beem.project.beem.service.Message message) {
+	String state = Environment.getExternalStorageState();
+	if (mIsHistory && Environment.MEDIA_MOUNTED.equals(state))
+	    saveHistory(message, mAccountUser);
+
+    }
+
+    private com.beem.project.beem.service.Message otrEncryptMessage(com.beem.project.beem.service.Message unencrypted) {
+
+	if (mOtrSessionId != null && unencrypted != null && unencrypted.getBody() != null) {
+	    try {
+		String body = BeemOtrManager.getInstance().getOtrManager().transformSending(mOtrSessionId, unencrypted.getBody());
+		Message result = new Message(unencrypted.getTo(), unencrypted.getType());
+		result.setBody(body);
+		return result;
+	    } catch (OtrException e) {
+		Log.e(TAG, "OTR: Unable to encrypt message", e);
+	    }
+	}
+	return null;
+    }
+
     /**
      * Listener.
      */
@@ -339,16 +347,14 @@
 	    String body;
 
 	    if (mOtrSessionId != null) {
-
 		try {
 		    body = BeemOtrManager.getInstance().getOtrManager()
 			.transformReceiving(mOtrSessionId, msg.getBody());
 		    msg.setBody(body);
 		} catch (OtrException e) {
-		    e.printStackTrace();
+		    Log.w(TAG, "Unable to decrypt OTR message", e);
 		}
 	    }
-
 	    //TODO add que les message pas de type errors
 	    ChatAdapter.this.addMessage(msg);
 	    final int n = mRemoteListeners.beginBroadcast();
@@ -390,6 +396,9 @@
      * @param otrState the new state of otr session.
      */
     public void otrStateChanged(final String otrState) {
+	Message m = new Message(null, Message.MSG_TYPE_INFO);
+	m.setBody(otrState);
+	addMessage(m);
 	final int n = mRemoteListeners.beginBroadcast();
 
 	for (int i = 0; i < n; i++) {
--- a/src/com/beem/project/beem/service/Message.java	Tue Aug 23 14:14:22 2011 +0200
+++ b/src/com/beem/project/beem/service/Message.java	Wed Aug 24 23:15:26 2011 +0200
@@ -69,6 +69,9 @@
     /** Error message type. */
     public static final int MSG_TYPE_ERROR = 400;
 
+    /** Informational message type. */
+    public static final int MSG_TYPE_INFO = 500;
+
     /** Parcelable.Creator needs by Android. */
     public static final Parcelable.Creator<Message> CREATOR = new Parcelable.Creator<Message>() {
 
--- a/src/com/beem/project/beem/ui/Chat.java	Tue Aug 23 14:14:22 2011 +0200
+++ b/src/com/beem/project/beem/ui/Chat.java	Wed Aug 24 23:15:26 2011 +0200
@@ -414,6 +414,10 @@
 	    if (m.getType() == Message.MSG_TYPE_ERROR) {
 		lastMessage = null;
 		result.add(new MessageText(fromBareJid, name, m.getBody(), true, m.getTimestamp()));
+	    } else if  (m.getType() == Message.MSG_TYPE_INFO) {
+		lastMessage = new MessageText("", "", m.getBody(), false);
+		result.add(lastMessage);
+
 	    } else if (m.getType() == Message.MSG_TYPE_CHAT) {
 		if (fromBareJid == null) { //nofrom or from == yours
 		    name = localName;
@@ -610,6 +614,8 @@
 		@Override
 		public void run() {
 		    updateOtrInformations(otrState);
+		    mListMessages.add(new MessageText("", "", otrState, false));
+		    mMessagesListAdapter.notifyDataSetChanged();
 		}
 	    });
 
@@ -788,8 +794,10 @@
 	    msgText.setText(msg.getMessage());
 	    registerForContextMenu(msgText);
 	    TextView msgDate = (TextView) sv.findViewById(R.id.chatmessagedate);
-	    String date = df.format(msg.getTimestamp());
-	    msgDate.setText(date);
+	    if (msg.getTimestamp() != null) {
+		String date = df.format(msg.getTimestamp());
+		msgDate.setText(date);
+	    }
 	    if (msg.isError()) {
 		String err = getString(R.string.chat_error);
 		msgName.setText(err);