NekoX/TMessagesProj/src/main/java/org/telegram/messenger/ImageReceiver.java

1446 lines
55 KiB
Java
Raw Normal View History

/*
* This is the source code of Telegram for Android v. 1.3.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
*/
2015-09-24 22:52:02 +02:00
package org.telegram.messenger;
import android.graphics.Bitmap;
2014-10-21 22:35:16 +02:00
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
2014-10-21 22:35:16 +02:00
import android.graphics.Matrix;
import android.graphics.Paint;
2015-01-02 23:15:07 +01:00
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
2014-10-21 22:35:16 +02:00
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
2019-01-23 18:03:33 +01:00
import android.support.annotation.Keep;
import android.view.View;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.TLObject;
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.Components.AnimatedFileDrawable;
2019-01-23 18:03:33 +01:00
import org.telegram.ui.Components.RecyclableDrawable;
2015-02-01 19:51:02 +01:00
public class ImageReceiver implements NotificationCenter.NotificationCenterDelegate {
public interface ImageReceiverDelegate {
void didSetImage(ImageReceiver imageReceiver, boolean set, boolean thumb);
2015-02-01 19:51:02 +01:00
}
2018-07-30 04:07:02 +02:00
public static class BitmapHolder {
private String key;
public Bitmap bitmap;
public BitmapHolder(Bitmap b, String k) {
bitmap = b;
key = k;
if (key != null) {
ImageLoader.getInstance().incrementUseCount(key);
}
}
public int getWidth() {
return bitmap != null ? bitmap.getWidth() : 0;
}
public int getHeight() {
return bitmap != null ? bitmap.getHeight() : 0;
}
public boolean isRecycled() {
return bitmap == null || bitmap.isRecycled();
}
public void release() {
if (key == null) {
bitmap = null;
return;
}
boolean canDelete = ImageLoader.getInstance().decrementUseCount(key);
if (!ImageLoader.getInstance().isInCache(key)) {
if (canDelete) {
bitmap.recycle();
}
}
key = null;
bitmap = null;
}
}
private class SetImageBackup {
2019-02-08 03:30:32 +01:00
public Object fileLocation;
public String filter;
public Drawable thumb;
2019-02-08 03:30:32 +01:00
public Object thumbLocation;
public String thumbFilter;
public int size;
public int cacheType;
2019-01-23 18:03:33 +01:00
public Object parentObject;
2015-05-21 23:27:27 +02:00
public String ext;
}
2018-07-30 04:07:02 +02:00
private int currentAccount;
2015-02-01 19:51:02 +01:00
private View parentView;
2018-07-30 04:07:02 +02:00
private int tag;
private int thumbTag;
2017-12-08 18:35:59 +01:00
private int param;
2019-01-23 18:03:33 +01:00
private Object currentParentObject;
2015-02-01 19:51:02 +01:00
private boolean canceledLoading;
2016-04-22 15:49:00 +02:00
private static PorterDuffColorFilter selectedColorFilter = new PorterDuffColorFilter(0xffdddddd, PorterDuff.Mode.MULTIPLY);
2017-12-08 18:35:59 +01:00
private static PorterDuffColorFilter selectedGroupColorFilter = new PorterDuffColorFilter(0xffbbbbbb, PorterDuff.Mode.MULTIPLY);
private boolean forceLoding;
2015-02-01 19:51:02 +01:00
private SetImageBackup setImageBackup;
2019-02-08 03:30:32 +01:00
private Object currentImageLocation;
2015-02-01 19:51:02 +01:00
private String currentKey;
private String currentThumbKey;
private String currentFilter;
private String currentThumbFilter;
2015-05-21 23:27:27 +02:00
private String currentExt;
2019-02-08 03:30:32 +01:00
private Object currentThumbLocation;
2015-02-01 19:51:02 +01:00
private int currentSize;
private int currentCacheType;
private Drawable currentImage;
private Drawable currentThumb;
2015-02-01 19:51:02 +01:00
private Drawable staticThumb;
private boolean allowStartAnimation = true;
2017-07-08 18:32:04 +02:00
private boolean allowDecodeSingleFrame;
2015-02-01 19:51:02 +01:00
2017-12-08 18:35:59 +01:00
private boolean crossfadeWithOldImage;
2019-01-23 18:03:33 +01:00
private boolean crossfadingWithThumb;
2017-12-08 18:35:59 +01:00
private Drawable crossfadeImage;
private String crossfadeKey;
private BitmapShader crossfadeShader;
2015-02-01 19:51:02 +01:00
private boolean needsQualityThumb;
private boolean shouldGenerateQualityThumb;
2019-01-23 18:03:33 +01:00
private boolean currentKeyQuality;
2015-05-21 23:27:27 +02:00
private boolean invalidateAll;
2015-02-01 19:51:02 +01:00
private int imageX, imageY, imageW, imageH;
private Rect drawRegion = new Rect();
private boolean isVisible = true;
2015-02-01 19:51:02 +01:00
private boolean isAspectFit;
private boolean forcePreview;
private boolean forceCrossfade;
2015-02-01 19:51:02 +01:00
private int roundRadius;
private BitmapShader bitmapShader;
2016-04-22 15:49:00 +02:00
private BitmapShader bitmapShaderThumb;
2017-12-08 18:35:59 +01:00
private Paint roundPaint;
2016-04-22 15:49:00 +02:00
private RectF roundRect = new RectF();
private RectF bitmapRect = new RectF();
private Matrix shaderMatrix = new Matrix();
private float overrideAlpha = 1.0f;
2017-12-08 18:35:59 +01:00
private int isPressed;
private int orientation;
2019-01-23 18:03:33 +01:00
private int thumbOrientation;
private boolean centerRotation;
2015-02-01 19:51:02 +01:00
private ImageReceiverDelegate delegate;
private float currentAlpha;
private long lastUpdateAlphaTime;
private byte crossfadeAlpha = 1;
private boolean manualAlphaAnimator;
private boolean crossfadeWithThumb;
private ColorFilter colorFilter;
public ImageReceiver() {
2016-04-22 15:49:00 +02:00
this(null);
}
public ImageReceiver(View view) {
parentView = view;
2019-01-23 18:03:33 +01:00
roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
2018-07-30 04:07:02 +02:00
currentAccount = UserConfig.selectedAccount;
}
2015-02-01 19:51:02 +01:00
public void cancelLoadImage() {
2017-12-08 18:35:59 +01:00
forceLoding = false;
2015-02-01 19:51:02 +01:00
ImageLoader.getInstance().cancelLoadingForImageReceiver(this, 0);
canceledLoading = true;
}
2017-12-08 18:35:59 +01:00
public void setForceLoading(boolean value) {
forceLoding = value;
}
public boolean isForceLoding() {
return forceLoding;
}
2019-01-23 18:03:33 +01:00
public void setImage(TLObject path, String filter, Drawable thumb, String ext, Object parentObject, int cacheType) {
2019-02-08 03:30:32 +01:00
setImage(path, filter, thumb, null, null, 0, ext, parentObject, cacheType);
}
2019-01-23 18:03:33 +01:00
public void setImage(TLObject path, String filter, Drawable thumb, int size, String ext, Object parentObject, int cacheType) {
2019-02-08 03:30:32 +01:00
setImage(path, filter, thumb, null, null, size, ext, parentObject, cacheType);
}
2015-05-21 23:27:27 +02:00
public void setImage(String httpUrl, String filter, Drawable thumb, String ext, int size) {
2019-02-08 03:30:32 +01:00
setImage(httpUrl, filter, thumb, null, null, size, ext, null, 1);
2015-02-01 19:51:02 +01:00
}
2019-01-23 18:03:33 +01:00
public void setImage(TLObject fileLocation, String filter, TLObject thumbLocation, String thumbFilter, String ext, Object parentObject, int cacheType) {
2019-02-08 03:30:32 +01:00
setImage(fileLocation, filter, null, thumbLocation, thumbFilter, 0, ext, parentObject, cacheType);
2015-02-01 19:51:02 +01:00
}
2019-01-23 18:03:33 +01:00
public void setImage(TLObject fileLocation, String filter, TLObject thumbLocation, String thumbFilter, int size, String ext, Object parentObject, int cacheType) {
2019-02-08 03:30:32 +01:00
setImage(fileLocation, filter, null, thumbLocation, thumbFilter, size, ext, parentObject, cacheType);
2015-02-01 19:51:02 +01:00
}
2019-02-08 03:30:32 +01:00
public void setImage(Object fileLocation, String filter, Drawable thumb, Object thumbLocation, String thumbFilter, int size, String ext, Object parentObject, int cacheType) {
if (setImageBackup != null) {
setImageBackup.fileLocation = null;
setImageBackup.thumbLocation = null;
setImageBackup.thumb = null;
}
2019-02-08 03:30:32 +01:00
if ((fileLocation == null && thumbLocation == null)
2015-02-01 19:51:02 +01:00
|| (fileLocation != null && !(fileLocation instanceof TLRPC.TL_fileLocation)
&& !(fileLocation instanceof TLRPC.TL_fileEncryptedLocation)
2016-03-06 02:49:31 +01:00
&& !(fileLocation instanceof TLRPC.TL_document)
2018-07-30 04:07:02 +02:00
&& !(fileLocation instanceof WebFile)
&& !(fileLocation instanceof TLRPC.TL_documentEncrypted)
2019-01-23 18:03:33 +01:00
&& !(fileLocation instanceof TLRPC.PhotoSize)
2019-02-08 03:30:32 +01:00
&& !(fileLocation instanceof SecureDocument)
&& !(fileLocation instanceof String))) {
2017-12-08 18:35:59 +01:00
for (int a = 0; a < 3; a++) {
recycleBitmap(null, a);
}
2015-02-01 19:51:02 +01:00
currentKey = null;
2015-05-21 23:27:27 +02:00
currentExt = ext;
2015-02-01 19:51:02 +01:00
currentThumbKey = null;
currentThumbFilter = null;
currentImageLocation = null;
currentFilter = null;
2019-01-23 18:03:33 +01:00
currentParentObject = null;
currentCacheType = 0;
2015-02-01 19:51:02 +01:00
staticThumb = thumb;
currentAlpha = 1;
2015-02-01 19:51:02 +01:00
currentThumbLocation = null;
currentSize = 0;
currentImage = null;
2015-02-01 19:51:02 +01:00
bitmapShader = null;
2016-04-22 15:49:00 +02:00
bitmapShaderThumb = null;
2017-12-08 18:35:59 +01:00
crossfadeShader = null;
2015-02-01 19:51:02 +01:00
ImageLoader.getInstance().cancelLoadingForImageReceiver(this, 0);
if (parentView != null) {
2015-05-21 23:27:27 +02:00
if (invalidateAll) {
parentView.invalidate();
} else {
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
}
}
2015-02-01 19:51:02 +01:00
if (delegate != null) {
delegate.didSetImage(this, currentImage != null || currentThumb != null || staticThumb != null, currentImage == null);
}
return;
}
2015-02-01 19:51:02 +01:00
2019-02-08 03:30:32 +01:00
if (!(thumbLocation instanceof String) && !(thumbLocation instanceof TLRPC.PhotoSize) && !(thumbLocation instanceof TLRPC.TL_fileLocation) && !(thumbLocation instanceof TLRPC.TL_fileEncryptedLocation)) {
2015-02-01 19:51:02 +01:00
thumbLocation = null;
}
2015-01-02 23:15:07 +01:00
String key = null;
2019-02-08 03:30:32 +01:00
if (fileLocation instanceof SecureDocument) {
SecureDocument document = (SecureDocument) fileLocation;
key = document.secureFile.dc_id + "_" + document.secureFile.id;
} else if (fileLocation instanceof TLRPC.FileLocation) {
TLRPC.FileLocation location = (TLRPC.FileLocation) fileLocation;
key = location.volume_id + "_" + location.local_id;
} else if (fileLocation instanceof TLRPC.TL_photoStrippedSize) {
TLRPC.TL_photoStrippedSize location = (TLRPC.TL_photoStrippedSize) fileLocation;
key = "stripped" + FileRefController.getKeyForParentObject(parentObject);
} else if (fileLocation instanceof TLRPC.TL_photoSize || fileLocation instanceof TLRPC.TL_photoCachedSize) {
TLRPC.PhotoSize photoSize = (TLRPC.PhotoSize) fileLocation;
key = photoSize.location.volume_id + "_" + photoSize.location.local_id;
} else if (fileLocation instanceof WebFile) {
WebFile location = (WebFile) fileLocation;
key = Utilities.MD5(location.url);
} else if (fileLocation instanceof TLRPC.Document) {
TLRPC.Document location = (TLRPC.Document) fileLocation;
if (location.dc_id != 0) {
key = location.dc_id + "_" + location.id;
2015-09-24 22:52:02 +02:00
} else {
2019-02-08 03:30:32 +01:00
fileLocation = null;
2015-01-02 23:15:07 +01:00
}
2019-02-08 03:30:32 +01:00
} else if (fileLocation instanceof String) {
key = Utilities.MD5((String) fileLocation);
}
2019-02-08 03:30:32 +01:00
2019-01-23 18:03:33 +01:00
currentKeyQuality = false;
if (key == null && needsQualityThumb && parentObject instanceof MessageObject) {
TLRPC.Document document = ((MessageObject) parentObject).getDocument();
if (document != null && document.dc_id != 0 && document.id != 0) {
key = "q_" + document.dc_id + "_" + document.id;
currentKeyQuality = true;
}
}
2019-01-23 18:03:33 +01:00
if (key != null && filter != null) {
key += "@" + filter;
}
2015-02-01 19:51:02 +01:00
2019-01-23 18:03:33 +01:00
if (currentKey != null && currentKey.equals(key)) {
2015-02-01 19:51:02 +01:00
if (delegate != null) {
delegate.didSetImage(this, currentImage != null || currentThumb != null || staticThumb != null, currentImage == null);
2015-01-02 23:15:07 +01:00
}
2015-02-01 19:51:02 +01:00
if (!canceledLoading && !forcePreview) {
return;
}
}
2015-02-01 19:51:02 +01:00
String thumbKey = null;
2019-01-23 18:03:33 +01:00
if (thumbLocation instanceof TLRPC.FileLocation) {
TLRPC.FileLocation location = (TLRPC.FileLocation) thumbLocation;
thumbKey = location.volume_id + "_" + location.local_id;
} else if (thumbLocation instanceof TLRPC.TL_photoStrippedSize) {
TLRPC.TL_photoStrippedSize location = (TLRPC.TL_photoStrippedSize) thumbLocation;
thumbKey = "stripped" + FileRefController.getKeyForParentObject(parentObject);
2019-02-08 03:30:32 +01:00
} else if (thumbLocation instanceof TLRPC.TL_photoSize || thumbLocation instanceof TLRPC.TL_photoCachedSize) {
2019-01-23 18:03:33 +01:00
TLRPC.PhotoSize photoSize = (TLRPC.PhotoSize) thumbLocation;
thumbKey = photoSize.location.volume_id + "_" + photoSize.location.local_id;
2019-02-08 03:30:32 +01:00
} else if (thumbLocation instanceof String) {
thumbKey = Utilities.MD5((String) thumbLocation);
2019-01-23 18:03:33 +01:00
}
if (thumbKey != null && thumbFilter != null) {
thumbKey += "@" + thumbFilter;
}
2015-02-01 19:51:02 +01:00
2017-12-08 18:35:59 +01:00
if (crossfadeWithOldImage) {
if (currentImage != null) {
recycleBitmap(thumbKey, 1);
recycleBitmap(null, 2);
crossfadeShader = bitmapShader;
crossfadeImage = currentImage;
crossfadeKey = currentKey;
2019-01-23 18:03:33 +01:00
crossfadingWithThumb = false;
2017-12-08 18:35:59 +01:00
currentImage = null;
currentKey = null;
} else if (currentThumb != null) {
recycleBitmap(key, 0);
recycleBitmap(null, 2);
crossfadeShader = bitmapShaderThumb;
crossfadeImage = currentThumb;
crossfadeKey = currentThumbKey;
2019-01-23 18:03:33 +01:00
crossfadingWithThumb = false;
currentThumb = null;
currentThumbKey = null;
} else if (staticThumb != null) {
recycleBitmap(key, 0);
recycleBitmap(thumbKey, 1);
recycleBitmap(null, 2);
crossfadeShader = bitmapShaderThumb;
crossfadeImage = staticThumb;
crossfadingWithThumb = false;
crossfadeKey = null;
2017-12-08 18:35:59 +01:00
currentThumb = null;
currentThumbKey = null;
} else {
recycleBitmap(key, 0);
recycleBitmap(thumbKey, 1);
recycleBitmap(null, 2);
crossfadeShader = null;
}
} else {
recycleBitmap(key, 0);
recycleBitmap(thumbKey, 1);
recycleBitmap(null, 2);
crossfadeShader = null;
}
2015-02-01 19:51:02 +01:00
2019-01-23 18:03:33 +01:00
currentParentObject = parentObject;
2015-02-01 19:51:02 +01:00
currentThumbKey = thumbKey;
currentKey = key;
2015-05-21 23:27:27 +02:00
currentExt = ext;
2015-02-01 19:51:02 +01:00
currentImageLocation = fileLocation;
currentFilter = filter;
currentThumbFilter = thumbFilter;
currentSize = size;
currentCacheType = cacheType;
2015-02-01 19:51:02 +01:00
currentThumbLocation = thumbLocation;
staticThumb = thumb;
bitmapShader = null;
2016-04-22 15:49:00 +02:00
bitmapShaderThumb = null;
currentAlpha = 1.0f;
2015-02-01 19:51:02 +01:00
if (delegate != null) {
delegate.didSetImage(this, currentImage != null || currentThumb != null || staticThumb != null, currentImage == null);
2014-10-21 22:35:16 +02:00
}
2015-02-01 19:51:02 +01:00
ImageLoader.getInstance().loadImageForImageReceiver(this);
2014-08-29 23:06:04 +02:00
if (parentView != null) {
2015-05-21 23:27:27 +02:00
if (invalidateAll) {
parentView.invalidate();
} else {
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
}
}
}
2019-01-23 18:03:33 +01:00
public boolean canInvertBitmap() {
return currentImage instanceof ExtendedBitmapDrawable || currentThumb instanceof ExtendedBitmapDrawable || staticThumb instanceof ExtendedBitmapDrawable;
}
public void setColorFilter(ColorFilter filter) {
colorFilter = filter;
}
2015-02-01 19:51:02 +01:00
public void setDelegate(ImageReceiverDelegate delegate) {
this.delegate = delegate;
}
2017-12-08 18:35:59 +01:00
public void setPressed(int value) {
2015-01-02 23:15:07 +01:00
isPressed = value;
}
public boolean getPressed() {
2017-12-08 18:35:59 +01:00
return isPressed != 0;
2015-01-02 23:15:07 +01:00
}
public void setOrientation(int angle, boolean center) {
2016-03-06 02:49:31 +01:00
while (angle < 0) {
angle += 360;
}
while (angle > 360) {
angle -= 360;
}
2019-01-23 18:03:33 +01:00
orientation = thumbOrientation = angle;
centerRotation = center;
}
2015-05-21 23:27:27 +02:00
public void setInvalidateAll(boolean value) {
invalidateAll = value;
}
2017-03-31 01:58:05 +02:00
public Drawable getStaticThumb() {
return staticThumb;
}
2016-10-11 13:57:01 +02:00
public int getAnimatedOrientation() {
if (currentImage instanceof AnimatedFileDrawable) {
return ((AnimatedFileDrawable) currentImage).getOrientation();
} else if (staticThumb instanceof AnimatedFileDrawable) {
return ((AnimatedFileDrawable) staticThumb).getOrientation();
2019-01-23 18:03:33 +01:00
} else if (currentImage instanceof ExtendedBitmapDrawable) {
return ((ExtendedBitmapDrawable) currentImage).getOrientation();
2016-10-11 13:57:01 +02:00
}
return 0;
}
public int getOrientation() {
return orientation;
}
public void setImageBitmap(Bitmap bitmap) {
2015-01-02 23:15:07 +01:00
setImageBitmap(bitmap != null ? new BitmapDrawable(null, bitmap) : null);
}
public void setImageBitmap(Drawable bitmap) {
2015-02-01 19:51:02 +01:00
ImageLoader.getInstance().cancelLoadingForImageReceiver(this, 0);
2019-01-23 18:03:33 +01:00
if (crossfadeWithOldImage) {
if (currentImage != null) {
recycleBitmap(null, 1);
recycleBitmap(null, 2);
crossfadeShader = bitmapShader;
crossfadeImage = currentImage;
crossfadeKey = currentKey;
crossfadingWithThumb = true;
} else if (currentThumb != null) {
recycleBitmap(null, 0);
recycleBitmap(null, 2);
crossfadeShader = bitmapShaderThumb;
crossfadeImage = currentThumb;
crossfadeKey = currentThumbKey;
crossfadingWithThumb = true;
} else if (staticThumb != null) {
recycleBitmap(null, 0);
recycleBitmap(null, 1);
recycleBitmap(null, 2);
crossfadeShader = bitmapShaderThumb;
crossfadeImage = staticThumb;
crossfadingWithThumb = true;
crossfadeKey = null;
} else {
for (int a = 0; a < 3; a++) {
recycleBitmap(null, a);
}
crossfadeShader = null;
}
} else {
for (int a = 0; a < 3; a++) {
recycleBitmap(null, a);
}
}
if (staticThumb instanceof RecyclableDrawable) {
RecyclableDrawable drawable = (RecyclableDrawable) staticThumb;
drawable.recycle();
2017-12-08 18:35:59 +01:00
}
2015-02-01 19:51:02 +01:00
staticThumb = bitmap;
2017-12-08 18:35:59 +01:00
if (roundRadius != 0 && bitmap instanceof BitmapDrawable) {
Bitmap object = ((BitmapDrawable) bitmap).getBitmap();
bitmapShaderThumb = new BitmapShader(object, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
} else {
bitmapShaderThumb = null;
}
2015-02-01 19:51:02 +01:00
currentThumbLocation = null;
currentKey = null;
2015-05-21 23:27:27 +02:00
currentExt = null;
2015-02-01 19:51:02 +01:00
currentThumbKey = null;
2019-01-23 18:03:33 +01:00
currentKeyQuality = false;
currentImage = null;
2015-02-01 19:51:02 +01:00
currentThumbFilter = null;
currentImageLocation = null;
currentFilter = null;
currentSize = 0;
currentCacheType = 0;
2014-10-21 22:35:16 +02:00
bitmapShader = null;
if (setImageBackup != null) {
setImageBackup.fileLocation = null;
setImageBackup.thumbLocation = null;
setImageBackup.thumb = null;
}
currentAlpha = 1;
2015-02-01 19:51:02 +01:00
if (delegate != null) {
2015-09-24 22:52:02 +02:00
delegate.didSetImage(this, currentThumb != null || staticThumb != null, true);
2015-02-01 19:51:02 +01:00
}
if (parentView != null) {
2015-05-21 23:27:27 +02:00
if (invalidateAll) {
parentView.invalidate();
} else {
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
}
}
2019-01-23 18:03:33 +01:00
if (forceCrossfade && crossfadeWithOldImage && crossfadeImage != null) {
currentAlpha = 0.0f;
lastUpdateAlphaTime = System.currentTimeMillis();
crossfadeWithThumb = currentThumb != null || staticThumb != null;
}
}
public void clearImage() {
2017-12-08 18:35:59 +01:00
for (int a = 0; a < 3; a++) {
recycleBitmap(null, a);
}
ImageLoader.getInstance().cancelLoadingForImageReceiver(this, 0);
}
public void onDetachedFromWindow() {
2019-02-08 03:30:32 +01:00
if (currentImageLocation != null || currentThumbLocation != null || staticThumb != null) {
if (setImageBackup == null) {
setImageBackup = new SetImageBackup();
}
setImageBackup.fileLocation = currentImageLocation;
setImageBackup.filter = currentFilter;
setImageBackup.thumb = staticThumb;
setImageBackup.thumbLocation = currentThumbLocation;
setImageBackup.thumbFilter = currentThumbFilter;
setImageBackup.size = currentSize;
2015-05-21 23:27:27 +02:00
setImageBackup.ext = currentExt;
setImageBackup.cacheType = currentCacheType;
2019-01-23 18:03:33 +01:00
setImageBackup.parentObject = currentParentObject;
}
2018-07-30 04:07:02 +02:00
NotificationCenter.getGlobalInstance().removeObserver(this, NotificationCenter.didReplacedPhotoInMemCache);
clearImage();
}
public boolean onAttachedToWindow() {
2018-07-30 04:07:02 +02:00
NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.didReplacedPhotoInMemCache);
2019-02-08 03:30:32 +01:00
if (setImageBackup != null && (setImageBackup.fileLocation != null || setImageBackup.thumbLocation != null || setImageBackup.thumb != null)) {
setImage(setImageBackup.fileLocation, setImageBackup.filter, setImageBackup.thumb, setImageBackup.thumbLocation, setImageBackup.thumbFilter, setImageBackup.size, setImageBackup.ext, setImageBackup.parentObject, setImageBackup.cacheType);
return true;
}
return false;
}
2019-01-23 18:03:33 +01:00
private void drawDrawable(Canvas canvas, Drawable drawable, int alpha, BitmapShader shader, boolean thumb) {
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2019-01-23 18:03:33 +01:00
int o = thumb ? thumbOrientation : orientation;
2016-04-22 15:49:00 +02:00
Paint paint;
if (shader != null) {
paint = roundPaint;
} else {
paint = bitmapDrawable.getPaint();
}
boolean hasFilter = paint != null && paint.getColorFilter() != null;
2017-12-08 18:35:59 +01:00
if (hasFilter && isPressed == 0) {
2016-04-22 15:49:00 +02:00
if (shader != null) {
roundPaint.setColorFilter(null);
2017-03-31 01:58:05 +02:00
} else if (staticThumb != drawable) {
2016-04-22 15:49:00 +02:00
bitmapDrawable.setColorFilter(null);
}
2017-12-08 18:35:59 +01:00
} else if (!hasFilter && isPressed != 0) {
if (isPressed == 1) {
if (shader != null) {
roundPaint.setColorFilter(selectedColorFilter);
} else {
bitmapDrawable.setColorFilter(selectedColorFilter);
}
2016-04-22 15:49:00 +02:00
} else {
2017-12-08 18:35:59 +01:00
if (shader != null) {
roundPaint.setColorFilter(selectedGroupColorFilter);
} else {
bitmapDrawable.setColorFilter(selectedGroupColorFilter);
}
2016-04-22 15:49:00 +02:00
}
}
if (colorFilter != null) {
2016-04-22 15:49:00 +02:00
if (shader != null) {
roundPaint.setColorFilter(colorFilter);
} else {
bitmapDrawable.setColorFilter(colorFilter);
2015-01-02 23:15:07 +01:00
}
2016-04-22 15:49:00 +02:00
}
int bitmapW;
int bitmapH;
if (bitmapDrawable instanceof AnimatedFileDrawable) {
2019-01-23 18:03:33 +01:00
if (o % 360 == 90 || o % 360 == 270) {
bitmapW = bitmapDrawable.getIntrinsicHeight();
bitmapH = bitmapDrawable.getIntrinsicWidth();
} else {
bitmapW = bitmapDrawable.getIntrinsicWidth();
bitmapH = bitmapDrawable.getIntrinsicHeight();
}
2016-04-22 15:49:00 +02:00
} else {
2019-01-23 18:03:33 +01:00
if (o % 360 == 90 || o % 360 == 270) {
2016-04-22 15:49:00 +02:00
bitmapW = bitmapDrawable.getBitmap().getHeight();
bitmapH = bitmapDrawable.getBitmap().getWidth();
} else {
bitmapW = bitmapDrawable.getBitmap().getWidth();
bitmapH = bitmapDrawable.getBitmap().getHeight();
}
}
float scaleW = bitmapW / (float) imageW;
float scaleH = bitmapH / (float) imageH;
if (shader != null) {
roundPaint.setShader(shader);
float scale = Math.min(scaleW, scaleH);
roundRect.set(imageX, imageY, imageX + imageW, imageY + imageH);
shaderMatrix.reset();
if (Math.abs(scaleW - scaleH) > 0.00001f) {
if (bitmapW / scaleH > imageW) {
drawRegion.set(imageX - ((int) (bitmapW / scaleH) - imageW) / 2, imageY, imageX + ((int) (bitmapW / scaleH) + imageW) / 2, imageY + imageH);
} else {
drawRegion.set(imageX, imageY - ((int) (bitmapH / scaleW) - imageH) / 2, imageX + imageW, imageY + ((int) (bitmapH / scaleW) + imageH) / 2);
}
} else {
drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
}
if (isVisible) {
if (Math.abs(scaleW - scaleH) > 0.00001f) {
int w = (int) Math.floor(imageW * scale);
int h = (int) Math.floor(imageH * scale);
bitmapRect.set((bitmapW - w) / 2, (bitmapH - h) / 2, (bitmapW + w) / 2, (bitmapH + h) / 2);
shaderMatrix.setRectToRect(bitmapRect, roundRect, Matrix.ScaleToFit.START);
} else {
bitmapRect.set(0, 0, bitmapW, bitmapH);
shaderMatrix.setRectToRect(bitmapRect, roundRect, Matrix.ScaleToFit.FILL);
}
shader.setLocalMatrix(shaderMatrix);
roundPaint.setAlpha(alpha);
canvas.drawRoundRect(roundRect, roundRadius, roundRadius, roundPaint);
}
} else {
if (isAspectFit) {
float scale = Math.max(scaleW, scaleH);
canvas.save();
bitmapW /= scale;
bitmapH /= scale;
drawRegion.set(imageX + (imageW - bitmapW) / 2, imageY + (imageH - bitmapH) / 2, imageX + (imageW + bitmapW) / 2, imageY + (imageH + bitmapH) / 2);
bitmapDrawable.setBounds(drawRegion);
2019-02-08 03:30:32 +01:00
if (isVisible) {
try {
bitmapDrawable.setAlpha(alpha);
bitmapDrawable.draw(canvas);
} catch (Exception e) {
if (bitmapDrawable == currentImage && currentKey != null) {
ImageLoader.getInstance().removeImage(currentKey);
currentKey = null;
} else if (bitmapDrawable == currentThumb && currentThumbKey != null) {
ImageLoader.getInstance().removeImage(currentThumbKey);
currentThumbKey = null;
}
setImage(currentImageLocation, currentFilter, currentThumb, currentThumbLocation, currentThumbFilter, currentSize, currentExt, currentParentObject, currentCacheType);
FileLog.e(e);
}
}
canvas.restore();
} else {
if (Math.abs(scaleW - scaleH) > 0.00001f) {
2014-10-21 22:35:16 +02:00
canvas.save();
canvas.clipRect(imageX, imageY, imageX + imageW, imageY + imageH);
2019-01-23 18:03:33 +01:00
if (o % 360 != 0) {
if (centerRotation) {
2019-01-23 18:03:33 +01:00
canvas.rotate(o, imageW / 2, imageH / 2);
} else {
2019-01-23 18:03:33 +01:00
canvas.rotate(o, 0, 0);
2014-10-01 21:55:24 +02:00
}
2014-07-10 02:15:58 +02:00
}
2016-05-25 23:49:47 +02:00
if (bitmapW / scaleH > imageW) {
bitmapW /= scaleH;
drawRegion.set(imageX - (bitmapW - imageW) / 2, imageY, imageX + (bitmapW + imageW) / 2, imageY + imageH);
} else {
2016-05-25 23:49:47 +02:00
bitmapH /= scaleW;
drawRegion.set(imageX, imageY - (bitmapH - imageH) / 2, imageX + imageW, imageY + (bitmapH + imageH) / 2);
}
2017-03-31 01:58:05 +02:00
if (bitmapDrawable instanceof AnimatedFileDrawable) {
((AnimatedFileDrawable) bitmapDrawable).setActualDrawRect(imageX, imageY, imageW, imageH);
}
2019-01-23 18:03:33 +01:00
if (o % 360 == 90 || o % 360 == 270) {
int width = (drawRegion.right - drawRegion.left) / 2;
int height = (drawRegion.bottom - drawRegion.top) / 2;
int centerX = (drawRegion.right + drawRegion.left) / 2;
int centerY = (drawRegion.top + drawRegion.bottom) / 2;
bitmapDrawable.setBounds(centerX - height, centerY - width, centerX + height, centerY + width);
} else {
bitmapDrawable.setBounds(drawRegion);
}
if (isVisible) {
try {
bitmapDrawable.setAlpha(alpha);
bitmapDrawable.draw(canvas);
} catch (Exception e) {
if (bitmapDrawable == currentImage && currentKey != null) {
ImageLoader.getInstance().removeImage(currentKey);
currentKey = null;
} else if (bitmapDrawable == currentThumb && currentThumbKey != null) {
ImageLoader.getInstance().removeImage(currentThumbKey);
currentThumbKey = null;
}
2019-02-08 03:30:32 +01:00
setImage(currentImageLocation, currentFilter, currentThumb, currentThumbLocation, currentThumbFilter, currentSize, currentExt, currentParentObject, currentCacheType);
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
}
canvas.restore();
} else {
canvas.save();
2019-01-23 18:03:33 +01:00
if (o % 360 != 0) {
if (centerRotation) {
2019-01-23 18:03:33 +01:00
canvas.rotate(o, imageW / 2, imageH / 2);
} else {
2019-01-23 18:03:33 +01:00
canvas.rotate(o, 0, 0);
2014-10-21 22:35:16 +02:00
}
}
drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
2017-03-31 01:58:05 +02:00
if (bitmapDrawable instanceof AnimatedFileDrawable) {
((AnimatedFileDrawable) bitmapDrawable).setActualDrawRect(imageX, imageY, imageW, imageH);
}
2019-01-23 18:03:33 +01:00
if (o % 360 == 90 || o % 360 == 270) {
int width = (drawRegion.right - drawRegion.left) / 2;
int height = (drawRegion.bottom - drawRegion.top) / 2;
int centerX = (drawRegion.right + drawRegion.left) / 2;
int centerY = (drawRegion.top + drawRegion.bottom) / 2;
bitmapDrawable.setBounds(centerX - height, centerY - width, centerX + height, centerY + width);
2014-10-21 22:35:16 +02:00
} else {
bitmapDrawable.setBounds(drawRegion);
}
if (isVisible) {
try {
bitmapDrawable.setAlpha(alpha);
bitmapDrawable.draw(canvas);
} catch (Exception e) {
if (bitmapDrawable == currentImage && currentKey != null) {
ImageLoader.getInstance().removeImage(currentKey);
currentKey = null;
} else if (bitmapDrawable == currentThumb && currentThumbKey != null) {
ImageLoader.getInstance().removeImage(currentThumbKey);
currentThumbKey = null;
}
2019-02-08 03:30:32 +01:00
setImage(currentImageLocation, currentFilter, currentThumb, currentThumbLocation, currentThumbFilter, currentSize, currentExt, currentParentObject, currentCacheType);
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
}
canvas.restore();
}
}
}
} else {
drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
drawable.setBounds(drawRegion);
if (isVisible) {
try {
drawable.setAlpha(alpha);
drawable.draw(canvas);
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
}
}
}
private void checkAlphaAnimation(boolean skip) {
if (manualAlphaAnimator) {
return;
}
if (currentAlpha != 1) {
if (!skip) {
long currentTime = System.currentTimeMillis();
2016-04-22 15:49:00 +02:00
long dt = currentTime - lastUpdateAlphaTime;
if (dt > 18) {
dt = 18;
}
currentAlpha += dt / 150.0f;
if (currentAlpha > 1) {
currentAlpha = 1;
2017-12-08 18:35:59 +01:00
if (crossfadeImage != null) {
recycleBitmap(null, 2);
crossfadeShader = null;
}
}
}
lastUpdateAlphaTime = System.currentTimeMillis();
if (parentView != null) {
2015-05-21 23:27:27 +02:00
if (invalidateAll) {
parentView.invalidate();
} else {
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
}
}
}
}
public boolean draw(Canvas canvas) {
try {
Drawable drawable = null;
boolean animationNotReady = currentImage instanceof AnimatedFileDrawable && !((AnimatedFileDrawable) currentImage).hasBitmap();
2016-04-22 15:49:00 +02:00
boolean isThumb = false;
2017-12-08 18:35:59 +01:00
BitmapShader customShader = null;
if (!forcePreview && currentImage != null && !animationNotReady) {
drawable = currentImage;
2019-01-23 18:03:33 +01:00
} else if (crossfadeImage != null && !crossfadingWithThumb) {
2017-12-08 18:35:59 +01:00
drawable = crossfadeImage;
customShader = crossfadeShader;
} else if (staticThumb instanceof BitmapDrawable) {
drawable = staticThumb;
2016-04-22 15:49:00 +02:00
isThumb = true;
} else if (currentThumb != null) {
drawable = currentThumb;
2016-04-22 15:49:00 +02:00
isThumb = true;
}
if (drawable != null) {
if (crossfadeAlpha != 0) {
if (crossfadeWithThumb && animationNotReady) {
2019-01-23 18:03:33 +01:00
drawDrawable(canvas, drawable, (int) (overrideAlpha * 255), bitmapShaderThumb, isThumb);
} else {
if (crossfadeWithThumb && currentAlpha != 1.0f) {
Drawable thumbDrawable = null;
2017-12-08 18:35:59 +01:00
BitmapShader customThumbShader = null;
if (drawable == currentImage) {
2017-12-08 18:35:59 +01:00
if (crossfadeImage != null) {
thumbDrawable = crossfadeImage;
customThumbShader = crossfadeShader;
} else if (staticThumb != null) {
thumbDrawable = staticThumb;
} else if (currentThumb != null) {
thumbDrawable = currentThumb;
}
2019-01-23 18:03:33 +01:00
} else if (drawable == currentThumb || drawable == crossfadeImage) {
if (staticThumb != null) {
thumbDrawable = staticThumb;
}
2019-01-23 18:03:33 +01:00
} else if (drawable == staticThumb) {
if (crossfadeImage != null) {
thumbDrawable = crossfadeImage;
customThumbShader = crossfadeShader;
}
}
if (thumbDrawable != null) {
2019-01-23 18:03:33 +01:00
drawDrawable(canvas, thumbDrawable, (int) (overrideAlpha * 255), customThumbShader != null ? customThumbShader : bitmapShaderThumb, true);
2014-10-01 21:55:24 +02:00
}
}
2019-01-23 18:03:33 +01:00
drawDrawable(canvas, drawable, (int) (overrideAlpha * currentAlpha * 255), customShader != null ? customShader : (isThumb ? bitmapShaderThumb : bitmapShader), isThumb);
}
} else {
2019-01-23 18:03:33 +01:00
drawDrawable(canvas, drawable, (int) (overrideAlpha * 255), customShader != null ? customShader : (isThumb ? bitmapShaderThumb : bitmapShader), isThumb);
}
checkAlphaAnimation(animationNotReady && crossfadeWithThumb);
2014-08-29 23:06:04 +02:00
return true;
2015-02-01 19:51:02 +01:00
} else if (staticThumb != null) {
2019-01-23 18:03:33 +01:00
drawDrawable(canvas, staticThumb, (int) (overrideAlpha * 255), null, true);
checkAlphaAnimation(animationNotReady);
2014-08-29 23:06:04 +02:00
return true;
} else {
checkAlphaAnimation(animationNotReady);
}
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
2014-08-29 23:06:04 +02:00
return false;
}
public void setManualAlphaAnimator(boolean value) {
manualAlphaAnimator = value;
}
2017-07-08 18:32:04 +02:00
public float getCurrentAlpha() {
return currentAlpha;
}
2019-01-23 18:03:33 +01:00
@Keep
public void setCurrentAlpha(float value) {
currentAlpha = value;
}
2019-01-23 18:03:33 +01:00
public Drawable getDrawable() {
if (currentImage != null) {
return currentImage;
} else if (currentThumb != null) {
return currentThumb;
} else if (staticThumb != null) {
return staticThumb;
}
return null;
}
public Bitmap getBitmap() {
if (currentImage instanceof AnimatedFileDrawable) {
return ((AnimatedFileDrawable) currentImage).getAnimatedBitmap();
} else if (staticThumb instanceof AnimatedFileDrawable) {
return ((AnimatedFileDrawable) staticThumb).getAnimatedBitmap();
} else if (currentImage instanceof BitmapDrawable) {
return ((BitmapDrawable) currentImage).getBitmap();
} else if (currentThumb instanceof BitmapDrawable) {
return ((BitmapDrawable) currentThumb).getBitmap();
2015-02-01 19:51:02 +01:00
} else if (staticThumb instanceof BitmapDrawable) {
return ((BitmapDrawable) staticThumb).getBitmap();
}
return null;
}
2018-07-30 04:07:02 +02:00
public BitmapHolder getBitmapSafe() {
Bitmap bitmap = null;
String key = null;
if (currentImage instanceof AnimatedFileDrawable) {
bitmap = ((AnimatedFileDrawable) currentImage).getAnimatedBitmap();
} else if (staticThumb instanceof AnimatedFileDrawable) {
bitmap = ((AnimatedFileDrawable) staticThumb).getAnimatedBitmap();
} else if (currentImage instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) currentImage).getBitmap();
key = currentKey;
} else if (currentThumb instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) currentThumb).getBitmap();
key = currentThumbKey;
} else if (staticThumb instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) staticThumb).getBitmap();
}
if (bitmap != null) {
return new BitmapHolder(bitmap, key);
}
return null;
}
public Bitmap getThumbBitmap() {
if (currentThumb instanceof BitmapDrawable) {
return ((BitmapDrawable) currentThumb).getBitmap();
} else if (staticThumb instanceof BitmapDrawable) {
return ((BitmapDrawable) staticThumb).getBitmap();
}
return null;
}
2018-07-30 04:07:02 +02:00
public BitmapHolder getThumbBitmapSafe() {
Bitmap bitmap = null;
String key = null;
if (currentThumb instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) currentThumb).getBitmap();
key = currentThumbKey;
} else if (staticThumb instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) staticThumb).getBitmap();
}
if (bitmap != null) {
return new BitmapHolder(bitmap, key);
}
return null;
}
public int getBitmapWidth() {
if (currentImage instanceof AnimatedFileDrawable) {
2016-03-06 02:49:31 +01:00
return orientation % 360 == 0 || orientation % 360 == 180 ? currentImage.getIntrinsicWidth() : currentImage.getIntrinsicHeight();
} else if (staticThumb instanceof AnimatedFileDrawable) {
return orientation % 360 == 0 || orientation % 360 == 180 ? staticThumb.getIntrinsicWidth() : staticThumb.getIntrinsicHeight();
}
Bitmap bitmap = getBitmap();
2017-07-08 18:32:04 +02:00
if (bitmap == null) {
if (staticThumb != null) {
return staticThumb.getIntrinsicWidth();
}
return 1;
}
2016-03-06 02:49:31 +01:00
return orientation % 360 == 0 || orientation % 360 == 180 ? bitmap.getWidth() : bitmap.getHeight();
}
public int getBitmapHeight() {
if (currentImage instanceof AnimatedFileDrawable) {
2016-03-06 02:49:31 +01:00
return orientation % 360 == 0 || orientation % 360 == 180 ? currentImage.getIntrinsicHeight() : currentImage.getIntrinsicWidth();
} else if (staticThumb instanceof AnimatedFileDrawable) {
return orientation % 360 == 0 || orientation % 360 == 180 ? staticThumb.getIntrinsicHeight() : staticThumb.getIntrinsicWidth();
}
Bitmap bitmap = getBitmap();
2017-07-08 18:32:04 +02:00
if (bitmap == null) {
if (staticThumb != null) {
return staticThumb.getIntrinsicHeight();
}
return 1;
}
2016-03-06 02:49:31 +01:00
return orientation % 360 == 0 || orientation % 360 == 180 ? bitmap.getHeight() : bitmap.getWidth();
}
public void setVisible(boolean value, boolean invalidate) {
if (isVisible == value) {
return;
}
isVisible = value;
2014-06-12 21:55:13 +02:00
if (invalidate && parentView != null) {
2015-05-21 23:27:27 +02:00
if (invalidateAll) {
parentView.invalidate();
} else {
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
}
}
}
public boolean getVisible() {
return isVisible;
}
2015-01-02 23:15:07 +01:00
public void setAlpha(float value) {
overrideAlpha = value;
}
public void setCrossfadeAlpha(byte value) {
crossfadeAlpha = value;
2015-01-02 23:15:07 +01:00
}
public boolean hasImage() {
2019-02-08 03:30:32 +01:00
return currentImage != null || currentThumb != null || currentKey != null || staticThumb != null;
}
public boolean hasBitmapImage() {
return currentImage != null || currentThumb != null || staticThumb != null;
}
2018-07-30 04:07:02 +02:00
public boolean hasNotThumb() {
return currentImage != null;
}
2019-02-08 03:30:32 +01:00
public boolean hasStaticThumb() {
return staticThumb != null;
}
public void setAspectFit(boolean value) {
isAspectFit = value;
}
2019-02-08 03:30:32 +01:00
public boolean isAspectFit() {
return isAspectFit;
}
public void setParentView(View view) {
parentView = view;
if (currentImage instanceof AnimatedFileDrawable) {
AnimatedFileDrawable fileDrawable = (AnimatedFileDrawable) currentImage;
fileDrawable.setParentView(parentView);
}
}
2017-12-08 18:35:59 +01:00
public void setImageX(int x) {
imageX = x;
}
2017-03-31 01:58:05 +02:00
public void setImageY(int y) {
imageY = y;
}
2017-12-08 18:35:59 +01:00
public void setImageWidth(int width) {
imageW = width;
}
public void setImageCoords(int x, int y, int width, int height) {
imageX = x;
imageY = y;
imageW = width;
imageH = height;
}
2017-07-08 18:32:04 +02:00
public float getCenterX() {
return imageX + imageW / 2.0f;
}
public float getCenterY() {
return imageY + imageH / 2.0f;
}
public int getImageX() {
return imageX;
}
2015-04-09 20:00:14 +02:00
public int getImageX2() {
return imageX + imageW;
}
public int getImageY() {
return imageY;
}
2015-04-09 20:00:14 +02:00
public int getImageY2() {
return imageY + imageH;
}
public int getImageWidth() {
return imageW;
}
public int getImageHeight() {
return imageH;
}
2019-01-23 18:03:33 +01:00
public float getImageAspectRatio() {
return orientation % 180 != 0 ? drawRegion.height() / (float) drawRegion.width() : drawRegion.width() / (float) drawRegion.height();
}
2015-05-21 23:27:27 +02:00
public String getExt() {
return currentExt;
}
public boolean isInsideImage(float x, float y) {
return x >= imageX && x <= imageX + imageW && y >= imageY && y <= imageY + imageH;
}
public Rect getDrawRegion() {
return drawRegion;
}
public String getFilter() {
2015-02-01 19:51:02 +01:00
return currentFilter;
}
public String getThumbFilter() {
return currentThumbFilter;
}
public String getKey() {
2015-02-01 19:51:02 +01:00
return currentKey;
}
public String getThumbKey() {
return currentThumbKey;
}
public int getSize() {
return currentSize;
}
2019-02-08 03:30:32 +01:00
public Object getImageLocation() {
2015-02-01 19:51:02 +01:00
return currentImageLocation;
}
2019-02-08 03:30:32 +01:00
public Object getThumbLocation() {
2015-02-01 19:51:02 +01:00
return currentThumbLocation;
}
public int getCacheType() {
return currentCacheType;
}
public void setForcePreview(boolean value) {
forcePreview = value;
}
2014-10-21 22:35:16 +02:00
public void setForceCrossfade(boolean value) {
forceCrossfade = value;
}
2015-02-01 19:51:02 +01:00
public boolean isForcePreview() {
return forcePreview;
}
2014-10-21 22:35:16 +02:00
public void setRoundRadius(int value) {
roundRadius = value;
}
2014-11-17 03:44:57 +01:00
2018-07-30 04:07:02 +02:00
public void setCurrentAccount(int value) {
currentAccount = value;
}
2014-11-17 03:44:57 +01:00
public int getRoundRadius() {
return roundRadius;
}
2015-02-01 19:51:02 +01:00
2019-01-23 18:03:33 +01:00
public Object getParentObject() {
return currentParentObject;
2015-02-01 19:51:02 +01:00
}
public void setNeedsQualityThumb(boolean value) {
needsQualityThumb = value;
}
2017-12-08 18:35:59 +01:00
public void setCrossfadeWithOldImage(boolean value) {
crossfadeWithOldImage = value;
}
2015-02-01 19:51:02 +01:00
public boolean isNeedsQualityThumb() {
return needsQualityThumb;
}
2019-01-23 18:03:33 +01:00
public boolean isCurrentKeyQuality() {
return currentKeyQuality;
}
2019-02-08 03:30:32 +01:00
public int getCurrentAccount() {
2018-07-30 04:07:02 +02:00
return currentAccount;
}
2015-02-01 19:51:02 +01:00
public void setShouldGenerateQualityThumb(boolean value) {
shouldGenerateQualityThumb = value;
}
public boolean isShouldGenerateQualityThumb() {
return shouldGenerateQualityThumb;
}
public void setAllowStartAnimation(boolean value) {
allowStartAnimation = value;
}
2017-07-08 18:32:04 +02:00
public void setAllowDecodeSingleFrame(boolean value) {
allowDecodeSingleFrame = value;
}
public boolean isAllowStartAnimation() {
return allowStartAnimation;
}
public void startAnimation() {
if (currentImage instanceof AnimatedFileDrawable) {
((AnimatedFileDrawable) currentImage).start();
}
}
public void stopAnimation() {
if (currentImage instanceof AnimatedFileDrawable) {
((AnimatedFileDrawable) currentImage).stop();
}
}
public boolean isAnimationRunning() {
return currentImage instanceof AnimatedFileDrawable && ((AnimatedFileDrawable) currentImage).isRunning();
}
public AnimatedFileDrawable getAnimation() {
return currentImage instanceof AnimatedFileDrawable ? (AnimatedFileDrawable) currentImage : null;
}
2018-07-30 04:07:02 +02:00
protected int getTag(boolean thumb) {
2015-02-01 19:51:02 +01:00
if (thumb) {
return thumbTag;
} else {
return tag;
}
}
2018-07-30 04:07:02 +02:00
protected void setTag(int value, boolean thumb) {
2015-02-01 19:51:02 +01:00
if (thumb) {
thumbTag = value;
} else {
tag = value;
}
}
2017-12-08 18:35:59 +01:00
public void setParam(int value) {
param = value;
}
public int getParam() {
return param;
}
protected boolean setImageBitmapByKey(BitmapDrawable bitmap, String key, boolean thumb, boolean memCache) {
2015-02-01 19:51:02 +01:00
if (bitmap == null || key == null) {
return false;
2015-02-01 19:51:02 +01:00
}
if (!thumb) {
2019-01-23 18:03:33 +01:00
if (!key.equals(currentKey)) {
return false;
}
if (!(bitmap instanceof AnimatedFileDrawable)) {
ImageLoader.getInstance().incrementUseCount(currentKey);
2015-02-01 19:51:02 +01:00
}
currentImage = bitmap;
2019-01-23 18:03:33 +01:00
if (bitmap instanceof ExtendedBitmapDrawable) {
orientation = ((ExtendedBitmapDrawable) bitmap).getOrientation();
}
2016-04-22 15:49:00 +02:00
if (roundRadius != 0 && bitmap instanceof BitmapDrawable) {
if (bitmap instanceof AnimatedFileDrawable) {
((AnimatedFileDrawable) bitmap).setRoundRadius(roundRadius);
} else {
Bitmap object = bitmap.getBitmap();
bitmapShader = new BitmapShader(object, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}
} else {
bitmapShader = null;
2015-02-01 19:51:02 +01:00
}
if (!memCache && !forcePreview || forceCrossfade) {
if (currentThumb == null && staticThumb == null || currentAlpha == 1.0f || forceCrossfade) {
currentAlpha = 0.0f;
lastUpdateAlphaTime = System.currentTimeMillis();
2019-01-23 18:03:33 +01:00
crossfadeWithThumb = crossfadeImage != null || currentThumb != null || staticThumb != null;
}
} else {
currentAlpha = 1.0f;
}
if (bitmap instanceof AnimatedFileDrawable) {
AnimatedFileDrawable fileDrawable = (AnimatedFileDrawable) bitmap;
fileDrawable.setParentView(parentView);
if (allowStartAnimation) {
fileDrawable.start();
2017-07-08 18:32:04 +02:00
} else {
fileDrawable.setAllowDecodeSingleFrame(allowDecodeSingleFrame);
}
}
2015-02-01 19:51:02 +01:00
if (parentView != null) {
2015-05-21 23:27:27 +02:00
if (invalidateAll) {
parentView.invalidate();
} else {
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
}
2015-02-01 19:51:02 +01:00
}
} else if (currentThumb == null && (currentImage == null || (currentImage instanceof AnimatedFileDrawable && !((AnimatedFileDrawable) currentImage).hasBitmap()) || forcePreview)) {
2019-01-23 18:03:33 +01:00
if (!key.equals(currentThumbKey)) {
return false;
2015-02-01 19:51:02 +01:00
}
ImageLoader.getInstance().incrementUseCount(currentThumbKey);
2015-02-01 19:51:02 +01:00
currentThumb = bitmap;
2019-01-23 18:03:33 +01:00
if (bitmap instanceof ExtendedBitmapDrawable) {
thumbOrientation = ((ExtendedBitmapDrawable) bitmap).getOrientation();
}
2017-07-08 18:32:04 +02:00
if (roundRadius != 0 && bitmap instanceof BitmapDrawable) {
2016-04-22 15:49:00 +02:00
if (bitmap instanceof AnimatedFileDrawable) {
((AnimatedFileDrawable) bitmap).setRoundRadius(roundRadius);
} else {
Bitmap object = bitmap.getBitmap();
bitmapShaderThumb = new BitmapShader(object, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}
} else {
bitmapShaderThumb = null;
}
if (!memCache && crossfadeAlpha != 2) {
2019-01-23 18:03:33 +01:00
if (currentParentObject instanceof MessageObject && ((MessageObject) currentParentObject).isRoundVideo() && ((MessageObject) currentParentObject).isSending()) {
2017-07-08 18:32:04 +02:00
currentAlpha = 1.0f;
} else {
currentAlpha = 0.0f;
lastUpdateAlphaTime = System.currentTimeMillis();
crossfadeWithThumb = staticThumb != null && currentKey == null;
}
} else {
currentAlpha = 1.0f;
}
2015-02-01 19:51:02 +01:00
if (!(staticThumb instanceof BitmapDrawable) && parentView != null) {
2015-05-21 23:27:27 +02:00
if (invalidateAll) {
parentView.invalidate();
} else {
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
}
2015-02-01 19:51:02 +01:00
}
}
if (delegate != null) {
delegate.didSetImage(this, currentImage != null || currentThumb != null || staticThumb != null, currentImage == null);
}
return true;
2015-02-01 19:51:02 +01:00
}
2017-12-08 18:35:59 +01:00
private void recycleBitmap(String newKey, int type) {
2015-02-01 19:51:02 +01:00
String key;
Drawable image;
2017-12-08 18:35:59 +01:00
if (type == 2) {
key = crossfadeKey;
image = crossfadeImage;
} else if (type == 1) {
2015-02-01 19:51:02 +01:00
key = currentThumbKey;
image = currentThumb;
} else {
key = currentKey;
image = currentImage;
}
2018-07-30 04:07:02 +02:00
if (key != null && key.startsWith("-")) {
String replacedKey = ImageLoader.getInstance().getReplacedKey(key);
if (replacedKey != null) {
key = replacedKey;
}
}
String replacedKey = ImageLoader.getInstance().getReplacedKey(key);
if (key != null && (newKey == null || !newKey.equals(key)) && image != null) {
if (image instanceof AnimatedFileDrawable) {
AnimatedFileDrawable fileDrawable = (AnimatedFileDrawable) image;
fileDrawable.recycle();
} else if (image instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) image).getBitmap();
boolean canDelete = ImageLoader.getInstance().decrementUseCount(key);
if (!ImageLoader.getInstance().isInCache(key)) {
if (canDelete) {
bitmap.recycle();
}
2015-09-24 22:52:02 +02:00
}
2015-02-01 19:51:02 +01:00
}
}
2017-12-08 18:35:59 +01:00
if (type == 2) {
crossfadeKey = null;
crossfadeImage = null;
} else if (type == 1) {
2015-02-01 19:51:02 +01:00
currentThumb = null;
currentThumbKey = null;
} else {
currentImage = null;
currentKey = null;
}
}
@Override
2018-07-30 04:07:02 +02:00
public void didReceivedNotification(int id, int account, Object... args) {
2019-01-23 18:03:33 +01:00
if (id == NotificationCenter.didReplacedPhotoInMemCache) {
String oldKey = (String) args[0];
if (currentKey != null && currentKey.equals(oldKey)) {
currentKey = (String) args[1];
2019-02-08 03:30:32 +01:00
currentImageLocation = args[2];
}
if (currentThumbKey != null && currentThumbKey.equals(oldKey)) {
currentThumbKey = (String) args[1];
2019-02-08 03:30:32 +01:00
currentThumbLocation = args[2];
}
if (setImageBackup != null) {
if (currentKey != null && currentKey.equals(oldKey)) {
currentKey = (String) args[1];
2019-02-08 03:30:32 +01:00
currentImageLocation = args[2];
}
if (currentThumbKey != null && currentThumbKey.equals(oldKey)) {
currentThumbKey = (String) args[1];
2019-02-08 03:30:32 +01:00
currentThumbLocation = args[2];
2015-02-01 19:51:02 +01:00
}
}
}
}
}