Contact liste recuperer.
authormarseille@KungFuh
Tue, 31 Mar 2009 20:46:06 +0200
changeset 47 743ccc7961dc
parent 46 d5a36d820645
child 48 e7a787b81100
Contact liste recuperer. Cest pas encore au top!
res/layout/contactlist.xml
res/layout/contactlistcontact.xml
res/layout/contactlistgroup.xml
src/com/beem/project/beem/BeemApplication.java
src/com/beem/project/beem/ui/ContactList.java
--- a/res/layout/contactlist.xml	Tue Mar 31 22:21:42 2009 +0200
+++ b/res/layout/contactlist.xml	Tue Mar 31 20:46:06 2009 +0200
@@ -1,19 +1,16 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 	android:layout_width="fill_parent"
-    android:layout_height="wrap_content"
-    android:orientation="vertical">
-                    	         	
-	<ImageView android:id="@+id/avatar"
-		android:src="@drawable/avatar"
-		android:paddingLeft="50sp"
-		android:layout_width="wrap_content"
-		android:layout_height="wrap_content"/>
- 
-     <TextView android:id="@+id/textchild"
-         android:textSize="16sp"         
-         android:paddingLeft="100sp"
-         android:layout_width="fill_parent"
-         android:layout_height="wrap_content"/>
-		
- </LinearLayout>
+        android:orientation="vertical"
+        android:layout_width="fill_parent"
+        android:layout_height="fill_parent">
+
+        <ExpandableListView android:id="@+id/android:list"
+			android:layout_width="fill_parent"
+			android:layout_height="fill_parent"
+            android:layout_weight="1" />
+
+        <TextView android:id="@+id/android:empty"
+			android:layout_width="fill_parent"
+            android:layout_height="fill_parent" android:text="Non connecte" />
+
+</LinearLayout> 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/res/layout/contactlistcontact.xml	Tue Mar 31 20:46:06 2009 +0200
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:orientation="horizontal"
+        android:layout_width="fill_parent"
+        android:layout_height="50sp" 
+        android:gravity="center_vertical" >
+
+        <ImageView android:id="@+id/avatar"
+        	android:layout_width="70px"
+            android:layout_height="40sp" />
+		<LinearLayout
+			android:orientation="vertical"
+        	android:layout_width="fill_parent"
+        	android:layout_height="40sp">
+        		
+        	<TextView android:id="@+id/textchild1"
+				android:layout_width="fill_parent"
+            	android:layout_height="wrap_content"
+            	android:paddingLeft="20sp" />
+
+        	<TextView android:id="@+id/textchild2"
+				android:layout_width="fill_parent"
+            	android:layout_height="wrap_content" />
+		</LinearLayout>
+
+</LinearLayout> 
\ No newline at end of file
--- a/res/layout/contactlistgroup.xml	Tue Mar 31 22:21:42 2009 +0200
+++ b/res/layout/contactlistgroup.xml	Tue Mar 31 20:46:06 2009 +0200
@@ -1,16 +1,18 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-	android:layout_width="fill_parent"
-    android:layout_height="wrap_content"
-    android:orientation="vertical">
+        android:orientation="horizontal"
+        android:layout_width="fill_parent"
+        android:layout_height="50sp"
+		android:paddingTop="5sp"
+		android:gravity="center_vertical">
 
-	<TextView android:id="@+id/textgroup"
-		android:height="50sp"
-		android:textSize="20sp"
-       	android:textStyle="bold"
-       	android:paddingTop="10sp"
-       	android:paddingLeft="40sp"
-       	android:layout_width="fill_parent"
-       	android:layout_height="wrap_content" />	
+        <TextView android:id="@+id/textgroup"
+        	android:layout_width="fill_parent"
+            android:layout_height="wrap_content"
+            android:paddingLeft="40sp" />
 
- </LinearLayout>
+        <TextView android:id="@+id/rowText2"
+			android:layout_width="fill_parent"
+            android:layout_height="wrap_content" />
+
+</LinearLayout> 
\ No newline at end of file
--- a/src/com/beem/project/beem/BeemApplication.java	Tue Mar 31 22:21:42 2009 +0200
+++ b/src/com/beem/project/beem/BeemApplication.java	Tue Mar 31 20:46:06 2009 +0200
@@ -31,6 +31,7 @@
     private Resources mPrivateResources;
     private static BeemApplication mBeemApp;
     private List<Message> mQueue = new LinkedList<Message>();
+    private boolean mIsConnected;
     
     public static BeemApplication getApplication(Activity activity) {
 	if (mBeemApp == null) {
@@ -68,12 +69,13 @@
     }
 
     public synchronized void startBeemService() {
-	if (mFacade == null) {
+	if (!mIsConnected) {
 	    // Intent intent = new Intent(this, BeemService.class);
 	    Intent intent = new Intent();
 	    intent.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
 	    mApplicationContext.startService(intent);
 	    mApplicationContext.bindService(intent, mServConn, BIND_AUTO_CREATE);
+	    mIsConnected = true;
 	}
     }
 
@@ -82,7 +84,7 @@
 	@Override
 	public void onServiceDisconnected(ComponentName name) {
 	    mFacade = null;
-
+	    mIsConnected = false;
 	}
 
 	@Override
@@ -110,7 +112,7 @@
     
     public void callWhenServiceConnected(Handler target, Runnable callback) {
 	Message msg = Message.obtain(target, callback);
-	if (mFacade != null) {
+	if (!mIsConnected) {
 	    msg.sendToTarget();
 	} else {
 	    startBeemService();
--- a/src/com/beem/project/beem/ui/ContactList.java	Tue Mar 31 22:21:42 2009 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java	Tue Mar 31 20:46:06 2009 +0200
@@ -6,145 +6,161 @@
 import java.util.Map;
 
 import android.app.ExpandableListActivity;
-import android.content.ComponentName;
-import android.content.Intent;
-import android.content.ServiceConnection;
+import android.app.ProgressDialog;
+import android.content.Context;
+import android.graphics.drawable.Drawable;
 import android.os.Bundle;
-import android.os.IBinder;
+import android.os.Handler;
 import android.os.RemoteException;
 import android.util.Log;
+import android.view.View;
+import android.view.ViewGroup;
 import android.widget.ExpandableListAdapter;
+import android.widget.ImageView;
 import android.widget.SimpleExpandableListAdapter;
+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.aidl.IBeemConnectionListener;
-import com.beem.project.beem.service.aidl.IRoster;
-import com.beem.project.beem.service.aidl.IXMPPConnection;
 import com.beem.project.beem.service.aidl.IXMPPFacade;
 
 public class ContactList extends ExpandableListActivity {
 
-    private static final String TAG = "CONTACTLIST_ACT";
-    
-    private IXMPPFacade mService = null;
-
-    private BeemApplication mBeemApplication;
-    
-    @Override
-    public void onCreate(Bundle saveBundle) {
-        super.onCreate(saveBundle);
-        mBeemApplication = BeemApplication.getApplication(this);
-        mBeemApplication.startBeemService();
-        mService = mBeemApplication.getXmppFacade();
-        /* bindService(new Intent(this, BeemService.class), mConnection,
-                BIND_AUTO_CREATE | BIND_DEBUG_UNBIND); */
-        showContactList();
-    }
-
-    private void showContactList() {
-        ExpandableListAdapter Adapter;
-        
-        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
-        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
-
-        for (int i = 0; i < 2; i++) {
-            Map<String, String> curGroupMap = new HashMap<String, String>();
-            groupData.add(curGroupMap);
-            curGroupMap.put("NAME", "Group " + i);
-            
-            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
-            for (int j = 0; j < 5; j++) {
-                Map<String, String> curChildMap = new HashMap<String, String>();
-                children.add(curChildMap);
-                curChildMap.put("NAOME CHILD", "Child " + j);                
-            }
-            childData.add(children);
-        }
-
-        Adapter = new SimpleExpandableListAdapter(this, 
-                groupData, R.layout.contactlistgroup,
-                new String[] {"NAME"}, new int[] {R.id.textgroup},
-                childData, R.layout.contactlist,
-                new String[] {"NAME CHILD"}, new int[] {R.id.textchild});
-        setListAdapter(Adapter);
-    }
-
-    private ServiceConnection mConnection = new ServiceConnection() {
-        @Override
-        public void onServiceConnected(ComponentName name, IBinder service) {
-            mService = IXMPPFacade.Stub.asInterface(service);
-            try {
-		IXMPPConnection con = mService.createConnection();
-		con.addConnectionListener(new TestConnectionListener());
-		mService.connectSync();
-		Log.i("BEEM", "Connected !!!");
-	    } catch (RemoteException e) {
-		// TODO Auto-generated catch block
-		e.printStackTrace();
-	    }
-            
-            /*
-             * mService.getGroupList(); mService.getContactList();
-             */
-        }
-
-        @Override
-        public void onServiceDisconnected(ComponentName name) {
-        }
-
-    };
-
-    @Override
-    public void onDestroy() {
-        super.onDestroy();
-        unbindService(mConnection);
-    }
-
-    private class TestConnectionListener extends IBeemConnectionListener.Stub {
+	private static final String TAG = "CONTACTLIST_ACT";
+	private IXMPPFacade mService = null;
+	private Handler mHandler;
+	private BeemApplication mBeemApplication;
 
 	@Override
-	public void connectionClosed() throws RemoteException {
-	    // TODO Auto-generated method stub
-	    
-	}
+	public void onCreate(Bundle saveBundle) {
+		super.onCreate(saveBundle);		
+		mHandler = new Handler();
+		mBeemApplication = BeemApplication.getApplication(this);
+	
 
-	@Override
-	public void connectionClosedOnError() throws RemoteException {
-	    // TODO Auto-generated method stub
-	    
 	}
-
+	
 	@Override
-	public void onConnect() throws RemoteException {
-	    // TODO Auto-generated method stub
-	    IRoster roster = mService.getRoster();
-	    for (Contact contact : roster.getContactList()) {
-		Log.v(TAG,"Contact name " + contact.getJID() );
-	    }
-	    showContactList();
+	public void onStart() {
+		super.onStart();		
+		mBeemApplication.startBeemService();
+		mBeemApplication.callWhenServiceConnected(mHandler, new Runnable() {
+			@Override
+			public void run() {
+				mService = mBeemApplication.getXmppFacade();
+				try {
+					showContactList(mService.getRoster().getContactList(),
+							mService.getRoster().getContactList());
+				} catch (RemoteException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+				
+			}
+		});
 	}
 
-	@Override
-	public void reconnectingIn(int seconds) throws RemoteException {
-	    // TODO Auto-generated method stub
-	    
+	private void showContactList(List<Contact> listGroup,
+			List<Contact> listContact) {
+		ExpandableListAdapter Adapter;
+
+		List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
+		List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
+
+		if (listGroup.size() == 0)
+			listGroup.add(new Contact());
+		for (int i = 0; i < listGroup.size() ; i++) {
+			Map<String, String> curGroupMap = new HashMap<String, String>();
+
+			groupData.add(curGroupMap);
+			curGroupMap.put("NAME", "Default");
+
+			List<Map<String, String>> children = new ArrayList<Map<String, String>>();
+			for (int j = 0; j < listContact.size() ; ++j) {
+				Map<String, String> curChildMap = new HashMap<String, String>();
+				children.add(curChildMap);
+				curChildMap.put("NAME_CHILD", listContact.get(j).getJID());
+				curChildMap.put("MSG", "Taper votre message perso");
+			}
+			childData.add(children);
+		}
+
+		Adapter = new ContactExpandableListAdapter(this, groupData,
+				R.layout.contactlistgroup, new String[] { "NAME" },
+				new int[] { R.id.textgroup }, childData,
+				R.layout.contactlistcontact,
+				new String[] { "NAME_CHILD", "MSG" }, new int[] {
+						R.id.textchild1, R.id.textchild2, R.id.avatar });
+		setListAdapter(Adapter);
+	}
+	
+	/**
+	 * A simple adapter which allows you to bind data to specific Views defined
+	 * within the layout of an Expandable Lists children (Implement
+	 * getGroupView() to define the layout of parents)
+	 */
+	public class ContactExpandableListAdapter extends
+			SimpleExpandableListAdapter {
+
+		private List<? extends List<? extends Map<String, ?>>> mChildData;
+		private String[] mChildFrom;
+		private int[] mChildTo;
+
+		public ContactExpandableListAdapter(Context context,
+				List<? extends Map<String, ?>> groupData, int groupLayout,
+				String[] groupFrom, int[] groupTo,
+				List<? extends List<? extends Map<String, ?>>> childData,
+				int childLayout, String[] childFrom, int[] childTo) {
+			super(context, groupData, groupLayout, groupFrom, groupTo,
+					childData, childLayout, childFrom, childTo);
+
+			mChildData = childData;
+			mChildFrom = childFrom;
+			mChildTo = childTo;
+
+		}
+
+		@Override
+		public View getChildView(int groupPosition, int childPosition,
+				boolean isLastChild, View convertView, ViewGroup parent) {
+
+			View v;
+			if (convertView == null) {
+				v = newChildView(isLastChild, parent);
+			} else {
+				v = convertView;
+			}
+			bindView(v, mChildData.get(groupPosition).get(childPosition),
+					mChildFrom, mChildTo, groupPosition, childPosition);
+			return v;
+		}
+
+		// This method binds my data to the Views specified in the child
+		// xmllayout
+		private void bindView(View view, Map<String, ?> data, String[] from,
+				int[] to, int groupPosition, int childPosition) {
+			// Apply TextViews
+			TextView v1 = (TextView) view.findViewById(to[0]);
+			if (v1 != null) {
+				Log.i("CONTACT LIST 1", (String) data.get(from[0]) + " "
+						+ to[0]);
+				v1.setText((String) data.get(from[0]));
+			}
+			TextView v2 = (TextView) view.findViewById(to[1]);
+			if (v2 != null) {
+				Log.i("CONTACT LIST 2", (String) data.get(from[1]) + " "
+						+ to[1]);
+				v2.setText((String) data.get(from[1]));
+			}
+			// Apply ImageView
+			ImageView imgV = (ImageView) view.findViewById(to[2]);
+			if (imgV != null) {
+				Drawable avatar = (Drawable) getResources().getDrawable(
+						R.drawable.avatar);
+				imgV.setImageDrawable(avatar);
+			}
+		}
 	}
 
-	@Override
-	public void reconnectionFailed() throws RemoteException {
-	    // TODO Auto-generated method stub
-	    
-	}
-
-	@Override
-	public void reconnectionSuccessful() throws RemoteException {
-	    // TODO Auto-generated method stub
-	    
-	}
-	
-    } 
-    
 }