VideoModel.java revision 8eed706474910ccb978acda03e85d3261037da6e
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 com.android.mms.ContentRestrictionException;
21import com.android.mms.dom.smil.SmilMediaElementImpl;
22import com.android.mms.drm.DrmWrapper;
23import com.google.android.mms.MmsException;
24import com.google.android.mms.util.SqliteWrapper;
25
26import org.w3c.dom.events.Event;
27import org.w3c.dom.smil.ElementTime;
28
29import android.content.ContentResolver;
30import android.content.Context;
31import android.database.Cursor;
32import android.net.Uri;
33import android.provider.MediaStore.Images;
34import android.text.TextUtils;
35import android.util.Config;
36import android.util.Log;
37
38import java.io.IOException;
39
40public class VideoModel extends RegionMediaModel {
41    private static final String TAG = "VideoModel";
42    private static final boolean DEBUG = false;
43    private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV;
44
45    public VideoModel(Context context, Uri uri, RegionModel region)
46            throws MmsException {
47        this(context, null, null, uri, region);
48        initModelFromUri(uri);
49        checkContentRestriction();
50    }
51
52    public VideoModel(Context context, String contentType, String src,
53            Uri uri, RegionModel region) {
54        super(context, SmilHelper.ELEMENT_TAG_VIDEO, contentType, src, uri, region);
55    }
56
57    public VideoModel(Context context, String contentType, String src,
58            DrmWrapper wrapper, RegionModel regionModel) throws IOException {
59        super(context, SmilHelper.ELEMENT_TAG_VIDEO, contentType, src, wrapper, regionModel);
60    }
61
62    private void initModelFromUri(Uri uri) throws MmsException {
63        ContentResolver cr = mContext.getContentResolver();
64        Cursor c = SqliteWrapper.query(mContext, cr, uri, null, null, null, null);
65
66        if (c != null) {
67            try {
68                if (c.moveToFirst()) {
69                    String path = c.getString(c.getColumnIndexOrThrow(Images.Media.DATA));
70                    mSrc = path.substring(path.lastIndexOf('/') + 1);
71                    mContentType = c.getString(c.getColumnIndexOrThrow(
72                            Images.Media.MIME_TYPE));
73                    if (TextUtils.isEmpty(mContentType)) {
74                        throw new MmsException("Type of media is unknown.");
75                    }
76
77                    if (LOCAL_LOGV) {
78                        Log.v(TAG, "New VideoModel created:"
79                                + " mSrc=" + mSrc
80                                + " mContentType=" + mContentType
81                                + " mUri=" + uri);
82                    }
83                } else {
84                    throw new MmsException("Nothing found: " + uri);
85                }
86            } finally {
87                c.close();
88            }
89        } else {
90            throw new MmsException("Bad URI: " + uri);
91        }
92
93        initMediaDuration();
94    }
95
96    // EventListener Interface
97    public void handleEvent(Event evt) {
98        String evtType = evt.getType();
99        if (LOCAL_LOGV) {
100            Log.v(TAG, "Handling event: " + evt.getType() + " on " + this);
101        }
102
103        MediaAction action = MediaAction.NO_ACTIVE_ACTION;
104        if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_START_EVENT)) {
105            action = MediaAction.START;
106            mVisible = true;
107        } else if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_END_EVENT)) {
108            action = MediaAction.STOP;
109            if (mFill != ElementTime.FILL_FREEZE) {
110                mVisible = false;
111            }
112        } else if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_PAUSE_EVENT)) {
113            action = MediaAction.PAUSE;
114            mVisible = true;
115        } else if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_SEEK_EVENT)) {
116            action = MediaAction.SEEK;
117            mSeekTo = evt.getSeekTo();
118            mVisible = true;
119        }
120
121        appendAction(action);
122        notifyModelChanged(false);
123    }
124
125    protected void checkContentRestriction() throws ContentRestrictionException {
126        ContentRestriction cr = ContentRestrictionFactory.getContentRestriction();
127        cr.checkVideoContentType(mContentType);
128    }
129
130    @Override
131    protected boolean isPlayable() {
132        return true;
133    }
134}
135