From a3a7f8fb6c033f951391cc975cfa68486694011b Mon Sep 17 00:00:00 2001 From: f41c0r Date: Sat, 25 Jul 2015 19:35:28 -0500 Subject: [PATCH] Add support to copy multiple messages Fixes #3748 Closes #3798 // FREEBIE --- .../securesms/ConversationFragment.java | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ConversationFragment.java b/src/org/thoughtcrime/securesms/ConversationFragment.java index bc987eb068..05c1a26053 100644 --- a/src/org/thoughtcrime/securesms/ConversationFragment.java +++ b/src/org/thoughtcrime/securesms/ConversationFragment.java @@ -15,6 +15,7 @@ import android.support.v4.widget.CursorAdapter; import android.support.v7.app.AppCompatActivity; import android.support.v7.view.ActionMode; import android.text.ClipboardManager; +import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -44,7 +45,10 @@ import org.thoughtcrime.securesms.util.FutureTaskListener; import org.thoughtcrime.securesms.util.ProgressDialogAsyncTask; import org.thoughtcrime.securesms.util.SaveAttachmentTask; import org.thoughtcrime.securesms.util.SaveAttachmentTask.Attachment; +import org.thoughtcrime.securesms.util.ServiceUtil; +import java.util.Collections; +import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.Locale; @@ -144,7 +148,6 @@ public class ConversationFragment extends ListFragment if (messageRecords.size() > 1) { menu.findItem(R.id.menu_context_forward).setVisible(false); - menu.findItem(R.id.menu_context_copy).setVisible(false); menu.findItem(R.id.menu_context_details).setVisible(false); menu.findItem(R.id.menu_context_save_attachment).setVisible(false); menu.findItem(R.id.menu_context_resend).setVisible(false); @@ -192,13 +195,34 @@ public class ConversationFragment extends ListFragment }); } - private void handleCopyMessage(MessageRecord message) { - String body = message.getDisplayBody().toString(); - if (body == null) return; + private void handleCopyMessage(final List messageRecords) { + Collections.sort(messageRecords, new Comparator() { + @Override + public int compare(MessageRecord lhs, MessageRecord rhs) { + if (lhs.getDateReceived() < rhs.getDateReceived()) return -1; + else if (lhs.getDateReceived() == rhs.getDateReceived()) return 0; + else return 1; + } + }); - ClipboardManager clipboard = (ClipboardManager)getActivity() - .getSystemService(Context.CLIPBOARD_SERVICE); - clipboard.setText(body); + StringBuilder bodyBuilder = new StringBuilder(); + ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE); + boolean first = true; + + for (MessageRecord messageRecord : messageRecords) { + String body = messageRecord.getDisplayBody().toString(); + + if (body != null) { + if (!first) bodyBuilder.append('\n'); + bodyBuilder.append(body); + first = false; + } + } + + String result = bodyBuilder.toString(); + + if (!TextUtils.isEmpty(result)) + clipboard.setText(result); } private void handleDeleteMessages(final List messageRecords) { @@ -385,7 +409,7 @@ public class ConversationFragment extends ListFragment public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch(item.getItemId()) { case R.id.menu_context_copy: - handleCopyMessage(getSelectedMessageRecord()); + handleCopyMessage(getSelectedMessageRecords()); actionMode.finish(); return true; case R.id.menu_context_delete_message: