From 541c8ba80d35f60500ff82d4cdd7ba1ccca45660 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 6 Oct 2023 12:34:07 +0200 Subject: [PATCH] modify mime type detection for shared files add support for audiobooks --- .../ui/adapter/MediaAdapter.java | 2 + .../ui/adapter/MediaPreviewAdapter.java | 2 +- .../siacs/conversations/utils/MimeUtils.java | 79 +++++++++++-------- .../siacs/conversations/utils/UIHelper.java | 2 + .../res/drawable/ic_play_lesson_black_24.xml | 6 ++ .../drawable/ic_play_lesson_white_48dp.xml | 6 ++ src/main/res/values/attrs.xml | 1 + src/main/res/values/strings.xml | 1 + src/main/res/values/themes.xml | 2 + 9 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 src/main/res/drawable/ic_play_lesson_black_24.xml create mode 100644 src/main/res/drawable/ic_play_lesson_white_48dp.xml diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MediaAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MediaAdapter.java index 2733e7b8b..2683876c7 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MediaAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MediaAdapter.java @@ -74,6 +74,8 @@ public class MediaAdapter extends RecyclerView.Adapter PATH_PRECEDENCE_MIME_TYPE = Arrays.asList("audio/x-m4b"); + private static void add(String mimeType, String extension) { // If we have an existing x -> y mapping, we do not want to // override it with another mapping x -> y2. @@ -537,44 +543,49 @@ public final class MimeUtils { } public static String guessMimeTypeFromUriAndMime(final Context context, final Uri uri, final String mime) { - Log.d(Config.LOGTAG, "guessMimeTypeFromUriAndMime " + uri + " and mime=" + mime); - if (mime == null || mime.equals("application/octet-stream")) { - final String guess = guessMimeTypeFromUri(context, uri); - if (guess != null) { - return guess; - } else { - return mime; - } + Log.d(Config.LOGTAG, "guessMimeTypeFromUriAndMime(" + uri + "," + mime+")"); + final String mimeFromUri = guessMimeTypeFromUri(context, uri); + Log.d(Config.LOGTAG,"mimeFromUri:"+mimeFromUri); + if (PATH_PRECEDENCE_MIME_TYPE.contains(mimeFromUri)) { + return mimeFromUri; + } else if (mime == null || mime.equals("application/octet-stream")) { + return mimeFromUri; + } else { + return mime; } - return guessMimeTypeFromUri(context, uri); } - public static String guessMimeTypeFromUri(Context context, Uri uri) { - // try the content resolver - String mimeType; + public static String guessMimeTypeFromUri(final Context context, final Uri uri) { + final String mimeTypeContentResolver = guessFromContentResolver(context, uri); + final String mimeTypeFromQueryParameter = uri.getQueryParameter("mimeType"); + final String name = "content".equals(uri.getScheme()) ? getDisplayName(context, uri) : null; + final String mimeTypeFromName = Strings.isNullOrEmpty(name) ? null : guessFromPath(name); + final String path = uri.getPath(); + final String mimeTypeFromPath = Strings.isNullOrEmpty(path) ? null : guessFromPath(path); + if (PATH_PRECEDENCE_MIME_TYPE.contains(mimeTypeFromName)) { + return mimeTypeFromName; + } + if (PATH_PRECEDENCE_MIME_TYPE.contains(mimeTypeFromPath)) { + return mimeTypeFromPath; + } + if (mimeTypeContentResolver != null && !"application/octet-stream".equals(mimeTypeContentResolver)) { + return mimeTypeContentResolver; + } + if (mimeTypeFromName != null) { + return mimeTypeFromName; + } + if (mimeTypeFromQueryParameter != null) { + return mimeTypeFromQueryParameter; + } + return mimeTypeFromPath; + } + + private static String guessFromContentResolver(final Context context, final Uri uri) { try { - mimeType = context.getContentResolver().getType(uri); - } catch (final Throwable throwable) { - mimeType = null; + return context.getContentResolver().getType(uri); + } catch (final Throwable e) { + return null; } - // try the extension - if (mimeType == null || mimeType.equals("application/octet-stream")) { - final String path = uri.getPath(); - if (path != null) { - mimeType = guessFromPath(path); - } - } - if (mimeType == null && "content".equals(uri.getScheme())) { - final String name = getDisplayName(context, uri); - if (name != null) { - mimeType = guessFromPath(name); - } - } - // sometimes this works (as with the commit content api) - if (mimeType == null) { - mimeType = uri.getQueryParameter("mimeType"); - } - return mimeType; } private static String getDisplayName(final Context context, final Uri uri) { diff --git a/src/main/java/eu/siacs/conversations/utils/UIHelper.java b/src/main/java/eu/siacs/conversations/utils/UIHelper.java index b70bfc558..da72ba9bd 100644 --- a/src/main/java/eu/siacs/conversations/utils/UIHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/UIHelper.java @@ -481,6 +481,8 @@ public class UIHelper { return context.getString(R.string.file); } else if (MimeUtils.AMBIGUOUS_CONTAINER_FORMATS.contains(mime)) { return context.getString(R.string.multimedia_file); + } else if (mime.equals("audio/x-m4b")) { + return context.getString(R.string.audiobook); } else if (mime.startsWith("audio/")) { return context.getString(R.string.audio); } else if (mime.startsWith("video/")) { diff --git a/src/main/res/drawable/ic_play_lesson_black_24.xml b/src/main/res/drawable/ic_play_lesson_black_24.xml new file mode 100644 index 000000000..4c4a46ce7 --- /dev/null +++ b/src/main/res/drawable/ic_play_lesson_black_24.xml @@ -0,0 +1,6 @@ + + + + diff --git a/src/main/res/drawable/ic_play_lesson_white_48dp.xml b/src/main/res/drawable/ic_play_lesson_white_48dp.xml new file mode 100644 index 000000000..67fe7c696 --- /dev/null +++ b/src/main/res/drawable/ic_play_lesson_white_48dp.xml @@ -0,0 +1,6 @@ + + + + diff --git a/src/main/res/values/attrs.xml b/src/main/res/values/attrs.xml index 2a9a4a8e6..f66437bf4 100644 --- a/src/main/res/values/attrs.xml +++ b/src/main/res/values/attrs.xml @@ -70,6 +70,7 @@ + diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 84b2cb61e..4981128de 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -420,6 +420,7 @@ multimedia file PDF document Android App + Audiobook Contact Avatar has been published! Sending %s diff --git a/src/main/res/values/themes.xml b/src/main/res/values/themes.xml index 9e757a626..75fd896af 100644 --- a/src/main/res/values/themes.xml +++ b/src/main/res/values/themes.xml @@ -88,6 +88,7 @@ @drawable/baseline_tour_black_48 @drawable/ic_person_black_48dp @drawable/ic_android_black_48dp + @drawable/ic_play_lesson_black_24 @drawable/ic_event_black_48dp @drawable/ic_archive_black_48dp @drawable/ic_book_black_48dp @@ -244,6 +245,7 @@ @drawable/baseline_tour_white_48 @drawable/ic_person_white_48dp @drawable/ic_android_white_48dp + @drawable/ic_play_lesson_white_48dp @drawable/ic_event_white_48dp @drawable/ic_archive_white_48dp @drawable/ic_book_white_48dp