fix some bugs. When you receive a new chat, a notification appears.
authorDa Risk <darisk972@gmail.com>
Mon, 20 Apr 2009 14:00:00 +0200
changeset 118 8bbe46055004
parent 109 ec30f9790f83
child 119 c93e4b69f031
fix some bugs. When you receive a new chat, a notification appears. Need some improvements cause if the chat was created before, then close no notification appears (nofif only on new created chat).
AndroidManifest.xml
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/service/BeemChatManager.java
src/com/beem/project/beem/service/ChatAdapter.java
src/com/beem/project/beem/service/Contact.java
src/com/beem/project/beem/service/XmppConnectionAdapter.java
src/com/beem/project/beem/service/aidl/IChatManager.aidl
src/com/beem/project/beem/ui/SendIM.java
--- a/AndroidManifest.xml	Wed Apr 15 20:09:24 2009 +0200
+++ b/AndroidManifest.xml	Mon Apr 20 14:00:00 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	Wed Apr 15 20:09:24 2009 +0200
+++ b/src/com/beem/project/beem/BeemService.java	Mon Apr 20 14:00:00 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	Wed Apr 15 20:09:24 2009 +0200
+++ b/src/com/beem/project/beem/service/BeemChatManager.java	Mon Apr 20 14:00:00 2009 +0200
@@ -14,14 +14,20 @@
 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 +42,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);
     }
@@ -61,7 +67,9 @@
 	if (mChats.containsKey(jid)) {
 	    return mChats.get(jid);
 	}
-	return new ChatAdapter( mAdaptee.createChat(jid, mChatListener));
+	// create the chat. the adaptee will be add automatically in the map
+	mAdaptee.createChat(jid, mChatListener);
+	return mChats.get(jid);
     }
 
     /**
@@ -90,7 +98,18 @@
     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)
+	    Log.w(TAG, "CA devrait pas 1!!");
+    }
+    
     private IChat getChat(Chat chat) {
 	if (mChats.containsKey(chat.getParticipant())) {
 	    return mChats.get(chat.getParticipant());
@@ -119,8 +138,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,6 +156,26 @@
 	    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);
@@ -145,7 +184,7 @@
 		IMessageListener listener = mRemoteMessageListeners.getBroadcastItem(i);
 		try {
 		    listener.processMessage(newchat, new com.beem.project.beem.service.Message(message));
-		    //listener.chatCreated(newchat, locally);
+		    // listener.chatCreated(newchat, locally);
 		} catch (RemoteException e) {
 		    // The RemoteCallbackList will take care of removing the
 		    // dead listeners.
@@ -165,4 +204,6 @@
 	}
     }
 
+
+
 }
--- a/src/com/beem/project/beem/service/ChatAdapter.java	Wed Apr 15 20:09:24 2009 +0200
+++ b/src/com/beem/project/beem/service/ChatAdapter.java	Mon Apr 20 14:00:00 2009 +0200
@@ -67,4 +67,8 @@
 	mState = state;
     }
 
+    public Chat getAdaptee() {
+	return mAdaptee;
+    }
+    
 }
--- a/src/com/beem/project/beem/service/Contact.java	Wed Apr 15 20:09:24 2009 +0200
+++ b/src/com/beem/project/beem/service/Contact.java	Mon Apr 20 14:00:00 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	Wed Apr 15 20:09:24 2009 +0200
+++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Mon Apr 20 14:00:00 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/aidl/IChatManager.aidl	Wed Apr 15 20:09:24 2009 +0200
+++ b/src/com/beem/project/beem/service/aidl/IChatManager.aidl	Mon Apr 20 14:00:00 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/SendIM.java	Wed Apr 15 20:09:24 2009 +0200
+++ b/src/com/beem/project/beem/ui/SendIM.java	Mon Apr 20 14:00:00 2009 +0200
@@ -32,8 +32,9 @@
 import com.beem.project.beem.service.aidl.IXmppFacade;
 
 /**
- * @author barbu This activity class provides the view for instant messaging
+ * This activity class provides the view for instant messaging
  *         after selecting a correspondant.
+ * @author barbu 
  */
 
 public class SendIM extends Activity implements OnClickListener,
@@ -88,16 +89,13 @@
 
 	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);
 	String status = mContact.getMsgState();
 	if (status == null)
 	    status = getString(R.string.SendIMNoStatusSet);
@@ -109,6 +107,9 @@
     @Override
     public void onStart() {
 	super.onStart();
+	if (mContact == null)
+	    mContact = getIntent().getParcelableExtra("contact");
+	setViewHeader();
 	mBeemApplication.startBeemService();
 	mService = mBeemApplication.getXmppFacade();
 	try {
@@ -121,6 +122,18 @@
 	}
     }
 
+    @Override
+    protected void onDestroy() {
+	super.onDestroy();
+	try {
+	    mChatManager.removeChatCreationListener(mChatManagerListener);
+	    mChatManager.destroyChat(mChat);
+	} catch (RemoteException e) {
+	    // TODO Auto-generated catch block
+	    e.printStackTrace();
+	}
+    }
+    
     /**
      * Abstract method inherited from OnClickListener
      */