(a)Smack Update/Remove PrivacyItem From List

PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(GlobalVar.mConnection);

List pList = privacyManager.getPrivacyList(“BlockedUsers”).getItems();

PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid, name+"@"+GlobalVar.mConnection.getServiceName(), false, 1);

item.setFilterMessage(true);

item.setFilterPresenceIn(false);

item.setFilterPresenceOut(true);

item.setFilterIQ(false);

pList.add(item);

privacyManager.updatePrivacyList(“BlockedUsers”, pList);

This works perfectly for adding a new user to the “BlockedUsers” list.

The issue is removing them.

int count =-1;

for(PrivacyItem e:pList){

count = count + 1;

//Looping through list removing item that has the same value as the user i want to unblock

if (e.getValue().equalsIgnoreCase(name+"@"+GlobalVar.mConnection.getServiceName()) ){

pList.remove(count);

//Update list with removed user

privacyManager.updatePrivacyList(“BlockedUsers”, pList);

}

}

I noticed that when I used spark to remove the same person it would just null-ify them.

tried to do something similar using e = null. Didn’t work.

Also tried to just change the same item doing the following.

e.setFilter(false);

seemed to add a new user to the list instead of updating the preexisting user.

@Flow i require your expertise all mighty one! (You have helped me at least 30 times by answering other questions. I can not for the life of me find something similar to this in my google searches)

(Using asmack 4.0.4 snapshot)

“e = null” does only nullify the local variable ‘e’. You need to remove the PrivacyItem from the List and then send the list to the server.

So you basic approach seems right, although when iterating over an Iterable and modifying it (e.g. removing elements), then you should use an iterator. You also didn’t explain what’s not working when you do so.

Okay I’m going to try that next and will edit this with how it turns out. The reason the first one didn’t work is unknown to me. It was removed and I did update the list, however when I use spark to check the list the item wasn’t removed. I didn’t use an iterator so I’ll try that next.

Simple oopsie. thanks for the help flow it worked great when using the Iterator.

My Code in case some one else comes along

List pList;

PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(GlobalVar.mConnection);

pList = privacyManager.getPrivacyList(“BlockedUsers”).getItems();

/*

//Adding a new entry here

PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid, “test@”+GlobalVar.mConnection.getServiceName(), false, 1);

item.setFilterMessage(true);

item.setFilterPresenceIn(false);

item.setFilterPresenceOut(true);

item.setFilterIQ(false);

pList.add(item);

privacyManager.updatePrivacyList(“BlockedUsers”, pList);*/

//Remove Item from list

Iterator it = pList.iterator();

while(it.hasNext())

{

//Only send update if the list has been changed while looping

//Optional to add break. If you don’t it will loop through entire list and remove all entries. If you know entries cannot be duplicated you can remove comments

boolean Changed = false;

Log.i(“SChat”,"it Value = " + it.next().getValue());

if(it.next().getValue().equalsIgnoreCase(“test@”+GlobalVar.mConnection.getServi ceName())){

it.remove();

Changed = true;

//privacyManager.updatePrivacyList(“BlockedUsers”, pList);

//break;

}

if(Changed){

privacyManager.updatePrivacyList(“BlockedUsers”, pList);

}

}