Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ import com.nextcloud.client.device.PowerManagementService
object PowerManagementFactory {
@JvmStatic
val mock: PowerManagementService = object : PowerManagementService {
override val isIgnoringOptimization: Boolean
override val isExemptFromBatteryOptimization: Boolean
get() = true
override val isPowerSavingEnabled: Boolean
get() = false
override val battery: BatteryStatus
get() = BatteryStatus(false, 0)
override val blocksAutoUpload: Boolean
override val isAppRestrictedByPowerSaving: Boolean
get() = false
}

@JvmStatic
@Suppress("MagicNumber")
val mockCharging: PowerManagementService = object : PowerManagementService {
override val isIgnoringOptimization: Boolean
override val isExemptFromBatteryOptimization: Boolean
get() = true
override val isPowerSavingEnabled: Boolean
get() = false
override val battery: BatteryStatus
get() = BatteryStatus(true, 100)
override val blocksAutoUpload: Boolean
override val isAppRestrictedByPowerSaving: Boolean
get() = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ interface PowerManagementService {
val isPowerSavingEnabled: Boolean

/**
* Checks app is excluded from battery optimization or not
* True when the user set the app's battery usage to "Unrestricted", which puts the app on Android's power save
* allowlist. Android then exempts the app from power saving mode as well: its jobs and alarms are not restricted
* while power saving mode is on.
*
* @see android.os.PowerManager.isIgnoringBatteryOptimizations
*/
val isIgnoringOptimization: Boolean
val isExemptFromBatteryOptimization: Boolean

/**
* Checks current battery status using platform [android.os.BatteryManager]
*/
val battery: BatteryStatus

val blocksAutoUpload: Boolean
/**
* True when power saving mode is on and the app is not exempt from battery optimization.
*/
val isAppRestrictedByPowerSaving: Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class PowerManagementServiceImpl(
}
}

override val isIgnoringOptimization: Boolean
override val isExemptFromBatteryOptimization: Boolean
get() {
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
return powerManager.isIgnoringBatteryOptimizations(context.packageName)
Expand All @@ -38,8 +38,8 @@ internal class PowerManagementServiceImpl(
return platformPowerManager.isPowerSaveMode
}

override val blocksAutoUpload: Boolean
get() = isPowerSavingEnabled && !isIgnoringOptimization
override val isAppRestrictedByPowerSaving: Boolean
get() = isPowerSavingEnabled && !isExemptFromBatteryOptimization

@Suppress("MagicNumber") // 100% is 100, we're not doing Cobol
override val battery: BatteryStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ContentObserverWork(
}

private suspend fun checkAndTriggerAutoUpload() = withContext(Dispatchers.IO) {
if (powerManagementService.blocksAutoUpload) {
if (powerManagementService.isAppRestrictedByPowerSaving) {
Log_OC.w(TAG, "⚡ Power saving mode active — skipping file sync.")
return@withContext
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class AutoUploadWorker(
}

private fun isBlocked(syncedFolderID: Long): Boolean {
if (powerManagementService.blocksAutoUpload && !overridePowerSaving) {
if (powerManagementService.isAppRestrictedByPowerSaving && !overridePowerSaving) {
Log_OC.w(TAG, "⚡ Skipping: device is in power saving mode")
return true
}
Expand Down Expand Up @@ -499,7 +499,7 @@ class AutoUploadWorker(
true,
FileDataStorageManager(user, context.contentResolver)
).apply {
if (overridePowerSaving || !powerManagementService.blocksAutoUpload) {
if (overridePowerSaving || !powerManagementService.isAppRestrictedByPowerSaving) {
isIgnoringPowerSaveMode = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ class FileUploadHelper {
conditions = UploadResult.DELAYED_FOR_CHARGING
}

// check that device is not in power save mode; delay upload otherwise
if (powerManagementService.isPowerSavingEnabled) {
// check that power save mode is not restricting the app; delay upload otherwise
if (powerManagementService.isAppRestrictedByPowerSaving) {
conditions = UploadResult.DELAYED_IN_POWER_SAVE_MODE
}

Expand Down
125 changes: 0 additions & 125 deletions app/src/main/java/com/nextcloud/ui/component/UploadWarningCard.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package com.nextcloud.ui.component.powerSettings

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.PowerManager
import android.provider.Settings
import android.view.View
import androidx.core.net.toUri
import com.nextcloud.client.device.PowerManagementService
import com.nextcloud.client.jobs.BackgroundJobManager
import com.nextcloud.utils.SnackbarUtil
import com.nextcloud.utils.extensions.setVisibleIf
import com.owncloud.android.databinding.PowerSettingsBannersBinding
import com.owncloud.android.datamodel.SyncedFolderProvider
import com.owncloud.android.utils.FilesSyncHelper
import com.owncloud.android.utils.theme.ViewThemeUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class PowerSettingsBanners(
private val context: Context,
private val powerManagementService: PowerManagementService,
private val syncedFolderProvider: SyncedFolderProvider,
private val backgroundJobManager: BackgroundJobManager,
private val scope: CoroutineScope,
private val viewThemeUtils: ViewThemeUtils
) {
private var binding: PowerSettingsBannersBinding? = null

private val powerSavingModeChangedReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action != PowerManager.ACTION_POWER_SAVE_MODE_CHANGED) return

binding?.let { bind(it) }
}
}

fun register(context: Context, binding: PowerSettingsBannersBinding) {
this.binding = binding
bind(binding)

context.registerReceiver(
powerSavingModeChangedReceiver,
IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)
)
}

fun unregister(context: Context) {
context.unregisterReceiver(powerSavingModeChangedReceiver)
binding = null
}

fun bind(binding: PowerSettingsBannersBinding) {
val state = PowerSettingsBannersState.from(powerManagementService)

binding.root.setVisibleIf(state != PowerSettingsBannersState.UNRESTRICTED)
bindPowerSavingBanner(binding, state)

bindBatteryOptimizationBanner(binding, state)
}

private fun bindPowerSavingBanner(binding: PowerSettingsBannersBinding, state: PowerSettingsBannersState) {
val messageId = state.powerSavingMessageId
binding.powerSavingBanner.setVisibleIf(messageId != null)
if (messageId == null) return

viewThemeUtils.material.run {
themeCardView(binding.powerSavingBanner)
colorMaterialButtonPrimaryBorderless(binding.powerSavingSettingsButton)
colorMaterialButtonPrimaryBorderless(binding.powerSavingSyncNowButton)
}

binding.powerSavingBannerMessage.setText(messageId)
binding.powerSavingBannerActions.setVisibleIf(
state == PowerSettingsBannersState.POWER_SAVING_RESTRICTING_APP
)
binding.powerSavingSettingsButton.setOnClickListener { openPowerSavingSettings() }

binding.powerSavingSyncNowButton.setOnClickListener { startAutoUploadIgnoringPowerSaving(it) }
}

private fun bindBatteryOptimizationBanner(binding: PowerSettingsBannersBinding, state: PowerSettingsBannersState) {
binding.batteryOptimizationBanner.setVisibleIf(state.isBatteryOptimizationBannerVisible)
if (!state.isBatteryOptimizationBannerVisible) return

viewThemeUtils.material.run {
themeCardView(binding.batteryOptimizationBanner)
colorMaterialButtonPrimaryBorderless(binding.batteryOptimizationExemptButton)
}

binding.batteryOptimizationExemptButton.setOnClickListener { requestBatteryOptimizationExemption() }
}

private fun openPowerSavingSettings() {
context.startActivity(Intent(Settings.ACTION_BATTERY_SAVER_SETTINGS))
}

@SuppressLint("BatteryLife")
private fun requestBatteryOptimizationExemption() {
val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
intent.data = "package:${context.packageName}".toUri()

context.startActivity(intent)
}

private fun startAutoUploadIgnoringPowerSaving(view: View) {
scope.launch {
val result = withContext(Dispatchers.IO) {
FilesSyncHelper.startAutoUploadIgnoringPowerSaving(syncedFolderProvider, backgroundJobManager)
}

SnackbarUtil.show(view, result.messageId)
}
}
}
Loading
Loading