feat(service): run backup jobs in the worker service
This commit is contained in:
1 parent
e7bc8479b5
commit
ef5d5c028c
3 files changed
+356
-99
No files matched your search
@@ -8,13 +8,16 @@ import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.ParcelFileDescriptor
|
||||
import android.os.PowerManager
|
||||
import android.provider.DocumentsContract
|
||||
import android.util.Log
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.lifecycle.LifecycleService
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import eu.depau.etchdroid.Intents
|
||||
import eu.depau.etchdroid.JobOperation
|
||||
import eu.depau.etchdroid.JobStatusInfo
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.getErrorIntent
|
||||
@@ -32,10 +35,12 @@ import eu.depau.etchdroid.service.WorkerServiceFlowImpl.writeImage
|
||||
import eu.depau.etchdroid.ui.ProgressActivity
|
||||
import eu.depau.etchdroid.utils.TimeoutExpiredException
|
||||
import eu.depau.etchdroid.utils.broadcastReceiver
|
||||
import eu.depau.etchdroid.utils.exception.CouldNotWriteBackupException
|
||||
import eu.depau.etchdroid.utils.exception.InitException
|
||||
import eu.depau.etchdroid.utils.exception.MissingDeviceException
|
||||
import eu.depau.etchdroid.utils.exception.NoMediumException
|
||||
import eu.depau.etchdroid.utils.exception.NotEnoughSpaceException
|
||||
import eu.depau.etchdroid.utils.exception.NotEnoughSpaceForBackupException
|
||||
import eu.depau.etchdroid.utils.exception.OpenFileException
|
||||
import eu.depau.etchdroid.utils.exception.UnknownException
|
||||
import eu.depau.etchdroid.utils.exception.UsbCommunicationException
|
||||
@@ -44,6 +49,7 @@ import eu.depau.etchdroid.utils.exception.UsbDriveTooLargeException
|
||||
import eu.depau.etchdroid.utils.exception.VerificationFailedException
|
||||
import eu.depau.etchdroid.utils.exception.base.EtchDroidException
|
||||
import eu.depau.etchdroid.utils.exception.base.FatalException
|
||||
import eu.depau.etchdroid.utils.isIncompleteBackupName
|
||||
import eu.depau.etchdroid.utils.ktexts.broadcastLocally
|
||||
import eu.depau.etchdroid.utils.ktexts.broadcastLocallySync
|
||||
import eu.depau.etchdroid.utils.ktexts.getDisplayName
|
||||
@@ -55,6 +61,7 @@ import eu.depau.etchdroid.utils.ktexts.threadIdCompat
|
||||
import eu.depau.etchdroid.utils.ktexts.toHRSize
|
||||
import eu.depau.etchdroid.utils.lateInit
|
||||
import eu.depau.etchdroid.utils.timeoutWatchdog
|
||||
import eu.depau.etchdroid.utils.withoutPartSuffix
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -69,6 +76,8 @@ import me.jahnen.libaums.core.driver.scsi.commands.sense.SenseException
|
||||
import me.jahnen.libaums.core.usb.PipeException
|
||||
import me.jahnen.libaums.libusbcommunication.ErrNoIOException
|
||||
import java.io.BufferedInputStream
|
||||
import java.io.BufferedOutputStream
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.InputStream
|
||||
import java.util.Random
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
@@ -90,6 +99,7 @@ class WorkerService : LifecycleService() {
|
||||
private var mLoggedNotificationWarning = false
|
||||
private var mNotificationsSetUp = false
|
||||
private val mProgressNotificationId = Random().nextInt()
|
||||
private var mOperation = JobOperation.WRITE
|
||||
private lateinit var mSourceUri: Uri
|
||||
private lateinit var mDestDevice: UsbMassStorageDeviceDescriptor
|
||||
private var mJobId by lateInit<Int>(mutable = true)
|
||||
@@ -164,21 +174,33 @@ class WorkerService : LifecycleService() {
|
||||
.setSmallIcon(R.drawable.ic_write_to_usb_anim)
|
||||
.setContentTitle(
|
||||
getString(
|
||||
if (status.isVerifying) R.string.notification_verify_progress_title
|
||||
else R.string.notification_write_progress_title
|
||||
when {
|
||||
status.isBackup -> R.string.notification_backup_progress_title
|
||||
status.isVerifying -> R.string.notification_verify_progress_title
|
||||
else -> R.string.notification_write_progress_title
|
||||
}
|
||||
)
|
||||
)
|
||||
.setContentText(
|
||||
if (status.isVerifying) getString(
|
||||
R.string.notification_verify_progress_content,
|
||||
mDestDevice.name,
|
||||
filenameStr
|
||||
)
|
||||
else getString(
|
||||
R.string.notification_write_progress_content,
|
||||
filenameStr,
|
||||
mDestDevice.name
|
||||
)
|
||||
when {
|
||||
status.isBackup -> getString(
|
||||
R.string.notification_backup_progress_content,
|
||||
mDestDevice.name,
|
||||
filenameStr
|
||||
)
|
||||
|
||||
status.isVerifying -> getString(
|
||||
R.string.notification_verify_progress_content,
|
||||
mDestDevice.name,
|
||||
filenameStr
|
||||
)
|
||||
|
||||
else -> getString(
|
||||
R.string.notification_write_progress_content,
|
||||
filenameStr,
|
||||
mDestDevice.name
|
||||
)
|
||||
}
|
||||
)
|
||||
.setContentIntent(
|
||||
getProgressUpdateIntent(
|
||||
@@ -190,7 +212,8 @@ class WorkerService : LifecycleService() {
|
||||
status.totalBytes,
|
||||
isVerifying = status.isVerifying,
|
||||
packageContext = this@WorkerService,
|
||||
cls = ProgressActivity::class.java
|
||||
cls = ProgressActivity::class.java,
|
||||
isBackup = status.isBackup
|
||||
).getProgressActivityPendingIntent(this@WorkerService)
|
||||
)
|
||||
.setSubText(
|
||||
@@ -215,8 +238,13 @@ class WorkerService : LifecycleService() {
|
||||
.setSmallIcon(R.drawable.ic_write_to_usb_failed)
|
||||
.setContentTitle(
|
||||
getString(
|
||||
if (status.exception is FatalException) R.string.write_failed
|
||||
else R.string.action_required
|
||||
when {
|
||||
status.exception is FatalException && status.isBackup ->
|
||||
R.string.backup_failed
|
||||
|
||||
status.exception is FatalException -> R.string.write_failed
|
||||
else -> R.string.action_required
|
||||
}
|
||||
)
|
||||
)
|
||||
.setContentText(status.exception.getUiMessage(this@WorkerService))
|
||||
@@ -229,7 +257,8 @@ class WorkerService : LifecycleService() {
|
||||
status.totalBytes,
|
||||
status.exception,
|
||||
packageContext = this@WorkerService,
|
||||
cls = ProgressActivity::class.java
|
||||
cls = ProgressActivity::class.java,
|
||||
isBackup = status.isBackup
|
||||
).getProgressActivityPendingIntent(this@WorkerService)
|
||||
)
|
||||
.setOngoing(false)
|
||||
@@ -246,9 +275,19 @@ class WorkerService : LifecycleService() {
|
||||
mJobId, NotificationCompat
|
||||
.Builder(this@WorkerService, JOB_RESULT_CHANNEL)
|
||||
.setSmallIcon(R.drawable.ic_written_to_usb)
|
||||
.setContentTitle(getString(R.string.write_finished))
|
||||
.setContentText(
|
||||
.setContentTitle(
|
||||
getString(
|
||||
if (status.isBackup) R.string.backup_finished
|
||||
else R.string.write_finished
|
||||
)
|
||||
)
|
||||
.setContentText(
|
||||
if (status.isBackup) getString(
|
||||
R.string.backup_success_notif_content_text,
|
||||
mDestDevice.name,
|
||||
filenameStr
|
||||
)
|
||||
else getString(
|
||||
R.string.success_notif_content_text, filenameStr, mDestDevice.name
|
||||
)
|
||||
)
|
||||
@@ -258,7 +297,9 @@ class WorkerService : LifecycleService() {
|
||||
mDestDevice,
|
||||
status.totalBytes,
|
||||
packageContext = this@WorkerService,
|
||||
cls = ProgressActivity::class.java
|
||||
cls = ProgressActivity::class.java,
|
||||
isBackup = status.isBackup,
|
||||
sha256 = status.sha256
|
||||
).getProgressActivityPendingIntent(this@WorkerService)
|
||||
)
|
||||
.setOngoing(false)
|
||||
@@ -294,7 +335,7 @@ class WorkerService : LifecycleService() {
|
||||
val verifyOnly: Boolean
|
||||
|
||||
try {
|
||||
if (intent?.action != Intents.START_JOB) {
|
||||
if (intent?.action != Intents.START_JOB && intent?.action != Intents.START_BACKUP) {
|
||||
Telemetry.captureMessage("Received invalid intent action: ${intent?.action}")
|
||||
stopSelf()
|
||||
return START_NOT_STICKY
|
||||
@@ -304,21 +345,30 @@ class WorkerService : LifecycleService() {
|
||||
// interface claim from each other, killing each other's in-flight transfers.
|
||||
// Also guards mJobId/mSourceUri/mDestDevice from being overwritten mid-job.
|
||||
if (!mJobActive.compareAndSet(false, true)) {
|
||||
Telemetry.captureMessage("Ignoring START_JOB: a job is already running")
|
||||
Telemetry.captureMessage("Ignoring ${intent.action}: a job is already running")
|
||||
return START_NOT_STICKY
|
||||
}
|
||||
|
||||
mOperation =
|
||||
if (intent.action == Intents.START_BACKUP) JobOperation.BACKUP
|
||||
else JobOperation.WRITE
|
||||
|
||||
mSourceUri = intent.data!!
|
||||
mDestDevice = intent.safeParcelableExtra<UsbMassStorageDeviceDescriptor>("destDevice")
|
||||
?: throw MissingDeviceException()
|
||||
mJobId = intent.getIntExtra("jobId", -1)
|
||||
offset = intent.getLongExtra("offset", 0L)
|
||||
verifyOnly = intent.getBooleanExtra("verifyOnly", false)
|
||||
// Backups always append to whatever the destination already holds.
|
||||
offset =
|
||||
if (mOperation == JobOperation.BACKUP) 0L
|
||||
else intent.getLongExtra("offset", 0L)
|
||||
verifyOnly =
|
||||
mOperation != JobOperation.BACKUP && intent.getBooleanExtra("verifyOnly", false)
|
||||
|
||||
val fileName = mSourceUri.getFileName(this@WorkerService) ?: "Unknown file"
|
||||
Telemetry.configureScope {
|
||||
setTag("intent", intent.toString())
|
||||
setTag("job.id", mJobId.toString())
|
||||
setTag("job.type", mOperation.name.lowercase())
|
||||
setTag("job.offset", offset.toString())
|
||||
setTag("job.verifyOnly", verifyOnly.toString())
|
||||
setTag("image.filename", fileName)
|
||||
@@ -355,7 +405,13 @@ class WorkerService : LifecycleService() {
|
||||
val downstreamException =
|
||||
exception as? EtchDroidException ?: UnknownException(exception)
|
||||
getErrorIntent(
|
||||
mSourceUri, mDestDevice, mJobId, 0, 0, exception = downstreamException
|
||||
mSourceUri,
|
||||
mDestDevice,
|
||||
mJobId,
|
||||
0,
|
||||
0,
|
||||
exception = downstreamException,
|
||||
isBackup = mOperation == JobOperation.BACKUP
|
||||
).broadcastLocallySync(this@WorkerService)
|
||||
}
|
||||
mJobActive.set(false)
|
||||
@@ -421,85 +477,134 @@ class WorkerService : LifecycleService() {
|
||||
throw UsbDriveTooLargeException()
|
||||
}
|
||||
|
||||
imageSize = mSourceUri.getFileSize(this@WorkerService)
|
||||
if (mOperation == JobOperation.BACKUP) {
|
||||
// The destination already holds the bytes written by an interrupted run:
|
||||
// the flow resumes from its size and appends, so the total is the device.
|
||||
imageSize = devSize
|
||||
|
||||
if (devSize < imageSize) {
|
||||
Telemetry.captureMessage(
|
||||
"Device size is smaller than image size: " +
|
||||
"device: ${devSize.toHRSize(false)}, " +
|
||||
"image: ${imageSize.toHRSize(false)}"
|
||||
)
|
||||
throw NotEnoughSpaceException(sourceSize = imageSize, destSize = devSize)
|
||||
}
|
||||
// Fail early when we can tell the destination cannot hold the rest. Both
|
||||
// values are best-effort: an unknown size must not abort a resumable job.
|
||||
val existingSize: Long? = try {
|
||||
mSourceUri.getFileSize(this@WorkerService)
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
val available = availableBytesFor(this@WorkerService, mSourceUri)
|
||||
if (existingSize != null && available != null &&
|
||||
available < devSize - existingSize
|
||||
) {
|
||||
throw NotEnoughSpaceForBackupException(devSize - existingSize, available)
|
||||
}
|
||||
|
||||
getProgressUpdateIntent(
|
||||
mSourceUri,
|
||||
mDestDevice,
|
||||
mJobId,
|
||||
0f,
|
||||
currentOffset,
|
||||
imageSize,
|
||||
isVerifying = verifyOnly
|
||||
).broadcastLocallySync(this@WorkerService)
|
||||
// The prefix-hash phase advances the bar to the resume offset, so start at 0
|
||||
// rather than at the resume offset: progress must never move backwards.
|
||||
getProgressUpdateIntent(
|
||||
mSourceUri,
|
||||
mDestDevice,
|
||||
mJobId,
|
||||
0f,
|
||||
0L,
|
||||
devSize,
|
||||
isBackup = true
|
||||
).broadcastLocallySync(this@WorkerService)
|
||||
|
||||
val bufferSize = BUFFER_BLOCKS * blockDev.blockSize
|
||||
Telemetry.info("Backing up USB drive", "worker")
|
||||
|
||||
// Write image
|
||||
if (!verifyOnly) {
|
||||
val sha256 = runBackup(blockDev, devSize, coroScope)
|
||||
|
||||
// Best-effort: the data is already complete, a rename failure is not fatal.
|
||||
renameToFinalName(mSourceUri)
|
||||
|
||||
getFinishedIntent(
|
||||
mSourceUri,
|
||||
mDestDevice,
|
||||
devSize,
|
||||
isBackup = true,
|
||||
sha256 = sha256
|
||||
).broadcastLocallySync(this@WorkerService)
|
||||
} else {
|
||||
imageSize = mSourceUri.getFileSize(this@WorkerService)
|
||||
|
||||
if (devSize < imageSize) {
|
||||
Telemetry.captureMessage(
|
||||
"Device size is smaller than image size: " +
|
||||
"device: ${devSize.toHRSize(false)}, " +
|
||||
"image: ${imageSize.toHRSize(false)}"
|
||||
)
|
||||
throw NotEnoughSpaceException(sourceSize = imageSize, destSize = devSize)
|
||||
}
|
||||
|
||||
getProgressUpdateIntent(
|
||||
mSourceUri,
|
||||
mDestDevice,
|
||||
mJobId,
|
||||
0f,
|
||||
currentOffset,
|
||||
imageSize,
|
||||
isVerifying = verifyOnly
|
||||
).broadcastLocallySync(this@WorkerService)
|
||||
|
||||
val bufferSize = BUFFER_BLOCKS * blockDev.blockSize
|
||||
|
||||
// Write image
|
||||
if (!verifyOnly) {
|
||||
try {
|
||||
rawSourceStream = contentResolver.openInputStream(mSourceUri)!!
|
||||
} catch (e: Exception) {
|
||||
Telemetry.warning("Failed to open image file: ${e.message}")
|
||||
throw OpenFileException("Failed to open image file", e)
|
||||
}
|
||||
|
||||
Telemetry.info("Writing image to USB drive", "worker")
|
||||
|
||||
writeImage(
|
||||
rawSourceStream,
|
||||
blockDev,
|
||||
imageSize,
|
||||
bufferSize,
|
||||
currentOffset,
|
||||
{ currentOffset = it },
|
||||
coroScope,
|
||||
::ensureWakelock,
|
||||
::sendProgressUpdate
|
||||
)
|
||||
} else {
|
||||
Telemetry.info(
|
||||
"Skipping write, only verifying image on USB drive", "worker"
|
||||
)
|
||||
}
|
||||
|
||||
// Verify written image
|
||||
try {
|
||||
rawSourceStream = contentResolver.openInputStream(mSourceUri)!!
|
||||
} catch (e: Exception) {
|
||||
Telemetry.warning("Failed to open image file: ${e.message}")
|
||||
Log.e(TAG, "Failed to open image file", e)
|
||||
throw OpenFileException("Failed to open image file", e)
|
||||
}
|
||||
|
||||
Telemetry.info("Writing image to USB drive", "worker")
|
||||
// Always verify the whole image, not just the part that was written
|
||||
currentOffset = 0
|
||||
mLast10Speeds.clear()
|
||||
|
||||
writeImage(
|
||||
Telemetry.info("Verifying image on USB drive", "worker")
|
||||
|
||||
verifyImage(
|
||||
rawSourceStream,
|
||||
blockDev,
|
||||
imageSize,
|
||||
bufferSize,
|
||||
currentOffset,
|
||||
{ currentOffset = it },
|
||||
coroScope,
|
||||
::ensureWakelock,
|
||||
::sendProgressUpdate
|
||||
::sendProgressUpdate,
|
||||
{ mVerificationCancelled },
|
||||
::ensureWakelock
|
||||
)
|
||||
|
||||
getFinishedIntent(mSourceUri, mDestDevice, imageSize).broadcastLocallySync(
|
||||
this@WorkerService
|
||||
)
|
||||
} else {
|
||||
Telemetry.info("Skipping write, only verifying image on USB drive", "worker")
|
||||
}
|
||||
|
||||
// Verify written image
|
||||
try {
|
||||
rawSourceStream = contentResolver.openInputStream(mSourceUri)!!
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to open image file", e)
|
||||
throw OpenFileException("Failed to open image file", e)
|
||||
}
|
||||
|
||||
// Always verify the whole image, not just the part that was written
|
||||
currentOffset = 0
|
||||
mLast10Speeds.clear()
|
||||
|
||||
Telemetry.info("Verifying image on USB drive", "worker")
|
||||
|
||||
verifyImage(
|
||||
rawSourceStream,
|
||||
blockDev,
|
||||
imageSize,
|
||||
bufferSize,
|
||||
{ currentOffset = it },
|
||||
coroScope,
|
||||
::sendProgressUpdate,
|
||||
{ mVerificationCancelled },
|
||||
::ensureWakelock
|
||||
)
|
||||
|
||||
getFinishedIntent(mSourceUri, mDestDevice, imageSize).broadcastLocallySync(
|
||||
this@WorkerService
|
||||
)
|
||||
|
||||
} catch (exception: Exception) {
|
||||
// A cancelled job (service destroyed) is not a failure: rethrow instead of
|
||||
// reporting it or broadcasting an error.
|
||||
@@ -546,7 +651,8 @@ class WorkerService : LifecycleService() {
|
||||
mJobId,
|
||||
currentOffset,
|
||||
imageSize,
|
||||
exception = it
|
||||
exception = it,
|
||||
isBackup = mOperation == JobOperation.BACKUP
|
||||
).broadcastLocallySync(this@WorkerService)
|
||||
}
|
||||
stopSelf()
|
||||
@@ -588,33 +694,145 @@ class WorkerService : LifecycleService() {
|
||||
newSpeed,
|
||||
processedBytes,
|
||||
imageSize,
|
||||
isVerifying = isVerifying
|
||||
isVerifying = isVerifying,
|
||||
isBackup = mOperation == JobOperation.BACKUP
|
||||
).broadcastLocally(this@WorkerService)
|
||||
|
||||
mLastProgressUpdate = newTime
|
||||
mBytesSinceLastUpdate = 0.0
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the device to the destination document, resuming from whatever it already holds,
|
||||
* and returns the SHA-256 of the resulting file. The document is opened in append mode and
|
||||
* never truncated: an interrupted backup keeps its prefix for the next attempt.
|
||||
*/
|
||||
private suspend fun runBackup(
|
||||
blockDev: BlockDeviceDriver,
|
||||
devSize: Long,
|
||||
coroScope: CoroutineScope,
|
||||
): String {
|
||||
val pfd = openBackupFile()
|
||||
var existing: InputStream? = null
|
||||
var out: BufferedOutputStream? = null
|
||||
|
||||
try {
|
||||
val resumeOffset = pfd.statSize
|
||||
if (resumeOffset < 0) throw CouldNotWriteBackupException(
|
||||
"Could not determine the size of the backup file", null
|
||||
)
|
||||
|
||||
if (resumeOffset > 0) {
|
||||
existing = try {
|
||||
contentResolver.openInputStream(mSourceUri)
|
||||
} catch (e: SecurityException) {
|
||||
throw CouldNotWriteBackupException("Failed to reopen the backup file", e)
|
||||
} catch (e: FileNotFoundException) {
|
||||
throw CouldNotWriteBackupException("Failed to reopen the backup file", e)
|
||||
} ?: throw CouldNotWriteBackupException("Failed to reopen the backup file", null)
|
||||
}
|
||||
|
||||
// Closing this also closes the descriptor: it owns pfd from here on.
|
||||
out = BufferedOutputStream(
|
||||
ParcelFileDescriptor.AutoCloseOutputStream(pfd), 1 shl 20
|
||||
)
|
||||
|
||||
val sha256 = BackupFlow.backupImage(
|
||||
blockDev,
|
||||
out,
|
||||
existing,
|
||||
resumeOffset,
|
||||
devSize,
|
||||
BUFFER_BLOCKS * blockDev.blockSize,
|
||||
{},
|
||||
coroScope,
|
||||
::ensureWakelock,
|
||||
::sendProgressUpdate,
|
||||
)
|
||||
|
||||
// Make the whole file durable before renaming it to its final name.
|
||||
try {
|
||||
out.flush()
|
||||
pfd.fileDescriptor.sync()
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
throw CouldNotWriteBackupException("Failed to sync the backup file", e)
|
||||
}
|
||||
|
||||
return sha256
|
||||
} finally {
|
||||
val stream = out
|
||||
try {
|
||||
if (stream != null) stream.close() else pfd.close()
|
||||
} catch (e: Exception) {
|
||||
Telemetry.warning("Failed to close the backup file: ${e.message}")
|
||||
}
|
||||
try {
|
||||
existing?.close()
|
||||
} catch (e: Exception) {
|
||||
Telemetry.warning("Failed to close the existing backup data: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun openBackupFile(): ParcelFileDescriptor = try {
|
||||
contentResolver.openFileDescriptor(mSourceUri, "wa")
|
||||
} catch (e: SecurityException) {
|
||||
throw CouldNotWriteBackupException("Failed to open the backup file", e)
|
||||
} catch (e: FileNotFoundException) {
|
||||
throw CouldNotWriteBackupException("Failed to open the backup file", e)
|
||||
} ?: throw CouldNotWriteBackupException("Failed to open the backup file", null)
|
||||
|
||||
/**
|
||||
* Drops the `.part` suffix from a finished backup. Best-effort: the file already holds the
|
||||
* complete data, so neither a provider that does not support renaming nor a failure may turn
|
||||
* a successful backup into a failed one.
|
||||
*/
|
||||
private fun renameToFinalName(uri: Uri) {
|
||||
try {
|
||||
val name = uri.getDisplayName(this) ?: return
|
||||
if (!name.isIncompleteBackupName()) return
|
||||
DocumentsContract.renameDocument(contentResolver, uri, name.withoutPartSuffix())
|
||||
} catch (e: Exception) {
|
||||
Telemetry.warning("Failed to rename the backup file: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private val filenameStr: String by lazy {
|
||||
mSourceUri.getDisplayName(this) ?: getString(R.string.unknown_filename)
|
||||
}
|
||||
|
||||
private val basicForegroundNotification: Notification
|
||||
get() = NotificationCompat
|
||||
.Builder(this, JOB_PROGRESS_CHANNEL)
|
||||
.setSmallIcon(R.drawable.ic_write_to_usb_anim)
|
||||
.setContentTitle(getString(R.string.notification_write_progress_title))
|
||||
.setContentText(
|
||||
getString(
|
||||
if (notificationsAllowed) R.string.notification_write_tap_for_progress_text
|
||||
else R.string.notification_write_no_notifications_text,
|
||||
filenameStr,
|
||||
mDestDevice.name
|
||||
get() {
|
||||
val isBackup = mOperation == JobOperation.BACKUP
|
||||
return NotificationCompat
|
||||
.Builder(this, JOB_PROGRESS_CHANNEL)
|
||||
.setSmallIcon(R.drawable.ic_write_to_usb_anim)
|
||||
.setContentTitle(
|
||||
getString(
|
||||
if (isBackup) R.string.notification_backup_progress_title
|
||||
else R.string.notification_write_progress_title
|
||||
)
|
||||
)
|
||||
)
|
||||
.setProgress(100, 0, true)
|
||||
.setOngoing(true)
|
||||
.build()
|
||||
.setContentText(
|
||||
if (isBackup) getString(
|
||||
if (notificationsAllowed) R.string.notification_backup_tap_for_progress_text
|
||||
else R.string.notification_backup_no_notifications_text,
|
||||
mDestDevice.name,
|
||||
filenameStr
|
||||
)
|
||||
else getString(
|
||||
if (notificationsAllowed) R.string.notification_write_tap_for_progress_text
|
||||
else R.string.notification_write_no_notifications_text,
|
||||
filenameStr,
|
||||
mDestDevice.name
|
||||
)
|
||||
)
|
||||
.setProgress(100, 0, true)
|
||||
.setOngoing(true)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun trySetupNotifications() {
|
||||
if (mNotificationsSetUp || Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
|
||||
|
||||
@@ -16,6 +16,13 @@
|
||||
<string name="notification_write_tap_for_progress_text" comment=" Notification content for long-running job status, used when the user has granted the notification permission but no progress info is available. The first parameter will be replaced with the source file name, the second one with the USB stick name">Tap for progress • Writing %1$s to %2$s</string>
|
||||
<string name="notification_write_progress_content" comment=" Notification content: "{file name} to {device name}", used when the user has granted the notification permission; The notification title will be: "Writing image…" and this string will show up right below it.">%1$s to %2$s</string>
|
||||
<string name="notification_verify_progress_content" comment=" Notification content: "{device name} against {file name}", used when the user has granted the notification permission; The notification title will be: "Verifying image…" and this string will show up right below it.">%1$s against %2$s</string>
|
||||
<string name="notification_backup_progress_title" comment="Notification title used for long-running backup job status">Backing up…</string>
|
||||
<string name="notification_backup_progress_content" comment="Notification content: "{device name} to {file name}", used when the user has granted the notification permission; The notification title will be: "Backing up…" and this string will show up right below it.">%1$s to %2$s</string>
|
||||
<string name="notification_backup_no_notifications_text" comment="Notification content for long-running backup job status, used when the user has not granted the notification permission. The first parameter will be replaced with the USB drive name, the second one with the destination file name">Enable notifications for progress • Backing up %1$s to %2$s</string>
|
||||
<string name="notification_backup_tap_for_progress_text" comment="Notification content for long-running backup job status, used when the user has granted the notification permission but no progress info is available. The first parameter will be replaced with the USB drive name, the second one with the destination file name">Tap for progress • Backing up %1$s to %2$s</string>
|
||||
<string name="backup_finished" comment="Notification title when the backup operation is successful">Backup finished</string>
|
||||
<string name="backup_success_notif_content_text" comment="Notification content when the backup operation is successful. Parameters: device name, destination file name">%1$s backed up to %2$s</string>
|
||||
<string name="backup_failed" comment="Notification title when a non-recoverable backup error occurs">Backup failed</string>
|
||||
<string name="unknown_filename" comment="Placeholder file name">Unknown file name</string>
|
||||
<string name="no_file_picker" comment="Toast shown when the device has no app to pick a file">No file picker app found on this device. Install a file manager and try again.</string>
|
||||
<string name="permission_denied_for_usb_device" comment="Displayed in a toast when the usb permission is denied">"Permission denied for USB device %1$s"</string>
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package eu.depau.etchdroid
|
||||
|
||||
import eu.depau.etchdroid.service.WorkerServiceFlowImpl
|
||||
import eu.depau.etchdroid.utils.exception.CouldNotWriteBackupException
|
||||
import eu.depau.etchdroid.utils.exception.NotEnoughSpaceForBackupException
|
||||
import eu.depau.etchdroid.utils.exception.UsbCommunicationException
|
||||
import me.jahnen.libaums.core.usb.PipeException
|
||||
import org.junit.jupiter.api.Assertions.assertSame
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
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
|
||||
import java.io.IOException
|
||||
|
||||
@ExtendWith(RobolectricExtension::class)
|
||||
@Config(application = EtchDroidApplication::class)
|
||||
class BackupErrorMappingTest {
|
||||
@Test
|
||||
fun backupExceptionsPassThroughUnwrapped() {
|
||||
val write = CouldNotWriteBackupException("boom", IOException("ENOSPC"))
|
||||
assertSame(write, WorkerServiceFlowImpl.toJobException(write))
|
||||
val space = NotEnoughSpaceForBackupException(10, 5)
|
||||
assertSame(space, WorkerServiceFlowImpl.toJobException(space))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deviceErrorsStillMapToUsbCommunication() {
|
||||
val mapped = WorkerServiceFlowImpl.toJobException(PipeException())
|
||||
assertTrue(mapped is UsbCommunicationException)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user