Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 499f8819 authored by Xavier Ducrohet's avatar Xavier Ducrohet
Browse files

Add density support to layoutlib so that bitmap are scaled if needed.

This is integrated from Eclair branch.

DO NOT MERGE
parent 50128088
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -341,6 +341,7 @@ public final class Bridge implements ILayoutBridge {
        try {
            // setup the display Metrics.
            DisplayMetrics metrics = new DisplayMetrics();
            metrics.densityDpi = density;
            metrics.density = density / (float) DisplayMetrics.DENSITY_DEFAULT;
            metrics.scaledDensity = metrics.density;
            metrics.widthPixels = screenWidth;
@@ -388,7 +389,7 @@ public final class Bridge implements ILayoutBridge {

            // get the background drawable
            if (windowBackground != null) {
                Drawable d = ResourceHelper.getDrawable(windowBackground.getValue(),
                Drawable d = ResourceHelper.getDrawable(windowBackground,
                        context, true /* isFramework */);
                root.setBackgroundDrawable(d);
            }
+1 −1
Original line number Diff line number Diff line
@@ -124,7 +124,7 @@ public final class BridgeResources extends Resources {
        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            return ResourceHelper.getDrawable(value.getValue(), mContext, value.isFramework());
            return ResourceHelper.getDrawable(value, mContext, value.isFramework());
        }

        // id was not found or not resolved. Throw a NotFoundException.
+3 −2
Original line number Diff line number Diff line
@@ -659,8 +659,9 @@ public final class BridgeTypedArray extends TypedArray {
            return null;
        }

        String value = mData[index].getValue();
        if (value == null || BridgeConstants.REFERENCE_NULL.equals(value)) {
        IResourceValue value = mData[index];
        String stringValue = value.getValue();
        if (stringValue == null || BridgeConstants.REFERENCE_NULL.equals(stringValue)) {
            return null;
        }

+71 −42
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.layoutlib.bridge;

import com.android.layoutlib.api.IDensityBasedResourceValue;
import com.android.layoutlib.api.IResourceValue;
import com.android.layoutlib.api.IDensityBasedResourceValue.Density;
import com.android.ninepatch.NinePatch;

import org.kxml2.io.KXmlParser;
@@ -96,28 +99,30 @@ public final class ResourceHelper {

    /**
     * Returns a drawable from the given value.
     * @param value The value. A path to a 9 patch, a bitmap or a xml based drawable,
     * @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
     * @param isFramework indicates whether the resource is a framework resources.
     * Framework resources are cached, and loaded only once.
     */
    public static Drawable getDrawable(String value, BridgeContext context, boolean isFramework) {
    public static Drawable getDrawable(IResourceValue value, BridgeContext context, boolean isFramework) {
        Drawable d = null;

        String lowerCaseValue = value.toLowerCase();
        String stringValue = value.getValue();

        String lowerCaseValue = stringValue.toLowerCase();

        if (lowerCaseValue.endsWith(NinePatch.EXTENSION_9PATCH)) {
            File f = new File(value);
            if (f.isFile()) {
                NinePatch ninePatch = Bridge.getCached9Patch(value,
            File file = new File(stringValue);
            if (file.isFile()) {
                NinePatch ninePatch = Bridge.getCached9Patch(stringValue,
                        isFramework ? null : context.getProjectKey());

                if (ninePatch == null) {
                    try {
                        ninePatch = NinePatch.load(new File(value).toURL(), false /* convert */);
                        ninePatch = NinePatch.load(file.toURL(), false /* convert */);

                        Bridge.setCached9Patch(value, ninePatch,
                        Bridge.setCached9Patch(stringValue, ninePatch,
                                isFramework ? null : context.getProjectKey());
                    } catch (MalformedURLException e) {
                        // URL is wrong, we'll return null below
@@ -134,7 +139,7 @@ public final class ResourceHelper {
            return null;
        } else if (lowerCaseValue.endsWith(".xml")) {
            // create a blockparser for the file
            File f = new File(value);
            File f = new File(stringValue);
            if (f.isFile()) {
                try {
                    // let the framework inflate the Drawable from the XML file.
@@ -157,19 +162,43 @@ public final class ResourceHelper {

            return null;
        } else {
            File bmpFile = new File(value);
            File bmpFile = new File(stringValue);
            if (bmpFile.isFile()) {
                try {
                    Bitmap bitmap = Bridge.getCachedBitmap(value,
                    Bitmap bitmap = Bridge.getCachedBitmap(stringValue,
                            isFramework ? null : context.getProjectKey());

                    if (bitmap == null) {
                        bitmap = new Bitmap(bmpFile);
                        Bridge.setCachedBitmap(value, bitmap,
                        try {
                            bitmap.setDensity(Density.MEDIUM.getValue());
                        } catch (NoClassDefFoundError error) {
                            // look like we're running in an older version of ADT that doesn't
                            // include the new layoutlib_api. Let's just ignore this, the drawing
                            // will just be wrong.
                        }
                        Bridge.setCachedBitmap(stringValue, bitmap,
                                isFramework ? null : context.getProjectKey());
                    }

                    return new BitmapDrawable(bitmap);
                    try {
                        if (value instanceof IDensityBasedResourceValue) {
                            Density density = ((IDensityBasedResourceValue)value).getDensity();
                            if (density != Density.MEDIUM) {
                                // create a copy of the bitmap
                                bitmap = Bitmap.createBitmap(bitmap);

                                // apply the density
                                bitmap.setDensity(density.getValue());
                            }
                        }
                    } catch (NoClassDefFoundError error) {
                        // look like we're running in an older version of ADT that doesn't include
                        // the new layoutlib_api. Let's just ignore this, the drawing will just be
                        // wrong.
                    }

                    return new BitmapDrawable(context.getResources(), bitmap);
                } catch (IOException e) {
                    // we'll return null below
                    // TODO: log the error.
@@ -177,7 +206,7 @@ public final class ResourceHelper {
            } else {
                // attempt to get a color from the value
                try {
                    int color = getColor(value);
                    int color = getColor(stringValue);
                    return new ColorDrawable(color);
                } catch (NumberFormatException e) {
                    // we'll return null below.