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