AvatarService.java

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