Run extension auto updater only when library service isnt running

Will wait for library service to finish, or downloads to finish before running
This commit is contained in:
Jays2Kings 2021-08-22 23:25:02 -04:00
parent 1b08e07037
commit 9eef1b48a8
4 changed files with 34 additions and 7 deletions

View File

@ -11,12 +11,15 @@ import android.os.Build
import android.os.IBinder import android.os.IBinder
import android.os.PowerManager import android.os.PowerManager
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.work.NetworkType
import com.github.pwittchen.reactivenetwork.library.Connectivity import com.github.pwittchen.reactivenetwork.library.Connectivity
import com.github.pwittchen.reactivenetwork.library.ReactiveNetwork import com.github.pwittchen.reactivenetwork.library.ReactiveNetwork
import com.jakewharton.rxrelay.BehaviorRelay import com.jakewharton.rxrelay.BehaviorRelay
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.extension.ExtensionUpdateJob
import eu.kanade.tachiyomi.util.lang.plusAssign import eu.kanade.tachiyomi.util.lang.plusAssign
import eu.kanade.tachiyomi.util.system.connectivityManager import eu.kanade.tachiyomi.util.system.connectivityManager
import eu.kanade.tachiyomi.util.system.isServiceRunning import eu.kanade.tachiyomi.util.system.isServiceRunning
@ -137,6 +140,10 @@ class DownloadService : Service() {
downloadManager.stopDownloads() downloadManager.stopDownloads()
callListeners(downloadManager.hasQueue()) callListeners(downloadManager.hasQueue())
wakeLock.releaseIfNeeded() wakeLock.releaseIfNeeded()
if (LibraryUpdateService.runExtensionUpdatesAfter) {
ExtensionUpdateJob.runJobAgain(this, NetworkType.CONNECTED)
LibraryUpdateService.runExtensionUpdatesAfter = false
}
super.onDestroy() super.onDestroy()
} }

View File

@ -6,6 +6,7 @@ import android.content.Intent
import android.os.Build import android.os.Build
import android.os.IBinder import android.os.IBinder
import android.os.PowerManager import android.os.PowerManager
import androidx.work.NetworkType
import coil.Coil import coil.Coil
import coil.request.CachePolicy import coil.request.CachePolicy
import coil.request.ImageRequest import coil.request.ImageRequest
@ -27,6 +28,7 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.data.track.UnattendedTrackService import eu.kanade.tachiyomi.data.track.UnattendedTrackService
import eu.kanade.tachiyomi.extension.ExtensionUpdateJob
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.model.toSChapter import eu.kanade.tachiyomi.source.model.toSChapter
@ -355,6 +357,10 @@ class LibraryUpdateService(
mangaShortcutManager.updateShortcuts() mangaShortcutManager.updateShortcuts()
failedUpdates.clear() failedUpdates.clear()
notifier.cancelProgressNotification() notifier.cancelProgressNotification()
if (runExtensionUpdatesAfter && !DownloadService.isRunning(this)) {
ExtensionUpdateJob.runJobAgain(this, NetworkType.CONNECTED)
runExtensionUpdatesAfter = false
}
} }
private suspend fun updateMangaInSource(source: Long): Boolean { private suspend fun updateMangaInSource(source: Long): Boolean {
@ -561,14 +567,14 @@ class LibraryUpdateService(
*/ */
const val KEY_MANGAS = "mangas" const val KEY_MANGAS = "mangas"
var runExtensionUpdatesAfter = false
/** /**
* Returns the status of the service. * Returns the status of the service.
* *
* @return true if the service is running, false otherwise. * @return true if the service is running, false otherwise.
*/ */
fun isRunning(): Boolean { fun isRunning() = instance != null
return instance != null
}
/** /**
* Starts the service. It will be started only if there isn't another instance already * Starts the service. It will be started only if there isn't another instance already

View File

@ -166,6 +166,13 @@ class ExtensionInstallService(
context.stopService(Intent(context, ExtensionUpdateJob::class.java)) context.stopService(Intent(context, ExtensionUpdateJob::class.java))
} }
/**
* Returns the status of the service.
*
* @return true if the service is running, false otherwise.
*/
fun isRunning() = instance != null
/** /**
* Key that defines what should be updated. * Key that defines what should be updated.
*/ */

View File

@ -17,6 +17,7 @@ import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager import androidx.work.WorkManager
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
@ -54,12 +55,16 @@ class ExtensionUpdateJob(private val context: Context, workerParams: WorkerParam
preferences.extensionUpdatesCount().set(extensions.size) preferences.extensionUpdatesCount().set(extensions.size)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
inputData.getBoolean(RUN_AUTO, true) && inputData.getBoolean(RUN_AUTO, true) &&
preferences.autoUpdateExtensions() != AutoUpdaterJob.NEVER preferences.autoUpdateExtensions() != AutoUpdaterJob.NEVER &&
!ExtensionInstallService.isRunning()
) { ) {
val cm = context.connectivityManager val cm = context.connectivityManager
val libraryServiceRunning = LibraryUpdateService.isRunning()
if ( if (
preferences.autoUpdateExtensions() == AutoUpdaterJob.ALWAYS || (
!cm.isActiveNetworkMetered preferences.autoUpdateExtensions() == AutoUpdaterJob.ALWAYS ||
!cm.isActiveNetworkMetered
) && !libraryServiceRunning
) { ) {
val extensionManager = Injekt.get<ExtensionManager>() val extensionManager = Injekt.get<ExtensionManager>()
val extensionsInstalledByApp = val extensionsInstalledByApp =
@ -83,8 +88,10 @@ class ExtensionUpdateJob(private val context: Context, workerParams: WorkerParam
extensions.removeAll(extensionsInstalledByApp) extensions.removeAll(extensionsInstalledByApp)
} }
} }
} else { } else if (!libraryServiceRunning) {
runJobAgain(context, NetworkType.UNMETERED) runJobAgain(context, NetworkType.UNMETERED)
} else {
LibraryUpdateService.runExtensionUpdatesAfter = true
} }
} }
NotificationManagerCompat.from(context).apply { NotificationManagerCompat.from(context).apply {