From 6f588444a953a54543661f7603d45a9093b7196a Mon Sep 17 00:00:00 2001 From: steckbrief Date: Sat, 5 Aug 2017 22:25:23 +0200 Subject: Implements FS#249: Remove code duplication for avatar creation --- .../ui/tasks/AvatarBitmapTask.java | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/de/thedevstack/conversationsplus/ui/tasks/AvatarBitmapTask.java (limited to 'src/main/java/de/thedevstack/conversationsplus/ui/tasks') diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/tasks/AvatarBitmapTask.java b/src/main/java/de/thedevstack/conversationsplus/ui/tasks/AvatarBitmapTask.java new file mode 100644 index 00000000..ec89b001 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/ui/tasks/AvatarBitmapTask.java @@ -0,0 +1,47 @@ +package de.thedevstack.conversationsplus.ui.tasks; + +import android.graphics.Bitmap; +import android.os.AsyncTask; +import android.widget.ImageView; + +import java.lang.ref.WeakReference; + +import de.thedevstack.conversationsplus.dto.LoadAvatarFor; +import de.thedevstack.conversationsplus.entities.Conversation; +import de.thedevstack.conversationsplus.entities.Message; +import de.thedevstack.conversationsplus.services.AvatarService; + +/** + * + */ +public class AvatarBitmapTask extends AsyncTask { + private final WeakReference imageViewReference; + private int avatarSize; + + public AvatarBitmapTask(ImageView imageView, int size) { + imageViewReference = new WeakReference<>(imageView); + this.avatarSize = size; + } + + @Override + protected Bitmap doInBackground(T... params) { + if (params[0] instanceof Conversation) { + return AvatarService.getInstance().get((Conversation)params[0], this.avatarSize); + } else if (params[0] instanceof Message) { + return AvatarService.getInstance().get((Message) params[0], this.avatarSize, isCancelled()); // Wirklich die richtige Nutzung von isCancelled()??? + } else { + return null; + } + } + + @Override + protected void onPostExecute(Bitmap bitmap) { + if (bitmap != null) { + final ImageView imageView = imageViewReference.get(); + if (imageView != null) { + imageView.setImageBitmap(bitmap); + imageView.setBackgroundColor(0x00000000); + } + } + } +} -- cgit v1.2.3