1/*
2 * Copyright (C) 2008 Esmertec AG.
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mms.ui;
19
20import com.google.android.mms.ContentType;
21import com.google.android.mms.MmsException;
22import com.android.mms.model.AudioModel;
23import com.android.mms.model.ImageModel;
24import com.android.mms.model.RegionModel;
25import com.android.mms.model.SlideModel;
26import com.android.mms.model.SlideshowModel;
27import com.android.mms.model.TextModel;
28import com.android.mms.model.VideoModel;
29
30import android.content.Context;
31import android.net.Uri;
32import android.util.Log;
33
34/**
35 * An utility to edit contents of a slide.
36 */
37public class SlideshowEditor {
38    private static final String TAG = "Mms:slideshow";
39
40    public static final int MAX_SLIDE_NUM = 10;
41
42    private final Context mContext;
43    private final SlideshowModel mModel;
44
45    public SlideshowEditor(Context context, SlideshowModel model) {
46        mContext = context;
47        mModel = model;
48    }
49
50    /**
51     * Add a new slide to the end of message.
52     *
53     * @return true if success, false if reach the max slide number.
54     */
55    public boolean addNewSlide() {
56        int position = mModel.size();
57        return addNewSlide(position);
58    }
59
60    /**
61     * Add a new slide at the specified position in the message.
62     *
63     * @return true if success, false if reach the max slide number.
64     * @throws IndexOutOfBoundsException - if position is out of range
65     *         (position < 0 || position > size()).
66     */
67    public boolean addNewSlide(int position) {
68        int size = mModel.size();
69        if (size < MAX_SLIDE_NUM) {
70            SlideModel slide = new SlideModel(mModel);
71
72            TextModel text = new TextModel(
73                    mContext, ContentType.TEXT_PLAIN, "text_" + size + ".txt",
74                    mModel.getLayout().getTextRegion());
75            slide.add(text);
76
77            mModel.add(position, slide);
78            return true;
79        } else {
80            Log.w(TAG, "The limitation of the number of slides is reached.");
81            return false;
82        }
83    }
84
85    /**
86     * Remove one slide.
87     *
88     * @param position
89     */
90    public void removeSlide(int position) {
91        mModel.remove(position);
92    }
93
94    /**
95     * Remove all slides.
96     */
97    public void removeAllSlides() {
98        while (mModel.size() > 0) {
99            removeSlide(0);
100        }
101    }
102
103    /**
104     * Remove the text of the specified slide.
105     *
106     * @param position index of the slide
107     * @return true if success, false if no text in the slide.
108     */
109    public boolean removeText(int position) {
110        return mModel.get(position).removeText();
111    }
112
113    public boolean removeImage(int position) {
114        return mModel.get(position).removeImage();
115    }
116
117    public boolean removeVideo(int position) {
118        return mModel.get(position).removeVideo();
119    }
120
121    public boolean removeAudio(int position) {
122        return mModel.get(position).removeAudio();
123    }
124
125    public void changeText(int position, String newText) {
126        if (newText != null) {
127            SlideModel slide = mModel.get(position);
128            TextModel text = slide.getText();
129            if (text == null) {
130                text = new TextModel(mContext,
131                        ContentType.TEXT_PLAIN, "text_" + position + ".txt",
132                        mModel.getLayout().getTextRegion());
133                text.setText(newText);
134                slide.add(text);
135            } else if (!newText.equals(text.getText())) {
136                text.setText(newText);
137            }
138        }
139    }
140
141    public void changeImage(int position, Uri newImage) throws MmsException {
142        mModel.get(position).add(new ImageModel(
143                mContext, newImage, mModel.getLayout().getImageRegion()));
144    }
145
146    public void changeAudio(int position, Uri newAudio) throws MmsException {
147        AudioModel audio = new AudioModel(mContext, newAudio);
148        SlideModel slide = mModel.get(position);
149        slide.add(audio);
150        slide.updateDuration(audio.getDuration());
151    }
152
153    public void changeVideo(int position, Uri newVideo) throws MmsException {
154        VideoModel video = new VideoModel(mContext, newVideo,
155                mModel.getLayout().getImageRegion());
156        SlideModel slide = mModel.get(position);
157        slide.add(video);
158        slide.updateDuration(video.getDuration());
159    }
160
161    public void moveSlideUp(int position) {
162        mModel.add(position - 1, mModel.remove(position));
163    }
164
165    public void moveSlideDown(int position) {
166        mModel.add(position + 1, mModel.remove(position));
167    }
168
169    public void changeDuration(int position, int dur) {
170        if (dur >= 0) {
171            mModel.get(position).setDuration(dur);
172        }
173    }
174
175    public void changeLayout(int layout) {
176        mModel.getLayout().changeTo(layout);
177    }
178
179    public RegionModel getImageRegion() {
180        return mModel.getLayout().getImageRegion();
181    }
182
183    public RegionModel getTextRegion() {
184        return mModel.getLayout().getTextRegion();
185    }
186}
187