use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by AOSPA.
the class ResourceHelper method getInternalComplexColor.
/**
* Returns a {@link ComplexColor} from the given {@link ResourceValue}
*
* @param resValue the value containing a color value or a file path to a complex color
* definition
* @param context the current context
* @param theme the theme to use when resolving the complex color
* @param allowGradients when false, only {@link ColorStateList} will be returned. If a {@link
* GradientColor} is found, null will be returned.
*/
@Nullable
private static ComplexColor getInternalComplexColor(@NonNull ResourceValue resValue, @NonNull BridgeContext context, @Nullable Theme theme, boolean allowGradients) {
String value = resValue.getValue();
if (value == null || RenderResources.REFERENCE_NULL.equals(value)) {
return null;
}
XmlPullParser parser = null;
// first check if the value is a file (xml most likely)
Boolean psiParserSupport = context.getLayoutlibCallback().getFlag(RenderParamsFlags.FLAG_KEY_XML_FILE_PARSER_SUPPORT);
if (psiParserSupport != null && psiParserSupport) {
parser = context.getLayoutlibCallback().getXmlFileParser(value);
}
if (parser == null) {
File f = new File(value);
if (f.isFile()) {
// providing an XmlPullParser
try {
parser = ParserFactory.create(f);
} catch (XmlPullParserException | FileNotFoundException e) {
Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed to parse file " + value, e, null);
}
}
}
if (parser != null) {
try {
BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(parser, context, resValue.isFramework());
try {
// Advance the parser to the first element so we can detect if it's a
// color list or a gradient color
int type;
//noinspection StatementWithEmptyBody
while ((type = blockParser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
// Seek parser to start tag.
}
if (type != XmlPullParser.START_TAG) {
throw new XmlPullParserException("No start tag found");
}
final String name = blockParser.getName();
if (allowGradients && "gradient".equals(name)) {
return ComplexColor_Accessor.createGradientColorFromXmlInner(context.getResources(), blockParser, blockParser, theme);
} else if ("selector".equals(name)) {
return ComplexColor_Accessor.createColorStateListFromXmlInner(context.getResources(), blockParser, blockParser, theme);
}
} finally {
blockParser.ensurePopped();
}
} catch (XmlPullParserException e) {
Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Failed to configure parser for " + value, e, null);
// we'll return null below.
} catch (Exception e) {
// this is an error and not warning since the file existence is
// checked before attempting to parse it.
Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed to parse file " + value, e, null);
return null;
}
} else {
// try to load the color state list from an int
try {
int color = getColor(value);
return ColorStateList.valueOf(color);
} catch (NumberFormatException e) {
Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, "Failed to convert " + value + " into a ColorStateList", e, null);
}
}
return null;
}
use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by AOSPA.
the class RenderSessionImpl method init.
/**
* Initializes and acquires the scene, creating various Android objects such as context,
* inflater, and parser.
*
* @param timeout the time to wait if another rendering is happening.
*
* @return whether the scene was prepared
*
* @see #acquire(long)
* @see #release()
*/
@Override
public Result init(long timeout) {
Result result = super.init(timeout);
if (!result.isSuccess()) {
return result;
}
SessionParams params = getParams();
BridgeContext context = getContext();
// use default of true in case it's not found to use alpha by default
mIsAlphaChannelImage = ResourceHelper.getBooleanThemeValue(params.getResources(), "windowIsFloating", true, true);
mLayoutBuilder = new Layout.Builder(params, context);
// FIXME: find those out, and possibly add them to the render params
boolean hasNavigationBar = true;
//noinspection ConstantConditions
IWindowManager iwm = new IWindowManagerImpl(getContext().getConfiguration(), context.getMetrics(), Surface.ROTATION_0, hasNavigationBar);
WindowManagerGlobal_Delegate.setWindowManagerService(iwm);
// build the inflater and parser.
mInflater = new BridgeInflater(context, params.getLayoutlibCallback());
context.setBridgeInflater(mInflater);
mBlockParser = new BridgeXmlBlockParser(params.getLayoutDescription(), context, false);
return SUCCESS.createResult();
}
use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project platform_frameworks_base by android.
the class Resources_Delegate method getXml.
@LayoutlibDelegate
static XmlResourceParser getXml(Resources resources, int id) throws NotFoundException {
Pair<String, ResourceValue> value = getResourceValue(resources, id, mPlatformResourceFlag);
if (value != null) {
String v = value.getSecond().getValue();
if (v != null) {
// check this is a file
File f = new File(v);
if (f.isFile()) {
try {
XmlPullParser parser = ParserFactory.create(f);
return new BridgeXmlBlockParser(parser, resources.mContext, mPlatformResourceFlag[0]);
} catch (XmlPullParserException e) {
NotFoundException newE = new NotFoundException();
newE.initCause(e);
throw newE;
} catch (FileNotFoundException e) {
NotFoundException newE = new NotFoundException();
newE.initCause(e);
throw newE;
}
}
}
}
// id was not found or not resolved. Throw a NotFoundException.
throwException(resources, id);
// this is not used since the method above always throws
return null;
}
use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project platform_frameworks_base by android.
the class Resources_Delegate method getLayout.
@LayoutlibDelegate
static XmlResourceParser getLayout(Resources resources, int id) throws NotFoundException {
Pair<String, ResourceValue> v = getResourceValue(resources, id, mPlatformResourceFlag);
if (v != null) {
ResourceValue value = v.getSecond();
XmlPullParser parser = null;
try {
// check if the current parser can provide us with a custom parser.
if (!mPlatformResourceFlag[0]) {
parser = resources.mLayoutlibCallback.getParser(value);
}
// create a new one manually if needed.
if (parser == null) {
File xml = new File(value.getValue());
if (xml.isFile()) {
// we need to create a pull parser around the layout XML file, and then
// give that to our XmlBlockParser
parser = ParserFactory.create(xml, true);
}
}
if (parser != null) {
return new BridgeXmlBlockParser(parser, resources.mContext, mPlatformResourceFlag[0]);
}
} catch (XmlPullParserException e) {
Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Failed to configure parser for " + value.getValue(), e, null);
// we'll return null below.
} catch (FileNotFoundException e) {
// this shouldn't happen since we check above.
}
}
// id was not found or not resolved. Throw a NotFoundException.
throwException(resources, id);
// this is not used since the method above always throws
return null;
}
use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project platform_frameworks_base by android.
the class Resources_Delegate method loadXmlResourceParser.
@LayoutlibDelegate
static XmlResourceParser loadXmlResourceParser(Resources resources, String file, int id, int assetCookie, String type) throws NotFoundException {
// even though we know the XML file to load directly, we still need to resolve the
// id so that we can know if it's a platform or project resource.
// (mPlatformResouceFlag will get the result and will be used later).
getResourceValue(resources, id, mPlatformResourceFlag);
File f = new File(file);
try {
XmlPullParser parser = ParserFactory.create(f);
return new BridgeXmlBlockParser(parser, resources.mContext, mPlatformResourceFlag[0]);
} catch (XmlPullParserException e) {
NotFoundException newE = new NotFoundException();
newE.initCause(e);
throw newE;
} catch (FileNotFoundException e) {
NotFoundException newE = new NotFoundException();
newE.initCause(e);
throw newE;
}
}
Aggregations