From e9aa5d9daac2cc82d893dfdc26e344fb1a9dc8e2 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Sun, 12 Jun 2016 11:44:23 +0200 Subject: Implements FS#217: Rework display of /me messages --- .../ui/adapter/MessageAdapter.java | 277 ++++++++++++--------- src/main/res/drawable/mecmd_message_border.xml | 15 ++ .../res/layout/fragment_conversations_overview.xml | 2 +- src/main/res/layout/message_mecmd.xml | 112 +++++++++ src/main/res/layout/message_received.xml | 10 +- src/main/res/layout/message_sent.xml | 75 +++++- src/main/res/layout/message_status.xml | 4 +- src/main/res/values/dimens.xml | 3 + 8 files changed, 366 insertions(+), 132 deletions(-) create mode 100644 src/main/res/drawable/mecmd_message_border.xml create mode 100644 src/main/res/layout/message_mecmd.xml diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java b/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java index 5c62b1f4..f4c40edf 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java @@ -38,6 +38,7 @@ import java.util.concurrent.RejectedExecutionException; import java.util.regex.Matcher; import java.util.regex.Pattern; +import de.thedevstack.android.logcat.Logging; import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.ConversationsPlusColors; import de.thedevstack.conversationsplus.ConversationsPlusPreferences; @@ -62,7 +63,7 @@ public class MessageAdapter extends ArrayAdapter { private static final int SENT = 0; private static final int RECEIVED = 1; private static final int STATUS = 2; - private static final int NULL = 3; + private static final int ME_COMMAND = 3; private static final Pattern XMPP_PATTERN = Pattern .compile("xmpp\\:(?:(?:[" + Patterns.GOOD_IRI_CHAR @@ -102,11 +103,13 @@ public class MessageAdapter extends ArrayAdapter { @Override public int getViewTypeCount() { - return 3; + return 4; } public int getItemViewType(Message message) { - if (message.getType() == Message.TYPE_STATUS) { + if (message.hasMeCommand()) { + return ME_COMMAND; + } else if (message.getType() == Message.TYPE_STATUS) { return STATUS; } else if (message.getStatus() <= Message.STATUS_RECEIVED) { return RECEIVED; @@ -253,7 +256,10 @@ public class MessageAdapter extends ArrayAdapter { if (viewHolder.download_button != null) { viewHolder.download_button.setVisibility(View.GONE); } - viewHolder.image.setVisibility(View.GONE); + if (null != viewHolder.image) { + viewHolder.image.setVisibility(View.GONE); + } + viewHolder.messageBody.setVisibility(View.VISIBLE); viewHolder.messageBody.setText(text); viewHolder.messageBody.setTextColor(getMessageTextColor(darkBackground, false)); @@ -265,7 +271,9 @@ public class MessageAdapter extends ArrayAdapter { if (viewHolder.download_button != null) { viewHolder.download_button.setVisibility(View.GONE); } - viewHolder.image.setVisibility(View.GONE); + if (null != viewHolder.image) { + viewHolder.image.setVisibility(View.GONE); + } viewHolder.messageBody.setVisibility(View.VISIBLE); viewHolder.messageBody.setText(getContext().getString( R.string.decryption_failed)); @@ -278,7 +286,10 @@ public class MessageAdapter extends ArrayAdapter { if (viewHolder.download_button != null) { viewHolder.download_button.setVisibility(View.GONE); } - viewHolder.image.setVisibility(View.GONE); + if (null != viewHolder.image) { + viewHolder.image.setVisibility(View.GONE); + } + viewHolder.messageBody.setVisibility(View.VISIBLE); viewHolder.messageBody.setIncludeFontPadding(true); if (message.getBody() != null) { @@ -383,7 +394,9 @@ public class MessageAdapter extends ArrayAdapter { private void displayDownloadableMessage(ViewHolder viewHolder, final Message message, String text) { - viewHolder.image.setVisibility(View.GONE); + if (null != viewHolder.image) { + viewHolder.image.setVisibility(View.GONE); + } viewHolder.messageBody.setVisibility(View.GONE); viewHolder.download_button.setVisibility(View.VISIBLE); viewHolder.download_button.setText(text); @@ -429,6 +442,10 @@ public class MessageAdapter extends ArrayAdapter { private void displayImageMessage(ViewHolder viewHolder, final Message message) { + if (null == viewHolder.image) { + Toast.makeText(activity, "ImageView null??? " + message.getBody(), Toast.LENGTH_LONG); + return; + } if (viewHolder.download_button != null) { viewHolder.download_button.setVisibility(View.GONE); } @@ -461,6 +478,116 @@ public class MessageAdapter extends ArrayAdapter { viewHolder.image.setOnLongClickListener(openContextMenu); } + private View initializeView(Message message, int type, ViewGroup parent) { + Integer viewResId = null; + + switch (type) { + case SENT: + viewResId = R.layout.message_sent; + break; + case RECEIVED: + viewResId = R.layout.message_received; + break; + case STATUS: + viewResId = R.layout.message_status; + break; + case ME_COMMAND: + viewResId = R.layout.message_mecmd; + break; + } + + return activity.getLayoutInflater().inflate(viewResId, parent, false); + } + + private ViewHolder initializeViewHolderAndView(Message message, int type, ViewGroup parent) { + View view = initializeView(message, type, parent); + ViewHolder viewHolder = new ViewHolder(view); + if (SENT == type + || RECEIVED == type + || ME_COMMAND == type) { + viewHolder.message_box = (LinearLayout) view.findViewById(R.id.message_box); + viewHolder.message_box.setVisibility(View.VISIBLE); + viewHolder.indicator = (ImageView) view.findViewById(R.id.security_indicator); + viewHolder.messageBody = (TextView) view.findViewById(R.id.message_body); + viewHolder.time = (TextView) view.findViewById(R.id.message_time); + viewHolder.indicatorReceived = (ImageView) view.findViewById(R.id.indicator_received); + } + if ((SENT == type + || RECEIVED == type) + && ME_COMMAND != type) { + viewHolder.download_button = (Button) view.findViewById(R.id.download_button); + viewHolder.image = (ImageView) view.findViewById(R.id.message_image); + } + if (ME_COMMAND == type + || (RECEIVED == type && message.getConversation().getMode() == Conversation.MODE_MULTI)) { + viewHolder.contact_picture = (ImageView) view.findViewById(R.id.message_photo); + viewHolder.contact_picture.setVisibility(View.VISIBLE); + } + if (RECEIVED == type) { + viewHolder.encryption = (TextView) view.findViewById(R.id.message_encryption); + } + if (STATUS == type) { + viewHolder.contact_picture = (ImageView) view.findViewById(R.id.message_photo); + viewHolder.status_message = (TextView) view.findViewById(R.id.status_message); + } + view.setTag(viewHolder); + + return viewHolder; + } + + private void displayAvatar(final Message message, int type, ViewHolder viewHolder) { + if (type == ME_COMMAND || (type == RECEIVED && message.getConversation().getMode() == Conversation.MODE_MULTI)) { + ImageView imageView = viewHolder.contact_picture; + if (null != imageView) { + if (cancelPotentialWork(message, imageView)) { + //int avatarPixel = activity.getPixel(48); + int avatarPixel = imageView.getResources().getDimensionPixelOffset(message.hasMeCommand() ? R.dimen.msg_mecmd_avatar_size : R.dimen.msg_avatar_size); + final Bitmap bm = AvatarService.getInstance().get(message, avatarPixel, true); + if (bm != null) { + imageView.setImageBitmap(bm); + imageView.setBackgroundColor(0x00000000); + } else { + imageView.setBackgroundColor(UIHelper.getColorForName(UIHelper.getMessageDisplayName(message))); + imageView.setImageDrawable(null); + final BitmapWorkerTask task = new BitmapWorkerTask(imageView); + final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task); + imageView.setImageDrawable(asyncDrawable); + try { + task.execute(message); + } catch (final RejectedExecutionException ignored) { + } + } + } + imageView.setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + if (MessageAdapter.this.mOnContactPictureClickedListener != null) { + MessageAdapter.this.mOnContactPictureClickedListener + .onContactPictureClicked(message); + } + + } + }); + if (message.getConversation().getMode() == Conversation.MODE_MULTI) { + imageView.setOnLongClickListener(new OnLongClickListener() { + + @Override + public boolean onLongClick(View v) { + if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) { + MessageAdapter.this.mOnContactPictureLongClickedListener + .onContactPictureLongClicked(message); + return true; + } else { + return false; + } + } + }); + } + } + } + } + @Override public View getView(int position, View view, ViewGroup parent) { final Message message = getItem(position); @@ -468,69 +595,18 @@ public class MessageAdapter extends ArrayAdapter { final Conversation conversation = message.getConversation(); final Account account = conversation.getAccount(); final int type = getItemViewType(position); - ViewHolder viewHolder; - if (view == null) { - viewHolder = new ViewHolder(); - switch (type) { - case SENT: - view = activity.getLayoutInflater().inflate( - R.layout.message_sent, parent, false); - viewHolder.message_box = (LinearLayout) view - .findViewById(R.id.message_box); - viewHolder.contact_picture = (ImageView) view - .findViewById(R.id.message_photo); - viewHolder.download_button = (Button) view - .findViewById(R.id.download_button); - viewHolder.indicator = (ImageView) view - .findViewById(R.id.security_indicator); - viewHolder.image = (ImageView) view - .findViewById(R.id.message_image); - viewHolder.messageBody = (TextView) view - .findViewById(R.id.message_body); - viewHolder.time = (TextView) view - .findViewById(R.id.message_time); - viewHolder.indicatorReceived = (ImageView) view - .findViewById(R.id.indicator_received); - break; - case RECEIVED: - view = activity.getLayoutInflater().inflate( - R.layout.message_received, parent, false); - viewHolder.message_box = (LinearLayout) view - .findViewById(R.id.message_box); - viewHolder.contact_picture = (ImageView) view - .findViewById(R.id.message_photo); - viewHolder.download_button = (Button) view - .findViewById(R.id.download_button); - viewHolder.indicator = (ImageView) view - .findViewById(R.id.security_indicator); - viewHolder.image = (ImageView) view - .findViewById(R.id.message_image); - viewHolder.messageBody = (TextView) view - .findViewById(R.id.message_body); - viewHolder.time = (TextView) view - .findViewById(R.id.message_time); - viewHolder.indicatorReceived = (ImageView) view - .findViewById(R.id.indicator_received); - viewHolder.encryption = (TextView) view.findViewById(R.id.message_encryption); - break; - case STATUS: - view = activity.getLayoutInflater().inflate(R.layout.message_status, parent, false); - viewHolder.contact_picture = (ImageView) view.findViewById(R.id.message_photo); - viewHolder.status_message = (TextView) view.findViewById(R.id.status_message); - break; - default: - viewHolder = null; - break; - } - view.setTag(viewHolder); - } else { - viewHolder = (ViewHolder) view.getTag(); - if (viewHolder == null) { - return view; - } - } + ViewHolder viewHolder; + if (null == view) { + viewHolder = initializeViewHolderAndView(message, type, parent); + view = viewHolder.view; + } else { + viewHolder = (ViewHolder) view.getTag(); + if (null == viewHolder) { + return view; + } + } + Logging.d("MessageAdapter", "Received: " + (type == RECEIVED) + ", SENT: " + (type == SENT) + ", Status: " + (type == STATUS) + ", /me: " + (type == ME_COMMAND)); - boolean darkBackground = (type == RECEIVED && !isInValidSession); if (type == STATUS) { viewHolder.status_message.setVisibility(View.VISIBLE); @@ -542,41 +618,13 @@ public class MessageAdapter extends ArrayAdapter { } viewHolder.status_message.setText(message.getBody()); return view; - } else { - if (message.getConversation().getMode() == Conversation.MODE_MULTI) { - loadAvatar(message, viewHolder.contact_picture); + } + this.displayAvatar(message, type, viewHolder); - viewHolder.contact_picture - .setOnClickListener(new OnClickListener() { + boolean darkBackground = (type == RECEIVED && !isInValidSession); - @Override - public void onClick(View v) { - if (MessageAdapter.this.mOnContactPictureClickedListener != null) { - MessageAdapter.this.mOnContactPictureClickedListener - .onContactPictureClicked(message); - } - - } - }); - viewHolder.contact_picture - .setOnLongClickListener(new OnLongClickListener() { - - @Override - public boolean onLongClick(View v) { - if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) { - MessageAdapter.this.mOnContactPictureLongClickedListener - .onContactPictureLongClicked(message); - return true; - } else { - return false; - } - } - }); - } else { - viewHolder.contact_picture.setVisibility(View.GONE); - } - } + this.displayStatus(viewHolder, message, type, darkBackground); final Transferable transferable = message.getTransferable(); if (transferable != null && transferable.getStatus() != Transferable.STATUS_UPLOADING) { @@ -649,8 +697,6 @@ public class MessageAdapter extends ArrayAdapter { } } - displayStatus(viewHolder, message, type, darkBackground); - return view; } @@ -718,7 +764,10 @@ public class MessageAdapter extends ArrayAdapter { } private static class ViewHolder { - + protected ViewHolder(View view) { + this.view = view; + } + protected View view; protected LinearLayout message_box; protected Button download_button; protected ImageView image; @@ -756,26 +805,6 @@ public class MessageAdapter extends ArrayAdapter { } } - public void loadAvatar(Message message, ImageView imageView) { - if (cancelPotentialWork(message, imageView)) { - final Bitmap bm = AvatarService.getInstance().get(message, activity.getPixel(48), true); - if (bm != null) { - imageView.setImageBitmap(bm); - imageView.setBackgroundColor(0x00000000); - } else { - imageView.setBackgroundColor(UIHelper.getColorForName(UIHelper.getMessageDisplayName(message))); - imageView.setImageDrawable(null); - final BitmapWorkerTask task = new BitmapWorkerTask(imageView); - final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task); - imageView.setImageDrawable(asyncDrawable); - try { - task.execute(message); - } catch (final RejectedExecutionException ignored) { - } - } - } - } - public static boolean cancelPotentialWork(Message message, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); diff --git a/src/main/res/drawable/mecmd_message_border.xml b/src/main/res/drawable/mecmd_message_border.xml new file mode 100644 index 00000000..990d0288 --- /dev/null +++ b/src/main/res/drawable/mecmd_message_border.xml @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/main/res/layout/fragment_conversations_overview.xml b/src/main/res/layout/fragment_conversations_overview.xml index 6563dc2b..2fe182bb 100644 --- a/src/main/res/layout/fragment_conversations_overview.xml +++ b/src/main/res/layout/fragment_conversations_overview.xml @@ -69,7 +69,7 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/res/layout/message_received.xml b/src/main/res/layout/message_received.xml index d331b3ef..4a51ba59 100644 --- a/src/main/res/layout/message_received.xml +++ b/src/main/res/layout/message_received.xml @@ -11,13 +11,14 @@ + app:riv_corner_radius="2dp" + android:visibility="gone" /> + android:longClickable="true" + android:visibility="gone"> + android:longClickable="true" + android:visibility="gone"> + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/res/layout/message_status.xml b/src/main/res/layout/message_status.xml index aa02e154..4c1dbbc3 100644 --- a/src/main/res/layout/message_status.xml +++ b/src/main/res/layout/message_status.xml @@ -11,8 +11,8 @@ 18dp 56dp @dimen/avatar_size + 24dp + 48dp + 32dp -- cgit v1.2.3