refactor(PhotoViewer): deduplicate file sharing code
This commit is contained in:
@@ -482,40 +482,29 @@ public class PhotoViewer implements ZoomPanView.Listener{
|
|||||||
|
|
||||||
private void shareCurrentFile(){
|
private void shareCurrentFile(){
|
||||||
Attachment att=attachments.get(pager.getCurrentItem());
|
Attachment att=attachments.get(pager.getCurrentItem());
|
||||||
Intent intent = new Intent(Intent.ACTION_SEND);
|
|
||||||
|
|
||||||
if(att.type==Attachment.Type.IMAGE){
|
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{
|
|
||||||
shareAfterDownloading(att);
|
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(){
|
private void saveCurrentFile(){
|
||||||
@@ -649,20 +638,21 @@ public class PhotoViewer implements ZoomPanView.Listener{
|
|||||||
|
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
|
shareFile(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)));
|
|
||||||
} catch(IOException e){
|
} catch(IOException e){
|
||||||
Toast.makeText(activity, R.string.error, Toast.LENGTH_SHORT).show();
|
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){
|
private void onAudioFocusChanged(int change){
|
||||||
if(change==AudioManager.AUDIOFOCUS_LOSS || change==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || change==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){
|
if(change==AudioManager.AUDIOFOCUS_LOSS || change==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || change==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){
|
||||||
pauseVideo();
|
pauseVideo();
|
||||||
|
|||||||
Reference in New Issue
Block a user