From 183f8742a7d034c951eca20e854b49cee5541d31 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Fri, 20 Jan 2017 09:30:08 -0800 Subject: [PATCH] ViewStub for ThumbnailView in ConversationItem // FREEBIE --- res/layout/conversation_item_received.xml | 13 ++--- .../conversation_item_received_thumbnail.xml | 13 +++++ res/layout/conversation_item_sent.xml | 13 ++--- .../conversation_item_sent_thumbnail.xml | 15 ++++++ .../securesms/ConversationAdapter.java | 23 +++++++-- .../securesms/ConversationItem.java | 47 ++++++++++--------- 6 files changed, 78 insertions(+), 46 deletions(-) create mode 100644 res/layout/conversation_item_received_thumbnail.xml create mode 100644 res/layout/conversation_item_sent_thumbnail.xml diff --git a/res/layout/conversation_item_received.xml b/res/layout/conversation_item_received.xml index f2fc4196fb..dd5bac8b87 100644 --- a/res/layout/conversation_item_received.xml +++ b/res/layout/conversation_item_received.xml @@ -49,16 +49,11 @@ android:orientation="vertical" tools:backgroundTint="@color/blue_900"> - + android:layout_height="@dimen/media_bubble_height"/> + diff --git a/res/layout/conversation_item_sent.xml b/res/layout/conversation_item_sent.xml index b0ef521bad..20667217d2 100644 --- a/res/layout/conversation_item_sent.xml +++ b/res/layout/conversation_item_sent.xml @@ -38,18 +38,11 @@ android:background="@drawable/sent_bubble" android:orientation="vertical"> - + android:layout="@layout/conversation_item_sent_thumbnail"/> + diff --git a/src/org/thoughtcrime/securesms/ConversationAdapter.java b/src/org/thoughtcrime/securesms/ConversationAdapter.java index 7cc3fd6f71..5680007d50 100644 --- a/src/org/thoughtcrime/securesms/ConversationAdapter.java +++ b/src/org/thoughtcrime/securesms/ConversationAdapter.java @@ -64,11 +64,13 @@ public class ConversationAdapter private final Map> messageRecordCache = Collections.synchronizedMap(new LRUCache>(MAX_CACHE_SIZE)); - private static final int MESSAGE_TYPE_OUTGOING = 0; - private static final int MESSAGE_TYPE_INCOMING = 1; - private static final int MESSAGE_TYPE_UPDATE = 2; - private static final int MESSAGE_TYPE_AUDIO_OUTGOING = 3; - private static final int MESSAGE_TYPE_AUDIO_INCOMING = 4; + private static final int MESSAGE_TYPE_OUTGOING = 0; + private static final int MESSAGE_TYPE_INCOMING = 1; + private static final int MESSAGE_TYPE_UPDATE = 2; + private static final int MESSAGE_TYPE_AUDIO_OUTGOING = 3; + private static final int MESSAGE_TYPE_AUDIO_INCOMING = 4; + private static final int MESSAGE_TYPE_THUMBNAIL_OUTGOING = 5; + private static final int MESSAGE_TYPE_THUMBNAIL_INCOMING = 6; private final Set batchSelected = Collections.synchronizedSet(new HashSet()); @@ -172,8 +174,10 @@ public class ConversationAdapter private @LayoutRes int getLayoutForViewType(int viewType) { switch (viewType) { case MESSAGE_TYPE_AUDIO_OUTGOING: + case MESSAGE_TYPE_THUMBNAIL_OUTGOING: case MESSAGE_TYPE_OUTGOING: return R.layout.conversation_item_sent; case MESSAGE_TYPE_AUDIO_INCOMING: + case MESSAGE_TYPE_THUMBNAIL_INCOMING: case MESSAGE_TYPE_INCOMING: return R.layout.conversation_item_received; case MESSAGE_TYPE_UPDATE: return R.layout.conversation_item_update; default: throw new IllegalArgumentException("unsupported item view type given to ConversationAdapter"); @@ -192,6 +196,9 @@ public class ConversationAdapter } else if (hasAudio(messageRecord)) { if (messageRecord.isOutgoing()) return MESSAGE_TYPE_AUDIO_OUTGOING; else return MESSAGE_TYPE_AUDIO_INCOMING; + } else if (hasThumbnail(messageRecord)) { + if (messageRecord.isOutgoing()) return MESSAGE_TYPE_THUMBNAIL_OUTGOING; + else return MESSAGE_TYPE_THUMBNAIL_INCOMING; } else if (messageRecord.isOutgoing()) { return MESSAGE_TYPE_OUTGOING; } else { @@ -240,4 +247,10 @@ public class ConversationAdapter !messageRecord.isMmsNotification() && ((MediaMmsMessageRecord)messageRecord).getSlideDeck().getAudioSlide() != null; } + + private boolean hasThumbnail(MessageRecord messageRecord) { + return messageRecord.isMms() && + !messageRecord.isMmsNotification() && + ((MediaMmsMessageRecord)messageRecord).getSlideDeck().getThumbnailSlide() != null; + } } diff --git a/src/org/thoughtcrime/securesms/ConversationItem.java b/src/org/thoughtcrime/securesms/ConversationItem.java index 7dad93b0da..743d242a44 100644 --- a/src/org/thoughtcrime/securesms/ConversationItem.java +++ b/src/org/thoughtcrime/securesms/ConversationItem.java @@ -111,7 +111,7 @@ public class ConversationItem extends LinearLayout private @NonNull Set batchSelected = new HashSet<>(); private @Nullable Recipients conversationRecipients; - private @NonNull ThumbnailView mediaThumbnail; + private @NonNull Stub mediaThumbnailStub; private @NonNull Stub audioViewStub; private @NonNull Button mmsDownloadButton; private @NonNull TextView mmsDownloadingLabel; @@ -158,18 +158,13 @@ public class ConversationItem extends LinearLayout this.mmsDownloadingLabel = (TextView) findViewById(R.id.mms_label_downloading); this.contactPhoto = (AvatarImageView) findViewById(R.id.contact_photo); this.bodyBubble = findViewById(R.id.body_bubble); - this.mediaThumbnail = (ThumbnailView) findViewById(R.id.image_view); + this.mediaThumbnailStub = new Stub<>((ViewStub) findViewById(R.id.image_view_stub)); this.audioViewStub = new Stub<>((ViewStub) findViewById(R.id.audio_view_stub)); this.expirationTimer = (ExpirationTimerView) findViewById(R.id.expiration_indicator); - setOnClickListener(new ClickListener(null)); mmsDownloadButton.setOnClickListener(mmsDownloadClickListener); - mediaThumbnail.setThumbnailClickListener(new ThumbnailClickListener()); - mediaThumbnail.setDownloadClickListener(downloadClickListener); - mediaThumbnail.setOnLongClickListener(passthroughClickListener); - mediaThumbnail.setOnClickListener(passthroughClickListener); bodyText.setOnLongClickListener(passthroughClickListener); bodyText.setOnClickListener(passthroughClickListener); } @@ -192,9 +187,9 @@ public class ConversationItem extends LinearLayout this.recipient.addListener(this); this.conversationRecipients.addListener(this); - setInteractionState(messageRecord); setBodyText(messageRecord); setMediaAttributes(messageRecord); + setInteractionState(messageRecord); setBubbleState(messageRecord, recipient); setStatusIcons(messageRecord); setContactPhoto(recipient); @@ -232,11 +227,11 @@ public class ConversationItem extends LinearLayout private void setBubbleState(MessageRecord messageRecord, Recipient recipient) { if (messageRecord.isOutgoing()) { bodyBubble.getBackground().setColorFilter(defaultBubbleColor, PorterDuff.Mode.MULTIPLY); - mediaThumbnail.setBackgroundColorHint(defaultBubbleColor); + if (mediaThumbnailStub.resolved()) mediaThumbnailStub.get().setBackgroundColorHint(defaultBubbleColor); } else { int color = recipient.getColor().toConversationColor(context); bodyBubble.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY); - mediaThumbnail.setBackgroundColorHint(color); + if (mediaThumbnailStub.resolved()) mediaThumbnailStub.get().setBackgroundColorHint(color); } if (audioViewStub.resolved()) { @@ -258,10 +253,13 @@ public class ConversationItem extends LinearLayout private void setInteractionState(MessageRecord messageRecord) { setSelected(batchSelected.contains(messageRecord)); - mediaThumbnail.setFocusable(!shouldInterceptClicks(messageRecord) && batchSelected.isEmpty()); - mediaThumbnail.setClickable(!shouldInterceptClicks(messageRecord) && batchSelected.isEmpty()); - mediaThumbnail.setLongClickable(batchSelected.isEmpty()); bodyText.setAutoLinkMask(batchSelected.isEmpty() ? Linkify.ALL : 0); + + if (mediaThumbnailStub.resolved()) { + mediaThumbnailStub.get().setFocusable(!shouldInterceptClicks(messageRecord) && batchSelected.isEmpty()); + mediaThumbnailStub.get().setClickable(!shouldInterceptClicks(messageRecord) && batchSelected.isEmpty()); + mediaThumbnailStub.get().setLongClickable(batchSelected.isEmpty()); + } } private boolean isCaptionlessMms(MessageRecord messageRecord) { @@ -296,14 +294,14 @@ public class ConversationItem extends LinearLayout boolean showControls = !messageRecord.isFailed() && (!messageRecord.isOutgoing() || messageRecord.isPending()); if (messageRecord.isMmsNotification()) { - mediaThumbnail.setVisibility(View.GONE); - if (audioViewStub.resolved()) audioViewStub.get().setVisibility(View.GONE); + if (mediaThumbnailStub.resolved()) mediaThumbnailStub.get().setVisibility(View.GONE); + if (audioViewStub.resolved()) audioViewStub.get().setVisibility(View.GONE); bodyText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); setNotificationMmsAttributes((NotificationMmsMessageRecord) messageRecord); } else if (hasAudio(messageRecord)) { audioViewStub.get().setVisibility(View.VISIBLE); - mediaThumbnail.setVisibility(View.GONE); + if (mediaThumbnailStub.resolved()) mediaThumbnailStub.get().setVisibility(View.GONE); //noinspection ConstantConditions audioViewStub.get().setAudio(masterSecret, ((MediaMmsMessageRecord) messageRecord).getSlideDeck().getAudioSlide(), showControls); @@ -312,17 +310,22 @@ public class ConversationItem extends LinearLayout bodyText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } else if (hasThumbnail(messageRecord)) { - mediaThumbnail.setVisibility(View.VISIBLE); + mediaThumbnailStub.get().setVisibility(View.VISIBLE); if (audioViewStub.resolved()) audioViewStub.get().setVisibility(View.GONE); //noinspection ConstantConditions - mediaThumbnail.setImageResource(masterSecret, - ((MediaMmsMessageRecord)messageRecord).getSlideDeck().getThumbnailSlide(), - showControls); + mediaThumbnailStub.get().setImageResource(masterSecret, + ((MediaMmsMessageRecord)messageRecord).getSlideDeck().getThumbnailSlide(), + showControls); + mediaThumbnailStub.get().setThumbnailClickListener(new ThumbnailClickListener()); + mediaThumbnailStub.get().setDownloadClickListener(downloadClickListener); + mediaThumbnailStub.get().setOnLongClickListener(passthroughClickListener); + mediaThumbnailStub.get().setOnClickListener(passthroughClickListener); + bodyText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } else { - mediaThumbnail.setVisibility(View.GONE); - if (audioViewStub.resolved()) audioViewStub.get().setVisibility(View.GONE); + if (mediaThumbnailStub.resolved()) mediaThumbnailStub.get().setVisibility(View.GONE); + if (audioViewStub.resolved()) audioViewStub.get().setVisibility(View.GONE); bodyText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); } }