public SlideModel set(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 SlideModel remove(int location) {
   SlideModel slide = mSlides.remove(location);
   if (slide != null) {
     decreaseMessageSize(slide.getSlideSize());
     slide.unregisterAllModelChangedObservers();
     notifyModelChanged(true);
   }
   return slide;
 }
 public boolean remove(Object object) {
   if ((object != null) && mSlides.remove(object)) {
     SlideModel slide = (SlideModel) object;
     decreaseMessageSize(slide.getSlideSize());
     slide.unregisterAllModelChangedObservers();
     notifyModelChanged(true);
     return true;
   }
   return false;
 }
  public void add(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);
    }
  }
  //
  // Implement List<E> interface.
  //
  public boolean add(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;
  }
  private SlideshowModel(
      LayoutModel layouts,
      ArrayList<SlideModel> slides,
      SMILDocument documentCache,
      PduBody pbCache,
      ContentResolver contentResolver) {
    mLayout = layouts;
    mSlides = slides;
    mContentResolver = contentResolver;

    mDocumentCache = documentCache;
    mPduBodyCache = pbCache;
    for (SlideModel slide : mSlides) {
      increaseMessageSize(slide.getSlideSize());
      slide.setParent(this);
    }
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case MENU_PREVIEW_SLIDESHOW:
        previewSlideshow();
        break;

      case MENU_REMOVE_TEXT:
        SlideModel slide = mSlideshowModel.get(mPosition);
        if (slide != null) {
          slide.removeText();
        }
        break;

      case MENU_ADD_PICTURE:
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType(ContentType.IMAGE_UNSPECIFIED);
        startActivityForResult(intent, REQUEST_CODE_CHANGE_PICTURE);
        break;

      case MENU_TAKE_PICTURE:
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // We have to pass a uri to store the picture data, otherwise the camera will return
        // a very small image bitmap.
        intent.putExtra(MediaStore.EXTRA_OUTPUT, TempFileProvider.SCRAP_CONTENT_URI);
        startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
        break;

      case MENU_DEL_PICTURE:
        mSlideshowEditor.removeImage(mPosition);
        setReplaceButtonText(R.string.add_picture);
        break;

      case MENU_ADD_AUDIO:
        MessageUtils.selectAudio(this, REQUEST_CODE_CHANGE_MUSIC);
        break;

      case MENU_RECORD_SOUND:
        slide = mSlideshowModel.get(mPosition);
        int currentSlideSize = slide.getSlideSize();
        long sizeLimit =
            ComposeMessageActivity.computeAttachmentSizeLimit(mSlideshowModel, currentSlideSize);
        MessageUtils.recordSound(this, REQUEST_CODE_RECORD_SOUND, sizeLimit);
        break;

      case MENU_DEL_AUDIO:
        mSlideshowEditor.removeAudio(mPosition);
        break;

      case MENU_ADD_VIDEO:
        intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType(ContentType.VIDEO_UNSPECIFIED);
        intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
        startActivityForResult(intent, REQUEST_CODE_CHANGE_VIDEO);
        break;

      case MENU_TAKE_VIDEO:
        slide = mSlideshowModel.get(mPosition);
        currentSlideSize = slide.getSlideSize();
        sizeLimit =
            ComposeMessageActivity.computeAttachmentSizeLimit(mSlideshowModel, currentSlideSize);
        if (sizeLimit > 0) {
          MessageUtils.recordVideo(this, REQUEST_CODE_TAKE_VIDEO, sizeLimit);
        } else {
          Toast.makeText(this, getString(R.string.message_too_big_for_video), Toast.LENGTH_SHORT)
              .show();
        }
        break;

      case MENU_DEL_VIDEO:
        mSlideshowEditor.removeVideo(mPosition);
        break;

      case MENU_ADD_SLIDE:
        mPosition++;
        if (mSlideshowEditor.addNewSlide(mPosition)) {
          // add successfully
          showCurrentSlide();
        } else {
          // move position back
          mPosition--;
          Toast.makeText(this, R.string.cannot_add_slide_anymore, Toast.LENGTH_SHORT).show();
        }
        break;

      case MENU_LAYOUT:
        showLayoutSelectorDialog();
        break;

      case MENU_DURATION:
        showDurationDialog();
        break;
    }

    return true;
  }