2 * ownCloud Android client application
4 * @author Tobias Kaminsky
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.datamodel
;
25 import java
.io
.InputStream
;
26 import java
.lang
.ref
.WeakReference
;
27 import java
.net
.FileNameMap
;
28 import java
.net
.URLConnection
;
30 import org
.apache
.commons
.httpclient
.HttpStatus
;
31 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
33 import android
.accounts
.Account
;
34 import android
.accounts
.AccountManager
;
35 import android
.content
.Context
;
36 import android
.content
.res
.Resources
;
37 import android
.graphics
.Bitmap
;
38 import android
.graphics
.Bitmap
.CompressFormat
;
39 import android
.graphics
.BitmapFactory
;
40 import android
.graphics
.Canvas
;
41 import android
.graphics
.Point
;
42 import android
.graphics
.Canvas
;
43 import android
.graphics
.Paint
;
44 import android
.graphics
.drawable
.BitmapDrawable
;
45 import android
.graphics
.drawable
.ColorDrawable
;
46 import android
.graphics
.drawable
.Drawable
;
47 import android
.media
.ThumbnailUtils
;
48 import android
.net
.Uri
;
49 import android
.os
.AsyncTask
;
50 import android
.view
.Display
;
51 import android
.view
.View
;
52 import android
.view
.WindowManager
;
53 import android
.widget
.ImageView
;
54 import android
.widget
.ProgressBar
;
56 import com
.owncloud
.android
.MainApp
;
57 import com
.owncloud
.android
.R
;
58 import com
.owncloud
.android
.authentication
.AccountUtils
;
59 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
60 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
61 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
62 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
63 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
64 import com
.owncloud
.android
.ui
.adapter
.DiskLruImageCache
;
65 import com
.owncloud
.android
.utils
.BitmapUtils
;
66 import com
.owncloud
.android
.utils
.DisplayUtils
;
67 import com
.owncloud
.android
.utils
.FileStorageUtils
;
70 * Manager for concurrent access to thumbnails cache.
72 public class ThumbnailsCacheManager
{
74 private static final String TAG
= ThumbnailsCacheManager
.class.getSimpleName();
76 private static final String CACHE_FOLDER
= "thumbnailCache";
78 private static final Object mThumbnailsDiskCacheLock
= new Object();
79 private static DiskLruImageCache mThumbnailCache
= null
;
80 private static boolean mThumbnailCacheStarting
= true
;
82 private static final int DISK_CACHE_SIZE
= 1024 * 1024 * 10; // 10MB
83 private static final CompressFormat mCompressFormat
= CompressFormat
.JPEG
;
84 private static final int mCompressQuality
= 70;
85 private static OwnCloudClient mClient
= null
;
87 public static Bitmap mDefaultImg
=
88 BitmapFactory
.decodeResource(
89 MainApp
.getAppContext().getResources(),
94 public static class InitDiskCacheTask
extends AsyncTask
<File
, Void
, Void
> {
97 protected Void
doInBackground(File
... params
) {
98 synchronized (mThumbnailsDiskCacheLock
) {
99 mThumbnailCacheStarting
= true
;
101 if (mThumbnailCache
== null
) {
103 // Check if media is mounted or storage is built-in, if so,
104 // try and use external cache dir; otherwise use internal cache dir
105 final String cachePath
=
106 MainApp
.getAppContext().getExternalCacheDir().getPath() +
107 File
.separator
+ CACHE_FOLDER
;
108 Log_OC
.d(TAG
, "create dir: " + cachePath
);
109 final File diskCacheDir
= new File(cachePath
);
110 mThumbnailCache
= new DiskLruImageCache(
116 } catch (Exception e
) {
117 Log_OC
.d(TAG
, "Thumbnail cache could not be opened ", e
);
118 mThumbnailCache
= null
;
121 mThumbnailCacheStarting
= false
; // Finished initialization
122 mThumbnailsDiskCacheLock
.notifyAll(); // Wake any waiting threads
129 public static void addBitmapToCache(String key
, Bitmap bitmap
) {
130 synchronized (mThumbnailsDiskCacheLock
) {
131 if (mThumbnailCache
!= null
) {
132 mThumbnailCache
.put(key
, bitmap
);
138 public static Bitmap
getBitmapFromDiskCache(String key
) {
139 synchronized (mThumbnailsDiskCacheLock
) {
140 // Wait while disk cache is started from background thread
141 while (mThumbnailCacheStarting
) {
143 mThumbnailsDiskCacheLock
.wait();
144 } catch (InterruptedException e
) {
145 Log_OC
.e(TAG
, "Wait in mThumbnailsDiskCacheLock was interrupted", e
);
148 if (mThumbnailCache
!= null
) {
149 return mThumbnailCache
.getBitmap(key
);
156 * Sets max size of cache
157 * @param maxSize in MB
160 public static boolean setMaxSize(long maxSize
){
161 if (mThumbnailCache
!= null
){
162 mThumbnailCache
.setMaxSize(maxSize
* 1024 * 1024);
169 public static long getMaxSize(){
170 if (mThumbnailCache
!= null
) {
171 return mThumbnailCache
.getMaxSize();
177 public static class ThumbnailGenerationTask
extends AsyncTask
<Object
, Void
, Bitmap
> {
178 private final WeakReference
<ImageView
> mImageViewReference
;
179 private WeakReference
<ProgressBar
> mProgressWheelRef
;
180 private static Account mAccount
;
181 private Object mFile
;
182 private Boolean mIsThumbnail
;
183 private FileDataStorageManager mStorageManager
;
185 public ThumbnailGenerationTask(ImageView imageView
, FileDataStorageManager storageManager
,
187 // Use a WeakReference to ensure the ImageView can be garbage collected
188 mImageViewReference
= new WeakReference
<ImageView
>(imageView
);
189 if (storageManager
== null
)
190 throw new IllegalArgumentException("storageManager must not be NULL");
191 mStorageManager
= storageManager
;
195 public ThumbnailGenerationTask(ImageView imageView
, FileDataStorageManager storageManager
,
196 Account account
, ProgressBar progressWheel
) {
197 this(imageView
, storageManager
, account
);
198 mProgressWheelRef
= new WeakReference
<ProgressBar
>(progressWheel
);
201 public ThumbnailGenerationTask(ImageView imageView
) {
202 // Use a WeakReference to ensure the ImageView can be garbage collected
203 mImageViewReference
= new WeakReference
<ImageView
>(imageView
);
207 protected Bitmap
doInBackground(Object
... params
) {
208 Bitmap thumbnail
= null
;
211 if (mAccount
!= null
) {
212 OwnCloudAccount ocAccount
= new OwnCloudAccount(mAccount
,
213 MainApp
.getAppContext());
214 mClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
215 getClientFor(ocAccount
, MainApp
.getAppContext());
219 mIsThumbnail
= (Boolean
) params
[1];
222 if (mFile
instanceof OCFile
) {
223 thumbnail
= doOCFileInBackground(mIsThumbnail
);
225 if (((OCFile
) mFile
).isVideo()){
226 thumbnail
= addVideoOverlay(thumbnail
);
228 } else if (mFile
instanceof File
) {
229 thumbnail
= doFileInBackground(mIsThumbnail
);
231 String url
= ((File
) mFile
).getAbsolutePath();
232 String mMimeType
= FileStorageUtils
.getMimeTypeFromName(url
);
234 if (mMimeType
!= null
&& mMimeType
.startsWith("video/")){
235 thumbnail
= addVideoOverlay(thumbnail
);
237 //} else { do nothing
241 // the app should never break due to a problem with thumbnails
242 Log_OC
.e(TAG
, "Generation of thumbnail for " + mFile
+ " failed", t
);
243 if (t
instanceof OutOfMemoryError
) {
251 protected void onPostExecute(Bitmap bitmap
){
252 if (bitmap
!= null
) {
253 final ImageView imageView
= mImageViewReference
.get();
254 final ThumbnailGenerationTask bitmapWorkerTask
= getBitmapWorkerTask(imageView
);
255 if (this == bitmapWorkerTask
) {
257 if (mFile
instanceof OCFile
){
258 tagId
= String
.valueOf(((OCFile
)mFile
).getFileId());
259 } else if (mFile
instanceof File
){
260 tagId
= String
.valueOf(mFile
.hashCode());
262 if (String
.valueOf(imageView
.getTag()).equals(tagId
)) {
263 if (mProgressWheelRef
!= null
) {
264 final ProgressBar progressWheel
= mProgressWheelRef
.get();
265 if (progressWheel
!= null
) {
266 progressWheel
.setVisibility(View
.GONE
);
269 imageView
.setImageBitmap(bitmap
);
270 imageView
.setVisibility(View
.VISIBLE
);
277 * Add thumbnail to cache
278 * @param imageKey: thumb key
279 * @param bitmap: image for extracting thumbnail
280 * @param path: image path
281 * @param pxW: thumbnail width
282 * @param pxH: thumbnail height
285 private Bitmap
addThumbnailToCache(String imageKey
, Bitmap bitmap
, String path
, int pxW
, int pxH
){
287 Bitmap thumbnail
= ThumbnailUtils
.extractThumbnail(bitmap
, pxW
, pxH
);
289 // Rotate image, obeying exif tag
290 thumbnail
= BitmapUtils
.rotateImage(thumbnail
,path
);
292 // Add thumbnail to cache
293 addBitmapToCache(imageKey
, thumbnail
);
299 * Converts size of file icon from dp to pixel
302 private int getThumbnailDimension(){
303 // Converts dp to pixel
304 Resources r
= MainApp
.getAppContext().getResources();
305 return Math
.round(r
.getDimension(R
.dimen
.file_icon_size_grid
));
308 private Point
getScreenDimension(){
309 WindowManager wm
= (WindowManager
) MainApp
.getAppContext().getSystemService(Context
.WINDOW_SERVICE
);
310 Display display
= wm
.getDefaultDisplay();
311 Point test
= new Point();
312 display
.getSize(test
);
316 private Bitmap
doOCFileInBackground(Boolean isThumbnail
) {
317 Bitmap thumbnail
= null
;
318 OCFile file
= (OCFile
)mFile
;
320 // distinguish between thumbnail and resized image
321 String temp
= String
.valueOf(file
.getRemoteId());
328 final String imageKey
= temp
;
330 // Check disk cache in background thread
331 thumbnail
= getBitmapFromDiskCache(imageKey
);
333 // Not found in disk cache
334 if (thumbnail
== null
|| file
.needsUpdateThumbnail()) {
338 pxW
= pxH
= getThumbnailDimension();
340 Point p
= getScreenDimension();
346 Bitmap tempBitmap
= BitmapUtils
.decodeSampledBitmapFromFile(
347 file
.getStoragePath(), pxW
, pxH
);
348 Bitmap bitmap
= ThumbnailUtils
.extractThumbnail(tempBitmap
, pxW
, pxH
);
350 if (bitmap
!= null
) {
352 if (file
.getMimetype().equalsIgnoreCase("image/png")) {
353 bitmap
= handlePNG(bitmap
, pxW
);
356 thumbnail
= addThumbnailToCache(imageKey
, bitmap
,
357 file
.getStoragePath(), pxW
, pxH
);
359 file
.setNeedsUpdateThumbnail(false
);
360 mStorageManager
.saveFile(file
);
364 // Download thumbnail from server
365 OwnCloudVersion serverOCVersion
= AccountUtils
.getServerVersion(mAccount
);
366 if (mClient
!= null
&& serverOCVersion
!= null
) {
367 if (serverOCVersion
.supportsRemoteThumbnails()) {
370 String uri
= mClient
.getBaseUri() + "" +
371 "/index.php/apps/files/api/v1/thumbnail/" +
372 pxW
+ "/" + pxH
+ Uri
.encode(file
.getRemotePath(), "/");
373 Log_OC
.d("Thumbnail", "Download URI: " + uri
);
374 GetMethod get
= new GetMethod(uri
);
375 int status
= mClient
.executeMethod(get
);
376 if (status
== HttpStatus
.SC_OK
) {
377 InputStream inputStream
= get
.getResponseBodyAsStream();
378 Bitmap bitmap
= BitmapFactory
.decodeStream(inputStream
);
379 thumbnail
= ThumbnailUtils
.extractThumbnail(bitmap
, pxW
, pxH
);
381 Log_OC
.d(TAG
, "Status: " + status
);
385 if (serverOCVersion
.supportsNativeGallery()){
388 gallery
= "galleryplus";
391 String uri
= mClient
.getBaseUri() +
392 "/index.php/apps/" + gallery
+ "/api/preview/" + Integer
.parseInt(file
.getRemoteId().substring(0,8)) +
393 "/" + pxW
+ "/" + pxH
;
394 Log_OC
.d("Thumbnail", "FileName: " + file
.getFileName() + " Download URI: " + uri
);
395 GetMethod get
= new GetMethod(uri
);
396 int status
= mClient
.executeMethod(get
);
397 if (status
== HttpStatus
.SC_OK
) {
398 InputStream inputStream
= get
.getResponseBodyAsStream();
399 Bitmap bitmap
= BitmapFactory
.decodeStream(inputStream
);
400 // Download via gallery app
406 if (thumbnail
!= null
&& file
.getMimetype().equalsIgnoreCase("image/png")) {
407 thumbnail
= handlePNG(thumbnail
, pxW
);
410 // Add thumbnail to cache
411 if (thumbnail
!= null
) {
412 addBitmapToCache(imageKey
, thumbnail
);
414 } catch (Exception e
) {
418 Log_OC
.d(TAG
, "Server too old");
428 private Bitmap
handlePNG(Bitmap bitmap
, int px
){
429 Bitmap resultBitmap
= Bitmap
.createBitmap(px
,
431 Bitmap
.Config
.ARGB_8888
);
432 Canvas c
= new Canvas(resultBitmap
);
434 c
.drawColor(MainApp
.getAppContext().getResources().
435 getColor(R
.color
.background_color
));
436 c
.drawBitmap(bitmap
, 0, 0, null
);
441 private Bitmap
doFileInBackground(Boolean mIsThumbnail
) {
442 File file
= (File
)mFile
;
444 // distinguish between thumbnail and resized image
445 String temp
= String
.valueOf(file
.hashCode());
452 final String imageKey
= temp
;
454 // Check disk cache in background thread
455 Bitmap thumbnail
= getBitmapFromDiskCache(imageKey
);
457 // Not found in disk cache
458 if (thumbnail
== null
) {
462 pxW
= pxH
= getThumbnailDimension();
464 Point p
= getScreenDimension();
469 Bitmap bitmap
= BitmapUtils
.decodeSampledBitmapFromFile(
470 file
.getAbsolutePath(), pxW
, pxH
);
472 if (bitmap
!= null
) {
473 thumbnail
= addThumbnailToCache(imageKey
, bitmap
, file
.getPath(), pxW
, pxH
);
481 public static boolean cancelPotentialWork(Object file
, ImageView imageView
) {
482 final ThumbnailGenerationTask bitmapWorkerTask
= getBitmapWorkerTask(imageView
);
484 if (bitmapWorkerTask
!= null
) {
485 final Object bitmapData
= bitmapWorkerTask
.mFile
;
486 // If bitmapData is not yet set or it differs from the new data
487 if (bitmapData
== null
|| bitmapData
!= file
) {
488 // Cancel previous task
489 bitmapWorkerTask
.cancel(true
);
490 Log_OC
.v(TAG
, "Cancelled generation of thumbnail for a reused imageView");
492 // The same work is already in progress
496 // No task associated with the ImageView, or an existing task was cancelled
500 public static ThumbnailGenerationTask
getBitmapWorkerTask(ImageView imageView
) {
501 if (imageView
!= null
) {
502 final Drawable drawable
= imageView
.getDrawable();
503 if (drawable
instanceof AsyncDrawable
) {
504 final AsyncDrawable asyncDrawable
= (AsyncDrawable
) drawable
;
505 return asyncDrawable
.getBitmapWorkerTask();
511 public static Bitmap
addVideoOverlay(Bitmap thumbnail
){
512 Bitmap playButton
= BitmapFactory
.decodeResource(MainApp
.getAppContext().getResources(),
513 R
.drawable
.view_play
);
515 Bitmap resizedPlayButton
= Bitmap
.createScaledBitmap(playButton
,
516 (int) (thumbnail
.getWidth() * 0.3),
517 (int) (thumbnail
.getHeight() * 0.3), true
);
519 Bitmap resultBitmap
= Bitmap
.createBitmap(thumbnail
.getWidth(),
520 thumbnail
.getHeight(),
521 Bitmap
.Config
.ARGB_8888
);
523 Canvas c
= new Canvas(resultBitmap
);
525 // compute visual center of play button, according to resized image
526 int x1
= resizedPlayButton
.getWidth();
527 int y1
= resizedPlayButton
.getHeight() / 2;
529 int y2
= resizedPlayButton
.getWidth();
533 double ym
= ( ((Math
.pow(x3
,2) - Math
.pow(x1
,2) + Math
.pow(y3
,2) - Math
.pow(y1
,2)) *
534 (x2
- x1
)) - (Math
.pow(x2
,2) - Math
.pow(x1
,2) + Math
.pow(y2
,2) -
535 Math
.pow(y1
,2)) * (x3
- x1
) ) / (2 * ( ((y3
- y1
) * (x2
- x1
)) -
536 ((y2
- y1
) * (x3
- x1
)) ));
537 double xm
= ( (Math
.pow(x2
,2) - Math
.pow(x1
,2)) + (Math
.pow(y2
,2) - Math
.pow(y1
,2)) -
538 (2*ym
*(y2
- y1
)) ) / (2*(x2
- x1
));
540 // offset to top left
542 double oy
= thumbnail
.getHeight() - ym
;
545 c
.drawBitmap(thumbnail
, 0, 0, null
);
547 Paint p
= new Paint();
550 c
.drawBitmap(resizedPlayButton
, (float) ((thumbnail
.getWidth() / 2) + ox
),
551 (float) ((thumbnail
.getHeight() / 2) - ym
), p
);
556 public static class AsyncDrawable
extends BitmapDrawable
{
557 private final WeakReference
<ThumbnailGenerationTask
> bitmapWorkerTaskReference
;
559 public AsyncDrawable(
560 Resources res
, Bitmap bitmap
, ThumbnailGenerationTask bitmapWorkerTask
564 bitmapWorkerTaskReference
=
565 new WeakReference
<ThumbnailGenerationTask
>(bitmapWorkerTask
);
568 public ThumbnailGenerationTask
getBitmapWorkerTask() {
569 return bitmapWorkerTaskReference
.get();