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 final String letter = name.isEmpty() ? "X" : name.substring(0,1);
134 final int color = UIHelper.getColorForName(name);
135 drawTile(canvas, letter, color, 0, 0, size, size);
136 } else if (count == 1) {
137 drawTile(canvas, users.get(0), 0, 0, size, size);
138 } else if (count == 2) {
139 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
140 drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size);
141 } else if (count == 3) {
142 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
143 drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1);
144 drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size,
145 size);
146 } else if (count == 4) {
147 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
148 drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
149 drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
150 drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size,
151 size);
152 } else {
153 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
154 drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
155 drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
156 drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1,
157 size, size);
158 }
159 this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
160 return bitmap;
161 }
162
163 public void clear(MucOptions options) {
164 synchronized (this.sizes) {
165 for (Integer size : sizes) {
166 this.mXmppConnectionService.getBitmapCache().remove(
167 key(options, size));
168 }
169 }
170 }
171
172 private String key(MucOptions options, int size) {
173 synchronized (this.sizes) {
174 if (!this.sizes.contains(size)) {
175 this.sizes.add(size);
176 }
177 }
178 return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
179 + "_" + String.valueOf(size);
180 }
181
182 public Bitmap get(Account account, int size) {
183 return get(account, size, false);
184 }
185
186 public Bitmap get(Account account, int size, boolean cachedOnly) {
187 final String KEY = key(account, size);
188 Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
189 if (avatar != null || cachedOnly) {
190 return avatar;
191 }
192 avatar = mXmppConnectionService.getFileBackend().getAvatar(
193 account.getAvatar(), size);
194 if (avatar == null) {
195 avatar = get(account.getJid().toBareJid().toString(), size,false);
196 }
197 mXmppConnectionService.getBitmapCache().put(KEY, avatar);
198 return avatar;
199 }
200
201 public Bitmap get(Message message, int size, boolean cachedOnly) {
202 if (message.getStatus() == Message.STATUS_RECEIVED) {
203 Contact contact = message.getContact();
204 if (contact != null) {
205 return get(contact, size, cachedOnly);
206 } else {
207 return get(UIHelper.getMessageDisplayName(message), size, cachedOnly);
208 }
209 } else {
210 return get(message.getConversation().getAccount(), size, cachedOnly);
211 }
212 }
213
214 public void clear(Account account) {
215 synchronized (this.sizes) {
216 for (Integer size : sizes) {
217 this.mXmppConnectionService.getBitmapCache().remove(
218 key(account, size));
219 }
220 }
221 }
222
223 private String key(Account account, int size) {
224 synchronized (this.sizes) {
225 if (!this.sizes.contains(size)) {
226 this.sizes.add(size);
227 }
228 }
229 return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
230 + String.valueOf(size);
231 }
232
233 public Bitmap get(String name, int size) {
234 return get(name,size,false);
235 }
236
237 public Bitmap get(final String name, final int size, boolean cachedOnly) {
238 final String KEY = key(name, size);
239 Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
240 if (bitmap != null || cachedOnly) {
241 return bitmap;
242 }
243 bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
244 Canvas canvas = new Canvas(bitmap);
245 final String trimmedName = name.trim();
246 final String letter = trimmedName.isEmpty() ? "X" : trimmedName.substring(0,1);
247 final int color = UIHelper.getColorForName(name);
248 drawTile(canvas, letter, color, 0, 0, size, size);
249 mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
250 return bitmap;
251 }
252
253 private String key(String name, int size) {
254 synchronized (this.sizes) {
255 if (!this.sizes.contains(size)) {
256 this.sizes.add(size);
257 }
258 }
259 return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
260 }
261
262 private void drawTile(Canvas canvas, String letter, int tileColor,
263 int left, int top, int right, int bottom) {
264 letter = letter.toUpperCase(Locale.getDefault());
265 Paint tilePaint = new Paint(), textPaint = new Paint();
266 tilePaint.setColor(tileColor);
267 textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
268 textPaint.setColor(FG_COLOR);
269 textPaint.setTypeface(Typeface.create("sans-serif-light",
270 Typeface.NORMAL));
271 textPaint.setTextSize((float) ((right - left) * 0.8));
272 Rect rect = new Rect();
273
274 canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
275 textPaint.getTextBounds(letter, 0, 1, rect);
276 float width = textPaint.measureText(letter);
277 canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
278 / 2 + rect.height() / 2, textPaint);
279 }
280
281 private void drawTile(Canvas canvas, MucOptions.User user, int left,
282 int top, int right, int bottom) {
283 Contact contact = user.getContact();
284 if (contact != null) {
285 Uri uri = null;
286 if (contact.getProfilePhoto() != null) {
287 uri = Uri.parse(contact.getProfilePhoto());
288 } else if (contact.getAvatar() != null) {
289 uri = mXmppConnectionService.getFileBackend().getAvatarUri(
290 contact.getAvatar());
291 }
292 if (uri != null) {
293 Bitmap bitmap = mXmppConnectionService.getFileBackend()
294 .cropCenter(uri, bottom - top, right - left);
295 if (bitmap != null) {
296 drawTile(canvas, bitmap, left, top, right, bottom);
297 return;
298 }
299 }
300 }
301 String name = contact != null ? contact.getDisplayName() : user.getName();
302 final String letter = name.isEmpty() ? "X" : name.substring(0,1);
303 final int color = UIHelper.getColorForName(name);
304 drawTile(canvas, letter, color, left, top, right, bottom);
305 }
306
307 private void drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop,
308 int dstright, int dstbottom) {
309 Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
310 canvas.drawBitmap(bm, null, dst, null);
311 }
312}