Skip to content

Commit 1d68499

Browse files
committed
fix(xml): extract API classes from aaptcompiler to :xml:resources-api
1 parent 6fcb177 commit 1d68499

File tree

96 files changed

+1900
-1061
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1900
-1061
lines changed

.idea/compiler.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/projects/src/main/java/com/itsaky/androidide/projects/android/AndroidModule.kt

+29-26
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package com.itsaky.androidide.projects.android
1919

2020
import com.android.SdkConstants
2121
import com.android.aaptcompiler.AaptResourceType
22-
import com.android.aaptcompiler.ResourceTable
2322
import com.android.builder.model.v2.ide.LibraryType.ANDROID_LIBRARY
2423
import com.android.builder.model.v2.ide.LibraryType.JAVA_LIBRARY
2524
import com.android.builder.model.v2.ide.LibraryType.PROJECT
@@ -38,6 +37,7 @@ import com.itsaky.androidide.tooling.api.models.BasicAndroidVariantMetadata
3837
import com.itsaky.androidide.tooling.api.models.GradleTask
3938
import com.itsaky.androidide.tooling.api.util.findPackageName
4039
import com.itsaky.androidide.utils.withStopWatch
40+
import com.itsaky.androidide.xml.res.IResourceTable
4141
import com.itsaky.androidide.xml.resources.ResourceTableRegistry
4242
import com.itsaky.androidide.xml.versions.ApiVersions
4343
import com.itsaky.androidide.xml.versions.ApiVersionsRegistry
@@ -314,7 +314,7 @@ open class AndroidModule( // Class must be open because BaseXMLTest mocks this..
314314
}
315315

316316
/** Get the resource table for this module i.e. without resource tables for dependent modules. */
317-
fun getResourceTable(): ResourceTable? {
317+
fun getResourceTable(): IResourceTable? {
318318
val namespace = this.namespace ?: return null
319319

320320
val resDirs = mainSourceSet?.sourceProvider?.resDirectories ?: return null
@@ -336,11 +336,11 @@ open class AndroidModule( // Class must be open because BaseXMLTest mocks this..
336336
}
337337

338338
/**
339-
* Get the [ResourceTable] instance for this module's compile SDK.
339+
* Get the [IResourceTable] instance for this module's compile SDK.
340340
*
341341
* @return The [ApiVersions] for this module.
342342
*/
343-
fun getFrameworkResourceTable(): ResourceTable? {
343+
fun getFrameworkResourceTable(): IResourceTable? {
344344
val platformDir = getPlatformDir()
345345
if (platformDir != null) {
346346
return ResourceTableRegistry.getInstance().forPlatformDir(platformDir)
@@ -354,7 +354,7 @@ open class AndroidModule( // Class must be open because BaseXMLTest mocks this..
354354
*
355355
* @return The set of resource tables. Empty when project is not initalized.
356356
*/
357-
fun getSourceResourceTables(): Set<ResourceTable> {
357+
fun getSourceResourceTables(): Set<IResourceTable> {
358358
val set = mutableSetOf(getResourceTable() ?: return emptySet())
359359
getCompileModuleProjects().filterIsInstance<AndroidModule>().forEach {
360360
it.getResourceTable()?.also { table -> set.add(table) }
@@ -363,22 +363,22 @@ open class AndroidModule( // Class must be open because BaseXMLTest mocks this..
363363
}
364364

365365
/** Get the resource tables for external dependencies (not local module project dependencies). */
366-
fun getDependencyResourceTables(): Set<ResourceTable> {
367-
return mutableSetOf<ResourceTable>().also {
366+
fun getDependencyResourceTables(): Set<IResourceTable> {
367+
return mutableSetOf<IResourceTable>().also {
368368
var deps: Int
369369
it.addAll(libraryMap.values.filter { library ->
370-
library.type == ANDROID_LIBRARY && library.androidLibraryData!!.resFolder.exists() && library.findPackageName() != UNKNOWN_PACKAGE
371-
}.also { libs -> deps = libs.size }.mapNotNull { library ->
372-
ResourceTableRegistry.getInstance().let { registry ->
373-
registry.isLoggingEnabled = false
374-
registry.forPackage(
375-
library.packageName,
376-
library.androidLibraryData!!.resFolder,
377-
).also {
378-
registry.isLoggingEnabled = true
379-
}
380-
}
381-
})
370+
library.type == ANDROID_LIBRARY && library.androidLibraryData!!.resFolder.exists() && library.findPackageName() != UNKNOWN_PACKAGE
371+
}.also { libs -> deps = libs.size }.mapNotNull { library ->
372+
ResourceTableRegistry.getInstance().let { registry ->
373+
registry.isLoggingEnabled = false
374+
registry.forPackage(
375+
library.packageName,
376+
library.androidLibraryData!!.resFolder,
377+
).also {
378+
registry.isLoggingEnabled = true
379+
}
380+
}
381+
})
382382

383383
log.info("Created {} resource tables for {} dependencies of module '{}'", it.size, deps, path)
384384
}
@@ -390,7 +390,10 @@ open class AndroidModule( // Class must be open because BaseXMLTest mocks this..
390390
*
391391
* @param pck The package to look for.
392392
*/
393-
fun findResourceTableForPackage(pck: String, hasGroup: AaptResourceType? = null): ResourceTable? {
393+
fun findResourceTableForPackage(
394+
pck: String,
395+
hasGroup: AaptResourceType? = null
396+
): IResourceTable? {
394397
return findAllResourceTableForPackage(pck, hasGroup).let {
395398
if (it.isNotEmpty()) {
396399
return it.first()
@@ -406,18 +409,18 @@ open class AndroidModule( // Class must be open because BaseXMLTest mocks this..
406409
*/
407410
fun findAllResourceTableForPackage(
408411
pck: String, hasGroup: AaptResourceType? = null
409-
): List<ResourceTable> {
412+
): List<IResourceTable> {
410413
if (pck == SdkConstants.ANDROID_PKG) {
411414
return getFrameworkResourceTable()?.let { listOf(it) } ?: emptyList()
412415
}
413416

414-
val tables: List<ResourceTable> = mutableListOf<ResourceTable>().apply {
417+
val tables: List<IResourceTable> = mutableListOf<IResourceTable>().apply {
415418
getResourceTable()?.let { add(it) }
416419
addAll(getSourceResourceTables())
417420
addAll(getDependencyResourceTables())
418421
}
419422

420-
val result = mutableListOf<ResourceTable>()
423+
val result = mutableListOf<IResourceTable>()
421424
for (table in tables) {
422425
val resPck = table.findPackage(pck) ?: continue
423426
if (hasGroup == null) {
@@ -439,8 +442,8 @@ open class AndroidModule( // Class must be open because BaseXMLTest mocks this..
439442
*
440443
* @return The associated resource tables.
441444
*/
442-
fun getAllResourceTables(): Set<ResourceTable> {
443-
return mutableSetOf<ResourceTable>().apply {
445+
fun getAllResourceTables(): Set<IResourceTable> {
446+
return mutableSetOf<IResourceTable>().apply {
444447
getResourceTable()?.let { add(it) }
445448
getFrameworkResourceTable()?.let { add(it) }
446449
addAll(getSourceResourceTables())
@@ -449,7 +452,7 @@ open class AndroidModule( // Class must be open because BaseXMLTest mocks this..
449452
}
450453

451454
/** Get the resource table for the attrs_manifest.xml file. */
452-
fun getManifestAttrTable(): ResourceTable? {
455+
fun getManifestAttrTable(): IResourceTable? {
453456
val platform = getPlatformDir() ?: return null
454457
return ResourceTableRegistry.getInstance().getManifestAttrTable(platform)
455458
}

settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ include(
177177
":xml:aaptcompiler",
178178
":xml:dom",
179179
":xml:lsp",
180+
":xml:resources-api",
180181
":xml:utils",
181182
)
182183

utilities/shared/build.gradle.kts

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ plugins {
2525

2626

2727
dependencies {
28-
implementation(libs.androidx.annotation)
29-
implementation(libs.androidx.collection)
28+
api(libs.androidx.annotation)
29+
api(libs.androidx.collection)
30+
3031
implementation(libs.google.guava)
3132
implementation(libs.google.gson)
3233

utilities/xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/utils/basicParseUtils.kt

+22-19
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ import com.android.aaptcompiler.tryParseInt
6060
import com.android.aaptcompiler.tryParseReference
6161
import com.itsaky.androidide.inflater.drawable.DrawableParserFactory
6262
import com.itsaky.androidide.inflater.utils.module
63+
import com.itsaky.androidide.xml.res.IResourceEntry
64+
import com.itsaky.androidide.xml.res.IResourceTable
65+
import com.itsaky.androidide.xml.res.IResourceTablePackage
6366
import org.slf4j.LoggerFactory
6467
import java.io.File
6568
import java.text.SimpleDateFormat
@@ -72,31 +75,31 @@ private const val DEFAULT_STRING_VALUE = "AndroidIDE"
7275
private val stringResolver =
7376
fun(it: Value?): String? {
7477
return when (it) {
75-
is BasicString -> it.ref.value()
76-
is RawString -> it.value.value()
77-
is StyledString -> it.ref.value()
78+
is com.android.aaptcompiler.BasicString -> it.ref.value()
79+
is com.android.aaptcompiler.RawString -> it.value.value()
80+
is com.android.aaptcompiler.StyledString -> it.ref.value()
7881
else -> null
7982
}
8083
}
8184

8285
private val intResolver =
8386
fun(it: Value?): Int? {
84-
return if (it is BinaryPrimitive) {
87+
return if (it is com.android.aaptcompiler.BinaryPrimitive) {
8588
it.resValue.data
8689
} else null
8790
}
8891

8992
val colorResolver: (Value?) -> Int? =
9093
fun(it): Int? {
9194
// TODO(itsaky) : Implement color state list parser
92-
if (it is BinaryPrimitive) {
95+
if (it is com.android.aaptcompiler.BinaryPrimitive) {
9396
return it.resValue.data
9497
}
9598
return null
9699
}
97100

98101
inline fun <reified T> ((Value?) -> T?).arrayResolver(value: Value?): Array<T>? {
99-
return if (value is ArrayResource) {
102+
return if (value is com.android.aaptcompiler.ArrayResource) {
100103
Array(value.elements.size) { invoke(value.elements[it]) ?: return null }
101104
} else emptyArray()
102105
}
@@ -151,7 +154,7 @@ fun parseInteger(value: String, def: Int = 0): Int {
151154
return def
152155
}
153156
if (value.isDigitsOnly()) {
154-
tryParseInt(value)?.resValue?.apply {
157+
com.android.aaptcompiler.tryParseInt(value)?.resValue?.apply {
155158
return data
156159
}
157160
}
@@ -169,14 +172,14 @@ fun parseBoolean(value: String, def: Boolean = false): Boolean {
169172
return def
170173
}
171174

172-
tryParseBool(value)?.resValue?.apply {
175+
com.android.aaptcompiler.tryParseBool(value)?.resValue?.apply {
173176
return data == -1
174177
}
175178

176179
if (value[0] == '@') {
177180
val resolver: (Value?) -> Boolean? =
178181
fun(resValue): Boolean {
179-
return if (resValue is BinaryPrimitive) {
182+
return if (resValue is com.android.aaptcompiler.BinaryPrimitive) {
180183
resValue.resValue.data == -1
181184
} else def
182185
}
@@ -190,7 +193,7 @@ fun parseBoolean(value: String, def: Boolean = false): Boolean {
190193
fun parseDrawable(context: Context, value: String, def: Drawable = unknownDrawable()): Drawable {
191194
val drawableResolver: (Value?) -> Drawable? =
192195
fun(it): Drawable? {
193-
if (it is FileReference) {
196+
if (it is com.android.aaptcompiler.FileReference) {
194197
val file = File(it.path.value())
195198
if (!file.exists() || file.extension != EXT_XML) {
196199
return null
@@ -229,7 +232,7 @@ fun parseLayoutReference(value: String): File? {
229232

230233
val layoutResolver: (Value?) -> File? =
231234
fun(it): File? {
232-
return if (it is FileReference) {
235+
return if (it is com.android.aaptcompiler.FileReference) {
233236
File(it.source.path)
234237
} else null
235238
}
@@ -310,7 +313,7 @@ fun parseDimension(
310313
return complexToDimension(data, displayMetrics)
311314
} else if (c == '@') {
312315
val resolver: (Value?) -> Float? = {
313-
if (it is BinaryPrimitive) {
316+
if (it is com.android.aaptcompiler.BinaryPrimitive) {
314317
complexToDimension(it.resValue.data, displayMetrics)
315318
} else null
316319
}
@@ -357,8 +360,8 @@ fun parseGravity(value: String, def: Int = defaultGravity()): Int {
357360
return parseFlag(attr = attr, value = value, def = def)
358361
}
359362

360-
fun parseFlag(attr: AttributeResource, value: String, def: Int = -1): Int {
361-
return tryParseFlagSymbol(attr, value)?.resValue?.data ?: def
363+
fun parseFlag(attr: com.android.aaptcompiler.AttributeResource, value: String, def: Int = -1): Int {
364+
return com.android.aaptcompiler.tryParseFlagSymbol(attr, value)?.resValue?.data ?: def
362365
}
363366

364367
fun defaultGravity(): Int {
@@ -441,15 +444,15 @@ fun <T> resolveQualifiedResourceReference(
441444
}
442445

443446
fun <T> resolveResourceReference(
444-
table: ResourceTable,
447+
table: IResourceTable,
445448
type: AaptResourceType,
446449
pck: String,
447450
name: String,
448451
def: T,
449452
resolver: (Value?) -> T?
450453
): T {
451454
val result =
452-
table.findResource(ResourceName(pck = pck, type = type, entry = name))
455+
table.findResource(com.android.aaptcompiler.ResourceName(pck = pck, type = type, entry = name))
453456
?: run { throw IllegalArgumentException("$type resource '$name' not found") }
454457
return resolveResourceReference(
455458
table,
@@ -463,9 +466,9 @@ fun <T> resolveResourceReference(
463466
}
464467

465468
fun <T> resolveResourceReference(
466-
table: ResourceTable,
467-
pck: ResourceTablePackage,
468-
entry: ResourceEntry,
469+
table: IResourceTable,
470+
pck: IResourceTablePackage,
471+
entry: IResourceEntry,
469472
type: AaptResourceType,
470473
name: String,
471474
def: T,

utilities/xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/utils/lookup.kt

+13-13
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ import com.android.SdkConstants
2121
import com.android.aaptcompiler.AaptResourceType
2222
import com.android.aaptcompiler.AttributeResource
2323
import com.android.aaptcompiler.ConfigDescription
24-
import com.android.aaptcompiler.ResourceEntry
25-
import com.android.aaptcompiler.ResourceGroup
2624
import com.android.aaptcompiler.ResourceName
27-
import com.android.aaptcompiler.ResourceTable
28-
import com.android.aaptcompiler.ResourceTablePackage
2925
import com.itsaky.androidide.inflater.utils.module
26+
import com.itsaky.androidide.xml.res.IResourceEntry
27+
import com.itsaky.androidide.xml.res.IResourceGroup
28+
import com.itsaky.androidide.xml.res.IResourceTable
29+
import com.itsaky.androidide.xml.res.IResourceTablePackage
3030
import org.slf4j.LoggerFactory
3131

3232
private val log = LoggerFactory.getLogger("ParseLookupUtils")
3333

3434
internal data class LookupResult(
35-
val table: ResourceTable,
36-
val group: ResourceGroup,
37-
val pack: ResourceTablePackage,
38-
val entry: ResourceEntry
35+
val table: IResourceTable,
36+
val group: IResourceGroup,
37+
val pack: IResourceTablePackage,
38+
val entry: IResourceEntry
3939
)
4040

4141
internal fun lookupUnqualifedResource(
@@ -57,10 +57,10 @@ internal fun lookupUnqualifedResource(
5757
}
5858

5959
internal fun findUnqualifiedResourceEntry(type: AaptResourceType, name: String): LookupResult? {
60-
var resTable: ResourceTable? = null
61-
var resGrp: ResourceGroup? = null
62-
var resPck: ResourceTablePackage? = null
63-
var resEntry: ResourceEntry? = null
60+
var resTable: IResourceTable? = null
61+
var resGrp: IResourceGroup? = null
62+
var resPck: IResourceTablePackage? = null
63+
var resEntry: IResourceEntry? = null
6464
for (t in module.getAllResourceTables()) {
6565
val entries =
6666
t.packages.mapNotNull {
@@ -93,7 +93,7 @@ internal fun findQualifedResourceEntry(
9393
pack: String,
9494
type: AaptResourceType,
9595
name: String
96-
): ResourceEntry? {
96+
): IResourceEntry? {
9797
return module
9898
.findResourceTableForPackage(pack, type)
9999
?.findResource(ResourceName(pack, type, name))

0 commit comments

Comments
 (0)