2012-07-17 04:56:10 +02:00
|
|
|
/**
|
2011-12-20 19:20:44 +01:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-07-17 04:56:10 +02:00
|
|
|
*
|
2011-12-20 19:20:44 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-07-17 04:56:10 +02:00
|
|
|
*
|
2011-12-20 19:20:44 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
2015-07-02 23:59:50 +02:00
|
|
|
import android.annotation.TargetApi;
|
2011-12-20 19:20:44 +01:00
|
|
|
import android.content.Context;
|
2015-07-02 23:59:50 +02:00
|
|
|
import android.content.res.ColorStateList;
|
2012-07-17 06:35:32 +02:00
|
|
|
import android.graphics.Typeface;
|
2015-07-02 23:59:50 +02:00
|
|
|
import android.graphics.drawable.RippleDrawable;
|
|
|
|
import android.os.Build.VERSION;
|
|
|
|
import android.os.Build.VERSION_CODES;
|
2012-12-24 17:40:37 +01:00
|
|
|
import android.os.Handler;
|
2015-08-07 01:28:51 +02:00
|
|
|
import android.support.annotation.DrawableRes;
|
2015-10-16 22:59:40 +02:00
|
|
|
import android.support.annotation.NonNull;
|
2011-12-20 19:20:44 +01:00
|
|
|
import android.util.AttributeSet;
|
2015-10-16 22:59:40 +02:00
|
|
|
import android.view.View;
|
2011-12-20 19:20:44 +01:00
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2015-05-04 20:36:18 +02:00
|
|
|
import org.thoughtcrime.securesms.components.AvatarImageView;
|
2015-11-24 16:06:41 +01:00
|
|
|
import org.thoughtcrime.securesms.components.DeliveryStatusView;
|
|
|
|
import org.thoughtcrime.securesms.components.AlertView;
|
2015-05-04 20:36:18 +02:00
|
|
|
import org.thoughtcrime.securesms.components.FromTextView;
|
2015-10-16 22:59:40 +02:00
|
|
|
import org.thoughtcrime.securesms.components.ThumbnailView;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2012-10-29 00:04:24 +01:00
|
|
|
import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
2012-07-17 04:56:10 +02:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2014-01-08 23:29:05 +01:00
|
|
|
import org.thoughtcrime.securesms.util.DateUtils;
|
2015-08-07 01:28:51 +02:00
|
|
|
import org.thoughtcrime.securesms.util.ResUtil;
|
2015-11-24 00:07:41 +01:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2012-07-17 04:56:10 +02:00
|
|
|
|
2015-03-19 21:08:48 +01:00
|
|
|
import java.util.Locale;
|
2012-07-17 04:56:10 +02:00
|
|
|
import java.util.Set;
|
2015-02-06 03:35:13 +01:00
|
|
|
|
|
|
|
import static org.thoughtcrime.securesms.util.SpanUtil.color;
|
2012-07-17 04:56:10 +02:00
|
|
|
|
2011-12-20 19:20:44 +01:00
|
|
|
/**
|
|
|
|
* A view that displays the element in a list of multiple conversation threads.
|
|
|
|
* Used by SecureSMS's ListActivity via a ConversationListAdapter.
|
|
|
|
*
|
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
2012-12-24 17:40:37 +01:00
|
|
|
public class ConversationListItem extends RelativeLayout
|
2015-11-24 00:07:41 +01:00
|
|
|
implements Recipients.RecipientsModifiedListener,
|
|
|
|
BindableConversationListItem, Unbindable
|
2012-12-24 17:40:37 +01:00
|
|
|
{
|
2014-02-19 21:29:00 +01:00
|
|
|
private final static String TAG = ConversationListItem.class.getSimpleName();
|
2011-12-20 19:20:44 +01:00
|
|
|
|
2015-02-06 03:35:13 +01:00
|
|
|
private final static Typeface BOLD_TYPEFACE = Typeface.create("sans-serif", Typeface.BOLD);
|
|
|
|
private final static Typeface LIGHT_TYPEFACE = Typeface.create("sans-serif-light", Typeface.NORMAL);
|
|
|
|
|
2015-11-24 16:06:41 +01:00
|
|
|
private Set<Long> selectedThreads;
|
|
|
|
private Recipients recipients;
|
|
|
|
private long threadId;
|
|
|
|
private TextView subjectView;
|
|
|
|
private FromTextView fromView;
|
|
|
|
private TextView dateView;
|
|
|
|
private TextView archivedView;
|
|
|
|
private DeliveryStatusView deliveryStatusIndicator;
|
|
|
|
private AlertView alertView;
|
|
|
|
|
2015-05-04 20:36:18 +02:00
|
|
|
private boolean read;
|
|
|
|
private AvatarImageView contactPhotoImage;
|
2015-10-16 22:59:40 +02:00
|
|
|
private ThumbnailView thumbnailView;
|
2012-07-17 04:56:10 +02:00
|
|
|
|
2015-08-07 01:28:51 +02:00
|
|
|
private final @DrawableRes int readBackground;
|
|
|
|
private final @DrawableRes int unreadBackround;
|
|
|
|
|
2012-12-24 17:40:37 +01:00
|
|
|
private final Handler handler = new Handler();
|
2013-04-26 03:59:49 +02:00
|
|
|
private int distributionType;
|
2012-12-24 17:40:37 +01:00
|
|
|
|
|
|
|
public ConversationListItem(Context context) {
|
2015-08-07 01:28:51 +02:00
|
|
|
this(context, null);
|
2012-12-24 17:40:37 +01:00
|
|
|
}
|
2011-12-20 19:20:44 +01:00
|
|
|
|
2012-12-24 17:40:37 +01:00
|
|
|
public ConversationListItem(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
2015-08-07 01:28:51 +02:00
|
|
|
readBackground = ResUtil.getDrawableRes(context, R.attr.conversation_list_item_background_read);
|
|
|
|
unreadBackround = ResUtil.getDrawableRes(context, R.attr.conversation_list_item_background_unread);
|
2012-12-24 17:40:37 +01:00
|
|
|
}
|
2012-07-17 04:56:10 +02:00
|
|
|
|
2012-12-24 17:40:37 +01:00
|
|
|
@Override
|
|
|
|
protected void onFinishInflate() {
|
2015-06-09 16:37:20 +02:00
|
|
|
super.onFinishInflate();
|
2015-11-24 16:06:41 +01:00
|
|
|
this.subjectView = (TextView) findViewById(R.id.subject);
|
|
|
|
this.fromView = (FromTextView) findViewById(R.id.from);
|
|
|
|
this.dateView = (TextView) findViewById(R.id.date);
|
|
|
|
this.deliveryStatusIndicator = (DeliveryStatusView) findViewById(R.id.delivery_status);
|
|
|
|
this.alertView = (AlertView) findViewById(R.id.indicators_parent);
|
|
|
|
this.contactPhotoImage = (AvatarImageView) findViewById(R.id.contact_photo_image);
|
|
|
|
this.thumbnailView = (ThumbnailView) findViewById(R.id.thumbnail);
|
|
|
|
this.archivedView = ViewUtil.findById(this, R.id.archived);
|
2015-11-12 23:44:25 +01:00
|
|
|
thumbnailView.setClickable(false);
|
2015-11-26 19:04:27 +01:00
|
|
|
|
|
|
|
ViewUtil.setTextViewGravityStart(this.fromView, getContext());
|
|
|
|
ViewUtil.setTextViewGravityStart(this.subjectView, getContext());
|
2011-12-20 19:20:44 +01:00
|
|
|
}
|
2012-07-17 04:56:10 +02:00
|
|
|
|
2015-11-24 00:07:41 +01:00
|
|
|
public void bind(@NonNull MasterSecret masterSecret, @NonNull ThreadRecord thread,
|
|
|
|
@NonNull Locale locale, @NonNull Set<Long> selectedThreads, boolean batchMode)
|
2015-10-16 22:59:40 +02:00
|
|
|
{
|
2013-04-26 03:59:49 +02:00
|
|
|
this.selectedThreads = selectedThreads;
|
|
|
|
this.recipients = thread.getRecipients();
|
|
|
|
this.threadId = thread.getThreadId();
|
|
|
|
this.read = thread.isRead();
|
|
|
|
this.distributionType = thread.getDistributionType();
|
2011-12-20 19:20:44 +01:00
|
|
|
|
2013-01-07 00:46:26 +01:00
|
|
|
this.recipients.addListener(this);
|
2015-05-04 20:36:18 +02:00
|
|
|
this.fromView.setText(recipients, read);
|
2014-02-15 00:59:57 +01:00
|
|
|
|
2015-05-06 22:53:55 +02:00
|
|
|
this.subjectView.setText(thread.getDisplayBody());
|
2015-02-06 03:35:13 +01:00
|
|
|
this.subjectView.setTypeface(read ? LIGHT_TYPEFACE : BOLD_TYPEFACE);
|
|
|
|
|
|
|
|
if (thread.getDate() > 0) {
|
2015-08-07 01:28:51 +02:00
|
|
|
CharSequence date = DateUtils.getBriefRelativeTimeSpanString(getContext(), locale, thread.getDate());
|
2015-02-06 03:35:13 +01:00
|
|
|
dateView.setText(read ? date : color(getResources().getColor(R.color.textsecure_primary), date));
|
|
|
|
dateView.setTypeface(read ? LIGHT_TYPEFACE : BOLD_TYPEFACE);
|
|
|
|
}
|
2011-12-20 19:20:44 +01:00
|
|
|
|
2015-11-24 00:07:41 +01:00
|
|
|
if (thread.isArchived()) {
|
|
|
|
this.archivedView.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
this.archivedView.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2015-11-24 16:06:41 +01:00
|
|
|
setStatusIcons(thread);
|
2015-10-16 22:59:40 +02:00
|
|
|
setThumbnailSnippet(masterSecret, thread);
|
2015-06-02 01:51:14 +02:00
|
|
|
setBatchState(batchMode);
|
2015-08-07 01:28:51 +02:00
|
|
|
setBackground(thread);
|
2015-07-02 23:59:50 +02:00
|
|
|
setRippleColor(recipients);
|
2015-06-14 20:20:19 +02:00
|
|
|
this.contactPhotoImage.setAvatar(recipients, true);
|
2013-01-07 00:46:26 +01:00
|
|
|
}
|
|
|
|
|
2015-09-16 00:28:27 +02:00
|
|
|
@Override
|
2013-01-07 00:46:26 +01:00
|
|
|
public void unbind() {
|
2015-09-16 00:28:27 +02:00
|
|
|
if (this.recipients != null) this.recipients.removeListener(this);
|
2011-12-20 19:20:44 +01:00
|
|
|
}
|
2012-07-17 04:56:10 +02:00
|
|
|
|
2015-06-02 01:51:14 +02:00
|
|
|
private void setBatchState(boolean batch) {
|
|
|
|
setSelected(batch && selectedThreads.contains(threadId));
|
2013-02-09 19:03:38 +01:00
|
|
|
}
|
|
|
|
|
2011-12-20 19:20:44 +01:00
|
|
|
public Recipients getRecipients() {
|
|
|
|
return recipients;
|
|
|
|
}
|
2012-07-17 04:56:10 +02:00
|
|
|
|
2011-12-20 19:20:44 +01:00
|
|
|
public long getThreadId() {
|
|
|
|
return threadId;
|
|
|
|
}
|
2012-07-17 04:56:10 +02:00
|
|
|
|
2015-12-03 21:36:47 +01:00
|
|
|
public boolean getRead() {
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
2013-04-26 03:59:49 +02:00
|
|
|
public int getDistributionType() {
|
|
|
|
return distributionType;
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:59:40 +02:00
|
|
|
private void setThumbnailSnippet(MasterSecret masterSecret, ThreadRecord thread) {
|
|
|
|
if (thread.getSnippetUri() != null) {
|
|
|
|
this.thumbnailView.setVisibility(View.VISIBLE);
|
|
|
|
this.thumbnailView.setImageResource(masterSecret, thread.getSnippetUri());
|
|
|
|
|
|
|
|
LayoutParams subjectParams = (RelativeLayout.LayoutParams)this.subjectView.getLayoutParams();
|
|
|
|
subjectParams.addRule(RelativeLayout.LEFT_OF, R.id.thumbnail);
|
2015-12-23 23:57:03 +01:00
|
|
|
subjectParams.addRule(RelativeLayout.START_OF, R.id.thumbnail);
|
2015-10-16 22:59:40 +02:00
|
|
|
this.subjectView.setLayoutParams(subjectParams);
|
2015-11-24 16:06:41 +01:00
|
|
|
this.post(new ThumbnailPositioner(thumbnailView, archivedView, deliveryStatusIndicator, dateView));
|
2015-10-16 22:59:40 +02:00
|
|
|
} else {
|
|
|
|
this.thumbnailView.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
LayoutParams subjectParams = (RelativeLayout.LayoutParams)this.subjectView.getLayoutParams();
|
2015-11-24 16:06:41 +01:00
|
|
|
subjectParams.addRule(RelativeLayout.LEFT_OF, R.id.delivery_status);
|
2015-12-23 23:57:03 +01:00
|
|
|
subjectParams.addRule(RelativeLayout.START_OF, R.id.delivery_status);
|
2015-10-16 22:59:40 +02:00
|
|
|
this.subjectView.setLayoutParams(subjectParams);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 16:06:41 +01:00
|
|
|
private void setStatusIcons(ThreadRecord thread) {
|
|
|
|
if (!thread.isOutgoing()) {
|
|
|
|
deliveryStatusIndicator.setNone();
|
|
|
|
alertView.setNone();
|
|
|
|
} else if (thread.isFailed()) {
|
|
|
|
deliveryStatusIndicator.setNone();
|
|
|
|
alertView.setFailed();
|
|
|
|
} else if (thread.isPendingInsecureSmsFallback()) {
|
|
|
|
deliveryStatusIndicator.setNone();
|
|
|
|
alertView.setPendingApproval();
|
|
|
|
} else {
|
|
|
|
alertView.setNone();
|
|
|
|
|
|
|
|
if (thread.isPending()) deliveryStatusIndicator.setPending();
|
|
|
|
else if (thread.isDelivered()) deliveryStatusIndicator.setDelivered();
|
|
|
|
else deliveryStatusIndicator.setSent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 01:28:51 +02:00
|
|
|
private void setBackground(ThreadRecord thread) {
|
|
|
|
if (thread.isRead()) setBackgroundResource(readBackground);
|
|
|
|
else setBackgroundResource(unreadBackround);
|
|
|
|
}
|
|
|
|
|
2015-07-02 23:59:50 +02:00
|
|
|
@TargetApi(VERSION_CODES.LOLLIPOP)
|
2015-08-07 01:28:51 +02:00
|
|
|
private void setRippleColor(Recipients recipients) {
|
2015-07-02 23:59:50 +02:00
|
|
|
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
|
|
|
|
((RippleDrawable)(getBackground()).mutate())
|
2015-08-07 01:28:51 +02:00
|
|
|
.setColor(ColorStateList.valueOf(recipients.getColor().toConversationColor(getContext())));
|
2015-07-02 23:59:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-24 17:40:37 +01:00
|
|
|
@Override
|
2015-06-09 16:37:20 +02:00
|
|
|
public void onModified(final Recipients recipients) {
|
2012-12-24 17:40:37 +01:00
|
|
|
handler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-05-04 20:36:18 +02:00
|
|
|
fromView.setText(recipients, read);
|
2015-06-14 20:20:19 +02:00
|
|
|
contactPhotoImage.setAvatar(recipients, true);
|
2015-07-02 23:59:50 +02:00
|
|
|
setRippleColor(recipients);
|
2012-12-24 17:40:37 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-11-24 00:07:41 +01:00
|
|
|
|
2015-11-30 19:36:56 +01:00
|
|
|
private static class ThumbnailPositioner implements Runnable {
|
|
|
|
|
|
|
|
private final View thumbnailView;
|
|
|
|
private final View archivedView;
|
2015-11-24 16:06:41 +01:00
|
|
|
private final View deliveryStatusView;
|
2015-11-30 19:36:56 +01:00
|
|
|
private final View dateView;
|
|
|
|
|
2015-11-24 16:06:41 +01:00
|
|
|
public ThumbnailPositioner(View thumbnailView, View archivedView, View deliveryStatusView, View dateView) {
|
|
|
|
this.thumbnailView = thumbnailView;
|
|
|
|
this.archivedView = archivedView;
|
|
|
|
this.deliveryStatusView = deliveryStatusView;
|
|
|
|
this.dateView = dateView;
|
2015-11-30 19:36:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
LayoutParams thumbnailParams = (RelativeLayout.LayoutParams)thumbnailView.getLayoutParams();
|
|
|
|
|
2015-11-24 16:06:41 +01:00
|
|
|
if (archivedView.getVisibility() == View.VISIBLE &&
|
|
|
|
(archivedView.getWidth() + deliveryStatusView.getWidth()) > dateView.getWidth())
|
|
|
|
{
|
|
|
|
thumbnailParams.addRule(RelativeLayout.LEFT_OF, R.id.delivery_status);
|
2015-12-23 23:57:03 +01:00
|
|
|
thumbnailParams.addRule(RelativeLayout.START_OF, R.id.delivery_status);
|
2015-11-30 19:36:56 +01:00
|
|
|
} else {
|
|
|
|
thumbnailParams.addRule(RelativeLayout.LEFT_OF, R.id.date);
|
2015-12-23 23:57:03 +01:00
|
|
|
thumbnailParams.addRule(RelativeLayout.START_OF, R.id.date);
|
2015-11-30 19:36:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
thumbnailView.setLayoutParams(thumbnailParams);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 19:20:44 +01:00
|
|
|
}
|