diff options
author | Christian Schneppe <christian@pix-art.de> | 2019-01-27 10:16:18 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2019-01-27 10:19:46 +0100 |
commit | e859e69674cd13e82fb6363e219b80853e87ef15 (patch) | |
tree | 2114545b2f68e4e9dd882f45df60d5255756fd08 /src/main/java/de/pixart/messenger | |
parent | 7b9ced9fb752ee485b657daf6d827f311ed6e8bc (diff) |
make bitmap worker task static
Diffstat (limited to 'src/main/java/de/pixart/messenger')
7 files changed, 68 insertions, 37 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/XmppActivity.java b/src/main/java/de/pixart/messenger/ui/XmppActivity.java index 87e89ecff..5b714984b 100644 --- a/src/main/java/de/pixart/messenger/ui/XmppActivity.java +++ b/src/main/java/de/pixart/messenger/ui/XmppActivity.java @@ -1122,7 +1122,7 @@ public abstract class XmppActivity extends ActionBarActivity { if (cancelPotentialWork(message, imageView)) { imageView.setBackgroundColor(0xff333333); imageView.setImageDrawable(null); - final BitmapWorkerTask task = new BitmapWorkerTask(this, imageView); + final BitmapWorkerTask task = new BitmapWorkerTask(imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable( getResources(), null, task); imageView.setImageDrawable(asyncDrawable); @@ -1177,11 +1177,9 @@ public abstract class XmppActivity extends ActionBarActivity { static class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; - private final WeakReference<XmppActivity> activity; private Message message = null; - private BitmapWorkerTask(XmppActivity activity, ImageView imageView) { - this.activity = new WeakReference<>(activity); + private BitmapWorkerTask(ImageView imageView) { this.imageViewReference = new WeakReference<>(imageView); } @@ -1192,7 +1190,7 @@ public abstract class XmppActivity extends ActionBarActivity { } message = params[0]; try { - XmppActivity activity = this.activity.get(); + final XmppActivity activity = find(imageViewReference); if (activity != null && activity.xmppConnectionService != null) { return activity.xmppConnectionService.getFileBackend().getThumbnail(message, (int) (activity.metrics.density * 288), false); } else { @@ -1230,6 +1228,18 @@ public abstract class XmppActivity extends ActionBarActivity { } } + public static XmppActivity find(WeakReference<ImageView> viewWeakReference) { + final View view = viewWeakReference.get(); + if (view == null) { + return null; + } + final Context context = view.getContext(); + if (context instanceof XmppActivity) { + return (XmppActivity) context; + } + return null; + } + protected boolean installFromUnknownSourceAllowed() { boolean installFromUnknownSource = false; final PackageManager packageManager = this.getPackageManager(); 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 e060d8828..2624828a8 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/AccountAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/AccountAdapter.java @@ -97,17 +97,21 @@ public class AccountAdapter extends ArrayAdapter<Account> { } } - class BitmapWorkerTask extends AsyncTask<Account, Void, Bitmap> { + private static class BitmapWorkerTask extends AsyncTask<Account, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; private Account account = null; - public BitmapWorkerTask(ImageView imageView) { + 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()); } @@ -123,7 +127,7 @@ public class AccountAdapter extends ArrayAdapter<Account> { } } - public void loadAvatar(Account account, ImageView imageView) { + 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) { @@ -149,7 +153,7 @@ public class AccountAdapter extends ArrayAdapter<Account> { void onClickTglAccountState(Account account, boolean state); } - public static boolean cancelPotentialWork(Account account, ImageView imageView) { + private static boolean cancelPotentialWork(Account account, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { @@ -177,12 +181,12 @@ public class AccountAdapter extends ArrayAdapter<Account> { static class AsyncDrawable extends BitmapDrawable { private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference; - public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { + AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { super(res, bitmap); bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); } - public BitmapWorkerTask getBitmapWorkerTask() { + 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 a60631523..e97a8d407 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java @@ -390,33 +390,38 @@ public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapte static class AsyncDrawable extends BitmapDrawable { private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference; - public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { + AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { super(res, bitmap); bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); } - public BitmapWorkerTask getBitmapWorkerTask() { + BitmapWorkerTask getBitmapWorkerTask() { return bitmapWorkerTaskReference.get(); } } - class BitmapWorkerTask extends AsyncTask<Conversation, Void, Bitmap> { + static class BitmapWorkerTask extends AsyncTask<Conversation, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; private Conversation conversation = null; - public BitmapWorkerTask(ImageView imageView) { + 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) { + if (bitmap != null && !isCancelled()) { final ImageView imageView = imageViewReference.get(); if (imageView != null) { imageView.setImageBitmap(bitmap); 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 e99353f13..71f8aaa34 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/ListItemAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/ListItemAdapter.java @@ -133,17 +133,21 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> { void onTagClicked(String tag); } - class BitmapWorkerTask extends AsyncTask<ListItem, Void, Bitmap> { + private static class BitmapWorkerTask extends AsyncTask<ListItem, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; private ListItem item = null; - public BitmapWorkerTask(ImageView imageView) { + 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()); } @@ -159,7 +163,7 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> { } } - public void loadAvatar(ListItem item, ImageView imageView) { + 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) { @@ -181,7 +185,7 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> { } } - public static boolean cancelPotentialWork(ListItem item, ImageView imageView) { + private static boolean cancelPotentialWork(ListItem item, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { @@ -209,12 +213,12 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> { static class AsyncDrawable extends BitmapDrawable { private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference; - public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { + AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { super(res, bitmap); bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); } - public BitmapWorkerTask getBitmapWorkerTask() { + BitmapWorkerTask getBitmapWorkerTask() { return bitmapWorkerTaskReference.get(); } } diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MediaAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MediaAdapter.java index 8b8c05d05..50460c0f1 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MediaAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MediaAdapter.java @@ -89,7 +89,7 @@ public class MediaAdapter extends RecyclerView.Adapter<MediaAdapter.MediaViewHol return attr; } - public static void renderPreview(Context context, Attachment attachment, ImageView imageView) { + static void renderPreview(Context context, Attachment attachment, ImageView imageView) { imageView.setBackgroundColor(StyledAttributes.getColor(context, R.attr.color_background_tertiary)); imageView.setImageAlpha(Math.round(StyledAttributes.getFloat(context, R.attr.icon_alpha) * 255)); imageView.setImageDrawable(StyledAttributes.getDrawable(context, getImageAttr(attachment))); @@ -161,7 +161,7 @@ public class MediaAdapter extends RecyclerView.Adapter<MediaAdapter.MediaViewHol } else { imageView.setBackgroundColor(0xff333333); imageView.setImageDrawable(null); - final BitmapWorkerTask task = new BitmapWorkerTask(imageView); + final BitmapWorkerTask task = new BitmapWorkerTask(mediaSize, imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task); imageView.setImageDrawable(asyncDrawable); try { @@ -200,17 +200,23 @@ public class MediaAdapter extends RecyclerView.Adapter<MediaAdapter.MediaViewHol } } - class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> { + private static class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; private Attachment attachment = null; + private final int mediaSize; - BitmapWorkerTask(ImageView imageView) { + BitmapWorkerTask(int mediaSize, ImageView imageView) { + this.mediaSize = mediaSize; imageViewReference = new WeakReference<>(imageView); } @Override protected Bitmap doInBackground(Attachment... params) { this.attachment = params[0]; + final XmppActivity activity = XmppActivity.find(imageViewReference); + if (activity == null) { + return null; + } return activity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, mediaSize, false); } diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MediaPreviewAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MediaPreviewAdapter.java index 64cb63439..2c7ce7471 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MediaPreviewAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MediaPreviewAdapter.java @@ -158,7 +158,7 @@ public class MediaPreviewAdapter extends RecyclerView.Adapter<MediaPreviewAdapte } } - class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> { + private static class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; private Attachment attachment = null; @@ -168,14 +168,12 @@ public class MediaPreviewAdapter extends RecyclerView.Adapter<MediaPreviewAdapte @Override protected Bitmap doInBackground(Attachment... params) { - Activity activity = conversationFragment.getActivity(); - if (activity instanceof XmppActivity) { - final XmppActivity xmppActivity = (XmppActivity) activity; - this.attachment = params[0]; - return xmppActivity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, Math.round(xmppActivity.getResources().getDimension(R.dimen.media_preview_size)), false); - } else { + this.attachment = params[0]; + final XmppActivity activity = XmppActivity.find(imageViewReference); + if (activity == null) { return null; } + return activity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, Math.round(activity.getResources().getDimension(R.dimen.media_preview_size)), false); } @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 9d7624e79..f89d1a7c9 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -1123,7 +1123,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie ViewUtil.view(activity, file); } - public void showLocation(Message message) { + private void showLocation(Message message) { for (Intent intent : GeoHelper.createGeoIntentsFromMessage(this.getContext(), message)) { if (intent.resolveActivity(getContext().getPackageManager()) != null) { getContext().startActivity(intent); @@ -1139,7 +1139,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie this.mIndicateReceived = p.getBoolean("indicate_received", activity.getResources().getBoolean(R.bool.indicate_received)); } - public void loadAvatar(Message message, ImageView imageView, int size) { + 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) { @@ -1260,12 +1260,12 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie public void onDestroyActionMode(ActionMode mode) {} } - class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> { + private static class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; private final int size; private Message message = null; - public BitmapWorkerTask(ImageView imageView, int size) { + BitmapWorkerTask(ImageView imageView, int size) { imageViewReference = new WeakReference<>(imageView); this.size = size; } @@ -1273,6 +1273,10 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie @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()); } |