merge
authorDa Risk <darisk972@gmail.com>
Tue, 21 Apr 2009 19:41:58 +0200
changeset 128 52d32ded7b9d
parent 127 395f1beb409f (diff)
parent 113 01b2fc87427b (current diff)
child 129 a223ee5e7faa
merge
src/com/beem/project/beem/BeemApplication.java
src/com/beem/project/beem/ui/ChangeStatus.java
--- a/AndroidManifest.xml	Tue Apr 21 19:06:38 2009 +0200
+++ b/AndroidManifest.xml	Tue Apr 21 19:41:58 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/BeemApplication.java	Tue Apr 21 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/BeemApplication.java	Tue Apr 21 19:41:58 2009 +0200
@@ -126,6 +126,13 @@
 	}
     }
 
+    public synchronized void unbindBeemService() {
+	if (mIsConnected) {
+	    mApplicationContext.unbindService(mServConn);
+	    mIsConnected = false;
+	}
+     }
+
     /**
      * Get the facade to use to access the Beem service.
      * @return the facade or null if the application is not connected to the beem service.
@@ -160,15 +167,15 @@
     }
 
     private class ConnectionRunnable implements Runnable {
-	private String mErrorMsg;	
-	
+	private String mErrorMsg;
+
 	public ConnectionRunnable(String string) {
 	    this.mErrorMsg = string;
 	}
 
 	@Override
 	public void run() {
-	    mBeemApp.mProgressDialog.setMessage(mErrorMsg);	    
+	    mBeemApp.mProgressDialog.setMessage(mErrorMsg);
 	}
 
 	/**
@@ -206,7 +213,7 @@
 	@Override
 	public void connectionClosed() throws RemoteException {
 	    // TODO Auto-generated method stub
-	    Log.e("BeemApp","test1");
+	    Log.e("BeemApp", "test1");
 
 	}
 
@@ -216,7 +223,7 @@
 	@Override
 	public void connectionClosedOnError() throws RemoteException {
 	    mBeemApp.mProgressDialog.setMessage("Connection closed on error");
-	    Log.e(TAG,"ConnectionClosedOnError");
+	    Log.e(TAG, "ConnectionClosedOnError");
 	    // TODO afficher une notification et reafficher le progress dialog
 	}
 
@@ -252,7 +259,7 @@
 	@Override
 	public void reconnectionFailed() throws RemoteException {
 	    // TODO Auto-generated method stub
-	    Log.e("BeemApp","test3");
+	    Log.e("BeemApp", "test3");
 
 	}
 
--- a/src/com/beem/project/beem/BeemService.java	Tue Apr 21 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/BeemService.java	Tue Apr 21 19:41:58 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 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/service/BeemChatManager.java	Tue Apr 21 19:41:58 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 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/service/ChatAdapter.java	Tue Apr 21 19:41:58 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 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/service/Contact.java	Tue Apr 21 19:41:58 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/PresenceAdapter.java	Tue Apr 21 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/service/PresenceAdapter.java	Tue Apr 21 19:41:58 2009 +0200
@@ -46,9 +46,4 @@
 		// TODO Auto-generated method stub
 		
 	}
-
-	public IBinder asBinder() {
-		// TODO Auto-generated method stub
-		return null;
-	}
 }
--- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Tue Apr 21 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Tue Apr 21 19:41:58 2009 +0200
@@ -16,6 +16,7 @@
 import android.os.RemoteException;
 import android.util.Log;
 import com.beem.project.beem.BeemException;
+import com.beem.project.beem.BeemService;
 import com.beem.project.beem.service.aidl.IBeemConnectionListener;
 import com.beem.project.beem.service.aidl.IChatManager;
 import com.beem.project.beem.service.aidl.IRoster;
@@ -34,6 +35,7 @@
     private String mPassword;
     private RosterAdapter mRoster;
     private Object mLastException;
+    private BeemService mService;
 
     private RemoteCallbackList<IBeemConnectionListener> mRemoteConnListeners = new RemoteCallbackList<IBeemConnectionListener>();
     private ConnexionListenerAdapter mConListener = new ConnexionListenerAdapter();
@@ -44,10 +46,11 @@
      * @param login The login to use
      * @param password The password to use
      */
-    public XmppConnectionAdapter(final XMPPConnection con, final String login, final String password) {
+    public XmppConnectionAdapter(final XMPPConnection con, final String login, final String password, BeemService service) {
 	mAdaptee = con;
 	mLogin = login;
 	mPassword = password;
+	mService = service;
     }
 
     /**
@@ -56,8 +59,8 @@
      * @param login login to use on connect
      * @param password password to use on connect
      */
-    public XmppConnectionAdapter(final String serviceName, final String login, final String password) {
-	this(new XMPPConnection(serviceName), login, password);
+    public XmppConnectionAdapter(final String serviceName, final String login, final String password, BeemService service) {
+	this(new XMPPConnection(serviceName), login, password, service);
     }
 
     /**
@@ -66,8 +69,8 @@
      * @param login login to use on connect
      * @param password password to use on connect
      */
-    public XmppConnectionAdapter(final ConnectionConfiguration config, final String login, final String password) {
-	this(new XMPPConnection(config), login, password);
+    public XmppConnectionAdapter(final ConnectionConfiguration config, final String login, final String password, BeemService service) {
+	this(new XMPPConnection(config), login, password, service);
     }
 
     /**
@@ -79,7 +82,7 @@
 	    mAdaptee.connect();
 	    mAdaptee.addConnectionListener(mConListener);
 	    mAdaptee.login(mLogin, mPassword, "BEEM");
-	    mChatManager = new BeemChatManager(mAdaptee.getChatManager());
+	    mChatManager = new BeemChatManager(mAdaptee.getChatManager(), mService);
 	    // TODO find why this cause a null pointer exception
 	    // this.initFeatures(); // pour declarer les features xmpp qu'on supporte
 	    mLastException = null;
@@ -343,4 +346,8 @@
 	return mAdaptee;
     }
     
+    public BeemService getContext() {
+	return mService;
+    }
+    
 }
--- a/src/com/beem/project/beem/service/XmppFacade.java	Tue Apr 21 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/service/XmppFacade.java	Tue Apr 21 19:41:58 2009 +0200
@@ -13,6 +13,7 @@
 import com.beem.project.beem.service.aidl.IRoster;
 import com.beem.project.beem.service.aidl.IXmppConnection;
 import com.beem.project.beem.service.aidl.IXmppFacade;
+import com.beem.project.beem.ui.ChangeStatus;
 
 /**
  * This class is a facade for the Beem Service.
@@ -103,7 +104,7 @@
 	// TODO
 	// mStatusNotification.contentView = ;
 	mStatusNotification.setLatestEventInfo(mBeemService, "Beem Status", text, PendingIntent.getActivity(
-	    mBeemService, 0, new Intent(), 0));
+	    mBeemService, 0, new Intent(mBeemService,ChangeStatus.class), 0));
 	mBeemService.sendNotification(BeemService.NOTIFICATION_STATUS_ID, mStatusNotification);
     }
 }
--- a/src/com/beem/project/beem/service/aidl/IChat.aidl	Tue Apr 21 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/service/aidl/IChat.aidl	Tue Apr 21 19:41:58 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/service/aidl/IChatManager.aidl	Tue Apr 21 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/service/aidl/IChatManager.aidl	Tue Apr 21 19:41:58 2009 +0200
@@ -22,6 +22,12 @@
 	IChat createChat(in Contact contact, in IMessageListener listener);
 	
 	/**
+    	 * Destroy a chat session with a contact.
+    	 * @param chat	the chat session
+    	 */
+	void destroyChat(in IChat chat);
+	
+	/**
 	 * Register a callback to call when a new chat session is created.
 	 * @param listener	the callback to add
 	 */
--- a/src/com/beem/project/beem/ui/ChangeStatus.java	Tue Apr 21 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/ui/ChangeStatus.java	Tue Apr 21 19:41:58 2009 +0200
@@ -1,7 +1,10 @@
 package com.beem.project.beem.ui;
 
 import android.app.Activity;
+import android.content.Intent;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.RemoteException;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
@@ -10,49 +13,131 @@
 import android.widget.Spinner;
 import android.widget.TextView;
 
+import com.beem.project.beem.BeemApplication;
+import com.beem.project.beem.BeemService;
 import com.beem.project.beem.R;
+import com.beem.project.beem.R.string;
+import com.beem.project.beem.service.aidl.IXmppFacade;
+import com.beem.project.beem.utils.Status;
 
 public class ChangeStatus extends Activity {
-    
+
     private TextView mTextStatus;
     private Button mOk;
     private Button mClear;
-    private Spinner mSpin;
-    private ArrayAdapter<String> mAdapter;
-    private static final String[] mStatus = {"Available for chat", "Available", "Busy", "Away",
-	"Unavailable", "Disconnected"};
-    
+    private Handler mHandler;
+    private BeemApplication mBeemApplication;
+    private IXmppFacade mService = null;
+    private Spinner mSpinner;
+    private static final String[] STATUS = { "Available for chat", "Available", "Busy", "Away", "Unavailable",
+	"Disconnected" };
+    private static final int DISCONNECTED_IDX = 5;
+    private static final int AVAILABLE_FOR_CHAT_IDX = 0;
+    private static final int AVAILABLE_IDX = 1;
+    private static final int BUSY_IDX = 2;
+    private static final int AWAY_IDX = 3;
+    private static final int UNAVAILABLE_IDX = 4;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
-        // TODO Auto-generated method stub
-        super.onCreate(savedInstanceState);
-        setContentView(R.layout.changestatus);
-        
-        mTextStatus = (TextView) findViewById(R.id.ChangeStatusText);
-        mOk = (Button) findViewById(R.id.ChangeStatusOk);
-        mClear = (Button) findViewById(R.id.ChangeStatusClear);
-        mOk.setOnClickListener(mOnClickOk);
-        mClear.setOnClickListener(mOnClickClear);
-        
-        
-        mSpin = (Spinner) findViewById(R.id.ChangeStatusSpinner);
-        mAdapter = new ArrayAdapter<String>(this,
-                android.R.layout.simple_spinner_item, mStatus);
-        mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
-        mSpin.setAdapter(mAdapter);
+	super.onCreate(savedInstanceState);
+	setContentView(R.layout.changestatus);
+
+	// Beem Application specific
+	mHandler = new Handler();
+	mBeemApplication = BeemApplication.getApplication(this);
+
+	mTextStatus = (TextView) findViewById(R.id.ChangeStatusText);
+	mOk = (Button) findViewById(R.id.ChangeStatusOk);
+	mClear = (Button) findViewById(R.id.ChangeStatusClear);
+	mOk.setOnClickListener(mOnClickOk);
+	mClear.setOnClickListener(mOnClickClear);
+
+	mSpinner = (Spinner) findViewById(R.id.ChangeStatusSpinner);
+	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, STATUS);
+	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+	mSpinner.setAdapter(adapter);
+    }
+
+    @Override
+    protected void onStart() {
+	super.onStart();
+	mBeemApplication.startBeemService();
+    }
+
+    @Override
+    protected void onResume() {
+	super.onResume();
+	mBeemApplication.callWhenConnectedToServer(mHandler, new Runnable() {
+	    @Override
+	    public void run() {
+		mService = mBeemApplication.getXmppFacade();
+	    }
+	});
     }
     
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        mBeemApplication.unbindBeemService();
+    }
+
     private OnClickListener mOnClickOk = new OnClickListener() {
 	public void onClick(View v) {
-	    CharSequence text = mTextStatus.getText();
-	    String status = mAdapter.getItem(0);
-	    Log.i("Selected item --->", status);
-	}
+	    if (v == mOk) {
+		String selected = (String) mSpinner.getSelectedItem();
+		if (selected != null) {
+		    CharSequence msg = mTextStatus.getText();
+		    int status = getStatusForService(selected);
+		    if (status == Status.CONTACT_STATUS_DISCONNECT) {
+			stopService(new Intent(ChangeStatus.this, BeemService.class));
+		    } else
+			try {
+			    mService.changeStatus(status, msg.toString());
+
+			} catch (RemoteException e) {
+			    // TODO Auto-generated catch block
+			    e.printStackTrace();
+			}
+		    ChangeStatus.this.finish();
+		}
+	    } else if (v == mClear) {
+		mTextStatus.setText(null);
+	    }
+
+	};
     };
-    
+
     private OnClickListener mOnClickClear = new OnClickListener() {
 	public void onClick(View v) {
 	    mTextStatus.setText(null);
 	};
     };
+
+    private int getStatusForService(String item) {
+	int res = Status.CONTACT_STATUS_AVAILABLE;
+	for (int i = 0; i < ChangeStatus.STATUS.length; i++) {
+	    String str = ChangeStatus.STATUS[i];
+	    if (str.equals(item)) {
+		switch (i) {
+		    case ChangeStatus.DISCONNECTED_IDX:
+			return Status.CONTACT_STATUS_DISCONNECT;
+		    case ChangeStatus.AVAILABLE_FOR_CHAT_IDX:
+			return Status.CONTACT_STATUS_AVAILABLE_FOR_CHAT;
+		    case ChangeStatus.AVAILABLE_IDX:
+			return Status.CONTACT_STATUS_AVAILABLE;
+		    case ChangeStatus.AWAY_IDX:
+			return Status.CONTACT_STATUS_AWAY;
+		    case ChangeStatus.BUSY_IDX:
+			return Status.CONTACT_STATUS_BUSY;
+		    case ChangeStatus.UNAVAILABLE_IDX:
+			return Status.CONTACT_STATUS_UNAVAILABLE;
+		    default:
+			res = Status.CONTACT_STATUS_AVAILABLE;
+			break;
+		}
+	    }
+	}
+	return res;
+    }
 }
--- a/src/com/beem/project/beem/ui/ContactList.java	Tue Apr 21 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java	Tue Apr 21 19:41:58 2009 +0200
@@ -74,6 +74,13 @@
 	super.onStart();
 	mBeemApplication.startBeemService();
     }
+    
+    @Override
+    protected void onDestroy() {
+        // TODO Auto-generated method stub
+        super.onDestroy();
+        mBeemApplication.unbindBeemService();
+    }
 
     @Override
     protected void onResume() {
--- a/src/com/beem/project/beem/ui/SendIM.java	Tue Apr 21 19:06:38 2009 +0200
+++ b/src/com/beem/project/beem/ui/SendIM.java	Tue Apr 21 19:41:58 2009 +0200
@@ -29,15 +29,14 @@
 import com.beem.project.beem.service.aidl.IXmppFacade;
 
 /**
- * @author barbu This activity class provides the view for instant messaging
- *         after selecting a correspondant.
+ * This activity class provides the view for instant messaging after selecting a correspondant.
+ * @author barbu
  */
 
-public class SendIM extends Activity implements OnClickListener,
-	OnKeyListener {
+public class SendIM extends Activity implements OnClickListener, OnKeyListener {
     private EditText mToSend;
-    //private ArrayList<String> mMessages = new ArrayList<String>();
-    //private ArrayAdapter<String> mAdapter;
+    // private ArrayList<String> mMessages = new ArrayList<String>();
+    // private ArrayAdapter<String> mAdapter;
     private SendIMDialogSmiley mSmyDialog;
     private SharedPreferences mSet;
     private SharedPreferences mGlobalSettings;
@@ -53,7 +52,7 @@
     private TextView mText;
     private TextView mLogin;
     private ScrollView mScrolling;
-    private Boolean mSpeak;
+    private boolean mSpeak;
 
     /**
      * Constructor.
@@ -77,24 +76,19 @@
 	mToSend = (EditText) findViewById(R.id.userText);
 	mSet = getSharedPreferences("lol", MODE_PRIVATE);
 	mSmyDialog = new SendIMDialogSmiley(this, mSet);
-	mGlobalSettings = getSharedPreferences(
-		getString(R.string.PreferenceFileName), MODE_PRIVATE);
-	/*mAdapter = new ArrayAdapter<String>(this, R.layout.messagelist,
-		mMessages);
-	setListAdapter(mAdapter);*/
+	mGlobalSettings = getSharedPreferences(getString(R.string.PreferenceFileName), MODE_PRIVATE);
+	/*
+	 * mAdapter = new ArrayAdapter<String>(this, R.layout.messagelist, mMessages); setListAdapter(mAdapter);
+	 */
 
 	mToSend.setOnClickListener(this);
 	mToSend.setOnKeyListener(this);
-	
-	mContact = getIntent().getParcelableExtra("contact");
-	setViewHeader();
+	mLogin = (TextView) findViewById(R.id.sendimlogin);
 	mText = (TextView) findViewById(R.id.sendimlist);
 	mScrolling = (ScrollView) findViewById(R.id.sendimscroll);
     }
-    
-    private void setViewHeader()
-    {
-	mLogin = (TextView) findViewById(R.id.sendimlogin);
+
+    private void setViewHeader() {
 	String status = mContact.getMsgState();
 	if (status == null)
 	    status = getString(R.string.SendIMNoStatusSet);
@@ -106,18 +100,40 @@
     @Override
     public void onStart() {
 	super.onStart();
+	// TODO cancel the notification if any
+	if (mContact == null)
+	    mContact = getIntent().getParcelableExtra("contact");
+	setViewHeader();
 	mBeemApplication.startBeemService();
 	mService = mBeemApplication.getXmppFacade();
 	try {
 	    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();
 	}
     }
 
+    @Override
+    protected void onDestroy() {
+	super.onDestroy();
+	try {
+	    mChatManager.removeChatCreationListener(mChatManagerListener);
+	    mChatManager.destroyChat(mChat);
+	} catch (RemoteException e) {
+	    // TODO Auto-generated catch block
+	    e.printStackTrace();
+	}
+	mBeemApplication.unbindBeemService();
+    }
+
     /**
      * Abstract method inherited from OnClickListener
      */
@@ -126,12 +142,11 @@
     }
 
     /**
-     * This method send a message to the server over the XMPP connection and
-     * display it on activity view TODO : Exception si la connexion se coupe
-     * pendant la conversation
+     * This method send a message to the server over the XMPP connection and display it on activity view TODO :
+     * Exception si la connexion se coupe pendant la conversation
      */
     private void sendText() {
-	if (mSpeak == null)
+	if (mSpeak)
 	    mSpeak = false;
 	String text = mToSend.getText().toString();
 	if (!text.equals("")) {
@@ -171,9 +186,7 @@
 
     /**
      * Callback for menu creation.
-     * 
-     * @param menu
-     *            the menu created
+     * @param menu the menu created
      * @return true on success, false otherwise
      */
     @Override
@@ -198,8 +211,7 @@
     private class OnChatListener extends IChatManagerListener.Stub {
 
 	@Override
-	public void chatCreated(IChat chat, boolean locally)
-		throws RemoteException {
+	public void chatCreated(IChat chat, boolean locally) throws RemoteException {
 	    Log.i("LOG", "chatCreated");
 
 	}
@@ -209,29 +221,29 @@
     private class OnMessageListener extends IMessageListener.Stub {
 
 	@Override
-	public void processMessage(IChat chat, Message msg)
-		throws RemoteException {
+	public void processMessage(IChat chat, Message msg) throws RemoteException {
 	    Log.i("LOG", "processMessage");
-	    /*mAdapter.add(mContact.getJID() + " "
-		    + getString(R.string.SendIMSays) + msg.getBody());*/
-	    
+	    /*
+	     * mAdapter.add(mContact.getJID() + " " + getString(R.string.SendIMSays) + msg.getBody());
+	     */
+	    if (chat != mChat)
+		return;
+
 	    final Message m = msg;
 	    mHandler.post(new Runnable() {
-	    
-	        @Override
-	        public void run() {
-	            if (m.getBody() != null)
-	            {
-	        	if (!mSpeak)
-	        	    mText.append(m.getBody() + "\n");
-	        	else
-	        	    mText.append(mContact.getJID() + " "
-				    + getString(R.string.SendIMSays) + m.getBody() + "\n");
-	        	mSpeak = false;
-	        	mScrolling.fullScroll(ScrollView.FOCUS_DOWN);
-	        	mToSend.requestFocus();
-	            }
-	        }
+
+		@Override
+		public void run() {
+		    if (m.getBody() != null) {
+			if (!mSpeak)
+			    mText.append(m.getBody() + "\n");
+			else
+			    mText.append(mContact.getJID() + " " + getString(R.string.SendIMSays) + m.getBody() + "\n");
+			mSpeak = false;
+			mScrolling.fullScroll(ScrollView.FOCUS_DOWN);
+			mToSend.requestFocus();
+		    }
+		}
 	    });
 	}
     }