pull/1194/head
gedoor 3 years ago
parent 38e262393e
commit 94d4d15e7d
  1. 6
      app/src/main/java/io/legado/app/service/WebService.kt
  2. 35
      app/src/main/java/io/legado/app/service/help/AudioPlay.kt
  3. 25
      app/src/main/java/io/legado/app/service/help/CacheBook.kt
  4. 14
      app/src/main/java/io/legado/app/service/help/CheckSource.kt
  5. 16
      app/src/main/java/io/legado/app/service/help/Download.kt
  6. 4
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt

@ -28,9 +28,9 @@ class WebService : BaseService() {
fun stop(context: Context) { fun stop(context: Context) {
if (isRun) { if (isRun) {
val intent = Intent(context, WebService::class.java) context.startService<WebService> {
intent.action = IntentAction.stop action = IntentAction.stop
context.startService(intent) }
} }
} }

@ -13,6 +13,7 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.AudioPlayService import io.legado.app.service.AudioPlayService
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import io.legado.app.utils.startService
object AudioPlay { object AudioPlay {
var titleData = MutableLiveData<String>() var titleData = MutableLiveData<String>()
@ -52,43 +53,43 @@ object AudioPlay {
fun pause(context: Context) { fun pause(context: Context) {
if (AudioPlayService.isRun) { if (AudioPlayService.isRun) {
val intent = Intent(context, AudioPlayService::class.java) context.startService<AudioPlayService> {
intent.action = IntentAction.pause action = IntentAction.pause
context.startService(intent) }
} }
} }
fun resume(context: Context) { fun resume(context: Context) {
if (AudioPlayService.isRun) { if (AudioPlayService.isRun) {
val intent = Intent(context, AudioPlayService::class.java) context.startService<AudioPlayService> {
intent.action = IntentAction.resume action = IntentAction.resume
context.startService(intent) }
} }
} }
fun stop(context: Context) { fun stop(context: Context) {
if (AudioPlayService.isRun) { if (AudioPlayService.isRun) {
val intent = Intent(context, AudioPlayService::class.java) context.startService<AudioPlayService> {
intent.action = IntentAction.stop action = IntentAction.stop
context.startService(intent) }
} }
} }
fun adjustSpeed(context: Context, adjust: Float) { fun adjustSpeed(context: Context, adjust: Float) {
if (AudioPlayService.isRun) { if (AudioPlayService.isRun) {
val intent = Intent(context, AudioPlayService::class.java) context.startService<AudioPlayService> {
intent.action = IntentAction.adjustSpeed action = IntentAction.adjustSpeed
intent.putExtra("adjust", adjust) putExtra("adjust", adjust)
context.startService(intent) }
} }
} }
fun adjustProgress(context: Context, position: Int) { fun adjustProgress(context: Context, position: Int) {
if (AudioPlayService.isRun) { if (AudioPlayService.isRun) {
val intent = Intent(context, AudioPlayService::class.java) context.startService<AudioPlayService> {
intent.action = IntentAction.adjustProgress action = IntentAction.adjustProgress
intent.putExtra("position", position) putExtra("position", position)
context.startService(intent) }
} }
} }

@ -1,7 +1,6 @@
package io.legado.app.service.help package io.legado.app.service.help
import android.content.Context import android.content.Context
import android.content.Intent
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
@ -9,6 +8,7 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.CacheBookService import io.legado.app.service.CacheBookService
import io.legado.app.utils.msg import io.legado.app.utils.msg
import io.legado.app.utils.startService
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import splitties.init.appCtx import splitties.init.appCtx
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
@ -29,27 +29,24 @@ object CacheBook {
} }
fun start(context: Context, bookUrl: String, start: Int, end: Int) { fun start(context: Context, bookUrl: String, start: Int, end: Int) {
Intent(context, CacheBookService::class.java).let { context.startService<CacheBookService> {
it.action = IntentAction.start action = IntentAction.start
it.putExtra("bookUrl", bookUrl) putExtra("bookUrl", bookUrl)
it.putExtra("start", start) putExtra("start", start)
it.putExtra("end", end) putExtra("end", end)
context.startService(it)
} }
} }
fun remove(context: Context, bookUrl: String) { fun remove(context: Context, bookUrl: String) {
Intent(context, CacheBookService::class.java).let { context.startService<CacheBookService> {
it.action = IntentAction.remove action = IntentAction.remove
it.putExtra("bookUrl", bookUrl) putExtra("bookUrl", bookUrl)
context.startService(it)
} }
} }
fun stop(context: Context) { fun stop(context: Context) {
Intent(context, CacheBookService::class.java).let { context.startService<CacheBookService> {
it.action = IntentAction.stop action = IntentAction.stop
context.startService(it)
} }
} }

@ -1,11 +1,11 @@
package io.legado.app.service.help package io.legado.app.service.help
import android.content.Context import android.content.Context
import android.content.Intent
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.service.CheckSourceService import io.legado.app.service.CheckSourceService
import io.legado.app.utils.startService
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
object CheckSource { object CheckSource {
@ -20,17 +20,15 @@ object CheckSource {
sources.map { sources.map {
selectedIds.add(it.bookSourceUrl) selectedIds.add(it.bookSourceUrl)
} }
Intent(context, CheckSourceService::class.java).let { context.startService<CheckSourceService> {
it.action = IntentAction.start action = IntentAction.start
it.putExtra("selectIds", selectedIds) putExtra("selectIds", selectedIds)
context.startService(it)
} }
} }
fun stop(context: Context) { fun stop(context: Context) {
Intent(context, CheckSourceService::class.java).let { context.startService<CheckSourceService> {
it.action = IntentAction.stop action = IntentAction.stop
context.startService(it)
} }
} }
} }

@ -1,25 +1,23 @@
package io.legado.app.service.help package io.legado.app.service.help
import android.content.Context import android.content.Context
import android.content.Intent
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.service.DownloadService import io.legado.app.service.DownloadService
import io.legado.app.utils.startService
object Download { object Download {
fun start(context: Context, downloadId: Long, fileName: String) { fun start(context: Context, downloadId: Long, fileName: String) {
Intent(context, DownloadService::class.java).let { context.startService<DownloadService> {
it.action = IntentAction.start action = IntentAction.start
it.putExtra("downloadId", downloadId) putExtra("downloadId", downloadId)
it.putExtra("fileName", fileName) putExtra("fileName", fileName)
context.startService(it)
} }
} }
fun stop(context: Context) { fun stop(context: Context) {
Intent(context, DownloadService::class.java).let { context.startService<DownloadService> {
it.action = IntentAction.stop action = IntentAction.stop
context.startService(it)
} }
} }

@ -38,6 +38,10 @@ inline fun <reified T : Service> Context.startService(configIntent: Intent.() ->
startService(Intent(this, T::class.java).apply(configIntent)) startService(Intent(this, T::class.java).apply(configIntent))
} }
inline fun <reified T : Service> Context.stopService() {
stopService(Intent(this, T::class.java))
}
fun Context.toastOnUi(message: Int) { fun Context.toastOnUi(message: Int) {
runOnUI { runOnUI {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show() Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

Loading…
Cancel
Save