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