modify code to be simpler
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / ImageViewCustom.java
1 package com.owncloud.android.ui.preview;
2
3 import android.annotation.SuppressLint;
4 import android.content.Context;
5 import android.graphics.Bitmap;
6 import android.graphics.Canvas;
7 import android.os.Build;
8 import android.util.AttributeSet;
9 import android.view.View;
10 import android.widget.ImageView;
11
12 import com.owncloud.android.lib.common.utils.Log_OC;
13
14 public class ImageViewCustom extends ImageView {
15
16 private static final String TAG = ImageViewCustom.class.getSimpleName();
17
18 private static final boolean IS_ICS_OR_HIGHER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
19 private static final boolean IS_VERSION_BUGGY_ON_RECYCLES =
20 Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1 ||
21 Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR2;
22
23 private int mBitmapHeight;
24 private int mBitmapWidth;
25
26
27 public ImageViewCustom(Context context) {
28 super(context);
29 }
30
31 public ImageViewCustom(Context context, AttributeSet attrs) {
32 super(context, attrs);
33 }
34
35 public ImageViewCustom(Context context, AttributeSet attrs, int defStyle) {
36 super(context, attrs, defStyle);
37 }
38
39 @SuppressLint("NewApi")
40 @Override
41 protected void onDraw(Canvas canvas) {
42
43 if(IS_ICS_OR_HIGHER && checkIfMaximumBitmapExceed(canvas) || IS_VERSION_BUGGY_ON_RECYCLES ) {
44 // Software type is set with two targets:
45 // 1. prevent that bitmaps larger than maximum textures allowed are shown as black views in devices
46 // with LAYER_TYPE_HARDWARE enabled by default;
47 // 2. grant that bitmaps are correctly dellocated from memory in versions suffering the bug fixed in
48 // https://android.googlesource.com/platform/frameworks/base/+/034de6b1ec561797a2422314e6ef03e3cd3e08e0;
49 //
50 setLayerType(View.LAYER_TYPE_SOFTWARE, null);
51 }
52
53 super.onDraw(canvas);
54 }
55
56 /**
57 * Checks if current bitmaps exceed the maximum OpenGL texture size limit
58 * @param canvas Canvas where the view will be drawn into.
59 * @return boolean True means that the bitmap is too big for the canvas.
60 */
61 @SuppressLint("NewApi")
62 private boolean checkIfMaximumBitmapExceed(Canvas canvas) {
63 Log_OC.v(TAG, "Canvas maximum: " + canvas.getMaximumBitmapWidth() + " - " + canvas.getMaximumBitmapHeight());
64 if (mBitmapWidth > canvas.getMaximumBitmapWidth()
65 || mBitmapHeight > canvas.getMaximumBitmapHeight()) {
66 return true;
67 }
68
69 return false;
70 }
71
72 @Override
73 /**
74 * Keeps the size of the bitmap cached in member variables for faster access in {@link #onDraw(Canvas)} ,
75 * but without keeping another reference to the {@link Bitmap}
76 */
77 public void setImageBitmap (Bitmap bm) {
78 mBitmapWidth = bm.getWidth();
79 mBitmapHeight = bm.getHeight();
80 super.setImageBitmap(bm);
81 }
82
83 }