package com.android.mms.ui;
import com.google.android.mms.ContentType;
import com.android.mms.ExceedMessageSizeException;
import com.google.android.mms.MmsException;
import com.android.mms.MmsConfig;
import com.android.mms.R;
import com.android.mms.ResolutionException;
import com.android.mms.UnsupportContentTypeException;
import com.android.mms.model.IModelChangedObserver;
import com.android.mms.model.LayoutModel;
import com.android.mms.model.Model;
import com.android.mms.model.SlideModel;
import com.android.mms.model.SlideshowModel;
import com.google.android.mms.pdu.PduBody;
import com.google.android.mms.pdu.PduPart;
import com.google.android.mms.pdu.PduPersister;
import com.android.mms.ui.BasicSlideEditorView.OnTextChangedListener;
import com.android.mms.ui.MessageUtils.ResizeImageResultCallback;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Config;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import android.text.InputFilter.LengthFilter;
import android.widget.EditText;
import android.text.InputFilter;
/**
* This activity allows user to edit the contents of a slide.
*/
public class SlideEditorActivity extends Activity {
private static final String TAG = "SlideEditorActivity";
private static final boolean DEBUG = false;
private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV;
// Key for extra data.
public static final String SLIDE_INDEX = "slide_index";
// Menu ids.
private final static int MENU_REMOVE_TEXT = 0;
private final static int MENU_ADD_PICTURE = 1;
private final static int MENU_TAKE_PICTURE = 2;
private final static int MENU_DEL_PICTURE = 3;
private final static int MENU_ADD_AUDIO = 4;
private final static int MENU_DEL_AUDIO = 5;
private final static int MENU_ADD_VIDEO = 6;
private final static int MENU_ADD_SLIDE = 7;
private final static int MENU_DEL_VIDEO = 8;
private final static int MENU_LAYOUT = 9;
private final static int MENU_DURATION = 10;
private final static int MENU_PREVIEW_SLIDESHOW = 11;
private final static int MENU_RECORD_SOUND = 12;
private final static int MENU_SUB_AUDIO = 13;
// Request code.
private final static int REQUEST_CODE_EDIT_TEXT = 0;
private final static int REQUEST_CODE_CHANGE_PICTURE = 1;
private final static int REQUEST_CODE_TAKE_PICTURE = 2;
private final static int REQUEST_CODE_CHANGE_MUSIC = 3;
private final static int REQUEST_CODE_RECORD_SOUND = 4;
private final static int REQUEST_CODE_CHANGE_VIDEO = 5;
private final static int REQUEST_CODE_CHANGE_DURATION = 6;
// number of items in the duration selector dialog that directly map from
// item index to duration in seconds (duration = index + 1)
private final static int NUM_DIRECT_DURATIONS = 10;
private ImageButton mNextSlide;
private ImageButton mPreSlide;
private Button mPreview;
private Button mReplaceImage;
private Button mRemoveSlide;
private EditText mTextEditor;
private Button mDone;
private BasicSlideEditorView mSlideView;
private SlideshowModel mSlideshowModel;
private SlideshowEditor mSlideshowEditor;
private SlideshowPresenter mPresenter;
private boolean mDirty;
private int mPosition;
private Uri mUri;
private final static String MESSAGE_URI = "message_uri";
@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);
// 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;
}
}
private void initActivityState(Bundle savedInstanceState, Intent intent) {
if (savedInstanceState != null) {
mUri = (Uri) savedInstanceState.getParcelable(MESSAGE_URI);
mPosition = savedInstanceState.getInt(SLIDE_INDEX, 0);
} else {
mUri = intent.getData();
mPosition = intent.getIntExtra(SLIDE_INDEX, 0);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(SLIDE_INDEX, mPosition);
outState.putParcelable(MESSAGE_URI, mUri);
}
@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);
}
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mSlideshowModel != null) {
mSlideshowModel.unregisterModelChangedObserver(
mModelChangedObserver);
}
}
private final IModelChangedObserver mModelChangedObserver =
new IModelChangedObserver() {
public void onModelChanged(Model model, boolean dataChanged) {
synchronized (SlideEditorActivity.this) {