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.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	public Bitmap get(final Contact contact, final int size) {
 42		final String KEY = key(contact, size);
 43		Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
 44		if (avatar != null) {
 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);
 55		}
 56		this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
 57		return avatar;
 58	}
 59
 60	public void clear(Contact contact) {
 61		for (Integer size : sizes) {
 62			this.mXmppConnectionService.getBitmapCache().remove(
 63					key(contact, size));
 64		}
 65	}
 66
 67	private String key(Contact contact, int size) {
 68		synchronized (this.sizes) {
 69			if (!this.sizes.contains(size)) {
 70				this.sizes.add(size);
 71			}
 72		}
 73		return PREFIX_CONTACT + "_" + contact.getAccount().getJid().toBareJid() + "_"
 74				+ contact.getJid() + "_" + String.valueOf(size);
 75	}
 76
 77	public Bitmap get(ListItem item, int size) {
 78		if (item instanceof Contact) {
 79			return get((Contact) item, size);
 80		} else if (item instanceof Bookmark) {
 81			Bookmark bookmark = (Bookmark) item;
 82			if (bookmark.getConversation() != null) {
 83				return get(bookmark.getConversation(), size);
 84			} else {
 85				return get(bookmark.getDisplayName(), size);
 86			}
 87		} else {
 88			return get(item.getDisplayName(), size);
 89		}
 90	}
 91
 92	public Bitmap get(Conversation conversation, int size) {
 93		if (conversation.getMode() == Conversation.MODE_SINGLE) {
 94			return get(conversation.getContact(), size);
 95		} else {
 96			return get(conversation.getMucOptions(), size);
 97		}
 98	}
 99
100	public void clear(Conversation conversation) {
101		if (conversation.getMode() == Conversation.MODE_SINGLE) {
102			clear(conversation.getContact());
103		} else {
104			clear(conversation.getMucOptions());
105		}
106	}
107
108	public Bitmap get(MucOptions mucOptions, int size) {
109		final String KEY = key(mucOptions, size);
110		Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
111		if (bitmap != null) {
112			return bitmap;
113		}
114		List<MucOptions.User> users = mucOptions.getUsers();
115		int count = users.size();
116		bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
117		Canvas canvas = new Canvas(bitmap);
118		bitmap.eraseColor(TRANSPARENT);
119
120		if (count == 0) {
121			String name = mucOptions.getConversation().getName();
122			String letter = name.substring(0, 1);
123			int color = UIHelper.getColorForName(name);
124			drawTile(canvas, letter, color, 0, 0, size, size);
125		} else if (count == 1) {
126			drawTile(canvas, users.get(0), 0, 0, size, size);
127		} else if (count == 2) {
128			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
129			drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size);
130		} else if (count == 3) {
131			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
132			drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1);
133			drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size,
134					size);
135		} else if (count == 4) {
136			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
137			drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
138			drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
139			drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size,
140					size);
141		} else {
142			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
143			drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
144			drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
145			drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1,
146					size, size);
147		}
148		this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
149		return bitmap;
150	}
151
152	public void clear(MucOptions options) {
153		for (Integer size : sizes) {
154			this.mXmppConnectionService.getBitmapCache().remove(
155					key(options, size));
156		}
157	}
158
159	private String key(MucOptions options, int size) {
160		synchronized (this.sizes) {
161			if (!this.sizes.contains(size)) {
162				this.sizes.add(size);
163			}
164		}
165		return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
166				+ "_" + String.valueOf(size);
167	}
168
169	public Bitmap get(Account account, int size) {
170		final String KEY = key(account, size);
171		Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
172		if (avatar != null) {
173			return avatar;
174		}
175		avatar = mXmppConnectionService.getFileBackend().getAvatar(
176				account.getAvatar(), size);
177		if (avatar == null) {
178			avatar = get(account.getJid().toBareJid().toString(), size);
179		}
180		mXmppConnectionService.getBitmapCache().put(KEY, avatar);
181		return avatar;
182	}
183
184	public void clear(Account account) {
185		for (Integer size : sizes) {
186			this.mXmppConnectionService.getBitmapCache().remove(
187					key(account, size));
188		}
189	}
190
191	private String key(Account account, int size) {
192		synchronized (this.sizes) {
193			if (!this.sizes.contains(size)) {
194				this.sizes.add(size);
195			}
196		}
197		return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
198				+ String.valueOf(size);
199	}
200
201	public Bitmap get(final String name, final int size) {
202		final String KEY = key(name, size);
203		Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
204		if (bitmap != null) {
205			return bitmap;
206		}
207		bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
208		Canvas canvas = new Canvas(bitmap);
209		String letter;
210		int color;
211		if (name.length() > 0) {
212			letter = name.substring(0, 1);
213			color = UIHelper.getColorForName(name);
214		} else {
215			letter = "X";
216			color = PLACEHOLDER_COLOR;
217		}
218		drawTile(canvas, letter, color, 0, 0, size, size);
219		mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
220		return bitmap;
221	}
222
223	private String key(String name, int size) {
224		synchronized (this.sizes) {
225			if (!this.sizes.contains(size)) {
226				this.sizes.add(size);
227			}
228		}
229		return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
230	}
231
232	private void drawTile(Canvas canvas, String letter, int tileColor,
233						  int left, int top, int right, int bottom) {
234		letter = letter.toUpperCase(Locale.getDefault());
235		Paint tilePaint = new Paint(), textPaint = new Paint();
236		tilePaint.setColor(tileColor);
237		textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
238		textPaint.setColor(FG_COLOR);
239		textPaint.setTypeface(Typeface.create("sans-serif-light",
240				Typeface.NORMAL));
241		textPaint.setTextSize((float) ((right - left) * 0.8));
242		Rect rect = new Rect();
243
244		canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
245		textPaint.getTextBounds(letter, 0, 1, rect);
246		float width = textPaint.measureText(letter);
247		canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
248				/ 2 + rect.height() / 2, textPaint);
249	}
250
251	private void drawTile(Canvas canvas, MucOptions.User user, int left,
252						  int top, int right, int bottom) {
253		Contact contact = user.getContact();
254		if (contact != null) {
255			Uri uri = null;
256			if (contact.getProfilePhoto() != null) {
257				uri = Uri.parse(contact.getProfilePhoto());
258			} else if (contact.getAvatar() != null) {
259				uri = mXmppConnectionService.getFileBackend().getAvatarUri(
260						contact.getAvatar());
261			}
262			if (uri != null) {
263				Bitmap bitmap = mXmppConnectionService.getFileBackend()
264						.cropCenter(uri, bottom - top, right - left);
265				if (bitmap != null) {
266					drawTile(canvas, bitmap, left, top, right, bottom);
267					return;
268				}
269			}
270		}
271		String name = user.getName();
272		String letter;
273		int color;
274		if (name.length() > 0) {
275			letter = name.substring(0, 1);
276			color = UIHelper.getColorForName(name);
277		} else {
278			letter = "X";
279			color = PLACEHOLDER_COLOR;
280		}
281		drawTile(canvas, letter, color, left, top, right, bottom);
282	}
283
284	private void drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop,
285						  int dstright, int dstbottom) {
286		Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
287		canvas.drawBitmap(bm, null, dst, null);
288	}
289
290}