@@ -0,0 +1,2174 @@
+package com.google.android.gnd.workers;%0A%0Aimport android.content.Context;%0Aimport android.util.Log;%0A%0Aimport androidx.annotation.NonNull;%0Aimport androidx.work.Data;%0Aimport androidx.work.Worker;%0Aimport androidx.work.WorkerParameters;%0A%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.net.URL;%0A%0A/**%0A * A worker that downloads files to the device in the background. The target URL and file name are%0A * provided in a %7B@link Data%7D object. This worker should only run when the device has a network%0A * connection.%0A */%0Apublic class FileDownloadWorker extends Worker %7B%0A  private static final String TARGET_URL = %22url%22;%0A  private static final String FILENAME = %22filename%22;%0A  private final Context context;%0A%0A  public FileDownloadWorker(@NonNull Context context, @NonNull WorkerParameters params) %7B%0A    super(context, params);%0A    this.context = context;%0A  %7D%0A%0A  private static final String TAG = FileDownloadWorker.class.getSimpleName();%0A%0A  /** Creates input data for the FileDownloadWorker. */%0A  public static Data createInputData(String url, String filename) %7B%0A    return new Data.Builder().putString(TARGET_URL, url).putString(FILENAME, filename).build();%0A  %7D%0A%0A  /**%0A   * Downloads a file from a given url and saves it to the application's file directory. The%0A   * directory is given by the provided context. Returns a successful result containing the filename%0A   * of the written file upon success.%0A   */%0A  @NonNull%0A  @Override%0A  public Result doWork() %7B%0A    String url = getInputData().getString(TARGET_URL);%0A    // TODO: If the filename is no good, fail.%0A    String filename = getInputData().getString(FILENAME);%0A%0A    try %7B%0A      InputStream is = new URL(url).openStream();%0A      FileOutputStream fos = context.openFileOutput(filename, context.MODE_PRIVATE);%0A      byte%5B%5D byteChunk = new byte%5B4096%5D;%0A      int n;%0A      while ((n = is.read(byteChunk)) %3E 0) %7B%0A        fos.write(byteChunk, 0, n);%0A      %7D%0A      is.close();%0A      fos.close();%0A      return Result.success(new Data.Builder().putString(FILENAME, filename).build());%0A    %7D catch (IOException e) %7B%0A      Log.d(TAG, e.getMessage());%0A    %7D%0A    return Result.failure();%0A  %7D%0A%7D%0A

Add a worker to download files