Search in sources :

Example 1 with ResourceVisibilityLookup

use of com.android.ide.common.repository.ResourceVisibilityLookup in project kotlin by JetBrains.

the class PrivateResourceDetector method getLibraryName.

/** Pick a suitable name to describe the library defining the private resource */
@Nullable
private static String getLibraryName(@NonNull Context context, @NonNull ResourceType type, @NonNull String name) {
    ResourceVisibilityLookup lookup = context.getProject().getResourceVisibility();
    AndroidLibrary library = lookup.getPrivateIn(type, name);
    if (library != null) {
        String libraryName = library.getProject();
        if (libraryName != null) {
            return libraryName;
        }
        MavenCoordinates coordinates = library.getResolvedCoordinates();
        if (coordinates != null) {
            return coordinates.getGroupId() + ':' + coordinates.getArtifactId();
        }
    }
    return "the library";
}
Also used : MavenCoordinates(com.android.builder.model.MavenCoordinates) AndroidLibrary(com.android.builder.model.AndroidLibrary) ResourceVisibilityLookup(com.android.ide.common.repository.ResourceVisibilityLookup) Nullable(com.android.annotations.Nullable)

Example 2 with ResourceVisibilityLookup

use of com.android.ide.common.repository.ResourceVisibilityLookup in project android by JetBrains.

the class AppResourceRepository method getResourceVisibility.

@NotNull
public ResourceVisibilityLookup getResourceVisibility(@NotNull AndroidFacet facet) {
    // TODO: b/23032391
    AndroidModuleModel androidModel = AndroidModuleModel.get(facet);
    if (androidModel != null) {
        ResourceVisibilityLookup.Provider provider = getResourceVisibilityProvider();
        if (provider != null) {
            AndroidProject androidProject = androidModel.getAndroidProject();
            Variant variant = androidModel.getSelectedVariant();
            return provider.get(androidProject, variant);
        }
    }
    return ResourceVisibilityLookup.NONE;
}
Also used : Variant(com.android.builder.model.Variant) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) ResourceVisibilityLookup(com.android.ide.common.repository.ResourceVisibilityLookup) AndroidProject(com.android.builder.model.AndroidProject) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ResourceVisibilityLookup

use of com.android.ide.common.repository.ResourceVisibilityLookup in project android by JetBrains.

the class AndroidJavaCompletionContributor method filterPrivateResources.

public void filterPrivateResources(@NotNull CompletionParameters parameters, @NotNull final CompletionResultSet resultSet, AndroidFacet facet) {
    final ResourceVisibilityLookup lookup = AppResourceRepository.getAppResources(facet, true).getResourceVisibility(facet);
    if (lookup.isEmpty()) {
        return;
    }
    resultSet.runRemainingContributors(parameters, new Consumer<CompletionResult>() {

        @Override
        public void consume(CompletionResult result) {
            final Object obj = result.getLookupElement().getObject();
            if (obj instanceof PsiField) {
                PsiField field = (PsiField) obj;
                PsiClass containingClass = field.getContainingClass();
                if (containingClass != null) {
                    PsiClass rClass = containingClass.getContainingClass();
                    if (rClass != null && rClass.getName().equals(R_CLASS)) {
                        ResourceType type = ResourceType.getEnum(containingClass.getName());
                        if (type != null && lookup.isPrivate(type, field.getName())) {
                            return;
                        }
                    }
                }
            }
            resultSet.passResult(result);
        }
    });
}
Also used : CompletionResult(com.intellij.codeInsight.completion.CompletionResult) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) ResourceType(com.android.resources.ResourceType) ResourceVisibilityLookup(com.android.ide.common.repository.ResourceVisibilityLookup)

Example 4 with ResourceVisibilityLookup

use of com.android.ide.common.repository.ResourceVisibilityLookup in project android by JetBrains.

the class ResourceHelper method getCompletionFromTypes.

/**
   * Returns the list of all resource names that can be used as a value for one of the {@link ResourceType} in completionTypes,
   * optionally sorting/not sorting the results.
   */
@NotNull
public static List<String> getCompletionFromTypes(@NotNull AndroidFacet facet, @NotNull EnumSet<ResourceType> completionTypes, boolean sort) {
    EnumSet<ResourceType> types = Sets.newEnumSet(completionTypes, ResourceType.class);
    // Use drawables for mipmaps
    if (types.contains(ResourceType.MIPMAP)) {
        types.add(ResourceType.DRAWABLE);
    } else if (types.contains(ResourceType.DRAWABLE)) {
        types.add(ResourceType.MIPMAP);
    }
    boolean completionTypesContainsColor = types.contains(ResourceType.COLOR);
    if (types.contains(ResourceType.DRAWABLE)) {
        // The Drawable type accepts colors as value but not color state lists.
        types.add(ResourceType.COLOR);
    }
    AppResourceRepository repository = AppResourceRepository.getAppResources(facet, true);
    ResourceVisibilityLookup lookup = repository.getResourceVisibility(facet);
    AndroidPlatform androidPlatform = AndroidPlatform.getInstance(facet.getModule());
    FrameworkResources frameworkResources = null;
    if (androidPlatform != null) {
        AndroidTargetData targetData = androidPlatform.getSdkData().getTargetData(androidPlatform.getTarget());
        try {
            frameworkResources = targetData.getFrameworkResources(true);
        } catch (IOException ignore) {
        }
    }
    List<String> resources = Lists.newArrayListWithCapacity(500);
    for (ResourceType type : types) {
        // If type == ResourceType.COLOR, we want to include file resources (i.e. color state lists) only in the case where
        // color was present in completionTypes, and not if we added it because of the presence of ResourceType.DRAWABLES.
        // For any other ResourceType, we always include file resources.
        boolean includeFileResources = (type != ResourceType.COLOR) || completionTypesContainsColor;
        if (frameworkResources != null) {
            addFrameworkItems(resources, type, includeFileResources, frameworkResources);
        }
        addProjectItems(resources, type, includeFileResources, repository, lookup);
    }
    if (sort) {
        Collections.sort(resources, ResourceHelper::compareResourceReferences);
    }
    return resources;
}
Also used : FrameworkResources(com.android.ide.common.resources.FrameworkResources) ResourceType(com.android.resources.ResourceType) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) ResourceVisibilityLookup(com.android.ide.common.repository.ResourceVisibilityLookup) IOException(java.io.IOException) AndroidTargetData(org.jetbrains.android.sdk.AndroidTargetData) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ResourceVisibilityLookup (com.android.ide.common.repository.ResourceVisibilityLookup)4 ResourceType (com.android.resources.ResourceType)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (com.android.annotations.Nullable)1 AndroidLibrary (com.android.builder.model.AndroidLibrary)1 AndroidProject (com.android.builder.model.AndroidProject)1 MavenCoordinates (com.android.builder.model.MavenCoordinates)1 Variant (com.android.builder.model.Variant)1 FrameworkResources (com.android.ide.common.resources.FrameworkResources)1 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 CompletionResult (com.intellij.codeInsight.completion.CompletionResult)1 PsiClass (com.intellij.psi.PsiClass)1 PsiField (com.intellij.psi.PsiField)1 IOException (java.io.IOException)1 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)1 AndroidTargetData (org.jetbrains.android.sdk.AndroidTargetData)1