2014-02-04 19:36:55 +01:00
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*
|
|
|
|
* Copyright Nikolai Kudashov, 2013.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.ui.Views;
|
|
|
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Canvas;
|
2014-06-07 01:35:21 +02:00
|
|
|
import android.graphics.Rect;
|
2014-02-04 19:36:55 +01:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.view.View;
|
|
|
|
|
2014-02-28 23:28:25 +01:00
|
|
|
import org.telegram.messenger.TLRPC;
|
2014-02-04 19:36:55 +01:00
|
|
|
import org.telegram.messenger.FileLoader;
|
|
|
|
import org.telegram.messenger.FileLog;
|
|
|
|
import org.telegram.messenger.Utilities;
|
|
|
|
|
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
|
|
|
|
public class ImageReceiver {
|
|
|
|
private TLRPC.FileLocation last_path = null;
|
|
|
|
private String last_httpUrl = null;
|
|
|
|
private String last_filter = null;
|
|
|
|
private Drawable last_placeholder = null;
|
|
|
|
private int last_size = 0;
|
|
|
|
private String currentPath = null;
|
|
|
|
private boolean isPlaceholder = false;
|
|
|
|
private Drawable currentImage = null;
|
|
|
|
public Integer TAG = null;
|
|
|
|
public WeakReference<View> parentView = null;
|
|
|
|
public int imageX = 0, imageY = 0, imageW = 0, imageH = 0;
|
2014-06-07 01:35:21 +02:00
|
|
|
public Rect drawRegion = new Rect();
|
|
|
|
private boolean isVisible = true;
|
2014-04-04 01:22:09 +02:00
|
|
|
private boolean selfSetting = false;
|
2014-02-04 19:36:55 +01:00
|
|
|
|
|
|
|
public void setImage(TLRPC.FileLocation path, String filter, Drawable placeholder) {
|
|
|
|
setImage(path, null, filter, placeholder, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setImage(TLRPC.FileLocation path, String filter, Drawable placeholder, int size) {
|
|
|
|
setImage(path, null, filter, placeholder, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setImage(String path, String filter, Drawable placeholder) {
|
|
|
|
setImage(null, path, filter, placeholder, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setImage(TLRPC.FileLocation path, String httpUrl, String filter, Drawable placeholder, int size) {
|
|
|
|
if ((path == null && httpUrl == null) || (path != null && !(path instanceof TLRPC.TL_fileLocation) && !(path instanceof TLRPC.TL_fileEncryptedLocation))) {
|
|
|
|
recycleBitmap(null);
|
|
|
|
currentPath = null;
|
|
|
|
isPlaceholder = true;
|
|
|
|
last_path = null;
|
|
|
|
last_httpUrl = null;
|
|
|
|
last_filter = null;
|
|
|
|
last_placeholder = placeholder;
|
|
|
|
last_size = 0;
|
|
|
|
currentImage = null;
|
2014-03-22 23:31:55 +01:00
|
|
|
FileLoader.getInstance().cancelLoadingForImageView(this);
|
2014-02-04 19:36:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
String key;
|
|
|
|
if (path != null) {
|
|
|
|
key = path.volume_id + "_" + path.local_id;
|
|
|
|
} else {
|
|
|
|
key = Utilities.MD5(httpUrl);
|
|
|
|
}
|
|
|
|
if (filter != null) {
|
|
|
|
key += "@" + filter;
|
|
|
|
}
|
|
|
|
Bitmap img;
|
|
|
|
if (currentPath != null) {
|
|
|
|
if (currentPath.equals(key)) {
|
|
|
|
return;
|
|
|
|
} else {
|
2014-03-22 23:31:55 +01:00
|
|
|
img = FileLoader.getInstance().getImageFromMemory(path, httpUrl, this, filter, true);
|
2014-02-04 19:36:55 +01:00
|
|
|
recycleBitmap(img);
|
|
|
|
}
|
|
|
|
} else {
|
2014-03-22 23:31:55 +01:00
|
|
|
img = FileLoader.getInstance().getImageFromMemory(path, httpUrl, this, filter, true);
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
currentPath = key;
|
|
|
|
last_path = path;
|
|
|
|
last_httpUrl = httpUrl;
|
|
|
|
last_filter = filter;
|
|
|
|
last_placeholder = placeholder;
|
|
|
|
last_size = size;
|
|
|
|
if (img == null) {
|
|
|
|
isPlaceholder = true;
|
2014-03-22 23:31:55 +01:00
|
|
|
FileLoader.getInstance().loadImage(path, httpUrl, this, filter, true, size);
|
2014-02-04 19:36:55 +01:00
|
|
|
} else {
|
2014-04-04 01:22:09 +02:00
|
|
|
selfSetting = true;
|
2014-02-04 19:36:55 +01:00
|
|
|
setImageBitmap(img, currentPath);
|
2014-04-04 01:22:09 +02:00
|
|
|
selfSetting = false;
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setImageBitmap(Bitmap bitmap, String imgKey) {
|
|
|
|
if (currentPath == null || !imgKey.equals(currentPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
isPlaceholder = false;
|
2014-03-22 23:31:55 +01:00
|
|
|
FileLoader.getInstance().incrementUseCount(currentPath);
|
2014-02-04 19:36:55 +01:00
|
|
|
currentImage = new BitmapDrawable(null, bitmap);
|
2014-04-04 01:22:09 +02:00
|
|
|
if (!selfSetting && parentView.get() != null) {
|
2014-02-04 19:36:55 +01:00
|
|
|
if (imageW != 0) {
|
|
|
|
parentView.get().invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
} else {
|
|
|
|
parentView.get().invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 16:14:49 +02:00
|
|
|
public void setImageBitmap(Bitmap bitmap) {
|
2014-04-04 01:22:09 +02:00
|
|
|
FileLoader.getInstance().cancelLoadingForImageView(this);
|
|
|
|
recycleBitmap(null);
|
|
|
|
last_placeholder = new BitmapDrawable(null, bitmap);
|
2014-03-31 16:14:49 +02:00
|
|
|
currentPath = null;
|
|
|
|
last_path = null;
|
|
|
|
last_httpUrl = null;
|
|
|
|
last_filter = null;
|
2014-04-04 01:22:09 +02:00
|
|
|
currentImage = null;
|
2014-03-31 16:14:49 +02:00
|
|
|
last_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setImageBitmap(Drawable bitmap) {
|
2014-04-04 01:22:09 +02:00
|
|
|
FileLoader.getInstance().cancelLoadingForImageView(this);
|
|
|
|
recycleBitmap(null);
|
|
|
|
last_placeholder = bitmap;
|
|
|
|
isPlaceholder = true;
|
2014-03-31 16:14:49 +02:00
|
|
|
currentPath = null;
|
2014-04-04 01:22:09 +02:00
|
|
|
currentImage = null;
|
2014-03-31 16:14:49 +02:00
|
|
|
last_path = null;
|
|
|
|
last_httpUrl = null;
|
|
|
|
last_filter = null;
|
|
|
|
last_size = 0;
|
|
|
|
}
|
|
|
|
|
2014-02-04 19:36:55 +01:00
|
|
|
public void clearImage() {
|
|
|
|
recycleBitmap(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void recycleBitmap(Bitmap newBitmap) {
|
|
|
|
if (currentImage == null || isPlaceholder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (currentImage instanceof BitmapDrawable) {
|
|
|
|
Bitmap bitmap = ((BitmapDrawable)currentImage).getBitmap();
|
|
|
|
if (bitmap != null && bitmap != newBitmap) {
|
|
|
|
if (currentPath != null) {
|
2014-03-22 23:31:55 +01:00
|
|
|
boolean canDelete = FileLoader.getInstance().decrementUseCount(currentPath);
|
|
|
|
if (!FileLoader.getInstance().isInCache(currentPath)) {
|
|
|
|
if (FileLoader.getInstance().runtimeHack != null) {
|
|
|
|
FileLoader.getInstance().runtimeHack.trackAlloc(bitmap.getRowBytes() * bitmap.getHeight());
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
if (canDelete) {
|
|
|
|
currentImage = null;
|
|
|
|
if (Build.VERSION.SDK_INT < 11) {
|
|
|
|
bitmap.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
currentImage = null;
|
|
|
|
}
|
2014-03-27 15:25:53 +01:00
|
|
|
currentPath = null;
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void draw(Canvas canvas, int x, int y, int w, int h) {
|
2014-06-07 01:35:21 +02:00
|
|
|
if (!isVisible) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
try {
|
|
|
|
if (currentImage != null) {
|
2014-05-17 01:05:49 +02:00
|
|
|
int bitmapW = currentImage.getIntrinsicWidth();
|
|
|
|
int bitmapH = currentImage.getIntrinsicHeight();
|
|
|
|
float scaleW = bitmapW / (float)w;
|
|
|
|
float scaleH = bitmapH / (float)h;
|
|
|
|
|
2014-06-07 01:35:21 +02:00
|
|
|
if (Math.abs(scaleW - scaleH) > 0.00001f) {
|
2014-05-17 01:05:49 +02:00
|
|
|
canvas.save();
|
|
|
|
canvas.clipRect(x, y, x + w, y + h);
|
|
|
|
|
|
|
|
if (bitmapW / scaleH > w) {
|
|
|
|
bitmapW /= scaleH;
|
2014-06-07 01:35:21 +02:00
|
|
|
drawRegion.set(x - (bitmapW - w) / 2, y, x + (bitmapW + w) / 2, y + h);
|
2014-05-17 01:05:49 +02:00
|
|
|
} else {
|
|
|
|
bitmapH /= scaleW;
|
2014-06-07 01:35:21 +02:00
|
|
|
drawRegion.set(x, y - (bitmapH - h) / 2, x + w, y + (bitmapH + h) / 2);
|
2014-05-17 01:05:49 +02:00
|
|
|
}
|
2014-06-07 01:35:21 +02:00
|
|
|
currentImage.setBounds(drawRegion);
|
2014-05-17 01:05:49 +02:00
|
|
|
currentImage.draw(canvas);
|
|
|
|
|
|
|
|
canvas.restore();
|
|
|
|
} else {
|
2014-06-07 01:35:21 +02:00
|
|
|
drawRegion.set(x, y, x + w, y + h);
|
|
|
|
currentImage.setBounds(drawRegion);
|
2014-05-17 01:05:49 +02:00
|
|
|
currentImage.draw(canvas);
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
} else if (last_placeholder != null) {
|
2014-06-07 01:35:21 +02:00
|
|
|
drawRegion.set(x, y, x + w, y + h);
|
|
|
|
last_placeholder.setBounds(drawRegion);
|
2014-02-04 19:36:55 +01:00
|
|
|
last_placeholder.draw(canvas);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (currentPath != null) {
|
2014-03-22 23:31:55 +01:00
|
|
|
FileLoader.getInstance().removeImage(currentPath);
|
2014-02-04 19:36:55 +01:00
|
|
|
currentPath = null;
|
|
|
|
}
|
|
|
|
setImage(last_path, last_httpUrl, last_filter, last_placeholder, last_size);
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
|
|
|
}
|
2014-06-07 01:35:21 +02:00
|
|
|
|
|
|
|
public Bitmap getBitmap() {
|
|
|
|
if (currentImage != null && currentImage instanceof BitmapDrawable) {
|
|
|
|
return ((BitmapDrawable)currentImage).getBitmap();
|
|
|
|
} else if (isPlaceholder && last_placeholder != null && last_placeholder instanceof BitmapDrawable) {
|
|
|
|
return ((BitmapDrawable)last_placeholder).getBitmap();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setVisible(boolean value) {
|
|
|
|
if (isVisible == value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
isVisible = value;
|
|
|
|
View parent = parentView.get();
|
|
|
|
if (parent != null) {
|
|
|
|
parent.invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getVisible() {
|
|
|
|
return isVisible;
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|