Handle thumbnail generation for invalid local path

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-09-25 11:25:45 +02:00 committed by Alper Öztürk
parent 7e0e59fadb
commit ce41617086
2 changed files with 57 additions and 29 deletions

View file

@ -485,9 +485,8 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
&& fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId() != null &&
item.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED) {
// Thumbnail in Cache?
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
String.valueOf(fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId())
);
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId()));
if (thumbnail != null && !fakeFileToCheatThumbnailsCacheManagerInterface.isUpdateThumbnailNeeded()) {
itemViewHolder.binding.thumbnail.setImageBitmap(thumbnail);
} else {
@ -520,47 +519,51 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
}
if ("image/png".equals(item.getMimeType())) {
itemViewHolder.binding.thumbnail.setBackgroundColor(parentActivity.getResources()
.getColor(R.color.bg_default));
itemViewHolder.binding.thumbnail.setBackgroundColor(parentActivity.getResources().getColor(R.color.bg_default));
}
} else if (MimeTypeUtil.isImage(fakeFileToCheatThumbnailsCacheManagerInterface)) {
File file = new File(item.getLocalPath());
// Thumbnail in Cache?
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
String.valueOf(file.hashCode()));
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(file.hashCode()));
if (thumbnail != null) {
itemViewHolder.binding.thumbnail.setImageBitmap(thumbnail);
} else {
// generate new Thumbnail
if (allowedToCreateNewThumbnail) {
final ThumbnailsCacheManager.ThumbnailGenerationTask task =
new ThumbnailsCacheManager.ThumbnailGenerationTask(itemViewHolder.binding.thumbnail);
} else if (allowedToCreateNewThumbnail) {
getThumbnailFromFileTypeAndSetIcon(item.getLocalPath(), itemViewHolder);
if (MimeTypeUtil.isVideo(file)) {
thumbnail = ThumbnailsCacheManager.mDefaultVideo;
} else {
thumbnail = ThumbnailsCacheManager.mDefaultImg;
final ThumbnailsCacheManager.ThumbnailGenerationTask task =
new ThumbnailsCacheManager.ThumbnailGenerationTask(itemViewHolder.binding.thumbnail);
if (MimeTypeUtil.isVideo(file)) {
thumbnail = ThumbnailsCacheManager.mDefaultVideo;
} else {
thumbnail = ThumbnailsCacheManager.mDefaultImg;
}
final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable =
new ThumbnailsCacheManager.AsyncThumbnailDrawable(parentActivity.getResources(), thumbnail,
task);
task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, null));
task.setListener(new ThumbnailsCacheManager.ThumbnailGenerationTask.Listener() {
@Override
public void onSuccess() {
itemViewHolder.binding.thumbnail.setImageDrawable(asyncDrawable);
}
final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable =
new ThumbnailsCacheManager.AsyncThumbnailDrawable(parentActivity.getResources(), thumbnail,
task);
@Override
public void onError() {
getThumbnailFromFileTypeAndSetIcon(item.getLocalPath(), itemViewHolder);
}
});
itemViewHolder.binding.thumbnail.setImageDrawable(asyncDrawable);
task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, null));
Log_OC.v(TAG, "Executing task to generate a new thumbnail");
}
Log_OC.v(TAG, "Executing task to generate a new thumbnail");
}
if ("image/png".equalsIgnoreCase(item.getMimeType())) {
itemViewHolder.binding.thumbnail.setBackgroundColor(parentActivity.getResources()
.getColor(R.color.bg_default));
itemViewHolder.binding.thumbnail.setBackgroundColor(parentActivity.getResources().getColor(R.color.bg_default));
}
} else {
if (optionalUser.isPresent()) {
final User user = optionalUser.get();
final Drawable icon = MimeTypeUtil.getFileTypeIcon(item.getMimeType(),
fileName,
parentActivity,
@ -570,6 +573,12 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
}
}
private void getThumbnailFromFileTypeAndSetIcon(String localPath, ItemViewHolder itemViewHolder) {
Drawable drawable = MimeTypeUtil.getIcon(localPath, parentActivity, viewThemeUtils);
if (drawable == null) return;
itemViewHolder.binding.thumbnail.setImageDrawable(drawable);
}
private boolean checkAndOpenConflictResolutionDialog(User user,
ItemViewHolder itemViewHolder,
OCUpload item,

View file

@ -17,6 +17,7 @@ import android.net.Uri;
import android.webkit.MimeTypeMap;
import com.nextcloud.android.common.ui.theme.utils.ColorRole;
import com.nextcloud.utils.autoRename.AutoRename;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.resources.files.model.ServerFileInterface;
@ -106,6 +107,23 @@ public final class MimeTypeUtil {
}
}
public static Drawable getIcon(String localPath, Context context, ViewThemeUtils viewThemeUtils) {
if (context == null) return null;
String mimeType = getMimeTypeFromPath(localPath);
List<String> possibleMimeTypes = Collections.singletonList(mimeType);
int iconId = determineIconIdByMimeTypeList(possibleMimeTypes);
Drawable result = ContextCompat.getDrawable(context, iconId);
if (result == null) return null;
if (R.drawable.file_zip == iconId) {
viewThemeUtils.platform.tintDrawable(context, result, ColorRole.PRIMARY);
}
return result;
}
/**
* Returns the resource identifier of an image to use as icon associated to a known MIME type.
*
@ -317,6 +335,7 @@ public final class MimeTypeUtil {
* @return the file's mime type
*/
private static String extractMimeType(File file) {
file = new File(file.getName().trim());
Uri selectedUri = Uri.fromFile(file);
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString().toLowerCase(Locale.ROOT));
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);