From: jabarros Date: Fri, 19 Dec 2014 09:58:08 +0000 (+0100) Subject: Merge branch 'develop' into thumbnailForUpload X-Git-Tag: oc-android-1.7.0_signed~42^2~13 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/6e3d3cd016c235462d9f623622dfaaefb77fec95?ds=inline;hp=-c Merge branch 'develop' into thumbnailForUpload --- 6e3d3cd016c235462d9f623622dfaaefb77fec95 diff --combined src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java index e8b0cb34,ce53c444..bf62a23c --- a/src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java +++ b/src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java @@@ -29,8 -29,10 +29,10 @@@ import android.content.res.Resources import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; + import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; + import android.media.ExifInterface; import android.media.ThumbnailUtils; import android.net.Uri; import android.os.AsyncTask; @@@ -156,24 -158,6 +158,24 @@@ public class ThumbnailsCacheManager return true; } + public static boolean cancelPotentialWork(File file, ImageView imageView) { + final ThumbnailLocalGenerationTask bitmapWorkerTask = getBitmapLocalWorkerTask(imageView); + + if (bitmapWorkerTask != null) { + final File bitmapData = bitmapWorkerTask.mFile; + // If bitmapData is not yet set or it differs from the new data + if (bitmapData == null || bitmapData != file) { + // Cancel previous task + bitmapWorkerTask.cancel(true); + } else { + // The same work is already in progress + return false; + } + } + // No task associated with the ImageView, or an existing task was cancelled + return true; + } + public static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) { if (imageView != null) { final Drawable drawable = imageView.getDrawable(); @@@ -184,84 -168,6 +186,84 @@@ } return null; } + + public static ThumbnailLocalGenerationTask getBitmapLocalWorkerTask(ImageView imageView) { + if (imageView != null) { + final Drawable drawable = imageView.getDrawable(); + if (drawable instanceof AsyncLocalDrawable) { + final AsyncLocalDrawable asyncDrawable = (AsyncLocalDrawable) drawable; + return asyncDrawable.getBitmapWorkerTask(); + } + } + return null; + } + + public static class ThumbnailLocalGenerationTask extends AsyncTask { + private final WeakReference mImageViewLocalReference; + private File mFile; + + public ThumbnailLocalGenerationTask(ImageView imageView) { + // Use a WeakReference to ensure the ImageView can be garbage collected + mImageViewLocalReference = new WeakReference(imageView); + } + + // Decode image in background. + @Override + protected Bitmap doInBackground(File... params) { + Bitmap thumbnail = null; + + try { + mFile = params[0]; + final String imageKey = String.valueOf(mFile.hashCode()); + + // Check disk cache in background thread + thumbnail = getBitmapFromDiskCache(imageKey); + + // Not found in disk cache + if (thumbnail == null) { + // Converts dp to pixel + Resources r = MainApp.getAppContext().getResources(); + + int px = (int) Math.round(r.getDimension(R.dimen.file_icon_size)); + + Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile( + mFile.getAbsolutePath(), px, px); + + if (bitmap != null) { + thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px); + + // Add thumbnail to cache + addBitmapToCache(imageKey, thumbnail); + } + } + + } catch (Throwable t) { + // the app should never break due to a problem with thumbnails + Log_OC.e(TAG, "Generation of thumbnail for " + mFile + " failed", t); + if (t instanceof OutOfMemoryError) { + System.gc(); + } + } + + return thumbnail; + } + + protected void onPostExecute(Bitmap bitmap){ + if (isCancelled()) { + bitmap = null; + } + + if (mImageViewLocalReference != null && bitmap != null) { + final ImageView imageView = mImageViewLocalReference.get(); + final ThumbnailLocalGenerationTask bitmapWorkerTask = getBitmapLocalWorkerTask(imageView); + if (this == bitmapWorkerTask && imageView != null) { + if (imageView.getTag().equals(mFile.hashCode())) { + imageView.setImageBitmap(bitmap); + } + } + } + } + } public static class ThumbnailGenerationTask extends AsyncTask { private final WeakReference mImageViewReference; @@@ -312,6 -218,9 +314,9 @@@ if (bitmap != null) { thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px); + + // Rotate image, obeying exif tag + thumbnail = BitmapUtils.rotateImage(thumbnail, mFile.getStoragePath()); // Add thumbnail to cache addBitmapToCache(imageKey, thumbnail); @@@ -399,20 -308,6 +404,20 @@@ return bitmapWorkerTaskReference.get(); } } + + public static class AsyncLocalDrawable extends BitmapDrawable { + private final WeakReference bitmapWorkerLocalTaskReference; + + public AsyncLocalDrawable(Resources res, Bitmap bitmap, ThumbnailLocalGenerationTask bitmapWorkerTask) { + super(res, bitmap); + bitmapWorkerLocalTaskReference = + new WeakReference(bitmapWorkerTask); + } + + public ThumbnailLocalGenerationTask getBitmapWorkerTask() { + return bitmapWorkerLocalTaskReference.get(); + } + } /** @@@ -427,5 -322,5 +432,5 @@@ mThumbnailsDiskCacheLock.notifyAll(); // Wake any waiting threads } } - + }