Search in sources :

Example 76 with ResourceType

use of com.android.resources.ResourceType in project android_frameworks_base by ParanoidAndroid.

the class RenderSessionImpl method setupTabHost.

/**
     * Sets up a {@link TabHost} object.
     * @param tabHost the TabHost to setup.
     * @param projectCallback The project callback object to access the project R class.
     * @throws PostInflateException
     */
private void setupTabHost(TabHost tabHost, IProjectCallback projectCallback) throws PostInflateException {
    // look for the TabWidget, and the FrameLayout. They have their own specific names
    View v = tabHost.findViewById(android.R.id.tabs);
    if (v == null) {
        throw new PostInflateException("TabHost requires a TabWidget with id \"android:id/tabs\".\n");
    }
    if ((v instanceof TabWidget) == false) {
        throw new PostInflateException(String.format("TabHost requires a TabWidget with id \"android:id/tabs\".\n" + "View found with id 'tabs' is '%s'", v.getClass().getCanonicalName()));
    }
    v = tabHost.findViewById(android.R.id.tabcontent);
    if (v == null) {
        // TODO: see if we can fake tabs even without the FrameLayout (same below when the framelayout is empty)
        throw new PostInflateException("TabHost requires a FrameLayout with id \"android:id/tabcontent\".");
    }
    if ((v instanceof FrameLayout) == false) {
        throw new PostInflateException(String.format("TabHost requires a FrameLayout with id \"android:id/tabcontent\".\n" + "View found with id 'tabcontent' is '%s'", v.getClass().getCanonicalName()));
    }
    FrameLayout content = (FrameLayout) v;
    // now process the content of the framelayout and dynamically create tabs for it.
    final int count = content.getChildCount();
    // this must be called before addTab() so that the TabHost searches its TabWidget
    // and FrameLayout.
    tabHost.setup();
    if (count == 0) {
        // Create a dummy child to get a single tab
        TabSpec spec = tabHost.newTabSpec("tag").setIndicator("Tab Label", tabHost.getResources().getDrawable(android.R.drawable.ic_menu_info_details)).setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                return new LinearLayout(getContext());
            }
        });
        tabHost.addTab(spec);
        return;
    } else {
        // for each child of the framelayout, add a new TabSpec
        for (int i = 0; i < count; i++) {
            View child = content.getChildAt(i);
            String tabSpec = String.format("tab_spec%d", i + 1);
            int id = child.getId();
            Pair<ResourceType, String> resource = projectCallback.resolveResourceId(id);
            String name;
            if (resource != null) {
                name = resource.getSecond();
            } else {
                // default name if id is unresolved.
                name = String.format("Tab %d", i + 1);
            }
            tabHost.addTab(tabHost.newTabSpec(tabSpec).setIndicator(name).setContent(id));
        }
    }
}
Also used : TabHost(android.widget.TabHost) ResourceType(com.android.resources.ResourceType) TabWidget(android.widget.TabWidget) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) AbsListView(android.widget.AbsListView) ExpandableListView(android.widget.ExpandableListView) TabSpec(android.widget.TabHost.TabSpec) FrameLayout(android.widget.FrameLayout) LinearLayout(android.widget.LinearLayout)

Example 77 with ResourceType

use of com.android.resources.ResourceType in project android_frameworks_base by ParanoidAndroid.

the class BridgeInflater method inflate.

@Override
public View inflate(int resource, ViewGroup root) {
    Context context = getContext();
    if (context instanceof BridgeContext) {
        BridgeContext bridgeContext = (BridgeContext) context;
        ResourceValue value = null;
        Pair<ResourceType, String> layoutInfo = Bridge.resolveResourceId(resource);
        if (layoutInfo != null) {
            value = bridgeContext.getRenderResources().getFrameworkResource(ResourceType.LAYOUT, layoutInfo.getSecond());
        } else {
            layoutInfo = mProjectCallback.resolveResourceId(resource);
            if (layoutInfo != null) {
                value = bridgeContext.getRenderResources().getProjectResource(ResourceType.LAYOUT, layoutInfo.getSecond());
            }
        }
        if (value != null) {
            File f = new File(value.getValue());
            if (f.isFile()) {
                try {
                    XmlPullParser parser = ParserFactory.create(f);
                    BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(parser, bridgeContext, false);
                    return inflate(bridgeParser, root);
                } catch (Exception e) {
                    Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed to parse file " + f.getAbsolutePath(), e, null);
                    return null;
                }
            }
        }
    }
    return null;
}
Also used : Context(android.content.Context) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) XmlPullParser(org.xmlpull.v1.XmlPullParser) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceType(com.android.resources.ResourceType) File(java.io.File) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser) InflateException(android.view.InflateException)

Example 78 with ResourceType

use of com.android.resources.ResourceType in project android by JetBrains.

the class AndroidPsiAugmentProvider method getAugments.

@SuppressWarnings("unchecked")
@NotNull
@Override
public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement element, @NotNull Class<Psi> type) {
    if ((type != PsiClass.class && type != PsiField.class) || !(element instanceof PsiExtensibleClass)) {
        return Collections.emptyList();
    }
    final PsiExtensibleClass aClass = (PsiExtensibleClass) element;
    final String className = aClass.getName();
    final boolean rClassAugment = AndroidUtils.R_CLASS_NAME.equals(className) && type == PsiClass.class;
    if (DumbService.isDumb(element.getProject())) {
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because of dumb mode");
        }
        return Collections.emptyList();
    }
    final AndroidFacet facet = AndroidFacet.getInstance(element);
    if (facet == null) {
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because no facet");
        }
        return Collections.emptyList();
    }
    final PsiFile containingFile = element.getContainingFile();
    if (containingFile == null) {
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because of no containing file");
        }
        return Collections.emptyList();
    }
    if (type == PsiClass.class) {
        if (AndroidUtils.R_CLASS_NAME.equals(className) && AndroidResourceUtil.isRJavaFile(facet, containingFile)) {
            final Set<String> existingInnerClasses = getOwnInnerClasses(aClass);
            final Set<ResourceType> types = ResourceReferenceConverter.getResourceTypesInCurrentModule(facet);
            final List<Psi> result = new ArrayList<Psi>();
            for (ResourceType resType : types) {
                if (!existingInnerClasses.contains(resType.getName())) {
                    final AndroidLightClass resClass = new ResourceTypeClass(facet, resType.getName(), aClass);
                    result.add((Psi) resClass);
                }
            }
            if (rClassAugment) {
                LOG.debug("R_CLASS_AUGMENT: " + result.size() + " classes added");
            }
            return result;
        } else if (AndroidUtils.MANIFEST_CLASS_NAME.equals(className) && AndroidResourceUtil.isManifestJavaFile(facet, containingFile)) {
            return Arrays.asList((Psi) new PermissionClass(facet, aClass), (Psi) new PermissionGroupClass(facet, aClass));
        }
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because containing file is not actual R.java file");
        }
    } else if (type == PsiField.class && !(aClass instanceof AndroidLightClass)) {
        // extend existing inner classes, not provided by this augment (ex. they can be generated by maven)
        final PsiClass parentClass = aClass.getContainingClass();
        if (parentClass != null && AndroidUtils.R_CLASS_NAME.equals(parentClass.getName()) && AndroidResourceUtil.isRJavaFile(facet, containingFile)) {
            final String resClassName = aClass.getName();
            if (resClassName != null && ResourceType.getEnum(resClassName) != null) {
                final Set<String> existingFields = getOwnFields(aClass);
                final PsiField[] newFields = ResourceTypeClass.buildLocalResourceFields(facet, resClassName, aClass);
                final List<Psi> result = new ArrayList<Psi>();
                for (PsiField field : newFields) {
                    if (!existingFields.contains(field.getName())) {
                        result.add((Psi) field);
                    }
                }
                return result;
            }
        }
    }
    return Collections.emptyList();
}
Also used : HashSet(com.intellij.util.containers.HashSet) PsiClass(com.intellij.psi.PsiClass) ResourceType(com.android.resources.ResourceType) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) PsiField(com.intellij.psi.PsiField) PsiExtensibleClass(com.intellij.psi.impl.source.PsiExtensibleClass) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 79 with ResourceType

use of com.android.resources.ResourceType in project android by JetBrains.

the class AndroidColorAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof XmlTag) {
        XmlTag tag = (XmlTag) element;
        String tagName = tag.getName();
        if ((ResourceType.COLOR.getName().equals(tagName) || ResourceType.DRAWABLE.getName().equals(tagName) || ResourceType.MIPMAP.getName().equals(tagName))) {
            DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement(tag);
            if (domElement instanceof ResourceElement || ApplicationManager.getApplication().isUnitTestMode()) {
                String value = tag.getValue().getText().trim();
                annotateXml(element, holder, value);
            }
        } else if (TAG_ITEM.equals(tagName)) {
            XmlTagValue value = tag.getValue();
            String text = value.getText();
            annotateXml(element, holder, text);
        }
    } else if (element instanceof XmlAttributeValue) {
        XmlAttributeValue v = (XmlAttributeValue) element;
        String value = v.getValue();
        if (value == null || value.isEmpty()) {
            return;
        }
        annotateXml(element, holder, value);
    } else if (element instanceof PsiReferenceExpression) {
        ResourceReferenceType referenceType = AndroidPsiUtils.getResourceReferenceType(element);
        if (referenceType != ResourceReferenceType.NONE) {
            // (isResourceReference will return true for both "R.drawable.foo" and the foo literal leaf in the
            // same expression, which would result in both elements getting annotated and the icon showing up
            // in the gutter twice. Instead we only count the outer one.
            ResourceType type = AndroidPsiUtils.getResourceType(element);
            if (type == ResourceType.COLOR || type == ResourceType.DRAWABLE || type == ResourceType.MIPMAP) {
                String name = AndroidPsiUtils.getResourceName(element);
                annotateResourceReference(type, holder, element, name, referenceType == ResourceReferenceType.FRAMEWORK);
            }
        }
    }
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) DomElement(com.intellij.util.xml.DomElement) XmlTagValue(com.intellij.psi.xml.XmlTagValue) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) ResourceReferenceType(com.android.tools.idea.AndroidPsiUtils.ResourceReferenceType) ResourceType(com.android.resources.ResourceType) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag)

Example 80 with ResourceType

use of com.android.resources.ResourceType in project android by JetBrains.

the class AndroidJavaCompletionContributor method filterPrivateResources.

public void filterPrivateResources(@NotNull CompletionParameters parameters, @NotNull final CompletionResultSet resultSet, AndroidFacet facet) {
    final ResourceVisibilityLookup lookup = AppResourceRepository.getAppResources(facet, true).getResourceVisibility(facet);
    if (lookup.isEmpty()) {
        return;
    }
    resultSet.runRemainingContributors(parameters, new Consumer<CompletionResult>() {

        @Override
        public void consume(CompletionResult result) {
            final Object obj = result.getLookupElement().getObject();
            if (obj instanceof PsiField) {
                PsiField field = (PsiField) obj;
                PsiClass containingClass = field.getContainingClass();
                if (containingClass != null) {
                    PsiClass rClass = containingClass.getContainingClass();
                    if (rClass != null && rClass.getName().equals(R_CLASS)) {
                        ResourceType type = ResourceType.getEnum(containingClass.getName());
                        if (type != null && lookup.isPrivate(type, field.getName())) {
                            return;
                        }
                    }
                }
            }
            resultSet.passResult(result);
        }
    });
}
Also used : CompletionResult(com.intellij.codeInsight.completion.CompletionResult) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) ResourceType(com.android.resources.ResourceType) ResourceVisibilityLookup(com.android.ide.common.repository.ResourceVisibilityLookup)

Aggregations

ResourceType (com.android.resources.ResourceType)137 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)31 NotNull (org.jetbrains.annotations.NotNull)16 Field (java.lang.reflect.Field)13 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)12 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 Map (java.util.Map)10 Nullable (org.jetbrains.annotations.Nullable)10 Nullable (com.android.annotations.Nullable)8 ResourceFolderType (com.android.resources.ResourceFolderType)8 File (java.io.File)8 EnumMap (java.util.EnumMap)7 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)7 Context (android.content.Context)6 View (android.view.View)6 AbsListView (android.widget.AbsListView)6 AdapterView (android.widget.AdapterView)6 ExpandableListView (android.widget.ExpandableListView)6 FrameLayout (android.widget.FrameLayout)6