conversations-classic/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java

847 lines
31 KiB
Java
Raw Normal View History

package eu.siacs.conversations.ui.adapter;
import android.content.ActivityNotFoundException;
import android.content.Intent;
2014-11-15 14:16:40 +00:00
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
2014-11-13 20:04:05 +00:00
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v4.content.FileProvider;
2014-08-22 11:22:07 +00:00
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
2014-08-22 11:22:07 +00:00
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
2014-08-22 11:22:07 +00:00
import android.text.style.StyleSpan;
import android.text.util.Linkify;
import android.util.DisplayMetrics;
import android.util.Patterns;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
2014-11-07 14:38:20 +00:00
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.ref.WeakReference;
2016-03-04 20:30:34 +00:00
import java.net.URL;
2014-11-07 14:38:20 +00:00
import java.util.List;
import java.util.concurrent.RejectedExecutionException;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2014-11-07 14:38:20 +00:00
import eu.siacs.conversations.Config;
2014-11-07 14:38:20 +00:00
import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
2014-12-03 09:35:30 +00:00
import eu.siacs.conversations.entities.Account;
2014-11-07 14:38:20 +00:00
import eu.siacs.conversations.entities.Conversation;
2014-11-13 20:04:05 +00:00
import eu.siacs.conversations.entities.DownloadableFile;
2014-11-07 14:38:20 +00:00
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.Message.FileParams;
2015-07-20 12:26:29 +00:00
import eu.siacs.conversations.entities.Transferable;
2014-11-07 14:38:20 +00:00
import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.ui.widget.ClickableMovementMethod;
import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.GeoHelper;
2014-11-07 14:38:20 +00:00
import eu.siacs.conversations.utils.UIHelper;
public class MessageAdapter extends ArrayAdapter<Message> {
private static final int SENT = 0;
2014-08-28 14:49:52 +00:00
private static final int RECEIVED = 1;
private static final int STATUS = 2;
private static final Pattern XMPP_PATTERN = Pattern
.compile("xmpp\\:(?:(?:["
+ Patterns.GOOD_IRI_CHAR
+ "\\;\\/\\?\\@\\&\\=\\#\\~\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])"
+ "|(?:\\%[a-fA-F0-9]{2}))+");
private ConversationActivity activity;
private DisplayMetrics metrics;
private OnContactPictureClicked mOnContactPictureClickedListener;
private OnContactPictureLongClicked mOnContactPictureLongClickedListener;
private OnLongClickListener openContextMenu = new OnLongClickListener() {
2015-01-11 15:22:29 +00:00
@Override
public boolean onLongClick(View v) {
v.showContextMenu();
return true;
}
};
private boolean mIndicateReceived = false;
private boolean mUseGreenBackground = false;
2015-01-11 15:22:29 +00:00
public MessageAdapter(ConversationActivity activity, List<Message> messages) {
super(activity, 0, messages);
this.activity = activity;
metrics = getContext().getResources().getDisplayMetrics();
updatePreferences();
}
public void setOnContactPictureClicked(OnContactPictureClicked listener) {
this.mOnContactPictureClickedListener = listener;
}
2014-08-31 14:28:21 +00:00
public void setOnContactPictureLongClicked(
OnContactPictureLongClicked listener) {
this.mOnContactPictureLongClickedListener = listener;
2015-01-11 15:22:29 +00:00
}
@Override
public int getViewTypeCount() {
return 3;
}
2015-07-19 12:26:03 +00:00
public int getItemViewType(Message message) {
if (message.getType() == Message.TYPE_STATUS) {
return STATUS;
2015-07-19 12:26:03 +00:00
} else if (message.getStatus() <= Message.STATUS_RECEIVED) {
2014-08-28 14:49:52 +00:00
return RECEIVED;
}
2015-07-19 12:26:03 +00:00
return SENT;
}
@Override
public int getItemViewType(int position) {
return this.getItemViewType(getItem(position));
}
private int getMessageTextColor(boolean onDark, boolean primary) {
if (onDark) {
2015-07-30 10:40:50 +00:00
return activity.getResources().getColor(primary ? R.color.white : R.color.white70);
} else {
return activity.getResources().getColor(primary ? R.color.black87 : R.color.black54);
2015-07-19 12:26:03 +00:00
}
}
private void displayStatus(ViewHolder viewHolder, Message message, int type, boolean darkBackground) {
String filesize = null;
String info = null;
boolean error = false;
if (viewHolder.indicatorReceived != null) {
viewHolder.indicatorReceived.setVisibility(View.GONE);
}
if (viewHolder.edit_indicator != null) {
if (message.edited()) {
viewHolder.edit_indicator.setVisibility(View.VISIBLE);
viewHolder.edit_indicator.setImageResource(darkBackground ? R.drawable.ic_mode_edit_white_18dp : R.drawable.ic_mode_edit_black_18dp);
viewHolder.edit_indicator.setAlpha(darkBackground ? 0.7f : 0.57f);
} else {
viewHolder.edit_indicator.setVisibility(View.GONE);
}
}
boolean multiReceived = message.getConversation().getMode() == Conversation.MODE_MULTI
2015-01-11 15:22:29 +00:00
&& message.getMergedStatus() <= Message.STATUS_RECEIVED;
2015-07-10 13:11:03 +00:00
if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE || message.getTransferable() != null) {
FileParams params = message.getFileParams();
if (params.size > (1.5 * 1024 * 1024)) {
filesize = params.size / (1024 * 1024)+ " MiB";
} else if (params.size > 0) {
filesize = params.size / 1024 + " KiB";
2014-10-14 17:33:35 +00:00
}
2015-07-10 13:11:03 +00:00
if (message.getTransferable() != null && message.getTransferable().getStatus() == Transferable.STATUS_FAILED) {
2014-10-29 10:31:03 +00:00
error = true;
}
}
switch (message.getMergedStatus()) {
2015-01-11 15:22:29 +00:00
case Message.STATUS_WAITING:
info = getContext().getString(R.string.waiting);
break;
case Message.STATUS_UNSEND:
2015-07-10 13:11:03 +00:00
Transferable d = message.getTransferable();
2015-01-11 15:22:29 +00:00
if (d!=null) {
info = getContext().getString(R.string.sending_file,d.getProgress());
} else {
2015-01-11 15:22:29 +00:00
info = getContext().getString(R.string.sending);
}
2015-01-11 15:22:29 +00:00
break;
case Message.STATUS_OFFERED:
info = getContext().getString(R.string.offering);
break;
case Message.STATUS_SEND_RECEIVED:
if (mIndicateReceived) {
2015-01-11 15:22:29 +00:00
viewHolder.indicatorReceived.setVisibility(View.VISIBLE);
}
break;
case Message.STATUS_SEND_DISPLAYED:
if (mIndicateReceived) {
2015-01-11 15:22:29 +00:00
viewHolder.indicatorReceived.setVisibility(View.VISIBLE);
}
break;
case Message.STATUS_SEND_FAILED:
info = getContext().getString(R.string.send_failed);
error = true;
break;
default:
if (multiReceived) {
info = UIHelper.getMessageDisplayName(message);
2015-01-11 15:22:29 +00:00
}
break;
}
2015-07-30 10:40:50 +00:00
if (error && type == SENT) {
viewHolder.time.setTextColor(activity.getWarningTextColor());
} else {
viewHolder.time.setTextColor(this.getMessageTextColor(darkBackground,false));
}
if (message.getEncryption() == Message.ENCRYPTION_NONE) {
viewHolder.indicator.setVisibility(View.GONE);
} else {
viewHolder.indicator.setImageResource(darkBackground ? R.drawable.ic_lock_white_18dp : R.drawable.ic_lock_black_18dp);
viewHolder.indicator.setVisibility(View.VISIBLE);
if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
XmppAxolotlSession.Trust trust = message.getConversation()
.getAccount().getAxolotlService().getFingerprintTrust(
2016-03-31 19:15:49 +00:00
message.getFingerprint());
if(trust == null || (!trust.trusted() && !trust.trustedInactive())) {
2015-07-30 10:40:50 +00:00
viewHolder.indicator.setColorFilter(activity.getWarningTextColor());
viewHolder.indicator.setAlpha(1.0f);
} else {
viewHolder.indicator.clearColorFilter();
if (darkBackground) {
2015-07-30 10:40:50 +00:00
viewHolder.indicator.setAlpha(0.7f);
} else {
viewHolder.indicator.setAlpha(0.57f);
2015-07-30 10:40:50 +00:00
}
}
} else {
viewHolder.indicator.clearColorFilter();
if (darkBackground) {
2015-07-30 10:40:50 +00:00
viewHolder.indicator.setAlpha(0.7f);
} else {
viewHolder.indicator.setAlpha(0.57f);
2015-07-30 10:40:50 +00:00
}
}
}
String formatedTime = UIHelper.readableTimeDifferenceFull(getContext(),
message.getMergedTimeSent());
2014-08-28 09:01:24 +00:00
if (message.getStatus() <= Message.STATUS_RECEIVED) {
if ((filesize != null) && (info != null)) {
viewHolder.time.setText(formatedTime + " \u00B7 " + filesize +" \u00B7 " + info);
} else if ((filesize == null) && (info != null)) {
viewHolder.time.setText(formatedTime + " \u00B7 " + info);
} else if ((filesize != null) && (info == null)) {
viewHolder.time.setText(formatedTime + " \u00B7 " + filesize);
} else {
viewHolder.time.setText(formatedTime);
}
} else {
if ((filesize != null) && (info != null)) {
viewHolder.time.setText(filesize + " \u00B7 " + info);
} else if ((filesize == null) && (info != null)) {
if (error) {
viewHolder.time.setText(info + " \u00B7 " + formatedTime);
} else {
viewHolder.time.setText(info);
}
} else if ((filesize != null) && (info == null)) {
viewHolder.time.setText(filesize + " \u00B7 " + formatedTime);
} else {
viewHolder.time.setText(formatedTime);
}
}
}
private void displayInfoMessage(ViewHolder viewHolder, String text, boolean darkBackground) {
if (viewHolder.download_button != null) {
viewHolder.download_button.setVisibility(View.GONE);
}
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.VISIBLE);
2014-11-13 20:04:05 +00:00
viewHolder.messageBody.setText(text);
viewHolder.messageBody.setTextColor(getMessageTextColor(darkBackground, false));
viewHolder.messageBody.setTypeface(null, Typeface.ITALIC);
viewHolder.messageBody.setTextIsSelectable(false);
}
private void displayDecryptionFailed(ViewHolder viewHolder, boolean darkBackground) {
if (viewHolder.download_button != null) {
viewHolder.download_button.setVisibility(View.GONE);
}
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.VISIBLE);
viewHolder.messageBody.setText(getContext().getString(
R.string.decryption_failed));
viewHolder.messageBody.setTextColor(getMessageTextColor(darkBackground, false));
viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
viewHolder.messageBody.setTextIsSelectable(false);
}
2015-05-11 06:45:38 +00:00
private void displayHeartMessage(final ViewHolder viewHolder, final String body) {
if (viewHolder.download_button != null) {
viewHolder.download_button.setVisibility(View.GONE);
}
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.VISIBLE);
2015-05-11 06:45:38 +00:00
viewHolder.messageBody.setIncludeFontPadding(false);
Spannable span = new SpannableString(body);
span.setSpan(new RelativeSizeSpan(4.0f), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new ForegroundColorSpan(activity.getWarningTextColor()), 0, body.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
viewHolder.messageBody.setText(span);
}
private void displayTextMessage(final ViewHolder viewHolder, final Message message, boolean darkBackground, int type) {
if (viewHolder.download_button != null) {
viewHolder.download_button.setVisibility(View.GONE);
}
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.VISIBLE);
2015-05-11 06:45:38 +00:00
viewHolder.messageBody.setIncludeFontPadding(true);
if (message.getBody() != null) {
final String nick = UIHelper.getMessageDisplayName(message);
2015-10-02 09:58:03 +00:00
String body;
try {
body = message.getMergedBody().replaceAll("^" + Message.ME_COMMAND, nick + " ");
} catch (ArrayIndexOutOfBoundsException e) {
body = message.getMergedBody();
}
if (body.length() > Config.MAX_DISPLAY_MESSAGE_CHARS) {
2016-05-28 23:14:45 +00:00
body = body.substring(0, Config.MAX_DISPLAY_MESSAGE_CHARS)+"\u2026";
}
Spannable formattedBody = new SpannableString(body);
int i = body.indexOf(Message.MERGE_SEPARATOR);
while(i >= 0) {
final int end = i + Message.MERGE_SEPARATOR.length();
formattedBody.setSpan(new RelativeSizeSpan(0.3f),i,end,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
i = body.indexOf(Message.MERGE_SEPARATOR,end);
}
if (message.getType() != Message.TYPE_PRIVATE) {
2015-01-11 15:22:29 +00:00
if (message.hasMeCommand()) {
formattedBody.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, nick.length(),
2015-01-11 15:22:29 +00:00
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else {
2014-08-22 11:22:07 +00:00
String privateMarker;
2014-08-28 09:01:24 +00:00
if (message.getStatus() <= Message.STATUS_RECEIVED) {
2014-08-31 14:28:21 +00:00
privateMarker = activity
2015-01-11 15:22:29 +00:00
.getString(R.string.private_message);
} else {
final String to;
if (message.getCounterpart() != null) {
to = message.getCounterpart().getResourcepart();
} else {
to = "";
}
2014-11-07 14:09:28 +00:00
privateMarker = activity.getString(R.string.private_message_to, to);
}
formattedBody = new SpannableStringBuilder().append(privateMarker).append(' ').append(formattedBody);
formattedBody.setSpan(new ForegroundColorSpan(getMessageTextColor(darkBackground,false)), 0, privateMarker
2015-01-11 15:22:29 +00:00
.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
formattedBody.setSpan(new StyleSpan(Typeface.BOLD), 0,
2014-08-31 14:28:21 +00:00
privateMarker.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
2015-01-11 15:22:29 +00:00
if (message.hasMeCommand()) {
formattedBody.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), privateMarker.length() + 1,
2015-01-11 15:22:29 +00:00
privateMarker.length() + 1 + nick.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
Linkify.addLinks(formattedBody, Linkify.WEB_URLS);
Linkify.addLinks(formattedBody, XMPP_PATTERN, "xmpp");
Linkify.addLinks(formattedBody, GeoHelper.GEO_URI, "geo");
viewHolder.messageBody.setAutoLinkMask(0);
viewHolder.messageBody.setText(formattedBody);
viewHolder.messageBody.setTextIsSelectable(true);
viewHolder.messageBody.setMovementMethod(ClickableMovementMethod.getInstance());
} else {
viewHolder.messageBody.setText("");
viewHolder.messageBody.setTextIsSelectable(false);
}
viewHolder.messageBody.setTextColor(this.getMessageTextColor(darkBackground, true));
viewHolder.messageBody.setLinkTextColor(this.getMessageTextColor(darkBackground, true));
viewHolder.messageBody.setHighlightColor(activity.getResources().getColor(darkBackground ? (type == SENT || !mUseGreenBackground ? R.color.black26 : R.color.grey800) : R.color.grey500));
viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
viewHolder.messageBody.setOnLongClickListener(openContextMenu);
}
private void displayDownloadableMessage(ViewHolder viewHolder,
2014-11-13 20:04:05 +00:00
final Message message, String text) {
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.GONE);
viewHolder.download_button.setVisibility(View.VISIBLE);
2014-11-13 20:04:05 +00:00
viewHolder.download_button.setText(text);
viewHolder.download_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
activity.startDownloadable(message);
}
});
viewHolder.download_button.setOnLongClickListener(openContextMenu);
}
2014-11-13 20:04:05 +00:00
private void displayOpenableMessage(ViewHolder viewHolder,final Message message) {
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.GONE);
viewHolder.download_button.setVisibility(View.VISIBLE);
viewHolder.download_button.setText(activity.getString(R.string.open_x_file, UIHelper.getFileDescriptionString(activity, message)));
2014-11-13 20:04:05 +00:00
viewHolder.download_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openDownloadable(message);
2014-11-13 20:04:05 +00:00
}
});
viewHolder.download_button.setOnLongClickListener(openContextMenu);
}
private void displayLocationMessage(ViewHolder viewHolder, final Message message) {
viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.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.setOnLongClickListener(openContextMenu);
}
private void displayImageMessage(ViewHolder viewHolder,
final Message message) {
if (viewHolder.download_button != null) {
viewHolder.download_button.setVisibility(View.GONE);
}
viewHolder.messageBody.setVisibility(View.GONE);
viewHolder.image.setVisibility(View.VISIBLE);
FileParams params = message.getFileParams();
2014-10-14 16:16:03 +00:00
double target = metrics.density * 288;
int scaledW;
int scaledH;
if (Math.max(params.height, params.width) * metrics.density <= target) {
scaledW = (int) (params.width * metrics.density);
scaledH = (int) (params.height * metrics.density);
} else if (Math.max(params.height,params.width) <= target) {
scaledW = params.width;
scaledH = params.height;
} else if (params.width <= params.height) {
scaledW = (int) (params.width / ((double) params.height / target));
scaledH = (int) target;
2014-10-14 16:16:03 +00:00
} else {
scaledW = (int) target;
scaledH = (int) (params.height / ((double) params.width / target));
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(scaledW, scaledH);
layoutParams.setMargins(0, (int) (metrics.density * 4), 0, (int) (metrics.density * 4));
2015-07-19 12:26:03 +00:00
viewHolder.image.setLayoutParams(layoutParams);
activity.loadBitmap(message, viewHolder.image);
viewHolder.image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openDownloadable(message);
}
});
viewHolder.image.setOnLongClickListener(openContextMenu);
}
private void loadMoreMessages(Conversation conversation) {
conversation.setLastClearHistory(0);
activity.xmppConnectionService.databaseBackend.updateConversation(conversation);
conversation.setHasMessagesLeftOnServer(true);
conversation.setFirstMamReference(null);
long timestamp = conversation.getLastMessageTransmitted();
if (timestamp == 0) {
timestamp = System.currentTimeMillis();
}
activity.setMessagesLoaded();
activity.xmppConnectionService.getMessageArchiveService().query(conversation, 0, timestamp);
Toast.makeText(activity, R.string.fetching_history_from_server,Toast.LENGTH_LONG).show();
}
@Override
public View getView(int position, View view, ViewGroup parent) {
2014-12-03 09:35:30 +00:00
final Message message = getItem(position);
final boolean isInValidSession = message.isValidInSession();
2014-12-03 09:35:30 +00:00
final Conversation conversation = message.getConversation();
final Account account = conversation.getAccount();
final int type = getItemViewType(position);
ViewHolder viewHolder;
if (view == null) {
viewHolder = new ViewHolder();
switch (type) {
2015-01-11 15:22:29 +00:00
case SENT:
view = activity.getLayoutInflater().inflate(
R.layout.message_sent, parent, false);
viewHolder.message_box = (LinearLayout) view
.findViewById(R.id.message_box);
2015-01-11 15:22:29 +00:00
viewHolder.contact_picture = (ImageView) view
.findViewById(R.id.message_photo);
2015-01-11 15:22:29 +00:00
viewHolder.download_button = (Button) view
.findViewById(R.id.download_button);
2015-01-11 15:22:29 +00:00
viewHolder.indicator = (ImageView) view
.findViewById(R.id.security_indicator);
viewHolder.edit_indicator = (ImageView) view.findViewById(R.id.edit_indicator);
2015-01-11 15:22:29 +00:00
viewHolder.image = (ImageView) view
.findViewById(R.id.message_image);
2015-01-11 15:22:29 +00:00
viewHolder.messageBody = (TextView) view
.findViewById(R.id.message_body);
2015-01-11 15:22:29 +00:00
viewHolder.time = (TextView) view
.findViewById(R.id.message_time);
2015-01-11 15:22:29 +00:00
viewHolder.indicatorReceived = (ImageView) view
.findViewById(R.id.indicator_received);
2015-01-11 15:22:29 +00:00
break;
case RECEIVED:
view = activity.getLayoutInflater().inflate(
R.layout.message_received, parent, false);
viewHolder.message_box = (LinearLayout) view
.findViewById(R.id.message_box);
2015-01-11 15:22:29 +00:00
viewHolder.contact_picture = (ImageView) view
.findViewById(R.id.message_photo);
2015-01-11 15:22:29 +00:00
viewHolder.download_button = (Button) view
.findViewById(R.id.download_button);
2015-01-11 15:22:29 +00:00
viewHolder.indicator = (ImageView) view
.findViewById(R.id.security_indicator);
viewHolder.edit_indicator = (ImageView) view.findViewById(R.id.edit_indicator);
2015-01-11 15:22:29 +00:00
viewHolder.image = (ImageView) view
.findViewById(R.id.message_image);
2015-01-11 15:22:29 +00:00
viewHolder.messageBody = (TextView) view
.findViewById(R.id.message_body);
2015-01-11 15:22:29 +00:00
viewHolder.time = (TextView) view
.findViewById(R.id.message_time);
2015-01-11 15:22:29 +00:00
viewHolder.indicatorReceived = (ImageView) view
2014-11-07 14:38:20 +00:00
.findViewById(R.id.indicator_received);
viewHolder.encryption = (TextView) view.findViewById(R.id.message_encryption);
2015-01-11 15:22:29 +00:00
break;
case STATUS:
2015-01-18 12:44:18 +00:00
view = activity.getLayoutInflater().inflate(R.layout.message_status, parent, false);
viewHolder.contact_picture = (ImageView) view.findViewById(R.id.message_photo);
viewHolder.status_message = (TextView) view.findViewById(R.id.status_message);
viewHolder.load_more_messages = (Button) view.findViewById(R.id.load_more_messages);
2015-01-11 15:22:29 +00:00
break;
default:
viewHolder = null;
break;
}
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
if (viewHolder == null) {
return view;
}
}
boolean darkBackground = type == RECEIVED && (!isInValidSession || mUseGreenBackground) || activity.isDarkTheme();
if (type == STATUS) {
if ("LOAD_MORE".equals(message.getBody())) {
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());
}
});
} else {
viewHolder.status_message.setVisibility(View.VISIBLE);
viewHolder.contact_picture.setVisibility(View.VISIBLE);
viewHolder.load_more_messages.setVisibility(View.GONE);
if (conversation.getMode() == Conversation.MODE_SINGLE) {
viewHolder.contact_picture.setImageBitmap(activity
.avatarService().get(conversation.getContact(),
activity.getPixel(32)));
viewHolder.contact_picture.setAlpha(0.5f);
}
viewHolder.status_message.setText(message.getBody());
2014-11-07 14:38:20 +00:00
}
return view;
} else {
loadAvatar(message, viewHolder.contact_picture);
}
2014-12-15 15:55:38 +00:00
viewHolder.contact_picture
2015-01-11 15:22:29 +00:00
.setOnClickListener(new OnClickListener() {
2014-12-15 15:55:38 +00:00
2015-01-11 15:22:29 +00:00
@Override
public void onClick(View v) {
if (MessageAdapter.this.mOnContactPictureClickedListener != null) {
MessageAdapter.this.mOnContactPictureClickedListener
.onContactPictureClicked(message);
2014-12-15 15:55:38 +00:00
}
2015-01-11 15:22:29 +00:00
}
});
2014-12-15 15:55:38 +00:00
viewHolder.contact_picture
2015-01-11 15:22:29 +00:00
.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) {
MessageAdapter.this.mOnContactPictureLongClickedListener
.onContactPictureLongClicked(message);
2015-01-11 15:22:29 +00:00
return true;
} else {
return false;
2014-12-15 15:55:38 +00:00
}
2015-01-11 15:22:29 +00:00
}
});
2014-09-20 14:02:49 +00:00
2015-07-10 13:11:03 +00:00
final Transferable transferable = message.getTransferable();
if (transferable != null && transferable.getStatus() != Transferable.STATUS_UPLOADING) {
if (transferable.getStatus() == Transferable.STATUS_OFFER) {
displayDownloadableMessage(viewHolder,message,activity.getString(R.string.download_x_file, UIHelper.getFileDescriptionString(activity, message)));
2015-07-10 13:11:03 +00:00
} else if (transferable.getStatus() == Transferable.STATUS_OFFER_CHECK_FILESIZE) {
2015-07-02 21:13:00 +00:00
displayDownloadableMessage(viewHolder, message, activity.getString(R.string.check_x_filesize, UIHelper.getFileDescriptionString(activity, message)));
} else {
displayInfoMessage(viewHolder, UIHelper.getMessagePreview(activity, message).first,darkBackground);
2014-11-13 20:04:05 +00:00
}
2014-12-03 09:35:30 +00:00
} else if (message.getType() == Message.TYPE_IMAGE && message.getEncryption() != Message.ENCRYPTION_PGP && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
displayImageMessage(viewHolder, message);
} else if (message.getType() == Message.TYPE_FILE && message.getEncryption() != Message.ENCRYPTION_PGP && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED) {
if (message.getFileParams().width > 0) {
2014-12-03 09:35:30 +00:00
displayImageMessage(viewHolder,message);
} else {
2014-12-03 09:35:30 +00:00
displayOpenableMessage(viewHolder, message);
}
2014-12-03 09:35:30 +00:00
} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
2016-06-13 11:32:14 +00:00
if (account.isPgpDecryptionServiceConnected()) {
if (!account.hasPendingPgpIntent(conversation)) {
displayInfoMessage(viewHolder, activity.getString(R.string.message_decrypting), darkBackground);
} else {
displayInfoMessage(viewHolder, activity.getString(R.string.pgp_message), darkBackground);
}
} else {
displayInfoMessage(viewHolder,activity.getString(R.string.install_openkeychain),darkBackground);
if (viewHolder != null) {
viewHolder.message_box
2015-01-11 15:22:29 +00:00
.setOnClickListener(new OnClickListener() {
2015-01-11 15:22:29 +00:00
@Override
public void onClick(View v) {
activity.showInstallPgpDialog();
}
});
}
}
2014-12-03 09:35:30 +00:00
} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
displayDecryptionFailed(viewHolder,darkBackground);
} else {
if (GeoHelper.isGeoUri(message.getBody())) {
displayLocationMessage(viewHolder,message);
2015-07-02 21:13:00 +00:00
} else if (message.bodyIsHeart()) {
displayHeartMessage(viewHolder, message.getBody().trim());
} else if (message.treatAsDownloadable() == Message.Decision.MUST) {
2016-03-04 20:30:34 +00:00
try {
URL url = new URL(message.getBody());
displayDownloadableMessage(viewHolder,
message,
activity.getString(R.string.check_x_filesize_on_host,
UIHelper.getFileDescriptionString(activity, message),
url.getHost()));
} catch (Exception e) {
displayDownloadableMessage(viewHolder,
message,
activity.getString(R.string.check_x_filesize,
UIHelper.getFileDescriptionString(activity, message)));
}
} else {
displayTextMessage(viewHolder, message, darkBackground, type);
}
}
if (type == RECEIVED) {
if(isInValidSession) {
int bubble;
if (!mUseGreenBackground) {
bubble = activity.getThemeResource(R.attr.message_bubble_received_monochrome, R.drawable.message_bubble_received_white);
} else {
bubble = activity.getThemeResource(R.attr.message_bubble_received_green, R.drawable.message_bubble_received);
}
viewHolder.message_box.setBackgroundResource(bubble);
viewHolder.encryption.setVisibility(View.GONE);
} else {
viewHolder.message_box.setBackgroundResource(R.drawable.message_bubble_received_warning);
viewHolder.encryption.setVisibility(View.VISIBLE);
viewHolder.encryption.setText(CryptoHelper.encryptionTypeToText(message.getEncryption()));
}
}
displayStatus(viewHolder, message, type, darkBackground);
return view;
}
public void openDownloadable(Message message) {
DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
2014-11-15 14:16:40 +00:00
if (!file.exists()) {
Toast.makeText(activity,R.string.file_deleted,Toast.LENGTH_SHORT).show();
return;
}
Intent openIntent = new Intent(Intent.ACTION_VIEW);
String mime = file.getMimeType();
if (mime == null) {
mime = "*/*";
}
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
uri = FileProvider.getUriForFile(activity, "eu.siacs.conversations.files", file);
} catch (IllegalArgumentException e) {
Toast.makeText(activity,activity.getString(R.string.no_permission_to_access_x,file.getAbsolutePath()), Toast.LENGTH_SHORT).show();
return;
}
openIntent.setDataAndType(uri, mime);
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.fromFile(file);
}
openIntent.setDataAndType(uri, mime);
2014-11-15 14:16:40 +00:00
PackageManager manager = activity.getPackageManager();
List<ResolveInfo> info = manager.queryIntentActivities(openIntent, 0);
if (info.size() == 0) {
openIntent.setDataAndType(Uri.fromFile(file),"*/*");
}
try {
getContext().startActivity(openIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(activity,R.string.no_application_found_to_open_file,Toast.LENGTH_SHORT).show();
2014-11-15 14:16:40 +00:00
}
2014-11-13 20:04:05 +00:00
}
public void showLocation(Message message) {
for(Intent intent : GeoHelper.createGeoIntentsFromMessage(message)) {
if (intent.resolveActivity(getContext().getPackageManager()) != null) {
getContext().startActivity(intent);
return;
}
}
Toast.makeText(activity,R.string.no_application_found_to_display_location,Toast.LENGTH_SHORT).show();
}
public void updatePreferences() {
this.mIndicateReceived = activity.indicateReceived();
this.mUseGreenBackground = activity.useGreenBackground();
}
2014-11-07 14:38:20 +00:00
public interface OnContactPictureClicked {
void onContactPictureClicked(Message message);
2014-11-07 14:38:20 +00:00
}
public interface OnContactPictureLongClicked {
void onContactPictureLongClicked(Message message);
2014-11-07 14:38:20 +00:00
}
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 TextView messageBody;
protected ImageView contact_picture;
2015-01-18 12:44:18 +00:00
protected TextView status_message;
protected TextView encryption;
public Button load_more_messages;
public ImageView edit_indicator;
}
class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private Message message = null;
public BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<>(imageView);
}
@Override
protected Bitmap doInBackground(Message... params) {
return activity.avatarService().get(params[0], activity.getPixel(48), isCancelled());
}
@Override
protected void onPostExecute(Bitmap bitmap) {
2016-04-29 18:38:23 +00:00
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) {
if (cancelPotentialWork(message, imageView)) {
final Bitmap bm = activity.avatarService().get(message, activity.getPixel(48), true);
if (bm != null) {
2016-04-29 18:38:23 +00:00
cancelPotentialWork(message, imageView);
imageView.setImageBitmap(bm);
imageView.setBackgroundColor(0x00000000);
} else {
imageView.setBackgroundColor(UIHelper.getColorForName(UIHelper.getMessageDisplayName(message)));
imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
imageView.setImageDrawable(asyncDrawable);
try {
task.execute(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();
}
}
}