Search in sources :

Example 41 with FolderConfiguration

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;
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Example 42 with FolderConfiguration

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;
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) NotNull(org.jetbrains.annotations.NotNull)

Example 43 with FolderConfiguration

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;
}
Also used : DomElement(com.intellij.util.xml.DomElement) ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) PsiElement(com.intellij.psi.PsiElement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Document(org.w3c.dom.Document) Density(com.android.resources.Density) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceItem(com.android.ide.common.resources.ResourceItem) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) DensityQualifier(com.android.ide.common.resources.configuration.DensityQualifier) Nullable(org.jetbrains.annotations.Nullable)

Example 44 with FolderConfiguration

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;
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) Density(com.android.resources.Density) DensityQualifier(com.android.ide.common.resources.configuration.DensityQualifier) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with FolderConfiguration

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);
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Device(com.android.sdklib.devices.Device) State(com.android.sdklib.devices.State) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) IAndroidTarget(com.android.sdklib.IAndroidTarget) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) DensityQualifier(com.android.ide.common.resources.configuration.DensityQualifier)

Aggregations

FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)69 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 NotNull (org.jetbrains.annotations.NotNull)14 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)12 Nullable (org.jetbrains.annotations.Nullable)12 ResourceItem (com.android.ide.common.res2.ResourceItem)10 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)10 DensityQualifier (com.android.ide.common.resources.configuration.DensityQualifier)9 ResourceFolderType (com.android.resources.ResourceFolderType)9 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)7 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 Project (com.intellij.openapi.project.Project)7 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)6 ResourceResolver (com.android.ide.common.resources.ResourceResolver)6 LayoutDirectionQualifier (com.android.ide.common.resources.configuration.LayoutDirectionQualifier)6 ScreenOrientationQualifier (com.android.ide.common.resources.configuration.ScreenOrientationQualifier)6 ScreenSizeQualifier (com.android.ide.common.resources.configuration.ScreenSizeQualifier)6 SessionParams (com.android.ide.common.rendering.api.SessionParams)5 CountryCodeQualifier (com.android.ide.common.resources.configuration.CountryCodeQualifier)5