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(
201 key(options, size));
202 }
203 }
204 }
205
206 private String key(MucOptions options, int size) {
207 synchronized (this.sizes) {
208 if (!this.sizes.contains(size)) {
209 this.sizes.add(size);
210 }
211 }
212 return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
213 + "_" + String.valueOf(size);
214 }
215
216 public Bitmap get(Account account, int size) {
217 return get(account, size, false);
218 }
219
220 public Bitmap get(Account account, int size, boolean cachedOnly) {
221 final String KEY = key(account, size);
222 Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
223 if (avatar != null || cachedOnly) {
224 return avatar;
225 }
226 avatar = mXmppConnectionService.getFileBackend().getAvatar(
227 account.getAvatar(), size);
228 if (avatar == null) {
229 avatar = get(account.getJid().toBareJid().toString(), size,false);
230 }
231 mXmppConnectionService.getBitmapCache().put(KEY, avatar);
232 return avatar;
233 }
234
235 public Bitmap get(Message message, int size, boolean cachedOnly) {
236 final Conversation conversation = message.getConversation();
237 if (message.getStatus() == Message.STATUS_RECEIVED) {
238 Contact c = message.getContact();
239 if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null)) {
240 return get(c, size, cachedOnly);
241 } else if (message.getConversation().getMode() == Conversation.MODE_MULTI){
242 MucOptions.User user = conversation.getMucOptions().findUser(message.getCounterpart().getResourcepart());
243 if (user != null) {
244 return getImpl(user,size,cachedOnly);
245 }
246 }
247 return get(UIHelper.getMessageDisplayName(message), size, cachedOnly);
248 } else {
249 return get(conversation.getAccount(), size, cachedOnly);
250 }
251 }
252
253 public void clear(Account account) {
254 synchronized (this.sizes) {
255 for (Integer size : sizes) {
256 this.mXmppConnectionService.getBitmapCache().remove(
257 key(account, size));
258 }
259 }
260 }
261
262 private String key(Account account, int size) {
263 synchronized (this.sizes) {
264 if (!this.sizes.contains(size)) {
265 this.sizes.add(size);
266 }
267 }
268 return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
269 + String.valueOf(size);
270 }
271
272 public Bitmap get(String name, int size) {
273 return get(name,size,false);
274 }
275
276 public Bitmap get(final String name, final int size, boolean cachedOnly) {
277 final String KEY = key(name, size);
278 Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
279 if (bitmap != null || cachedOnly) {
280 return bitmap;
281 }
282 bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
283 Canvas canvas = new Canvas(bitmap);
284 final String trimmedName = name.trim();
285 drawTile(canvas, trimmedName, 0, 0, size, size);
286 mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
287 return bitmap;
288 }
289
290 private String key(String name, int size) {
291 synchronized (this.sizes) {
292 if (!this.sizes.contains(size)) {
293 this.sizes.add(size);
294 }
295 }
296 return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
297 }
298
299 private boolean drawTile(Canvas canvas, String letter, int tileColor,
300 int left, int top, int right, int bottom) {
301 letter = letter.toUpperCase(Locale.getDefault());
302 Paint tilePaint = new Paint(), textPaint = new Paint();
303 tilePaint.setColor(tileColor);
304 textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
305 textPaint.setColor(FG_COLOR);
306 textPaint.setTypeface(Typeface.create("sans-serif-light",
307 Typeface.NORMAL));
308 textPaint.setTextSize((float) ((right - left) * 0.8));
309 Rect rect = new Rect();
310
311 canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
312 textPaint.getTextBounds(letter, 0, 1, rect);
313 float width = textPaint.measureText(letter);
314 canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
315 / 2 + rect.height() / 2, textPaint);
316 return true;
317 }
318
319 private boolean drawTile(Canvas canvas, MucOptions.User user, int left,
320 int top, int right, int bottom) {
321 Contact contact = user.getContact();
322 if (contact != null) {
323 Uri uri = null;
324 if (contact.getProfilePhoto() != null) {
325 uri = Uri.parse(contact.getProfilePhoto());
326 } else if (contact.getAvatar() != null) {
327 uri = mXmppConnectionService.getFileBackend().getAvatarUri(
328 contact.getAvatar());
329 }
330 if (drawTile(canvas, uri, left, top, right, bottom)) {
331 return true;
332 }
333 } else if (user.getAvatar() != null) {
334 Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(user.getAvatar());
335 if (drawTile(canvas, uri, left, top, right, bottom)) {
336 return true;
337 }
338 }
339 String name = contact != null ? contact.getDisplayName() : user.getName();
340 drawTile(canvas, name, left, top, right, bottom);
341 return true;
342 }
343
344 private boolean drawTile(Canvas canvas, Account account, int left, int top, int right, int bottom) {
345 String avatar = account.getAvatar();
346 if (avatar != null) {
347 Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(avatar);
348 if (uri != null) {
349 drawTile(canvas, uri, left, top, right, bottom);
350 }
351 } else {
352 drawTile(canvas, account.getJid().toBareJid().toString(), left, top, right, bottom);
353 }
354 return true;
355 }
356
357 private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) {
358 if (name != null) {
359 final String letter = name.isEmpty() ? "X" : name.substring(0, 1);
360 final int color = UIHelper.getColorForName(name);
361 drawTile(canvas, letter, color, left, top, right, bottom);
362 return true;
363 }
364 return false;
365 }
366
367 private boolean drawTile(Canvas canvas, Uri uri, int left, int top, int right, int bottom) {
368 if (uri != null) {
369 Bitmap bitmap = mXmppConnectionService.getFileBackend()
370 .cropCenter(uri, bottom - top, right - left);
371 if (bitmap != null) {
372 drawTile(canvas, bitmap, left, top, right, bottom);
373 return true;
374 }
375 }
376 return false;
377 }
378
379 private boolean drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop,
380 int dstright, int dstbottom) {
381 Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
382 canvas.drawBitmap(bm, null, dst, null);
383 return true;
384 }
385}