package com.eko import android.app.DownloadManager import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter import android.media.MediaScannerConnection import android.net.Uri import android.os.Build import android.util.Log import android.webkit.MimeTypeMap import com.eko.handlers.OnBegin import com.eko.handlers.OnProgress import com.eko.handlers.OnProgressState import com.eko.utils.HeaderUtils import com.eko.utils.StorageManager import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReadableArray import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.ReadableType import com.facebook.react.bridge.WritableMap import com.facebook.react.modules.core.DeviceEventManagerModule import org.json.JSONObject import java.io.File import java.net.HttpURLConnection import java.net.URL import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import java.util.concurrent.Future class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicationContext) { companion object { const val NAME = "RNBackgroundDownloader" private val stateMap = mapOf( DownloadManager.STATUS_FAILED to DownloadConstants.TASK_CANCELING, DownloadManager.STATUS_PAUSED to DownloadConstants.TASK_SUSPENDED, DownloadManager.STATUS_PENDING to DownloadConstants.TASK_RUNNING, DownloadManager.STATUS_RUNNING to DownloadConstants.TASK_RUNNING, DownloadManager.STATUS_SUCCESSFUL to DownloadConstants.TASK_COMPLETED ) private val sharedLock = Any() // Controls whether debug logs are enabled @Volatile private var isLogsEnabled = false // Helper functions for conditional logging fun logD(tag: String, message: String) { if (isLogsEnabled) Log.d(tag, message) } fun logW(tag: String, message: String) { if (isLogsEnabled) Log.w(tag, message) } fun logE(tag: String, message: String) { if (isLogsEnabled) Log.e(tag, message) } /** * Convert a ReadableMap to JSONObject. * Handles nested maps and arrays properly. */ fun readableMapToJsonObject(map: ReadableMap?): JSONObject? { if (map == null) return null val json = JSONObject() val iterator = map.keySetIterator() while (iterator.hasNextKey()) { val key = iterator.nextKey() when (map.getType(key)) { ReadableType.Null -> json.put(key, JSONObject.NULL) ReadableType.Boolean -> json.put(key, map.getBoolean(key)) ReadableType.Number -> json.put(key, map.getDouble(key)) ReadableType.String -> json.put(key, map.getString(key)) ReadableType.Map -> json.put(key, readableMapToJsonObject(map.getMap(key))) ReadableType.Array -> json.put(key, readableArrayToJsonArray(map.getArray(key))) } } return json } /** * Convert a ReadableArray to JSONArray. */ private fun readableArrayToJsonArray(array: ReadableArray?): org.json.JSONArray? { if (array == null) return null val json = org.json.JSONArray() for (i in 0 until array.size()) { when (array.getType(i)) { ReadableType.Null -> json.put(JSONObject.NULL) ReadableType.Boolean -> json.put(array.getBoolean(i)) ReadableType.Number -> json.put(array.getDouble(i)) ReadableType.String -> json.put(array.getString(i)) ReadableType.Map -> json.put(readableMapToJsonObject(array.getMap(i))) ReadableType.Array -> json.put(readableArrayToJsonArray(array.getArray(i))) } } return json } } // Storage manager for persistent state private val storageManager = StorageManager(reactContext, NAME) private val cachedExecutorPool: ExecutorService = Executors.newCachedThreadPool() private val fixedExecutorPool: ExecutorService = Executors.newFixedThreadPool(1) private val downloader: Downloader private var downloadReceiver: BroadcastReceiver? = null private var downloadIdToConfig = mutableMapOf() private val configIdToDownloadId = mutableMapOf() private val configIdToProgressFuture = mutableMapOf>() private val configIdToHeaders = mutableMapOf>() private lateinit var ee: DeviceEventManagerModule.RCTDeviceEventEmitter // Centralized progress reporting with threshold filtering and batching private val progressReporter = ProgressReporter( onEmitProgress = { reportsArray -> getEventEmitter()?.emit("downloadProgress", reportsArray) } ) // Centralized event emitter for download events private val eventEmitter by lazy { DownloadEventEmitter { getEventEmitter()!! } } // Uploader for handling file uploads private val uploader: Uploader by lazy { Uploader(reactContext) } // Centralized event emitter for upload events private val uploadEventEmitter by lazy { UploadEventEmitter { getEventEmitter()!! } } // Centralized progress reporting for uploads private val uploadProgressReporter = ProgressReporter( onEmitProgress = { reportsArray -> getEventEmitter()?.emit("uploadProgress", reportsArray) }, bytesFieldName = "bytesUploaded" ) // Upload task configs mapping configId -> config private val uploadConfigs = mutableMapOf() // Flag to track if module is fully initialized @Volatile private var isInitialized = false // Flag to track if download receiver is registered @Volatile private var isReceiverRegistered = false // Map to store metadata for paused downloads private val configIdToMetadata = mutableMapOf() // Last time (ms) a recovery snapshot was persisted for a resumable download, // used to throttle snapshot writes from the frequent progress callbacks. private val configIdToLastSnapshotMs = mutableMapOf() /** * Get the event emitter, ensuring it's initialized. * Returns null if the module hasn't been initialized yet. */ private fun getEventEmitter(): DeviceEventManagerModule.RCTDeviceEventEmitter? { if (!isInitialized) { // Attempt lazy initialization if not yet initialized // This handles the case where download() is called before initialize() try { if (!::ee.isInitialized) { ee = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) } } catch (e: Exception) { logW(NAME, "Event emitter not ready yet: ${e.message}") return null } } return if (::ee.isInitialized) ee else null } /** * Ensure event emitter is initialized before starting downloads. * This is called before download() to fix issues on first app install * where download() might be called before initialize() completes. */ private fun ensureEventEmitterInitialized() { if (!::ee.isInitialized) { try { ee = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) logD(NAME, "Event emitter initialized eagerly before download") } catch (e: Exception) { logW(NAME, "Could not initialize event emitter: ${e.message}") } } } /** * Ensure download receiver is registered before starting downloads. * This fixes issues on first app install where download() might be called * before initialize() completes, causing download completion events to be missed. */ private fun ensureReceiverRegistered() { synchronized(sharedLock) { if (!isReceiverRegistered) { registerDownloadReceiver() logD(NAME, "Download receiver registered eagerly before download") } } } // Listener for resumable downloads private val resumableDownloadListener = object : ResumableDownloader.DownloadListener { override fun onBegin(id: String, expectedBytes: Long, headers: Map) { val headersMap = Arguments.createMap() for ((key, value) in headers) { headersMap.putString(key, value) } onBeginDownload(id, headersMap, expectedBytes) } override fun onProgress(id: String, bytesDownloaded: Long, bytesTotal: Long) { onProgressDownload(id, bytesDownloaded, bytesTotal) // Persist a recovery snapshot so this download can be recovered if the app // is force-stopped (which kills the foreground service and its in-memory state). maybeSnapshotActiveResumable(id, bytesDownloaded, bytesTotal) } override fun onComplete(id: String, location: String, bytesDownloaded: Long, bytesTotal: Long) { // Drop any buffered progress for this task so it cannot arrive in JS after downloadComplete progressReporter.clearPendingReport(id) eventEmitter.emitComplete(id, location, bytesDownloaded, bytesTotal) // Clean up all download state synchronized(sharedLock) { cleanupDownloadState(id) } } override fun onError(id: String, error: String, errorCode: Int) { // Drop any buffered progress for this task so it cannot arrive in JS after downloadFailed progressReporter.clearPendingReport(id) eventEmitter.emitFailed(id, error, errorCode) // Clean up all download state synchronized(sharedLock) { cleanupDownloadState(id) } } } init { loadDownloadIdToConfigMap() loadConfigMap() downloader = Downloader(reactContext, storageManager) } fun getConstants(): Map? { val constants = mutableMapOf() // Use internal storage (filesDir) for consistency with iOS and to avoid // issues with external storage paths on some devices constants["documents"] = reactContext.filesDir.absolutePath constants["TaskRunning"] = DownloadConstants.TASK_RUNNING constants["TaskSuspended"] = DownloadConstants.TASK_SUSPENDED constants["TaskCanceling"] = DownloadConstants.TASK_CANCELING constants["TaskCompleted"] = DownloadConstants.TASK_COMPLETED return constants } fun setLogsEnabled(enabled: Boolean) { isLogsEnabled = enabled } fun setMaxParallelDownloads(max: Int) { // Android DownloadManager doesn't support setting max parallel downloads // This is a no-op on Android, but we keep the method for API consistency logD(NAME, "setMaxParallelDownloads called with $max (no-op on Android)") } fun setAllowsCellularAccess(allows: Boolean) { // Store the setting for future downloads // This will be used in the download() method to set isAllowedOverMetered storageManager.saveBooleanSync("allowsCellularAccess", allows) logD(NAME, "setAllowsCellularAccess: $allows") } fun setNotificationGroupingConfig(config: ReadableMap) { val enabled = if (config.hasKey("enabled")) config.getBoolean("enabled") else false val showNotificationsEnabled = if (config.hasKey("showNotificationsEnabled")) config.getBoolean("showNotificationsEnabled") else false val mode = if (config.hasKey("mode")) config.getString("mode") ?: "individual" else "individual" val texts = if (config.hasKey("texts")) config.getMap("texts") else null val textsMap = mutableMapOf() texts?.let { val iterator = it.keySetIterator() while (iterator.hasNextKey()) { val key = iterator.nextKey() textsMap[key] = it.getString(key) ?: "" } } // Store the config for use by UIDTDownloadJobService if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { UIDTDownloadJobService.setNotificationGroupingConfig(enabled, showNotificationsEnabled, mode, textsMap) } logD(NAME, "setNotificationGroupingConfig: enabled=$enabled, showNotificationsEnabled=$showNotificationsEnabled, mode=$mode, texts=$textsMap") } fun initialize() { ee = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) isInitialized = true registerDownloadReceiver() // Set the listener for resumable downloads (used by the background service) downloader.setResumableDownloadListener(resumableDownloadListener) // Note: We don't clean up paused notifications here because they should remain visible // showing that downloads are paused. They will be cancelled when download is resumed or stopped. // Load persisted upload configs (for app restart recovery) synchronized(sharedLock) { val persistedUploads = storageManager.loadUploadConfigs() for ((id, config) in persistedUploads) { // Mark as suspended since they need to be restarted after app restart config.state = DownloadConstants.TASK_SUSPENDED uploadConfigs[id] = config } if (persistedUploads.isNotEmpty()) { logD(NAME, "Loaded ${persistedUploads.size} persisted upload configs") } } for ((downloadId, config) in downloadIdToConfig) { resumeTasks(downloadId, config) } } fun invalidate() { // Cancel all download notifications when app is closed if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { // Cancel notifications for all known downloads synchronized(sharedLock) { for ((_, config) in downloadIdToConfig) { UIDTDownloadJobService.cancelNotification(reactContext, config.id) } } // Also cancel notifications for paused downloads val pausedDownloads = downloader.getAllPausedDownloads() for ((configId, _) in pausedDownloads) { UIDTDownloadJobService.cancelNotification(reactContext, configId) } logD(NAME, "Cancelled all download notifications on invalidate") } unregisterDownloadReceiver() downloader.unbindService() } private fun registerDownloadReceiver() { // Prevent double registration if (isReceiverRegistered) { return } val filter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE) downloadReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) // Check if this download is being intentionally cancelled (paused or stopped) val cancelIntent = downloader.getCancelIntent(downloadId) if (cancelIntent != null) { downloader.clearCancelIntent(downloadId) logD(NAME, "Ignoring broadcast for ${cancelIntent.name.lowercase()} download: $downloadId") return } val config = downloadIdToConfig[downloadId] if (config != null) { val downloadStatus = downloader.checkDownloadStatus(downloadId) val status = downloadStatus.getInt("status") val localUri = downloadStatus.getString("localUri") stopTaskProgress(config.id) synchronized(sharedLock) { // Drop any buffered progress that slipped past stopTaskProgress's clearPendingReport // (the polling thread may have re-added it between clearPendingReport and this lock) progressReporter.clearPendingReport(config.id) when (status) { DownloadManager.STATUS_SUCCESSFUL -> { onSuccessfulDownload(config, downloadStatus) } DownloadManager.STATUS_FAILED -> { onFailedDownload(config, downloadStatus) } } if (localUri != null) { // Prevent memory leaks from MediaScanner. // Download successful, clean task after media scanning. val paths = arrayOf(localUri) MediaScannerConnection.scanFile(context, paths, null) { _, _ -> stopTask(config.id) } } else { // Download failed, clean task. stopTask(config.id) } } } } } compatRegisterReceiver(reactContext, downloadReceiver!!, filter, true) isReceiverRegistered = true } // TAKEN FROM // https://github.com/facebook/react-native/pull/38256/files\#diff-d5e21477eeadeb0c536d5870f487a8528f9a16ae928c397fec7b255805cc8ad3 private fun compatRegisterReceiver( context: Context, receiver: BroadcastReceiver, filter: IntentFilter, exported: Boolean ) { if (Build.VERSION.SDK_INT >= 34 && context.applicationInfo.targetSdkVersion >= 34) { context.registerReceiver( receiver, filter, if (exported) Context.RECEIVER_EXPORTED else Context.RECEIVER_NOT_EXPORTED ) } else { context.registerReceiver(receiver, filter) } } private fun unregisterDownloadReceiver() { downloadReceiver?.let { try { reactContext.unregisterReceiver(it) } catch (e: Exception) { logW(NAME, "Could not unregister receiver: ${e.message}") } downloadReceiver = null isReceiverRegistered = false } } private fun resumeTasks(downloadId: Long, config: RNBGDTaskConfig) { Thread { try { var bytesDownloaded: Long = 0 var bytesTotal: Long = 0 if (!config.reportedBegin) { val onBeginCallable = OnBegin(config, this::onBeginDownload) val onBeginFuture = cachedExecutorPool.submit(onBeginCallable) val onBeginState = onBeginFuture.get() bytesTotal = onBeginState.expectedBytes config.reportedBegin = true downloadIdToConfig[downloadId] = config saveDownloadIdToConfigMap() } val onProgressCallable = OnProgress( config, downloader, downloadId, bytesDownloaded, bytesTotal, this::onProgressDownload ) val onProgressFuture = cachedExecutorPool.submit(onProgressCallable) configIdToProgressFuture[config.id] = onProgressFuture } catch (e: Exception) { logE(NAME, "resumeTasks: ${Log.getStackTraceString(e)}") } }.start() } private fun removeTaskFromMap(downloadId: Long) { synchronized(sharedLock) { val config = downloadIdToConfig[downloadId] if (config != null) { configIdToDownloadId.remove(config.id) progressReporter.clearDownloadState(config.id) downloadIdToConfig.remove(downloadId) saveDownloadIdToConfigMap() } } } /** * Cleans up all state associated with a download. * This consolidates cleanup logic that was previously duplicated across * onComplete, onError, stopTask, and removeTaskFromMap. * * @param configId The config ID of the download to clean up * @param downloadId Optional download ID for DownloadManager cleanup * @param removePausedState Whether to remove paused state from downloader */ private fun cleanupDownloadState( configId: String, downloadId: Long? = null, removePausedState: Boolean = true ) { configIdToDownloadId.remove(configId) configIdToHeaders.remove(configId) configIdToMetadata.remove(configId) configIdToLastSnapshotMs.remove(configId) progressReporter.clearDownloadState(configId) // Drop any force-stop recovery snapshot for this download. downloader.removeActiveDownloadSnapshot(configId) if (downloadId != null) { downloadIdToConfig.remove(downloadId) saveDownloadIdToConfigMap() } if (removePausedState) { downloader.removePausedState(configId) } } /** * Persist a recovery snapshot for an in-progress resumable download, throttled so * the frequent progress callbacks don't write to storage on every buffer read. * If the app is force-stopped, restoreRecoverableDownloads() uses this snapshot to * surface the download again via getExistingDownloadTasks. */ private fun maybeSnapshotActiveResumable(configId: String, bytesDownloaded: Long, bytesTotal: Long) { val now = System.currentTimeMillis() val last = configIdToLastSnapshotMs[configId] ?: 0L if (now - last < DownloadConstants.RECOVERY_SNAPSHOT_INTERVAL_MS) { return } configIdToLastSnapshotMs[configId] = now // The active state lives in the foreground-service ResumableDownloader (Android < 14) // or, on Android 14+, in the UIDT job. Check both so force-stop recovery works on // every supported version. val state = downloader.getActiveResumableDownloads()[configId] ?: (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) UIDTDownloadJobService.getJobDownloadState(configId) else null) ?: return val headers = configIdToHeaders[configId] ?: state.headers val metadata = configIdToMetadata[configId] ?: "{}" downloader.saveActiveDownloadSnapshot( configId = configId, url = state.url, destination = state.destination, headers = headers, bytesDownloaded = bytesDownloaded, bytesTotal = bytesTotal, metadata = metadata ) } /** * Resolve redirects for a URL up to maxRedirects limit * @param originalUrl The original URL to follow * @param maxRedirects Maximum number of redirects to follow (0 means no redirect resolution) * @param headers Headers to include in redirect resolution requests * @return The final resolved URL, or original URL if maxRedirects is 0 or resolution fails */ private fun resolveRedirects(originalUrl: String, maxRedirects: Int, headers: ReadableMap?): String { if (maxRedirects <= 0) { return originalUrl } try { var currentUrl = originalUrl var redirectCount = 0 while (redirectCount < maxRedirects) { val url = URL(currentUrl) val connection = url.openConnection() as HttpURLConnection // Add headers to the redirect resolution request HeaderUtils.applyToConnection(connection, headers) // Add default headers for consistency with DownloadManager HeaderUtils.applyDefaultHeaders(connection, headers) connection.instanceFollowRedirects = false connection.requestMethod = "HEAD" // Use HEAD to avoid downloading content connection.connectTimeout = DownloadConstants.REDIRECT_TIMEOUT_MS connection.readTimeout = DownloadConstants.REDIRECT_TIMEOUT_MS val responseCode = connection.responseCode if (responseCode in 300..399) { // This is a redirect val location = connection.getHeaderField("Location") if (location == null) { logW(NAME, "Redirect response without Location header at: $currentUrl") break } // Handle relative URLs currentUrl = when { location.startsWith("/") -> { val baseUrl = URL(currentUrl) "${baseUrl.protocol}://${baseUrl.host}$location" } !location.startsWith("http") -> { val baseUrl = URL(currentUrl) "${baseUrl.protocol}://${baseUrl.host}/$location" } else -> location } logD(NAME, "Redirect ${redirectCount + 1}/$maxRedirects: $currentUrl -> $location") redirectCount++ } else { // Not a redirect, we've found the final URL break } connection.disconnect() } if (redirectCount >= maxRedirects) { logW( NAME, "Reached maximum redirects ($maxRedirects) for URL: $originalUrl. Final URL: $currentUrl" ) } else { logD(NAME, "Resolved URL after $redirectCount redirects: $currentUrl") } return currentUrl } catch (e: Exception) { logE(NAME, "Failed to resolve redirects for URL: $originalUrl. Error: ${e.message}") // Return original URL if redirect resolution fails return originalUrl } } fun download(options: ReadableMap) { // Ensure event emitter is initialized before starting download // This fixes issues on first app install where download() might be called // before initialize() completes ensureEventEmitterInitialized() // Ensure download receiver is registered before starting download // This fixes issues on first app install where download completion events // might be missed if download() is called before initialize() completes ensureReceiverRegistered() val id = options.getString("id") var url = options.getString("url") val destination = options.getString("destination") val headers = options.getMap("headers") // Handle metadata - it should be a string, but handle object case defensively val metadata = if (options.hasKey("metadata")) { when (options.getType("metadata")) { ReadableType.String -> options.getString("metadata") ReadableType.Map -> { // If passed as object, convert to JSON string properly try { val map = options.getMap("metadata") readableMapToJsonObject(map)?.toString() ?: "{}" } catch (e: Exception) { logW(NAME, "Failed to convert metadata map to string: ${e.message}") "{}" } } else -> null } } else null val progressIntervalScope = options.getInt("progressInterval") val progressMinBytesScope = options.getDouble("progressMinBytes").toLong() if (progressIntervalScope > 0 || progressMinBytesScope > 0) { val newInterval = if (progressIntervalScope > 0) progressIntervalScope.toLong() else progressReporter.getProgressInterval() val newMinBytes = if (progressMinBytesScope > 0) progressMinBytesScope else progressReporter.getProgressMinBytes() progressReporter.configure(newInterval, newMinBytes) // Sync notification update interval with progress interval for Android 14+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { UIDTDownloadJobService.setNotificationUpdateInterval(newInterval) } saveConfigMap() } val isAllowedOverRoaming = options.getBoolean("isAllowedOverRoaming") // Use per-download setting if provided, otherwise fall back to global setting val isAllowedOverMetered = if (options.hasKey("isAllowedOverMetered")) { options.getBoolean("isAllowedOverMetered") } else { // Fall back to global allowsCellularAccess setting storageManager.getBooleanSync("allowsCellularAccess", true) } // Use global notification setting instead of per-task setting val isNotificationVisible = UIDTDownloadJobService.isNotificationsEnabled() // Get maxRedirects parameter var maxRedirects = 0 if (options.hasKey("maxRedirects")) { maxRedirects = options.getInt("maxRedirects") } if (id == null || url == null || destination == null) { logE(NAME, "download: id, url and destination must be set.") return } // Resolve redirects if maxRedirects is specified if (maxRedirects > 0) { logD(NAME, "Resolving redirects for URL: $url (maxRedirects: $maxRedirects)") url = resolveRedirects(url, maxRedirects, headers) logD(NAME, "Final resolved URL: $url") } val request = DownloadManager.Request(Uri.parse(url)) request.setAllowedOverRoaming(isAllowedOverRoaming) request.setAllowedOverMetered(isAllowedOverMetered) request.setNotificationVisibility( if (isNotificationVisible) DownloadManager.Request.VISIBILITY_VISIBLE else DownloadManager.Request.VISIBILITY_HIDDEN ) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { request.setRequiresCharging(false) } // Note: On Android 16+, we use ResumableDownloader with UIDT jobs instead of DownloadManager // for better background execution support. See the Android 16+ check below in download path. // Add default headers to improve connection handling for slow-responding URLs HeaderUtils.applyDefaultHeaders(request, headers) headers?.let { val iterator = it.keySetIterator() while (iterator.hasNextKey()) { val headerKey = iterator.nextKey() request.addRequestHeader(headerKey, it.getString(headerKey)) } } // DownloadManager requires external storage for downloads // Use app's private external files directory: /storage/emulated/0/Android/data//files/ // This keeps files private to the app while being compatible with DownloadManager val uuid = (System.currentTimeMillis() and 0xfffffff).toInt() val extension = MimeTypeMap.getFileExtensionFromUrl(destination) val filename = "$uuid.$extension" // Get external files directory and validate it's a proper external storage path val externalFilesDir = reactContext.getExternalFilesDir(null) val isValidExternalPath = externalFilesDir != null && (externalFilesDir.absolutePath.startsWith("/storage/") || externalFilesDir.absolutePath.startsWith("/sdcard/") || externalFilesDir.absolutePath.startsWith("/mnt/")) // Save headers for potential pause/resume functionality val headersMap = HeaderUtils.toMap(headers) val metadataValue = metadata ?: "{}" // Helper function to start download with DownloadManager and track state fun startDownloadManagerDownload(downloadId: Long) { val config = RNBGDTaskConfig(id, url, destination, metadataValue) synchronized(sharedLock) { // Clean up any stale state from previous downloads with the same ID downloader.cleanupStaleState(id) // Clear any stale progress data and initialize tracking for new download progressReporter.clearDownloadState(id) progressReporter.initializeDownload(id) configIdToDownloadId[id] = downloadId configIdToHeaders[id] = headersMap configIdToMetadata[id] = metadataValue downloadIdToConfig[downloadId] = config saveDownloadIdToConfigMap() resumeTasks(downloadId, config) } } // Helper function to fall back to ResumableDownloader fun startWithResumableDownloader() { synchronized(sharedLock) { // Clean up any stale state from previous downloads with the same ID downloader.cleanupStaleState(id) // Clear any stale progress data and initialize tracking for new download progressReporter.clearDownloadState(id) progressReporter.initializeDownload(id) configIdToHeaders[id] = headersMap configIdToMetadata[id] = metadataValue } downloader.startResumableDownload(id, url, destination, headersMap, resumableDownloadListener, metadataValue) } // On Android 16+ (API 36), DownloadManager has strict path restrictions and throws // SecurityException for app-specific external storage paths. Use ResumableDownloader instead. if (Build.VERSION.SDK_INT >= 36) { logD(NAME, "Android 16+ detected: Using ResumableDownloader to avoid DownloadManager path restrictions") startWithResumableDownloader() return } if (isValidExternalPath) { // Use DownloadManager with valid external storage path // Download directly to final destination to avoid file duplication val destFile = File(destination) val parentDir = destFile.parentFile if (parentDir != null && !parentDir.exists()) { parentDir.mkdirs() } request.setDestinationUri(Uri.fromFile(destFile)) try { startDownloadManagerDownload(downloader.download(request)) } catch (e: SecurityException) { // Handle "Unsupported path" SecurityException on Android 16+ logW(NAME, "DownloadManager SecurityException (path not supported): ${e.message}") logD(NAME, "Falling back to ResumableDownloader for download: $id") startWithResumableDownloader() } } else { // External files directory path is invalid or null // Try setDestinationInExternalFilesDir as fallback, then ResumableDownloader if that fails logW(NAME, "External files directory path may be invalid for DownloadManager: ${externalFilesDir?.absolutePath}") try { // Try standard setDestinationInExternalFilesDir - may work on some devices request.setDestinationInExternalFilesDir(reactContext, null, filename) startDownloadManagerDownload(downloader.download(request)) logD(NAME, "Using setDestinationInExternalFilesDir for download: $id") } catch (e: Exception) { // DownloadManager failed - fall back to ResumableDownloader // This handles OnePlus and other devices that return paths like /data/local/tmp/external/ logW(NAME, "DownloadManager failed with: ${e.message}") logD(NAME, "Using ResumableDownloader as fallback for download: $id") startWithResumableDownloader() } } } // Pause a download. If it's a DownloadManager download, this cancels it and saves state. // If it's already using ResumableDownloader, it pauses the HTTP connection. fun pauseTask(configId: String) { synchronized(sharedLock) { // First check if it's an active resumable download if (downloader.isResumableDownload(configId)) { // Get state before pausing - check both UIDT jobs and regular service var state: ResumableDownloader.DownloadState? = null // Check UIDT jobs first (Android 14+) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { state = UIDTDownloadJobService.getJobDownloadState(configId) } // Fall back to regular service if not found in UIDT jobs if (state == null) { state = downloader.resumableDownloader.getState(configId) } // Pause the download val paused = downloader.pauseResumable(configId) if (paused && state != null) { // Save to persistent storage so we can resume after app restart val headers = configIdToHeaders[configId] ?: state.headers val metadata = configIdToMetadata[configId] ?: "{}" downloader.savePausedDownloadState( configId = configId, url = state.url, destination = state.destination, headers = headers, bytesDownloaded = state.bytesDownloaded.get(), bytesTotal = state.bytesTotal, metadata = metadata ) // Now tracked as an explicit paused download - drop the recovery snapshot. downloader.removeActiveDownloadSnapshot(configId) configIdToLastSnapshotMs.remove(configId) logD(NAME, "Paused resumable download: $configId (saved state: ${state.bytesDownloaded.get()}/${state.bytesTotal} bytes)") } return } // Otherwise, it's a DownloadManager download - pause by canceling and saving state val downloadId = configIdToDownloadId[configId] if (downloadId != null) { val config = downloadIdToConfig[downloadId] if (config != null) { val headers = configIdToHeaders[configId] ?: emptyMap() val metadata = configIdToMetadata[configId] ?: config.metadata // Stop progress tracking stopTaskProgress(configId) // Pause the download (this cancels DownloadManager and saves state) val paused = downloader.pause(downloadId, configId, config.url, config.destination, headers, metadata) if (paused) { // Remove from DownloadManager tracking downloadIdToConfig.remove(downloadId) configIdToDownloadId.remove(configId) saveDownloadIdToConfigMap() logD(NAME, "Paused DownloadManager download: $configId") } } } } } // Resume a paused download using HTTP Range headers via ResumableDownloader. fun resumeTask(configId: String) { synchronized(sharedLock) { // First check if it's a paused UIDT job (Android 14+) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { if (UIDTDownloadJobService.isPausedJob(configId)) { downloader.resumeResumable(configId, resumableDownloadListener) logD(NAME, "Resumed paused UIDT job: $configId") return } } // Check if it's a paused resumable download in the foreground service if (downloader.resumableDownloader.isPaused(configId)) { downloader.resumeResumable(configId, resumableDownloadListener) logD(NAME, "Resumed paused resumable download: $configId") return } // Check if we have saved paused state from a DownloadManager download if (downloader.isPaused(configId)) { val resumed = downloader.resume(configId, resumableDownloadListener) if (resumed) { logD(NAME, "Resumed download via ResumableDownloader: $configId") } return } } } fun stopTask(configId: String) { synchronized(sharedLock) { // Stop progress tracking stopTaskProgress(configId) // Cancel any detached paused notification (Android 14+) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { UIDTDownloadJobService.cancelNotification(reactContext, configId) } // Cancel resumable download if active downloader.cancelResumable(configId) // Cancel DownloadManager download if active and clean up val downloadId = configIdToDownloadId[configId] if (downloadId != null) { downloader.cancel(downloadId) } // Clean up all download state cleanupDownloadState(configId, downloadId) } } fun completeHandler(configId: String) { // Firebase Performance compatibility: Add defensive programming to prevent crashes // when Firebase Performance SDK is installed and uses bytecode instrumentation logD(NAME, "completeHandler called with configId: $configId") // Defensive programming: Validate parameters if (configId.isEmpty()) { logW(NAME, "completeHandler: Invalid configId provided") return } try { // Currently this method doesn't have any implementation on Android // as completion handlers are handled differently than iOS. // This defensive structure ensures Firebase Performance compatibility. logD(NAME, "completeHandler executed successfully for configId: $configId") } catch (e: Exception) { // Catch any potential exceptions that might be thrown due to Firebase Performance // bytecode instrumentation interfering with method dispatch logE(NAME, "completeHandler: Exception occurred: ${Log.getStackTraceString(e)}") } } /** * Update headers for a paused download task. * This allows changing auth tokens before resuming a download. */ fun updateTaskHeaders(configId: String, headers: ReadableMap, promise: Promise) { try { synchronized(sharedLock) { val headersMap = HeaderUtils.toMap(headers) // Update the in-memory headers map configIdToHeaders[configId] = headersMap // Update paused download state if it exists downloader.updatePausedDownloadHeaders(configId, headersMap) logD(NAME, "Updated headers for task: $configId") promise.resolve(true) } } catch (e: Exception) { logE(NAME, "Failed to update task headers: ${Log.getStackTraceString(e)}") promise.resolve(false) } } fun getExistingDownloadTasks(promise: Promise) { val foundTasks = Arguments.createArray() val processedIds = mutableSetOf() synchronized(sharedLock) { // Phase 1: Query active downloads from DownloadManager val query = DownloadManager.Query() try { downloader.downloadManager.query(query)?.use { cursor -> if (cursor.moveToFirst()) { do { val downloadStatus = downloader.getDownloadStatus(cursor) val downloadId = downloadStatus.getString("downloadId")?.toLong() if (downloadId != null && downloadIdToConfig.containsKey(downloadId)) { val config = downloadIdToConfig[downloadId] if (config != null) { val status = downloadStatus.getInt("status") // Handle completed downloads - file is already at final destination if (status == DownloadManager.STATUS_SUCCESSFUL) { val localUri = downloadStatus.getString("localUri") if (localUri != null) { // Verify the file exists at the destination val destFile = File(config.destination) if (!destFile.exists()) { logE(NAME, "Completed download file not found at destination: ${config.destination}") } } } val params = Arguments.createMap() params.putString("id", config.id) params.putString("metadata", config.metadata) val state = stateMap[status] ?: 0 params.putInt("state", state) val bytesDownloaded = downloadStatus.getDouble("bytesDownloaded") params.putDouble("bytesDownloaded", bytesDownloaded) val bytesTotal = downloadStatus.getDouble("bytesTotal") params.putDouble("bytesTotal", bytesTotal) val percent = if (bytesTotal > 0) bytesDownloaded / bytesTotal else 0.0 foundTasks.pushMap(params) processedIds.add(config.id) configIdToDownloadId[config.id] = downloadId progressReporter.setPercent(config.id, percent) } } else if (downloadId != null) { downloader.cancel(downloadId) } } while (cursor.moveToNext()) } } } catch (e: Exception) { logE(NAME, "getExistingDownloadTasks: ${Log.getStackTraceString(e)}") } // Phase 2: Query active UIDT jobs (Android 14+) // On Android 14+, downloads may use UIDT instead of DownloadManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { try { val uidtJobs = UIDTDownloadJobService.getAllActiveJobs() for (job in uidtJobs) { // Skip if already processed from DownloadManager if (processedIds.contains(job.id)) { continue } val params = Arguments.createMap() params.putString("id", job.id) params.putString("metadata", job.metadata) val state = stateMap[job.status] ?: DownloadConstants.TASK_RUNNING params.putInt("state", state) params.putDouble("bytesDownloaded", job.bytesDownloaded.toDouble()) params.putDouble("bytesTotal", job.bytesTotal.toDouble()) params.putString("destination", job.destination) val percent = if (job.bytesTotal > 0) job.bytesDownloaded.toDouble() / job.bytesTotal else 0.0 foundTasks.pushMap(params) processedIds.add(job.id) progressReporter.setPercent(job.id, percent) } } catch (e: Exception) { logE(NAME, "getExistingDownloadTasks UIDT: ${Log.getStackTraceString(e)}") } } // Phase 3: Add paused downloads (persisted across app restarts) val pausedDownloads = downloader.getAllPausedDownloads() for ((configId, pausedInfo) in pausedDownloads) { if (processedIds.contains(configId)) { continue } val params = Arguments.createMap() params.putString("id", configId) params.putString("metadata", pausedInfo.metadata) params.putInt("state", DownloadConstants.TASK_SUSPENDED) // PAUSED state params.putDouble("bytesDownloaded", pausedInfo.bytesDownloaded.toDouble()) params.putDouble("bytesTotal", pausedInfo.bytesTotal.toDouble()) params.putString("destination", pausedInfo.destination) foundTasks.pushMap(params) processedIds.add(configId) } // Phase 4: Add active resumable downloads (in-progress via ResumableDownloader). // These run in the foreground service (Android < 14) and are invisible to // DownloadManager, so without this phase getExistingDownloadTasks misses them // while they are actively downloading (it only saw them once paused, via Phase 3). // On Android 14+ active downloads run as UIDT jobs and are surfaced in Phase 2. for ((configId, state) in downloader.getActiveResumableDownloads()) { if (processedIds.contains(configId) || state.isCancelled.get()) { continue } val params = Arguments.createMap() params.putString("id", configId) params.putString("metadata", configIdToMetadata[configId] ?: "{}") params.putInt( "state", if (state.isPaused.get()) DownloadConstants.TASK_SUSPENDED else DownloadConstants.TASK_RUNNING ) val bytesDownloaded = state.bytesDownloaded.get() val bytesTotal = state.bytesTotal params.putDouble("bytesDownloaded", bytesDownloaded.toDouble()) params.putDouble("bytesTotal", bytesTotal.toDouble()) params.putString("destination", state.destination) val percent = if (bytesTotal > 0) bytesDownloaded.toDouble() / bytesTotal else 0.0 foundTasks.pushMap(params) processedIds.add(configId) progressReporter.setPercent(configId, percent) } logD(NAME, "getExistingDownloadTasks: found ${foundTasks.size()} tasks") } promise.resolve(foundTasks) } @Suppress("UNUSED_PARAMETER") fun addListener(eventName: String) { } @Suppress("UNUSED_PARAMETER") fun removeListeners(count: Int) { } private fun onBeginDownload(configId: String, headers: WritableMap, expectedBytes: Long) { eventEmitter.emitBegin(configId, headers, expectedBytes) } private fun onProgressDownload(configId: String, bytesDownloaded: Long, bytesTotal: Long) { // Delegate all progress handling to ProgressReporter // It handles threshold filtering, batching, and emission progressReporter.reportProgress(configId, bytesDownloaded, bytesTotal) } private fun onSuccessfulDownload(config: RNBGDTaskConfig, downloadStatus: WritableMap) { @Suppress("UNUSED_VARIABLE") val localUri = downloadStatus.getString("localUri") // File is already at the final destination - no need to move it // Verify it exists at the expected location val destinationFile = File(config.destination) if (!destinationFile.exists()) { logE(NAME, "Downloaded file not found at destination: ${config.destination}") val newDownloadStatus = Arguments.createMap() newDownloadStatus.putString("downloadId", downloadStatus.getString("downloadId")) newDownloadStatus.putInt("status", DownloadManager.STATUS_FAILED) newDownloadStatus.putInt("reason", DownloadManager.ERROR_FILE_ERROR) newDownloadStatus.putString("reasonText", "Downloaded file not found at destination") onFailedDownload(config, newDownloadStatus) return } eventEmitter.emitComplete( config.id, config.destination, downloadStatus.getDouble("bytesDownloaded").toLong(), downloadStatus.getDouble("bytesTotal").toLong() ) } private fun onFailedDownload(config: RNBGDTaskConfig, downloadStatus: WritableMap) { logE( NAME, "onFailedDownload: " + "${downloadStatus.getInt("status")}:" + "${downloadStatus.getInt("reason")}:" + downloadStatus.getString("reasonText") ) val reason = downloadStatus.getInt("reason") var reasonText = downloadStatus.getString("reasonText") // Enhanced handling for ERROR_CANNOT_RESUME (1008) if (reason == DownloadManager.ERROR_CANNOT_RESUME) { logW( NAME, "ERROR_CANNOT_RESUME detected for download: ${config.id}" + ". This is a known Android DownloadManager issue with larger files. " + "Consider restarting the download or using smaller file segments." ) // Clean up the failed download entry removeTaskFromMap(downloadStatus.getString("downloadId")?.toLong() ?: 0L) // Provide more helpful error message reasonText = "ERROR_CANNOT_RESUME - Unable to resume download. This may occur with large files due to Android DownloadManager limitations. Try restarting the download." } eventEmitter.emitFailed(config.id, reasonText ?: "Unknown error", reason) } private fun saveDownloadIdToConfigMap() { logD(NAME, "saveDownloadIdToConfigMap() called with ${downloadIdToConfig.size} entries") synchronized(sharedLock) { storageManager.saveDownloadIdToConfigMap(downloadIdToConfig) } } private fun loadDownloadIdToConfigMap() { logD(NAME, "loadDownloadIdToConfigMap() called") synchronized(sharedLock) { downloadIdToConfig = storageManager.loadDownloadIdToConfigMap() logD(NAME, "loadDownloadIdToConfigMap() loaded ${downloadIdToConfig.size} entries") } } private fun saveConfigMap() { synchronized(sharedLock) { storageManager.saveProgressConfig( progressReporter.getProgressInterval(), progressReporter.getProgressMinBytes() ) } } private fun loadConfigMap() { synchronized(sharedLock) { val (interval, minBytes) = storageManager.loadProgressConfig() if (interval > 0 || minBytes > 0) { progressReporter.configure(interval, minBytes) // Sync notification update interval with progress interval for Android 14+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { UIDTDownloadJobService.setNotificationUpdateInterval(interval) } } } } private fun stopTaskProgress(configId: String) { val onProgressFuture = configIdToProgressFuture[configId] if (onProgressFuture != null) { onProgressFuture.cancel(true) configIdToProgressFuture.remove(configId) } // Clear any batched progress report to prevent stale data from being emitted // when a new download starts with the same configId progressReporter.clearPendingReport(configId) } // ============= Upload methods ============= // Listener for upload events private val uploadListener = object : Uploader.UploadListener { override fun onBegin(id: String, expectedBytes: Long) { synchronized(sharedLock) { uploadConfigs[id]?.let { config -> config.reportedBegin = true config.bytesTotal = expectedBytes } } uploadEventEmitter.emitBegin(id, expectedBytes) } override fun onProgress(id: String, bytesUploaded: Long, bytesTotal: Long) { synchronized(sharedLock) { uploadConfigs[id]?.let { config -> config.bytesUploaded = bytesUploaded config.bytesTotal = bytesTotal } } // Use progress reporter for batching uploadProgressReporter.reportProgress(id, bytesUploaded, bytesTotal) } override fun onComplete(id: String, responseCode: Int, responseBody: String, bytesUploaded: Long, bytesTotal: Long) { synchronized(sharedLock) { uploadConfigs[id]?.state = DownloadConstants.TASK_COMPLETED uploadConfigs.remove(id) // Remove from persistent storage on completion storageManager.saveUploadConfigs(uploadConfigs) } uploadEventEmitter.emitComplete(id, responseCode, responseBody, bytesUploaded, bytesTotal) } override fun onError(id: String, error: String, errorCode: Int) { synchronized(sharedLock) { uploadConfigs[id]?.state = DownloadConstants.TASK_CANCELING uploadConfigs.remove(id) // Remove from persistent storage on error storageManager.saveUploadConfigs(uploadConfigs) } uploadEventEmitter.emitFailed(id, error, errorCode) } } fun upload(options: ReadableMap) { // Ensure event emitter is initialized ensureEventEmitterInitialized() val id = options.getString("id") val url = options.getString("url") val source = options.getString("source") val method = if (options.hasKey("method")) options.getString("method") else "POST" val metadata = if (options.hasKey("metadata")) { when (options.getType("metadata")) { ReadableType.String -> options.getString("metadata") ReadableType.Map -> { try { val map = options.getMap("metadata") readableMapToJsonObject(map)?.toString() ?: "{}" } catch (e: Exception) { logW(NAME, "Failed to convert metadata map to string: ${e.message}") "{}" } } else -> null } } else null val fieldName = if (options.hasKey("fieldName")) options.getString("fieldName") else null val mimeType = if (options.hasKey("mimeType")) options.getString("mimeType") else null val headers = if (options.hasKey("headers")) { val headersMap = options.getMap("headers") val result = mutableMapOf() if (headersMap != null) { val iterator = headersMap.keySetIterator() while (iterator.hasNextKey()) { val key = iterator.nextKey() result[key] = headersMap.getString(key) ?: "" } } result } else null val parameters = if (options.hasKey("parameters")) { val paramsMap = options.getMap("parameters") val result = mutableMapOf() if (paramsMap != null) { val iterator = paramsMap.keySetIterator() while (iterator.hasNextKey()) { val key = iterator.nextKey() result[key] = paramsMap.getString(key) ?: "" } } result } else null // Progress settings val progressIntervalScope = options.getInt("progressInterval") val progressMinBytesScope = options.getDouble("progressMinBytes").toLong() if (progressIntervalScope > 0 || progressMinBytesScope > 0) { val newInterval = if (progressIntervalScope > 0) progressIntervalScope.toLong() else uploadProgressReporter.getProgressInterval() val newMinBytes = if (progressMinBytesScope > 0) progressMinBytesScope else uploadProgressReporter.getProgressMinBytes() uploadProgressReporter.configure(newInterval, newMinBytes) } if (id == null || url == null || source == null) { logE(NAME, "upload: id, url and source must be set.") return } val config = RNBGDUploadTaskConfig( id = id, url = url, source = source, metadata = metadata ?: "{}", method = method ?: "POST", headers = headers, fieldName = fieldName, mimeType = mimeType, parameters = parameters ) synchronized(sharedLock) { uploadConfigs[id] = config uploadProgressReporter.initializeDownload(id) // Persist upload config for app restart recovery storageManager.saveUploadConfigs(uploadConfigs) } uploader.startUpload(config, uploadListener) } fun pauseUploadTask(configId: String) { synchronized(sharedLock) { uploadConfigs[configId]?.state = DownloadConstants.TASK_SUSPENDED // Persist state change storageManager.saveUploadConfigs(uploadConfigs) } uploader.pause(configId) logD(NAME, "Paused upload: $configId") } fun resumeUploadTask(configId: String) { synchronized(sharedLock) { uploadConfigs[configId]?.state = DownloadConstants.TASK_RUNNING // Persist state change storageManager.saveUploadConfigs(uploadConfigs) } uploader.resume(configId, uploadListener) logD(NAME, "Resumed upload: $configId") } fun stopUploadTask(configId: String) { synchronized(sharedLock) { uploadConfigs.remove(configId) uploadProgressReporter.clearDownloadState(configId) // Remove from persistent storage storageManager.saveUploadConfigs(uploadConfigs) } uploader.cancel(configId) logD(NAME, "Stopped upload: $configId") } fun getExistingUploadTasks(promise: Promise) { val foundTasks = Arguments.createArray() synchronized(sharedLock) { for ((id, config) in uploadConfigs) { val uploadState = uploader.getState(id) val params = Arguments.createMap() params.putString("id", config.id) params.putString("metadata", config.metadata) val state = when { uploadState?.isPaused?.get() == true -> DownloadConstants.TASK_SUSPENDED uploadState?.isCancelled?.get() == true -> DownloadConstants.TASK_CANCELING uploadState != null -> DownloadConstants.TASK_RUNNING else -> config.state } params.putInt("state", state) val bytesUploaded = uploadState?.bytesUploaded?.get() ?: config.bytesUploaded val bytesTotal = uploadState?.bytesTotal ?: config.bytesTotal params.putDouble("bytesUploaded", bytesUploaded.toDouble()) params.putDouble("bytesTotal", bytesTotal.toDouble()) foundTasks.pushMap(params) } } promise.resolve(foundTasks) } }