diff --git a/retrofitLib/build.gradle b/retrofitLib/build.gradle index d8cb126..2e0801e 100644 --- a/retrofitLib/build.gradle +++ b/retrofitLib/build.gradle @@ -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' } diff --git a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadApi.kt b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadApi.kt index 91f9e92..31236af 100644 --- a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadApi.kt +++ b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadApi.kt @@ -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 diff --git a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadService.kt b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadService.kt index 88696de..2462bc7 100644 --- a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadService.kt +++ b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadService.kt @@ -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 @@ -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 @@ -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") + } } \ No newline at end of file diff --git a/sample/src/main/java/com/zeropercenthappy/retrofitutilsample/MainActivity.kt b/sample/src/main/java/com/zeropercenthappy/retrofitutilsample/MainActivity.kt index 3cc36c1..565461c 100644 --- a/sample/src/main/java/com/zeropercenthappy/retrofitutilsample/MainActivity.kt +++ b/sample/src/main/java/com/zeropercenthappy/retrofitutilsample/MainActivity.kt @@ -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" } }