X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/cd1a150bf90461a4ace3c80c2352d295fba000c1..288c148b51b951017a3a9b333797a7e8eeede38a:/src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java diff --git a/src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java b/src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java index 584fbc08..7825b959 100644 --- a/src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java +++ b/src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java @@ -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 { @@ -74,7 +76,7 @@ public class ThumbnailsCacheManager { public static Bitmap mDefaultImg = BitmapFactory.decodeResource( MainApp.getAppContext().getResources(), - DisplayUtils.getResourceId("image/png", "default.png") + DisplayUtils.getFileTypeIconId("image/png", "default.png") ); @@ -137,125 +139,15 @@ public class ThumbnailsCacheManager { 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 { + public static class ThumbnailGenerationTask extends AsyncTask { private final WeakReference mImageViewReference; private static Account mAccount; - private AsyncTaskFile mFile; + private Object mFile; 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); if (storageManager == null) @@ -264,13 +156,13 @@ public class ThumbnailsCacheManager { 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); } @Override - protected Bitmap doInBackground(AsyncTaskFile... params) { + protected Bitmap doInBackground(Object... params) { Bitmap thumbnail = null; try { @@ -278,85 +170,20 @@ 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]; - - 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){ @@ -367,7 +194,6 @@ public class ThumbnailsCacheManager { } } - return thumbnail; } @@ -378,9 +204,15 @@ public class ThumbnailsCacheManager { 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 (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); } } @@ -415,15 +247,107 @@ 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(file.getRemoteId()); + + // Check disk cache in background thread + thumbnail = getBitmapFromDiskCache(imageKey); + + // Not found in disk cache + if (thumbnail == null || file.needsUpdateThumbnail()) { + + int px = getThumbnailDimension(); + + if (file.isDown()) { + Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile( + file.getStoragePath(), px, px); + + if (bitmap != null) { + thumbnail = addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), px); + + 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) { + try { + int status = -1; + + 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); + InputStream inputStream = get.getResponseBodyAsStream(); + Bitmap bitmap = BitmapFactory.decodeStream(inputStream); + 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; + File file = (File)mFile; + + final String imageKey = String.valueOf(file.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.getAbsolutePath(), px, px); + + if (bitmap != null) { + thumbnail = addThumbnailToCache(imageKey, bitmap, file.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) { - 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 @@ -437,30 +361,30 @@ public class ThumbnailsCacheManager { return true; } - public static ThumbnailGenerationGlobalTask getBitmapGlobalWorkerTask(ImageView imageView) { + public static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) { 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; } - public static class AsyncGlobalDrawable extends BitmapDrawable { - private final WeakReference bitmapWorkerTaskReference; + public static class AsyncDrawable extends BitmapDrawable { + private final WeakReference bitmapWorkerTaskReference; - public AsyncGlobalDrawable( - Resources res, Bitmap bitmap, ThumbnailGenerationGlobalTask bitmapWorkerTask + public AsyncDrawable( + Resources res, Bitmap bitmap, ThumbnailGenerationTask bitmapWorkerTask ) { super(res, bitmap); bitmapWorkerTaskReference = - new WeakReference(bitmapWorkerTask); + new WeakReference(bitmapWorkerTask); } - public ThumbnailGenerationGlobalTask getBitmapWorkerTask() { + public ThumbnailGenerationTask getBitmapWorkerTask() { return bitmapWorkerTaskReference.get(); } }