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