diff --git a/app/src/main/java/io/legado/app/service/WebService.kt b/app/src/main/java/io/legado/app/service/WebService.kt index 719f7f6e4..e8662a51f 100644 --- a/app/src/main/java/io/legado/app/service/WebService.kt +++ b/app/src/main/java/io/legado/app/service/WebService.kt @@ -28,9 +28,9 @@ class WebService : BaseService() { fun stop(context: Context) { if (isRun) { - val intent = Intent(context, WebService::class.java) - intent.action = IntentAction.stop - context.startService(intent) + context.startService { + action = IntentAction.stop + } } } diff --git a/app/src/main/java/io/legado/app/service/help/AudioPlay.kt b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt index daf2f2156..2bc8f006a 100644 --- a/app/src/main/java/io/legado/app/service/help/AudioPlay.kt +++ b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt @@ -13,6 +13,7 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.webBook.WebBook import io.legado.app.service.AudioPlayService import io.legado.app.utils.postEvent +import io.legado.app.utils.startService object AudioPlay { var titleData = MutableLiveData() @@ -52,43 +53,43 @@ object AudioPlay { fun pause(context: Context) { if (AudioPlayService.isRun) { - val intent = Intent(context, AudioPlayService::class.java) - intent.action = IntentAction.pause - context.startService(intent) + context.startService { + action = IntentAction.pause + } } } fun resume(context: Context) { if (AudioPlayService.isRun) { - val intent = Intent(context, AudioPlayService::class.java) - intent.action = IntentAction.resume - context.startService(intent) + context.startService { + action = IntentAction.resume + } } } fun stop(context: Context) { if (AudioPlayService.isRun) { - val intent = Intent(context, AudioPlayService::class.java) - intent.action = IntentAction.stop - context.startService(intent) + context.startService { + action = IntentAction.stop + } } } fun adjustSpeed(context: Context, adjust: Float) { if (AudioPlayService.isRun) { - val intent = Intent(context, AudioPlayService::class.java) - intent.action = IntentAction.adjustSpeed - intent.putExtra("adjust", adjust) - context.startService(intent) + context.startService { + action = IntentAction.adjustSpeed + putExtra("adjust", adjust) + } } } fun adjustProgress(context: Context, position: Int) { if (AudioPlayService.isRun) { - val intent = Intent(context, AudioPlayService::class.java) - intent.action = IntentAction.adjustProgress - intent.putExtra("position", position) - context.startService(intent) + context.startService { + action = IntentAction.adjustProgress + putExtra("position", position) + } } } diff --git a/app/src/main/java/io/legado/app/service/help/CacheBook.kt b/app/src/main/java/io/legado/app/service/help/CacheBook.kt index 3a8a5d0ce..520497c7e 100644 --- a/app/src/main/java/io/legado/app/service/help/CacheBook.kt +++ b/app/src/main/java/io/legado/app/service/help/CacheBook.kt @@ -1,7 +1,6 @@ package io.legado.app.service.help import android.content.Context -import android.content.Intent import io.legado.app.R import io.legado.app.constant.IntentAction 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.service.CacheBookService import io.legado.app.utils.msg +import io.legado.app.utils.startService import kotlinx.coroutines.CoroutineScope import splitties.init.appCtx import java.util.concurrent.ConcurrentHashMap @@ -29,27 +29,24 @@ object CacheBook { } fun start(context: Context, bookUrl: String, start: Int, end: Int) { - Intent(context, CacheBookService::class.java).let { - it.action = IntentAction.start - it.putExtra("bookUrl", bookUrl) - it.putExtra("start", start) - it.putExtra("end", end) - context.startService(it) + context.startService { + action = IntentAction.start + putExtra("bookUrl", bookUrl) + putExtra("start", start) + putExtra("end", end) } } fun remove(context: Context, bookUrl: String) { - Intent(context, CacheBookService::class.java).let { - it.action = IntentAction.remove - it.putExtra("bookUrl", bookUrl) - context.startService(it) + context.startService { + action = IntentAction.remove + putExtra("bookUrl", bookUrl) } } fun stop(context: Context) { - Intent(context, CacheBookService::class.java).let { - it.action = IntentAction.stop - context.startService(it) + context.startService { + action = IntentAction.stop } } diff --git a/app/src/main/java/io/legado/app/service/help/CheckSource.kt b/app/src/main/java/io/legado/app/service/help/CheckSource.kt index 725c303e7..1c00d5f77 100644 --- a/app/src/main/java/io/legado/app/service/help/CheckSource.kt +++ b/app/src/main/java/io/legado/app/service/help/CheckSource.kt @@ -1,11 +1,11 @@ package io.legado.app.service.help import android.content.Context -import android.content.Intent import io.legado.app.R import io.legado.app.constant.IntentAction import io.legado.app.data.entities.BookSource import io.legado.app.service.CheckSourceService +import io.legado.app.utils.startService import io.legado.app.utils.toastOnUi object CheckSource { @@ -20,17 +20,15 @@ object CheckSource { sources.map { selectedIds.add(it.bookSourceUrl) } - Intent(context, CheckSourceService::class.java).let { - it.action = IntentAction.start - it.putExtra("selectIds", selectedIds) - context.startService(it) + context.startService { + action = IntentAction.start + putExtra("selectIds", selectedIds) } } fun stop(context: Context) { - Intent(context, CheckSourceService::class.java).let { - it.action = IntentAction.stop - context.startService(it) + context.startService { + action = IntentAction.stop } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/help/Download.kt b/app/src/main/java/io/legado/app/service/help/Download.kt index cddbf59b6..1a5fcd657 100644 --- a/app/src/main/java/io/legado/app/service/help/Download.kt +++ b/app/src/main/java/io/legado/app/service/help/Download.kt @@ -1,25 +1,23 @@ package io.legado.app.service.help import android.content.Context -import android.content.Intent import io.legado.app.constant.IntentAction import io.legado.app.service.DownloadService +import io.legado.app.utils.startService object Download { fun start(context: Context, downloadId: Long, fileName: String) { - Intent(context, DownloadService::class.java).let { - it.action = IntentAction.start - it.putExtra("downloadId", downloadId) - it.putExtra("fileName", fileName) - context.startService(it) + context.startService { + action = IntentAction.start + putExtra("downloadId", downloadId) + putExtra("fileName", fileName) } } fun stop(context: Context) { - Intent(context, DownloadService::class.java).let { - it.action = IntentAction.stop - context.startService(it) + context.startService { + action = IntentAction.stop } } diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 315c76c67..c0a5b054a 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -38,6 +38,10 @@ inline fun Context.startService(configIntent: Intent.() -> startService(Intent(this, T::class.java).apply(configIntent)) } +inline fun Context.stopService() { + stopService(Intent(this, T::class.java)) +} + fun Context.toastOnUi(message: Int) { runOnUI { Toast.makeText(this, message, Toast.LENGTH_SHORT).show()