diff --git a/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePluginManager.kt b/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePluginManager.kt index 509c0f5d..d4614e39 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePluginManager.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePluginManager.kt @@ -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 + } + } + } diff --git a/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt b/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt index 64a1cbed..e47e7e84 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt @@ -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()