pull/63/head
kunfei 5 years ago
parent e568c14b04
commit dfbb47b56e
  1. 1
      app/src/main/java/io/legado/app/constant/Bus.kt
  2. 13
      app/src/main/java/io/legado/app/service/DownloadService.kt
  3. 8
      app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt

@ -19,4 +19,5 @@ object Bus {
const val AUDIO_SPEED = "audioSpeed" const val AUDIO_SPEED = "audioSpeed"
const val SHOW_RSS = "showRss" const val SHOW_RSS = "showRss"
const val WEB_SERVICE_STOP = "webServiceStop" const val WEB_SERVICE_STOP = "webServiceStop"
const val UP_DOWNLOAD = "upDownload"
} }

@ -1,16 +1,19 @@
package io.legado.app.service package io.legado.app.service
import android.content.Intent import android.content.Intent
import android.os.Handler
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import io.legado.app.App import io.legado.app.App
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.Action import io.legado.app.constant.Action
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.Bus
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.IntentHelp import io.legado.app.help.IntentHelp
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.utils.postEvent
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -18,10 +21,13 @@ import java.util.concurrent.Executors
class DownloadService : BaseService() { class DownloadService : BaseService() {
private var searchPool = Executors.newFixedThreadPool(16).asCoroutineDispatcher() private var searchPool = Executors.newFixedThreadPool(16).asCoroutineDispatcher()
private var tasks: ArrayList<Coroutine<*>> = arrayListOf() private var tasks: ArrayList<Coroutine<*>> = arrayListOf()
private val handler = Handler()
private var runnable: Runnable = Runnable { upDownload() }
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
updateNotification("正在启动下载") updateNotification("正在启动下载")
handler.postDelayed(runnable, 1000)
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@ -41,6 +47,7 @@ class DownloadService : BaseService() {
override fun onDestroy() { override fun onDestroy() {
tasks.clear() tasks.clear()
searchPool.close() searchPool.close()
handler.removeCallbacks(runnable)
super.onDestroy() super.onDestroy()
} }
@ -80,6 +87,12 @@ class DownloadService : BaseService() {
stopSelf() stopSelf()
} }
private fun upDownload() {
postEvent(Bus.UP_DOWNLOAD, true)
handler.removeCallbacks(runnable)
handler.postDelayed(runnable, 1000)
}
/** /**
* 更新通知 * 更新通知
*/ */

@ -9,8 +9,10 @@ import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
import io.legado.app.constant.Bus
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.service.help.Download import io.legado.app.service.help.Download
import io.legado.app.utils.observeEvent
import kotlinx.android.synthetic.main.activity_download.* import kotlinx.android.synthetic.main.activity_download.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -60,4 +62,10 @@ class DownloadActivity : BaseActivity(R.layout.activity_download) {
adapter.setItems(it) adapter.setItems(it)
}) })
} }
override fun observeLiveBus() {
observeEvent<Boolean>(Bus.UP_DOWNLOAD) {
adapter.notifyItemRangeChanged(0, adapter.itemCount, true)
}
}
} }
Loading…
Cancel
Save