use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by DirtyUnicorns.
the class RenderSessionImpl method insertChild.
/**
* Insert a new child into an existing parent.
* <p>
* {@link #acquire(long)} must have been called before this.
*
* @throws IllegalStateException if the current context is different than the one owned by
* the scene, or if {@link #acquire(long)} was not called.
*
* @see RenderSession#insertChild(Object, ILayoutPullParser, int, IAnimationListener)
*/
public Result insertChild(final ViewGroup parentView, ILayoutPullParser childXml, final int index, IAnimationListener listener) {
checkLock();
BridgeContext context = getContext();
// create a block parser for the XML
BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(childXml, context, false);
// inflate the child without adding it to the root since we want to control where it'll
// get added. We do pass the parentView however to ensure that the layoutParams will
// be created correctly.
final View child = mInflater.inflate(blockParser, parentView, false);
blockParser.ensurePopped();
invalidateRenderingSize();
if (listener != null) {
new AnimationThread(this, "insertChild", listener) {
@Override
public Result preAnimation() {
parentView.setLayoutTransition(new LayoutTransition());
return addView(parentView, child, index);
}
@Override
public void postAnimation() {
parentView.setLayoutTransition(null);
}
}.start();
// always return success since the real status will come through the listener.
return SUCCESS.createResult(child);
}
// add it to the parentView in the correct location
Result result = addView(parentView, child, index);
if (!result.isSuccess()) {
return result;
}
result = render(false);
if (result.isSuccess()) {
result = result.getCopyWithData(child);
}
return result;
}
use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by DirtyUnicorns.
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 DirtyUnicorns.
the class StatusBar method loadIcon.
@Override
protected void loadIcon(int index, String iconName, Density density) {
if (!iconName.endsWith(".xml")) {
super.loadIcon(index, iconName, density);
return;
}
View child = getChildAt(index);
if (child instanceof ImageView) {
ImageView imageView = (ImageView) child;
// The xml is stored only in xhdpi.
IconLoader iconLoader = new IconLoader(iconName, Density.XHIGH, mSimulatedPlatformVersion, null);
InputStream stream = iconLoader.getIcon();
if (stream != null) {
try {
BridgeXmlBlockParser parser = new BridgeXmlBlockParser(ParserFactory.create(stream, null), (BridgeContext) mContext, true);
imageView.setImageDrawable(Drawable.createFromXml(mContext.getResources(), parser));
} catch (XmlPullParserException e) {
Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e, null);
} catch (IOException e) {
Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e, null);
}
}
}
}
use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by DirtyUnicorns.
the class BridgeInflater method inflate.
@Override
public View inflate(int resource, ViewGroup root) {
Context context = getContext();
context = getBaseContext(context);
if (context instanceof BridgeContext) {
BridgeContext bridgeContext = (BridgeContext) context;
ResourceValue value = null;
@SuppressWarnings("deprecation") Pair<ResourceType, String> layoutInfo = Bridge.resolveResourceId(resource);
if (layoutInfo != null) {
value = bridgeContext.getRenderResources().getFrameworkResource(ResourceType.LAYOUT, layoutInfo.getSecond());
} else {
layoutInfo = mLayoutlibCallback.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, true);
BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(parser, bridgeContext, value.isFramework());
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;
}
use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by AOSPA.
the class StatusBar method loadIcon.
@Override
protected void loadIcon(int index, String iconName, Density density) {
if (!iconName.endsWith(".xml")) {
super.loadIcon(index, iconName, density);
return;
}
View child = getChildAt(index);
if (child instanceof ImageView) {
ImageView imageView = (ImageView) child;
// The xml is stored only in xhdpi.
IconLoader iconLoader = new IconLoader(iconName, Density.XHIGH, mSimulatedPlatformVersion, null);
InputStream stream = iconLoader.getIcon();
if (stream != null) {
try {
BridgeXmlBlockParser parser = new BridgeXmlBlockParser(ParserFactory.create(stream, null), (BridgeContext) mContext, true);
imageView.setImageDrawable(Drawable.createFromXml(mContext.getResources(), parser));
} catch (XmlPullParserException e) {
Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e, null);
} catch (IOException e) {
Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e, null);
}
}
}
}
Aggregations