Messaging: FrameSequence -> ImageDecoder

fixes playing / displaying gifs
replaces old deprecated FrameSequence & FrameSequenceDrawable libs

Change-Id: Id65fff90bdd09af86480636a8a0e404aeb39580f
This commit is contained in:
Ido Ben-Hur
2024-09-16 02:25:12 +03:00
committed by Michael Bestas
parent b11039ed8c
commit d8a0bf022d
4 changed files with 31 additions and 52 deletions
@@ -17,47 +17,47 @@ package com.android.messaging.datamodel.media;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.ImageDecoder;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.media.ExifInterface; import android.media.ExifInterface;
import android.support.rastermill.FrameSequence;
import android.support.rastermill.FrameSequenceDrawable;
import com.android.messaging.util.Assert; import com.android.messaging.util.Assert;
import com.android.messaging.util.LogUtil; import com.android.messaging.util.LogUtil;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
public class GifImageResource extends ImageResource { public class GifImageResource extends ImageResource {
private FrameSequence mFrameSequence; private ImageDecoder.Source mImageDecoderSource;
public GifImageResource(String key, FrameSequence frameSequence) { public GifImageResource(String key, ImageDecoder.Source imageDecoderSource) {
// GIF does not support exif tags // GIF does not support exif tags
super(key, ExifInterface.ORIENTATION_NORMAL); super(key, ExifInterface.ORIENTATION_NORMAL);
mFrameSequence = frameSequence; mImageDecoderSource = imageDecoderSource;
} }
public static GifImageResource createGifImageResource(String key, InputStream inputStream) { public static GifImageResource createGifImageResource(String key, InputStream inputStream) {
final FrameSequence frameSequence; final byte[] bytes;
try { try {
frameSequence = FrameSequence.decodeStream(inputStream); bytes = inputStream.readAllBytes();
} finally { } catch (Exception e) {
try { e.printStackTrace();
inputStream.close();
} catch (IOException e) {
// Nothing to do if we fail closing the stream
}
}
if (frameSequence == null) {
return null; return null;
} }
return new GifImageResource(key, frameSequence);
// prepare mImageDecoderSource
final ImageDecoder imageDecoder;
ImageDecoder.Source source = ImageDecoder.createSource(bytes);
if (source == null) {
return null;
}
return new GifImageResource(key, source);
} }
@Override @Override
public Drawable getDrawable(Resources resources) { public Drawable getDrawable(Resources resources) {
try { try {
return new FrameSequenceDrawable(mFrameSequence); return (AnimatedImageDrawable) ImageDecoder.decodeDrawable(mImageDecoderSource);
} catch (final Throwable t) { } catch (final Throwable t) {
// Malicious gif images can make the platform throw different kind of throwables, such // Malicious gif images can make the platform throw different kind of throwables, such
// as OutOfMemoryError and NullPointerException. Catch them all. // as OutOfMemoryError and NullPointerException. Catch them all.
@@ -85,9 +85,6 @@ public class GifImageResource extends ImageResource {
@Override @Override
public boolean supportsBitmapReuse() { public boolean supportsBitmapReuse() {
// FrameSequenceDrawable a.) takes two bitmaps and thus does not fit into the current
// bitmap pool architecture b.) will rarely use bitmaps from one FrameSequenceDrawable to
// the next that are the same sizes since they are used by attachments.
return false; return false;
} }
@@ -107,12 +104,11 @@ public class GifImageResource extends ImageResource {
protected void close() { protected void close() {
acquireLock(); acquireLock();
try { try {
if (mFrameSequence != null) { if (mImageDecoderSource != null) {
mFrameSequence = null; mImageDecoderSource = null;
} }
} finally { } finally {
releaseLock(); releaseLock();
} }
} }
} }
@@ -22,10 +22,10 @@ import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Path; import android.graphics.Path;
import android.graphics.RectF; import android.graphics.RectF;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import android.support.rastermill.FrameSequenceDrawable;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.widget.ImageView; import android.widget.ImageView;
@@ -194,8 +194,8 @@ public class AsyncImageView extends ImageView implements MediaResourceLoadListen
mImageResource = resource; mImageResource = resource;
mImageResource.addRef(); mImageResource.addRef();
setImageDrawable(drawable); setImageDrawable(drawable);
if (drawable instanceof FrameSequenceDrawable) { if (drawable instanceof AnimatedImageDrawable) {
((FrameSequenceDrawable) drawable).start(); ((AnimatedImageDrawable) drawable).start();
} }
if (getVisibility() == VISIBLE) { if (getVisibility() == VISIBLE) {
@@ -249,9 +249,8 @@ public class AsyncImageView extends ImageView implements MediaResourceLoadListen
private void releaseImageResource() { private void releaseImageResource() {
final Drawable drawable = getDrawable(); final Drawable drawable = getDrawable();
if (drawable instanceof FrameSequenceDrawable) { if (drawable instanceof AnimatedImageDrawable) {
((FrameSequenceDrawable) drawable).stop(); ((AnimatedImageDrawable) drawable).stop();
((FrameSequenceDrawable) drawable).destroy();
} }
if (mImageResource != null) { if (mImageResource != null) {
mImageResource.release(); mImageResource.release();
@@ -18,7 +18,6 @@ package com.android.messaging.ui.photoviewer;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.support.rastermill.FrameSequenceDrawable;
import androidx.loader.content.AsyncTaskLoader; import androidx.loader.content.AsyncTaskLoader;
import com.android.ex.photo.PhotoViewController; import com.android.ex.photo.PhotoViewController;
@@ -91,7 +90,6 @@ public class BuglePhotoBitmapLoader extends AsyncTaskLoader<BitmapResult>
final Drawable drawable = result != null ? result.drawable : null; final Drawable drawable = result != null ? result.drawable : null;
if (isReset()) { if (isReset()) {
// An async query came in while the loader is stopped. We don't need the result. // An async query came in while the loader is stopped. We don't need the result.
releaseDrawable(drawable);
return; return;
} }
@@ -140,11 +138,6 @@ public class BuglePhotoBitmapLoader extends AsyncTaskLoader<BitmapResult>
@Override @Override
public void onCanceled(BitmapResult result) { public void onCanceled(BitmapResult result) {
super.onCanceled(result); super.onCanceled(result);
// At this point we can release the resources associated with 'drawable' if needed.
if (result != null) {
releaseDrawable(result.drawable);
}
} }
/** /**
@@ -160,14 +153,6 @@ public class BuglePhotoBitmapLoader extends AsyncTaskLoader<BitmapResult>
releaseImageResource(); releaseImageResource();
} }
private void releaseDrawable(Drawable drawable) {
if (drawable != null && drawable instanceof FrameSequenceDrawable
&& !((FrameSequenceDrawable) drawable).isDestroyed()) {
((FrameSequenceDrawable) drawable).destroy();
}
}
private void setImageResource(final ImageResource resource) { private void setImageResource(final ImageResource resource) {
if (mImageResource != resource) { if (mImageResource != resource) {
// Clear out any information for what is currently used // Clear out any information for what is currently used
@@ -181,7 +166,6 @@ public class BuglePhotoBitmapLoader extends AsyncTaskLoader<BitmapResult>
private void releaseImageResource() { private void releaseImageResource() {
// If we are getting rid of the imageResource backing the drawable, we must also // If we are getting rid of the imageResource backing the drawable, we must also
// destroy the drawable before releasing it. // destroy the drawable before releasing it.
releaseDrawable(mDrawable);
mDrawable = null; mDrawable = null;
if (mImageResource != null) { if (mImageResource != null) {
@@ -189,4 +173,4 @@ public class BuglePhotoBitmapLoader extends AsyncTaskLoader<BitmapResult>
} }
mImageResource = null; mImageResource = null;
} }
} }
@@ -16,8 +16,8 @@
package com.android.messaging.ui.photoviewer; package com.android.messaging.ui.photoviewer;
import android.content.Intent; import android.content.Intent;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.rastermill.FrameSequenceDrawable;
import androidx.loader.content.Loader; import androidx.loader.content.Loader;
import com.android.ex.photo.PhotoViewCallbacks; import com.android.ex.photo.PhotoViewCallbacks;
@@ -75,15 +75,15 @@ public class BuglePhotoViewFragment extends PhotoViewFragment {
private void stopGif() { private void stopGif() {
final Drawable drawable = getDrawable(); final Drawable drawable = getDrawable();
if (drawable != null && drawable instanceof FrameSequenceDrawable) { if (drawable != null && drawable instanceof AnimatedImageDrawable) {
((FrameSequenceDrawable) drawable).stop(); ((AnimatedImageDrawable) drawable).stop();
} }
} }
private void startGif() { private void startGif() {
final Drawable drawable = getDrawable(); final Drawable drawable = getDrawable();
if (drawable != null && drawable instanceof FrameSequenceDrawable) { if (drawable != null && drawable instanceof AnimatedImageDrawable) {
((FrameSequenceDrawable) drawable).start(); ((AnimatedImageDrawable) drawable).start();
} }
} }
} }