diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 840d5d3621..f608bb4cb7 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -107,7 +107,7 @@ These structural facts shape every module. Day-to-day build *commands* live in ` - **ABI product flavors.** Every Android module *except* `:plugin-api` and `:subprojects:sqlite-js` gets two flavors on the `abi` dimension — `v7` (`armeabi-v7a`) and `v8` (`arm64-v8a`) — defined centrally. There is no flavorless variant; tasks are `assembleV8Debug`, `assembleV7Release`, etc. - **SDK levels** (`build-logic/.../build/config/BuildConfig.kt`): `COMPILE_SDK=36`, `MIN_SDK=28`, `TARGET_SDK=28`. **`TARGET_SDK` is deliberately pinned at 28:** higher targets enforce W^X (write-xor-execute), which blocks executing code from app-writable files. That is fatal for an on-device IDE that compiles and runs code (Gradle, `javac`, Termux binaries), so it is a hard requirement, not tech debt. `MIN_SDK_FOR_APPS_BUILT_WITH_COGO=16` is the floor for the apps a *user* builds with CoGo — distinct from CoGo's own `MIN_SDK`. - **Native asset bundling.** The on-device LLM (`llama-impl`) ships as a per-flavor native AAR, wired through the root `build.gradle.kts` (`bundleLlamaV8Assets` / `assembleV8Assets`, …); prebuilt per-flavor assets live under `assets/release/v7/` and `assets/release/v8/`. -- **Bundled add-ons** (ADFA-7238). A build is *fat* when `assets/plugins/*.cgp` and `assets/templates/*.cgt` exist in the repo root: release builds put them in the APK assets as `plugins/` and `templates/` (`StageBundledAddons` in `app/build.gradle.kts`), and `createAssetsZip()` adds the same entries to the debug `assets-.zip`. Both also write the bundle index `bundled-addons.tsv` (one line per add-on: entry name, size, SHA-256; empty in a thin build), so the app lists and fingerprints the bundle without opening an add-on, which a release APK stores compressed, and a rebuilt add-on of the same size changes the fingerprint. A bundle without a valid index, such as an older debug zip, is listed from its folders by name and size. A missing or empty folder is the thin build. First-run setup (`AssetsInstallationHelper`) lists the bundle (`assets/addons/BundledAddons`), installs each file in the same parallel phase through `BundledAddonInstaller` (scratch file, then rename; a plugin lands in `filesDir/plugins` as `-.cgp`, a template in `Environment.TEMPLATES_DIR`; a copy whose SHA-256 differs from the index is rejected and reported like a plugin with no readable manifest), and writes `filesDir/bundled_addons.properties` last (`BundledAddonsRecord`: bundle fingerprint, bundled plugin IDs and template files, plugin load failures not yet reported to the user). `InstallationViewModel` then reloads the plugins once, and `MainActivity` shows one error bar for the recorded failures. +- **Bundled add-ons** (ADFA-7238). A build is *fat* when `assets/plugins/*.cgp` and `assets/templates/*.cgt` exist in the repo root: release builds put them in the APK assets as `plugins/` and `templates/` (`StageBundledAddons` in `app/build.gradle.kts`), and `createAssetsZip()` adds the same entries to the debug `assets-.zip`. Both also write the bundle index `bundled-addons.tsv` (one line per add-on: entry name, size, SHA-256; empty in a thin build), so the app lists and fingerprints the bundle without opening an add-on, which a release APK stores compressed, and a rebuilt add-on of the same size changes the fingerprint. A bundle without a valid index, such as an older debug zip, is listed from its folders by name and size. A missing or empty folder is the thin build. First-run setup (`AssetsInstallationHelper`) lists the bundle (`assets/addons/BundledAddons`), installs each file in the same parallel phase through `BundledAddonInstaller` (scratch file, then rename; a plugin lands in `filesDir/plugins` as `-.cgp`, a template in `Environment.TEMPLATES_DIR`; a copy whose SHA-256 differs from the index is rejected and reported like a plugin with no readable manifest), and writes `filesDir/bundled_addons.properties` last (`BundledAddonsRecord`: bundle fingerprint, bundled plugin IDs and template files, plugin load failures not yet reported to the user). `InstallationViewModel` then reloads the plugins once, and `MainActivity` shows one error bar for the recorded failures. The Add-ons Manager reads the same record to warn (ADFA-7240) before it uninstalls or disables a bundled plugin, or uninstalls a bundled template; the record is not changed by either. - **sqlite-js for apps built with CoGo** (ADFA-7082). `subprojects:sqlite-js` builds the AAR that the JavaScript-in-SQLite project template depends on: the sqlite-js release vendored (see its README), for arm64-v8a and armeabi-v7a, minSdk 26, without core-library desugaring. Nothing in CoGo depends on it. Its default entry point runs no code stored in a database; loading stored functions is a separate entry point, only for databases the app made, since loading runs their code. `test-sqlite-js.yml` runs its host tests on pull requests. The manual `release-sqlite-js.yml` publishes it to the moving `sqlite-js-latest` release, from which dev-assets fetches it into the template, as it fetches `plugin-api-latest` for the plugin template. - **Native lib compression** (ADFA-2306, ADFA-4729). The app manifest hard-codes `android:extractNativeLibs="true"` (required: the installer must materialize libs in `nativeLibraryDir`, e.g. `libshizuku.so` is an executable the adb shell runs from there). That attribute overrides the `jniLibs.useLegacyPackaging` DSL, so AGP packages `lib//*.so` deflate-compressed in **every** APK — ~5.9 MB smaller (`libtree-sitter-kotlin.so` alone is 4.18 MB → 339 kB). The trap is the `recompressApk` post-step (release always, debug in CI only): its no-compress lists in `app/build.gradle.kts` must NOT contain `"so"`, or it silently re-stores the libs and undoes the saving — which is what ADFA-2306 fixed for release and ADFA-4729 for CI debug. Locally built debug APKs (including the e2e farm's) never run that step and were always fine. - **Quick Build shares the Gradle distribution's toolchain** (ADFA-4931). `:app`'s `quickBuildDaemonZip` leaves the Kotlin compiler and eight other jars out of the daemon zip, because the on-device Gradle distribution already carries them; `QuickBuildArtifactStager` links them in at provision time. That makes the daemon's Kotlin version the distribution's, so `:quickbuild:daemon` takes its Kotlin artifacts at `org.adfa.constants.GRADLE_DISTRIBUTION_KOTLIN_VERSION`, and `:app:quickBuildDistJarCheck` fails the build when the distribution it packages lacks a linked jar. See [ADR 0016](docs/adr/0016-quick-build-compiles-outside-gradle.md). diff --git a/app/src/main/java/com/itsaky/androidide/di/PluginModule.kt b/app/src/main/java/com/itsaky/androidide/di/PluginModule.kt index 760695b316..6252214662 100644 --- a/app/src/main/java/com/itsaky/androidide/di/PluginModule.kt +++ b/app/src/main/java/com/itsaky/androidide/di/PluginModule.kt @@ -1,6 +1,7 @@ package com.itsaky.androidide.di import com.itsaky.androidide.app.IDEApplication +import com.itsaky.androidide.assets.addons.BundledAddonsRecord import com.itsaky.androidide.preferences.internal.BuildPreferences import com.itsaky.androidide.repositories.DownloadFilesRepository import com.itsaky.androidide.repositories.DownloadFilesRepositoryImpl @@ -53,6 +54,13 @@ val pluginModule = filesDir = IDEApplication.cachedFilesDir, connectivityObserver = get(), isOfflineModeEnabled = { BuildPreferences.isOfflineEnabled }, + isBundledPlugin = { id -> + BundledAddonsRecord + .inFilesDir(IDEApplication.cachedFilesDir) + .read() + ?.pluginIds + ?.contains(id) == true + }, ) } diff --git a/app/src/main/java/com/itsaky/androidide/di/TemplateModule.kt b/app/src/main/java/com/itsaky/androidide/di/TemplateModule.kt index a4e92a0265..3eff23e732 100644 --- a/app/src/main/java/com/itsaky/androidide/di/TemplateModule.kt +++ b/app/src/main/java/com/itsaky/androidide/di/TemplateModule.kt @@ -1,5 +1,7 @@ package com.itsaky.androidide.di +import com.itsaky.androidide.app.IDEApplication +import com.itsaky.androidide.assets.addons.BundledAddonsRecord import com.itsaky.androidide.repositories.TemplateCollectionRepository import com.itsaky.androidide.repositories.TemplateRepository import com.itsaky.androidide.repositories.TemplateRepositoryImpl @@ -27,6 +29,13 @@ val templateModule = TemplateManagerViewModel( templateRepository = get(), collectionInstalls = get().installs, + isBundledTemplate = { fileName -> + BundledAddonsRecord + .inFilesDir(IDEApplication.cachedFilesDir) + .read() + ?.templateFiles + ?.contains(fileName) == true + }, ) } } diff --git a/app/src/main/java/com/itsaky/androidide/ui/compose/plugins/PluginManagerContent.kt b/app/src/main/java/com/itsaky/androidide/ui/compose/plugins/PluginManagerContent.kt index ca3fc657b4..ac225140cc 100644 --- a/app/src/main/java/com/itsaky/androidide/ui/compose/plugins/PluginManagerContent.kt +++ b/app/src/main/java/com/itsaky/androidide/ui/compose/plugins/PluginManagerContent.kt @@ -89,6 +89,13 @@ private sealed interface PluginManagerDialogState : Parcelable { @Parcelize data class UninstallConfirm( val pluginId: String, + val bundled: Boolean, + ) : PluginManagerDialogState + + @Parcelize + data class DisableConfirm( + val pluginId: String, + val pluginName: String, ) : PluginManagerDialogState @Parcelize @@ -167,7 +174,11 @@ fun PluginManagerContent( } is PluginManagerUiEffect.ShowUninstallConfirmation -> { - dialogState = PluginManagerDialogState.UninstallConfirm(effect.plugin.metadata.id) + dialogState = PluginManagerDialogState.UninstallConfirm(effect.plugin.metadata.id, effect.bundled) + } + + is PluginManagerUiEffect.ShowDisableConfirmation -> { + dialogState = PluginManagerDialogState.DisableConfirm(effect.pluginId, effect.pluginName) } is PluginManagerUiEffect.ShowRestartPrompt -> { @@ -254,6 +265,7 @@ fun PluginManagerContent( if (plugin != null) { UninstallConfirmationDialog( plugin = plugin, + bundled = dialog.bundled, onConfirm = { viewModel.confirmUninstallPlugin(plugin.metadata.id) dialogState = PluginManagerDialogState.None @@ -263,6 +275,18 @@ fun PluginManagerContent( } } + is PluginManagerDialogState.DisableConfirm -> { + // Carries its own name, so it shows even while the plugin list reloads. + DisableBundledPluginDialog( + pluginName = dialog.pluginName, + onConfirm = { + viewModel.confirmDisablePlugin(dialog.pluginId) + dialogState = PluginManagerDialogState.None + }, + onDismiss = { dialogState = PluginManagerDialogState.None }, + ) + } + is PluginManagerDialogState.Details -> { val plugin = uiState.plugins.firstOrNull { it.metadata.id == dialog.pluginId } if (plugin != null) { diff --git a/app/src/main/java/com/itsaky/androidide/ui/compose/plugins/PluginManagerDialogs.kt b/app/src/main/java/com/itsaky/androidide/ui/compose/plugins/PluginManagerDialogs.kt index 1e30b95006..076609af50 100644 --- a/app/src/main/java/com/itsaky/androidide/ui/compose/plugins/PluginManagerDialogs.kt +++ b/app/src/main/java/com/itsaky/androidide/ui/compose/plugins/PluginManagerDialogs.kt @@ -81,13 +81,53 @@ fun UninstallConfirmationDialog( plugin: PluginInfo, onConfirm: () -> Unit, onDismiss: () -> Unit, + bundled: Boolean = false, ) { AlertDialog( onDismissRequest = onDismiss, - title = { Text(stringResource(R.string.title_uninstall_plugin)) }, - text = { Text(stringResource(R.string.msg_uninstall_plugin_confirm, plugin.metadata.name)) }, + title = { + Text(stringResource(if (bundled) R.string.title_uninstall_bundled_plugin else R.string.title_uninstall_plugin)) + }, + // AlertDialog's text slot does not scroll; at 2x font scale the text would clip. + text = { + Text( + if (bundled) { + stringResource(R.string.msg_uninstall_bundled_plugin_confirm, plugin.metadata.name) + } else { + stringResource(R.string.msg_uninstall_plugin_confirm, plugin.metadata.name) + }, + modifier = Modifier.verticalScroll(rememberScrollState()), + ) + }, + confirmButton = { + TextButton(onClick = onConfirm) { + Text(stringResource(if (bundled) R.string.action_continue else R.string.uninstall_plugin)) + } + }, + dismissButton = { + TextButton(onClick = onDismiss) { Text(stringResource(android.R.string.cancel)) } + }, + ) +} + +/** Disabling a plugin that came with the app can break the installation (ADFA-7240). */ +@Composable +fun DisableBundledPluginDialog( + pluginName: String, + onConfirm: () -> Unit, + onDismiss: () -> Unit, +) { + AlertDialog( + onDismissRequest = onDismiss, + title = { Text(stringResource(R.string.title_disable_bundled_plugin)) }, + text = { + Text( + stringResource(R.string.msg_disable_bundled_plugin_confirm, pluginName), + modifier = Modifier.verticalScroll(rememberScrollState()), + ) + }, confirmButton = { - TextButton(onClick = onConfirm) { Text(stringResource(R.string.uninstall_plugin)) } + TextButton(onClick = onConfirm) { Text(stringResource(R.string.action_continue)) } }, dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(android.R.string.cancel)) } diff --git a/app/src/main/java/com/itsaky/androidide/ui/compose/templates/TemplateManagerDialogs.kt b/app/src/main/java/com/itsaky/androidide/ui/compose/templates/TemplateManagerDialogs.kt index 661bf7112e..e7c0890c32 100644 --- a/app/src/main/java/com/itsaky/androidide/ui/compose/templates/TemplateManagerDialogs.kt +++ b/app/src/main/java/com/itsaky/androidide/ui/compose/templates/TemplateManagerDialogs.kt @@ -81,19 +81,28 @@ fun UninstallTemplateConfirmationDialog( item: CgtFileItem, onConfirm: () -> Unit, onDismiss: () -> Unit, + bundled: Boolean = false, ) { AlertDialog( onDismissRequest = onDismiss, - title = { Text(stringResource(R.string.title_uninstall_template)) }, + title = { + Text(stringResource(if (bundled) R.string.title_uninstall_bundled_template else R.string.title_uninstall_template)) + }, // AlertDialog's text slot does not scroll; a long file name at 2x font scale would clip. text = { Text( - stringResource(R.string.msg_uninstall_template_confirm, item.displayName), + if (bundled) { + stringResource(R.string.msg_uninstall_bundled_template_confirm, item.displayName) + } else { + stringResource(R.string.msg_uninstall_template_confirm, item.displayName) + }, modifier = Modifier.verticalScroll(rememberScrollState()), ) }, confirmButton = { - TextButton(onClick = onConfirm) { Text(stringResource(R.string.action_uninstall_template)) } + TextButton(onClick = onConfirm) { + Text(stringResource(if (bundled) R.string.action_continue else R.string.action_uninstall_template)) + } }, dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(android.R.string.cancel)) } diff --git a/app/src/main/java/com/itsaky/androidide/ui/compose/templates/TemplateManagerScreen.kt b/app/src/main/java/com/itsaky/androidide/ui/compose/templates/TemplateManagerScreen.kt index 135314207b..62289ab3e9 100644 --- a/app/src/main/java/com/itsaky/androidide/ui/compose/templates/TemplateManagerScreen.kt +++ b/app/src/main/java/com/itsaky/androidide/ui/compose/templates/TemplateManagerScreen.kt @@ -70,6 +70,7 @@ private sealed interface TemplateManagerDialogState : Parcelable { @Parcelize data class UninstallConfirm( val path: String, + val bundled: Boolean, ) : TemplateManagerDialogState @Parcelize @@ -165,7 +166,7 @@ fun TemplateManagerScreen( } is TemplateManagerUiEffect.ShowUninstallConfirmation -> { - dialogState = TemplateManagerDialogState.UninstallConfirm(effect.item.file.absolutePath) + dialogState = TemplateManagerDialogState.UninstallConfirm(effect.item.file.absolutePath, effect.bundled) } is TemplateManagerUiEffect.ShowDeleteConfirmation -> { @@ -239,6 +240,7 @@ fun TemplateManagerScreen( if (item != null) { UninstallTemplateConfirmationDialog( item = item, + bundled = dialog.bundled, onConfirm = { viewModel.confirmUninstallTemplate(item) dialogState = TemplateManagerDialogState.None diff --git a/app/src/main/java/com/itsaky/androidide/ui/models/PluginManagerUiState.kt b/app/src/main/java/com/itsaky/androidide/ui/models/PluginManagerUiState.kt index 011e370949..16999020fd 100644 --- a/app/src/main/java/com/itsaky/androidide/ui/models/PluginManagerUiState.kt +++ b/app/src/main/java/com/itsaky/androidide/ui/models/PluginManagerUiState.kt @@ -125,8 +125,19 @@ sealed class PluginManagerUiEffect { val source: PluginInstallSource, ) : PluginManagerUiEffect() + /** [bundled]: the plugin came with the app (ADFA-7240), so the dialog warns about removing it. */ data class ShowUninstallConfirmation( val plugin: PluginInfo, + val bundled: Boolean, + ) : PluginManagerUiEffect() + + /** + * Sent instead of disabling at once, for a plugin that came with the app (ADFA-7240). + * [pluginName] is the plugin's name, or its ID when the loaded list does not have it. + */ + data class ShowDisableConfirmation( + val pluginId: String, + val pluginName: String, ) : PluginManagerUiEffect() object ShowRestartPrompt : PluginManagerUiEffect() diff --git a/app/src/main/java/com/itsaky/androidide/ui/models/TemplateManagerUiState.kt b/app/src/main/java/com/itsaky/androidide/ui/models/TemplateManagerUiState.kt index d8d8ec9dbe..b4adae7ba1 100644 --- a/app/src/main/java/com/itsaky/androidide/ui/models/TemplateManagerUiState.kt +++ b/app/src/main/java/com/itsaky/androidide/ui/models/TemplateManagerUiState.kt @@ -50,8 +50,10 @@ sealed class TemplateManagerUiEffect { val item: CgtFileItem, ) : TemplateManagerUiEffect() + /** [bundled]: the template came with the app (ADFA-7240), so the dialog warns about removing it. */ data class ShowUninstallConfirmation( val item: CgtFileItem, + val bundled: Boolean, ) : TemplateManagerUiEffect() data class ShowDeleteConfirmation( diff --git a/app/src/main/java/com/itsaky/androidide/viewmodels/PluginManagerViewModel.kt b/app/src/main/java/com/itsaky/androidide/viewmodels/PluginManagerViewModel.kt index 3e02e104b0..5c5b92130b 100644 --- a/app/src/main/java/com/itsaky/androidide/viewmodels/PluginManagerViewModel.kt +++ b/app/src/main/java/com/itsaky/androidide/viewmodels/PluginManagerViewModel.kt @@ -47,6 +47,8 @@ class PluginManagerViewModel( private val connectivityObserver: ConnectivityObserver, // Gradle's offline mode (Build & Run preferences). A function so tests need no preferences. private val isOfflineModeEnabled: () -> Boolean, + // Whether the plugin came with the app (ADFA-7240). Reads a file, so it is called from IO only. + private val isBundledPlugin: (String) -> Boolean, ) : ViewModel() { private companion object { private const val TAG = "PluginManagerViewModel" @@ -145,7 +147,7 @@ class PluginManagerViewModel( } is PluginManagerUiEvent.DisablePlugin -> { - disablePlugin(event.pluginId) + showDisableConfirmationIfBundled(event.pluginId) } is PluginManagerUiEvent.UninstallPlugin -> { @@ -267,9 +269,29 @@ class PluginManagerViewModel( } /** - * Disable a plugin + * A plugin that came with the app asks for confirmation first; any other disables at once. + * Decided by ID, not by the loaded list, so a list being reloaded cannot skip the warning; the + * list only supplies the name, and the ID stands in when it lacks the plugin. */ - private fun disablePlugin(pluginId: String) { + private fun showDisableConfirmationIfBundled(pluginId: String) { + viewModelScope.launch { + if (withContext(Dispatchers.IO) { isBundledPlugin(pluginId) }) { + val name = + _uiState.value.plugins + .find { it.metadata.id == pluginId } + ?.metadata + ?.name ?: pluginId + _uiEffect.send(PluginManagerUiEffect.ShowDisableConfirmation(pluginId, name)) + } else { + confirmDisablePlugin(pluginId) + } + } + } + + /** + * Disable a plugin (called after confirmation, or directly for a plugin the user added) + */ + fun confirmDisablePlugin(pluginId: String) { viewModelScope.launch { _currentOperation.value = PluginOperation.Disabling(pluginId) @@ -305,7 +327,8 @@ class PluginManagerViewModel( val plugin = _uiState.value.plugins.find { it.metadata.id == pluginId } if (plugin != null) { viewModelScope.launch { - _uiEffect.send(PluginManagerUiEffect.ShowUninstallConfirmation(plugin)) + val bundled = withContext(Dispatchers.IO) { isBundledPlugin(pluginId) } + _uiEffect.send(PluginManagerUiEffect.ShowUninstallConfirmation(plugin, bundled)) } } } diff --git a/app/src/main/java/com/itsaky/androidide/viewmodels/TemplateManagerViewModel.kt b/app/src/main/java/com/itsaky/androidide/viewmodels/TemplateManagerViewModel.kt index 0e0641e366..b227ae0697 100644 --- a/app/src/main/java/com/itsaky/androidide/viewmodels/TemplateManagerViewModel.kt +++ b/app/src/main/java/com/itsaky/androidide/viewmodels/TemplateManagerViewModel.kt @@ -10,6 +10,7 @@ import com.itsaky.androidide.templates.manager.models.displayName import com.itsaky.androidide.ui.models.TemplateManagerUiEffect import com.itsaky.androidide.ui.models.TemplateManagerUiEvent import com.itsaky.androidide.ui.models.TemplateManagerUiState +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow @@ -19,6 +20,7 @@ import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext /** * ViewModel for the Templates tab. Same UDF shape as [PluginManagerViewModel]. @@ -26,6 +28,8 @@ import kotlinx.coroutines.launch class TemplateManagerViewModel( private val templateRepository: TemplateRepository, collectionInstalls: Flow, + // Whether an installed template file (by file name) came with the app (ADFA-7240). Reads a file, so IO only. + private val isBundledTemplate: (String) -> Boolean, ) : ViewModel() { private companion object { private const val TAG = "TemplateManagerViewModel" @@ -138,7 +142,8 @@ class TemplateManagerViewModel( private fun showUninstallConfirmation(item: CgtFileItem) { viewModelScope.launch { - _uiEffect.send(TemplateManagerUiEffect.ShowUninstallConfirmation(item)) + val bundled = item.installed && withContext(Dispatchers.IO) { isBundledTemplate(item.file.name) } + _uiEffect.send(TemplateManagerUiEffect.ShowUninstallConfirmation(item, bundled)) } } diff --git a/app/src/test/java/com/itsaky/androidide/viewmodels/PluginManagerViewModelSourceDeleteTest.kt b/app/src/test/java/com/itsaky/androidide/viewmodels/PluginManagerViewModelSourceDeleteTest.kt index fe52ffb1ab..ba316dfa54 100644 --- a/app/src/test/java/com/itsaky/androidide/viewmodels/PluginManagerViewModelSourceDeleteTest.kt +++ b/app/src/test/java/com/itsaky/androidide/viewmodels/PluginManagerViewModelSourceDeleteTest.kt @@ -66,6 +66,7 @@ class PluginManagerViewModelSourceDeleteTest { override fun observe() = flowOf(true) }, isOfflineModeEnabled = { false }, + isBundledPlugin = { false }, ) private fun downloadedPlugin(): File = File(tempFolder.newFolder("Download"), "my-plugin.cgp").apply { writeText("cgp") } diff --git a/app/src/test/java/com/itsaky/androidide/viewmodels/PluginManagerViewModelTest.kt b/app/src/test/java/com/itsaky/androidide/viewmodels/PluginManagerViewModelTest.kt index 7452f99eee..cde6edeaaa 100644 --- a/app/src/test/java/com/itsaky/androidide/viewmodels/PluginManagerViewModelTest.kt +++ b/app/src/test/java/com/itsaky/androidide/viewmodels/PluginManagerViewModelTest.kt @@ -1,19 +1,29 @@ package com.itsaky.androidide.viewmodels +import androidx.arch.core.executor.testing.InstantTaskExecutorRule import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.plugins.PluginInfo +import com.itsaky.androidide.plugins.PluginMetadata import com.itsaky.androidide.repositories.PluginRepository +import com.itsaky.androidide.ui.models.PluginManagerUiEffect +import com.itsaky.androidide.ui.models.PluginManagerUiEvent import com.itsaky.androidide.utils.ConnectivityObserver import com.itsaky.androidide.viewmodel.MainDispatcherRule +import io.mockk.coEvery +import io.mockk.coVerify import io.mockk.every import io.mockk.mockk import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.advanceUntilIdle import kotlinx.coroutines.test.runTest import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 +import org.robolectric.RobolectricTestRunner import java.io.File /** A fake [ConnectivityObserver] whose emissions are driven by [state], for tests that need to simulate a change over time. */ @@ -51,6 +61,7 @@ class PluginManagerViewModelTest { filesDir = File("/tmp"), connectivityObserver = connectivityObserver, isOfflineModeEnabled = { offlineMode }, + isBundledPlugin = { false }, ) @Test @@ -103,3 +114,121 @@ class PluginManagerViewModelTest { assertThat(viewModel.uiState.value.showDiscoverPlugins).isFalse() } } + +/** + * Warnings before removing or disabling a plugin that came with the app (ADFA-7240). Robolectric + * because loading the plugin list refreshes [com.itsaky.androidide.utils.EditorDecorationBridge]. + */ +@RunWith(RobolectricTestRunner::class) +@OptIn(ExperimentalCoroutinesApi::class) +class PluginManagerViewModelBundledTest { + @get:Rule + val instantExecutorRule = InstantTaskExecutorRule() + + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + + private fun plugin(id: String) = + PluginInfo( + // A name unlike the ID, so a warning that names the plugin by ID fails. + metadata = PluginMetadata(id, "Name of $id", "1.0", "d", "a", "1.0"), + isEnabled = true, + isLoaded = true, + ) + + private val bundledPlugin = plugin("bundled.plugin") + private val userPlugin = plugin("user.plugin") + + private val pluginRepository = + mockk(relaxed = true) { + every { isPluginManagerAvailable() } returns true + coEvery { getAllPlugins() } returns Result.success(listOf(bundledPlugin, userPlugin)) + coEvery { disablePlugin(any()) } returns Result.success(true) + } + + private fun TestScope.viewModel(isBundledPlugin: (String) -> Boolean): PluginManagerViewModel { + val viewModel = + PluginManagerViewModel( + pluginRepository, + mockk(relaxed = true), + File("/tmp"), + FakeConnectivityObserver(initialValue = true), + { false }, + isBundledPlugin, + ) + advanceUntilIdle() + return viewModel + } + + // No withTimeout: the bundled check runs on Dispatchers.IO, outside runTest's virtual time, and a + // virtual-time timeout could fire before it returns. + private suspend fun PluginManagerViewModel.firstEffect() = uiEffect.first() + + @Test + fun uninstall_bundledPlugin_sendsBundledTrue() = + runTest { + val viewModel = viewModel { it == "bundled.plugin" } + + viewModel.onEvent(PluginManagerUiEvent.UninstallPlugin("bundled.plugin")) + + assertThat(viewModel.firstEffect()) + .isEqualTo(PluginManagerUiEffect.ShowUninstallConfirmation(bundledPlugin, bundled = true)) + } + + @Test + fun uninstall_userPlugin_sendsBundledFalse() = + runTest { + val viewModel = viewModel { it == "bundled.plugin" } + + viewModel.onEvent(PluginManagerUiEvent.UninstallPlugin("user.plugin")) + + assertThat(viewModel.firstEffect()) + .isEqualTo(PluginManagerUiEffect.ShowUninstallConfirmation(userPlugin, bundled = false)) + } + + @Test + fun disable_bundledPlugin_asksFirstAndDoesNotDisable() = + runTest { + val viewModel = viewModel { it == "bundled.plugin" } + + viewModel.onEvent(PluginManagerUiEvent.DisablePlugin("bundled.plugin")) + + assertThat(viewModel.firstEffect()) + .isEqualTo(PluginManagerUiEffect.ShowDisableConfirmation("bundled.plugin", "Name of bundled.plugin")) + coVerify(exactly = 0) { pluginRepository.disablePlugin(any()) } + } + + @Test + fun disable_bundledPluginMissingFromTheList_asksAndNamesItById() = + runTest { + val viewModel = viewModel { true } + + viewModel.onEvent(PluginManagerUiEvent.DisablePlugin("reloading.plugin")) + + assertThat(viewModel.firstEffect()) + .isEqualTo(PluginManagerUiEffect.ShowDisableConfirmation("reloading.plugin", "reloading.plugin")) + coVerify(exactly = 0) { pluginRepository.disablePlugin(any()) } + } + + @Test + fun disable_userPlugin_disablesAtOnce() = + runTest { + val viewModel = viewModel { it == "bundled.plugin" } + + viewModel.onEvent(PluginManagerUiEvent.DisablePlugin("user.plugin")) + + assertThat(viewModel.firstEffect()).isInstanceOf(PluginManagerUiEffect.ShowSuccess::class.java) + coVerify(exactly = 1) { pluginRepository.disablePlugin("user.plugin") } + } + + @Test + fun confirmDisablePlugin_disables() = + runTest { + val viewModel = viewModel { true } + + viewModel.confirmDisablePlugin("bundled.plugin") + + assertThat(viewModel.firstEffect()).isInstanceOf(PluginManagerUiEffect.ShowSuccess::class.java) + coVerify(exactly = 1) { pluginRepository.disablePlugin("bundled.plugin") } + } +} diff --git a/app/src/test/java/com/itsaky/androidide/viewmodels/TemplateManagerViewModelTest.kt b/app/src/test/java/com/itsaky/androidide/viewmodels/TemplateManagerViewModelTest.kt index d5ff0975d0..338040249b 100644 --- a/app/src/test/java/com/itsaky/androidide/viewmodels/TemplateManagerViewModelTest.kt +++ b/app/src/test/java/com/itsaky/androidide/viewmodels/TemplateManagerViewModelTest.kt @@ -72,7 +72,7 @@ class TemplateManagerViewModelTest { runTest { coEvery { repository.listTemplateFiles() } returns Result.success(listOf(item)) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() assertThat(viewModel.uiState.value.isLoading).isFalse() @@ -87,7 +87,7 @@ class TemplateManagerViewModelTest { coEvery { repository.listTemplateFiles() } returnsMany listOf(Result.success(emptyList()), Result.success(listOf(installedItem))) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() assertThat(viewModel.uiState.value.items).isEmpty() @@ -109,7 +109,7 @@ class TemplateManagerViewModelTest { if (calls++ == 0) initialScan.await() else Result.success(listOf(installedItem)) } - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() collectionInstalls.emit(Unit) @@ -126,7 +126,7 @@ class TemplateManagerViewModelTest { runTest { coEvery { repository.listTemplateFiles() } returns Result.success(emptyList()) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() viewModel.onEvent(TemplateManagerUiEvent.InstallTemplate(item)) @@ -142,7 +142,7 @@ class TemplateManagerViewModelTest { coEvery { repository.listTemplateFiles() } returns Result.success(emptyList()) coEvery { repository.installTemplate(item) } returns Result.success(Unit) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() viewModel.confirmInstallTemplate(item, deleteSourceAfterInstall = false) @@ -158,7 +158,7 @@ class TemplateManagerViewModelTest { coEvery { repository.installTemplate(item) } returns Result.success(Unit) coEvery { repository.deleteDownloadFile(item) } returns Result.success(Unit) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() viewModel.confirmInstallTemplate(item, deleteSourceAfterInstall = true) @@ -177,7 +177,7 @@ class TemplateManagerViewModelTest { coEvery { repository.installTemplate(item) } returns Result.success(Unit) coEvery { repository.deleteDownloadFile(item) } returns Result.failure(java.io.IOException("ro")) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() viewModel.confirmInstallTemplate(item, deleteSourceAfterInstall = true) @@ -194,7 +194,7 @@ class TemplateManagerViewModelTest { coEvery { repository.listTemplateFiles() } returns Result.success(emptyList()) coEvery { repository.installTemplate(item) } returns Result.failure(java.io.IOException("boom")) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() viewModel.confirmInstallTemplate(item, deleteSourceAfterInstall = true) @@ -218,7 +218,7 @@ class TemplateManagerViewModelTest { coEvery { repository.listTemplateFiles() } returns Result.success(emptyList()) coEvery { repository.installTemplate(item) } returns Result.success(Unit) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() viewModel.confirmInstallTemplate(item, deleteSourceAfterInstall = false) @@ -236,7 +236,7 @@ class TemplateManagerViewModelTest { coEvery { repository.listTemplateFiles() } returns Result.success(emptyList()) coEvery { repository.installTemplate(item) } returns Result.failure(java.io.IOException("boom")) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() viewModel.confirmInstallTemplate(item, deleteSourceAfterInstall = false) @@ -255,23 +255,76 @@ class TemplateManagerViewModelTest { // assertion below, rather than throwing inside the coroutine and hanging first(). coEvery { repository.uninstallTemplate(installedItem) } returns Result.success(Unit) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() viewModel.onEvent(TemplateManagerUiEvent.UninstallTemplate(installedItem)) advanceUntilIdle() - assertThat(viewModel.uiEffect.first()).isEqualTo(TemplateManagerUiEffect.ShowUninstallConfirmation(installedItem)) + assertThat(viewModel.uiEffect.first()) + .isEqualTo(TemplateManagerUiEffect.ShowUninstallConfirmation(installedItem, bundled = false)) coVerify(exactly = 0) { repository.uninstallTemplate(any()) } } + @Test + fun uninstallTemplate_installedAndInTheBundledRecord_isMarkedBundled() = + runTest { + coEvery { repository.listTemplateFiles() } returns Result.success(emptyList()) + val asked = mutableListOf() + + val viewModel = + TemplateManagerViewModel( + repository, + collectionInstalls, + isBundledTemplate = { name -> + asked += name + name == "install.cgt" + }, + ) + advanceUntilIdle() + + viewModel.onEvent(TemplateManagerUiEvent.UninstallTemplate(installedItem)) + + assertThat(viewModel.uiEffect.first()) + .isEqualTo(TemplateManagerUiEffect.ShowUninstallConfirmation(installedItem, bundled = true)) + assertThat(asked).containsExactly("install.cgt") + } + + @Test + fun uninstallTemplate_installedButNotInTheRecord_isNotBundled() = + runTest { + coEvery { repository.listTemplateFiles() } returns Result.success(emptyList()) + + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) + advanceUntilIdle() + + viewModel.onEvent(TemplateManagerUiEvent.UninstallTemplate(installedItem)) + + assertThat(viewModel.uiEffect.first()) + .isEqualTo(TemplateManagerUiEffect.ShowUninstallConfirmation(installedItem, bundled = false)) + } + + @Test + fun uninstallTemplate_notInstalled_isNeverBundledEvenIfTheNameMatches() = + runTest { + coEvery { repository.listTemplateFiles() } returns Result.success(emptyList()) + + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { true }) + advanceUntilIdle() + + viewModel.onEvent(TemplateManagerUiEvent.UninstallTemplate(item)) + + assertThat(viewModel.uiEffect.first()) + .isEqualTo(TemplateManagerUiEffect.ShowUninstallConfirmation(item, bundled = false)) + } + @Test fun confirmUninstallTemplate_onSuccess_reloadsAndSendsShowSuccessEffect() = runTest { coEvery { repository.listTemplateFiles() } returns Result.success(emptyList()) coEvery { repository.uninstallTemplate(installedItem) } returns Result.success(Unit) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() viewModel.confirmUninstallTemplate(installedItem) @@ -289,7 +342,7 @@ class TemplateManagerViewModelTest { coEvery { repository.listTemplateFiles() } returns Result.success(emptyList()) coEvery { repository.uninstallTemplate(installedItem) } returns Result.failure(java.io.IOException("boom")) - val viewModel = TemplateManagerViewModel(repository, collectionInstalls) + val viewModel = TemplateManagerViewModel(repository, collectionInstalls, isBundledTemplate = { false }) advanceUntilIdle() viewModel.confirmUninstallTemplate(installedItem) diff --git a/resources/src/main/res/values/strings.xml b/resources/src/main/res/values/strings.xml index c8584484b5..9e382bd3aa 100644 --- a/resources/src/main/res/values/strings.xml +++ b/resources/src/main/res/values/strings.xml @@ -1352,6 +1352,14 @@ https://addons.appdevforall.org Uninstall Plugin Are you sure you want to uninstall \'%1$s\'? + + Remove a plugin that came with the app? + \'%1$s\' was installed automatically. If you don\'t replace it with something equivalent, the installation will probably break. + Disable a plugin that came with the app? + \'%1$s\' was installed automatically. If you disable it without replacing it with something equivalent, the installation will probably break. + Remove a template that came with the app? + \'%1$s\' was installed automatically. If you don\'t replace it with something equivalent, the installation will probably break. + Continue Name Plugin ID Version