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.MucOptions;
20import eu.siacs.conversations.utils.UIHelper;
21
22public class AvatarService {
23
24 private static final int FG_COLOR = 0xFFFAFAFA;
25 private static final int TRANSPARENT = 0x00000000;
26 private static final int PLACEHOLDER_COLOR = 0xFF202020;
27
28 private static final String PREFIX_CONTACT = "contact";
29 private static final String PREFIX_CONVERSATION = "conversation";
30 private static final String PREFIX_ACCOUNT = "account";
31 private static final String PREFIX_GENERIC = "generic";
32
33 final private ArrayList<Integer> sizes = new ArrayList<>();
34
35 protected XmppConnectionService mXmppConnectionService = null;
36
37 public AvatarService(XmppConnectionService service) {
38 this.mXmppConnectionService = service;
39 }
40
41 private Bitmap get(final Contact contact, final int size, boolean cachedOnly) {
42 final String KEY = key(contact, size);
43 Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
44 if (avatar != null || cachedOnly) {
45 return avatar;
46 }
47 if (contact.getProfilePhoto() != null) {
48 avatar = mXmppConnectionService.getFileBackend().cropCenterSquare(Uri.parse(contact.getProfilePhoto()), size);
49 }
50 if (avatar == null && contact.getAvatar() != null) {
51 avatar = mXmppConnectionService.getFileBackend().getAvatar(contact.getAvatar(), size);
52 }
53 if (avatar == null) {
54 avatar = get(contact.getDisplayName(), size, cachedOnly);
55 }
56 this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
57 return avatar;
58 }
59
60 public void clear(Contact contact) {
61 synchronized (this.sizes) {
62 for (Integer size : sizes) {
63 this.mXmppConnectionService.getBitmapCache().remove(
64 key(contact, size));
65 }
66 }
67 }
68
69 private String key(Contact contact, int size) {
70 synchronized (this.sizes) {
71 if (!this.sizes.contains(size)) {
72 this.sizes.add(size);
73 }
74 }
75 return PREFIX_CONTACT + "_" + contact.getAccount().getJid().toBareJid() + "_"
76 + contact.getJid() + "_" + String.valueOf(size);
77 }
78
79 public Bitmap get(ListItem item, int size) {
80 return get(item,size,false);
81 }
82
83 public Bitmap get(ListItem item, int size, boolean cachedOnly) {
84 if (item instanceof Contact) {
85 return get((Contact) item, size,cachedOnly);
86 } else if (item instanceof Bookmark) {
87 Bookmark bookmark = (Bookmark) item;
88 if (bookmark.getConversation() != null) {
89 return get(bookmark.getConversation(), size, cachedOnly);
90 } else {
91 return get(bookmark.getDisplayName(), size, cachedOnly);
92 }
93 } else {
94 return get(item.getDisplayName(), size, cachedOnly);
95 }
96 }
97
98 public Bitmap get(Conversation conversation, int size) {
99 return get(conversation,size,false);
100 }
101
102 public Bitmap get(Conversation conversation, int size, boolean cachedOnly) {
103 if (conversation.getMode() == Conversation.MODE_SINGLE) {
104 return get(conversation.getContact(), size, cachedOnly);
105 } else {
106 return get(conversation.getMucOptions(), size, cachedOnly);
107 }
108 }
109
110 public void clear(Conversation conversation) {
111 if (conversation.getMode() == Conversation.MODE_SINGLE) {
112 clear(conversation.getContact());
113 } else {
114 clear(conversation.getMucOptions());
115 }
116 }
117
118 private Bitmap get(MucOptions mucOptions, int size, boolean cachedOnly) {
119 final String KEY = key(mucOptions, size);
120 Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
121 if (bitmap != null || cachedOnly) {
122 return bitmap;
123 }
124 final List<MucOptions.User> users = new ArrayList<>(mucOptions.getUsers());
125 int count = users.size();
126 bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
127 Canvas canvas = new Canvas(bitmap);
128 bitmap.eraseColor(TRANSPARENT);
129
130 if (count == 0) {
131 String name = mucOptions.getConversation().getName();
132 final String letter = name.isEmpty() ? "X" : name.substring(0,1);
133 final int color = UIHelper.getColorForName(name);
134 drawTile(canvas, letter, color, 0, 0, size, size);
135 } else if (count == 1) {
136 drawTile(canvas, users.get(0), 0, 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 final String KEY = key(account, size);
183 Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
184 if (avatar != null) {
185 return avatar;
186 }
187 avatar = mXmppConnectionService.getFileBackend().getAvatar(
188 account.getAvatar(), size);
189 if (avatar == null) {
190 avatar = get(account.getJid().toBareJid().toString(), size,false);
191 }
192 mXmppConnectionService.getBitmapCache().put(KEY, avatar);
193 return avatar;
194 }
195
196 public void clear(Account account) {
197 synchronized (this.sizes) {
198 for (Integer size : sizes) {
199 this.mXmppConnectionService.getBitmapCache().remove(
200 key(account, size));
201 }
202 }
203 }
204
205 private String key(Account account, int size) {
206 synchronized (this.sizes) {
207 if (!this.sizes.contains(size)) {
208 this.sizes.add(size);
209 }
210 }
211 return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
212 + String.valueOf(size);
213 }
214
215 public Bitmap get(String name, int size) {
216 return get(name,size,false);
217 }
218
219 public Bitmap get(final String name, final int size, boolean cachedOnly) {
220 final String KEY = key(name, size);
221 Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
222 if (bitmap != null || cachedOnly) {
223 return bitmap;
224 }
225 bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
226 Canvas canvas = new Canvas(bitmap);
227 final String trimmedName = name.trim();
228 final String letter = trimmedName.isEmpty() ? "X" : trimmedName.substring(0,1);
229 final int color = UIHelper.getColorForName(name);
230 drawTile(canvas, letter, color, 0, 0, size, size);
231 mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
232 return bitmap;
233 }
234
235 private String key(String name, int size) {
236 synchronized (this.sizes) {
237 if (!this.sizes.contains(size)) {
238 this.sizes.add(size);
239 }
240 }
241 return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
242 }
243
244 private void drawTile(Canvas canvas, String letter, int tileColor,
245 int left, int top, int right, int bottom) {
246 letter = letter.toUpperCase(Locale.getDefault());
247 Paint tilePaint = new Paint(), textPaint = new Paint();
248 tilePaint.setColor(tileColor);
249 textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
250 textPaint.setColor(FG_COLOR);
251 textPaint.setTypeface(Typeface.create("sans-serif-light",
252 Typeface.NORMAL));
253 textPaint.setTextSize((float) ((right - left) * 0.8));
254 Rect rect = new Rect();
255
256 canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
257 textPaint.getTextBounds(letter, 0, 1, rect);
258 float width = textPaint.measureText(letter);
259 canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
260 / 2 + rect.height() / 2, textPaint);
261 }
262
263 private void drawTile(Canvas canvas, MucOptions.User user, int left,
264 int top, int right, int bottom) {
265 Contact contact = user.getContact();
266 if (contact != null) {
267 Uri uri = null;
268 if (contact.getProfilePhoto() != null) {
269 uri = Uri.parse(contact.getProfilePhoto());
270 } else if (contact.getAvatar() != null) {
271 uri = mXmppConnectionService.getFileBackend().getAvatarUri(
272 contact.getAvatar());
273 }
274 if (uri != null) {
275 Bitmap bitmap = mXmppConnectionService.getFileBackend()
276 .cropCenter(uri, bottom - top, right - left);
277 if (bitmap != null) {
278 drawTile(canvas, bitmap, left, top, right, bottom);
279 return;
280 }
281 }
282 }
283 String name = contact != null ? contact.getDisplayName() : user.getName();
284 final String letter = name.isEmpty() ? "X" : name.substring(0,1);
285 final int color = UIHelper.getColorForName(name);
286 drawTile(canvas, letter, color, left, top, right, bottom);
287 }
288
289 private void drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop,
290 int dstright, int dstbottom) {
291 Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
292 canvas.drawBitmap(bm, null, dst, null);
293 }
294
295}