rewrite BeemOtrManager as a singleton and merge it with BeemOtrEngineHostImpl
authorNikita Kozlov <nikita@mbdsys.com>
Wed, 08 Dec 2010 21:43:17 +0100
changeset 816 bf953743f5a1
parent 815 ca323cff3ac9
child 817 5123895ac95f
rewrite BeemOtrManager as a singleton and merge it with BeemOtrEngineHostImpl
res/menu/chat.xml
res/values/strings.xml
src/com/beem/project/beem/otr/BeemOtrEngineHostImpl.java
src/com/beem/project/beem/otr/BeemOtrManager.java
src/com/beem/project/beem/service/ChatAdapter.java
src/com/beem/project/beem/service/aidl/IChat.aidl
src/com/beem/project/beem/ui/Chat.java
--- a/res/menu/chat.xml	Tue Dec 07 22:57:56 2010 +0100
+++ b/res/menu/chat.xml	Wed Dec 08 21:43:17 2010 +0100
@@ -9,6 +9,8 @@
 		android:title="@string/chat_menu_close_chat" android:icon="@drawable/ic_menu_end_conversation" />
 	<item android:id="@+id/chat_menu_start_otr_session" android:visible="true"
 		android:title="@string/chat_menu_start_otr_session" />
+	<item android:id="@+id/chat_menu_listen_otr_session" android:visible="true"
+		android:title="@string/chat_menu_listen_otr_session" />
 	<item android:id="@+id/chat_menu_stop_otr_session" android:visible="true"
 		android:title="@string/chat_menu_stop_otr_session" />
 </menu>
--- a/res/values/strings.xml	Tue Dec 07 22:57:56 2010 +0100
+++ b/res/values/strings.xml	Wed Dec 08 21:43:17 2010 +0100
@@ -250,6 +250,7 @@
 	<string name="chat_menu_contacts_list">Contacts list</string>
 	<string name="chat_menu_change_chat">Switch chat</string>
 	<string name="chat_menu_start_otr_session">Start OTR session</string>
+	<string name="chat_menu_listen_otr_session">Listen for OTR session</string>
 	<string name="chat_menu_stop_otr_session">Stop OTR session</string>
 	<string name="chat_dialog_change_chat_title">Opened chats</string>
 	<string name="chat_menu_close_chat">Close this chat</string>
--- a/src/com/beem/project/beem/otr/BeemOtrEngineHostImpl.java	Tue Dec 07 22:57:56 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,80 +0,0 @@
-package com.beem.project.beem.otr;
-
-import java.security.KeyPair;
-import java.security.KeyPairGenerator;
-import java.security.NoSuchAlgorithmException;
-import java.util.HashMap;
-import java.util.Map;
-
-import net.java.otr4j.OtrEngineHost;
-import net.java.otr4j.OtrPolicy;
-import net.java.otr4j.OtrPolicyImpl;
-import net.java.otr4j.session.SessionID;
-
-import com.beem.project.beem.service.ChatAdapter;
-
-public class BeemOtrEngineHostImpl implements OtrEngineHost {
-
-	//Map of chat, needed because of the message injection
-	private final Map<SessionID, ChatAdapter> mChats = new HashMap<SessionID, ChatAdapter>();
-
-	//We will have a global policy for Beem as long as we won't need to modify the policy per chat.
-	private final OtrPolicy mGlobalPolicy = new OtrPolicyImpl(OtrPolicy.ALLOW_V2 | OtrPolicy.ERROR_START_AKE);
-	
-	private Map<SessionID, KeyPair> mKeys = new HashMap<SessionID, KeyPair>();
-
-	/*
-	 * We must call addChat before stating a new otr session because we will need the chat instance for message injection
-	 */
-	public void addChat(final SessionID sessionID, final ChatAdapter chat) {
-		mChats.put(sessionID, chat);
-	}
-
-	/*
-	 * We must remove the chat from the map after we ended the corresponding otr session.
-	 */
-	public void removeChat(final SessionID sessionID) {
-		mChats.remove(sessionID);
-	}
-
-	@Override
-	public void injectMessage(SessionID sessionID, String msg) {
-		ChatAdapter chat = mChats.get(sessionID);
-		chat.sendMessage(msg);		
-	}
-
-	@Override
-	public void showWarning(SessionID sessionID, String warning) {
-		// TODO Auto-generated method stub
-	}
-
-	@Override
-	public void showError(SessionID sessionID, String error) {
-		// TODO Auto-generated method stub
-	}
-
-	@Override
-	public OtrPolicy getSessionPolicy(SessionID sessionID) {
-		return mGlobalPolicy;
-	}
-
-	@Override
-	public KeyPair getKeyPair(SessionID sessionID) {	
-		KeyPair key = mKeys.get(sessionID);
-
-		if (key == null) {
-			KeyPairGenerator kg;
-			try {
-				kg = KeyPairGenerator.getInstance("DSA");
-				key = kg.genKeyPair();
-				mKeys.put(sessionID, key);
-			} catch (NoSuchAlgorithmException e) {
-				e.printStackTrace();
-				return null;
-			}
-		}
-
-		return key;
-	}
-
-}
--- a/src/com/beem/project/beem/otr/BeemOtrManager.java	Tue Dec 07 22:57:56 2010 +0100
+++ b/src/com/beem/project/beem/otr/BeemOtrManager.java	Wed Dec 08 21:43:17 2010 +0100
@@ -1,31 +1,112 @@
 package com.beem.project.beem.otr;
 
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.util.HashMap;
+import java.util.Map;
+
+import net.java.otr4j.OtrEngine;
+import net.java.otr4j.OtrEngineHost;
+import net.java.otr4j.OtrEngineImpl;
+import net.java.otr4j.OtrEngineListener;
+import net.java.otr4j.OtrPolicy;
+import net.java.otr4j.OtrPolicyImpl;
+import net.java.otr4j.session.SessionID;
+import android.util.Log;
+
 import com.beem.project.beem.service.ChatAdapter;
 
-import net.java.otr4j.OtrEngine;
-import net.java.otr4j.OtrEngineImpl;
-import net.java.otr4j.session.SessionID;
+public class BeemOtrManager implements OtrEngineHost {
+
+    private static final String TAG = "BeemOtrEngineHostImpl";
+    private static final BeemOtrManager INSTANCE = new BeemOtrManager();
+    
+    private OtrEngine mOtrEngine = new OtrEngineImpl(INSTANCE);;
+
+    //Map of chat, needed because of the message injection
+    private final Map<SessionID, ChatAdapter> mChats = new HashMap<SessionID, ChatAdapter>();
+
+    //We will have a global policy for Beem as long as we won't need to modify the policy per chat.
+    private final OtrPolicy mGlobalPolicy = new OtrPolicyImpl(OtrPolicy.ALLOW_V2 | OtrPolicy.ERROR_START_AKE);
+
+    private final OtrEngineListener mOtrListener = new BeemOtrListener();
+
+    private Map<SessionID, KeyPair> mKeys = new HashMap<SessionID, KeyPair>();
 
-public class BeemOtrManager {
+    public OtrEngine getOtrManager() {
+	return mOtrEngine;
+    }
+
+    // Private constructor prevents instantiation from other classes
+    private BeemOtrManager() { 
+	mOtrEngine.addOtrEngineListener(mOtrListener);
+    }
+
+    public static BeemOtrManager getInstance() {
+	return INSTANCE;
+    }
+
+    /*
+     * We must call addChat before stating a new otr session because we will need the chat instance for message injection
+     */
+    public void addChat(final SessionID sessionID, final ChatAdapter chat) {
+	mChats.put(sessionID, chat);
+    }
 
-	private static final BeemOtrEngineHostImpl mOtrHost= new BeemOtrEngineHostImpl();
-	private static OtrEngine mOtrEngine = new OtrEngineImpl(mOtrHost);
-	
-	public static OtrEngine getOtrManager() {
-		return mOtrEngine;
+    /*
+     * We must remove the chat from the map after we ended the corresponding otr session.
+     */
+    public void removeChat(final SessionID sessionID) {
+	mChats.remove(sessionID);
+    }
+
+    @Override
+    public void injectMessage(SessionID sessionID, String msg) {
+	ChatAdapter chat = mChats.get(sessionID);
+	chat.sendMessage(msg);		
+    }
+
+    @Override
+    public void showWarning(SessionID sessionID, String warning) {
+	Log.d(TAG, "Warning for "+sessionID + " : "+warning);
+    }
+
+    @Override
+    public void showError(SessionID sessionID, String error) {
+	Log.d(TAG, "Error for "+sessionID + " : "+error);
+    }
+
+    @Override
+    public OtrPolicy getSessionPolicy(SessionID sessionID) {
+	return mGlobalPolicy;
+    }
+
+    @Override
+    public KeyPair getKeyPair(SessionID sessionID) {	
+	KeyPair key = mKeys.get(sessionID);
+
+	if (key == null) {
+	    KeyPairGenerator kg;
+	    try {
+		kg = KeyPairGenerator.getInstance("DSA");
+		key = kg.genKeyPair();
+		mKeys.put(sessionID, key);
+	    } catch (NoSuchAlgorithmException e) {
+		e.printStackTrace();
+		return null;
+	    }
 	}
-	
-	/*
-	 * We must call addChat before stating a new otr session because we will need the chat instance for message injection
-	 */
-	public static void addChat(final SessionID sessionID, final ChatAdapter chat) {
-		mOtrHost.addChat(sessionID, chat);
+
+	return key;
+    }
+
+    private class BeemOtrListener implements OtrEngineListener {
+
+	@Override
+	public void sessionStatusChanged(final SessionID sessionID) {
+	    Log.d(TAG, "OTR Status changed for " + sessionID + " : " + mOtrEngine.getSessionStatus(sessionID));
+
 	}
-	
-	/*
-	 * We must remove the chat from the map after we ended the corresponding otr session.
-	 */
-	public static void removeChat(final SessionID sessionID) {
-		mOtrHost.removeChat(sessionID);
-	}
+    }
 }
--- a/src/com/beem/project/beem/service/ChatAdapter.java	Tue Dec 07 22:57:56 2010 +0100
+++ b/src/com/beem/project/beem/service/ChatAdapter.java	Wed Dec 08 21:43:17 2010 +0100
@@ -40,7 +40,7 @@
     Flavien Astraud, November 26, 2009
     Head of the EIP Laboratory.
 
-*/
+ */
 package com.beem.project.beem.service;
 
 import java.util.Collections;
@@ -109,17 +109,17 @@
 	String msgBody = message.getBody();
 	send.setTo(message.getTo());
 	Log.w(TAG, "message to " + message.getTo());
-	
+
 	if (mOtrSessionId != null) {
-		String body;
-		try {
-			body = BeemOtrManager.getOtrManager().transformSending(mOtrSessionId, msgBody);
-			msgBody = body;
-		} catch (OtrException e) {
-			e.printStackTrace();
-		}
+	    String body;
+	    try {
+		body = BeemOtrManager.getInstance().getOtrManager().transformSending(mOtrSessionId, msgBody);
+		msgBody = body;
+	    } catch (OtrException e) {
+		e.printStackTrace();
+	    }
 	}
-		
+
 	send.setBody(msgBody);
 	send.setThread(message.getThread());
 	send.setSubject(message.getSubject());
@@ -134,15 +134,15 @@
 	    e.printStackTrace();
 	}
     }
-    
+
     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();
-		}
+	Message msgToSend = new Message(mParticipant.getJIDWithRes(), Message.MSG_TYPE_CHAT);
+	msgToSend.setBody(msg);
+	try {
+	    sendMessage(msgToSend);
+	} catch (RemoteException e) {
+	    e.printStackTrace();
+	}
     }
 
     /**
@@ -235,17 +235,17 @@
 	public void processMessage(Chat chat, org.jivesoftware.smack.packet.Message message) {
 	    Message  msg = new Message(message);
 	    Log.d(TAG, "new msg " + msg.getBody());
-	    
+
 	    if (mOtrSessionId != null) {
-	    	String body;
-			try {
-				body = BeemOtrManager.getOtrManager().transformReceiving(mOtrSessionId, msg.getBody());
-				msg.setBody(body);
-			} catch (OtrException e) {
-				e.printStackTrace();
-			}			
-		}
-	    	
+		String body;
+		try {
+		    body = BeemOtrManager.getInstance().getOtrManager().transformReceiving(mOtrSessionId, msg.getBody());
+		    msg.setBody(body);
+		} catch (OtrException e) {
+		    e.printStackTrace();
+		}			
+	    }
+
 	    //TODO add que les message pas de type errors
 	    ChatAdapter.this.addMessage(msg);
 	    final int n = mRemoteListeners.beginBroadcast();
@@ -279,7 +279,7 @@
 	    }
 	    mRemoteListeners.finishBroadcast();
 	}
-	
+
 	/**
 	 * This method is executed when the otr session status change.
 	 * @param otrState the new state of otr session.
@@ -299,34 +299,47 @@
 	}
     }
 
-	@Override
-	public void startOtrSession() throws RemoteException {
-		if (mOtrSessionId != null)
-			return ;
+    @Override
+    public void startOtrSession() throws RemoteException {
+	if (mOtrSessionId != null)
+	    return ;
 
-		mOtrSessionId = new SessionID(mParticipant.getJIDWithRes(), mParticipant.getJID(), PROTOCOL);
-		try {
-			BeemOtrManager.addChat(mOtrSessionId, this);
-			BeemOtrManager.getOtrManager().startSession(mOtrSessionId);
-		} catch (OtrException e) {
-			e.printStackTrace();
-			throw new RemoteException();
-		}
+	mOtrSessionId = new SessionID(mParticipant.getJIDWithRes(), mParticipant.getJID(), PROTOCOL);
+	try {
+	    BeemOtrManager.getInstance().addChat(mOtrSessionId, this);
+	    BeemOtrManager.getInstance().getOtrManager().startSession(mOtrSessionId);
+	} catch (OtrException e) {
+	    mOtrSessionId = null;
+	    e.printStackTrace();
+	    throw new RemoteException();
 	}
+    }
+
+    @Override
+    public void endOtrSession() throws RemoteException {
+	if (mOtrSessionId == null)
+	    return ;
 
-	@Override
-	public void endOtrSession() throws RemoteException {
-		if (mOtrSessionId == null)
-			return ;
-		
-		try {
-			BeemOtrManager.getOtrManager().endSession(mOtrSessionId);			
-		} catch (OtrException e) {
-			e.printStackTrace();
-			throw new RemoteException();
-		}
-		BeemOtrManager.removeChat(mOtrSessionId);
-		mOtrSessionId = null;
+	try {
+	    BeemOtrManager.getInstance().getOtrManager().endSession(mOtrSessionId);			
+	} catch (OtrException e) {
+	    e.printStackTrace();
+	    throw new RemoteException();
 	}
+	BeemOtrManager.getInstance().removeChat(mOtrSessionId);
+	mOtrSessionId = null;
+    }
+
+    @Override
+    public void listenOtrSession() throws RemoteException {
+	if (mOtrSessionId != null)
+	    return ;
+
+	mOtrSessionId = new SessionID(mParticipant.getJIDWithRes(), mParticipant.getJID(), PROTOCOL);
+	BeemOtrManager.getInstance().addChat(mOtrSessionId, this);
+	//OtrEngineImpl will make a call to "this.getSession(sessionID)" which will instantiate our session.
+	BeemOtrManager.getInstance().getOtrManager().getSessionStatus(mOtrSessionId);
+
+    }
 }
 
--- a/src/com/beem/project/beem/service/aidl/IChat.aidl	Tue Dec 07 22:57:56 2010 +0100
+++ b/src/com/beem/project/beem/service/aidl/IChat.aidl	Wed Dec 08 21:43:17 2010 +0100
@@ -87,13 +87,18 @@
 	List<Message> getMessages();
 	
 	/**
-	 * Try to start an OTR session for the current chat.
+	 * Try to start an OTR session.
 	 */
 	void startOtrSession();
 	
 	/**
-	 * Stop the OTR session of the current chat.
+	 * Stop the OTR session.
 	 */
 	void endOtrSession();
+	
+	/**
+	 * Listen for an incoming OTR session.
+	 */
+	void listenOtrSession();
 
 }
--- a/src/com/beem/project/beem/ui/Chat.java	Tue Dec 07 22:57:56 2010 +0100
+++ b/src/com/beem/project/beem/ui/Chat.java	Wed Dec 08 21:43:17 2010 +0100
@@ -40,7 +40,7 @@
     Flavien Astraud, November 26, 2009
     Head of the EIP Laboratory.
 
-*/
+ */
 package com.beem.project.beem.ui;
 
 import java.text.DateFormat;
@@ -289,35 +289,47 @@
 		this.finish();
 		break;
 	    case R.id.chat_menu_start_otr_session:
-			try {
-				Log.d(TAG, "opened chats = " + mChat + " for "+mContact);
-				if (mChat == null) {
-					mChat = mChatManager.createChat(mContact, mMessageListener);
-					if (mChat != null) {
-					    mChat.setOpen(true);
-					}
-					
-				}
-			   mChat.startOtrSession();
-			} catch (RemoteException e) {
-			    Log.e(TAG, e.getMessage());
+		try {
+		    Log.d(TAG, "opened otr chats = " + mChat + " for "+mContact);
+		    if (mChat == null) {
+			mChat = mChatManager.createChat(mContact, mMessageListener);
+			if (mChat != null) {
+			    mChat.setOpen(true);
 			}
-			break;
+		    }
+		    mChat.startOtrSession();
+		} catch (RemoteException e) {
+		    Log.e(TAG, e.getMessage());
+		}
+		break;
+	    case R.id.chat_menu_listen_otr_session:
+		try {
+		    Log.d(TAG, "listen otr chats = " + mChat + " for "+mContact);
+		    if (mChat == null) {
+			mChat = mChatManager.createChat(mContact, mMessageListener);
+			if (mChat != null) {
+			    mChat.setOpen(true);
+			}
+		    }
+		    mChat.listenOtrSession();
+		} catch (RemoteException e) {
+		    Log.e(TAG, e.getMessage());
+		}
+		break;
 	    case R.id.chat_menu_stop_otr_session:
-			try {
-				Log.d(TAG, "opened chats = " + mChat + " for "+mContact);
-				if (mChat == null) {
-				    mChat = mChatManager.createChat(mContact, mMessageListener);
-					if (mChat != null) {
-					    mChat.setOpen(true);
-					}
-					
-				}
-			   mChat.endOtrSession();
-			} catch (RemoteException e) {
-			    Log.e(TAG, e.getMessage());
+		try {
+		    Log.d(TAG, "close otr chats = " + mChat + " for "+mContact);
+		    if (mChat == null) {
+			mChat = mChatManager.createChat(mContact, mMessageListener);
+			if (mChat != null) {
+			    mChat.setOpen(true);
 			}
-			break;
+		    }
+		    mChat.endOtrSession();
+		} catch (RemoteException e) {
+		    Log.e(TAG, e.getMessage());
+		}
+		break;
 	    default:
 		return false;
 	}
@@ -586,7 +598,7 @@
 		    mContactOtrState.setText(text);
 		}
 	    });
-		
+
 	}
     }