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