Skip to content
Merged
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 @@ -11,6 +11,7 @@ import com.itsaky.androidide.databinding.ActivityPluginManagerBinding
import com.itsaky.androidide.ui.compose.ManagerScreen
import com.itsaky.androidide.ui.compose.theme.ManagerTheme
import com.itsaky.androidide.utils.flashError
import com.itsaky.androidide.viewmodels.DownloadPickerViewModel
import com.itsaky.androidide.viewmodels.ExternalFileInstallViewModel
import com.itsaky.androidide.viewmodels.PluginManagerViewModel
import com.itsaky.androidide.viewmodels.TemplateManagerViewModel
Expand Down Expand Up @@ -40,6 +41,9 @@ class PluginManagerActivity : EdgeToEdgeIDEActivity() {
// uses, so a template picked here goes through the identical confirm/conflict/rename path.
private val externalFileInstallViewModel: ExternalFileInstallViewModel by viewModel()

// Lists the matching .cgp/.cgt files in Downloads for the + button (ADFA-6729).
private val downloadPickerViewModel: DownloadPickerViewModel by viewModel()

override fun bindLayout(): View {
_binding = ActivityPluginManagerBinding.inflate(layoutInflater)
return binding.root
Expand All @@ -57,6 +61,7 @@ class PluginManagerActivity : EdgeToEdgeIDEActivity() {
val resolvedPluginViewModel = pluginViewModel
val resolvedTemplateViewModel = templateViewModel
val resolvedExternalFileInstallViewModel = externalFileInstallViewModel
val resolvedDownloadPickerViewModel = downloadPickerViewModel

binding.composeView.setContent {
ManagerTheme {
Expand All @@ -65,6 +70,7 @@ class PluginManagerActivity : EdgeToEdgeIDEActivity() {
pluginViewModel = resolvedPluginViewModel,
templateViewModel = resolvedTemplateViewModel,
externalFileInstallViewModel = resolvedExternalFileInstallViewModel,
downloadPickerViewModel = resolvedDownloadPickerViewModel,
)
}
}
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/itsaky/androidide/di/PluginModule.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.itsaky.androidide.di

import com.itsaky.androidide.app.IDEApplication
import com.itsaky.androidide.repositories.DownloadFilesRepository
import com.itsaky.androidide.repositories.DownloadFilesRepositoryImpl
import com.itsaky.androidide.repositories.PluginRepository
import com.itsaky.androidide.repositories.PluginRepositoryImpl
import com.itsaky.androidide.repositories.TemplateCollectionRepository
import com.itsaky.androidide.repositories.TemplateCollectionRepositoryImpl
import com.itsaky.androidide.utils.Environment
import com.itsaky.androidide.viewmodels.DownloadPickerViewModel
import com.itsaky.androidide.viewmodels.ExternalFileInstallViewModel
import com.itsaky.androidide.viewmodels.PluginManagerViewModel
import org.koin.android.ext.koin.androidContext
Expand All @@ -30,6 +34,10 @@ val pluginModule =
TemplateCollectionRepositoryImpl()
}

single<DownloadFilesRepository> {
DownloadFilesRepositoryImpl(downloadDir = Environment.DOWNLOAD_DIR)
}

// ViewModel
viewModel {
PluginManagerViewModel(
Expand All @@ -39,6 +47,10 @@ val pluginModule =
)
}

viewModel {
DownloadPickerViewModel(downloadFilesRepository = get())
}

viewModel {
ExternalFileInstallViewModel(
pluginRepository = get(),
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/java/com/itsaky/androidide/repositories/AddonFiles.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.itsaky.androidide.repositories

import org.adfa.constants.TEMPLATE_CORE_ARCHIVE
import java.io.File

/**
* Add-on archives directly in [dir] (not its subfolders) whose extension is [extension], ignoring
* case. Shared by the Templates tab and the Manager screen's + picker so the two list the same
* files. Empty if [dir] is missing or unreadable.
*
* Leaves out:
* - MediaProvider's placeholders. On Android 11+ it keeps a download still in progress, or a file
* in the trash, in its folder as `.pending-<expiry>-<name>` or `.trashed-<expiry>-<name>`, which
* keep the add-on extension. Offering one would install a partial archive, or a file the user
* deleted. Only that exact form is skipped, not every hidden name: the installer accepts a
* collection named `.name`, stored as `.name.cgt`, and uninstalling moves it to Downloads.
* - The bundled core.cgt (ADFA-5645). It ships with the IDE, and installing a copy fails on its
* reserved name.
*/
internal fun addonFilesIn(
dir: File,
extension: String,
): List<File> =
dir
// Name checks first: they are free, and isFile is a stat per entry.
.listFiles { file ->
!MEDIA_STORE_PLACEHOLDER.matches(file.name) &&
file.extension.equals(extension, ignoreCase = true) &&
!file.name.equals(TEMPLATE_CORE_ARCHIVE, ignoreCase = true) &&
file.isFile
}.orEmpty()
.toList()

// AOSP MediaProvider's FileUtils.PATTERN_EXPIRES_FILE, which names these files ".%s-%d-%s".
private val MEDIA_STORE_PLACEHOLDER = Regex("""^\.(pending|trashed)-\d+-.+$""", RegexOption.IGNORE_CASE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.itsaky.androidide.repositories

import java.io.File

/** A file in the Downloads folder, as offered by the Manager screen's install picker. */
data class DownloadedFile(
val file: File,
val sizeBytes: Long,
val lastModified: Long,
) {
val name: String get() = file.name
}

/**
* Lists add-on files in the Downloads folder by extension.
*
* The system file picker cannot do this: SAF filters by MIME type, and neither `.cgp` nor `.cgt`
* has a registered one, so it lists every file (ADFA-6729).
*/
interface DownloadFilesRepository {
/**
* Files directly in the Downloads folder (not its subfolders) whose extension is [extension],
* ignoring case, newest first. Hidden files and the bundled core.cgt are left out; see
* [addonFilesIn]. Empty if the folder is missing or unreadable.
*/
suspend fun list(extension: String): List<DownloadedFile>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.itsaky.androidide.repositories

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

class DownloadFilesRepositoryImpl(
Comment thread
jimturner-adfa marked this conversation as resolved.
Resolved
private val downloadDir: File,
) : DownloadFilesRepository {
override suspend fun list(extension: String): List<DownloadedFile> =
withContext(Dispatchers.IO) {
addonFilesIn(downloadDir, extension)
.map { file -> DownloadedFile(file, file.length(), file.lastModified()) }
// Newest first: the file the user just downloaded is the one they are looking for.
.sortedByDescending { it.lastModified }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ class TemplateRepositoryImpl(
* (`TemplateCollectionRepository.findExistingCollision`), so any row still listed as
* "not installed" is one the user can actually install.
*
* The bundled `core.cgt` is excluded outright (ADFA-5645): it ships with the IDE, isn't
* something the user installed or can uninstall, and has nothing to do with this screen's
* install/uninstall/delete flows.
* The bundled `core.cgt` is excluded outright (ADFA-5645), from both folders, by
* [addonFilesIn]: it ships with the IDE, isn't something the user installed or can uninstall,
* and has nothing to do with this screen's install/uninstall/delete flows. That holds even
* when templatesDir doesn't have it yet (bootstrap incomplete) and only a Downloads copy exists.
*/
private fun scanTemplates(): List<CgtFileItem> {
val installed = cgtFilesIn(templatesDir).mapNotNull { file -> parseCgtFile(file, installed = true) }
Expand All @@ -68,19 +69,13 @@ class TemplateRepositoryImpl(
cgtFilesIn(downloadDir)
.filterNot { file -> file.name.lowercase() in installedNames }
.mapNotNull { file -> parseCgtFile(file, installed = false) }
// Filtered out of the combined list, not just `installed`: core.cgt must be excluded even
// if templatesDir doesn't have it yet (bootstrap incomplete) and only a Downloads copy exists.
return (installed + downloaded).filterNot { item -> isCoreArchive(item.name) }
return installed + downloaded
}

/** Case-insensitive, like the twin dedup above and `findExistingCollision`'s reserved name. */
private fun isCoreArchive(fileName: String): Boolean = fileName.equals(TEMPLATE_CORE_ARCHIVE, ignoreCase = true)

private fun cgtFilesIn(dir: File): List<File> =
dir
.listFiles { file -> file.isFile && file.extension.equals(CGT_EXTENSION, ignoreCase = true) }
?.sortedBy { it.name }
?: emptyList()
private fun cgtFilesIn(dir: File): List<File> = addonFilesIn(dir, CGT_EXTENSION).sortedBy { it.name }

/** Parses a .cgt (which may bundle multiple templates) into a card item, or null if it contains no template.json. */
private fun parseCgtFile(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.itsaky.androidide.ui.compose

import android.text.format.DateUtils
import android.text.format.Formatter
import android.view.View
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.dp
import com.itsaky.androidide.R
import com.itsaky.androidide.repositories.DownloadedFile
import com.itsaky.androidide.ui.models.AddonFileKind
import com.itsaky.androidide.ui.models.DownloadPickerUiState

/**
* The + button's picker: the open tab's add-on files in Downloads, plus a way out to the system
* picker for files kept anywhere else.
*
* Long-press on a file row or on Browse other folders calls [onShowHelp] with the dialog's own
* view, to anchor the help popup above the dialog.
*/
@Composable
fun DownloadPickerDialog(
Comment thread
jimturner-adfa marked this conversation as resolved.
Resolved
state: DownloadPickerUiState.Open,
onPick: (DownloadedFile) -> Unit,
onBrowse: () -> Unit,
onDismiss: () -> Unit,
onShowHelp: (anchor: View) -> Unit,
) {
AlertDialog(
onDismissRequest = onDismiss,
title = {
Text(
stringResource(
when (state.kind) {
AddonFileKind.PLUGIN -> R.string.title_choose_plugin_file
AddonFileKind.TEMPLATE -> R.string.title_choose_template_file
},
),
)
},
text = {
val files = state.files
when {
files == null -> {
Box(modifier = Modifier.fillMaxWidth().padding(16.dp), contentAlignment = Alignment.Center) {
CircularProgressIndicator()
}
}

files.isEmpty() -> {
Text(
stringResource(R.string.msg_no_download_files, ".${state.kind.extension}"),
style = MaterialTheme.typography.bodyMedium,
)
}

// Scrolls: Downloads can hold many add-ons, and rows grow with the font size.
else -> {
LazyColumn {
items(files, key = { it.file.absolutePath }) { file ->
DownloadFileRow(file = file, onClick = { onPick(file) }, onShowHelp = onShowHelp)
HorizontalDivider()
}
}
}
}
},
confirmButton = {
val view = LocalView.current
val longPressAwareClick = rememberLongPressInteractionSource { onShowHelp(view) }
TextButton(
onClick = {
// TextButton's clickable() fires on the lift that ends the long press; drop that one.
if (longPressAwareClick.consumeIfSuppressed()) return@TextButton
onBrowse()
},
interactionSource = longPressAwareClick.interactionSource,
) {
Text(stringResource(R.string.action_browse_other_folders))
}
},
dismissButton = {
TextButton(onClick = onDismiss) { Text(stringResource(android.R.string.cancel)) }
},
)
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun DownloadFileRow(
file: DownloadedFile,
onClick: () -> Unit,
onShowHelp: (anchor: View) -> Unit,
) {
val context = LocalContext.current
val view = LocalView.current
val size = Formatter.formatShortFileSize(context, file.sizeBytes)
val date =
DateUtils.formatDateTime(
context,
file.lastModified,
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_ABBREV_MONTH,
)
Column(
modifier =
Modifier
.fillMaxWidth()
.combinedClickable(
role = Role.Button,
onClickLabel = stringResource(R.string.action_install),
onLongClickLabel = stringResource(R.string.cd_show_tooltip),
onClick = onClick,
onLongClick = { onShowHelp(view) },
).padding(vertical = 12.dp),
) {
// Not ellipsized: names like "fun-and-games-libgdx-v2.cgt" differ only at the end.
Text(file.name, style = MaterialTheme.typography.titleSmall)
Text(
stringResource(R.string.label_download_file_details, size, date),
style = MaterialTheme.typography.bodySmall,
)
}
}
Loading