Added deleting attachments

pull/213/head
M66B 1 year ago
parent ba9c9f2165
commit b458d5f4e3

@ -137,7 +137,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
lparam.setMarginStart(attachment.subsequence == null ? 0 : dp12);
view.setLayoutParams(lparam);
ibDelete.setVisibility(readonly && !BuildConfig.DEBUG ? View.GONE : View.VISIBLE);
ibDelete.setVisibility(readonly ? View.GONE : View.VISIBLE);
if (!readonly && attachment.isImage()) {
if (attachment.available) {
@ -344,13 +344,11 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
private void onDelete(final EntityAttachment attachment) {
Bundle args = new Bundle();
args.putLong("id", attachment.id);
args.putBoolean("readonly", readonly);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
boolean readonly = args.getBoolean("readonly");
EntityAttachment attachment;
@ -362,12 +360,6 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
if (attachment == null)
return null;
if (readonly) {
EntityMessage message = db.message().getMessage(attachment.message);
if (message == null)
return null;
EntityOperation.queue(context, message, EntityOperation.DETACH, attachment.id, true);
} else
db.attachment().deleteAttachment(attachment.id);
db.setTransactionSuccessful();
@ -375,7 +367,6 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
db.endTransaction();
}
if (!readonly)
attachment.getFile(context).delete();
return null;

@ -5780,6 +5780,15 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
properties.move(message.id, EntityFolder.TRASH);
}
private void onActionDeleteAttachments(TupleMessageEx message) {
Bundle args = new Bundle();
args.putLong("id", message.id);
FragmentDialogDeleteAttachments fragment = new FragmentDialogDeleteAttachments();
fragment.setArguments(args);
fragment.show(parentFragment.getParentFragmentManager(), "attachments:delete");
}
private void onActionDelete(TupleMessageEx message) {
boolean leaveDeleted =
(message.accountProtocol == EntityAccount.TYPE_POP &&
@ -5888,6 +5897,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
.setEnabled(message.uid != null && !message.folderReadOnly)
.setVisible(message.accountProtocol == EntityAccount.TYPE_IMAP);
popupMenu.getMenu().findItem(R.id.menu_delete_attachments)
.setEnabled(message.uid != null && !message.folderReadOnly)
.setVisible(message.accountProtocol == EntityAccount.TYPE_IMAP);
popupMenu.getMenu().findItem(R.id.menu_delete)
.setEnabled(message.uid == null || !message.folderReadOnly)
.setVisible(message.accountProtocol == EntityAccount.TYPE_IMAP);
@ -5984,6 +5997,9 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} else if (itemId == R.id.menu_copy_to) {
onActionMove(message, true);
return true;
} else if (itemId == R.id.menu_delete_attachments) {
onActionDeleteAttachments(message);
return true;
} else if (itemId == R.id.menu_delete) {
onActionDelete(message);
return true;

@ -508,7 +508,7 @@ class Core {
break;
case EntityOperation.DETACH:
onDetach(context, jargs, folder, message, op, (IMAPFolder) ifolder);
onDetach(context, jargs, message, (IMAPFolder) ifolder);
break;
case EntityOperation.EXISTS:
@ -2090,14 +2090,19 @@ class Core {
EntityLog.log(context, "Operation attachment size=" + attachment.size);
}
private static void onDetach(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, EntityOperation op, IMAPFolder ifolder) throws JSONException, MessagingException, IOException {
private static void onDetach(Context context, JSONArray jargs, EntityMessage message, IMAPFolder ifolder) throws JSONException, MessagingException, IOException {
DB db = DB.getInstance(context);
long id = jargs.getLong(0);
JSONArray jids = jargs.getJSONArray(0);
List<Long> ids = new ArrayList<>();
for (int i = 0; i < jids.length(); i++)
ids.add(jids.getLong(i));
if (message.uid == null)
throw new IllegalArgumentException("Delete attachments uid missing");
EntityFolder trash = db.folder().getFolderByType(message.account, EntityFolder.TRASH);
// Get message
Message imessage = ifolder.getMessageByUID(message.uid);
if (imessage == null)
@ -2111,7 +2116,7 @@ class Core {
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
for (EntityAttachment attachment : attachments)
if (attachment.id.equals(id) && BuildConfig.DEBUG) {
if (ids.contains(attachment.id)) {
Part apart = aparts.get(attachment.sequence - 1).part;
if (!deletePart(icopy, apart))
throw new IllegalArgumentException("Attachment part not found");
@ -2119,16 +2124,19 @@ class Core {
ifolder.appendMessages(new Message[]{icopy});
if (trash == null) {
imessage.setFlag(Flags.Flag.DELETED, true);
expunge(context, ifolder, Arrays.asList(imessage));
} else {
EntityOperation.queue(context, message, EntityOperation.MOVE, trash.id);
}
}
static boolean deletePart(Part part, Part attachment) throws MessagingException, IOException {
boolean deleted = false;
if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int count = multipart.getCount();
for (int i = 0; i < count; i++) {
for (int i = 0; i < multipart.getCount(); i++) {
Part child = multipart.getBodyPart(i);
if (child == attachment) {
multipart.removeBodyPart(i);

@ -31,6 +31,7 @@ import androidx.core.content.FileProvider;
import androidx.preference.PreferenceManager;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;
@ -98,6 +99,9 @@ public class EntityAttachment {
public String media_uri;
public String error;
@Ignore
public boolean selected = true;
// Gmail sends inline images as attachments with a name and cid
boolean isInline() {

@ -937,6 +937,9 @@ public class EntityOperation {
Log.e(ex);
}
if (DETACH.equals(name) && message != null)
db.message().setMessageUiHide(message, false);
if (SYNC.equals(name))
db.folder().setFolderSyncState(folder, null);

@ -0,0 +1,273 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2023 by Marcel Bokhorst (M66B)
*/
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.json.JSONArray;
import java.util.ArrayList;
import java.util.List;
public class FragmentDialogDeleteAttachments extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Context context = getContext();
final View dview = LayoutInflater.from(context).inflate(R.layout.dialog_delete_attachments, null);
final RecyclerView rvAttachment = dview.findViewById(R.id.rvAttachment);
final ProgressBar pbWait = dview.findViewById(R.id.pbWait);
rvAttachment.setHasFixedSize(false);
rvAttachment.setLayoutManager(new LinearLayoutManager(context));
AdapterAttachmentSelect adapter = new AdapterAttachmentSelect(context);
rvAttachment.setAdapter(adapter);
new SimpleTask<List<EntityAttachment>>() {
@Override
protected void onPreExecute(Bundle args) {
pbWait.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bundle args) {
pbWait.setVisibility(View.GONE);
}
@Override
protected List<EntityAttachment> onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
return db.attachment().getAttachments(id);
}
@Override
protected void onExecuted(final Bundle args, List<EntityAttachment> attachments) {
if (attachments == null)
attachments = new ArrayList<>();
adapter.set(attachments);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, getArguments(), "attachments:delete");
return new AlertDialog.Builder(context)
.setIcon(R.drawable.twotone_attachment_24)
.setTitle(R.string.title_delete_attachments)
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Bundle args = new Bundle();
args.putLong("id", getArguments().getLong("id"));
args.putLongArray("ids", Helper.toLongArray(adapter.getSelectedItems()));
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
long[] ids = args.getLongArray("ids");
DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id);
if (message == null)
return null;
JSONArray jids = new JSONArray();
for (int i = 0; i < ids.length; i++)
jids.put(i, ids[i]);
EntityOperation.queue(context, message, EntityOperation.DETACH, jids);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(FragmentDialogDeleteAttachments.this, args, "delete:attachments");
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
public static class AdapterAttachmentSelect extends RecyclerView.Adapter<AdapterAttachmentSelect.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private List<EntityAttachment> items = new ArrayList<>();
public class ViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener {
private CheckBox cbEnabled;
private TextView tvInfo;
ViewHolder(View itemView) {
super(itemView);
cbEnabled = itemView.findViewById(R.id.cbEnabled);
tvInfo = itemView.findViewById(R.id.tvInfo);
}
private void wire() {
cbEnabled.setOnCheckedChangeListener(this);
}
private void unwire() {
cbEnabled.setOnCheckedChangeListener(null);
}
private void bindTo(EntityAttachment attachment) {
cbEnabled.setText(attachment.name);
cbEnabled.setChecked(attachment.selected);
StringBuilder sb = new StringBuilder();
if (!TextUtils.isEmpty(attachment.type))
sb.append(attachment.type);
if (attachment.size != null) {
if (sb.length() > 0)
sb.append(' ');
sb.append(Helper.humanReadableByteCount(attachment.size));
}
tvInfo.setText(sb.toString());
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
items.get(pos).selected = isChecked;
}
}
AdapterAttachmentSelect(Context context) {
this.context = context;
this.inflater = LayoutInflater.from(context);
setHasStableIds(true);
}
public void set(List<EntityAttachment> attachments) {
Log.i("Set attachments=" + attachments.size());
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, attachments), false);
items = attachments;
try {
diff.dispatchUpdatesTo(this);
} catch (Throwable ex) {
Log.e(ex);
}
}
List<Long> getSelectedItems() {
List<Long> result = new ArrayList<>();
for (EntityAttachment attachment : items)
if (attachment.selected)
result.add(attachment.id);
return result;
}
private class DiffCallback extends DiffUtil.Callback {
private List<EntityAttachment> prev = new ArrayList<>();
private List<EntityAttachment> next = new ArrayList<>();
DiffCallback(List<EntityAttachment> prev, List<EntityAttachment> next) {
this.prev.addAll(prev);
this.next.addAll(next);
}
@Override
public int getOldListSize() {
return prev.size();
}
@Override
public int getNewListSize() {
return next.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
EntityAttachment a1 = prev.get(oldItemPosition);
EntityAttachment a2 = next.get(newItemPosition);
return a1.id.equals(a2.id);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
EntityAttachment a1 = prev.get(oldItemPosition);
EntityAttachment a2 = next.get(newItemPosition);
return a1.equals(a2);
}
}
@Override
public long getItemId(int position) {
return items.get(position).id;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
@NonNull
public AdapterAttachmentSelect.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new AdapterAttachmentSelect.ViewHolder(inflater.inflate(R.layout.item_attachment_delete, parent, false));
}
@Override
public void onBindViewHolder(@NonNull AdapterAttachmentSelect.ViewHolder holder, int position) {
holder.unwire();
EntityAttachment attachment = items.get(position);
holder.bindTo(attachment);
holder.wire();
}
}
}

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="12dp">
<eu.faircode.email.FixedRecyclerView
android:id="@+id/rvAttachment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:clipToPadding="false"
android:minHeight="48dp"
android:paddingEnd="6dp"
android:paddingBottom="48dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="none"
app:fastScrollEnabled="false"
app:fastScrollHorizontalThumbDrawable="@drawable/scroll_thumb"
app:fastScrollHorizontalTrackDrawable="@drawable/scroll_track"
app:fastScrollVerticalThumbDrawable="@drawable/scroll_thumb"
app:fastScrollVerticalTrackDrawable="@drawable/scroll_track"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<eu.faircode.email.ContentLoadingProgressBar
android:id="@+id/pbWait"
style="@style/Base.Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:padding="24dp"
app:layout_constraintBottom_toBottomOf="@id/rvAttachment"
app:layout_constraintEnd_toEndOf="@id/rvAttachment"
app:layout_constraintStart_toStartOf="@id/rvAttachment"
app:layout_constraintTop_toTopOf="@id/rvAttachment" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clItem"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/cbEnabled"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Attachment"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvInfo"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="end"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbEnabled" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

@ -54,6 +54,11 @@
android:icon="@drawable/twotone_file_copy_24"
android:title="@string/title_copy_to" />
<item
android:id="@+id/menu_delete_attachments"
android:icon="@drawable/twotone_attachment_24"
android:title="@string/title_delete_attachments" />
<item
android:id="@+id/menu_delete"
android:icon="@drawable/twotone_delete_forever_24"

@ -1440,6 +1440,7 @@
<string name="title_move_to_account">Move to %1$s &#8230;</string>
<string name="title_report_spam">Treat as spam</string>
<string name="title_delete_permanently">Delete permanently</string>
<string name="title_delete_attachments">Delete attachments</string>
<string name="title_snooze">Snooze &#8230;</string>
<string name="title_archive">Archive</string>
<string name="title_reply">Reply</string>

Loading…
Cancel
Save