Skip to content
Open
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
34 changes: 34 additions & 0 deletions android/src/main/java/com/rivereactnative/RiveReactNativeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.net.Uri
import android.util.Log
import android.widget.FrameLayout
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
Expand Down Expand Up @@ -83,6 +84,9 @@ class ReactNativeRiveAnimationView(private val context: ThemedReactContext) :
}
}

private const val LOG_TAG = "RiveReactNativeView"
private val RIVE_MAGIC = "RIVE".toByteArray(Charsets.US_ASCII)

@SuppressLint("ViewConstructor")
class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout(context) {
private var riveAnimationView: ReactNativeRiveAnimationView? = null
Expand Down Expand Up @@ -708,6 +712,7 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
sendRiveLoadedEvent()
url = null
} catch (ex: RiveException) {
discardRetainedResource()
handleRiveException(ex)
}

Expand All @@ -722,6 +727,10 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout

private fun setUrlRiveResource(url: String, autoplay: Boolean = this.autoplay) {
downloadUrlAsset(url) { bytes ->
if (!isRiveFile(bytes)) {
handleUnreadableUrlAsset(bytes.size)
return@downloadUrlAsset
}
try {
riveAnimationView?.setRiveBytes(
bytes,
Expand All @@ -736,11 +745,36 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
configureDataBinding()
sendRiveLoadedEvent()
} catch (ex: RiveException) {
discardRetainedResource()
handleRiveException(ex)
}
}
}

// rive-android stores the bytes in rendererAttributes.resource before importing them and
// RiveAnimationView.onAttachedToWindow re-imports that resource without a try/catch, so a
// load error that was already reported to JS becomes a fatal MalformedFileException on the
// next Fabric (re)attach unless the failed resource is dropped.
private fun discardRetainedResource() {
riveAnimationView?.rendererAttributes?.resource = null
}

private fun isRiveFile(bytes: ByteArray): Boolean {
return bytes.size >= RIVE_MAGIC.size && bytes.copyOfRange(0, RIVE_MAGIC.size).contentEquals(RIVE_MAGIC)
}

private fun handleUnreadableUrlAsset(byteCount: Int) {
val message = "Downloaded Rive asset is not a Rive file ($byteCount bytes)"
Log.w(LOG_TAG, message)
if (isUserHandlingErrors) {
val rnRiveError = RNRiveError.MalformedFile
rnRiveError.message = message
sendErrorToRN(rnRiveError)
} else {
showRNRiveError(message, null)
}
}

fun setArtboardName(artboardName: String) {
try {
this.artboardName = artboardName
Expand Down