OSDN Git Service

Handle album and artist view intents
authormyfreeweb <floatboth@me.com>
Wed, 12 Mar 2014 17:10:27 +0000 (21:10 +0400)
committermyfreeweb <floatboth@me.com>
Fri, 14 Mar 2014 16:18:10 +0000 (20:18 +0400)
Starting playback of an album (or all songs by artist) is very useful.
My current use case is a music library smartwatch app,
like Peapod for Pebble 1.0 and iOS.

Relevant SO question: http://stackoverflow.com/q/15571153/239140

Change-Id: Id6026176436be3d207c7fbf150044adfc14258e2

AndroidManifest.xml
src/com/andrew/apollo/ui/activities/AudioPlayerActivity.java
src/com/andrew/apollo/utils/MusicUtils.java

index c2d488d..2670d03 100644 (file)
                 <category android:name="android.intent.category.DEFAULT" />
 
                 <data android:mimeType="vnd.android.cursor.dir/playlist" />
+                <data android:mimeType="vnd.android.cursor.dir/albums" />
+                <data android:mimeType="vnd.android.cursor.dir/artists" />
             </intent-filter>
             <intent-filter>
                 <action android:name="com.andrew.apollo.AUDIO_PLAYER" />
index 8586569..bc91310 100644 (file)
@@ -32,6 +32,8 @@ import android.os.IBinder;
 import android.os.Message;
 import android.os.SystemClock;
 import android.provider.MediaStore.Audio.Playlists;
+import android.provider.MediaStore.Audio.Albums;
+import android.provider.MediaStore.Audio.Artists;
 import android.support.v4.app.FragmentActivity;
 import android.support.v4.view.ViewPager;
 import android.view.Menu;
@@ -559,6 +561,22 @@ public class AudioPlayerActivity extends FragmentActivity implements ServiceConn
 
     }
 
+    private long parseIdFromIntent(Intent intent, String longKey,
+        String stringKey, long defaultId) {
+        long id = intent.getLongExtra(longKey, -1);
+        if (id < 0) {
+            String idString = intent.getStringExtra(stringKey);
+            if (idString != null) {
+                try {
+                    id = Long.parseLong(idString);
+                } catch (NumberFormatException e) {
+                    // ignore
+                }
+            }
+        }
+        return id;
+    }
+
     /**
      * Checks whether the passed intent contains a playback request,
      * and starts playback if that's the case
@@ -578,21 +596,25 @@ public class AudioPlayerActivity extends FragmentActivity implements ServiceConn
             MusicUtils.playFile(this, uri);
             handled = true;
         } else if (Playlists.CONTENT_TYPE.equals(mimeType)) {
-            long id = intent.getLongExtra("playlistId", -1);
-            if (id < 0) {
-                String idString = intent.getStringExtra("playlist");
-                if (idString != null) {
-                    try {
-                        id = Long.parseLong(idString);
-                    } catch (NumberFormatException e) {
-                        // ignore
-                    }
-                }
-            }
+            long id = parseIdFromIntent(intent, "playlistId", "playlist", -1);
             if (id >= 0) {
                 MusicUtils.playPlaylist(this, id);
                 handled = true;
             }
+        } else if (Albums.CONTENT_TYPE.equals(mimeType)) {
+            long id = parseIdFromIntent(intent, "albumId", "album", -1);
+            if (id >= 0) {
+                int position = intent.getIntExtra("position", 0);
+                MusicUtils.playAlbum(this, id, position);
+                handled = true;
+            }
+        } else if (Artists.CONTENT_TYPE.equals(mimeType)) {
+            long id = parseIdFromIntent(intent, "artistId", "artist", -1);
+            if (id >= 0) {
+                int position = intent.getIntExtra("position", 0);
+                MusicUtils.playArtist(this, id, position);
+                handled = true;
+            }
         }
 
         if (handled) {
index 1998a8c..4f47e81 100644 (file)
@@ -543,6 +543,20 @@ public final class MusicUtils {
     }
 
     /**
+     * Plays songs by an artist.
+     *
+     * @param context The {@link Context} to use.
+     * @param artistId The artist Id.
+     * @param position Specify where to start.
+     */
+    public static void playArtist(final Context context, final long artistId, int position) {
+        final long[] artistList = getSongListForArtist(context, artistId);
+        if (artistList != null) {
+            playAll(context, artistList, position, false);
+        }
+    }
+
+    /**
      * @param context The {@link Context} to use.
      * @param id The ID of the genre.
      * @return The song list for an genre.
@@ -751,6 +765,20 @@ public final class MusicUtils {
         return id;
     }
 
+    /**
+     * Plays songs from an album.
+     *
+     * @param context The {@link Context} to use.
+     * @param albumId The album Id.
+     * @param position Specify where to start.
+     */
+    public static void playAlbum(final Context context, final long albumId, int position) {
+        final long[] albumList = getSongListForAlbum(context, albumId);
+        if (albumList != null) {
+            playAll(context, albumList, position, false);
+        }
+    }
+
     /*  */
     public static void makeInsertItems(final long[] ids, final int offset, int len, final int base) {
         if (offset + len > ids.length) {