--- a/AndroidManifest.xml Tue Apr 21 16:38:25 2009 +0200
+++ b/AndroidManifest.xml Tue Apr 21 16:38:50 2009 +0200
@@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.beem.project.beem" android:versionCode="1"
android:versionName="1.0">
- <application android:label="@string/app_name" android:name="BeemApplication" android:theme="@style/customtheme.contactList">
+ <application android:label="@string/app_name" android:name="BeemApplication" android:theme="@style/customtheme.contactList" android:icon="@drawable/logo">
<activity android:name=".ui.ContactList" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
--- a/src/com/beem/project/beem/BeemService.java Tue Apr 21 16:38:25 2009 +0200
+++ b/src/com/beem/project/beem/BeemService.java Tue Apr 21 16:38:50 2009 +0200
@@ -38,7 +38,7 @@
/**
* The id to use for status notification.
*/
- public static final int NOTIFICATION_STATUS_ID = 1;
+ public static final int NOTIFICATION_STATUS_ID = 100;
private NotificationManager mNotificationManager;
private XmppConnectionAdapter mConnection;
@@ -80,7 +80,7 @@
mHost = "10.0.2.2";
initConnectionConfig();
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- mConnection = new XmppConnectionAdapter(mConnectionConfiguration, mLogin, mPassword);
+ mConnection = new XmppConnectionAdapter(mConnectionConfiguration, mLogin, mPassword, this);
initRosterRequestListener();
mBind = new XmppFacade(mConnection, this);
}
@@ -115,7 +115,7 @@
public void sendNotification(int id, Notification notif) {
mNotificationManager.notify(id, notif);
}
-
+
/**
* Initialise la configuration de la connexion.
*/
@@ -231,7 +231,6 @@
// TODO Auto-generated catch block
e.printStackTrace();
}
-
}
/**
@@ -242,4 +241,5 @@
mConnection.disconnect();
}
+
}
--- a/src/com/beem/project/beem/service/BeemChatManager.java Tue Apr 21 16:38:25 2009 +0200
+++ b/src/com/beem/project/beem/service/BeemChatManager.java Tue Apr 21 16:38:50 2009 +0200
@@ -11,17 +11,24 @@
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;
+import android.R;
+import android.app.Notification;
+import android.app.PendingIntent;
+import android.content.Intent;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.util.Log;
+import com.beem.project.beem.BeemService;
import com.beem.project.beem.service.aidl.IChat;
import com.beem.project.beem.service.aidl.IChatManager;
import com.beem.project.beem.service.aidl.IChatManagerListener;
import com.beem.project.beem.service.aidl.IMessageListener;
+import com.beem.project.beem.ui.SendIM;
/**
* An adapter for smack's ChatManager. This class provides functionnality to handle chats.
@@ -36,16 +43,16 @@
private ChatManager mAdaptee;
private Map<String, IChat> mChats = new HashMap<String, IChat>();
private ChatListener mChatListener = new ChatListener();
- private RemoteCallbackList<IChatManagerListener> mRemoteChatCreationListeners =
- new RemoteCallbackList<IChatManagerListener>();
+ private RemoteCallbackList<IChatManagerListener> mRemoteChatCreationListeners = new RemoteCallbackList<IChatManagerListener>();
private RemoteCallbackList<IMessageListener> mRemoteMessageListeners = new RemoteCallbackList<IMessageListener>();
+ private BeemService mService;
/**
* Constructor.
* @param chatManager the smack ChatManager to adapt
*/
- public BeemChatManager(final ChatManager chatManager) {
- // TODO Auto-generated constructor stub
+ public BeemChatManager(final ChatManager chatManager, BeemService service) {
+ mService = service;
mAdaptee = chatManager;
mAdaptee.addChatListener(mChatListener);
}
@@ -58,10 +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);
}
- return new ChatAdapter( mAdaptee.createChat(jid, mChatListener));
+ // create the chat. the adaptee will be add automatically in the map
+ mAdaptee.createChat(key, mChatListener);
+ return mChats.get(key);
}
/**
@@ -91,12 +101,24 @@
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)
+ 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;
}
@@ -119,8 +141,8 @@
public void chatCreated(Chat chat, boolean locally) {
IChat newchat = getChat(chat);
if (!locally) {
- mChats.put(chat.getParticipant(), newchat);
- // TODO startActivity
+ chat.addMessageListener(mChatListener);
+ notifyNewChat(newchat);
}
final int n = mRemoteChatCreationListeners.beginBroadcast();
@@ -137,22 +159,43 @@
mRemoteChatCreationListeners.finishBroadcast();
}
+ private void notifyNewChat(IChat chat) {
+ try {
+ String text = chat.getParticipant().getJID();
+ Notification notif = new Notification(com.beem.project.beem.R.drawable.logo, text, System
+ .currentTimeMillis());
+ notif.defaults = Notification.DEFAULT_ALL;
+ notif.flags = Notification.FLAG_AUTO_CANCEL;
+ Intent intent = new Intent(mService, SendIM.class);
+ // TODO use prefix for name
+ intent.putExtra("contact", chat.getParticipant());
+ notif.setLatestEventInfo(mService, text, "nouveau message", PendingIntent.getActivity(mService, 0,
+ intent, PendingIntent.FLAG_ONE_SHOT));
+ int id = chat.hashCode();
+ mService.sendNotification(id, notif);
+ } catch (RemoteException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
@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
--- a/src/com/beem/project/beem/service/ChatAdapter.java Tue Apr 21 16:38:25 2009 +0200
+++ b/src/com/beem/project/beem/service/ChatAdapter.java Tue Apr 21 16:38:50 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());
@@ -67,4 +74,23 @@
mState = state;
}
+ 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/Contact.java Tue Apr 21 16:38:25 2009 +0200
+++ b/src/com/beem/project/beem/service/Contact.java Tue Apr 21 16:38:50 2009 +0200
@@ -200,4 +200,11 @@
public List<String> getMRes() {
return mRes;
}
+
+ @Override
+ public String toString() {
+ if (mJID != null)
+ return mJID;
+ return super.toString();
+ }
}
--- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java Tue Apr 21 16:38:25 2009 +0200
+++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java Tue Apr 21 16:38:50 2009 +0200
@@ -16,6 +16,7 @@
import android.os.RemoteException;