Search in sources :

Example 41 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class ThemeEditorUtils method getModuleThemeQualifiedNamesList.

/**
   * Returns the list of the qualified names of all the user-defined themes available from a given module
   */
@NotNull
public static ImmutableList<String> getModuleThemeQualifiedNamesList(@NotNull Module module) {
    AndroidFacet facet = AndroidFacet.getInstance(module);
    assert facet != null;
    ConfigurationManager manager = facet.getConfigurationManager();
    // We create a new ResourceResolverCache instead of using cache from myConfiguration to optimize memory instead of time/speed,
    // because we are about to create a lot of instances of ResourceResolver here that won't be used outside of this method
    final ResourceResolverCache resolverCache = new ResourceResolverCache(manager);
    final IAndroidTarget target = manager.getTarget();
    final Map<ResourceValue, Boolean> cache = new HashMap<ResourceValue, Boolean>();
    final Set<String> themeNamesSet = Sets.newTreeSet(String.CASE_INSENSITIVE_ORDER);
    ResourceFolderVisitor visitor = new ResourceFolderVisitor() {

        @Override
        public void visitResourceFolder(@NotNull LocalResourceRepository resources, String moduleName, @NotNull String variantName, boolean isSelected) {
            if (!isSelected) {
                return;
            }
            for (String simpleThemeName : resources.getItemsOfType(ResourceType.STYLE)) {
                String themeStyleResourceUrl = SdkConstants.STYLE_RESOURCE_PREFIX + simpleThemeName;
                List<ResourceItem> themeItems = resources.getResourceItem(ResourceType.STYLE, simpleThemeName);
                assert themeItems != null;
                for (ResourceItem themeItem : themeItems) {
                    ResourceResolver resolver = resolverCache.getResourceResolver(target, themeStyleResourceUrl, themeItem.getConfiguration());
                    ResourceValue themeItemResourceValue = themeItem.getResourceValue(false);
                    assert themeItemResourceValue != null;
                    if (resolver.isTheme(themeItemResourceValue, cache)) {
                        themeNamesSet.add(simpleThemeName);
                        break;
                    }
                }
            }
        }
    };
    acceptResourceResolverVisitor(facet, visitor);
    return ImmutableList.copyOf(themeNamesSet);
}
Also used : IAndroidTarget(com.android.sdklib.IAndroidTarget) NotNull(org.jetbrains.annotations.NotNull) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) ResourceResolverCache(com.android.tools.idea.configurations.ResourceResolverCache) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ResourceItem(com.android.ide.common.res2.ResourceItem) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class ThemeEditorUtils method getMinApiLevel.

public static int getMinApiLevel(@NotNull Module module) {
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null) {
        return 1;
    }
    AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(facet);
    return moduleInfo.getMinSdkVersion().getApiLevel();
}
Also used : AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 43 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class ThemeEditorUtils method acceptResourceResolverVisitor.

/**
   * Visits every ResourceFolderRepository. It visits every resource in order, meaning that the later calls may override resources from
   * previous ones.
   */
public static void acceptResourceResolverVisitor(@NotNull final AndroidFacet mainFacet, @NotNull final ResourceFolderVisitor visitor) {
    // Get all the dependencies of the module in reverse order (first one is the lowest priority one)
    List<AndroidFacet> dependencies = Lists.reverse(AndroidUtils.getAllAndroidDependencies(mainFacet.getModule(), true));
    // The order of iteration here is important since the resources from the mainFacet will override those in the dependencies.
    for (AndroidFacet dependency : Iterables.concat(dependencies, ImmutableList.of(mainFacet))) {
        AndroidModuleModel androidModel = AndroidModuleModel.get(dependency);
        if (androidModel == null) {
            // For non gradle module, get the main source provider
            SourceProvider provider = dependency.getMainSourceProvider();
            for (LocalResourceRepository resourceRepository : getResourceFolderRepositoriesFromSourceSet(dependency, provider)) {
                visitor.visitResourceFolder(resourceRepository, dependency.getName(), provider.getName(), true);
            }
        } else {
            // For gradle modules, get all source providers and go through them
            // We need to iterate the providers in the returned to make sure that they correctly override each other
            List<SourceProvider> activeProviders = androidModel.getActiveSourceProviders();
            for (SourceProvider provider : activeProviders) {
                for (LocalResourceRepository resourceRepository : getResourceFolderRepositoriesFromSourceSet(dependency, provider)) {
                    visitor.visitResourceFolder(resourceRepository, dependency.getName(), provider.getName(), true);
                }
            }
            // Not go through all the providers that are not in the activeProviders
            ImmutableSet<SourceProvider> selectedProviders = ImmutableSet.copyOf(activeProviders);
            for (SourceProvider provider : androidModel.getAllSourceProviders()) {
                if (!selectedProviders.contains(provider)) {
                    for (LocalResourceRepository resourceRepository : getResourceFolderRepositoriesFromSourceSet(dependency, provider)) {
                        visitor.visitResourceFolder(resourceRepository, dependency.getName(), provider.getName(), false);
                    }
                }
            }
        }
    }
}
Also used : AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) SourceProvider(com.android.builder.model.SourceProvider) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 44 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class DrawableRendererEditor method configureRenderTask.

@NotNull
public static RenderTask configureRenderTask(@NotNull final Module module, @NotNull final Configuration configuration) {
    AndroidFacet facet = AndroidFacet.getInstance(module);
    assert facet != null;
    final RenderService service = RenderService.get(facet);
    RenderTask task = service.createTask(null, configuration, DRAWABLE_RENDER_LOGGER, null);
    assert task != null;
    return task;
}
Also used : RenderTask(com.android.tools.idea.rendering.RenderTask) RenderService(com.android.tools.idea.rendering.RenderService) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 45 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class GradleBuildInvoker method findAndAddGradleBuildTasks.

private static void findAndAddGradleBuildTasks(@NotNull Module module, @NotNull BuildMode buildMode, @NotNull List<String> tasks, @NotNull TestCompileType testCompileType) {
    GradleFacet gradleFacet = GradleFacet.getInstance(module);
    if (gradleFacet == null) {
        return;
    }
    String gradlePath = gradleFacet.getConfiguration().GRADLE_PROJECT_PATH;
    if (isEmpty(gradlePath)) {
        // Gradle project path is never, ever null. If the path is empty, it shows as ":". We had reports of this happening. It is likely that
        // users manually added the Android-Gradle facet to a project. After all it is likely not to be a Gradle module. Better quit and not
        // build the module.
        String msg = String.format("Module '%1$s' does not have a Gradle path. It is likely that this module was manually added by the user.", module.getName());
        getLogger().info(msg);
        return;
    }
    AndroidFacet androidFacet = AndroidFacet.getInstance(module);
    if (androidFacet != null) {
        JpsAndroidModuleProperties properties = androidFacet.getProperties();
        AndroidModuleModel androidModel = AndroidModuleModel.get(module);
        switch(buildMode) {
            // Intentional fall-through.
            case CLEAN:
            case SOURCE_GEN:
                addAfterSyncTasks(tasks, gradlePath, properties);
                addAfterSyncTasksForTestArtifacts(tasks, gradlePath, testCompileType, androidModel);
                break;
            case ASSEMBLE:
                tasks.add(createBuildTask(gradlePath, properties.ASSEMBLE_TASK_NAME));
                // Add assemble tasks for tests.
                if (testCompileType != TestCompileType.NONE) {
                    for (BaseArtifact artifact : getArtifactsForTestCompileType(testCompileType, androidModel)) {
                        addTaskIfSpecified(tasks, gradlePath, artifact.getAssembleTaskName());
                    }
                }
                break;
            default:
                addAfterSyncTasks(tasks, gradlePath, properties);
                addAfterSyncTasksForTestArtifacts(tasks, gradlePath, testCompileType, androidModel);
                // no *.class files and would be just a waste of time.
                if (testCompileType != TestCompileType.UNIT_TESTS) {
                    addTaskIfSpecified(tasks, gradlePath, properties.COMPILE_JAVA_TASK_NAME);
                }
                // Add compile tasks for tests.
                for (BaseArtifact artifact : getArtifactsForTestCompileType(testCompileType, androidModel)) {
                    addTaskIfSpecified(tasks, gradlePath, artifact.getCompileTaskName());
                }
                break;
        }
    } else {
        JavaFacet javaFacet = JavaFacet.getInstance(module);
        if (javaFacet != null && javaFacet.getConfiguration().BUILDABLE) {
            String gradleTaskName = javaFacet.getGradleTaskName(buildMode);
            if (gradleTaskName != null) {
                tasks.add(createBuildTask(gradlePath, gradleTaskName));
            }
            if (testCompileType == TestCompileType.UNIT_TESTS) {
                tasks.add(createBuildTask(gradlePath, JavaFacet.TEST_CLASSES_TASK_NAME));
            }
        }
    }
}
Also used : JavaFacet(com.android.tools.idea.gradle.project.facet.java.JavaFacet) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) JpsAndroidModuleProperties(org.jetbrains.jps.android.model.impl.JpsAndroidModuleProperties) BaseArtifact(com.android.builder.model.BaseArtifact) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Aggregations

AndroidFacet (org.jetbrains.android.facet.AndroidFacet)299 Module (com.intellij.openapi.module.Module)122 VirtualFile (com.intellij.openapi.vfs.VirtualFile)73 NotNull (org.jetbrains.annotations.NotNull)61 Nullable (org.jetbrains.annotations.Nullable)51 Project (com.intellij.openapi.project.Project)39 File (java.io.File)29 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)28 PsiFile (com.intellij.psi.PsiFile)24 XmlFile (com.intellij.psi.xml.XmlFile)20 PsiElement (com.intellij.psi.PsiElement)17 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)16 XmlTag (com.intellij.psi.xml.XmlTag)16 ArrayList (java.util.ArrayList)16 Manifest (org.jetbrains.android.dom.manifest.Manifest)14 IAndroidTarget (com.android.sdklib.IAndroidTarget)13 ResourceFolderType (com.android.resources.ResourceFolderType)11 Configuration (com.android.tools.idea.configurations.Configuration)10 PsiClass (com.intellij.psi.PsiClass)10 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)10