diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt index 463a16ec5..92403178d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt @@ -6,6 +6,7 @@ import android.app.ActivityManager.MemoryInfo import android.app.ActivityOptions import android.content.Context import android.content.Context.ACTIVITY_SERVICE +import android.content.Context.POWER_SERVICE import android.content.ContextWrapper import android.content.OperationApplicationException import android.content.SharedPreferences @@ -17,6 +18,7 @@ import android.graphics.Color import android.net.Uri import android.os.Build import android.os.Bundle +import android.os.PowerManager import android.provider.Settings import android.view.View import android.view.ViewPropertyAnimator @@ -51,6 +53,9 @@ import kotlin.math.roundToLong val Context.activityManager: ActivityManager? get() = getSystemService(ACTIVITY_SERVICE) as? ActivityManager +val Context.powerManager: PowerManager? + get() = getSystemService(POWER_SERVICE) as? PowerManager + fun String.toUriOrNull() = if (isEmpty()) null else Uri.parse(this) suspend fun CoroutineWorker.trySetForeground(): Boolean = runCatchingCancellable { @@ -141,6 +146,10 @@ fun Context.isLowRamDevice(): Boolean { return activityManager?.isLowRamDevice ?: false } +fun Context.isPowerSaveMode(): Boolean { + return powerManager?.isPowerSaveMode == true +} + val Context.ramAvailable: Long get() { val result = MemoryInfo() diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/details/service/MangaPrefetchService.kt b/app/src/main/kotlin/org/koitharu/kotatsu/details/service/MangaPrefetchService.kt index 6c483d475..0f0844c13 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/details/service/MangaPrefetchService.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/details/service/MangaPrefetchService.kt @@ -10,12 +10,13 @@ import org.koitharu.kotatsu.core.model.parcelable.ParcelableMangaChapters import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.ui.CoroutineIntentService import org.koitharu.kotatsu.core.util.ext.getParcelableExtraCompat +import org.koitharu.kotatsu.core.util.ext.isPowerSaveMode +import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug import org.koitharu.kotatsu.history.data.HistoryRepository import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaChapter import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.util.runCatchingCancellable -import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug import javax.inject.Inject @AndroidEntryPoint @@ -115,6 +116,9 @@ class MangaPrefetchService : CoroutineIntentService() { if (source == MangaSource.LOCAL) { return false } + if (context.isPowerSaveMode()) { + return false + } val entryPoint = EntryPointAccessors.fromApplication(context, PrefetchCompanionEntryPoint::class.java) return entryPoint.contentCache.isCachingEnabled && entryPoint.settings.isContentPrefetchEnabled } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt index 48e5faf0e..7403c9019 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt @@ -34,6 +34,8 @@ import org.koitharu.kotatsu.core.util.FileSize import org.koitharu.kotatsu.core.util.RetainedLifecycleCoroutineScope import org.koitharu.kotatsu.core.util.ext.ensureSuccess import org.koitharu.kotatsu.core.util.ext.isNotEmpty +import org.koitharu.kotatsu.core.util.ext.isPowerSaveMode +import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug import org.koitharu.kotatsu.core.util.ext.ramAvailable import org.koitharu.kotatsu.core.util.ext.withProgress import org.koitharu.kotatsu.core.util.progress.ProgressDeferred @@ -42,7 +44,6 @@ import org.koitharu.kotatsu.local.data.PagesCache import org.koitharu.kotatsu.parsers.model.MangaPage import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.reader.ui.pager.ReaderPage -import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug import java.io.File import java.util.LinkedList import java.util.concurrent.atomic.AtomicInteger @@ -83,7 +84,10 @@ class PageLoader @Inject constructor( } fun isPrefetchApplicable(): Boolean { - return repository is RemoteMangaRepository && settings.isPagesPreloadEnabled && !isLowRam() + return repository is RemoteMangaRepository + && settings.isPagesPreloadEnabled + && !context.isPowerSaveMode() + && !isLowRam() } @AnyThread diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt index fb369ee01..18a2289ba 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt @@ -24,6 +24,7 @@ import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.ui.BasePreferenceFragment import org.koitharu.kotatsu.core.util.ext.observe +import org.koitharu.kotatsu.core.util.ext.powerManager import org.koitharu.kotatsu.settings.tracker.categories.TrackerCategoriesConfigSheet import org.koitharu.kotatsu.settings.utils.MultiSummaryProvider import org.koitharu.kotatsu.tracker.work.TrackerNotificationChannels @@ -155,7 +156,7 @@ class TrackerSettingsFragment : return } val packageName = context.packageName - val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager + val powerManager = context.powerManager ?: return if (!powerManager.isIgnoringBatteryOptimizations(packageName)) { try { val intent = Intent(