FileDocCategorySizeDatePackage
ConversationHeaderView.javaAPI DocAndroid 1.5 API8112Wed May 06 22:42:46 BST 2009com.android.mms.ui

ConversationHeaderView

public class ConversationHeaderView extends android.widget.RelativeLayout
This class manages the view for given conversation.

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private android.widget.TextView
mSubjectView
private android.widget.TextView
mFromView
private android.widget.TextView
mDateView
private android.view.View
mAttachmentView
private android.view.View
mUnreadIndicator
private android.view.View
mErrorIndicator
private android.widget.ImageView
mPresenceView
private android.os.Handler
mHandler
private final Object
mConversationHeaderLock
private ConversationHeader
mConversationHeader
private static final android.text.style.StyleSpan
STYLE_BOLD
Constructors Summary
public ConversationHeaderView(android.content.Context context)


       
        super(context);
    
public ConversationHeaderView(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);
    
Methods Summary
public final voidbind(android.content.Context context, ConversationHeader ch)

        if (DEBUG) Log.v(TAG, "bind()");

        ConversationHeader oldHeader = getConversationHeader();
        setConversationHeader(ch);

        LayoutParams attachmentLayout = (LayoutParams)mAttachmentView.getLayoutParams();
        boolean hasError = ch.hasError();
        // When there's an error icon, the attachment icon is left of the error icon.
        // When there is not an error icon, the attachment icon is left of the date text.
        // As far as I know, there's no way to specify that relationship in xml.
        if (hasError) {
            attachmentLayout.addRule(RelativeLayout.LEFT_OF, R.id.error);
        } else {
            attachmentLayout.addRule(RelativeLayout.LEFT_OF, R.id.date);
        }

        boolean hasAttachment = ch.hasAttachment();
        mAttachmentView.setVisibility(hasAttachment ? VISIBLE : GONE);

        // Date
        mDateView.setText(ch.getDate());

        // From.
        mFromView.setText(formatMessage(ch));

        // The From above may be incomplete (still loading), so we register ourselves
        // as a callback later to get woken up in onHeaderLoaded() when it changes.
        if (ch.getFrom() == null) {
            ch.setWaitingView(this);
        }

        mUnreadIndicator.setVisibility(ch.isRead() ? INVISIBLE : VISIBLE);

        // Subject
        mSubjectView.setText(ch.getSubject());
        LayoutParams subjectLayout = (LayoutParams)mSubjectView.getLayoutParams();
        // We have to make the subject left of whatever optional items are shown on the right.
        subjectLayout.addRule(RelativeLayout.LEFT_OF, hasAttachment ? R.id.attachment :
            (hasError ? R.id.error : R.id.date));

        // Transmission error indicator.
        mErrorIndicator.setVisibility(hasError ? VISIBLE : GONE);
    
public voidbind(java.lang.String title, java.lang.String explain)
Only used for header binding.

        mFromView.setText(title);
        mSubjectView.setText(explain);
    
private java.lang.CharSequenceformatMessage(ConversationHeader ch)

        final int size = android.R.style.TextAppearance_Small;
        final int color = android.R.styleable.Theme_textColorSecondary;
        String from = ch.getFrom();
        if (from == null) {
            // The temporary text users see while the names of contacts are loading.
            // TODO: evaluate a better or prettier solution for this?
            from = "...";
        }

        SpannableStringBuilder buf = new SpannableStringBuilder(from);

        if (ch.getMessageCount() > 1) {
            buf.append(" (" + ch.getMessageCount() + ") ");
        }

        int before = buf.length();
        if (ch.hasDraft()) {
            buf.append(" ");
            buf.append(mContext.getResources().getString(R.string.has_draft));
            buf.setSpan(new TextAppearanceSpan(mContext, size, color), before,
                    buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            buf.setSpan(new ForegroundColorSpan(
                    mContext.getResources().getColor(R.drawable.text_color_red)),
                    before, buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        
        // Unread messages are shown in bold
        if (!ch.isRead()) {
            buf.setSpan(STYLE_BOLD, 0, buf.length(),
                    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        return buf;
    
public ConversationHeadergetConversationHeader()

        synchronized (mConversationHeaderLock) {
            return mConversationHeader;
        }
    
protected voidonFinishInflate()

        super.onFinishInflate();

        mFromView = (TextView) findViewById(R.id.from);
        mSubjectView = (TextView) findViewById(R.id.subject);

        mDateView = (TextView) findViewById(R.id.date);
        mAttachmentView = findViewById(R.id.attachment);
        mUnreadIndicator = findViewById(R.id.unread_indicator);
        mErrorIndicator = findViewById(R.id.error);
        mPresenceView = (ImageView) findViewById(R.id.presence);
    
public voidonHeaderLoaded(ConversationHeader newHeader)

        synchronized (mConversationHeaderLock) {
            if (mConversationHeader != newHeader) {
                // The user scrolled away before the item loaded and
                // this view has been repurposed.
                return;
            }

            // TODO: as an optimization, send a message to mHandler instead
            // of posting a Runnable.
            mHandler.post(new Runnable() {
                    public void run() {
                        synchronized (mConversationHeaderLock) {
                            if (mConversationHeader == newHeader) {
                                mFromView.setText(formatMessage(newHeader));
                                setPresenceIcon(newHeader.getPresenceResourceId());

                            }
                        }
                    }
                });
        }
    
private voidsetConversationHeader(ConversationHeader header)

        synchronized (mConversationHeaderLock) {
            mConversationHeader = header;
        }
    
public voidsetPresenceIcon(int iconId)

        if (iconId == 0) {
            mPresenceView.setVisibility(View.GONE);            
        } else {
            mPresenceView.setImageResource(iconId);
            mPresenceView.setVisibility(View.VISIBLE);
        }