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 com.android.mms.ContentRestrictionException;
     21 import com.android.mms.LogTag;
     22 import com.android.mms.dom.events.EventImpl;
     23 import com.android.mms.dom.smil.SmilMediaElementImpl;
     24 import com.android.mms.drm.DrmWrapper;
     25 import com.google.android.mms.MmsException;
     26 import android.database.sqlite.SqliteWrapper;
     27 
     28 import org.w3c.dom.events.Event;
     29 import org.w3c.dom.smil.ElementTime;
     30 
     31 import android.content.ContentResolver;
     32 import android.content.Context;
     33 import android.database.Cursor;
     34 import android.net.Uri;
     35 import android.provider.MediaStore.Images;
     36 import android.text.TextUtils;
     37 import android.util.Config;
     38 import android.util.Log;
     39 
     40 import com.google.android.mms.ContentType;
     41 import java.io.IOException;
     42 
     43 public class VideoModel extends RegionMediaModel {
     44     private static final String TAG = MediaModel.TAG;
     45     private static final boolean DEBUG = true;
     46     private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV;
     47 
     48     public VideoModel(Context context, Uri uri, RegionModel region)
     49             throws MmsException {
     50         this(context, null, null, uri, region);
     51         initModelFromUri(uri);
     52         checkContentRestriction();
     53     }
     54 
     55     public VideoModel(Context context, String contentType, String src,
     56             Uri uri, RegionModel region) throws MmsException {
     57         super(context, SmilHelper.ELEMENT_TAG_VIDEO, contentType, src, uri, region);
     58     }
     59 
     60     public VideoModel(Context context, String contentType, String src,
     61             DrmWrapper wrapper, RegionModel regionModel) throws IOException {
     62         super(context, SmilHelper.ELEMENT_TAG_VIDEO, contentType, src, wrapper, regionModel);
     63     }
     64 
     65     private void initModelFromUri(Uri uri) throws MmsException {
     66         ContentResolver cr = mContext.getContentResolver();
     67         Cursor c = SqliteWrapper.query(mContext, cr, uri, null, null, null, null);
     68 
     69         if (c != null) {
     70             try {
     71                 if (c.moveToFirst()) {
     72                     String path = c.getString(c.getColumnIndexOrThrow(Images.Media.DATA));
     73                     mSrc = path.substring(path.lastIndexOf('/') + 1);
     74                     mContentType = c.getString(c.getColumnIndexOrThrow(
     75                             Images.Media.MIME_TYPE));
     76                     if (TextUtils.isEmpty(mContentType)) {
     77                         throw new MmsException("Type of media is unknown.");
     78                     }
     79 
     80                     if (mContentType.equals(ContentType.VIDEO_MP4) && !(TextUtils.isEmpty(mSrc))) {
     81                         int index = mSrc.lastIndexOf(".");
     82                         if (index != -1) {
     83                             try {
     84                                 String extension = mSrc.substring(index + 1);
     85                                 if (!(TextUtils.isEmpty(extension)) &&
     86                                         (extension.equalsIgnoreCase("3gp") ||
     87                                         extension.equalsIgnoreCase("3gpp") ||
     88                                         extension.equalsIgnoreCase("3g2"))) {
     89                                     mContentType = ContentType.VIDEO_3GPP;
     90                                 }
     91                             } catch(IndexOutOfBoundsException ex) {
     92                                 if (LOCAL_LOGV) {
     93                                     Log.v(TAG, "Media extension is unknown.");
     94                                 }
     95                             }
     96                         }
     97                     }
     98 
     99                     if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
    100                         Log.v(TAG, "New VideoModel created:"
    101                                 + " mSrc=" + mSrc
    102                                 + " mContentType=" + mContentType
    103                                 + " mUri=" + uri);
    104                     }
    105                 } else {
    106                     throw new MmsException("Nothing found: " + uri);
    107                 }
    108             } finally {
    109                 c.close();
    110             }
    111         } else {
    112             throw new MmsException("Bad URI: " + uri);
    113         }
    114 
    115         initMediaDuration();
    116     }
    117 
    118     // EventListener Interface
    119     public void handleEvent(Event evt) {
    120         String evtType = evt.getType();
    121         if (LOCAL_LOGV || Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
    122             Log.v(TAG, "[VideoModel] handleEvent " + evt.getType() + " on " + this);
    123         }
    124 
    125         MediaAction action = MediaAction.NO_ACTIVE_ACTION;
    126         if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_START_EVENT)) {
    127             action = MediaAction.START;
    128 
    129             // if the Music player app is playing audio, we should pause that so it won't
    130             // interfere with us playing video here.
    131             pauseMusicPlayer();
    132 
    133             mVisible = true;
    134         } else if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_END_EVENT)) {
    135             action = MediaAction.STOP;
    136             if (mFill != ElementTime.FILL_FREEZE) {
    137                 mVisible = false;
    138             }
    139         } else if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_PAUSE_EVENT)) {
    140             action = MediaAction.PAUSE;
    141             mVisible = true;
    142         } else if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_SEEK_EVENT)) {
    143             action = MediaAction.SEEK;
    144             mSeekTo = ((EventImpl) evt).getSeekTo();
    145             mVisible = true;
    146         }
    147 
    148         appendAction(action);
    149         notifyModelChanged(false);
    150     }
    151 
    152     protected void checkContentRestriction() throws ContentRestrictionException {
    153         ContentRestriction cr = ContentRestrictionFactory.getContentRestriction();
    154         cr.checkVideoContentType(mContentType);
    155     }
    156 
    157     @Override
    158     protected boolean isPlayable() {
    159         return true;
    160     }
    161 }
    162