Search in sources :

Example 36 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class RenderErrorContributor method reportTagResourceFormat.

private void reportTagResourceFormat(@NotNull RenderResult result, @NotNull RenderProblem message) {
    Object clientData = message.getClientData();
    if (!(clientData instanceof String[])) {
        return;
    }
    String[] strings = (String[]) clientData;
    if (strings.length != 2) {
        return;
    }
    RenderTask renderTask = result.getRenderTask();
    if (renderTask == null) {
        return;
    }
    IAndroidTarget target = renderTask.getConfiguration().getRealTarget();
    if (target == null) {
        return;
    }
    AndroidPlatform platform = renderTask.getPlatform();
    if (platform == null) {
        return;
    }
    AndroidTargetData targetData = platform.getSdkData().getTargetData(target);
    AttributeDefinitions definitionLookup = targetData.getPublicAttrDefs(result.getFile().getProject());
    final String attributeName = strings[0];
    final String currentValue = strings[1];
    if (definitionLookup == null) {
        return;
    }
    AttributeDefinition definition = definitionLookup.getAttrDefByName(attributeName);
    if (definition == null) {
        return;
    }
    Set<AttributeFormat> formats = definition.getFormats();
    if (formats.contains(AttributeFormat.Flag) || formats.contains(AttributeFormat.Enum)) {
        String[] values = definition.getValues();
        if (values.length > 0) {
            HtmlBuilder builder = new HtmlBuilder();
            builder.add("Change ").add(currentValue).add(" to: ");
            boolean first = true;
            for (String value : values) {
                if (first) {
                    first = false;
                } else {
                    builder.add(", ");
                }
                builder.addLink(value, myLinkManager.createReplaceAttributeValueUrl(attributeName, currentValue, value));
            }
            addRefreshAction(builder);
            addIssue().setSummary("Incorrect resource value format").setHtmlContent(builder).build();
        }
    }
}
Also used : HtmlBuilder(com.android.utils.HtmlBuilder) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) IAndroidTarget(com.android.sdklib.IAndroidTarget) AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) AndroidTargetData(org.jetbrains.android.sdk.AndroidTargetData)

Example 37 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class RenderErrorContributor method reportThrowable.

/**
   * Display the problem list encountered during a render.
   *
   * @return if the throwable was hidden.
   */
private boolean reportThrowable(@NotNull HtmlBuilder builder, @NotNull final Throwable throwable, boolean hideIfIrrelevant) {
    StackTraceElement[] frames = throwable.getStackTrace();
    int end = -1;
    boolean haveInterestingFrame = false;
    for (int i = 0; i < frames.length; i++) {
        StackTraceElement frame = frames[i];
        if (isInterestingFrame(frame)) {
            haveInterestingFrame = true;
        }
        String className = frame.getClassName();
        if (className.equals(RENDER_SESSION_IMPL_FQCN)) {
            end = i;
            break;
        }
    }
    if (end == -1 || !haveInterestingFrame) {
        // Not a recognized stack trace range: just skip it
        if (hideIfIrrelevant) {
            if (RenderLogger.isLoggingAllErrors()) {
                ShowExceptionFix detailsFix = new ShowExceptionFix(myResult.getModule().getProject(), throwable);
                builder.addLink("Show Exception", myLinkManager.createRunnableLink(detailsFix));
            }
            return true;
        } else {
            // List just the top frames
            for (int i = 0; i < frames.length; i++) {
                StackTraceElement frame = frames[i];
                if (!isVisible(frame)) {
                    end = i;
                    if (end == 0) {
                        // Find end instead
                        for (int j = 0; j < frames.length; j++) {
                            frame = frames[j];
                            String className = frame.getClassName();
                            if (className.equals(RENDER_SESSION_IMPL_FQCN)) {
                                end = j;
                                break;
                            }
                        }
                    }
                    break;
                }
            }
        }
    }
    builder.addHtml(StringUtil.replace(throwable.toString(), "\n", "<BR/>")).newline();
    boolean wasHidden = false;
    int indent = 2;
    File platformSource = null;
    boolean platformSourceExists = true;
    for (int i = 0; i < end; i++) {
        StackTraceElement frame = frames[i];
        if (isHiddenFrame(frame)) {
            wasHidden = true;
            continue;
        }
        String className = frame.getClassName();
        String methodName = frame.getMethodName();
        builder.addNbsps(indent);
        builder.add("at ").add(className).add(".").add(methodName);
        String fileName = frame.getFileName();
        if (fileName != null && !fileName.isEmpty()) {
            int lineNumber = frame.getLineNumber();
            String location = fileName + ':' + lineNumber;
            if (isInterestingFrame(frame)) {
                if (wasHidden) {
                    builder.addNbsps(indent).add("    ...").newline();
                    wasHidden = false;
                }
                String url = myLinkManager.createOpenStackUrl(className, methodName, fileName, lineNumber);
                builder.add("(").addLink(location, url).add(")");
            } else {
                // Try to link to local documentation
                String url = null;
                if (isFramework(frame) && platformSourceExists) {
                    // try to link to documentation, if available
                    if (platformSource == null) {
                        IAndroidTarget target = myResult.getRenderTask() != null ? myResult.getRenderTask().getConfiguration().getRealTarget() : null;
                        platformSource = target != null ? AndroidSdks.getInstance().findPlatformSources(target) : null;
                        platformSourceExists = platformSource != null;
                    }
                    if (platformSourceExists) {
                        File classFile = new File(platformSource, frame.getClassName().replace('.', File.separatorChar) + DOT_JAVA);
                        if (!classFile.exists()) {
                            // Probably an innerclass like foo.bar.Outer.Inner; the above would look for foo/bar/Outer/Inner.java; try
                            // again at foo/bar/
                            File parentFile = classFile.getParentFile();
                            classFile = new File(parentFile.getParentFile(), parentFile.getName() + DOT_JAVA);
                            if (!classFile.exists()) {
                                // in theory we should keep trying this repeatedly for more deeply nested inner classes
                                classFile = null;
                            }
                        }
                        if (classFile != null) {
                            url = HtmlLinkManager.createFilePositionUrl(classFile, lineNumber, 0);
                        }
                    }
                }
                if (url != null) {
                    builder.add("(").addLink(location, url).add(")");
                } else {
                    builder.add("(").add(location).add(")");
                }
            }
            builder.newline();
        }
    }
    builder.addLink("Copy stack to clipboard", myLinkManager.createRunnableLink(() -> {
        String text = Throwables.getStackTraceAsString(throwable);
        try {
            CopyPasteManager.getInstance().setContents(new StringSelection(text));
            RenderErrorPanel.showNotification("Stack trace copied to clipboard");
        } catch (Exception ignore) {
        }
    }));
    return false;
}
Also used : IAndroidTarget(com.android.sdklib.IAndroidTarget) XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) IOException(java.io.IOException) StringSelection(java.awt.datatransfer.StringSelection)

Example 38 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class AndroidPlatform method parse.

/** @deprecated Use only for converting. */
@Deprecated
@Nullable
public static AndroidPlatform parse(@NotNull Library library, @Nullable Library.ModifiableModel model, @Nullable Map<String, AndroidSdkData> parsedSdks) {
    VirtualFile[] files = model != null ? model.getFiles(CLASSES) : library.getFiles(CLASSES);
    Set<String> jarPaths = Sets.newHashSet();
    VirtualFile frameworkLibrary = null;
    for (VirtualFile file : files) {
        VirtualFile vFile = JarFileSystem.getInstance().getVirtualFileForJar(file);
        if (vFile != null) {
            if (vFile.getName().equals(FN_FRAMEWORK_LIBRARY)) {
                frameworkLibrary = vFile;
            }
            jarPaths.add(vFile.getPath());
        }
    }
    if (frameworkLibrary != null) {
        VirtualFile sdkDir = frameworkLibrary.getParent();
        if (sdkDir != null) {
            VirtualFile platformsDir = sdkDir.getParent();
            if (platformsDir != null && platformsDir.getName().equals(FD_PLATFORMS)) {
                sdkDir = platformsDir.getParent();
                if (sdkDir == null)
                    return null;
            }
            String sdkPath = sdkDir.getPath();
            AndroidSdkData sdkData = parsedSdks != null ? parsedSdks.get(sdkPath) : null;
            if (sdkData == null) {
                sdkData = AndroidSdkData.getSdkData(sdkPath);
                if (sdkData == null)
                    return null;
                if (parsedSdks != null) {
                    parsedSdks.put(sdkPath, sdkData);
                }
            }
            IAndroidTarget resultTarget = null;
            for (IAndroidTarget target : sdkData.getTargets()) {
                String targetsFrameworkLibPath = getCanonicalPath(target.getPath(ANDROID_JAR));
                if (frameworkLibrary.getPath().equals(targetsFrameworkLibPath)) {
                    if (target.isPlatform()) {
                        if (resultTarget == null)
                            resultTarget = target;
                    } else {
                        boolean ok = true;
                        List<OptionalLibrary> libraries = target.getAdditionalLibraries();
                        if (libraries.isEmpty()) {
                            // we cannot identify add-on target without optional libraries by classpath
                            ok = false;
                        } else {
                            for (OptionalLibrary optionalLibrary : libraries) {
                                if (!jarPaths.contains(getCanonicalPath(optionalLibrary.getJar().getAbsolutePath()))) {
                                    ok = false;
                                }
                            }
                        }
                        if (ok) {
                            resultTarget = target;
                        }
                    }
                }
            }
            if (resultTarget != null) {
                return new AndroidPlatform(sdkData, resultTarget);
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OptionalLibrary(com.android.sdklib.IAndroidTarget.OptionalLibrary) IAndroidTarget(com.android.sdklib.IAndroidTarget) Nullable(org.jetbrains.annotations.Nullable)

Example 39 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class MergedManifestTest method testGetActivityThemes6.

public void testGetActivityThemes6() throws Exception {
    // Ensures that when the *rendering* target is less than version 11, we don't
    // use Holo even though the manifest SDK version calls for it.
    MergedManifest info = getMergedManifest("<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" + "    package='com.android.unittest'>\n" + "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='11'/>\n" + "</manifest>\n");
    Map<String, ActivityAttributes> map = info.getActivityAttributesMap();
    assertEquals(map.toString(), 0, map.size());
    assertEquals("com.android.unittest", info.getPackage());
    assertEquals("Theme.Holo", ResourceHelper.styleToTheme(info.getDefaultTheme(null, XLARGE, null)));
    // Here's the check
    IAndroidTarget olderVersion = new TestAndroidTarget(4);
    assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(olderVersion, XLARGE, null)));
}
Also used : ActivityAttributes(com.android.tools.idea.model.MergedManifest.ActivityAttributes) IAndroidTarget(com.android.sdklib.IAndroidTarget)

Example 40 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class AndroidSdksTest method findAndroidTarget.

@NotNull
private IAndroidTarget findAndroidTarget() {
    AndroidSdkData sdkData = getSdkData(mySdkPath);
    assertNotNull(sdkData);
    IAndroidTarget[] targets = sdkData.getTargets(false);
    assertThat(targets).isNotEmpty();
    // Use the latest platform, which is checked-in as a full SDK. Older platforms may not be checked in full, to save space.
    IAndroidTarget result = ContainerUtil.find(targets, target -> target.hashString().equals(TestUtils.getLatestAndroidPlatform()));
    assertThat(result).isNotNull();
    return result;
}
Also used : AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) IAndroidTarget(com.android.sdklib.IAndroidTarget) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IAndroidTarget (com.android.sdklib.IAndroidTarget)105 Nullable (org.jetbrains.annotations.Nullable)24 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 Module (com.intellij.openapi.module.Module)17 NotNull (org.jetbrains.annotations.NotNull)16 AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)15 Sdk (com.intellij.openapi.projectRoots.Sdk)14 File (java.io.File)13 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)13 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)13 AndroidVersion (com.android.sdklib.AndroidVersion)11 Device (com.android.sdklib.devices.Device)11 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)9 State (com.android.sdklib.devices.State)8 AndroidSdkAdditionalData (org.jetbrains.android.sdk.AndroidSdkAdditionalData)8 AndroidTargetData (org.jetbrains.android.sdk.AndroidTargetData)8 Configuration (com.android.tools.idea.configurations.Configuration)7 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)7 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)7 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)6