Trigger media scan after file upload
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / ThumbnailsCacheManager.java
index 3083d7e..7825b95 100644 (file)
@@ -1,5 +1,9 @@
-/* ownCloud Android client application
- *   Copyright (C) 2012-2014 ownCloud Inc.
+/**
+ *   ownCloud Android client application
+ *
+ *   @author Tobias Kaminsky
+ *   @author David A. Velasco
+ *   Copyright (C) 2015 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License version 2,
@@ -18,6 +22,7 @@
 package com.owncloud.android.datamodel;
 
 import java.io.File;
+import java.io.InputStream;
 import java.lang.ref.WeakReference;
 
 import org.apache.commons.httpclient.HttpStatus;
@@ -49,10 +54,7 @@ import com.owncloud.android.utils.BitmapUtils;
 import com.owncloud.android.utils.DisplayUtils;
 
 /**
- * Manager for concurrent access to thumbnails cache. 
- *  
- * @author Tobias Kaminsky
- * @author David A. Velasco
+ * Manager for concurrent access to thumbnails cache.
  */
 public class ThumbnailsCacheManager {
     
@@ -144,7 +146,8 @@ public class ThumbnailsCacheManager {
         private FileDataStorageManager mStorageManager;
 
 
-        public ThumbnailGenerationTask(ImageView imageView, FileDataStorageManager storageManager, Account account) {
+        public ThumbnailGenerationTask(ImageView imageView, FileDataStorageManager storageManager,
+                                       Account account) {
             // Use a WeakReference to ensure the ImageView can be garbage collected
             mImageViewReference = new WeakReference<ImageView>(imageView);
             if (storageManager == null)
@@ -167,13 +170,14 @@ public class ThumbnailsCacheManager {
                     AccountManager accountMgr = AccountManager.get(MainApp.getAppContext());
 
                     mServerVersion = accountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION);
-                    OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, MainApp.getAppContext());
+                    OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount,
+                            MainApp.getAppContext());
                     mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
                             getClientFor(ocAccount, MainApp.getAppContext());
                 }
 
                 mFile = params[0];
-
+                
                 if (mFile instanceof OCFile) {
                     thumbnail = doOCFileInBackground();
                 }  else if (mFile instanceof File) {
@@ -243,49 +247,55 @@ public class ThumbnailsCacheManager {
         private int getThumbnailDimension(){
             // Converts dp to pixel
             Resources r = MainApp.getAppContext().getResources();
-            return (int) Math.round(r.getDimension(R.dimen.file_icon_size));
+            return (int) Math.round(r.getDimension(R.dimen.file_icon_size_grid));
         }
 
         private Bitmap doOCFileInBackground() {
             Bitmap thumbnail = null;
+            OCFile file = (OCFile)mFile;
 
-            final String imageKey = String.valueOf(((OCFile) mFile).getRemoteId());
+            final String imageKey = String.valueOf(file.getRemoteId());
 
             // Check disk cache in background thread
             thumbnail = getBitmapFromDiskCache(imageKey);
 
             // Not found in disk cache
-            if (thumbnail == null || ((OCFile)mFile).needsUpdateThumbnail()) {
+            if (thumbnail == null || file.needsUpdateThumbnail()) {
 
                 int px = getThumbnailDimension();
 
-                if (((OCFile)mFile).isDown()) {
+                if (file.isDown()) {
                     Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
-                            ((OCFile)mFile).getStoragePath(), px, px);
+                            file.getStoragePath(), px, px);
 
                     if (bitmap != null) {
-                        thumbnail = addThumbnailToCache(imageKey, bitmap, ((OCFile)mFile).getStoragePath(), px);
+                        thumbnail = addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), px);
 
-                        ((OCFile)mFile).setNeedsUpdateThumbnail(false);
-                        mStorageManager.saveFile(((OCFile)mFile));
+                        file.setNeedsUpdateThumbnail(false);
+                        mStorageManager.saveFile(file);
                     }
 
                 } else {
                     // Download thumbnail from server
                     if (mClient != null && mServerVersion != null) {
                         OwnCloudVersion serverOCVersion = new OwnCloudVersion(mServerVersion);
-                        if (serverOCVersion.compareTo(new OwnCloudVersion(MINOR_SERVER_VERSION_FOR_THUMBS)) >= 0) {
+                        if (serverOCVersion.compareTo(
+                                new OwnCloudVersion(MINOR_SERVER_VERSION_FOR_THUMBS)) >= 0) {
                             try {
                                 int status = -1;
 
-                                String uri = mClient.getBaseUri() + "/index.php/apps/files/api/v1/thumbnail/" +
-                                        px + "/" + px + Uri.encode(((OCFile)mFile).getRemotePath(), "/");
+                                String uri = mClient.getBaseUri() + "" +
+                                        "/index.php/apps/files/api/v1/thumbnail/" +
+                                        px + "/" + px + Uri.encode(file.getRemotePath(), "/");
                                 Log_OC.d("Thumbnail", "URI: " + uri);
                                 GetMethod get = new GetMethod(uri);
                                 status = mClient.executeMethod(get);
                                 if (status == HttpStatus.SC_OK) {
-                                    byte[] bytes = get.getResponseBody();
-                                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
+//                                    byte[] bytes = get.getResponseBody();
+//                                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
+//                                            bytes.length);
+                                    InputStream inputStream = get.getResponseBodyAsStream();
+                                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                                     thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
 
                                     // Add thumbnail to cache
@@ -309,7 +319,9 @@ public class ThumbnailsCacheManager {
 
         private Bitmap doFileInBackground() {
             Bitmap thumbnail = null;
-            final String imageKey = String.valueOf(mFile.hashCode());
+            File file = (File)mFile;
+
+            final String imageKey = String.valueOf(file.hashCode());
 
             // Check disk cache in background thread
             thumbnail = getBitmapFromDiskCache(imageKey);
@@ -320,10 +332,10 @@ public class ThumbnailsCacheManager {
                 int px = getThumbnailDimension();
 
                 Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
-                        ((File)mFile).getAbsolutePath(), px, px);
+                        file.getAbsolutePath(), px, px);
 
                 if (bitmap != null) {
-                    thumbnail = addThumbnailToCache(imageKey, bitmap, ((File)mFile).getPath(), px);
+                    thumbnail = addThumbnailToCache(imageKey, bitmap, file.getPath(), px);
                 }
             }
             return thumbnail;