pull/1344/head
gedoor 3 years ago
parent d36a59ad12
commit 73f5285c09
  1. 2
      app/src/main/java/io/legado/app/constant/AppPattern.kt
  2. 37
      app/src/main/java/io/legado/app/service/DownloadService.kt

@ -12,7 +12,7 @@ object AppPattern {
val imgPattern: Pattern = Pattern.compile("<img[^>]*src=\"([^\"]*(?:\"[^>]+\\})?)\"[^>]*>") val imgPattern: Pattern = Pattern.compile("<img[^>]*src=\"([^\"]*(?:\"[^>]+\\})?)\"[^>]*>")
val nameRegex = Regex("\\s+作\\s*者.*|\\s+\\S+\\s+著") val nameRegex = Regex("\\s+作\\s*者.*|\\s+\\S+\\s+著")
val authorRegex = Regex("^.*?\\s*者[::\\s]*|\\s+著") val authorRegex = Regex("^\\s*\\s*者[::\\s]+|\\s+著")
val fileNameRegex = Regex("[\\\\/:*?\"<>|.]") val fileNameRegex = Regex("[\\\\/:*?\"<>|.]")
val splitGroupRegex = Regex("[,;,;]") val splitGroupRegex = Regex("[,;,;]")

@ -8,6 +8,7 @@ import android.content.IntentFilter
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.FileProvider 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
@ -21,12 +22,15 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import splitties.init.appCtx
import splitties.systemservices.downloadManager import splitties.systemservices.downloadManager
import splitties.systemservices.notificationManager
import java.io.File import java.io.File
class DownloadService : BaseService() { class DownloadService : BaseService() {
private val groupKey = "${appCtx.packageName}.download"
private val summaryId = 745893
private val downloads = hashMapOf<Long, String>() private val downloads = hashMapOf<Long, String>()
private val completeDownloads = hashSetOf<Long>() private val completeDownloads = hashSetOf<Long>()
private var upStateJob: Job? = null private var upStateJob: Job? = null
@ -38,6 +42,7 @@ class DownloadService : BaseService() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
upSummaryNotification()
registerReceiver(downloadReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) registerReceiver(downloadReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
} }
@ -61,7 +66,7 @@ class DownloadService : BaseService() {
IntentAction.stop -> { IntentAction.stop -> {
val downloadId = intent.getLongExtra("downloadId", 0) val downloadId = intent.getLongExtra("downloadId", 0)
downloads.remove(downloadId) downloads.remove(downloadId)
stopSelf() notificationManager.cancel(downloadId.toInt())
} }
} }
return super.onStartCommand(intent, flags, startId) return super.onStartCommand(intent, flags, startId)
@ -87,6 +92,10 @@ class DownloadService : BaseService() {
//查询下载进度 //查询下载进度
private fun queryState() { private fun queryState() {
if (downloads.isEmpty()) {
stopSelf()
return
}
val ids = downloads.keys val ids = downloads.keys
val query = DownloadManager.Query() val query = DownloadManager.Query()
query.setFilterById(*ids.toLongArray()) query.setFilterById(*ids.toLongArray())
@ -144,6 +153,24 @@ class DownloadService : BaseService() {
} }
} }
private fun upSummaryNotification() {
val notificationBuilder = NotificationCompat.Builder(this, AppConst.channelIdDownload)
.setSmallIcon(R.drawable.ic_download)
.setOngoing(true)
.setContentTitle(getString(R.string.action_download))
notificationBuilder.addAction(
R.drawable.ic_stop_black_24dp,
getString(R.string.cancel),
servicePendingIntent<DownloadService>(IntentAction.stop) {
putExtra("downloadId", -1)
}
)
notificationBuilder.setGroup(groupKey)
.setGroupSummary(true)
val notification = notificationBuilder.build()
startForeground(summaryId, notification)
}
/** /**
* 更新通知 * 更新通知
*/ */
@ -172,9 +199,11 @@ class DownloadService : BaseService() {
notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
notificationBuilder.setContentText(content) notificationBuilder.setContentText(content)
notificationBuilder.setProgress(max, progress, false) notificationBuilder.setProgress(max, progress, false)
notificationBuilder.setAutoCancel(true) notificationBuilder.setGroup(groupKey)
val notification = notificationBuilder.build() val notification = notificationBuilder.build()
startForeground(downloadId.toInt(), notification) NotificationManagerCompat.from(this).apply {
notify(downloadId.toInt(), notification)
}
} }
} }
Loading…
Cancel
Save