From 493357cee384913f4db5b4e5c5ed8f97172b2537 Mon Sep 17 00:00:00 2001 From: khaykov Date: Thu, 18 Jul 2024 13:01:20 -0500 Subject: [PATCH 1/4] Use weak references and clear them when aztec is detached from window. --- .../kotlin/org/wordpress/aztec/AztecText.kt | 6 ++++-- .../wordpress/aztec/EnhancedMovementMethod.kt | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index 3ac7ac57e..e8c9eb5da 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -542,7 +542,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown exclusiveBlockStyles = BlockFormatter.ExclusiveBlockStyles(styles.getBoolean(R.styleable.AztecText_exclusiveBlocks, false), verticalParagraphPadding), paragraphStyle = BlockFormatter.ParagraphStyle(verticalParagraphMargin) ) - EnhancedMovementMethod.taskListClickHandler = TaskListClickHandler(listStyle) + EnhancedMovementMethod.setTaskListClickHandler(TaskListClickHandler(listStyle)) linkFormatter = LinkFormatter(this, LinkFormatter.LinkStyle(styles.getColor( R.styleable.AztecText_linkColor, 0), @@ -954,6 +954,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown if (blockEditorDialog != null && blockEditorDialog!!.isShowing) { blockEditorDialog!!.dismiss() } + EnhancedMovementMethod.setLinkTappedListener(null) + EnhancedMovementMethod.setTaskListClickHandler(null) } // We are exposing this method in order to allow subclasses to set their own alpha value @@ -1178,7 +1180,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown } fun setOnLinkTappedListener(listener: OnLinkTappedListener) { - EnhancedMovementMethod.linkTappedListener = listener + EnhancedMovementMethod.setLinkTappedListener(listener) } fun setLinkTapEnabled(isLinkTapEnabled: Boolean) { diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt b/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt index 74760feac..5ea763d7a 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt @@ -9,14 +9,23 @@ 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 taskListClickHandlerRef: WeakReference = WeakReference(null) + private var linkTappedListenerRef: WeakReference = WeakReference(null) var isLinkTapEnabled = false - var linkTappedListener: AztecText.OnLinkTappedListener? = null + + fun setTaskListClickHandler(handler: TaskListClickHandler?) { + taskListClickHandlerRef = WeakReference(handler) + } + + fun setLinkTappedListener(listener: AztecText.OnLinkTappedListener?) { + linkTappedListenerRef = WeakReference(listener) + } override fun onTouchEvent(widget: TextView, text: Spannable, event: MotionEvent): Boolean { val action = event.action @@ -38,7 +47,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 (taskListClickHandlerRef.get()?.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) @@ -85,7 +96,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 } } From bc58e151b5117f6202f4b0362c66a591510b9ed3 Mon Sep 17 00:00:00 2001 From: khaykov Date: Thu, 18 Jul 2024 13:14:33 -0500 Subject: [PATCH 2/4] Fixed lint. --- .../main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt b/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt index 5ea763d7a..dd04a899c 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt @@ -47,7 +47,7 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() { val off = layout.getOffsetForHorizontal(line, x.toFloat()) // This handles the case when the task list checkbox is clicked - if (taskListClickHandlerRef.get()?.handleTaskListClick(text, off, x, widget.totalPaddingStart) == true){ + if (taskListClickHandlerRef.get()?.handleTaskListClick(text, off, x, widget.totalPaddingStart) == true) { return true } From 8f5b6c3fa3c983dc27b295df88a1db6ec1b38e12 Mon Sep 17 00:00:00 2001 From: khaykov Date: Thu, 18 Jul 2024 15:02:11 -0500 Subject: [PATCH 3/4] Revert change to task click handler, since it's not leaking. --- aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt | 3 +-- .../kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt | 7 ++----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index e8c9eb5da..0693fd306 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -542,7 +542,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown exclusiveBlockStyles = BlockFormatter.ExclusiveBlockStyles(styles.getBoolean(R.styleable.AztecText_exclusiveBlocks, false), verticalParagraphPadding), paragraphStyle = BlockFormatter.ParagraphStyle(verticalParagraphMargin) ) - EnhancedMovementMethod.setTaskListClickHandler(TaskListClickHandler(listStyle)) + EnhancedMovementMethod.taskListClickHandler = TaskListClickHandler(listStyle) linkFormatter = LinkFormatter(this, LinkFormatter.LinkStyle(styles.getColor( R.styleable.AztecText_linkColor, 0), @@ -955,7 +955,6 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown blockEditorDialog!!.dismiss() } EnhancedMovementMethod.setLinkTappedListener(null) - EnhancedMovementMethod.setTaskListClickHandler(null) } // We are exposing this method in order to allow subclasses to set their own alpha value diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt b/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt index dd04a899c..523d8e072 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt @@ -15,13 +15,10 @@ import java.lang.ref.WeakReference * http://stackoverflow.com/a/23566268/569430 */ object EnhancedMovementMethod : ArrowKeyMovementMethod() { - private var taskListClickHandlerRef: WeakReference = WeakReference(null) + var taskListClickHandler: TaskListClickHandler? = null private var linkTappedListenerRef: WeakReference = WeakReference(null) var isLinkTapEnabled = false - fun setTaskListClickHandler(handler: TaskListClickHandler?) { - taskListClickHandlerRef = WeakReference(handler) - } fun setLinkTappedListener(listener: AztecText.OnLinkTappedListener?) { linkTappedListenerRef = WeakReference(listener) @@ -47,7 +44,7 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() { val off = layout.getOffsetForHorizontal(line, x.toFloat()) // This handles the case when the task list checkbox is clicked - if (taskListClickHandlerRef.get()?.handleTaskListClick(text, off, x, widget.totalPaddingStart) == true) { + if (taskListClickHandler?.handleTaskListClick(text, off, x, widget.totalPaddingStart) == true) { return true } From 233823ea91ae51d3f259574d4422214a3451783b Mon Sep 17 00:00:00 2001 From: khaykov Date: Thu, 18 Jul 2024 16:11:39 -0500 Subject: [PATCH 4/4] Fixed lint. --- .../main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt b/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt index 523d8e072..251b974fe 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt @@ -19,7 +19,6 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() { private var linkTappedListenerRef: WeakReference = WeakReference(null) var isLinkTapEnabled = false - fun setLinkTappedListener(listener: AztecText.OnLinkTappedListener?) { linkTappedListenerRef = WeakReference(listener) }