+
+ /**
+ * Add thumbnail to cache
+ * @param imageKey: thumb key
+ * @param bitmap: image for extracting thumbnail
+ * @param path: image path
+ * @param px: thumbnail dp
+ * @return Bitmap
+ */
+ private Bitmap addThumbnailToCache(String imageKey, Bitmap bitmap, String path, int px){
+
+ Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);
+
+ // Rotate image, obeying exif tag
+ thumbnail = BitmapUtils.rotateImage(thumbnail,path);
+
+ // Add thumbnail to cache
+ addBitmapToCache(imageKey, thumbnail);
+
+ return thumbnail;
+ }
+
+ /**
+ * Converts size of file icon from dp to pixel
+ * @return int
+ */
+ private int getThumbnailDimension(){
+ // Converts dp to pixel
+ 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;
+ }
+