1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.music;
18 
19 import android.app.PendingIntent;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.media.browse.MediaBrowser.MediaItem;
23 import android.media.session.MediaSession;
24 import android.media.session.PlaybackState;
25 import android.os.Bundle;
26 import android.service.media.MediaBrowserService;
27 import android.util.Log;
28 
29 import java.util.*;
30 
31 /**
32  * Provides "background" audio playback capabilities, allowing the
33  * user to switch between activities without stopping playback.
34  */
35 public class MediaPlaybackService extends MediaBrowserService {
36     private MediaSession mSession;
37 
MediaPlaybackService()38     public MediaPlaybackService() {}
39 
40     @Override
onCreate()41     public void onCreate() {
42         super.onCreate();
43 
44         // Start a new MediaSession
45         mSession = new MediaSession(this, "MediaPlaybackService");
46         // Enable callbacks from MediaButtons and TransportControls
47         mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS
48                 | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
49         // Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player
50         PlaybackState.Builder stateBuilder = new PlaybackState.Builder().setActions(
51                 PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE);
52         mSession.setPlaybackState(stateBuilder.build());
53         setSessionToken(mSession.getSessionToken());
54 
55         Context context = getApplicationContext();
56         Intent intent = new Intent(context, MusicBrowserActivity.class);
57         PendingIntent pi = PendingIntent.getActivity(
58                 context, 99 /*request code*/, intent, PendingIntent.FLAG_UPDATE_CURRENT);
59         mSession.setSessionActivity(pi);
60     }
61 
62     @Override
onStartCommand(Intent startIntent, int flags, int startId)63     public int onStartCommand(Intent startIntent, int flags, int startId) {
64         return START_STICKY;
65     }
66 
67     @Override
onDestroy()68     public void onDestroy() {}
69 
70     @Override
onGetRoot(String clientPackageName, int clientUid, Bundle rootHints)71     public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints) {
72         return null;
73     }
74 
75     @Override
onLoadChildren(final String parentMediaId, final Result<List<MediaItem>> result)76     public void onLoadChildren(final String parentMediaId, final Result<List<MediaItem>> result) {
77         result.sendResult(null);
78         return;
79     }
80 
81     private final class MediaSessionCallback extends MediaSession.Callback {
82         @Override
onPlay()83         public void onPlay() {}
84 
85         @Override
onSkipToQueueItem(long queueId)86         public void onSkipToQueueItem(long queueId) {}
87 
88         @Override
onSeekTo(long position)89         public void onSeekTo(long position) {}
90 
91         @Override
onPlayFromMediaId(String mediaId, Bundle extras)92         public void onPlayFromMediaId(String mediaId, Bundle extras) {}
93 
94         @Override
onPause()95         public void onPause() {}
96 
97         @Override
onStop()98         public void onStop() {}
99 
100         @Override
onSkipToNext()101         public void onSkipToNext() {}
102 
103         @Override
onSkipToPrevious()104         public void onSkipToPrevious() {}
105 
106         @Override
onPlayFromSearch(String query, Bundle extras)107         public void onPlayFromSearch(String query, Bundle extras) {}
108 
109         @Override
onCustomAction(String action, Bundle extras)110         public void onCustomAction(String action, Bundle extras) {}
111     }
112 }
113