fix otr message in history
authorNikita Kozlov <nikita@beem-project.com>
Wed, 25 May 2011 00:36:01 +0200
changeset 902 c2eb19cd7032
parent 901 6c77794969c5
child 911 0de5ddd06ecd
fix otr message in history
src/com/beem/project/beem/service/ChatAdapter.java
--- a/src/com/beem/project/beem/service/ChatAdapter.java	Tue May 24 15:15:08 2011 +0200
+++ b/src/com/beem/project/beem/service/ChatAdapter.java	Wed May 25 00:36:01 2011 +0200
@@ -62,7 +62,6 @@
 import android.os.Environment;
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
-import android.util.AndroidException;
 import android.util.Log;
 
 import com.beem.project.beem.otr.BeemOtrManager;
@@ -114,22 +113,35 @@
      */
     @Override
     public void sendMessage(com.beem.project.beem.service.Message message) throws RemoteException {
+	sendMessage(message, true);
+    }
+
+    /**
+     * private method for sending message.
+     * @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) {
 	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 (mOtrSessionId != null) {
-	    String body;
+
 	    try {
-		body = BeemOtrManager.getInstance().getOtrManager().transformSending(mOtrSessionId, msgBody);
-		msgBody = body;
+		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());
 	send.setType(org.jivesoftware.smack.packet.Message.Type.chat);
@@ -137,12 +149,13 @@
 	// send.set
 	try {
 	    mAdaptee.sendMessage(send);
-	    mMessages.add(message);
+	    if (log && otrBody != null)
+		mMessages.add(message);
 	} catch (XMPPException e) {
 	    e.printStackTrace();
 	}
 	String state = Environment.getExternalStorageState();
-	if (mIsHistory && Environment.MEDIA_MOUNTED.equals(state))
+	if (log && mIsHistory && Environment.MEDIA_MOUNTED.equals(state))
 	    saveHistory(message, mAccountUser);
     }
 
@@ -153,11 +166,7 @@
     public void sendMessage(String msg) {
 	Message msgToSend = new Message(mParticipant.getJIDWithRes(), Message.MSG_TYPE_CHAT);
 	msgToSend.setBody(msg);
-	try {
-	    sendMessage(msgToSend);
-	} catch (RemoteException e) {
-	    e.printStackTrace();
-	}
+	sendMessage(msgToSend, false);
     }
 
     /**
@@ -328,9 +337,10 @@
 	public void processMessage(Chat chat, org.jivesoftware.smack.packet.Message message) {
 	    Message  msg = new Message(message);
 	    Log.d(TAG, "new msg " + msg.getBody());
+	    String body;
 
 	    if (mOtrSessionId != null) {
-		String body;
+
 		try {
 		    body = BeemOtrManager.getInstance().getOtrManager().transformReceiving(mOtrSessionId, msg.getBody());
 		    msg.setBody(body);
@@ -379,7 +389,7 @@
      * This method is executed when the otr session status change.
      * @param otrState the new state of otr session.
      */
-    public	void otrStateChanged(final String otrState) {
+    public void otrStateChanged(final String otrState) {
 	final int n = mRemoteListeners.beginBroadcast();
 
 	for (int i = 0; i < n; i++) {
@@ -413,13 +423,13 @@
     @Override
     public void endOtrSession() throws RemoteException {
 	try {
-	   localEndOtrSession();	        
+	    localEndOtrSession();	        
 	} catch (OtrException e) {
 	    e.printStackTrace();
 	    throw new RemoteException();
 	}
     }
-    
+
     /**
      * end an Otr session.
      * @return false if something bad happened.
@@ -427,7 +437,7 @@
     public boolean localEndOtrSession() throws OtrException {
 	if (mOtrSessionId == null)
 	    return true;
-	
+
 	BeemOtrManager.getInstance().getOtrManager().endSession(mOtrSessionId);
 	BeemOtrManager.getInstance().removeChat(mOtrSessionId);
 	mOtrSessionId = null;
@@ -450,7 +460,7 @@
     public String getLocalOtrFingerprint() throws RemoteException {
 	if (mOtrSessionId == null)
 	    return null;
-	
+
 	return BeemOtrManager.getInstance().getLocalFingerprint(mOtrSessionId);
     }
 
@@ -458,7 +468,7 @@
     public String getRemoteOtrFingerprint() throws RemoteException {
 	if (mOtrSessionId == null)
 	    return null;
-	
+
 	return BeemOtrManager.getInstance().getRemoteFingerprint(mOtrSessionId);
     }
 }