Changes after CR
authorjabarros <jabarros@solidgear.es>
Mon, 12 Jan 2015 11:52:40 +0000 (12:52 +0100)
committerjabarros <jabarros@solidgear.es>
Mon, 12 Jan 2015 11:52:40 +0000 (12:52 +0100)
src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java
src/com/owncloud/android/ui/adapter/FileListListAdapter.java
src/com/owncloud/android/ui/adapter/LocalFileListAdapter.java
src/com/owncloud/android/utils/DisplayUtils.java

index 70c9f96..3083d7e 100644 (file)
@@ -137,125 +137,14 @@ public class ThumbnailsCacheManager {
         return null;
     }
 
         return null;
     }
 
-    public interface AsyncTaskFile {
-        public String getId();
-        public String getTagId();
-        public String getPath();
-        public String getRemotePath();
-        public boolean getNeedsUpdateThumbnail();
-        public void setNeedsUpdateThumbnail(boolean needsUpdate);
-        public boolean getIsDown();
-        public Object getFile();
-    }
-
-    public static class AsyncTaskFileLocal implements AsyncTaskFile{
-
-        private File mFile;
-        private boolean mNeedsUpdate;
-
-        public AsyncTaskFileLocal(File file){
-            mFile = file;
-            mNeedsUpdate = false;
-        }
-
-        @Override
-         public String getId() {
-            return String.valueOf(mFile.hashCode());
-        }
-
-        @Override
-        public String getTagId() {
-            return String.valueOf(mFile.hashCode());
-        }
-
-        @Override
-        public String getPath() {
-            return mFile.getAbsolutePath();
-        }
-
-        @Override
-        public String getRemotePath() {
-            return null;
-        }
-
-        @Override
-        public boolean getNeedsUpdateThumbnail() {
-            return mNeedsUpdate;
-        }
-
-        @Override
-        public void setNeedsUpdateThumbnail(boolean needsUpdate) {
-            mNeedsUpdate = needsUpdate;
-        }
-
-        @Override
-        public boolean getIsDown() {
-            return false;
-        }
-
-        @Override
-        public Object getFile() {
-            return mFile;
-        }
-    }
-
-    public static class AsyncTaskOCFile implements AsyncTaskFile{
-
-        private OCFile mFile;
-
-        public AsyncTaskOCFile(OCFile file){
-            mFile = file;
-        }
-
-        @Override
-        public String getId() {
-            return mFile.getRemoteId();
-        }
-
-        @Override
-        public String getTagId() {
-            return String.valueOf(mFile.getFileId());
-        }
-
-        @Override
-        public String getPath() {
-            return mFile.getStoragePath();
-        }
-
-        @Override
-        public String getRemotePath() {
-            return mFile.getRemotePath();
-        }
-
-        @Override
-        public boolean getNeedsUpdateThumbnail() {
-            return mFile.needsUpdateThumbnail();
-        }
-
-        @Override
-        public void setNeedsUpdateThumbnail(boolean needsUpdate) {
-            mFile.setNeedsUpdateThumbnail(needsUpdate);
-        }
-
-        @Override
-        public boolean getIsDown() {
-            return mFile.isDown();
-        }
-
-        @Override
-        public Object getFile() {
-            return mFile;
-        }
-    }
-
-    public static class ThumbnailGenerationGlobalTask extends AsyncTask<AsyncTaskFile, Void, Bitmap> {
+    public static class ThumbnailGenerationTask extends AsyncTask<Object, Void, Bitmap> {
         private final WeakReference<ImageView> mImageViewReference;
         private static Account mAccount;
         private final WeakReference<ImageView> mImageViewReference;
         private static Account mAccount;
-        private AsyncTaskFile mFile;
+        private Object mFile;
         private FileDataStorageManager mStorageManager;
 
 
         private FileDataStorageManager mStorageManager;
 
 
-        public ThumbnailGenerationGlobalTask(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)
             // Use a WeakReference to ensure the ImageView can be garbage collected
             mImageViewReference = new WeakReference<ImageView>(imageView);
             if (storageManager == null)
@@ -264,13 +153,13 @@ public class ThumbnailsCacheManager {
             mAccount = account;
         }
 
             mAccount = account;
         }
 
-        public ThumbnailGenerationGlobalTask(ImageView imageView) {
+        public ThumbnailGenerationTask(ImageView imageView) {
             // Use a WeakReference to ensure the ImageView can be garbage collected
             mImageViewReference = new WeakReference<ImageView>(imageView);
         }
 
         @Override
             // Use a WeakReference to ensure the ImageView can be garbage collected
             mImageViewReference = new WeakReference<ImageView>(imageView);
         }
 
         @Override
-        protected Bitmap doInBackground(AsyncTaskFile... params) {
+        protected Bitmap doInBackground(Object... params) {
             Bitmap thumbnail = null;
 
             try {
             Bitmap thumbnail = null;
 
             try {
@@ -285,78 +174,12 @@ public class ThumbnailsCacheManager {
 
                 mFile = params[0];
 
 
                 mFile = params[0];
 
-                final String imageKey = String.valueOf(mFile.getId());
-
-                // Check disk cache in background thread
-                thumbnail = getBitmapFromDiskCache(imageKey);
-
-                // Check if mFile passed is an OCFile (for OCFile thumbs)
-                if (mFile instanceof AsyncTaskOCFile) {
-
-                    // Not found in disk cache
-                    if (thumbnail == null || mFile.getNeedsUpdateThumbnail()) {
-
-                        int px = getThumbnailDimension();
-
-                        if (mFile.getIsDown()) {
-                            Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
-                                    mFile.getPath(), px, px);
-
-                            if (bitmap != null) {
-                                thumbnail = addThumbnailToCache(imageKey, bitmap, mFile.getPath(), px);
-
-                                mFile.setNeedsUpdateThumbnail(false);
-                                mStorageManager.saveFile((OCFile) mFile.getFile());
-                            }
-
-                        } 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) {
-                                    try {
-                                        int status = -1;
-
-                                        String uri = mClient.getBaseUri() + "/index.php/apps/files/api/v1/thumbnail/" +
-                                                px + "/" + px + Uri.encode(mFile.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);
-                                            thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
-
-                                            // Add thumbnail to cache
-                                            if (thumbnail != null) {
-                                                addBitmapToCache(imageKey, thumbnail);
-                                            }
-                                        }
-                                    } catch (Exception e) {
-                                        e.printStackTrace();
-                                    }
-                                } else {
-                                    Log_OC.d(TAG, "Server too old");
-                                }
-                            }
-                        }
-                    }
-                // Check if mFile passed is a File (for local thumbs)
-                } else if (mFile instanceof AsyncTaskFileLocal) {
-
-                    // Not found in disk cache
-                    if (thumbnail == null) {
-
-                        int px = getThumbnailDimension();
-
-                        Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
-                                mFile.getPath(), px, px);
-
-                        if (bitmap != null) {
-                            thumbnail = addThumbnailToCache(imageKey, bitmap, mFile.getPath(), px);
-                        }
-                    }
-
+                if (mFile instanceof OCFile) {
+                    thumbnail = doOCFileInBackground();
+                }  else if (mFile instanceof File) {
+                    thumbnail = doFileInBackground();
+                } else {
+                    // do nothing
                 }
 
                 }catch(Throwable t){
                 }
 
                 }catch(Throwable t){
@@ -367,7 +190,6 @@ public class ThumbnailsCacheManager {
                     }
                 }
 
                     }
                 }
 
-
             return thumbnail;
         }
 
             return thumbnail;
         }
 
@@ -378,9 +200,15 @@ public class ThumbnailsCacheManager {
 
             if (mImageViewReference != null && bitmap != null) {
                 final ImageView imageView = mImageViewReference.get();
 
             if (mImageViewReference != null && bitmap != null) {
                 final ImageView imageView = mImageViewReference.get();
-                final ThumbnailGenerationGlobalTask bitmapWorkerTask = getBitmapGlobalWorkerTask(imageView);
+                final ThumbnailGenerationTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
                 if (this == bitmapWorkerTask && imageView != null) {
                 if (this == bitmapWorkerTask && imageView != null) {
-                    if (String.valueOf(imageView.getTag()).equals(mFile.getTagId())) {
+                    String tagId = "";
+                    if (mFile instanceof OCFile){
+                        tagId = String.valueOf(((OCFile)mFile).getFileId());
+                    } else if (mFile instanceof File){
+                        tagId = String.valueOf(((File)mFile).hashCode());
+                    }
+                    if (String.valueOf(imageView.getTag()).equals(tagId)) {
                         imageView.setImageBitmap(bitmap);
                     }
                 }
                         imageView.setImageBitmap(bitmap);
                     }
                 }
@@ -417,13 +245,97 @@ public class ThumbnailsCacheManager {
             Resources r = MainApp.getAppContext().getResources();
             return (int) Math.round(r.getDimension(R.dimen.file_icon_size));
         }
             Resources r = MainApp.getAppContext().getResources();
             return (int) Math.round(r.getDimension(R.dimen.file_icon_size));
         }
+
+        private Bitmap doOCFileInBackground() {
+            Bitmap thumbnail = null;
+
+            final String imageKey = String.valueOf(((OCFile) mFile).getRemoteId());
+
+            // Check disk cache in background thread
+            thumbnail = getBitmapFromDiskCache(imageKey);
+
+            // Not found in disk cache
+            if (thumbnail == null || ((OCFile)mFile).needsUpdateThumbnail()) {
+
+                int px = getThumbnailDimension();
+
+                if (((OCFile)mFile).isDown()) {
+                    Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
+                            ((OCFile)mFile).getStoragePath(), px, px);
+
+                    if (bitmap != null) {
+                        thumbnail = addThumbnailToCache(imageKey, bitmap, ((OCFile)mFile).getStoragePath(), px);
+
+                        ((OCFile)mFile).setNeedsUpdateThumbnail(false);
+                        mStorageManager.saveFile(((OCFile)mFile));
+                    }
+
+                } 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) {
+                            try {
+                                int status = -1;
+
+                                String uri = mClient.getBaseUri() + "/index.php/apps/files/api/v1/thumbnail/" +
+                                        px + "/" + px + Uri.encode(((OCFile)mFile).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);
+                                    thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
+
+                                    // Add thumbnail to cache
+                                    if (thumbnail != null) {
+                                        addBitmapToCache(imageKey, thumbnail);
+                                    }
+                                }
+                            } catch (Exception e) {
+                                e.printStackTrace();
+                            }
+                        } else {
+                            Log_OC.d(TAG, "Server too old");
+                        }
+                    }
+                }
+            }
+
+            return thumbnail;
+
+        }
+
+        private Bitmap doFileInBackground() {
+            Bitmap thumbnail = null;
+            final String imageKey = String.valueOf(mFile.hashCode());
+
+            // Check disk cache in background thread
+            thumbnail = getBitmapFromDiskCache(imageKey);
+
+            // Not found in disk cache
+            if (thumbnail == null) {
+
+                int px = getThumbnailDimension();
+
+                Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
+                        ((File)mFile).getAbsolutePath(), px, px);
+
+                if (bitmap != null) {
+                    thumbnail = addThumbnailToCache(imageKey, bitmap, ((File)mFile).getPath(), px);
+                }
+            }
+            return thumbnail;
+        }
+
     }
 
     }
 
-    public static boolean cancelPotentialGlobalWork(AsyncTaskFile file, ImageView imageView) {
-        final ThumbnailGenerationGlobalTask bitmapWorkerTask = getBitmapGlobalWorkerTask(imageView);
+    public static boolean cancelPotentialWork(Object file, ImageView imageView) {
+        final ThumbnailGenerationTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
 
         if (bitmapWorkerTask != null) {
 
         if (bitmapWorkerTask != null) {
-            final AsyncTaskFile bitmapData = bitmapWorkerTask.mFile;
+            final Object bitmapData = bitmapWorkerTask.mFile;
             // If bitmapData is not yet set or it differs from the new data
             if (bitmapData == null || bitmapData != file) {
                 // Cancel previous task
             // If bitmapData is not yet set or it differs from the new data
             if (bitmapData == null || bitmapData != file) {
                 // Cancel previous task
@@ -437,30 +349,30 @@ public class ThumbnailsCacheManager {
         return true;
     }
 
         return true;
     }
 
-    public static ThumbnailGenerationGlobalTask getBitmapGlobalWorkerTask(ImageView imageView) {
+    public static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) {
         if (imageView != null) {
             final Drawable drawable = imageView.getDrawable();
         if (imageView != null) {
             final Drawable drawable = imageView.getDrawable();
-            if (drawable instanceof AsyncGlobalDrawable) {
-                final AsyncGlobalDrawable asyncDrawable = (AsyncGlobalDrawable) drawable;
+            if (drawable instanceof AsyncDrawable) {
+                final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
                 return asyncDrawable.getBitmapWorkerTask();
             }
         }
         return null;
     }
 
                 return asyncDrawable.getBitmapWorkerTask();
             }
         }
         return null;
     }
 
-    public static class AsyncGlobalDrawable extends BitmapDrawable {
-        private final WeakReference<ThumbnailGenerationGlobalTask> bitmapWorkerTaskReference;
+    public static class AsyncDrawable extends BitmapDrawable {
+        private final WeakReference<ThumbnailGenerationTask> bitmapWorkerTaskReference;
 
 
-        public AsyncGlobalDrawable(
-                Resources res, Bitmap bitmap, ThumbnailGenerationGlobalTask bitmapWorkerTask
+        public AsyncDrawable(
+                Resources res, Bitmap bitmap, ThumbnailGenerationTask bitmapWorkerTask
         ) {
 
             super(res, bitmap);
             bitmapWorkerTaskReference =
         ) {
 
             super(res, bitmap);
             bitmapWorkerTaskReference =
-                    new WeakReference<ThumbnailGenerationGlobalTask>(bitmapWorkerTask);
+                    new WeakReference<ThumbnailGenerationTask>(bitmapWorkerTask);
         }
 
         }
 
-        public ThumbnailGenerationGlobalTask getBitmapWorkerTask() {
+        public ThumbnailGenerationTask getBitmapWorkerTask() {
             return bitmapWorkerTaskReference.get();
         }
     }
             return bitmapWorkerTaskReference.get();
         }
     }
index 4da5a98..a5caea2 100644 (file)
@@ -214,23 +214,22 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
                         fileIcon.setImageBitmap(thumbnail);\r
                     } else {\r
 \r
                         fileIcon.setImageBitmap(thumbnail);\r
                     } else {\r
 \r
-                        ThumbnailsCacheManager.AsyncTaskFile asyncTaskFile = new ThumbnailsCacheManager.AsyncTaskOCFile(file);\r
                         // generate new Thumbnail\r
                         // generate new Thumbnail\r
-                        if (ThumbnailsCacheManager.cancelPotentialGlobalWork(asyncTaskFile, fileIcon)) {\r
-                            final ThumbnailsCacheManager.ThumbnailGenerationGlobalTask task =\r
-                                    new ThumbnailsCacheManager.ThumbnailGenerationGlobalTask(\r
+                        if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {\r
+                            final ThumbnailsCacheManager.ThumbnailGenerationTask task =\r
+                                    new ThumbnailsCacheManager.ThumbnailGenerationTask(\r
                                             fileIcon, mStorageManager, mAccount\r
                                     );\r
                             if (thumbnail == null) {\r
                                 thumbnail = ThumbnailsCacheManager.mDefaultImg;\r
                             }\r
                                             fileIcon, mStorageManager, mAccount\r
                                     );\r
                             if (thumbnail == null) {\r
                                 thumbnail = ThumbnailsCacheManager.mDefaultImg;\r
                             }\r
-                            final ThumbnailsCacheManager.AsyncGlobalDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncGlobalDrawable(\r
+                            final ThumbnailsCacheManager.AsyncDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncDrawable(\r
                                     mContext.getResources(), \r
                                     thumbnail, \r
                                     task\r
                             );\r
                             fileIcon.setImageDrawable(asyncDrawable);\r
                                     mContext.getResources(), \r
                                     thumbnail, \r
                                     task\r
                             );\r
                             fileIcon.setImageDrawable(asyncDrawable);\r
-                            task.execute(asyncTaskFile);\r
+                            task.execute(file);\r
                         }\r
                     }\r
                 } else {\r
                         }\r
                     }\r
                 } else {\r
index 9a4095a..996851e 100644 (file)
@@ -139,23 +139,22 @@ public class LocalFileListAdapter extends BaseAdapter implements ListAdapter {
                     if (thumbnail != null){
                         fileIcon.setImageBitmap(thumbnail);
                     } else {
                     if (thumbnail != null){
                         fileIcon.setImageBitmap(thumbnail);
                     } else {
-                        ThumbnailsCacheManager.AsyncTaskFile asyncTaskFile = 
-                                       new ThumbnailsCacheManager.AsyncTaskFileLocal(file);
+
                         // generate new Thumbnail
                         // generate new Thumbnail
-                        if (ThumbnailsCacheManager.cancelPotentialGlobalWork(asyncTaskFile, fileIcon)) {
-                            final ThumbnailsCacheManager.ThumbnailGenerationGlobalTask task =
-                                    new ThumbnailsCacheManager.ThumbnailGenerationGlobalTask(fileIcon);
+                        if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) {
+                            final ThumbnailsCacheManager.ThumbnailGenerationTask task =
+                                    new ThumbnailsCacheManager.ThumbnailGenerationTask(fileIcon);
                             if (thumbnail == null) {
                                 thumbnail = ThumbnailsCacheManager.mDefaultImg;
                             }
                             if (thumbnail == null) {
                                 thumbnail = ThumbnailsCacheManager.mDefaultImg;
                             }
-                            final  ThumbnailsCacheManager.AsyncGlobalDrawable  asyncDrawable = 
-                                       new  ThumbnailsCacheManager.AsyncGlobalDrawable (
+                            final ThumbnailsCacheManager.AsyncDrawable asyncDrawable =
+                                       new ThumbnailsCacheManager.AsyncDrawable(
                                     mContext.getResources(), 
                                     thumbnail, 
                                     task
                                        );
                             fileIcon.setImageDrawable(asyncDrawable);
                                     mContext.getResources(), 
                                     thumbnail, 
                                     task
                                        );
                             fileIcon.setImageDrawable(asyncDrawable);
-                            task.execute(asyncTaskFile);
+                            task.execute(file);
                         }
                     }
                 } else {
                         }
                     }
                 } else {
index f0f5265..32d9017 100644 (file)
@@ -222,7 +222,7 @@ public class DisplayUtils {
 \r
     \r
     private static String getExtension(String filename) {\r
 \r
     \r
     private static String getExtension(String filename) {\r
-        String extension = filename.toLowerCase().substring(filename.lastIndexOf(".") + 1);\r
+        String extension = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();\r
         return extension;\r
     }\r
     \r
         return extension;\r
     }\r
     \r
@@ -234,8 +234,7 @@ public class DisplayUtils {
     public static String unixTimeToHumanReadable(long milliseconds) {\r
         Date date = new Date(milliseconds);\r
         DateFormat df = DateFormat.getDateTimeInstance();\r
     public static String unixTimeToHumanReadable(long milliseconds) {\r
         Date date = new Date(milliseconds);\r
         DateFormat df = DateFormat.getDateTimeInstance();\r
-        //return date.toLocaleString();\r
-        return df.format(date); \r
+        return df.format(date);\r
     }\r
     \r
     \r
     }\r
     \r
     \r