use of com.android.tools.build.bundletool.model.ResourceTableEntry in project bundletool by google.
the class ResourcesUtils method filterResourceTable.
/**
* Filters the given resource table according to the specified criteria.
*
* <p>If any of {@link Package}, {@link Type} or {@link Entry} is empty after the filtering, it
* gets removed from the table altogether.
*
* @param originalTable the original resource table
* @param removeEntryPredicate determines whether an entry should be completely removed with all
* their configurations
* @param configValuesFilterFn computes a new {@link Entry} with filtered {@link ConfigValue} list
* (possibly empty)
* @return filtered resource table
*/
public static ResourceTable filterResourceTable(ResourceTable originalTable, Predicate<ResourceTableEntry> removeEntryPredicate, Function<ResourceTableEntry, Entry> configValuesFilterFn) {
ResourceTable.Builder filteredTable = originalTable.toBuilder();
for (int pkgIdx = filteredTable.getPackageCount() - 1; pkgIdx >= 0; pkgIdx--) {
Package.Builder pkg = filteredTable.getPackageBuilder(pkgIdx);
for (int typeIdx = pkg.getTypeCount() - 1; typeIdx >= 0; typeIdx--) {
Type.Builder type = pkg.getTypeBuilder(typeIdx);
List<Entry> unfilteredEntries = type.getEntryList();
type.clearEntry();
for (Entry unfilteredEntry : unfilteredEntries) {
ResourceTableEntry entry = ResourceTableEntry.create(filteredTable.getPackage(pkgIdx), pkg.getType(typeIdx), unfilteredEntry);
if (removeEntryPredicate.test(entry)) {
continue;
}
Entry filteredEntry = configValuesFilterFn.apply(entry);
if (filteredEntry.getConfigValueCount() > 0) {
type.addEntry(filteredEntry);
}
}
if (type.getEntryCount() == 0) {
pkg.removeType(typeIdx);
}
}
if (pkg.getTypeCount() == 0) {
filteredTable.removePackage(pkgIdx);
}
}
return filteredTable.build();
}
use of com.android.tools.build.bundletool.model.ResourceTableEntry in project bundletool by google.
the class ResourcesUtilsTest method filter_withRemoveTypePredicate.
@Test
public void filter_withRemoveTypePredicate() throws Exception {
ResourceTable table = resourceTable(pkg(0x7f, "package.without.density.resources", type(0x01, "drawable", entry(0x00, "layout_main", fileReference("res/drawable/image.png", Configuration.getDefaultInstance()))), type(0x02, "layout", entry(0x00, "layout_menu", fileReference("res/layout/menu.xml", Configuration.getDefaultInstance())))));
ResourceTable filteredTable = ResourcesUtils.filterResourceTable(table, resource -> resource.getType().getTypeId().getId() == 0x02, ResourceTableEntry::getEntry);
assertThat(filteredTable).ignoringRepeatedFieldOrder().isEqualTo(resourceTable(pkg(0x7f, "package.without.density.resources", type(0x01, "drawable", entry(0x00, "layout_main", fileReference("res/drawable/image.png", Configuration.getDefaultInstance()))))));
}
use of com.android.tools.build.bundletool.model.ResourceTableEntry in project bundletool by google.
the class DumpManager method printResources.
void printResources(Predicate<ResourceTableEntry> resourcePredicate, boolean printValues) {
ImmutableList<ResourceTable> resourceTables;
try (ZipFile zipFile = new ZipFile(bundlePath.toFile())) {
resourceTables = ZipUtils.allFileEntriesPaths(zipFile).filter(path -> path.endsWith(SpecialModuleEntry.RESOURCE_TABLE.getPath())).map(path -> extractAndParse(zipFile, path, ResourceTable::parseFrom)).collect(toImmutableList());
} catch (IOException e) {
throw new UncheckedIOException("Error occurred when reading the bundle.", e);
}
ImmutableListMultimap<String, ResourceTableEntry> entriesByPackageName = resourceTables.stream().flatMap(ResourcesUtils::entries).filter(resourcePredicate).collect(groupingBySortedKeys(entry -> entry.getPackage().getPackageName()));
for (String packageName : entriesByPackageName.keySet()) {
printStream.printf("Package '%s':%n", packageName);
entriesByPackageName.get(packageName).forEach(entry -> printEntry(entry, printValues));
printStream.println();
}
}
use of com.android.tools.build.bundletool.model.ResourceTableEntry in project bundletool by google.
the class ArchivedApksGenerator method getArchivedResourceTable.
private Optional<ResourceTable> getArchivedResourceTable(AppBundle appBundle, BundleModule bundleModule, AndroidManifest archivedManifest) throws IOException {
if (!bundleModule.getResourceTable().isPresent()) {
return Optional.empty();
}
ImmutableSet<ResourceId> referredResources = new ResourceAnalyzer(appBundle).findAllAppResourcesReachableFromManifest(archivedManifest);
ResourceTable archivedResourceTable = ResourcesUtils.filterResourceTable(bundleModule.getResourceTable().get(), /* removeEntryPredicate= */
entry -> !referredResources.contains(entry.getResourceId()), /* configValuesFilterFn= */
ResourceTableEntry::getEntry);
return Optional.of(archivedResourceTable);
}
use of com.android.tools.build.bundletool.model.ResourceTableEntry in project bundletool by google.
the class DumpCommand method parseResourcePredicate.
private Predicate<ResourceTableEntry> parseResourcePredicate() {
if (getResourceId().isPresent()) {
return entry -> entry.getResourceId().getFullResourceId() == getResourceId().get().intValue();
}
if (getResourceName().isPresent()) {
String resourceName = getResourceName().get();
Matcher matcher = RESOURCE_NAME_PATTERN.matcher(resourceName);
if (!matcher.matches()) {
throw InvalidCommandException.builder().withInternalMessage("Resource name must match the format '<type>/<name>', e.g. 'drawable/icon'.").build();
}
return entry -> entry.getType().getName().equals(matcher.group("type")) && entry.getEntry().getName().equals(matcher.group("name"));
}
return entry -> true;
}
Aggregations