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

Commit 1fad09d4 authored by Leon Scroggins III's avatar Leon Scroggins III
Browse files

Add ImageDecoder.ImageInfo.getMimeType

Bug: 63909536
Test: CTS: Ib2877276da8464b5f3eef0bbb848de202c90e97e

Allows a listener to determine the mimetype.

Change-Id: I0d2aa32f2dbfb37dba97a896037c48814390273d
parent b1cc8e64
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */

#include "Bitmap.h"
#include "BitmapFactory.h"
#include "ByteBufferStreamAdaptor.h"
#include "CreateJavaOutputStreamAdaptor.h"
#include "GraphicsJNI.h"
@@ -500,6 +501,11 @@ static void ImageDecoder_nClose(JNIEnv* /*env*/, jobject /*clazz*/, jlong native
    delete reinterpret_cast<ImageDecoder*>(nativePtr);
}

static jstring ImageDecoder_nGetMimeType(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
    auto* decoder = reinterpret_cast<ImageDecoder*>(nativePtr);
    return encodedFormatToString(env, decoder->mCodec->getEncodedFormat());
}

static const JNINativeMethod gImageDecoderMethods[] = {
    { "nCreate",        "(J)Landroid/graphics/ImageDecoder;",    (void*) ImageDecoder_nCreateAsset },
    { "nCreate",        "(Ljava/nio/ByteBuffer;II)Landroid/graphics/ImageDecoder;", (void*) ImageDecoder_nCreateByteBuffer },
@@ -511,6 +517,7 @@ static const JNINativeMethod gImageDecoderMethods[] = {
    { "nGetSampledSize","(JI)Landroid/graphics/Point;",          (void*) ImageDecoder_nGetSampledSize },
    { "nGetPadding",    "(JLandroid/graphics/Rect;)V",           (void*) ImageDecoder_nGetPadding },
    { "nClose",         "(J)V",                                  (void*) ImageDecoder_nClose},
    { "nGetMimeType",   "(J)Ljava/lang/String;",                 (void*) ImageDecoder_nGetMimeType },
};

int register_android_graphics_ImageDecoder(JNIEnv* env) {
+39 −8
Original line number Diff line number Diff line
@@ -210,13 +210,31 @@ public final class ImageDecoder implements AutoCloseable {
     *  Contains information about the encoded image.
     */
    public static class ImageInfo {
        /**
         * Width of the image, without scaling or cropping.
         */
        public final int width;

        /**
         * Height of the image, without scaling or cropping.
         */
        public final int height;
        // TODO?: Add more info? mimetype, ninepatch etc?

        ImageInfo(int width, int height) {
            this.width = width;
            this.height = height;
        /* @hide */
        ImageDecoder decoder;

        /* @hide */
        ImageInfo(ImageDecoder decoder) {
            this.width   = decoder.mWidth;
            this.height  = decoder.mHeight;
            this.decoder = decoder;
        }

        /**
         * The mimeType of the image, if known.
         */
        public String getMimeType() {
            return decoder.getMimeType();
        }
    };

@@ -671,8 +689,12 @@ public final class ImageDecoder implements AutoCloseable {
            @Nullable OnHeaderDecodedListener listener) throws IOException {
        try (ImageDecoder decoder = src.createImageDecoder()) {
            if (listener != null) {
                ImageInfo info = new ImageInfo(decoder.mWidth, decoder.mHeight);
                ImageInfo info = new ImageInfo(decoder);
                try {
                    listener.onHeaderDecoded(info, decoder);
                } finally {
                    info.decoder = null;
                }
            }

            decoder.checkState();
@@ -753,8 +775,12 @@ public final class ImageDecoder implements AutoCloseable {
            @Nullable OnHeaderDecodedListener listener) throws IOException {
        try (ImageDecoder decoder = src.createImageDecoder()) {
            if (listener != null) {
                ImageInfo info = new ImageInfo(decoder.mWidth, decoder.mHeight);
                ImageInfo info = new ImageInfo(decoder);
                try {
                    listener.onHeaderDecoded(info, decoder);
                } finally {
                    info.decoder = null;
                }
            }

            decoder.checkState();
@@ -773,6 +799,10 @@ public final class ImageDecoder implements AutoCloseable {
        }
    }

    private String getMimeType() {
        return nGetMimeType(mNativePtr);
    }

    /**
     *  See {@link #decodeBitmap(Source, OnHeaderDecodedListener)}.
     */
@@ -802,4 +832,5 @@ public final class ImageDecoder implements AutoCloseable {
                                                int sampleSize);
    private static native void nGetPadding(long nativePtr, Rect outRect);
    private static native void nClose(long nativePtr);
    private static native String nGetMimeType(long nativePtr);
}