androidx.work.WorkRequest Java Examples

The following examples show how to use androidx.work.WorkRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FlutterDownloaderPlugin.java    From flutter_downloader with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
private WorkRequest buildRequest(String url, String savedDir, 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)
                    .setRequiredNetworkType(NetworkType.CONNECTED)
                    .build())
            .addTag(TAG)
            .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 5, TimeUnit.SECONDS)
            .setInputData(new Data.Builder()
                    .putString(DownloadWorker.ARG_URL, url)
                    .putString(DownloadWorker.ARG_SAVED_DIR, savedDir)
                    .putString(DownloadWorker.ARG_FILE_NAME, filename)
                    .putString(DownloadWorker.ARG_HEADERS, headers)
                    .putBoolean(DownloadWorker.ARG_SHOW_NOTIFICATION, showNotification)
                    .putBoolean(DownloadWorker.ARG_OPEN_FILE_FROM_NOTIFICATION, openFileFromNotification)
                    .putBoolean(DownloadWorker.ARG_IS_RESUME, isResume)
                    .putLong(DownloadWorker.ARG_CALLBACK_HANDLE, callbackHandle)
                    .putBoolean(DownloadWorker.ARG_DEBUG, debugMode == 1)
                    .build()
            )
            .build();
    return request;
}
 
Example #2
Source File: FlutterDownloaderPlugin.java    From flutter_downloader with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void retry(MethodCall call, MethodChannel.Result result) {
    String taskId = call.argument("task_id");
    DownloadTask task = taskDao.loadTask(taskId);
    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);
            String newTaskId = request.getId().toString();
            result.success(newTaskId);
            sendUpdateProgress(newTaskId, DownloadStatus.ENQUEUED, task.progress);
            taskDao.updateTask(taskId, newTaskId, DownloadStatus.ENQUEUED, task.progress, false);
            WorkManager.getInstance(context).enqueue(request);
        } else {
            result.error("invalid_status", "only failed and canceled task can be retried", null);
        }
    } else {
        result.error("invalid_task_id", "not found task corresponding to given task id", null);
    }
}
 
Example #3
Source File: CandyBarArtWorker.java    From candybar with Apache License 2.0 5 votes vote down vote up
public static void enqueueLoad(Context context) {
    WorkManager manager = WorkManager.getInstance(context);
    Constraints constraints = new Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .build();
    WorkRequest request = new OneTimeWorkRequest.Builder(CandyBarArtWorker.class)
            .setConstraints(constraints)
            .build();
    manager.enqueue(request);
}
 
Example #4
Source File: FlutterUploaderPlugin.java    From flutter_uploader with MIT License 5 votes vote down vote up
private WorkRequest buildRequest(UploadTask task) {
  Gson gson = new Gson();

  Data.Builder dataBuilder =
      new Data.Builder()
          .putString(UploadWorker.ARG_URL, task.getURL())
          .putString(UploadWorker.ARG_METHOD, task.getMethod())
          .putInt(UploadWorker.ARG_REQUEST_TIMEOUT, task.getTimeout())
          .putBoolean(UploadWorker.ARG_SHOW_NOTIFICATION, task.canShowNotification())
          .putBoolean(UploadWorker.ARG_BINARY_UPLOAD, task.isBinaryUpload())
          .putString(UploadWorker.ARG_UPLOAD_REQUEST_TAG, task.getTag())
          .putInt(UploadWorker.ARG_ID, task.getId());

  List<FileItem> files = task.getFiles();

  String fileItemsJson = gson.toJson(files);
  dataBuilder.putString(UploadWorker.ARG_FILES, fileItemsJson);

  if (task.getHeaders() != null) {
    String headersJson = gson.toJson(task.getHeaders());
    dataBuilder.putString(UploadWorker.ARG_HEADERS, headersJson);
  }

  if (task.getParameters() != null) {
    String parametersJson = gson.toJson(task.getParameters());
    dataBuilder.putString(UploadWorker.ARG_DATA, parametersJson);
  }

  return new OneTimeWorkRequest.Builder(UploadWorker.class)
      .setConstraints(
          new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build())
      .addTag(TAG)
      .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 5, TimeUnit.SECONDS)
      .setInputData(dataBuilder.build())
      .build();
}
 
Example #5
Source File: FlutterDownloaderPlugin.java    From flutter_downloader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void enqueue(MethodCall call, MethodChannel.Result result) {
    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);
    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);
}
 
Example #6
Source File: FlutterDownloaderPlugin.java    From flutter_downloader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void resume(MethodCall call, MethodChannel.Result result) {
    String taskId = call.argument("task_id");
    DownloadTask task = taskDao.loadTask(taskId);
    boolean requiresStorageNotLow = call.argument("requires_storage_not_low");
    if (task != null) {
        if (task.status == DownloadStatus.PAUSED) {
            String filename = task.filename;
            if (filename == null) {
                filename = task.url.substring(task.url.lastIndexOf("/") + 1, task.url.length());
            }
            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);
                String newTaskId = request.getId().toString();
                result.success(newTaskId);
                sendUpdateProgress(newTaskId, DownloadStatus.RUNNING, task.progress);
                taskDao.updateTask(taskId, newTaskId, DownloadStatus.RUNNING, task.progress, false);
                WorkManager.getInstance(context).enqueue(request);
            } else {
                result.error("invalid_data", "not found partial downloaded data, this task cannot be resumed", null);
            }
        } else {
            result.error("invalid_status", "only paused task can be resumed", null);
        }
    } else {
        result.error("invalid_task_id", "not found task corresponding to given task id", null);
    }
}
 
Example #7
Source File: FlutterUploaderPlugin.java    From flutter_uploader with MIT License 4 votes vote down vote up
private void enqueue(MethodCall call, MethodChannel.Result result) {
  taskIdKey++;
  String url = call.argument("url");
  String method = call.argument("method");
  List<Map<String, String>> files = call.argument("files");
  Map<String, String> parameters = call.argument("data");
  Map<String, String> headers = call.argument("headers");
  boolean showNotification = call.argument("show_notification");
  String tag = call.argument("tag");

  List<String> methods = Arrays.asList(validHttpMethods);

  if (method == null) {
    method = "POST";
  }

  if (!methods.contains(method.toUpperCase())) {
    result.error("invalid_method", "Method must be either POST | PUT | PATCH", null);
    return;
  }

  List<FileItem> items = new ArrayList<>();

  for (Map<String, String> file : files) {
    items.add(FileItem.fromJson(file));
  }

  WorkRequest request =
      buildRequest(
          new UploadTask(
              taskIdKey,
              url,
              method,
              items,
              headers,
              parameters,
              connectionTimeout,
              showNotification,
              false,
              tag));
  WorkManager.getInstance(register.context()).enqueue(request);
  String taskId = request.getId().toString();
  if (!tasks.containsKey(taskId)) {
    tasks.put(taskId, tag);
  }
  result.success(taskId);
  sendUpdateProgress(taskId, UploadStatus.ENQUEUED, 0);
}
 
Example #8
Source File: FlutterUploaderPlugin.java    From flutter_uploader with MIT License 4 votes vote down vote up
private void enqueueBinary(MethodCall call, MethodChannel.Result result) {
  taskIdKey++;
  String url = call.argument("url");
  String method = call.argument("method");
  Map<String, String> files = call.argument("file");
  Map<String, String> headers = call.argument("headers");
  boolean showNotification = call.argument("show_notification");
  String tag = call.argument("tag");

  List<String> methods = Arrays.asList(validHttpMethods);

  if (method == null) {
    method = "POST";
  }

  if (!methods.contains(method.toUpperCase())) {
    result.error("invalid_method", "Method must be either POST | PUT | PATCH", null);
    return;
  }

  WorkRequest request =
      buildRequest(
          new UploadTask(
              taskIdKey,
              url,
              method,
              Collections.singletonList(FileItem.fromJson(files)),
              headers,
              Collections.emptyMap(),
              connectionTimeout,
              showNotification,
              true,
              tag));
  WorkManager.getInstance(register.context()).enqueue(request);
  String taskId = request.getId().toString();

  if (!tasks.containsKey(taskId)) {
    tasks.put(taskId, tag);
  }

  result.success(taskId);
  sendUpdateProgress(taskId, UploadStatus.ENQUEUED, 0);
}