feat: Add low on storage info; Closes #56

Signed-off-by: Myzel394 <50424412+Myzel394@users.noreply.github.com>
This commit is contained in:
Myzel394 2024-03-21 20:11:59 +01:00
parent 0ebbf86450
commit a6856476ce
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
5 changed files with 72 additions and 1 deletions

View File

@ -7,12 +7,15 @@ import android.content.Context
import android.database.Cursor import android.database.Cursor
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.storage.StorageManager
import android.provider.MediaStore import android.provider.MediaStore
import android.provider.MediaStore.Video.Media import android.provider.MediaStore.Video.Media
import android.util.Log import android.util.Log
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.net.toFile
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import app.myzel394.alibi.db.AppSettings
import app.myzel394.alibi.ui.MEDIA_RECORDINGS_PREFIX import app.myzel394.alibi.ui.MEDIA_RECORDINGS_PREFIX
import app.myzel394.alibi.ui.RECORDER_INTERNAL_SELECTED_VALUE import app.myzel394.alibi.ui.RECORDER_INTERNAL_SELECTED_VALUE
import app.myzel394.alibi.ui.RECORDER_MEDIA_SELECTED_VALUE import app.myzel394.alibi.ui.RECORDER_MEDIA_SELECTED_VALUE
@ -519,10 +522,32 @@ abstract class BatchesFolder(
return uri!! return uri!!
} }
fun getAvailableBytes(): Long {
val storageManager = context.getSystemService(StorageManager::class.java) ?: return -1
val file = when (type) {
BatchType.INTERNAL -> context.filesDir
BatchType.CUSTOM -> customFolder!!.uri.toFile()
BatchType.MEDIA -> scopedMediaContentUri.toFile()
}
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
storageManager.getAllocatableBytes(storageManager.getUuidForPath(file))
} else {
file.usableSpace;
}
}
enum class BatchType { enum class BatchType {
INTERNAL, INTERNAL,
CUSTOM, CUSTOM,
MEDIA, MEDIA,
} }
companion object {
fun requiredBytesForOneMinuteOfRecording(appSettings: AppSettings): Long {
// 300 MiB sounds like a good default
return 300 * 1024 * 1024
}
}
} }

View File

@ -135,7 +135,8 @@ class VideoBatchesFolder(
fun viaMediaFolder(context: Context) = VideoBatchesFolder(context, BatchType.MEDIA) fun viaMediaFolder(context: Context) = VideoBatchesFolder(context, BatchType.MEDIA)
fun importFromFolder(folder: String, context: Context) = when (folder) { fun importFromFolder(folder: String?, context: Context) = when (folder) {
null -> viaInternalFolder(context)
RECORDER_INTERNAL_SELECTED_VALUE -> viaInternalFolder(context) RECORDER_INTERNAL_SELECTED_VALUE -> viaInternalFolder(context)
RECORDER_MEDIA_SELECTED_VALUE -> viaMediaFolder(context) RECORDER_MEDIA_SELECTED_VALUE -> viaMediaFolder(context)
else -> viaCustomFolder( else -> viaCustomFolder(

View File

@ -0,0 +1,41 @@
package app.myzel394.alibi.ui.components.RecorderScreen.atoms
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import app.myzel394.alibi.R
import app.myzel394.alibi.db.AppSettings
import app.myzel394.alibi.helpers.BatchesFolder
import app.myzel394.alibi.helpers.VideoBatchesFolder
import app.myzel394.alibi.ui.components.atoms.MessageBox
import app.myzel394.alibi.ui.components.atoms.MessageType
@Composable
fun LowStorageInfo(
appSettings: AppSettings,
) {
val context = LocalContext.current
val availableBytes =
VideoBatchesFolder.importFromFolder(appSettings.saveFolder, context).getAvailableBytes()
val bytesPerMinute = BatchesFolder.requiredBytesForOneMinuteOfRecording(appSettings)
val requiredBytes = appSettings.maxDuration / 1000 / 60 * bytesPerMinute
// Allow for a 10% margin of error
val isLowOnStorage = availableBytes < requiredBytes * 1.1
println("LowStorageInfo: availableBytes: $availableBytes, requiredBytes: $requiredBytes, isLowOnStorage: $isLowOnStorage")
if (isLowOnStorage)
Box(
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)
) {
MessageBox(
type = MessageType.WARNING,
message = stringResource(R.string.ui_recorder_lowOnStorage_hint),
)
}
}

View File

@ -44,6 +44,7 @@ import app.myzel394.alibi.R
import app.myzel394.alibi.db.AppSettings import app.myzel394.alibi.db.AppSettings
import app.myzel394.alibi.ui.BIG_PRIMARY_BUTTON_MAX_WIDTH import app.myzel394.alibi.ui.BIG_PRIMARY_BUTTON_MAX_WIDTH
import app.myzel394.alibi.ui.BIG_PRIMARY_BUTTON_SIZE import app.myzel394.alibi.ui.BIG_PRIMARY_BUTTON_SIZE
import app.myzel394.alibi.ui.components.RecorderScreen.atoms.LowStorageInfo
import app.myzel394.alibi.ui.components.RecorderScreen.molecules.AudioRecordingStart import app.myzel394.alibi.ui.components.RecorderScreen.molecules.AudioRecordingStart
import app.myzel394.alibi.ui.components.RecorderScreen.molecules.QuickMaxDurationSelector import app.myzel394.alibi.ui.components.RecorderScreen.molecules.QuickMaxDurationSelector
import app.myzel394.alibi.ui.components.RecorderScreen.molecules.VideoRecordingStart import app.myzel394.alibi.ui.components.RecorderScreen.molecules.VideoRecordingStart
@ -212,5 +213,7 @@ fun StartRecording(
} }
} }
} }
LowStorageInfo(appSettings = appSettings)
} }
} }

View File

@ -193,4 +193,5 @@
<string name="goBack">Back</string> <string name="goBack">Back</string>
<string name="ui_recorder_action_saveCurrent">Save now?</string> <string name="ui_recorder_action_saveCurrent">Save now?</string>
<string name="ui_recorder_action_saveCurrent_explanation">You can save the current ongoing recording by pressing and holding down on the save button. The recording will continue in the background.</string> <string name="ui_recorder_action_saveCurrent_explanation">You can save the current ongoing recording by pressing and holding down on the save button. The recording will continue in the background.</string>
<string name="ui_recorder_lowOnStorage_hint">You are low on storage. Alibi may not function properly. Please free up some space.</string>
</resources> </resources>