Implémentation de fonctions pour la classe PrivacyListManagerAdapter.
authorJean-Manuel Da Silva <dasilvj@gmail.com>
Sat, 14 Nov 2009 00:45:45 +0100
changeset 513 1bd07ab7a1ad
parent 512 296f08e23545
child 514 c5d0b38dd35f
child 524 e1a9ba4611dc
Implémentation de fonctions pour la classe PrivacyListManagerAdapter.
src/com/beem/project/beem/service/PrivacyListManagerAdapter.java
--- a/src/com/beem/project/beem/service/PrivacyListManagerAdapter.java	Fri Nov 13 20:33:07 2009 +0100
+++ b/src/com/beem/project/beem/service/PrivacyListManagerAdapter.java	Sat Nov 14 00:45:45 2009 +0100
@@ -3,6 +3,7 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.jivesoftware.smack.PrivacyList;
 import org.jivesoftware.smack.PrivacyListListener;
 import org.jivesoftware.smack.PrivacyListManager;
 import org.jivesoftware.smack.XMPPConnection;
@@ -28,10 +29,10 @@
     /**
      * default privacy list name.
      */
-    public static final String DEFAULT_PRIVACYLIST = "default";
+    public static final String DEFAULT_PRIVACYLIST = "BEEM_DEFAULT_PRIVACY_LIST";
 
     private PrivacyListManager mAdaptee;
-    private List<String> mBlockedUser = new ArrayList<String>();
+    private List<String> mBlockedUsers = new ArrayList<String>();
     private XMPPConnection mConnection;
     private MyPrivacyListListener mPrivacyPacketListener = new MyPrivacyListListener();
 
@@ -46,29 +47,31 @@
     }
 
     /**
-     * return the blocked user list.
+     * Return the blocked user list.
      * @return blocked user list
+     * @throws RemoteException
      */
     public List<String> getBlockedUsers() {
-	return mBlockedUser;
+	return mBlockedUsers;
     }
 
     /**
-     * add an user to the blocked user list.
-     * @param jid blocked user jid
+     * Add a user to the blocked user list.
+     * @param jid blocked user jid TODO: Ne bloque pas uniquement un utilisateur, fonction a découper/revoir
      */
     public synchronized void addBlockedUser(final String jid) {
+	Log.d(TAG, "BEGIN addBlockedUser.");
 	if (mAdaptee == null) {
 	    mAdaptee = PrivacyListManager.getInstanceFor(mConnection);
 	    mAdaptee.addListener(mPrivacyPacketListener);
 	}
 	List<PrivacyItem> pItemList = new ArrayList<PrivacyItem>();
-	PrivacyItem pItem = new PrivacyItem("jid", true, 1);
+	PrivacyItem pItem = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
 	pItem.setFilterMessage(true);
 	pItem.setValue(jid);
 	pItemList.add(pItem);
 
-	pItem = new PrivacyItem("subscription", true, 2);
+	pItem = new PrivacyItem(PrivacyItem.Type.subscription.name(), true, 2);
 	pItem.setValue(PrivacyRule.SUBSCRIPTION_BOTH);
 	pItemList.add(pItem);
 
@@ -76,33 +79,56 @@
 	    if (mAdaptee.getPrivacyList(DEFAULT_PRIVACYLIST) == null) {
 		mAdaptee.createPrivacyList(DEFAULT_PRIVACYLIST, pItemList);
 	    }
-
 	} catch (XMPPException e) {
-	    // TODO Auto-generated catch block
-	    e.printStackTrace();
+	    Log.e(TAG, e.getMessage());
 	} catch (ClassCastException e) {
-	    e.printStackTrace();
+	    Log.e(TAG, e.getMessage());
 	    try {
 		mAdaptee.createPrivacyList(DEFAULT_PRIVACYLIST, pItemList);
 	    } catch (XMPPException e1) {
-		// TODO Auto-generated catch block
-		e1.printStackTrace();
+		Log.e(TAG, e1.getMessage());
 	    }
 	}
 	try {
 	    mAdaptee.setActiveListName(DEFAULT_PRIVACYLIST);
 	    mAdaptee.setDefaultListName(DEFAULT_PRIVACYLIST);
 	} catch (XMPPException e) {
-	    // TODO Auto-generated catch block
-	    e.printStackTrace();
+	    Log.e(TAG, e.getMessage());
 	}
-
-	Log.d(TAG, "addBlockedUser");
+	Log.d(TAG, "END addBlockedUser.");
     }
 
     /**
-     * privacy list listener.
-     * @author nikita
+     * Return a list of users blocked for a specified list.
+     */
+    @Override
+    public List<String> getBlockedUsersByList(String listName) throws RemoteException {
+	List<String> blockedUsersList = new ArrayList<String>();
+	try {
+	    PrivacyList pList = mAdaptee.getPrivacyList(listName);
+	    for (PrivacyItem p : pList.getItems()) {
+		if (p.getType().equals(PrivacyItem.Type.jid) && !p.isAllow()) {
+		    blockedUsersList.add(p.getValue());
+		}
+	    }
+	} catch (XMPPException e) {
+	    Log.e(TAG, e.getMessage());
+	}
+	return blockedUsersList;
+    }
+
+    /**
+     * Return the user's PrivacyListManager.
+     * @return the current PrivacyListManager instance
+     */
+    public PrivacyListManager getManager() {
+	return mAdaptee;
+    }
+
+    /**
+     * A class which allows listening on a PrivacyList
+     * 
+     * TODO: Besoin d'en discuter. Va t-on gérer plusieurs listes ? Une seule ?
      */
     class MyPrivacyListListener implements PrivacyListListener {
 	/**
@@ -113,26 +139,29 @@
 
 	@Override
 	public void setPrivacyList(String listName, List<PrivacyItem> listItem) {
-	    Log.d(TAG, "setPrivacyList");
+	    Log.d(TAG, "BEGIN setPrivacyList.");
+	    try {
+		if (mAdaptee.getPrivacyList(listName) == null)
+		    mAdaptee.createPrivacyList(listName, listItem);
+		else
+		    mAdaptee.updatePrivacyList(listName, listItem);
+	    } catch (XMPPException e) {
+		Log.e(TAG, e.getMessage());
+	    }
+	    Log.d(TAG, "END setPrivacyList.");
 	}
 
 	@Override
 	public void updatedPrivacyList(String listName) {
-	    Log.d(TAG, "updatedPrivacyList");
+	    Log.d(TAG, "BEGIN updatedPrivacyList.");
+	    if (listName.equals(DEFAULT_PRIVACYLIST)) {
+		try {
+		    mBlockedUsers = getBlockedUsersByList(DEFAULT_PRIVACYLIST);
+		} catch (RemoteException e) {
+		    Log.e(TAG, e.getMessage());
+		}
+	    }
+	    Log.d(TAG, "END updatedPrivacyList.");
 	}
     }
-
-    @Override
-    public List<String> getBlockedUsersByList(String listName) throws RemoteException {
-	// TODO Auto-generated method stub
-	return null;
-    }
-
-    /**
-     * adaptee getter.
-     * @return the current PrivacyListManager instance
-     */
-    public PrivacyListManager getManager() {
-	return mAdaptee;
-    }
 }