use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by DirtyUnicorns.
the class BridgePreferenceInflater method onCreateItem.
@Override
protected Preference onCreateItem(String name, AttributeSet attrs) throws ClassNotFoundException {
Object viewKey = null;
BridgeContext bc = null;
Context context = getContext();
if (context instanceof BridgeContext) {
bc = (BridgeContext) context;
}
if (attrs instanceof BridgeXmlBlockParser) {
viewKey = ((BridgeXmlBlockParser) attrs).getViewCookie();
}
Preference preference = super.onCreateItem(name, attrs);
if (viewKey != null && bc != null) {
bc.addCookie(preference, viewKey);
}
return preference;
}
use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by DirtyUnicorns.
the class BridgeInflater method getViewKeyFromParser.
/*package*/
static Object getViewKeyFromParser(AttributeSet attrs, BridgeContext bc, ResourceReference resourceReference, boolean isInMerge) {
if (!(attrs instanceof BridgeXmlBlockParser)) {
return null;
}
BridgeXmlBlockParser parser = ((BridgeXmlBlockParser) attrs);
// get the view key
Object viewKey = parser.getViewCookie();
if (viewKey == null) {
int currentDepth = parser.getDepth();
// test whether we are in an included file or in a adapter binding view.
BridgeXmlBlockParser previousParser = bc.getPreviousParser();
if (previousParser != null) {
// looks like we are inside an embedded layout.
// only apply the cookie of the calling node (<include>) if we are at the
// top level of the embedded layout. If there is a merge tag, then
// skip it and look for the 2nd level
int testDepth = isInMerge ? 2 : 1;
if (currentDepth == testDepth) {
viewKey = previousParser.getViewCookie();
// if we are in a merge, wrap the cookie in a MergeCookie.
if (viewKey != null && isInMerge) {
viewKey = new MergeCookie(viewKey);
}
}
} else if (resourceReference != null && currentDepth == 1) {
// else if there's a resource reference, this means we are in an adapter
// binding case. Set the resource ref as the view cookie only for the top
// level view.
viewKey = resourceReference;
}
}
return viewKey;
}
use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by DirtyUnicorns.
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 android_frameworks_base by AOSPA.
the class ResourceHelper method getDrawable.
/**
* Returns a drawable from the given value.
* @param value The value that contains a path to a 9 patch, a bitmap or a xml based drawable,
* or an hexadecimal color
* @param context the current context
* @param theme the theme to be used to inflate the drawable.
*/
public static Drawable getDrawable(ResourceValue value, BridgeContext context, Theme theme) {
if (value == null) {
return null;
}
String stringValue = value.getValue();
if (RenderResources.REFERENCE_NULL.equals(stringValue)) {
return null;
}
String lowerCaseValue = stringValue.toLowerCase();
Density density = Density.MEDIUM;
if (value instanceof DensityBasedResourceValue) {
density = ((DensityBasedResourceValue) value).getResourceDensity();
}
if (lowerCaseValue.endsWith(NinePatch.EXTENSION_9PATCH)) {
File file = new File(stringValue);
if (file.isFile()) {
try {
return getNinePatchDrawable(new FileInputStream(file), density, value.isFramework(), stringValue, context);
} catch (IOException e) {
// failed to read the file, we'll return null below.
Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed lot load " + file.getAbsolutePath(), e, null);
}
}
return null;
} else if (lowerCaseValue.endsWith(".xml")) {
// create a block parser for the file
File f = new File(stringValue);
if (f.isFile()) {
try {
// let the framework inflate the Drawable from the XML file.
XmlPullParser parser = ParserFactory.create(f);
BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(parser, context, value.isFramework());
try {
return Drawable.createFromXml(context.getResources(), blockParser, theme);
} finally {
blockParser.ensurePopped();
}
} catch (Exception e) {
// this is an error and not warning since the file existence is checked before
// attempting to parse it.
Bridge.getLog().error(null, "Failed to parse file " + stringValue, e, null);
}
} else {
Bridge.getLog().error(LayoutLog.TAG_BROKEN, String.format("File %s does not exist (or is not a file)", stringValue), null);
}
return null;
} else {
File bmpFile = new File(stringValue);
if (bmpFile.isFile()) {
try {
Bitmap bitmap = Bridge.getCachedBitmap(stringValue, value.isFramework() ? null : context.getProjectKey());
if (bitmap == null) {
bitmap = Bitmap_Delegate.createBitmap(bmpFile, false, /*isMutable*/
density);
Bridge.setCachedBitmap(stringValue, bitmap, value.isFramework() ? null : context.getProjectKey());
}
return new BitmapDrawable(context.getResources(), bitmap);
} catch (IOException e) {
// we'll return null below
Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed lot load " + bmpFile.getAbsolutePath(), e, null);
}
} else {
// attempt to get a color from the value
try {
int color = getColor(stringValue);
return new ColorDrawable(color);
} catch (NumberFormatException e) {
// we'll return null below.
Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, "Failed to convert " + stringValue + " into a drawable", e, null);
}
}
}
return null;
}
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;
}
Aggregations