feat: handle insufficient storage errors in UI

This commit is contained in:
rushiiMachine 2025-06-26 23:55:46 -07:00
parent 495f7d0630
commit 3b23f0c69d
No known key found for this signature in database
GPG key ID: DCBE5952BB3B6420
6 changed files with 44 additions and 6 deletions

View file

@ -17,6 +17,7 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
@ -90,7 +91,11 @@ class InstallLogManager(
errorStacktrace = error?.let { Log.getStackTraceString(it).trimEnd() },
)
path.writeText(json.encodeToString(data))
try {
path.writeText(json.encodeToString(data))
} catch (e: IOException) {
Log.e(BuildConfig.TAG, "Failed to write log to disk", e)
}
}
/**

View file

@ -8,6 +8,7 @@ import androidx.annotation.StringRes
import androidx.core.content.getSystemService
import com.aliucord.manager.R
import com.aliucord.manager.manager.download.IDownloadManager.Result
import com.aliucord.manager.patcher.util.InsufficientStorageException
import com.aliucord.manager.util.IS_PROBABLY_EMULATOR
import io.ktor.client.HttpClient
import io.ktor.client.request.header
@ -59,8 +60,13 @@ class KtorDownloadManager(
tmpOut.outputStream().use { stream ->
// Preallocate space for this file
if (total > 0 && Build.VERSION.SDK_INT >= 26) {
application.getSystemService<StorageManager>()!!
.allocateBytes(stream.fd, total)
val storageManager = application.getSystemService<StorageManager>()!!
try {
storageManager.allocateBytes(stream.fd, total)
} catch (e: IOException) {
throw InsufficientStorageException(e.message)
}
}
while (!channel.isClosedForRead) {
@ -86,9 +92,12 @@ class KtorDownloadManager(
} catch (_: CancellationException) {
tmpOut.delete()
return Result.Cancelled(systemTriggered = false)
} catch (t: SocketTimeoutException) {
} catch (e: SocketTimeoutException) {
tmpOut.delete()
return Error(t, localizedError = R.string.downloader_err_timeout)
return Error(e, localizedError = R.string.downloader_err_timeout)
} catch (e: InsufficientStorageException) {
tmpOut.delete()
return Error(e, localizedError = R.string.downloader_err_storage_space)
} catch (t: Throwable) {
tmpOut.delete()
return Error(t)

View file

@ -9,9 +9,11 @@ import com.aliucord.manager.manager.PathManager
import com.aliucord.manager.patcher.StepRunner
import com.aliucord.manager.patcher.steps.StepGroup
import com.aliucord.manager.patcher.steps.base.Step
import com.aliucord.manager.patcher.util.InsufficientStorageException
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.io.File
import java.io.IOException
/**
* Step to duplicate the Discord APK to be worked on.
@ -47,7 +49,12 @@ class CopyDependenciesStep : Step(), KoinComponent {
// 2) Modifying the copied APK (whether this is necessary I'm not sure)
// 2) Extracting native libs and other various operations
val allocSize = (fileSize * 3.5).toLong()
storageManager.allocateBytes(targetFileStorageId, allocSize)
try {
storageManager.allocateBytes(targetFileStorageId, allocSize)
} catch (e: IOException) {
throw InsufficientStorageException(e.message)
}
}
container.log("Copying patched apk from ${srcApk.absolutePath} to ${patchedApk.absolutePath}")

View file

@ -0,0 +1,11 @@
package com.aliucord.manager.patcher.util
/**
* Used to indicate that pre-allocating storage space via [android.os.storage.StorageManager.allocateBytes]
* failed due to insufficient storage space, or cache space able to be cleared.
*/
class InsufficientStorageException(
message: String?,
) : Exception() {
override val message = "Failed to preallocate sufficient storage space: $message"
}

View file

@ -15,6 +15,7 @@ import com.aliucord.manager.patcher.StepRunner
import com.aliucord.manager.patcher.steps.StepGroup
import com.aliucord.manager.patcher.steps.base.*
import com.aliucord.manager.patcher.steps.install.InstallStep
import com.aliucord.manager.patcher.util.InsufficientStorageException
import com.aliucord.manager.ui.screens.patchopts.PatchOptions
import com.aliucord.manager.ui.util.toUnsafeImmutable
import com.aliucord.manager.util.launchBlock
@ -161,6 +162,10 @@ class PatchingScreenModel(
Log.e(BuildConfig.TAG, "Failed to perform installation process", error)
mutableState.value = PatchingScreenState.Failed(installId = installId!!)
if (error is InsufficientStorageException) {
application.showToast(R.string.installer_insufficient_storage)
}
error
}
}

View file

@ -100,6 +100,7 @@
<string name="installer_uninstall_success">Successfully uninstalled Aliucord</string>
<string name="installer_uninstall_aborted">Cancelled uninstallation</string>
<string name="installer_dl_verify_fail">Failed to verify download</string>
<string name="installer_insufficient_storage">Insufficient storage space!</string>
<string name="installer_banner_minimization">Since battery optimizations have not been disabled, minimizing this screen may abort the installation process!</string>
<string name="installer_banner_failure">Installation failed! You can either retry or click this banner to open the Aliucord server for help.</string>
<string name="installer_banner_success">Successfully installed Aliucord! Do *NOT* uninstall the manager (this app) as it is required to perform certain types of updates.</string>