refactor(PhotoViewer): deduplicate file sharing code

This commit is contained in:
FineFindus 2024-05-22 19:51:10 +02:00
parent c64d6db859
commit f4a94bc42e
No known key found for this signature in database
GPG Key ID: 64873EE210FF8E6B
1 changed files with 28 additions and 38 deletions

View File

@ -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();