-
-
Notifications
You must be signed in to change notification settings - Fork 23
ADFA-3583 | Automate image import for YOLO image placeholders #1243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jatezzz
merged 4 commits into
stage
from
feat/ADFA-3583-image-import-placeholder-experimental
May 5, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
687fb25
feat: automate image import on placeholder tap
jatezzz 172500a
refactor: use AttributeKey xmlName for background attrs
jatezzz 071582e
fix: avoid TOCTOU race condition and refactor extension detection
jatezzz d377866
fix: add support for `selectedImageOverrides` in `LayoutRenderer` and…
jatezzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
.../java/org/appdevforall/codeonthego/computervision/data/repository/DrawableImportHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package org.appdevforall.codeonthego.computervision.data.repository | ||
|
|
||
| import android.content.ContentResolver | ||
| import android.net.Uri | ||
| import android.provider.OpenableColumns | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import java.io.File | ||
| import java.util.Locale | ||
|
|
||
| class DrawableImportHelper( | ||
| private val contentResolver: ContentResolver | ||
| ) { | ||
|
|
||
| suspend fun importDrawable( | ||
| sourceUri: Uri, | ||
| layoutFilePath: String?, | ||
| fallbackName: String | ||
| ): Result<ImportedDrawable> = withContext(Dispatchers.IO) { | ||
| runCatching { | ||
| requireNotNull(layoutFilePath) { "Layout file path is not available." } | ||
| val layoutFile = File(layoutFilePath) | ||
|
|
||
| val drawableDir = resolveDrawableDir(layoutFile) | ||
| check(drawableDir.exists() || drawableDir.mkdirs()) { | ||
| "Could not create drawable directory: ${drawableDir.absolutePath}" | ||
| } | ||
|
|
||
| val extension = resolveSupportedExtension(sourceUri, fallbackName) | ||
| val baseName = sanitizeResourceName(resolveDisplayName(sourceUri) ?: fallbackName) | ||
| val destinationFile = resolveAvailableFile(drawableDir, baseName, extension) | ||
|
|
||
| contentResolver.openInputStream(sourceUri)?.use { input -> | ||
| destinationFile.outputStream().use(input::copyTo) | ||
| } ?: error("Could not open selected image.") | ||
|
|
||
| ImportedDrawable( | ||
| resourceName = destinationFile.nameWithoutExtension, | ||
| drawableReference = "@drawable/${destinationFile.nameWithoutExtension}", | ||
| file = destinationFile | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private fun resolveDrawableDir(layoutFile: File): File { | ||
| val resDir = generateSequence(layoutFile.parentFile) { it.parentFile } | ||
| .firstOrNull { it.name == "res" } | ||
| ?: throw IllegalStateException("Could not resolve res directory from: ${layoutFile.absolutePath}") | ||
|
|
||
| return File(resDir, "drawable") | ||
| } | ||
|
|
||
| private fun resolveDisplayName(uri: Uri): String? { | ||
| return contentResolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null) | ||
| ?.use { cursor -> | ||
| val index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) | ||
| if (index >= 0 && cursor.moveToFirst()) cursor.getString(index) else null | ||
| } | ||
| } | ||
|
|
||
| private fun resolveSupportedExtension(uri: Uri, fallbackName: String): String { | ||
| val mimeType = contentResolver.getType(uri)?.lowercase(Locale.US) | ||
| var extension = when (mimeType) { | ||
| "image/png" -> "png" | ||
| "image/jpeg", "image/jpg" -> "jpg" | ||
| "image/webp" -> "webp" | ||
| else -> null | ||
| } | ||
|
|
||
| if (extension == null) { | ||
| val nameToUse = resolveDisplayName(uri) ?: fallbackName | ||
| extension = nameToUse | ||
| .substringAfterLast('.', missingDelimiterValue = "") | ||
| .lowercase(Locale.US) | ||
| .takeIf { it.isNotBlank() } | ||
| } | ||
|
|
||
| return when (extension) { | ||
| "png", "jpg", "jpeg", "webp" -> extension | ||
| else -> throw IllegalArgumentException("Unsupported image format. Use PNG, JPG, JPEG, or WEBP.") | ||
| } | ||
| } | ||
|
|
||
| private fun sanitizeResourceName(rawName: String): String { | ||
| val nameWithoutExtension = rawName.substringBeforeLast('.') | ||
| val normalized = nameWithoutExtension | ||
| .lowercase(Locale.US) | ||
| .replace(Regex("[^a-z0-9_]"), "_") | ||
| .replace(Regex("_+"), "_") | ||
| .trim('_') | ||
|
|
||
| val safeName = normalized.ifBlank { "imported_image" } | ||
|
|
||
| return if (safeName.first().isDigit()) { | ||
| "img_$safeName" | ||
| } else { | ||
| safeName | ||
| } | ||
| } | ||
|
|
||
| private fun resolveAvailableFile( | ||
| drawableDir: File, | ||
| baseName: String, | ||
| extension: String | ||
| ): File { | ||
| var candidate = File(drawableDir, "$baseName.$extension") | ||
| var index = 1 | ||
|
|
||
| while (!candidate.createNewFile()) { | ||
| candidate = File(drawableDir, "${baseName}_$index.$extension") | ||
| index++ | ||
| } | ||
|
|
||
| return candidate | ||
| } | ||
| } | ||
|
|
||
| data class ImportedDrawable( | ||
| val resourceName: String, | ||
| val drawableReference: String, | ||
| val file: File | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.