refactor(PhotoViewer): deduplicate file sharing code
This commit is contained in:
parent
c64d6db859
commit
f4a94bc42e
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue