diff --git a/app/src/main/java/org/linphone/chat/ChatMessagesOldAdapter.java b/app/src/main/java/org/linphone/chat/ChatMessagesOldAdapter.java index 6d3c38b4d..a23d69e95 100644 --- a/app/src/main/java/org/linphone/chat/ChatMessagesOldAdapter.java +++ b/app/src/main/java/org/linphone/chat/ChatMessagesOldAdapter.java @@ -61,6 +61,8 @@ import org.linphone.utils.FileUtils; import org.linphone.utils.LinphoneUtils; import org.linphone.utils.SelectableAdapter; import org.linphone.utils.SelectableHelper; +import org.linphone.views.AsyncBitmap; +import org.linphone.views.BitmapWorkerTask; import org.linphone.views.ContactAvatar; import java.io.File; @@ -489,7 +491,7 @@ public class ChatMessagesOldAdapter extends SelectableAdapter { - private final WeakReference imageViewReference; - public String path; - - public BitmapWorkerTask(ImageView imageView) { - path = null; - // Use a WeakReference to ensure the ImageView can be garbage collected - imageViewReference = new WeakReference<>(imageView); - } - - private Bitmap scaleToFitHeight(Bitmap b, int height) { - float factor = height / (float) b.getHeight(); - return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true); - } - - // Decode image in background. - @Override - protected Bitmap doInBackground(String... params) { - path = params[0]; - Bitmap bm = null; - Bitmap thumbnail = null; - if (FileUtils.isExtensionImage(path)) { - if (path.startsWith("content")) { - try { - bm = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), Uri.parse(path)); - } catch (FileNotFoundException e) { - Log.e(e); - } catch (IOException e) { - Log.e(e); - } - } else { - bm = BitmapFactory.decodeFile(path); - } - - ImageView imageView = imageViewReference.get(); - try { - // Rotate the bitmap if possible/needed, using EXIF data - Matrix matrix = new Matrix(); - ExifInterface exif = new ExifInterface(path); - int pictureOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); - if (pictureOrientation == 6 || pictureOrientation == 3 || pictureOrientation == 8) { - if (pictureOrientation == 6) { - matrix.postRotate(90); - } else if (pictureOrientation == 3) { - matrix.postRotate(180); - } else { - matrix.postRotate(270); - } - if (pictureOrientation == 6 || pictureOrientation == 8) { - matrix.postScale(1, imageView.getMeasuredHeight() / (float) bm.getHeight()); - } else { - matrix.postScale(imageView.getMeasuredHeight() / (float) bm.getHeight(), 1); - } - thumbnail = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); - if (thumbnail != bm) { - bm.recycle(); - bm = null; - } - } - } catch (Exception e) { - Log.e(e); - } - - if (thumbnail == null && bm != null) { - thumbnail = scaleToFitHeight(bm, imageView.getMeasuredHeight()); - bm.recycle(); - } - return thumbnail; - } else { - return mDefaultBitmap; - } - } - - // Once complete, see if ImageView is still around and set bitmap. - @Override - protected void onPostExecute(Bitmap bitmap) { - if (isCancelled()) { - bitmap.recycle(); - bitmap = null; - } - if (imageViewReference != null && bitmap != null) { - final ImageView imageView = imageViewReference.get(); - final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); - if (this == bitmapWorkerTask && imageView != null) { - imageView.setImageBitmap(bitmap); - imageView.setTag(path); - imageView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - openFile((String) v.getTag()); - } - }); - } - } - } - } - - class AsyncBitmap extends BitmapDrawable { - private final WeakReference bitmapWorkerTaskReference; - - public AsyncBitmap(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { - super(res, bitmap); - bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask); - } - - public BitmapWorkerTask getBitmapWorkerTask() { - return bitmapWorkerTaskReference.get(); - } - } - private boolean cancelPotentialWork(String path, ImageView imageView) { - final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); + final BitmapWorkerTask bitmapWorkerTask = BitmapWorkerTask.getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final String bitmapData = bitmapWorkerTask.path; @@ -691,15 +579,4 @@ public class ChatMessagesOldAdapter extends SelectableAdapter