|
|
@ -9,12 +9,11 @@ import android.net.Uri |
|
|
|
import android.os.Build |
|
|
|
import android.os.Build |
|
|
|
import android.os.Environment |
|
|
|
import android.os.Environment |
|
|
|
import androidx.core.app.NotificationCompat |
|
|
|
import androidx.core.app.NotificationCompat |
|
|
|
import androidx.core.content.FileProvider |
|
|
|
|
|
|
|
import io.legado.app.R |
|
|
|
import io.legado.app.R |
|
|
|
import io.legado.app.base.BaseService |
|
|
|
import io.legado.app.base.BaseService |
|
|
|
import io.legado.app.constant.AppConst |
|
|
|
import io.legado.app.constant.AppConst |
|
|
|
import io.legado.app.constant.IntentAction |
|
|
|
import io.legado.app.constant.IntentAction |
|
|
|
import io.legado.app.utils.RealPathUtil |
|
|
|
import io.legado.app.utils.IntentType |
|
|
|
import io.legado.app.utils.msg |
|
|
|
import io.legado.app.utils.msg |
|
|
|
import io.legado.app.utils.servicePendingIntent |
|
|
|
import io.legado.app.utils.servicePendingIntent |
|
|
|
import io.legado.app.utils.toastOnUi |
|
|
|
import io.legado.app.utils.toastOnUi |
|
|
@ -25,9 +24,10 @@ import kotlinx.coroutines.launch |
|
|
|
import splitties.init.appCtx |
|
|
|
import splitties.init.appCtx |
|
|
|
import splitties.systemservices.downloadManager |
|
|
|
import splitties.systemservices.downloadManager |
|
|
|
import splitties.systemservices.notificationManager |
|
|
|
import splitties.systemservices.notificationManager |
|
|
|
import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 下载文件 |
|
|
|
|
|
|
|
*/ |
|
|
|
class DownloadService : BaseService() { |
|
|
|
class DownloadService : BaseService() { |
|
|
|
private val groupKey = "${appCtx.packageName}.download" |
|
|
|
private val groupKey = "${appCtx.packageName}.download" |
|
|
|
private val downloads = hashMapOf<Long, Pair<String, String>>() |
|
|
|
private val downloads = hashMapOf<Long, Pair<String, String>>() |
|
|
@ -58,12 +58,10 @@ class DownloadService : BaseService() { |
|
|
|
) |
|
|
|
) |
|
|
|
IntentAction.play -> { |
|
|
|
IntentAction.play -> { |
|
|
|
val id = intent.getLongExtra("downloadId", 0) |
|
|
|
val id = intent.getLongExtra("downloadId", 0) |
|
|
|
if (completeDownloads.contains(id) |
|
|
|
if (completeDownloads.contains(id)) { |
|
|
|
&& downloads[id]?.second?.endsWith(".apk") == true |
|
|
|
openDownload(id) |
|
|
|
) { |
|
|
|
|
|
|
|
installApk(id) |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
toastOnUi("下载的文件在Download文件夹") |
|
|
|
toastOnUi("未完成,下载的文件夹Download") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
IntentAction.stop -> { |
|
|
|
IntentAction.stop -> { |
|
|
@ -117,7 +115,7 @@ class DownloadService : BaseService() { |
|
|
|
completeDownloads.add(downloadId) |
|
|
|
completeDownloads.add(downloadId) |
|
|
|
val fileName = downloads[downloadId]?.second |
|
|
|
val fileName = downloads[downloadId]?.second |
|
|
|
if (fileName?.endsWith(".apk") == true) { |
|
|
|
if (fileName?.endsWith(".apk") == true) { |
|
|
|
installApk(downloadId) |
|
|
|
openDownload(downloadId) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
toastOnUi("$fileName ${getString(R.string.download_success)}") |
|
|
|
toastOnUi("$fileName ${getString(R.string.download_success)}") |
|
|
|
} |
|
|
|
} |
|
|
@ -172,22 +170,18 @@ class DownloadService : BaseService() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun installApk(downloadId: Long) { |
|
|
|
private fun openDownload(downloadId: Long) { |
|
|
|
downloadManager.getUriForDownloadedFile(downloadId)?.let { |
|
|
|
downloadManager.getUriForDownloadedFile(downloadId)?.let { uri -> |
|
|
|
val filePath = RealPathUtil.getPath(this, it) ?: return |
|
|
|
|
|
|
|
val file = File(filePath) |
|
|
|
|
|
|
|
//调用系统安装apk |
|
|
|
//调用系统安装apk |
|
|
|
val intent = Intent() |
|
|
|
val intent = Intent() |
|
|
|
intent.action = Intent.ACTION_VIEW |
|
|
|
intent.action = Intent.ACTION_VIEW |
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
|
|
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0版本以上 |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0版本以上 |
|
|
|
val contentUrl = FileProvider.getUriForFile(this, AppConst.authority, file) |
|
|
|
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) |
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) |
|
|
|
intent.setDataAndType(contentUrl, "application/vnd.android.package-archive") |
|
|
|
intent.setDataAndType(uri, IntentType.from(uri)) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
val uri: Uri = Uri.fromFile(file) |
|
|
|
intent.setDataAndType(uri, IntentType.from(uri)) |
|
|
|
intent.setDataAndType(uri, "application/vnd.android.package-archive") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|