Java Code Examples for com.android.ide.common.rendering.api.ResourceValue#getValue()

The following examples show how to use com.android.ide.common.rendering.api.ResourceValue#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppIndexingApiDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private static String replaceUrlWithValue(@NonNull XmlContext context,
        @NonNull String str) {
    Project project = context.getProject();
    LintClient client = context.getClient();
    if (!client.supportsProjectResources()) {
        return str;
    }
    ResourceUrl style = ResourceUrl.parse(str);
    if (style == null || style.type != ResourceType.STRING || style.framework) {
        return str;
    }
    AbstractResourceRepository resources = client.getProjectResources(project, true);
    if (resources == null) {
        return str;
    }
    List<ResourceItem> items = resources.getResourceItem(ResourceType.STRING, style.name);
    if (items == null || items.isEmpty()) {
        return str;
    }
    ResourceValue resourceValue = items.get(0).getResourceValue(false);
    if (resourceValue == null) {
        return str;
    }
    return resourceValue.getValue() == null ? str : resourceValue.getValue();
}
 
Example 2
Source File: ResourceResolver.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private ResourceValue resolveResValue(ResourceValue resValue, int depth) {
    if (resValue == null) {
        return null;
    }

    // if the resource value is null, we simply return it.
    String value = resValue.getValue();
    if (value == null) {
        return resValue;
    }

    // else attempt to find another ResourceValue referenced by this one.
    ResourceValue resolvedResValue = findResValue(value, resValue.isFramework());

    // if the value did not reference anything, then we simply return the input value
    if (resolvedResValue == null) {
        return resValue;
    }

    // detect potential loop due to mishandled namespace in attributes
    if (resValue == resolvedResValue || depth >= MAX_RESOURCE_INDIRECTION) {
        if (mLogger != null) {
            mLogger.error(LayoutLog.TAG_BROKEN,
                    String.format("Potential stack overflow trying to resolve '%s': cyclic resource definitions? Render may not be accurate.", value),
                    null);
        }
        return resValue;
    }

    // otherwise, we attempt to resolve this new value as well
    return resolveResValue(resolvedResValue, depth + 1);
}
 
Example 3
Source File: ResourceItemResolver.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Nullable
private ResourceValue resolveResValue(@Nullable ResourceValue resValue, int depth) {
    if (resValue == null) {
        return null;
    }

    // if the resource value is null, we simply return it.
    String value = resValue.getValue();
    if (value == null) {
        return resValue;
    }

    // else attempt to find another ResourceValue referenced by this one.
    ResourceValue resolvedResValue = findResValue(value, resValue.isFramework());

    // if the value did not reference anything, then we simply return the input value
    if (resolvedResValue == null) {
        return resValue;
    }

    // detect potential loop due to mishandled namespace in attributes
    if (resValue == resolvedResValue || depth >= MAX_RESOURCE_INDIRECTION) {
        if (mLogger != null) {
            mLogger.error(LayoutLog.TAG_BROKEN,
                    String.format(
                            "Potential stack overflow trying to resolve '%s': cyclic resource definitions? Render may not be accurate.",
                            value),
                    null);
        }
        return resValue;
    }

    // otherwise, we attempt to resolve this new value as well
    return resolveResValue(resolvedResValue, depth + 1);
}
 
Example 4
Source File: ResourceItemResolver.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a display string for a resource lookup
 * @param url the resource url, such as {@code @string/foo}
 * @param lookupChain the list of resolved items to display
 * @return the display string
 */
@NonNull
public static String getDisplayString(
        @NonNull String url,
        @NonNull List<ResourceValue> lookupChain) {
    StringBuilder sb = new StringBuilder();
    sb.append(url);
    String prev = url;
    for (ResourceValue element : lookupChain) {
        if (element == null) {
            continue;
        }
        String value = element.getValue();
        if (value == null) {
            continue;
        }
        String text = value;
        if (text.equals(prev)) {
            continue;
        }

        sb.append(" => ");

        // Strip paths
        if (!(text.startsWith(PREFIX_THEME_REF) || text.startsWith(PREFIX_RESOURCE_REF))) {
            int end = Math.max(text.lastIndexOf('/'), text.lastIndexOf('\\'));
            if (end != -1) {
                text = text.substring(end + 1);
            }
        }
        sb.append(text);

        prev = value;
    }

    return sb.toString();
}
 
Example 5
Source File: ResourceResolver.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private ResourceValue resolveResValue(ResourceValue resValue, int depth) {
    if (resValue == null) {
        return null;
    }

    // if the resource value is null, we simply return it.
    String value = resValue.getValue();
    if (value == null) {
        return resValue;
    }

    // else attempt to find another ResourceValue referenced by this one.
    ResourceValue resolvedResValue = findResValue(value, resValue.isFramework());

    // if the value did not reference anything, then we simply return the input value
    if (resolvedResValue == null) {
        return resValue;
    }

    // detect potential loop due to mishandled namespace in attributes
    if (resValue == resolvedResValue || depth >= MAX_RESOURCE_INDIRECTION) {
        if (mLogger != null) {
            mLogger.error(LayoutLog.TAG_BROKEN,
                    String.format("Potential stack overflow trying to resolve '%s': cyclic resource definitions? Render may not be accurate.", value),
                    null);
        }
        return resValue;
    }

    // otherwise, we attempt to resolve this new value as well
    return resolveResValue(resolvedResValue, depth + 1);
}
 
Example 6
Source File: ResourceItemResolver.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
private ResourceValue resolveResValue(@Nullable ResourceValue resValue, int depth) {
    if (resValue == null) {
        return null;
    }

    // if the resource value is null, we simply return it.
    String value = resValue.getValue();
    if (value == null) {
        return resValue;
    }

    // else attempt to find another ResourceValue referenced by this one.
    ResourceValue resolvedResValue = findResValue(value, resValue.isFramework());

    // if the value did not reference anything, then we simply return the input value
    if (resolvedResValue == null) {
        return resValue;
    }

    // detect potential loop due to mishandled namespace in attributes
    if (resValue == resolvedResValue || depth >= MAX_RESOURCE_INDIRECTION) {
        if (mLogger != null) {
            mLogger.error(LayoutLog.TAG_BROKEN,
                    String.format(
                            "Potential stack overflow trying to resolve '%s': cyclic resource definitions? Render may not be accurate.",
                            value),
                    null);
        }
        return resValue;
    }

    // otherwise, we attempt to resolve this new value as well
    return resolveResValue(resolvedResValue, depth + 1);
}
 
Example 7
Source File: ResourceItemResolver.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a display string for a resource lookup
 * @param url the resource url, such as {@code @string/foo}
 * @param lookupChain the list of resolved items to display
 * @return the display string
 */
@NonNull
public static String getDisplayString(
        @NonNull String url,
        @NonNull List<ResourceValue> lookupChain) {
    StringBuilder sb = new StringBuilder();
    sb.append(url);
    String prev = url;
    for (ResourceValue element : lookupChain) {
        if (element == null) {
            continue;
        }
        String value = element.getValue();
        if (value == null) {
            continue;
        }
        String text = value;
        if (text.equals(prev)) {
            continue;
        }

        sb.append(" => ");

        // Strip paths
        if (!(text.startsWith(PREFIX_THEME_REF) || text.startsWith(PREFIX_RESOURCE_REF))) {
            int end = Math.max(text.lastIndexOf('/'), text.lastIndexOf('\\'));
            if (end != -1) {
                text = text.substring(end + 1);
            }
        }
        sb.append(text);

        prev = value;
    }

    return sb.toString();
}
 
Example 8
Source File: LayoutLibCallback.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
@Override
public ILayoutPullParser getParser(ResourceValue layoutResource) {
    if (layoutResource.getValue() != null) {
        return new LayoutPullParser(new File(layoutResource.getValue()), ResourceNamespace.RES_AUTO);
    }
    return null;
}
 
Example 9
Source File: RenderService.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Renders the model and returns the result as a {@link RenderSession}.
 * @return the {@link RenderSession} resulting from rendering the current model
 * @throws XmlPullParserException
 * @throws FileNotFoundException
 */
public RenderSession createRenderSession(String layoutName) throws FileNotFoundException,
        XmlPullParserException {
    finishConfiguration();

    if (mResourceResolver == null) {
        // Abort the rendering if the resources are not found.
        return null;
    }

    // find the layout to run
    ResourceValue value = mResourceResolver.getProjectResource(ResourceType.LAYOUT, layoutName);
    if (value == null || value.getValue() == null) {
        throw new IllegalArgumentException("layout does not exist");
    }

    File layoutFile = new File(value.getValue());

    ILayoutPullParser parser = null;
    parser = new XmlParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    parser.setInput(new FileInputStream(layoutFile), "UTF-8"); //$NON-NLS-1$

    figureSomeValuesOut();

    SessionParams params = new SessionParams(
            parser,
            mRenderingMode,
            this /* projectKey */,
            mWidth, mHeight,
            mConfig.getDensityQualifier().getValue(),
            mXdpi, mYdpi,
            mResourceResolver,
            mProjectCallback,
            mMinSdkVersion,
            mTargetSdkVersion,
            mLogger);

    // Request margin and baseline information.
    // TODO: Be smarter about setting this; start without it, and on the first request
    // for an extended view info, re-render in the same session, and then set a flag
    // which will cause this to create extended view info each time from then on in the
    // same session
    params.setExtendedViewInfoMode(true);

    if (!mShowDecorations) {
        params.setForceNoDecor();
    } else {
        if (mAppLabel == null) {
            mAppLabel = "Random App";
        }

        params.setAppLabel(mAppLabel);
        params.setAppIcon(mAppIconName); // ok to be null
    }

    params.setConfigScreenSize(mConfig.getScreenSizeQualifier().getValue());

    if (mOverrideBgColor != null) {
        params.setOverrideBgColor(mOverrideBgColor.intValue());
    }

    // set the Image Overlay as the image factory.
    params.setImageFactory(mImageFactory);

    try {
        return mLayoutLib.createSession(params);
    } catch (RuntimeException t) {
        // Exceptions from the bridge
        mLogger.error(null, t.getLocalizedMessage(), t, null);
        throw t;
    }
}