Skip to content

Commit

Permalink
Share sheet previews (AND-139)
Browse files Browse the repository at this point in the history
  • Loading branch information
grishka authored and LucasGGamerM committed May 23, 2024
1 parent ef23734 commit 0c8f903
Show file tree
Hide file tree
Showing 9 changed files with 941 additions and 14 deletions.
8 changes: 8 additions & 0 deletions mastodon/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@
android:resource="@xml/file_paths" />
</provider>

<provider
android:authorities="${applicationId}.fileprovider"
android:name=".TweakedFileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/fileprovider_paths"/>
</provider>

</application>

</manifest>
841 changes: 841 additions & 0 deletions mastodon/src/main/java/org/joinmastodon/android/FileProvider.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.joinmastodon.android;

import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.FileNotFoundException;
import java.util.Arrays;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class TweakedFileProvider extends FileProvider{
private static final String TAG="TweakedFileProvider";

@Override
public String getType(@NonNull Uri uri){
Log.d(TAG, "getType() called with: uri = ["+uri+"]");
if(uri.getPathSegments().get(0).equals("image_cache")){
Log.i(TAG, "getType: HERE!");
return "image/jpeg"; // might as well be a png but image decoding APIs don't care, needs to be image/* though
}
return super.getType(uri);
}

@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder){
Log.d(TAG, "query() called with: uri = ["+uri+"], projection = ["+Arrays.toString(projection)+"], selection = ["+selection+"], selectionArgs = ["+Arrays.toString(selectionArgs)+"], sortOrder = ["+sortOrder+"]");
return super.query(uri, projection, selection, selectionArgs, sortOrder);
}

@Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException{
Log.d(TAG, "openFile() called with: uri = ["+uri+"], mode = ["+mode+"]");
return super.openFile(uri, mode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -865,10 +865,7 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
public boolean onOptionsItemSelected(MenuItem item){
int id=item.getItemId();
if(id==R.id.share){
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, account.url);
startActivity(Intent.createChooser(intent, item.getTitle()));
UiUtils.openSystemShareSheet(getActivity(), account);
}else if(id==R.id.mute){
UiUtils.confirmToggleMuteUser(getActivity(), accountID, account, relationship.muting, this::updateRelationship);
}else if(id==R.id.block){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,7 @@ private boolean onBookmarkLongClick(View v) {
private void onShareClick(View v){
if(item.status.preview) return;
UiUtils.opacityIn(v);
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, item.status.url);
v.getContext().startActivity(Intent.createChooser(intent, v.getContext().getString(R.string.share_toot_title)));
UiUtils.openSystemShareSheet(v.getContext(), item.status);
}

private boolean onShareLongClick(View v){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public void onError(ErrorResponse error){
args.putString("profileDisplayUsername", account.getDisplayUsername());
Nav.go(item.parentFragment.getActivity(), ListsFragment.class, args);
}else if(id==R.id.share){
UiUtils.openSystemShareSheet(activity, item.status.url);
UiUtils.openSystemShareSheet(activity, item.status);
}
return true;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
Expand All @@ -37,6 +38,8 @@
import android.os.ext.SdkExtensions;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.system.ErrnoException;
import android.system.Os;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
Expand All @@ -48,6 +51,7 @@
import android.transition.TransitionManager;
import android.transition.TransitionSet;
import android.util.Log;
import android.util.Log;
import android.util.Pair;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
Expand All @@ -72,6 +76,7 @@
import android.widget.Toast;

import org.joinmastodon.android.E;
import org.joinmastodon.android.FileProvider;
import org.joinmastodon.android.GlobalUserPreferences;
import org.joinmastodon.android.MainActivity;
import org.joinmastodon.android.MastodonApp;
Expand Down Expand Up @@ -134,6 +139,7 @@

import java.io.File;
import java.lang.reflect.Field;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.IDN;
import java.net.URI;
Expand Down Expand Up @@ -181,6 +187,7 @@
import me.grishka.appkit.Nav;
import me.grishka.appkit.api.Callback;
import me.grishka.appkit.api.ErrorResponse;
import me.grishka.appkit.imageloader.ImageCache;
import me.grishka.appkit.imageloader.ViewImageLoader;
import me.grishka.appkit.imageloader.requests.UrlImageLoaderRequest;
import me.grishka.appkit.utils.CubicBezierInterpolator;
Expand Down Expand Up @@ -1754,10 +1761,48 @@ public static String formatDuration(Context context, int seconds){
}
}

public static void openSystemShareSheet(Context context, String url){
public static Uri getFileProviderUri(Context context, File file){
return FileProvider.getUriForFile(context, context.getPackageName()+".fileprovider", file);
}

public static void openSystemShareSheet(Context context, Object obj){
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
Account account;
String url;
String previewTitle;

if(obj instanceof Account acc){
account=acc;
url=acc.url;
previewTitle=context.getString(R.string.share_sheet_preview_profile, account.displayName);
}else if(obj instanceof Status st){
account=st.account;
url=st.url;
String postText=st.getStrippedText();
if(TextUtils.isEmpty(postText)){
previewTitle=context.getString(R.string.share_sheet_preview_profile, account.displayName);
}else{
if(postText.length()>100)
postText=postText.substring(0, 100)+"...";
previewTitle=context.getString(R.string.share_sheet_preview_post, account.displayName, postText);
}
}else{
throw new IllegalArgumentException("Unsupported share object type");
}

intent.putExtra(Intent.EXTRA_TEXT, url);
intent.putExtra(Intent.EXTRA_TITLE, previewTitle);
ImageCache cache=ImageCache.getInstance(context);
try{
File ava=cache.getFile(new UrlImageLoaderRequest(account.avatarStatic));
if(!ava.exists())
ava=cache.getFile(new UrlImageLoaderRequest(account.avatar));
if(ava.exists()){
intent.setClipData(ClipData.newRawUri(null, getFileProviderUri(context, ava)));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}catch(IOException ignore){}
context.startActivity(Intent.createChooser(intent, context.getString(R.string.share_toot_title)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,7 @@ private boolean onContextMenuItemSelected(MenuItem item){

int id=item.getItemId();
if(id==R.id.share){
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, account.url);
fragment.startActivity(Intent.createChooser(intent, item.getTitle()));
UiUtils.openSystemShareSheet(fragment.getActivity(), account);
}else if(id==R.id.mute){
UiUtils.confirmToggleMuteUser(fragment.getActivity(), accountID, account, relationship.muting, this::updateRelationship);
}else if(id==R.id.block){
Expand Down
4 changes: 4 additions & 0 deletions mastodon/src/main/res/xml/fileprovider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="image_cache" path="images/"/>
</paths>

0 comments on commit 0c8f903

Please sign in to comment.