public void increaseMessageSize(int increaseSize) {
   if ((increaseSize > 0) && (null != mParent)) {
     int size = mParent.getCurrentMessageSize();
     size += increaseSize;
     mParent.setCurrentMessageSize(size);
   }
 }
  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    menu.clear();

    int position = mList.getSelectedItemPosition();
    if ((position >= 0) && (position != (mList.getCount() - 1))) {
      // Selected one slide.
      if (position > 0) {
        menu.add(0, MENU_MOVE_UP, 0, R.string.move_up).setIcon(R.drawable.ic_menu_move_up);
      }

      if (position < (mSlideListAdapter.getCount() - 1)) {
        menu.add(0, MENU_MOVE_DOWN, 0, R.string.move_down).setIcon(R.drawable.ic_menu_move_down);
      }
      if (mSlideshowModel.size() < SlideshowEditor.MAX_SLIDE_NUM) {
        menu.add(0, MENU_ADD_SLIDE, 0, R.string.add_slide).setIcon(R.drawable.ic_menu_add_slide);
      }

      menu.add(0, MENU_REMOVE_SLIDE, 0, R.string.remove_slide)
          .setIcon(android.R.drawable.ic_menu_delete);
    } else {
      if (mSlideshowModel.size() < SlideshowEditor.MAX_SLIDE_NUM) {
        menu.add(0, MENU_ADD_SLIDE, 0, R.string.add_slide).setIcon(R.drawable.ic_menu_add_slide);
      }
    }

    menu.add(0, MENU_DISCARD_SLIDESHOW, 0, R.string.discard_slideshow)
        .setIcon(R.drawable.ic_menu_delete_played);

    return true;
  }
  private void internalAddOrReplace(MediaModel old, MediaModel media) {
    // If the media is resizable, at this point consider it to be zero length.
    // Just before we send the slideshow, we take the remaining space in the
    // slideshow and equally allocate it to all the resizeable media items and resize them.
    int addSize = media.getMediaResizable() ? 0 : media.getMediaSize();
    int removeSize;
    if (old == null) {
      if (null != mParent) {
        mParent.checkMessageSize(addSize);
      }
      mMedia.add(media);
      increaseSlideSize(addSize);
      increaseMessageSize(addSize);
    } else {
      removeSize = old.getMediaResizable() ? 0 : old.getMediaSize();
      if (addSize > removeSize) {
        if (null != mParent) {
          mParent.checkMessageSize(addSize - removeSize);
        }
        increaseSlideSize(addSize - removeSize);
        increaseMessageSize(addSize - removeSize);
      } else {
        decreaseSlideSize(removeSize - addSize);
        decreaseMessageSize(removeSize - addSize);
      }
      mMedia.set(mMedia.indexOf(old), media);
      old.unregisterAllModelChangedObservers();
    }

    for (IModelChangedObserver observer : mModelChangedObservers) {
      media.registerModelChangedObserver(observer);
    }
  }
 private static void viewMmsMessageAttachment(
     Context context, Uri msgUri, SlideshowModel slideshow, int requestCode) {
   boolean isSimple = (slideshow == null) ? false : slideshow.isSimple();
   if (isSimple) {
     // In attachment-editor mode, we only ever have one slide.
     MessageUtils.viewSimpleSlideshow(context, slideshow);
   } else {
     // If a slideshow was provided, save it to disk first.
     if (slideshow != null) {
       PduPersister persister = PduPersister.getPduPersister(context);
       try {
         PduBody pb = slideshow.toPduBody();
         persister.updateParts(msgUri, pb);
         slideshow.sync(pb);
       } catch (MmsException e) {
         Log.e(TAG, "Unable to save message for preview");
         return;
       }
     }
     // Launch the slideshow activity to play/view.
     Intent intent = new Intent(context, SlideshowActivity.class);
     intent.setData(msgUri);
     if (requestCode > 0 && context instanceof Activity) {
       ((Activity) context).startActivityForResult(intent, requestCode);
     } else {
       context.startActivity(intent);
     }
   }
 }
  public static void viewSimpleSlideshow(Context context, SlideshowModel slideshow) {
    if (!slideshow.isSimple()) {
      throw new IllegalArgumentException("viewSimpleSlideshow() called on a non-simple slideshow");
    }
    SlideModel slide = slideshow.get(0);
    MediaModel mm = null;
    if (slide.hasImage()) {
      mm = slide.getImage();
    } else if (slide.hasVideo()) {
      mm = slide.getVideo();
    }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.putExtra("SingleItemOnly", true); // So we don't see "surrounding" images in Gallery

    String contentType;
    if (mm.isDrmProtected()) {
      contentType = mm.getDrmObject().getContentType();
    } else {
      contentType = mm.getContentType();
    }
    intent.setDataAndType(mm.getUri(), contentType);
    context.startActivity(intent);
  }
  public static int getAttachmentType(SlideshowModel model) {
    if (model == null) {
      return WorkingMessage.TEXT;
    }

    int numberOfSlides = model.size();
    if (numberOfSlides > 1) {
      return WorkingMessage.SLIDESHOW;
    } else if (numberOfSlides == 1) {
      // Only one slide in the slide-show.
      SlideModel slide = model.get(0);
      if (slide.hasVideo()) {
        return WorkingMessage.VIDEO;
      }

      if (slide.hasAudio() && slide.hasImage()) {
        return WorkingMessage.SLIDESHOW;
      }

      if (slide.hasAudio()) {
        return WorkingMessage.AUDIO;
      }

      if (slide.hasImage()) {
        return WorkingMessage.IMAGE;
      }

      if (slide.hasText()) {
        return WorkingMessage.TEXT;
      }
    }

    return WorkingMessage.TEXT;
  }
 public void decreaseMessageSize(int decreaseSize) {
   if ((decreaseSize > 0) && (null != mParent)) {
     int size = mParent.getCurrentMessageSize();
     size -= decreaseSize;
     mParent.setCurrentMessageSize(size);
   }
 }
  private void showDurationDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.drawable.ic_mms_duration);
    String title = getResources().getString(R.string.duration_selector_title);
    builder.setTitle(title + (mPosition + 1) + "/" + mSlideshowModel.size());

    builder.setItems(
        R.array.select_dialog_items,
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            if ((which >= 0) && (which < NUM_DIRECT_DURATIONS)) {
              mSlideshowEditor.changeDuration(mPosition, (which + 1) * 1000);
            } else {
              Intent intent = new Intent(SlideEditorActivity.this, EditSlideDurationActivity.class);
              intent.putExtra(EditSlideDurationActivity.SLIDE_INDEX, mPosition);
              intent.putExtra(EditSlideDurationActivity.SLIDE_TOTAL, mSlideshowModel.size());
              intent.putExtra(
                  EditSlideDurationActivity.SLIDE_DUR,
                  mSlideshowModel.get(mPosition).getDuration() / 1000); // in seconds
              startActivityForResult(intent, REQUEST_CODE_CHANGE_DURATION);
            }
            dialog.dismiss();
          }
        });

    builder.show();
  }
  private void showLayoutSelectorDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.drawable.ic_mms_layout);

    String title = getResources().getString(R.string.layout_selector_title);
    builder.setTitle(title + (mPosition + 1) + "/" + mSlideshowModel.size());

    LayoutSelectorAdapter adapter = new LayoutSelectorAdapter(this);
    builder.setAdapter(
        adapter,
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            switch (which) {
              case 0: // Top text.
                mSlideshowEditor.changeLayout(LayoutModel.LAYOUT_TOP_TEXT);
                break;
              case 1: // Bottom text.
                mSlideshowEditor.changeLayout(LayoutModel.LAYOUT_BOTTOM_TEXT);
                break;
            }
            dialog.dismiss();
          }
        });

    builder.show();
  }
  @Override
  protected void onPause() {
    super.onPause();

    synchronized (this) {
      if (mDirty) {
        try {
          PduBody pb = mSlideshowModel.toPduBody();
          PduPersister.getPduPersister(this).updateParts(mUri, pb);
          mSlideshowModel.sync(pb);
        } catch (MmsException e) {
          Log.e(TAG, "Cannot update the message: " + mUri, e);
        }
      }
    }
  }
 private void adjustAddSlideVisibility() {
   if (mSlideshowModel.size() >= SlideshowEditor.MAX_SLIDE_NUM) {
     mAddSlideItem.setVisibility(View.GONE);
   } else {
     mAddSlideItem.setVisibility(View.VISIBLE);
   }
 }
 public void onClick(View v) {
   // Validate mPosition
   if (mPosition >= 0 && mPosition < mSlideshowModel.size()) {
     mSlideshowEditor.removeSlide(mPosition);
     int size = mSlideshowModel.size();
     if (size > 0) {
       if (mPosition >= size) {
         mPosition--;
       }
       showCurrentSlide();
     } else {
       finish();
       return;
     }
   }
 }
  @Override
  protected void onDestroy() {
    super.onDestroy();

    if (mSlideshowModel != null) {
      mSlideshowModel.unregisterModelChangedObserver(mModelChangedObserver);
    }
  }
 public static void viewMmsMessageAttachment(
     Context context, WorkingMessage msg, int requestCode) {
   SlideshowModel slideshow = msg.getSlideshow();
   if (slideshow == null) {
     throw new IllegalStateException("msg.getSlideshow() == null");
   }
   if (slideshow.isSimple()) {
     MessageUtils.viewSimpleSlideshow(context, slideshow);
   } else {
     Uri uri = msg.saveAsMms(false);
     if (uri != null) {
       // Pass null for the slideshow paramater, otherwise viewMmsMessageAttachment
       // will persist the slideshow to disk again (we just did that above in saveAsMms)
       viewMmsMessageAttachment(context, uri, null, requestCode);
     }
   }
 }
 private void initSlideList() throws MmsException {
   cleanupSlideshowModel();
   mSlideshowModel = SlideshowModel.createFromMessageUri(this, mUri);
   mSlideshowModel.registerModelChangedObserver(mModelChangedObserver);
   mSlideshowEditor = new SlideshowEditor(this, mSlideshowModel);
   if ((mSlideshowModel.size() == 0)) {
     boolean b = getSharedPreferences("SetDefaultLayout", 0).getBoolean("SetDefaultLayout", true);
     if (b) {
       mSlideshowEditor.changeLayout(LayoutModel.LAYOUT_BOTTOM_TEXT);
     } else {
       getSharedPreferences("SetDefaultLayout", 0)
           .edit()
           .putBoolean("SetDefaultLayout", true)
           .commit();
     }
   }
   mSlideListAdapter = new SlideListAdapter(this, R.layout.slideshow_edit_item, mSlideshowModel);
   mList.setAdapter(mSlideListAdapter);
 }
  private void showCurrentSlide() {
    mPresenter.setLocation(mPosition);
    mPresenter.present();
    updateTitle();

    if (mSlideshowModel.get(mPosition).hasImage()) {
      setReplaceButtonText(R.string.replace_image);
    } else {
      setReplaceButtonText(R.string.add_picture);
    }
  }
  private void addNewSlide() {
    if (mSlideshowEditor.addNewSlide()) {
      // add successfully
      mSlideListAdapter.notifyDataSetChanged();

      // Select the new slide.
      mList.requestFocus();
      mList.setSelection(mSlideshowModel.size() - 1);
    } else {
      Toast.makeText(this, R.string.cannot_add_slide_anymore, Toast.LENGTH_SHORT).show();
    }
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    int position = mList.getSelectedItemPosition();

    switch (item.getItemId()) {
      case MENU_MOVE_UP:
        if ((position > 0) && (position < mSlideshowModel.size())) {
          mSlideshowEditor.moveSlideUp(position);
          mSlideListAdapter.notifyDataSetChanged();
          mList.setSelection(position - 1);
        }
        break;
      case MENU_MOVE_DOWN:
        if ((position >= 0) && (position < mSlideshowModel.size() - 1)) {
          mSlideshowEditor.moveSlideDown(position);
          mSlideListAdapter.notifyDataSetChanged();
          mList.setSelection(position + 1);
        }
        break;
      case MENU_REMOVE_SLIDE:
        if ((position >= 0) && (position < mSlideshowModel.size())) {
          mSlideshowEditor.removeSlide(position);
          mSlideListAdapter.notifyDataSetChanged();
        }
        break;
      case MENU_ADD_SLIDE:
        addNewSlide();
        break;
      case MENU_DISCARD_SLIDESHOW:
        // delete all slides from slideshow.
        mSlideshowEditor.removeAllSlides();
        mSlideListAdapter.notifyDataSetChanged();
        finish();
        break;
    }

    return true;
  }
 public void onClick(View v) {
   SlideModel slide = mSlideshowModel.get(mPosition);
   if (slide != null && slide.hasVideo()) {
     Toast.makeText(
             SlideEditorActivity.this,
             R.string.cannot_add_picture_and_video,
             Toast.LENGTH_SHORT)
         .show();
     return;
   }
   Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
   intent.setType(ContentType.IMAGE_UNSPECIFIED);
   startActivityForResult(intent, REQUEST_CODE_CHANGE_PICTURE);
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_slide_activity);

    mSlideView = (BasicSlideEditorView) findViewById(R.id.slide_editor_view);
    mSlideView.setOnTextChangedListener(mOnTextChangedListener);

    mPreSlide = (ImageButton) findViewById(R.id.pre_slide_button);
    mPreSlide.setOnClickListener(mOnNavigateBackward);

    mNextSlide = (ImageButton) findViewById(R.id.next_slide_button);
    mNextSlide.setOnClickListener(mOnNavigateForward);

    mPreview = (Button) findViewById(R.id.preview_button);
    mPreview.setOnClickListener(mOnPreview);

    mReplaceImage = (Button) findViewById(R.id.replace_image_button);
    mReplaceImage.setOnClickListener(mOnReplaceImage);

    mRemoveSlide = (Button) findViewById(R.id.remove_slide_button);
    mRemoveSlide.setOnClickListener(mOnRemoveSlide);

    mTextEditor = (EditText) findViewById(R.id.text_message);
    mTextEditor.setFilters(new InputFilter[] {new LengthFilter(MmsConfig.getMaxTextLimit())});

    mDone = (Button) findViewById(R.id.done_button);
    mDone.setOnClickListener(mDoneClickListener);

    initActivityState(savedInstanceState, getIntent());

    try {
      mSlideshowModel = SlideshowModel.createFromMessageUri(this, mUri);
      // Confirm that we have at least 1 slide to display
      if (mSlideshowModel.size() == 0) {
        Log.e(TAG, "Loaded slideshow is empty; can't edit nothingness, exiting.");
        finish();
        return;
      }
      // Register an observer to watch whether the data model is changed.
      mSlideshowModel.registerModelChangedObserver(mModelChangedObserver);
      mSlideshowEditor = new SlideshowEditor(this, mSlideshowModel);
      mPresenter =
          (SlideshowPresenter)
              PresenterFactory.getPresenter(
                  "SlideshowPresenter", this, mSlideView, mSlideshowModel);

      // Sanitize mPosition
      if (mPosition >= mSlideshowModel.size()) {
        mPosition = Math.max(0, mSlideshowModel.size() - 1);
      } else if (mPosition < 0) {
        mPosition = 0;
      }

      showCurrentSlide();
    } catch (MmsException e) {
      Log.e(TAG, "Create SlideshowModel failed!", e);
      finish();
      return;
    }
  }
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    mHandler = new Handler();
    /// M: Code analyze 004, new feature, Slideshow Plugin(unknown) @{
    initPlugin(this);
    /// @}
    // Play slide-show in full-screen mode.
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.slideshow);

    Intent intent = getIntent();
    Uri msg = intent.getData();
    final SlideshowModel model;

    try {
      model = SlideshowModel.createFromMessageUri(this, msg);
      mSlideCount = model.size();
      /** M: Confirm that we have at least 1 supported_slide to display @{ */
      if (mSlideCount == 0) {
        Toast.makeText(this, getString(R.string.unsupport_media_type), Toast.LENGTH_SHORT).show();
        throw new MmsException();
      }
      /** @} */
    } catch (MmsException e) {
      Log.e(TAG, "Cannot present the slide show.", e);
      finish();
      return;
    }
    mSlideView = (SlideView) findViewById(R.id.slide_view);

    /// M: Code analyze 003, fix bug ALPS00119632, present slide return
    /// (mActivityRunning=false) when SlideshowActivity stop @{
    mPresenter =
        (SlideshowPresenter)
            PresenterFactory.getPresenter("SlideshowPresenter", this, mSlideView, model);
    /// @}

    mRotate = true;

    /// M: Code analyze 005, fix bug ALPS00071669, Handel delay problem
    /// when progress bar update @{
    mSmilPlayer = SmilPlayer.getPlayer();
    initMediaController();
    mSlideView.setMediaController(mMediaController);
    mSlideView.setActivity(this);
    mSlideView.setMmsSlideShowPlugin(mMmsSlideShowPlugin);
    // Use SmilHelper.getDocument() to ensure rebuilding the
    /// M: entire SMIL document.
    mSmilDoc = SmilHelper.getDocument(model);
    /// @}
    mHandler.post(
        new Runnable() {
          private boolean isRotating() {
            return mSmilPlayer.isPausedState()
                || mSmilPlayer.isPlayingState()
                || mSmilPlayer.isPlayedState();
          }

          public void run() {
            if (isMMSConformance(mSmilDoc)) {
              int imageLeft = 0;
              int imageTop = 0;
              int textLeft = 0;
              int textTop = 0;
              /// M: Code analyze 006, fix bug ALPS00300308, Modified Text layout @{
              int textWidth = 0;
              int textHeight = 0;
              /// @}
              LayoutModel layout = model.getLayout();
              if (layout != null) {
                RegionModel imageRegion = layout.getImageRegion();
                if (imageRegion != null) {
                  imageLeft = imageRegion.getLeft();
                  imageTop = imageRegion.getTop();
                }
                RegionModel textRegion = layout.getTextRegion();
                if (textRegion != null) {
                  textLeft = textRegion.getLeft();
                  textTop = textRegion.getTop();
                }
              }
              /// M: Code analyze 006, fix bug ALPS00300308, Modified Text layout @{
              mSlideView.enableMMSConformanceMode(
                  textLeft, textTop, textWidth, textHeight, imageLeft, imageTop);
              /// @}
            } else {
              /// M: init page divider view
              mSlideView.initPageDivider();
            }
            if (DEBUG) {
              ByteArrayOutputStream ostream = new ByteArrayOutputStream();
              SmilXmlSerializer.serialize(mSmilDoc, ostream);
              if (LOCAL_LOGV) {
                Log.v(TAG, ostream.toString());
              }
            }

            // Add event listener.
            ((EventTarget) mSmilDoc)
                .addEventListener(
                    SmilDocumentImpl.SMIL_DOCUMENT_END_EVENT, SlideshowActivity.this, false);

            mSmilPlayer.init(mSmilDoc);
            if (isRotating()) {
              mSmilPlayer.reload();
            } else {
              /// M: Code analyze 002, fix bug ALPS00111374, (CMCC feature) pause / not auto play
              /// but need prepareToPlay when enter the slideshow activity @{
              if (mMmsSlideShowPlugin.getInitialPlayState() == IMmsSlideShow.PLAY_AS_PAUSE) {
                mSmilPlayer.prepareToPlay();
                bNeedResume = true;
                mSmilPlayerController.pause();
              } else {
                mSmilPlayer.play();
                bNeedResume = true;
              }
              /// @}
            }
          }
        });
  }
 private void cleanupSlideshowModel() {
   if (mSlideshowModel != null) {
     mSlideshowModel.unregisterModelChangedObserver(mModelChangedObserver);
     mSlideshowModel = null;
   }
 }
 private void updateTitle() {
   setTitle(getString(R.string.slide_show_part, (mPosition + 1), mSlideshowModel.size()));
 }
  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    if (isFinishing()) {
      return false;
    }
    menu.clear();

    SlideModel slide = mSlideshowModel.get(mPosition);

    if (slide == null) {
      return false;
    }

    // Preview slideshow.
    menu.add(0, MENU_PREVIEW_SLIDESHOW, 0, R.string.preview_slideshow)
        .setIcon(com.android.internal.R.drawable.ic_menu_play_clip);

    // Text
    if (slide.hasText() && !TextUtils.isEmpty(slide.getText().getText())) {
      // "Change text" if text is set.
      menu.add(0, MENU_REMOVE_TEXT, 0, R.string.remove_text)
          .setIcon(R.drawable.ic_menu_remove_text);
    }

    // Picture
    if (slide.hasImage()) {
      menu.add(0, MENU_DEL_PICTURE, 0, R.string.remove_picture)
          .setIcon(R.drawable.ic_menu_remove_picture);
    } else if (!slide.hasVideo()) {
      menu.add(0, MENU_ADD_PICTURE, 0, R.string.add_picture).setIcon(R.drawable.ic_menu_picture);
      menu.add(0, MENU_TAKE_PICTURE, 0, R.string.attach_take_photo)
          .setIcon(R.drawable.ic_menu_picture);
    }

    // Music
    if (slide.hasAudio()) {
      menu.add(0, MENU_DEL_AUDIO, 0, R.string.remove_music)
          .setIcon(R.drawable.ic_menu_remove_sound);
    } else if (!slide.hasVideo()) {
      if (MmsConfig.getAllowAttachAudio()) {
        SubMenu subMenu =
            menu.addSubMenu(0, MENU_SUB_AUDIO, 0, R.string.add_music)
                .setIcon(R.drawable.ic_menu_add_sound);
        subMenu.add(0, MENU_ADD_AUDIO, 0, R.string.attach_sound);
        subMenu.add(0, MENU_RECORD_SOUND, 0, R.string.attach_record_sound);
      } else {
        menu.add(0, MENU_RECORD_SOUND, 0, R.string.attach_record_sound)
            .setIcon(R.drawable.ic_menu_add_sound);
      }
    }

    // Video
    if (slide.hasVideo()) {
      menu.add(0, MENU_DEL_VIDEO, 0, R.string.remove_video)
          .setIcon(R.drawable.ic_menu_remove_video);
    } else if (!slide.hasAudio() && !slide.hasImage()) {
      menu.add(0, MENU_ADD_VIDEO, 0, R.string.add_video).setIcon(R.drawable.ic_menu_movie);
      menu.add(0, MENU_TAKE_VIDEO, 0, R.string.attach_record_video)
          .setIcon(R.drawable.ic_menu_movie);
    }

    // Add slide
    menu.add(0, MENU_ADD_SLIDE, 0, R.string.add_slide).setIcon(R.drawable.ic_menu_add_slide);

    // Slide duration
    String duration = getResources().getString(R.string.duration_sec);
    menu.add(
            0, MENU_DURATION, 0, duration.replace("%s", String.valueOf(slide.getDuration() / 1000)))
        .setIcon(R.drawable.ic_menu_duration);

    // Slide layout
    int resId;
    if (mSlideshowModel.getLayout().getLayoutType() == LayoutModel.LAYOUT_TOP_TEXT) {
      resId = R.string.layout_top;
    } else {
      resId = R.string.layout_bottom;
    }
    // FIXME: set correct icon when layout icon is available.
    menu.add(0, MENU_LAYOUT, 0, resId).setIcon(R.drawable.ic_menu_picture);
    return true;
  }
  @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;
  }
 public void onClick(View v) {
   if (mPosition < mSlideshowModel.size() - 1) {
     mPosition++;
     showCurrentSlide();
   }
 }
  public static SlideshowModel createFromPduBody(Context context, PduBody pb) throws MmsException {
    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;
  }