EML share attachments

This commit is contained in:
M66B 2020-01-24 11:00:09 +01:00
parent fed48455b0
commit c31308f347
3 changed files with 64 additions and 7 deletions

View File

@ -51,6 +51,7 @@ import com.sun.mail.imap.IMAPFolder;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
@ -177,7 +178,41 @@ public class ActivityEML extends ActivityBase {
result.parts.getAttachmentParts(), result.parts.getAttachmentParts(),
new AdapterAttachmentEML.IEML() { new AdapterAttachmentEML.IEML() {
@Override @Override
public void onSelected(MessageHelper.AttachmentPart apart) { public void onShare(MessageHelper.AttachmentPart apart) {
new SimpleTask<File>() {
@Override
protected File onExecute(Context context, Bundle args) throws Throwable {
apart.attachment.id = 0L;
File file = apart.attachment.getFile(context);
Log.i("Writing to " + file);
try (InputStream is = apart.part.getInputStream()) {
try (OutputStream os = new FileOutputStream(file)) {
byte[] buffer = new byte[Helper.BUFFER_SIZE];
for (int len = is.read(buffer); len != -1; len = is.read(buffer))
os.write(buffer, 0, len);
}
}
return file;
}
@Override
protected void onExecuted(Bundle args, File file) {
Helper.share(ActivityEML.this, file,
apart.attachment.getMimeType(),
apart.attachment.name);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex);
}
}.execute(ActivityEML.this, new Bundle(), "eml:share");
}
@Override
public void onSave(MessageHelper.AttachmentPart apart) {
ActivityEML.this.apart = apart; ActivityEML.this.apart = apart;
Intent create = new Intent(Intent.ACTION_CREATE_DOCUMENT); Intent create = new Intent(Intent.ACTION_CREATE_DOCUMENT);

View File

@ -23,6 +23,7 @@ import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -41,6 +42,7 @@ public class AdapterAttachmentEML extends RecyclerView.Adapter<AdapterAttachment
private TextView tvName; private TextView tvName;
private TextView tvSize; private TextView tvSize;
private TextView tvType; private TextView tvType;
private ImageButton ibSave;
ViewHolder(View itemView) { ViewHolder(View itemView) {
super(itemView); super(itemView);
@ -49,14 +51,17 @@ public class AdapterAttachmentEML extends RecyclerView.Adapter<AdapterAttachment
tvName = itemView.findViewById(R.id.tvName); tvName = itemView.findViewById(R.id.tvName);
tvSize = itemView.findViewById(R.id.tvSize); tvSize = itemView.findViewById(R.id.tvSize);
tvType = itemView.findViewById(R.id.tvType); tvType = itemView.findViewById(R.id.tvType);
ibSave = itemView.findViewById(R.id.ibSave);
} }
private void wire() { private void wire() {
view.setOnClickListener(this); view.setOnClickListener(this);
ibSave.setOnClickListener(this);
} }
private void unwire() { private void unwire() {
view.setOnClickListener(null); view.setOnClickListener(null);
ibSave.setOnClickListener(null);
} }
private void bindTo(MessageHelper.AttachmentPart apart) { private void bindTo(MessageHelper.AttachmentPart apart) {
@ -81,7 +86,10 @@ public class AdapterAttachmentEML extends RecyclerView.Adapter<AdapterAttachment
MessageHelper.AttachmentPart apart = aparts.get(pos); MessageHelper.AttachmentPart apart = aparts.get(pos);
if (apart != null) if (apart != null)
intf.onSelected(apart); if (view.getId() == R.id.ibSave)
intf.onSave(apart);
else
intf.onShare(apart);
} }
} }
@ -115,6 +123,8 @@ public class AdapterAttachmentEML extends RecyclerView.Adapter<AdapterAttachment
} }
interface IEML { interface IEML {
void onSelected(MessageHelper.AttachmentPart apart); void onShare(MessageHelper.AttachmentPart apart);
void onSave(MessageHelper.AttachmentPart apart);
} }
} }

View File

@ -17,7 +17,7 @@
android:layout_width="24dp" android:layout_width="24dp"
android:layout_height="24dp" android:layout_height="24dp"
android:contentDescription="@string/title_legend_attachment" android:contentDescription="@string/title_legend_attachment"
app:layout_constraintBottom_toBottomOf="@+id/tvType" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/baseline_attachment_24" /> app:srcCompat="@drawable/baseline_attachment_24" />
@ -57,9 +57,21 @@
android:layout_marginStart="6dp" android:layout_marginStart="6dp"
android:text="10 kB" android:text="10 kB"
android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="@id/tvType" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toStartOf="@+id/ibSave"
app:layout_constraintStart_toEndOf="@id/tvType" app:layout_constraintStart_toEndOf="@id/tvType"
app:layout_constraintTop_toTopOf="@id/tvName" /> app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/ibSave"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/title_legend_save"
android:paddingStart="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/baseline_save_24" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout> </FrameLayout>