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