ViewStub for AudioView in ConversationItems

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2017-01-20 09:11:40 -08:00
parent 7286fd9b06
commit e270e8d429
7 changed files with 83 additions and 44 deletions

View File

@ -60,14 +60,11 @@
tools:src="@drawable/ic_video_light"
tools:visibility="gone" />
<org.thoughtcrime.securesms.components.AudioView
android:id="@+id/audio_view"
<ViewStub
android:id="@+id/audio_view_stub"
android:layout="@layout/conversation_item_received_audio"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:visibility="gone"
app:foregroundTintColor="@color/white"
app:backgroundTintColor="@color/blue_500"
tools:visibility="visible"/>
android:layout_height="wrap_content"/>
<org.thoughtcrime.securesms.components.emoji.EmojiTextView
android:id="@+id/conversation_item_body"

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<org.thoughtcrime.securesms.components.AudioView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/audio_view"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:visibility="gone"
app:foregroundTintColor="@color/white"
app:backgroundTintColor="@color/blue_500"
tools:visibility="visible"/>

View File

@ -51,13 +51,11 @@
tools:src="@drawable/ic_video_light"
tools:visibility="visible" />
<org.thoughtcrime.securesms.components.AudioView
android:id="@+id/audio_view"
<ViewStub
android:id="@+id/audio_view_stub"
android:layout="@layout/conversation_item_sent_audio"
android:layout_width="210dp"
android:layout_height="wrap_content"
app:foregroundTintColor="@color/grey_500"
app:backgroundTintColor="@color/white"
android:visibility="gone"/>
android:layout_height="wrap_content"/>
<org.thoughtcrime.securesms.components.emoji.EmojiTextView
android:id="@+id/conversation_item_body"

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<org.thoughtcrime.securesms.components.AudioView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/audio_view"
android:layout_width="210dp"
android:layout_height="wrap_content"
app:foregroundTintColor="@color/grey_500"
app:backgroundTintColor="@color/white"
android:visibility="gone"/>

View File

@ -35,6 +35,7 @@ import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MmsSmsColumns;
import org.thoughtcrime.securesms.database.MmsSmsDatabase;
import org.thoughtcrime.securesms.database.SmsDatabase;
import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord;
import org.thoughtcrime.securesms.database.model.MessageRecord;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.LRUCache;
@ -63,9 +64,11 @@ public class ConversationAdapter <V extends View & BindableConversationItem>
private final Map<String,SoftReference<MessageRecord>> messageRecordCache =
Collections.synchronizedMap(new LRUCache<String, SoftReference<MessageRecord>>(MAX_CACHE_SIZE));
public static final int MESSAGE_TYPE_OUTGOING = 0;
public static final int MESSAGE_TYPE_INCOMING = 1;
public static final int MESSAGE_TYPE_UPDATE = 2;
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 final Set<MessageRecord> batchSelected = Collections.synchronizedSet(new HashSet<MessageRecord>());
@ -168,10 +171,12 @@ public class ConversationAdapter <V extends View & BindableConversationItem>
private @LayoutRes int getLayoutForViewType(int viewType) {
switch (viewType) {
case ConversationAdapter.MESSAGE_TYPE_OUTGOING: return R.layout.conversation_item_sent;
case ConversationAdapter.MESSAGE_TYPE_INCOMING: return R.layout.conversation_item_received;
case ConversationAdapter.MESSAGE_TYPE_UPDATE: return R.layout.conversation_item_update;
default: throw new IllegalArgumentException("unsupported item view type given to ConversationAdapter");
case MESSAGE_TYPE_AUDIO_OUTGOING:
case MESSAGE_TYPE_OUTGOING: return R.layout.conversation_item_sent;
case MESSAGE_TYPE_AUDIO_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");
}
}
@ -182,9 +187,11 @@ public class ConversationAdapter <V extends View & BindableConversationItem>
MessageRecord messageRecord = getMessageRecord(id, cursor, type);
if (messageRecord.isGroupAction() || messageRecord.isCallLog() || messageRecord.isJoined() ||
messageRecord.isExpirationTimerUpdate() || messageRecord.isEndSession() || messageRecord.isIdentityUpdate())
{
messageRecord.isExpirationTimerUpdate() || messageRecord.isEndSession() || messageRecord.isIdentityUpdate()) {
return MESSAGE_TYPE_UPDATE;
} else if (hasAudio(messageRecord)) {
if (messageRecord.isOutgoing()) return MESSAGE_TYPE_AUDIO_OUTGOING;
else return MESSAGE_TYPE_AUDIO_INCOMING;
} else if (messageRecord.isOutgoing()) {
return MESSAGE_TYPE_OUTGOING;
} else {
@ -227,4 +234,10 @@ public class ConversationAdapter <V extends View & BindableConversationItem>
public Set<MessageRecord> getSelectedItems() {
return Collections.unmodifiableSet(new HashSet<>(batchSelected));
}
private boolean hasAudio(MessageRecord messageRecord) {
return messageRecord.isMms() &&
!messageRecord.isMmsNotification() &&
((MediaMmsMessageRecord)messageRecord).getSlideDeck().getAudioSlide() != null;
}
}

View File

@ -33,6 +33,7 @@ import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
@ -70,6 +71,7 @@ import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.dualsim.SubscriptionInfoCompat;
import org.thoughtcrime.securesms.util.dualsim.SubscriptionManagerCompat;
import org.thoughtcrime.securesms.util.views.Stub;
import org.whispersystems.libsignal.util.guava.Optional;
import java.util.HashSet;
@ -96,7 +98,7 @@ public class ConversationItem extends LinearLayout
private boolean groupThread;
private Recipient recipient;
private View bodyBubble;
protected View bodyBubble;
private TextView bodyText;
private TextView dateText;
private TextView simInfoText;
@ -110,16 +112,19 @@ public class ConversationItem extends LinearLayout
private @NonNull Set<MessageRecord> batchSelected = new HashSet<>();
private @Nullable Recipients conversationRecipients;
private @NonNull ThumbnailView mediaThumbnail;
private @NonNull AudioView audioView;
private @NonNull Stub<AudioView> audioViewStub;
private @NonNull Button mmsDownloadButton;
private @NonNull TextView mmsDownloadingLabel;
private @NonNull ExpirationTimerView expirationTimer;
private int defaultBubbleColor;
private final MmsDownloadClickListener mmsDownloadClickListener = new MmsDownloadClickListener();
private final MmsPreferencesClickListener mmsPreferencesClickListener = new MmsPreferencesClickListener();
private final Context context;
private final MmsDownloadClickListener mmsDownloadClickListener = new MmsDownloadClickListener();
private final MmsPreferencesClickListener mmsPreferencesClickListener = new MmsPreferencesClickListener();
private final PassthroughClickListener passthroughClickListener = new PassthroughClickListener();
private final AttachmentDownloadClickListener downloadClickListener = new AttachmentDownloadClickListener();
private final Context context;
public ConversationItem(Context context) {
this(context, null);
@ -154,20 +159,17 @@ public class ConversationItem extends LinearLayout
this.contactPhoto = (AvatarImageView) findViewById(R.id.contact_photo);
this.bodyBubble = findViewById(R.id.body_bubble);
this.mediaThumbnail = (ThumbnailView) findViewById(R.id.image_view);
this.audioView = (AudioView) findViewById(R.id.audio_view);
this.audioViewStub = new Stub<>((ViewStub) findViewById(R.id.audio_view_stub));
this.expirationTimer = (ExpirationTimerView) findViewById(R.id.expiration_indicator);
setOnClickListener(new ClickListener(null));
PassthroughClickListener passthroughClickListener = new PassthroughClickListener();
AttachmentDownloadClickListener downloadClickListener = new AttachmentDownloadClickListener();
mmsDownloadButton.setOnClickListener(mmsDownloadClickListener);
mediaThumbnail.setThumbnailClickListener(new ThumbnailClickListener());
mediaThumbnail.setDownloadClickListener(downloadClickListener);
mediaThumbnail.setOnLongClickListener(passthroughClickListener);
mediaThumbnail.setOnClickListener(passthroughClickListener);
audioView.setDownloadClickListener(downloadClickListener);
audioView.setOnLongClickListener(passthroughClickListener);
bodyText.setOnLongClickListener(passthroughClickListener);
bodyText.setOnClickListener(passthroughClickListener);
}
@ -192,12 +194,12 @@ public class ConversationItem extends LinearLayout
setInteractionState(messageRecord);
setBodyText(messageRecord);
setMediaAttributes(messageRecord);
setBubbleState(messageRecord, recipient);
setStatusIcons(messageRecord);
setContactPhoto(recipient);
setGroupMessageStatus(messageRecord, recipient);
setMinimumWidth();
setMediaAttributes(messageRecord);
setSimInfo(messageRecord);
setExpiration(messageRecord);
}
@ -237,18 +239,20 @@ public class ConversationItem extends LinearLayout
mediaThumbnail.setBackgroundColorHint(color);
}
setAudioViewTint(messageRecord, conversationRecipients);
if (audioViewStub.resolved()) {
setAudioViewTint(messageRecord, conversationRecipients);
}
}
private void setAudioViewTint(MessageRecord messageRecord, Recipients recipients) {
if (messageRecord.isOutgoing()) {
if (DynamicTheme.LIGHT.equals(TextSecurePreferences.getTheme(context))) {
audioView.setTint(recipients.getColor().toConversationColor(context), defaultBubbleColor);
audioViewStub.get().setTint(recipients.getColor().toConversationColor(context), defaultBubbleColor);
} else {
audioView.setTint(Color.WHITE, defaultBubbleColor);
audioViewStub.get().setTint(Color.WHITE, defaultBubbleColor);
}
} else {
audioView.setTint(Color.WHITE, recipients.getColor().toConversationColor(context));
audioViewStub.get().setTint(Color.WHITE, recipients.getColor().toConversationColor(context));
}
}
@ -293,20 +297,23 @@ public class ConversationItem extends LinearLayout
if (messageRecord.isMmsNotification()) {
mediaThumbnail.setVisibility(View.GONE);
audioView.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)) {
audioView.setVisibility(View.VISIBLE);
audioViewStub.get().setVisibility(View.VISIBLE);
mediaThumbnail.setVisibility(View.GONE);
//noinspection ConstantConditions
audioView.setAudio(masterSecret, ((MediaMmsMessageRecord) messageRecord).getSlideDeck().getAudioSlide(), showControls);
audioViewStub.get().setAudio(masterSecret, ((MediaMmsMessageRecord) messageRecord).getSlideDeck().getAudioSlide(), showControls);
audioViewStub.get().setDownloadClickListener(downloadClickListener);
audioViewStub.get().setOnLongClickListener(passthroughClickListener);
bodyText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
} else if (hasThumbnail(messageRecord)) {
mediaThumbnail.setVisibility(View.VISIBLE);
audioView.setVisibility(View.GONE);
if (audioViewStub.resolved()) audioViewStub.get().setVisibility(View.GONE);
//noinspection ConstantConditions
mediaThumbnail.setImageResource(masterSecret,
@ -315,7 +322,7 @@ public class ConversationItem extends LinearLayout
bodyText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} else {
mediaThumbnail.setVisibility(View.GONE);
audioView.setVisibility(View.GONE);
if (audioViewStub.resolved()) audioViewStub.get().setVisibility(View.GONE);
bodyText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}

View File

@ -1,20 +1,22 @@
package org.thoughtcrime.securesms.util.views;
import android.support.annotation.NonNull;
import android.view.ViewStub;
public class Stub<T> {
private final ViewStub viewStub;
private ViewStub viewStub;
private T view;
public Stub(ViewStub viewStub) {
public Stub(@NonNull ViewStub viewStub) {
this.viewStub = viewStub;
}
public T get() {
if (view == null) {
view = (T)viewStub.inflate();
viewStub = null;
}
return view;