--- a/src/com/beem/project/beem/service/BeemChatManager.java Mon Apr 20 14:00:00 2009 +0200
+++ b/src/com/beem/project/beem/service/BeemChatManager.java Mon Apr 20 21:46:52 2009 +0200
@@ -11,6 +11,7 @@
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.packet.Message;
+import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.ChatState;
import org.jivesoftware.smackx.ChatStateListener;
@@ -64,12 +65,13 @@
*/
public IChat createChat(String jid, IMessageListener listener) {
mRemoteMessageListeners.register(listener);
- if (mChats.containsKey(jid)) {
- return mChats.get(jid);
+ String key = StringUtils.parseBareAddress(jid);
+ if (mChats.containsKey(key)) {
+ return mChats.get(key);
}
// create the chat. the adaptee will be add automatically in the map
- mAdaptee.createChat(jid, mChatListener);
- return mChats.get(jid);
+ mAdaptee.createChat(key, mChatListener);
+ return mChats.get(key);
}
/**
@@ -98,24 +100,25 @@
public void removeChatCreationListener(IChatManagerListener listener) throws RemoteException {
mRemoteChatCreationListeners.unregister(listener);
}
-
+
/**
* {@inheritDoc}
*/
@Override
public void destroyChat(IChat chat) throws RemoteException {
- //TODO gerer les resources egalement
- IChat c=mChats.remove(chat.getParticipant().getJID());
- if (c==null)
+ // TODO gerer les resources egalement
+ IChat c = mChats.remove(chat.getParticipant().getJID());
+ if (c == null)
Log.w(TAG, "CA devrait pas 1!!");
}
-
+
private IChat getChat(Chat chat) {
- if (mChats.containsKey(chat.getParticipant())) {
- return mChats.get(chat.getParticipant());
+ String key = StringUtils.parseBareAddress(chat.getParticipant());
+ if (mChats.containsKey(key)) {
+ return mChats.get(key);
}
IChat res = new ChatAdapter(chat);
- mChats.put(chat.getParticipant(), res);
+ mChats.put(key, res);
return res;
}
@@ -160,7 +163,7 @@
try {
String text = chat.getParticipant().getJID();
Notification notif = new Notification(com.beem.project.beem.R.drawable.logo, text, System
- .currentTimeMillis());
+ .currentTimeMillis());
notif.defaults = Notification.DEFAULT_ALL;
notif.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(mService, SendIM.class);
@@ -179,19 +182,20 @@
@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 {
+ try {
+ newchat.addToLastMessages(message.getBody());
+ final int n = mRemoteMessageListeners.beginBroadcast();
+ for (int i = 0; i < n; i++) {
+ IMessageListener listener = mRemoteMessageListeners.getBroadcastItem(i);
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();
+ } 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();
}
@Override
@@ -204,6 +208,4 @@
}
}
-
-
}
--- a/src/com/beem/project/beem/service/ChatAdapter.java Mon Apr 20 14:00:00 2009 +0200
+++ b/src/com/beem/project/beem/service/ChatAdapter.java Mon Apr 20 21:46:52 2009 +0200
@@ -4,11 +4,16 @@
package com.beem.project.beem.service;
import org.jivesoftware.smack.Chat;
+import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
+import org.jivesoftware.smack.packet.Message;
+import android.os.RemoteCallbackList;
import android.os.RemoteException;
+import android.util.Log;
import com.beem.project.beem.service.aidl.IChat;
+import com.beem.project.beem.service.aidl.IMessageListener;
/**
* An adapter for smack's Chat class.
@@ -18,12 +23,14 @@
private Chat mAdaptee;
private Contact mParticipant;
private String mState;
-
+ private StringBuffer mLastMessages;
+
/**
* Constructor.
* @param chat The chat to adapt
*/
public ChatAdapter(final Chat chat) {
+ mLastMessages = new StringBuffer();
mAdaptee = chat;
mParticipant = new Contact(chat.getParticipant());
}
@@ -40,7 +47,7 @@
* {@inheritDoc}
*/
@Override
- public void sendMessage(Message message) throws RemoteException {
+ public void sendMessage(com.beem.project.beem.service.Message message) throws RemoteException {
org.jivesoftware.smack.packet.Message send = new org.jivesoftware.smack.packet.Message();
send.setTo(message.getTo());
send.setBody(message.getBody());
@@ -70,5 +77,20 @@
public Chat getAdaptee() {
return mAdaptee;
}
-
+
+ @Override
+ public String getLastMessages() throws RemoteException {
+ return mLastMessages.toString();
+ }
+
+ @Override
+ public void addToLastMessages(String msg) throws RemoteException {
+ mLastMessages.append(msg).append('\n');
+ }
+
+ @Override
+ public void clearLastMessages() throws RemoteException {
+ mLastMessages.delete(0, mLastMessages.length());
+ }
+
}
--- a/src/com/beem/project/beem/service/aidl/IChat.aidl Mon Apr 20 14:00:00 2009 +0200
+++ b/src/com/beem/project/beem/service/aidl/IChat.aidl Mon Apr 20 21:46:52 2009 +0200
@@ -2,6 +2,7 @@
import com.beem.project.beem.service.Contact;
import com.beem.project.beem.service.Message;
+import com.beem.project.beem.service.aidl.IMessageListener;
/**
* An aidl interface for Chat session.
@@ -22,5 +23,16 @@
String getState();
- void setState(String state);
+ void setState(in String state);
+
+ String getLastMessages();
+
+ void addToLastMessages(in String msg);
+
+ void clearLastMessages();
+/*
+ void addMessageListener(in IMessageListener listener);
+
+ void removeMessageListener(in IMessageListener listener);
+*/
}
\ No newline at end of file
--- a/src/com/beem/project/beem/ui/SendIM.java Mon Apr 20 14:00:00 2009 +0200
+++ b/src/com/beem/project/beem/ui/SendIM.java Mon Apr 20 21:46:52 2009 +0200
@@ -57,7 +57,7 @@
private TextView mText;
private TextView mLogin;
private ScrollView mScrolling;
- private Boolean mSpeak;
+ private boolean mSpeak;
/**
* Constructor.
@@ -107,6 +107,7 @@
@Override
public void onStart() {
super.onStart();
+ //TODO cancel the notification if any
if (mContact == null)
mContact = getIntent().getParcelableExtra("contact");
setViewHeader();
@@ -116,6 +117,11 @@
mChatManager = mService.getChatManager();
mChatManager.addChatCreationListener(mChatManagerListener);
mChat = mChatManager.createChat(mContact, mMessageListener);
+ String text = mChat.getLastMessages();
+ if (! "".equals(text)) {
+ mText.append(text);
+ mChat.clearLastMessages();
+ }
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -147,7 +153,7 @@
* pendant la conversation
*/
private void sendText() {
- if (mSpeak == null)
+ if (mSpeak)
mSpeak = false;
String text = mToSend.getText().toString();
String from = mGlobalSettings.getString(
@@ -233,6 +239,8 @@
Log.i("LOG", "processMessage");
/*mAdapter.add(mContact.getJID() + " "
+ getString(R.string.SendIMSays) + msg.getBody());*/
+ if(chat!= mChat)
+ return;
final Message m = msg;
mHandler.post(new Runnable() {