Skip to content

Commit

Permalink
update download service
Browse files Browse the repository at this point in the history
  • Loading branch information
zeropercenthappy committed Jan 14, 2020
1 parent f98bc76 commit 7a71d27
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion retrofitLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"

implementation 'com.github.zeropercenthappy:ZPHAndroidUtils:1.2.4'
implementation 'com.github.zeropercenthappy:ZPHAndroidUtils:1.2.7'
api "com.squareup.okhttp3:okhttp:$okhttp_version"
api "com.squareup.retrofit2:retrofit:$retrofit_version"
api "com.squareup.retrofit2:converter-gson:$retrofit_version"
api 'org.conscrypt:conscrypt-android:2.2.1'
api 'me.jessyan:progressmanager:1.5.0'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import okhttp3.ResponseBody
import retrofit2.http.GET
import retrofit2.http.Url

interface DownloadApi {
internal interface DownloadApi {

@GET
suspend fun download(@Url url: String): ResponseBody
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.content.Context
import android.content.Intent
import com.zeropercenthappy.retrofitutil.RetrofitBuilder
import com.zeropercenthappy.utilslibrary.utils.FileUtils
import com.zeropercenthappy.utilslibrary.utils.ZPHLogger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
import java.io.File
Expand All @@ -15,13 +14,10 @@ import java.io.Serializable
private const val EXTRA_DOWNLOAD_MAP = "DownloadService.download.map"

/**
* Start a download service running in a independent process.
* Download service which is running in a independent process.
*
* Warning: The result of download is not reliable. Because download task may be terminated in progress.
*
* So you have to check the file integrity before use it.
*/
class DownloadService : IntentService("DownloadService"), ZPHLogger {
class DownloadService : IntentService("DownloadService") {

private lateinit var api: DownloadApi
private lateinit var downloadMap: Map<String, File>
Expand Down Expand Up @@ -66,8 +62,25 @@ class DownloadService : IntentService("DownloadService"), ZPHLogger {
downloadTask = this
for (url in downloadMap.keys) {
val destinationFile = downloadMap[url] ?: continue
val cacheFile = checkCacheFile(destinationFile)
if (destinationFile.exists()) {
continue
}
val responseBody = api.download(url)
FileUtils.writeFileByIS(destinationFile, responseBody.byteStream(), false)
val result = FileUtils.writeFileByIS(cacheFile, responseBody.byteStream())

// Download success, move cache file to destination file
if (result) {
cacheFile.renameTo(destinationFile)
}
}
}

/**
* @return Download cache file.
*/
private fun checkCacheFile(destinationFile: File): File {
val dir = destinationFile.parent
return File(dir, "${destinationFile.name}.cache")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ class MainActivity : AppCompatActivity(), AnkoLogger {
// download completely
doAsync {
if (response.isSuccessful && response.body() != null) {
val cacheFile = CacheUtils.createFormatedCacheFile(this@MainActivity, "jpg")
val cacheFile =
CacheUtils.createFormatedCacheFile(this@MainActivity, "jpg", true)
if (cacheFile != null) {
val result = FileUtils.writeFileByIS(
cacheFile,
response.body()!!.byteStream(),
false
response.body()!!.byteStream()
)
info { if (result) "download success" else "download failed" }
}
Expand Down

0 comments on commit 7a71d27

Please sign in to comment.