Skip to content

Commit

Permalink
added disableCrashPreventerInputFilter and disableMediaDeletedListene…
Browse files Browse the repository at this point in the history
…r to prevent inputFilter from being called twice and MediaDeletedListener from being mistakingly called by a non-user operation
  • Loading branch information
mzorz committed Mar 29, 2019
1 parent 39bf93f commit 79e1740
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
36 changes: 35 additions & 1 deletion aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
private var consumeSelectionChangedEvent: Boolean = false
private var isInlineTextHandlerEnabled: Boolean = true
private var bypassObservationQueue: Boolean = false
private var bypassMediaDeletedListener: Boolean = false
private var bypassCrashPreventerInputFilter: Boolean = false

var initialEditorContentParsedSHA256: ByteArray = ByteArray(0)

Expand Down Expand Up @@ -456,22 +458,34 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
// https://android-review.googlesource.com/c/platform/frameworks/base/+/634929
val dynamicLayoutCrashPreventer = InputFilter { source, start, end, dest, dstart, dend ->
var temp = source
if (dest.length > dend+1) {
if (dest.length > dend+1 && !bypassCrashPreventerInputFilter) {
// if there are any images right after the destination position, hack the text
val spans = dest.getSpans(dstart, dend+1, AztecImageSpan::class.java)
if (spans.isNotEmpty()) {
// test: is this an insertion?
if (dstart == dend) {

// prevent this filter from running twice when `text.insert()` gets called a few lines below
disableCrashPreventerInputFilter()
// disable MediaDeleted listener before operating on content
disableMediaDeletedListener()

// take the source (that is, what is being inserted), and append the Image to it. We will delete
// the original Image later so to not have a duplicate.
// use Spannable to copy / keep the current spans
temp = SpannableStringBuilder(source).append(dest.subSequence(dend, dend+1))


// delete the original AztecImageSpan
text.delete(dend, dend+1)
// now insert both the new insertion _and_ the original AztecImageSpan
text.insert(dend, temp)
temp = "" // discard the original source parameter as an ouput from this InputFilter

// re-enable MediaDeleted listener
enableMediaDeletedListener()
// re-enable this very filter
enableCrashPreventerInputFilter()
}
}
}
Expand Down Expand Up @@ -1340,6 +1354,26 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
bypassObservationQueue = false
}

fun disableCrashPreventerInputFilter() {
bypassCrashPreventerInputFilter = true
}

fun enableCrashPreventerInputFilter() {
bypassCrashPreventerInputFilter = false
}

fun disableMediaDeletedListener() {
bypassMediaDeletedListener = true
}

fun enableMediaDeletedListener() {
bypassMediaDeletedListener = false
}

fun isMediaDeletedListenerDisabled(): Boolean {
return bypassMediaDeletedListener
}

fun isTextChangedListenerDisabled(): Boolean {
return consumeEditEvent
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class DeleteMediaElementWatcherAPI25AndHigher(aztecText: AztecText) : TextWatche
return
}

if (aztecTextRef.get()?.isMediaDeletedListenerDisabled() ?: true) {
return
}

if (count > 0) {
deleted = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class DeleteMediaElementWatcherPreAPI25(aztecText: AztecText) : TextWatcher {
return
}

if (aztecTextRef.get()?.isMediaDeletedListenerDisabled() ?: true) {
return
}

if (count > 0) {
aztecTextRef.get()?.text?.getSpans(start, start + count, AztecMediaSpan::class.java)
?.forEach {
Expand Down

0 comments on commit 79e1740

Please sign in to comment.