Search in sources :

Example 21 with HtmlBuilder

use of com.android.utils.HtmlBuilder in project android by JetBrains.

the class GradleRenderErrorContributor method reportIssue170841.

private void reportIssue170841(RenderLogger logger, AndroidFacet facet) {
    Map<String, Throwable> brokenClasses = logger.getBrokenClasses();
    if (brokenClasses == null || brokenClasses.isEmpty() || facet == null) {
        return;
    }
    AndroidModuleModel model = AndroidModuleModel.get(facet);
    if (model == null || !model.getFeatures().isLayoutRenderingIssuePresent()) {
        return;
    }
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            FixAndroidGradlePluginVersionHyperlink quickFix = new FixAndroidGradlePluginVersionHyperlink(GradleVersion.parse(GRADLE_PLUGIN_RECOMMENDED_VERSION), null);
            quickFix.executeIfClicked(facet.getModule().getProject(), new HyperlinkEvent(this, HyperlinkEvent.EventType.ACTIVATED, null, quickFix.getUrl()));
        }
    };
    HtmlBuilder builder = new HtmlBuilder();
    HtmlLinkManager linkManager = logger.getLinkManager();
    builder.add("Tip: Either ").addLink("update the Gradle plugin build version to 1.2.3", linkManager.createRunnableLink(runnable)).add(" or later, or downgrade to version 1.1.3, or as a workaround, ");
    builder.beginList().listItem().addLink("", "Build the project", ", then", linkManager.createBuildProjectUrl()).listItem().addLink("", "Gradle Sync the project", ", then", linkManager.createSyncProjectUrl()).listItem().addLink("Manually ", "refresh the layout", " (or restart the IDE)", linkManager.createRefreshRenderUrl()).endList();
    builder.newline();
    GradleVersion modelVersion = model.getModelVersion();
    addIssue().setSeverity(HighlightSeverity.ERROR).setSummary("Using an obsolete version of the Gradle plugin (" + modelVersion + "); " + "this can lead to layouts not rendering correctly.").setHtmlContent(builder).build();
}
Also used : FixAndroidGradlePluginVersionHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.FixAndroidGradlePluginVersionHyperlink) HyperlinkEvent(javax.swing.event.HyperlinkEvent) HtmlLinkManager(com.android.tools.idea.rendering.HtmlLinkManager) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) HtmlBuilder(com.android.utils.HtmlBuilder) GradleVersion(com.android.ide.common.repository.GradleVersion)

Example 22 with HtmlBuilder

use of com.android.utils.HtmlBuilder in project android by JetBrains.

the class RenderErrorContributor method reportSandboxError.

private boolean reportSandboxError(@NotNull Throwable throwable, boolean newlineBefore, boolean newlineAfter) {
    if (!(throwable instanceof SecurityException)) {
        return false;
    }
    HtmlBuilder builder = new HtmlBuilder();
    if (newlineBefore) {
        builder.newline();
    }
    builder.addLink("Turn off custom view rendering sandbox", myLinkManager.createDisableSandboxUrl());
    String lastFailedPath = RenderSecurityManager.getLastFailedPath();
    if (lastFailedPath != null) {
        builder.newline().newline().add("Diagnostic info for Studio bug report:").newline().add("Failed path: ").add(lastFailedPath).newline();
        String tempDir = System.getProperty("java.io.tmpdir");
        builder.add("Normal temp dir: ").add(tempDir).newline();
        File normalized = new File(tempDir);
        builder.add("Normalized temp dir: ").add(normalized.getPath()).newline();
        try {
            builder.add("Canonical temp dir: ").add(normalized.getCanonicalPath()).newline();
        } catch (IOException e) {
        // ignore
        }
        builder.add("os.name: ").add(SystemInfo.OS_NAME).newline().add("os.version: ").add(SystemInfo.OS_VERSION).newline().add("java.runtime.version: ").add(SystemInfo.JAVA_RUNTIME_VERSION);
    }
    if (throwable.getMessage().equals("Unable to create temporary file")) {
        if (JAVA_VERSION.startsWith("1.7.0_")) {
            int version = Integer.parseInt(JAVA_VERSION.substring(JAVA_VERSION.indexOf('_') + 1));
            if (version > 0 && version < 45) {
                builder.newline().addIcon(HtmlBuilderHelper.getTipIconPath()).add("Tip: This may be caused by using an older version of JDK 1.7.0; try using at least 1.7.0_45 " + "(you are using " + JAVA_VERSION + ")");
            }
        }
    }
    if (newlineAfter) {
        builder.newline().newline();
    }
    reportThrowable(builder, throwable, false);
    addRefreshAction(builder);
    addIssue().setSeverity(HighlightSeverity.ERROR).setSummary("Rendering sandbox error").setHtmlContent(builder).build();
    return true;
}
Also used : HtmlBuilder(com.android.utils.HtmlBuilder) IOException(java.io.IOException) XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Example 23 with HtmlBuilder

use of com.android.utils.HtmlBuilder in project android by JetBrains.

the class RenderErrorContributor method reportBrokenClasses.

private void reportBrokenClasses(@NotNull RenderLogger logger) {
    Map<String, Throwable> brokenClasses = logger.getBrokenClasses();
    if (brokenClasses == null || brokenClasses.isEmpty()) {
        return;
    }
    HtmlBuilder builder = new HtmlBuilder();
    final Module module = logger.getModule();
    for (Throwable throwable : brokenClasses.values()) {
        if (RenderLogger.isIssue164378(throwable)) {
            RenderLogger.addHtmlForIssue164378(throwable, module, myLinkManager, builder, false);
            break;
        }
    }
    builder.add("The following classes could not be instantiated:");
    Throwable firstThrowable = null;
    builder.beginList();
    for (Map.Entry<String, Throwable> entry : brokenClasses.entrySet()) {
        String className = entry.getKey();
        Throwable throwable = entry.getValue();
        builder.listItem().add(className).add(" (").addLink("Open Class", myLinkManager.createOpenClassUrl(className));
        if (throwable != null && module != null) {
            builder.add(", ");
            ShowExceptionFix detailsFix = new ShowExceptionFix(module.getProject(), throwable);
            builder.addLink("Show Exception", myLinkManager.createRunnableLink(detailsFix));
        }
        builder.add(", ").addLink("Clear Cache", myLinkManager.createClearCacheUrl()).add(")");
        if (firstThrowable == null && throwable != null) {
            firstThrowable = throwable;
        }
    }
    builder.endList().addIcon(HtmlBuilderHelper.getTipIconPath()).addLink("Tip: Use ", "View.isInEditMode()", " in your custom views to skip code or show sample data when shown in the IDE.", "http://developer.android.com/reference/android/view/View.html#isInEditMode()").newline().newline().add("If this is an unexpected error you can also try to ").addLink("", "build the project", ", then ", myLinkManager.createBuildProjectUrl()).addLink("manually ", "refresh the layout", ".", myLinkManager.createRefreshRenderUrl());
    if (firstThrowable != null) {
        builder.newline().newline().addHeading("Exception Details", HtmlBuilderHelper.getHeaderFontColor()).newline();
        reportThrowable(builder, firstThrowable, false);
        reportSandboxError(firstThrowable, true, false);
    }
    builder.newline().newline();
    addIssue().setSeverity(HighlightSeverity.ERROR, HIGH_PRIORITY).setSummary("Failed to instantiate one or more classes").setHtmlContent(builder).build();
}
Also used : HtmlBuilder(com.android.utils.HtmlBuilder) Module(com.intellij.openapi.module.Module)

Example 24 with HtmlBuilder

use of com.android.utils.HtmlBuilder in project android by JetBrains.

the class RenderErrorContributor method reportRenderingFidelityProblems.

private void reportRenderingFidelityProblems(@NotNull RenderLogger logger, @NotNull final RenderTask renderTask) {
    List<RenderProblem> fidelityWarnings = logger.getFidelityWarnings();
    if (fidelityWarnings == null || fidelityWarnings.isEmpty()) {
        return;
    }
    HtmlBuilder builder = new HtmlBuilder();
    builder.add("The graphics preview in the layout editor may not be accurate:").newline();
    builder.beginList();
    int count = 0;
    for (final RenderProblem warning : fidelityWarnings) {
        builder.listItem();
        warning.appendHtml(builder.getStringBuilder());
        final Object clientData = warning.getClientData();
        if (clientData != null) {
            builder.addLink(" (Ignore for this session)", myLinkManager.createRunnableLink(() -> {
                RenderLogger.ignoreFidelityWarning(clientData);
                EditorDesignSurface surface = renderTask.getDesignSurface();
                if (surface != null) {
                    surface.requestRender();
                }
            }));
        }
        builder.newline();
        count++;
        // Only display the first 3 render fidelity issues
        if (count == 3) {
            @SuppressWarnings("ConstantConditions") int remaining = fidelityWarnings.size() - count;
            if (remaining > 0) {
                builder.add("(").addHtml(Integer.toString(remaining)).add(" additional render fidelity issues hidden)");
                break;
            }
        }
    }
    builder.endList();
    builder.addLink("Ignore all fidelity warnings for this session", myLinkManager.createRunnableLink(() -> {
        RenderLogger.ignoreAllFidelityWarnings();
        EditorDesignSurface surface = renderTask.getDesignSurface();
        if (surface != null) {
            surface.requestRender();
        }
    }));
    builder.newline();
    addIssue().setSeverity(HighlightSeverity.WARNING).setSummary("Layout fidelity warning").setHtmlContent(builder).build();
}
Also used : EditorDesignSurface(com.android.tools.idea.ui.designer.EditorDesignSurface) HtmlBuilder(com.android.utils.HtmlBuilder)

Example 25 with HtmlBuilder

use of com.android.utils.HtmlBuilder in project android by JetBrains.

the class RenderErrorContributor method reportMissingSizeAttributes.

private void reportMissingSizeAttributes(@NotNull final RenderLogger logger, @NotNull RenderTask renderTask) {
    Module module = logger.getModule();
    if (module == null) {
        return;
    }
    Project project = module.getProject();
    if (logger.isMissingSize()) {
        HtmlBuilder builder = new HtmlBuilder();
        // Emit hyperlink about missing attributes; the action will operate on all of them
        builder.addBold("NOTE: One or more layouts are missing the layout_width or layout_height attributes. " + "These are required in most layouts.").newline();
        final ResourceResolver resourceResolver = renderTask.getResourceResolver();
        XmlFile psiFile = renderTask.getPsiFile();
        if (psiFile == null) {
            LOG.error("PsiFile is missing in RenderTask used in RenderErrorPanel!");
            return;
        }
        AddMissingAttributesFix fix = new AddMissingAttributesFix(project, psiFile, resourceResolver);
        List<XmlTag> missing = fix.findViewsMissingSizes();
        // See whether we should offer match_parent instead of fill_parent
        AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(module);
        final String fill = moduleInfo == null || moduleInfo.getBuildSdkVersion() == null || moduleInfo.getBuildSdkVersion().getApiLevel() >= 8 ? VALUE_MATCH_PARENT : VALUE_FILL_PARENT;
        for (final XmlTag tag : missing) {
            ApplicationManager.getApplication().runReadAction(() -> {
                boolean missingWidth = !AddMissingAttributesFix.definesWidth(tag, resourceResolver);
                boolean missingHeight = !AddMissingAttributesFix.definesHeight(tag, resourceResolver);
                assert missingWidth || missingHeight;
                String id = tag.getAttributeValue(ATTR_ID);
                if (id == null || id.length() == 0) {
                    id = '<' + tag.getName() + '>';
                } else {
                    id = '"' + stripIdPrefix(id) + '"';
                }
                if (missingWidth) {
                    reportMissingSize(builder, logger, fill, tag, id, ATTR_LAYOUT_WIDTH);
                }
                if (missingHeight) {
                    reportMissingSize(builder, logger, fill, tag, id, ATTR_LAYOUT_HEIGHT);
                }
            });
        }
        builder.newline().add("Or: ").addLink("Automatically add all missing attributes", myLinkManager.createCommandLink(fix)).newline().newline().newline();
        addIssue().setSeverity(HighlightSeverity.ERROR).setSummary("One or more layouts are missing the layout_width or layout_height attributes").setHtmlContent(builder).build();
    }
}
Also used : AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) HtmlBuilder(com.android.utils.HtmlBuilder) ResourceResolver(com.android.ide.common.resources.ResourceResolver) Module(com.intellij.openapi.module.Module) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

HtmlBuilder (com.android.utils.HtmlBuilder)36 NotNull (org.jetbrains.annotations.NotNull)9 XmlFile (com.intellij.psi.xml.XmlFile)8 Module (com.intellij.openapi.module.Module)7 Project (com.intellij.openapi.project.Project)7 File (java.io.File)6 IOException (java.io.IOException)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 VisibleForTesting (com.android.annotations.VisibleForTesting)2 SourceFile (com.android.ide.common.blame.SourceFile)2 ResourceResolver (com.android.ide.common.resources.ResourceResolver)2 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)2 RepositoryPackages (com.android.repository.impl.meta.RepositoryPackages)2 ResourceFolderType (com.android.resources.ResourceFolderType)2 AndroidVersion (com.android.sdklib.AndroidVersion)2 IAndroidTarget (com.android.sdklib.IAndroidTarget)2 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)2 EditorDesignSurface (com.android.tools.idea.ui.designer.EditorDesignSurface)2