2018-12-21 04:09:07 +00:00
|
|
|
package com.adlerosn.brasilfurfest.schedule.managers
|
|
|
|
|
|
|
|
import android.content.Context
|
2019-01-05 06:06:44 +00:00
|
|
|
import com.adlerosn.brasilfurfest.helper.lastPathPart
|
2018-12-21 04:09:07 +00:00
|
|
|
import org.jetbrains.anko.doAsync
|
|
|
|
import org.jetbrains.anko.uiThread
|
|
|
|
import java.io.Serializable
|
|
|
|
import java.lang.IllegalStateException
|
|
|
|
import java.net.HttpURLConnection
|
|
|
|
import java.net.HttpURLConnection.HTTP_NOT_MODIFIED
|
|
|
|
import java.net.HttpURLConnection.HTTP_OK
|
|
|
|
import java.net.URL
|
|
|
|
|
|
|
|
class UpdateChecker(context: Context): Serializable {
|
2018-12-28 04:26:22 +00:00
|
|
|
val cacheManager = CacheManager(context).also { it.startMonitoringNeededFiles() }
|
2019-06-22 04:04:57 +00:00
|
|
|
private val baseJsonFileName = RemoteAssets.json.lastPathPart()
|
2018-12-21 04:09:07 +00:00
|
|
|
val hasBaseJsonCached get() = baseJsonFileName.let {
|
|
|
|
cacheManager.getStamp(it) != null
|
|
|
|
}
|
|
|
|
|
|
|
|
fun downloadBaseJsonWithWatcher(watcher: (DownloadState, ByteArray?, Throwable?)->Any?) =
|
|
|
|
downloadWithWatcher(RemoteAssets.json, watcher)
|
|
|
|
|
2019-06-22 04:04:57 +00:00
|
|
|
fun downloadWithWatcher(httpThingToFetch: String, watcher: (DownloadState, ByteArray?, Throwable?)->Any?) =
|
|
|
|
if(cacheManager.goesOnline)
|
2019-01-05 06:06:44 +00:00
|
|
|
doAsync {
|
2018-12-21 04:09:07 +00:00
|
|
|
try {
|
2019-01-05 06:06:44 +00:00
|
|
|
val url = URL(httpThingToFetch)
|
|
|
|
val diskIdentifier = url.toString().lastPathPart()
|
|
|
|
val connection = url.openConnection() as HttpURLConnection
|
|
|
|
cacheManager.getStamp(diskIdentifier)?.let {
|
|
|
|
connection.setRequestProperty("If-Modified-Since", it)
|
|
|
|
}
|
|
|
|
connection.useCaches = false
|
|
|
|
connection.connectTimeout = 3000
|
|
|
|
connection.readTimeout = 5000
|
|
|
|
connection.allowUserInteraction = false
|
|
|
|
connection.doOutput = false
|
|
|
|
connection.requestMethod = "GET"
|
|
|
|
uiThread { watcher(DownloadState.CONNECTING, null, null) }
|
|
|
|
connection.connect()
|
|
|
|
try {
|
|
|
|
val resCode = connection.responseCode
|
|
|
|
when (resCode) {
|
|
|
|
HTTP_OK -> {
|
|
|
|
uiThread { watcher(DownloadState.CACHE_MISS, null, null) }
|
|
|
|
val lastModified = connection.headerFields["last-modified"]?.firstOrNull()
|
|
|
|
val data = connection.inputStream.buffered().readBytes()
|
|
|
|
cacheManager[diskIdentifier] = Pair(data, lastModified ?: "")
|
|
|
|
uiThread { watcher(DownloadState.SUCCESS, data, null) }
|
|
|
|
}
|
|
|
|
HTTP_NOT_MODIFIED -> {
|
|
|
|
uiThread { watcher(DownloadState.CACHE_HIT, null, null) }
|
|
|
|
val bytes = cacheManager[diskIdentifier]?.first?.readBytes()
|
|
|
|
uiThread { watcher(DownloadState.SUCCESS, bytes, null) }
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
throw IllegalStateException("Server replied with code $resCode. Expected either $HTTP_OK or $HTTP_NOT_MODIFIED.")
|
|
|
|
}
|
2018-12-21 04:09:07 +00:00
|
|
|
}
|
2019-01-05 06:06:44 +00:00
|
|
|
} catch (t: Throwable) {
|
|
|
|
uiThread { watcher(DownloadState.ERROR, null, t) }
|
|
|
|
} finally {
|
|
|
|
connection.disconnect()
|
2018-12-21 04:09:07 +00:00
|
|
|
}
|
|
|
|
} catch (t: Throwable) {
|
|
|
|
uiThread { watcher(DownloadState.ERROR, null, t) }
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 04:04:57 +00:00
|
|
|
else
|
2019-01-05 06:06:44 +00:00
|
|
|
doAsync { uiThread {
|
|
|
|
watcher(DownloadState.CONNECTING, null, null)
|
|
|
|
watcher(DownloadState.CACHE_HIT, null, null)
|
|
|
|
watcher(DownloadState.SUCCESS, cacheManager[httpThingToFetch.lastPathPart()]!!.first.readBytes(), null)
|
|
|
|
} }
|
2018-12-21 04:09:07 +00:00
|
|
|
|
|
|
|
enum class DownloadState{
|
|
|
|
CONNECTING,
|
|
|
|
CACHE_MISS,
|
|
|
|
CACHE_HIT,
|
|
|
|
ERROR,
|
|
|
|
SUCCESS
|
|
|
|
}
|
|
|
|
}
|