feat(ui): add backup mode to the confirmation screen

This commit is contained in:
opencode.siil.arti.ee committed 2026-11-17 17:45:03 +00:00
1 parent 0cb99758d4
commit 5b188bf555
3 files changed
+320 -121

No files matched your search

@@ -88,8 +88,10 @@ import com.airbnb.lottie.compose.rememberLottieDynamicProperties
import com.airbnb.lottie.compose.rememberLottieDynamicProperty
import eu.depau.etchdroid.AppSettings
import eu.depau.etchdroid.Intents
import eu.depau.etchdroid.JobOperation
import eu.depau.etchdroid.R
import eu.depau.etchdroid.getProgressUpdateIntent
import eu.depau.etchdroid.getStartBackupJobIntent
import eu.depau.etchdroid.getStartJobIntent
import eu.depau.etchdroid.massstorage.PreviewUsbDevice
import eu.depau.etchdroid.massstorage.UsbMassStorageDeviceDescriptor
@@ -110,6 +112,7 @@ import eu.depau.etchdroid.utils.ktexts.startForegroundServiceCompat
import eu.depau.etchdroid.utils.ktexts.toHRSize
import eu.depau.etchdroid.utils.ktexts.toast
import eu.depau.etchdroid.utils.ktexts.usbDevice
import eu.depau.etchdroid.utils.withoutPartSuffix
import kotlin.random.Random
private const val TAG = "ConfirmOperationActivit"
@@ -211,11 +214,17 @@ class ConfirmOperationActivity : ActivityBase() {
unregisterUsbReceiver()
}
private fun writeImage(uri: Uri, device: UsbMassStorageDeviceDescriptor) {
println("writeImage $uri, $device")
private fun startJob(
uri: Uri, device: UsbMassStorageDeviceDescriptor, operation: JobOperation
) {
println("startJob $operation $uri, $device")
val jobId = Random.nextInt()
val intent =
val isBackup = operation == JobOperation.BACKUP
val intent = if (isBackup) {
getStartBackupJobIntent(uri, device, jobId, this, WorkerService::class.java)
} else {
getStartJobIntent(uri, device, jobId, 0, false, this, WorkerService::class.java)
}
Telemetry.addBreadcrumb {
category = "flow"
message = "Starting worker service; job ID: $jobId"
@@ -225,7 +234,8 @@ class ConfirmOperationActivity : ActivityBase() {
startForegroundServiceCompat(intent)
startActivity(
getProgressUpdateIntent(
uri, device, jobId, 0f, 0, 0, false, this, ProgressActivity::class.java
uri, device, jobId, 0f, 0, 0, false, this, ProgressActivity::class.java,
isBackup = isBackup,
)
)
finish()
@@ -234,6 +244,17 @@ class ConfirmOperationActivity : ActivityBase() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val operation = try {
JobOperation.valueOf(
intent.getStringExtra("operation") ?: JobOperation.WRITE.name
)
} catch (_: IllegalArgumentException) {
Log.w(TAG, "Unknown operation in intent, falling back to write")
JobOperation.WRITE
}
val isBackup = operation == JobOperation.BACKUP
// In backup mode this is the destination file, in write mode the source image.
val openedImage = intent.safeParcelableExtra<Uri>("sourceUri") ?: run {
Log.e(TAG, "No source image URI provided")
toast(getString(R.string.no_image_uri_provided))
@@ -241,8 +262,13 @@ class ConfirmOperationActivity : ActivityBase() {
return
}
if (!openedImage.isAccessible(this)) {
Log.e(TAG, "Source image is no longer accessible: $openedImage")
toast(getString(R.string.could_not_open_the_source_file))
Log.e(TAG, "Job file is no longer accessible: $openedImage")
toast(
getString(
if (isBackup) R.string.could_not_write_backup_file
else R.string.could_not_open_the_source_file
)
)
finish()
return
}
@@ -253,13 +279,16 @@ class ConfirmOperationActivity : ActivityBase() {
finish()
return
}
mViewModel.init(openedImage, selectedDevice)
mViewModel.init(openedImage, selectedDevice, operation)
val imageFileName = openedImage.getFileName(this) ?: "unknown"
Telemetry.addBreadcrumb {
category = "flow"
message = "Image opened"
message = if (isBackup) "Backup destination opened" else "Image opened"
data["image.filename"] = imageFileName
if (isBackup) {
data["job.type"] = "backup"
}
}
Telemetry.configureScope {
@@ -268,13 +297,19 @@ class ConfirmOperationActivity : ActivityBase() {
setTag("usb.vidpid", selectedDevice.vidpid)
setTag("usb.name", selectedDevice.name)
setTag("image.filename", imageFileName)
try {
setTag(
"image.size",
openedImage.getFileSize(this@ConfirmOperationActivity).toString()
)
} catch (_: Exception) {
if (isBackup) {
setTag("job.type", "backup")
// The destination file is created empty: its size says nothing about the job.
setTag("image.size", "unknown")
} else {
try {
setTag(
"image.size",
openedImage.getFileSize(this@ConfirmOperationActivity).toString()
)
} catch (_: Exception) {
setTag("image.size", "unknown")
}
}
}
@@ -330,8 +365,8 @@ class ConfirmOperationActivity : ActivityBase() {
LayFlatOnTableBottomSheet(
onReady = {
println("onReady")
writeImage(
uiState.openedImage!!, uiState.selectedDevice!!
startJob(
uiState.openedImage!!, uiState.selectedDevice!!, uiState.operation
)
},
onDismissRequest = { showLayFlatDialog = false },
@@ -509,146 +544,239 @@ fun ConfirmationView(
askUsbPermission: () -> Unit,
) {
val uiState by viewModel.state.collectAsState()
val isBackup = uiState.operation == JobOperation.BACKUP
ConfirmationViewLayout(
modifier = Modifier.fillMaxSize(),
title = {
Text(
text = stringResource(R.string.confirm_operation),
text = stringResource(
if (isBackup) R.string.back_up_usb_drive else R.string.confirm_operation
),
style = MaterialTheme.typography.titleLarge.copy(fontSize = 28.sp),
)
},
sourceCard = { modifier, fillMaxSize ->
Card(modifier) {
Row(
modifier = Modifier
.align(Alignment.CenterHorizontally)
.then(if (fillMaxSize) Modifier.fillMaxSize() else Modifier),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Start
) {
Icon(
if (isBackup) {
Card(modifier) {
Row(
modifier = Modifier
.size(128.dp)
.padding(16.dp, 16.dp, 0.dp, 16.dp),
imageVector = ImageVector.vectorResource(
id = R.drawable.ic_disk_image_large
), contentDescription = stringResource(R.string.disk_image)
)
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(
8.dp,
Alignment.CenterVertically
)
.align(Alignment.CenterHorizontally)
.then(if (fillMaxSize) Modifier.fillMaxSize() else Modifier),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Start
) {
val sourceUri = uiState.openedImage
val context = LocalContext.current
val sourceFileName by remember {
derivedStateOf {
sourceUri?.getFileName(context)
?: context.getString(R.string.unknown_filename)
Icon(
modifier = Modifier
.size(128.dp)
.padding(16.dp, 16.dp, 0.dp, 16.dp),
imageVector = ImageVector.vectorResource(
id = R.drawable.ic_usb_stick_large
), contentDescription = stringResource(R.string.usb_drive)
)
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(
8.dp,
Alignment.CenterVertically
)
) {
Text(
text = stringResource(R.string.source_usb_device),
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.Bold,
)
Column {
Text(
text = uiState.selectedDevice?.name ?: stringResource(
R.string.unknown_device
),
style = MaterialTheme.typography.labelLarge,
maxLines = 1,
softWrap = true,
overflow = TextOverflow.Ellipsis
)
Text(
text = uiState.selectedDevice?.vidpid ?: "Unknown VID:PID",
style = MaterialTheme.typography.labelMedium,
softWrap = true,
fontStyle = FontStyle.Italic, maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
GrantAccessButton(
hasUsbPermission = uiState.hasUsbPermission,
askUsbPermission = askUsbPermission,
)
}
val sourceFileSize by remember {
derivedStateOf {
try {
sourceUri!!.getFileSize(context).toHRSize()
} catch (e: Exception) {
Log.w(TAG, "Failed to get file size", e)
"Unknown file size"
}
}
} else {
Card(modifier) {
Row(
modifier = Modifier
.align(Alignment.CenterHorizontally)
.then(if (fillMaxSize) Modifier.fillMaxSize() else Modifier),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Start
) {
Icon(
modifier = Modifier
.size(128.dp)
.padding(16.dp, 16.dp, 0.dp, 16.dp),
imageVector = ImageVector.vectorResource(
id = R.drawable.ic_disk_image_large
), contentDescription = stringResource(R.string.disk_image)
)
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(
8.dp,
Alignment.CenterVertically
)
) {
val sourceUri = uiState.openedImage
val context = LocalContext.current
val sourceFileName by remember {
derivedStateOf {
sourceUri?.getFileName(context)
?: context.getString(R.string.unknown_filename)
}
}
val sourceFileSize by remember {
derivedStateOf {
try {
sourceUri!!.getFileSize(context).toHRSize()
} catch (e: Exception) {
Log.w(TAG, "Failed to get file size", e)
"Unknown file size"
}
}
}
}
Text(
text = stringResource(R.string.image_to_write),
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.Bold,
modifier = Modifier.padding(0.dp, 0.dp, 0.dp, 8.dp)
)
Column {
Text(
text = sourceFileName,
text = stringResource(R.string.image_to_write),
style = MaterialTheme.typography.labelLarge,
maxLines = 2,
softWrap = true,
overflow = TextOverflow.Ellipsis
)
Text(
text = sourceFileSize,
style = MaterialTheme.typography.labelMedium,
fontStyle = FontStyle.Italic,
maxLines = 1,
overflow = TextOverflow.Ellipsis
fontWeight = FontWeight.Bold,
modifier = Modifier.padding(0.dp, 0.dp, 0.dp, 8.dp)
)
Column {
Text(
text = sourceFileName,
style = MaterialTheme.typography.labelLarge,
maxLines = 2,
softWrap = true,
overflow = TextOverflow.Ellipsis
)
Text(
text = sourceFileSize,
style = MaterialTheme.typography.labelMedium,
fontStyle = FontStyle.Italic,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
}
}
}
}
},
destinationCard = { modifier ->
Card(modifier) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Start
) {
Icon(
modifier = Modifier
.size(128.dp)
.padding(16.dp, 16.dp, 0.dp, 16.dp),
imageVector = ImageVector.vectorResource(
id = R.drawable.ic_usb_stick_large
), contentDescription = stringResource(R.string.usb_drive)
)
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(
8.dp,
Alignment.CenterVertically
)
if (isBackup) {
Card(modifier) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Start
) {
Text(
text = stringResource(R.string.destination_usb_device),
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.Bold,
Icon(
modifier = Modifier
.size(128.dp)
.padding(16.dp, 16.dp, 0.dp, 16.dp),
imageVector = ImageVector.vectorResource(
id = R.drawable.ic_disk_image_large
), contentDescription = stringResource(R.string.disk_image)
)
Column {
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(
8.dp,
Alignment.CenterVertically
)
) {
val destinationUri = uiState.openedImage
val context = LocalContext.current
val unknownFileName = stringResource(R.string.unknown_filename)
val destinationFileName by remember {
derivedStateOf {
destinationUri?.getFileName(context)?.withoutPartSuffix()
?: unknownFileName
}
}
Text(
text = uiState.selectedDevice?.name ?: stringResource(
R.string.unknown_device
),
text = stringResource(R.string.destination_file),
style = MaterialTheme.typography.labelLarge,
maxLines = 1,
softWrap = true,
overflow = TextOverflow.Ellipsis
fontWeight = FontWeight.Bold,
)
Text(
text = uiState.selectedDevice?.vidpid ?: "Unknown VID:PID",
style = MaterialTheme.typography.labelMedium,
text = destinationFileName,
style = MaterialTheme.typography.labelLarge,
maxLines = 2,
softWrap = true,
fontStyle = FontStyle.Italic, maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
Button(
modifier = Modifier.appiumTag("grantUsbPermissionButton"),
onClick = askUsbPermission,
enabled = !uiState.hasUsbPermission,
contentPadding = if (!uiState.hasUsbPermission) PaddingValues(
24.dp, 8.dp
}
}
} else {
Card(modifier) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Start
) {
Icon(
modifier = Modifier
.size(128.dp)
.padding(16.dp, 16.dp, 0.dp, 16.dp),
imageVector = ImageVector.vectorResource(
id = R.drawable.ic_usb_stick_large
), contentDescription = stringResource(R.string.usb_drive)
)
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(
8.dp,
Alignment.CenterVertically
)
else PaddingValues(24.dp, 8.dp, 16.dp, 8.dp),
) {
Text(text = stringResource(R.string.grant_access))
if (uiState.hasUsbPermission) {
Icon(
modifier = Modifier.padding(8.dp, 0.dp, 0.dp, 0.dp),
imageVector = Icons.TwoTone.Check,
contentDescription = stringResource(R.string.permission_granted)
Text(
text = stringResource(R.string.destination_usb_device),
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.Bold,
)
Column {
Text(
text = uiState.selectedDevice?.name ?: stringResource(
R.string.unknown_device
),
style = MaterialTheme.typography.labelLarge,
maxLines = 1,
softWrap = true,
overflow = TextOverflow.Ellipsis
)
Text(
text = uiState.selectedDevice?.vidpid ?: "Unknown VID:PID",
style = MaterialTheme.typography.labelMedium,
softWrap = true,
fontStyle = FontStyle.Italic, maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
GrantAccessButton(
hasUsbPermission = uiState.hasUsbPermission,
askUsbPermission = askUsbPermission,
)
}
}
}
@@ -684,7 +812,8 @@ fun ConfirmationView(
)
Text(
text = stringResource(
R.string.writing_the_image_will_erase
if (isBackup) R.string.backup_warning
else R.string.writing_the_image_will_erase
), style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onErrorContainer
)
@@ -703,12 +832,41 @@ fun ConfirmationView(
onClick = onConfirm,
enabled = uiState.selectedDevice != null && uiState.hasUsbPermission
) {
Text(text = stringResource(R.string.write_image))
Text(
text = stringResource(
if (isBackup) R.string.start_backup else R.string.write_image
)
)
}
}
)
}
@Composable
private fun GrantAccessButton(
hasUsbPermission: Boolean,
askUsbPermission: () -> Unit,
) {
Button(
modifier = Modifier.appiumTag("grantUsbPermissionButton"),
onClick = askUsbPermission,
enabled = !hasUsbPermission,
contentPadding = if (!hasUsbPermission) PaddingValues(
24.dp, 8.dp
)
else PaddingValues(24.dp, 8.dp, 16.dp, 8.dp),
) {
Text(text = stringResource(R.string.grant_access))
if (hasUsbPermission) {
Icon(
modifier = Modifier.padding(8.dp, 0.dp, 0.dp, 0.dp),
imageVector = Icons.TwoTone.Check,
contentDescription = stringResource(R.string.permission_granted)
)
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LayFlatOnTableBottomSheet(
@@ -878,18 +1036,31 @@ fun LayFlatOnTableBottomSheet(
@PreviewScreenSizes
@Composable
fun ConfirmationViewPreview() {
fun ConfirmationViewPreview() = ConfirmationViewPreviewContent(JobOperation.WRITE)
@PreviewScreenSizes
@Composable
fun ConfirmationViewBackupPreview() = ConfirmationViewPreviewContent(JobOperation.BACKUP)
@Composable
private fun ConfirmationViewPreviewContent(operation: JobOperation) {
val isBackup = operation == JobOperation.BACKUP
val viewModel = remember {
ConfirmOperationActivityViewModel().apply {
setState(
state.value.copy(
operation = operation,
selectedDevice = UsbMassStorageDeviceDescriptor(
previewUsbDevice = PreviewUsbDevice(
name = "Kingston DataTraveler 3.0",
vidpid = "dead:beef",
)
),
openedImage = "file:///storage/emulated/0/Download/etchdroid-test-image-very-long-name-lol-rip-ive-never-seen-a-filename-this-long-its-absolutely-crazy.img".toUri(),
openedImage = if (isBackup) {
"file:///storage/emulated/0/Download/backup-kingston-datatraveler-3-0-20261117-1200.img.part".toUri()
} else {
"file:///storage/emulated/0/Download/etchdroid-test-image-very-long-name-lol-rip-ive-never-seen-a-filename-this-long-its-absolutely-crazy.img".toUri()
},
)
)
}
+5
View File
@@ -46,16 +46,21 @@
<string name="connect_a_usb_drive" comment="Title of the dialog listing the available USB drives, when no USB drive is connected">Connect a USB drive</string>
<string name="looking_for_usb_drives">Looking for USB drives</string>
<string name="confirm_operation" comment="Title of the page where the operation details are listed; the user can confirm or cancel the operation from here">Confirm operation</string>
<string name="back_up_usb_drive" comment="Title of the page where the backup details are listed; the user can confirm or cancel the backup from here">Back up USB drive</string>
<string name="disk_image">Disk image</string>
<string name="image_to_write" comment="Label for the card that displays the details about the image that is about to be written">Image to write</string>
<string name="usb_drive">USB drive</string>
<string name="destination_usb_device" comment="Label for the card that displays the details about the USB drive that is about to be used">Destination USB device</string>
<string name="source_usb_device" comment="Label for the card that displays the details about the USB drive that is about to be backed up">Source USB device</string>
<string name="destination_file" comment="Label for the card that displays the details about the file that the backup will be written to">Destination file</string>
<string name="unknown_device" comment="Device name placeholder">Unknown device</string>
<string name="grant_access" comment="Displayed on the button to request the permission to write to the USB drive. Keep it short.">Grant access</string>
<string name="permission_granted">Permission granted</string>
<string name="be_careful" comment="Title of the card that alerts the user that, if they continue, all data on the USB drive will be lost">Be careful!</string>
<string name="writing_the_image_will_erase" comment="Message displayed on the card that alerts the user that, if they continue, all data on the USB drive will be lost">Writing the image will erase all data on the selected drive</string>
<string name="backup_warning" comment="Message displayed on the card that warns the user about what to expect while the backup is running">Keep the reader connected. The backup is as large as the card and may take a long time.</string>
<string name="write_image" comment="Confirmation button; when pressed, the operation starts">Write image</string>
<string name="start_backup" comment="Confirmation button of the backup page; when pressed, the backup starts">Start backup</string>
<string name="lay_your_device_flat_no_accel" comment="Title of the dialog that forces the user to lay the device flat on a table before continuing, when the device has no gravity sensor">Lay your device flat on a table before continuing.</string>
<string name="continue_" comment="Button displayed in the dialog that forces the user to lay the device flat on a table before continuing, when the device has no gravity sensor; when pressed the operation starts.">Continue</string>
<string name="lay_your_device_flat" comment="Title of the dialog that forces the user to lay the device flat on a table before continuing, when the device has a gravity sensor">Lay your device flat on a table and don\'t touch it</string>
@@ -0,0 +1,23 @@
package eu.depau.etchdroid
import androidx.core.net.toUri
import eu.depau.etchdroid.massstorage.UsbMassStorageDeviceDescriptor
import eu.depau.etchdroid.ui.ConfirmOperationActivityState
import eu.depau.etchdroid.ui.ConfirmOperationActivityViewModel
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.robolectric.annotation.Config
import tech.apter.junit.jupiter.robolectric.RobolectricExtension
@ExtendWith(RobolectricExtension::class)
@Config(application = EtchDroidApplication::class)
class ConfirmOperationActivityViewModelTest {
@Test
fun initStoresOperation() {
val vm = ConfirmOperationActivityViewModel()
vm.init("content://test/x".toUri(), UsbMassStorageDeviceDescriptor(), JobOperation.BACKUP)
assertEquals(JobOperation.BACKUP, vm.state.value.operation)
assertEquals(JobOperation.WRITE, ConfirmOperationActivityState.Empty.operation)
}
}