FileDocCategorySizeDatePackage
MusicProvider.javaAPI DocAndroid 5.1 API10122Thu Mar 12 22:22:44 GMT 2015com.example.android.musicservicedemo.browser

MusicProvider

public class MusicProvider extends Object
Utility class to get a list of MusicTrack's based on a server-side JSON configuration.

Fields Summary
private static final String
TAG
private static final String
MUSIC_URL
private static String
MUSIC
private static String
TITLE
private static String
ALBUM
private static String
ARTIST
private static String
GENRE
private static String
SOURCE
private static String
IMAGE
private static String
TRACK_NUMBER
private static String
TOTAL_TRACK_COUNT
private static String
DURATION
private static List
mMusicList
Constructors Summary
Methods Summary
public java.util.ListgetMedia()
Get the cached list of music tracks

return
throws
JSONException


                   
         
        if (null != mMusicList && mMusicList.size() > 0) {
            return mMusicList;
        }
        return null;
    
public android.database.MatrixCursorgetRootContainerCurser()

        MatrixCursor matrixCursor = new MatrixCursor(BrowserService.MEDIA_CONTAINER_PROJECTION);
        Uri.Builder pianoBuilder = new Uri.Builder();
        pianoBuilder.authority(BrowserService.AUTHORITY);
        pianoBuilder.appendPath(BrowserService.PIANO_BASE_PATH);
        matrixCursor.addRow(new Object[] {
                pianoBuilder.build(),
                BrowserService.PIANO_BASE_PATH,
                "subtitle",
                null,
                0
        });

        Uri.Builder voiceBuilder = new Uri.Builder();
        voiceBuilder.authority(BrowserService.AUTHORITY);
        voiceBuilder.appendPath(BrowserService.VOICE_BASE_PATH);
        matrixCursor.addRow(new Object[] {
                voiceBuilder.build(),
                BrowserService.VOICE_BASE_PATH,
                "subtitle",
                null,
                0
        });
        return matrixCursor;
    
public android.database.MatrixCursorgetRootItemCursor(int type)

        if (type == BrowserService.NOW_PLAYING) {
            MatrixCursor matrixCursor = new MatrixCursor(BrowserService.MEDIA_CONTAINER_PROJECTION);

            try {
                // Just return all of the tracks for now
                List<MusicTrack> musicTracks = retreiveMedia();
                for (MusicTrack musicTrack : musicTracks) {
                    Uri.Builder builder = new Uri.Builder();
                    builder.authority(BrowserService.AUTHORITY);
                    builder.appendPath(BrowserService.NOW_PLAYING_PATH);
                    builder.appendPath(musicTrack.getTitle());
                    matrixCursor.addRow(new Object[] {
                            builder.build(),
                            musicTrack.getTitle(),
                            musicTrack.getArtist(),
                            musicTrack.getImage(),
                            PlaybackState.ACTION_PLAY
                    });
                    Log.d(TAG, "Uri " + builder.build());
                }
            } catch (JSONException e) {
                Log.e(TAG, "::getRootItemCursor:", e);
            }

            Log.d(TAG, "cursor: " + matrixCursor.getCount());
            return matrixCursor;
        } else if (type == BrowserService.PIANO) {
            MatrixCursor matrixCursor = new MatrixCursor(BrowserService.MEDIA_CONTAINER_PROJECTION);

            try {
                List<MusicTrack> musicTracks = retreiveMedia();
                for (MusicTrack musicTrack : musicTracks) {
                    Uri.Builder builder = new Uri.Builder();
                    builder.authority(BrowserService.AUTHORITY);
                    builder.appendPath(BrowserService.PIANO_BASE_PATH);
                    builder.appendPath(musicTrack.getTitle());
                    matrixCursor.addRow(new Object[] {
                            builder.build(),
                            musicTrack.getTitle(),
                            musicTrack.getArtist(),
                            musicTrack.getImage(),
                            PlaybackState.ACTION_PLAY
                    });
                    Log.d(TAG, "Uri " + builder.build());
                }
            } catch (JSONException e) {
                Log.e(TAG, "::getRootItemCursor:", e);
            }

            Log.d(TAG, "cursor: " + matrixCursor.getCount());
            return matrixCursor;
        } else if (type == BrowserService.VOICE) {
            MatrixCursor matrixCursor = new MatrixCursor(BrowserService.MEDIA_CONTAINER_PROJECTION);

            try {
                List<MusicTrack> musicTracks = retreiveMedia();
                for (MusicTrack musicTrack : musicTracks) {
                    Uri.Builder builder = new Uri.Builder();
                    builder.authority(BrowserService.AUTHORITY);
                    builder.appendPath(BrowserService.VOICE_BASE_PATH);
                    builder.appendPath(musicTrack.getTitle());
                    matrixCursor.addRow(new Object[] {
                            builder.build(),
                            musicTrack.getTitle(),
                            musicTrack.getArtist(),
                            musicTrack.getImage(),
                            PlaybackState.ACTION_PLAY
                    });
                    Log.d(TAG, "Uri " + builder.build());
                }
            } catch (JSONException e) {
                Log.e(TAG, "::getRootItemCursor:", e);
            }

            Log.d(TAG, "cursor: " + matrixCursor.getCount());
            return matrixCursor;

        }
        return null;
    
private org.json.JSONObjectparseUrl(java.lang.String urlString)
Download a JSON file from a server, parse the content and return the JSON object.

param
urlString
return

        InputStream is = null;
        try {
            java.net.URL url = new java.net.URL(urlString);
            URLConnection urlConnection = url.openConnection();
            is = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    urlConnection.getInputStream(), "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            return new JSONObject(sb.toString());
        } catch (Exception e) {
            Log.d(TAG, "Failed to parse the json for media list", e);
            return null;
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    
public java.util.ListretreiveMedia()
Get the list of music tracks from a server and return the list of MusicTrack objects.

return
throws
JSONException

        if (null != mMusicList) {
            return mMusicList;
        }
        int slashPos = MUSIC_URL.lastIndexOf('/");
        String path = MUSIC_URL.substring(0, slashPos + 1);
        JSONObject jsonObj = parseUrl(MUSIC_URL);

        try {
            JSONArray videos = jsonObj.getJSONArray(MUSIC);
            if (null != videos) {
                mMusicList = new ArrayList<MusicTrack>();
                for (int j = 0; j < videos.length(); j++) {
                    JSONObject music = videos.getJSONObject(j);
                    String title = music.getString(TITLE);
                    String album = music.getString(ALBUM);
                    String artist = music.getString(ARTIST);
                    String genre = music.getString(GENRE);
                    String source = music.getString(SOURCE);
                    // Media is stored relative to JSON file
                    if (!source.startsWith("http")) {
                        source = path + source;
                    }
                    String image = music.getString(IMAGE);
                    if (!image.startsWith("http")) {
                        image = path + image;
                    }
                    int trackNumber = music.getInt(TRACK_NUMBER);
                    int totalTrackCount = music.getInt(TOTAL_TRACK_COUNT);
                    int duration = music.getInt(DURATION) * 1000; // ms

                    mMusicList.add(new MusicTrack(title, album, artist, genre, source,
                            image, trackNumber, totalTrackCount, duration));
                }
            }
        } catch (NullPointerException e) {
            Log.e(TAG, "retreiveMedia", e);
        }
        return mMusicList;