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 @@ -10,7 +10,7 @@ package com.nextcloud.client.device
* This class exposes battery status information
* in platform-independent way.
*
* @param isCharging true if device battery is charging
* @param isCharging true if the battery is charging or full, false when plugged in but held at a charge limit
* @param level Battery level, from 0 to 100%
*
* @see [android.os.BatteryManager]
Expand All @@ -20,11 +20,4 @@ data class BatteryStatus(val isCharging: Boolean = false, val level: Int = 0) {
companion object {
const val BATTERY_FULL = 100
}

/**
* True if battery is fully loaded, false otherwise.
* Some dodgy devices can report battery charging
* status as "battery full".
*/
val isFull: Boolean get() = level >= BATTERY_FULL
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ internal class PowerManagementServiceImpl(
ReceiverFlag.NotExported
)
val isCharging = intent?.let {
when (it.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0)) {
BatteryManager.BATTERY_PLUGGED_USB -> true
BatteryManager.BATTERY_PLUGGED_AC -> true
BatteryManager.BATTERY_PLUGGED_WIRELESS -> true
when (it.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN)) {
BatteryManager.BATTERY_STATUS_CHARGING -> true
BatteryManager.BATTERY_STATUS_FULL -> true
else -> false
}
} ?: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ class AutoUploadWorker(

override suspend fun doWork(): Result {
return try {
trySetForeground()

val syncFolderId = inputData.getLong(SYNCED_FOLDER_ID, -1)
syncedFolder = syncedFolderProvider.getSyncedFolderByID(syncFolderId)
?.takeIf { it.isEnabled } ?: return Result.failure()
Expand Down Expand Up @@ -139,28 +137,13 @@ class AutoUploadWorker(
return notificationManager.getForegroundInfo(notification)
}

private suspend fun updateNotification() {
getStartNotificationTitle()?.let { (localFolderName, remoteFolderName) ->
try {
val startNotification = createNotification(
context.getString(
R.string.auto_upload_worker_start_text,
localFolderName,
remoteFolderName
)
)

setForeground(notificationManager.getForegroundInfo(startNotification))
} catch (e: Exception) {
Log_OC.w(TAG, "⚠️ Could not update notification: ${e.message}")
}
}
}

private suspend fun trySetForeground() {
val title = getStartNotificationTitle()?.let { (localFolderName, remoteFolderName) ->
context.getString(R.string.auto_upload_worker_start_text, localFolderName, remoteFolderName)
} ?: context.getString(R.string.upload_files)

try {
val notification = createNotification(context.getString(R.string.upload_files))
setForeground(notificationManager.getForegroundInfo(notification))
setForeground(notificationManager.getForegroundInfo(createNotification(title)))
} catch (e: Exception) {
Log_OC.w(TAG, "⚠️ Could not set foreground service: ${e.message}")
}
Expand Down Expand Up @@ -208,9 +191,17 @@ class AutoUploadWorker(
return true
}

if (isWaitingForCharging()) {
Log_OC.w(TAG, "🔌 Skipping: charging only folder but battery is not charging")
return true
}

return false
}

private fun isWaitingForCharging(): Boolean =
syncedFolder.isChargingOnly && !powerManagementService.battery.isCharging

private suspend fun hasNothingToDo(): Boolean {
if (repository.hasPendingFiles(syncedFolder)) {
Log_OC.d(TAG, "pending files found, starting...")
Expand Down Expand Up @@ -272,7 +263,7 @@ class AutoUploadWorker(
.getClientFor(ocAccount, context)
val capability = CapabilityUtils.getCapability(user, context)

updateNotification()
trySetForeground()

var lastId = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ class FileUploadHelper {
}

// check if charging conditions are met; delay upload otherwise
if (upload.isWhileChargingOnly && !battery.isCharging && !battery.isFull) {
if (upload.isWhileChargingOnly && !battery.isCharging) {
conditions = UploadResult.DELAYED_FOR_CHARGING
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,15 @@ class TestPowerManagementService {
// GIVEN
// battery status sticky intent is available
whenever(deviceInfo.apiLevel).thenReturn(Build.VERSION_CODES.P)
val powerSources = setOf(
BatteryManager.BATTERY_PLUGGED_AC,
BatteryManager.BATTERY_PLUGGED_USB,
BatteryManager.BATTERY_PLUGGED_WIRELESS
val chargingStatuses = setOf(
BatteryManager.BATTERY_STATUS_CHARGING,
BatteryManager.BATTERY_STATUS_FULL
)

for (row in powerSources) {
for (row in chargingStatuses) {
// WHEN
// device is charging using supported power source
whenever(intent.getIntExtra(eq(BatteryManager.EXTRA_PLUGGED), any()))
// battery is charging or full
whenever(intent.getIntExtra(eq(BatteryManager.EXTRA_STATUS), any()))
.thenReturn(row)

// THEN
Expand Down
Loading