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

156 lines
5.4 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.content.Context;
import android.text.Spannable;
import android.text.format.DateUtils;
import android.text.style.StyleSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
2012-07-17 03:20:13 +00:00
import android.widget.QuickContactBadge;
2011-12-20 18:20:44 +00:00
import android.widget.RelativeLayout;
import android.widget.TextView;
2012-07-17 02:56:10 +00:00
import org.thoughtcrime.securesms.database.MessageRecord;
import org.thoughtcrime.securesms.recipients.Recipients;
import java.util.Set;
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 ConversationHeaderView extends RelativeLayout {
private final Context context;
private Set<Long> selectedThreads;
2012-07-17 02:56:10 +00:00
2012-07-17 03:20:13 +00:00
private Recipients recipients;
private long threadId;
private boolean first;
private TextView subjectView;
private TextView fromView;
private TextView dateView;
private View unreadIndicator;
private View keyIndicator;
private CheckBox checkbox;
private QuickContactBadge contactPhoto;
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
public ConversationHeaderView(Context context, boolean first) {
this(context, (Set<Long>)null);
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
this.first = true;
contactPhoto.setVisibility(View.GONE);
}
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
public ConversationHeaderView(Context context, Set<Long> selectedThreads) {
super(context);
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
li.inflate(R.layout.conversation_header_view, this, true);
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
this.context = context;
this.selectedThreads = selectedThreads;
this.subjectView = (TextView)findViewById(R.id.subject);
this.fromView = (TextView)findViewById(R.id.from);
this.dateView = (TextView)findViewById(R.id.date);
this.unreadIndicator = findViewById(R.id.unread_indicator);
this.keyIndicator = findViewById(R.id.key_indicator);
2012-07-17 03:20:13 +00:00
this.contactPhoto = (QuickContactBadge)findViewById(R.id.contact_photo);
2011-12-20 18:20:44 +00:00
this.checkbox = (CheckBox)findViewById(R.id.checkbox);
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
intializeListeners();
}
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
public ConversationHeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public void set(MessageRecord message, boolean batchMode) {
this.recipients = message.getRecipients();
this.threadId = message.getThreadId();
this.fromView.setText(formatFrom(recipients, message.getCount()));
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
if (message.isKeyExchange())
this.subjectView.setText("Key exchange message...", TextView.BufferType.SPANNABLE);
else
this.subjectView.setText(message.getBody(), TextView.BufferType.SPANNABLE);
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
if (message.getEmphasis())
((Spannable)this.subjectView.getText()).setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, this.subjectView.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
2012-07-17 02:56:10 +00:00
if (message.getDate() > 0)
2011-12-20 18:20:44 +00:00
this.dateView.setText(DateUtils.getRelativeTimeSpanString(getContext(), message.getDate(), false));
2012-07-17 02:56:10 +00:00
if (selectedThreads != null)
2011-12-20 18:20:44 +00:00
this.checkbox.setChecked(selectedThreads.contains(threadId));
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
clearIndicators();
2012-07-17 02:56:10 +00:00
setIndicators(message.getRead(), message.isKeyExchange());
2011-12-20 18:20:44 +00:00
if (!first) {
if (batchMode) checkbox.setVisibility(View.VISIBLE);
2012-07-17 02:56:10 +00:00
else checkbox.setVisibility(View.GONE);
2011-12-20 18:20:44 +00:00
2012-07-17 03:20:13 +00:00
contactPhoto.setImageBitmap(this.recipients.getPrimaryRecipient().getContactPhoto());
contactPhoto.assignContactFromPhone(this.recipients.getPrimaryRecipient().getNumber(), true);
contactPhoto.setVisibility(View.VISIBLE);
2011-12-20 18:20:44 +00:00
}
}
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
private void intializeListeners() {
checkbox.setOnCheckedChangeListener(new CheckedChangedListener());
}
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
private void clearIndicators() {
this.keyIndicator.setVisibility(View.INVISIBLE);
this.unreadIndicator.setVisibility(View.INVISIBLE);
}
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
private void setIndicators(boolean read, boolean key) {
if (!read && key) this.keyIndicator.setVisibility(View.VISIBLE);
else if (!read) this.unreadIndicator.setVisibility(View.VISIBLE);
}
2012-07-17 02:56:10 +00:00
2011-12-20 18:20:44 +00:00
private String formatFrom(Recipients from, long count) {
return from.toShortString() + (count > 0 ? " (" + count + ")" : "");
}
2012-07-17 02:56:10 +00:00
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
2011-12-20 18:20:44 +00:00
private class CheckedChangedListener implements CompoundButton.OnCheckedChangeListener {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) selectedThreads.add(threadId);
else selectedThreads.remove(threadId);
}
}
}