[guardian-dev] [PATCH 3/3] XMPP: Refactor contact list loading.

Adrian-Ken Rueegsegger ken at codelabs.ch
Sat Nov 20 17:23:50 EST 2010


Factor out reading of contacts into container to private
fillContacts() function.

Signed-off-by: Adrian-Ken Rueegsegger <ken at codelabs.ch>
---
 .../android/im/plugin/xmpp/XmppConnection.java     |   59 ++++++++++----------
 1 files changed, 30 insertions(+), 29 deletions(-)

diff --git a/src/org/gitian/android/im/plugin/xmpp/XmppConnection.java b/src/org/gitian/android/im/plugin/xmpp/XmppConnection.java
index 1e8567d..a248d0d 100755
--- a/src/org/gitian/android/im/plugin/xmpp/XmppConnection.java
+++ b/src/org/gitian/android/im/plugin/xmpp/XmppConnection.java
@@ -634,7 +634,34 @@ public class XmppConnection extends ImConnection {
 				}
 			});
 		}
-		
+
+		/**
+		 *  Create new list of contacts from roster entries.
+		 *
+		 *  @param entryIter	iterator of roster entries to add to contact list
+		 *  @param skipList		list of contacts which should be omitted; new contacts are added to this list automatically
+		 *  @return				contacts from roster which were not present in skiplist.
+		 */
+		private Collection<Contact> fillContacts(Iterator<RosterEntry> entryIter, Set<String> skipList) {
+			Collection<Contact> contacts = new ArrayList<Contact>();
+			for (; entryIter.hasNext();) {
+				RosterEntry entry = entryIter.next();
+				String address = parseAddressBase(entry.getUser());
+
+				/* Skip entries present in the skip list */
+				if (skipList != null && !skipList.add(address))
+					continue;
+
+				String name = entry.getName();
+				if (name == null)
+					name = address;
+				XmppAddress xaddress = new XmppAddress(name, address);
+				Contact contact = new Contact(xaddress, name);
+				contacts.add(contact);
+			}
+			return contacts;
+		}
+
 		private void do_loadContactLists() {
 			Log.d(TAG, "load contact lists");
 			Roster roster = mConnection.getRoster();
@@ -643,23 +670,7 @@ public class XmppConnection extends ImConnection {
 			Set<String> seen = new HashSet<String>();
 			for (Iterator<RosterGroup> giter = roster.getGroups().iterator(); giter.hasNext();) {
 				RosterGroup group = giter.next();
-				Collection<Contact> contacts = new ArrayList<Contact>();
-				for (Iterator<RosterEntry> iter = group.getEntries().iterator(); iter.hasNext();) {
-					RosterEntry entry = iter.next();
-					String address = parseAddressBase(entry.getUser());
-					if (seen.add(address)) {
-						
-						String name = entry.getName();
-						if (name == null)
-							name = address;
-						XmppAddress xaddress = new XmppAddress(name, address);
-						Contact contact = new Contact(xaddress, name);
-						contacts.add(contact);
-					}
-					else {
-						Log.d(TAG, "skipped duplicate contact");
-					}
-				}
+				Collection<Contact> contacts = fillContacts(group.getEntries().iterator(), seen);
 				ContactList cl = new ContactList(mUser.getAddress(), group.getName(), true, contacts, this);
 				mContactLists.add(cl);
 				if (mDefaultContactList == null)
@@ -669,17 +680,7 @@ public class XmppConnection extends ImConnection {
 			}
 			if (roster.getUnfiledEntryCount() > 0) {
 				roster.createGroup("General");
-				Collection<Contact> contacts = new ArrayList<Contact>();
-				for (Iterator<RosterEntry> iter = roster.getUnfiledEntries().iterator(); iter.hasNext();) {
-					RosterEntry entry = iter.next();
-					String address = parseAddressBase(entry.getUser());
-					String name = entry.getName();
-					if (name == null)
-						name = address;
-					XmppAddress xaddress = new XmppAddress(name, address);
-					Contact contact = new Contact(xaddress, name);
-					contacts.add(contact);
-				}
+				Collection<Contact> contacts = fillContacts(roster.getUnfiledEntries().iterator(), null);
 				ContactList cl = new ContactList(mUser.getAddress(), "General" , true, contacts, this);
 				mDefaultContactList = cl;
 				notifyContactListLoaded(cl);
-- 
1.6.5



More information about the Guardian-dev mailing list