Search in sources :

Example 1 with NinePatchInputStream

use of com.android.layoutlib.bridge.util.NinePatchInputStream in project platform_frameworks_base by android.

the class Resources_Delegate method openRawResource.

@LayoutlibDelegate
static InputStream openRawResource(Resources resources, int id, TypedValue value) throws NotFoundException {
    getValue(resources, id, value, true);
    String path = value.string.toString();
    File f = new File(path);
    if (f.isFile()) {
        try {
            // and actually load it as a 9-patch instead of a normal bitmap
            if (path.toLowerCase().endsWith(NinePatch.EXTENSION_9PATCH)) {
                return new NinePatchInputStream(f);
            }
            return new FileInputStream(f);
        } catch (FileNotFoundException e) {
            NotFoundException exception = new NotFoundException();
            exception.initCause(e);
            throw exception;
        }
    }
    throw new NotFoundException();
}
Also used : NinePatchInputStream(com.android.layoutlib.bridge.util.NinePatchInputStream) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(android.content.res.Resources.NotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 2 with NinePatchInputStream

use of com.android.layoutlib.bridge.util.NinePatchInputStream in project android_frameworks_base by ResurrectionRemix.

the class Resources_Delegate method openRawResource.

@LayoutlibDelegate
static InputStream openRawResource(Resources resources, int id, TypedValue value) throws NotFoundException {
    getValue(resources, id, value, true);
    String path = value.string.toString();
    File f = new File(path);
    if (f.isFile()) {
        try {
            // and actually load it as a 9-patch instead of a normal bitmap
            if (path.toLowerCase().endsWith(NinePatch.EXTENSION_9PATCH)) {
                return new NinePatchInputStream(f);
            }
            return new FileInputStream(f);
        } catch (FileNotFoundException e) {
            NotFoundException exception = new NotFoundException();
            exception.initCause(e);
            throw exception;
        }
    }
    throw new NotFoundException();
}
Also used : NinePatchInputStream(com.android.layoutlib.bridge.util.NinePatchInputStream) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(android.content.res.Resources.NotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 3 with NinePatchInputStream

use of com.android.layoutlib.bridge.util.NinePatchInputStream in project android_frameworks_base by crdroidandroid.

the class BitmapFactory_Delegate method nativeDecodeStream.

// ------ Native Delegates ------
@LayoutlibDelegate
static /*package*/
Bitmap nativeDecodeStream(InputStream is, byte[] storage, @Nullable Rect padding, @Nullable Options opts) {
    Bitmap bm = null;
    Density density = Density.MEDIUM;
    Set<BitmapCreateFlags> bitmapCreateFlags = EnumSet.of(BitmapCreateFlags.MUTABLE);
    if (opts != null) {
        density = Density.getEnum(opts.inDensity);
        if (opts.inPremultiplied) {
            bitmapCreateFlags.add(BitmapCreateFlags.PREMULTIPLIED);
        }
        opts.inScaled = false;
    }
    try {
        if (is instanceof NinePatchInputStream) {
            NinePatchInputStream npis = (NinePatchInputStream) is;
            npis.disableFakeMarkSupport();
            // load the bitmap as a nine patch
            com.android.ninepatch.NinePatch ninePatch = com.android.ninepatch.NinePatch.load(npis, true, /*is9Patch*/
            false);
            // get the bitmap and chunk objects.
            bm = Bitmap_Delegate.createBitmap(ninePatch.getImage(), bitmapCreateFlags, density);
            NinePatchChunk chunk = ninePatch.getChunk();
            // put the chunk in the bitmap
            bm.setNinePatchChunk(NinePatch_Delegate.serialize(chunk));
            if (padding != null) {
                // read the padding
                int[] paddingArray = chunk.getPadding();
                padding.left = paddingArray[0];
                padding.top = paddingArray[1];
                padding.right = paddingArray[2];
                padding.bottom = paddingArray[3];
            }
        } else {
            // load the bitmap directly.
            bm = Bitmap_Delegate.createBitmap(is, bitmapCreateFlags, density);
        }
    } catch (IOException e) {
        Bridge.getLog().error(null, "Failed to load image", e, null);
    }
    return bm;
}
Also used : NinePatchInputStream(com.android.layoutlib.bridge.util.NinePatchInputStream) NinePatchChunk(com.android.ninepatch.NinePatchChunk) IOException(java.io.IOException) Density(com.android.resources.Density) BitmapCreateFlags(android.graphics.Bitmap_Delegate.BitmapCreateFlags) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 4 with NinePatchInputStream

use of com.android.layoutlib.bridge.util.NinePatchInputStream in project android_frameworks_base by crdroidandroid.

the class Resources_Delegate method openRawResource.

@LayoutlibDelegate
static InputStream openRawResource(Resources resources, int id) throws NotFoundException {
    Pair<String, ResourceValue> value = getResourceValue(resources, id, mPlatformResourceFlag);
    if (value != null) {
        String path = value.getSecond().getValue();
        if (path != null) {
            // check this is a file
            File f = new File(path);
            if (f.isFile()) {
                try {
                    // and actually load it as a 9-patch instead of a normal bitmap
                    if (path.toLowerCase().endsWith(NinePatch.EXTENSION_9PATCH)) {
                        return new NinePatchInputStream(f);
                    }
                    return new FileInputStream(f);
                } 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;
}
Also used : NinePatchInputStream(com.android.layoutlib.bridge.util.NinePatchInputStream) DensityBasedResourceValue(com.android.ide.common.rendering.api.DensityBasedResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(android.content.res.Resources.NotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 5 with NinePatchInputStream

use of com.android.layoutlib.bridge.util.NinePatchInputStream in project platform_frameworks_base by android.

the class Resources_Delegate method openRawResource.

@LayoutlibDelegate
static InputStream openRawResource(Resources resources, int id) throws NotFoundException {
    Pair<String, ResourceValue> value = getResourceValue(resources, id, mPlatformResourceFlag);
    if (value != null) {
        String path = value.getSecond().getValue();
        if (path != null) {
            // check this is a file
            File f = new File(path);
            if (f.isFile()) {
                try {
                    // and actually load it as a 9-patch instead of a normal bitmap
                    if (path.toLowerCase().endsWith(NinePatch.EXTENSION_9PATCH)) {
                        return new NinePatchInputStream(f);
                    }
                    return new FileInputStream(f);
                } 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;
}
Also used : NinePatchInputStream(com.android.layoutlib.bridge.util.NinePatchInputStream) DensityBasedResourceValue(com.android.ide.common.rendering.api.DensityBasedResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(android.content.res.Resources.NotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Aggregations

NinePatchInputStream (com.android.layoutlib.bridge.util.NinePatchInputStream)12 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)12 NotFoundException (android.content.res.Resources.NotFoundException)8 File (java.io.File)8 FileInputStream (java.io.FileInputStream)8 FileNotFoundException (java.io.FileNotFoundException)8 BitmapCreateFlags (android.graphics.Bitmap_Delegate.BitmapCreateFlags)4 ArrayResourceValue (com.android.ide.common.rendering.api.ArrayResourceValue)4 DensityBasedResourceValue (com.android.ide.common.rendering.api.DensityBasedResourceValue)4 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)4 NinePatchChunk (com.android.ninepatch.NinePatchChunk)4 Density (com.android.resources.Density)4 IOException (java.io.IOException)4