1
votes

I am trying to block user in xmpp asmack, following is the code for that:

try {
        PrivacyListManager privacyManager = PrivacyListManager
                .getInstanceFor(MyXMPPConnection.getXMPPConnection());
        if (privacyManager == null) {
            return false;
        }
        String Black_List = "blockedList";
        PrivacyList[] plists = privacyManager.getPrivacyLists();
        if (plists.length == 0) {// No blacklisted or is not listed, direct getPrivacyList error
            List<PrivacyItem> items = new ArrayList<PrivacyItem>();
            Log.i("", "addToPrivacyList plists.length==0");
            PrivacyItem newitem = new PrivacyItem("jid", false, 100);
            newitem.setValue(name);
            items.add(newitem);

            privacyManager.updatePrivacyList(Black_List, items);
            privacyManager.setActiveListName(Black_List);
            return true;
        }

        PrivacyList plist = privacyManager.getPrivacyList(Black_List);
        if (plist != null) {
            String ser = "@" + connection.getServiceName();

            List<PrivacyItem> items = plist.getItems();
            for (PrivacyItem item : items) {
                String from = item.getValue().substring(0,
                        item.getValue().indexOf(ser));
                Log.i("",
                        "addToPrivacyList item.getValue=" + item.getValue());
                if (from.equalsIgnoreCase(name)) {
                    items.remove(item);
                    break;
                }
            }

            PrivacyItem newitem = new PrivacyItem("jid", false, 100);
            newitem.setValue(name + "@"
                    + connection.getServiceName());
            items.add(newitem);
            Log.i("", "addToPrivacyList item.getValue=" + newitem.toXML());
            Log.i("", "deleteFromPrivacyList items size=" + items.size());
            privacyManager.updatePrivacyList(Black_List, items);
            privacyManager.setActiveListName(Black_List);

        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }

I am getting privacyManager as null

PrivacyListManager privacyManager = PrivacyListManager
                    .getInstanceFor(MyXMPPConnection.getXMPPConnection());

Connection is good and I am struggling for this issue. None of SO post helped me. I am wondering why privacyManager is getting null. Please help me to resolve issue.Thanks in advance.

1
Smack is open source, why don't you debug the code to determine why it's returning null?Flow

1 Answers

0
votes

I resolved the issue, I dont know why the privacylist classes doesnot load into jvm onload.so I explicitly loaded the class as Class.forName(PrivacyListManager.class.getName());`

 try {
        ProviderManager.getInstance().addIQProvider("query", "jabber:iq:privacy", new PrivacyProvider());
        Class.forName(PrivacyListManager.class.getName());
}