Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Fix data-saver always being enabled on API 23 #611

Merged
merged 1 commit into from
May 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions app/src/main/java/app/tivi/util/AndroidPowerController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package app.tivi.util
import android.content.Context
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.os.Build
import android.os.PowerManager
import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService
import androidx.core.net.ConnectivityManagerCompat
import app.tivi.settings.TiviPreferences
import javax.inject.Inject
import javax.inject.Singleton
Expand Down Expand Up @@ -49,17 +50,22 @@ internal class AndroidPowerController @Inject constructor(
}
}

override fun shouldSaveData(): SaveData {
if (preferences.useLessData) {
return SaveData.Enabled(SaveDataReason.PREFERENCE)
override fun shouldSaveData(): SaveData = when {
preferences.useLessData -> {
SaveData.Enabled(SaveDataReason.PREFERENCE)
}
if (powerManager.isPowerSaveMode) {
return SaveData.Enabled(SaveDataReason.SYSTEM_POWER_SAVER)
powerManager.isPowerSaveMode -> {
SaveData.Enabled(SaveDataReason.SYSTEM_POWER_SAVER)
}
if (ConnectivityManagerCompat.getRestrictBackgroundStatus(connectivityManager)
== ConnectivityManagerCompat.RESTRICT_BACKGROUND_STATUS_ENABLED) {
return SaveData.Enabled(SaveDataReason.SYSTEM_DATA_SAVER)
Build.VERSION.SDK_INT >= 24 && isBackgroundDataRestricted() -> {
SaveData.Enabled(SaveDataReason.SYSTEM_DATA_SAVER)
}
return SaveData.Disabled
else -> SaveData.Disabled
}

@RequiresApi(24)
private fun isBackgroundDataRestricted(): Boolean {
return connectivityManager.restrictBackgroundStatus ==
ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED
}
}