use of com.android.ide.common.resources.configuration.FolderConfiguration in project bazel by bazelbuild.
the class ResourceConfigurationFilter method matches.
private boolean matches(String containingFolder) {
FolderConfiguration config = FolderConfiguration.getConfigForFolder(containingFolder);
if (config == null) {
ruleContext.ruleError("Resource folder '" + containingFolder + "' has invalid resource qualifiers");
return true;
}
// aapt explicitly ignores the version qualifier; duplicate this behavior here.
config.setVersionQualifier(VersionQualifier.getQualifier(""));
for (FolderConfiguration filter : filters) {
if (config.isMatchFor(filter)) {
return true;
}
}
return false;
}
use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.
the class ConfigurationManager method create.
/**
* Creates a new {@link Configuration} associated with this manager
* @return a new {@link Configuration}
*/
@NotNull
private Configuration create(@NotNull VirtualFile file) {
ConfigurationStateManager stateManager = getStateManager();
ConfigurationFileState fileState = stateManager.getConfigurationState(file);
assert file.getParent() != null : file;
FolderConfiguration config = FolderConfiguration.getConfigForFolder(file.getParent().getName());
if (config == null) {
config = new FolderConfiguration();
}
Configuration configuration = Configuration.create(this, file, fileState, config);
LocalResourceRepository resources = AppResourceRepository.getAppResources(myModule, true);
ConfigurationMatcher matcher = new ConfigurationMatcher(configuration, resources, file);
if (fileState != null) {
matcher.adaptConfigSelection(true);
} else {
matcher.findAndSetCompatibleConfig(false);
}
return configuration;
}
use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.
the class AndroidColorAnnotator method pickBitmapFromXml.
@Nullable
private static File pickBitmapFromXml(@NotNull File file, @NotNull ResourceResolver resourceResolver, @NotNull Project project, @NonNull AndroidFacet facet, @NonNull ResourceValue resourceValue) {
try {
String xml = Files.toString(file, Charsets.UTF_8);
Document document = XmlUtils.parseDocumentSilently(xml, true);
if (document != null && document.getDocumentElement() != null) {
Element root = document.getDocumentElement();
String tag = root.getTagName();
Element target = null;
String attribute = null;
if ("vector".equals(tag)) {
// Take a look and see if we have a bitmap we can fall back to
AppResourceRepository resourceRepository = AppResourceRepository.getAppResources(facet, true);
List<com.android.ide.common.res2.ResourceItem> items = resourceRepository.getResourceItem(resourceValue.getResourceType(), resourceValue.getName());
if (items != null) {
for (com.android.ide.common.res2.ResourceItem item : items) {
FolderConfiguration configuration = item.getConfiguration();
DensityQualifier densityQualifier = configuration.getDensityQualifier();
if (densityQualifier != null) {
Density density = densityQualifier.getValue();
if (density != null && density.isValidValueForDevice()) {
File bitmap = item.getFile();
if (bitmap != null && bitmap.isFile()) {
return bitmap;
}
}
}
}
}
// Vectors are handled in the icon cache
return file;
} else if ("bitmap".equals(tag) || "nine-patch".equals(tag)) {
target = root;
attribute = ATTR_SRC;
} else if ("selector".equals(tag) || "level-list".equals(tag) || "layer-list".equals(tag) || "transition".equals(tag)) {
NodeList children = root.getChildNodes();
for (int i = children.getLength() - 1; i >= 0; i--) {
Node item = children.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE && TAG_ITEM.equals(item.getNodeName())) {
target = (Element) item;
if (target.hasAttributeNS(ANDROID_URI, ATTR_DRAWABLE)) {
attribute = ATTR_DRAWABLE;
break;
}
}
}
} else if ("clip".equals(tag) || "inset".equals(tag) || "scale".equals(tag)) {
target = root;
attribute = ATTR_DRAWABLE;
} else {
// <shape> etc - no bitmap to be found
return null;
}
if (attribute != null && target.hasAttributeNS(ANDROID_URI, attribute)) {
String src = target.getAttributeNS(ANDROID_URI, attribute);
ResourceValue value = resourceResolver.findResValue(src, false);
if (value != null) {
return ResourceHelper.resolveDrawable(resourceResolver, value, project);
}
}
}
} catch (Throwable ignore) {
// Not logging for now; afraid to risk unexpected crashes in upcoming preview. TODO: Re-enable.
//Logger.getInstance(AndroidColorAnnotator.class).warn(String.format("Could not read/render icon image %1$s", file), e);
}
return null;
}
use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.
the class AndroidColorAnnotator method findSmallestDpiVersion.
@Nullable
private static File findSmallestDpiVersion(@NonNull File bitmap) {
File parentFile = bitmap.getParentFile();
if (parentFile == null) {
return null;
}
File resFolder = parentFile.getParentFile();
if (resFolder == null) {
return null;
}
String parentName = parentFile.getName();
FolderConfiguration config = FolderConfiguration.getConfigForFolder(parentName);
if (config == null) {
return null;
}
DensityQualifier qualifier = config.getDensityQualifier();
if (qualifier == null) {
return null;
}
Density density = qualifier.getValue();
if (density != null && density.isValidValueForDevice()) {
String fileName = bitmap.getName();
Density[] densities = Density.values();
// Iterate in reverse, since the Density enum is in descending order
for (int i = densities.length - 1; i >= 0; i--) {
Density d = densities[i];
if (d.isValidValueForDevice()) {
String folder = parentName.replace(density.getResourceValue(), d.getResourceValue());
bitmap = new File(resFolder, folder + File.separator + fileName);
if (bitmap.exists()) {
if (bitmap.length() > MAX_ICON_SIZE) {
// No point continuing the loop; the other densities will be too big too
return null;
}
return bitmap;
}
}
}
}
return null;
}
use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.
the class ConfigurationTest method test.
public void test() throws Exception {
final AndroidFacet facet = AndroidFacet.getInstance(myModule);
assertNotNull(facet);
ConfigurationManager manager = facet.getConfigurationManager();
assertNotNull(manager);
assertSame(manager, facet.getConfigurationManager());
Configuration configuration = Configuration.create(manager, null, new FolderConfiguration());
assertNotNull(configuration);
configuration.startBulkEditing();
configuration.setDisplayName("myconfig");
configuration.setTheme("@style/Theme1");
configuration.setNightMode(NightMode.NIGHT);
configuration.setActivity("tes.tpkg.MyActivity1");
configuration.setUiMode(UiMode.TELEVISION);
IAndroidTarget target = configuration.getConfigurationManager().getTarget();
Device device = configuration.getConfigurationManager().getDefaultDevice();
State deviceState = device != null ? device.getState("Portrait") : null;
if (target != null) {
configuration.setTarget(target);
}
if (device != null) {
configuration.setDevice(device, false);
assertNotNull(deviceState);
configuration.setDeviceState(deviceState);
}
configuration.setLocale(Locale.create("en-rUS"));
configuration.finishBulkEditing();
assertEquals("myconfig", configuration.getDisplayName());
assertEquals("@style/Theme1", configuration.getTheme());
assertEquals(NightMode.NIGHT, configuration.getNightMode());
assertEquals("tes.tpkg.MyActivity1", configuration.getActivity());
assertEquals(UiMode.TELEVISION, configuration.getUiMode());
assertEquals(Locale.create("en-rUS"), configuration.getLocale());
if (target != null) {
assertSame(target, configuration.getRealTarget());
}
if (device != null) {
assertSame(device, configuration.getDevice());
assertNotNull(deviceState);
assertSame(deviceState, configuration.getDeviceState());
}
FolderConfiguration fullConfig = configuration.getFullConfig();
LocaleQualifier languageQualifier = fullConfig.getLocaleQualifier();
String configDisplayString = fullConfig.toDisplayString();
assertNotNull(configDisplayString, languageQualifier);
assertEquals("en", languageQualifier.getLanguage());
String region = fullConfig.getLocaleQualifier().getRegion();
assertNotNull(configDisplayString, region);
assertEquals("US", region);
assertEquals(UiMode.TELEVISION, fullConfig.getUiModeQualifier().getValue());
assertEquals(NightMode.NIGHT, fullConfig.getNightModeQualifier().getValue());
assertEquals(ScreenOrientation.PORTRAIT, fullConfig.getScreenOrientationQualifier().getValue());
if (device != null) {
State landscape = device.getState("Landscape");
assertNotNull(landscape);
configuration.setDeviceState(landscape);
assertEquals(ScreenOrientation.LANDSCAPE, configuration.getFullConfig().getScreenOrientationQualifier().getValue());
}
Density density = configuration.getDensity();
assertEquals(Density.XHIGH, density);
DensityQualifier qualifier = new DensityQualifier().getNullQualifier();
configuration.getFullConfig().setDensityQualifier(qualifier);
density = configuration.getDensity();
assertEquals(Density.MEDIUM, density);
}
Aggregations