Search in sources :

Example 36 with AndroidFacet

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

the class AndroidResourceUtil method findResourceFieldsForValueResource.

@NotNull
public static PsiField[] findResourceFieldsForValueResource(XmlTag tag, boolean onlyInOwnPackages) {
    final AndroidFacet facet = AndroidFacet.getInstance(tag);
    if (facet == null) {
        return PsiField.EMPTY_ARRAY;
    }
    ResourceFolderType fileResType = ResourceHelper.getFolderType(tag.getContainingFile());
    final String resourceType = fileResType == ResourceFolderType.VALUES ? getResourceTypeByValueResourceTag(tag) : null;
    if (resourceType == null) {
        return PsiField.EMPTY_ARRAY;
    }
    String name = tag.getAttributeValue(ATTR_NAME);
    if (name == null) {
        return PsiField.EMPTY_ARRAY;
    }
    return findResourceFields(facet, resourceType, name, onlyInOwnPackages);
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with AndroidFacet

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

the class AndroidResourceUtil method manifestPackageForModule.

@Nullable
private static String manifestPackageForModule(@NotNull Module module) {
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null) {
        return null;
    }
    Manifest manifest = facet.getManifest();
    if (manifest == null) {
        return null;
    }
    return manifest.getPackage().getValue();
}
Also used : Manifest(org.jetbrains.android.dom.manifest.Manifest) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with AndroidFacet

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

the class ModuleClassLoader method getExternalJars.

@Override
protected List<URL> getExternalJars() {
    final Module module = myModuleReference.get();
    if (module == null) {
        return Collections.emptyList();
    }
    final List<URL> result = new ArrayList<>();
    if (ThemeEditorProvider.THEME_EDITOR_ENABLE) {
        URL customWidgetsUrl = ThemeEditorUtils.getCustomWidgetsJarUrl();
        if (customWidgetsUrl != null) {
            result.add(customWidgetsUrl);
        }
    }
    List<VirtualFile> externalLibraries;
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet != null && facet.requiresAndroidModel() && facet.getAndroidModel() != null) {
        AndroidModel androidModel = facet.getAndroidModel();
        externalLibraries = androidModel.getClassJarProvider().getModuleExternalLibraries(module);
    } else {
        externalLibraries = AndroidRootUtil.getExternalLibraries(module);
    }
    for (VirtualFile libFile : externalLibraries) {
        if (EXT_JAR.equals(libFile.getExtension())) {
            final File file = new File(libFile.getPath());
            if (file.exists()) {
                try {
                    result.add(SdkUtils.fileToUrl(file));
                    File aarDir = file.getParentFile();
                    if (aarDir != null && (aarDir.getPath().endsWith(DOT_AAR) || aarDir.getPath().contains(EXPLODED_AAR))) {
                        if (aarDir.getPath().contains(EXPLODED_AAR)) {
                            if (aarDir.getPath().endsWith(LIBS_FOLDER)) {
                                // Some libraries recently started packaging jars inside a sub libs folder inside jars
                                aarDir = aarDir.getParentFile();
                            }
                            // Gradle plugin version 1.2.x and later has classes in aar-dir/jars/
                            if (aarDir.getPath().endsWith(FD_JARS)) {
                                aarDir = aarDir.getParentFile();
                            }
                        }
                        AppResourceRepository appResources = AppResourceRepository.getAppResources(module, true);
                        if (appResources != null) {
                            ResourceClassRegistry.get(module.getProject()).addAarLibrary(appResources, aarDir);
                        }
                    } else if (aarDir != null) {
                        // Build cache? We need to compute the package name in a slightly different way
                        File parentFile = aarDir.getParentFile();
                        if (parentFile != null) {
                            File manifest = new File(parentFile, ANDROID_MANIFEST_XML);
                            if (manifest.exists()) {
                                AppResourceRepository appResources = AppResourceRepository.getAppResources(module, true);
                                if (appResources != null) {
                                    FileResourceRepository repository = appResources.findRepositoryFor(parentFile);
                                    if (repository != null) {
                                        ResourceClassRegistry registry = ResourceClassRegistry.get(module.getProject());
                                        registry.addLibrary(appResources, registry.getAarPackage(parentFile));
                                    }
                                }
                            }
                        }
                    }
                } catch (MalformedURLException e) {
                    LOG.error(e);
                }
            }
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MalformedURLException(java.net.MalformedURLException) FileResourceRepository(com.android.tools.idea.res.FileResourceRepository) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceClassRegistry(com.android.tools.idea.res.ResourceClassRegistry) URL(java.net.URL) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) AndroidModel(com.android.tools.idea.model.AndroidModel) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Example 39 with AndroidFacet

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

the class AndroidGradleTestCase method loadProject.

protected void loadProject(@NotNull String relativePath, @Nullable GradleSyncListener listener, @Nullable String chosenModuleName) throws Exception {
    prepareProjectForImport(relativePath);
    Project project = getProject();
    File projectRoot = virtualToIoFile(project.getBaseDir());
    importProject(project.getName(), projectRoot, listener);
    assertTrue(AndroidProjectInfo.getInstance(project).requiresAndroidModel());
    assertFalse(isLegacyIdeaAndroidProject(project));
    ModuleManager moduleManager = ModuleManager.getInstance(project);
    Module[] modules = moduleManager.getModules();
    // if module name is specified, find it
    if (chosenModuleName != null) {
        for (Module module : modules) {
            if (chosenModuleName.equals(module.getName())) {
                myAndroidFacet = AndroidFacet.getInstance(module);
                break;
            }
        }
    }
    if (myAndroidFacet == null) {
        // then try and find a non-lib facet
        for (Module module : modules) {
            AndroidFacet androidFacet = AndroidFacet.getInstance(module);
            if (androidFacet != null && androidFacet.isAppProject()) {
                myAndroidFacet = androidFacet;
                break;
            }
        }
    }
    // then try and find ANY android facet
    if (myAndroidFacet == null) {
        for (Module module : modules) {
            myAndroidFacet = AndroidFacet.getInstance(module);
            if (myAndroidFacet != null) {
                break;
            }
        }
    }
    refreshProjectFiles();
}
Also used : Projects.isLegacyIdeaAndroidProject(com.android.tools.idea.gradle.util.Projects.isLegacyIdeaAndroidProject) Project(com.intellij.openapi.project.Project) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) TestUtils.getWorkspaceFile(com.android.testutils.TestUtils.getWorkspaceFile) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 40 with AndroidFacet

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

the class ThemeEditorUtils method showCreateNewStyleDialog.

/**
   * Creates a new style by displaying the dialog of the {@link NewStyleDialog}.
   * @param defaultParentStyle is used in NewStyleDialog, will be preselected in the parent text field and name will be suggested based on it
   * @param themeEditorContext  current theme editor context
   * @param isTheme whether theme or style will be created
   * @param message is used in NewStyleDialog to display message to user
   * @return the new style name or null if the style wasn't created
   */
@Nullable
public static String showCreateNewStyleDialog(@Nullable ConfiguredThemeEditorStyle defaultParentStyle, @NotNull final ThemeEditorContext themeEditorContext, boolean isTheme, boolean enableParentChoice, @Nullable final String message, @Nullable ThemeSelectionPanel.ThemeChangedListener themeChangedListener) {
    // if isTheme is true, defaultParentStyle shouldn't be null
    String defaultParentStyleName = null;
    if (isTheme && defaultParentStyle == null) {
        ImmutableList<String> defaultThemes = getDefaultThemeNames(themeEditorContext.getThemeResolver());
        defaultParentStyleName = !defaultThemes.isEmpty() ? defaultThemes.get(0) : null;
    } else if (defaultParentStyle != null) {
        defaultParentStyleName = defaultParentStyle.getQualifiedName();
    }
    final NewStyleDialog dialog = new NewStyleDialog(isTheme, themeEditorContext, defaultParentStyleName, (defaultParentStyle == null) ? null : defaultParentStyle.getName(), message);
    dialog.enableParentChoice(enableParentChoice);
    if (themeChangedListener != null) {
        dialog.setThemeChangedListener(themeChangedListener);
    }
    boolean createStyle = dialog.showAndGet();
    if (!createStyle) {
        return null;
    }
    int minModuleApi = getMinApiLevel(themeEditorContext.getCurrentContextModule());
    int minAcceptableApi = ResolutionUtils.getOriginalApiLevel(ResolutionUtils.getStyleResourceUrl(dialog.getStyleParentName()), themeEditorContext.getProject());
    final String fileName = AndroidResourceUtil.getDefaultResourceFileName(ResourceType.STYLE);
    FolderConfiguration config = new FolderConfiguration();
    if (minModuleApi < minAcceptableApi) {
        VersionQualifier qualifier = new VersionQualifier(minAcceptableApi);
        config.setVersionQualifier(qualifier);
    }
    if (fileName == null) {
        LOG.error("Couldn't find a default filename for ResourceType.STYLE");
        return null;
    }
    final List<String> dirNames = Collections.singletonList(config.getFolderName(ResourceFolderType.VALUES));
    String parentStyleName = dialog.getStyleParentName();
    Module module = themeEditorContext.getCurrentContextModule();
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null) {
        LOG.error("Create new style for non-Android module " + module.getName());
        return null;
    }
    Project project = module.getProject();
    VirtualFile resourceDir = facet.getPrimaryResourceDir();
    if (resourceDir == null) {
        AndroidUtils.reportError(project, AndroidBundle.message("check.resource.dir.error", module.getName()));
        return null;
    }
    boolean isCreated = createNewStyle(project, resourceDir, dialog.getStyleName(), parentStyleName, fileName, dirNames);
    return isCreated ? dialog.getStyleName() : null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

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