diff options
author | Rohit Yengisetty <rohit@cyngn.com> | 2014-10-21 17:24:59 -0700 |
---|---|---|
committer | linus_lee <llee@cyngn.com> | 2014-11-20 12:51:34 -0800 |
commit | 5a2209133a66ae55a0f9028423ce5cd1ed384d91 (patch) | |
tree | 73b4fb170636f88c74b2b8a39525bbb46f5c9e0d /src/com | |
parent | 4d3a310950ec3e6b7ccddf195808f7d319f9ed4e (diff) | |
download | android_packages_apps_Eleven-5a2209133a66ae55a0f9028423ce5cd1ed384d91.tar.gz android_packages_apps_Eleven-5a2209133a66ae55a0f9028423ce5cd1ed384d91.tar.bz2 android_packages_apps_Eleven-5a2209133a66ae55a0f9028423ce5cd1ed384d91.zip |
Eleven - Fix MusicPlaybackSerivce to show the proper metadata when a
downloaded audio file is playing. A call to notify UI of a change in
the song metada was missing which consequently led to song information
not updating in the audio player UI. The ability to handle download
uri ("content://downloads/*") has been added.
The Jira ticket states that there itself isn't working, but
MediaPlayer was able to handle download uri and playback the audio
file. There was code missing to get audio info in such a case.
https://cyanogen.atlassian.net/browse/MUSIC-4
Change-Id: I2c642c337c42c28ac28237de5a655ee55b5b8d33
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/cyngn/eleven/MusicPlaybackService.java | 105 |
1 files changed, 97 insertions, 8 deletions
diff --git a/src/com/cyngn/eleven/MusicPlaybackService.java b/src/com/cyngn/eleven/MusicPlaybackService.java index 443088c..a39577e 100644 --- a/src/com/cyngn/eleven/MusicPlaybackService.java +++ b/src/com/cyngn/eleven/MusicPlaybackService.java @@ -24,6 +24,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.database.Cursor; +import android.database.MatrixCursor; import android.graphics.Bitmap; import android.media.AudioManager; import android.media.AudioManager.OnAudioFocusChangeListener; @@ -44,6 +45,7 @@ import android.os.SystemClock; import android.provider.MediaStore; import android.provider.MediaStore.Audio.AlbumColumns; import android.provider.MediaStore.Audio.AudioColumns; +import android.text.TextUtils; import android.util.Log; import com.cyngn.eleven.Config.IdType; @@ -1538,40 +1540,62 @@ public class MusicPlaybackService extends Service { // If mCursor is null, try to associate path with a database cursor if (mCursor == null) { Uri uri = Uri.parse(path); + boolean shouldAddToPlaylist = true; // should try adding audio info to playlist long id = -1; - try { + try { id = Long.valueOf(uri.getLastPathSegment()); } catch (NumberFormatException ex) { // Ignore } - if (id != -1 && path.startsWith(MediaStore.Audio.Media. - EXTERNAL_CONTENT_URI.toString())) { + if (id != -1 && path.startsWith( + MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString())) { updateCursor(uri); - } else if (id != -1 && path.startsWith(MediaStore.Files.getContentUri( - "external").toString())) { + } else if (id != -1 && path.startsWith( + MediaStore.Files.getContentUri("external").toString())) { updateCursor(id); + // handle downloaded media files + } else if ( path.startsWith("content://downloads/") ) { + + // extract MediaProvider(MP) uri , if available + // Downloads.Impl.COLUMN_MEDIAPROVIDER_URI + String mpUri = getValueForDownloadedFile(this, uri, "mediaprovider_uri"); + if (D) Log.i(TAG, "Downloaded file's MP uri : " + mpUri); + if ( !TextUtils.isEmpty(mpUri) ) { + // if mpUri is valid, play that URI instead + return openFile(mpUri); + } else { + // create phantom cursor with download info, if a MP uri wasn't found + updateCursorForDownloadedFile(this, uri); + shouldAddToPlaylist = false; // song info isn't available in MediaStore + } + } else { + // assuming a "file://" uri by this point ... String where = MediaStore.Audio.Media.DATA + "=?"; - String[] selectionArgs = new String[] {path}; + String[] selectionArgs = new String[]{path}; updateCursor(where, selectionArgs); } try { - if (mCursor != null) { + if (mCursor != null && shouldAddToPlaylist) { mPlaylist.clear(); - mPlaylist.add(new MusicPlaybackTrack(mCursor.getLong(IDCOLIDX), -1, IdType.NA, -1)); + mPlaylist.add(new MusicPlaybackTrack( + mCursor.getLong(IDCOLIDX), -1, IdType.NA, -1)); mPlayPos = 0; mHistory.clear(); } } catch (final UnsupportedOperationException ex) { + // Ignore } } + mFileToPlay = path; mPlayer.setDataSource(mFileToPlay); if (mPlayer.isInitialized()) { mOpenFailedCounter = 0; + notifyChange(META_CHANGED); // notify impending change in track return true; } stop(true); @@ -1579,6 +1603,71 @@ public class MusicPlaybackService extends Service { } } + /* + Columns for a pseudo cursor we are creating for downloaded songs + Modeled after mCursor to be able to respond to respond to the same queries as it + */ + private static final String[] PROJECTION_MATRIX = new String[] { + "_id", MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, + MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, + MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.ALBUM_ID, + MediaStore.Audio.Media.ARTIST_ID + }; + + /** + * Creates a pseudo cursor for downloaded audio files with minimal info + * @param context needed to query the download uri + * @param uri the uri of the downloaded file + */ + private void updateCursorForDownloadedFile(Context context, Uri uri) { + synchronized (this) { + closeCursor(); // clear mCursor + MatrixCursor cursor = new MatrixCursor(PROJECTION_MATRIX); + // get title of the downloaded file ; Downloads.Impl.COLUMN_TITLE + String title = getValueForDownloadedFile(this, uri, "title" ); + // populating the cursor with bare minimum info + cursor.addRow(new Object[] { + null , + null , + null , + title , + null , + null , + null , + null + }); + mCursor = cursor; + mCursor.moveToFirst(); + } + } + + /** + * Query the DownloadProvider to get the value in the specified column + * @param context + * @param uri the uri of the downloaded file + * @param column + * @return + */ + private String getValueForDownloadedFile(Context context, Uri uri, String column) { + + Cursor cursor = null; + final String[] projection = { + column + }; + + try { + cursor = context.getContentResolver().query(uri, projection, null, null, null); + if (cursor != null && cursor.moveToFirst()) { + return cursor.getString(0); + } + } finally { + if (cursor != null) { + cursor.close(); + } + } + return null; + } + /** * Returns the audio session ID * |