diff --git a/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java b/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java index 8982715053..9f299d8230 100644 --- a/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java +++ b/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java @@ -193,6 +193,10 @@ public final class ThumbnailsCacheManager { } } + public static boolean containsBitmap(String key) { + return mThumbnailCache.containsKey(key); + } + public static Bitmap getBitmapFromDiskCache(String key) { synchronized (mThumbnailsDiskCacheLock) { // Wait while disk cache is started from background thread @@ -1166,4 +1170,53 @@ public final class ThumbnailsCacheManager { addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), pxW, pxH); } } + + public static void generateThumbnailFromOCFile(OCFile file) { + int pxW; + int pxH; + pxW = pxH = getThumbnailDimension(); + String imageKey = PREFIX_THUMBNAIL + String.valueOf(file.getRemoteId()); + + GetMethod getMethod = null; + + try { + Bitmap thumbnail = null; + + String uri = mClient.getBaseUri() + "/index.php/apps/files/api/v1/thumbnail/" + + pxW + "/" + pxH + Uri.encode(file.getRemotePath(), "/"); + + Log_OC.d(TAG, "generate thumbnail: " + file.getFileName() + " URI: " + uri); + getMethod = new GetMethod(uri); + getMethod.setRequestHeader("Cookie", "nc_sameSiteCookielax=true;nc_sameSiteCookiestrict=true"); + + getMethod.setRequestHeader(RemoteOperation.OCS_API_HEADER, + RemoteOperation.OCS_API_HEADER_VALUE); + + int status = mClient.executeMethod(getMethod); + if (status == HttpStatus.SC_OK) { + InputStream inputStream = getMethod.getResponseBodyAsStream(); + Bitmap bitmap = BitmapFactory.decodeStream(inputStream); + thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH); + } else { + mClient.exhaustResponse(getMethod.getResponseBodyAsStream()); + } + + // Add thumbnail to cache + if (thumbnail != null) { + // Handle PNG + if (file.getMimeType().equalsIgnoreCase(PNG_MIMETYPE)) { + thumbnail = handlePNG(thumbnail, pxW, pxH); + } + + Log_OC.d(TAG, "add thumbnail to cache: " + file.getFileName()); + addBitmapToCache(imageKey, thumbnail); + } + } catch (Exception e) { + Log_OC.d(TAG, e.getMessage(), e); + } finally { + if (getMethod != null) { + getMethod.releaseConnection(); + } + } + } } diff --git a/src/main/java/com/owncloud/android/jobs/AccountRemovalJob.java b/src/main/java/com/owncloud/android/jobs/AccountRemovalJob.java index 37cef45aa0..04a5472ba9 100644 --- a/src/main/java/com/owncloud/android/jobs/AccountRemovalJob.java +++ b/src/main/java/com/owncloud/android/jobs/AccountRemovalJob.java @@ -74,10 +74,7 @@ public class AccountRemovalJob extends Job implements AccountManagerCallback ownContactList = new TreeMap<>(new ContactListFragment.VCardComparator()); + TreeMap ownContactMap = new TreeMap<>(new ContactListFragment.VCardComparator()); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); for (int i = 0; i < cursor.getCount(); i++) { VCard vCard = getContactFromCursor(cursor); if (vCard != null) { - ownContactList.put(vCard, cursor.getLong(cursor.getColumnIndex("NAME_RAW_CONTACT_ID"))); + ownContactMap.put(vCard, cursor.getLong(cursor.getColumnIndex("NAME_RAW_CONTACT_ID"))); } cursor.moveToNext(); } @@ -91,10 +91,10 @@ public class ContactsImportJob extends Job { for (int checkedItem : intArray) { VCard vCard = vCards.get(checkedItem); if (ContactListFragment.getDisplayName(vCard).length() != 0) { - if (!ownContactList.containsKey(vCard)) { + if (!ownContactMap.containsKey(vCard)) { operations.insertContact(vCard); } else { - operations.updateContact(vCard, ownContactList.get(vCard)); + operations.updateContact(vCard, ownContactMap.get(vCard)); } } else { operations.insertContact(vCard); //Insert All the contacts without name diff --git a/src/main/java/com/owncloud/android/providers/DiskLruImageCacheFileProvider.java b/src/main/java/com/owncloud/android/providers/DiskLruImageCacheFileProvider.java index 8baabda7a3..29956e81de 100644 --- a/src/main/java/com/owncloud/android/providers/DiskLruImageCacheFileProvider.java +++ b/src/main/java/com/owncloud/android/providers/DiskLruImageCacheFileProvider.java @@ -86,7 +86,7 @@ public class DiskLruImageCacheFileProvider extends ContentProvider { //Convert bitmap to byte array ByteArrayOutputStream bos = new ByteArrayOutputStream(); - thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bos); + thumbnail.compress(Bitmap.CompressFormat.PNG, 90, bos); byte[] bitmapData = bos.toByteArray(); //write the bytes in file diff --git a/src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java b/src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java index b9a22104fa..c42ba3f91e 100644 --- a/src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java +++ b/src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java @@ -32,6 +32,7 @@ import android.content.SharedPreferences; import android.content.res.AssetFileDescriptor; import android.database.Cursor; import android.graphics.Point; +import android.net.Uri; import android.os.Build; import android.os.CancellationSignal; import android.os.Handler; @@ -49,6 +50,7 @@ import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.datamodel.ThumbnailsCacheManager; import com.owncloud.android.files.services.FileDownloader; import com.owncloud.android.files.services.FileUploader; import com.owncloud.android.lib.common.OwnCloudAccount; @@ -67,6 +69,7 @@ import com.owncloud.android.operations.UploadFileOperation; import com.owncloud.android.ui.activity.ConflictsResolveActivity; import com.owncloud.android.ui.activity.Preferences; import com.owncloud.android.utils.FileStorageUtils; +import com.owncloud.android.utils.UriUtils; import org.nextcloud.providers.cursors.FileCursor; import org.nextcloud.providers.cursors.RootCursor; @@ -304,12 +307,22 @@ public class DocumentsStorageProvider extends DocumentsProvider { throw new FileNotFoundException("File with id " + documentId + " not found!"); } - File realFile = new File(file.getStoragePath()); + Context context = getContext(); - return new AssetFileDescriptor( - ParcelFileDescriptor.open(realFile, ParcelFileDescriptor.MODE_READ_ONLY), - 0, - AssetFileDescriptor.UNKNOWN_LENGTH); + if (context == null) { + throw new FileNotFoundException("Context may not be null!"); + } + + boolean exists = ThumbnailsCacheManager.containsBitmap(ThumbnailsCacheManager.PREFIX_THUMBNAIL + + file.getRemoteId()); + + if (!exists) { + ThumbnailsCacheManager.generateThumbnailFromOCFile(file); + } + + Uri uri = Uri.parse(UriUtils.URI_CONTENT_SCHEME + context.getResources().getString( + R.string.image_cache_provider_authority) + file.getRemotePath()); + return context.getContentResolver().openAssetFileDescriptor(uri, "r"); } @Override diff --git a/src/main/java/com/owncloud/android/ui/adapter/DiskLruImageCache.java b/src/main/java/com/owncloud/android/ui/adapter/DiskLruImageCache.java index d28ec01318..f56423c365 100644 --- a/src/main/java/com/owncloud/android/ui/adapter/DiskLruImageCache.java +++ b/src/main/java/com/owncloud/android/ui/adapter/DiskLruImageCache.java @@ -108,7 +108,7 @@ public class DiskLruImageCache { Bitmap bitmap = null; DiskLruCache.Snapshot snapshot = null; String validKey = convertToValidKey(key); - + try { snapshot = mDiskCache.get(validKey); if (snapshot == null) { @@ -189,4 +189,4 @@ public class DiskLruImageCache { Log_OC.d(TAG, e.getMessage(), e); } } -} \ No newline at end of file +} diff --git a/src/main/java/org/nextcloud/providers/cursors/FileCursor.java b/src/main/java/org/nextcloud/providers/cursors/FileCursor.java index 13fda66133..b764f498e8 100644 --- a/src/main/java/org/nextcloud/providers/cursors/FileCursor.java +++ b/src/main/java/org/nextcloud/providers/cursors/FileCursor.java @@ -48,10 +48,9 @@ public class FileCursor extends MatrixCursor { final int iconRes = MimeTypeUtil.getFileTypeIconId(file.getMimeType(), file.getFileName()); final String mimeType = file.isFolder() ? Document.MIME_TYPE_DIR : file.getMimeType(); - final String imagePath = MimeTypeUtil.isImage(file) && file.isDown() ? file.getStoragePath() : null; int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE | - (imagePath != null ? Document.FLAG_SUPPORTS_THUMBNAIL : 0); + (MimeTypeUtil.isImage(file) ? Document.FLAG_SUPPORTS_THUMBNAIL : 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { flags = Document.FLAG_SUPPORTS_COPY | Document.FLAG_SUPPORTS_MOVE | Document.FLAG_SUPPORTS_REMOVE | flags;