From e5210bbaa0f867c21c5689477e97a35e15f57b61 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 7 Nov 2017 15:58:27 +0100 Subject: [PATCH] brought back 'copy to clipboard' and 'quote' context menu options --- .../ui/ConversationFragment.java | 91 ++++++++++--------- .../ui/widget/ListSelectionManager.java | 4 - src/main/res/menu/message_context.xml | 14 ++- src/main/res/values/strings.xml | 4 +- 4 files changed, 60 insertions(+), 53 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 63816d193..d1d6d5d7a 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -555,28 +555,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa @Override public void onQuote(String text) { - if (mEditMessage.isEnabled()) { - text = text.replaceAll("(\n *){2,}", "\n").replaceAll("(^|\n)", "$1> ").replaceAll("\n$", ""); - Editable editable = mEditMessage.getEditableText(); - int position = mEditMessage.getSelectionEnd(); - if (position == -1) position = editable.length(); - if (position > 0 && editable.charAt(position - 1) != '\n') { - editable.insert(position++, "\n"); - } - editable.insert(position, text); - position += text.length(); - editable.insert(position++, "\n"); - if (position < editable.length() && editable.charAt(position) != '\n') { - editable.insert(position, "\n"); - } - mEditMessage.setSelection(position); - mEditMessage.requestFocus(); - InputMethodManager inputMethodManager = (InputMethodManager) getActivity() - .getSystemService(Context.INPUT_METHOD_SERVICE); - if (inputMethodManager != null) { - inputMethodManager.showSoftInput(mEditMessage, InputMethodManager.SHOW_IMPLICIT); - } - } + quoteText(text); } }); messagesView.setAdapter(messageListAdapter); @@ -586,6 +565,35 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa return view; } + private void quoteText(String text) { + if (mEditMessage.isEnabled()) { + text = text.replaceAll("(\n *){2,}", "\n").replaceAll("(^|\n)", "$1> ").replaceAll("\n$", ""); + Editable editable = mEditMessage.getEditableText(); + int position = mEditMessage.getSelectionEnd(); + if (position == -1) position = editable.length(); + if (position > 0 && editable.charAt(position - 1) != '\n') { + editable.insert(position++, "\n"); + } + editable.insert(position, text); + position += text.length(); + editable.insert(position++, "\n"); + if (position < editable.length() && editable.charAt(position) != '\n') { + editable.insert(position, "\n"); + } + mEditMessage.setSelection(position); + mEditMessage.requestFocus(); + InputMethodManager inputMethodManager = (InputMethodManager) getActivity() + .getSystemService(Context.INPUT_METHOD_SERVICE); + if (inputMethodManager != null) { + inputMethodManager.showSoftInput(mEditMessage, InputMethodManager.SHOW_IMPLICIT); + } + } + } + + private void quoteMessage(Message message) { + quoteText(message.getMergedBody().toString()); + } + @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { synchronized (this.messageList) { @@ -607,9 +615,12 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa final boolean treatAsFile = m.getType() != Message.TYPE_TEXT && m.getType() != Message.TYPE_PRIVATE && t == null; + final boolean encrypted = m.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED + || m.getEncryption() == Message.ENCRYPTION_PGP; activity.getMenuInflater().inflate(R.menu.message_context, menu); menu.setHeaderTitle(R.string.message_options); - MenuItem selectText = menu.findItem(R.id.select_text); + MenuItem copyMessage = menu.findItem(R.id.copy_message); + MenuItem quoteMessage = menu.findItem(R.id.quote_message); MenuItem retryDecryption = menu.findItem(R.id.retry_decryption); MenuItem correctMessage = menu.findItem(R.id.correct_message); MenuItem shareWith = menu.findItem(R.id.share_with); @@ -619,8 +630,9 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa MenuItem cancelTransmission = menu.findItem(R.id.cancel_transmission); MenuItem deleteFile = menu.findItem(R.id.delete_file); MenuItem showErrorMessage = menu.findItem(R.id.show_error_message); - if (!treatAsFile && !m.isGeoUri() && !m.treatAsDownloadable()) { - selectText.setVisible(ListSelectionManager.isSupported()); + if (!treatAsFile && !encrypted && !m.isGeoUri() && !m.treatAsDownloadable()) { + copyMessage.setVisible(true); + quoteMessage.setVisible(true); } if (m.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) { retryDecryption.setVisible(true); @@ -671,12 +683,15 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa case R.id.share_with: shareWith(selectedMessage); return true; - case R.id.select_text: - selectText(selectedMessage); - return true; case R.id.correct_message: correctMessage(selectedMessage); return true; + case R.id.copy_message: + copyMessage(selectedMessage); + return true; + case R.id.quote_message: + quoteMessage(selectedMessage); + return true; case R.id.send_again: resendMessage(selectedMessage); return true; @@ -743,23 +758,11 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa } } - private void selectText(Message message) { - final int index; - synchronized (this.messageList) { - index = this.messageList.indexOf(message); + private void copyMessage(Message message) { + if (activity.copyTextToClipboard(message.getMergedBody().toString(), R.string.message)) { + Toast.makeText(activity, R.string.message_copied_to_clipboard, Toast.LENGTH_SHORT).show(); } - if (index >= 0) { - final int first = this.messagesView.getFirstVisiblePosition(); - final int last = first + this.messagesView.getChildCount(); - if (index >= first && index < last) { - final View view = this.messagesView.getChildAt(index - first); - final TextView messageBody = this.messageListAdapter.getMessageBody(view); - if (messageBody != null) { - ListSelectionManager.startSelection(messageBody); - } - } - } - } + } private void deleteFile(Message message) { if (activity.xmppConnectionService.getFileBackend().deleteFile(message)) { diff --git a/src/main/java/eu/siacs/conversations/ui/widget/ListSelectionManager.java b/src/main/java/eu/siacs/conversations/ui/widget/ListSelectionManager.java index 4be90712d..1c32fd75a 100644 --- a/src/main/java/eu/siacs/conversations/ui/widget/ListSelectionManager.java +++ b/src/main/java/eu/siacs/conversations/ui/widget/ListSelectionManager.java @@ -194,10 +194,6 @@ public class ListSelectionManager { return SUPPORTED; } - public static void startSelection(TextView textView) { - startSelection(textView, 0, textView.getText().length()); - } - public static void startSelection(TextView textView, int start, int end) { final CharSequence text = textView.getText(); if (SUPPORTED && start >= 0 && end > start && textView.isTextSelectable() && text instanceof Spannable) { diff --git a/src/main/res/menu/message_context.xml b/src/main/res/menu/message_context.xml index bd2208846..fe65ea099 100644 --- a/src/main/res/menu/message_context.xml +++ b/src/main/res/menu/message_context.xml @@ -1,14 +1,20 @@ - + + + + Check %s size Check %1$s size on %2$s Message options - Select text Quote Copy original URL Send again @@ -724,4 +723,7 @@ Certificate does not contain a Jabber ID partial Record video + Copy to clipboard + Message copied to clipboard + Message