From 3c59b2958f2ec2d707a9e7af10ecd5be7ae3d2a4 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Sun, 27 Jan 2019 14:07:08 +0100 Subject: refactored avatar workers into single class --- .../java/de/pixart/messenger/entities/Account.java | 9 +- .../de/pixart/messenger/entities/Bookmark.java | 5 + .../java/de/pixart/messenger/entities/Contact.java | 5 + .../de/pixart/messenger/entities/Conversation.java | 9 +- .../de/pixart/messenger/entities/ListItem.java | 3 +- .../java/de/pixart/messenger/entities/Message.java | 13 ++- .../pixart/messenger/services/AvatarService.java | 19 ++++ .../messenger/ui/ConferenceDetailsActivity.java | 6 +- .../messenger/ui/ContactDetailsActivity.java | 3 +- .../pixart/messenger/ui/EditAccountActivity.java | 5 +- .../java/de/pixart/messenger/ui/XmppActivity.java | 10 +- .../messenger/ui/adapter/AccountAdapter.java | 92 +---------------- .../messenger/ui/adapter/ConversationAdapter.java | 93 +---------------- .../messenger/ui/adapter/ListItemAdapter.java | 96 +----------------- .../messenger/ui/adapter/MessageAdapter.java | 111 ++------------------- .../pixart/messenger/ui/util/AvatarWorkerTask.java | 111 +++++++++++++++++++++ src/main/res/values/dimens.xml | 3 + 17 files changed, 203 insertions(+), 390 deletions(-) create mode 100644 src/main/java/de/pixart/messenger/ui/util/AvatarWorkerTask.java diff --git a/src/main/java/de/pixart/messenger/entities/Account.java b/src/main/java/de/pixart/messenger/entities/Account.java index 2c7dbfbdc..3ebb1fafa 100644 --- a/src/main/java/de/pixart/messenger/entities/Account.java +++ b/src/main/java/de/pixart/messenger/entities/Account.java @@ -29,12 +29,14 @@ import de.pixart.messenger.crypto.OtrService; import de.pixart.messenger.crypto.PgpDecryptionService; import de.pixart.messenger.crypto.axolotl.AxolotlService; import de.pixart.messenger.crypto.axolotl.XmppAxolotlSession; +import de.pixart.messenger.services.AvatarService; import de.pixart.messenger.services.XmppConnectionService; +import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.utils.XmppUri; import de.pixart.messenger.xmpp.XmppConnection; import rocks.xmpp.addr.Jid; -public class Account extends AbstractEntity { +public class Account extends AbstractEntity implements AvatarService.Avatarable { public static final String TABLENAME = "accounts"; @@ -616,6 +618,11 @@ public class Account extends AbstractEntity { return this.getStatus() == State.ONLINE && this.getXmppConnection() != null; } + @Override + public int getAvatarBackgroundColor() { + return UIHelper.getColorForName(jid.asBareJid().toString()); + } + public enum State { DISABLED(false, false), OFFLINE(false), diff --git a/src/main/java/de/pixart/messenger/entities/Bookmark.java b/src/main/java/de/pixart/messenger/entities/Bookmark.java index e670673dd..0d1f5781d 100644 --- a/src/main/java/de/pixart/messenger/entities/Bookmark.java +++ b/src/main/java/de/pixart/messenger/entities/Bookmark.java @@ -186,4 +186,9 @@ public class Bookmark extends Element implements ListItem { } return StringUtils.changed(before, name); } + + @Override + public int getAvatarBackgroundColor() { + return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName()); + } } diff --git a/src/main/java/de/pixart/messenger/entities/Contact.java b/src/main/java/de/pixart/messenger/entities/Contact.java index 3ee636fe4..ad9aeeb13 100644 --- a/src/main/java/de/pixart/messenger/entities/Contact.java +++ b/src/main/java/de/pixart/messenger/entities/Contact.java @@ -603,6 +603,11 @@ public class Contact implements ListItem, Blockable { return Options.SYNCED_VIA_OTHER; } + @Override + public int getAvatarBackgroundColor() { + return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName()); + } + public final class Options { public static final int TO = 0; public static final int FROM = 1; diff --git a/src/main/java/de/pixart/messenger/entities/Conversation.java b/src/main/java/de/pixart/messenger/entities/Conversation.java index 22c363f3a..ee882ebec 100644 --- a/src/main/java/de/pixart/messenger/entities/Conversation.java +++ b/src/main/java/de/pixart/messenger/entities/Conversation.java @@ -30,8 +30,10 @@ import de.pixart.messenger.crypto.OmemoSetting; import de.pixart.messenger.crypto.PgpDecryptionService; import de.pixart.messenger.crypto.axolotl.AxolotlService; import de.pixart.messenger.persistance.DatabaseBackend; +import de.pixart.messenger.services.AvatarService; import de.pixart.messenger.services.QuickConversationsService; import de.pixart.messenger.utils.JidHelper; +import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.xmpp.chatstate.ChatState; import de.pixart.messenger.xmpp.mam.MamReference; import rocks.xmpp.addr.Jid; @@ -39,7 +41,7 @@ import rocks.xmpp.addr.Jid; import static de.pixart.messenger.entities.Bookmark.printableValue; -public class Conversation extends AbstractEntity implements Blockable, Comparable, Conversational { +public class Conversation extends AbstractEntity implements Blockable, Comparable, Conversational, AvatarService.Avatarable { public static final String TABLENAME = "conversations"; public static final int STATUS_AVAILABLE = 0; @@ -1189,6 +1191,11 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl return 0; } + @Override + public int getAvatarBackgroundColor() { + return UIHelper.getColorForName(getName().toString()); + } + public interface OnMessageFound { void onMessageFound(final Message message); } diff --git a/src/main/java/de/pixart/messenger/entities/ListItem.java b/src/main/java/de/pixart/messenger/entities/ListItem.java index 21fb6c2b6..fba37cc4c 100644 --- a/src/main/java/de/pixart/messenger/entities/ListItem.java +++ b/src/main/java/de/pixart/messenger/entities/ListItem.java @@ -4,9 +4,10 @@ import android.content.Context; import java.util.List; +import de.pixart.messenger.services.AvatarService; import rocks.xmpp.addr.Jid; -public interface ListItem extends Comparable { +public interface ListItem extends Comparable, AvatarService.Avatarable { String getDisplayName(); int getOffline(); diff --git a/src/main/java/de/pixart/messenger/entities/Message.java b/src/main/java/de/pixart/messenger/entities/Message.java index c5843d840..b32c440a5 100644 --- a/src/main/java/de/pixart/messenger/entities/Message.java +++ b/src/main/java/de/pixart/messenger/entities/Message.java @@ -2,6 +2,7 @@ package de.pixart.messenger.entities; import android.content.ContentValues; import android.database.Cursor; +import android.graphics.Color; import android.text.SpannableStringBuilder; import android.util.Log; @@ -16,6 +17,7 @@ import java.util.Set; import de.pixart.messenger.Config; import de.pixart.messenger.crypto.axolotl.FingerprintStatus; +import de.pixart.messenger.services.AvatarService; import de.pixart.messenger.utils.CryptoHelper; import de.pixart.messenger.utils.Emoticons; import de.pixart.messenger.utils.GeoHelper; @@ -25,7 +27,7 @@ import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.utils.XmppUri; import rocks.xmpp.addr.Jid; -public class Message extends AbstractEntity { +public class Message extends AbstractEntity implements AvatarService.Avatarable { public static final String TABLENAME = "messages"; @@ -633,6 +635,15 @@ public class Message extends AbstractEntity { return this.counterparts; } + @Override + public int getAvatarBackgroundColor() { + if (type == Message.TYPE_STATUS && getCounterparts() != null && getCounterparts().size() > 1) { + return Color.TRANSPARENT; + } else { + return UIHelper.getColorForName(UIHelper.getMessageDisplayName(this)); + } + } + public static class MergeSeparator { } diff --git a/src/main/java/de/pixart/messenger/services/AvatarService.java b/src/main/java/de/pixart/messenger/services/AvatarService.java index f4a9b9818..4ed01c42a 100644 --- a/src/main/java/de/pixart/messenger/services/AvatarService.java +++ b/src/main/java/de/pixart/messenger/services/AvatarService.java @@ -14,6 +14,7 @@ import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.support.annotation.ColorInt; import android.support.annotation.Nullable; import android.support.v4.content.res.ResourcesCompat; import android.text.TextUtils; @@ -72,6 +73,19 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded { return (int) (SYSTEM_UI_AVATAR_SIZE * context.getResources().getDisplayMetrics().density); } + public Bitmap get(final Avatarable avatarable, final int size, final boolean cachedOnly) { + if (avatarable instanceof Account) { + return get((Account) avatarable,size,cachedOnly); + } else if (avatarable instanceof Conversation) { + return get((Conversation) avatarable, size, cachedOnly); + } else if (avatarable instanceof Message) { + return get((Message) avatarable, size, cachedOnly); + } else if (avatarable instanceof ListItem) { + return get((ListItem) avatarable, size, cachedOnly); + } + throw new AssertionError("AvatarService does not know how to generate avatar from "+avatarable.getClass().getName()); + } + private Bitmap get(final Contact contact, final int size, boolean cachedOnly) { if (contact.isSelf()) { return get(contact.getAccount(), size, cachedOnly); @@ -650,4 +664,9 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded { private static String emptyOnNull(@Nullable Jid value) { return value == null ? "" : value.toString(); } + + public interface Avatarable { + @ColorInt + int getAvatarBackgroundColor(); + } } \ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java b/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java index 8b88d849e..3508fef7c 100644 --- a/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java @@ -56,6 +56,7 @@ import de.pixart.messenger.services.XmppConnectionService.OnMucRosterUpdate; import de.pixart.messenger.ui.adapter.MediaAdapter; import de.pixart.messenger.ui.interfaces.OnMediaLoaded; import de.pixart.messenger.ui.util.Attachment; +import de.pixart.messenger.ui.util.AvatarWorkerTask; import de.pixart.messenger.ui.util.GridManager; import de.pixart.messenger.ui.util.MucDetailsContextMenuHelper; import de.pixart.messenger.ui.util.MyLinkify; @@ -577,8 +578,9 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers } else { this.binding.detailsAccount.setVisibility(View.GONE); } - this.binding.detailsMucAvatar.setImageBitmap(avatarService().get(mConversation, getPixel(Config.AVATAR_SIZE))); - this.binding.yourPhoto.setImageBitmap(avatarService().get(mConversation.getAccount(), getPixel(48))); + AvatarWorkerTask.loadAvatar(mConversation, binding.detailsMucAvatar, getPixel(Config.AVATAR_SIZE)); + AvatarWorkerTask.loadAvatar(mConversation, binding.yourPhoto, R.dimen.avatar_on_details_screen_size); + String roomName = mucOptions.getName(); String subject = mucOptions.getSubject(); final boolean hasTitle; diff --git a/src/main/java/de/pixart/messenger/ui/ContactDetailsActivity.java b/src/main/java/de/pixart/messenger/ui/ContactDetailsActivity.java index 9db7ce7c9..8f8bd87d9 100644 --- a/src/main/java/de/pixart/messenger/ui/ContactDetailsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ContactDetailsActivity.java @@ -50,6 +50,7 @@ import de.pixart.messenger.services.XmppConnectionService.OnRosterUpdate; import de.pixart.messenger.ui.adapter.MediaAdapter; import de.pixart.messenger.ui.interfaces.OnMediaLoaded; import de.pixart.messenger.ui.util.Attachment; +import de.pixart.messenger.ui.util.AvatarWorkerTask; import de.pixart.messenger.ui.util.GridManager; import de.pixart.messenger.ui.util.JidDialog; import de.pixart.messenger.utils.Compatibility; @@ -527,7 +528,7 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp account = contact.getAccount().getJid().asBareJid().toString(); } binding.detailsAccount.setText(getString(R.string.using_account, account)); - binding.detailsContactBadge.setImageBitmap(avatarService().get(contact, getPixel(Config.AVATAR_SIZE))); + AvatarWorkerTask.loadAvatar(contact, binding.detailsContactBadge, Config.AVATAR_SIZE); binding.detailsContactBadge.setOnClickListener(this.onBadgeClick); if (xmppConnectionService.multipleAccounts()) { diff --git a/src/main/java/de/pixart/messenger/ui/EditAccountActivity.java b/src/main/java/de/pixart/messenger/ui/EditAccountActivity.java index a380a3368..37693540d 100644 --- a/src/main/java/de/pixart/messenger/ui/EditAccountActivity.java +++ b/src/main/java/de/pixart/messenger/ui/EditAccountActivity.java @@ -64,6 +64,7 @@ import de.pixart.messenger.services.XmppConnectionService.OnAccountUpdate; import de.pixart.messenger.services.XmppConnectionService.OnCaptchaRequested; import de.pixart.messenger.ui.adapter.KnownHostsAdapter; import de.pixart.messenger.ui.adapter.PresenceTemplateAdapter; +import de.pixart.messenger.ui.util.AvatarWorkerTask; import de.pixart.messenger.ui.util.PendingItem; import de.pixart.messenger.ui.util.SoftKeyboardUtils; import de.pixart.messenger.utils.CryptoHelper; @@ -639,7 +640,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat } private void refreshAvatar() { - binding.avater.setImageBitmap(avatarService().get(mAccount, (int) getResources().getDimension(R.dimen.avatar_on_details_screen_size))); + AvatarWorkerTask.loadAvatar(mAccount, binding.avater, R.dimen.avatar_on_details_screen_size); } @Override @@ -1057,7 +1058,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat if (!mInitMode) { binding.avater.setVisibility(View.VISIBLE); - binding.avater.setImageBitmap(avatarService().get(this.mAccount, getPixel(Config.AVATAR_SIZE))); + AvatarWorkerTask.loadAvatar(mAccount, binding.avater, getPixel(Config.AVATAR_SIZE)); this.binding.accountJid.setEnabled(false); } else { binding.avater.setVisibility(View.GONE); diff --git a/src/main/java/de/pixart/messenger/ui/XmppActivity.java b/src/main/java/de/pixart/messenger/ui/XmppActivity.java index 5b714984b..d269f11ac 100644 --- a/src/main/java/de/pixart/messenger/ui/XmppActivity.java +++ b/src/main/java/de/pixart/messenger/ui/XmppActivity.java @@ -38,6 +38,7 @@ import android.os.SystemClock; import android.preference.PreferenceManager; import android.provider.Settings; import android.support.annotation.BoolRes; +import android.support.annotation.NonNull; import android.support.annotation.StringRes; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; @@ -1228,11 +1229,12 @@ public abstract class XmppActivity extends ActionBarActivity { } } - public static XmppActivity find(WeakReference viewWeakReference) { + public static XmppActivity find(@NonNull WeakReference viewWeakReference) { final View view = viewWeakReference.get(); - if (view == null) { - return null; - } + return view == null ? null : find(view); + } + + public static XmppActivity find(@NonNull final View view) { final Context context = view.getContext(); if (context instanceof XmppActivity) { return (XmppActivity) context; 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 2624828a8..3955ca4cc 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/AccountAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/AccountAdapter.java @@ -22,6 +22,7 @@ import de.pixart.messenger.R; import de.pixart.messenger.databinding.AccountRowBinding; import de.pixart.messenger.entities.Account; import de.pixart.messenger.ui.XmppActivity; +import de.pixart.messenger.ui.util.AvatarWorkerTask; import de.pixart.messenger.ui.util.StyledAttributes; import de.pixart.messenger.utils.UIHelper; @@ -59,7 +60,7 @@ public class AccountAdapter extends ArrayAdapter { } else { viewHolder.binding.accountJid.setText(account.getJid().asBareJid().toString()); } - loadAvatar(account, viewHolder.binding.accountImage); + AvatarWorkerTask.loadAvatar(account, viewHolder.binding.accountImage, R.dimen.avatar); viewHolder.binding.accountStatus.setText(getContext().getString(account.getStatus().getReadableId())); switch (account.getStatus()) { case ONLINE: @@ -97,97 +98,8 @@ public class AccountAdapter extends ArrayAdapter { } } - private static class BitmapWorkerTask extends AsyncTask { - private final WeakReference imageViewReference; - private Account account = null; - - BitmapWorkerTask(ImageView imageView) { - imageViewReference = new WeakReference<>(imageView); - } - - @Override - protected Bitmap doInBackground(Account... params) { - this.account = params[0]; - final XmppActivity activity = XmppActivity.find(imageViewReference); - if (activity == null) { - return null; - } - return activity.avatarService().get(this.account, activity.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); - } - } - } - } - - private void loadAvatar(Account account, ImageView imageView) { - if (cancelPotentialWork(account, imageView)) { - final Bitmap bm = activity.avatarService().get(account, activity.getPixel(48), true); - if (bm != null) { - cancelPotentialWork(account, imageView); - imageView.setImageBitmap(bm); - imageView.setBackgroundColor(0x00000000); - } else { - imageView.setBackgroundColor(UIHelper.getColorForName(account.getJid().asBareJid().toString())); - imageView.setImageDrawable(null); - final BitmapWorkerTask task = new BitmapWorkerTask(imageView); - final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task); - imageView.setImageDrawable(asyncDrawable); - try { - task.execute(account); - } catch (final RejectedExecutionException ignored) { - } - } - } - } - public interface OnTglAccountState { void onClickTglAccountState(Account account, boolean state); } - - private static boolean cancelPotentialWork(Account account, ImageView imageView) { - final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); - - if (bitmapWorkerTask != null) { - final Account oldAccount = bitmapWorkerTask.account; - if (oldAccount == null || account != oldAccount) { - 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; - } - - static class AsyncDrawable extends BitmapDrawable { - private final WeakReference bitmapWorkerTaskReference; - - AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { - super(res, bitmap); - bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); - } - - BitmapWorkerTask getBitmapWorkerTask() { - return bitmapWorkerTaskReference.get(); - } - } } 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 5656ce625..45fb05c5f 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java @@ -29,6 +29,7 @@ import de.pixart.messenger.entities.Message; import de.pixart.messenger.entities.MucOptions; import de.pixart.messenger.ui.ConversationFragment; import de.pixart.messenger.ui.XmppActivity; +import de.pixart.messenger.ui.util.AvatarWorkerTask; import de.pixart.messenger.ui.util.StyledAttributes; import de.pixart.messenger.utils.EmojiWrapper; import de.pixart.messenger.utils.IrregularUnicodeDetector; @@ -47,31 +48,6 @@ public class ConversationAdapter extends RecyclerView.Adapter listener.onConversationClick(v, conversation)); if (conversation.getMode() == Conversation.MODE_SINGLE && ShowPresenceColoredNames()) { @@ -317,27 +293,6 @@ public class ConversationAdapter extends RecyclerView.Adapter bitmapWorkerTaskReference; - - AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { - super(res, bitmap); - bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); - } - - BitmapWorkerTask getBitmapWorkerTask() { - return bitmapWorkerTaskReference.get(); - } - } - - static class BitmapWorkerTask extends AsyncTask { - private final WeakReference imageViewReference; - private Conversation conversation = null; - - BitmapWorkerTask(ImageView imageView) { - imageViewReference = new WeakReference<>(imageView); - } - - - @Override - protected Bitmap doInBackground(Conversation... params) { - this.conversation = params[0]; - final XmppActivity activity = XmppActivity.find(imageViewReference); - if (activity == null) { - return null; - } - return activity.avatarService().get(this.conversation, activity.getPixel(56), 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); - } - } - } - } - private boolean ShowPresenceColoredNames() { return getPreferences().getBoolean("presence_colored_names", activity.getResources().getBoolean(R.bool.presence_colored_names)); } diff --git a/src/main/java/de/pixart/messenger/ui/adapter/ListItemAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/ListItemAdapter.java index 71f8aaa34..a9b07052e 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/ListItemAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/ListItemAdapter.java @@ -26,6 +26,7 @@ import de.pixart.messenger.databinding.ContactBinding; import de.pixart.messenger.entities.ListItem; import de.pixart.messenger.ui.SettingsActivity; import de.pixart.messenger.ui.XmppActivity; +import de.pixart.messenger.ui.util.AvatarWorkerTask; import de.pixart.messenger.ui.util.StyledAttributes; import de.pixart.messenger.utils.EmojiWrapper; import de.pixart.messenger.utils.IrregularUnicodeDetector; @@ -121,7 +122,7 @@ public class ListItemAdapter extends ArrayAdapter { viewHolder.avatar.setAlpha(ACTIVE_ALPHA); viewHolder.tags.setAlpha(ACTIVE_ALPHA); } - loadAvatar(item, viewHolder.avatar); + AvatarWorkerTask.loadAvatar(item, viewHolder.avatar, R.dimen.avatar); return view; } @@ -133,96 +134,6 @@ public class ListItemAdapter extends ArrayAdapter { void onTagClicked(String tag); } - private static class BitmapWorkerTask extends AsyncTask { - private final WeakReference imageViewReference; - private ListItem item = null; - - BitmapWorkerTask(ImageView imageView) { - imageViewReference = new WeakReference<>(imageView); - } - - @Override - protected Bitmap doInBackground(ListItem... params) { - this.item = params[0]; - final XmppActivity activity = XmppActivity.find(imageViewReference); - if (activity == null) { - return null; - } - return activity.avatarService().get(this.item, activity.getPixel(56), 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); - } - } - } - } - - private void loadAvatar(ListItem item, ImageView imageView) { - if (cancelPotentialWork(item, imageView)) { - final Bitmap bm = activity.avatarService().get(item, activity.getPixel(48), true); - if (bm != null) { - cancelPotentialWork(item, imageView); - imageView.setImageBitmap(bm); - imageView.setBackgroundColor(0x00000000); - } else { - String seed = item.getJid() != null ? item.getJid().asBareJid().toString() : item.getDisplayName(); - imageView.setBackgroundColor(UIHelper.getColorForName(seed)); - imageView.setImageDrawable(null); - final BitmapWorkerTask task = new BitmapWorkerTask(imageView); - final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task); - imageView.setImageDrawable(asyncDrawable); - try { - task.executeOnExecutor(BitmapWorkerTask.THREAD_POOL_EXECUTOR, item); - } catch (final RejectedExecutionException ignored) { - } - } - } - } - - private static boolean cancelPotentialWork(ListItem item, ImageView imageView) { - final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); - - if (bitmapWorkerTask != null) { - final ListItem oldItem = bitmapWorkerTask.item; - if (oldItem == null || item != oldItem) { - 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; - } - - static class AsyncDrawable extends BitmapDrawable { - private final WeakReference bitmapWorkerTaskReference; - - AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { - super(res, bitmap); - bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); - } - - BitmapWorkerTask getBitmapWorkerTask() { - return bitmapWorkerTaskReference.get(); - } - } - private static class ViewHolder { private TextView name; private TextView jid; @@ -243,8 +154,7 @@ public class ListItemAdapter extends ArrayAdapter { } } - - public boolean ShowPresenceColoredNames() { + private boolean ShowPresenceColoredNames() { return getPreferences().getBoolean("presence_colored_names", activity.getResources().getBoolean(R.bool.presence_colored_names)); } 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 f89d1a7c9..bdc82d6fa 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -74,6 +74,7 @@ import de.pixart.messenger.ui.ConversationsActivity; import de.pixart.messenger.ui.XmppActivity; import de.pixart.messenger.ui.text.DividerSpan; import de.pixart.messenger.ui.text.QuoteSpan; +import de.pixart.messenger.ui.util.AvatarWorkerTask; import de.pixart.messenger.ui.util.MyLinkify; import de.pixart.messenger.ui.util.ViewUtil; import de.pixart.messenger.ui.widget.ClickableMovementMethod; @@ -121,31 +122,6 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie updatePreferences(); } - public static boolean cancelPotentialWork(Message message, ImageView imageView) { - final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); - - if (bitmapWorkerTask != null) { - final Message oldMessage = bitmapWorkerTask.message; - if (oldMessage == null || message != oldMessage) { - 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; - } - private static void resetClickListener(View... views) { for (View view : views) { view.setOnClickListener(null); @@ -252,7 +228,7 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie filesize = Math.round(params.size * 1f / (1024 * 1024)) + " MiB"; } else if (params.size >= (1 * 1024)) { filesize = Math.round(params.size * 1f / 1024) + " KiB"; - } else if (params.size > 0){ + } else if (params.size > 0) { filesize = params.size + " B"; } if (transferable != null && transferable.getStatus() == Transferable.STATUS_FAILED) { @@ -605,7 +581,7 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie if (highlightedTerm != null) { StylingHelper.highlight(activity, body, highlightedTerm, StylingHelper.isDarkText(viewHolder.messageBody)); } - MyLinkify.addLinks(body,true); + MyLinkify.addLinks(body, true); viewHolder.messageBody.setAutoLinkMask(0); viewHolder.messageBody.setText(EmojiWrapper.transform(body)); viewHolder.messageBody.setTextIsSelectable(true); @@ -922,10 +898,10 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie boolean showAvatar; if (conversation.getMode() == Conversation.MODE_SINGLE) { showAvatar = true; - loadAvatar(message,viewHolder.contact_picture,activity.getPixel(32)); + AvatarWorkerTask.loadAvatar(message, viewHolder.contact_picture, R.dimen.avatar_on_status_message); } else if (message.getCounterpart() != null || message.getTrueCounterpart() != null || (message.getCounterparts() != null && message.getCounterparts().size() > 0)) { showAvatar = true; - loadAvatar(message,viewHolder.contact_picture,activity.getPixel(32)); + AvatarWorkerTask.loadAvatar(message, viewHolder.contact_picture, R.dimen.avatar_on_status_message); } else { showAvatar = false; } @@ -938,7 +914,7 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie } return view; } else { - loadAvatar(message, viewHolder.contact_picture, activity.getPixel(48)); + AvatarWorkerTask.loadAvatar(message, viewHolder.contact_picture, R.dimen.avatar); } resetClickListener(viewHolder.message_box, viewHolder.messageBody); @@ -1035,7 +1011,7 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie viewHolder.encryption.setVisibility(View.GONE); viewHolder.encryption.setTextColor(this.getMessageTextColor(darkBackground, false)); } else { - viewHolder.message_box.setBackgroundResource(darkBackground ? R.drawable.message_bubble_received_warning_dark: R.drawable.message_bubble_received_warning); + viewHolder.message_box.setBackgroundResource(darkBackground ? R.drawable.message_bubble_received_warning_dark : R.drawable.message_bubble_received_warning); viewHolder.encryption.setVisibility(View.VISIBLE); viewHolder.encryption.setTextColor(this.getWarningTextColor(darkBackground)); if (omemoEncryption && !message.isTrusted()) { @@ -1139,33 +1115,6 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie this.mIndicateReceived = p.getBoolean("indicate_received", activity.getResources().getBoolean(R.bool.indicate_received)); } - private void loadAvatar(Message message, ImageView imageView, int size) { - if (cancelPotentialWork(message, imageView)) { - final Bitmap bm = activity.avatarService().get(message, size, true); - if (bm != null) { - cancelPotentialWork(message, imageView); - imageView.setImageBitmap(bm); - imageView.setBackgroundColor(Color.TRANSPARENT); - } else { - @ColorInt int bg; - if (message.getType() == Message.TYPE_STATUS && message.getCounterparts() != null && message.getCounterparts().size() > 1) { - bg = Color.TRANSPARENT; - } else { - bg = UIHelper.getColorForName(UIHelper.getMessageDisplayName(message)); - } - imageView.setBackgroundColor(bg); - imageView.setImageDrawable(null); - final BitmapWorkerTask task = new BitmapWorkerTask(imageView, size); - final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task); - imageView.setImageDrawable(asyncDrawable); - try { - task.executeOnExecutor(BitmapWorkerTask.THREAD_POOL_EXECUTOR, message); - } catch (final RejectedExecutionException ignored) { - } - } - } - } - public void setHighlightedTerm(List terms) { this.highlightedTerm = terms == null ? null : StylingHelper.filterHighlightedWords(terms); } @@ -1202,19 +1151,6 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie protected TextView encryption; } - static class AsyncDrawable extends BitmapDrawable { - private final WeakReference bitmapWorkerTaskReference; - - public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { - super(res, bitmap); - bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); - } - - public BitmapWorkerTask getBitmapWorkerTask() { - return bitmapWorkerTaskReference.get(); - } - } - private class MessageBodyActionModeCallback implements ActionMode.Callback { private final TextView textView; @@ -1257,38 +1193,7 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie } @Override - public void onDestroyActionMode(ActionMode mode) {} - } - - private static class BitmapWorkerTask extends AsyncTask { - private final WeakReference imageViewReference; - private final int size; - private Message message = null; - - BitmapWorkerTask(ImageView imageView, int size) { - imageViewReference = new WeakReference<>(imageView); - this.size = size; - } - - @Override - protected Bitmap doInBackground(Message... params) { - this.message = params[0]; - final XmppActivity activity = XmppActivity.find(imageViewReference); - if (activity == null) { - return null; - } - return activity.avatarService().get(this.message, size, 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); - } - } + public void onDestroyActionMode(ActionMode mode) { } } } \ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/ui/util/AvatarWorkerTask.java b/src/main/java/de/pixart/messenger/ui/util/AvatarWorkerTask.java new file mode 100644 index 000000000..afddb7e8b --- /dev/null +++ b/src/main/java/de/pixart/messenger/ui/util/AvatarWorkerTask.java @@ -0,0 +1,111 @@ +package de.pixart.messenger.ui.util; + +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.os.AsyncTask; +import android.support.annotation.DimenRes; +import android.widget.ImageView; + +import java.lang.ref.WeakReference; +import java.util.concurrent.RejectedExecutionException; + +import de.pixart.messenger.services.AvatarService; +import de.pixart.messenger.ui.XmppActivity; + +public class AvatarWorkerTask extends AsyncTask { + private final WeakReference imageViewReference; + private AvatarService.Avatarable avatarable = null; + private @DimenRes + int size; + + public AvatarWorkerTask(ImageView imageView, @DimenRes int size) { + imageViewReference = new WeakReference<>(imageView); + this.size = size; + } + + @Override + protected Bitmap doInBackground(AvatarService.Avatarable... params) { + this.avatarable = params[0]; + final XmppActivity activity = XmppActivity.find(imageViewReference); + if (activity == null) { + return null; + } + return activity.avatarService().get(avatarable, (int) activity.getResources().getDimension(size), 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); + } + } + } + + public static boolean cancelPotentialWork(AvatarService.Avatarable avatarable, ImageView imageView) { + final AvatarWorkerTask workerTask = getBitmapWorkerTask(imageView); + + if (workerTask != null) { + final AvatarService.Avatarable old = workerTask.avatarable; + if (old == null || avatarable != old) { + workerTask.cancel(true); + } else { + return false; + } + } + return true; + } + + public static AvatarWorkerTask getBitmapWorkerTask(ImageView imageView) { + if (imageView != null) { + final Drawable drawable = imageView.getDrawable(); + if (drawable instanceof AsyncDrawable) { + final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable; + return asyncDrawable.getAvatarWorkerTask(); + } + } + return null; + } + + public static void loadAvatar(final AvatarService.Avatarable avatarable, final ImageView imageView, final @DimenRes int size) { + if (cancelPotentialWork(avatarable, imageView)) { + final XmppActivity activity = XmppActivity.find(imageView); + if (activity == null) { + return; + } + final Bitmap bm = activity.avatarService().get(avatarable, (int) activity.getResources().getDimension(size), true); + if (bm != null) { + cancelPotentialWork(avatarable, imageView); + imageView.setImageBitmap(bm); + imageView.setBackgroundColor(0x00000000); + } else { + imageView.setBackgroundColor(avatarable.getAvatarBackgroundColor()); + imageView.setImageDrawable(null); + final AvatarWorkerTask task = new AvatarWorkerTask(imageView, size); + final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task); + imageView.setImageDrawable(asyncDrawable); + try { + task.execute(avatarable); + } catch (final RejectedExecutionException ignored) { + } + } + } + } + + static class AsyncDrawable extends BitmapDrawable { + private final WeakReference avatarWorkerTaskReference; + + AsyncDrawable(Resources res, Bitmap bitmap, AvatarWorkerTask workerTask) { + super(res, bitmap); + avatarWorkerTaskReference = new WeakReference<>(workerTask); + } + + AvatarWorkerTask getAvatarWorkerTask() { + return avatarWorkerTaskReference.get(); + } + } +} diff --git a/src/main/res/values/dimens.xml b/src/main/res/values/dimens.xml index 0c6ef41fb..969d1c8c2 100644 --- a/src/main/res/values/dimens.xml +++ b/src/main/res/values/dimens.xml @@ -28,4 +28,7 @@ 0.12 5dp + 48dp + 56dp + 32dp -- cgit v1.2.3