diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-03-29 23:44:59 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-03-29 23:44:59 +0200 |
commit | 255406a86fd8bfa757b39b07c1adac7cfe493cc5 (patch) | |
tree | 0a3496028ff53c70bfa4f29b839aebec8c8a9119 | |
parent | 0e597dcc357703317c23d6ef2c1780d7fd3f789f (diff) |
made download button in message adapter work
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/ConversationFragment.java | 12 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java | 339 |
2 files changed, 154 insertions, 197 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index 7e9fb1aec..d056194a5 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -1707,6 +1707,18 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke activity.xmppConnectionService.getHttpConnectionManager().createNewDownloadConnection(message, true); } + public static void downloadFile(Activity activity, Message message) { + Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment); + if (fragment != null && fragment instanceof ConversationFragment) { + ((ConversationFragment) fragment).downloadFile(message); + return; + } + fragment = activity.getFragmentManager().findFragmentById(R.id.secondary_fragment); + if (fragment != null && fragment instanceof ConversationFragment) { + ((ConversationFragment) fragment).downloadFile(message); + } + } + private void cancelTransmission(Message message) { Transferable transferable = message.getTransferable(); if (transferable != null) { 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 ba240192f..defaffa99 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -34,7 +34,6 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; -import android.view.View.OnLongClickListener; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.ArrayAdapter; @@ -93,12 +92,12 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie ConversationFragment mConversationFragment; + public static final String DATE_SEPARATOR_BODY = "DATE_SEPARATOR"; private static final int SENT = 0; private static final int RECEIVED = 1; private static final int STATUS = 2; - private static final int DATE_SEPARATOR = 3; - public static final String DATE_SEPARATOR_BODY = "DATE_SEPARATOR"; + private static final int DATE_SEPARATOR = 3; private static final Pattern XMPP_PATTERN = Pattern .compile("xmpp\\:(?:(?:[" @@ -137,17 +136,14 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie }; private final XmppActivity activity; - + private final ListSelectionManager listSelectionManager = new ListSelectionManager(); + public final AudioPlayer audioPlayer; private DisplayMetrics metrics; - private OnContactPictureClicked mOnContactPictureClickedListener; private OnContactPictureLongClicked mOnContactPictureLongClickedListener; - private boolean mIndicateReceived = false; - private OnQuoteListener onQuoteListener; - private final ListSelectionManager listSelectionManager = new ListSelectionManager(); - public final AudioPlayer audioPlayer; private boolean mUseWhiteBackground = false; + private OnQuoteListener onQuoteListener; public MessageAdapter(XmppActivity activity, List<Message> messages) { super(activity, 0, messages); @@ -157,6 +153,31 @@ public class MessageAdapter extends ArrayAdapter<Message> 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; + } + public void flagScreenOn() { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } @@ -402,14 +423,10 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie viewHolder.download_button.setVisibility(View.VISIBLE); viewHolder.download_button.setText(add_contact); viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_account_card_details_grey600_48dp, 0, 0, 0); - viewHolder.download_button.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(Uri.parse(body)); - activity.startActivity(intent); - } + viewHolder.download_button.setOnClickListener(v -> { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse(body)); + activity.startActivity(intent); }); viewHolder.image.setVisibility(View.GONE); viewHolder.messageBody.setVisibility(View.GONE); @@ -597,13 +614,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie viewHolder.download_button.setVisibility(View.VISIBLE); viewHolder.download_button.setText(text); viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_download_grey600_48dp, 0, 0, 0); - viewHolder.download_button.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - mConversationFragment.startDownloadable(message); - } - }); + viewHolder.download_button.setOnClickListener(v -> ConversationFragment.downloadFile(activity, message)); } private void displayOpenableMessage(ViewHolder viewHolder, final Message message) { @@ -636,13 +647,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie viewHolder.download_button.setText(activity.getString(R.string.open_x_file, UIHelper.getFileDescriptionString(activity, message))); } } - viewHolder.download_button.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - openDownloadable(message); - } - }); + viewHolder.download_button.setOnClickListener(v -> openDownloadable(message)); } private void showAPK(final Message message, ViewHolder viewHolder) { @@ -696,13 +701,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(scaledW, scaledH); layoutParams.setMargins(0, (int) (metrics.density * 4), 0, (int) (metrics.density * 4)); viewHolder.image.setLayoutParams(layoutParams); - viewHolder.image.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - showLocation(message); - } - }); + viewHolder.image.setOnClickListener(v -> showLocation(message)); Glide .with(activity) .load(Uri.parse(url)) @@ -717,13 +716,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie viewHolder.download_button.setVisibility(View.GONE); viewHolder.download_button.setText(R.string.show_location); viewHolder.download_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_map_marker_grey600_48dp, 0, 0, 0); - viewHolder.download_button.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - showLocation(message); - } - }); + viewHolder.download_button.setOnClickListener(v -> showLocation(message)); } @@ -763,13 +756,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie layoutParams.setMargins(0, (int) (metrics.density * 4), 0, (int) (metrics.density * 4)); viewHolder.image.setLayoutParams(layoutParams); activity.loadBitmap(message, viewHolder.image); - viewHolder.image.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - openDownloadable(message); - } - }); + viewHolder.image.setOnClickListener(v -> openDownloadable(message)); } private void loadMoreMessages(Conversation conversation) { @@ -901,29 +888,21 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie } viewHolder.contact_picture - .setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - if (MessageAdapter.this.mOnContactPictureClickedListener != null) { - MessageAdapter.this.mOnContactPictureClickedListener - .onContactPictureClicked(message); - } - + .setOnClickListener(v -> { + if (MessageAdapter.this.mOnContactPictureClickedListener != null) { + MessageAdapter.this.mOnContactPictureClickedListener + .onContactPictureClicked(message); } + }); viewHolder.contact_picture - .setOnLongClickListener(new OnLongClickListener() { - - @Override - public boolean onLongClick(View v) { - if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) { - MessageAdapter.this.mOnContactPictureLongClickedListener - .onContactPictureLongClicked(message); - return true; - } else { - return false; - } + .setOnLongClickListener(v -> { + if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) { + MessageAdapter.this.mOnContactPictureLongClickedListener + .onContactPictureLongClicked(message); + return true; + } else { + return false; } }); @@ -1066,55 +1045,6 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie audioPlayer.stop(); } - public interface OnQuoteListener { - public void onQuote(String text); - } - - private class MessageBodyActionModeCallback implements ActionMode.Callback { - - private final TextView textView; - - public MessageBodyActionModeCallback(TextView textView) { - this.textView = textView; - } - - @Override - public boolean onCreateActionMode(ActionMode mode, Menu menu) { - if (onQuoteListener != null) { - int quoteResId = activity.getThemeResource(R.attr.icon_quote, R.drawable.ic_action_reply); - // 3rd item is placed after "copy" item - menu.add(0, android.R.id.button1, 3, R.string.quote).setIcon(quoteResId) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); - } - return false; - } - - @Override - public boolean onPrepareActionMode(ActionMode mode, Menu menu) { - return false; - } - - @Override - public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - if (item.getItemId() == android.R.id.button1) { - int start = textView.getSelectionStart(); - int end = textView.getSelectionEnd(); - if (end > start) { - String text = transformText(textView.getText(), start, end, false); - if (onQuoteListener != null) { - onQuoteListener.onQuote(text); - } - mode.finish(); - } - return true; - } - return false; - } - - @Override - public void onDestroyActionMode(ActionMode mode) {} - } - public void openDownloadable(Message message) { DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message); if (!file.exists()) { @@ -1183,13 +1113,35 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie this.mUseWhiteBackground = p.getBoolean("use_white_background", activity.getResources().getBoolean(R.bool.use_white_background)); } - public TextView getMessageBody(View view) { - final Object tag = view.getTag(); - if (tag instanceof ViewHolder) { - final ViewHolder viewHolder = (ViewHolder) tag; - return viewHolder.messageBody; + public 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) { + } + } } - return null; + } + + public interface OnQuoteListener { + void onQuote(String text); } public interface OnContactPictureClicked { @@ -1202,6 +1154,9 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie private static class ViewHolder { + public Button load_more_messages; + public ImageView edit_indicator; + public RelativeLayout audioPlayer; protected LinearLayout message_box; protected Button download_button; protected Button resend_button; @@ -1214,15 +1169,70 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie protected ImageView contact_picture; protected TextView status_message; protected TextView encryption; - public Button load_more_messages; - public ImageView edit_indicator; - public RelativeLayout audioPlayer; + } + + 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(); + } + } + + private class MessageBodyActionModeCallback implements ActionMode.Callback { + + private final TextView textView; + + public MessageBodyActionModeCallback(TextView textView) { + this.textView = textView; + } + + @Override + public boolean onCreateActionMode(ActionMode mode, Menu menu) { + if (onQuoteListener != null) { + int quoteResId = activity.getThemeResource(R.attr.icon_quote, R.drawable.ic_action_reply); + // 3rd item is placed after "copy" item + menu.add(0, android.R.id.button1, 3, R.string.quote).setIcon(quoteResId) + .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + } + return false; + } + + @Override + public boolean onPrepareActionMode(ActionMode mode, Menu menu) { + return false; + } + + @Override + public boolean onActionItemClicked(ActionMode mode, MenuItem item) { + if (item.getItemId() == android.R.id.button1) { + int start = textView.getSelectionStart(); + int end = textView.getSelectionEnd(); + if (end > start) { + String text = transformText(textView.getText(), start, end, false); + if (onQuoteListener != null) { + onQuoteListener.onQuote(text); + } + mode.finish(); + } + return true; + } + return false; + } + + @Override + public void onDestroyActionMode(ActionMode mode) {} } class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; - private Message message = null; private final int size; + private Message message = null; public BitmapWorkerTask(ImageView imageView, int size) { imageViewReference = new WeakReference<>(imageView); @@ -1246,69 +1256,4 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie } } } - - public 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 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; - } - - 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(); - } - } -} +}
\ No newline at end of file |