complete chat manager
authorDa Risk <darisk972@gmail.com>
Mon, 06 Apr 2009 21:52:03 +0200
changeset 75 b2500e2392f7
parent 74 ffe07347ceba
child 76 144e837ee4a6
complete chat manager
src/com/beem/project/beem/service/BeemChatManager.java
src/com/beem/project/beem/service/Message.java
--- a/src/com/beem/project/beem/service/BeemChatManager.java	Mon Apr 06 18:28:51 2009 +0200
+++ b/src/com/beem/project/beem/service/BeemChatManager.java	Mon Apr 06 21:52:03 2009 +0200
@@ -10,6 +10,7 @@
 import org.jivesoftware.smack.ChatManager;
 import org.jivesoftware.smack.ChatManagerListener;
 import org.jivesoftware.smack.MessageListener;
+import org.jivesoftware.smack.packet.Message;
 
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
@@ -31,10 +32,11 @@
      */
     public static final String TAG = "BeemChatManager";
     private ChatManager mAdaptee;
-    private Map<String, Chat> mChats = new HashMap<String, Chat>();
+    private Map<String, IChat> mChats = new HashMap<String, IChat>();
     private ChatListener mChatListener = new ChatListener();
     private RemoteCallbackList<IChatManagerListener> mRemoteChatCreationListeners =
 	new RemoteCallbackList<IChatManagerListener>();
+    private RemoteCallbackList<IMessageListener> mRemoteMessageListeners = new RemoteCallbackList<IMessageListener>();
 
     /**
      * Constructor.
@@ -52,8 +54,12 @@
      * @param listener listener to use for chat events on this chat session
      * @return the chat session
      */
-    public Chat createChat(String jid, MessageListener listener) {
-	return mAdaptee.createChat(jid, listener);
+    public IChat createChat(String jid, IMessageListener listener) {
+	mRemoteMessageListeners.register(listener);
+	if (mChats.containsKey(jid)) {
+	    return mChats.get(jid);
+	}
+	return new ChatAdapter( mAdaptee.createChat(jid, mChatListener));
     }
 
     /**
@@ -62,7 +68,7 @@
      * @param listener listener to use for chat events on this chat session
      * @return the chat session
      */
-    public Chat createChat(Contact contact, MessageListener listener) {
+    public IChat createChat(Contact contact, IMessageListener listener) {
 	String jid = contact.getJID();
 	return createChat(jid, listener);
     }
@@ -72,7 +78,6 @@
      */
     @Override
     public void addChatCreationListener(IChatManagerListener listener) throws RemoteException {
-	// TODO Auto-generated method stub
 	mRemoteChatCreationListeners.register(listener);
     }
 
@@ -80,25 +85,24 @@
      * {@inheritDoc}
      */
     @Override
-    public IChat createChat(Contact contact, IMessageListener listener) throws RemoteException {
-	// TODO Auto-generated method stub
-	return null;
+    public void removeChatCreationListener(IChatManagerListener listener) throws RemoteException {
+	mRemoteChatCreationListeners.unregister(listener);
     }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void removeChatCreationListener(IChatManagerListener listener) throws RemoteException {
-	// TODO Auto-generated method stub
-	mRemoteChatCreationListeners.unregister(listener);
+    
+    private IChat getChat(Chat chat) {
+	if (mChats.containsKey(chat.getParticipant())) {
+	    return mChats.get(chat.getParticipant());
+	}
+	IChat res = new ChatAdapter(chat);
+	mChats.put(chat.getParticipant(), res);
+	return res;
     }
 
     /**
      * A listener for all the chat creation event that happens on the connection.
      * @author darisk
      */
-    private class ChatListener implements ChatManagerListener {
+    private class ChatListener implements ChatManagerListener, MessageListener {
 
 	/**
 	 * Constructor.
@@ -112,15 +116,15 @@
 	 */
 	@Override
 	public void chatCreated(Chat chat, boolean locally) {
+	    IChat newchat = getChat(chat);
 	    if (!locally) {
-		mChats.put(chat.getParticipant(), chat);
+		mChats.put(chat.getParticipant(), newchat);
 	    }
 	    final int n = mRemoteChatCreationListeners.beginBroadcast();
 
 	    for (int i = 0; i < n; i++) {
 		IChatManagerListener listener = mRemoteChatCreationListeners.getBroadcastItem(i);
 		try {
-		    IChat newchat = new ChatAdapter(chat);
 		    listener.chatCreated(newchat, locally);
 		} catch (RemoteException e) {
 		    // The RemoteCallbackList will take care of removing the
@@ -130,6 +134,24 @@
 	    }
 	    mRemoteChatCreationListeners.finishBroadcast();
 	}
+
+	@Override
+	public void processMessage(Chat chat, Message message) {
+	    IChat newchat = getChat(chat);
+	   final int n = mRemoteMessageListeners.beginBroadcast();
+	   for (int i = 0; i < n; i++) {
+		IMessageListener listener = mRemoteMessageListeners.getBroadcastItem(i);
+		try {
+		    listener.processMessage(newchat, new com.beem.project.beem.service.Message(message));
+		    //listener.chatCreated(newchat, locally);
+		} catch (RemoteException e) {
+		    // The RemoteCallbackList will take care of removing the
+		    // dead listeners.
+		    Log.w(TAG, "Error while triggering remote connection listeners", e);
+		}
+	    }
+	    mRemoteMessageListeners.finishBroadcast();
+	}
     }
 
 }
--- a/src/com/beem/project/beem/service/Message.java	Mon Apr 06 18:28:51 2009 +0200
+++ b/src/com/beem/project/beem/service/Message.java	Mon Apr 06 21:52:03 2009 +0200
@@ -5,6 +5,7 @@
 
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.util.Log;
 
 /**
  * This class represents a instant message.
@@ -71,6 +72,27 @@
 	this(to, MSG_TYPE_CHAT);
     }
 
+    public Message(org.jivesoftware.smack.packet.Message smackMsg) {
+	this(smackMsg.getTo());
+	switch (smackMsg.getType()) {
+	    case chat:
+		mType = MSG_TYPE_CHAT;
+		break;
+	    case groupchat:
+		mType = MSG_TYPE_GROUP_CHAT;
+		break;
+	    case normal:
+		mType = MSG_TYPE_NORMAL;
+		break;
+	    default:
+		Log.w("BEEM_MESSAGE", "type de message non gerer" + smackMsg.getType());
+		break;
+	}
+	mBody = smackMsg.getBody();
+	mSubject = smackMsg.getSubject();
+	mThread = smackMsg.getThread();
+    }
+    
     /**
      * Construct a message from a parcel.
      * @param in parcel to use for construction