1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
|
package eu.siacs.conversations.services;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.net.Uri;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import de.thedevstack.android.logcat.Logging;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.utils.AvatarUtil;
import de.thedevstack.conversationsplus.utils.ImageUtil;
import de.thedevstack.conversationsplus.utils.UiUpdateHelper;
import de.thedevstack.conversationsplus.utils.XmppSendUtil;
import de.thedevstack.conversationsplus.xmpp.avatar.AvatarPacketGenerator;
import de.thedevstack.conversationsplus.xmpp.avatar.AvatarPacketParser;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Bookmark;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.generator.IqGenerator;
import eu.siacs.conversations.persistance.DatabaseBackend;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.ui.UiCallback;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.pep.Avatar;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
private static final int FG_COLOR = 0xFFFAFAFA;
private static final int TRANSPARENT = 0x00000000;
private static final int PLACEHOLDER_COLOR = 0xFF202020;
private static final String PREFIX_CONTACT = "contact";
private static final String PREFIX_CONVERSATION = "conversation";
private static final String PREFIX_ACCOUNT = "account";
private static final String PREFIX_GENERIC = "generic";
private static final AvatarService INSTANCE = new AvatarService();
final private ArrayList<Integer> sizes = new ArrayList<>();
private final List<String> mInProgressAvatarFetches = new ArrayList<>();
public static AvatarService getInstance() {
return INSTANCE;
}
private Bitmap get(final Contact contact, final int size, boolean cachedOnly) {
final String KEY = key(contact, size);
Bitmap avatar = ImageUtil.getBitmapFromCache(KEY);
if (avatar != null || cachedOnly) {
return avatar;
}
if (contact.getProfilePhoto() != null) {
avatar = ImageUtil.cropCenterSquare(Uri.parse(contact.getProfilePhoto()), size);
}
if (avatar == null && contact.getAvatar() != null) {
avatar = AvatarUtil.getAvatar(contact.getAvatar(), size);
}
if (avatar == null) {
avatar = get(contact.getDisplayName(), size, cachedOnly);
}
ImageUtil.addBitmapToCache(KEY, avatar);
return avatar;
}
public Bitmap get(final MucOptions.User user, final int size, boolean cachedOnly) {
Contact c = user.getContact();
if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null)) {
return get(c, size, cachedOnly);
} else {
return getImpl(user, size, cachedOnly);
}
}
private Bitmap getImpl(final MucOptions.User user, final int size, boolean cachedOnly) {
final String KEY = key(user, size);
Bitmap avatar = ImageUtil.getBitmapFromCache(KEY);
if (avatar != null || cachedOnly) {
return avatar;
}
if (user.getAvatar() != null) {
avatar = AvatarUtil.getAvatar(user.getAvatar(), size);
}
if (avatar == null) {
Contact contact = user.getContact();
if (contact != null) {
avatar = get(contact, size, cachedOnly);
} else {
avatar = get(user.getName(), size, cachedOnly);
}
}
ImageUtil.addBitmapToCache(KEY, avatar);
return avatar;
}
public void clear(Contact contact) {
synchronized (this.sizes) {
for (Integer size : sizes) {
ImageUtil.removeBitmapFromCache(key(contact, size));
}
}
}
private String key(Contact contact, int size) {
synchronized (this.sizes) {
if (!this.sizes.contains(size)) {
this.sizes.add(size);
}
}
return PREFIX_CONTACT + "_" + contact.getAccount().getJid().toBareJid() + "_"
+ contact.getJid() + "_" + String.valueOf(size);
}
private String key(MucOptions.User user, int size) {
synchronized (this.sizes) {
if (!this.sizes.contains(size)) {
this.sizes.add(size);
}
}
return PREFIX_CONTACT + "_" + user.getAccount().getJid().toBareJid() + "_"
+ user.getFullJid() + "_" + String.valueOf(size);
}
public Bitmap get(ListItem item, int size) {
return get(item,size,false);
}
public Bitmap get(ListItem item, int size, boolean cachedOnly) {
if (item instanceof Contact) {
return get((Contact) item, size,cachedOnly);
} else if (item instanceof Bookmark) {
Bookmark bookmark = (Bookmark) item;
if (bookmark.getConversation() != null) {
return get(bookmark.getConversation(), size, cachedOnly);
} else {
return get(bookmark.getDisplayName(), size, cachedOnly);
}
} else {
return get(item.getDisplayName(), size, cachedOnly);
}
}
public Bitmap get(Conversation conversation, int size) {
return get(conversation,size,false);
}
public Bitmap get(Conversation conversation, int size, boolean cachedOnly) {
if (conversation.getMode() == Conversation.MODE_SINGLE) {
return get(conversation.getContact(), size, cachedOnly);
} else {
return get(conversation.getMucOptions(), size, cachedOnly);
}
}
public void clear(Conversation conversation) {
if (conversation.getMode() == Conversation.MODE_SINGLE) {
clear(conversation.getContact());
} else {
clear(conversation.getMucOptions());
}
}
private Bitmap get(MucOptions mucOptions, int size, boolean cachedOnly) {
final String KEY = key(mucOptions, size);
Bitmap bitmap = ImageUtil.getBitmapFromCache(KEY);
if (bitmap != null || cachedOnly) {
return bitmap;
}
final List<MucOptions.User> users = mucOptions.getUsers();
int count = users.size();
bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
bitmap.eraseColor(TRANSPARENT);
if (count == 0) {
String name = mucOptions.getConversation().getName();
drawTile(canvas, name, 0, 0, size, size);
} else if (count == 1) {
drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
drawTile(canvas, mucOptions.getConversation().getAccount(), size / 2 + 1, 0, size, size);
} else if (count == 2) {
drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size);
} else if (count == 3) {
drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1);
drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size,
size);
} else if (count == 4) {
drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size,
size);
} else {
drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1,
size, size);
}
ImageUtil.addBitmapToCache(KEY, bitmap);
return bitmap;
}
public void clear(MucOptions options) {
synchronized (this.sizes) {
for (Integer size : sizes) {
ImageUtil.removeBitmapFromCache(key(options, size));
}
}
}
private String key(MucOptions options, int size) {
synchronized (this.sizes) {
if (!this.sizes.contains(size)) {
this.sizes.add(size);
}
}
return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
+ "_" + String.valueOf(size);
}
public Bitmap get(Account account, int size) {
return get(account, size, false);
}
public Bitmap get(Account account, int size, boolean cachedOnly) {
final String KEY = key(account, size);
Bitmap avatar = ImageUtil.getBitmapFromCache(KEY);
if (avatar != null || cachedOnly) {
return avatar;
}
avatar = AvatarUtil.getAvatar(account.getAvatar(), size);
if (avatar == null) {
avatar = get(account.getJid().toBareJid().toString(), size,false);
}
ImageUtil.addBitmapToCache(KEY, avatar);
return avatar;
}
public Bitmap get(Message message, int size, boolean cachedOnly) {
final Conversation conversation = message.getConversation();
if (message.getStatus() == Message.STATUS_RECEIVED) {
Contact c = message.getContact();
if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null)) {
return get(c, size, cachedOnly);
} else if (message.getConversation().getMode() == Conversation.MODE_MULTI){
MucOptions.User user = conversation.getMucOptions().findUser(message.getCounterpart().getResourcepart());
if (user != null) {
return getImpl(user,size,cachedOnly);
}
}
return get(UIHelper.getMessageDisplayName(message), size, cachedOnly);
} else {
return get(conversation.getAccount(), size, cachedOnly);
}
}
public void clear(Account account) {
synchronized (this.sizes) {
for (Integer size : sizes) {
ImageUtil.removeBitmapFromCache(key(account, size));
}
}
}
public void clear(MucOptions.User user) {
synchronized (this.sizes) {
for (Integer size : sizes) {
ImageUtil.removeBitmapFromCache(key(user, size));
}
}
}
private String key(Account account, int size) {
synchronized (this.sizes) {
if (!this.sizes.contains(size)) {
this.sizes.add(size);
}
}
return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
+ String.valueOf(size);
}
public Bitmap get(String name, int size) {
return get(name,size,false);
}
public Bitmap get(final String name, final int size, boolean cachedOnly) {
final String KEY = key(name, size);
Bitmap bitmap = ImageUtil.getBitmapFromCache(KEY);
if (bitmap != null || cachedOnly) {
return bitmap;
}
bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawTile(canvas, name, 0, 0, size, size);
ImageUtil.addBitmapToCache(KEY, bitmap);
return bitmap;
}
private String key(String name, int size) {
synchronized (this.sizes) {
if (!this.sizes.contains(size)) {
this.sizes.add(size);
}
}
return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
}
private boolean drawTile(Canvas canvas, String letter, int tileColor,
int left, int top, int right, int bottom) {
letter = letter.toUpperCase(Locale.getDefault());
Paint tilePaint = new Paint(), textPaint = new Paint();
tilePaint.setColor(tileColor);
textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(FG_COLOR);
textPaint.setTypeface(Typeface.create("sans-serif-light",
Typeface.NORMAL));
textPaint.setTextSize((float) ((right - left) * 0.8));
Rect rect = new Rect();
canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
textPaint.getTextBounds(letter, 0, 1, rect);
float width = textPaint.measureText(letter);
canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
/ 2 + rect.height() / 2, textPaint);
return true;
}
private boolean drawTile(Canvas canvas, MucOptions.User user, int left,
int top, int right, int bottom) {
Contact contact = user.getContact();
if (contact != null) {
Uri uri = null;
if (contact.getProfilePhoto() != null) {
uri = Uri.parse(contact.getProfilePhoto());
} else if (contact.getAvatar() != null) {
uri = AvatarUtil.getAvatarUri(contact.getAvatar());
}
if (drawTile(canvas, uri, left, top, right, bottom)) {
return true;
}
} else if (user.getAvatar() != null) {
Uri uri = AvatarUtil.getAvatarUri(user.getAvatar());
if (drawTile(canvas, uri, left, top, right, bottom)) {
return true;
}
}
String name = contact != null ? contact.getDisplayName() : user.getName();
drawTile(canvas, name, left, top, right, bottom);
return true;
}
private boolean drawTile(Canvas canvas, Account account, int left, int top, int right, int bottom) {
String avatar = account.getAvatar();
if (avatar != null) {
Uri uri = AvatarUtil.getAvatarUri(avatar);
if (uri != null) {
if (drawTile(canvas, uri, left, top, right, bottom)) {
return true;
}
}
}
return drawTile(canvas, account.getJid().toBareJid().toString(), left, top, right, bottom);
}
private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) {
if (name != null) {
String trimmedName = name.trim();
final String letter = trimmedName.isEmpty() ? "X" : trimmedName.substring(0, 1);
final int color = UIHelper.getColorForName(name);
drawTile(canvas, letter, color, left, top, right, bottom);
return true;
}
return false;
}
private boolean drawTile(Canvas canvas, Uri uri, int left, int top, int right, int bottom) {
if (uri != null) {
Bitmap bitmap = ImageUtil.cropCenter(uri, bottom - top, right - left);
if (bitmap != null) {
drawTile(canvas, bitmap, left, top, right, bottom);
return true;
}
}
return false;
}
private boolean drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop,
int dstright, int dstbottom) {
Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
canvas.drawBitmap(bm, null, dst, null);
return true;
}
@Override
public void onAdvancedStreamFeaturesAvailable(Account account) {
XmppConnection.Features features = account.getXmppConnection().getFeatures();
if (features.pep() && !features.pepPersistent()) {
Logging.d(Config.LOGTAG,account.getJid().toBareJid()+": has pep but is not persistent");
if (account.getAvatar() != null) {
republishAvatarIfNeeded(account);
}
}
}
public void publishAvatar(final Account account,
final Uri image,
final UiCallback<Avatar> callback) {
final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
final int size = Config.AVATAR_SIZE;
final Avatar avatar = AvatarUtil.getPepAvatar(image, size, format);
if (avatar != null) {
avatar.height = size;
avatar.width = size;
if (format.equals(Bitmap.CompressFormat.WEBP)) {
avatar.type = "image/webp";
} else if (format.equals(Bitmap.CompressFormat.JPEG)) {
avatar.type = "image/jpeg";
} else if (format.equals(Bitmap.CompressFormat.PNG)) {
avatar.type = "image/png";
}
if (!AvatarUtil.save(avatar)) {
callback.error(R.string.error_saving_avatar, avatar);
return;
}
sendAndReceiveIqPackages(avatar, account, callback);
} else {
callback.error(R.string.error_publish_avatar_converting, null);
}
}
public void publishAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
final IqPacket packet = AvatarPacketGenerator.generatePublishAvatarPacket(avatar);
XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket result) {
if (result.getType() == IqPacket.TYPE.RESULT) {
sendAndReceiveIqPackages(avatar, account, callback);
} else {
if (callback != null) {
callback.error(
R.string.error_publish_avatar_server_reject,
avatar);
}
}
}
});
}
private static String generateFetchKey(Account account, final Avatar avatar) {
return account.getJid().toBareJid()+"_"+avatar.owner+"_"+avatar.sha1sum;
}
public void republishAvatarIfNeeded(Account account) {
if (account.getAxolotlService().isPepBroken()) {
Logging.d(Config.LOGTAG,account.getJid().toBareJid()+": skipping republication of avatar because pep is broken");
return;
}
IqPacket packet = AvatarPacketGenerator.generateRetrieveAvatarMetadataPacket(null);
XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() {
private Avatar parseAvatar(IqPacket packet) {
Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub");
if (pubsub != null) {
Element items = pubsub.findChild("items");
if (items != null) {
return Avatar.parseMetadata(items);
}
}
return null;
}
private boolean errorIsItemNotFound(IqPacket packet) {
Element error = packet.findChild("error");
return packet.getType() == IqPacket.TYPE.ERROR
&& error != null
&& error.hasChild("item-not-found");
}
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE.RESULT || errorIsItemNotFound(packet)) {
Avatar serverAvatar = parseAvatar(packet);
if (serverAvatar == null && account.getAvatar() != null) {
Avatar avatar = AvatarUtil.getStoredPepAvatar(account.getAvatar());
if (avatar != null) {
Logging.d(Config.LOGTAG,account.getJid().toBareJid()+": avatar on server was null. republishing");
publishAvatar(account, AvatarUtil.getStoredPepAvatar(account.getAvatar()), null);
} else {
Logging.e(Config.LOGTAG, account.getJid().toBareJid()+": error rereading avatar");
}
}
}
}
});
}
public void fetchAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
final String KEY = generateFetchKey(account, avatar);
synchronized(this.mInProgressAvatarFetches) {
if (this.mInProgressAvatarFetches.contains(KEY)) {
return;
} else {
switch (avatar.origin) {
case PEP:
this.mInProgressAvatarFetches.add(KEY);
fetchAvatarPep(account, avatar, callback);
break;
case VCARD:
this.mInProgressAvatarFetches.add(KEY);
fetchAvatarVcard(account, avatar, callback);
break;
}
}
}
}
private void fetchAvatarPep(final Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
IqPacket packet = AvatarPacketGenerator.generateRetrieveAvatarPacket(avatar);
XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket result) {
synchronized (mInProgressAvatarFetches) {
mInProgressAvatarFetches.remove(generateFetchKey(account, avatar));
}
final String ERROR = account.getJid().toBareJid()
+ ": fetching avatar for " + avatar.owner + " failed ";
if (result.getType() == IqPacket.TYPE.RESULT) {
avatar.image = AvatarPacketParser.parseAvatarData(result);
if (avatar.image != null) {
if (AvatarUtil.save(avatar)) {
if (account.getJid().toBareJid().equals(avatar.owner)) {
if (account.setAvatar(avatar.getFilename())) {
DatabaseBackend.getInstance(ConversationsPlusApplication.getAppContext()).updateAccount(account);
}
AvatarService.this.clear(account);
UiUpdateHelper.updateConversationUi();
UiUpdateHelper.updateAccountUi();
} else {
Contact contact = account.getRoster()
.getContact(avatar.owner);
contact.setAvatar(avatar);
AvatarService.this.clear(contact);
UiUpdateHelper.updateConversationUi();
UiUpdateHelper.updateRosterUi();
}
if (callback != null) {
callback.success(avatar);
}
Logging.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": succesfuly fetched pep avatar for " + avatar.owner);
return;
}
} else {
Logging.d(Config.LOGTAG, ERROR + "(parsing error)");
}
} else {
Element error = result.findChild("error");
if (error == null) {
Logging.d(Config.LOGTAG, ERROR + "(server error)");
} else {
Logging.d(Config.LOGTAG, ERROR + error.toString());
}
}
if (callback != null) {
callback.error(0, null);
}
}
});
}
private void fetchAvatarVcard(final Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
IqPacket packet = IqGenerator.retrieveVcardAvatar(avatar);
XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
synchronized (mInProgressAvatarFetches) {
mInProgressAvatarFetches.remove(generateFetchKey(account, avatar));
}
if (packet.getType() == IqPacket.TYPE.RESULT) {
Element vCard = packet.findChild("vCard", "vcard-temp");
Element photo = vCard != null ? vCard.findChild("PHOTO") : null;
String image = photo != null ? photo.findChildContent("BINVAL") : null;
if (image != null) {
avatar.image = image;
if (AvatarUtil.save(avatar)) {
Logging.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": successfully fetched vCard avatar for " + avatar.owner);
Contact contact = account.getRoster()
.getContact(avatar.owner);
contact.setAvatar(avatar);
AvatarService.this.clear(contact);
UiUpdateHelper.updateConversationUi();
UiUpdateHelper.updateRosterUi();
}
}
}
}
});
}
public void checkForAvatar(Account account, final UiCallback<Avatar> callback) {
IqPacket packet = AvatarPacketGenerator.generateRetrieveAvatarMetadataPacket(null);
XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE.RESULT) {
Element pubsub = packet.findChild("pubsub",
"http://jabber.org/protocol/pubsub");
if (pubsub != null) {
Element items = pubsub.findChild("items");
if (items != null) {
Avatar avatar = Avatar.parseMetadata(items);
if (avatar != null) {
avatar.owner = account.getJid().toBareJid();
if (AvatarUtil.isAvatarCached(avatar)) {
if (account.setAvatar(avatar.getFilename())) {
DatabaseBackend.getInstance(ConversationsPlusApplication.getAppContext()).updateAccount(account);
}
AvatarService.this.clear(account);
callback.success(avatar);
} else {
fetchAvatarPep(account, avatar, callback);
}
return;
}
}
}
}
callback.error(0, null);
}
});
}
public void fetchAvatar(Account account, Avatar avatar) {
fetchAvatar(account, avatar, null);
}
public void clearFetchInProgress(Account account) {
synchronized (this.mInProgressAvatarFetches) {
for(Iterator<String> iterator = this.mInProgressAvatarFetches.iterator(); iterator.hasNext();) {
final String KEY = iterator.next();
if (KEY.startsWith(account.getJid().toBareJid()+"_")) {
iterator.remove();
}
}
}
}
private void sendAndReceiveIqPackages(final Avatar avatar, final Account account, final UiCallback callback) {
final IqPacket packet = AvatarPacketGenerator.generatePublishAvatarPacket(avatar);
XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket result) {
if (result.getType() == IqPacket.TYPE.RESULT) {
final IqPacket packet = AvatarPacketGenerator.generatePublishAvatarMetadataPacket(avatar);
XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account,
IqPacket result) {
if (result.getType() == IqPacket.TYPE.RESULT) {
if (account.setAvatar(avatar.getFilename())) {
AvatarService.getInstance().clear(account);
DatabaseBackend.getInstance(ConversationsPlusApplication.getAppContext()).updateAccount(account);
}
callback.success(avatar);
} else {
callback.error(
R.string.error_publish_avatar_server_reject,
avatar);
}
}
});
} else {
callback.error(
R.string.error_publish_avatar_server_reject,
avatar);
}
}
});
}
}
|