-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05026c4
commit f98bc76
Showing
3 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.zeropercenthappy.retrofitutil" /> | ||
package="com.zeropercenthappy.retrofitutil"> | ||
|
||
<application> | ||
<service | ||
android:name=".service.download.DownloadService" | ||
android:exported="false" | ||
android:process=":ZphDownloadService" /> | ||
</application> | ||
|
||
</manifest> |
11 changes: 11 additions & 0 deletions
11
retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.zeropercenthappy.retrofitutil.service.download | ||
|
||
import okhttp3.ResponseBody | ||
import retrofit2.http.GET | ||
import retrofit2.http.Url | ||
|
||
interface DownloadApi { | ||
|
||
@GET | ||
suspend fun download(@Url url: String): ResponseBody | ||
} |
73 changes: 73 additions & 0 deletions
73
...itLib/src/main/java/com/zeropercenthappy/retrofitutil/service/download/DownloadService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.zeropercenthappy.retrofitutil.service.download | ||
|
||
import android.app.ActivityManager | ||
import android.app.IntentService | ||
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 | ||
import java.io.Serializable | ||
|
||
private const val EXTRA_DOWNLOAD_MAP = "DownloadService.download.map" | ||
|
||
/** | ||
* Start a download service 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 { | ||
|
||
private lateinit var api: DownloadApi | ||
private lateinit var downloadMap: Map<String, File> | ||
private lateinit var downloadTask: CoroutineScope | ||
|
||
companion object { | ||
@JvmStatic | ||
fun startDownloadTask(context: Context, downloadMap: Map<String, File>) { | ||
val intent = Intent(context, DownloadService::class.java) | ||
intent.putExtra(EXTRA_DOWNLOAD_MAP, downloadMap as Serializable) | ||
context.startService(intent) | ||
} | ||
|
||
@JvmStatic | ||
fun stopDownloadTask(context: Context) { | ||
val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager | ||
val processes = am.runningAppProcesses | ||
for (process in processes) { | ||
if (process.processName == "${context.packageName}:ZphDownloadService") { | ||
android.os.Process.killProcess(process.pid) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
api = RetrofitBuilder() | ||
.baseUrl("http://localhost/") | ||
.build(this) | ||
.create(DownloadApi::class.java) | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun onHandleIntent(intent: Intent) { | ||
downloadMap = intent.getSerializableExtra(EXTRA_DOWNLOAD_MAP) as Map<String, File> | ||
download() | ||
} | ||
|
||
private fun download() = runBlocking { | ||
downloadTask = this | ||
for (url in downloadMap.keys) { | ||
val destinationFile = downloadMap[url] ?: continue | ||
val responseBody = api.download(url) | ||
FileUtils.writeFileByIS(destinationFile, responseBody.byteStream(), false) | ||
} | ||
} | ||
} |