made download button in message adapter work

This commit is contained in:
Daniel Gultsch 2018-02-24 20:34:14 +01:00
parent e93b06f45c
commit e0317cfa14
2 changed files with 213 additions and 257 deletions

View file

@ -1471,6 +1471,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) {

View file

@ -63,6 +63,7 @@ import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.MessageArchiveService;
import eu.siacs.conversations.services.NotificationService;
import eu.siacs.conversations.ui.ConversationFragment;
import eu.siacs.conversations.ui.XmppActivity;
import eu.siacs.conversations.ui.service.AudioPlayer;
import eu.siacs.conversations.ui.text.DividerSpan;
@ -83,13 +84,11 @@ import eu.siacs.conversations.xmpp.mam.MamReference;
public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextView.CopyHandler {
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 Pattern XMPP_PATTERN = Pattern
.compile("xmpp\\:(?:(?:["
+ Patterns.GOOD_IRI_CHAR
@ -126,21 +125,15 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
};
private final XmppActivity activity;
private final ListSelectionManager listSelectionManager = new ListSelectionManager();
private final AudioPlayer audioPlayer;
private DisplayMetrics metrics;
private OnContactPictureClicked mOnContactPictureClickedListener;
private OnContactPictureLongClicked mOnContactPictureLongClickedListener;
private boolean mIndicateReceived = false;
private boolean mUseGreenBackground = false;
private OnQuoteListener onQuoteListener;
private final ListSelectionManager listSelectionManager = new ListSelectionManager();
private final AudioPlayer audioPlayer;
public MessageAdapter(XmppActivity activity, List<Message> messages) {
super(activity, 0, messages);
this.audioPlayer = new AudioPlayer(this);
@ -149,6 +142,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);
}
@ -531,14 +549,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
viewHolder.audioPlayer.setVisibility(View.GONE);
viewHolder.download_button.setVisibility(View.VISIBLE);
viewHolder.download_button.setText(text);
viewHolder.download_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//TODO make proper reference to fragment
//activity.startDownloadable(message);
}
});
viewHolder.download_button.setOnClickListener(v -> ConversationFragment.downloadFile(activity, message));
}
private void displayOpenableMessage(ViewHolder viewHolder, final Message message) {
@ -547,13 +558,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
viewHolder.audioPlayer.setVisibility(View.GONE);
viewHolder.download_button.setVisibility(View.VISIBLE);
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 displayLocationMessage(ViewHolder viewHolder, final Message message) {
@ -562,13 +567,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
viewHolder.audioPlayer.setVisibility(View.GONE);
viewHolder.download_button.setVisibility(View.VISIBLE);
viewHolder.download_button.setText(R.string.show_location);
viewHolder.download_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showLocation(message);
}
});
viewHolder.download_button.setOnClickListener(v -> showLocation(message));
}
private void displayAudioMessage(ViewHolder viewHolder, Message message, boolean darkBackground) {
@ -581,7 +580,6 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
this.audioPlayer.init(audioPlayer, message);
}
private void displayImageMessage(ViewHolder viewHolder, final Message message) {
viewHolder.download_button.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.GONE);
@ -608,13 +606,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) {
@ -719,12 +711,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
viewHolder.status_message.setVisibility(View.GONE);
viewHolder.contact_picture.setVisibility(View.GONE);
viewHolder.load_more_messages.setVisibility(View.VISIBLE);
viewHolder.load_more_messages.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
loadMoreMessages(message.getConversation());
}
});
viewHolder.load_more_messages.setOnClickListener(v -> loadMoreMessages(message.getConversation()));
} else {
viewHolder.status_message.setVisibility(View.VISIBLE);
viewHolder.load_more_messages.setVisibility(View.GONE);
@ -751,23 +738,14 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
loadAvatar(message, viewHolder.contact_picture, activity.getPixel(48));
}
viewHolder.contact_picture
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.contact_picture.setOnClickListener(v -> {
if (MessageAdapter.this.mOnContactPictureClickedListener != null) {
MessageAdapter.this.mOnContactPictureClickedListener
.onContactPictureClicked(message);
}
}
});
viewHolder.contact_picture
.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
viewHolder.contact_picture.setOnLongClickListener(v -> {
if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) {
MessageAdapter.this.mOnContactPictureLongClickedListener
.onContactPictureLongClicked(message);
@ -775,7 +753,6 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
} else {
return false;
}
}
});
final Transferable transferable = message.getTransferable();
@ -912,55 +889,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()) {
@ -1010,68 +938,6 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
this.mUseGreenBackground = p.getBoolean("use_green_background", activity.getResources().getBoolean(R.bool.use_green_background));
}
public TextView getMessageBody(View view) {
final Object tag = view.getTag();
if (tag instanceof ViewHolder) {
final ViewHolder viewHolder = (ViewHolder) tag;
return viewHolder.messageBody;
}
return null;
}
public interface OnContactPictureClicked {
void onContactPictureClicked(Message message);
}
public interface OnContactPictureLongClicked {
void onContactPictureLongClicked(Message message);
}
private static class ViewHolder {
protected LinearLayout message_box;
protected Button download_button;
protected ImageView image;
protected ImageView indicator;
protected ImageView indicatorReceived;
protected TextView time;
protected CopyTextView messageBody;
protected ImageView contact_picture;
protected TextView status_message;
protected TextView encryption;
public Button load_more_messages;
public ImageView edit_indicator;
public RelativeLayout audioPlayer;
}
class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private Message message = null;
private final int size;
public BitmapWorkerTask(ImageView imageView, int size) {
imageViewReference = new WeakReference<>(imageView);
this.size = size;
}
@Override
protected Bitmap doInBackground(Message... params) {
this.message = params[0];
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 loadAvatar(Message message, ImageView imageView, int size) {
if (cancelPotentialWork(message, imageView)) {
final Bitmap bm = activity.avatarService().get(message, size, true);
@ -1099,29 +965,33 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
}
}
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;
public interface OnQuoteListener {
void onQuote(String text);
}
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();
public interface OnContactPictureClicked {
void onContactPictureClicked(Message message);
}
public interface OnContactPictureLongClicked {
void onContactPictureLongClicked(Message message);
}
return null;
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 ImageView image;
protected ImageView indicator;
protected ImageView indicatorReceived;
protected TextView time;
protected CopyTextView messageBody;
protected ImageView contact_picture;
protected TextView status_message;
protected TextView encryption;
}
static class AsyncDrawable extends BitmapDrawable {
@ -1136,4 +1006,78 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
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 final int size;
private Message message = null;
public BitmapWorkerTask(ImageView imageView, int size) {
imageViewReference = new WeakReference<>(imageView);
this.size = size;
}
@Override
protected Bitmap doInBackground(Message... params) {
this.message = params[0];
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);
}
}
}
}
}