Suppression de la beemapplication dans contactlist.
authormarseille@marseille-desktop
Wed, 17 Jun 2009 20:14:11 +0200
changeset 237 66ea747448ea
parent 236 23647e075321
child 238 ef8d8745894a
Suppression de la beemapplication dans contactlist.
src/com/beem/project/beem/service/RosterAdapter.java
src/com/beem/project/beem/service/aidl/IRoster.aidl
src/com/beem/project/beem/ui/ContactList.java
--- a/src/com/beem/project/beem/service/RosterAdapter.java	Wed Jun 17 19:03:11 2009 +0200
+++ b/src/com/beem/project/beem/service/RosterAdapter.java	Wed Jun 17 20:14:11 2009 +0200
@@ -181,7 +181,7 @@
      * {@inheritDoc}
      */
     @Override
-    public void addConnectionListener(IBeemRosterListener listen) throws RemoteException {
+    public void addRosterListener(IBeemRosterListener listen) throws RemoteException {
 	if (listen != null)
 	    mRemoteRosListeners.register(listen);
     }
@@ -257,7 +257,7 @@
      * {@inheritDoc}
      */
     @Override
-    public void removeConnectionListener(IBeemRosterListener listen) throws RemoteException {
+    public void removeRosterListener(IBeemRosterListener listen) throws RemoteException {
 	if (listen != null)
 	    mRemoteRosListeners.unregister(listen);
     }
--- a/src/com/beem/project/beem/service/aidl/IRoster.aidl	Wed Jun 17 19:03:11 2009 +0200
+++ b/src/com/beem/project/beem/service/aidl/IRoster.aidl	Wed Jun 17 20:14:11 2009 +0200
@@ -18,7 +18,7 @@
     
     List<String> getGroupsNames();
     
-    void addConnectionListener(in IBeemRosterListener listen);
-    void removeConnectionListener(in IBeemRosterListener listen);
+    void addRosterListener(in IBeemRosterListener listen);
+    void removeRosterListener(in IBeemRosterListener listen);
 
 }
\ No newline at end of file
--- a/src/com/beem/project/beem/ui/ContactList.java	Wed Jun 17 19:03:11 2009 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java	Wed Jun 17 20:14:11 2009 +0200
@@ -8,11 +8,14 @@
 import org.jivesoftware.smack.util.StringUtils;
 
 import android.app.ExpandableListActivity;
+import android.content.ComponentName;
 import android.content.Intent;
+import android.content.ServiceConnection;
 import android.database.DataSetObserver;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.IBinder;
 import android.os.RemoteException;
 import android.util.Log;
 import android.view.LayoutInflater;
@@ -27,7 +30,7 @@
 import android.widget.ImageView;
 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.service.Contact;
 import com.beem.project.beem.service.PresenceAdapter;
@@ -40,15 +43,14 @@
 
     private static final String TAG = "CONTACTLIST_ACT";
     private static final String DEFAULT_GROUP = "Default";
-    private IXmppFacade mService = null;
     private MyExpandableListAdapter mAdapter;
-    private BeemApplication mBeemApplication;
-    private BeemRosterListener mRosterListener;
     private IRoster mRoster;
     private Map<String, List<Contact>> groupMap;
     private List<String> groupName;
     private List<Contact> mListContact;
     private Handler mHandler;
+    private IXmppFacade xmppFacade = null;
+    private final ServiceConnection mServConn = new BeemServiceConnection();
 
     /**
      * Callback for menu creation.
@@ -85,41 +87,10 @@
     @Override
     protected void onCreate(Bundle saveBundle) {
 	super.onCreate(saveBundle);
+	bindService(new Intent(this, BeemService.class), mServConn, BIND_AUTO_CREATE);
 	mHandler = new Handler();
-	mRosterListener = new BeemRosterListener();
-	mBeemApplication = BeemApplication.getApplication(this);
 	groupMap = new HashMap<String, List<Contact>>();
-	groupName = new ArrayList<String>();
-	mBeemApplication.callWhenConnectedToServer(mHandler, new Runnable() {
-
-	    @Override
-	    public void run() {
-		mService = mBeemApplication.getXmppFacade();
-		try {
-		    mRoster = mService.getRoster();
-		} catch (RemoteException e1) {
-		    Log.e(TAG, "Get roster failed", e1);
-		}
-		if (mRoster != null) {
-		    try {
-			mRoster.addConnectionListener(mRosterListener);
-		    } catch (RemoteException e) {
-			e.printStackTrace();
-		    }
-		}
-		callbackShowContactList();
-	    }
-	});
-    }
-
-    private void callbackShowContactList() {
-	if (mRoster != null) {
-	    try {
-		buildContactList(mRoster.getContactList());
-	    } catch (RemoteException e) {
-		e.printStackTrace();
-	    }
-	}
+	groupName = new ArrayList<String>();	
     }
 
     private void buildContactList(List<Contact> listContact) {
@@ -462,4 +433,25 @@
 	    observers.remove(observer);
 	}
     }
+
+    private class BeemServiceConnection implements ServiceConnection {
+
+	@Override
+	public void onServiceConnected(ComponentName name, IBinder service) {
+	    xmppFacade = IXmppFacade.Stub.asInterface(service);
+	    try {
+		mRoster = xmppFacade.getRoster();
+		mRoster.addRosterListener(new BeemRosterListener());
+		buildContactList(mRoster.getContactList());
+	    } catch (RemoteException e) {
+		e.printStackTrace();
+	    }
+	}
+
+	@Override
+	public void onServiceDisconnected(ComponentName name) {
+	    xmppFacade = null;
+	    mRoster = null;
+	}
+    }
 }