Skip to content

Commit

Permalink
Multi Download Support Added
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofrancis committed Apr 9, 2017
1 parent 3e6f88d commit 91376d5
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 58 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/tonyodev/fetchdemo/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void onCreate() {
new Fetch.Settings(this)
.setAllowedNetwork(Fetch.NETWORK_ALL)
.enableLogging(true)
.setConcurrentDownloadsLimit(1)
.apply();

fetch = Fetch.getInstance(this);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/tonyodev/fetchdemo/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class Data {
"http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_640x360.m4v",
"http://media.mongodb.org/zips.json",
"http://www.example/some/unknown/123/Errorlink.txt",
"http://www.drodd.com/gump/boxachoc.mp3",
"http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_640x360.m4v",
"http://storage.googleapis.com/ix_choosemuse/uploads/2016/02/android-logo.png"};

private Data() {
Expand All @@ -38,7 +38,7 @@ static List<Request> getFetchRequests() {
return requests;
}

private static String getFilePath(String url) {
public static String getFilePath(String url) {

Uri uri = Uri.parse(url);

Expand Down
19 changes: 19 additions & 0 deletions fetch/src/main/java/com/tonyodev/fetch/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,25 @@ synchronized Cursor getNextPendingRequest() {
+ FetchConst.STATUS_QUEUED + " LIMIT 1" ,null);
}

synchronized boolean hasPendingRequests() {

Cursor cursor = db.rawQuery("SELECT " + COLUMN_ID + " FROM "
+ TABLE_NAME + " WHERE " + COLUMN_STATUS + " = "
+ FetchConst.STATUS_QUEUED + " LIMIT 1" ,null);

boolean hasPending = false;

if(cursor != null && cursor.getCount() > 0) {
hasPending = true;
}

if(cursor != null) {
cursor.close();
}

return hasPending;
}

synchronized void verifyOK() {

try {
Expand Down
21 changes: 19 additions & 2 deletions fetch/src/main/java/com/tonyodev/fetch/Fetch.java
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,23 @@ public void enableLogging(boolean enabled) {
new Settings(context).enableLogging(enabled).apply();
}

/**
* Sets the allowed concurrent downloads value between 1-7. The default is 1
* and the max is 7. The Fetch Service will only allow up to the MAX concurrent downloads.
*
* <p>See Fetch.DEFAULT_DOWNLOADS_LIMIT and Fetch.MAX_DOWNLOADS_LIMIT
*
* @param limit concurrent downloads limit
*
* @throws NotUsableException if the release method has been called on Fetch.
*
* */
public void setConcurrentDownloadsLimit(int limit) {

Utils.throwIfNotUsable(this);
new Settings(context).setConcurrentDownloadsLimit(limit).apply();
}

/**
* The Settings class is used to apply
* settings to Fetch and the FetchService.
Expand Down Expand Up @@ -1039,8 +1056,8 @@ public Settings setAllowedNetwork(int networkType) {
}

/**
* Sets the allowed concurrent downloads by the Fetch Service between 1-7. The default is 1
* and the max is 7. The Fetch Service will only allow up to 7 concurrent downloads.
* Sets the allowed concurrent downloads value between 1-7. The default is 1
* and the max is 7. The Fetch Service will only allow up to the MAX concurrent downloads.
*
* <p>See Fetch.DEFAULT_DOWNLOADS_LIMIT and Fetch.MAX_DOWNLOADS_LIMIT
*
Expand Down
7 changes: 4 additions & 3 deletions fetch/src/main/java/com/tonyodev/fetch/FetchRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ public void run() {
progress = Utils.getProgress(downloadedBytes,fileSize);
}

databaseHelper.updateStatus(id,FetchConst.STATUS_DOWNLOADING,FetchConst.DEFAULT_EMPTY_VALUE);

output = new RandomAccessFile(filePath,"rw");
if(responseCode == HttpURLConnection.HTTP_PARTIAL) {
output.seek(downloadedBytes);
Expand All @@ -148,7 +146,10 @@ public void run() {

databaseHelper.updateFileBytes(id,downloadedBytes,fileSize);

if(downloadedBytes >= fileSize && !isInterrupted()) {
if (isInterrupted()) {
throw new DownloadInterruptedException("DIE",ErrorUtils.DOWNLOAD_INTERRUPTED);

}else if(downloadedBytes >= fileSize && !isInterrupted()) {

if(fileSize < 1) {
fileSize = Utils.getFileSize(filePath);
Expand Down
Loading

0 comments on commit 91376d5

Please sign in to comment.