Android Background Processing

Sep 23 2022 · Kotlin 1.6, Android 12, Android Studio Chipmunk 2021.2.1

Part 2: Use WorkManager in Complex Apps

11. Challenge - WorkManager

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 10. Chain Work Next episode: 12. Use DownloadManager

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 11. Challenge - WorkManager

The student materials have been reviewed and are updated as of SEPTEMBER 2022.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Nice job repeating the concepts from the first part of the course, and reaching your first challenge in this part! :] In this challenge, you have to implement a few things! You need to create not one, but two new workers!

class ClearLocalStorageWorker(context: Context, workerParameters: WorkerParameters) :
    Worker(context, workerParameters) {

  override fun doWork(): Result {
    val rootFolder = applicationContext.externalMediaDirs.first()

    rootFolder?.listFiles()?.forEach {
      if (it.isDirectory) {
        it.deleteRecursively()
      } else {
        it.delete()
      }
    }

    return Result.success()
  }
}
class SynchronizeImagesWorker(context: Context, workerParameters: WorkerParameters) :
    Worker(context, workerParameters) {

  override fun doWork(): Result {
    val images = inputData.getStringArray("images") ?: return Result.failure()

    images.forEach { imagePath ->
      val file = File(applicationContext.externalMediaDirs.first(), imagePath)
      FileUtils.downloadImage(file, imagePath)
    }

    return Result.success()
  }
}
  fun downloadImage(file: File, imageDownloadPath: String) {
    val imageUrl = URL("$BASE_URL/files/$imageDownloadPath")

    val connection = imageUrl.openConnection() as HttpURLConnection
    connection.doInput = true
    connection.connect()

    val inputStream = connection.inputStream

    val outputStream = FileOutputStream(file)
    outputStream.use { output ->
      val buffer = ByteArray(4 * 1024)

      var byteCount = inputStream.read(buffer)
      while (byteCount > 0) {
        output.write(buffer, 0, byteCount)

        byteCount = inputStream.read(buffer)
      }

      output.flush()
    }
  }
val imagePath = parts.last()
val file = File(applicationContext.externalMediaDirs.first(), imagePath)

FileUtils.downloadImage(file, imagePath)
  private val remoteApi = App.remoteApi
  private val networkStatusChecker by lazy {
    NetworkStatusChecker(activity?.getSystemService(ConnectivityManager::class.java))
  }
    syncImages.setOnClickListener {
      networkStatusChecker.performIfConnectedToInternet {
        GlobalScope.launch(Dispatchers.Main) {
          val result = remoteApi.getImages()

          if (result is Success) {
            val images = result.data.map { it.imagePath }
            synchronizeImages(images)
          }
        }
      }
    }
  }
  private fun synchronizeImages(images: List<String>) {
    val clearStorageWorker = OneTimeWorkRequestBuilder<ClearLocalStorageWorker>()
        .build()

    val synchronizeImagesWorker = OneTimeWorkRequestBuilder<SynchronizeImagesWorker>()
        .setInputData(workDataOf("images" to images.toTypedArray()))
        .build()

    val workManager = WorkManager.getInstance(requireActivity())

    workManager.beginWith(clearStorageWorker)
        .then(synchronizeImagesWorker)
        .enqueue()

    workManager.getWorkInfoByIdLiveData(synchronizeImagesWorker.id).observe(this, Observer {
      if (it.state.isFinished) {
        activity?.toast("Synchronized images!")
      }
    })
  }