Skip to content

Commit

Permalink
Inital support to concurrent downloads
Browse files Browse the repository at this point in the history
part1
  • Loading branch information
tonyofrancis committed Apr 8, 2017
1 parent b40f581 commit 3e6f88d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.3.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
20 changes: 20 additions & 0 deletions fetch/src/main/java/com/tonyodev/fetch/Fetch.java
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,26 @@ public Settings setAllowedNetwork(int networkType) {
return this;
}

/**
* 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.
*
* <p>See Fetch.DEFAULT_DOWNLOADS_LIMIT and Fetch.MAX_DOWNLOADS_LIMIT
*
* @param limit concurrent downloads limit
*
* @return the settings instance
* */
public Settings setConcurrentDownloadsLimit(int limit) {

Bundle extras = new Bundle();
extras.putInt(FetchService.ACTION_TYPE,FetchService.ACTION_CONCURRENT_DOWNLOADS_LIMIT);
extras.putInt(FetchService.EXTRA_CONCURRENT_DOWNLOADS_LIMIT,limit);
settings.add(extras);

return this;
}

/**
* Apply the new settings to Fetch and the FetchService
* */
Expand Down
10 changes: 10 additions & 0 deletions fetch/src/main/java/com/tonyodev/fetch/FetchConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,14 @@ interface FetchConst {
* Default empty value of a Field.
* */
int DEFAULT_EMPTY_VALUE = DatabaseHelper.EMPTY_COLUMN_VALUE;

/**
* Default concurrent downloads limit.
* */
int DEFAULT_DOWNLOADS_LIMIT = 1;

/**
* Max concurrent downloads limit.
* */
int MAX_DOWNLOADS_LIMIT = 7;
}
27 changes: 26 additions & 1 deletion fetch/src/main/java/com/tonyodev/fetch/FetchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -72,6 +73,7 @@ public final class FetchService extends Service implements FetchConst {
public static final String EXTRA_PRIORITY = "com.tonyodev.fetch.extra_priority";
public static final String EXTRA_QUERY_TYPE = "com.tonyodev.fetch.extra_query_type";
public static final String EXTRA_LOGGING_ID = "com.tonyodev.fetch.extra_logging_id";
public static final String EXTRA_CONCURRENT_DOWNLOADS_LIMIT = "com.tonyodev.fetch.extra_concurrent_download_limit";

public static final String ACTION_TYPE = "com.tonyodev.fetch.action_type";

Expand All @@ -86,6 +88,7 @@ public final class FetchService extends Service implements FetchConst {
public static final int ACTION_RETRY = 318;
public static final int ACTION_REMOVE_ALL = 319;
public static final int ACTION_LOGGING = 320;
public static final int ACTION_CONCURRENT_DOWNLOADS_LIMIT = 321;

public static final int QUERY_SINGLE = 480;
public static final int QUERY_ALL = 481;
Expand All @@ -102,10 +105,12 @@ public final class FetchService extends Service implements FetchConst {
private volatile boolean fetchRunnableQueued = false;
private volatile boolean runningTask = false;
private volatile boolean shuttingDown = false;
private int downloadsLimit = DEFAULT_DOWNLOADS_LIMIT;
private boolean loggingEnabled = true;
private int preferredNetwork = NETWORK_ALL;
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final List<BroadcastReceiver> registeredReceivers = new ArrayList<>();
private final ConcurrentHashMap<Long,FetchRunnable> fetchRunnableMap = new ConcurrentHashMap<>();

public static void sendToService(@NonNull Context context,@Nullable Bundle extras) {

Expand Down Expand Up @@ -163,7 +168,7 @@ public void onCreate() {
databaseHelper = DatabaseHelper.getInstance(context);
broadcastManager.registerReceiver(doneReceiver,FetchRunnable.getDoneFilter());
registeredReceivers.add(doneReceiver);

downloadsLimit = getDownloadsLimit();
preferredNetwork = getAllowedNetwork();
loggingEnabled = isLoggingEnabled();
databaseHelper.setLoggingEnabled(loggingEnabled);
Expand Down Expand Up @@ -288,6 +293,11 @@ public void run() {
removeAll();
break;
}
case ACTION_CONCURRENT_DOWNLOADS_LIMIT: {
int limit = intent.getIntExtra(EXTRA_CONCURRENT_DOWNLOADS_LIMIT,DEFAULT_DOWNLOADS_LIMIT);
setDownloadsLimit(limit);
break;
}
default: {
startDownload();
break;
Expand Down Expand Up @@ -665,6 +675,21 @@ private void sendEventQuery(long queryId,ArrayList<Bundle> results) {
broadcastManager.sendBroadcast(intent);
}

private int getDownloadsLimit() {
return sharedPreferences.getInt(EXTRA_CONCURRENT_DOWNLOADS_LIMIT,DEFAULT_DOWNLOADS_LIMIT);
}

private void setDownloadsLimit(int limit) {

if(limit > MAX_DOWNLOADS_LIMIT || limit < DEFAULT_DOWNLOADS_LIMIT) {
limit = DEFAULT_DOWNLOADS_LIMIT;
}

sharedPreferences.edit().putInt(EXTRA_CONCURRENT_DOWNLOADS_LIMIT,limit).apply();
downloadsLimit = limit;
startDownload();
}

private void setLoggingEnabled(boolean enabled) {
sharedPreferences.edit().putBoolean(EXTRA_LOGGING_ID,enabled).apply();
loggingEnabled = enabled;
Expand Down

0 comments on commit 3e6f88d

Please sign in to comment.