src/com/beem/project/beem/service/PrivacyListManagerAdapter.java
changeset 917 f7cfc4a68225
parent 916 d697250668af
child 918 80d2be82fa84
equal deleted inserted replaced
916:d697250668af 917:f7cfc4a68225
     1 /*
       
     2     BEEM is a videoconference application on the Android Platform.
       
     3 
       
     4     Copyright (C) 2009 by Frederic-Charles Barthelery,
       
     5                           Jean-Manuel Da Silva,
       
     6                           Nikita Kozlov,
       
     7                           Philippe Lago,
       
     8                           Jean Baptiste Vergely,
       
     9                           Vincent Veronis.
       
    10 
       
    11     This file is part of BEEM.
       
    12 
       
    13     BEEM is free software: you can redistribute it and/or modify
       
    14     it under the terms of the GNU General Public License as published by
       
    15     the Free Software Foundation, either version 3 of the License, or
       
    16     (at your option) any later version.
       
    17 
       
    18     BEEM is distributed in the hope that it will be useful,
       
    19     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    21     GNU General Public License for more details.
       
    22 
       
    23     You should have received a copy of the GNU General Public License
       
    24     along with BEEM.  If not, see <http://www.gnu.org/licenses/>.
       
    25 
       
    26     Please send bug reports with examples or suggestions to
       
    27     contact@beem-project.com or http://dev.beem-project.com/
       
    28 
       
    29     Epitech, hereby disclaims all copyright interest in the program "Beem"
       
    30     written by Frederic-Charles Barthelery,
       
    31                Jean-Manuel Da Silva,
       
    32                Nikita Kozlov,
       
    33                Philippe Lago,
       
    34                Jean Baptiste Vergely,
       
    35                Vincent Veronis.
       
    36 
       
    37     Nicolas Sadirac, November 26, 2009
       
    38     President of Epitech.
       
    39 
       
    40     Flavien Astraud, November 26, 2009
       
    41     Head of the EIP Laboratory.
       
    42 
       
    43 */
       
    44 package com.beem.project.beem.service;
       
    45 
       
    46 import java.util.ArrayList;
       
    47 import java.util.List;
       
    48 
       
    49 import org.jivesoftware.smack.PrivacyList;
       
    50 import org.jivesoftware.smack.PrivacyListListener;
       
    51 import org.jivesoftware.smack.PrivacyListManager;
       
    52 import org.jivesoftware.smack.XMPPException;
       
    53 import org.jivesoftware.smack.packet.PrivacyItem;
       
    54 import org.jivesoftware.smack.packet.PrivacyItem.PrivacyRule;
       
    55 
       
    56 import android.os.RemoteCallbackList;
       
    57 import android.os.RemoteException;
       
    58 import android.util.Log;
       
    59 
       
    60 import com.beem.project.beem.service.aidl.IPrivacyListListener;
       
    61 import com.beem.project.beem.service.aidl.IPrivacyListManager;
       
    62 
       
    63 /**
       
    64  * An adapter for the Smack's PrivacyListManager.
       
    65  * @author Jean-Manuel Da Silva <dasilvj at beem-project dot com>
       
    66  */
       
    67 public class PrivacyListManagerAdapter extends IPrivacyListManager.Stub {
       
    68 
       
    69     /** Class's Tag. */
       
    70     public static final String TAG = "PrivacyListManagerAdapter";
       
    71 
       
    72     private final PrivacyListManager mPrivacyListManager;
       
    73 
       
    74     private final RemoteCallbackList<IPrivacyListListener> mPrivacyListListeners =
       
    75 	new RemoteCallbackList<IPrivacyListListener>();
       
    76     private final PrivacyListListenerAdapter mPrivacyListListener = new PrivacyListListenerAdapter();
       
    77 
       
    78     /**
       
    79      * Constructor.
       
    80      * @param privacyListManager the privacy list manager
       
    81      */
       
    82     public PrivacyListManagerAdapter(final PrivacyListManager privacyListManager) {
       
    83 	mPrivacyListManager = privacyListManager;
       
    84 	mPrivacyListManager.addListener(mPrivacyListListener);
       
    85     }
       
    86 
       
    87     /* (non-Javadoc)
       
    88      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#blockUser(java.lang.String, java.lang.String)
       
    89      */
       
    90     @Override
       
    91     public void blockUser(String listName, String jid) throws RemoteException {
       
    92     }
       
    93 
       
    94     /* (non-Javadoc)
       
    95      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#createPrivacyList(java.lang.String, java.util.List)
       
    96      */
       
    97     @Override
       
    98     public void createPrivacyList(String listName, List<PrivacyListItem> items) throws RemoteException {
       
    99 	Log.d(TAG, "BEGIN createPrivacyList.");
       
   100 	try {
       
   101 	    List<PrivacyItem> privacyItems = new ArrayList<PrivacyItem>();
       
   102 
       
   103 	    PrivacyItem item = new PrivacyItem(PrivacyItem.Type.subscription.name(), true, 2);
       
   104 	    item.setValue(PrivacyRule.SUBSCRIPTION_BOTH);
       
   105 	    privacyItems.add(item);
       
   106 
       
   107 	    mPrivacyListManager.createPrivacyList(listName, privacyItems);
       
   108 	} catch (XMPPException e) {
       
   109 	    Log.e(TAG, e.getMessage());
       
   110 	}
       
   111 	Log.d(TAG, "END createPrivacyList.");
       
   112     }
       
   113 
       
   114     /* (non-Javadoc)
       
   115      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#declineActivePrivacyList()
       
   116      */
       
   117     @Override
       
   118     public void declineActivePrivacyList() throws RemoteException {
       
   119 	try {
       
   120 	    mPrivacyListManager.declineActiveList();
       
   121 	} catch (XMPPException e) {
       
   122 	    Log.e(TAG, e.getMessage());
       
   123 	}
       
   124     }
       
   125 
       
   126     /* (non-Javadoc)
       
   127      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#declineDefaultPrivacyList()
       
   128      */
       
   129     @Override
       
   130     public void declineDefaultPrivacyList() throws RemoteException {
       
   131 	try {
       
   132 	    mPrivacyListManager.declineDefaultList();
       
   133 	} catch (XMPPException e) {
       
   134 	    Log.e(TAG, e.getMessage());
       
   135 	}
       
   136     }
       
   137 
       
   138     /* (non-Javadoc)
       
   139      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#editPrivacyList(java.lang.String, java.util.List)
       
   140      */
       
   141     @Override
       
   142     public void editPrivacyList(String listName, List<PrivacyListItem> items) throws RemoteException {
       
   143 	Log.d(TAG, "BEGIN editPrivacyList.");
       
   144 	try {
       
   145 	    mPrivacyListManager.updatePrivacyList(listName, tranformPrivacyListItemsToPrivacyItems(items));
       
   146 	} catch (XMPPException e) {
       
   147 	    Log.e(TAG, e.getMessage());
       
   148 	}
       
   149 	Log.d(TAG, "END editPrivacyList.");
       
   150     }
       
   151 
       
   152     /* (non-Javadoc)
       
   153      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#getActivePrivacyList()
       
   154      */
       
   155     @Override
       
   156     public String getActivePrivacyList() throws RemoteException {
       
   157 	try {
       
   158 	    PrivacyList activePrivacyList = mPrivacyListManager.getActiveList();
       
   159 	    return activePrivacyList.toString();
       
   160 	} catch (XMPPException e) {
       
   161 	    Log.e(TAG, e.getMessage());
       
   162 	}
       
   163 	return null;
       
   164     }
       
   165 
       
   166     /* (non-Javadoc)
       
   167      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#getBlockedGroupsByList(java.lang.String)
       
   168      */
       
   169     @Override
       
   170     public List<String> getBlockedGroupsByList(String listName) throws RemoteException {
       
   171 	List<String> blockedGroups = new ArrayList<String>();
       
   172 	try {
       
   173 	    PrivacyList pL = mPrivacyListManager.getPrivacyList(listName);
       
   174 	    for (PrivacyItem pI : pL.getItems()) {
       
   175 		if (pI.getType().equals(PrivacyItem.Type.group) && !pI.isAllow())
       
   176 		    blockedGroups.add(pI.getValue());
       
   177 	    }
       
   178 	} catch (XMPPException e) {
       
   179 	    Log.e(TAG, e.getMessage());
       
   180 	}
       
   181 	return blockedGroups;
       
   182     }
       
   183 
       
   184     /* (non-Javadoc)
       
   185      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#getBlockedUsersByList(java.lang.String)
       
   186      */
       
   187     @Override
       
   188     public List<String> getBlockedUsersByList(String listName) throws RemoteException {
       
   189 	List<String> blockedUsers = new ArrayList<String>();
       
   190 	try {
       
   191 	    PrivacyList pL = mPrivacyListManager.getPrivacyList(listName);
       
   192 	    for (PrivacyItem pI : pL.getItems()) {
       
   193 		if (pI.getType().equals(PrivacyItem.Type.jid) && !pI.isAllow())
       
   194 		    blockedUsers.add(pI.getValue());
       
   195 	    }
       
   196 	} catch (XMPPException e) {
       
   197 	    Log.e(TAG, e.getMessage());
       
   198 	}
       
   199 	return blockedUsers;
       
   200     }
       
   201 
       
   202     /* (non-Javadoc)
       
   203      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#getDefaultPrivacyList()
       
   204      */
       
   205     @Override
       
   206     public String getDefaultPrivacyList() throws RemoteException {
       
   207 	try {
       
   208 	    PrivacyList defaultPrivacyList = mPrivacyListManager.getDefaultList();
       
   209 	    return defaultPrivacyList.toString();
       
   210 	} catch (XMPPException e) {
       
   211 	    Log.e(TAG, e.getMessage());
       
   212 	}
       
   213 	return null;
       
   214     }
       
   215 
       
   216     /* (non-Javadoc)
       
   217      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#removePrivacyList(java.lang.String)
       
   218      */
       
   219     @Override
       
   220     public void removePrivacyList(String listName) throws RemoteException {
       
   221 	try {
       
   222 	    mPrivacyListManager.deletePrivacyList(listName);
       
   223 	} catch (XMPPException e) {
       
   224 	    Log.e(TAG, e.getMessage());
       
   225 	}
       
   226     }
       
   227 
       
   228     /* (non-Javadoc)
       
   229      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#setActivePrivacyList(java.lang.String)
       
   230      */
       
   231     @Override
       
   232     public void setActivePrivacyList(String listName) throws RemoteException {
       
   233 	try {
       
   234 	    mPrivacyListManager.setActiveListName(listName);
       
   235 	} catch (XMPPException e) {
       
   236 	    Log.e(TAG, e.getMessage());
       
   237 	}
       
   238     }
       
   239 
       
   240     /* (non-Javadoc)
       
   241      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#setDefaultPrivacyList(java.lang.String)
       
   242      */
       
   243     @Override
       
   244     public void setDefaultPrivacyList(String listName) throws RemoteException {
       
   245 	try {
       
   246 	    mPrivacyListManager.setDefaultListName(listName);
       
   247 	} catch (XMPPException e) {
       
   248 	    Log.e(TAG, e.getMessage());
       
   249 	}
       
   250     }
       
   251 
       
   252     /**
       
   253      * From a List of PrivacyListItem get a List of PrivacyItem.
       
   254      * @param items The List of PrivacyListItem.
       
   255      * @return A list of PrivacyItem.
       
   256      */
       
   257     private List<PrivacyItem> tranformPrivacyListItemsToPrivacyItems(List<PrivacyListItem> items) {
       
   258 	List<PrivacyItem> rItems = new ArrayList<PrivacyItem>();
       
   259 	PrivacyItem.Type[] itemTypes = PrivacyItem.Type.values();
       
   260 
       
   261 	for (int i = 0; i < items.size(); i++) {
       
   262 	    rItems.add(new PrivacyItem(itemTypes[items.get(i).getType()].name(), false, i));
       
   263 	}
       
   264 
       
   265 	return rItems;
       
   266     }
       
   267 
       
   268     /**
       
   269      * From a List of PrivacyItem get a List of PrivacyListItem.
       
   270      * @param items The List of PrivacyItem.
       
   271      * @return A list of PrivacyListItem.
       
   272      */
       
   273     private List<PrivacyListItem> tranformPrivacyItemsToPrivacyListItems(List<PrivacyItem> items) {
       
   274 	List<PrivacyListItem> rItems = new ArrayList<PrivacyListItem>();
       
   275 
       
   276 	for (int i = 0; i < items.size(); i++) {
       
   277 	    rItems.add(new PrivacyListItem(items.get(i).getType().ordinal(), items.get(i).getValue()));
       
   278 	}
       
   279 	return rItems;
       
   280     }
       
   281 
       
   282     /**
       
   283      * An adapter for the Smack's PrivacyListListener.
       
   284      * @author Jean-Manuel Da Silva <dasilvj at beem-project dot com>
       
   285      */
       
   286     private class PrivacyListListenerAdapter implements PrivacyListListener {
       
   287 	/**
       
   288 	 * Constructor.
       
   289 	 */
       
   290 	public PrivacyListListenerAdapter() { }
       
   291 
       
   292 	@Override
       
   293 	public void setPrivacyList(final String listName, final List<PrivacyItem> listItem) {
       
   294 	    int i = mPrivacyListListeners.beginBroadcast();
       
   295 	    while (i > 0) {
       
   296 		i--;
       
   297 		try {
       
   298 		    mPrivacyListListeners.getBroadcastItem(i).setPrivacyList(listName,
       
   299 			tranformPrivacyItemsToPrivacyListItems(listItem));
       
   300 		} catch (RemoteException e) {
       
   301 		    Log.w(TAG, e.getMessage());
       
   302 		}
       
   303 	    }
       
   304 	    mPrivacyListListeners.finishBroadcast();
       
   305 	}
       
   306 
       
   307 	@Override
       
   308 	public void updatedPrivacyList(final String listName) {
       
   309 	    Log.d(TAG, "BEGIN updatedPrivacyList.");
       
   310 	    int i = mPrivacyListListeners.beginBroadcast();
       
   311 	    while (i > 0) {
       
   312 		i--;
       
   313 		try {
       
   314 		    mPrivacyListListeners.getBroadcastItem(i).updatedPrivacyList(listName);
       
   315 		} catch (RemoteException e) {
       
   316 		    Log.w(TAG, e.getMessage());
       
   317 		}
       
   318 	    }
       
   319 	    mPrivacyListListeners.finishBroadcast();
       
   320 	    Log.d(TAG, "END updatedPrivacyList.");
       
   321 	}
       
   322     }
       
   323 
       
   324     @Override
       
   325     public void addPrivacyListListener(IPrivacyListListener listener) throws RemoteException {
       
   326 	if (listener != null)
       
   327 	    mPrivacyListListeners.register(listener);
       
   328     }
       
   329 
       
   330     @Override
       
   331     public void removePrivacyListListener(IPrivacyListListener listener) throws RemoteException {
       
   332 	if (listener != null)
       
   333 	    mPrivacyListListeners.unregister(listener);
       
   334     }
       
   335 
       
   336     /* (non-Javadoc)
       
   337      * @see com.beem.project.beem.service.aidl.IPrivacyListManager#getPrivacyLists()
       
   338      */
       
   339     @Override
       
   340     public List<String> getPrivacyLists() throws RemoteException {
       
   341 	Log.d(TAG, "BEGIN getPrivacyLists.");
       
   342 	List<String> res = new ArrayList<String>();
       
   343 	try {
       
   344 	    PrivacyList[] registeredPrivacyLists = mPrivacyListManager.getPrivacyLists();
       
   345 	    Log.d(TAG, "> registeredPrivacyLists size: " + registeredPrivacyLists.length);
       
   346 	    if (registeredPrivacyLists.length > 0) {
       
   347 		for (int i = 0; i < registeredPrivacyLists.length; i++) {
       
   348 		    res.add(registeredPrivacyLists[i].toString());
       
   349 		    Log.d(TAG, "> " + res.get(i) + " added.");
       
   350 		}
       
   351 	    }
       
   352 	} catch (XMPPException e) {
       
   353 	    Log.e(TAG, e.getMessage());
       
   354 	}
       
   355 	Log.d(TAG, "END getPrivacyLists.");
       
   356 	return res;
       
   357     }
       
   358 }