Search in sources :

Example 16 with LocalResourceRepository

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

the class AndroidResourceReferenceBase method resolveInner.

@NotNull
private ResolveResult[] resolveInner() {
    final List<PsiElement> elements = new ArrayList<>();
    final boolean attrReference = myResourceValue.getPrefix() == '?';
    collectTargets(myFacet, myResourceValue, elements, attrReference);
    final List<ResolveResult> result = new ArrayList<>();
    if (elements.isEmpty() && myResourceValue.getResourceName() != null && !AndroidUtils.SYSTEM_RESOURCE_PACKAGE.equals(myResourceValue.getNamespace())) {
        // Dynamic items do not appear in the XML scanning file index; look for
        // these in the resource repositories.
        LocalResourceRepository resources = AppResourceRepository.getAppResources(myFacet.getModule(), true);
        ResourceType resourceType = myResourceValue.getType();
        if (resourceType != null && (resourceType != ResourceType.ATTR || attrReference)) {
            // If not, it could be some broken source, such as @android/test
            assert resources != null;
            List<ResourceItem> items = resources.getResourceItem(resourceType, myResourceValue.getResourceName());
            if (items != null && FolderTypeRelationship.getRelatedFolders(resourceType).contains(ResourceFolderType.VALUES)) {
                for (ResourceItem item : items) {
                    XmlTag tag = LocalResourceRepository.getItemTag(myFacet.getModule().getProject(), item);
                    if (tag != null) {
                        elements.add(tag);
                    } else if (item instanceof DynamicResourceValueItem) {
                        result.add(((DynamicResourceValueItem) item).createResolveResult());
                    }
                }
            }
        }
    }
    if (elements.size() > 1) {
        Collections.sort(elements, AndroidResourceUtil.RESOURCE_ELEMENT_COMPARATOR);
    }
    for (PsiElement target : elements) {
        result.add(new PsiElementResolveResult(target));
    }
    return result.toArray(new ResolveResult[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) DynamicResourceValueItem(com.android.tools.idea.res.DynamicResourceValueItem) ResourceType(com.android.resources.ResourceType) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with LocalResourceRepository

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

the class ConfigurationMatcher method getBetterMatch.

/**
   * Returns a different file which is a better match for the given device, orientation, target version, etc
   * than the current one. You can supply {@code null} for all parameters; in that case, the current value
   * in the configuration is used.
   */
@Nullable
public static VirtualFile getBetterMatch(@NotNull Configuration configuration, @Nullable Device device, @Nullable String stateName, @Nullable Locale locale, @Nullable IAndroidTarget target) {
    VirtualFile file = configuration.getFile();
    Module module = configuration.getModule();
    if (file != null && module != null) {
        if (device == null) {
            device = configuration.getDevice();
        }
        if (stateName == null) {
            State deviceState = configuration.getDeviceState();
            stateName = deviceState != null ? deviceState.getName() : null;
        }
        State selectedState = ConfigurationFileState.getState(device, stateName);
        if (selectedState == null) {
            // Invalid state name passed in for the current device
            return null;
        }
        if (locale == null) {
            locale = configuration.getLocale();
        }
        if (target == null) {
            target = configuration.getTarget();
        }
        FolderConfiguration currentConfig = Configuration.getFolderConfig(module, selectedState, locale, target);
        if (currentConfig != null) {
            LocalResourceRepository resources = AppResourceRepository.getAppResources(module, true);
            if (resources != null) {
                ResourceFolderType folderType = ResourceHelper.getFolderType(file);
                if (folderType != null) {
                    List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
                    if (!types.isEmpty()) {
                        ResourceType type = types.get(0);
                        List<VirtualFile> matches = resources.getMatchingFiles(file, type, currentConfig);
                        if (!matches.contains(file) && !matches.isEmpty()) {
                            return matches.get(0);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) State(com.android.sdklib.devices.State) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with LocalResourceRepository

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

the class Configuration method computeBestDevice.

@Nullable
private Device computeBestDevice() {
    for (Device device : myManager.getRecentDevices()) {
        String stateName = myStateName;
        if (stateName == null) {
            stateName = device.getDefaultState().getName();
        }
        State selectedState = ConfigurationFileState.getState(device, stateName);
        Module module = myManager.getModule();
        FolderConfiguration currentConfig = getFolderConfig(module, selectedState, getLocale(), getTarget());
        if (currentConfig != null) {
            if (myEditedConfig.isMatchFor(currentConfig)) {
                LocalResourceRepository resources = AppResourceRepository.getAppResources(module, true);
                if (resources != null && myFile != null) {
                    ResourceFolderType folderType = ResourceHelper.getFolderType(myFile);
                    if (folderType != null) {
                        if (ResourceFolderType.VALUES.equals(folderType)) {
                            // If it's a file in the values folder, ResourceRepository.getMatchingFiles won't work.
                            // We get instead all the available folders and check that there is one compatible.
                            LocalResourceManager resourceManager = LocalResourceManager.getInstance(module);
                            if (resourceManager != null) {
                                for (PsiFile resourceFile : resourceManager.findResourceFiles("values")) {
                                    if (myFile.equals(resourceFile.getVirtualFile()) && resourceFile.getParent() != null) {
                                        FolderConfiguration folderConfiguration = FolderConfiguration.getConfigForFolder(resourceFile.getParent().getName());
                                        if (currentConfig.isMatchFor(folderConfiguration)) {
                                            return device;
                                        }
                                    }
                                }
                            }
                        } else {
                            List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
                            if (!types.isEmpty()) {
                                ResourceType type = types.get(0);
                                List<VirtualFile> matches = resources.getMatchingFiles(myFile, type, currentConfig);
                                if (matches.contains(myFile)) {
                                    return device;
                                }
                            }
                        }
                    } else if ("Kotlin".equals(myFile.getFileType().getName())) {
                        return device;
                    } else if (myFile.equals(myManager.getProject().getProjectFile())) {
                        // takes care of correct device selection for Theme Editor
                        return device;
                    }
                }
            }
        }
    }
    return myManager.getDefaultDevice();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) Device(com.android.sdklib.devices.Device) State(com.android.sdklib.devices.State) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with LocalResourceRepository

use of com.android.tools.idea.res.LocalResourceRepository 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 20 with LocalResourceRepository

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

the class AndroidColorAnnotator method findResourceValue.

/** Looks up the resource item of the given type and name for the given configuration, if any */
@Nullable
private static ResourceValue findResourceValue(ResourceType type, String name, boolean isFramework, Module module, Configuration configuration) {
    if (isFramework) {
        ResourceRepository frameworkResources = configuration.getFrameworkResources();
        if (frameworkResources == null) {
            return null;
        }
        if (!frameworkResources.hasResourceItem(type, name)) {
            return null;
        }
        ResourceItem item = frameworkResources.getResourceItem(type, name);
        return item.getResourceValue(type, configuration.getFullConfig(), false);
    } else {
        LocalResourceRepository appResources = AppResourceRepository.getAppResources(module, true);
        if (appResources == null) {
            return null;
        }
        if (!appResources.hasResourceItem(type, name)) {
            return null;
        }
        return appResources.getConfiguredValue(type, name, configuration.getFullConfig());
    }
}
Also used : LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceRepository(com.android.ide.common.resources.ResourceRepository) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceItem(com.android.ide.common.resources.ResourceItem) Nullable(org.jetbrains.annotations.Nullable)

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