Note on FileDownloader2 (OkDownload)
masterFileDownloader2-OkDownload) for new projects. All new features and enhancements are being implemented in OkDownload, while FileDownloader is primarily focused on bug fixes due to low unit-test coverage.repository·master·Indexed 11 days ago
https://github.com/lingochamp/filedownloaderAn Android multi-task file download engine supporting chunked, serial, and parallel tasks. It features customizable components for connections, output streams, and databases, including specialized implementations like RemitDatabase for high-frequency small tasks and NoDatabaseImpl for database-less downloading. Supports foreground services for Android 8.0 and 9.0, multi-connection downloads, and serial download queues.
FileDownloader2-OkDownload) for new projects. All new features and enhancements are being implemented in OkDownload, while FileDownloader is primarily focused on bug fixes due to low unit-test coverage.FileDownloader is an Android file download engine with the following capabilities:
ConnectionCountAdapter.paused. To resume a download, simply call start() again. By default, the engine performs breakpoint resumption (resuming from where it left off).To prevent high-frequency callbacks from causing UI frame drops (especially during high-concurrency file detection), FileDownloader uses an 'avoid drop frame' mechanism. By default, this is enabled. It limits the frequency of messages sent to the UI thread and batches the number of callbacks processed per message.
Key configuration methods:
FileDownloader.getImpl().disableAvoidDropFrame(): Disables the mechanism. All callbacks will be sent to the UI thread immediately, which may cause UI stuttering under high load.FileDownloader.setGlobalPost2UIInterval(intervalMillisecond: int): Sets the maximum interval (in milliseconds) between messages sent to the UI thread. Setting this to a value < 0 disables the mechanism.FileDownloader.setGlobalHandleSubPackageSize(packageSize: int): Sets how many callbacks are processed per message on the UI thread. Default is 5.FileDownloader.isEnabledAvoidDropFrame(): Checks if the mechanism is currently enabled.// Enable/Disable and configure the avoid-drop-frame mechanism
FileDownloader.getImpl().enableAvoidDropFrame();
FileDownloader.getImpl().setGlobalPost2UIInterval(10); // 10ms interval
FileDownloader.getImpl().setGlobalHandleSubPackageSize(5);FileDownloader manages memory by separating the UI process (non-download process) from the download process (service process). This architecture ensures download continuity even when the system is under memory pressure.
This process holds queue data. Its behavior depends on its state when memory is reclaimed:
This process uses buffers and is an active service process, making it a low-priority target for memory reclamation compared to background apps.
Resilience and Recovery:
If the download process is reclaimed, it will attempt to restart automatically via START_STICKY. When the system provides sufficient memory, the download process restarts, reconnects to the UI process, and resumes all pending tasks (both parallel and serial) using breakpoint resumption (resuming from where they left off). This ensures a seamless experience.
Note on Service Lifecycle: To prevent the service from restarting automatically, you can manually call:
FileDownloader#unBindServiceFileDownloader#unBindServiceIfIdle// To prevent automatic service restart if desired:
FileDownloader.unBindService(context);
// OR
FileDownloader.unBindServiceIfIdle(context);When downloading files larger than 1.99GB ($2^{31}-1 = 2,147,483,647$ bytes), the standard FileDownloadListener may encounter issues. To correctly handle large files, you must use the following specialized components:
FileDownloadLargeFileListener instead of FileDownloadListener.getLargeFileSofarBytes() and getLargeFileTotalBytes() to retrieve progress data.By default, the engine includes processing to avoid frame drops. This prevents the FileDownloadListener from sending callbacks too frequently, which could otherwise overwhelm the UI thread. If you need to disable this and revert to the behavior of version 0.1.9 (where all callbacks are immediately dispatched to the UI thread via a Handler), you can adjust the engine configuration.
To reduce the I/O overhead caused by Inter-Process Communication (IPC) during callbacks, set process.non-separate to true in your filedownloader.properties configuration file, unless you have a specific requirement for independent processes.
By default, the FileDownloadService runs in a separate process.
filedownloader.properties file.FileDownloadUtils.isDownloaderProcess(Context) to determine if the service is currently running in the downloader process or the main process.Starting from version 1.5.0, FileDownloader supports multi-connection (multi-threaded) downloads for a single task. You can customize the number of connections used per task by implementing a ConnectionCountAdapter and providing it during initialization.
By default, the connection strategy is based on file size:
FileDownloader allows you to replace its default components with your own implementations. This is useful if you want to use okhttp for connections or if you want to disable the database entirely.
Supported customizable components:
| Name | Interface | Default Impl |
|---|---|---|
| Connection | FileDownloadConnection | FileDownloadUrlConnection |
| OutputStream | FileDownloadOutputStream | FileDownloadRandomAccessFile |
| Database | FileDownloadDatabase | RemitDatabase |
| ConnectionCountAdapter | ConnectionCountAdapter | DefaultConnectionCountAdapter |
| IdGenerator | IdGenerator | DefaultIdGenerator |
| ForegroundServiceConfig | ForegroundServiceConfig | ForegroundServiceConfig |
Tips:
okhttp as the connection component, you can use a dedicated adapter like filedownloader-okhttp3-connection.NoDatabaseImpl.java.Introduced in version 1.6.9, RemitDatabase is a specialized database implementation designed to handle a large number of very small tasks (e.g., tasks completing in < 2 seconds) without overwhelming the file system with frequent I/O operations.
How it works:
pause or error state, the final status update is always persisted to both memory and the database to ensure reliability.Due to background service restrictions in Android 8.0+, FileDownloader (starting from version 1.7.6) automatically runs as a foreground service when started from the background.