Skip to content

Commit

Permalink
feat: allow sharing attachments from PhotoViewer
Browse files Browse the repository at this point in the history
Addresses #290
  • Loading branch information
LucasGGamerM committed Dec 27, 2023
1 parent e0ff1f6 commit 30726cb
Showing 1 changed file with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@
import org.joinmastodon.android.model.Attachment;
import org.joinmastodon.android.ui.ImageDescriptionSheet;
import org.joinmastodon.android.ui.M3AlertDialogBuilder;
import org.joinmastodon.android.utils.FileProvider;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -72,6 +74,9 @@
import me.grishka.appkit.utils.CubicBezierInterpolator;
import me.grishka.appkit.utils.V;
import me.grishka.appkit.views.FragmentRootLinearLayout;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okio.BufferedSink;
import okio.Okio;
import okio.Sink;
Expand Down Expand Up @@ -187,6 +192,14 @@ public void onPageSelected(int position){
return true;
});
imageDescriptionButton.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
toolbar.getMenu()
.add(R.string.button_share)
.setIcon(R.drawable.ic_fluent_share_24_regular)
.setOnMenuItemClickListener(item -> {
shareCurrentFile();
return true;
})
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
toolbar.getMenu()
.add(R.string.download)
.setIcon(R.drawable.ic_fluent_arrow_download_24_regular)
Expand Down Expand Up @@ -441,6 +454,44 @@ public void onPause(){
pauseVideo();
}

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){
saveViaDownloadManager(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, "doSaveShareCurrentFile: ", x);
Toast.makeText(activity, R.string.error, Toast.LENGTH_SHORT).show();
}
}else{
shareAfterDownloading(att);
}


}

private void saveCurrentFile(){
if(Build.VERSION.SDK_INT>=29){
doSaveCurrentFile();
Expand Down Expand Up @@ -545,6 +596,47 @@ private void saveViaDownloadManager(Attachment att){
Toast.makeText(activity, R.string.downloading, Toast.LENGTH_SHORT).show();
}

private void shareAfterDownloading(Attachment att){
Uri uri=Uri.parse(att.url);

MastodonAPIController.runInBackground(()->{
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(att.url).build();

Response response = client.newCall(request).execute();
Toast.makeText(activity, R.string.downloading, Toast.LENGTH_SHORT);
if (!response.isSuccessful()) {
throw new IOException("" + response);
}

File imageDir = new File(activity.getCacheDir(), ".");
InputStream inputStream = response.body().byteStream();
File file = new File(imageDir, uri.getLastPathSegment());
FileOutputStream outputStream = new FileOutputStream(file);

byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}

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)));
} catch(IOException e){
Toast.makeText(activity, R.string.error, Toast.LENGTH_SHORT).show();
}
});
}

private void onAudioFocusChanged(int change){
if(change==AudioManager.AUDIOFOCUS_LOSS || change==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || change==AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){
pauseVideo();
Expand Down

0 comments on commit 30726cb

Please sign in to comment.