• 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 android.drm.mobile1.DrmException;
22 import com.android.mms.drm.DrmWrapper;
23 import com.google.android.mms.pdu.CharacterSets;
24 
25 import org.w3c.dom.events.Event;
26 import org.w3c.dom.smil.ElementTime;
27 
28 import android.content.Context;
29 import android.util.Log;
30 
31 import java.io.IOException;
32 import java.io.UnsupportedEncodingException;
33 
34 public class TextModel extends RegionMediaModel {
35     private static final String TAG = "Mms/text";
36 
37     private CharSequence mText;
38     private final int mCharset;
39 
TextModel(Context context, String contentType, String src, RegionModel region)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 
TextModel(Context context, String contentType, String src, int charset, byte[] data, RegionModel region)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 
extractTextFromData(byte[] data)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 
TextModel(Context context, String contentType, String src, int charset, DrmWrapper wrapper, RegionModel regionModel)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 
getText()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 
setText(CharSequence text)108     public void setText(CharSequence text) {
109         mText = text;
110         notifyModelChanged(true);
111     }
112 
cloneText()113     public void cloneText() {
114         mText = new String(mText.toString());
115     }
116 
getCharset()117     public int getCharset() {
118         return mCharset;
119     }
120 
121     // EventListener Interface
handleEvent(Event evt)122     public void handleEvent(Event evt) {
123         if (evt.getType().equals(SmilMediaElementImpl.SMIL_MEDIA_START_EVENT)) {
124             mVisible = true;
125         } else if (mFill != ElementTime.FILL_FREEZE) {
126             mVisible = false;
127         }
128 
129         notifyModelChanged(false);
130     }
131 }
132