smanikandan14 / ThinDownloadManager

To download files and to avoid using DOWNLOAD_WITHOUT_NOTIFICATION permission if you are using Android's DownloadManager in your apps.
Apache License 2.0
784 stars 186 forks source link

Feature request: support resume loading after disconnect #48

Open akadatsky opened 8 years ago

akadatsky commented 8 years ago

Any plans to support reconnect feature?

ghost commented 8 years ago

maybe .setRetryPolicy(new DefaultRetryPolicy())? ThinDownloadManager has this.

akadatsky commented 8 years ago

Hi @JackLauu

Enven on main page noted: NOTE: Android's DownloadManager has plenty of features which is not available in ThinDownloadManager. For ex. pause and continue download when network connectivity changes.

Yes, DefaultRetryPolicy is in sample.

The problem is: if we lose connection, for example for 1 second, or server close connection on it side due to 502 or 503 error, it can't restart loading. Default DownloadManager can do it.

Android DownloadManager in this case at first sends partial content request, which return just size of file, and then start loading. If connection lost, it sends range request, something like this "Range: bytes=31246-58342" and continue to load from that place where it lost connection.

smanikandan14 commented 8 years ago

@akadatsky You are right. ThinDownloadManager doesn't keep track of how many bytes so far downloaded and if connection breaks it deletes the file which is not fully downloaded at the time. The general advice is if you are downloading a very large file in terms of 100MBs, stick with android download manager.

akadatsky commented 8 years ago

@smanikandan14 I can't due to of DOWNLOAD_WITHOUT_NOTIFICATION permission. I believe it's not hard to do in the technical plan, or ThinDownloadManager arch is not designed for this? Or you have no desire to maintain?

matyasatfp commented 8 years ago

+1 for this feature

BenOien commented 8 years ago

I may be implementing this depending on my current project requirements. I'll update this thread in the next couple weeks either way.

BenOien commented 8 years ago

DownloadRequest can now be set to not delete files on download failure. It also supports html headers for setting range information. I am currently looking into whether this is all that is nessisary. If no changes are required, I'll post a few tips. Otherwise, I'll be implementing this.

BenOien commented 8 years ago

There is a one line change required in ThinDownloadManager to enable resuming using the aforementioned headers. I will put together a quick pull request when I get to my computer later today. I will also post some example code in the pull request description.

BenOien commented 8 years ago

See this pull request.

smanikandan14 commented 8 years ago

@PrimeKang How can we test this ? Any prescribed steps.

BenOien commented 8 years ago

@smanikandan14

To test this pull request:

  1. upload a small test binary with known values.
  2. Download using the Range header value to grab a handful of bytes from the middle . downloadRequest.addCustomHeader("Range", "bytes="+startByteZeroIndexed+"-"+endByteOrOmittedForEOF);
  3. Compare with expected values

To test the sudocode I provided in the description of the pull request:

  1. create a part1 file with the same part1-postfix
  2. write a few recognizable bytes to it
  3. start a resumable request for a very small binary file on dropbox that has more bytes in it than your part1 file, It also must not start with your part1 test bytes.
  4. read the completed file into a byte buffer
  5. check that the first bytes match your test part1 bytes
  6. check that the remainder of the buffer matches the same part of the remote file.
  7. Celebrate great success :)