author | Da Risk <darisk972@gmail.com> |
Tue, 26 Oct 2010 00:59:27 +0200 | |
changeset 807 | e4b9ec9f30e6 |
parent 802 | 20308ad77837 |
child 808 | 98d220b7fe9d |
permissions | -rw-r--r-- |
801 | 1 |
/* |
2 |
BEEM is a videoconference application on the Android Platform. |
|
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
3 |
|
801 | 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 |
*/ |
|
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
44 |
package com.beem.project.beem.smack.avatar; |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
45 |
|
802 | 46 |
import com.beem.project.beem.smack.avatar.AvatarMetadataExtension.Info; |
47 |
import com.beem.project.beem.smack.pep.PEPListener; |
|
48 |
import com.beem.project.beem.smack.pep.PepSubManager; |
|
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
49 |
|
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
50 |
import java.io.IOException; |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
51 |
|
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
52 |
import java.util.List; |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
53 |
|
801 | 54 |
import org.jivesoftware.smack.Connection; |
800
1eefc5da624a
Port the avatar manager on smackx.pubsub.* instead of PEPManager.
Da Risk <darisk972@gmail.com>
parents:
799
diff
changeset
|
55 |
import org.jivesoftware.smack.packet.PacketExtension; |
1eefc5da624a
Port the avatar manager on smackx.pubsub.* instead of PEPManager.
Da Risk <darisk972@gmail.com>
parents:
799
diff
changeset
|
56 |
import org.jivesoftware.smackx.pubsub.Item; |
1eefc5da624a
Port the avatar manager on smackx.pubsub.* instead of PEPManager.
Da Risk <darisk972@gmail.com>
parents:
799
diff
changeset
|
57 |
import org.jivesoftware.smackx.pubsub.PayloadItem; |
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
58 |
|
801 | 59 |
/** |
60 |
* This class deals with the avatar data. |
|
61 |
* It can be configured to auto retrieve the avatar and put it in cache. |
|
62 |
* |
|
63 |
*/ |
|
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
64 |
public class AvatarManager { |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
65 |
|
800
1eefc5da624a
Port the avatar manager on smackx.pubsub.* instead of PEPManager.
Da Risk <darisk972@gmail.com>
parents:
799
diff
changeset
|
66 |
private PepSubManager mPep; |
801 | 67 |
private Connection mCon; |
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
68 |
private boolean mAutoDownload; |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
69 |
private AvatarCache mCache; |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
70 |
|
801 | 71 |
/** |
72 |
* Create an AvatarManager. |
|
73 |
* |
|
74 |
* @param con the connection |
|
75 |
* @param cache the cache which will store the avatars |
|
76 |
* @param autoDownload true to enable auto download of avatars |
|
77 |
*/ |
|
78 |
public AvatarManager(final Connection con, final AvatarCache cache, final boolean autoDownload) { |
|
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
79 |
mCon = con; |
800
1eefc5da624a
Port the avatar manager on smackx.pubsub.* instead of PEPManager.
Da Risk <darisk972@gmail.com>
parents:
799
diff
changeset
|
80 |
mPep = new PepSubManager(mCon); |
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
81 |
mAutoDownload = autoDownload; |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
82 |
mCache = cache; |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
83 |
if (mAutoDownload) |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
84 |
mPep.addPEPListener(new Listener()); |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
85 |
} |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
86 |
|
801 | 87 |
/** |
88 |
* Get an avatar from the cache. |
|
89 |
* |
|
90 |
* @param avatarId the id of the avatar |
|
91 |
* @return the avatar or null if it cannot be retrieved from the cache |
|
92 |
*/ |
|
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
93 |
public byte[] getAvatar(String avatarId) { |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
94 |
try { |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
95 |
return mCache.get(avatarId); |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
96 |
} catch (IOException e) { |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
97 |
return null; |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
98 |
} |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
99 |
} |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
100 |
|
801 | 101 |
/** |
102 |
* Select the avatar to download. |
|
103 |
* Subclass should override this method to take control over the selection process. |
|
104 |
* This implementation select the first element. |
|
105 |
* |
|
106 |
* @param available list of the avatar metadata information |
|
107 |
* @return the metadata of the avatar to download |
|
108 |
*/ |
|
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
109 |
protected Info selectAvatar(List<Info> available) { |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
110 |
return available.get(0); |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
111 |
} |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
112 |
|
801 | 113 |
/** |
114 |
* Doawload an avatar. |
|
115 |
* |
|
116 |
* @param from The jid of the user |
|
117 |
* @param info the metadata information of the avatar to download |
|
118 |
*/ |
|
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
119 |
private void downloadAvatar(String from, Info info) { |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
120 |
try { |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
121 |
AvatarRetriever retriever = AvatarRetrieverFactory.getRetriever(mCon, from, info); |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
122 |
byte[] avatar = retriever.getAvatar(); |
801 | 123 |
// TODO check the hash before store |
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
124 |
mCache.put(info.getId(), avatar); |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
125 |
} catch (IOException e) { |
807 | 126 |
System.err.println("Error while downloading avatar"); |
127 |
e.printStackTrace(); |
|
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
128 |
} |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
129 |
} |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
130 |
|
801 | 131 |
/** |
132 |
* A listener to PEPEevent. |
|
133 |
*/ |
|
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
134 |
private class Listener implements PEPListener { |
801 | 135 |
|
136 |
/** |
|
137 |
* Create a listener. |
|
138 |
*/ |
|
139 |
public Listener() { |
|
140 |
} |
|
141 |
||
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
142 |
@Override |
800
1eefc5da624a
Port the avatar manager on smackx.pubsub.* instead of PEPManager.
Da Risk <darisk972@gmail.com>
parents:
799
diff
changeset
|
143 |
public void eventReceived(String from, String node, List<Item> items) { |
801 | 144 |
Item i = items.get(0); |
145 |
if (i instanceof PayloadItem) { |
|
146 |
PayloadItem<PacketExtension> pi = (PayloadItem<PacketExtension>) i; |
|
147 |
PacketExtension ex = pi.getPayload(); |
|
148 |
if (ex instanceof AvatarMetadataExtension) { |
|
149 |
AvatarMetadataExtension ext = (AvatarMetadataExtension) ex; |
|
150 |
Info info = selectAvatar(ext.getInfos()); |
|
151 |
if (!mCache.contains(info.getId())) |
|
152 |
downloadAvatar(from, info); |
|
153 |
} |
|
154 |
} |
|
799
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
155 |
} |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
156 |
} |
b2a796654230
Implementation of XEP-084 which loads avatar from http.
Da Risk <darisk972@gmail.com>
parents:
diff
changeset
|
157 |
} |