feat(intents): add backup job plumbing
This commit is contained in:
1 parent
0843aa3701
commit
9ff2cdb502
3 files changed
+144
-4
No files matched your search
@@ -13,8 +13,12 @@ import kotlinx.parcelize.IgnoredOnParcel
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import java.util.Random
|
||||
|
||||
/** Which way the image data flows for a job. */
|
||||
enum class JobOperation { WRITE, BACKUP }
|
||||
|
||||
object Intents {
|
||||
const val START_JOB = "eu.depau.etchdroid.action.START_JOB"
|
||||
const val START_BACKUP = "eu.depau.etchdroid.action.START_BACKUP"
|
||||
const val USB_PERMISSION = "eu.depau.etchdroid.action.USB_PERMISSION"
|
||||
const val SKIP_VERIFY = "eu.depau.etchdroid.action.CANCEL_VERIFY"
|
||||
const val JOB_PROGRESS = "eu.depau.etchdroid.broadcast.JOB_PROGRESS"
|
||||
@@ -32,6 +36,8 @@ data class JobStatusInfo(
|
||||
val jobId: Int,
|
||||
val isVerifying: Boolean = false,
|
||||
val exception: EtchDroidException? = null,
|
||||
val isBackup: Boolean = false,
|
||||
val sha256: String? = null,
|
||||
) : Parcelable {
|
||||
@IgnoredOnParcel
|
||||
val percent =
|
||||
@@ -48,11 +54,13 @@ fun getConfirmOperationActivityIntent(
|
||||
destDevice: UsbMassStorageDeviceDescriptor,
|
||||
packageContext: Context? = null,
|
||||
cls: Class<*>? = null,
|
||||
operation: JobOperation = JobOperation.WRITE,
|
||||
): Intent {
|
||||
return mkIntent(packageContext, cls).apply {
|
||||
data = sourceUri
|
||||
putExtra("sourceUri", sourceUri)
|
||||
putExtra("destDevice", destDevice)
|
||||
putExtra("operation", operation.name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +91,32 @@ fun getStartJobIntent(
|
||||
}
|
||||
}
|
||||
|
||||
fun getStartBackupJobIntent(
|
||||
destinationUri: Uri,
|
||||
sourceDevice: UsbMassStorageDeviceDescriptor,
|
||||
jobId: Int,
|
||||
packageContext: Context? = null,
|
||||
cls: Class<*>? = null,
|
||||
): Intent {
|
||||
return mkIntent(packageContext, cls).apply {
|
||||
action = Intents.START_BACKUP
|
||||
data = destinationUri
|
||||
// Same rationale as getStartJobIntent: hand the URI grants to the service so they outlive
|
||||
// the activity. A backup writes the image and may read it back to verify, so request both
|
||||
// grants, but only when we actually hold them (granting a lost permission throws).
|
||||
if (packageContext?.checkCallingOrSelfUriPermission(
|
||||
destinationUri, Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
) == PackageManager.PERMISSION_GRANTED
|
||||
) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
if (packageContext?.checkCallingOrSelfUriPermission(
|
||||
destinationUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||
) == PackageManager.PERMISSION_GRANTED
|
||||
) addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||
putExtra("destDevice", sourceDevice)
|
||||
putExtra("jobId", jobId)
|
||||
}
|
||||
}
|
||||
|
||||
fun getProgressUpdateIntent(
|
||||
sourceUri: Uri,
|
||||
destDevice: UsbMassStorageDeviceDescriptor,
|
||||
@@ -93,13 +127,14 @@ fun getProgressUpdateIntent(
|
||||
isVerifying: Boolean = false,
|
||||
packageContext: Context? = null,
|
||||
cls: Class<*>? = null,
|
||||
isBackup: Boolean = false,
|
||||
) = mkIntent(packageContext, cls).apply {
|
||||
action = Intents.JOB_PROGRESS
|
||||
putExtra("sourceUri", sourceUri)
|
||||
putExtra(
|
||||
"status", JobStatusInfo(
|
||||
sourceUri, destDevice, processedBytes, totalBytes, speed, jobId,
|
||||
isVerifying = isVerifying
|
||||
isVerifying = isVerifying, isBackup = isBackup
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -113,6 +148,7 @@ fun getErrorIntent(
|
||||
exception: EtchDroidException,
|
||||
packageContext: Context? = null,
|
||||
cls: Class<*>? = null,
|
||||
isBackup: Boolean = false,
|
||||
) = mkIntent(packageContext, cls).apply {
|
||||
action = Intents.ERROR
|
||||
putExtra("sourceUri", sourceUri)
|
||||
@@ -120,7 +156,7 @@ fun getErrorIntent(
|
||||
"status",
|
||||
JobStatusInfo(
|
||||
sourceUri, destDevice, processedBytes, totalBytes,
|
||||
jobId = jobId, exception = exception
|
||||
jobId = jobId, exception = exception, isBackup = isBackup
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -131,10 +167,18 @@ fun getFinishedIntent(
|
||||
totalBytes: Long,
|
||||
packageContext: Context? = null,
|
||||
cls: Class<*>? = null,
|
||||
isBackup: Boolean = false,
|
||||
sha256: String? = null,
|
||||
) = mkIntent(packageContext, cls).apply {
|
||||
action = Intents.FINISHED
|
||||
putExtra("sourceUri", sourceUri)
|
||||
putExtra("status", JobStatusInfo(sourceUri, destDevice, totalBytes, totalBytes, jobId = -1))
|
||||
putExtra(
|
||||
"status",
|
||||
JobStatusInfo(
|
||||
sourceUri, destDevice, totalBytes, totalBytes, jobId = -1,
|
||||
isBackup = isBackup, sha256 = sha256
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun Intent.getProgressActivityPendingIntent(
|
||||
|
||||
@@ -13,6 +13,7 @@ import androidx.compose.runtime.remember
|
||||
import androidx.lifecycle.ViewModel
|
||||
import eu.depau.etchdroid.AppSettings
|
||||
import eu.depau.etchdroid.Intents
|
||||
import eu.depau.etchdroid.JobOperation
|
||||
import eu.depau.etchdroid.JobStatusInfo
|
||||
import eu.depau.etchdroid.SettingChangeListener
|
||||
import eu.depau.etchdroid.ThemeMode
|
||||
@@ -172,6 +173,7 @@ data class ConfirmOperationActivityState(
|
||||
val openedImage: Uri? = null,
|
||||
val selectedDevice: UsbMassStorageDeviceDescriptor? = null,
|
||||
val hasUsbPermission: Boolean = false,
|
||||
val operation: JobOperation = JobOperation.WRITE,
|
||||
) : IThemeState {
|
||||
companion object {
|
||||
val Empty: ConfirmOperationActivityState
|
||||
@@ -197,11 +199,16 @@ class ConfirmOperationActivityViewModel : ViewModel(), SettingChangeListener,
|
||||
_state.update { state }
|
||||
}
|
||||
|
||||
fun init(openedImage: Uri?, selectedDevice: UsbMassStorageDeviceDescriptor?) = _state.update {
|
||||
fun init(
|
||||
openedImage: Uri?,
|
||||
selectedDevice: UsbMassStorageDeviceDescriptor?,
|
||||
operation: JobOperation = JobOperation.WRITE,
|
||||
) = _state.update {
|
||||
it.copy(
|
||||
openedImage = openedImage,
|
||||
selectedDevice = selectedDevice,
|
||||
hasUsbPermission = false,
|
||||
operation = operation,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -235,6 +242,8 @@ data class ProgressActivityState(
|
||||
val lastNotificationTime: Long = System.currentTimeMillis(),
|
||||
/** Whether the progress screen is currently receiving job updates. */
|
||||
val isForeground: Boolean = true,
|
||||
val isBackup: Boolean = false,
|
||||
val sha256: String? = null,
|
||||
) : IThemeState {
|
||||
companion object {
|
||||
val Empty: ProgressActivityState
|
||||
@@ -302,6 +311,7 @@ class ProgressActivityViewModel : ViewModel(), SettingChangeListener, IThemeView
|
||||
destDevice = status.destDevice,
|
||||
exception = null,
|
||||
lastNotificationTime = System.currentTimeMillis(),
|
||||
isBackup = status.isBackup,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -316,6 +326,8 @@ class ProgressActivityViewModel : ViewModel(), SettingChangeListener, IThemeView
|
||||
totalBytes = status.totalBytes,
|
||||
percent = 100,
|
||||
lastNotificationTime = System.currentTimeMillis(),
|
||||
isBackup = status.isBackup,
|
||||
sha256 = status.sha256,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -332,6 +344,7 @@ class ProgressActivityViewModel : ViewModel(), SettingChangeListener, IThemeView
|
||||
totalBytes = status.totalBytes,
|
||||
percent = status.percent,
|
||||
lastNotificationTime = System.currentTimeMillis(),
|
||||
isBackup = status.isBackup,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package eu.depau.etchdroid
|
||||
|
||||
import androidx.core.net.toUri
|
||||
import eu.depau.etchdroid.massstorage.UsbMassStorageDeviceDescriptor
|
||||
import eu.depau.etchdroid.ui.ConfirmOperationActivityViewModel
|
||||
import eu.depau.etchdroid.ui.JobState
|
||||
import eu.depau.etchdroid.ui.ProgressActivityViewModel
|
||||
import eu.depau.etchdroid.utils.ktexts.safeParcelableExtra
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.BeforeAll
|
||||
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 IntentsTest {
|
||||
private val uri = "content://com.android.externalstorage.documents/document/primary%3ADownload%2Fb.img".toUri()
|
||||
private val device = UsbMassStorageDeviceDescriptor()
|
||||
|
||||
@Test
|
||||
fun backupIntentCarriesActionDataAndDevice() {
|
||||
val intent = getStartBackupJobIntent(uri, device, jobId = 42)
|
||||
assertEquals(Intents.START_BACKUP, intent.action)
|
||||
assertEquals(uri, intent.data)
|
||||
assertEquals(42, intent.getIntExtra("jobId", -1))
|
||||
assertEquals(device, intent.safeParcelableExtra<UsbMassStorageDeviceDescriptor>("destDevice"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun finishedBackupStatusCarriesHashAndFlag() {
|
||||
val intent = getFinishedIntent(uri, device, 100, isBackup = true, sha256 = "abc")
|
||||
val status = intent.safeParcelableExtra<JobStatusInfo>("status")!!
|
||||
assertEquals(Intents.FINISHED, intent.action)
|
||||
assertTrue(status.isBackup)
|
||||
assertEquals("abc", status.sha256)
|
||||
assertEquals(100, status.percent)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun progressUpdateMarksBackup() {
|
||||
val intent = getProgressUpdateIntent(uri, device, 1, 12f, 50, 100, isBackup = true)
|
||||
assertTrue(intent.safeParcelableExtra<JobStatusInfo>("status")!!.isBackup)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun confirmIntentCarriesOperation() {
|
||||
val intent = getConfirmOperationActivityIntent(uri, device, operation = JobOperation.BACKUP)
|
||||
assertEquals("BACKUP", intent.getStringExtra("operation"))
|
||||
val write = getConfirmOperationActivityIntent(uri, device)
|
||||
assertEquals("WRITE", write.getStringExtra("operation"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun progressStateTracksBackupFields() {
|
||||
val vm = ProgressActivityViewModel()
|
||||
vm.updateFromIntent(getProgressUpdateIntent(uri, device, 1, 1f, 50, 100, isBackup = true))
|
||||
assertTrue(vm.state.value.isBackup)
|
||||
vm.updateFromIntent(getFinishedIntent(uri, device, 100, isBackup = true, sha256 = "abc"))
|
||||
assertEquals(JobState.SUCCESS, vm.state.value.jobState)
|
||||
assertEquals("abc", vm.state.value.sha256)
|
||||
assertTrue(vm.state.value.isBackup)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun confirmViewModelStoresOperation() {
|
||||
val vm = ConfirmOperationActivityViewModel()
|
||||
vm.init(uri, device, JobOperation.BACKUP)
|
||||
assertEquals(JobOperation.BACKUP, vm.state.value.operation)
|
||||
assertEquals(uri, vm.state.value.openedImage)
|
||||
assertEquals(device, vm.state.value.selectedDevice)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
@BeforeAll
|
||||
fun setUp() {
|
||||
setUpMockTelemetry()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user