fix memory leak

This commit is contained in:
kosyak 2024-02-19 00:46:18 +01:00
parent 3db7c2c3da
commit 94d9b69c6c
3 changed files with 35 additions and 13 deletions

View file

@ -1511,6 +1511,11 @@ public class ConversationFragment extends XmppFragment
Log.d(Config.LOGTAG, "ConversationFragment.onDestroyView()");
messageListAdapter.setOnContactPictureClicked(null);
messageListAdapter.setOnContactPictureLongClicked(null);
messageListAdapter.setOnMessageBoxSwiped(null);
messageListAdapter.setMessageEmptyPartLongClickListener(null);
messageListAdapter.setReplyClickListener(null);
messageListAdapter.setSelectionStatusProvider(null);
binding.messagesView.clearDragHelper();
binding.conversationViewPager.setAdapter(null);
if (conversation != null) conversation.setupViewPager(null, null, null);
binding.tabLayout.setListener(null);

View file

@ -18,6 +18,10 @@ class DraggableListView : ListView {
private var dragHelper: ViewDragHelper? = null
fun clearDragHelper() {
dragHelper = null;
}
override fun setAdapter(adapter: ListAdapter?) {
super.setAdapter(adapter)
val dragHelperCallback = if (adapter is DraggableAdapter) {

View file

@ -43,6 +43,7 @@ import androidx.customview.widget.ViewDragHelper;
import com.google.common.base.Strings;
import java.lang.ref.WeakReference;
import java.net.URI;
import java.util.HashSet;
import java.util.List;
@ -502,19 +503,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements DraggableLi
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
body.setSpan(new QuoteSpan(color, highlightReply ? ContextCompat.getColor(activity, R.color.blue_a100) : -1, metrics), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (highlightReply) {
body.setSpan(new ClickableSpan() {
@Override
public void onClick(@NonNull View widget) {
if (replyClickListener != null) {
replyClickListener.onReplyClick(message);
}
}
@Override
public void updateDrawState(@NonNull TextPaint ds) {
ds.setUnderlineText(false);
}
}, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
body.setSpan(new ReplyClickableSpan(new WeakReference(replyClickListener), new WeakReference(message)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
@ -1252,6 +1241,30 @@ public class MessageAdapter extends ArrayAdapter<Message> implements DraggableLi
void onReplyClick(Message message);
}
private static class ReplyClickableSpan extends ClickableSpan {
private WeakReference<ReplyClickListener> replyClickListener;
private WeakReference<Message> message;
public ReplyClickableSpan(WeakReference<ReplyClickListener> replyClickListener, WeakReference<Message> message) {
this.replyClickListener = replyClickListener;
this.message = message;
}
@Override
public void onClick(@NonNull View widget) {
ReplyClickListener listener = replyClickListener.get();
Message message = this.message.get();
if (listener != null && message != null) {
listener.onReplyClick(message);
}
}
@Override
public void updateDrawState(@NonNull TextPaint ds) {
ds.setUnderlineText(false);
}
}
private static class ViewHolder {
public View root;