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