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