a72f50940cf8820a841e8bfe8d5761b969ffa776
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / ThumbnailsCacheManager.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Tobias Kaminsky
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
7 *
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.
11 *
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.
16 *
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/>.
19 *
20 */
21
22 package com.owncloud.android.datamodel;
23
24 import java.io.File;
25 import java.lang.ref.WeakReference;
26
27 import org.apache.commons.httpclient.HttpStatus;
28 import org.apache.commons.httpclient.methods.GetMethod;
29
30 import android.accounts.Account;
31 import android.accounts.AccountManager;
32 import android.content.res.Resources;
33 import android.graphics.Bitmap;
34 import android.graphics.Bitmap.CompressFormat;
35 import android.graphics.BitmapFactory;
36 import android.graphics.drawable.BitmapDrawable;
37 import android.graphics.drawable.Drawable;
38 import android.media.ThumbnailUtils;
39 import android.net.Uri;
40 import android.os.AsyncTask;
41 import android.widget.ImageView;
42
43 import com.owncloud.android.MainApp;
44 import com.owncloud.android.R;
45 import com.owncloud.android.lib.common.OwnCloudAccount;
46 import com.owncloud.android.lib.common.OwnCloudClient;
47 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
48 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
49 import com.owncloud.android.lib.common.utils.Log_OC;
50 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
51 import com.owncloud.android.ui.adapter.DiskLruImageCache;
52 import com.owncloud.android.utils.BitmapUtils;
53 import com.owncloud.android.utils.DisplayUtils;
54
55 /**
56 * Manager for concurrent access to thumbnails cache.
57 */
58 public class ThumbnailsCacheManager {
59
60 private static final String TAG = ThumbnailsCacheManager.class.getSimpleName();
61
62 private static final String CACHE_FOLDER = "thumbnailCache";
63 private static final String MINOR_SERVER_VERSION_FOR_THUMBS = "7.8.0";
64
65 private static final Object mThumbnailsDiskCacheLock = new Object();
66 private static DiskLruImageCache mThumbnailCache = null;
67 private static boolean mThumbnailCacheStarting = true;
68
69 private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
70 private static final CompressFormat mCompressFormat = CompressFormat.JPEG;
71 private static final int mCompressQuality = 70;
72 private static OwnCloudClient mClient = null;
73 private static String mServerVersion = null;
74
75 public static Bitmap mDefaultImg =
76 BitmapFactory.decodeResource(
77 MainApp.getAppContext().getResources(),
78 DisplayUtils.getFileTypeIconId("image/png", "default.png")
79 );
80
81
82 public static class InitDiskCacheTask extends AsyncTask<File, Void, Void> {
83
84 @Override
85 protected Void doInBackground(File... params) {
86 synchronized (mThumbnailsDiskCacheLock) {
87 mThumbnailCacheStarting = true;
88
89 if (mThumbnailCache == null) {
90 try {
91 // Check if media is mounted or storage is built-in, if so,
92 // try and use external cache dir; otherwise use internal cache dir
93 final String cachePath =
94 MainApp.getAppContext().getExternalCacheDir().getPath() +
95 File.separator + CACHE_FOLDER;
96 Log_OC.d(TAG, "create dir: " + cachePath);
97 final File diskCacheDir = new File(cachePath);
98 mThumbnailCache = new DiskLruImageCache(
99 diskCacheDir,
100 DISK_CACHE_SIZE,
101 mCompressFormat,
102 mCompressQuality
103 );
104 } catch (Exception e) {
105 Log_OC.d(TAG, "Thumbnail cache could not be opened ", e);
106 mThumbnailCache = null;
107 }
108 }
109 mThumbnailCacheStarting = false; // Finished initialization
110 mThumbnailsDiskCacheLock.notifyAll(); // Wake any waiting threads
111 }
112 return null;
113 }
114 }
115
116
117 public static void addBitmapToCache(String key, Bitmap bitmap) {
118 synchronized (mThumbnailsDiskCacheLock) {
119 if (mThumbnailCache != null) {
120 mThumbnailCache.put(key, bitmap);
121 }
122 }
123 }
124
125
126 public static Bitmap getBitmapFromDiskCache(String key) {
127 synchronized (mThumbnailsDiskCacheLock) {
128 // Wait while disk cache is started from background thread
129 while (mThumbnailCacheStarting) {
130 try {
131 mThumbnailsDiskCacheLock.wait();
132 } catch (InterruptedException e) {}
133 }
134 if (mThumbnailCache != null) {
135 return (Bitmap) mThumbnailCache.getBitmap(key);
136 }
137 }
138 return null;
139 }
140
141 public static class ThumbnailGenerationTask extends AsyncTask<Object, Void, Bitmap> {
142 private final WeakReference<ImageView> mImageViewReference;
143 private static Account mAccount;
144 private Object mFile;
145 private FileDataStorageManager mStorageManager;
146
147
148 public ThumbnailGenerationTask(ImageView imageView, FileDataStorageManager storageManager,
149 Account account) {
150 // Use a WeakReference to ensure the ImageView can be garbage collected
151 mImageViewReference = new WeakReference<ImageView>(imageView);
152 if (storageManager == null)
153 throw new IllegalArgumentException("storageManager must not be NULL");
154 mStorageManager = storageManager;
155 mAccount = account;
156 }
157
158 public ThumbnailGenerationTask(ImageView imageView) {
159 // Use a WeakReference to ensure the ImageView can be garbage collected
160 mImageViewReference = new WeakReference<ImageView>(imageView);
161 }
162
163 @Override
164 protected Bitmap doInBackground(Object... params) {
165 Bitmap thumbnail = null;
166
167 try {
168 if (mAccount != null) {
169 AccountManager accountMgr = AccountManager.get(MainApp.getAppContext());
170
171 mServerVersion = accountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION);
172 OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount,
173 MainApp.getAppContext());
174 mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
175 getClientFor(ocAccount, MainApp.getAppContext(),
176 MainApp.getUserAgent());
177 }
178
179 mFile = params[0];
180
181 if (mFile instanceof OCFile) {
182 thumbnail = doOCFileInBackground();
183 } else if (mFile instanceof File) {
184 thumbnail = doFileInBackground();
185 } else {
186 // do nothing
187 }
188
189 }catch(Throwable t){
190 // the app should never break due to a problem with thumbnails
191 Log_OC.e(TAG, "Generation of thumbnail for " + mFile + " failed", t);
192 if (t instanceof OutOfMemoryError) {
193 System.gc();
194 }
195 }
196
197 return thumbnail;
198 }
199
200 protected void onPostExecute(Bitmap bitmap){
201 if (isCancelled()) {
202 bitmap = null;
203 }
204
205 if (mImageViewReference != null && bitmap != null) {
206 final ImageView imageView = mImageViewReference.get();
207 final ThumbnailGenerationTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
208 if (this == bitmapWorkerTask && imageView != null) {
209 String tagId = "";
210 if (mFile instanceof OCFile){
211 tagId = String.valueOf(((OCFile)mFile).getFileId());
212 } else if (mFile instanceof File){
213 tagId = String.valueOf(((File)mFile).hashCode());
214 }
215 if (String.valueOf(imageView.getTag()).equals(tagId)) {
216 imageView.setImageBitmap(bitmap);
217 }
218 }
219 }
220 }
221
222 /**
223 * Add thumbnail to cache
224 * @param imageKey: thumb key
225 * @param bitmap: image for extracting thumbnail
226 * @param path: image path
227 * @param px: thumbnail dp
228 * @return Bitmap
229 */
230 private Bitmap addThumbnailToCache(String imageKey, Bitmap bitmap, String path, int px){
231
232 Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
233
234 // Rotate image, obeying exif tag
235 thumbnail = BitmapUtils.rotateImage(thumbnail,path);
236
237 // Add thumbnail to cache
238 addBitmapToCache(imageKey, thumbnail);
239
240 return thumbnail;
241 }
242
243 /**
244 * Converts size of file icon from dp to pixel
245 * @return int
246 */
247 private int getThumbnailDimension(){
248 // Converts dp to pixel
249 Resources r = MainApp.getAppContext().getResources();
250 return (int) Math.round(r.getDimension(R.dimen.file_icon_size_grid));
251 }
252
253 private Bitmap doOCFileInBackground() {
254 Bitmap thumbnail = null;
255 OCFile file = (OCFile)mFile;
256
257 final String imageKey = String.valueOf(file.getRemoteId());
258
259 // Check disk cache in background thread
260 thumbnail = getBitmapFromDiskCache(imageKey);
261
262 // Not found in disk cache
263 if (thumbnail == null || file.needsUpdateThumbnail()) {
264
265 int px = getThumbnailDimension();
266
267 if (file.isDown()) {
268 Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
269 file.getStoragePath(), px, px);
270
271 if (bitmap != null) {
272 thumbnail = addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), px);
273
274 file.setNeedsUpdateThumbnail(false);
275 mStorageManager.saveFile(file);
276 }
277
278 } else {
279 // Download thumbnail from server
280 if (mClient != null && mServerVersion != null) {
281 OwnCloudVersion serverOCVersion = new OwnCloudVersion(mServerVersion);
282 if (serverOCVersion.compareTo(
283 new OwnCloudVersion(MINOR_SERVER_VERSION_FOR_THUMBS)) >= 0) {
284 try {
285 int status = -1;
286
287 String uri = mClient.getBaseUri() + "" +
288 "/index.php/apps/files/api/v1/thumbnail/" +
289 px + "/" + px + Uri.encode(file.getRemotePath(), "/");
290 Log_OC.d("Thumbnail", "URI: " + uri);
291 GetMethod get = new GetMethod(uri);
292 status = mClient.executeMethod(get);
293 if (status == HttpStatus.SC_OK) {
294 byte[] bytes = get.getResponseBody();
295 Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
296 bytes.length);
297 thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
298
299 // Add thumbnail to cache
300 if (thumbnail != null) {
301 addBitmapToCache(imageKey, thumbnail);
302 }
303 }
304 } catch (Exception e) {
305 e.printStackTrace();
306 }
307 } else {
308 Log_OC.d(TAG, "Server too old");
309 }
310 }
311 }
312 }
313
314 return thumbnail;
315
316 }
317
318 private Bitmap doFileInBackground() {
319 Bitmap thumbnail = null;
320 File file = (File)mFile;
321
322 final String imageKey = String.valueOf(file.hashCode());
323
324 // Check disk cache in background thread
325 thumbnail = getBitmapFromDiskCache(imageKey);
326
327 // Not found in disk cache
328 if (thumbnail == null) {
329
330 int px = getThumbnailDimension();
331
332 Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
333 file.getAbsolutePath(), px, px);
334
335 if (bitmap != null) {
336 thumbnail = addThumbnailToCache(imageKey, bitmap, file.getPath(), px);
337 }
338 }
339 return thumbnail;
340 }
341
342 }
343
344 public static boolean cancelPotentialWork(Object file, ImageView imageView) {
345 final ThumbnailGenerationTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
346
347 if (bitmapWorkerTask != null) {
348 final Object bitmapData = bitmapWorkerTask.mFile;
349 // If bitmapData is not yet set or it differs from the new data
350 if (bitmapData == null || bitmapData != file) {
351 // Cancel previous task
352 bitmapWorkerTask.cancel(true);
353 } else {
354 // The same work is already in progress
355 return false;
356 }
357 }
358 // No task associated with the ImageView, or an existing task was cancelled
359 return true;
360 }
361
362 public static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) {
363 if (imageView != null) {
364 final Drawable drawable = imageView.getDrawable();
365 if (drawable instanceof AsyncDrawable) {
366 final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
367 return asyncDrawable.getBitmapWorkerTask();
368 }
369 }
370 return null;
371 }
372
373 public static class AsyncDrawable extends BitmapDrawable {
374 private final WeakReference<ThumbnailGenerationTask> bitmapWorkerTaskReference;
375
376 public AsyncDrawable(
377 Resources res, Bitmap bitmap, ThumbnailGenerationTask bitmapWorkerTask
378 ) {
379
380 super(res, bitmap);
381 bitmapWorkerTaskReference =
382 new WeakReference<ThumbnailGenerationTask>(bitmapWorkerTask);
383 }
384
385 public ThumbnailGenerationTask getBitmapWorkerTask() {
386 return bitmapWorkerTaskReference.get();
387 }
388 }
389 }