aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/services
diff options
context:
space:
mode:
authorsteckbrief <steckbrief@chefmail.de>2017-08-05 22:25:23 +0200
committersteckbrief <steckbrief@chefmail.de>2017-08-05 22:25:23 +0200
commit6f588444a953a54543661f7603d45a9093b7196a (patch)
treed9c2e1d8982336bbb3c1dcdb4d701c311ccc2d86 /src/main/java/de/thedevstack/conversationsplus/services
parent530d64beca87181e9bea8034353353cd8a7083a4 (diff)
Implements FS#249: Remove code duplication for avatar creation
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/services')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/services/AvatarService.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/services/AvatarService.java b/src/main/java/de/thedevstack/conversationsplus/services/AvatarService.java
index ed9c259f..0e10b883 100644
--- a/src/main/java/de/thedevstack/conversationsplus/services/AvatarService.java
+++ b/src/main/java/de/thedevstack/conversationsplus/services/AvatarService.java
@@ -1,19 +1,27 @@
package de.thedevstack.conversationsplus.services;
+import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
+import android.graphics.drawable.Drawable;
import android.net.Uri;
+import android.widget.ImageView;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
+import java.util.concurrent.RejectedExecutionException;
import de.thedevstack.android.logcat.Logging;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
+import de.thedevstack.conversationsplus.dto.LoadAvatarFor;
+import de.thedevstack.conversationsplus.ui.AsyncDrawable;
+import de.thedevstack.conversationsplus.ui.adapter.ConversationAdapter;
+import de.thedevstack.conversationsplus.ui.tasks.AvatarBitmapTask;
import de.thedevstack.conversationsplus.utils.AvatarUtil;
import de.thedevstack.conversationsplus.utils.ImageUtil;
import de.thedevstack.conversationsplus.utils.UiUpdateHelper;
@@ -176,6 +184,56 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
}
}
+ public void loadAvatar(LoadAvatarFor loadAvatarFor, ImageView imageView) {
+ if (cancelPotentialWork(imageView)) {
+ Resources resources = ConversationsPlusApplication.getAppContext().getResources();
+ int avatarSize = resources.getDimensionPixelSize(R.dimen.avatar_size);
+ final Bitmap bm;
+ if (loadAvatarFor instanceof Conversation) {
+ bm = AvatarService.getInstance().get((Conversation) loadAvatarFor, avatarSize, true);
+ } else if (loadAvatarFor instanceof Message) {
+ bm = AvatarService.getInstance().get((Message) loadAvatarFor, avatarSize, true);
+ } else {
+ bm = null;
+ }
+ if (bm != null) {
+ imageView.setImageBitmap(bm);
+ imageView.setBackgroundColor(0x00000000);
+ } else {
+ int color = 0x00000000;
+ if (loadAvatarFor instanceof Conversation) {
+ color = UIHelper.getColorForName(((Conversation) loadAvatarFor).getName());
+ } else if (loadAvatarFor instanceof Message) {
+ color = UIHelper.getColorForName(UIHelper.getMessageDisplayName((Message) loadAvatarFor));
+ }
+ imageView.setBackgroundColor(color);
+ imageView.setImageDrawable(null);
+ final AvatarBitmapTask<LoadAvatarFor> task = new AvatarBitmapTask<>(imageView, avatarSize);
+ final AsyncDrawable asyncDrawable = new AsyncDrawable(resources, null, task);
+ imageView.setImageDrawable(asyncDrawable);
+ try {
+ task.execute(loadAvatarFor);
+ } catch (final RejectedExecutionException ignored) {
+ }
+ }
+ }
+ }
+
+ public static boolean cancelPotentialWork(ImageView imageView) {
+ return null == getBitmapWorkerTask(imageView);
+ }
+
+ private static AvatarBitmapTask<LoadAvatarFor> getBitmapWorkerTask(ImageView imageView) {
+ if (imageView != null) {
+ final Drawable drawable = imageView.getDrawable();
+ if (drawable instanceof AsyncDrawable) {
+ final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
+ return asyncDrawable.getBitmapWorkerTask();
+ }
+ }
+ return null;
+ }
+
private Bitmap get(MucOptions mucOptions, int size, boolean cachedOnly) {
final String KEY = key(mucOptions, size);
Bitmap bitmap = ImageUtil.getBitmapFromCache(KEY);