1package eu.siacs.conversations.services;
2
3import android.graphics.Bitmap;
4import android.graphics.Canvas;
5import android.graphics.Paint;
6import android.graphics.PorterDuff;
7import android.graphics.PorterDuffXfermode;
8import android.graphics.Rect;
9import android.graphics.Typeface;
10import android.net.Uri;
11import android.util.DisplayMetrics;
12import android.util.Log;
13import android.util.LruCache;
14
15import java.util.ArrayList;
16import java.util.HashMap;
17import java.util.HashSet;
18import java.util.List;
19import java.util.Locale;
20import java.util.Set;
21
22import eu.siacs.conversations.Config;
23import eu.siacs.conversations.entities.Account;
24import eu.siacs.conversations.entities.Bookmark;
25import eu.siacs.conversations.entities.Contact;
26import eu.siacs.conversations.entities.Conversation;
27import eu.siacs.conversations.entities.ListItem;
28import eu.siacs.conversations.entities.Message;
29import eu.siacs.conversations.entities.MucOptions;
30import eu.siacs.conversations.utils.UIHelper;
31import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
32import eu.siacs.conversations.xmpp.XmppConnection;
33import eu.siacs.conversations.xmpp.jid.Jid;
34
35public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
36
37 private static final int FG_COLOR = 0xFFFAFAFA;
38 private static final int TRANSPARENT = 0x00000000;
39 private static final int PLACEHOLDER_COLOR = 0xFF202020;
40
41 private static final String PREFIX_CONTACT = "contact";
42 private static final String PREFIX_CONVERSATION = "conversation";
43 private static final String PREFIX_ACCOUNT = "account";
44 private static final String PREFIX_GENERIC = "generic";
45
46 final private ArrayList<Integer> sizes = new ArrayList<>();
47 final private HashMap<String,Set<String>> conversationDependentKeys = new HashMap<>();
48
49 protected XmppConnectionService mXmppConnectionService = null;
50
51 public AvatarService(XmppConnectionService service) {
52 this.mXmppConnectionService = service;
53 }
54
55 private Bitmap get(final Contact contact, final int size, boolean cachedOnly) {
56 if (contact.isSelf()) {
57 return get(contact.getAccount(),size,cachedOnly);
58 }
59 final String KEY = key(contact, size);
60 Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
61 if (avatar != null || cachedOnly) {
62 return avatar;
63 }
64 if (contact.getProfilePhoto() != null) {
65 avatar = mXmppConnectionService.getFileBackend().cropCenterSquare(Uri.parse(contact.getProfilePhoto()), size);
66 }
67 if (avatar == null && contact.getAvatar() != null) {
68 avatar = mXmppConnectionService.getFileBackend().getAvatar(contact.getAvatar(), size);
69 }
70 if (avatar == null) {
71 avatar = get(contact.getDisplayName(), contact.getJid().toBareJid().toString(), size, cachedOnly);
72 }
73 this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
74 return avatar;
75 }
76
77 public Bitmap getRoundedShortcut(final Contact contact) {
78 DisplayMetrics metrics = mXmppConnectionService.getResources().getDisplayMetrics();
79 int size = Math.round(metrics.density * 48);
80 Bitmap bitmap = get(contact,size);
81 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
82 Canvas canvas = new Canvas(output);
83
84 final Paint paint = new Paint();
85 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
86
87 paint.setAntiAlias(true);
88 canvas.drawARGB(0, 0, 0, 0);
89 canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
90 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
91 canvas.drawBitmap(bitmap, rect, rect, paint);
92 return output;
93 }
94
95 public Bitmap get(final MucOptions.User user, final int size, boolean cachedOnly) {
96 Contact c = user.getContact();
97 if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null || user.getAvatar() == null)) {
98 return get(c, size, cachedOnly);
99 } else {
100 return getImpl(user, size, cachedOnly);
101 }
102 }
103
104 private Bitmap getImpl(final MucOptions.User user, final int size, boolean cachedOnly) {
105 final String KEY = key(user, size);
106 Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
107 if (avatar != null || cachedOnly) {
108 return avatar;
109 }
110 if (user.getAvatar() != null) {
111 avatar = mXmppConnectionService.getFileBackend().getAvatar(user.getAvatar(), size);
112 }
113 if (avatar == null) {
114 Contact contact = user.getContact();
115 if (contact != null) {
116 avatar = get(contact, size, cachedOnly);
117 } else {
118 String seed = user.getRealJid() != null ? user.getRealJid().toBareJid().toString() : null;
119 avatar = get(user.getName(), seed, size, cachedOnly);
120 }
121 }
122 this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
123 return avatar;
124 }
125
126 public void clear(Contact contact) {
127 synchronized (this.sizes) {
128 for (Integer size : sizes) {
129 this.mXmppConnectionService.getBitmapCache().remove(
130 key(contact, size));
131 }
132 }
133 for(Conversation conversation : mXmppConnectionService.findAllConferencesWith(contact)) {
134 clear(conversation);
135 }
136 }
137
138 private String key(Contact contact, int size) {
139 synchronized (this.sizes) {
140 if (!this.sizes.contains(size)) {
141 this.sizes.add(size);
142 }
143 }
144 return PREFIX_CONTACT + "_" + contact.getAccount().getJid().toBareJid() + "_"
145 + contact.getJid() + "_" + String.valueOf(size);
146 }
147
148 private String key(MucOptions.User user, int size) {
149 synchronized (this.sizes) {
150 if (!this.sizes.contains(size)) {
151 this.sizes.add(size);
152 }
153 }
154 return PREFIX_CONTACT + "_" + user.getAccount().getJid().toBareJid() + "_"
155 + user.getFullJid() + "_" + String.valueOf(size);
156 }
157
158 public Bitmap get(ListItem item, int size) {
159 return get(item,size,false);
160 }
161
162 public Bitmap get(ListItem item, int size, boolean cachedOnly) {
163 if (item instanceof Contact) {
164 return get((Contact) item, size,cachedOnly);
165 } else if (item instanceof Bookmark) {
166 Bookmark bookmark = (Bookmark) item;
167 if (bookmark.getConversation() != null) {
168 return get(bookmark.getConversation(), size, cachedOnly);
169 } else {
170 String seed = bookmark.getJid() != null ? bookmark.getJid().toBareJid().toString() : null;
171 return get(bookmark.getDisplayName(), seed, size, cachedOnly);
172 }
173 } else {
174 String seed = item.getJid() != null ? item.getJid().toBareJid().toString() : null;
175 return get(item.getDisplayName(), seed, size, cachedOnly);
176 }
177 }
178
179 public Bitmap get(Conversation conversation, int size) {
180 return get(conversation,size,false);
181 }
182
183 public Bitmap get(Conversation conversation, int size, boolean cachedOnly) {
184 if (conversation.getMode() == Conversation.MODE_SINGLE) {
185 return get(conversation.getContact(), size, cachedOnly);
186 } else {
187 return get(conversation.getMucOptions(), size, cachedOnly);
188 }
189 }
190
191 public void clear(Conversation conversation) {
192 if (conversation.getMode() == Conversation.MODE_SINGLE) {
193 clear(conversation.getContact());
194 } else {
195 clear(conversation.getMucOptions());
196 synchronized (this.conversationDependentKeys) {
197 Set<String> keys = this.conversationDependentKeys.get(conversation.getUuid());
198 if (keys == null) {
199 return;
200 }
201 LruCache<String, Bitmap> cache = this.mXmppConnectionService.getBitmapCache();
202 for(String key : keys) {
203 cache.remove(key);
204 }
205 keys.clear();
206 }
207 }
208 }
209
210 private Bitmap get(MucOptions mucOptions, int size, boolean cachedOnly) {
211 final String KEY = key(mucOptions, size);
212 Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
213 if (bitmap != null || cachedOnly) {
214 return bitmap;
215 }
216 final List<MucOptions.User> users = mucOptions.getUsers(5);
217 if (users.size() == 0) {
218 Conversation c = mucOptions.getConversation();
219 bitmap = getImpl(c.getName(),c.getJid().toBareJid().toString(),size);
220 } else {
221 bitmap = getImpl(users,size);
222 }
223 this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
224 return bitmap;
225 }
226
227 private Bitmap get(List<MucOptions.User> users, int size, boolean cachedOnly) {
228 final String KEY = key(users, size);
229 Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
230 if (bitmap != null || cachedOnly) {
231 return bitmap;
232 }
233 bitmap = getImpl(users, size);
234 this.mXmppConnectionService.getBitmapCache().put(KEY,bitmap);
235 return bitmap;
236 }
237
238 private Bitmap getImpl(List<MucOptions.User> users, int size) {
239 int count = users.size();
240 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
241 Canvas canvas = new Canvas(bitmap);
242 bitmap.eraseColor(TRANSPARENT);
243 if (count == 0) {
244 throw new AssertionError("Unable to draw tiles for 0 users");
245 } else if (count == 1) {
246 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
247 drawTile(canvas, users.get(0).getAccount(), size / 2 + 1, 0, size, size);
248 } else if (count == 2) {
249 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
250 drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size);
251 } else if (count == 3) {
252 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
253 drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1);
254 drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size,
255 size);
256 } else if (count == 4) {
257 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
258 drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
259 drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
260 drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size,
261 size);
262 } else {
263 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
264 drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
265 drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
266 drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1,
267 size, size);
268 }
269 return bitmap;
270 }
271
272 public void clear(MucOptions options) {
273 synchronized (this.sizes) {
274 for (Integer size : sizes) {
275 this.mXmppConnectionService.getBitmapCache().remove(key(options, size));
276 }
277 }
278 }
279
280 private String key(MucOptions options, int size) {
281 synchronized (this.sizes) {
282 if (!this.sizes.contains(size)) {
283 this.sizes.add(size);
284 }
285 }
286 return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
287 + "_" + String.valueOf(size);
288 }
289
290 private String key(List<MucOptions.User> users, int size) {
291 final Conversation conversation = users.get(0).getConversation();
292 StringBuilder builder = new StringBuilder("TILE_");
293 builder.append(conversation.getUuid());
294
295 for(MucOptions.User user : users) {
296 builder.append("\0");
297 builder.append(user.getRealJid() == null ? "" : user.getRealJid().toBareJid().toPreppedString());
298 builder.append("\0");
299 builder.append(user.getFullJid() == null ? "" : user.getFullJid().toPreppedString());
300 }
301 final String key = builder.toString();
302 synchronized (this.conversationDependentKeys) {
303 Set<String> keys;
304 if (this.conversationDependentKeys.containsKey(conversation.getUuid())) {
305 keys = this.conversationDependentKeys.get(conversation.getUuid());
306 } else {
307 keys = new HashSet<>();
308 this.conversationDependentKeys.put(conversation.getUuid(),keys);
309 }
310 keys.add(key);
311 }
312 return key;
313 }
314
315 public Bitmap get(Account account, int size) {
316 return get(account, size, false);
317 }
318
319 public Bitmap get(Account account, int size, boolean cachedOnly) {
320 final String KEY = key(account, size);
321 Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
322 if (avatar != null || cachedOnly) {
323 return avatar;
324 }
325 avatar = mXmppConnectionService.getFileBackend().getAvatar(account.getAvatar(), size);
326 if (avatar == null) {
327 avatar = get(account.getJid().toBareJid().toString(), null, size,false);
328 }
329 mXmppConnectionService.getBitmapCache().put(KEY, avatar);
330 return avatar;
331 }
332
333 public Bitmap get(Message message, int size, boolean cachedOnly) {
334 final Conversation conversation = message.getConversation();
335 if (message.getType() == Message.TYPE_STATUS && message.getCounterparts() != null && message.getCounterparts().size() > 1) {
336 return get(message.getCounterparts(),size,cachedOnly);
337 } else if (message.getStatus() == Message.STATUS_RECEIVED) {
338 Contact c = message.getContact();
339 if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null)) {
340 return get(c, size, cachedOnly);
341 } else if (message.getConversation().getMode() == Conversation.MODE_MULTI){
342 MucOptions.User user = conversation.getMucOptions().findUserByFullJid(message.getCounterpart());
343 if (user != null) {
344 return getImpl(user,size,cachedOnly);
345 }
346 } else if (c != null) {
347 return get(c, size, cachedOnly);
348 }
349 Jid tcp = message.getTrueCounterpart();
350 String seed = tcp != null ? tcp.toBareJid().toString() :null;
351 return get(UIHelper.getMessageDisplayName(message), seed, size, cachedOnly);
352 } else {
353 return get(conversation.getAccount(), size, cachedOnly);
354 }
355 }
356
357 public void clear(Account account) {
358 synchronized (this.sizes) {
359 for (Integer size : sizes) {
360 this.mXmppConnectionService.getBitmapCache().remove(key(account, size));
361 }
362 }
363 }
364
365 public void clear(MucOptions.User user) {
366 synchronized (this.sizes) {
367 for (Integer size : sizes) {
368 this.mXmppConnectionService.getBitmapCache().remove(key(user, size));
369 }
370 }
371 }
372
373 private String key(Account account, int size) {
374 synchronized (this.sizes) {
375 if (!this.sizes.contains(size)) {
376 this.sizes.add(size);
377 }
378 }
379 return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
380 + String.valueOf(size);
381 }
382
383 /*public Bitmap get(String name, int size) {
384 return get(name,null, size,false);
385 }*/
386
387 public Bitmap get(final String name, String seed, final int size, boolean cachedOnly) {
388 final String KEY = key(seed == null ? name : seed, size);
389 Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
390 if (bitmap != null || cachedOnly) {
391 return bitmap;
392 }
393 bitmap = getImpl(name, seed, size);
394 mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
395 return bitmap;
396 }
397
398 private Bitmap getImpl(final String name, final String seed, final int size) {
399 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
400 Canvas canvas = new Canvas(bitmap);
401 final String trimmedName = name == null ? "" : name.trim();
402 drawTile(canvas, trimmedName, seed, 0, 0, size, size);
403 return bitmap;
404 }
405
406 private String key(String name, int size) {
407 synchronized (this.sizes) {
408 if (!this.sizes.contains(size)) {
409 this.sizes.add(size);
410 }
411 }
412 return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
413 }
414
415 private boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top, int right, int bottom) {
416 letter = letter.toUpperCase(Locale.getDefault());
417 Paint tilePaint = new Paint(), textPaint = new Paint();
418 tilePaint.setColor(tileColor);
419 textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
420 textPaint.setColor(FG_COLOR);
421 textPaint.setTypeface(Typeface.create("sans-serif-light",
422 Typeface.NORMAL));
423 textPaint.setTextSize((float) ((right - left) * 0.8));
424 Rect rect = new Rect();
425
426 canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
427 textPaint.getTextBounds(letter, 0, 1, rect);
428 float width = textPaint.measureText(letter);
429 canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
430 / 2 + rect.height() / 2, textPaint);
431 return true;
432 }
433
434 private boolean drawTile(Canvas canvas, MucOptions.User user, int left,
435 int top, int right, int bottom) {
436 Contact contact = user.getContact();
437 if (contact != null) {
438 Uri uri = null;
439 if (contact.getProfilePhoto() != null) {
440 uri = Uri.parse(contact.getProfilePhoto());
441 } else if (contact.getAvatar() != null) {
442 uri = mXmppConnectionService.getFileBackend().getAvatarUri(
443 contact.getAvatar());
444 }
445 if (drawTile(canvas, uri, left, top, right, bottom)) {
446 return true;
447 }
448 } else if (user.getAvatar() != null) {
449 Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(user.getAvatar());
450 if (drawTile(canvas, uri, left, top, right, bottom)) {
451 return true;
452 }
453 }
454 if (contact != null) {
455 String seed = contact.getJid().toBareJid().toString();
456 drawTile(canvas, contact.getDisplayName(), seed, left, top, right, bottom);
457 } else {
458 String seed = user.getRealJid() == null ? null : user.getRealJid().toBareJid().toString();
459 drawTile(canvas, user.getName(), seed, left, top, right, bottom);
460 }
461 return true;
462 }
463
464 private boolean drawTile(Canvas canvas, Account account, int left, int top, int right, int bottom) {
465 String avatar = account.getAvatar();
466 if (avatar != null) {
467 Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(avatar);
468 if (uri != null) {
469 if (drawTile(canvas, uri, left, top, right, bottom)) {
470 return true;
471 }
472 }
473 }
474 String name = account.getJid().toBareJid().toString();
475 return drawTile(canvas, name, name, left, top, right, bottom);
476 }
477
478 private boolean drawTile(Canvas canvas, String name, String seed, int left, int top, int right, int bottom) {
479 if (name != null) {
480 final String letter = getFirstLetter(name);
481 final int color = UIHelper.getColorForName(seed == null ? name : seed);
482 drawTile(canvas, letter, color, left, top, right, bottom);
483 return true;
484 }
485 return false;
486 }
487
488 private static String getFirstLetter(String name) {
489 for(Character c : name.toCharArray()) {
490 if (Character.isLetterOrDigit(c)) {
491 return c.toString();
492 }
493 }
494 return "X";
495 }
496
497 private boolean drawTile(Canvas canvas, Uri uri, int left, int top, int right, int bottom) {
498 if (uri != null) {
499 Bitmap bitmap = mXmppConnectionService.getFileBackend()
500 .cropCenter(uri, bottom - top, right - left);
501 if (bitmap != null) {
502 drawTile(canvas, bitmap, left, top, right, bottom);
503 return true;
504 }
505 }
506 return false;
507 }
508
509 private boolean drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop, int dstright, int dstbottom) {
510 Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
511 canvas.drawBitmap(bm, null, dst, null);
512 return true;
513 }
514
515 @Override
516 public void onAdvancedStreamFeaturesAvailable(Account account) {
517 XmppConnection.Features features = account.getXmppConnection().getFeatures();
518 if (features.pep() && !features.pepPersistent()) {
519 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": has pep but is not persistent");
520 if (account.getAvatar() != null) {
521 mXmppConnectionService.republishAvatarIfNeeded(account);
522 }
523 }
524 }
525}