Suppression du Beemapplication dans ContactDialog
authormarseille@marseille-desktop
Thu, 18 Jun 2009 20:22:56 +0200
changeset 241 a81519fc4024
parent 240 f879726d6295
child 242 11f8de688fcb
Suppression du Beemapplication dans ContactDialog
src/com/beem/project/beem/ui/ContactDialog.java
src/com/beem/project/beem/ui/ContactList.java
--- a/src/com/beem/project/beem/ui/ContactDialog.java	Thu Jun 18 11:53:00 2009 +0200
+++ b/src/com/beem/project/beem/ui/ContactDialog.java	Thu Jun 18 20:22:56 2009 +0200
@@ -4,14 +4,18 @@
 
 import android.app.Activity;
 import android.app.Dialog;
+import android.app.Service;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.ServiceConnection;
+import android.os.IBinder;
 import android.os.RemoteException;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;
 
-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;
@@ -19,6 +23,34 @@
 
 public class ContactDialog extends Dialog {
 
+    public static final String TAG = "Option Dialog";
+    private final Contact mContact;
+    private final Context mContext;
+    private IXmppFacade xmppFacade = null;
+    private final ServiceConnection mServConn = new BeemServiceConnection();
+
+    public ContactDialog(final Context context, Contact curContact) {
+	super(context);
+	mContext = context;
+
+	setContentView(R.layout.contactdialog);
+	mContact = curContact;
+	setTitle(curContact.getJID());
+
+	Button button = (Button) findViewById(R.id.CDChat);
+	button.setOnClickListener(new chatListener());
+	button = (Button) findViewById(R.id.CDAlias);
+	button.setOnClickListener(new aliasListener());
+	button = (Button) findViewById(R.id.CDGroup);
+	button.setOnClickListener(new groupListener());
+	button = (Button) findViewById(R.id.CDResend);
+	button.setOnClickListener(new resendListener());
+	button = (Button) findViewById(R.id.CDInfos);
+	button.setOnClickListener(new infosListener());
+	
+	mContext.bindService(new Intent(mContext, BeemService.class), mServConn, Service.BIND_AUTO_CREATE);
+    }
+
     class aliasListener implements View.OnClickListener {
 
 	@Override
@@ -72,7 +104,7 @@
 	    Presence presencePacket = new Presence(Presence.Type.subscribe);
 	    presencePacket.setTo(mContact.getJID());
 	    try {
-		mService.sendPresencePacket(new PresenceAdapter(presencePacket));
+		xmppFacade.sendPresencePacket(new PresenceAdapter(presencePacket));
 	    } catch (RemoteException e) {
 		Log.e(TAG, "resend subscription error", e);
 	    }
@@ -81,36 +113,17 @@
 
     }
 
-    public static final String TAG = "Option Dialog";
-
-    private final Contact      mContact;
-
-    private final Context      mContext;
-
-    private IXmppFacade        mService;
-
-    public ContactDialog(final Context context, Contact curContact) {
-	super(context);
-	mContext = context;
+    private class BeemServiceConnection implements ServiceConnection {
 
-	setContentView(R.layout.contactdialog);
-	mContact = curContact;
-	setTitle(curContact.getJID());
+	@Override
+	public void onServiceConnected(ComponentName name, IBinder service) {
+	    xmppFacade = IXmppFacade.Stub.asInterface(service);
+	}
 
-	Button chat = (Button) findViewById(R.id.CDChat);
-	chat.setOnClickListener(new chatListener());
-	Button alias = (Button) findViewById(R.id.CDAlias);
-	alias.setOnClickListener(new aliasListener());
-	Button group = (Button) findViewById(R.id.CDGroup);
-	group.setOnClickListener(new groupListener());
-	Button resend = (Button) findViewById(R.id.CDResend);
-	resend.setOnClickListener(new resendListener());
-	Button infos = (Button) findViewById(R.id.CDInfos);
-	infos.setOnClickListener(new infosListener());
-    }
-
-    public void initService() {
-	mService = BeemApplication.getApplication(ContactDialog.this.getOwnerActivity()).getXmppFacade();
+	@Override
+	public void onServiceDisconnected(ComponentName name) {
+	    xmppFacade = null;
+	}
     }
 
 }
--- a/src/com/beem/project/beem/ui/ContactList.java	Thu Jun 18 11:53:00 2009 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java	Thu Jun 18 20:22:56 2009 +0200
@@ -1,13 +1,11 @@
 package com.beem.project.beem.ui;
 
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.SortedMap;
 
 import org.jivesoftware.smack.util.StringUtils;
 
@@ -30,7 +28,6 @@
 import android.view.ViewGroup;
 import android.view.View.OnClickListener;
 import android.view.View.OnLongClickListener;
-import android.widget.ArrayAdapter;
 import android.widget.ExpandableListAdapter;
 import android.widget.ImageView;
 import android.widget.TextView;
@@ -105,16 +102,13 @@
     }
 
     class ComparatorContactListByName<T> implements Comparator<T> {
-
 	@Override
 	public int compare(T c1, T c2) {
 	    return ((Contact) c1).getName().compareToIgnoreCase(((Contact) c2).getName());
 	}
-
     }
 
     class ComparatorContactListByStatusAndName<T> implements Comparator<T> {
-
 	@Override
 	public int compare(T c1, T c2) {
 	    if (((Contact) c1).getStatus() < ((Contact) c2).getStatus()) {
@@ -124,14 +118,12 @@
 	    } else
 		return ((Contact) c1).getName().compareToIgnoreCase(((Contact) c2).getName());
 	}
-
     }
 
     private void buildContactList(List<Contact> listContact) {
-	Collections.sort(listContact, new ComparatorContactListByStatusAndName<Contact>());
-	// Collections.sort(listContact, new ComparatorContactListByName<Contact>());
 	mListContact = listContact;
-	for (Contact contact : listContact) {
+	Collections.sort(mListContact, new ComparatorContactListByStatusAndName<Contact>());
+	for (Contact contact : mListContact) {
 	    for (String group : contact.getGroups()) {
 		if (!groupMap.containsKey(group)) {
 		    groupMap.put(group, new ArrayList<Contact>());
@@ -344,7 +336,6 @@
 	void createDialog(Contact contact) {
 	    ContactDialog dialogContact = new ContactDialog(ContactList.this, contact);
 	    dialogContact.setOwnerActivity(ContactList.this);
-	    dialogContact.initService();
 	    dialogContact.show();
 	}