From a69e4a090d67366206398a4fc415ce4873747949 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 23 Aug 2021 09:19:05 +0200 Subject: [PATCH] Added share files into existing draft --- .../eu/faircode/email/ActivityCompose.java | 30 ++++++++-- .../eu/faircode/email/FragmentCompose.java | 56 +++++++++++++++++++ 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityCompose.java b/app/src/main/java/eu/faircode/email/ActivityCompose.java index cca882dc6f..caa3acc42c 100644 --- a/app/src/main/java/eu/faircode/email/ActivityCompose.java +++ b/app/src/main/java/eu/faircode/email/ActivityCompose.java @@ -30,6 +30,7 @@ import android.text.TextUtils; import androidx.core.app.TaskStackBuilder; import androidx.core.net.MailTo; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.LifecycleOwner; @@ -54,14 +55,14 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB getSupportFragmentManager().addOnBackStackChangedListener(this); if (getSupportFragmentManager().getBackStackEntryCount() == 0) - handle(getIntent()); + handle(getIntent(), true); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); - handle(intent); + handle(intent, false); } @Override @@ -84,13 +85,13 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB } } - private void handle(Intent intent) { + private void handle(Intent intent, boolean create) { Bundle args; String action = intent.getAction(); + Log.i("Handle action=" + action + " create=" + create + " " + this); + if (isShared(action)) { args = new Bundle(); - args.putString("action", "new"); - args.putLong("account", -1); Uri uri = intent.getData(); if (uri != null && "mailto".equalsIgnoreCase(uri.getScheme())) { @@ -204,10 +205,27 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB } else args = intent.getExtras(); + FragmentManager fm = getSupportFragmentManager(); + + if (!create && + args.size() == 1 && args.containsKey("attachments")) { + List fragments = fm.getFragments(); + if (fragments.size() == 1) { + ((FragmentCompose) fragments.get(0)).onSharedAttachments( + args.getParcelableArrayList("attachments")); + return; + } + } + + if (isShared(action)) { + args.putString("action", "new"); + args.putLong("account", -1); + } + FragmentCompose fragment = new FragmentCompose(); fragment.setArguments(args); - FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); + FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("compose"); fragmentTransaction.commit(); } diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index de0daba8af..fcaeaa8a80 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -2852,6 +2852,62 @@ public class FragmentCompose extends FragmentBase { }.setExecutor(executor).execute(this, args, "compose:attachment:add"); } + void onSharedAttachments(ArrayList uris) { + Bundle args = new Bundle(); + args.putLong("id", working); + args.putParcelableArrayList("uris", uris); + + new SimpleTask>() { + @Override + protected ArrayList onExecute(Context context, Bundle args) throws Throwable { + long id = args.getLong("id"); + List uris = args.getParcelableArrayList("uris"); + + ArrayList images = new ArrayList<>(); + for (Uri uri : uris) + try { + UriInfo info = getInfo(uri, context); + if (info.isImage()) + images.add(uri); + else + addAttachment(context, id, uri, false, 0, false); + } catch (IOException ex) { + Log.e(ex); + } + + return images; + } + + @Override + protected void onExecuted(Bundle args, ArrayList images) { + if (images.size() == 0) + return; + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + boolean image_dialog = prefs.getBoolean("image_dialog", true); + + if (image_dialog) { + Helper.hideKeyboard(view); + + Bundle aargs = new Bundle(); + aargs.putInt("title", android.R.string.ok); + aargs.putParcelableArrayList("images", images); + + FragmentDialogAddImage fragment = new FragmentDialogAddImage(); + fragment.setArguments(aargs); + fragment.setTargetFragment(FragmentCompose.this, REQUEST_SHARED); + fragment.show(getParentFragmentManager(), "compose:shared"); + } else + onAddImageFile(images); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(getParentFragmentManager(), ex); + } + }.execute(this, args, "compose:shared"); + } + private List getUris(Intent data) { List result = new ArrayList<>();