Added sharing of attachments

This commit is contained in:
M66B 2020-07-17 09:33:14 +02:00
parent e4558ae50f
commit 0c9e85caf9
1 changed files with 29 additions and 1 deletions

View File

@ -22,6 +22,7 @@ package eu.faircode.email;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -32,6 +33,7 @@ import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver; import androidx.lifecycle.LifecycleObserver;
@ -43,6 +45,7 @@ import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback; import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -62,7 +65,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
private List<EntityAttachment> items = new ArrayList<>(); private List<EntityAttachment> items = new ArrayList<>();
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private View view; private View view;
private ImageButton ibDelete; private ImageButton ibDelete;
private ImageView ivType; private ImageView ivType;
@ -95,12 +98,14 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
view.setOnClickListener(this); view.setOnClickListener(this);
ibDelete.setOnClickListener(this); ibDelete.setOnClickListener(this);
ibSave.setOnClickListener(this); ibSave.setOnClickListener(this);
view.setOnLongClickListener(this);
} }
private void unwire() { private void unwire() {
view.setOnClickListener(null); view.setOnClickListener(null);
ibDelete.setOnClickListener(null); ibDelete.setOnClickListener(null);
ibSave.setOnClickListener(null); ibSave.setOnClickListener(null);
view.setOnLongClickListener(null);
} }
private void bindTo(EntityAttachment attachment) { private void bindTo(EntityAttachment attachment) {
@ -185,6 +190,29 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
} }
} }
@Override
public boolean onLongClick(View view) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
EntityAttachment attachment = items.get(pos);
if (attachment == null || !attachment.available)
return false;
File file = attachment.getFile(context);
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);
Intent send = new Intent();
send.setAction(Intent.ACTION_SEND);
send.putExtra(Intent.EXTRA_STREAM, uri);
send.setType(attachment.getMimeType());
send.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(send, context.getString(R.string.title_select_app)));
return true;
}
private void onDelete(final EntityAttachment attachment) { private void onDelete(final EntityAttachment attachment) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", attachment.id); args.putLong("id", attachment.id);