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

Added an optional display title for the notification panel on Android #296

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -5,6 +5,7 @@ public class DownloadTask {
String taskId;
int status;
int progress;
String title;
String url;
String filename;
String savedDir;
Expand All @@ -15,12 +16,13 @@ public class DownloadTask {
boolean openFileFromNotification;
long timeCreated;

DownloadTask(int primaryId, String taskId, int status, int progress, String url, String filename, String savedDir,
DownloadTask(int primaryId, String taskId, int status, int progress, String title, String url, String filename, String savedDir,
String headers, String mimeType, boolean resumable, boolean showNotification, boolean openFileFromNotification, long timeCreated) {
this.primaryId = primaryId;
this.taskId = taskId;
this.status = status;
this.progress = progress;
this.title = title;
this.url = url;
this.filename = filename;
this.savedDir = savedDir;
Expand All @@ -34,6 +36,6 @@ public class DownloadTask {

@Override
public String toString() {
return "DownloadTask{taskId=" + taskId + ",status=" + status + ",progress=" + progress + ",url=" + url + ",filename=" + filename + ",savedDir=" + savedDir + ",headers=" + headers + "}";
return "DownloadTask{taskId=" + taskId + ",status=" + status + ",progress=" + progress + ",title=" + title + ",url=" + url + ",filename=" + filename + ",savedDir=" + savedDir + ",headers=" + headers + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.flutter.view.FlutterRunArguments;

public class DownloadWorker extends Worker implements MethodChannel.MethodCallHandler {
public static final String ARG_TITLE = "title";
public static final String ARG_URL = "url";
public static final String ARG_FILE_NAME = "file_name";
public static final String ARG_SAVED_DIR = "saved_file";
Expand Down Expand Up @@ -160,6 +161,7 @@ public Result doWork() {
dbHelper = TaskDbHelper.getInstance(context);
taskDao = new TaskDao(dbHelper);

String title = getInputData().getString(ARG_TITLE);
String url = getInputData().getString(ARG_URL);
String filename = getInputData().getString(ARG_FILE_NAME);
String savedDir = getInputData().getString(ARG_SAVED_DIR);
Expand All @@ -185,17 +187,22 @@ public Result doWork() {

buildNotification(context);

updateNotification(context, filename == null ? url : filename, DownloadStatus.RUNNING, task.progress, null);
// Default to the filename or URL if a title has not been specified.
if (title == null || title.isEmpty()) {
title = filename == null ? url : filename;
}

updateNotification(context, title, DownloadStatus.RUNNING, task.progress, null);
taskDao.updateTask(getId().toString(), DownloadStatus.RUNNING, 0);

try {
downloadFile(context, url, savedDir, filename, headers, isResume);
downloadFile(context, title, url, savedDir, filename, headers, isResume);
cleanUp();
dbHelper = null;
taskDao = null;
return Result.success();
} catch (Exception e) {
updateNotification(context, filename == null ? url : filename, DownloadStatus.FAILED, -1, null);
updateNotification(context, title, DownloadStatus.FAILED, -1, null);
taskDao.updateTask(getId().toString(), DownloadStatus.FAILED, lastProgress);
e.printStackTrace();
dbHelper = null;
Expand Down Expand Up @@ -231,7 +238,7 @@ private long setupPartialDownloadedDataHeader(HttpURLConnection conn, String fil
return downloadedBytes;
}

private void downloadFile(Context context, String fileURL, String savedDir, String filename, String headers, boolean isResume) throws IOException {
private void downloadFile(Context context, String title, String fileURL, String savedDir, String filename, String headers, boolean isResume) throws IOException {
String url = fileURL;
URL resourceUrl, base, next;
Map<String, Integer> visited;
Expand Down Expand Up @@ -340,7 +347,7 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
if ((lastProgress == 0 || progress > lastProgress + STEP_UPDATE || progress == 100)
&& progress != lastProgress) {
lastProgress = progress;
updateNotification(context, filename, DownloadStatus.RUNNING, progress, null);
updateNotification(context, title, DownloadStatus.RUNNING, progress, null);

// This line possibly causes system overloaded because of accessing to DB too many ?!!!
// but commenting this line causes tasks loaded from DB missing current downloading progress,
Expand Down Expand Up @@ -370,19 +377,19 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
}
}
}
updateNotification(context, filename, status, progress, pendingIntent);
updateNotification(context, title, status, progress, pendingIntent);
taskDao.updateTask(getId().toString(), status, progress);

log(isStopped() ? "Download canceled" : "File downloaded");
} else {
DownloadTask task = taskDao.loadTask(getId().toString());
int status = isStopped() ? (task.resumable ? DownloadStatus.PAUSED : DownloadStatus.CANCELED) : DownloadStatus.FAILED;
updateNotification(context, filename, status, -1, null);
updateNotification(context, title, status, -1, null);
taskDao.updateTask(getId().toString(), status, lastProgress);
log(isStopped() ? "Download canceled" : "Server replied HTTP code: " + responseCode);
}
} catch (IOException e) {
updateNotification(context, filename == null ? fileURL : filename, DownloadStatus.FAILED, -1, null);
updateNotification(context, title, DownloadStatus.FAILED, -1, null);
taskDao.updateTask(getId().toString(), DownloadStatus.FAILED, lastProgress);
e.printStackTrace();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void onDetachedFromEngine(FlutterPluginBinding binding) {
}
}

private WorkRequest buildRequest(String url, String savedDir, String filename, String headers, boolean showNotification, boolean openFileFromNotification, boolean isResume, boolean requiresStorageNotLow) {
private WorkRequest buildRequest(String url, String savedDir, String title, String filename, String headers, boolean showNotification, boolean openFileFromNotification, boolean isResume, boolean requiresStorageNotLow) {
WorkRequest request = new OneTimeWorkRequest.Builder(DownloadWorker.class)
.setConstraints(new Constraints.Builder()
.setRequiresStorageNotLow(requiresStorageNotLow)
Expand All @@ -121,6 +121,7 @@ private WorkRequest buildRequest(String url, String savedDir, String filename, S
.addTag(TAG)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 5, TimeUnit.SECONDS)
.setInputData(new Data.Builder()
.putString(DownloadWorker.ARG_TITLE, title)
.putString(DownloadWorker.ARG_URL, url)
.putString(DownloadWorker.ARG_SAVED_DIR, savedDir)
.putString(DownloadWorker.ARG_FILE_NAME, filename)
Expand Down Expand Up @@ -162,19 +163,20 @@ private void registerCallback(MethodCall call, MethodChannel.Result result) {
}

private void enqueue(MethodCall call, MethodChannel.Result result) {
String title = call.argument("title");
String url = call.argument("url");
String savedDir = call.argument("saved_dir");
String filename = call.argument("file_name");
String headers = call.argument("headers");
boolean showNotification = call.argument("show_notification");
boolean openFileFromNotification = call.argument("open_file_from_notification");
boolean requiresStorageNotLow = call.argument("requires_storage_not_low");
WorkRequest request = buildRequest(url, savedDir, filename, headers, showNotification, openFileFromNotification, false, requiresStorageNotLow);
WorkRequest request = buildRequest(url, savedDir, title, filename, headers, showNotification, openFileFromNotification, false, requiresStorageNotLow);
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);
taskDao.insertOrUpdateNewTask(taskId, title, url, DownloadStatus.ENQUEUED, 0, filename, savedDir, headers, showNotification, openFileFromNotification);
}

private void loadTasks(MethodCall call, MethodChannel.Result result) {
Expand All @@ -185,6 +187,7 @@ private void loadTasks(MethodCall call, MethodChannel.Result result) {
item.put("task_id", task.taskId);
item.put("status", task.status);
item.put("progress", task.progress);
item.put("title", task.title);
item.put("url", task.url);
item.put("file_name", task.filename);
item.put("saved_dir", task.savedDir);
Expand All @@ -203,6 +206,7 @@ private void loadTasksWithRawQuery(MethodCall call, MethodChannel.Result result)
item.put("task_id", task.taskId);
item.put("status", task.status);
item.put("progress", task.progress);
item.put("title", task.title);
item.put("url", task.url);
item.put("file_name", task.filename);
item.put("saved_dir", task.savedDir);
Expand Down Expand Up @@ -243,7 +247,7 @@ private void resume(MethodCall call, MethodChannel.Result result) {
String partialFilePath = task.savedDir + File.separator + filename;
File partialFile = new File(partialFilePath);
if (partialFile.exists()) {
WorkRequest request = buildRequest(task.url, task.savedDir, task.filename, task.headers, task.showNotification, task.openFileFromNotification, true, requiresStorageNotLow);
WorkRequest request = buildRequest(task.url, task.savedDir, task.title, task.filename, task.headers, task.showNotification, task.openFileFromNotification, true, requiresStorageNotLow);
String newTaskId = request.getId().toString();
result.success(newTaskId);
sendUpdateProgress(newTaskId, DownloadStatus.RUNNING, task.progress);
Expand All @@ -266,7 +270,7 @@ private void retry(MethodCall call, MethodChannel.Result result) {
boolean requiresStorageNotLow = call.argument("requires_storage_not_low");
if (task != null) {
if (task.status == DownloadStatus.FAILED || task.status == DownloadStatus.CANCELED) {
WorkRequest request = buildRequest(task.url, task.savedDir, task.filename, task.headers, task.showNotification, task.openFileFromNotification, false, requiresStorageNotLow);
WorkRequest request = buildRequest(task.url, task.savedDir, task.title, task.filename, task.headers, task.showNotification, task.openFileFromNotification, false, requiresStorageNotLow);
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 @@ -11,6 +11,7 @@ public static class TaskEntry implements BaseColumns {
public static final String COLUMN_NAME_TASK_ID = "task_id";
public static final String COLUMN_NAME_STATUS = "status";
public static final String COLUMN_NAME_PROGRESS = "progress";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_URL = "url";
public static final String COLUMN_NAME_SAVED_DIR = "saved_dir";
public static final String COLUMN_NAME_FILE_NAME = "file_name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class TaskDao {
TaskContract.TaskEntry.COLUMN_NAME_TASK_ID,
TaskContract.TaskEntry.COLUMN_NAME_PROGRESS,
TaskContract.TaskEntry.COLUMN_NAME_STATUS,
TaskContract.TaskEntry.COLUMN_NAME_TITLE,
TaskContract.TaskEntry.COLUMN_NAME_URL,
TaskContract.TaskEntry.COLUMN_NAME_FILE_NAME,
TaskContract.TaskEntry.COLUMN_NAME_SAVED_DIR,
Expand All @@ -31,12 +32,13 @@ public TaskDao(TaskDbHelper helper) {
dbHelper = helper;
}

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

ContentValues values = new ContentValues();
values.put(TaskContract.TaskEntry.COLUMN_NAME_TASK_ID, taskId);
values.put(TaskContract.TaskEntry.COLUMN_NAME_TITLE, title);
values.put(TaskContract.TaskEntry.COLUMN_NAME_URL, url);
values.put(TaskContract.TaskEntry.COLUMN_NAME_STATUS, status);
values.put(TaskContract.TaskEntry.COLUMN_NAME_PROGRESS, progress);
Expand Down Expand Up @@ -214,6 +216,7 @@ private DownloadTask parseCursor(Cursor cursor) {
String taskId = cursor.getString(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_NAME_TASK_ID));
int status = cursor.getInt(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_NAME_STATUS));
int progress = cursor.getInt(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_NAME_PROGRESS));
String title = cursor.getString(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_NAME_TITLE));
String url = cursor.getString(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_NAME_URL));
String filename = cursor.getString(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_NAME_FILE_NAME));
String savedDir = cursor.getString(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_NAME_SAVED_DIR));
Expand All @@ -223,7 +226,7 @@ private DownloadTask parseCursor(Cursor cursor) {
int showNotification = cursor.getShort(cursor.getColumnIndexOrThrow(TaskContract.TaskEntry.COLUMN_NAME_SHOW_NOTIFICATION));
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));
return new DownloadTask(primaryId, taskId, status, progress, url, filename, savedDir, headers,
return new DownloadTask(primaryId, taskId, status, progress, title, url, filename, savedDir, headers,
mimeType, resumable == 1, showNotification == 1, clickToOpenDownloadedFile == 1, timeCreated);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import vn.hunghd.flutterdownloader.TaskContract.TaskEntry;

public class TaskDbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 2;
public static final int DATABASE_VERSION = 3;
public static final String DATABASE_NAME = "download_tasks.db";

private static TaskDbHelper instance = null;
Expand All @@ -17,6 +17,7 @@ public class TaskDbHelper extends SQLiteOpenHelper {
TaskEntry._ID + " INTEGER PRIMARY KEY," +
TaskEntry.COLUMN_NAME_TASK_ID + " VARCHAR(256), " +
TaskEntry.COLUMN_NAME_URL + " TEXT, " +
TaskEntry.COLUMN_NAME_TITLE + " TEXT, " +
TaskEntry.COLUMN_NAME_STATUS + " INTEGER DEFAULT 0, " +
TaskEntry.COLUMN_NAME_PROGRESS + " INTEGER DEFAULT 0, " +
TaskEntry.COLUMN_NAME_FILE_NAME + " TEXT, " +
Expand Down
13 changes: 7 additions & 6 deletions example/android/app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@
<orderEntry type="library" scope="TEST" name="Gradle: androidx.test:monitor:1.1.1@aar" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: androidx.test.espresso:espresso-idling-resource:3.1.1@aar" level="project" />
<orderEntry type="library" name="Gradle: __local_aars__:./../build/app/intermediates/flutter/debug/libs.jar:unspecified@jar" level="project" />
<orderEntry type="library" name="Gradle: io.flutter:flutter_embedding_debug:1.0.0-af51afceb8886cc11e25047523c4e0c7e1f5d408@jar" level="project" />
<orderEntry type="library" name="Gradle: io.flutter:armeabi_v7a_debug:1.0.0-af51afceb8886cc11e25047523c4e0c7e1f5d408@jar" level="project" />
<orderEntry type="library" name="Gradle: io.flutter:arm64_v8a_debug:1.0.0-af51afceb8886cc11e25047523c4e0c7e1f5d408@jar" level="project" />
<orderEntry type="library" name="Gradle: io.flutter:x86_debug:1.0.0-af51afceb8886cc11e25047523c4e0c7e1f5d408@jar" level="project" />
<orderEntry type="library" name="Gradle: io.flutter:x86_64_debug:1.0.0-af51afceb8886cc11e25047523c4e0c7e1f5d408@jar" level="project" />
<orderEntry type="library" name="Gradle: androidx.lifecycle:lifecycle-common-java8:2.0.0@jar" level="project" />
<orderEntry type="library" name="Gradle: io.flutter:flutter_embedding_debug:1.0.0-6bc433c6b6b5b98dcf4cc11aff31cdee90849f32@jar" level="project" />
<orderEntry type="library" name="Gradle: io.flutter:armeabi_v7a_debug:1.0.0-6bc433c6b6b5b98dcf4cc11aff31cdee90849f32@jar" level="project" />
<orderEntry type="library" name="Gradle: io.flutter:arm64_v8a_debug:1.0.0-6bc433c6b6b5b98dcf4cc11aff31cdee90849f32@jar" level="project" />
<orderEntry type="library" name="Gradle: io.flutter:x86_debug:1.0.0-6bc433c6b6b5b98dcf4cc11aff31cdee90849f32@jar" level="project" />
<orderEntry type="library" name="Gradle: io.flutter:x86_64_debug:1.0.0-6bc433c6b6b5b98dcf4cc11aff31cdee90849f32@jar" level="project" />
<orderEntry type="library" name="Gradle: androidx.lifecycle:lifecycle-common-java8:2.2.0@jar" level="project" />
<orderEntry type="library" name="Gradle: androidx.lifecycle:lifecycle-common:2.2.0@jar" level="project" />
<orderEntry type="library" name="Gradle: androidx.arch.core:core-common:2.1.0@jar" level="project" />
<orderEntry type="library" name="Gradle: androidx.collection:collection:1.1.0@jar" level="project" />
Expand All @@ -145,6 +145,7 @@
<orderEntry type="library" name="Gradle: androidx.arch.core:core-runtime:2.1.0@aar" level="project" />
<orderEntry type="library" name="Gradle: androidx.versionedparcelable:versionedparcelable:1.1.0@aar" level="project" />
<orderEntry type="library" name="Gradle: androidx.lifecycle:lifecycle-viewmodel:2.2.0@aar" level="project" />
<orderEntry type="module" module-name="path_provider_macos" />
<orderEntry type="module" module-name="flutter_downloader" />
<orderEntry type="module" module-name="permission_handler" />
<orderEntry type="module" module-name="path_provider" />
Expand Down
Loading