Skip to content

Commit

Permalink
Merge pull request #1086 from wordpress-mobile/issue/fix-memory-leak-…
Browse files Browse the repository at this point in the history
…in-enhanced-movement-method

Fix memory leak in EnhancedMovementMethod
  • Loading branch information
zwarm authored Jul 22, 2024
2 parents 320e42f + 233823e commit 3c95e7b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
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

0 comments on commit 3c95e7b

Please sign in to comment.