FileDocCategorySizeDatePackage
SlideshowModel.javaAPI DocAndroid 1.5 API19779Wed May 06 22:42:46 BST 2009com.android.mms.model

SlideshowModel

public class SlideshowModel extends Model implements IModelChangedObserver, List

Fields Summary
private static final String
TAG
private final LayoutModel
mLayout
private final ArrayList
mSlides
private org.w3c.dom.smil.SMILDocument
mDocumentCache
private com.google.android.mms.pdu.PduBody
mPduBodyCache
private int
mCurrentMessageSize
private android.content.ContentResolver
mContentResolver
Constructors Summary
private SlideshowModel(android.content.ContentResolver contentResolver)


       
        mLayout = new LayoutModel();
        mSlides = new ArrayList<SlideModel>();
        mContentResolver = contentResolver;
    
private SlideshowModel(LayoutModel layouts, ArrayList slides, org.w3c.dom.smil.SMILDocument documentCache, com.google.android.mms.pdu.PduBody pbCache, android.content.ContentResolver contentResolver)

        mLayout = layouts;
        mSlides = slides;
        mContentResolver = contentResolver;

        mDocumentCache = documentCache;
        mPduBodyCache = pbCache;
        for (SlideModel slide : mSlides) {
            increaseMessageSize(slide.getSlideSize());
            slide.setParent(this);
        }
    
Methods Summary
public booleanadd(SlideModel object)

        int increaseSize = object.getSlideSize();
        checkMessageSize(increaseSize);

        if ((object != null) && mSlides.add(object)) {
            increaseMessageSize(increaseSize);
            object.registerModelChangedObserver(this);
            for (IModelChangedObserver observer : mModelChangedObservers) {
                object.registerModelChangedObserver(observer);
            }
            notifyModelChanged(true);
            return true;
        }
        return false;
    
public voidadd(int location, SlideModel object)

        if (object != null) {
            int increaseSize = object.getSlideSize();
            checkMessageSize(increaseSize);

            mSlides.add(location, object);
            increaseMessageSize(increaseSize);
            object.registerModelChangedObserver(this);
            for (IModelChangedObserver observer : mModelChangedObservers) {
                object.registerModelChangedObserver(observer);
            }
            notifyModelChanged(true);
        }
    
public booleanaddAll(java.util.Collection collection)

        throw new UnsupportedOperationException("Operation not supported.");
    
public booleanaddAll(int location, java.util.Collection collection)

        throw new UnsupportedOperationException("Operation not supported.");
    
public voidcheckMessageSize(int increaseSize)

        ContentRestriction cr = ContentRestrictionFactory.getContentRestriction();
        cr.checkMessageSize(mCurrentMessageSize, increaseSize, mContentResolver);
    
public voidclear()

        if (mSlides.size() > 0) {
            for (SlideModel slide : mSlides) {
                slide.unregisterModelChangedObserver(this);
                for (IModelChangedObserver observer : mModelChangedObservers) {
                    slide.unregisterModelChangedObserver(observer);
                }
            }
            mCurrentMessageSize = 0;
            mSlides.clear();
            notifyModelChanged(true);
        }
    
public booleancontains(java.lang.Object object)

        return mSlides.contains(object);
    
public booleancontainsAll(java.util.Collection collection)

        return mSlides.containsAll(collection);
    
public static com.android.mms.model.SlideshowModelcreateFromMessageUri(android.content.Context context, android.net.Uri uri)

        return createFromPduBody(context, getPduBody(context, uri));
    
public static com.android.mms.model.SlideshowModelcreateFromPduBody(android.content.Context context, com.google.android.mms.pdu.PduBody pb)

        SMILDocument document = SmilHelper.getDocument(pb);

        // Create root-layout model.
        SMILLayoutElement sle = document.getLayout();
        SMILRootLayoutElement srle = sle.getRootLayout();
        int w = srle.getWidth();
        int h = srle.getHeight();
        if ((w == 0) || (h == 0)) {
            w = LayoutManager.getInstance().getLayoutParameters().getWidth();
            h = LayoutManager.getInstance().getLayoutParameters().getHeight();
            srle.setWidth(w);
            srle.setHeight(h);
        }
        RegionModel rootLayout = new RegionModel(
                null, 0, 0, w, h);

        // Create region models.
        ArrayList<RegionModel> regions = new ArrayList<RegionModel>();
        NodeList nlRegions = sle.getRegions();
        int regionsNum = nlRegions.getLength();

        for (int i = 0; i < regionsNum; i++) {
            SMILRegionElement sre = (SMILRegionElement) nlRegions.item(i);
            RegionModel r = new RegionModel(sre.getId(), sre.getFit(),
                    sre.getLeft(), sre.getTop(), sre.getWidth(), sre.getHeight(),
                    sre.getBackgroundColor());
            regions.add(r);
        }
        LayoutModel layouts = new LayoutModel(rootLayout, regions);

        // Create slide models.
        SMILElement docBody = document.getBody();
        NodeList slideNodes = docBody.getChildNodes();
        int slidesNum = slideNodes.getLength();
        ArrayList<SlideModel> slides = new ArrayList<SlideModel>(slidesNum);

        for (int i = 0; i < slidesNum; i++) {
            // FIXME: This is NOT compatible with the SMILDocument which is
            // generated by some other mobile phones.
            SMILParElement par = (SMILParElement) slideNodes.item(i);

            // Create media models for each slide.
            NodeList mediaNodes = par.getChildNodes();
            int mediaNum = mediaNodes.getLength();
            ArrayList<MediaModel> mediaSet = new ArrayList<MediaModel>(mediaNum);

            for (int j = 0; j < mediaNum; j++) {
                SMILMediaElement sme = (SMILMediaElement) mediaNodes.item(j);
                try {
                    MediaModel media = MediaModelFactory.getMediaModel(
                            context, sme, layouts, pb);
                    SmilHelper.addMediaElementEventListeners(
                            (EventTarget) sme, media);
                    mediaSet.add(media);
                } catch (DrmException e) {
                    Log.e(TAG, e.getMessage(), e);
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage(), e);
                } catch (IllegalArgumentException e) {
                    Log.e(TAG, e.getMessage(), e);
                }
            }

            SlideModel slide = new SlideModel((int) (par.getDur() * 1000), mediaSet);
            slide.setFill(par.getFill());
            SmilHelper.addParElementEventListeners((EventTarget) par, slide);
            slides.add(slide);
        }

        SlideshowModel slideshow = new SlideshowModel(layouts, slides, document, pb,
                context.getContentResolver());
        slideshow.registerModelChangedObserver(slideshow);
        return slideshow;
    
public static com.android.mms.model.SlideshowModelcreateNew(android.content.Context context)

        return new SlideshowModel(context.getContentResolver());
    
public voiddecreaseMessageSize(int decreaseSize)

        if (decreaseSize > 0) {
            mCurrentMessageSize -= decreaseSize;
        }
    
public SlideModelget(int location)

        return mSlides.get(location);
    
public intgetCurrentMessageSize()

        return mCurrentMessageSize;
    
public LayoutModelgetLayout()

        return mLayout;
    
public static com.google.android.mms.pdu.PduBodygetPduBody(android.content.Context context, android.net.Uri msg)

        PduPersister p = PduPersister.getPduPersister(context);
        GenericPdu pdu = p.load(msg);

        int msgType = pdu.getMessageType();
        if ((msgType == PduHeaders.MESSAGE_TYPE_SEND_REQ)
                || (msgType == PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF)) {
            return ((MultimediaMessagePdu) pdu).getBody();
        } else {
            throw new MmsException();
        }
    
public voidincreaseMessageSize(int increaseSize)

        if (increaseSize > 0) {
            mCurrentMessageSize += increaseSize;
        }
    
public intindexOf(java.lang.Object object)

        return mSlides.indexOf(object);
    
public booleanisEmpty()

        return mSlides.isEmpty();
    
public booleanisSimple()
Determines whether this is a "simple" slideshow. Criteria: - Exactly one slide - Exactly one multimedia attachment, but no audio - It can optionally have a caption

        // There must be one (and only one) slide.
        if (size() != 1)
            return false;
        
        SlideModel slide = get(0);
        // The slide must have either an image or video, but not both.
        if (!(slide.hasImage() ^ slide.hasVideo()))
            return false;
     
        // No audio allowed.
        if (slide.hasAudio())
            return false;
        
        return true;
    
public java.util.Iteratoriterator()

        return mSlides.iterator();
    
public intlastIndexOf(java.lang.Object object)

        return mSlides.lastIndexOf(object);
    
public java.util.ListIteratorlistIterator()

        return mSlides.listIterator();
    
public java.util.ListIteratorlistIterator(int location)

        return mSlides.listIterator(location);
    
public com.google.android.mms.pdu.PduBodymakeCopy(android.content.Context context)

        return makePduBody(context, SmilHelper.getDocument(this), true);
    
private com.google.android.mms.pdu.PduBodymakePduBody(org.w3c.dom.smil.SMILDocument document)

        return makePduBody(null, document, false);
    
private com.google.android.mms.pdu.PduBodymakePduBody(android.content.Context context, org.w3c.dom.smil.SMILDocument document, boolean isMakingCopy)

        PduBody pb = new PduBody();

        boolean hasForwardLock = false;
        for (SlideModel slide : mSlides) {
            for (MediaModel media : slide) {
                if (isMakingCopy) {
                    if (media.isDrmProtected() && !media.isAllowedToForward()) {
                        hasForwardLock = true;
                        continue;
                    }
                }

                PduPart part = new PduPart();

                if (media.isText()) {
                    TextModel text = (TextModel) media;
                    // Don't create empty text part.
                    if (TextUtils.isEmpty(text.getText())) {
                        continue;
                    }
                    // Set Charset if it's a text media.
                    part.setCharset(text.getCharset());
                }

                // Set Content-Type.
                part.setContentType(media.getContentType().getBytes());

                String src = media.getSrc();
                String location;
                boolean startWithContentId = src.startsWith("cid:");
                if (startWithContentId) {
                    location = src.substring("cid:".length());
                } else {
                    location = src;
                }

                // Set Content-Location.
                part.setContentLocation(location.getBytes());

                // Set Content-Id.
                if (startWithContentId) {
                    //Keep the original Content-Id.
                    part.setContentId(location.getBytes());
                }
                else {
                    int index = location.lastIndexOf(".");
                    String contentId = (index == -1) ? location
                            : location.substring(0, index);
                    part.setContentId(contentId.getBytes());
                }

                if (media.isDrmProtected()) {
                    DrmWrapper wrapper = media.getDrmObject();
                    part.setDataUri(wrapper.getOriginalUri());
                    part.setData(wrapper.getOriginalData());
                } else if (media.isText()) {
                    part.setData(((TextModel) media).getText().getBytes());
                } else if (media.isImage() || media.isVideo() || media.isAudio()) {
                    part.setDataUri(media.getUri());
                } else {
                    Log.w(TAG, "Unsupport media: " + media);
                }

                pb.addPart(part);
            }
        }

        if (hasForwardLock && isMakingCopy && context != null) {
            Toast.makeText(context,
                    context.getString(R.string.cannot_forward_drm_obj),
                    Toast.LENGTH_LONG).show();
            document = SmilHelper.getDocument(pb);
        }

        // Create and insert SMIL part(as the first part) into the PduBody.
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        SmilXmlSerializer.serialize(document, out);
        PduPart smilPart = new PduPart();
        smilPart.setContentId("smil".getBytes());
        smilPart.setContentLocation("smil.xml".getBytes());
        smilPart.setContentType(ContentType.APP_SMIL.getBytes());
        smilPart.setData(out.toByteArray());
        pb.addPart(0, smilPart);

        return pb;
    
public voidonModelChanged(Model model, boolean dataChanged)

        if (dataChanged) {
            mDocumentCache = null;
            mPduBodyCache = null;
        }
    
public voidprepareForSend()
Make sure the text in slide 0 is no longer holding onto a reference to the text in the message text box.

        if (size() == 1) {
            TextModel text = get(0).getText();
            if (text != null) {
                text.cloneText();
            }
        }
    
protected voidregisterModelChangedObserverInDescendants(IModelChangedObserver observer)

        mLayout.registerModelChangedObserver(observer);

        for (SlideModel slide : mSlides) {
            slide.registerModelChangedObserver(observer);
        }
    
public booleanremove(java.lang.Object object)

        if ((object != null) && mSlides.remove(object)) {
            SlideModel slide = (SlideModel) object;
            decreaseMessageSize(slide.getSlideSize());
            slide.unregisterAllModelChangedObservers();
            notifyModelChanged(true);
            return true;
        }
        return false;
    
public SlideModelremove(int location)

        SlideModel slide = mSlides.remove(location);
        if (slide != null) {
            decreaseMessageSize(slide.getSlideSize());
            slide.unregisterAllModelChangedObservers();
            notifyModelChanged(true);
        }
        return slide;
    
public booleanremoveAll(java.util.Collection collection)

        throw new UnsupportedOperationException("Operation not supported.");
    
public booleanretainAll(java.util.Collection collection)

        throw new UnsupportedOperationException("Operation not supported.");
    
public SlideModelset(int location, SlideModel object)

        SlideModel slide = mSlides.get(location);
        if (null != object) {
            int removeSize = 0;
            int addSize = object.getSlideSize();
            if (null != slide) {
                removeSize = slide.getSlideSize();
            }
            if (addSize > removeSize) {
                checkMessageSize(addSize - removeSize);
                increaseMessageSize(addSize - removeSize);
            } else {
                decreaseMessageSize(removeSize - addSize);
            }
        }

        slide =  mSlides.set(location, object);
        if (slide != null) {
            slide.unregisterAllModelChangedObservers();
        }

        if (object != null) {
            object.registerModelChangedObserver(this);
            for (IModelChangedObserver observer : mModelChangedObservers) {
                object.registerModelChangedObserver(observer);
            }
        }

        notifyModelChanged(true);
        return slide;
    
public voidsetCurrentMessageSize(int size)

        mCurrentMessageSize = size;
    
public intsize()

        return mSlides.size();
    
public java.util.ListsubList(int start, int end)

        return mSlides.subList(start, end);
    
public voidsync(com.google.android.mms.pdu.PduBody pb)

        for (SlideModel slide : mSlides) {
            for (MediaModel media : slide) {
                PduPart part = pb.getPartByContentLocation(media.getSrc());
                if (part != null) {
                    media.setUri(part.getDataUri());
                }
            }
        }
    
public java.lang.Object[]toArray()

        return mSlides.toArray();
    
public T[]toArray(T[] array)

        return mSlides.toArray(array);
    
public com.google.android.mms.pdu.PduBodytoPduBody()

        if (mPduBodyCache == null) {
            mDocumentCache = SmilHelper.getDocument(this);
            mPduBodyCache = makePduBody(mDocumentCache);
        }
        return mPduBodyCache;
    
public org.w3c.dom.smil.SMILDocumenttoSmilDocument()

        if (mDocumentCache == null) {
            mDocumentCache = SmilHelper.getDocument(this);
        }
        return mDocumentCache;
    
protected voidunregisterAllModelChangedObserversInDescendants()

        mLayout.unregisterAllModelChangedObservers();

        for (SlideModel slide : mSlides) {
            slide.unregisterAllModelChangedObservers();
        }
    
protected voidunregisterModelChangedObserverInDescendants(IModelChangedObserver observer)

        mLayout.unregisterModelChangedObserver(observer);

        for (SlideModel slide : mSlides) {
            slide.unregisterModelChangedObserver(observer);
        }