AudioModel.java revision 72735c62aba8fd2a9420a0f9f83d22543e3c164f
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;
27
28import android.content.ContentResolver;
29import android.content.Context;
30import android.database.Cursor;
31import android.net.Uri;
32import android.provider.MediaStore.Audio;
33import android.provider.Telephony.Mms.Part;
34import android.text.TextUtils;
35import android.util.Config;
36import android.util.Log;
37
38import java.io.IOException;
39import java.util.HashMap;
40import java.util.Map;
41
42public class AudioModel extends MediaModel {
43    private static final String TAG = "AudioModel";
44    private static final boolean DEBUG = false;
45    private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV;
46
47    private final HashMap<String, String> mExtras;
48
49    public AudioModel(Context context, Uri uri) throws MmsException {
50        this(context, null, null, uri);
51        initModelFromUri(uri);
52        checkContentRestriction();
53    }
54
55    public AudioModel(Context context, String contentType, String src, Uri uri) throws MmsException {
56        super(context, SmilHelper.ELEMENT_TAG_AUDIO, contentType, src, uri);
57        mExtras = new HashMap<String, String>();
58    }
59
60    public AudioModel(Context context, String contentType, String src,
61            DrmWrapper wrapper) throws IOException {
62        super(context, SmilHelper.ELEMENT_TAG_AUDIO, contentType, src, wrapper);
63        mExtras = new HashMap<String, String>();
64    }
65
66    private void initModelFromUri(Uri uri) throws MmsException {
67        ContentResolver cr = mContext.getContentResolver();
68        Cursor c = SqliteWrapper.query(mContext, cr, uri, null, null, null, null);
69
70        if (c != null) {
71            try {
72                if (c.moveToFirst()) {
73                    String path;
74                    boolean isFromMms = isMmsUri(uri);
75
76                    // FIXME We suppose that there should be only two sources
77                    // of the audio, one is the media store, the other is
78                    // our MMS database.
79                    if (isFromMms) {
80                        path = c.getString(c.getColumnIndexOrThrow(Part._DATA));
81                        mContentType = c.getString(c.getColumnIndexOrThrow(Part.CONTENT_TYPE));
82                    } else {
83                        path = c.getString(c.getColumnIndexOrThrow(Audio.Media.DATA));
84                        mContentType = c.getString(c.getColumnIndexOrThrow(
85                                Audio.Media.MIME_TYPE));
86                        // Get more extras information which would be useful
87                        // to the user.
88                        String album = c.getString(c.getColumnIndexOrThrow("album"));
89                        if (!TextUtils.isEmpty(album)) {
90                            mExtras.put("album", album);
91                        }
92
93                        String artist = c.getString(c.getColumnIndexOrThrow("artist"));
94                        if (!TextUtils.isEmpty(artist)) {
95                            mExtras.put("artist", artist);
96                        }
97                    }
98                    mSrc = path.substring(path.lastIndexOf('/') + 1);
99
100                    if (TextUtils.isEmpty(mContentType)) {
101                        throw new MmsException("Type of media is unknown.");
102                    }
103
104                    if (LOCAL_LOGV) {
105                        Log.v(TAG, "New AudioModel created:"
106                                + " mSrc=" + mSrc
107                                + " mContentType=" + mContentType
108                                + " mUri=" + uri
109                                + " mExtras=" + mExtras);
110                    }
111                } else {
112                    throw new MmsException("Nothing found: " + uri);
113                }
114            } finally {
115                c.close();
116            }
117        } else {
118            throw new MmsException("Bad URI: " + uri);
119        }
120
121        initMediaDuration();
122    }
123
124    public void stop() {
125        appendAction(MediaAction.STOP);
126        notifyModelChanged(false);
127    }
128
129    public void handleEvent(Event evt) {
130        String evtType = evt.getType();
131        if (LOCAL_LOGV) {
132            Log.v(TAG, "Handling event: " + evtType + " on " + this);
133        }
134
135        MediaAction action = MediaAction.NO_ACTIVE_ACTION;
136        if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_START_EVENT)) {
137            action = MediaAction.START;
138        } else if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_END_EVENT)) {
139            action = MediaAction.STOP;
140        } else if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_PAUSE_EVENT)) {
141            action = MediaAction.PAUSE;
142        } else if (evtType.equals(SmilMediaElementImpl.SMIL_MEDIA_SEEK_EVENT)) {
143            action = MediaAction.SEEK;
144            mSeekTo = evt.getSeekTo();
145        }
146
147        appendAction(action);
148        notifyModelChanged(false);
149    }
150
151    public Map<String, ?> getExtras() {
152        return mExtras;
153    }
154
155    protected void checkContentRestriction() throws ContentRestrictionException {
156        ContentRestriction cr = ContentRestrictionFactory.getContentRestriction();
157        cr.checkAudioContentType(mContentType);
158    }
159
160    @Override
161    protected boolean isPlayable() {
162        return true;
163    }
164}
165