session-android/src/org/thoughtcrime/securesms/ConversationListItem.java

197 lines
7.0 KiB
Java
Raw Normal View History

2012-07-17 02:56:10 +00:00
/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
2012-07-17 02:56:10 +00:00
*
2011-12-20 18:20:44 +00: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 02:56:10 +00:00
*
2011-12-20 18:20:44 +00: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;
import android.annotation.TargetApi;
2011-12-20 18:20:44 +00:00
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.graphics.drawable.RippleDrawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
2011-12-20 18:20:44 +00:00
import android.util.AttributeSet;
import android.view.View;
2011-12-20 18:20:44 +00:00
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.thoughtcrime.securesms.components.AvatarImageView;
import org.thoughtcrime.securesms.components.FromTextView;
import org.thoughtcrime.securesms.components.ThumbnailView;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.model.ThreadRecord;
2012-07-17 02:56:10 +00:00
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.DateUtils;
import org.thoughtcrime.securesms.util.ResUtil;
2012-07-17 02:56:10 +00:00
import java.util.Locale;
2012-07-17 02:56:10 +00:00
import java.util.Set;
import static org.thoughtcrime.securesms.util.SpanUtil.color;
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00: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
*/
public class ConversationListItem extends RelativeLayout
implements Recipients.RecipientsModifiedListener, Unbindable
{
private final static String TAG = ConversationListItem.class.getSimpleName();
2011-12-20 18:20:44 +00: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);
private Set<Long> selectedThreads;
private Recipients recipients;
private long threadId;
private TextView subjectView;
private FromTextView fromView;
private TextView dateView;
private boolean read;
private AvatarImageView contactPhotoImage;
private ThumbnailView thumbnailView;
2012-07-17 02:56:10 +00:00
private final @DrawableRes int readBackground;
private final @DrawableRes int unreadBackround;
private final Handler handler = new Handler();
2013-04-26 01:59:49 +00:00
private int distributionType;
public ConversationListItem(Context context) {
this(context, null);
}
2011-12-20 18:20:44 +00:00
public ConversationListItem(Context context, AttributeSet attrs) {
super(context, attrs);
readBackground = ResUtil.getDrawableRes(context, R.attr.conversation_list_item_background_read);
unreadBackround = ResUtil.getDrawableRes(context, R.attr.conversation_list_item_background_unread);
}
2012-07-17 02:56:10 +00:00
@Override
protected void onFinishInflate() {
super.onFinishInflate();
this.subjectView = (TextView) findViewById(R.id.subject);
this.fromView = (FromTextView) findViewById(R.id.from);
this.dateView = (TextView) findViewById(R.id.date);
this.contactPhotoImage = (AvatarImageView) findViewById(R.id.contact_photo_image);
this.thumbnailView = (ThumbnailView) findViewById(R.id.thumbnail);
this.thumbnailView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ConversationListItem.this.performClick();
}
});
2011-12-20 18:20:44 +00:00
}
2012-07-17 02:56:10 +00:00
public void set(@NonNull MasterSecret masterSecret, @NonNull ThreadRecord thread,
@NonNull Locale locale, @NonNull Set<Long> selectedThreads, boolean batchMode)
{
2013-04-26 01:59:49 +00:00
this.selectedThreads = selectedThreads;
this.recipients = thread.getRecipients();
this.threadId = thread.getThreadId();
this.read = thread.isRead();
this.distributionType = thread.getDistributionType();
2011-12-20 18:20:44 +00:00
this.recipients.addListener(this);
this.fromView.setText(recipients, read);
this.subjectView.setText(thread.getDisplayBody());
this.subjectView.setTypeface(read ? LIGHT_TYPEFACE : BOLD_TYPEFACE);
if (thread.getDate() > 0) {
CharSequence date = DateUtils.getBriefRelativeTimeSpanString(getContext(), locale, thread.getDate());
dateView.setText(read ? date : color(getResources().getColor(R.color.textsecure_primary), date));
dateView.setTypeface(read ? LIGHT_TYPEFACE : BOLD_TYPEFACE);
}
2011-12-20 18:20:44 +00:00
setThumbnailSnippet(masterSecret, thread);
setBatchState(batchMode);
setBackground(thread);
setRippleColor(recipients);
this.contactPhotoImage.setAvatar(recipients, true);
}
@Override
public void unbind() {
if (this.recipients != null) this.recipients.removeListener(this);
2011-12-20 18:20:44 +00:00
}
2012-07-17 02:56:10 +00:00
private void setBatchState(boolean batch) {
setSelected(batch && selectedThreads.contains(threadId));
}
2011-12-20 18:20:44 +00:00
public Recipients getRecipients() {
return recipients;
}
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
public long getThreadId() {
return threadId;
}
2012-07-17 02:56:10 +00:00
2013-04-26 01:59:49 +00:00
public int getDistributionType() {
return distributionType;
}
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);
this.subjectView.setLayoutParams(subjectParams);
} else {
this.thumbnailView.setVisibility(View.GONE);
LayoutParams subjectParams = (RelativeLayout.LayoutParams)this.subjectView.getLayoutParams();
subjectParams.addRule(RelativeLayout.LEFT_OF, 0);
this.subjectView.setLayoutParams(subjectParams);
}
}
private void setBackground(ThreadRecord thread) {
if (thread.isRead()) setBackgroundResource(readBackground);
else setBackgroundResource(unreadBackround);
}
@TargetApi(VERSION_CODES.LOLLIPOP)
private void setRippleColor(Recipients recipients) {
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
((RippleDrawable)(getBackground()).mutate())
.setColor(ColorStateList.valueOf(recipients.getColor().toConversationColor(getContext())));
}
}
@Override
public void onModified(final Recipients recipients) {
handler.post(new Runnable() {
@Override
public void run() {
fromView.setText(recipients, read);
contactPhotoImage.setAvatar(recipients, true);
setRippleColor(recipients);
}
});
}
2011-12-20 18:20:44 +00:00
}