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 android.content.Context;
21import android.net.Uri;
22import android.util.Log;
23
24import com.android.mms.model.AudioModel;
25import com.android.mms.model.ImageModel;
26import com.android.mms.model.RegionModel;
27import com.android.mms.model.SlideModel;
28import com.android.mms.model.SlideshowModel;
29import com.android.mms.model.TextModel;
30import com.android.mms.model.VideoModel;
31import com.google.android.mms.ContentType;
32import com.google.android.mms.MmsException;
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 SlideshowModel mModel;
44
45    public SlideshowEditor(Context context, SlideshowModel model) {
46        mContext = context;
47        mModel = model;
48    }
49
50    public void setSlideshow(SlideshowModel model) {
51        mModel = model;
52    }
53
54    /**
55     * Add a new slide to the end of message.
56     *
57     * @return true if success, false if reach the max slide number.
58     */
59    public boolean addNewSlide() {
60        int position = mModel.size();
61        return addNewSlide(position);
62    }
63
64    /**
65     * Add a new slide at the specified position in the message.
66     *
67     * @return true if success, false if reach the max slide number.
68     * @throws IndexOutOfBoundsException - if position is out of range
69     *         (position < 0 || position > size()).
70     */
71    public boolean addNewSlide(int position) {
72        int size = mModel.size();
73        if (size < MAX_SLIDE_NUM) {
74            SlideModel slide = new SlideModel(mModel);
75
76            TextModel text = new TextModel(
77                    mContext, ContentType.TEXT_PLAIN, "text_" + size + ".txt",
78                    mModel.getLayout().getTextRegion());
79            slide.add(text);
80
81            mModel.add(position, slide);
82            return true;
83        } else {
84            Log.w(TAG, "The limitation of the number of slides is reached.");
85            return false;
86        }
87    }
88    /**
89     * Add an existing slide at the specified position in the message.
90     *
91     * @return true if success, false if reach the max slide number.
92     * @throws IndexOutOfBoundsException - if position is out of range
93     *         (position < 0 || position > size()).
94     */
95    public boolean addSlide(int position, SlideModel slide) {
96        int size = mModel.size();
97        if (size < MAX_SLIDE_NUM) {
98            mModel.add(position, slide);
99            return true;
100        } else {
101            Log.w(TAG, "The limitation of the number of slides is reached.");
102            return false;
103        }
104    }
105
106    /**
107     * Remove one slide.
108     *
109     * @param position
110     */
111    public void removeSlide(int position) {
112        mModel.remove(position);
113    }
114
115    /**
116     * Remove all slides.
117     */
118    public void removeAllSlides() {
119        while (mModel.size() > 0) {
120            removeSlide(0);
121        }
122    }
123
124    /**
125     * Remove the text of the specified slide.
126     *
127     * @param position index of the slide
128     * @return true if success, false if no text in the slide.
129     */
130    public boolean removeText(int position) {
131        return mModel.get(position).removeText();
132    }
133
134    public boolean removeImage(int position) {
135        return mModel.get(position).removeImage();
136    }
137
138    public boolean removeVideo(int position) {
139        return mModel.get(position).removeVideo();
140    }
141
142    public boolean removeAudio(int position) {
143        return mModel.get(position).removeAudio();
144    }
145
146    public void changeText(int position, String newText) {
147        if (newText != null) {
148            SlideModel slide = mModel.get(position);
149            TextModel text = slide.getText();
150            if (text == null) {
151                text = new TextModel(mContext,
152                        ContentType.TEXT_PLAIN, "text_" + position + ".txt",
153                        mModel.getLayout().getTextRegion());
154                text.setText(newText);
155                slide.add(text);
156            } else if (!newText.equals(text.getText())) {
157                text.setText(newText);
158            }
159        }
160    }
161
162    public void changeImage(int position, Uri newImage) throws MmsException {
163        mModel.get(position).add(new ImageModel(
164                mContext, newImage, mModel.getLayout().getImageRegion()));
165    }
166
167    public void changeAudio(int position, Uri newAudio) throws MmsException {
168        AudioModel audio = new AudioModel(mContext, newAudio);
169        SlideModel slide = mModel.get(position);
170        slide.add(audio);
171        slide.updateDuration(audio.getDuration());
172    }
173
174    public void changeVideo(int position, Uri newVideo) throws MmsException {
175        VideoModel video = new VideoModel(mContext, newVideo,
176                mModel.getLayout().getImageRegion());
177        SlideModel slide = mModel.get(position);
178        slide.add(video);
179        slide.updateDuration(video.getDuration());
180    }
181
182    public void moveSlideUp(int position) {
183        mModel.add(position - 1, mModel.remove(position));
184    }
185
186    public void moveSlideDown(int position) {
187        mModel.add(position + 1, mModel.remove(position));
188    }
189
190    public void changeDuration(int position, int dur) {
191        if (dur >= 0) {
192            mModel.get(position).setDuration(dur);
193        }
194    }
195
196    public void changeLayout(int layout) {
197        mModel.getLayout().changeTo(layout);
198    }
199
200    public RegionModel getImageRegion() {
201        return mModel.getLayout().getImageRegion();
202    }
203
204    public RegionModel getTextRegion() {
205        return mModel.getLayout().getTextRegion();
206    }
207}
208