Search in sources :

Example 21 with ResourceUrl

use of com.android.ide.common.resources.ResourceUrl in project kotlin by JetBrains.

the class LintUtils method getInheritedStyles.

@Nullable
public static List<StyleResourceValue> getInheritedStyles(@NonNull Project project, @NonNull LintClient client, @NonNull String styleUrl) {
    if (!client.supportsProjectResources()) {
        return null;
    }
    AbstractResourceRepository resources = client.getProjectResources(project, true);
    if (resources == null) {
        return null;
    }
    ResourceUrl style = ResourceUrl.parse(styleUrl);
    if (style == null || style.framework) {
        return null;
    }
    List<StyleResourceValue> result = null;
    Queue<ResourceValue> queue = new ArrayDeque<ResourceValue>();
    queue.add(new ResourceValue(style.type, style.name, false));
    Set<String> seen = Sets.newHashSet();
    int count = 0;
    while (count < 30 && !queue.isEmpty()) {
        ResourceValue front = queue.remove();
        String name = front.getName();
        seen.add(name);
        List<ResourceItem> items = resources.getResourceItem(front.getResourceType(), name);
        if (items != null) {
            for (ResourceItem item : items) {
                ResourceValue rv = item.getResourceValue(false);
                if (rv instanceof StyleResourceValue) {
                    StyleResourceValue srv = (StyleResourceValue) rv;
                    if (result == null) {
                        result = Lists.newArrayList();
                    }
                    result.add(srv);
                    String parent = srv.getParentStyle();
                    if (parent != null && !parent.startsWith(ANDROID_PREFIX)) {
                        ResourceUrl p = ResourceUrl.parse(parent);
                        if (p != null && !p.framework && !seen.contains(p.name)) {
                            seen.add(p.name);
                            queue.add(new ResourceValue(ResourceType.STYLE, p.name, false));
                        }
                    }
                    int index = name.lastIndexOf('.');
                    if (index > 0) {
                        String parentName = name.substring(0, index);
                        if (!seen.contains(parentName)) {
                            seen.add(parentName);
                            queue.add(new ResourceValue(ResourceType.STYLE, parentName, false));
                        }
                    }
                }
            }
        }
        count++;
    }
    return result;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) AbstractResourceRepository(com.android.ide.common.res2.AbstractResourceRepository) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem) ResourceUrl(com.android.ide.common.resources.ResourceUrl) Nullable(com.android.annotations.Nullable)

Example 22 with ResourceUrl

use of com.android.ide.common.resources.ResourceUrl in project android by JetBrains.

the class AndroidLintMipmapIconsInspection method getQuickFixes.

@NotNull
@Override
public AndroidLintQuickFix[] getQuickFixes(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
    PsiElement parent = startElement.getParent();
    if (parent instanceof XmlAttribute) {
        XmlAttribute attribute = (XmlAttribute) parent;
        String value = attribute.getValue();
        if (value != null) {
            ResourceUrl url = ResourceUrl.parse(value);
            if (url != null && !url.framework) {
                return new AndroidLintQuickFix[] { new MigrateDrawableToMipmapFix(url) };
            }
        }
    }
    return AndroidLintQuickFix.EMPTY_ARRAY;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) PsiElement(com.intellij.psi.PsiElement) ResourceUrl(com.android.ide.common.resources.ResourceUrl) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with ResourceUrl

use of com.android.ide.common.resources.ResourceUrl in project android by JetBrains.

the class ThemeEditorUtils method generateToolTipText.

@NotNull
public static String generateToolTipText(@NotNull final ItemResourceValue resValue, @NotNull final Module module, @NotNull final Configuration configuration) {
    final LocalResourceRepository repository = AppResourceRepository.getAppResources(module, true);
    if (repository == null) {
        return "";
    }
    String tooltipKey = resValue.toString() + module.toString() + configuration.toString() + repository.getModificationCount();
    String cachedTooltip = ourTooltipCache.getIfPresent(tooltipKey);
    if (cachedTooltip != null) {
        return cachedTooltip;
    }
    ResourceUrl url = ResourceUrl.parse(ResolutionUtils.getResourceUrlFromQualifiedName(ResolutionUtils.getQualifiedItemName(resValue), SdkConstants.TAG_ATTR));
    assert url != null;
    String tooltipContents = AndroidJavaDocRenderer.render(module, configuration, url);
    assert tooltipContents != null;
    ourTooltipCache.put(tooltipKey, tooltipContents);
    return tooltipContents;
}
Also used : ResourceUrl(com.android.ide.common.resources.ResourceUrl) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with ResourceUrl

use of com.android.ide.common.resources.ResourceUrl in project android by JetBrains.

the class GradleInstantRunContextTest method getAppResourceReferences.

@Test
public void getAppResourceReferences() throws Exception {
    @Language("XML") String manifest = "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + "          package=\"com.google.samples.apps.topeka\">\n" + "\n" + "    <application android:allowBackup=\"@bool/allowBackup\"\n" + "                 android:icon=\"@mipmap/ic_launcher\"\n" + "                 android:label=\"@string/app_name\"\n" + "                 android:supportsRtl=\"false\"\n" + "                 android:theme=\"@style/Topeka\"\n" + "                 android:name=\".MyApplication\">\n" + "\n" + "        <activity android:name=\".activity.SignInActivity\"\n" + "                  android:theme=\"@style/Topeka.SignInActivity\">\n" + "            <intent-filter>\n" + "                <action android:name=\"android.intent.action.MAIN\" />\n" + "                <category android:name=\"android.intent.category.LAUNCHER\" />\n" + "            </intent-filter>\n" + "        </activity>\n" + "\n" + "        <activity android:name=\".activity.QuizActivity\"\n" + "                  android:theme=\"@style/Topeka.QuizActivity\"/>\n" + "\n" + "    </application>\n" + "</manifest>";
    Document document = XmlUtils.parseDocument(manifest, false);
    Set<ResourceUrl> appResourceReferences = GradleInstantRunContext.getAppResourceReferences(document.getDocumentElement());
    //noinspection ConstantConditions
    assertEquals(ImmutableList.copyOf(appResourceReferences), ImmutableList.of(ResourceUrl.parse("@bool/allowBackup"), ResourceUrl.parse("@mipmap/ic_launcher"), ResourceUrl.parse("@string/app_name"), ResourceUrl.parse("@style/Topeka"), ResourceUrl.parse("@style/Topeka.QuizActivity"), ResourceUrl.parse("@style/Topeka.SignInActivity")));
}
Also used : Language(org.intellij.lang.annotations.Language) Document(org.w3c.dom.Document) ResourceUrl(com.android.ide.common.resources.ResourceUrl) Test(org.junit.Test)

Aggregations

ResourceUrl (com.android.ide.common.resources.ResourceUrl)24 PsiElement (com.intellij.psi.PsiElement)7 Nullable (com.android.annotations.Nullable)6 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)5 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)5 ResourceItem (com.android.ide.common.res2.ResourceItem)5 ResourceType (com.android.resources.ResourceType)4 PsiMethod (com.intellij.psi.PsiMethod)4 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)3 LintClient (com.android.tools.klint.client.api.LintClient)3 UExpression (org.jetbrains.uast.UExpression)3 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)2 Module (com.intellij.openapi.module.Module)2 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)2 PsiClass (com.intellij.psi.PsiClass)2 PsiConditionalExpression (com.intellij.psi.PsiConditionalExpression)2 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)2 PsiExpression (com.intellij.psi.PsiExpression)2 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)2 PsiField (com.intellij.psi.PsiField)2