Search in sources :

Example 1 with IdeaSourceProvider

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

the class FmGetAppManifestDirMethod method exec.

@Override
public Object exec(List arguments) throws TemplateModelException {
    Module module = findAppModuleIfAny();
    if (module == null) {
        return null;
    }
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null) {
        return null;
    }
    IdeaSourceProvider provider = facet.getMainIdeaSourceProvider();
    VirtualFile file = provider.getManifestFile();
    if (file == null) {
        return null;
    }
    return file.getParent().getCanonicalPath();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 2 with IdeaSourceProvider

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

the class ManifestPanel method getErrorRemoveHtml.

@NotNull
private static String getErrorRemoveHtml(@NotNull final AndroidFacet facet, @NotNull String message, @NotNull final SourceFilePosition position, @NotNull HtmlLinkManager htmlLinkManager, @Nullable final VirtualFile currentlyOpenFile) {
    /*
    Example Input:
    ERROR Overlay manifest:package attribute declared at AndroidManifest.xml:3:5-49
    value=(com.foo.manifestapplication.debug) has a different value=(com.foo.manifestapplication)
    declared in main manifest at AndroidManifest.xml:5:5-43 Suggestion: remove the overlay
    declaration at AndroidManifest.xml and place it in the build.gradle: flavorName
    { applicationId = "com.foo.manifestapplication.debug" } AndroidManifest.xml (debug)
     */
    HtmlBuilder sb = new HtmlBuilder();
    int start = message.indexOf('{');
    int end = message.indexOf('}', start + 1);
    final String declaration = message.substring(start + 1, end).trim();
    if (!declaration.startsWith("applicationId")) {
        throw new IllegalArgumentException("unexpected remove suggestion format " + message);
    }
    final GradleBuildFile buildFile = GradleBuildFile.get(facet.getModule());
    Runnable link = null;
    if (buildFile != null) {
        final String applicationId = declaration.substring(declaration.indexOf('"') + 1, declaration.lastIndexOf('"'));
        final File manifestOverlayFile = position.getFile().getSourceFile();
        assert manifestOverlayFile != null;
        VirtualFile manifestOverlayVirtualFile = LocalFileSystem.getInstance().findFileByIoFile(manifestOverlayFile);
        assert manifestOverlayVirtualFile != null;
        IdeaSourceProvider sourceProvider = ManifestUtils.findManifestSourceProvider(facet, manifestOverlayVirtualFile);
        assert sourceProvider != null;
        final String name = sourceProvider.getName();
        AndroidModuleModel androidModuleModel = AndroidModuleModel.get(facet.getModule());
        assert androidModuleModel != null;
        final XmlFile manifestOverlayPsiFile = (XmlFile) PsiManager.getInstance(facet.getModule().getProject()).findFile(manifestOverlayVirtualFile);
        assert manifestOverlayPsiFile != null;
        if (androidModuleModel.getBuildTypeNames().contains(name)) {
            final String packageName = MergedManifest.get(facet).getPackage();
            assert packageName != null;
            if (applicationId.startsWith(packageName)) {
                link = () -> new WriteCommandAction.Simple(facet.getModule().getProject(), "Apply manifest suggestion", buildFile.getPsiFile(), manifestOverlayPsiFile) {

                    @Override
                    protected void run() throws Throwable {
                        if (currentlyOpenFile != null) {
                            // We mark this action as affecting the currently open file, so the Undo is available in this editor
                            CommandProcessor.getInstance().addAffectedFiles(facet.getModule().getProject(), currentlyOpenFile);
                        }
                        removePackageAttribute(manifestOverlayPsiFile);
                        final String applicationIdSuffix = applicationId.substring(packageName.length());
                        @SuppressWarnings("unchecked") List<NamedObject> buildTypes = (List<NamedObject>) buildFile.getValue(BuildFileKey.BUILD_TYPES);
                        if (buildTypes == null) {
                            buildTypes = new ArrayList<>();
                        }
                        NamedObject buildType = find(buildTypes, name);
                        if (buildType == null) {
                            buildType = new NamedObject(name);
                            buildTypes.add(buildType);
                        }
                        buildType.setValue(BuildFileKey.APPLICATION_ID_SUFFIX, applicationIdSuffix);
                        buildFile.setValue(BuildFileKey.BUILD_TYPES, buildTypes);
                        GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(facet.getModule().getProject(), null);
                    }
                }.execute();
            }
        } else if (androidModuleModel.getProductFlavorNames().contains(name)) {
            link = () -> new WriteCommandAction.Simple(facet.getModule().getProject(), "Apply manifest suggestion", buildFile.getPsiFile(), manifestOverlayPsiFile) {

                @Override
                protected void run() throws Throwable {
                    if (currentlyOpenFile != null) {
                        // We mark this action as affecting the currently open file, so the Undo is available in this editor
                        CommandProcessor.getInstance().addAffectedFiles(facet.getModule().getProject(), currentlyOpenFile);
                    }
                    removePackageAttribute(manifestOverlayPsiFile);
                    @SuppressWarnings("unchecked") List<NamedObject> flavors = (List<NamedObject>) buildFile.getValue(BuildFileKey.FLAVORS);
                    assert flavors != null;
                    NamedObject flavor = find(flavors, name);
                    assert flavor != null;
                    flavor.setValue(BuildFileKey.APPLICATION_ID, applicationId);
                    buildFile.setValue(BuildFileKey.FLAVORS, flavors);
                    GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(facet.getModule().getProject(), null);
                }
            }.execute();
        }
    }
    if (link != null) {
        sb.addLink(message.substring(0, end + 1), htmlLinkManager.createRunnableLink(link));
        sb.add(message.substring(end + 1));
    } else {
        sb.add(message);
    }
    return sb.getHtml();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) XmlFile(com.intellij.psi.xml.XmlFile) HtmlBuilder(com.android.utils.HtmlBuilder) NamedObject(com.android.tools.idea.gradle.parser.NamedObject) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) List(java.util.List) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFile(com.android.ide.common.blame.SourceFile) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with IdeaSourceProvider

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

the class AndroidManifestFileNode method update.

@Override
public void update(PresentationData data) {
    super.update(data);
    PsiFile file = getValue();
    // could be null if the file was deleted
    if (file == null) {
        return;
    }
    // if it is not part of the main source set, then append the provider name
    IdeaSourceProvider sourceProvider = getSourceProvider(myFacet, file);
    if (sourceProvider != null && !SdkConstants.FD_MAIN.equals(sourceProvider.getName())) {
        data.addText(file.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
        data.addText(" (" + sourceProvider.getName() + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
        data.setPresentableText(file.getName());
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider)

Example 4 with IdeaSourceProvider

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

the class ManifestPanel method describePosition.

private void describePosition(@NotNull HtmlBuilder sb, @NotNull AndroidFacet facet, @NotNull SourceFilePosition sourceFilePosition) {
    SourceFile sourceFile = sourceFilePosition.getFile();
    SourcePosition sourcePosition = sourceFilePosition.getPosition();
    File file = sourceFile.getSourceFile();
    if (file == GRADLE_MODEL_MARKER_FILE) {
        VirtualFile gradleBuildFile = GradleUtil.getGradleBuildFile(facet.getModule());
        if (gradleBuildFile != null) {
            file = VfsUtilCore.virtualToIoFile(gradleBuildFile);
            sb.addHtml("<a href=\"");
            sb.add(file.toURI().toString());
            sb.addHtml("\">");
            sb.add(file.getName());
            sb.addHtml("</a>");
            sb.add(" injection");
        } else {
            sb.add("build.gradle injection (source location unknown)");
        }
        return;
    }
    AndroidLibrary library;
    if (file != null) {
        String source = null;
        Module libraryModule = null;
        Module[] modules = ModuleManager.getInstance(facet.getModule().getProject()).getModules();
        VirtualFile vFile = LocalFileSystem.getInstance().findFileByIoFile(file);
        if (vFile != null) {
            Module module = ModuleUtilCore.findModuleForFile(vFile, facet.getModule().getProject());
            if (module != null) {
                if (modules.length >= 2) {
                    source = module.getName();
                }
                // AAR Library?
                if (file.getPath().contains(EXPLODED_AAR)) {
                    AndroidModuleModel androidModel = AndroidModuleModel.get(module);
                    if (androidModel != null) {
                        library = GradleUtil.findLibrary(file.getParentFile(), androidModel.getSelectedVariant(), androidModel.getModelVersion());
                        if (library != null) {
                            if (library.getProject() != null) {
                                libraryModule = GradleUtil.findModuleByGradlePath(facet.getModule().getProject(), library.getProject());
                                if (libraryModule != null) {
                                    module = libraryModule;
                                    source = module.getName();
                                } else {
                                    source = library.getProject();
                                    source = StringUtil.trimStart(source, ":");
                                }
                            } else {
                                MavenCoordinates coordinates = library.getResolvedCoordinates();
                                source = /*coordinates.getGroupId() + ":" +*/
                                coordinates.getArtifactId() + ":" + coordinates.getVersion();
                            }
                        }
                    }
                }
            }
            IdeaSourceProvider provider = ManifestUtils.findManifestSourceProvider(facet, vFile);
            if (provider != null) /*&& !provider.equals(facet.getMainIdeaSourceProvider())*/
            {
                String providerName = provider.getName();
                if (source == null) {
                    source = providerName;
                } else {
                    // "the app main manifest" - "app" is the module name, "main" is the source provider name
                    source = source + " " + providerName;
                }
            }
        }
        if (source == null) {
            source = file.getName();
            if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
                source += ":" + String.valueOf(sourcePosition);
            }
        }
        sb.addHtml("<a href=\"");
        boolean redirected = false;
        if (libraryModule != null) {
            AndroidFacet libraryFacet = AndroidFacet.getInstance(libraryModule);
            if (libraryFacet != null) {
                File manifestFile = libraryFacet.getMainSourceProvider().getManifestFile();
                if (manifestFile.exists()) {
                    sb.add(manifestFile.toURI().toString());
                    redirected = true;
                    // Line numbers probably aren't right
                    sourcePosition = SourcePosition.UNKNOWN;
                // TODO: Set URL which points to the element/attribute path
                }
            }
        }
        if (!redirected) {
            sb.add(file.toURI().toString());
            if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
                sb.add(":");
                sb.add(String.valueOf(sourcePosition.getStartLine()));
                sb.add(":");
                sb.add(String.valueOf(sourcePosition.getStartColumn()));
            }
        }
        sb.addHtml("\">");
        sb.add(source);
        sb.addHtml("</a>");
        sb.add(" manifest");
        if (FileUtil.filesEqual(file, VfsUtilCore.virtualToIoFile(myFile))) {
            sb.add(" (this file)");
        }
        if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
            sb.add(", line ");
            sb.add(Integer.toString(sourcePosition.getStartLine()));
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) MavenCoordinates(com.android.builder.model.MavenCoordinates) AndroidLibrary(com.android.builder.model.AndroidLibrary) SourcePosition(com.android.ide.common.blame.SourcePosition) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) SourceFile(com.android.ide.common.blame.SourceFile) Module(com.intellij.openapi.module.Module) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFile(com.android.ide.common.blame.SourceFile) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File)

Example 5 with IdeaSourceProvider

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

the class CreateMissingClassQuickFix method applyFix.

@Override
public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiPackage aPackage = myPackage.getElement();
    if (aPackage == null) {
        return;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(myModule);
    if (facet == null) {
        return;
    }
    final List<IdeaSourceProvider> providerList = IdeaSourceProvider.getCurrentSourceProviders(facet);
    final List<VirtualFile> javaDirectories = Lists.newArrayList();
    for (IdeaSourceProvider provider : providerList) {
        javaDirectories.addAll(provider.getJavaDirectories());
    }
    final PsiDirectory[] directories = aPackage.getDirectories();
    final List<PsiDirectory> filteredDirectories = Lists.newArrayListWithExpectedSize(directories.length);
    for (PsiDirectory directory : directories) {
        for (VirtualFile file : javaDirectories) {
            if (VfsUtilCore.isAncestor(file, directory.getVirtualFile(), true)) {
                filteredDirectories.add(directory);
                break;
            }
        }
    }
    final PsiDirectory directory;
    switch(filteredDirectories.size()) {
        case 0:
            directory = null;
            break;
        case 1:
            directory = filteredDirectories.get(0);
            break;
        default:
            // There are several directories, present a dialog window for a user to choose a particular destination directory
            final PsiDirectory[] array = filteredDirectories.toArray(new PsiDirectory[filteredDirectories.size()]);
            directory = DirectoryChooserUtil.selectDirectory(aPackage.getProject(), array, filteredDirectories.get(0), "");
    }
    if (directory == null) {
        return;
    }
    final RunResult<PsiClass> result = new WriteCommandAction<PsiClass>(project) {

        @Override
        protected void run(@NotNull Result<PsiClass> result) throws Throwable {
            // Create a new class
            final PsiClass psiClass = JavaDirectoryService.getInstance().createClass(directory, myClassName);
            final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
            // Add a base class to "extends" list
            final PsiReferenceList list = psiClass.getExtendsList();
            if (list != null && myBaseClassFqcn != null) {
                final PsiClass parentClass = facade.findClass(myBaseClassFqcn, GlobalSearchScope.allScope(project));
                if (parentClass != null) {
                    list.add(facade.getElementFactory().createClassReferenceElement(parentClass));
                }
            }
            // Add a "public" modifier, which is absent by default. Required because classes references in AndroidManifest
            // have to point to public classes.
            final PsiModifierList modifierList = psiClass.getModifierList();
            if (modifierList != null) {
                modifierList.setModifierProperty(PsiModifier.PUBLIC, true);
            }
            result.setResult(psiClass);
        }
    }.execute();
    PsiClass aClass = result.getResultObject();
    OpenFileDescriptor fileDescriptor = new OpenFileDescriptor(project, aClass.getContainingFile().getVirtualFile());
    FileEditorManager.getInstance(project).openEditor(fileDescriptor, true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Aggregations

IdeaSourceProvider (org.jetbrains.android.facet.IdeaSourceProvider)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 PsiFile (com.intellij.psi.PsiFile)4 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)4 Module (com.intellij.openapi.module.Module)3 Nullable (org.jetbrains.annotations.Nullable)3 SourceFile (com.android.ide.common.blame.SourceFile)2 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 XmlFile (com.intellij.psi.xml.XmlFile)2 File (java.io.File)2 AndroidLibrary (com.android.builder.model.AndroidLibrary)1 MavenCoordinates (com.android.builder.model.MavenCoordinates)1 SourcePosition (com.android.ide.common.blame.SourcePosition)1 NamedObject (com.android.tools.idea.gradle.parser.NamedObject)1 HtmlBuilder (com.android.utils.HtmlBuilder)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiManager (com.intellij.psi.PsiManager)1