# HG changeset patch # User nikita@nikita-lab # Date 1245509850 -7200 # Node ID bce0846bc3ed598736115b14f4040ee9b0d514a5 # Parent cc4c230b5bd2aef8bd2f239fb2e01a72f0e59fd8 Bug #178 Résolu, ajout d'un tri sur les listes dans groupMap dans ContactList.java diff -r cc4c230b5bd2 -r bce0846bc3ed src/com/beem/project/beem/ui/ContactList.java --- a/src/com/beem/project/beem/ui/ContactList.java Sat Jun 20 15:28:42 2009 +0200 +++ b/src/com/beem/project/beem/ui/ContactList.java Sat Jun 20 16:57:30 2009 +0200 @@ -43,444 +43,446 @@ public class ContactList extends ExpandableListActivity { - private static final String TAG = "CONTACTLIST_ACT"; - private static final String DEFAULT_GROUP = "Default"; - private MyExpandableListAdapter mAdapter; - private IRoster mRoster; - private Map> groupMap; - private List groupName; - private List mListContact; - private Handler mHandler; - private IXmppFacade xmppFacade = null; - private final ServiceConnection mServConn = new BeemServiceConnection(); - - /** - * Callback for menu creation. - * @param menu the menu created - * @return true on success, false otherwise - */ - @Override - public final boolean onCreateOptionsMenu(Menu menu) { - super.onCreateOptionsMenu(menu); - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.contact_list, menu); - return true; - } - - /** - * Callback for menu item selected. - * @param item the item selected - * @return true on success, false otherwise - */ - @Override - public final boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.contact_list_menu_settings: - startActivity(new Intent(this, EditSettings.class)); - return true; - case R.id.contact_list_menu_add_contact: - startActivity(new Intent(ContactList.this, AddContact.class)); - return true; - default: - return false; - } - } - - @Override - protected void onCreate(Bundle saveBundle) { - super.onCreate(saveBundle); - bindService(new Intent(this, BeemService.class), mServConn, BIND_AUTO_CREATE); - mHandler = new Handler(); - groupMap = new HashMap>(); - groupName = new ArrayList(); - } + private static final String TAG = "CONTACTLIST_ACT"; + private static final String DEFAULT_GROUP = "Default"; + private MyExpandableListAdapter mAdapter; + private IRoster mRoster; + private Map> groupMap; + private List groupName; + private List mListContact; + private Handler mHandler; + private IXmppFacade xmppFacade = null; + private final ServiceConnection mServConn = new BeemServiceConnection(); - @Override - protected void onDestroy() { - super.onDestroy(); - unbindService(mServConn); - } - - class ComparatorContactListByName implements Comparator { + /** + * Callback for menu creation. + * @param menu the menu created + * @return true on success, false otherwise + */ @Override - public int compare(T c1, T c2) { - return ((Contact) c1).getName().compareToIgnoreCase(((Contact) c2).getName()); + public final boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.contact_list, menu); + return true; } - } - - class ComparatorContactListByStatusAndName implements Comparator { - @Override - public int compare(T c1, T c2) { - if (((Contact) c1).getStatus() < ((Contact) c2).getStatus()) { - return 1; - } else if (((Contact) c1).getStatus() > ((Contact) c2).getStatus()) { - return -1; - } else - return ((Contact) c1).getName().compareToIgnoreCase(((Contact) c2).getName()); - } - } - private void buildContactList(List listContact) { - mListContact = listContact; - Collections.sort(mListContact, new ComparatorContactListByStatusAndName()); - for (Contact contact : mListContact) { - for (String group : contact.getGroups()) { - if (!groupMap.containsKey(group)) { - groupMap.put(group, new ArrayList()); - groupName.add(group); - } - try { - if (!groupMap.get(group).contains(contact)) - groupMap.get(group).add(contact); - } catch (NullPointerException e) { - Log.e(TAG, "Failed to find group in groupMap", e); + /** + * Callback for menu item selected. + * @param item the item selected + * @return true on success, false otherwise + */ + @Override + public final boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.contact_list_menu_settings: + startActivity(new Intent(this, EditSettings.class)); + return true; + case R.id.contact_list_menu_add_contact: + startActivity(new Intent(ContactList.this, AddContact.class)); + return true; + default: + return false; } - } - if (contact.getGroups().isEmpty()) { - if (!groupMap.containsKey(DEFAULT_GROUP)) { - groupMap.put(DEFAULT_GROUP, new ArrayList()); - groupName.add(DEFAULT_GROUP); - } - groupMap.get(DEFAULT_GROUP).add(contact); - } } - mAdapter = new MyExpandableListAdapter(); - setListAdapter(mAdapter); - } - - private class BeemRosterListener extends IBeemRosterListener.Stub { @Override - public void onEntriesAdded(List addresses) throws RemoteException { - for (String str : addresses) { - Contact curContact = mRoster.getContact(str); - for (String group : curContact.getGroups()) { - if (!groupMap.containsKey(group)) { - groupMap.put(group, new ArrayList()); - groupName.add(group); - } - try { - groupMap.get(group).add(curContact); - } catch (NullPointerException e) { - Log.e(TAG, "Failed to find group in groupMap", e); - } - } - } - mHandler.post(new RunnableChange()); + protected void onCreate(Bundle saveBundle) { + super.onCreate(saveBundle); + bindService(new Intent(this, BeemService.class), mServConn, BIND_AUTO_CREATE); + mHandler = new Handler(); + groupMap = new HashMap>(); + groupName = new ArrayList(); } @Override - public void onEntriesDeleted(List addresses) throws RemoteException { - for (List cList : groupMap.values()) { - for (Contact curContact : cList) { - for (String addr : addresses) { - if (addr.equals(curContact.getJID())) { - cList.remove(curContact); - if (cList.isEmpty()) { - groupMap.values().remove(cList); - } + protected void onDestroy() { + super.onDestroy(); + unbindService(mServConn); + } + + class ComparatorContactListByName implements Comparator { + @Override + public int compare(T c1, T c2) { + return ((Contact) c1).getName().compareToIgnoreCase(((Contact) c2).getName()); + } + } + + class ComparatorContactListByStatusAndName implements Comparator { + @Override + public int compare(T c1, T c2) { + if (((Contact) c1).getStatus() < ((Contact) c2).getStatus()) { + return 1; + } else if (((Contact) c1).getStatus() > ((Contact) c2).getStatus()) { + return -1; + } else + return ((Contact) c1).getName().compareToIgnoreCase(((Contact) c2).getName()); + } + } + + private void buildContactList(List listContact) { + mListContact = listContact; + Collections.sort(mListContact, new ComparatorContactListByStatusAndName()); + for (Contact contact : mListContact) { + for (String group : contact.getGroups()) { + if (!groupMap.containsKey(group)) { + groupMap.put(group, new ArrayList()); + Collections.sort(groupMap.get(group), new ComparatorContactListByStatusAndName()); + groupName.add(group); + } + try { + if (!groupMap.get(group).contains(contact)) + groupMap.get(group).add(contact); + } catch (NullPointerException e) { + Log.e(TAG, "Failed to find group in groupMap", e); + } } - } + if (contact.getGroups().isEmpty()) { + if (!groupMap.containsKey(DEFAULT_GROUP)) { + groupMap.put(DEFAULT_GROUP, new ArrayList()); + groupName.add(DEFAULT_GROUP); + } + groupMap.get(DEFAULT_GROUP).add(contact); + } } - } - mHandler.post(new RunnableChange()); + mAdapter = new MyExpandableListAdapter(); + setListAdapter(mAdapter); } - @Override - public void onEntriesUpdated(List addresses) throws RemoteException { - for (String str : addresses) { - Contact curContact = mRoster.getContact(str); - for (String group : curContact.getGroups()) { - if (!groupMap.containsKey(group)) { - groupMap.put(group, new ArrayList()); - groupName.add(group); - groupMap.get(group).add(curContact); - } else { - boolean found = false; - for (Contact tempContact : groupMap.get(group)) { - if (tempContact.getJID() == str) { - curContact = tempContact; - found = true; - break; - } + private class BeemRosterListener extends IBeemRosterListener.Stub { + + @Override + public void onEntriesAdded(List addresses) throws RemoteException { + for (String str : addresses) { + Contact curContact = mRoster.getContact(str); + for (String group : curContact.getGroups()) { + if (!groupMap.containsKey(group)) { + groupMap.put(group, new ArrayList()); + groupName.add(group); + } + try { + groupMap.get(group).add(curContact); + } catch (NullPointerException e) { + Log.e(TAG, "Failed to find group in groupMap", e); + } + } } - if (!found) { - groupMap.get(group).add(curContact); + mHandler.post(new RunnableChange()); + } + + @Override + public void onEntriesDeleted(List addresses) throws RemoteException { + for (List cList : groupMap.values()) { + for (Contact curContact : cList) { + for (String addr : addresses) { + if (addr.equals(curContact.getJID())) { + cList.remove(curContact); + if (cList.isEmpty()) { + groupMap.values().remove(cList); + } + } + } + } } - } + mHandler.post(new RunnableChange()); } - } - mHandler.post(new RunnableChange()); - } - @Override - public void onPresenceChanged(PresenceAdapter presence) throws RemoteException { - for (Contact curContact : mListContact) { - if (curContact.getJID().equals(StringUtils.parseBareAddress(presence.getFrom()))) { - curContact.setStatus(presence); - mHandler.post(new RunnableChange()); - return; + @Override + public void onEntriesUpdated(List addresses) throws RemoteException { + for (String str : addresses) { + Contact curContact = mRoster.getContact(str); + for (String group : curContact.getGroups()) { + if (!groupMap.containsKey(group)) { + groupMap.put(group, new ArrayList()); + groupName.add(group); + groupMap.get(group).add(curContact); + } else { + boolean found = false; + for (Contact tempContact : groupMap.get(group)) { + if (tempContact.getJID() == str) { + curContact = tempContact; + found = true; + break; + } + } + if (!found) { + groupMap.get(group).add(curContact); + } + } + } + } + mHandler.post(new RunnableChange()); } - } - } - - private class RunnableChange implements Runnable { - @Override - public void run() { - mAdapter.changed(); - } - } - } - - private class MyExpandableListAdapter implements ExpandableListAdapter { - - class MyOnClickListener implements OnClickListener { - - private final Contact mContact; + @Override + public void onPresenceChanged(PresenceAdapter presence) throws RemoteException { + for (Contact curContact : mListContact) { + if (curContact.getJID().equals(StringUtils.parseBareAddress(presence.getFrom()))) { + curContact.setStatus(presence); + mHandler.post(new RunnableChange()); + return; + } + } + } - public MyOnClickListener(Contact contact) { - mContact = contact; - } - - @Override - public void onClick(View v) { - Intent i = new Intent(ContactList.this, SendIM.class); - i.setData(mContact.toUri()); - startActivity(i); - } + private class RunnableChange implements Runnable { + @Override + public void run() { + mAdapter.changed(); + } + } } - class MyOnLongClickListener implements OnLongClickListener { + private class MyExpandableListAdapter implements ExpandableListAdapter { + + class MyOnClickListener implements OnClickListener { - private final Contact mContact; + private final Contact mContact; + + public MyOnClickListener(Contact contact) { + mContact = contact; + } - public MyOnLongClickListener(Contact contact) { - mContact = contact; - } + @Override + public void onClick(View v) { + Intent i = new Intent(ContactList.this, SendIM.class); + i.setData(mContact.toUri()); + startActivity(i); + } + + } + + class MyOnLongClickListener implements OnLongClickListener { + + private final Contact mContact; - @Override - public boolean onLongClick(View v) { - createDialog(mContact); - return true; - } - } + public MyOnLongClickListener(Contact contact) { + mContact = contact; + } - private final List observers; + @Override + public boolean onLongClick(View v) { + createDialog(mContact); + return true; + } + } - public MyExpandableListAdapter() { - observers = new ArrayList(); - } + private final List observers; + + public MyExpandableListAdapter() { + observers = new ArrayList(); + } + - @Override - public boolean areAllItemsEnabled() { - return true; - } + @Override + public boolean areAllItemsEnabled() { + return true; + } - private void bindView(View view, Contact curContact) { + private void bindView(View view, Contact curContact) { - if (curContact != null) { - ImageView imgV = (ImageView) view.findViewById(R.id.contactliststatus); - Drawable imageDrawable = null; - switch (curContact.getStatus()) { - case Status.CONTACT_STATUS_AVAILABLE: - imageDrawable = getResources().getDrawable(R.drawable.online); - break; - case Status.CONTACT_STATUS_AVAILABLE_FOR_CHAT: - imageDrawable = getResources().getDrawable(R.drawable.chat); - break; - case Status.CONTACT_STATUS_AWAY: - imageDrawable = getResources().getDrawable(R.drawable.away); - break; - case Status.CONTACT_STATUS_BUSY: - imageDrawable = getResources().getDrawable(R.drawable.dnd); - break; - case Status.CONTACT_STATUS_DISCONNECT: - imageDrawable = getResources().getDrawable(R.drawable.offline); - break; - case Status.CONTACT_STATUS_UNAVAILABLE: - imageDrawable = getResources().getDrawable(R.drawable.requested); - break; - default: - imageDrawable = getResources().getDrawable(R.drawable.error); - break; + if (curContact != null) { + ImageView imgV = (ImageView) view.findViewById(R.id.contactliststatus); + Drawable imageDrawable = null; + switch (curContact.getStatus()) { + case Status.CONTACT_STATUS_AVAILABLE: + imageDrawable = getResources().getDrawable(R.drawable.online); + break; + case Status.CONTACT_STATUS_AVAILABLE_FOR_CHAT: + imageDrawable = getResources().getDrawable(R.drawable.chat); + break; + case Status.CONTACT_STATUS_AWAY: + imageDrawable = getResources().getDrawable(R.drawable.away); + break; + case Status.CONTACT_STATUS_BUSY: + imageDrawable = getResources().getDrawable(R.drawable.dnd); + break; + case Status.CONTACT_STATUS_DISCONNECT: + imageDrawable = getResources().getDrawable(R.drawable.offline); + break; + case Status.CONTACT_STATUS_UNAVAILABLE: + imageDrawable = getResources().getDrawable(R.drawable.requested); + break; + default: + imageDrawable = getResources().getDrawable(R.drawable.error); + break; + } + imgV.setImageDrawable(imageDrawable); + + TextView v = (TextView) view.findViewById(R.id.contactlistpseudo); + if (v != null) { + v.setText(curContact.getName()); + } + + v = (TextView) view.findViewById(R.id.contactlistmsgperso); + if (v != null) { + v.setText(curContact.getMsgState()); + } + + // TODO: Rajouter l'avatar du contact getAvatar() dans la classe + imgV = (ImageView) view.findViewById(R.id.contactlistavatar); + if (imgV != null) { + imageDrawable = getResources().getDrawable(R.drawable.avatar); + imgV.setImageDrawable(imageDrawable); + } + } } - imgV.setImageDrawable(imageDrawable); - TextView v = (TextView) view.findViewById(R.id.contactlistpseudo); - if (v != null) { - v.setText(curContact.getName()); + public void changed() { + for (DataSetObserver obs : observers) { + obs.onChanged(); + } + } + + void createDialog(Contact contact) { + ContactDialog dialogContact = new ContactDialog(ContactList.this, contact); + dialogContact.setOwnerActivity(ContactList.this); + dialogContact.show(); } - v = (TextView) view.findViewById(R.id.contactlistmsgperso); - if (v != null) { - v.setText(curContact.getMsgState()); + @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; + } + } + + @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 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 View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, + ViewGroup parent) { + View v; + if (convertView == null) { + v = LayoutInflater.from(ContactList.this).inflate(R.layout.contactlistcontact, null); + } else { + v = convertView; + } + Contact contact = groupMap.get(groupName.get(groupPosition)).get(childPosition); + bindView(v, contact); + + v.setOnLongClickListener(new MyOnLongClickListener(contact)); + v.setOnClickListener(new MyOnClickListener(contact)); + return v; + } + + @Override + public long getCombinedChildId(long groupId, long childId) { + return 1000 * groupId + childId; + } + + @Override + public long getCombinedGroupId(long groupId) { + return 1000 * groupId; } - // TODO: Rajouter l'avatar du contact getAvatar() dans la classe - imgV = (ImageView) view.findViewById(R.id.contactlistavatar); - if (imgV != null) { - imageDrawable = getResources().getDrawable(R.drawable.avatar); - imgV.setImageDrawable(imageDrawable); + @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; + } - public void changed() { - for (DataSetObserver obs : observers) { - obs.onChanged(); - } - } + @Override + public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { + if (convertView == null) { + convertView = LayoutInflater.from(ContactList.this).inflate(R.layout.contactlistgroup, null); + } + TextView groupTextView = (TextView) convertView.findViewById(R.id.textgroup); + groupTextView.setText(groupName.get(groupPosition)); + return convertView; + } - void createDialog(Contact contact) { - ContactDialog dialogContact = new ContactDialog(ContactList.this, contact); - dialogContact.setOwnerActivity(ContactList.this); - dialogContact.show(); - } + @Override + public boolean hasStableIds() { + return false; + } + + @Override + public boolean isChildSelectable(int groupPosition, int childPosition) { + return true; + } + + @Override + public boolean isEmpty() { + return groupMap.isEmpty(); + } - @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; - } - } + @Override + public void onGroupCollapsed(int groupPosition) { + } + + @Override + public void onGroupExpanded(int groupPosition) { + } - @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 void registerDataSetObserver(DataSetObserver observer) { + observers.add(observer); + } + + @Override + public void unregisterDataSetObserver(DataSetObserver observer) { + observers.remove(observer); + } } - @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 View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, - ViewGroup parent) { - View v; - if (convertView == null) { - v = LayoutInflater.from(ContactList.this).inflate(R.layout.contactlistcontact, null); - } else { - v = convertView; - } - Contact contact = groupMap.get(groupName.get(groupPosition)).get(childPosition); - bindView(v, contact); - - v.setOnLongClickListener(new MyOnLongClickListener(contact)); - v.setOnClickListener(new MyOnClickListener(contact)); - return v; - } - - @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; - } + private class BeemServiceConnection implements ServiceConnection { - @Override - public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { - if (convertView == null) { - convertView = LayoutInflater.from(ContactList.this).inflate(R.layout.contactlistgroup, null); - } - TextView groupTextView = (TextView) convertView.findViewById(R.id.textgroup); - groupTextView.setText(groupName.get(groupPosition)); - return convertView; - } - - @Override - public boolean hasStableIds() { - return false; - } - - @Override - public boolean isChildSelectable(int groupPosition, int childPosition) { - return true; - } - - @Override - public boolean isEmpty() { - return groupMap.isEmpty(); - } - - @Override - public void onGroupCollapsed(int groupPosition) { - } - - @Override - public void onGroupExpanded(int groupPosition) { - } + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + xmppFacade = IXmppFacade.Stub.asInterface(service); + try { + mRoster = xmppFacade.getRoster(); + if (mRoster != null) { + mRoster.addRosterListener(new BeemRosterListener()); + buildContactList(mRoster.getContactList()); + } + } catch (RemoteException e) { + e.printStackTrace(); + } + } - @Override - public void registerDataSetObserver(DataSetObserver observer) { - observers.add(observer); - } - - @Override - public void unregisterDataSetObserver(DataSetObserver observer) { - observers.remove(observer); + @Override + public void onServiceDisconnected(ComponentName name) { + xmppFacade = null; + mRoster = null; + } } - } - - private class BeemServiceConnection implements ServiceConnection { - - @Override - public void onServiceConnected(ComponentName name, IBinder service) { - xmppFacade = IXmppFacade.Stub.asInterface(service); - try { - mRoster = xmppFacade.getRoster(); - if (mRoster != null) { - mRoster.addRosterListener(new BeemRosterListener()); - buildContactList(mRoster.getContactList()); - } - } catch (RemoteException e) { - e.printStackTrace(); - } - } - - @Override - public void onServiceDisconnected(ComponentName name) { - xmppFacade = null; - mRoster = null; - } - } }