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