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