2 |
2 |
3 import java.util.ArrayList; |
3 import java.util.ArrayList; |
4 import java.util.List; |
4 import java.util.List; |
5 |
5 |
6 import org.jivesoftware.smack.PrivacyList; |
6 import org.jivesoftware.smack.PrivacyList; |
|
7 import org.jivesoftware.smack.PrivacyListListener; |
7 import org.jivesoftware.smack.PrivacyListManager; |
8 import org.jivesoftware.smack.PrivacyListManager; |
8 import org.jivesoftware.smack.XMPPConnection; |
9 import org.jivesoftware.smack.XMPPConnection; |
9 import org.jivesoftware.smack.XMPPException; |
10 import org.jivesoftware.smack.XMPPException; |
10 import org.jivesoftware.smack.packet.PrivacyItem; |
11 import org.jivesoftware.smack.packet.PrivacyItem; |
11 |
12 |
|
13 import android.os.RemoteCallbackList; |
12 import android.os.RemoteException; |
14 import android.os.RemoteException; |
13 import android.util.Log; |
15 import android.util.Log; |
14 |
16 |
|
17 import com.beem.project.beem.service.aidl.IPrivacyListListener; |
15 import com.beem.project.beem.service.aidl.IPrivacyListManager; |
18 import com.beem.project.beem.service.aidl.IPrivacyListManager; |
16 |
19 |
17 /** |
20 /** |
18 * An adapter for the Smack's PrivacyListManager. |
21 * An adapter for the Smack's PrivacyListManager. |
19 * @author Jean-Manuel Da Silva <dasilvj at beem-project dot com> |
22 * @author Jean-Manuel Da Silva <dasilvj at beem-project dot com> |
23 /** |
26 /** |
24 * Class's Tag. |
27 * Class's Tag. |
25 */ |
28 */ |
26 public static final String TAG = "PrivacyListManagerAdapter"; |
29 public static final String TAG = "PrivacyListManagerAdapter"; |
27 |
30 |
|
31 private final XMPPConnection mXmppConnection; |
28 private final PrivacyListManager mPrivacyListManager; |
32 private final PrivacyListManager mPrivacyListManager; |
29 private final XMPPConnection mXmppConnection; |
33 |
|
34 private final RemoteCallbackList<IPrivacyListListener> mPrivacyListListeners = new RemoteCallbackList<IPrivacyListListener>(); |
|
35 private final PrivacyListListenerAdapter mPrivacyListListener = new PrivacyListListenerAdapter(); |
30 |
36 |
31 /** |
37 /** |
32 * Constructor. |
38 * Constructor. |
33 * @param connection The XMPP connection that will be used by the PrivacyListManagerAdapter. |
39 * @param connection The XMPP connection that will be used by the PrivacyListManagerAdapter. |
34 */ |
40 */ |
35 public PrivacyListManagerAdapter(final XMPPConnection connection) { |
41 public PrivacyListManagerAdapter(final XMPPConnection connection) { |
36 mXmppConnection = connection; |
42 mXmppConnection = connection; |
37 mPrivacyListManager = PrivacyListManager.getInstanceFor(mXmppConnection); |
43 mPrivacyListManager = PrivacyListManager.getInstanceFor(mXmppConnection); |
|
44 mPrivacyListManager.addListener(mPrivacyListListener); |
38 } |
45 } |
39 |
46 |
40 @Override |
47 @Override |
41 public void blockUser(String listName, String jid) throws RemoteException { |
48 public void blockUser(String listName, String jid) throws RemoteException { |
42 } |
49 } |
169 rItems.add(new PrivacyItem(itemTypes[items.get(i).getType()].name(), false, i)); |
176 rItems.add(new PrivacyItem(itemTypes[items.get(i).getType()].name(), false, i)); |
170 } |
177 } |
171 |
178 |
172 return rItems; |
179 return rItems; |
173 } |
180 } |
|
181 |
|
182 /** |
|
183 * From a List of PrivacyItem get a List of PrivacyListItem. |
|
184 * @param items The List of PrivacyItem. |
|
185 * @return A list of PrivacyListItem. |
|
186 */ |
|
187 private List<PrivacyListItem> tranformPrivacyItemsToPrivacyListItems(List<PrivacyItem> items) { |
|
188 List<PrivacyListItem> rItems = new ArrayList<PrivacyListItem>(); |
|
189 PrivacyItem.Type[] itemTypes = PrivacyItem.Type.values(); |
|
190 |
|
191 for (int i = 0; i < items.size(); i++) { |
|
192 rItems.add(new PrivacyListItem(items.get(i).getType().ordinal(), items.get(i).getValue())); |
|
193 } |
|
194 return rItems; |
|
195 } |
|
196 |
|
197 /** |
|
198 * An adapter for the Smack's PrivacyListListener. |
|
199 * @author Jean-Manuel Da Silva <dasilvj at beem-project dot com> |
|
200 */ |
|
201 private class PrivacyListListenerAdapter implements PrivacyListListener { |
|
202 |
|
203 @Override |
|
204 public void setPrivacyList(String listName, List<PrivacyItem> listItem) { |
|
205 final int n = mPrivacyListListeners.beginBroadcast(); |
|
206 for (int i = 0; i < n; i++) { |
|
207 IPrivacyListListener listener = mPrivacyListListeners.getBroadcastItem(i); |
|
208 try { |
|
209 listener.setPrivacyList(listName, tranformPrivacyItemsToPrivacyListItems(listItem)); |
|
210 } catch (RemoteException e) { |
|
211 Log.w(TAG, e.getMessage()); |
|
212 } |
|
213 } |
|
214 mPrivacyListListeners.finishBroadcast(); |
|
215 } |
|
216 |
|
217 @Override |
|
218 public void updatedPrivacyList(String listName) { |
|
219 final int n = mPrivacyListListeners.beginBroadcast(); |
|
220 for (int i = 0; i < n; i++) { |
|
221 IPrivacyListListener listener = mPrivacyListListeners.getBroadcastItem(i); |
|
222 try { |
|
223 listener.updatedPrivacyList(listName); |
|
224 } catch (RemoteException e) { |
|
225 Log.w(TAG, e.getMessage()); |
|
226 } |
|
227 } |
|
228 mPrivacyListListeners.finishBroadcast(); |
|
229 } |
|
230 } |
|
231 |
|
232 @Override |
|
233 public void addPrivacyListListener(IPrivacyListListener listener) throws RemoteException { |
|
234 if (listener != null) |
|
235 mPrivacyListListeners.register(listener); |
|
236 } |
|
237 |
|
238 @Override |
|
239 public void removePrivacyListListener(IPrivacyListListener listener) throws RemoteException { |
|
240 if (listener != null) |
|
241 mPrivacyListListeners.unregister(listener); |
|
242 } |
174 } |
243 } |