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

MessageListItem

public class MessageListItem extends android.widget.LinearLayout implements SlideViewInterface, android.view.View.OnClickListener
This class provides view of a message in the messages list.

Fields Summary
public static final String
EXTRA_URLS
private static final String
TAG
private static final android.text.style.StyleSpan
STYLE_BOLD
static final int
MSG_LIST_EDIT_MMS
static final int
MSG_LIST_EDIT_SMS
private android.view.View
mMsgListItem
private android.view.View
mMmsView
private android.widget.ImageView
mImageView
private android.widget.ImageView
mRightStatusIndicator
private android.widget.ImageButton
mSlideShowButton
private android.widget.TextView
mBodyTextView
private android.widget.Button
mDownloadButton
private android.widget.TextView
mDownloadingLabel
private android.os.Handler
mHandler
private MessageItem
mMessageItem
Constructors Summary
public MessageListItem(android.content.Context context)


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

        super(context, attrs);
    
Methods Summary
public voidbind(MessageItem msgItem)

        mMessageItem = msgItem;

        setLongClickable(false);

        switch (msgItem.mMessageType) {
            case PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND:
                bindNotifInd(msgItem);
                break;
            default:
                bindCommonMessage(msgItem);
                break;
        }
    
private voidbindCommonMessage(MessageItem msgItem)

        if (mDownloadButton != null) {
            mDownloadButton.setVisibility(View.GONE);
            mDownloadingLabel.setVisibility(View.GONE);
        }
        // Since the message text should be concatenated with the sender's
        // address(or name), I have to display it here instead of
        // displaying it by the Presenter.
        mBodyTextView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

        // Get and/or lazily set the formatted message from/on the
        // MessageItem.  Because the MessageItem instances come from a
        // cache (currently of size ~50), the hit rate on avoiding the
        // expensive formatMessage() call is very high.
        CharSequence formattedMessage = msgItem.getCachedFormattedMessage();
        if (formattedMessage == null) {
            formattedMessage = formatMessage(msgItem.mContact, msgItem.mBody,
                                             msgItem.mSubject, msgItem.mTimestamp);
            msgItem.setCachedFormattedMessage(formattedMessage);
        }
        mBodyTextView.setText(formattedMessage);

        if (msgItem.isSms()) {
            hideMmsViewIfNeeded();
        } else {
            Presenter presenter = PresenterFactory.getPresenter(
                    "MmsThumbnailPresenter", mContext,
                    this, msgItem.mSlideshow);
            presenter.present();

            if (msgItem.mAttachmentType != AttachmentEditor.TEXT_ONLY) {
                inflateMmsView();
                mMmsView.setVisibility(View.VISIBLE);
                setOnClickListener(msgItem);
                drawPlaybackButton(msgItem);
            } else {
                hideMmsViewIfNeeded();
            }
        }

        drawLeftStatusIndicator(msgItem.mBoxId);
        drawRightStatusIndicator(msgItem);
    
private voidbindNotifInd(MessageItem msgItem)

        hideMmsViewIfNeeded();

        String msgSizeText = mContext.getString(R.string.message_size_label)
                                + String.valueOf((msgItem.mMessageSize + 1023) / 1024)
                                + mContext.getString(R.string.kilobyte);

        mBodyTextView.setText(formatMessage(msgItem.mContact, null, msgItem.mSubject,
                                            msgSizeText + "\n" + msgItem.mTimestamp));

        int state = DownloadManager.getInstance().getState(msgItem.mMessageUri);
        switch (state) {
            case DownloadManager.STATE_DOWNLOADING:
                inflateDownloadControls();
                mDownloadingLabel.setVisibility(View.VISIBLE);
                mDownloadButton.setVisibility(View.GONE);
                break;
            case DownloadManager.STATE_UNSTARTED:
            case DownloadManager.STATE_TRANSIENT_FAILURE:
            case DownloadManager.STATE_PERMANENT_FAILURE:
            default:
                setLongClickable(true);
                inflateDownloadControls();
                mDownloadingLabel.setVisibility(View.GONE);
                mDownloadButton.setVisibility(View.VISIBLE);
                mDownloadButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        mDownloadingLabel.setVisibility(View.VISIBLE);
                        mDownloadButton.setVisibility(View.GONE);
                        Intent intent = new Intent(mContext, TransactionService.class);
                        intent.putExtra(TransactionBundle.URI, msgItem.mMessageUri.toString());
                        intent.putExtra(TransactionBundle.TRANSACTION_TYPE,
                                Transaction.RETRIEVE_TRANSACTION);
                        mContext.startService(intent);
                    }
                });
                break;
        }

        // Hide the error indicator.
        mRightStatusIndicator.setVisibility(View.GONE);

        drawLeftStatusIndicator(msgItem.mBoxId);
    
private voiddrawLeftStatusIndicator(int msgBoxId)

        switch (msgBoxId) {
            case Mms.MESSAGE_BOX_INBOX:
                mMsgListItem.setBackgroundResource(R.drawable.listitem_background_lightblue);
                break;

            case Mms.MESSAGE_BOX_DRAFTS:
            case Sms.MESSAGE_TYPE_FAILED:
            case Sms.MESSAGE_TYPE_QUEUED:
            case Mms.MESSAGE_BOX_OUTBOX:
                mMsgListItem.setBackgroundResource(R.drawable.listitem_background);
                break;

            default:
                mMsgListItem.setBackgroundResource(R.drawable.listitem_background);
                break;
        }
    
private voiddrawPlaybackButton(MessageItem msgItem)

        switch (msgItem.mAttachmentType) {
            case AttachmentEditor.SLIDESHOW_ATTACHMENT:
            case AttachmentEditor.AUDIO_ATTACHMENT:
            case AttachmentEditor.VIDEO_ATTACHMENT:
                // Show the 'Play' button and bind message info on it.
                mSlideShowButton.setTag(msgItem);
                // Set call-back for the 'Play' button.
                mSlideShowButton.setOnClickListener(this);
                mSlideShowButton.setVisibility(View.VISIBLE);
                setLongClickable(true);
                break;
            default:
                mSlideShowButton.setVisibility(View.GONE);
                break;
        }
    
private voiddrawRightStatusIndicator(MessageItem msgItem)

        if (msgItem.isOutgoingMessage()) {
            if (isFailedMessage(msgItem)) {
                mRightStatusIndicator.setImageResource(R.drawable.ic_sms_mms_not_delivered);
                setErrorIndicatorClickListener(msgItem);
            } else {
                mRightStatusIndicator.setImageResource(R.drawable.ic_email_pending);
            }
            mRightStatusIndicator.setVisibility(View.VISIBLE);
        } else if (msgItem.mDeliveryReport || msgItem.mReadReport) {
            mRightStatusIndicator.setImageResource(R.drawable.ic_mms_message_details);
            mRightStatusIndicator.setVisibility(View.VISIBLE);
        } else {
            mRightStatusIndicator.setVisibility(View.GONE);
        }
    
private java.lang.CharSequenceformatMessage(java.lang.String contact, java.lang.String body, java.lang.String subject, java.lang.String timestamp)

        SpannableStringBuilder buf = new SpannableStringBuilder(contact);
        buf.append(": ");
        buf.setSpan(STYLE_BOLD, 0, buf.length(),
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        boolean hasSubject = !TextUtils.isEmpty(subject);
        if (hasSubject) {
            buf.append(mContext.getResources().getString(R.string.inline_subject, subject));
        }

        if (!TextUtils.isEmpty(body)) {
            if (hasSubject) {
                buf.append(" - ");
            }
            SmileyParser parser = SmileyParser.getInstance();
            buf.append(parser.addSmileySpans(body));
        }

        buf.append("\n");
        int startOffset = buf.length();

        // put a one pixel high spacer line between the message and the time stamp as requested
        // by the spec.
        buf.append("\n");
        buf.setSpan(new AbsoluteSizeSpan(3), startOffset, buf.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        startOffset = buf.length();
        buf.append(timestamp);
        buf.setSpan(new AbsoluteSizeSpan(12), startOffset, buf.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        // Make the timestamp text not as dark
        int color = mContext.getResources().getColor(R.color.timestamp_color);
        buf.setSpan(new ForegroundColorSpan(color), startOffset, buf.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return buf;
    
public MessageItemgetMessageItem()

        return mMessageItem;
    
private voidhideMmsViewIfNeeded()

        if (mMmsView != null) {
            mMmsView.setVisibility(View.GONE);
        }
    
private voidinflateDownloadControls()

        if (mDownloadButton == null) {
            //inflate the download controls
            findViewById(R.id.mms_downloading_view_stub).setVisibility(VISIBLE);
            mDownloadButton = (Button) findViewById(R.id.btn_download_msg);
            mDownloadingLabel = (TextView) findViewById(R.id.label_downloading);
        }
    
private voidinflateMmsView()

        if (mMmsView == null) {
            //inflate the surrounding view_stub
            findViewById(R.id.mms_layout_view_stub).setVisibility(VISIBLE);

            mMmsView = findViewById(R.id.mms_view);
            mImageView = (ImageView) findViewById(R.id.image_view);
            mSlideShowButton = (ImageButton) findViewById(R.id.play_slideshow_button);
        }
    
public static booleanisFailedMessage(MessageItem msgItem)

        boolean isFailedMms = msgItem.isMms()
                            && (msgItem.mErrorType >= MmsSms.ERR_TYPE_GENERIC_PERMANENT);
        boolean isFailedSms = msgItem.isSms()
                            && (msgItem.mBoxId == Sms.MESSAGE_TYPE_FAILED);
        return isFailedMms || isFailedSms;
    
public voidonClick(android.view.View v)

        MessageItem mi = (MessageItem) v.getTag();
        switch (mi.mAttachmentType) {
            case AttachmentEditor.VIDEO_ATTACHMENT:
            case AttachmentEditor.AUDIO_ATTACHMENT:
            case AttachmentEditor.SLIDESHOW_ATTACHMENT:
                MessageUtils.viewMmsMessageAttachment(mContext,
                        mi.mMessageUri, mi.mSlideshow, null /* persister */);
                break;
        }
    
protected voidonFinishInflate()

        super.onFinishInflate();

        mMsgListItem = findViewById(R.id.msg_list_item);
        mBodyTextView = (TextView) findViewById(R.id.text_view);
        mRightStatusIndicator = (ImageView) findViewById(R.id.right_status_indicator);
    
public voidonMessageListItemClick()

        URLSpan[] spans = mBodyTextView.getUrls();

        if (spans.length == 0) {
            // Do nothing.
        } else if (spans.length == 1) {
            Uri uri = Uri.parse(spans[0].getURL());
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);

            mContext.startActivity(intent);
        } else {
            final java.util.ArrayList<String> urls = MessageUtils.extractUris(spans);

            ArrayAdapter<String> adapter = 
                new ArrayAdapter<String>(mContext, android.R.layout.select_dialog_item, urls) {
                public View getView(int position, View convertView, ViewGroup parent) {
                    View v = super.getView(position, convertView, parent);
                    try {
                        String url = getItem(position).toString();
                        TextView tv = (TextView) v;
                        Drawable d = mContext.getPackageManager().getActivityIcon(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                        if (d != null) {
                            d.setBounds(0, 0, d.getIntrinsicHeight(), d.getIntrinsicHeight());
                            tv.setCompoundDrawablePadding(10);
                            tv.setCompoundDrawables(d, null, null, null);
                        }
                        final String telPrefix = "tel:";
                        if (url.startsWith(telPrefix)) {
                            url = PhoneNumberUtils.formatNumber(url.substring(telPrefix.length()));
                        }
                        tv.setText(url);
                    } catch (android.content.pm.PackageManager.NameNotFoundException ex) {
                        ;
                    }
                    return v;
                }
            };

            AlertDialog.Builder b = new AlertDialog.Builder(mContext);

            DialogInterface.OnClickListener click = new DialogInterface.OnClickListener() {
                public final void onClick(DialogInterface dialog, int which) {
                    if (which >= 0) {
                        Uri uri = Uri.parse(urls.get(which));
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        intent.putExtra(Browser.EXTRA_APPLICATION_ID, mContext.getPackageName());
                        mContext.startActivity(intent);
                    }
                }
            };
                
            b.setTitle(R.string.select_link_title);
            b.setCancelable(true);
            b.setAdapter(adapter, click);

            b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                public final void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            b.show();
        }
    
public voidpauseAudio()

        // TODO Auto-generated method stub

    
public voidpauseVideo()

        // TODO Auto-generated method stub

    
public voidreset()

        if (mImageView != null) {
            mImageView.setVisibility(GONE);
        }
    
public voidseekAudio(int seekTo)

        // TODO Auto-generated method stub

    
public voidseekVideo(int seekTo)

        // TODO Auto-generated method stub

    
public voidsetAudio(android.net.Uri audio, java.lang.String name, java.util.Map extras)

        // TODO Auto-generated method stub
    
private voidsetErrorIndicatorClickListener(MessageItem msgItem)

        String type = msgItem.mType;
        final int what;
        if (type.equals("sms")) {
            what = MSG_LIST_EDIT_SMS;
        } else {
            what = MSG_LIST_EDIT_MMS;
        }
        mRightStatusIndicator.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (null != mHandler) {
                    Message msg = Message.obtain(mHandler, what);
                    msg.obj = new Long(msgItem.mMsgId);
                    msg.sendToTarget();
                }
            }
        });
    
public voidsetImage(java.lang.String name, android.graphics.Bitmap bitmap)

        inflateMmsView();

        if (null == bitmap) {
            bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_missing_thumbnail_picture);
        }
        mImageView.setImageBitmap(bitmap);
        mImageView.setVisibility(VISIBLE);
    
public voidsetImageRegionFit(java.lang.String fit)

        // TODO Auto-generated method stub
    
public voidsetImageVisibility(boolean visible)

        // TODO Auto-generated method stub
    
public voidsetMsgListItemHandler(android.os.Handler handler)

        mHandler = handler;
    
private voidsetOnClickListener(MessageItem msgItem)

        switch(msgItem.mAttachmentType) {
        case AttachmentEditor.IMAGE_ATTACHMENT:
        case AttachmentEditor.VIDEO_ATTACHMENT:
            mImageView.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    MessageUtils.viewMmsMessageAttachment(mContext,
                        null /* uri */, msgItem.mSlideshow, null /* persister */);
                }
            });
            mImageView.setOnLongClickListener(new OnLongClickListener() {
                public boolean onLongClick(View v) {
                    return v.showContextMenu();
                }
            });
            break;
            
        default:
            mImageView.setOnClickListener(null);
            break;
        }
    
public voidsetText(java.lang.String name, java.lang.String text)

        // TODO Auto-generated method stub
    
public voidsetTextVisibility(boolean visible)

        // TODO Auto-generated method stub
    
public voidsetVideo(java.lang.String name, android.net.Uri video)

        inflateMmsView();
        Bitmap bitmap = VideoAttachmentView.createVideoThumbnail(mContext, video);
        if (null == bitmap) {
            bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_missing_thumbnail_video);
        }
        mImageView.setImageBitmap(bitmap);
        mImageView.setVisibility(VISIBLE);
    
public voidsetVideoVisibility(boolean visible)

        // TODO Auto-generated method stub
    
public voidsetVisibility(boolean visible)

        // TODO Auto-generated method stub
    
public voidstartAudio()

        // TODO Auto-generated method stub
    
public voidstartVideo()

        // TODO Auto-generated method stub
    
public voidstopAudio()

        // TODO Auto-generated method stub
    
public voidstopVideo()

        // TODO Auto-generated method stub