Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alesanabriav/8e35260011f94a036d67cdd992ce39f1 to your computer and use it in GitHub Desktop.
Save alesanabriav/8e35260011f94a036d67cdd992ce39f1 to your computer and use it in GitHub Desktop.
package com.platzi.platzi.downloadManager
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.Observer
import android.os.Environment
import android.util.Log
import androidx.work.*
import com.facebook.react.bridge.*
import java.io.File
class DownloadManager(
val reactContext: ReactApplicationContext,
val lifecyle: LifecycleOwner) :
ReactContextBaseJavaModule(reactContext) {
val TAG = "DownloadManager"
val workInstance = WorkManager.getInstance()
var workContinuation: WorkContinuation? = null
val workTag = "platzi_downloads"
override fun getName(): String {
return "DownloadManager"
}
@ReactMethod
fun downloadsQueue(videos: ReadableArray, onlyWifiParam: Boolean) {
var i = 0
while (i < videos.size()) {
val video = videos.getMap(i)
val name = video?.getString("name")
val url = video?.getString("video")
val id = video?.getString("id")?.toInt()
val path = getPath()
val requestTag = "material-download-$id"
val data = Data.Builder().apply {
putInt("id", id!!)
putString("name", name)
putString("url", url)
putString("path", path)
}.build()
val request = OneTimeWorkRequest.Builder(DownloadWork::class.java)
.addTag(requestTag)
.setInputData(data)
.build()
if (workContinuation != null) {
workContinuation!!.then(request)
} else {
workContinuation = workInstance.beginUniqueWork(workTag, ExistingWorkPolicy.APPEND, request)
}
workContinuation?.enqueue()
workInstance.getWorkInfosByTagLiveData(requestTag).observe(lifecyle, Observer {
if (it != null && it.size > 0) {
Log.e(TAG, "STATE ${it.last().state}")
}
})
i++
}
}
fun cancelQueue() {
}
@ReactMethod
fun getListVideos() {
val dir = File(getPath())
if(dir.isDirectory) {
dir.listFiles().forEach {
Log.e(TAG, it.absolutePath)
}
}
}
@ReactMethod
fun getDownloadPath(videoId: String) {
val path = getPath()
val file = File("$path/$videoId")
if(file.exists()) {
Log.e(TAG,"exists")
} else {
Log.e(TAG, "no exists")
}
}
@ReactMethod
fun freeSpace(promise: Promise) {
try {
val res = Arguments.createMap()
val free = getInternalSpace()
res.putString("freeSpace", free.toString())
promise.resolve(res)
} catch (err: Exception) {
promise.reject(err)
}
}
fun getInternalSpace(): Int? {
val file = Environment.getDataDirectory()
val free = file.freeSpace.toInt()
return formatSize(free)
}
fun formatSize(size: Int): Int? {
var newSize: Int? = null
if (size > 1024) {
newSize = size / 1024
if (newSize > 1024) {
newSize /= 1024
}
}
return newSize
}
fun getPath(): String {
val external = Environment.getExternalStorageDirectory().absolutePath
var path: String = ""
if (!external.contains("com.platzi.platzi")) {
return "$external/Android/data/com.platzi.platzi/files/Downloads"
}
return external
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment