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;
  9import android.util.Log;
 10
 11import java.util.ArrayList;
 12import java.util.List;
 13import java.util.Locale;
 14
 15import eu.siacs.conversations.Config;
 16import eu.siacs.conversations.entities.Account;
 17import eu.siacs.conversations.entities.Bookmark;
 18import eu.siacs.conversations.entities.Contact;
 19import eu.siacs.conversations.entities.Conversation;
 20import eu.siacs.conversations.entities.ListItem;
 21import eu.siacs.conversations.entities.Message;
 22import eu.siacs.conversations.entities.MucOptions;
 23import eu.siacs.conversations.utils.UIHelper;
 24import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
 25import eu.siacs.conversations.xmpp.XmppConnection;
 26
 27public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
 28
 29	private static final int FG_COLOR = 0xFFFAFAFA;
 30	private static final int TRANSPARENT = 0x00000000;
 31	private static final int PLACEHOLDER_COLOR = 0xFF202020;
 32
 33	private static final String PREFIX_CONTACT = "contact";
 34	private static final String PREFIX_CONVERSATION = "conversation";
 35	private static final String PREFIX_ACCOUNT = "account";
 36	private static final String PREFIX_GENERIC = "generic";
 37
 38	final private ArrayList<Integer> sizes = new ArrayList<>();
 39
 40	protected XmppConnectionService mXmppConnectionService = null;
 41
 42	public AvatarService(XmppConnectionService service) {
 43		this.mXmppConnectionService = service;
 44	}
 45
 46	private Bitmap get(final Contact contact, final int size, boolean cachedOnly) {
 47		final String KEY = key(contact, size);
 48		Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
 49		if (avatar != null || cachedOnly) {
 50			return avatar;
 51		}
 52		if (contact.getProfilePhoto() != null) {
 53			avatar = mXmppConnectionService.getFileBackend().cropCenterSquare(Uri.parse(contact.getProfilePhoto()), size);
 54		}
 55		if (avatar == null && contact.getAvatar() != null) {
 56			avatar = mXmppConnectionService.getFileBackend().getAvatar(contact.getAvatar(), size);
 57		}
 58		if (avatar == null) {
 59            avatar = get(contact.getDisplayName(), size, cachedOnly);
 60		}
 61		this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
 62		return avatar;
 63	}
 64
 65	public Bitmap get(final MucOptions.User user, final int size, boolean cachedOnly) {
 66		Contact c = user.getContact();
 67		if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null || user.getAvatar() == null)) {
 68			return get(c, size, cachedOnly);
 69		} else {
 70			return getImpl(user, size, cachedOnly);
 71		}
 72	}
 73
 74	private Bitmap getImpl(final MucOptions.User user, final int size, boolean cachedOnly) {
 75		final String KEY = key(user, size);
 76		Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
 77		if (avatar != null || cachedOnly) {
 78			return avatar;
 79		}
 80		if (user.getAvatar() != null) {
 81			avatar = mXmppConnectionService.getFileBackend().getAvatar(user.getAvatar(), size);
 82		}
 83		if (avatar == null) {
 84			Contact contact = user.getContact();
 85			if (contact != null) {
 86				avatar = get(contact, size, cachedOnly);
 87			} else {
 88				avatar = get(user.getName(), size, cachedOnly);
 89			}
 90		}
 91		this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
 92		return avatar;
 93	}
 94
 95	public void clear(Contact contact) {
 96		synchronized (this.sizes) {
 97			for (Integer size : sizes) {
 98				this.mXmppConnectionService.getBitmapCache().remove(
 99						key(contact, size));
100			}
101		}
102		for(Conversation conversation : mXmppConnectionService.findAllConferencesWith(contact)) {
103			clear(conversation);
104		}
105	}
106
107	private String key(Contact contact, int size) {
108		synchronized (this.sizes) {
109			if (!this.sizes.contains(size)) {
110				this.sizes.add(size);
111			}
112		}
113		return PREFIX_CONTACT + "_" + contact.getAccount().getJid().toBareJid() + "_"
114				+ contact.getJid() + "_" + String.valueOf(size);
115	}
116
117	private String key(MucOptions.User user, int size) {
118		synchronized (this.sizes) {
119			if (!this.sizes.contains(size)) {
120				this.sizes.add(size);
121			}
122		}
123		return PREFIX_CONTACT + "_" + user.getAccount().getJid().toBareJid() + "_"
124				+ user.getFullJid() + "_" + String.valueOf(size);
125	}
126
127	public Bitmap get(ListItem item, int size) {
128		return get(item,size,false);
129	}
130
131	public Bitmap get(ListItem item, int size, boolean cachedOnly) {
132		if (item instanceof Contact) {
133			return get((Contact) item, size,cachedOnly);
134		} else if (item instanceof Bookmark) {
135			Bookmark bookmark = (Bookmark) item;
136			if (bookmark.getConversation() != null) {
137				return get(bookmark.getConversation(), size, cachedOnly);
138			} else {
139				return get(bookmark.getDisplayName(), size, cachedOnly);
140			}
141		} else {
142			return get(item.getDisplayName(), size, cachedOnly);
143		}
144	}
145
146	public Bitmap get(Conversation conversation, int size) {
147		return get(conversation,size,false);
148	}
149
150	public Bitmap get(Conversation conversation, int size, boolean cachedOnly) {
151		if (conversation.getMode() == Conversation.MODE_SINGLE) {
152			return get(conversation.getContact(), size, cachedOnly);
153		} else {
154			return get(conversation.getMucOptions(), size, cachedOnly);
155		}
156	}
157
158	public void clear(Conversation conversation) {
159		if (conversation.getMode() == Conversation.MODE_SINGLE) {
160			clear(conversation.getContact());
161		} else {
162			clear(conversation.getMucOptions());
163		}
164	}
165
166	private Bitmap get(MucOptions mucOptions, int size,  boolean cachedOnly) {
167		final String KEY = key(mucOptions, size);
168		Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
169		if (bitmap != null || cachedOnly) {
170			return bitmap;
171		}
172		final List<MucOptions.User> users = mucOptions.getUsers();
173		int count = users.size();
174		bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
175		Canvas canvas = new Canvas(bitmap);
176		bitmap.eraseColor(TRANSPARENT);
177
178		if (count == 0) {
179			String name = mucOptions.getConversation().getName();
180			drawTile(canvas, name, 0, 0, size, size);
181		} else if (count == 1) {
182			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
183			drawTile(canvas, mucOptions.getConversation().getAccount(), size / 2 + 1, 0, size, size);
184		} else if (count == 2) {
185			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
186			drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size);
187		} else if (count == 3) {
188			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
189			drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1);
190			drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size,
191					size);
192		} else if (count == 4) {
193			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
194			drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
195			drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
196			drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size,
197					size);
198		} else {
199			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
200			drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
201			drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
202			drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1,
203					size, size);
204		}
205		this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
206		return bitmap;
207	}
208
209	public void clear(MucOptions options) {
210		synchronized (this.sizes) {
211			for (Integer size : sizes) {
212				this.mXmppConnectionService.getBitmapCache().remove(key(options, size));
213			}
214		}
215	}
216
217	private String key(MucOptions options, int size) {
218		synchronized (this.sizes) {
219			if (!this.sizes.contains(size)) {
220				this.sizes.add(size);
221			}
222		}
223		return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
224				+ "_" + String.valueOf(size);
225	}
226
227	public Bitmap get(Account account, int size) {
228		return get(account, size, false);
229	}
230
231	public Bitmap get(Account account, int size, boolean cachedOnly) {
232		final String KEY = key(account, size);
233		Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
234		if (avatar != null || cachedOnly) {
235			return avatar;
236		}
237		avatar = mXmppConnectionService.getFileBackend().getAvatar(account.getAvatar(), size);
238		if (avatar == null) {
239			avatar = get(account.getJid().toBareJid().toString(), size,false);
240		}
241		mXmppConnectionService.getBitmapCache().put(KEY, avatar);
242		return avatar;
243	}
244
245	public Bitmap get(Message message, int size, boolean cachedOnly) {
246		final Conversation conversation = message.getConversation();
247		if (message.getStatus() == Message.STATUS_RECEIVED) {
248			Contact c = message.getContact();
249			if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null)) {
250				return get(c, size, cachedOnly);
251			} else if (message.getConversation().getMode() == Conversation.MODE_MULTI){
252				MucOptions.User user = conversation.getMucOptions().findUserByFullJid(message.getCounterpart());
253				if (user != null) {
254					return getImpl(user,size,cachedOnly);
255				}
256			}
257			return get(UIHelper.getMessageDisplayName(message), size, cachedOnly);
258		} else  {
259			return get(conversation.getAccount(), size, cachedOnly);
260		}
261	}
262
263	public void clear(Account account) {
264		synchronized (this.sizes) {
265			for (Integer size : sizes) {
266				this.mXmppConnectionService.getBitmapCache().remove(key(account, size));
267			}
268		}
269	}
270
271	public void clear(MucOptions.User user) {
272		synchronized (this.sizes) {
273			for (Integer size : sizes) {
274				this.mXmppConnectionService.getBitmapCache().remove(key(user, size));
275			}
276		}
277	}
278
279	private String key(Account account, int size) {
280		synchronized (this.sizes) {
281			if (!this.sizes.contains(size)) {
282				this.sizes.add(size);
283			}
284		}
285		return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
286				+ String.valueOf(size);
287	}
288
289	public Bitmap get(String name, int size) {
290		return get(name,size,false);
291	}
292
293	public Bitmap get(final String name, final int size, boolean cachedOnly) {
294		final String KEY = key(name, size);
295		Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
296		if (bitmap != null || cachedOnly) {
297			return bitmap;
298		}
299		bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
300		Canvas canvas = new Canvas(bitmap);
301		final String trimmedName = name == null ? "" : name.trim();
302		drawTile(canvas, trimmedName, 0, 0, size, size);
303		mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
304		return bitmap;
305	}
306
307	private String key(String name, int size) {
308		synchronized (this.sizes) {
309			if (!this.sizes.contains(size)) {
310				this.sizes.add(size);
311			}
312		}
313		return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
314	}
315
316	private boolean drawTile(Canvas canvas, String letter, int tileColor,
317						  int left, int top, int right, int bottom) {
318		letter = letter.toUpperCase(Locale.getDefault());
319		Paint tilePaint = new Paint(), textPaint = new Paint();
320		tilePaint.setColor(tileColor);
321		textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
322		textPaint.setColor(FG_COLOR);
323		textPaint.setTypeface(Typeface.create("sans-serif-light",
324				Typeface.NORMAL));
325		textPaint.setTextSize((float) ((right - left) * 0.8));
326		Rect rect = new Rect();
327
328		canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
329		textPaint.getTextBounds(letter, 0, 1, rect);
330		float width = textPaint.measureText(letter);
331		canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
332				/ 2 + rect.height() / 2, textPaint);
333		return true;
334	}
335
336	private boolean drawTile(Canvas canvas, MucOptions.User user, int left,
337						  int top, int right, int bottom) {
338		Contact contact = user.getContact();
339		if (contact != null) {
340			Uri uri = null;
341			if (contact.getProfilePhoto() != null) {
342				uri = Uri.parse(contact.getProfilePhoto());
343			} else if (contact.getAvatar() != null) {
344				uri = mXmppConnectionService.getFileBackend().getAvatarUri(
345						contact.getAvatar());
346			}
347			if (drawTile(canvas, uri, left, top, right, bottom)) {
348				return true;
349			}
350		} else if (user.getAvatar() != null) {
351			Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(user.getAvatar());
352			if (drawTile(canvas, uri, left, top, right, bottom)) {
353				return true;
354			}
355		}
356		String name = contact != null ? contact.getDisplayName() : user.getName();
357		drawTile(canvas, name, left, top, right, bottom);
358		return true;
359	}
360
361	private boolean drawTile(Canvas canvas, Account account, int left, int top, int right, int bottom) {
362		String avatar = account.getAvatar();
363		if (avatar != null) {
364			Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(avatar);
365			if (uri != null) {
366				if (drawTile(canvas, uri, left, top, right, bottom)) {
367					return true;
368				}
369			}
370		}
371		return drawTile(canvas, account.getJid().toBareJid().toString(), left, top, right, bottom);
372	}
373
374	private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) {
375		if (name != null) {
376			final String letter = getFirstLetter(name);
377			final int color = UIHelper.getColorForName(name);
378			drawTile(canvas, letter, color, left, top, right, bottom);
379			return true;
380		}
381		return false;
382	}
383
384	private static String getFirstLetter(String name) {
385		for(Character c : name.toCharArray()) {
386			if (Character.isLetterOrDigit(c)) {
387				return c.toString();
388			}
389		}
390		return "X";
391	}
392
393	private boolean drawTile(Canvas canvas, Uri uri, int left, int top, int right, int bottom) {
394		if (uri != null) {
395			Bitmap bitmap = mXmppConnectionService.getFileBackend()
396					.cropCenter(uri, bottom - top, right - left);
397			if (bitmap != null) {
398				drawTile(canvas, bitmap, left, top, right, bottom);
399				return true;
400			}
401		}
402		return false;
403	}
404
405	private boolean drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop, int dstright, int dstbottom) {
406		Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
407		canvas.drawBitmap(bm, null, dst, null);
408		return true;
409	}
410
411	@Override
412	public void onAdvancedStreamFeaturesAvailable(Account account) {
413		XmppConnection.Features features = account.getXmppConnection().getFeatures();
414		if (features.pep() && !features.pepPersistent()) {
415			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": has pep but is not persistent");
416			if (account.getAvatar() != null) {
417				mXmppConnectionService.republishAvatarIfNeeded(account);
418			}
419		}
420	}
421}