Create the Chat only when we send or receive a message
authorDa Risk <darisk972@gmail.com>
Thu, 18 Feb 2010 15:43:26 +0100
changeset 674 af23f43cf6e3
parent 673 5884f3bd703a
child 675 764eded78962
Create the Chat only when we send or receive a message
src/com/beem/project/beem/service/BeemChatManager.java
src/com/beem/project/beem/service/aidl/IChatManager.aidl
src/com/beem/project/beem/ui/Chat.java
--- a/src/com/beem/project/beem/service/BeemChatManager.java	Mon Feb 15 23:37:25 2010 +0100
+++ b/src/com/beem/project/beem/service/BeemChatManager.java	Thu Feb 18 15:43:26 2010 +0100
@@ -236,6 +236,8 @@
     public void destroyChat(IChat chat) throws RemoteException {
 	// Can't remove it. otherwise we will lose all futur message in this chat
 	// chat.removeMessageListener(mChatListener);
+	if (chat == null)
+	    return;
 	deleteChatNotification(chat);
 	mChats.remove(chat.getParticipant().getJID());
     }
@@ -267,6 +269,12 @@
 	return res;
     }
 
+    @Override
+    public ChatAdapter getChat(Contact contact) {
+	String key = StringUtils.parseBareAddress(contact.getJID());
+	return mChats.get(key);
+    }
+
     /**
      * This methods permits to retrieve the list of contacts who have an opened chat session with us.
      * @return An List containing Contact instances.
--- a/src/com/beem/project/beem/service/aidl/IChatManager.aidl	Mon Feb 15 23:37:25 2010 +0100
+++ b/src/com/beem/project/beem/service/aidl/IChatManager.aidl	Thu Feb 18 15:43:26 2010 +0100
@@ -63,6 +63,12 @@
 	IChat createChat(in Contact contact, in IMessageListener listener);
 
 	/**
+	 * Get an existing Chat session with a contact.
+	 * @return null if the chat session does not exist.
+	 */
+	IChat getChat(in Contact contact);
+
+	/**
     	 * Destroy a chat session with a contact.
     	 * @param chat	the chat session
     	 */
@@ -85,5 +91,9 @@
 	 */
 	void removeChatCreationListener(in IChatManagerListener listener);
 
+	/**
+	 * Get a list of contact which we are currently chatting.
+	 * @return list of contact.
+	 */
 	List<Contact> getOpenedChatList();
 }
--- a/src/com/beem/project/beem/ui/Chat.java	Mon Feb 15 23:37:25 2010 +0100
+++ b/src/com/beem/project/beem/ui/Chat.java	Thu Feb 18 15:43:26 2010 +0100
@@ -90,6 +90,7 @@
 import com.beem.project.beem.service.aidl.IChat;
 import com.beem.project.beem.service.aidl.IChatManager;
 import com.beem.project.beem.service.aidl.IMessageListener;
+import com.beem.project.beem.service.aidl.IChatManagerListener;
 import com.beem.project.beem.service.aidl.IRoster;
 import com.beem.project.beem.service.aidl.IXmppFacade;
 import com.beem.project.beem.utils.BeemBroadcastReceiver;
@@ -124,6 +125,7 @@
     private IChat mChat;
     private IChatManager mChatManager;
     private final IMessageListener mMessageListener = new OnMessageListener();
+    private final IChatManagerListener mChatManagerListener = new ChatManagerListener();
     private MessagesListAdapter mMessagesListAdapter = new MessagesListAdapter();
 
     private final ServiceConnection mConn = new BeemServiceConnection();
@@ -169,6 +171,7 @@
     @Override
     protected void onResume() {
 	super.onResume();
+	mContact = new Contact(getIntent().getData());
 	if (mXmppFacade == null)
 	    bindService(SERVICE_INTENT, mConn, BIND_AUTO_CREATE);
     }
@@ -195,6 +198,8 @@
 	    }
 	    if (mRoster != null)
 		mRoster.removeRosterListener(mBeemRosterListener);
+	    if (mChatManager != null)
+		mChatManager.removeChatCreationListener(mChatManagerListener);
 	} catch (RemoteException e) {
 	    Log.e(TAG, e.getMessage());
 	}
@@ -324,10 +329,16 @@
 	    mChat.setOpen(false);
 	    mChat.removeMessageListener(mMessageListener);
 	}
-	mChat = mChatManager.createChat(contact, mMessageListener);
+	/* mChat = mChatManager.createChat(contact, mMessageListener);
 	mChat.setOpen(true);
 	mChatManager.deleteChatNotification(mChat);
-
+*/
+	mChat = mChatManager.getChat(contact);
+	if (mChat != null){
+	    mChat.setOpen(true);
+	    mChat.addMessageListener(mMessageListener);
+	    mChatManager.deleteChatNotification(mChat);	
+	}
 	mContact = mRoster.getContact(contact.getJID());
 	if (mContact == null)
 	    mContact = contact;
@@ -343,9 +354,11 @@
      */
     private void playRegisteredTranscript() throws RemoteException {
 	mListMessages.clear();
-	List<MessageText> msgList = convertMessagesList(mChat.getMessages());
-	mListMessages.addAll(msgList);
-	mMessagesListAdapter.notifyDataSetChanged();
+	if (mChat != null) {
+	    List<MessageText> msgList = convertMessagesList(mChat.getMessages());
+	    mListMessages.addAll(msgList);
+	    mMessagesListAdapter.notifyDataSetChanged();
+	}
     }
 
     /**
@@ -404,9 +417,10 @@
 	    try {
 		if ((mRoster = mXmppFacade.getRoster()) != null)
 		    mRoster.addRosterListener(mBeemRosterListener);
-		mContact = new Contact(getIntent().getData());
-		if ((mChatManager = mXmppFacade.getChatManager()) != null)
+		if ((mChatManager = mXmppFacade.getChatManager()) != null) {
+		    mChatManager.addChatCreationListener(mChatManagerListener);
 		    changeCurrentChat(mContact);
+		}
 	    } catch (RemoteException e) {
 		Log.e(TAG, e.getMessage());
 	    }
@@ -420,6 +434,7 @@
 	    mXmppFacade = null;
 	    try {
 		mRoster.removeRosterListener(mBeemRosterListener);
+		mChatManager.removeChatCreationListener(mChatManagerListener);
 	    } catch (RemoteException e) {
 		Log.e(TAG, e.getMessage());
 	    }
@@ -764,6 +779,10 @@
 	    msgToSend.setBody(inputContent);
 
 	    try {
+		if (mChat == null) {
+		    mChat = mChatManager.createChat(mContact, mMessageListener);
+		    mChat.setOpen(true);
+		}
 		mChat.sendMessage(msgToSend);
 	    } catch (RemoteException e) {
 		Log.e(TAG, e.getMessage());
@@ -780,4 +799,38 @@
 	    mInputField.setText(null);
 	}
     }
+
+    private class ChatManagerListener extends IChatManagerListener.Stub {
+
+	/**
+	 * Constructor.
+	 */
+	public ChatManagerListener() {
+	}
+
+	@Override
+	public void chatCreated(IChat chat, boolean locally) {
+	    if (locally)
+		return;
+	    Log.d(TAG, "Chat created not locally" );
+	    try {
+		String contactJid = StringUtils.parseBareAddress(mContact.getJID());
+		String chatJid = StringUtils.parseBareAddress(chat.getParticipant().getJID());
+		if (chatJid.equals(contactJid)){
+		    Log.d(TAG, "Switching chat contact was the same");
+		    // This should not be happened but to be sure
+		    if (mChat != null) {
+			mChat.setOpen(false);
+			mChat.removeMessageListener(mMessageListener);
+		    }
+		    mChat = chat;
+		    mChat.setOpen(true);
+		    mChat.addMessageListener(mMessageListener);
+		    mChatManager.deleteChatNotification(mChat);	
+		}
+	    } catch (RemoteException ex) {
+		Log.d(TAG, "A remote exception occurs during the creation of a chat", ex);
+	    }
+	}
+    }
 }