Skip to content

refactor: improve file:// URL loading in Android (PR #329 improvements)#388

Closed
mfazekas wants to merge 2 commits intorive-app:mainfrom
mfazekas:mfazekas/pr329-improvements
Closed

refactor: improve file:// URL loading in Android (PR #329 improvements)#388
mfazekas wants to merge 2 commits intorive-app:mainfrom
mfazekas:mfazekas/pr329-improvements

Conversation

@mfazekas
Copy link
Copy Markdown
Collaborator

@mfazekas mfazekas commented Nov 5, 2025

Summary

Simplifies PR #329 by removing the unnecessary factory pattern and using a straightforward iOS-style approach with proper URI.scheme checking.

Changes

Removed:

  • ResourceLoader interface
  • VolleyHttpLoader class
  • FileSystemLoader class
  • ResourceLoaderFactory object

Added:

  • Simple if/else using URI.scheme in downloadUrlAsset()
  • loadFileUrlAsset() - Handles file:// URLs with background threading
  • loadRemoteUrlAsset() - Wraps existing Volley HTTP logic

Key Improvements

  1. Simpler architecture - Matches iOS pattern with simple conditional dispatch instead of factory pattern
  2. Proper URI parsing - Uses java.net.URI and .scheme property instead of string.startsWith("file://")
  3. Background threading - File I/O runs on Dispatchers.IO with callbacks on main thread
  4. Better error handling - Checks file existence/permissions, returns 404 for FileNotFoundException

Code Comparison

Before (factory pattern):

val loader = ResourceLoaderFactory.getLoader(url, context)
loader.loadResource(url, listener, errorListener)

After (simple dispatch):

val uri = URI(url)
when (uri.scheme) {
  "file" -> loadFileUrlAsset(uri, listener)
  else -> loadRemoteUrlAsset(url, listener)
}

Result: 16 fewer lines, more maintainable, platform consistency with iOS.

Related

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

ckknight and others added 2 commits November 5, 2025 11:30
This updates the definition of `RiveReactNativeView.downloadUrlAsset` to load `file://` URLs via `java.io.File`

Formerly, if a `file://` URL was loaded, that would lead to `com.android.volley.VolleyError: java.lang.ClassCastException: sun.net.www.protocol.file.FileURLConnection cannot be cast to java.net.HttpURLConnection`

This only affects Android apps built in release mode, as debug mode is unaffected.
Critical fixes:
- Add background threading to FileSystemLoader using coroutines with Dispatchers.IO
- Ensure callbacks on main thread with withContext(Dispatchers.Main)
- Reuse Volley RequestQueue singleton instead of creating new instance per request

High-priority improvements:
- Replace naive substring parsing with proper java.net.URI
- Handle URL encoding and edge cases (file:/// vs file://)
- Add specific error handling for FileNotFoundException with 404 status
- Check file existence and permissions before reading
- Provide clear error messages for permission denied

This aligns the Android implementation with iOS patterns:
- Explicit background threading (matching iOS DispatchQueue.global)
- Main thread callbacks (matching iOS DispatchQueue.main.async)
- Proper URL parsing (matching iOS URL.isFileURL)

Addresses concerns from code review:
- Thread safety: File I/O now happens on background thread
- Resource management: Single RequestQueue shared across requests
- Error handling: Granular exceptions with appropriate status codes
- URL parsing: Robust handling of special characters and edge cases

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@mfazekas mfazekas closed this Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants