MediaModelFactory.java revision 0f236f55349f070ac94e12cca963847173393da8
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.model;
19
20import com.android.mms.UnsupportContentTypeException;
21import com.android.mms.drm.DrmWrapper;
22import com.google.android.mms.ContentType;
23import com.google.android.mms.MmsException;
24import com.google.android.mms.pdu.PduBody;
25import com.google.android.mms.pdu.PduPart;
26
27import org.w3c.dom.smil.SMILMediaElement;
28import org.w3c.dom.smil.SMILRegionElement;
29import org.w3c.dom.smil.SMILRegionMediaElement;
30import org.w3c.dom.smil.Time;
31import org.w3c.dom.smil.TimeList;
32
33import android.content.Context;
34import android.drm.mobile1.DrmException;
35
36import java.io.IOException;
37
38public class MediaModelFactory {
39    private static final String TAG = "MediaModelFactory";
40
41    public static MediaModel getMediaModel(Context context,
42            SMILMediaElement sme, LayoutModel layouts, PduBody pb)
43            throws DrmException, IOException, IllegalArgumentException, MmsException {
44        String tag = sme.getTagName();
45        String src = sme.getSrc();
46        PduPart part = findPart(pb, src);
47
48        if (sme instanceof SMILRegionMediaElement) {
49            return getRegionMediaModel(
50                    context, tag, src, (SMILRegionMediaElement) sme, layouts, part);
51        } else {
52            return getGenericMediaModel(
53                    context, tag, src, sme, part, null);
54        }
55    }
56
57    private static PduPart findPart(PduBody pb, String src) {
58        PduPart part = null;
59
60        if (src != null) {
61            if (src.startsWith("cid:")) {
62                part = pb.getPartByContentId("<" + src.substring("cid:".length()) + ">");
63            } else {
64                part = pb.getPartByName(src);
65                if (part == null) {
66                    part = pb.getPartByFileName(src);
67                    if (part == null) {
68                        part = pb.getPartByContentLocation(src);
69                    }
70                }
71            }
72        }
73
74        if (part != null) {
75            return part;
76        }
77
78        throw new IllegalArgumentException("No part found for the model.");
79    }
80
81    private static MediaModel getRegionMediaModel(Context context,
82            String tag, String src, SMILRegionMediaElement srme,
83            LayoutModel layouts, PduPart part) throws DrmException, IOException, MmsException {
84        SMILRegionElement sre = srme.getRegion();
85        if (sre != null) {
86            RegionModel region = layouts.findRegionById(sre.getId());
87            if (region != null) {
88                return getGenericMediaModel(context, tag, src, srme, part, region);
89            }
90        } else {
91            String rId = null;
92
93            if (tag.equals(SmilHelper.ELEMENT_TAG_TEXT)) {
94                rId = LayoutModel.TEXT_REGION_ID;
95            } else {
96                rId = LayoutModel.IMAGE_REGION_ID;
97            }
98
99            RegionModel region = layouts.findRegionById(rId);
100            if (region != null) {
101                return getGenericMediaModel(context, tag, src, srme, part, region);
102            }
103        }
104
105        throw new IllegalArgumentException("Region not found or bad region ID.");
106    }
107
108    private static MediaModel getGenericMediaModel(Context context,
109            String tag, String src, SMILMediaElement sme, PduPart part,
110            RegionModel regionModel) throws DrmException, IOException, MmsException {
111        byte[] bytes = part.getContentType();
112        if (bytes == null) {
113            throw new IllegalArgumentException(
114                    "Content-Type of the part may not be null.");
115        }
116
117        String contentType = new String(bytes);
118        MediaModel media = null;
119        if (ContentType.isDrmType(contentType)) {
120            DrmWrapper wrapper = new DrmWrapper(
121                    contentType, part.getDataUri(), part.getData());
122            if (tag.equals(SmilHelper.ELEMENT_TAG_TEXT)) {
123                media = new TextModel(context, contentType, src,
124                        part.getCharset(), wrapper, regionModel);
125            } else if (tag.equals(SmilHelper.ELEMENT_TAG_IMAGE)) {
126                media = new ImageModel(context, contentType, src,
127                        wrapper, regionModel);
128            } else if (tag.equals(SmilHelper.ELEMENT_TAG_VIDEO)) {
129                media = new VideoModel(context, contentType, src,
130                        wrapper, regionModel);
131            } else if (tag.equals(SmilHelper.ELEMENT_TAG_AUDIO)) {
132                media = new AudioModel(context, contentType, src,
133                        wrapper);
134            } else if (tag.equals(SmilHelper.ELEMENT_TAG_REF)) {
135                String drmContentType = wrapper.getContentType();
136                if (ContentType.isTextType(drmContentType)) {
137                    media = new TextModel(context, contentType, src,
138                            part.getCharset(), wrapper, regionModel);
139                } else if (ContentType.isImageType(drmContentType)) {
140                    media = new ImageModel(context, contentType, src,
141                            wrapper, regionModel);
142                } else if (ContentType.isVideoType(drmContentType)) {
143                    media = new VideoModel(context, contentType, src,
144                            wrapper, regionModel);
145                } else if (ContentType.isAudioType(drmContentType)) {
146                    media = new AudioModel(context, contentType, src,
147                            wrapper);
148                } else {
149                    throw new UnsupportContentTypeException(
150                        "Unsupported Content-Type: " + drmContentType);
151                }
152            } else {
153                throw new IllegalArgumentException("Unsupported TAG: " + tag);
154            }
155        } else {
156            if (tag.equals(SmilHelper.ELEMENT_TAG_TEXT)) {
157                media = new TextModel(context, contentType, src,
158                        part.getCharset(), part.getData(), regionModel);
159            } else if (tag.equals(SmilHelper.ELEMENT_TAG_IMAGE)) {
160                media = new ImageModel(context, contentType, src,
161                        part.getDataUri(), regionModel);
162            } else if (tag.equals(SmilHelper.ELEMENT_TAG_VIDEO)) {
163                media = new VideoModel(context, contentType, src,
164                        part.getDataUri(), regionModel);
165            } else if (tag.equals(SmilHelper.ELEMENT_TAG_AUDIO)) {
166                media = new AudioModel(context, contentType, src,
167                        part.getDataUri());
168            } else if (tag.equals(SmilHelper.ELEMENT_TAG_REF)) {
169                if (ContentType.isTextType(contentType)) {
170                    media = new TextModel(context, contentType, src,
171                            part.getCharset(), part.getData(), regionModel);
172                } else if (ContentType.isImageType(contentType)) {
173                    media = new ImageModel(context, contentType, src,
174                            part.getDataUri(), regionModel);
175                } else if (ContentType.isVideoType(contentType)) {
176                    media = new VideoModel(context, contentType, src,
177                            part.getDataUri(), regionModel);
178                } else if (ContentType.isAudioType(contentType)) {
179                    media = new AudioModel(context, contentType, src,
180                            part.getDataUri());
181                } else {
182                    throw new UnsupportContentTypeException(
183                        "Unsupported Content-Type: " + contentType);
184                }
185            } else {
186                throw new IllegalArgumentException("Unsupported TAG: " + tag);
187            }
188        }
189
190        // Set 'begin' property.
191        int begin = 0;
192        TimeList tl = sme.getBegin();
193        if ((tl != null) && (tl.getLength() > 0)) {
194            // We only support a single begin value.
195            Time t = tl.item(0);
196            begin = (int) (t.getResolvedOffset() * 1000);
197        }
198        media.setBegin(begin);
199
200        // Set 'duration' property.
201        int duration = (int) (sme.getDur() * 1000);
202        if (duration <= 0) {
203            tl = sme.getEnd();
204            if ((tl != null) && (tl.getLength() > 0)) {
205                // We only support a single end value.
206                Time t = tl.item(0);
207                if (t.getTimeType() != Time.SMIL_TIME_INDEFINITE) {
208                    duration = (int) (t.getResolvedOffset() * 1000) - begin;
209                }
210            }
211        }
212        media.setDuration(duration);
213
214        // Set 'fill' property.
215        media.setFill(sme.getFill());
216        return media;
217    }
218}
219