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