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		synchronized (this.sizes) {
 62			for (Integer size : sizes) {
 63				this.mXmppConnectionService.getBitmapCache().remove(
 64						key(contact, size));
 65			}
 66		}
 67	}
 68
 69	private String key(Contact contact, int size) {
 70		synchronized (this.sizes) {
 71			if (!this.sizes.contains(size)) {
 72				this.sizes.add(size);
 73			}
 74		}
 75		return PREFIX_CONTACT + "_" + contact.getAccount().getJid().toBareJid() + "_"
 76				+ contact.getJid() + "_" + String.valueOf(size);
 77	}
 78
 79	public Bitmap get(ListItem item, int size) {
 80		if (item instanceof Contact) {
 81			return get((Contact) item, size);
 82		} else if (item instanceof Bookmark) {
 83			Bookmark bookmark = (Bookmark) item;
 84			if (bookmark.getConversation() != null) {
 85				return get(bookmark.getConversation(), size);
 86			} else {
 87				return get(bookmark.getDisplayName(), size);
 88			}
 89		} else {
 90			return get(item.getDisplayName(), size);
 91		}
 92	}
 93
 94	public Bitmap get(Conversation conversation, int size) {
 95		if (conversation.getMode() == Conversation.MODE_SINGLE) {
 96			return get(conversation.getContact(), size);
 97		} else {
 98			return get(conversation.getMucOptions(), size);
 99		}
100	}
101
102	public void clear(Conversation conversation) {
103		if (conversation.getMode() == Conversation.MODE_SINGLE) {
104			clear(conversation.getContact());
105		} else {
106			clear(conversation.getMucOptions());
107		}
108	}
109
110	public Bitmap get(MucOptions mucOptions, int size) {
111		final String KEY = key(mucOptions, size);
112		Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
113		if (bitmap != null) {
114			return bitmap;
115		}
116		final List<MucOptions.User> users = new ArrayList<>(mucOptions.getUsers());
117		int count = users.size();
118		bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
119		Canvas canvas = new Canvas(bitmap);
120		bitmap.eraseColor(TRANSPARENT);
121
122		if (count == 0) {
123			String name = mucOptions.getConversation().getName();
124			final String letter = name.isEmpty() ? "X" : name.substring(0,1);
125			final int color = UIHelper.getColorForName(name);
126			drawTile(canvas, letter, color, 0, 0, size, size);
127		} else if (count == 1) {
128			drawTile(canvas, users.get(0), 0, 0, size, size);
129		} else if (count == 2) {
130			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
131			drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size);
132		} else if (count == 3) {
133			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
134			drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1);
135			drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size,
136					size);
137		} else if (count == 4) {
138			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
139			drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
140			drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
141			drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size,
142					size);
143		} else {
144			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
145			drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
146			drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
147			drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1,
148					size, size);
149		}
150		this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
151		return bitmap;
152	}
153
154	public void clear(MucOptions options) {
155		synchronized (this.sizes) {
156			for (Integer size : sizes) {
157				this.mXmppConnectionService.getBitmapCache().remove(
158						key(options, size));
159			}
160		}
161	}
162
163	private String key(MucOptions options, int size) {
164		synchronized (this.sizes) {
165			if (!this.sizes.contains(size)) {
166				this.sizes.add(size);
167			}
168		}
169		return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
170				+ "_" + String.valueOf(size);
171	}
172
173	public Bitmap get(Account account, int size) {
174		final String KEY = key(account, size);
175		Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
176		if (avatar != null) {
177			return avatar;
178		}
179		avatar = mXmppConnectionService.getFileBackend().getAvatar(
180				account.getAvatar(), size);
181		if (avatar == null) {
182			avatar = get(account.getJid().toBareJid().toString(), size);
183		}
184		mXmppConnectionService.getBitmapCache().put(KEY, avatar);
185		return avatar;
186	}
187
188	public void clear(Account account) {
189		synchronized (this.sizes) {
190			for (Integer size : sizes) {
191				this.mXmppConnectionService.getBitmapCache().remove(
192						key(account, size));
193			}
194		}
195	}
196
197	private String key(Account account, int size) {
198		synchronized (this.sizes) {
199			if (!this.sizes.contains(size)) {
200				this.sizes.add(size);
201			}
202		}
203		return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
204				+ String.valueOf(size);
205	}
206
207	public Bitmap get(final String name, final int size) {
208		final String KEY = key(name, size);
209		Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
210		if (bitmap != null) {
211			return bitmap;
212		}
213		bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
214		Canvas canvas = new Canvas(bitmap);
215		final String letter = name.isEmpty() ? "X" : name.substring(0,1);
216		final int color = UIHelper.getColorForName(name);
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 = contact != null ? contact.getDisplayName() : user.getName();
271		final String letter = name.isEmpty() ? "X" : name.substring(0,1);
272		final int color = UIHelper.getColorForName(name);
273		drawTile(canvas, letter, color, left, top, right, bottom);
274	}
275
276	private void drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop,
277						  int dstright, int dstbottom) {
278		Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
279		canvas.drawBitmap(bm, null, dst, null);
280	}
281
282}