Java tutorial
/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.phearom.um; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.RemoteException; import android.support.annotation.NonNull; import android.support.v4.media.MediaBrowserCompat.MediaItem; import android.support.v4.media.MediaBrowserServiceCompat; import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.session.MediaButtonReceiver; import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.PlaybackStateCompat; import com.phearom.um.model.MusicProvider; import com.phearom.um.playback.LocalPlayback; import com.phearom.um.playback.PlaybackManager; import com.phearom.um.playback.QueueManager; import com.phearom.um.ui.NowPlayingActivity; import com.phearom.um.utils.LogHelper; import java.lang.ref.WeakReference; import java.util.List; import static com.phearom.um.utils.MediaIDHelper.MEDIA_ID_ROOT; public class MusicService extends MediaBrowserServiceCompat implements PlaybackManager.PlaybackServiceCallback { private static final String TAG = LogHelper.makeLogTag(MusicService.class); // Extra on MediaSession that contains the Cast device name currently connected to public static final String EXTRA_CONNECTED_CAST = "com.phearom.um.CAST_NAME"; // The action of the incoming Intent indicating that it contains a command // to be executed (see {@link #onStartCommand}) public static final String ACTION_CMD = "com.phearom.um.ACTION_CMD"; // The key in the extras of the incoming Intent indicating the command that // should be executed (see {@link #onStartCommand}) public static final String CMD_NAME = "CMD_NAME"; // A value of a CMD_NAME key in the extras of the incoming Intent that // indicates that the music playback should be paused (see {@link #onStartCommand}) public static final String CMD_PAUSE = "CMD_PAUSE"; // A value of a CMD_NAME key that indicates that the music playback should switch // to local playback from cast playback. public static final String CMD_STOP_CASTING = "CMD_STOP_CASTING"; // Delay stopSelf by using a handler. private static final int STOP_DELAY = 30000; private MusicProvider mMusicProvider; private PlaybackManager mPlaybackManager; private MediaSessionCompat mSession; private MediaNotificationManager mMediaNotificationManager; private Bundle mSessionExtras; private final DelayedStopHandler mDelayedStopHandler = new DelayedStopHandler(this); private PackageValidator mPackageValidator; @Override public void onCreate() { super.onCreate(); LogHelper.d(TAG, "onCreate"); mMusicProvider = new MusicProvider(getApplicationContext()); mMusicProvider.retrieveMediaAsync(null /* Callback */); mPackageValidator = new PackageValidator(this); QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() { @Override public void onMetadataChanged(MediaMetadataCompat metadata) { mSession.setMetadata(metadata); } @Override public void onMetadataRetrieveError() { mPlaybackManager.updatePlaybackState(getString(R.string.error_no_metadata)); } @Override public void onCurrentQueueIndexUpdated(int queueIndex) { mPlaybackManager.handlePlayRequest(); } @Override public void onQueueUpdated(String title, List<MediaSessionCompat.QueueItem> newQueue) { mSession.setQueue(newQueue); mSession.setQueueTitle(title); } }); LocalPlayback playback = new LocalPlayback(this, mMusicProvider); mPlaybackManager = new PlaybackManager(this, getResources(), mMusicProvider, queueManager, playback); // Start a new MediaSession mSession = new MediaSessionCompat(this, "MusicService"); setSessionToken(mSession.getSessionToken()); mSession.setCallback(mPlaybackManager.getMediaSessionCallback()); mSession.setFlags( MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); Context context = getApplicationContext(); Intent intent = new Intent(context, NowPlayingActivity.class); PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/, intent, PendingIntent.FLAG_UPDATE_CURRENT); mSession.setSessionActivity(pi); mSessionExtras = new Bundle(); mSession.setExtras(mSessionExtras); mPlaybackManager.updatePlaybackState(null); try { mMediaNotificationManager = new MediaNotificationManager(this); } catch (RemoteException e) { throw new IllegalStateException("Could not create a MediaNotificationManager", e); } } @Override public int onStartCommand(Intent startIntent, int flags, int startId) { if (startIntent != null) { String action = startIntent.getAction(); String command = startIntent.getStringExtra(CMD_NAME); if (ACTION_CMD.equals(action)) { if (CMD_PAUSE.equals(command)) { mPlaybackManager.handlePauseRequest(); } } else { // Try to handle the intent as a media button event wrapped by MediaButtonReceiver MediaButtonReceiver.handleIntent(mSession, startIntent); } } // Reset the delay handler to enqueue a message to stop the service if // nothing is playing. mDelayedStopHandler.removeCallbacksAndMessages(null); mDelayedStopHandler.sendEmptyMessageDelayed(0, STOP_DELAY); return START_STICKY; } @Override public void onDestroy() { LogHelper.d(TAG, "onDestroy"); mPlaybackManager.handleStopRequest(null); mMediaNotificationManager.stopNotification(); mDelayedStopHandler.removeCallbacksAndMessages(null); mSession.release(); } @Override public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, Bundle rootHints) { LogHelper.d(TAG, "OnGetRoot: clientPackageName=" + clientPackageName, "; clientUid=" + clientUid + " ; rootHints=", rootHints); if (!mPackageValidator.isCallerAllowed(this, clientPackageName, clientUid)) { return null; } return new BrowserRoot(MEDIA_ID_ROOT, null); } @Override public void onLoadChildren(@NonNull final String parentMediaId, @NonNull final Result<List<MediaItem>> result) { LogHelper.d(TAG, "OnLoadChildren: parentMediaId=", parentMediaId); if (mMusicProvider.isInitialized()) { // if music library is ready, return immediately result.sendResult(mMusicProvider.getChildren(parentMediaId)); } else { // otherwise, only return results when the music library is retrieved result.detach(); mMusicProvider.retrieveMediaAsync(new MusicProvider.Callback() { @Override public void onMusicCatalogReady(boolean success) { result.sendResult(mMusicProvider.getChildren(parentMediaId)); } }); } } @Override public void onPlaybackStart() { if (!mSession.isActive()) { mSession.setActive(true); } mDelayedStopHandler.removeCallbacksAndMessages(null); startService(new Intent(getApplicationContext(), MusicService.class)); } /** * Callback method called from PlaybackManager whenever the music stops playing. */ @Override public void onPlaybackStop() { mDelayedStopHandler.removeCallbacksAndMessages(null); mDelayedStopHandler.sendEmptyMessageDelayed(0, STOP_DELAY); stopForeground(true); } @Override public void onNotificationRequired() { mMediaNotificationManager.startNotification(); } @Override public void onPlaybackStateUpdated(PlaybackStateCompat newState) { mSession.setPlaybackState(newState); } private static class DelayedStopHandler extends Handler { private final WeakReference<MusicService> mWeakReference; private DelayedStopHandler(MusicService service) { mWeakReference = new WeakReference<>(service); } @Override public void handleMessage(Message msg) { MusicService service = mWeakReference.get(); if (service != null && service.mPlaybackManager.getPlayback() != null) { if (service.mPlaybackManager.getPlayback().isPlaying()) { LogHelper.d(TAG, "Ignoring delayed stop since the media player is in use."); return; } LogHelper.d(TAG, "Stopping service with delay handler."); service.stopSelf(); } } } }