Home | History | Annotate | Download | only in model
      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 java.io.UnsupportedEncodingException;
     21 
     22 import org.w3c.dom.events.Event;
     23 import org.w3c.dom.smil.ElementTime;
     24 
     25 import android.content.Context;
     26 import android.util.Log;
     27 
     28 import com.android.mms.LogTag;
     29 import com.android.mms.dom.smil.SmilMediaElementImpl;
     30 import com.google.android.mms.pdu.CharacterSets;
     31 
     32 public class TextModel extends RegionMediaModel {
     33     private static final String TAG = LogTag.TAG;
     34 
     35     private CharSequence mText;
     36     private final int mCharset;
     37 
     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 
     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 
     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 
     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 
     88     public void setText(CharSequence text) {
     89         mText = text;
     90         notifyModelChanged(true);
     91     }
     92 
     93     public void cloneText() {
     94         mText = new String((mText != null ? mText.toString() : ""));
     95     }
     96 
     97     public int getCharset() {
     98         return mCharset;
     99     }
    100 
    101     // EventListener Interface
    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