TextModel.java revision abd7b2d90f7491075f1daba4b4cccdfc82f8ddd1
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.dom.smil.SmilMediaElementImpl;
21import com.android.mms.drm.DrmWrapper;
22import com.google.android.mms.pdu.CharacterSets;
23
24import org.w3c.dom.events.Event;
25import org.w3c.dom.smil.ElementTime;
26
27import android.content.Context;
28import android.drm.mobile1.DrmException;
29import android.util.Log;
30
31import java.io.IOException;
32import java.io.UnsupportedEncodingException;
33
34public class TextModel extends RegionMediaModel {
35    private static final String TAG = "TextModel";
36
37    private CharSequence mText;
38    private final int mCharset;
39
40    public TextModel(Context context, String contentType, String src, RegionModel region) {
41        this(context, contentType, src, CharacterSets.UTF_8, new byte[0], region);
42    }
43
44    public TextModel(Context context, String contentType, String src,
45            int charset, byte[] data, RegionModel region) {
46        super(context, SmilHelper.ELEMENT_TAG_TEXT, contentType, src,
47                data != null ? data : new byte[0], region);
48
49        if (charset == CharacterSets.ANY_CHARSET) {
50            // By default, we use ISO_8859_1 to decode the data
51            // which character set wasn't set.
52            charset = CharacterSets.ISO_8859_1;
53        }
54        mCharset = charset;
55        mText = extractTextFromData(data);
56    }
57
58    private CharSequence extractTextFromData(byte[] data) {
59        if (data != null) {
60            try {
61                if (CharacterSets.ANY_CHARSET == mCharset) {
62                    return new String(data); // system default encoding.
63                } else {
64                    String name = CharacterSets.getMimeName(mCharset);
65                    return new String(data, name);
66                }
67            } catch (UnsupportedEncodingException e) {
68                Log.e(TAG, "Unsupported encoding: " + mCharset, e);
69                return new String(data); // system default encoding.
70            }
71        }
72        return "";
73    }
74
75    public TextModel(Context context, String contentType, String src, int charset,
76            DrmWrapper wrapper, RegionModel regionModel) throws IOException {
77        super(context, SmilHelper.ELEMENT_TAG_TEXT, contentType, src, wrapper, regionModel);
78
79        if (charset == CharacterSets.ANY_CHARSET) {
80            // By default, we use ISO_8859_1 to decode the data
81            // which character set wasn't set.
82            charset = CharacterSets.ISO_8859_1;
83        }
84        mCharset = charset;
85    }
86
87    public String getText() {
88        if (mText == null) {
89            try {
90                mText = extractTextFromData(getData());
91            } catch (DrmException e) {
92                Log.e(TAG, e.getMessage(), e);
93                // Display DRM error message in place.
94                mText = e.getMessage();
95            }
96        }
97
98        // If our internal CharSequence is not already a String,
99        // re-save it as a String so subsequent calls to getText will
100        // be less expensive.
101        if (!(mText instanceof String)) {
102            mText = mText.toString();
103        }
104
105        return mText.toString();
106    }
107
108    public void setText(CharSequence text) {
109        mText = text;
110        notifyModelChanged(true);
111    }
112
113    public int getCharset() {
114        return mCharset;
115    }
116
117    // EventListener Interface
118    public void handleEvent(Event evt) {
119        if (evt.getType().equals(SmilMediaElementImpl.SMIL_MEDIA_START_EVENT)) {
120            mVisible = true;
121        } else if (mFill != ElementTime.FILL_FREEZE) {
122            mVisible = false;
123        }
124
125        notifyModelChanged(false);
126    }
127}
128