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