From 6304bd3063e454a15438b95cf9c708c93b66fed0 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 16 Feb 2021 10:41:06 +0100 Subject: [PATCH] Delay binding conversation actions and images --- .../eu/faircode/email/AdapterMessage.java | 214 +++++++++--------- .../java/eu/faircode/email/WebViewEx.java | 17 ++ 2 files changed, 129 insertions(+), 102 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 0c1d92b354..85ab11d3d3 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -1509,7 +1509,7 @@ public class AdapterMessage extends RecyclerView.Adapter lastInlineImages && (show_images || inline))) bindBody(message, false); - bindAttachments(message, attachments); + bindAttachments(message, attachments, true); } }); @@ -2339,7 +2339,24 @@ public class AdapterMessage extends RecyclerView.Adapter actions = cactions.getConversationActions(); - for (final ConversationAction action : actions) { - final CharSequence text; - final CharSequence title; - final String type = action.getType(); - final RemoteAction raction = action.getAction(); - - switch (type) { - case ConversationAction.TYPE_TEXT_REPLY: - text = action.getTextReply(); - title = context.getString(R.string.title_conversation_action_reply, text); - break; - case "copy": - Bundle extras = action.getExtras().getParcelable("entities-extras"); - if (extras == null) - continue; - text = extras.getString("text"); - title = context.getString(R.string.title_conversation_action_copy, text); - break; - default: - if (raction == null) { - Log.w("Unknown action type=" + type); - continue; - } - text = null; - title = raction.getTitle(); - if (TextUtils.isEmpty(title)) { - Log.e("Empty action type=" + type); - continue; - } - } - - Button button = new Button(context, null, android.R.attr.buttonStyleSmall); - button.setId(View.generateViewId()); - button.setText(title); - button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - try { - switch (type) { - case ConversationAction.TYPE_TEXT_REPLY: - onReply(); - break; - case "copy": - onCopy(); - break; - default: - raction.getActionIntent().send(); - } - } catch (Throwable ex) { - Log.e(ex); - } - } - - private void onReply() { - Intent reply = new Intent(context, ActivityCompose.class) - .putExtra("action", "reply") - .putExtra("reference", message.id) - .putExtra("text", action.getTextReply()); - context.startActivity(reply); - } - - private void onCopy() { - ClipboardManager clipboard = - (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); - if (clipboard != null) { - ClipData clip = ClipData.newPlainText(title, text); - clipboard.setPrimaryClip(clip); - ToastEx.makeText(context, R.string.title_clipboard_copied, Toast.LENGTH_LONG).show(); - } - } - }); - - ((ConstraintLayout) flow.getParent()).addView(button); - flow.addView(button); - has = true; - } - grpAction.setVisibility(has ? View.VISIBLE : View.GONE); - } } if (scroll) properties.scrollTo(getAdapterPosition(), 0); - // Show attachments - cowner.start(); - boolean auto_decrypt = prefs.getBoolean("auto_decrypt", false); if (auto_decrypt && (EntityMessage.PGP_SIGNENCRYPT.equals(message.encrypt) || @@ -2493,7 +2417,93 @@ public class AdapterMessage extends RecyclerView.Adapter attachments) { + private void bindConversationActions(TupleMessageEx message, ConversationActions cactions) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + boolean has = false; + if (cactions != null) { + List actions = cactions.getConversationActions(); + for (final ConversationAction action : actions) { + final CharSequence text; + final CharSequence title; + final String type = action.getType(); + final RemoteAction raction = action.getAction(); + + switch (type) { + case ConversationAction.TYPE_TEXT_REPLY: + text = action.getTextReply(); + title = context.getString(R.string.title_conversation_action_reply, text); + break; + case "copy": + Bundle extras = action.getExtras().getParcelable("entities-extras"); + if (extras == null) + continue; + text = extras.getString("text"); + title = context.getString(R.string.title_conversation_action_copy, text); + break; + default: + if (raction == null) { + Log.w("Unknown action type=" + type); + continue; + } + text = null; + title = raction.getTitle(); + if (TextUtils.isEmpty(title)) { + Log.e("Empty action type=" + type); + continue; + } + } + + Button button = new Button(context, null, android.R.attr.buttonStyleSmall); + button.setId(View.generateViewId()); + button.setText(title); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + try { + switch (type) { + case ConversationAction.TYPE_TEXT_REPLY: + onReply(); + break; + case "copy": + onCopy(); + break; + default: + raction.getActionIntent().send(); + } + } catch (Throwable ex) { + Log.e(ex); + } + } + + private void onReply() { + Intent reply = new Intent(context, ActivityCompose.class) + .putExtra("action", "reply") + .putExtra("reference", message.id) + .putExtra("text", action.getTextReply()); + context.startActivity(reply); + } + + private void onCopy() { + ClipboardManager clipboard = + (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); + if (clipboard != null) { + ClipData clip = ClipData.newPlainText(title, text); + clipboard.setPrimaryClip(clip); + ToastEx.makeText(context, R.string.title_clipboard_copied, Toast.LENGTH_LONG).show(); + } + } + }); + + ((ConstraintLayout) flow.getParent()).addView(button); + flow.addView(button); + has = true; + } + grpAction.setVisibility(has ? View.VISIBLE : View.GONE); + } + } + } + + private void bindAttachments(final TupleMessageEx message, @Nullable List attachments, boolean bind_images) { if (attachments == null) attachments = new ArrayList<>(); properties.setAttachments(message.id, attachments); @@ -2554,14 +2564,14 @@ public class AdapterMessage extends RecyclerView.Adapter>() { @Override public void onChanged(@Nullable List attachments) { - bindAttachments(message, attachments); + bindAttachments(message, attachments, true); } }); } }); List images = new ArrayList<>(); - if (thumbnails) + if (thumbnails && bind_images) for (EntityAttachment attachment : attachments) if (attachment.isAttachment() && attachment.isImage()) images.add(attachment); diff --git a/app/src/main/java/eu/faircode/email/WebViewEx.java b/app/src/main/java/eu/faircode/email/WebViewEx.java index 6fe8e011f8..a13f88f5d3 100644 --- a/app/src/main/java/eu/faircode/email/WebViewEx.java +++ b/app/src/main/java/eu/faircode/email/WebViewEx.java @@ -42,6 +42,7 @@ import static androidx.webkit.WebSettingsCompat.FORCE_DARK_ON; public class WebViewEx extends WebView implements DownloadListener, View.OnLongClickListener { private int height; private IWebView intf; + private Runnable onPageFinished; public WebViewEx(Context context) { super(context); @@ -96,6 +97,18 @@ public class WebViewEx extends WebView implements DownloadListener, View.OnLongC this.intf = intf; setWebViewClient(new WebViewClient() { + @Override + public void onPageFinished(WebView view, String url) { + Log.i("Finished url=" + url); + } + + @Override + public void onPageCommitVisible(WebView view, String url) { + Log.i("Commit url=" + url); + if (onPageFinished != null) + onPageFinished.run(); + } + public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.i("Open url=" + url); return intf.onOpenLink(url); @@ -118,6 +131,10 @@ public class WebViewEx extends WebView implements DownloadListener, View.OnLongC }); } + void setOnPageFinished(Runnable runnable) { + onPageFinished = runnable; + } + void setImages(boolean show_images, boolean inline) { WebSettings settings = getSettings(); settings.setLoadsImagesAutomatically(show_images || inline);