Skip to content

Commit

Permalink
feat: support set connection count through task-builder
Browse files Browse the repository at this point in the history
refs #31
  • Loading branch information
Jacksgong committed Apr 27, 2018
1 parent c799b63 commit b4d90a5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.liulishuo.okdownload;

import android.net.Uri;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.SparseArray;
Expand Down Expand Up @@ -65,6 +66,8 @@ public class DownloadTask extends IdentifiedTask implements Cloneable, Comparabl
private final int syncBufferSize;
private final int syncBufferIntervalMills;

private final Integer connectionCount;

/**
* if this task has already completed with
*/
Expand Down Expand Up @@ -93,7 +96,7 @@ public DownloadTask(String url, Uri uri, int priority, int readBufferSize, int f
boolean autoCallbackToUIThread, int minIntervalMillisCallbackProcess,
Map<String, List<String>> headerMapFields, @Nullable String filename,
boolean passIfAlreadyCompleted, boolean isWifiRequired,
Boolean isFilenameFromResponse) {
Boolean isFilenameFromResponse, Integer connectionCount) {
this.url = url;
this.uri = uri;
this.priority = priority;
Expand All @@ -107,6 +110,7 @@ public DownloadTask(String url, Uri uri, int priority, int readBufferSize, int f
this.lastCallbackProcessTimestamp = new AtomicLong();
this.passIfAlreadyCompleted = passIfAlreadyCompleted;
this.isWifiRequired = isWifiRequired;
this.connectionCount = connectionCount;

if (Util.isUriFileScheme(uri)) {
final File file = new File(uri.getPath());
Expand Down Expand Up @@ -355,6 +359,25 @@ public int getMinIntervalMillisCallbackProcess() {
return minIntervalMillisCallbackProcess;
}

/**
* Get the connection count you have been set through {@link Builder#setConnectionCount(int)}
*
* @return the connection count you set.
*/
public Integer getSetConnectionCount() {
return connectionCount;
}

/**
* Get the connection count is effect on this task.
*
* @return the connection count.
*/
public int getConnectionCount() {
if (info == null) return 0;
return info.getBlockCount();
}

/**
* Get the tag with its {@code key}, which you set through {@link #addTag(int, Object)}.
*
Expand Down Expand Up @@ -651,6 +674,18 @@ public Builder(@NonNull String url, @NonNull Uri uri) {
private boolean isWifiRequired = DEFAULT_IS_WIFI_REQUIRED;

private Boolean isFilenameFromResponse;
private Integer connectionCount;

/**
* Set the count of connection establish for this task, if this task has already split block
* on the past and waiting for resuming, this set connection count will not effect really.
*
* @param connectionCount the count of connection establish for this task.
*/
public Builder setConnectionCount(@IntRange(from = 1) int connectionCount) {
this.connectionCount = connectionCount;
return this;
}

/**
* Set whether the provided Uri or path is just directory, and filename must be from
Expand Down Expand Up @@ -837,7 +872,7 @@ public DownloadTask build() {
syncBufferSize, syncBufferIntervalMillis,
autoCallbackToUIThread, minIntervalMillisCallbackProcess,
headerMapFields, filename, passIfAlreadyCompleted, isWifiRequired,
isFilenameFromResponse);
isFilenameFromResponse, connectionCount);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public ResumeAvailableResponseCheck resumeAvailableResponseCheck(
}

public int determineBlockCount(@NonNull DownloadTask task, long totalLength) {
if (task.getSetConnectionCount() != null) return task.getSetConnectionCount();

if (totalLength < ONE_CONNECTION_UPPER_LIMIT) {
return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,13 @@ public void constructor_path() {
.Builder("https://jacksgong.com", new File(parentPath))
.setFilenameFromResponse(false)
.build();

// connection count.
task = new DownloadTask
.Builder("https://jacksgong.com", "not-exist", null)
.setConnectionCount(2)
.build();
assertThat(task.getSetConnectionCount()).isEqualTo(2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ private DownloadStrategy.ResumeAvailableResponseCheck resumeAvailableResponseChe

@Test
public void determineBlockCount() {
when(task.getSetConnectionCount()).thenReturn(null);

// less than 1M
assertThat(strategy.determineBlockCount(task, 500)).isEqualTo(1);
assertThat(strategy.determineBlockCount(task, 900 * 1024))
Expand Down Expand Up @@ -242,6 +244,10 @@ public void determineBlockCount() {
.isEqualTo(5);
assertThat(strategy.determineBlockCount(task, 5323L * 1024 * 1024))
.isEqualTo(5);

// task has connection count
when(task.getSetConnectionCount()).thenReturn(100);
assertThat(strategy.determineBlockCount(task, 500)).isEqualTo(100);
}

@Test
Expand Down

0 comments on commit b4d90a5

Please sign in to comment.