Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in EnhancedMovementMethod #1086

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
if (blockEditorDialog != null && blockEditorDialog!!.isShowing) {
blockEditorDialog!!.dismiss()
}
EnhancedMovementMethod.setLinkTappedListener(null)
}

// We are exposing this method in order to allow subclasses to set their own alpha value
Expand Down Expand Up @@ -1178,7 +1179,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
}

fun setOnLinkTappedListener(listener: OnLinkTappedListener) {
EnhancedMovementMethod.linkTappedListener = listener
EnhancedMovementMethod.setLinkTappedListener(listener)
}

fun setLinkTapEnabled(isLinkTapEnabled: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ import android.widget.TextView
import org.wordpress.aztec.spans.AztecMediaClickableSpan
import org.wordpress.aztec.spans.AztecURLSpan
import org.wordpress.aztec.spans.UnknownClickableSpan
import java.lang.ref.WeakReference

/**
* http://stackoverflow.com/a/23566268/569430
*/
object EnhancedMovementMethod : ArrowKeyMovementMethod() {
var taskListClickHandler: TaskListClickHandler? = null
private var linkTappedListenerRef: WeakReference<AztecText.OnLinkTappedListener?> = WeakReference(null)
var isLinkTapEnabled = false
var linkTappedListener: AztecText.OnLinkTappedListener? = null

fun setLinkTappedListener(listener: AztecText.OnLinkTappedListener?) {
linkTappedListenerRef = WeakReference(listener)
}

override fun onTouchEvent(widget: TextView, text: Spannable, event: MotionEvent): Boolean {
val action = event.action
Expand All @@ -38,7 +43,9 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
val off = layout.getOffsetForHorizontal(line, x.toFloat())

// This handles the case when the task list checkbox is clicked
if (taskListClickHandler?.handleTaskListClick(text, off, x, widget.totalPaddingStart) == true) return true
if (taskListClickHandler?.handleTaskListClick(text, off, x, widget.totalPaddingStart) == true) {
return true
}

// get the character's position. This may be the left or the right edge of the character so, find the
// other edge by inspecting nearby characters (if they exist)
Expand Down Expand Up @@ -85,7 +92,7 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
link.onClick(widget)
return true
} else if (link is AztecURLSpan && isLinkTapEnabled) {
linkTappedListener?.onLinkTapped(widget, link.url) ?: link.onClick(widget)
linkTappedListenerRef.get()?.onLinkTapped(widget, link.url) ?: link.onClick(widget)
return true
}
}
Expand Down
Loading