Don't do backup, if space is already low, warn instead

This commit is contained in:
Torsten Grote 2024-05-07 15:38:18 -03:00 committed by Chirayu Desai
parent 36910fc777
commit 49262682d1
2 changed files with 22 additions and 0 deletions

View file

@ -6,6 +6,7 @@
package com.stevesoltys.seedvault.plugins
import android.content.Context
import android.util.Log
import androidx.annotation.WorkerThread
import com.stevesoltys.seedvault.getStorageContext
import com.stevesoltys.seedvault.permitDiskReads
@ -131,4 +132,17 @@ class StoragePluginManager(
return storage.isUnavailableUsb(systemContext)
}
/**
* Retrieves the amount of free space in bytes, or null if unknown.
*/
@WorkerThread
suspend fun getFreeSpace(): Long? {
return try {
appPlugin.getFreeSpace()
} catch (e: Exception) {
Log.e("StoragePluginManager", "Error getting free space: ", e)
null
}
}
}

View file

@ -22,6 +22,7 @@ import androidx.work.OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import com.stevesoltys.seedvault.plugins.StoragePluginManager
import com.stevesoltys.seedvault.settings.SettingsManager
import com.stevesoltys.seedvault.ui.notification.BackupNotificationManager
import com.stevesoltys.seedvault.ui.notification.NOTIFICATION_ID_OBSERVER
@ -38,6 +39,7 @@ class AppBackupWorker(
private val TAG = AppBackupWorker::class.simpleName
internal const val UNIQUE_WORK_NAME = "com.stevesoltys.seedvault.APP_BACKUP"
private const val TAG_RESCHEDULE = "com.stevesoltys.seedvault.TAG_RESCHEDULE"
private const val MIN_FREE_SPACE = 1024 * 1024 * 500 // 500 MB
/**
* (Re-)schedules the [AppBackupWorker].
@ -99,6 +101,7 @@ class AppBackupWorker(
private val backupRequester: BackupRequester by inject()
private val settingsManager: SettingsManager by inject()
private val apkBackupManager: ApkBackupManager by inject()
private val storagePluginManager: StoragePluginManager by inject()
private val nm: BackupNotificationManager by inject()
override suspend fun doWork(): Result {
@ -108,6 +111,11 @@ class AppBackupWorker(
} catch (e: Exception) {
Log.e(TAG, "Error while running setForeground: ", e)
}
val freeSpace = storagePluginManager.getFreeSpace()
if (freeSpace != null && freeSpace < MIN_FREE_SPACE) {
nm.onInsufficientSpaceError()
return Result.failure()
}
return try {
if (isStopped) {
Result.retry()