La liste marche, mais chut faut pas le dire a darkmouchefdeprojtyran
authornikita@nikita-lab
Wed, 20 May 2009 23:26:07 +0200
changeset 191 cc21283865ad
parent 190 079488da1bfc
child 192 b74384311dee
La liste marche, mais chut faut pas le dire a darkmouchefdeprojtyran sinon il va nous donner du boulot
res/layout/contactlistgroup.xml
src/com/beem/project/beem/service/Contact.java
src/com/beem/project/beem/service/PresenceAdapter.java
src/com/beem/project/beem/service/RosterAdapter.java
src/com/beem/project/beem/ui/AccountCreation.java
src/com/beem/project/beem/ui/ContactList.java
--- a/res/layout/contactlistgroup.xml	Sun May 17 23:33:13 2009 +0200
+++ b/res/layout/contactlistgroup.xml	Wed May 20 23:26:07 2009 +0200
@@ -11,8 +11,4 @@
             android:layout_height="wrap_content"
             android:paddingLeft="40sp" />
 
-        <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/service/Contact.java	Sun May 17 23:33:13 2009 +0200
+++ b/src/com/beem/project/beem/service/Contact.java	Wed May 20 23:26:07 2009 +0200
@@ -140,10 +140,15 @@
      * @param presence the presence containing status
      */
     public void setStatus(Presence presence) {
-	Log.i(TAG,"PRESENCE");
 	mStatus = Status.getStatusFromPresence(presence);
 	mMsgState = presence.getStatus();
     }
+    
+    public void setStatus(PresenceAdapter presence) {
+	mStatus = presence.getStatus();
+	mMsgState = presence.getStatusText();
+	
+    }
 
     /**
      * Get the message status of the contact.
@@ -220,6 +225,10 @@
 	    mGroups.add(rosterGroup.getName());
 	}
     }
+    
+    public void addGroup(String group) {
+	mGroups.add(group);
+    }
 
     /**
      * @param mGroups the mGroups to set
@@ -227,11 +236,11 @@
     public void setGroups(List<String> mGroups) {
 	this.mGroups = mGroups;
     }
-
+    
     /**
      * @return the mGroups
      */
     public List<String> getGroups() {
 	return mGroups;
-    }
+    }    
 }
--- a/src/com/beem/project/beem/service/PresenceAdapter.java	Sun May 17 23:33:13 2009 +0200
+++ b/src/com/beem/project/beem/service/PresenceAdapter.java	Wed May 20 23:26:07 2009 +0200
@@ -14,6 +14,7 @@
 	private int mStatus;
 	private String mTo;
 	private String mFrom;
+	private String mStatusText;
 	
 	/**
 	 * Parcelable.Creator needs by Android.
@@ -36,6 +37,7 @@
 		mStatus = Status.getStatusFromPresence(presence);
 		mTo = presence.getTo();
 		mFrom = presence.getFrom();
+		mStatusText = presence.getStatus();
 	}
 	
 	
@@ -44,6 +46,7 @@
 		mStatus = source.readInt();
 		mTo = source.readString();
 		mFrom = source.readString();
+		mStatusText = source.readString();
 	}
 
 	@Override
@@ -58,6 +61,7 @@
 		dest.writeInt(mStatus);
 		dest.writeString(mTo);
 		dest.writeString(mFrom);
+		dest.writeString(mStatusText);
 	}
 
 
@@ -122,4 +126,20 @@
 	public String getFrom() {
 	    return mFrom;
 	}
+
+
+	/**
+	 * @param mStatusText the mStatusText to set
+	 */
+	public void setStatusText(String mStatusText) {
+	    this.mStatusText = mStatusText;
+	}
+
+
+	/**
+	 * @return the mStatusText
+	 */
+	public String getStatusText() {
+	    return mStatusText;
+	}
 }
--- a/src/com/beem/project/beem/service/RosterAdapter.java	Sun May 17 23:33:13 2009 +0200
+++ b/src/com/beem/project/beem/service/RosterAdapter.java	Wed May 20 23:26:07 2009 +0200
@@ -71,6 +71,10 @@
 	    mAdaptee.createEntry(user, name, groups);
 	    Contact res = new Contact(user);
 	    mContacts.put(user, res);
+	    for (String group : groups) {
+		mAdaptee.createGroup(group);
+		res.addGroup(group);
+	    }
 	    return res;
 	} catch (XMPPException e) {
 	    Log.e(TAG, "Error while adding new contact", e);
--- a/src/com/beem/project/beem/ui/AccountCreation.java	Sun May 17 23:33:13 2009 +0200
+++ b/src/com/beem/project/beem/ui/AccountCreation.java	Wed May 20 23:26:07 2009 +0200
@@ -58,7 +58,7 @@
 		valid = false;
 		
 	    } else {
-		mAttributes.put("password", getWidgetText(R.id.password));
+		mAttributes.put("password", getWidgetText(R.id.ac_password));
 	    }
 	    if (getWidgetText(R.id.ac_email).length() == 0) {
 		valid = false;
--- a/src/com/beem/project/beem/ui/ContactList.java	Sun May 17 23:33:13 2009 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java	Wed May 20 23:26:07 2009 +0200
@@ -5,16 +5,19 @@
 import java.util.List;
 import java.util.Map;
 
+import org.jivesoftware.smack.util.StringUtils;
+
 import android.app.ExpandableListActivity;
-import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.database.DataSetObserver;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.RemoteException;
 import android.util.Log;
+import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
@@ -23,7 +26,6 @@
 import android.widget.ExpandableListAdapter;
 import android.widget.ExpandableListView;
 import android.widget.ImageView;
-import android.widget.SimpleExpandableListAdapter;
 import android.widget.TextView;
 
 import com.beem.project.beem.BeemApplication;
@@ -39,29 +41,22 @@
 
     private static final String TAG = "CONTACTLIST_ACT";
     private static final int PREFERENCECHANGED = 0;
-    private static final String CHILD = "CHILD";
-    private static final String GROUP = "GROUP";
+    private static final String DEFAULT_GROUP = "Default";
     private IXmppFacade mService = null;
-    private ExpandableListAdapter mAdapter;
+    private MyExpandableListAdapter mAdapter;
     private BeemApplication mBeemApplication;
     private BeemRosterListener mRosterListener;
     private SharedPreferences mSettings;
     private IRoster mRoster;
-
-    @SuppressWarnings("unchecked")
-    @Override
-    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
-	Map<String, Contact> child = (HashMap<String, Contact>) parent.getExpandableListAdapter().getChild(
-	    groupPosition, childPosition);
-	Intent i = new Intent(this, SendIM.class);
-	i.putExtra("contact", child.get(CHILD));
-	startActivity(i);
-	return true;
-    }
+    private Map<String, List<Contact>> groupMap;
+    private List<String> groupName;
+    private List<Contact> mListContact;
+    private Handler mHandler;
 
     @Override
     protected void onCreate(Bundle saveBundle) {
 	super.onCreate(saveBundle);
+	mHandler = new Handler();
 	mRosterListener = new BeemRosterListener();
 	mSettings = getSharedPreferences(getString(R.string.PreferenceFileName), MODE_PRIVATE);
 	mBeemApplication = BeemApplication.getApplication(this);
@@ -72,19 +67,15 @@
     @Override
     protected void onResume() {
 	super.onResume();
-
-	if (!mBeemApplication.isConnected()) {
-	    mBeemApplication = BeemApplication.getApplication(this);
-	    mBeemApplication.startBeemService();
-	}
-	mBeemApplication.callWhenConnectedToServer(new Handler(), new Runnable() {
+	
+	mBeemApplication.callWhenConnectedToServer(mHandler, new Runnable() {
 	    @Override
 	    public void run() {
 		mService = mBeemApplication.getXmppFacade();
 		try {
 		    mRoster = mService.getRoster();
 		} catch (RemoteException e1) {
-		    e1.printStackTrace();
+		    Log.e(TAG, "Get roster failed", e1);
 		}
 		if (mRoster != null) {
 		    try {
@@ -97,13 +88,16 @@
 		    callbackShowContactList();
 	    }
 	});
+	if (!mBeemApplication.isConnected()) {
+	    mBeemApplication = BeemApplication.getApplication(this);
+	    mBeemApplication.startBeemService();
+	}
     }
 
     @Override
     protected void onDestroy() {
-	// TODO Auto-generated method stub
-	super.onDestroy();
 	mBeemApplication.unbindBeemService();
+	super.onDestroy();	
     }
 
     private void callbackShowContactList() {
@@ -112,7 +106,7 @@
 	 */
 	if (mRoster != null)
 	    try {
-		showContactList(mRoster.getGroupsNames(), mRoster.getContactList());
+		buildContactList(mRoster.getContactList());
 	    } catch (RemoteException e) {
 		e.printStackTrace();
 	    }
@@ -157,7 +151,21 @@
 	}
     }
 
+    @Override
+    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
+	Intent i = new Intent(this, SendIM.class);
+	try {
+	    i.putExtra("contact", groupMap.get(groupName.get(groupPosition)).get(childPosition));
+	    startActivity(i);
+	    return true;
+	} catch (NullPointerException e) {
+	    Log.e(TAG, "Child not found", e);
+	    return false;
+	}
+    }
+
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+	//TODO:marche pas
 	if (requestCode == PREFERENCECHANGED) {
 	    if (resultCode == RESULT_OK) {
 		mAdapter = null;
@@ -167,82 +175,184 @@
 	}
     }
 
-    private void showContactList(List<String> listGroup, List<Contact> listContact) {
-	List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
-	List<List<Map<String, Contact>>> childData = new ArrayList<List<Map<String, Contact>>>();
-	int groupSize = listGroup.size();
-	if (groupSize == 0)
-	    listGroup.add("Default");
-	for (int i = 0; i < listGroup.size(); i++) {
-	    Map<String, String> curGroupMap = new HashMap<String, String>();
-
-	    groupData.add(curGroupMap);
-	    curGroupMap.put("GROUP", listGroup.get(i));
-
-	    List<Map<String, Contact>> children = new ArrayList<Map<String, Contact>>();
-	    for (int j = 0; j < listContact.size(); ++j) {
-		Contact c = listContact.get(j);
-		if (groupSize == 0 || c.getGroups().contains(listGroup.get(i))) {
-		    Log.i(TAG, c.getID() + " " + c.getJID());
-		    Map<String, Contact> curChildMap = new HashMap<String, Contact>();
-		    children.add(curChildMap);
-		    curChildMap.put(CHILD, c);
+    private void buildContactList(List<Contact> listContact) {
+	mListContact = listContact;
+	groupMap = new HashMap<String, List<Contact>>();
+	groupName = new ArrayList<String>();
+	for (Contact contact : listContact) {
+	    for (String group: contact.getGroups()) {
+		if (!groupMap.containsKey(group)) {
+		    groupMap.put(group, new ArrayList<Contact>());
+		    groupName.add(group);
+		}
+		try {
+		    groupMap.get(group).add(contact);
+		} catch (NullPointerException e) {
+		    Log.e(TAG, "Failed to find group in groupMap", e);
 		}
 	    }
-	    childData.add(children);
+	    if (contact.getGroups().isEmpty()) {
+		if (!groupMap.containsKey(DEFAULT_GROUP)) {
+		    groupMap.put(DEFAULT_GROUP, new ArrayList<Contact>());
+		    groupName.add(DEFAULT_GROUP);
+		}
+		groupMap.get(DEFAULT_GROUP).add(contact);
+	    }
 	}
-
-	mAdapter = new ContactExpandableListAdapter(this, groupData, R.layout.contactlistgroup, new String[] { GROUP },
-	    new int[] { R.id.textgroup }, childData, R.layout.contactlistcontact, new String[] { CHILD }, new int[] {
-		R.id.contactliststatus, R.id.contactlistpseudo, R.id.contactlistmsgperso, R.id.contactlistavatar });
+	mAdapter = new MyExpandableListAdapter();
 	setListAdapter(mAdapter);
     }
 
-    /**
-     * 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)
-     */
-    private class ContactExpandableListAdapter extends SimpleExpandableListAdapter {
+    private class MyExpandableListAdapter implements ExpandableListAdapter {
 
-	private List<? extends List<? extends Map<String, ?>>> mChildData;
-	private String[] mChildFrom;
-	private int[] mChildTo;
+	private List<DataSetObserver> observers; 
+	
+	public MyExpandableListAdapter() {
+	    observers = new ArrayList<DataSetObserver>();
+	}
+	
+	public void changed() {
+	    for (DataSetObserver obs : observers) {
+		obs.onChanged();
+	    }
+	}
+	
+	
+	@Override
+	public boolean areAllItemsEnabled() {
+	    return true;
+	}
 
-	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);
+	@Override
+	public Object getChild(int groupPosition, int childPosition) {
+	    try {
+		return groupMap.get(groupName.get(groupPosition)).get(childPosition);
+	    } catch (NullPointerException e) {
+		Log.e(TAG, "Child not found", e);
+		return null;
+	    }
+	}
 
-	    mChildData = childData;
-	    mChildFrom = childFrom;
-	    mChildTo = childTo;
-
+	@Override
+	public long getChildId(int groupPosition, int childPosition) {
+	    try {
+		groupMap.get(groupName.get(groupPosition)).get(childPosition);
+	    } catch (NullPointerException e) {
+		Log.e(TAG, "Child not found", e);
+		return 0;
+	    }
+	    return childPosition;
 	}
 
 	@Override
 	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
 	    ViewGroup parent) {
-
 	    View v;
 	    if (convertView == null) {
-		v = newChildView(isLastChild, parent);
+		v  = LayoutInflater.from(ContactList.this).inflate(R.layout.contactlistcontact, null);
+		Log.d(TAG, "Convert view est vide sur un getChildView");
 	    } else {
 		v = convertView;
 	    }
-	    bindView(v, mChildData.get(groupPosition).get(childPosition), mChildFrom, mChildTo, groupPosition,
-		childPosition);
+	    bindView(v, groupMap.get(groupName.get(groupPosition)).get(childPosition));
 	    return v;
 	}
 
-	private void bindView(View view, Map<String, ?> data, String[] from, int[] to, int groupPosition,
-	    int childPosition) {
-	    Contact c = (Contact) data.get(from[0]);
+	@Override
+	public int getChildrenCount(int groupPosition) {
+	    try {
+		return groupMap.get(groupName.get(groupPosition)).size();
+	    } catch (NullPointerException e) {
+		Log.e(TAG, "Child not found", e);
+		return 0;
+	    }
+	}
+
+	@Override
+	public long getCombinedChildId(long groupId, long childId) {
+	    return 1000 * groupId + childId;
+	}
+
+	@Override
+	public long getCombinedGroupId(long groupId) {
+	    return 1000 * groupId;
+	}
+
+	@Override
+	public Object getGroup(int groupPosition) {
+	    try {
+		return groupMap.get(groupName.get(groupPosition));
+	    } catch (NullPointerException e) {
+		Log.e(TAG, "Group not found", e);
+		return null;
+	    }
+	}
+
+	@Override
+	public int getGroupCount() {
+	    return groupMap.size();
+	}
+
+	@Override
+	public long getGroupId(int groupPosition) {
+	    return groupPosition;
+	}
 
-	    if (c != null) {
+	@Override
+	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
+	    Log.d(TAG, "View getGroupView");
+	    View holder = LayoutInflater.from(ContactList.this).inflate(R.layout.contactlistgroup, null);
+	    TextView groupTextView = (TextView) holder.findViewById(R.id.textgroup);
+	    groupTextView.setText(groupName.get(groupPosition));
+	    return holder;
+	}
+
+	@Override
+	public boolean hasStableIds() {
+	    Log.d(TAG, "boolean hasStableIds");
+	    return false;
+	}
+
+	@Override
+	public boolean isChildSelectable(int groupPosition, int childPosition) {
+	    Log.d(TAG, "boolean isChildSelectable");
+	    return !false;
+	}
+
+	@Override
+	public boolean isEmpty() {
+	    return groupMap.isEmpty();
+	}
 
-		ImageView imgV = (ImageView) view.findViewById(to[0]);
+	@Override
+	public void onGroupCollapsed(int groupPosition) {
+	    // TODO A voir
+	}
+
+	@Override
+	public void onGroupExpanded(int groupPosition) {
+	    // TODO A voir
+
+	}
+
+	@Override
+	public void registerDataSetObserver(DataSetObserver observer) {
+	    Log.d(TAG, "registerDataSetObserver");
+	    observers.add(observer);
+	}
+
+	@Override
+	public void unregisterDataSetObserver(DataSetObserver observer) {
+	    Log.d(TAG, "unregisterDataSetObserver");
+	    observers.remove(observer);
+	}
+
+	private void bindView(View view, Contact curContact) {
+
+	    if (curContact != null) {
+		ImageView imgV = (ImageView) view.findViewById(R.id.contactliststatus);
 		Drawable imageDrawable = null;
-		switch (c.getStatus()) {
+		switch (curContact.getStatus()) {
 		    case Status.CONTACT_STATUS_AVAILABLE:
 			imageDrawable = (Drawable) getResources().getDrawable(R.drawable.online);
 			break;
@@ -263,30 +373,29 @@
 			break;
 		    default:
 			imageDrawable = (Drawable) getResources().getDrawable(R.drawable.error);
-			break;
+		    break;
 		}
 		imgV.setImageDrawable(imageDrawable);
 
-		TextView v = (TextView) view.findViewById(to[1]);
+		TextView v = (TextView) view.findViewById(R.id.contactlistpseudo);
 		if (v != null) {
-		    v.setText(c.getJID());
+		    v.setText(curContact.getJID());
 		}
 
-		v = (TextView) view.findViewById(to[2]);
+		v = (TextView) view.findViewById(R.id.contactlistmsgperso);
 		if (v != null) {
-		    v.setText(c.getMsgState());
+		    v.setText(curContact.getMsgState());
 		}
 
-		/*
-		 * TODO: Rajouter l'avatar du contact getAvatar() dans la classe
-		 */
-		imgV = (ImageView) view.findViewById(to[3]);
+		//TODO: Rajouter l'avatar du contact getAvatar() dans la classe
+		imgV = (ImageView) view.findViewById(R.id.contactlistavatar);
 		if (imgV != null) {
 		    imageDrawable = (Drawable) getResources().getDrawable(R.drawable.avatar);
 		    imgV.setImageDrawable(imageDrawable);
 		}
 	    }
 	}
+
     }
 
     private class BeemRosterListener extends IBeemRosterListener.Stub {
@@ -311,8 +420,18 @@
 
 	@Override
 	public void onPresenceChanged(PresenceAdapter presence) throws RemoteException {
-	    Log.i(TAG, "PRESENCE CHANGED");
-
+	    for (Contact curContact :mListContact) {
+		if (curContact.getJID().equals(StringUtils.parseBareAddress(presence.getFrom()))) {
+		    curContact.setStatus(presence);
+		    mHandler.post(new Runnable() {
+			@Override
+			public void run() {
+			    mAdapter.changed();
+			}
+		    });
+		    return ;
+		}
+	    }
 	}
 
     }