up
authorNikita Kozlov <nikita@mbdsys.com>
Tue, 28 Dec 2010 16:33:33 +0100
changeset 819 3e4fb7a4c39f
parent 818 75ed181c8960
child 895 b2e1b45382a4
up
default.properties
src/com/beem/project/beem/otr/BeemOtrManager.java
src/com/beem/project/beem/service/ChatAdapter.java
--- a/default.properties	Mon Dec 13 12:13:56 2010 +0100
+++ b/default.properties	Tue Dec 28 16:33:33 2010 +0100
@@ -10,4 +10,4 @@
 # Indicates whether an apk should be generated for each density.
 split.density=false
 # Project target.
-target=android-4
+target=android-8
--- a/src/com/beem/project/beem/otr/BeemOtrManager.java	Mon Dec 13 12:13:56 2010 +0100
+++ b/src/com/beem/project/beem/otr/BeemOtrManager.java	Tue Dec 28 16:33:33 2010 +0100
@@ -1,5 +1,6 @@
 package com.beem.project.beem.otr;
 
+import java.io.IOException;
 import java.security.KeyPair;
 import java.security.KeyPairGenerator;
 import java.security.NoSuchAlgorithmException;
@@ -10,6 +11,7 @@
 import net.java.otr4j.OtrEngineHost;
 import net.java.otr4j.OtrEngineImpl;
 import net.java.otr4j.OtrEngineListener;
+import net.java.otr4j.OtrKeyManagerImpl;
 import net.java.otr4j.OtrPolicy;
 import net.java.otr4j.OtrPolicyImpl;
 import net.java.otr4j.session.SessionID;
@@ -19,97 +21,103 @@
 
 public class BeemOtrManager implements OtrEngineHost {
 
-    private static final String TAG = "BeemOtrEngineHostImpl";
-    private static BeemOtrManager INSTANCE;
-    
-    private OtrEngine mOtrEngine;
+	private static final String TAG = "BeemOtrEngineHostImpl";
+	private static BeemOtrManager INSTANCE;
+
+	private OtrEngine mOtrEngine;
+	private OtrKeyManagerImpl mOtrKeyManager;
 
-    //Map of chat, needed because of the message injection
-    private final Map<SessionID, ChatAdapter> mChats = new HashMap<SessionID, ChatAdapter>();
+	//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 static final OtrPolicy mGlobalPolicy = new OtrPolicyImpl(OtrPolicy.ALLOW_V2 | OtrPolicy.ERROR_START_AKE);
+	//We will have a global policy for Beem as long as we won't need to modify the policy per chat.
+	private static final OtrPolicy mGlobalPolicy = new OtrPolicyImpl(OtrPolicy.ALLOW_V2 | OtrPolicy.ERROR_START_AKE);
 
 
 
-    private Map<SessionID, KeyPair> mKeys = new HashMap<SessionID, KeyPair>();
-
-    public OtrEngine getOtrManager() {
-	return mOtrEngine;
-    }
+	private Map<SessionID, KeyPair> mKeys = new HashMap<SessionID, KeyPair>();
 
-    // Private constructor prevents instantiation from other classes
-    private BeemOtrManager() {  
-	mOtrEngine = new OtrEngineImpl(this);;
-	mOtrEngine.addOtrEngineListener( new BeemOtrListener());
-    }
+	public OtrEngine getOtrManager() {
+		return mOtrEngine;
+	}
 
-    public static BeemOtrManager getInstance() {
-	if (INSTANCE == null)
-		INSTANCE = new BeemOtrManager();
-	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);
-    }
-
-    /*
-     * We must remove the chat from the map after we ended the corresponding otr session.
-     */
-    public void removeChat(final SessionID sessionID) {
-	mChats.remove(sessionID);
-    }
+	// Private constructor prevents instantiation from other classes
+	private BeemOtrManager() {  
+		mOtrEngine = new OtrEngineImpl(this);;
+		mOtrEngine.addOtrEngineListener(new BeemOtrListener());
+		try {
+			mOtrKeyManager = new OtrKeyManagerImpl("/sdcard/beem.keystore");
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
 
-    @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);
-    }
+	public static BeemOtrManager getInstance() {
+		if (INSTANCE == null)
+			INSTANCE = new BeemOtrManager();
+		return INSTANCE;
+	}
 
-    @Override
-    public OtrPolicy getSessionPolicy(SessionID sessionID) {
-	return mGlobalPolicy;
-    }
-
-    @Override
-    public KeyPair getKeyPair(SessionID sessionID) {	
-	KeyPair key = mKeys.get(sessionID);
+	/*
+	 * 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);
+	}
 
-	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 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);		
 	}
 
-	return key;
-    }
+	@Override
+	public void showWarning(SessionID sessionID, String warning) {
+		Log.d(TAG, "Warning for "+sessionID + " : "+warning);
+	}
 
-    private class BeemOtrListener implements OtrEngineListener {
+	@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 void sessionStatusChanged(final SessionID sessionID) {
-	    Log.d(TAG, "OTR Status changed for " + sessionID + " : " + mOtrEngine.getSessionStatus(sessionID));
-	mChats.get(sessionID).otrStateChanged(mOtrEngine.getSessionStatus(sessionID).toString());	
+	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;
 	}
-    }
+
+	private class BeemOtrListener implements OtrEngineListener {
+
+		@Override
+		public void sessionStatusChanged(final SessionID sessionID) {
+			Log.d(TAG, "OTR Status changed for " + sessionID + " : " + mOtrEngine.getSessionStatus(sessionID));
+			mChats.get(sessionID).otrStateChanged(mOtrEngine.getSessionStatus(sessionID).toString());	
+		}
+	}
 }
--- a/src/com/beem/project/beem/service/ChatAdapter.java	Mon Dec 13 12:13:56 2010 +0100
+++ b/src/com/beem/project/beem/service/ChatAdapter.java	Tue Dec 28 16:33:33 2010 +0100
@@ -68,280 +68,284 @@
  * @author darisk
  */
 public class ChatAdapter extends IChat.Stub {
-    private static final int HISTORY_MAX_SIZE = 50;
-    private static final String TAG = "ChatAdapter";
-    private static final String PROTOCOL = "XMPP";
+	private static final int HISTORY_MAX_SIZE = 50;
+	private static final String TAG = "ChatAdapter";
+	private static final String PROTOCOL = "XMPP";
+
+	private final Chat mAdaptee;
+	private final Contact mParticipant;
+	private String mState;
+	private boolean mIsOpen;
+	private final List<Message> mMessages;
+	private final RemoteCallbackList<IMessageListener> mRemoteListeners = new RemoteCallbackList<IMessageListener>();
+	private final MsgListener mMsgListener = new MsgListener();
+	private SessionID mOtrSessionId;
 
-    private final Chat mAdaptee;
-    private final Contact mParticipant;
-    private String mState;
-    private boolean mIsOpen;
-    private final List<Message> mMessages;
-    private final RemoteCallbackList<IMessageListener> mRemoteListeners = new RemoteCallbackList<IMessageListener>();
-    private final MsgListener mMsgListener = new MsgListener();
-    private SessionID mOtrSessionId;
+	/**
+	 * Constructor.
+	 * @param chat The chat to adapt
+	 */
+	public ChatAdapter(final Chat chat) {
+		mAdaptee = chat;
+		mParticipant = new Contact(chat.getParticipant());
+		mMessages = new LinkedList<Message>();
+		mAdaptee.addMessageListener(mMsgListener);
+	}
 
-    /**
-     * Constructor.
-     * @param chat The chat to adapt
-     */
-    public ChatAdapter(final Chat chat) {
-	mAdaptee = chat;
-	mParticipant = new Contact(chat.getParticipant());
-	mMessages = new LinkedList<Message>();
-	mAdaptee.addMessageListener(mMsgListener);
-    }
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public Contact getParticipant() throws RemoteException {
+		return mParticipant;
+	}
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public Contact getParticipant() throws RemoteException {
-	return mParticipant;
-    }
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void sendMessage(com.beem.project.beem.service.Message message) throws RemoteException {
+		org.jivesoftware.smack.packet.Message send = new org.jivesoftware.smack.packet.Message();
+		String msgBody = message.getBody();
+		send.setTo(message.getTo());
+		Log.w(TAG, "message to " + message.getTo());
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void sendMessage(com.beem.project.beem.service.Message message) throws RemoteException {
-	org.jivesoftware.smack.packet.Message send = new org.jivesoftware.smack.packet.Message();
-	String msgBody = message.getBody();
-	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;
+			} catch (OtrException e) {
+				e.printStackTrace();
+			}
+		}
 
-	if (mOtrSessionId != null) {
-	    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());
+		send.setType(org.jivesoftware.smack.packet.Message.Type.chat);
+		// TODO gerer les messages contenant des XMPPError
+		// send.set
+		try {
+			mAdaptee.sendMessage(send);
+			mMessages.add(message);
+		} catch (XMPPException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
 	}
 
-	send.setBody(msgBody);
-	send.setThread(message.getThread());
-	send.setSubject(message.getSubject());
-	send.setType(org.jivesoftware.smack.packet.Message.Type.chat);
-	// TODO gerer les messages contenant des XMPPError
-	// send.set
-	try {
-	    mAdaptee.sendMessage(send);
-	    mMessages.add(message);
-	} catch (XMPPException e) {
-	    // TODO Auto-generated catch block
-	    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();
+		}
 	}
-    }
-
-    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();
-	}
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void addMessageListener(IMessageListener listen) {
-	if (listen != null)
-	    mRemoteListeners.register(listen);
-    }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void removeMessageListener(IMessageListener listen) {
-	if (listen != null) {
-	    mRemoteListeners.unregister(listen);
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void addMessageListener(IMessageListener listen) {
+		if (listen != null)
+			mRemoteListeners.register(listen);
 	}
-    }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public String getState() throws RemoteException {
-	return mState;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void setState(String state) throws RemoteException {
-	mState = state;
-    }
-
-    /**
-     * Get the adaptee for the Chat.
-     * @return The real chat object
-     */
-    public Chat getAdaptee() {
-	return mAdaptee;
-    }
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void removeMessageListener(IMessageListener listen) {
+		if (listen != null) {
+			mRemoteListeners.unregister(listen);
+		}
+	}
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void setOpen(boolean isOpen) {
-	this.mIsOpen = isOpen;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public boolean isOpen() {
-	return mIsOpen;
-    }
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public String getState() throws RemoteException {
+		return mState;
+	}
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public List<Message> getMessages() throws RemoteException {
-	return Collections.unmodifiableList(mMessages);
-    }
-
-    /**
-     * Add a message in the chat history.
-     * @param msg the message to add
-     */
-    void addMessage(Message msg) {
-	if (mMessages.size() == HISTORY_MAX_SIZE)
-	    mMessages.remove(0);
-	mMessages.add(msg);
-    }
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void setState(String state) throws RemoteException {
+		mState = state;
+	}
 
-    /**
-     * Listener.
-     */
-    private class MsgListener implements ChatStateListener {
 	/**
-	 * Constructor.
+	 * Get the adaptee for the Chat.
+	 * @return The real chat object
 	 */
-	public MsgListener() { }
-
-	@Override
-	public void processMessage(Chat chat, org.jivesoftware.smack.packet.Message message) {
-	    Message  msg = new Message(message);
-	    Log.d(TAG, "new msg " + msg.getBody());
+	public Chat getAdaptee() {
+		return mAdaptee;
+	}
 
-	    if (mOtrSessionId != null) {
-		String body;
-		try {
-		    body = BeemOtrManager.getInstance().getOtrManager().transformReceiving(mOtrSessionId, msg.getBody());
-		    msg.setBody(body);
-		} catch (OtrException e) {
-		    e.printStackTrace();
-		}			
-	    }
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void setOpen(boolean isOpen) {
+		this.mIsOpen = isOpen;
+	}
 
-	    //TODO add que les message pas de type errors
-	    ChatAdapter.this.addMessage(msg);
-	    final int n = mRemoteListeners.beginBroadcast();
-	    for (int i = 0; i < n; i++) {
-		IMessageListener listener = mRemoteListeners.getBroadcastItem(i);
-		try {
-		    if (listener != null)
-			listener.processMessage(ChatAdapter.this, msg);
-		} catch (RemoteException e) {
-		    Log.w(TAG, "Error while diffusing message to listener", e);
-		}
-	    }
-	    mRemoteListeners.finishBroadcast();
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public boolean isOpen() {
+		return mIsOpen;
 	}
 
 	/**
 	 * {@inheritDoc}
 	 */
 	@Override
-	public void stateChanged(Chat chat, ChatState state) {
-	    mState = state.name();
-	    final int n = mRemoteListeners.beginBroadcast();
+	public List<Message> getMessages() throws RemoteException {
+		return Collections.unmodifiableList(mMessages);
+	}
 
-	    for (int i = 0; i < n; i++) {
-		IMessageListener listener = mRemoteListeners.getBroadcastItem(i);
-		try {
-		    listener.stateChanged(ChatAdapter.this);
-		} catch (RemoteException e) {
-		    Log.w(TAG, e.getMessage());
-		}
-	    }
-	    mRemoteListeners.finishBroadcast();
+	/**
+	 * Add a message in the chat history.
+	 * @param msg the message to add
+	 */
+	void addMessage(Message msg) {
+		if (mMessages.size() == HISTORY_MAX_SIZE)
+			mMessages.remove(0);
+		mMessages.add(msg);
 	}
 
-	
-    }
-    	/**
+	/**
+	 * Listener.
+	 */
+	private class MsgListener implements ChatStateListener {
+		/**
+		 * Constructor.
+		 */
+		public MsgListener() { }
+
+		@Override
+		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.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();
+			for (int i = 0; i < n; i++) {
+				IMessageListener listener = mRemoteListeners.getBroadcastItem(i);
+				try {
+					if (listener != null)
+						listener.processMessage(ChatAdapter.this, msg);
+				} catch (RemoteException e) {
+					Log.w(TAG, "Error while diffusing message to listener", e);
+				}
+			}
+			mRemoteListeners.finishBroadcast();
+		}
+
+		/**
+		 * {@inheritDoc}
+		 */
+		@Override
+		public void stateChanged(Chat chat, ChatState state) {
+			mState = state.name();
+			final int n = mRemoteListeners.beginBroadcast();
+
+			for (int i = 0; i < n; i++) {
+				IMessageListener listener = mRemoteListeners.getBroadcastItem(i);
+				try {
+					listener.stateChanged(ChatAdapter.this);
+				} catch (RemoteException e) {
+					Log.w(TAG, e.getMessage());
+				}
+			}
+			mRemoteListeners.finishBroadcast();
+		}
+
+
+	}
+	/**
 	 * This method is executed when the otr session status change.
 	 * @param otrState the new state of otr session.
 	 */
-public	void otrStateChanged(final String otrState) {
-	    final int n = mRemoteListeners.beginBroadcast();
+	public	void otrStateChanged(final String otrState) {
+		final int n = mRemoteListeners.beginBroadcast();
 
-	    for (int i = 0; i < n; i++) {
-		IMessageListener listener = mRemoteListeners.getBroadcastItem(i);
-		try {
-		    listener.otrStateChanged(otrState);
-		} catch (RemoteException e) {
-		    Log.w(TAG, e.getMessage());
+		if ("FINISHED".equals(otrState)) {
+			BeemOtrManager.getInstance().removeChat(mOtrSessionId);
+			mOtrSessionId = null;
 		}
-	    }
-	    mRemoteListeners.finishBroadcast();		
-	}
-    }
-
-    @Override
-    public void startOtrSession() throws RemoteException {
-	if (mOtrSessionId != null)
-	    return ;
-
-	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();
+		for (int i = 0; i < n; i++) {
+			IMessageListener listener = mRemoteListeners.getBroadcastItem(i);
+			try {
+				listener.otrStateChanged(otrState);
+			} catch (RemoteException e) {
+				Log.w(TAG, e.getMessage());
+			}
+		}
+		mRemoteListeners.finishBroadcast();
 	}
-    }
+
+
+	@Override
+	public void startOtrSession() throws RemoteException {
+		if (mOtrSessionId != null)
+			return ;
 
-    @Override
-    public void endOtrSession() throws RemoteException {
-	if (mOtrSessionId == null)
-	    return ;
-
-	try {
-	    BeemOtrManager.getInstance().getOtrManager().endSession(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();
+		}
 	}
-	BeemOtrManager.getInstance().removeChat(mOtrSessionId);
-	mOtrSessionId = null;
-    }
+
+	@Override
+	public void endOtrSession() throws RemoteException {
+		if (mOtrSessionId == null)
+			return ;
 
-    @Override
-    public void listenOtrSession() throws RemoteException {
-	if (mOtrSessionId != null)
-	    return ;
+		try {
+			BeemOtrManager.getInstance().getOtrManager().endSession(mOtrSessionId);			
+		} catch (OtrException e) {
+			e.printStackTrace();
+			throw new RemoteException();
+		}
+		BeemOtrManager.getInstance().removeChat(mOtrSessionId);
+		mOtrSessionId = null;
+	}
 
-	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);
+	@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);
+
+	}
 }