From 55ac35b70c6c9abc66849f8ede774d6afe45722c Mon Sep 17 00:00:00 2001 From: nuclearfog Date: Thu, 6 Jul 2023 23:38:35 +0200 Subject: [PATCH] code cleanup, added video media description, fixed crash --- .../twidda/backend/api/mastodon/Mastodon.java | 8 +- .../backend/api/twitter/v1/TwitterV1.java | 12 +- .../twidda/backend/helper/MediaStatus.java | 13 +-- .../backend/helper/update/MessageUpdate.java | 40 ++----- .../backend/helper/update/ProfileUpdate.java | 87 +++----------- .../twidda/ui/activities/ImageViewer.java | 40 +++---- .../twidda/ui/activities/MessageEditor.java | 8 +- .../twidda/ui/activities/ProfileEditor.java | 34 +++--- .../twidda/ui/activities/StatusActivity.java | 11 +- .../twidda/ui/activities/StatusEditor.java | 41 ++++--- .../twidda/ui/activities/VideoViewer.java | 108 ++++++++++++------ 11 files changed, 186 insertions(+), 216 deletions(-) diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/api/mastodon/Mastodon.java b/app/src/main/java/org/nuclearfog/twidda/backend/api/mastodon/Mastodon.java index 60602b83..1018bf79 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/api/mastodon/Mastodon.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/api/mastodon/Mastodon.java @@ -1074,12 +1074,12 @@ public class Mastodon implements Connection { params.add("display_name=" + StringUtils.encode(update.getName())); params.add("note=" + StringUtils.encode(update.getDescription())); - if (update.getProfileImageStream() != null) { - streams.add(update.getProfileImageStream()); + if (update.getProfileImageMedia() != null) { + streams.add(update.getProfileImageMedia().getStream()); keys.add("avatar"); } - if (update.getBannerImageStream() != null) { - streams.add(update.getBannerImageStream()); + if (update.getBannerImageMedia() != null) { + streams.add(update.getBannerImageMedia().getStream()); keys.add("header"); } try { diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/api/twitter/v1/TwitterV1.java b/app/src/main/java/org/nuclearfog/twidda/backend/api/twitter/v1/TwitterV1.java index 7fc4bef9..58515b63 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/api/twitter/v1/TwitterV1.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/api/twitter/v1/TwitterV1.java @@ -1033,10 +1033,10 @@ public class TwitterV1 implements Connection { params.add("command=INIT"); params.add("media_type=" + mediaUpdate.getMimeType()); params.add("total_bytes=" + mediaUpdate.available()); - if (mediaUpdate.getMimeType().startsWith("video/")) { + if (mediaUpdate.getMimeType() != null && mediaUpdate.getMimeType().startsWith("video/")) { params.add("media_category=tweet_video"); enableChunk = true; - } else if (mediaUpdate.getMimeType().startsWith("image/gif")) { + } else if (mediaUpdate.getMimeType() != null && mediaUpdate.getMimeType().startsWith("image/gif")) { params.add("media_category=tweet_gif"); enableChunk = true; } else { @@ -1158,11 +1158,11 @@ public class TwitterV1 implements Connection { params.add("url=" + StringUtils.encode(update.getUrl())); params.add("location=" + StringUtils.encode(update.getLocation())); params.add("description=" + StringUtils.encode(update.getDescription())); - if (update.getProfileImageStream() != null) { - updateImage(PROFILE_UPDATE_IMAGE, update.getProfileImageStream(), "image"); + if (update.getProfileImageMedia() != null) { + updateImage(PROFILE_UPDATE_IMAGE, update.getProfileImageMedia().getStream(), "image"); } - if (update.getBannerImageStream() != null) { - updateImage(PROFILE_UPDATE_BANNER, update.getBannerImageStream(), "banner"); + if (update.getBannerImageMedia() != null) { + updateImage(PROFILE_UPDATE_BANNER, update.getBannerImageMedia().getStream(), "banner"); } return getUser(PROFILE_UPDATE, params); } diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/helper/MediaStatus.java b/app/src/main/java/org/nuclearfog/twidda/backend/helper/MediaStatus.java index eee283f8..ba21d477 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/helper/MediaStatus.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/helper/MediaStatus.java @@ -12,6 +12,7 @@ import org.nuclearfog.twidda.BuildConfig; import org.nuclearfog.twidda.model.Media; import java.io.Closeable; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; @@ -76,20 +77,18 @@ public class MediaStatus implements Serializable, Closeable { /** * create MediaStatus from an offline source * - * @param uri path to the local file - * @param description description of the media source - * @throws IllegalArgumentException when the file is invalid + * @param uri path to the local file + * @throws FileNotFoundException if the file is invalid */ - public MediaStatus(Context context, Uri uri, String description) throws IllegalArgumentException { + public MediaStatus(Context context, Uri uri) throws FileNotFoundException { DocumentFile file = DocumentFile.fromSingleUri(context, uri); if (file != null && file.length() > 0) { - this.description = description; mimeType = context.getContentResolver().getType(uri); type = getType(mimeType); path = uri.toString(); local = true; } else { - throw new IllegalArgumentException(); + throw new FileNotFoundException("file not supported!"); } } @@ -246,7 +245,7 @@ public class MediaStatus implements Serializable, Closeable { * @param mimeType mime type of the media file * @return media type {@link #GIF,#PHOTO ,#VIDEO,#AUDIO} or {@link #INVALID} if media file is not supported */ - private int getType(String mimeType) throws IllegalArgumentException { + private int getType(String mimeType) { if (mimeType.equals("image/gif")) return GIF; if (mimeType.startsWith("image/")) diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/MessageUpdate.java b/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/MessageUpdate.java index 2de23bbf..bde864af 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/MessageUpdate.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/MessageUpdate.java @@ -1,20 +1,15 @@ package org.nuclearfog.twidda.backend.helper.update; import android.content.ContentResolver; -import android.content.Context; -import android.net.Uri; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.documentfile.provider.DocumentFile; import org.nuclearfog.twidda.backend.helper.MediaStatus; import org.nuclearfog.twidda.model.Instance; import java.io.Closeable; import java.io.Serializable; -import java.util.Arrays; -import java.util.TreeSet; /** * This class is used to upload a message @@ -32,9 +27,6 @@ public class MessageUpdate implements Serializable, Closeable { private String name = ""; private String text = ""; - - private TreeSet supportedFormats = new TreeSet<>(); - /** * close inputstream of media file */ @@ -79,41 +71,24 @@ public class MessageUpdate implements Serializable, Closeable { /** - * get inputstream of the media file + * get media attachment * - * @return input stream + * @return media attachment */ @Nullable public MediaStatus getMediaStatus() { return mediaStatus; } + /** + * add/update media attachment + * + * @param mediaStatus media attachment + */ public void setMediaUpdate(MediaStatus mediaStatus) { this.mediaStatus = mediaStatus; } - /** - * add media uri and create input stream - * - * @param context context used to create inputstream and mime type - * @param uri uri of a local media file - * @return true if file is valid - */ - public boolean addMedia(Context context, @NonNull Uri uri) { - DocumentFile file = DocumentFile.fromSingleUri(context, uri); - String mime = context.getContentResolver().getType(uri); - // check if file is valid - if (mime == null || file == null || file.length() == 0 || !supportedFormats.contains(mime)) { - return false; - } - try { - mediaStatus = new MediaStatus(context, uri, ""); - } catch (IllegalArgumentException exception) { - return false; - } - return true; - } - /** * initialize inputstream of the file to upload * @@ -129,7 +104,6 @@ public class MessageUpdate implements Serializable, Closeable { * @param instance instance imformation */ public void setInstanceInformation(Instance instance) { - supportedFormats.addAll(Arrays.asList(instance.getSupportedFormats())); this.instance = instance; } diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/ProfileUpdate.java b/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/ProfileUpdate.java index 2a3e655e..302443b3 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/ProfileUpdate.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/helper/update/ProfileUpdate.java @@ -1,19 +1,12 @@ package org.nuclearfog.twidda.backend.helper.update; -import android.annotation.SuppressLint; import android.content.ContentResolver; -import android.content.Context; -import android.net.Uri; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.documentfile.provider.DocumentFile; - -import org.nuclearfog.twidda.BuildConfig; +import org.nuclearfog.twidda.backend.helper.MediaStatus; import java.io.Closeable; -import java.io.IOException; -import java.io.InputStream; /** * This class is used to upload profile information @@ -22,8 +15,7 @@ import java.io.InputStream; */ public class ProfileUpdate implements Closeable { - private Uri[] imageUrls = new Uri[2]; - private InputStream[] imageStreams = new InputStream[2]; + private MediaStatus profileImage, bannerImage; private String name = ""; private String url = ""; @@ -36,14 +28,11 @@ public class ProfileUpdate implements Closeable { */ @Override public void close() { - try { - for (InputStream imageStream : imageStreams) { - if (imageStream != null) { - imageStream.close(); - } - } - } catch (IOException e) { - // ignore + if (profileImage != null) { + profileImage.close(); + } + if (bannerImage != null) { + bannerImage.close(); } } @@ -63,35 +52,15 @@ public class ProfileUpdate implements Closeable { } /** - * add profile image Uri - * - * @param context context used to resolve Uri - * @param imageUrl Uri of the local image file - * @return true if file is valid, false otherwise */ - public boolean setImage(Context context, @NonNull Uri imageUrl) { - DocumentFile file = DocumentFile.fromSingleUri(context, imageUrl); - if (file != null && file.length() > 0) { - imageUrls[0] = imageUrl; - return true; - } - return false; + public void setProfileImage(MediaStatus profileImage) { + this.profileImage = profileImage; } /** - * add banner image Uri - * - * @param context context used to resolve Uri - * @param bannerUrl Uri of the local image file - * @return true if file is valid, false otherwise */ - public boolean setBanner(Context context, @NonNull Uri bannerUrl) { - DocumentFile file = DocumentFile.fromSingleUri(context, bannerUrl); - if (file != null && file.length() > 0) { - imageUrls[1] = bannerUrl; - return true; - } - return false; + public void setBannerImage(MediaStatus bannerImage) { + this.bannerImage = bannerImage; } /** @@ -126,23 +95,23 @@ public class ProfileUpdate implements Closeable { * @return true if any image is added */ public boolean imageAdded() { - return imageUrls[0] != null || imageUrls[1] != null; + return profileImage != null || bannerImage != null; } /** - * @return filestream of the profile image + * @return profile image media instance or null if not added */ @Nullable - public InputStream getProfileImageStream() { - return imageStreams[0]; + public MediaStatus getProfileImageMedia() { + return profileImage; } /** - * @return filestream of the banner image + * @return banner image media instance or null if not added */ @Nullable - public InputStream getBannerImageStream() { - return imageStreams[1]; + public MediaStatus getBannerImageMedia() { + return bannerImage; } /** @@ -151,26 +120,8 @@ public class ProfileUpdate implements Closeable { * * @return true if initialization finished without any error */ - @SuppressLint("Recycle") public boolean prepare(ContentResolver resolver) { - try { - for (int i = 0; i < imageUrls.length; i++) { - if (imageUrls[i] != null) { - InputStream profileImgStream = resolver.openInputStream(imageUrls[i]); - if (profileImgStream != null && profileImgStream.available() > 0) { - this.imageStreams[i] = profileImgStream; - } else { - return false; - } - } - } - } catch (IOException exception) { - if (BuildConfig.DEBUG) { - exception.printStackTrace(); - } - return false; - } - return true; + return (profileImage == null || profileImage.openStream(resolver)) && (bannerImage == null || bannerImage.openStream(resolver)); } diff --git a/app/src/main/java/org/nuclearfog/twidda/ui/activities/ImageViewer.java b/app/src/main/java/org/nuclearfog/twidda/ui/activities/ImageViewer.java index d5712d62..a96bfded 100644 --- a/app/src/main/java/org/nuclearfog/twidda/ui/activities/ImageViewer.java +++ b/app/src/main/java/org/nuclearfog/twidda/ui/activities/ImageViewer.java @@ -44,21 +44,32 @@ import java.io.Serializable; */ public class ImageViewer extends MediaActivity implements AsyncCallback, DescriptionCallback { - public static final int IMAGE_LOCAL = 900; - - public static final int GIF_LOCAL = 901; - + /** + * mode used to show local image/gif file + * requires {@link #KEY_MEDIA_LOCAL} to be set + */ public static final int MEDIA_LOCAL = 902; + /** + * mode used to show online image + * requires {@link #KEY_MEDIA_URL} to be set + */ public static final int IMAGE_ONLINE = 903; + /** + * mode used to show online image + * requires {@link #KEY_MEDIA_ONLINE} to be set + */ public static final int MEDIA_ONLINE = 904; + /** + * activity result code indicates that {@link MediaStatus} data has been updated + */ public static final int RETURN_MEDIA_STATUS_UPDATE = 0x5895; /** * key to set image format (image or gif) - * value type is Integer {@link #IMAGE_LOCAL,#IMAGE_ONLINE,#GIF_LOCAL,#MEDIA_LOCAL} + * value type is Integer {@link #IMAGE_ONLINE,#GIF_LOCAL,#MEDIA_LOCAL} */ public static final String TYPE = "image-type"; @@ -130,31 +141,17 @@ public class ImageViewer extends MediaActivity implements AsyncCallback