From f4a94bc42e7e9ad4420d58bcf060e1a8d8b3c7e6 Mon Sep 17 00:00:00 2001 From: FineFindus Date: Wed, 22 May 2024 19:51:10 +0200 Subject: [PATCH 1/2] refactor(PhotoViewer): deduplicate file sharing code --- .../android/ui/photoviewer/PhotoViewer.java | 66 ++++++++----------- 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java b/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java index 7a83da275..2e7ada04f 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java @@ -482,40 +482,29 @@ public class PhotoViewer implements ZoomPanView.Listener{ private void shareCurrentFile(){ Attachment att=attachments.get(pager.getCurrentItem()); - Intent intent = new Intent(Intent.ACTION_SEND); - if(att.type==Attachment.Type.IMAGE){ - UrlImageLoaderRequest req=new UrlImageLoaderRequest(att.url); - try{ - File file=ImageCache.getInstance(activity).getFile(req); - if(file==null){ - shareAfterDownloading(att); - return; - } - MastodonAPIController.runInBackground(()->{ - File imageDir = new File(activity.getCacheDir(), "."); - File renamedFile; - file.renameTo(renamedFile = new File(imageDir, Uri.parse(att.url).getLastPathSegment())); - Uri outputUri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileprovider", renamedFile); - - // setting type to image - intent.setType(mimeTypeForFileName(outputUri.getLastPathSegment())); - - intent.putExtra(Intent.EXTRA_STREAM, outputUri); - - // calling startactivity() to share - activity.startActivity(Intent.createChooser(intent, activity.getString(R.string.button_share))); - - }); - }catch(IOException x){ - Log.w(TAG, "shareCurrentFile: ", x); - Toast.makeText(activity, R.string.error, Toast.LENGTH_SHORT).show(); - } - }else{ + if(att.type!=Attachment.Type.IMAGE){ shareAfterDownloading(att); + return; } - + UrlImageLoaderRequest req=new UrlImageLoaderRequest(att.url); + try{ + File file=ImageCache.getInstance(activity).getFile(req); + if(file==null){ + shareAfterDownloading(att); + return; + } + MastodonAPIController.runInBackground(()->{ + File imageDir=new File(activity.getCacheDir(), "."); + File renamedFile; + file.renameTo(renamedFile=new File(imageDir, Uri.parse(att.url).getLastPathSegment())); + shareFile(renamedFile); + }); + }catch(IOException x){ + Log.w(TAG, "shareCurrentFile: ", x); + Toast.makeText(activity, R.string.error, Toast.LENGTH_SHORT).show(); + } } private void saveCurrentFile(){ @@ -649,20 +638,21 @@ public class PhotoViewer implements ZoomPanView.Listener{ outputStream.close(); inputStream.close(); - - Intent intent = new Intent(Intent.ACTION_SEND); - - Uri outputUri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileprovider", file); - - intent.setType(mimeTypeForFileName(outputUri.getLastPathSegment())); - intent.putExtra(Intent.EXTRA_STREAM, outputUri); - activity.startActivity(Intent.createChooser(intent, activity.getString(R.string.button_share))); + shareFile(file); } catch(IOException e){ Toast.makeText(activity, R.string.error, Toast.LENGTH_SHORT).show(); } }); } + private void shareFile(@NonNull File file) { + Intent intent = new Intent(Intent.ACTION_SEND); + Uri outputUri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileprovider", file); + intent.setType(mimeTypeForFileName(outputUri.getLastPathSegment())); + intent.putExtra(Intent.EXTRA_STREAM, outputUri); + activity.startActivity(Intent.createChooser(intent, activity.getString(R.string.button_share))); + } + private void onAudioFocusChanged(int change){ if(change==AudioManager.AUDIOFOCUS_LOSS || change==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || change==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){ pauseVideo(); From c0ab3a47aefbe7b6be23292bd8d1cf6e5f2eb528 Mon Sep 17 00:00:00 2001 From: FineFindus Date: Wed, 22 May 2024 19:53:57 +0200 Subject: [PATCH 2/2] feat(PhotoViewer): rich previews for image sharing --- .../org/joinmastodon/android/ui/photoviewer/PhotoViewer.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java b/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java index 2e7ada04f..abd203967 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java @@ -648,7 +648,8 @@ public class PhotoViewer implements ZoomPanView.Listener{ private void shareFile(@NonNull File file) { Intent intent = new Intent(Intent.ACTION_SEND); Uri outputUri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileprovider", file); - intent.setType(mimeTypeForFileName(outputUri.getLastPathSegment())); + intent.setDataAndType(outputUri, mimeTypeForFileName(outputUri.getLastPathSegment())); + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.putExtra(Intent.EXTRA_STREAM, outputUri); activity.startActivity(Intent.createChooser(intent, activity.getString(R.string.button_share))); }