Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to download only via wifi #500

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public class DownloadTask {
String notificationTitle;
long timeCreated;
boolean saveInPublicStorage;
boolean allowCellular;

DownloadTask(int primaryId, String taskId, int status, int progress, String url, String filename, String savedDir,
String headers, String mimeType, boolean resumable, boolean showNotification, boolean openFileFromNotification, String notificationTitle, long timeCreated, boolean saveInPublicStorage) {
String headers, String mimeType, boolean resumable, boolean showNotification, boolean openFileFromNotification, String notificationTitle, long timeCreated, boolean saveInPublicStorage, boolean allowCellular) {
this.primaryId = primaryId;
this.taskId = taskId;
this.status = status;
Expand All @@ -34,10 +35,11 @@ public class DownloadTask {
this.notificationTitle = notificationTitle;
this.timeCreated = timeCreated;
this.saveInPublicStorage = saveInPublicStorage;
this.allowCellular = allowCellular;
}

@Override
public String toString() {
return "DownloadTask{taskId=" + taskId + ", status=" + status + ", progress=" + progress + ", url=" + url + ", filename=" + filename + ", savedDir=" + savedDir + ", headers=" + headers + ", notificationTitle=" + notificationTitle + ", saveInPublicStorage= " + saveInPublicStorage + "}";
return "DownloadTask{taskId=" + taskId + ", status=" + status + ", progress=" + progress + ", url=" + url + ", filename=" + filename + ", savedDir=" + savedDir + ", headers=" + headers + ", notificationTitle=" + notificationTitle + ", saveInPublicStorage= " + saveInPublicStorage + ", allowCellular=" + allowCellular + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {

private WorkRequest buildRequest(String url, String savedDir, String filename, String headers,
boolean showNotification, boolean openFileFromNotification, String notificationTitle,
boolean isResume, boolean requiresStorageNotLow, boolean saveInPublicStorage) {
boolean isResume, boolean requiresStorageNotLow, boolean saveInPublicStorage,
boolean allowCellular) {
WorkRequest request = new OneTimeWorkRequest.Builder(DownloadWorker.class)
.setConstraints(new Constraints.Builder()
.setRequiredNetworkType(allowCellular ? NetworkType.CONNECTED : NetworkType.UNMETERED)
.setRequiresStorageNotLow(requiresStorageNotLow)
.setRequiredNetworkType(NetworkType.CONNECTED)
.build())
.addTag(TAG)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 10, TimeUnit.SECONDS)
Expand Down Expand Up @@ -176,14 +177,17 @@ private void enqueue(MethodCall call, MethodChannel.Result result) {
String notificationTitle = call.argument("notification_title");
boolean requiresStorageNotLow = call.argument("requires_storage_not_low");
boolean saveInPublicStorage = call.argument("save_in_public_storage");
boolean allowCellular = call.argument("allow_cellular");
WorkRequest request = buildRequest(url, savedDir, filename, headers, showNotification,
openFileFromNotification, notificationTitle, false, requiresStorageNotLow, saveInPublicStorage);
openFileFromNotification, notificationTitle, false, requiresStorageNotLow, saveInPublicStorage,
allowCellular);
WorkManager.getInstance(context).enqueue(request);
String taskId = request.getId().toString();
result.success(taskId);
sendUpdateProgress(taskId, DownloadStatus.ENQUEUED, 0);
taskDao.insertOrUpdateNewTask(taskId, url, DownloadStatus.ENQUEUED, 0, filename,
savedDir, headers, showNotification, openFileFromNotification, notificationTitle, saveInPublicStorage);
savedDir, headers, showNotification, openFileFromNotification, notificationTitle, saveInPublicStorage,
allowCellular);
}

private void loadTasks(MethodCall call, MethodChannel.Result result) {
Expand Down Expand Up @@ -257,7 +261,7 @@ private void resume(MethodCall call, MethodChannel.Result result) {
if (partialFile.exists()) {
WorkRequest request = buildRequest(task.url, task.savedDir, task.filename,
task.headers, task.showNotification, task.openFileFromNotification,
task.notificationTitle, true, requiresStorageNotLow, task.saveInPublicStorage);
task.notificationTitle, true, requiresStorageNotLow, task.saveInPublicStorage, task.allowCellular);
String newTaskId = request.getId().toString();
result.success(newTaskId);
sendUpdateProgress(newTaskId, DownloadStatus.RUNNING, task.progress);
Expand All @@ -283,7 +287,7 @@ private void retry(MethodCall call, MethodChannel.Result result) {
if (task.status == DownloadStatus.FAILED || task.status == DownloadStatus.CANCELED) {
WorkRequest request = buildRequest(task.url, task.savedDir, task.filename,
task.headers, task.showNotification, task.openFileFromNotification,
task.notificationTitle, false, requiresStorageNotLow, task.saveInPublicStorage);
task.notificationTitle, false, requiresStorageNotLow, task.saveInPublicStorage, task.allowCellular);
String newTaskId = request.getId().toString();
result.success(newTaskId);
sendUpdateProgress(newTaskId, DownloadStatus.ENQUEUED, task.progress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class TaskEntry implements BaseColumns {
public static final String COLUMN_NAME_NOTIFICATION_TITLE = "notification_title";
public static final String COLUMN_NAME_TIME_CREATED = "time_created";
public static final String COLUMN_SAVE_IN_PUBLIC_STORAGE = "save_in_public_storage";
public static final String COLUMN_NAME_ALLOW_CELLULAR = "wifi_only";
}

}
10 changes: 7 additions & 3 deletions android/src/main/java/vn/hunghd/flutterdownloader/TaskDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ public class TaskDao {
TaskContract.TaskEntry.COLUMN_NAME_SHOW_NOTIFICATION,
TaskContract.TaskEntry.COLUMN_NAME_NOTIFICATION_TITLE,
TaskContract.TaskEntry.COLUMN_NAME_TIME_CREATED,
TaskContract.TaskEntry.COLUMN_SAVE_IN_PUBLIC_STORAGE
TaskContract.TaskEntry.COLUMN_SAVE_IN_PUBLIC_STORAGE,
TaskContract.TaskEntry.COLUMN_NAME_ALLOW_CELLULAR
};

public TaskDao(TaskDbHelper helper) {
dbHelper = helper;
}

public void insertOrUpdateNewTask(String taskId, String url, int status, int progress, String fileName,
String savedDir, String headers, boolean showNotification, boolean openFileFromNotification, String notificationTitle, boolean saveInPublicStorage) {
String savedDir, String headers, boolean showNotification, boolean openFileFromNotification,
String notificationTitle, boolean saveInPublicStorage, boolean allowCellular) {
SQLiteDatabase db = dbHelper.getWritableDatabase();

ContentValues values = new ContentValues();
Expand All @@ -52,6 +54,7 @@ public void insertOrUpdateNewTask(String taskId, String url, int status, int pro
values.put(TaskContract.TaskEntry.COLUMN_NAME_RESUMABLE, 0);
values.put(TaskContract.TaskEntry.COLUMN_NAME_TIME_CREATED, System.currentTimeMillis());
values.put(TaskContract.TaskEntry.COLUMN_SAVE_IN_PUBLIC_STORAGE, saveInPublicStorage ? 1 : 0);
values.put(TaskContract.TaskEntry.COLUMN_NAME_ALLOW_CELLULAR, allowCellular ? 1 : 0);

db.beginTransaction();
try {
Expand Down Expand Up @@ -229,8 +232,9 @@ private DownloadTask parseCursor(Cursor cursor) {
int clickToOpenDownloadedFile = cursor.getShort(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_NAME_OPEN_FILE_FROM_NOTIFICATION));
long timeCreated = cursor.getLong(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_NAME_TIME_CREATED));
int saveInPublicStorage = cursor.getShort(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_SAVE_IN_PUBLIC_STORAGE));
int allowCellular = cursor.getInt(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_NAME_ALLOW_CELLULAR));
return new DownloadTask(primaryId, taskId, status, progress, url, filename, savedDir, headers,
mimeType, resumable == 1, showNotification == 1, clickToOpenDownloadedFile == 1, notificationTitle, timeCreated, saveInPublicStorage == 1);
mimeType, resumable == 1, showNotification == 1, clickToOpenDownloadedFile == 1, notificationTitle, timeCreated, saveInPublicStorage == 1, allowCellular == 1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class TaskDbHelper extends SQLiteOpenHelper {
TaskEntry.COLUMN_NAME_OPEN_FILE_FROM_NOTIFICATION + " TINYINT DEFAULT 0, " +
TaskEntry.COLUMN_NAME_NOTIFICATION_TITLE + " TEXT, " +
TaskEntry.COLUMN_NAME_TIME_CREATED + " INTEGER DEFAULT 0, " +
TaskEntry.COLUMN_SAVE_IN_PUBLIC_STORAGE + " TINYINT DEFAULT 0"
TaskEntry.COLUMN_SAVE_IN_PUBLIC_STORAGE + " TINYINT DEFAULT 0, " +
TaskEntry.COLUMN_NAME_ALLOW_CELLULAR + " INTEGER DEFAULT 0"
+ ")";

private static final String SQL_DELETE_ENTRIES =
Expand Down
Binary file modified ios/Assets/download_tasks.sql
Binary file not shown.
2 changes: 1 addition & 1 deletion ios/Classes/DBManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ -(void)runQuery:(const char *)query isQueryExecutable:(BOOL)queryExecutable{
}
}
else {
// In the database cannot be opened then show the error message on the debugger.
// If the database cannot be opened then show the error message on the debugger.
if (debug) {
NSLog(@"%s", sqlite3_errmsg(sqlite3Database));
}
Expand Down
Loading