Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,17 @@ class PermissionsFragment :
}

private fun startIdeSetup() {
val shouldProceed = viewModel.checkStorageAndNotify(requireContext())
if (!shouldProceed) {
return
}
viewLifecycleScope.launch {
val shouldProceed = viewModel.checkStorageAndNotify(requireContext())
if (!shouldProceed) {
return@launch
}

if (viewModel.isSetupComplete()) {
(activity as? OnboardingActivity)?.tryNavigateToMainIfSetupIsCompleted()
return
}
if (viewModel.isSetupCompleteAsync()) {
(activity as? OnboardingActivity)?.tryNavigateToMainIfSetupIsCompleted()
Copy link
Copy Markdown
Contributor

@itsaky-adfa itsaky-adfa Apr 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be a bit cautious here and wrap the startActivity part in tryNavigatetoToMainIfSetupIsCompleted in a withContext(Dispatchers.Main.immediate) { ... } here.

But as CodeRabbit suggested, tryNavigatetoToMainIfSetupIsCompleted may still perform disk IO when it calls isSetupComplete.

A better alternative would be to just do this :

if (isSetupCompleteAsync()) {
  (activity as? OnboardingActivity)?.apply {
    startActivity(Intent(this, MainActivity::class.java))
    finish()
  }
}

Or maybe OnboardingActivity can have a helper function to just do the navigation which can then be shared at both call sites.

return@launch
}

viewLifecycleScope.launch {
doAsyncWithProgress(
Dispatchers.IO,
configureFlashbar = { builder, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.io.ByteArrayOutputStream
import java.io.File
import java.io.InputStream
import java.io.PrintWriter
import android.net.TrafficStats
import java.net.InetSocketAddress
import java.net.ServerSocket
import java.net.Socket
Expand Down Expand Up @@ -110,6 +111,7 @@ FROM LastChange
}

fun start() {
TrafficStats.setThreadStatsTag(0xC0DE)
try {
log.info(
"Starting WebServer on {}, port {}, debugEnabled={}, debugEnablePath='{}', debugDatabasePath='{}', experimentsEnabled={}, experimentsEnablePath='{}'.",
Expand Down Expand Up @@ -220,6 +222,7 @@ clientSocket and the catch block logic are updated accordingly.
if (::serverSocket.isInitialized) {
serverSocket.close()
}
TrafficStats.clearThreadStatsTag()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,71 +60,69 @@ class InstallationViewModel : ViewModel() {
return
}

if (!checkStorageAndNotify(context)) {
return
}
if (!checkToolsIsInstalled()) {
viewModelScope.launch {
try {
_state.update { Installing() }

withContext(Dispatchers.IO) {
val result =
withStopWatch("Assets installation") {
AssetsInstallationHelper.install(context) { progress ->
log.debug("Assets installation progress: {}", progress.message)
_installationProgress.value = progress.message
}
viewModelScope.launch {
if (!checkStorageAndNotify(context)) {
return@launch
}
if (withContext(Dispatchers.IO) { checkToolsIsInstalled() }) {
// Tools already installed
_state.update { InstallationComplete }
return@launch
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
try {
_state.update { Installing() }

withContext(Dispatchers.IO) {
val result =
withStopWatch("Assets installation") {
AssetsInstallationHelper.install(context) { progress ->
log.debug("Assets installation progress: {}", progress.message)
_installationProgress.value = progress.message
}
}

log.info("Assets installation result: {}", result)
log.info("Assets installation result: {}", result)

when (result) {
is AssetsInstallationHelper.Result.Success -> {
val distributionProvider = IJdkDistributionProvider.getInstance()
distributionProvider.loadDistributions()
when (result) {
is AssetsInstallationHelper.Result.Success -> {
val distributionProvider = IJdkDistributionProvider.getInstance()
distributionProvider.loadDistributions()

_state.update { InstallationComplete }
_state.update { InstallationComplete }
}
is AssetsInstallationHelper.Result.Failure -> {
if (result.shouldReportToSentry) {
result.cause?.let { Sentry.captureException(it) }
}
is AssetsInstallationHelper.Result.Failure -> {
if (result.shouldReportToSentry) {
result.cause?.let { Sentry.captureException(it) }
}
val errorMsg = result.errorMessage
?: context.getString(R.string.title_installation_failed)
viewModelScope.launch {
_events.emit(InstallationEvent.ShowError(errorMsg))
}
_state.update {
InstallationError(errorMsg)
}
val errorMsg = result.errorMessage
?: context.getString(R.string.title_installation_failed)
_events.emit(InstallationEvent.ShowError(errorMsg))
_state.update {
InstallationError(errorMsg)
}
}
}
} catch (e: Exception) {
if (e is CancellationException) {
_state.update { InstallationPending }
throw e
}
Sentry.captureException(e)
log.error("IDE setup installation failed", e)
val errorMsg = e.message ?: context.getString(R.string.unknown_error)
viewModelScope.launch {
_events.emit(InstallationEvent.ShowError(errorMsg))
}
_state.update {
InstallationError(errorMsg)
}
}
} catch (e: Exception) {
if (e is CancellationException) {
_state.update { InstallationPending }
throw e
}
Sentry.captureException(e)
log.error("IDE setup installation failed", e)
val errorMsg = e.message ?: context.getString(R.string.unknown_error)
_events.emit(InstallationEvent.ShowError(errorMsg))
_state.update {
InstallationError(errorMsg)
}
}
} else {
// Tools already installed
_state.update { InstallationComplete }
}
}

fun isSetupComplete(): Boolean = checkToolsIsInstalled()

suspend fun isSetupCompleteAsync(): Boolean = withContext(Dispatchers.IO) { checkToolsIsInstalled() }

private fun checkToolsIsInstalled(): Boolean =
IJdkDistributionProvider.getInstance().installedDistributions.isNotEmpty() &&
Environment.ANDROID_HOME.exists()
Expand All @@ -147,7 +145,7 @@ class InstallationViewModel : ViewModel() {
return StorageInfo(isLowStorage, availableStorageInBytes, additionalBytesNeeded)
}

fun checkStorageAndNotify(context: Context): Boolean {
suspend fun checkStorageAndNotify(context: Context): Boolean = withContext(Dispatchers.IO) {
val storageInfo = getStorageInfo(context)

if (storageInfo.isLowStorage) {
Expand All @@ -160,13 +158,11 @@ class InstallationViewModel : ViewModel() {
availableGB
)

viewModelScope.launch {
_events.emit(InstallationEvent.ShowError(errorMessage))
}
return false
_events.emit(InstallationEvent.ShowError(errorMessage))
return@withContext false
}

return true
return@withContext true
}

}
Loading