Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/_coverpage.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- _coverpage.md -->

# Android Http Download Manager <small>2.0.0</small>
# Android Http Download Manager <small>2.1.0</small>

[GitHub](https://github.com/coolerfall/Android-HttpDownloadManager)
[Get Started](#android-http-download-manager)
82 changes: 38 additions & 44 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,48 @@

You can costomize `DownloadManager` with the fllowing configurations.


### context

* Type: `android.content.Context`
* Optional: `false`

Application context to get download root directory, this cannot be null.

### downloader

* Type: `com.coolerfall.download.Downloader`
* Optional: `true`

You can implement a `Downloader` with any http library. The download manager will detect http library and choose available `Downloader` to use if not set. The download manager provides `OkHttpDownloader` and `URLDownloader` currently.
You can implement a `Downloader` with any http library. The download manager will detect http
library and choose available `Downloader` to use if not set. The download manager
provides `OkHttpDownloader` and `URLDownloader` currently.

!> If you're using `OkHttpDownloader` with custom `OkHttpClient` as `Downloader` in `DownloadManager`, then you should not add `HttpLoggingInterceptor` in your custom `OkHttpClient`. It may be crashed(OOM) as `HttpLoggingInterceptor ` use `okio` to reqeust the whole body in memory.
!> If you're using `OkHttpDownloader` with custom `OkHttpClient` as `Downloader`
in `DownloadManager`, then you should not add `HttpLoggingInterceptor` in your custom `OkHttpClient`
. It may be crashed(OOM) as `HttpLoggingInterceptor ` use `okio` to reqeust the whole body in
memory.

### threadPoolSize

* Type: `int`
* Default: `3`

The pool size of the download dispatcher thread. The default size will be set if less than 0 or more than 10.
The pool size of the download dispatcher thread. The default size will be set if less than 0 or more
than 10.

### retryTime

* Type: `int`
* Default: `3`

How many times you want to retry if download failed for some network problem and so on.

### retryInterval

* Type `long, java.util.concurrent.TimeUnit`
* Default: `30 seconds`

Interval of each retry.

### progressInterval

* Type `long, java.util.concurrent.TimeUnit`
* Default: `100 milliseconds`

Interval of progress refreshing.

### logger

Expand All @@ -33,8 +52,7 @@ The pool size of the download dispatcher thread. The default size will be set if

Log necessary information when downloading. If you don't care this, just ignore.

> If you want to copy files to external public download directory, `DownloadManager` provides `copyToPublicDownloadDir(String filepath)`.

> If you want to custom `DownloadManager`, then use `DownloadManager.with(builder)`.

## Download Request

Expand All @@ -59,19 +77,16 @@ This is an alternative of [url](#url), you can choose one to set.

A unique id of `DownloadRequest`, it will be set automatically if not set.

### relativeDirectory
### pack

* Type: `String`
* Optional: `true`
* Type: `Pack`
* Optional: `false`

After android Q, we can just save files in external private directory and public download directory, so the download manager will use `Context#getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)` as download root directory. The download manager will extract filename from header or url.
`Pack` is the target where the file will be put. Some builtin pack:

### relativeFilepath

* Type: `String`
* Optional: `true`

Set filepath mannully. The download manager will use this filepath instead of auto detecting.
* ExtPublicPack: put file in external public directory such public `Download` directory,
see `Environment.getExternalStoragePublicDirectory`
* ExtFilePack: put file in external files directory, see `Context.getExternalFilesDir`

### priority

Expand All @@ -80,27 +95,6 @@ Set filepath mannully. The download manager will use this filepath instead of au

Higher priority will download first.

### retryTime

* Type: `int`
* Default: `3`

How many times you want to retry if download failed for some network problem and so on.

### retryInterval

* Type `long, java.util.concurrent.TimeUnit`
* Default: `30 seconds`

Interval of each retry.

### progressInterval

* Type `long, java.util.concurrent.TimeUnit`
* Default: `100 milliseconds`

Interval of progress refreshing.

### downloadCallback

* Type: `com.coolerfall.download.DownloadCallback`
Expand Down
7 changes: 3 additions & 4 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ implementation 'com.coolerfall:android-http-download-manager:2.0.0'
* Now download a file with this:

```java
DownloadManager manager = new DownloadManager.Builder()
.context(this)
.build();
import com.coolerfall.download.DownloadManager;
import com.coolerfall.download.DownloadRequest;

DownloadRequest request = new DownloadRequest.Builder()
.url("http://something.to.download")
.build();

int downloadId = manager.add(request);
int downloadId = DownloadManager.get().enqueue(request);
```

For more details, see [configuration](/configuration)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

GROUP=com.coolerfall
POM_ARTIFACT_ID=android-http-download-manager
VERSION_NAME=2.0.1-SNAPSHOT
VERSION_NAME=2.1.0-SNAPSHOT

POM_NAME=Android Http Download Manager
POM_DESCRIPTION=An useful and effective http/https download manager for Android, support breakpoint downloading
Expand Down
25 changes: 25 additions & 0 deletions library/src/main/java/com/coolerfall/download/BreakpointPack.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.coolerfall.download

/**
* This [Pack] represents a pack which supports breakpoint transfer.
*
* @author Vincent Cheung (coolingfall@gmail.com)
*/
abstract class BreakpointPack(filename: String?) : Pack(filename) {

/**
* Get pending length if pending file exists.
*
* @return pending length
*/
internal abstract fun pendingLength(): Long

/**
* Get a unique filename for pending file.
*
* @return unique filename
*/
internal fun uniqueFilename(): String {
return Helper.md5(requireFilename()) + "-" + requireFilename()
}
}
44 changes: 44 additions & 0 deletions library/src/main/java/com/coolerfall/download/DirectFilePack.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.coolerfall.download

import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream

/**
* A [Pack] with direct [File] api operation.
*
* @author Vincent Cheung (coolingfall@gmail.com)
*/
open class DirectFilePack(
private val dir: String, filename: String? = null
) : BreakpointPack(filename) {

init {
val directory = File(dir)
if (!directory.exists()) {
directory.mkdirs()
}
}

override fun pendingLength(): Long {
return File(pendingPath()).length()
}

override fun open(): OutputStream {
return FileOutputStream(pendingPath(), true)
}

override fun finish() {
val filepath = Helper.resolvePath(dir, requireFilename())
File(pendingPath()).renameTo(File(filepath))
}

/**
* Get a temporary filename when transferring.
*
* @return pending filename
*/
private fun pendingPath(): String {
return Helper.resolvePath(dir, ".pending-" + uniqueFilename())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ interface DownloadCallback {
* @param downloadId download id in download request queue
*/
@MainThread
fun onRetry(downloadId: Int) {}
fun onRetry(downloadId: Int) {
}

/**
* Invoked when downloading is in progress.
Expand All @@ -48,10 +49,25 @@ interface DownloadCallback {
* @param downloadId download id in download request queue
* @param filepath the filepath of downloaded file
*/
@Deprecated(
level = DeprecationLevel.ERROR,
message = "Use pack instead",
replaceWith = ReplaceWith("onSuccess(downloadId, pack)")
)
@MainThread
fun onSuccess(downloadId: Int, filepath: String) {
}

/**
* Invoked when downloading successfully.
*
* @param downloadId download id in download request queue
* @param pack the target [Pack]
*/
@MainThread
fun onSuccess(downloadId: Int, pack: Pack) {
}

/**
* Invoked when downloading failed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.coolerfall.download

import android.os.Handler
import java.util.concurrent.Executor
import java.lang.Runnable

/**
* This class is used to delivery callback to call back in main thread.
Expand Down Expand Up @@ -63,7 +62,7 @@ internal class DownloadDelivery(handler: Handler) {
*/
fun postSuccess(request: DownloadRequest) {
downloadPoster.execute {
request.downloadCallback.onSuccess(request.downloadId, request.destinationFilepath())
request.downloadCallback.onSuccess(request.downloadId, request.pack)
}
}

Expand Down
Loading