diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-03-20 20:56:01 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-03-20 20:56:01 +0100 |
commit | 35c70127720d048cd200dcefad8a710313019243 (patch) | |
tree | 7b72a0a417ed42c6a614f1a7ccf2c550679f4c8b /src | |
parent | d5430af2ecdd063d1d9295926cf74aaf2d9a3cb2 (diff) |
use async avatar loader in conference details
Diffstat (limited to 'src')
4 files changed, 104 insertions, 9 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java b/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java index 3cd57864f..d9fe66975 100644 --- a/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java @@ -5,7 +5,12 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentSender.SendIntentException; +import android.content.res.Resources; +import android.graphics.Bitmap; import android.graphics.PorterDuff; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; @@ -27,8 +32,10 @@ import android.widget.Toast; import org.openintents.openpgp.util.OpenPgpUtils; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collections; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.atomic.AtomicInteger; import de.pixart.messenger.Config; @@ -43,6 +50,7 @@ import de.pixart.messenger.entities.MucOptions.User; import de.pixart.messenger.services.XmppConnectionService; import de.pixart.messenger.services.XmppConnectionService.OnConversationUpdate; import de.pixart.messenger.services.XmppConnectionService.OnMucRosterUpdate; +import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.xmpp.jid.Jid; public class ConferenceDetailsActivity extends XmppActivity implements OnConversationUpdate, OnMucRosterUpdate, XmppConnectionService.OnAffiliationChanged, XmppConnectionService.OnRoleChanged, XmppConnectionService.OnConfigurationPushed { @@ -703,7 +711,7 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers } ImageView iv = view.findViewById(R.id.contact_photo); - iv.setImageBitmap(avatarService().get(user, getPixel(48), false)); + loadAvatar(user, iv); if (user.getRole() == MucOptions.Role.NONE) { tvDisplayName.setAlpha(INACTIVE_ALPHA); tvKey.setAlpha(INACTIVE_ALPHA); @@ -773,11 +781,95 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers } private void displayToast(final String msg) { - runOnUiThread(new Runnable() { - @Override - public void run() { - Toast.makeText(ConferenceDetailsActivity.this, msg, Toast.LENGTH_SHORT).show(); + runOnUiThread(() -> Toast.makeText(ConferenceDetailsActivity.this, msg, Toast.LENGTH_SHORT).show()); + } + + public static boolean cancelPotentialWork(User user, ImageView imageView) { + final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); + + if (bitmapWorkerTask != null) { + final User old = bitmapWorkerTask.o; + if (old == null || user != old) { + bitmapWorkerTask.cancel(true); + } else { + return false; + } + } + return true; + } + + private static BitmapWorkerTask 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; + } + + public void loadAvatar(User user, ImageView imageView) { + if (cancelPotentialWork(user, imageView)) { + final Bitmap bm = avatarService().get(user, getPixel(48), true); + if (bm != null) { + cancelPotentialWork(user, imageView); + imageView.setImageBitmap(bm); + imageView.setBackgroundColor(0x00000000); + } else { + String seed = user.getRealJid() != null ? user.getRealJid().toBareJid().toString() : null; + imageView.setBackgroundColor(UIHelper.getColorForName(seed == null ? user.getName() : seed)); + imageView.setImageDrawable(null); + final BitmapWorkerTask task = new BitmapWorkerTask(imageView); + final AsyncDrawable asyncDrawable = new AsyncDrawable(getResources(), null, task); + imageView.setImageDrawable(asyncDrawable); + try { + task.execute(user); + } catch (final RejectedExecutionException ignored) { + } + } + } + } + + static class AsyncDrawable extends BitmapDrawable { + private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference; + + public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { + super(res, bitmap); + bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); + } + + public BitmapWorkerTask getBitmapWorkerTask() { + return bitmapWorkerTaskReference.get(); + } + } + + class BitmapWorkerTask extends AsyncTask<User, Void, Bitmap> { + private final WeakReference<ImageView> imageViewReference; + private User o = null; + + private BitmapWorkerTask(ImageView imageView) { + imageViewReference = new WeakReference<>(imageView); + } + + @Override + protected Bitmap doInBackground(User... params) { + this.o = params[0]; + if (imageViewReference.get() == null) { + return null; } - }); + return avatarService().get(this.o, getPixel(48), isCancelled()); + } + + @Override + protected void onPostExecute(Bitmap bitmap) { + if (bitmap != null && !isCancelled()) { + final ImageView imageView = imageViewReference.get(); + if (imageView != null) { + imageView.setImageBitmap(bitmap); + imageView.setBackgroundColor(0x00000000); + } + } + } } } diff --git a/src/main/java/de/pixart/messenger/ui/adapter/AccountAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/AccountAdapter.java index 451e326e6..792d365ef 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/AccountAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/AccountAdapter.java @@ -134,7 +134,8 @@ public class AccountAdapter extends ArrayAdapter<Account> { @Override protected Bitmap doInBackground(Account... params) { - return activity.avatarService().get(params[0], activity.getPixel(56), isCancelled()); + this.account = params[0]; + return activity.avatarService().get(this.account, activity.getPixel(56), isCancelled()); } @Override diff --git a/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java index 162455732..3c2de008e 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java @@ -357,7 +357,8 @@ public class ConversationAdapter extends ArrayAdapter<Conversation> { @Override protected Bitmap doInBackground(Conversation... params) { - return activity.avatarService().get(params[0], activity.getPixel(56)); + this.conversation = params[0]; + return activity.avatarService().get(this.conversation, activity.getPixel(56), isCancelled()); } @Override diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java index ca6b02284..181bce607 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -1223,7 +1223,8 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie @Override protected Bitmap doInBackground(Message... params) { - return activity.avatarService().get(params[0], size, isCancelled()); + this.message = params[0]; + return activity.avatarService().get(this.message, size, isCancelled()); } @Override |