1package eu.siacs.conversations.services;
2
3import android.graphics.Bitmap;
4import android.graphics.Canvas;
5import android.graphics.Paint;
6import android.graphics.Rect;
7import android.graphics.Typeface;
8import android.net.Uri;
9import android.util.Log;
10
11import java.util.ArrayList;
12import java.util.List;
13import java.util.Locale;
14
15import eu.siacs.conversations.Config;
16import eu.siacs.conversations.entities.Account;
17import eu.siacs.conversations.entities.Bookmark;
18import eu.siacs.conversations.entities.Contact;
19import eu.siacs.conversations.entities.Conversation;
20import eu.siacs.conversations.entities.ListItem;
21import eu.siacs.conversations.entities.Message;
22import eu.siacs.conversations.entities.MucOptions;
23import eu.siacs.conversations.utils.UIHelper;
24import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
25import eu.siacs.conversations.xmpp.XmppConnection;
26
27public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
28
29 private static final int FG_COLOR = 0xFFFAFAFA;
30 private static final int TRANSPARENT = 0x00000000;
31 private static final int PLACEHOLDER_COLOR = 0xFF202020;
32
33 private static final String PREFIX_CONTACT = "contact";
34 private static final String PREFIX_CONVERSATION = "conversation";
35 private static final String PREFIX_ACCOUNT = "account";
36 private static final String PREFIX_GENERIC = "generic";
37
38 final private ArrayList<Integer> sizes = new ArrayList<>();
39
40 protected XmppConnectionService mXmppConnectionService = null;
41
42 public AvatarService(XmppConnectionService service) {
43 this.mXmppConnectionService = service;
44 }
45
46 private Bitmap get(final Contact contact, final int size, boolean cachedOnly) {
47 final String KEY = key(contact, size);
48 Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
49 if (avatar != null || cachedOnly) {
50 return avatar;
51 }
52 if (contact.getProfilePhoto() != null) {
53 avatar = mXmppConnectionService.getFileBackend().cropCenterSquare(Uri.parse(contact.getProfilePhoto()), size);
54 }
55 if (avatar == null && contact.getAvatar() != null) {
56 avatar = mXmppConnectionService.getFileBackend().getAvatar(contact.getAvatar(), size);
57 }
58 if (avatar == null) {
59 avatar = get(contact.getDisplayName(), size, cachedOnly);
60 }
61 this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
62 return avatar;
63 }
64
65 public Bitmap get(final MucOptions.User user, final int size, boolean cachedOnly) {
66 Contact c = user.getContact();
67 if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null || user.getAvatar() == null)) {
68 return get(c, size, cachedOnly);
69 } else {
70 return getImpl(user, size, cachedOnly);
71 }
72 }
73
74 private Bitmap getImpl(final MucOptions.User user, final int size, boolean cachedOnly) {
75 final String KEY = key(user, size);
76 Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
77 if (avatar != null || cachedOnly) {
78 return avatar;
79 }
80 if (user.getAvatar() != null) {
81 avatar = mXmppConnectionService.getFileBackend().getAvatar(user.getAvatar(), size);
82 }
83 if (avatar == null) {
84 Contact contact = user.getContact();
85 if (contact != null) {
86 avatar = get(contact, size, cachedOnly);
87 } else {
88 avatar = get(user.getName(), size, cachedOnly);
89 }
90 }
91 this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
92 return avatar;
93 }
94
95 public void clear(Contact contact) {
96 synchronized (this.sizes) {
97 for (Integer size : sizes) {
98 this.mXmppConnectionService.getBitmapCache().remove(
99 key(contact, size));
100 }
101 }
102 }
103
104 private String key(Contact contact, int size) {
105 synchronized (this.sizes) {
106 if (!this.sizes.contains(size)) {
107 this.sizes.add(size);
108 }
109 }
110 return PREFIX_CONTACT + "_" + contact.getAccount().getJid().toBareJid() + "_"
111 + contact.getJid() + "_" + String.valueOf(size);
112 }
113
114 private String key(MucOptions.User user, int size) {
115 synchronized (this.sizes) {
116 if (!this.sizes.contains(size)) {
117 this.sizes.add(size);
118 }
119 }
120 return PREFIX_CONTACT + "_" + user.getAccount().getJid().toBareJid() + "_"
121 + user.getFullJid() + "_" + String.valueOf(size);
122 }
123
124 public Bitmap get(ListItem item, int size) {
125 return get(item,size,false);
126 }
127
128 public Bitmap get(ListItem item, int size, boolean cachedOnly) {
129 if (item instanceof Contact) {
130 return get((Contact) item, size,cachedOnly);
131 } else if (item instanceof Bookmark) {
132 Bookmark bookmark = (Bookmark) item;
133 if (bookmark.getConversation() != null) {
134 return get(bookmark.getConversation(), size, cachedOnly);
135 } else {
136 return get(bookmark.getDisplayName(), size, cachedOnly);
137 }
138 } else {
139 return get(item.getDisplayName(), size, cachedOnly);
140 }
141 }
142
143 public Bitmap get(Conversation conversation, int size) {
144 return get(conversation,size,false);
145 }
146
147 public Bitmap get(Conversation conversation, int size, boolean cachedOnly) {
148 if (conversation.getMode() == Conversation.MODE_SINGLE) {
149 return get(conversation.getContact(), size, cachedOnly);
150 } else {
151 return get(conversation.getMucOptions(), size, cachedOnly);
152 }
153 }
154
155 public void clear(Conversation conversation) {
156 if (conversation.getMode() == Conversation.MODE_SINGLE) {
157 clear(conversation.getContact());
158 } else {
159 clear(conversation.getMucOptions());
160 }
161 }
162
163 private Bitmap get(MucOptions mucOptions, int size, boolean cachedOnly) {
164 final String KEY = key(mucOptions, size);
165 Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
166 if (bitmap != null || cachedOnly) {
167 return bitmap;
168 }
169 final List<MucOptions.User> users = mucOptions.getUsers();
170 int count = users.size();
171 bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
172 Canvas canvas = new Canvas(bitmap);
173 bitmap.eraseColor(TRANSPARENT);
174
175 if (count == 0) {
176 String name = mucOptions.getConversation().getName();
177 drawTile(canvas, name, 0, 0, size, size);
178 } else if (count == 1) {
179 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
180 drawTile(canvas, mucOptions.getConversation().getAccount(), size / 2 + 1, 0, size, size);
181 } else if (count == 2) {
182 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
183 drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size);
184 } else if (count == 3) {
185 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
186 drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1);
187 drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size,
188 size);
189 } else if (count == 4) {
190 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
191 drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
192 drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
193 drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size,
194 size);
195 } else {
196 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
197 drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
198 drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
199 drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1,
200 size, size);
201 }
202 this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
203 return bitmap;
204 }
205
206 public void clear(MucOptions options) {
207 synchronized (this.sizes) {
208 for (Integer size : sizes) {
209 this.mXmppConnectionService.getBitmapCache().remove(key(options, size));
210 }
211 }
212 }
213
214 private String key(MucOptions options, int size) {
215 synchronized (this.sizes) {
216 if (!this.sizes.contains(size)) {
217 this.sizes.add(size);
218 }
219 }
220 return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
221 + "_" + String.valueOf(size);
222 }
223
224 public Bitmap get(Account account, int size) {
225 return get(account, size, false);
226 }
227
228 public Bitmap get(Account account, int size, boolean cachedOnly) {
229 final String KEY = key(account, size);
230 Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
231 if (avatar != null || cachedOnly) {
232 return avatar;
233 }
234 avatar = mXmppConnectionService.getFileBackend().getAvatar(account.getAvatar(), size);
235 if (avatar == null) {
236 avatar = get(account.getJid().toBareJid().toString(), size,false);
237 }
238 mXmppConnectionService.getBitmapCache().put(KEY, avatar);
239 return avatar;
240 }
241
242 public Bitmap get(Message message, int size, boolean cachedOnly) {
243 final Conversation conversation = message.getConversation();
244 if (message.getStatus() == Message.STATUS_RECEIVED) {
245 Contact c = message.getContact();
246 if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null)) {
247 return get(c, size, cachedOnly);
248 } else if (message.getConversation().getMode() == Conversation.MODE_MULTI){
249 MucOptions.User user = conversation.getMucOptions().findUserByFullJid(message.getCounterpart());
250 if (user != null) {
251 return getImpl(user,size,cachedOnly);
252 }
253 }
254 return get(UIHelper.getMessageDisplayName(message), size, cachedOnly);
255 } else {
256 return get(conversation.getAccount(), size, cachedOnly);
257 }
258 }
259
260 public void clear(Account account) {
261 synchronized (this.sizes) {
262 for (Integer size : sizes) {
263 this.mXmppConnectionService.getBitmapCache().remove(key(account, size));
264 }
265 }
266 }
267
268 public void clear(MucOptions.User user) {
269 synchronized (this.sizes) {
270 for (Integer size : sizes) {
271 this.mXmppConnectionService.getBitmapCache().remove(key(user, size));
272 }
273 }
274 }
275
276 private String key(Account account, int size) {
277 synchronized (this.sizes) {
278 if (!this.sizes.contains(size)) {
279 this.sizes.add(size);
280 }
281 }
282 return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
283 + String.valueOf(size);
284 }
285
286 public Bitmap get(String name, int size) {
287 return get(name,size,false);
288 }
289
290 public Bitmap get(final String name, final int size, boolean cachedOnly) {
291 final String KEY = key(name, size);
292 Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
293 if (bitmap != null || cachedOnly) {
294 return bitmap;
295 }
296 bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
297 Canvas canvas = new Canvas(bitmap);
298 final String trimmedName = name.trim();
299 drawTile(canvas, trimmedName, 0, 0, size, size);
300 mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
301 return bitmap;
302 }
303
304 private String key(String name, int size) {
305 synchronized (this.sizes) {
306 if (!this.sizes.contains(size)) {
307 this.sizes.add(size);
308 }
309 }
310 return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
311 }
312
313 private boolean drawTile(Canvas canvas, String letter, int tileColor,
314 int left, int top, int right, int bottom) {
315 letter = letter.toUpperCase(Locale.getDefault());
316 Paint tilePaint = new Paint(), textPaint = new Paint();
317 tilePaint.setColor(tileColor);
318 textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
319 textPaint.setColor(FG_COLOR);
320 textPaint.setTypeface(Typeface.create("sans-serif-light",
321 Typeface.NORMAL));
322 textPaint.setTextSize((float) ((right - left) * 0.8));
323 Rect rect = new Rect();
324
325 canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
326 textPaint.getTextBounds(letter, 0, 1, rect);
327 float width = textPaint.measureText(letter);
328 canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
329 / 2 + rect.height() / 2, textPaint);
330 return true;
331 }
332
333 private boolean drawTile(Canvas canvas, MucOptions.User user, int left,
334 int top, int right, int bottom) {
335 Contact contact = user.getContact();
336 if (contact != null) {
337 Uri uri = null;
338 if (contact.getProfilePhoto() != null) {
339 uri = Uri.parse(contact.getProfilePhoto());
340 } else if (contact.getAvatar() != null) {
341 uri = mXmppConnectionService.getFileBackend().getAvatarUri(
342 contact.getAvatar());
343 }
344 if (drawTile(canvas, uri, left, top, right, bottom)) {
345 return true;
346 }
347 } else if (user.getAvatar() != null) {
348 Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(user.getAvatar());
349 if (drawTile(canvas, uri, left, top, right, bottom)) {
350 return true;
351 }
352 }
353 String name = contact != null ? contact.getDisplayName() : user.getName();
354 drawTile(canvas, name, left, top, right, bottom);
355 return true;
356 }
357
358 private boolean drawTile(Canvas canvas, Account account, int left, int top, int right, int bottom) {
359 String avatar = account.getAvatar();
360 if (avatar != null) {
361 Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(avatar);
362 if (uri != null) {
363 if (drawTile(canvas, uri, left, top, right, bottom)) {
364 return true;
365 }
366 }
367 }
368 return drawTile(canvas, account.getJid().toBareJid().toString(), left, top, right, bottom);
369 }
370
371 private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) {
372 if (name != null) {
373 final String letter = name.isEmpty() ? "X" : name.substring(0, 1);
374 final int color = UIHelper.getColorForName(name);
375 drawTile(canvas, letter, color, left, top, right, bottom);
376 return true;
377 }
378 return false;
379 }
380
381 private boolean drawTile(Canvas canvas, Uri uri, int left, int top, int right, int bottom) {
382 if (uri != null) {
383 Bitmap bitmap = mXmppConnectionService.getFileBackend()
384 .cropCenter(uri, bottom - top, right - left);
385 if (bitmap != null) {
386 drawTile(canvas, bitmap, left, top, right, bottom);
387 return true;
388 }
389 }
390 return false;
391 }
392
393 private boolean drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop, int dstright, int dstbottom) {
394 Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
395 canvas.drawBitmap(bm, null, dst, null);
396 return true;
397 }
398
399 @Override
400 public void onAdvancedStreamFeaturesAvailable(Account account) {
401 XmppConnection.Features features = account.getXmppConnection().getFeatures();
402 if (features.pep() && !features.pepPersistent()) {
403 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": has pep but is not persistent");
404 if (account.getAvatar() != null) {
405 mXmppConnectionService.republishAvatarIfNeeded(account);
406 }
407 }
408 }
409}