--- 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 ;
+ }
+ }
}
}