PrivacyListManagerAdapter implementation.
authorJean-Manuel Da Silva <dasilvj@gmail.com>
Sun, 15 Nov 2009 18:35:52 +0100
changeset 516 da650185bf8b
parent 515 5e4b8b7908f6
child 517 fd6c52c23165
PrivacyListManagerAdapter implementation.
src/com/beem/project/beem/service/PrivacyListItem.java
src/com/beem/project/beem/service/PrivacyListManagerAdapter.java
src/com/beem/project/beem/service/aidl/IPrivacyListManager.aidl
--- a/src/com/beem/project/beem/service/PrivacyListItem.java	Sun Nov 15 02:28:42 2009 +0100
+++ b/src/com/beem/project/beem/service/PrivacyListItem.java	Sun Nov 15 18:35:52 2009 +0100
@@ -3,11 +3,16 @@
 import android.os.Parcel;
 import android.os.Parcelable;
 
+/**
+ * A simplified version of the Smack PrivacyItem class.
+ * @author Jean-Manuel Da Silva <dasilvj at beem-project dot com>
+ */
 public class PrivacyListItem implements Parcelable {
 
-    private int mType;
-    private String mValue;
-
+    /**
+     * Constructor. Needed to implements the Parcelable.Creator interface. Generates instances of PrivacyListItem from a
+     * Parcel.
+     */
     public static final Parcelable.Creator<PrivacyListItem> CREATOR = new Parcelable.Creator<PrivacyListItem>() {
 	public PrivacyListItem createFromParcel(Parcel in) {
 	    return new PrivacyListItem(in);
@@ -18,46 +23,87 @@
 	}
     };
 
+    private int mType;
+    private String mValue;
+
+    /**
+     * Constructor.
+     */
     public PrivacyListItem() {
     }
 
-    public PrivacyListItem(Parcel in) {
+    /**
+     * Constructor. Generates instances of PrivacyListItem from a Parcel.
+     * @param in The Parcel used to initialize object's attributes.
+     */
+    public PrivacyListItem(final Parcel in) {
 	readFromParcel(in);
     }
 
-    public PrivacyListItem(int type, String value) {
+    /**
+     * Constructor.
+     * @param type The type of the item.
+     * @param value The value of the item.
+     */
+    public PrivacyListItem(final int type, final String value) {
 	mType = type;
 	mValue = value;
     }
 
+    /**
+     * {@inheritDoc}.
+     */
     @Override
     public int describeContents() {
 	return 0;
     }
 
+    /**
+     * Initialize object's attributes from a Parcel.
+     * @param in The Parcel used to initialize object's attributes.
+     */
     public void readFromParcel(Parcel in) {
 	mType = in.readInt();
 	mValue = in.readString();
     }
 
+    /**
+     * {@inheritDoc}.
+     */
     @Override
     public void writeToParcel(Parcel dest, int flags) {
 	dest.writeInt(mType);
 	dest.writeString(mValue);
     }
 
+    /**
+     * PrivacyListItem type accessor.
+     * @return The type of the PrivacyListItem.
+     */
     public int getType() {
 	return mType;
     }
 
+    /**
+     * PrivacyListItem value accessor.
+     * @return The value of the PrivacyListItem.
+     */
     public String getValue() {
 	return mValue;
     }
 
+    /**
+     * PrivacyListItem type mutator.
+     * @param type The type of the PrivacyListItem.
+     */
     public void setType(final int type) {
 	mType = type;
     }
 
+    /**
+     * PrivacyListItem value mutator.
+     * @param value The value of the PrivacyListItem.
+     */
     public void setValue(final String value) {
 	mValue = value;
     }
--- a/src/com/beem/project/beem/service/PrivacyListManagerAdapter.java	Sun Nov 15 02:28:42 2009 +0100
+++ b/src/com/beem/project/beem/service/PrivacyListManagerAdapter.java	Sun Nov 15 18:35:52 2009 +0100
@@ -1,11 +1,16 @@
 package com.beem.project.beem.service;
 
+import java.util.ArrayList;
 import java.util.List;
 
+import org.jivesoftware.smack.PrivacyList;
 import org.jivesoftware.smack.PrivacyListManager;
 import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.XMPPException;
+import org.jivesoftware.smack.packet.PrivacyItem;
 
 import android.os.RemoteException;
+import android.util.Log;
 
 import com.beem.project.beem.service.aidl.IPrivacyListManager;
 
@@ -20,93 +25,150 @@
      */
     public static final String TAG = "PrivacyListManagerAdapter";
 
-    private final PrivacyListManager mAdaptee;
-    private final XMPPConnection mConnection;
+    private final PrivacyListManager mPrivacyListManager;
+    private final XMPPConnection mXmppConnection;
 
     /**
      * Constructor.
-     * @param The XMPP connection that will be used by the PrivacyListManagerAdapter.
+     * @param connection The XMPP connection that will be used by the PrivacyListManagerAdapter.
      */
     public PrivacyListManagerAdapter(final XMPPConnection connection) {
-	mConnection = connection;
-	mAdaptee = PrivacyListManager.getInstanceFor(mConnection);
-    }
-
-    @Override
-    public void addPrivacyList(String listName) throws RemoteException {
-	// TODO Auto-generated method stub
-
+	mXmppConnection = connection;
+	mPrivacyListManager = PrivacyListManager.getInstanceFor(mXmppConnection);
     }
 
     @Override
     public void blockUser(String listName, String jid) throws RemoteException {
-	// TODO Auto-generated method stub
-
     }
 
     @Override
     public void createPrivacyList(String listName, List<PrivacyListItem> items) throws RemoteException {
-	// TODO Auto-generated method stub
-
+	try {
+	    mPrivacyListManager.createPrivacyList(listName, tranformPrivacyListItemsToPrivacyItems(items));
+	} catch (XMPPException e) {
+	    Log.e(TAG, e.getMessage());
+	}
     }
 
     @Override
     public void declineActivePrivacyList() throws RemoteException {
-	// TODO Auto-generated method stub
-
+	try {
+	    mPrivacyListManager.declineActiveList();
+	} catch (XMPPException e) {
+	    Log.e(TAG, e.getMessage());
+	}
     }
 
     @Override
     public void declineDefaultPrivacyList() throws RemoteException {
-	// TODO Auto-generated method stub
-
+	try {
+	    mPrivacyListManager.declineDefaultList();
+	} catch (XMPPException e) {
+	    Log.e(TAG, e.getMessage());
+	}
     }
 
     @Override
     public void editPrivacyList(String listName, List<PrivacyListItem> items) throws RemoteException {
-	// TODO Auto-generated method stub
-
+	try {
+	    mPrivacyListManager.updatePrivacyList(listName, tranformPrivacyListItemsToPrivacyItems(items));
+	} catch (XMPPException e) {
+	    Log.e(TAG, e.getMessage());
+	}
     }
 
     @Override
     public String getActivePrivacyList() throws RemoteException {
-	// TODO Auto-generated method stub
+	try {
+	    PrivacyList activePrivacyList = mPrivacyListManager.getActiveList();
+	    return activePrivacyList.toString();
+	} catch (XMPPException e) {
+	    Log.e(TAG, e.getMessage());
+	}
 	return null;
     }
 
     @Override
-    public void getBlockedGroupsByList(String listName, List<String> blockedGroupsByList) throws RemoteException {
-	// TODO Auto-generated method stub
-
+    public List<String> getBlockedGroupsByList(String listName) throws RemoteException {
+	List<String> blockedGroups = new ArrayList<String>();
+	try {
+	    PrivacyList pL = mPrivacyListManager.getPrivacyList(listName);
+	    for (PrivacyItem pI : pL.getItems()) {
+		if (pI.getType().equals(PrivacyItem.Type.group) && !pI.isAllow())
+		    blockedGroups.add(pI.getValue());
+	    }
+	} catch (XMPPException e) {
+	    Log.e(TAG, e.getMessage());
+	}
+	return blockedGroups;
     }
 
     @Override
-    public void getBlockedUsersByList(String listName, List<String> blockedUsersByList) throws RemoteException {
-	// TODO Auto-generated method stub
-
+    public List<String> getBlockedUsersByList(String listName) throws RemoteException {
+	List<String> blockedUsers = new ArrayList<String>();
+	try {
+	    PrivacyList pL = mPrivacyListManager.getPrivacyList(listName);
+	    for (PrivacyItem pI : pL.getItems()) {
+		if (pI.getType().equals(PrivacyItem.Type.jid) && !pI.isAllow())
+		    blockedUsers.add(pI.getValue());
+	    }
+	} catch (XMPPException e) {
+	    Log.e(TAG, e.getMessage());
+	}
+	return blockedUsers;
     }
 
     @Override
     public String getDefaultPrivacyList() throws RemoteException {
-	// TODO Auto-generated method stub
+	try {
+	    PrivacyList defaultPrivacyList = mPrivacyListManager.getDefaultList();
+	    return defaultPrivacyList.toString();
+	} catch (XMPPException e) {
+	    Log.e(TAG, e.getMessage());
+	}
 	return null;
     }
 
     @Override
     public void removePrivacyList(String listName) throws RemoteException {
-	// TODO Auto-generated method stub
-
+	try {
+	    mPrivacyListManager.deletePrivacyList(listName);
+	} catch (XMPPException e) {
+	    Log.e(TAG, e.getMessage());
+	}
     }
 
     @Override
     public void setActivePrivacyList(String listName) throws RemoteException {
-	// TODO Auto-generated method stub
-
+	try {
+	    mPrivacyListManager.setActiveListName(listName);
+	} catch (XMPPException e) {
+	    Log.e(TAG, e.getMessage());
+	}
     }
 
     @Override
     public void setDefaultPrivacyList(String listName) throws RemoteException {
-	// TODO Auto-generated method stub
+	try {
+	    mPrivacyListManager.setDefaultListName(listName);
+	} catch (XMPPException e) {
+	    e.getMessage();
+	}
+    }
 
+    /**
+     * From a List of PrivacyListItem get a List of PrivacyItem.
+     * @param items The List of PrivacyListItem.
+     * @return A list of PrivacyItem.
+     */
+    private List<PrivacyItem> tranformPrivacyListItemsToPrivacyItems(List<PrivacyListItem> items) {
+	List<PrivacyItem> rItems = new ArrayList<PrivacyItem>();
+	PrivacyItem.Type[] itemTypes = PrivacyItem.Type.values();
+
+	for (int i = 0; i < items.size(); i++) {
+	    rItems.add(new PrivacyItem(itemTypes[items.get(i).getType()].name(), false, i));
+	}
+
+	return rItems;
     }
 }
--- a/src/com/beem/project/beem/service/aidl/IPrivacyListManager.aidl	Sun Nov 15 02:28:42 2009 +0100
+++ b/src/com/beem/project/beem/service/aidl/IPrivacyListManager.aidl	Sun Nov 15 18:35:52 2009 +0100
@@ -5,8 +5,6 @@
 interface IPrivacyListManager {
 
 	void createPrivacyList(in String listName, in List<PrivacyListItem> items);
-
-	void addPrivacyList(in String listName);
 	
 	void removePrivacyList(in String listName);
 	
@@ -26,8 +24,8 @@
 
 	void blockUser(in String listName, in String jid);
 	
-	void getBlockedUsersByList(in String listName, out List<String> blockedUsersByList);
+	List<String> getBlockedUsersByList(in String listName);
 	
-	void getBlockedGroupsByList(in String listName, out List<String> blockedGroupsByList);
+	List<String> getBlockedGroupsByList(in String listName);
 
 }
\ No newline at end of file