Draft de reception d'une demande d'ajout.
authorDa Risk <darisk972@gmail.com>
Wed, 08 Apr 2009 20:04:35 +0200
changeset 94 904759c1bd62
parent 93 a4501bbd1549
child 95 da27ef678b0f
Draft de reception d'une demande d'ajout. L'envoie d'une demande d'ajout se fait en utilisant la methode pour ajouter un contact au roster
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/service/RosterAdapter.java
src/com/beem/project/beem/service/XmppConnectionAdapter.java
--- a/src/com/beem/project/beem/BeemService.java	Wed Apr 08 18:39:12 2009 +0200
+++ b/src/com/beem/project/beem/BeemService.java	Wed Apr 08 20:04:35 2009 +0200
@@ -1,6 +1,15 @@
 package com.beem.project.beem;
 
 import org.jivesoftware.smack.ConnectionConfiguration;
+import org.jivesoftware.smack.PacketListener;
+import org.jivesoftware.smack.Roster;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.Roster.SubscriptionMode;
+import org.jivesoftware.smack.filter.AndFilter;
+import org.jivesoftware.smack.filter.PacketFilter;
+import org.jivesoftware.smack.filter.PacketTypeFilter;
+import org.jivesoftware.smack.packet.Packet;
+import org.jivesoftware.smack.packet.Presence;
 import org.jivesoftware.smack.proxy.ProxyInfo;
 import org.jivesoftware.smack.proxy.ProxyInfo.ProxyType;
 import android.app.Notification;
@@ -10,8 +19,11 @@
 import android.content.SharedPreferences;
 import android.os.IBinder;
 import android.os.RemoteException;
+import android.util.Log;
+
 import com.beem.project.beem.service.XmppConnectionAdapter;
 import com.beem.project.beem.service.XmppFacade;
+import com.beem.project.beem.service.aidl.IBeemConnectionListener;
 import com.beem.project.beem.service.aidl.IXmppConnection;
 import com.beem.project.beem.service.aidl.IXmppFacade;
 
@@ -29,7 +41,7 @@
     public static final int NOTIFICATION_STATUS_ID = 1;
 
     private NotificationManager mNotificationManager;
-    private IXmppConnection mConnection;
+    private XmppConnectionAdapter mConnection;
     private SharedPreferences mSettings;
     private String mLogin;
     private String mPassword;
@@ -69,32 +81,11 @@
 	initConnectionConfig();
 	mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 	mConnection = new XmppConnectionAdapter(mConnectionConfiguration, mLogin, mPassword);
+	initRosterRequestListener();
 	mBind = new XmppFacade(mConnection, this);
     }
 
     /**
-     * Initialise la configuration de la connexion.
-     */
-    private void initConnectionConfig() {
-	//TODO mettre a false par defaut et remplacer les valeurs par defaut
-	mUseProxy = mSettings.getBoolean(getString(R.string.PreferenceUseProxy), false);
-	if (mUseProxy) {
-	    String stype = mSettings.getString(getString(R.string.PreferenceProxyType),
-		getString(R.string.PreferenceProxyTypeHttp));
-	    String phost = mSettings.getString(getString(R.string.PreferenceProxyHost), "");
-	    String puser = mSettings.getString(getString(R.string.PreferenceProxyUser), "");
-	    String ppass = mSettings.getString(getString(R.string.PreferenceProxyPassword), "");
-	    int pport =  mSettings.getInt(getString(R.string.PreferenceProxyPort), 3128);
-	    ProxyInfo.ProxyType type = ProxyType.valueOf(stype);
-	    mProxyInfo = new ProxyInfo(type, phost, pport, puser, ppass);
-	    mConnectionConfiguration = new ConnectionConfiguration(mHost, mProxyInfo);
-	} else {
-	    mConnectionConfiguration = new ConnectionConfiguration(mHost);
-	}
-	mConnectionConfiguration.setSendPresence(true);
-    }
-
-    /**
      * {@inheritDoc}
      */
     @Override
@@ -117,19 +108,6 @@
     }
 
     /**
-     * Close the connection to the xmpp server.
-     */
-    private void closeConnection() {
-	if (mConnection != null)
-	    try {
-		mConnection.disconnect();
-	    } catch (RemoteException e) {
-		// TODO Auto-generated catch block
-		e.printStackTrace();
-	    }
-    }
-
-    /**
      * Show a notification.
      * @param id 	the id of the notification.
      * @param notif	the notification to show
@@ -137,4 +115,124 @@
     public void sendNotification(int id, Notification notif) {
 	mNotificationManager.notify(id, notif);
     }
+
+    /**
+     * Initialise la configuration de la connexion.
+     */
+    private void initConnectionConfig() {
+	//TODO mettre a false par defaut et remplacer les valeurs par defaut
+	mUseProxy = mSettings.getBoolean(getString(R.string.PreferenceUseProxy), false);
+	if (mUseProxy) {
+	    String stype = mSettings.getString(getString(R.string.PreferenceProxyType),
+		getString(R.string.PreferenceProxyTypeHttp));
+	    String phost = mSettings.getString(getString(R.string.PreferenceProxyHost), "");
+	    String puser = mSettings.getString(getString(R.string.PreferenceProxyUser), "");
+	    String ppass = mSettings.getString(getString(R.string.PreferenceProxyPassword), "");
+	    int pport =  mSettings.getInt(getString(R.string.PreferenceProxyPort), 3128);
+	    ProxyInfo.ProxyType type = ProxyType.valueOf(stype);
+	    mProxyInfo = new ProxyInfo(type, phost, pport, puser, ppass);
+	    mConnectionConfiguration = new ConnectionConfiguration(mHost, mProxyInfo);
+	} else {
+	    mConnectionConfiguration = new ConnectionConfiguration(mHost);
+	}
+	mConnectionConfiguration.setSendPresence(true);
+    }
+    
+    private void initRosterRequestListener() {
+	Roster.setDefaultSubscriptionMode(SubscriptionMode.manual);
+	final XMPPConnection con =  mConnection.getAdaptee();
+	try {
+	    mConnection.addConnectionListener(new IBeemConnectionListener.Stub() {
+	    
+	        @Override
+	        public void reconnectionSuccessful() throws RemoteException {
+	    	// TODO Auto-generated method stub
+	            PacketFilter filter = new PacketFilter() {
+	        	
+	    	    @Override
+	    	    public boolean accept(Packet packet) {
+	    		if (packet instanceof Presence) {
+	    		    Presence pres = (Presence) packet;
+	    		    if (pres.getType() == Presence.Type.subscribe)
+	    			return true;
+	    		}
+	    		return false;
+	    	    }
+	    	};
+	    	con.addPacketListener(new PacketListener() {
+	    	
+	    	    @Override
+	    	    public void processPacket(Packet packet) {
+	    		// TODO Auto-generated method stub
+	    		Log.i("TEST", "Une demande recue");
+	    	    }
+	    	}, filter);
+	    	
+	        }
+	    
+	        @Override
+	        public void reconnectionFailed() throws RemoteException {
+	    	// TODO Auto-generated method stub
+	    
+	        }
+	    
+	        @Override
+	        public void reconnectingIn(int seconds) throws RemoteException {
+	    	// TODO Auto-generated method stub
+	    
+	        }
+	    
+	        @Override
+	        public void onConnect() throws RemoteException {
+	    	// TODO Auto-generated method stub
+	            PacketFilter filter = new PacketFilter() {
+	        	
+	    	    @Override
+	    	    public boolean accept(Packet packet) {
+	    		if (packet instanceof Presence) {
+	    		    Presence pres = (Presence) packet;
+	    		    if (pres.getType() == Presence.Type.subscribe)
+	    			return true;
+	    		}
+	    		return false;
+	    	    }
+	    	};
+	    	con.addPacketListener(new PacketListener() {
+	    	
+	    	    @Override
+	    	    public void processPacket(Packet packet) {
+	    		// TODO Auto-generated method stub
+	    		Log.i("TEST", "Une demande recue");
+	    	    }
+	    	}, filter);
+	    	
+	        }
+	    
+	        @Override
+	        public void connectionClosedOnError() throws RemoteException {
+	    	// TODO Auto-generated method stub
+	    
+	        }
+	    
+	        @Override
+	        public void connectionClosed() throws RemoteException {
+	    	// TODO Auto-generated method stub
+	    
+	        }
+	    });
+	} catch (RemoteException e) {
+	    // TODO Auto-generated catch block
+	    e.printStackTrace();
+	}
+	
+    }
+
+    /**
+     * Close the connection to the xmpp server.
+     */
+    private void closeConnection() {
+	if (mConnection != null)
+	    mConnection.disconnect();
+    }
+
 }
--- a/src/com/beem/project/beem/service/RosterAdapter.java	Wed Apr 08 18:39:12 2009 +0200
+++ b/src/com/beem/project/beem/service/RosterAdapter.java	Wed Apr 08 20:04:35 2009 +0200
@@ -70,6 +70,7 @@
 	    mContacts.put(user, res);
 	    return res;
 	} catch (XMPPException e) {
+	    Log.e(TAG, "Error while adding new contact", e);
 	    return null;
 	}
     }
--- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Wed Apr 08 18:39:12 2009 +0200
+++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Wed Apr 08 20:04:35 2009 +0200
@@ -321,4 +321,8 @@
 	return mAdaptee.isAuthenticated();
     }
 
+    public XMPPConnection getAdaptee() {
+	return mAdaptee;
+    }
+    
 }