check available beta version
[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.graphics.Movie;
8 import android.os.Build;
9 import android.util.AttributeSet;
10 import android.view.View;
11 import android.widget.ImageView;
12
13 import com.owncloud.android.datamodel.OCFile;
14
15 import java.io.FileInputStream;
16 import java.io.InputStream;
17
18 public class ImageViewCustom extends ImageView {
19
20 private static final String TAG = ImageViewCustom.class.getSimpleName();
21
22 private static final boolean IS_ICS_OR_HIGHER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
23 private static final boolean IS_VERSION_BUGGY_ON_RECYCLES =
24 Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1 ||
25 Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR2;
26
27 private int mBitmapHeight;
28 private int mBitmapWidth;
29
30 private Movie mGifMovie;
31 private int mMovieWidth, mMovieHeight;
32 private long mMovieDuration;
33 private long mMovieRunDuration;
34 private long mLastTick;
35
36 public ImageViewCustom(Context context) {
37 super(context);
38 }
39
40 public ImageViewCustom(Context context, AttributeSet attrs) {
41 super(context, attrs);
42 }
43
44 public ImageViewCustom(Context context, AttributeSet attrs, int defStyle) {
45 super(context, attrs, defStyle);
46 }
47
48 @SuppressLint("NewApi")
49 @Override
50 protected void onDraw(Canvas canvas) {
51 if(IS_ICS_OR_HIGHER && checkIfMaximumBitmapExceed(canvas) || IS_VERSION_BUGGY_ON_RECYCLES ) {
52 // Software type is set with two targets:
53 // 1. prevent that bitmaps larger than maximum textures allowed are shown as black views in devices
54 // with LAYER_TYPE_HARDWARE enabled by default;
55 // 2. grant that bitmaps are correctly de-allocated from memory in versions suffering the bug fixed in
56 // https://android.googlesource.com/platform/frameworks/base/+/034de6b1ec561797a2422314e6ef03e3cd3e08e0;
57 //
58 setLayerType(View.LAYER_TYPE_SOFTWARE, null);
59 }
60
61 if(mGifMovie == null){
62 super.onDraw(canvas);
63 } else {
64 long nowTick = android.os.SystemClock.uptimeMillis();
65 if (mLastTick == 0) {
66 mMovieRunDuration = 0;
67 } else {
68 mMovieRunDuration += nowTick - mLastTick;
69 if(mMovieRunDuration > mMovieDuration){
70 mMovieRunDuration = 0;
71 }
72 }
73
74 mGifMovie.setTime((int) mMovieRunDuration);
75
76 float scale;
77 if(mGifMovie.height() > getHeight() || mGifMovie.width() > getWidth()) {
78 scale = (1f / Math.min(canvas.getHeight() / mGifMovie.height(),
79 canvas.getWidth() / mGifMovie.width())) + 0.25f;
80 } else {
81 scale = Math.min(canvas.getHeight() / mGifMovie.height(),
82 canvas.getWidth() / mGifMovie.width());
83 }
84
85 canvas.scale(scale, scale);
86 canvas.translate(((float) getWidth() / scale - (float) mGifMovie.width()) / 2f,
87 ((float) getHeight() / scale - (float) mGifMovie.height()) /2f);
88
89 mGifMovie.draw(canvas, 0, 0);
90
91 mLastTick = nowTick;
92 invalidate();
93 }
94 }
95
96 @Override
97 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
98 if (mGifMovie == null){
99 setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
100 } else {
101 setMeasuredDimension(mMovieWidth, mMovieHeight);
102 }
103 }
104
105 /**
106 * Checks if current bitmaps exceed the maximum OpenGL texture size limit
107 * @param canvas Canvas where the view will be drawn into.
108 * @return boolean True means that the bitmap is too big for the canvas.
109 */
110 @SuppressLint("NewApi")
111 private boolean checkIfMaximumBitmapExceed(Canvas canvas) {
112 return mBitmapWidth > canvas.getMaximumBitmapWidth()
113 || mBitmapHeight > canvas.getMaximumBitmapHeight();
114
115 }
116
117 @Override
118 /**
119 * Keeps the size of the bitmap cached in member variables for faster access in {@link #onDraw(Canvas)} ,
120 * but without keeping another reference to the {@link Bitmap}
121 */
122 public void setImageBitmap(Bitmap bm) {
123 mBitmapWidth = bm.getWidth();
124 mBitmapHeight = bm.getHeight();
125 super.setImageBitmap(bm);
126 }
127
128 public void setGifImage(OCFile file){
129 try {
130 InputStream gifInputStream = new FileInputStream(file.getStoragePath());
131 setLayerType(View.LAYER_TYPE_SOFTWARE, null);
132 setFocusable(true);
133
134 mGifMovie = Movie.decodeStream(gifInputStream);
135 mMovieWidth = mGifMovie.width();
136 mMovieHeight = mGifMovie.height();
137 mMovieDuration = mGifMovie.duration();
138 } catch (Exception e) {
139 e.printStackTrace();
140 }
141 }
142
143 }