Skip to content

Commit

Permalink
Fix various crashes (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu authored Oct 2, 2023
1 parent 4d7874a commit 9580cdd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ internal class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView
if (!isSelecting && !mIsBeingDragged) {
mInitialVelocity = getCurrentXVelocity()
val pointerIndex = ev.findPointerIndex(mActivePointerId)
val x = ev.getX(pointerIndex)
val x = ev.safeGetX(pointerIndex)
val xDiff = abs(x - mLastMotionX)

if (xDiff > mTouchSlop) {
Expand All @@ -736,8 +736,8 @@ internal class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView
mHasAbortedScroller = false

val activePointerIndex = ev.findPointerIndex(mActivePointerId)
val x = ev.getX(activePointerIndex)
val y = ev.getY(activePointerIndex)
val x = ev.safeGetX(activePointerIndex)
val y = ev.safeGetY(activePointerIndex)

if (scrollMode) {
val totalDelta = (y - mInitialMotionY).toInt()
Expand Down Expand Up @@ -786,13 +786,13 @@ internal class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView
}
MotionEvent.ACTION_POINTER_DOWN -> {
val index = ev.actionIndex
val x = ev.getX(index)
val x = ev.safeGetX(index)
mLastMotionX = x
mActivePointerId = ev.getPointerId(index)
}
MotionEvent.ACTION_POINTER_UP -> {
onSecondaryPointerUp(ev)
mLastMotionX = ev.getX(ev.findPointerIndex(mActivePointerId))
mLastMotionX = ev.safeGetX(ev.findPointerIndex(mActivePointerId))
}
}

Expand Down Expand Up @@ -878,7 +878,7 @@ internal class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
val newPointerIndex = if (pointerIndex == 0) 1 else 0
mLastMotionX = ev.getX(newPointerIndex)
mLastMotionX = ev.safeGetX(newPointerIndex)
mActivePointerId = ev.getPointerId(newPointerIndex)
mVelocityTracker?.clear()
}
Expand Down Expand Up @@ -1092,3 +1092,15 @@ internal class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView
}
}
}

/**
* May crash with java.lang.IllegalArgumentException: pointerIndex out of range
*/
private fun MotionEvent.safeGetX(pointerIndex: Int): Float =
try { getX(pointerIndex) } catch (e: IllegalArgumentException) { 0F }

/**
* May crash with java.lang.IllegalArgumentException: pointerIndex out of range
*/
private fun MotionEvent.safeGetY(pointerIndex: Int): Float =
try { getY(pointerIndex) } catch (e: IllegalArgumentException) { 0F }
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public class AssetRetriever(
if (url.isContent) {
val contentHints = MediaTypeHints(
mediaType = contentResolver.getType(url.uri)
?.let { MediaType(it)!! }
?.let { MediaType(it) }
?.takeUnless { it.matches(MediaType.BINARY) },
fileExtension = contentResolver
.queryProjection(url.uri, MediaStore.MediaColumns.DISPLAY_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ import org.readium.r2.shared.resource.Resource
import org.readium.r2.shared.resource.readAsString
import org.readium.r2.shared.util.Language
import org.readium.r2.shared.util.Url
import org.readium.r2.shared.util.getOrThrow
import org.readium.r2.shared.util.getOrElse
import org.readium.r2.shared.util.mediatype.MediaType
import org.readium.r2.shared.util.use
import timber.log.Timber

/**
* Iterates an HTML [resource], starting from the given [locator].
Expand Down Expand Up @@ -147,7 +148,11 @@ public class HtmlResourceContentIterator internal constructor(

private suspend fun parseElements(): ParsedElements {
val document = resource.use { res ->
val html = res.readAsString().getOrThrow()
val html = res.readAsString().getOrElse {
Timber.w(it, "Failed to read HTML resource")
return ParsedElements()
}

Jsoup.parse(html)
}

Expand Down Expand Up @@ -206,8 +211,8 @@ public class HtmlResourceContentIterator internal constructor(
* possible. Defaults to 0.
*/
public data class ParsedElements(
val elements: List<Content.Element>,
val startIndex: Int
val elements: List<Content.Element> = emptyList(),
val startIndex: Int = 0
)

private class ContentParser(
Expand Down

0 comments on commit 9580cdd

Please sign in to comment.