X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/788ebbcdabfde85592bb2d26edb90e5a5e644a3e..d44c785d0727b71ab696d5a6216f00cf135867c6:/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 49bda88e..e8496951 100644 --- a/src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java +++ b/src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java @@ -35,8 +35,10 @@ import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; +import android.graphics.Canvas; import android.graphics.Point; import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.media.ThumbnailUtils; import android.net.Uri; @@ -79,8 +81,8 @@ public class ThumbnailsCacheManager { public static Bitmap mDefaultImg = BitmapFactory.decodeResource( - MainApp.getAppContext().getResources(), - DisplayUtils.getFileTypeIconId("image/png", "default.png") + MainApp.getAppContext().getResources(), + R.drawable.file_image ); @@ -210,10 +212,6 @@ public class ThumbnailsCacheManager { } protected void onPostExecute(Bitmap bitmap){ - if (isCancelled()) { - bitmap = null; - } - if (bitmap != null) { final ImageView imageView = mImageViewReference.get(); final ThumbnailGenerationTask bitmapWorkerTask = getBitmapWorkerTask(imageView); @@ -308,11 +306,18 @@ public class ThumbnailsCacheManager { } if (file.isDown()) { - Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile( + Bitmap tempBitmap = BitmapUtils.decodeSampledBitmapFromFile( file.getStoragePath(), pxW, pxH); + Bitmap bitmap = ThumbnailUtils.extractThumbnail(tempBitmap, pxW, pxH); if (bitmap != null) { - thumbnail = addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), pxW, pxH); + // Handle PNG + if (file.getMimetype().equalsIgnoreCase("image/png")) { + bitmap = handlePNG(bitmap, pxW); + } + + thumbnail = addThumbnailToCache(imageKey, bitmap, + file.getStoragePath(), pxW, pxH); file.setNeedsUpdateThumbnail(false); mStorageManager.saveFile(file); @@ -324,44 +329,51 @@ public class ThumbnailsCacheManager { if (mClient != null && serverOCVersion != null) { if (serverOCVersion.supportsRemoteThumbnails()) { try { - String uri = mClient.getBaseUri() + "" + - "/index.php/apps/files/api/v1/thumbnail/" + - pxW + "/" + pxH + Uri.encode(file.getRemotePath(), "/"); - Log_OC.d("Thumbnail", "URI: " + uri); - GetMethod get = new GetMethod(uri); - int 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, pxW, pxH); - byte[] bytes = get.getResponseBody(); - - String type = ""; - if (mIsThumbnail){ - type = "Thumbnail"; + if (mIsThumbnail) { + String uri = mClient.getBaseUri() + "" + + "/index.php/apps/files/api/v1/thumbnail/" + + pxW + "/" + pxH + Uri.encode(file.getRemotePath(), "/"); + Log_OC.d("Thumbnail", "Download URI: " + uri); + GetMethod get = new GetMethod(uri); + int status = mClient.executeMethod(get); + if (status == HttpStatus.SC_OK) { + InputStream inputStream = get.getResponseBodyAsStream(); + Bitmap bitmap = BitmapFactory.decodeStream(inputStream); + thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH); } else { - type = "Resized image"; + Log_OC.d(TAG, "Status: " + status); } - Log_OC.d("Thumbnail", - type + " size of " + file.getRemotePath() - + ": " + bytes.length); - - // bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); - - if (mIsThumbnail) { - thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH); + } else { + String gallery = ""; + if (serverOCVersion.supportsNativeGallery()){ + gallery = "gallery"; } else { - thumbnail = bitmap; + gallery = "galleryplus"; } - // Add thumbnail to cache - if (thumbnail != null) { - addBitmapToCache(imageKey, thumbnail); + String uri = mClient.getBaseUri() + + "/index.php/apps/" + gallery + "/api/preview/" + Integer.parseInt(file.getRemoteId().substring(0,8)) + + "/" + pxW + "/" + pxH; + Log_OC.d("Thumbnail", "FileName: " + file.getFileName() + " Download URI: " + uri); + GetMethod get = new GetMethod(uri); + int status = mClient.executeMethod(get); + if (status == HttpStatus.SC_OK) { + InputStream inputStream = get.getResponseBodyAsStream(); + Bitmap bitmap = BitmapFactory.decodeStream(inputStream); + // Download via gallery app + thumbnail = bitmap; } } + + // Handle PNG + if (thumbnail != null && file.getMimetype().equalsIgnoreCase("image/png")) { + thumbnail = handlePNG(thumbnail, pxW); + } + + // Add thumbnail to cache + if (thumbnail != null) { + addBitmapToCache(imageKey, thumbnail); + } } catch (Exception e) { e.printStackTrace(); } @@ -376,6 +388,19 @@ public class ThumbnailsCacheManager { } + private Bitmap handlePNG(Bitmap bitmap, int px){ + Bitmap resultBitmap = Bitmap.createBitmap(px, + px, + Bitmap.Config.ARGB_8888); + Canvas c = new Canvas(resultBitmap); + + c.drawColor(MainApp.getAppContext().getResources(). + getColor(R.color.background_color)); + c.drawBitmap(bitmap, 0, 0, null); + + return resultBitmap; + } + private Bitmap doFileInBackground(Boolean mIsThumbnail) { Bitmap thumbnail = null; File file = (File)mFile; @@ -418,6 +443,7 @@ public class ThumbnailsCacheManager { if (bitmapData == null || bitmapData != file) { // Cancel previous task bitmapWorkerTask.cancel(true); + Log_OC.v(TAG, "Cancelled generation of thumbnail for a reused imageView"); } else { // The same work is already in progress return false;