Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dc740bcc authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Use SessionPlaylistAgent in MediaSession2Impl" into pi-dev

parents ec57d7b5 0738f276
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -40,9 +40,6 @@ LOCAL_SRC_FILES := \

LOCAL_PROGUARD_FLAG_FILES := proguard.cfg

# TODO: Enable proguard (b/74090741)
LOCAL_PROGUARD_ENABLED := disabled

LOCAL_MULTILIB := first

LOCAL_JAVA_LIBRARIES += android-support-annotations
+57 −23
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ import android.media.MediaSession2.Command;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaSession2.ControllerInfo;
import android.media.MediaSession2.OnDataSourceMissingHelper;
import android.media.MediaSession2.SessionCallback;
import android.media.MediaSessionService2;
import android.media.SessionToken2;
@@ -110,9 +111,13 @@ public class MediaSession2Impl implements MediaSession2Provider {
    @GuardedBy("mLock")
    private MediaPlaylistAgent mPlaylistAgent;
    @GuardedBy("mLock")
    private SessionPlaylistAgent mSessionPlaylistAgent;
    @GuardedBy("mLock")
    private VolumeProvider2 mVolumeProvider;
    @GuardedBy("mLock")
    private PlaybackInfo mPlaybackInfo;
    @GuardedBy("mLock")
    private OnDataSourceMissingHelper mDsmHelper;

    /**
     * Can be only called by the {@link Builder#build()}.
@@ -206,7 +211,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
    }

    @Override
    public void updatePlayer_impl(MediaPlayerBase player, MediaPlaylistAgent playlistAgent,
    public void updatePlayer_impl(@NonNull MediaPlayerBase player, MediaPlaylistAgent playlistAgent,
            VolumeProvider2 volumeProvider) throws IllegalArgumentException {
        ensureCallingThread();
        if (player == null) {
@@ -224,9 +229,12 @@ public class MediaSession2Impl implements MediaSession2Provider {
            oldPlayer = mPlayer;
            oldAgent = mPlaylistAgent;
            mPlayer = player;
            // TODO(jaewan): Replace this with the proper default agent (b/74090741)
            if (agent == null) {
                agent = new MediaPlaylistAgent(mContext) {};
                mSessionPlaylistAgent = new SessionPlaylistAgent(mContext, this, mPlayer);
                if (mDsmHelper != null) {
                    mSessionPlaylistAgent.setOnDataSourceMissingHelper(mDsmHelper);
                }
                agent = mSessionPlaylistAgent;
            }
            mPlaylistAgent = agent;
            mVolumeProvider = volumeProvider;
@@ -311,6 +319,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
            mPlayer = null;
            agent = mPlaylistAgent;
            mPlaylistAgent = null;
            mSessionPlaylistAgent = null;
        }
        if (player != null) {
            player.unregisterPlayerEventCallback(mPlayerEventCallback);
@@ -384,7 +393,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
    }

    @Override
    public void skipToPlaylistItem_impl(MediaItem2 item) {
    public void skipToPlaylistItem_impl(@NonNull MediaItem2 item) {
        if (item == null) {
            throw new IllegalArgumentException("item shouldn't be null");
        }
@@ -417,7 +426,8 @@ public class MediaSession2Impl implements MediaSession2Provider {
    }

    @Override
    public void setCustomLayout_impl(ControllerInfo controller, List<CommandButton> layout) {
    public void setCustomLayout_impl(@NonNull ControllerInfo controller,
            @NonNull List<CommandButton> layout) {
        ensureCallingThread();
        if (controller == null) {
            throw new IllegalArgumentException("controller shouldn't be null");
@@ -433,7 +443,8 @@ public class MediaSession2Impl implements MediaSession2Provider {
    //////////////////////////////////////////////////////////////////////////////////////

    @Override
    public void setAllowedCommands_impl(ControllerInfo controller, CommandGroup commands) {
    public void setAllowedCommands_impl(@NonNull ControllerInfo controller,
            @NonNull CommandGroup commands) {
        if (controller == null) {
            throw new IllegalArgumentException("controller shouldn't be null");
        }
@@ -444,8 +455,8 @@ public class MediaSession2Impl implements MediaSession2Provider {
    }

    @Override
    public void sendCustomCommand_impl(ControllerInfo controller, Command command, Bundle args,
            ResultReceiver receiver) {
    public void sendCustomCommand_impl(@NonNull ControllerInfo controller, @NonNull Command command,
            Bundle args, ResultReceiver receiver) {
        if (controller == null) {
            throw new IllegalArgumentException("controller shouldn't be null");
        }
@@ -456,7 +467,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
    }

    @Override
    public void sendCustomCommand_impl(Command command, Bundle args) {
    public void sendCustomCommand_impl(@NonNull Command command, Bundle args) {
        if (command == null) {
            throw new IllegalArgumentException("command shouldn't be null");
        }
@@ -464,7 +475,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
    }

    @Override
    public void setPlaylist_impl(List<MediaItem2> list, MediaMetadata2 metadata) {
    public void setPlaylist_impl(@NonNull List<MediaItem2> list, MediaMetadata2 metadata) {
        if (list == null) {
            throw new IllegalArgumentException("list shouldn't be null");
        }
@@ -488,7 +499,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
    }

    @Override
    public void addPlaylistItem_impl(int index, MediaItem2 item) {
    public void addPlaylistItem_impl(int index, @NonNull MediaItem2 item) {
        if (index < 0) {
            throw new IllegalArgumentException("index shouldn't be negative");
        }
@@ -504,7 +515,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
    }

    @Override
    public void removePlaylistItem_impl(MediaItem2 item) {
    public void removePlaylistItem_impl(@NonNull MediaItem2 item) {
        if (item == null) {
            throw new IllegalArgumentException("item shouldn't be null");
        }
@@ -517,7 +528,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
    }

    @Override
    public void replacePlaylistItem_impl(int index, MediaItem2 item) {
    public void replacePlaylistItem_impl(int index, @NonNull MediaItem2 item) {
        if (index < 0) {
            throw new IllegalArgumentException("index shouldn't be negative");
        }
@@ -684,6 +695,29 @@ public class MediaSession2Impl implements MediaSession2Provider {
        mSessionStub.notifyError(errorCode, extras);
    }

    @Override
    public void setOnDataSourceMissingHelper_impl(@NonNull OnDataSourceMissingHelper helper) {
        if (helper == null) {
            throw new IllegalArgumentException("helper shouldn't be null");
        }
        synchronized (mLock) {
            mDsmHelper = helper;
            if (mSessionPlaylistAgent != null) {
                mSessionPlaylistAgent.setOnDataSourceMissingHelper(helper);
            }
        }
    }

    @Override
    public void clearOnDataSourceMissingHelper_impl() {
        synchronized (mLock) {
            mDsmHelper = null;
            if (mSessionPlaylistAgent != null) {
                mSessionPlaylistAgent.clearOnDataSourceMissingHelper();
            }
        }
    }

    ///////////////////////////////////////////////////
    // Protected or private methods
    ///////////////////////////////////////////////////
@@ -1021,7 +1055,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
        /**
         * @return a new Command instance from the Bundle
         */
        public static Command fromBundle_impl(Context context, Bundle command) {
        public static Command fromBundle_impl(Context context, @NonNull Bundle command) {
            if (command == null) {
                throw new IllegalArgumentException("command shouldn't be null");
            }
@@ -1091,7 +1125,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
        }

        @Override
        public void addCommand_impl(Command command) {
        public void addCommand_impl(@NonNull Command command) {
            if (command == null) {
                throw new IllegalArgumentException("command shouldn't be null");
            }
@@ -1128,7 +1162,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
        }

        @Override
        public void removeCommand_impl(Command command) {
        public void removeCommand_impl(@NonNull Command command) {
            if (command == null) {
                throw new IllegalArgumentException("command shouldn't be null");
            }
@@ -1136,7 +1170,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
        }

        @Override
        public boolean hasCommand_impl(Command command) {
        public boolean hasCommand_impl(@NonNull Command command) {
            if (command == null) {
                throw new IllegalArgumentException("command shouldn't be null");
            }
@@ -1216,7 +1250,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
        private final IMediaController2 mControllerBinder;

        public ControllerInfoImpl(Context context, ControllerInfo instance, int uid,
                int pid, String packageName, IMediaController2 callback) {
                int pid, @NonNull String packageName, @NonNull IMediaController2 callback) {
            if (TextUtils.isEmpty(packageName)) {
                throw new IllegalArgumentException("packageName shouldn't be empty");
            }
@@ -1467,7 +1501,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
         *      {@link MediaSession2} or {@link MediaController2}.
         */
        // TODO(jaewan): Also need executor
        public BuilderBaseImpl(Context context) {
        public BuilderBaseImpl(@NonNull Context context) {
            if (context == null) {
                throw new IllegalArgumentException("context shouldn't be null");
            }
@@ -1477,7 +1511,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
        }

        @Override
        public void setPlayer_impl(MediaPlayerBase player) {
        public void setPlayer_impl(@NonNull MediaPlayerBase player) {
            if (player == null) {
                throw new IllegalArgumentException("player shouldn't be null");
            }
@@ -1485,7 +1519,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
        }

        @Override
        public void setPlaylistAgent_impl(MediaPlaylistAgent playlistAgent) {
        public void setPlaylistAgent_impl(@NonNull MediaPlaylistAgent playlistAgent) {
            if (playlistAgent == null) {
                throw new IllegalArgumentException("playlistAgent shouldn't be null");
            }
@@ -1503,7 +1537,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
        }

        @Override
        public void setId_impl(String id) {
        public void setId_impl(@NonNull String id) {
            if (id == null) {
                throw new IllegalArgumentException("id shouldn't be null");
            }
@@ -1511,7 +1545,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
        }

        @Override
        public void setSessionCallback_impl(Executor executor, C callback) {
        public void setSessionCallback_impl(@NonNull Executor executor, @NonNull C callback) {
            if (executor == null) {
                throw new IllegalArgumentException("executor shouldn't be null");
            }
+16 −13
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import android.media.MediaItem2;
import android.media.MediaMetadata2;
import android.media.MediaPlayerBase;
import android.media.MediaPlaylistAgent;
import android.media.MediaSession2;
import android.media.MediaSession2.OnDataSourceMissingHelper;
import android.util.ArrayMap;

@@ -47,14 +46,13 @@ public class SessionPlaylistAgent extends MediaPlaylistAgent {
    private final PlayItem mEopPlayItem = new PlayItem(END_OF_PLAYLIST, null);

    private final Object mLock = new Object();
    private final MediaSession2 mSession;
    private final MediaSession2Impl mSessionImpl;

    // TODO: Set data sources properly into mPlayer (b/74090741)
    @GuardedBy("mLock")
    private MediaPlayerBase mPlayer;
    @GuardedBy("mLock")
    private OnDataSourceMissingHelper mDsdHelper;

    private OnDataSourceMissingHelper mDsmHelper;
    // TODO: Check if having the same item is okay (b/74090741)
    @GuardedBy("mLock")
    private ArrayList<MediaItem2> mPlaylist = new ArrayList<>();
@@ -118,16 +116,16 @@ public class SessionPlaylistAgent extends MediaPlaylistAgent {
        }
    }

    public SessionPlaylistAgent(@NonNull Context context, @NonNull MediaSession2 session,
    public SessionPlaylistAgent(@NonNull Context context, @NonNull MediaSession2Impl sessionImpl,
            @NonNull MediaPlayerBase player) {
        super(context);
        if (session == null) {
            throw new IllegalArgumentException("session shouldn't be null");
        if (sessionImpl == null) {
            throw new IllegalArgumentException("sessionImpl shouldn't be null");
        }
        if (player == null) {
            throw new IllegalArgumentException("player shouldn't be null");
        }
        mSession = session;
        mSessionImpl = sessionImpl;
        mPlayer = player;
    }

@@ -142,7 +140,13 @@ public class SessionPlaylistAgent extends MediaPlaylistAgent {

    public void setOnDataSourceMissingHelper(OnDataSourceMissingHelper helper) {
        synchronized (mLock) {
            mDsdHelper = helper;
            mDsmHelper = helper;
        }
    }

    public void clearOnDataSourceMissingHelper() {
        synchronized (mLock) {
            mDsmHelper = null;
        }
    }

@@ -154,8 +158,7 @@ public class SessionPlaylistAgent extends MediaPlaylistAgent {
    }

    @Override
    public void setPlaylist(@NonNull List<MediaItem2> list,
            @Nullable MediaMetadata2 metadata) {
    public void setPlaylist(@NonNull List<MediaItem2> list, @Nullable MediaMetadata2 metadata) {
        if (list == null) {
            throw new IllegalArgumentException("list shouldn't be null");
        }
@@ -359,10 +362,10 @@ public class SessionPlaylistAgent extends MediaPlaylistAgent {
        if (dsd != null) {
            return dsd;
        }
        OnDataSourceMissingHelper helper = mDsdHelper;
        OnDataSourceMissingHelper helper = mDsmHelper;
        if (helper != null) {
            // TODO: Do not call onDataSourceMissing with the lock (b/74090741).
            dsd = helper.onDataSourceMissing(mSession, item);
            dsd = helper.onDataSourceMissing(mSessionImpl.getInstance(), item);
            if (dsd != null) {
                mItemDsdMap.put(item, dsd);
            }
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ LOCAL_MODULE_TAGS := tests
LOCAL_STATIC_JAVA_LIBRARIES := \
    android.test.runner.stubs \
    android.test.base.stubs \
    mockito-target-minus-junit4 \
    junit

LOCAL_SRC_FILES := $(call all-java-files-under, src)
+106 −15
Original line number Diff line number Diff line
@@ -16,19 +16,21 @@

package com.android.media;

import static org.mockito.Mockito.*;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.DataSourceDesc;
import android.media.MediaItem2;
import android.media.MediaMetadata2;
import android.media.MediaPlayer2;
import android.media.MediaPlayerBase;
import android.media.MediaPlaylistAgent;
import android.media.MediaSession2;
import android.media.MediaSession2.OnDataSourceMissingHelper;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.test.AndroidTestCase;
import android.util.Log;

import org.junit.After;
import org.junit.Before;
@@ -52,10 +54,10 @@ public class SessionPlaylistAgentTest extends AndroidTestCase {

    private Object mWaitLock = new Object();
    private Context mContext;
    private MediaSession2 mSession;
    private MediaSession2Impl mSessionImpl;
    private MediaPlayerBase mPlayer;
    private SessionPlaylistAgent mAgent;
    private MyDataSourceHelper mDataSourceHelper;
    private OnDataSourceMissingHelper mDataSourceHelper;
    private MyPlaylistEventCallback mEventCallback;

    public class MyPlaylistEventCallback extends MediaPlaylistAgent.PlaylistEventCallback {
@@ -108,7 +110,7 @@ public class SessionPlaylistAgentTest extends AndroidTestCase {
        }
    }

    public class MyDataSourceHelper implements MediaSession2.OnDataSourceMissingHelper {
    public class MyDataSourceHelper implements OnDataSourceMissingHelper {
        @Override
        public DataSourceDesc onDataSourceMissing(MediaSession2 session, MediaItem2 item) {
            if (item.getMediaId().contains("WITHOUT_DSD")) {
@@ -121,8 +123,104 @@ public class SessionPlaylistAgentTest extends AndroidTestCase {
        }
    }

    public class MockPlayer extends MediaPlayerBase {
        @Override
        public void play() {
        }

        @Override
        public void prepare() {
        }

        @Override
        public void pause() {
        }

        @Override
        public void reset() {
        }

        @Override
        public void skipToNext() {
        }

        @Override
        public void seekTo(long pos) {
        }

        @Override
        public int getPlayerState() {
            return 0;
        }

        @Override
        public int getBufferingState() {
            return 0;
        }

        @Override
        public void setAudioAttributes(AudioAttributes attributes) {
        }

        @Override
        public AudioAttributes getAudioAttributes() {
            return null;
        }

        @Override
        public void setDataSource(DataSourceDesc dsd) {
        }

        @Override
        public void setNextDataSource(DataSourceDesc dsd) {
        }

        @Override
        public void setNextDataSources(List<DataSourceDesc> dsds) {
        }

        @Override
        public DataSourceDesc getCurrentDataSource() {
            return null;
        }

        @Override
        public void loopCurrent(boolean loop) {
        }

        @Override
        public void setPlaybackSpeed(float speed) {
        }

        @Override
        public void setPlayerVolume(float volume) {
        }

        @Override
        public float getPlayerVolume() {
            return 0;
        }

        @Override
        public void registerPlayerEventCallback(Executor e, PlayerEventCallback cb) {
        }

        @Override
        public void unregisterPlayerEventCallback(PlayerEventCallback cb) {
        }

        @Override
        public void close() throws Exception {
        }
    }

    @Before
    public void setUp() throws Exception {
        mContext = getContext();
        // Workaround for dexmaker bug: https://code.google.com/p/dexmaker/issues/detail?id=2
        // Dexmaker is used by mockito.
        System.setProperty("dexmaker.dexcache", mContext.getCacheDir().getPath());

        HandlerThread handlerThread = new HandlerThread("SessionPlaylistAgent");
        handlerThread.start();
        mHandler = new Handler(handlerThread.getLooper());
@@ -130,15 +228,10 @@ public class SessionPlaylistAgentTest extends AndroidTestCase {
            mHandler.post(runnable);
        };

        mContext = getContext();
        mPlayer = MediaPlayer2.create();
        mSession = new MediaSession2.Builder(mContext)
                .setPlayer(mPlayer)
                .setSessionCallback(mHandlerExecutor,
                        new MediaSession2.SessionCallback(mContext) {})
                .setId(TAG).build();
        mPlayer = mock(MockPlayer.class);
        mSessionImpl = mock(MediaSession2Impl.class);
        mDataSourceHelper = new MyDataSourceHelper();
        mAgent = new SessionPlaylistAgent(mContext, mSession, mPlayer);
        mAgent = new SessionPlaylistAgent(mContext, mSessionImpl, mPlayer);
        mAgent.setOnDataSourceMissingHelper(mDataSourceHelper);
        mEventCallback = new MyPlaylistEventCallback(mWaitLock);
        mAgent.registerPlaylistEventCallback(mHandlerExecutor, mEventCallback);
@@ -146,8 +239,6 @@ public class SessionPlaylistAgentTest extends AndroidTestCase {

    @After
    public void tearDown() throws Exception {
        mSession.close();
        mPlayer.close();
        mHandler.getLooper().quitSafely();
        mHandler = null;
        mHandlerExecutor = null;