kunfei 3 년 전
부모
커밋
e7d6cf4c96
1개의 변경된 파일과 16개의 추가작업 그리고 9개의 파일을 삭제
  1. 16 9
      app/src/main/java/io/legado/app/service/ExportBookService.kt

+ 16 - 9
app/src/main/java/io/legado/app/service/ExportBookService.kt

@@ -34,6 +34,7 @@ import io.legado.app.utils.HtmlFormatter
 import io.legado.app.utils.MD5Utils
 import io.legado.app.utils.MD5Utils
 import io.legado.app.utils.NetworkUtils
 import io.legado.app.utils.NetworkUtils
 import io.legado.app.utils.activityPendingIntent
 import io.legado.app.utils.activityPendingIntent
+import io.legado.app.utils.buildMainHandler
 import io.legado.app.utils.cnCompare
 import io.legado.app.utils.cnCompare
 import io.legado.app.utils.createFolderIfNotExist
 import io.legado.app.utils.createFolderIfNotExist
 import io.legado.app.utils.isContentScheme
 import io.legado.app.utils.isContentScheme
@@ -45,12 +46,10 @@ import io.legado.app.utils.toastOnUi
 import io.legado.app.utils.writeBytes
 import io.legado.app.utils.writeBytes
 import kotlinx.coroutines.Deferred
 import kotlinx.coroutines.Deferred
 import kotlinx.coroutines.Dispatchers.IO
 import kotlinx.coroutines.Dispatchers.IO
-import kotlinx.coroutines.Dispatchers.Main
 import kotlinx.coroutines.Job
 import kotlinx.coroutines.Job
 import kotlinx.coroutines.async
 import kotlinx.coroutines.async
 import kotlinx.coroutines.ensureActive
 import kotlinx.coroutines.ensureActive
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.launch
-import kotlinx.coroutines.withContext
 import me.ag2s.epublib.domain.Author
 import me.ag2s.epublib.domain.Author
 import me.ag2s.epublib.domain.Date
 import me.ag2s.epublib.domain.Date
 import me.ag2s.epublib.domain.EpubBook
 import me.ag2s.epublib.domain.EpubBook
@@ -86,6 +85,7 @@ class ExportBookService : BaseService() {
         val epubScope: String? = null
         val epubScope: String? = null
     )
     )
 
 
+    private val handler = buildMainHandler()
     private val waitExportBooks = linkedMapOf<String, ExportConfig>()
     private val waitExportBooks = linkedMapOf<String, ExportConfig>()
     private var exportJob: Job? = null
     private var exportJob: Job? = null
 
 
@@ -124,14 +124,21 @@ class ExportBookService : BaseService() {
     override fun upNotification() {
     override fun upNotification() {
         val notification = NotificationCompat.Builder(this, AppConst.channelIdDownload)
         val notification = NotificationCompat.Builder(this, AppConst.channelIdDownload)
             .setSmallIcon(R.drawable.ic_export)
             .setSmallIcon(R.drawable.ic_export)
-            .setOngoing(true)
             .setContentTitle(getString(R.string.export))
             .setContentTitle(getString(R.string.export))
             .setContentIntent(activityPendingIntent<CacheActivity>("cacheActivity"))
             .setContentIntent(activityPendingIntent<CacheActivity>("cacheActivity"))
-        notification.addAction(
-            R.drawable.ic_stop_black_24dp,
-            getString(R.string.cancel),
-            servicePendingIntent<ExportBookService>(IntentAction.stop)
-        )
+        if (exportJob?.isActive == true) {
+            notification.setOngoing(true)
+            notification.addAction(
+                R.drawable.ic_stop_black_24dp,
+                getString(R.string.cancel),
+                servicePendingIntent<ExportBookService>(IntentAction.stop)
+            )
+        } else {
+            notification.setOngoing(false)
+            notification.setDeleteIntent(
+                servicePendingIntent<ExportBookService>(IntentAction.stop)
+            )
+        }
         notification.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
         notification.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
         notification.setContentText(notificationContent)
         notification.setContentText(notificationContent)
         startForeground(AppConst.notificationIdCache, notification.build())
         startForeground(AppConst.notificationIdCache, notification.build())
@@ -175,7 +182,7 @@ class ExportBookService : BaseService() {
                 exportProgress.remove(bookUrl)
                 exportProgress.remove(bookUrl)
                 postEvent(EventBus.EXPORT_BOOK, bookUrl)
                 postEvent(EventBus.EXPORT_BOOK, bookUrl)
             }
             }
-            withContext(Main) {
+            handler.post {
                 export()
                 export()
             }
             }
         }
         }