AvatarService.java

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