summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTarun Gupta <tarun1995gupta@gmail.com>2015-06-09 10:08:25 (GMT)
committerTarun Gupta <tarun1995gupta@gmail.com>2015-06-14 13:30:22 (GMT)
commit2180f81bd45045021d98c3a2e0649f10680596f9 (patch)
tree872db3fdeb890a232b5a1dc3b823a1e6692b5aa0 /src/com/isode/stroke/avatars/CombinedAvatarProvider.java
parent9518f8b9d6924e95e4ad839f40866560bd7a7878 (diff)
downloadstroke-2180f81bd45045021d98c3a2e0649f10680596f9.zip
stroke-2180f81bd45045021d98c3a2e0649f10680596f9.tar.bz2
Add Avatar Functionality.
Adds AvatarManager, AvatarManagerImpl, AvatarStorage and different AvatarProviders including DummyAvatarProvider, NullAvatarProvider and OfflineAvatarProvider. Also adds VCardAvatarManager and VCardUpdateAvatarManager. Updates VCard Element, so that it does not return null on calling getPhoto() and thereby produce Null Pointer Exceptions. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Tests are added for AvatarManagerImpl, VCardAvatarManager and VCardUpdateAvatarManager, which passes. Tests for CombinedAvatarProvider will be added soon. Change-Id: Ia6c0f82ae496427dc0cd11841487f6c53fd0fe1c
Diffstat (limited to 'src/com/isode/stroke/avatars/CombinedAvatarProvider.java')
-rwxr-xr-xsrc/com/isode/stroke/avatars/CombinedAvatarProvider.java75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/com/isode/stroke/avatars/CombinedAvatarProvider.java b/src/com/isode/stroke/avatars/CombinedAvatarProvider.java
new file mode 100755
index 0000000..0426bd6
--- /dev/null
+++ b/src/com/isode/stroke/avatars/CombinedAvatarProvider.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.avatars;
+
+import com.isode.stroke.avatars.AvatarProvider;
+import com.isode.stroke.signals.SignalConnection;
+import com.isode.stroke.signals.Slot1;
+import com.isode.stroke.jid.JID;
+import java.util.logging.Logger;
+import java.util.*;
+
+public class CombinedAvatarProvider implements AvatarProvider {
+
+ private Vector<AvatarProvider> providers = new Vector<AvatarProvider>();
+ private Map<JID, String> avatars = new HashMap<JID, String>();
+ private SignalConnection onAvatarChangedConnection_;
+ private Logger logger_ = Logger.getLogger(this.getClass().getName());
+
+ public String getAvatarHash(JID jid) {
+ return getCombinedAvatarAndCache(jid);
+ }
+
+ public void addProvider(AvatarProvider provider) {
+ onAvatarChangedConnection_ = provider.onAvatarChanged.connect(new Slot1<JID>() {
+
+ public void call(JID p1) {
+ handleAvatarChanged(p1);
+ }
+ });
+ providers.add(provider);
+ }
+
+ public void removeProvider(AvatarProvider provider) {
+ while(providers.contains(provider)) {
+ providers.remove(provider);
+ onAvatarChangedConnection_.disconnect();
+ }
+ }
+
+ private void handleAvatarChanged(JID jid) {
+ String oldHash = new String();
+ if(avatars.containsKey(jid)) {
+ oldHash = avatars.get(jid);
+ }
+ String newHash = getCombinedAvatarAndCache(jid);
+ if (newHash != null && !newHash.equals(oldHash)) {
+ logger_.fine("Avatar changed: " + jid + ": " + oldHash + " -> " + ((newHash != null) ? newHash : "NULL") + "\n");
+ onAvatarChanged.emit(jid);
+ }
+ }
+
+ private String getCombinedAvatarAndCache(JID jid) {
+ logger_.fine("JID: " + jid + "\n");
+ String hash = null;
+ for (int i = 0; i < providers.size() && (hash==null); ++i) {
+ hash = providers.get(i).getAvatarHash(jid);
+ logger_.fine("Provider " + providers.get(i) + ": " + ((hash != null) ? hash : "NULL") + "\n");
+ }
+ if (hash != null) {
+ avatars.put(jid, hash);
+ } else {
+ avatars.put(jid, "");
+ }
+ return hash;
+ }
+} \ No newline at end of file