Search in sources :

Example 11 with LocalResourceRepository

use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.

the class ResourceResolverCache method getResourceResolver.

@NotNull
public ResourceResolver getResourceResolver(@Nullable IAndroidTarget target, @NotNull String themeStyle, @NotNull final FolderConfiguration fullConfiguration) {
    // Are caches up to date?
    final LocalResourceRepository resources = AppResourceRepository.getAppResources(myManager.getModule(), true);
    assert resources != null;
    if (myCachedGeneration != resources.getModificationCount()) {
        myResolverMap.clear();
        myAppResourceMap.clear();
    }
    // Store the modification count as soon as possible. This ensures that if there is any modification of resources while the
    // resolver is being created, it will be cleared subsequently.
    myCachedGeneration = resources.getModificationCount();
    // When looking up the configured project and framework resources, the theme doesn't matter, so we look up only
    // by the configuration qualifiers; for example, here's a sample key:
    // -ldltr-sw384dp-w384dp-h640dp-normal-notlong-port-notnight-xhdpi-finger-keyssoft-nokeys-navhidden-nonav-1280x768-v17
    // Note that the target version is already baked in via the -v qualifier.
    //
    // However, the resource resolver also depends on the theme, so we use a more specific key for the resolver map than
    // for the configured resource maps, by prepending the theme name:
    // @style/MyTheme-ldltr-sw384dp-w384dp-h640dp-normal-notlong-port-notnight-xhdpi-finger-keyssoft-nokeys-navhidden-nonav-1280x768-v17
    String configurationKey = fullConfiguration.getUniqueKey();
    String resolverKey = themeStyle + configurationKey;
    ResourceResolver resolver = myResolverMap.get(resolverKey);
    if (resolver == null) {
        Map<ResourceType, ResourceValueMap> configuredAppRes;
        Map<ResourceType, ResourceValueMap> frameworkResources;
        // Framework resources
        if (target == null) {
            target = myManager.getTarget();
        }
        if (target == null) {
            frameworkResources = Collections.emptyMap();
        } else {
            ResourceRepository frameworkRes = getFrameworkResources(fullConfiguration, target);
            if (frameworkRes == null) {
                frameworkResources = Collections.emptyMap();
            } else {
                // get the framework resource values based on the current config
                frameworkResources = myFrameworkResourceMap.get(configurationKey);
                if (frameworkResources == null) {
                    frameworkResources = frameworkRes.getConfiguredResources(fullConfiguration);
                    // assets replaced the look for the same theme; that doesn't happen to the same extend for Holo)
                    if (target instanceof CompatibilityRenderTarget && target.getVersion().getApiLevel() == 8) {
                        IAndroidTarget realTarget = ((CompatibilityRenderTarget) target).getRealTarget();
                        if (realTarget != null) {
                            replaceDrawableBitmaps(frameworkResources, target, realTarget);
                        }
                    }
                    myFrameworkResourceMap.put(configurationKey, frameworkResources);
                }
            }
        }
        // App resources
        configuredAppRes = myAppResourceMap.get(configurationKey);
        if (configuredAppRes == null) {
            // get the project resource values based on the current config
            Application application = ApplicationManager.getApplication();
            configuredAppRes = application.runReadAction(new Computable<Map<ResourceType, ResourceValueMap>>() {

                @Override
                public Map<ResourceType, ResourceValueMap> compute() {
                    return resources.getConfiguredResources(fullConfiguration);
                }
            });
            myAppResourceMap.put(configurationKey, configuredAppRes);
        }
        // Resource Resolver
        assert themeStyle.startsWith(PREFIX_RESOURCE_REF) : themeStyle;
        boolean isProjectTheme = ResourceHelper.isProjectStyle(themeStyle);
        String themeName = ResourceHelper.styleToTheme(themeStyle);
        resolver = ResourceResolver.create(configuredAppRes, frameworkResources, themeName, isProjectTheme);
        if (target instanceof CompatibilityRenderTarget) {
            int apiLevel = target.getVersion().getFeatureLevel();
            if (apiLevel >= 21) {
                resolver.setDeviceDefaults("Material");
            } else if (apiLevel >= 14) {
                resolver.setDeviceDefaults("Holo");
            } else {
                resolver.setDeviceDefaults(ResourceResolver.LEGACY_THEME);
            }
        }
        myResolverMap.put(resolverKey, resolver);
    }
    return resolver;
}
Also used : ResourceType(com.android.resources.ResourceType) IAndroidTarget(com.android.sdklib.IAndroidTarget) ResourceValueMap(com.android.ide.common.resources.ResourceValueMap) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceResolver(com.android.ide.common.resources.ResourceResolver) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceRepository(com.android.ide.common.resources.ResourceRepository) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) Application(com.intellij.openapi.application.Application) Computable(com.intellij.openapi.util.Computable) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with LocalResourceRepository

use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.

the class DataBindingUtil method invalidateAllSources.

public static boolean invalidateAllSources(DataBindingProjectComponent component) {
    boolean invalidated = false;
    for (AndroidFacet facet : component.getDataBindingEnabledFacets()) {
        LocalResourceRepository moduleResources = facet.getModuleResources(true);
        Map<String, DataBindingInfo> dataBindingResourceFiles = moduleResources.getDataBindingResourceFiles();
        if (dataBindingResourceFiles == null) {
            continue;
        }
        for (DataBindingInfo info : dataBindingResourceFiles.values()) {
            PsiClass psiClass = info.getPsiClass();
            if (psiClass != null) {
                PsiFile containingFile = psiClass.getContainingFile();
                if (containingFile != null) {
                    containingFile.subtreeChanged();
                    invalidated = true;
                }
            }
        }
    }
    return invalidated;
}
Also used : LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) DataBindingInfo(com.android.tools.idea.res.DataBindingInfo) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 13 with LocalResourceRepository

use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.

the class DataBindingClassFinder method findClass.

@Nullable
@Override
public PsiClass findClass(@NotNull String qualifiedName, @NotNull GlobalSearchScope scope) {
    if (!isEnabled()) {
        return null;
    }
    for (AndroidFacet facet : myComponent.getDataBindingEnabledFacets()) {
        LocalResourceRepository moduleResources = facet.getModuleResources(true);
        Map<String, DataBindingInfo> dataBindingResourceFiles = moduleResources.getDataBindingResourceFiles();
        if (dataBindingResourceFiles == null) {
            continue;
        }
        DataBindingInfo dataBindingInfo = dataBindingResourceFiles.get(qualifiedName);
        if (dataBindingInfo == null) {
            continue;
        }
        VirtualFile file = dataBindingInfo.getPsiFile().getVirtualFile();
        if (file != null && scope.accept(file)) {
            return DataBindingUtil.getOrCreatePsiClass(dataBindingInfo);
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) DataBindingInfo(com.android.tools.idea.res.DataBindingInfo) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with LocalResourceRepository

use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.

the class LocaleMenuAction method getRelevantLocales.

/**
   * Like {@link ConfigurationManager#getLocales} but filters out locales not compatible
   * with language and region qualifiers in the current configuration's folder config
   *
   * @return the list of relevant locales in the project
   */
@NotNull
private List<Locale> getRelevantLocales() {
    List<Locale> locales = new ArrayList<Locale>();
    Configuration configuration = myRenderContext.getConfiguration();
    if (configuration == null) {
        return Collections.emptyList();
    }
    Module module = configuration.getConfigurationManager().getModule();
    LocaleQualifier specificLocale = configuration.getEditedConfig().getLocaleQualifier();
    // locale.
    if (specificLocale != null) {
        List<VirtualFile> variations = ResourceHelper.getResourceVariations(configuration.getFile(), false);
        for (VirtualFile variation : variations) {
            FolderConfiguration config = FolderConfiguration.getConfigForFolder(variation.getParent().getName());
            if (config != null && config.getLocaleQualifier() == null) {
                specificLocale = null;
                break;
            }
        }
    }
    LocalResourceRepository projectResources = ProjectResourceRepository.getProjectResources(module, true);
    Set<LocaleQualifier> languages = projectResources != null ? projectResources.getLocales() : Collections.<LocaleQualifier>emptySet();
    for (LocaleQualifier l : languages) {
        if (specificLocale != null && !specificLocale.isMatchFor(l)) {
            continue;
        }
        locales.add(Locale.create(l));
    }
    return locales;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ArrayList(java.util.ArrayList) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Module(com.intellij.openapi.module.Module) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with LocalResourceRepository

use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.

the class ThemeEditorStyle method getStyleResourceItems.

/**
   * Returns all the {@link ResourceItem} where this style is defined. This includes all the definitions in the
   * different resource folders.
   */
@NotNull
protected Collection<ResourceItem> getStyleResourceItems() {
    assert !isFramework();
    Collection<ResourceItem> resultItems;
    final Module module = myManager.getModule();
    if (isProjectStyle()) {
        AndroidFacet facet = AndroidFacet.getInstance(module);
        assert facet != null : module.getName() + " module doesn't have AndroidFacet";
        // We need to keep a Set of ResourceItems to override them. The key is the folder configuration + the name
        final HashMap<String, ResourceItem> resourceItems = Maps.newHashMap();
        ThemeEditorUtils.acceptResourceResolverVisitor(facet, new ThemeEditorUtils.ResourceFolderVisitor() {

            @Override
            public void visitResourceFolder(@NotNull LocalResourceRepository resources, String moduleName, @NotNull String variantName, boolean isSourceSelected) {
                if (!isSourceSelected) {
                    // TODO: Process all source sets
                    return;
                }
                List<ResourceItem> items = resources.getResourceItem(ResourceType.STYLE, myQualifiedName);
                if (items == null) {
                    return;
                }
                for (ResourceItem item : items) {
                    String key = item.getConfiguration().toShortDisplayString() + "/" + item.getName();
                    resourceItems.put(key, item);
                }
            }
        });
        resultItems = ImmutableList.copyOf(resourceItems.values());
    } else {
        LocalResourceRepository resourceRepository = AppResourceRepository.getAppResources(module, true);
        assert resourceRepository != null;
        List<ResourceItem> items = resourceRepository.getResourceItem(ResourceType.STYLE, myQualifiedName);
        if (items != null) {
            resultItems = items;
        } else {
            resultItems = Collections.emptyList();
        }
    }
    return resultItems;
}
Also used : AndroidFacet(org.jetbrains.android.facet.AndroidFacet) ThemeEditorUtils(com.android.tools.idea.editors.theme.ThemeEditorUtils) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ResourceItem(com.android.ide.common.res2.ResourceItem) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)25 NotNull (org.jetbrains.annotations.NotNull)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 Nullable (org.jetbrains.annotations.Nullable)6 ResourceItem (com.android.ide.common.res2.ResourceItem)5 Module (com.intellij.openapi.module.Module)5 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)5 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)4 DataBindingInfo (com.android.tools.idea.res.DataBindingInfo)4 ResourceRepository (com.android.ide.common.resources.ResourceRepository)3 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)3 Locale (com.android.tools.idea.rendering.Locale)3 XmlTag (com.intellij.psi.xml.XmlTag)3 ResourceResolver (com.android.ide.common.resources.ResourceResolver)2 ResourceType (com.android.resources.ResourceType)2 State (com.android.sdklib.devices.State)2 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 PsiFile (com.intellij.psi.PsiFile)2 ArrayList (java.util.ArrayList)2