Minor cleanup of wakelocks, extension ID backup

This commit is contained in:
arkon 2020-05-07 09:14:52 -04:00
parent 03baa21185
commit b4c7992726
8 changed files with 32 additions and 30 deletions

View File

@ -9,6 +9,7 @@ import android.os.IBinder
import android.os.PowerManager import android.os.PowerManager
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.util.system.acquireWakeLock
import eu.kanade.tachiyomi.util.system.isServiceRunning import eu.kanade.tachiyomi.util.system.isServiceRunning
/** /**
@ -73,10 +74,7 @@ class BackupCreateService : Service() {
startForeground(Notifications.ID_BACKUP_PROGRESS, notifier.showBackupProgress().build()) startForeground(Notifications.ID_BACKUP_PROGRESS, notifier.showBackupProgress().build())
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock( wakeLock = acquireWakeLock(javaClass.name)
PowerManager.PARTIAL_WAKE_LOCK, "${javaClass.name}:WakeLock"
)
wakeLock.acquire()
} }
override fun stopService(name: Intent?): Boolean { override fun stopService(name: Intent?): Boolean {

View File

@ -46,6 +46,7 @@ import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.database.models.TrackImpl import eu.kanade.tachiyomi.data.database.models.TrackImpl
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
@ -131,8 +132,10 @@ class BackupManager(val context: Context, version: Int = CURRENT_VERSION) {
mangaEntries.add(backupMangaObject(manga, flags)) mangaEntries.add(backupMangaObject(manga, flags))
// Maintain set of extensions/sources used (excludes local source) // Maintain set of extensions/sources used (excludes local source)
if (manga.source != 0L && sourceManager.get(manga.source) != null) { if (manga.source != LocalSource.ID) {
extensions.add("${manga.source}:${sourceManager.get(manga.source)!!.name}") sourceManager.get(manga.source)?.let {
extensions.add("${manga.source}:${it.name}")
}
} }
} }

View File

@ -32,6 +32,7 @@ import eu.kanade.tachiyomi.data.database.models.TrackImpl
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.util.system.acquireWakeLock
import eu.kanade.tachiyomi.util.system.isServiceRunning import eu.kanade.tachiyomi.util.system.isServiceRunning
import java.io.File import java.io.File
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -127,10 +128,7 @@ class BackupRestoreService : Service() {
startForeground(Notifications.ID_RESTORE_PROGRESS, notifier.showRestoreProgress().build()) startForeground(Notifications.ID_RESTORE_PROGRESS, notifier.showRestoreProgress().build())
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock( wakeLock = acquireWakeLock(javaClass.name)
PowerManager.PARTIAL_WAKE_LOCK, "${javaClass.name}:WakeLock"
)
wakeLock.acquire()
} }
override fun stopService(name: Intent?): Boolean { override fun stopService(name: Intent?): Boolean {

View File

@ -16,9 +16,9 @@ import eu.kanade.tachiyomi.R
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.util.lang.plusAssign import eu.kanade.tachiyomi.util.lang.plusAssign
import eu.kanade.tachiyomi.util.system.acquireWakeLock
import eu.kanade.tachiyomi.util.system.connectivityManager import eu.kanade.tachiyomi.util.system.connectivityManager
import eu.kanade.tachiyomi.util.system.notification import eu.kanade.tachiyomi.util.system.notification
import eu.kanade.tachiyomi.util.system.powerManager
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers import rx.schedulers.Schedulers
@ -70,9 +70,7 @@ class DownloadService : Service() {
/** /**
* Wake lock to prevent the device to enter sleep mode. * Wake lock to prevent the device to enter sleep mode.
*/ */
private val wakeLock by lazy { private lateinit var wakeLock: PowerManager.WakeLock
powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DownloadService:WakeLock")
}
/** /**
* Subscriptions to store while the service is running. * Subscriptions to store while the service is running.
@ -85,6 +83,7 @@ class DownloadService : Service() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
startForeground(Notifications.ID_DOWNLOAD_CHAPTER, getPlaceholderNotification()) startForeground(Notifications.ID_DOWNLOAD_CHAPTER, getPlaceholderNotification())
wakeLock = acquireWakeLock(javaClass.name)
runningRelay.call(true) runningRelay.call(true)
subscriptions = CompositeSubscription() subscriptions = CompositeSubscription()
listenDownloaderState() listenDownloaderState()

View File

@ -35,6 +35,7 @@ import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
import eu.kanade.tachiyomi.util.lang.chop import eu.kanade.tachiyomi.util.lang.chop
import eu.kanade.tachiyomi.util.system.acquireWakeLock
import eu.kanade.tachiyomi.util.system.isServiceRunning import eu.kanade.tachiyomi.util.system.isServiceRunning
import eu.kanade.tachiyomi.util.system.notification import eu.kanade.tachiyomi.util.system.notification
import eu.kanade.tachiyomi.util.system.notificationBuilder import eu.kanade.tachiyomi.util.system.notificationBuilder
@ -182,11 +183,10 @@ class LibraryUpdateService(
*/ */
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
startForeground(Notifications.ID_LIBRARY_PROGRESS, progressNotificationBuilder.build()) startForeground(Notifications.ID_LIBRARY_PROGRESS, progressNotificationBuilder.build())
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "LibraryUpdateService:WakeLock" wakeLock = acquireWakeLock(javaClass.name)
)
wakeLock.acquire()
} }
/** /**

View File

@ -18,6 +18,7 @@ import eu.kanade.tachiyomi.network.newCallWithProgress
import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.storage.getUriCompat
import eu.kanade.tachiyomi.util.storage.saveTo import eu.kanade.tachiyomi.util.storage.saveTo
import eu.kanade.tachiyomi.util.system.acquireWakeLock
import eu.kanade.tachiyomi.util.system.isServiceRunning import eu.kanade.tachiyomi.util.system.isServiceRunning
import java.io.File import java.io.File
import timber.log.Timber import timber.log.Timber
@ -40,10 +41,7 @@ class UpdaterService : Service() {
startForeground(Notifications.ID_UPDATER, notifier.onDownloadStarted().build()) startForeground(Notifications.ID_UPDATER, notifier.onDownloadStarted().build())
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock( wakeLock = acquireWakeLock(javaClass.name)
PowerManager.PARTIAL_WAKE_LOCK, "${javaClass.name}:WakeLock"
)
wakeLock.acquire()
} }
/** /**
@ -143,11 +141,10 @@ class UpdaterService : Service() {
context.isServiceRunning(UpdaterService::class.java) context.isServiceRunning(UpdaterService::class.java)
/** /**
* Make a backup from library * Downloads a new update and let the user install the new version from a notification.
* *
* @param context context of application * @param context the application context.
* @param uri path of Uri * @param url the url to the new update.
* @param flags determines what to backup
*/ */
fun start(context: Context, url: String, title: String = context.getString(R.string.app_name)) { fun start(context: Context, url: String, title: String = context.getString(R.string.app_name)) {
if (!isRunning(context)) { if (!isRunning(context)) {

View File

@ -3,12 +3,10 @@ package eu.kanade.tachiyomi.ui.setting
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Dialog import android.app.Dialog
import android.content.ActivityNotFoundException import android.content.ActivityNotFoundException
import android.content.Context.POWER_SERVICE
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.PowerManager
import android.provider.Settings import android.provider.Settings
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.MaterialDialog
@ -28,6 +26,7 @@ import eu.kanade.tachiyomi.util.preference.preference
import eu.kanade.tachiyomi.util.preference.summaryRes import eu.kanade.tachiyomi.util.preference.summaryRes
import eu.kanade.tachiyomi.util.preference.switchPreference import eu.kanade.tachiyomi.util.preference.switchPreference
import eu.kanade.tachiyomi.util.preference.titleRes import eu.kanade.tachiyomi.util.preference.titleRes
import eu.kanade.tachiyomi.util.system.powerManager
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import rx.Observable import rx.Observable
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
@ -96,8 +95,7 @@ class SettingsAdvancedController : SettingsController() {
onClick { onClick {
val packageName: String = context.packageName val packageName: String = context.packageName
val pm = context.getSystemService(POWER_SERVICE) as PowerManager? if (!context.powerManager.isIgnoringBatteryOptimizations(packageName)) {
if (!pm!!.isIgnoringBatteryOptimizations(packageName)) {
try { try {
val intent = Intent().apply { val intent = Intent().apply {
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

View File

@ -147,6 +147,15 @@ val Context.connectivityManager: ConnectivityManager
val Context.powerManager: PowerManager val Context.powerManager: PowerManager
get() = getSystemService(Context.POWER_SERVICE) as PowerManager get() = getSystemService(Context.POWER_SERVICE) as PowerManager
/**
* Convenience method to acquire a partial wake lock.
*/
fun Context.acquireWakeLock(tag: String): PowerManager.WakeLock {
val wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag:WakeLock")
wakeLock.acquire()
return wakeLock
}
/** /**
* Function used to send a local broadcast asynchronous * Function used to send a local broadcast asynchronous
* *