1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.datamodel
;
21 import java
.lang
.ref
.WeakReference
;
23 import org
.apache
.commons
.httpclient
.HttpStatus
;
24 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
26 import android
.accounts
.Account
;
27 import android
.accounts
.AccountManager
;
28 import android
.content
.res
.Resources
;
29 import android
.graphics
.Bitmap
;
30 import android
.graphics
.Bitmap
.CompressFormat
;
31 import android
.graphics
.BitmapFactory
;
32 import android
.graphics
.Matrix
;
33 import android
.graphics
.drawable
.BitmapDrawable
;
34 import android
.graphics
.drawable
.Drawable
;
35 import android
.media
.ExifInterface
;
36 import android
.media
.ThumbnailUtils
;
37 import android
.net
.Uri
;
38 import android
.os
.AsyncTask
;
39 import android
.widget
.ImageView
;
41 import com
.owncloud
.android
.MainApp
;
42 import com
.owncloud
.android
.R
;
43 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
44 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
45 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
46 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.Constants
;
47 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
48 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
49 import com
.owncloud
.android
.ui
.adapter
.DiskLruImageCache
;
50 import com
.owncloud
.android
.utils
.BitmapUtils
;
51 import com
.owncloud
.android
.utils
.DisplayUtils
;
54 * Manager for concurrent access to thumbnails cache.
56 * @author Tobias Kaminsky
57 * @author David A. Velasco
59 public class ThumbnailsCacheManager
{
61 private static final String TAG
= ThumbnailsCacheManager
.class.getSimpleName();
63 private static final String CACHE_FOLDER
= "thumbnailCache";
64 private static final String MINOR_SERVER_VERSION_FOR_THUMBS
= "7.8.0";
66 private static final Object mThumbnailsDiskCacheLock
= new Object();
67 private static DiskLruImageCache mThumbnailCache
= null
;
68 private static boolean mThumbnailCacheStarting
= true
;
70 private static final int DISK_CACHE_SIZE
= 1024 * 1024 * 10; // 10MB
71 private static final CompressFormat mCompressFormat
= CompressFormat
.JPEG
;
72 private static final int mCompressQuality
= 70;
73 private static OwnCloudClient mClient
= null
;
74 private static String mServerVersion
= null
;
76 public static Bitmap mDefaultImg
=
77 BitmapFactory
.decodeResource(
78 MainApp
.getAppContext().getResources(),
79 DisplayUtils
.getResourceId("image/png", "default.png")
83 public static class InitDiskCacheTask
extends AsyncTask
<File
, Void
, Void
> {
86 protected Void
doInBackground(File
... params
) {
87 synchronized (mThumbnailsDiskCacheLock
) {
88 mThumbnailCacheStarting
= true
;
90 if (mThumbnailCache
== null
) {
92 // Check if media is mounted or storage is built-in, if so,
93 // try and use external cache dir; otherwise use internal cache dir
94 final String cachePath
=
95 MainApp
.getAppContext().getExternalCacheDir().getPath() +
96 File
.separator
+ CACHE_FOLDER
;
97 Log_OC
.d(TAG
, "create dir: " + cachePath
);
98 final File diskCacheDir
= new File(cachePath
);
99 mThumbnailCache
= new DiskLruImageCache(
105 } catch (Exception e
) {
106 Log_OC
.d(TAG
, "Thumbnail cache could not be opened ", e
);
107 mThumbnailCache
= null
;
110 mThumbnailCacheStarting
= false
; // Finished initialization
111 mThumbnailsDiskCacheLock
.notifyAll(); // Wake any waiting threads
118 public static void addBitmapToCache(String key
, Bitmap bitmap
) {
119 synchronized (mThumbnailsDiskCacheLock
) {
120 if (mThumbnailCache
!= null
) {
121 mThumbnailCache
.put(key
, bitmap
);
127 public static Bitmap
getBitmapFromDiskCache(String key
) {
128 synchronized (mThumbnailsDiskCacheLock
) {
129 // Wait while disk cache is started from background thread
130 while (mThumbnailCacheStarting
) {
132 mThumbnailsDiskCacheLock
.wait();
133 } catch (InterruptedException e
) {}
135 if (mThumbnailCache
!= null
) {
136 return (Bitmap
) mThumbnailCache
.getBitmap(key
);
143 public static boolean cancelPotentialWork(OCFile file
, ImageView imageView
) {
144 final ThumbnailGenerationTask bitmapWorkerTask
= getBitmapWorkerTask(imageView
);
146 if (bitmapWorkerTask
!= null
) {
147 final OCFile bitmapData
= bitmapWorkerTask
.mFile
;
148 // If bitmapData is not yet set or it differs from the new data
149 if (bitmapData
== null
|| bitmapData
!= file
) {
150 // Cancel previous task
151 bitmapWorkerTask
.cancel(true
);
153 // The same work is already in progress
157 // No task associated with the ImageView, or an existing task was cancelled
161 public static boolean cancelPotentialWork(File file
, ImageView imageView
) {
162 final ThumbnailLocalGenerationTask bitmapWorkerTask
= getBitmapLocalWorkerTask(imageView
);
164 if (bitmapWorkerTask
!= null
) {
165 final File bitmapData
= bitmapWorkerTask
.mFile
;
166 // If bitmapData is not yet set or it differs from the new data
167 if (bitmapData
== null
|| bitmapData
!= file
) {
168 // Cancel previous task
169 bitmapWorkerTask
.cancel(true
);
171 // The same work is already in progress
175 // No task associated with the ImageView, or an existing task was cancelled
179 public static ThumbnailGenerationTask
getBitmapWorkerTask(ImageView imageView
) {
180 if (imageView
!= null
) {
181 final Drawable drawable
= imageView
.getDrawable();
182 if (drawable
instanceof AsyncDrawable
) {
183 final AsyncDrawable asyncDrawable
= (AsyncDrawable
) drawable
;
184 return asyncDrawable
.getBitmapWorkerTask();
190 public static ThumbnailLocalGenerationTask
getBitmapLocalWorkerTask(ImageView imageView
) {
191 if (imageView
!= null
) {
192 final Drawable drawable
= imageView
.getDrawable();
193 if (drawable
instanceof AsyncLocalDrawable
) {
194 final AsyncLocalDrawable asyncDrawable
= (AsyncLocalDrawable
) drawable
;
195 return asyncDrawable
.getBitmapWorkerTask();
201 public static class ThumbnailLocalGenerationTask
extends AsyncTask
<File
, Void
, Bitmap
> {
202 private final WeakReference
<ImageView
> mImageViewLocalReference
;
205 public ThumbnailLocalGenerationTask(ImageView imageView
) {
206 // Use a WeakReference to ensure the ImageView can be garbage collected
207 mImageViewLocalReference
= new WeakReference
<ImageView
>(imageView
);
210 // Decode image in background.
212 protected Bitmap
doInBackground(File
... params
) {
213 Bitmap thumbnail
= null
;
217 final String imageKey
= String
.valueOf(mFile
.hashCode());
219 // Check disk cache in background thread
220 thumbnail
= getBitmapFromDiskCache(imageKey
);
222 // Not found in disk cache
223 if (thumbnail
== null
) {
224 // Converts dp to pixel
225 Resources r
= MainApp
.getAppContext().getResources();
227 int px
= (int) Math
.round(r
.getDimension(R
.dimen
.file_icon_size
));
229 Bitmap bitmap
= BitmapUtils
.decodeSampledBitmapFromFile(
230 mFile
.getAbsolutePath(), px
, px
);
232 if (bitmap
!= null
) {
233 thumbnail
= ThumbnailUtils
.extractThumbnail(bitmap
, px
, px
);
235 // Add thumbnail to cache
236 addBitmapToCache(imageKey
, thumbnail
);
240 } catch (Throwable t
) {
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
){
256 if (mImageViewLocalReference
!= null
&& bitmap
!= null
) {
257 final ImageView imageView
= mImageViewLocalReference
.get();
258 final ThumbnailLocalGenerationTask bitmapWorkerTask
= getBitmapLocalWorkerTask(imageView
);
259 if (this == bitmapWorkerTask
&& imageView
!= null
) {
260 if (imageView
.getTag().equals(mFile
.hashCode())) {
261 imageView
.setImageBitmap(bitmap
);
268 public static class ThumbnailGenerationTask
extends AsyncTask
<OCFile
, Void
, Bitmap
> {
269 private final WeakReference
<ImageView
> mImageViewReference
;
270 private static Account mAccount
;
271 private OCFile mFile
;
272 private FileDataStorageManager mStorageManager
;
274 public ThumbnailGenerationTask(ImageView imageView
, FileDataStorageManager storageManager
, Account account
) {
275 // Use a WeakReference to ensure the ImageView can be garbage collected
276 mImageViewReference
= new WeakReference
<ImageView
>(imageView
);
277 if (storageManager
== null
)
278 throw new IllegalArgumentException("storageManager must not be NULL");
279 mStorageManager
= storageManager
;
283 // Decode image in background.
285 protected Bitmap
doInBackground(OCFile
... params
) {
286 Bitmap thumbnail
= null
;
289 if (mAccount
!= null
) {
290 AccountManager accountMgr
= AccountManager
.get(MainApp
.getAppContext());
292 mServerVersion
= accountMgr
.getUserData(mAccount
, Constants
.KEY_OC_VERSION
);
293 OwnCloudAccount ocAccount
= new OwnCloudAccount(mAccount
, MainApp
.getAppContext());
294 mClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
295 getClientFor(ocAccount
, MainApp
.getAppContext());
299 final String imageKey
= String
.valueOf(mFile
.getRemoteId());
301 // Check disk cache in background thread
302 thumbnail
= getBitmapFromDiskCache(imageKey
);
304 // Not found in disk cache
305 if (thumbnail
== null
|| mFile
.needsUpdateThumbnail()) {
306 // Converts dp to pixel
307 Resources r
= MainApp
.getAppContext().getResources();
309 int px
= (int) Math
.round(r
.getDimension(R
.dimen
.file_icon_size
));
312 Bitmap bitmap
= BitmapUtils
.decodeSampledBitmapFromFile(
313 mFile
.getStoragePath(), px
, px
);
315 if (bitmap
!= null
) {
316 thumbnail
= ThumbnailUtils
.extractThumbnail(bitmap
, px
, px
);
318 // Rotate image, obeying exif tag
319 thumbnail
= BitmapUtils
.rotateImage(thumbnail
, mFile
.getStoragePath());
321 // Add thumbnail to cache
322 addBitmapToCache(imageKey
, thumbnail
);
324 mFile
.setNeedsUpdateThumbnail(false
);
325 mStorageManager
.saveFile(mFile
);
329 // Download thumbnail from server
330 if (mClient
!= null
&& mServerVersion
!= null
) {
331 OwnCloudVersion serverOCVersion
= new OwnCloudVersion(mServerVersion
);
332 if (serverOCVersion
.compareTo(new OwnCloudVersion(MINOR_SERVER_VERSION_FOR_THUMBS
)) >= 0) {
336 String uri
= mClient
.getBaseUri() + "/index.php/apps/files/api/v1/thumbnail/" +
337 px
+ "/" + px
+ Uri
.encode(mFile
.getRemotePath(), "/");
338 Log_OC
.d("Thumbnail", "URI: " + uri
);
339 GetMethod get
= new GetMethod(uri
);
340 status
= mClient
.executeMethod(get
);
341 if (status
== HttpStatus
.SC_OK
) {
342 byte[] bytes
= get
.getResponseBody();
343 Bitmap bitmap
= BitmapFactory
.decodeByteArray(bytes
, 0, bytes
.length
);
344 thumbnail
= ThumbnailUtils
.extractThumbnail(bitmap
, px
, px
);
346 // Add thumbnail to cache
347 if (thumbnail
!= null
) {
348 addBitmapToCache(imageKey
, thumbnail
);
351 } catch (Exception e
) {
355 Log_OC
.d(TAG
, "Server too old");
361 } catch (Throwable t
) {
362 // the app should never break due to a problem with thumbnails
363 Log_OC
.e(TAG
, "Generation of thumbnail for " + mFile
+ " failed", t
);
364 if (t
instanceof OutOfMemoryError
) {
372 protected void onPostExecute(Bitmap bitmap
){
377 if (mImageViewReference
!= null
&& bitmap
!= null
) {
378 final ImageView imageView
= mImageViewReference
.get();
379 final ThumbnailGenerationTask bitmapWorkerTask
=
380 getBitmapWorkerTask(imageView
);
381 if (this == bitmapWorkerTask
&& imageView
!= null
) {
382 if (imageView
.getTag().equals(mFile
.getFileId())) {
383 imageView
.setImageBitmap(bitmap
);
391 public static class AsyncDrawable
extends BitmapDrawable
{
392 private final WeakReference
<ThumbnailGenerationTask
> bitmapWorkerTaskReference
;
394 public AsyncDrawable(
395 Resources res
, Bitmap bitmap
, ThumbnailGenerationTask bitmapWorkerTask
399 bitmapWorkerTaskReference
=
400 new WeakReference
<ThumbnailGenerationTask
>(bitmapWorkerTask
);
403 public ThumbnailGenerationTask
getBitmapWorkerTask() {
404 return bitmapWorkerTaskReference
.get();
408 public static class AsyncLocalDrawable
extends BitmapDrawable
{
409 private final WeakReference
<ThumbnailLocalGenerationTask
> bitmapWorkerLocalTaskReference
;
411 public AsyncLocalDrawable(Resources res
, Bitmap bitmap
, ThumbnailLocalGenerationTask bitmapWorkerTask
) {
413 bitmapWorkerLocalTaskReference
=
414 new WeakReference
<ThumbnailLocalGenerationTask
>(bitmapWorkerTask
);
417 public ThumbnailLocalGenerationTask
getBitmapWorkerTask() {
418 return bitmapWorkerLocalTaskReference
.get();
424 * Remove from cache the remoteId passed
425 * @param fileRemoteId: remote id of mFile passed
427 public static void removeFileFromCache(String fileRemoteId
){
428 synchronized (mThumbnailsDiskCacheLock
) {
429 if (mThumbnailCache
!= null
) {
430 mThumbnailCache
.removeKey(fileRemoteId
);
432 mThumbnailsDiskCacheLock
.notifyAll(); // Wake any waiting threads