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

Commit 991a25e6 authored by Jaewan Kim's avatar Jaewan Kim
Browse files

MediaSession2: Make PlaybackInfoProvider inner interface

It should be inner interface of MediaController2Provider because
PlaybackInfo is inner class of MediaController2

Test: Build
Change-Id: Ib809d669c87c2a3b515f4b4824ef3c9da568a037
parent b66931ce
Loading
Loading
Loading
Loading
+94 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.AudioAttributes;
import android.media.MediaController2.PlaybackInfo;
import android.media.MediaItem2;
import android.media.MediaSession2;
@@ -859,4 +860,97 @@ public class MediaController2Impl implements MediaController2Provider {
            mInstance.close();
        }
    }

    public static final class PlaybackInfoImpl implements PlaybackInfoProvider {

        private static final String KEY_PLAYBACK_TYPE =
                "android.media.playbackinfo_impl.playback_type";
        private static final String KEY_CONTROL_TYPE =
                "android.media.playbackinfo_impl.control_type";
        private static final String KEY_MAX_VOLUME =
                "android.media.playbackinfo_impl.max_volume";
        private static final String KEY_CURRENT_VOLUME =
                "android.media.playbackinfo_impl.current_volume";
        private static final String KEY_AUDIO_ATTRIBUTES =
                "android.media.playbackinfo_impl.audio_attrs";

        private final Context mContext;
        private final PlaybackInfo mInstance;

        private final int mPlaybackType;
        private final int mControlType;
        private final int mMaxVolume;
        private final int mCurrentVolume;
        private final AudioAttributes mAudioAttrs;

        private PlaybackInfoImpl(Context context, int playbackType, AudioAttributes attrs,
                int controlType, int max, int current) {
            mContext = context;
            mPlaybackType = playbackType;
            mAudioAttrs = attrs;
            mControlType = controlType;
            mMaxVolume = max;
            mCurrentVolume = current;
            mInstance = new PlaybackInfo(this);
        }

        @Override
        public int getPlaybackType_impl() {
            return mPlaybackType;
        }

        @Override
        public AudioAttributes getAudioAttributes_impl() {
            return mAudioAttrs;
        }

        @Override
        public int getControlType_impl() {
            return mControlType;
        }

        @Override
        public int getMaxVolume_impl() {
            return mMaxVolume;
        }

        @Override
        public int getCurrentVolume_impl() {
            return mCurrentVolume;
        }

        public PlaybackInfo getInstance() {
            return mInstance;
        }

        public Bundle toBundle() {
            Bundle bundle = new Bundle();
            bundle.putInt(KEY_PLAYBACK_TYPE, mPlaybackType);
            bundle.putInt(KEY_CONTROL_TYPE, mControlType);
            bundle.putInt(KEY_MAX_VOLUME, mMaxVolume);
            bundle.putInt(KEY_CURRENT_VOLUME, mCurrentVolume);
            bundle.putParcelable(KEY_AUDIO_ATTRIBUTES, mAudioAttrs);
            return bundle;
        }

        public static PlaybackInfo createPlaybackInfo(Context context, int playbackType,
                AudioAttributes attrs, int controlType, int max, int current) {
            return new PlaybackInfoImpl(context, playbackType, attrs, controlType, max, current)
                    .getInstance();
        }

        public static PlaybackInfo fromBundle(Context context, Bundle bundle) {
            if (bundle == null) {
                return null;
            }
            final int volumeType = bundle.getInt(KEY_PLAYBACK_TYPE);
            final int volumeControl = bundle.getInt(KEY_CONTROL_TYPE);
            final int maxVolume = bundle.getInt(KEY_MAX_VOLUME);
            final int currentVolume = bundle.getInt(KEY_CURRENT_VOLUME);
            final AudioAttributes attrs = bundle.getParcelable(KEY_AUDIO_ATTRIBUTES);

            return createPlaybackInfo(
                    context, volumeType, attrs, volumeControl, maxVolume, currentVolume);
        }
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -252,7 +252,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
                    stream = AudioManager.STREAM_MUSIC;
                }
            }
            info = PlaybackInfoImpl.createPlaybackInfo(
            info = MediaController2Impl.PlaybackInfoImpl.createPlaybackInfo(
                    mContext,
                    PlaybackInfo.PLAYBACK_TYPE_LOCAL,
                    attrs,
@@ -262,7 +262,7 @@ public class MediaSession2Impl implements MediaSession2Provider {
                    mAudioManager.getStreamMaxVolume(stream),
                    mAudioManager.getStreamVolume(stream));
        } else {
            info = PlaybackInfoImpl.createPlaybackInfo(
            info = MediaController2Impl.PlaybackInfoImpl.createPlaybackInfo(
                    mContext,
                    PlaybackInfo.PLAYBACK_TYPE_REMOTE /* ControlType */,
                    attrs,
+2 −4
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.media;
import android.app.PendingIntent;
import android.content.Context;
import android.media.MediaController2;
import android.media.MediaController2.PlaybackInfo;
import android.media.MediaItem2;
import android.media.MediaLibraryService2.LibraryRoot;
import android.media.MediaLibraryService2.MediaLibrarySessionCallback;
@@ -31,7 +30,6 @@ import android.media.MediaSession2.ControllerInfo;
import android.media.MediaSession2.PlaylistParams;
import android.media.PlaybackState2;
import android.media.VolumeProvider2;
import android.media.update.MediaSession2Provider.CommandButtonProvider;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
@@ -136,7 +134,7 @@ public class MediaSession2Stub extends IMediaSession2.Stub {
                final PlaybackState2 state = session.getInstance().getPlaybackState();
                final Bundle playbackStateBundle = (state != null) ? state.toBundle() : null;
                final Bundle playbackInfoBundle =
                        ((PlaybackInfoImpl) session.getPlaybackInfo().getProvider()).toBundle();
                        ((MediaController2Impl.PlaybackInfoImpl) session.getPlaybackInfo().getProvider()).toBundle();
                final PlaylistParams params = session.getInstance().getPlaylistParams();
                final Bundle paramsBundle = (params != null) ? params.toBundle() : null;
                final int ratingType = session.getRatingType();
@@ -627,7 +625,7 @@ public class MediaSession2Stub extends IMediaSession2.Stub {
                    ControllerInfoImpl.from(list.get(i)).getControllerBinder();
            try {
                callbackBinder.onPlaybackInfoChanged(
                        ((PlaybackInfoImpl) playbackInfo.getProvider()).toBundle());
                        ((MediaController2Impl.PlaybackInfoImpl) playbackInfo.getProvider()).toBundle());
            } catch (RemoteException e) {
                Log.w(TAG, "Controller is gone", e);
                // TODO(jaewan): What to do when the controller is gone?
+0 −116
Original line number Diff line number Diff line
/*
 * Copyright 2018 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.android.media;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.MediaController2.PlaybackInfo;
import android.media.update.PlaybackInfoProvider;
import android.os.Bundle;

public final class PlaybackInfoImpl implements PlaybackInfoProvider {

    private static final String KEY_PLAYBACK_TYPE =
            "android.media.playbackinfo_impl.playback_type";
    private static final String KEY_CONTROL_TYPE =
            "android.media.playbackinfo_impl.control_type";
    private static final String KEY_MAX_VOLUME =
            "android.media.playbackinfo_impl.max_volume";
    private static final String KEY_CURRENT_VOLUME =
            "android.media.playbackinfo_impl.current_volume";
    private static final String KEY_AUDIO_ATTRIBUTES =
            "android.media.playbackinfo_impl.audio_attrs";

    private final Context mContext;
    private final PlaybackInfo mInstance;

    private final int mPlaybackType;
    private final int mControlType;
    private final int mMaxVolume;
    private final int mCurrentVolume;
    private final AudioAttributes mAudioAttrs;

    private PlaybackInfoImpl(Context context, int playbackType, AudioAttributes attrs,
            int controlType, int max, int current) {
        mContext = context;
        mPlaybackType = playbackType;
        mAudioAttrs = attrs;
        mControlType = controlType;
        mMaxVolume = max;
        mCurrentVolume = current;
        mInstance = new PlaybackInfo(this);
    }

    @Override
    public int getPlaybackType_impl() {
        return mPlaybackType;
    }

    @Override
    public AudioAttributes getAudioAttributes_impl() {
        return mAudioAttrs;
    }

    @Override
    public int getControlType_impl() {
        return mControlType;
    }

    @Override
    public int getMaxVolume_impl() {
        return mMaxVolume;
    }

    @Override
    public int getCurrentVolume_impl() {
        return mCurrentVolume;
    }

    public PlaybackInfo getInstance() {
        return mInstance;
    }

    public Bundle toBundle() {
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_PLAYBACK_TYPE, mPlaybackType);
        bundle.putInt(KEY_CONTROL_TYPE, mControlType);
        bundle.putInt(KEY_MAX_VOLUME, mMaxVolume);
        bundle.putInt(KEY_CURRENT_VOLUME, mCurrentVolume);
        bundle.putParcelable(KEY_AUDIO_ATTRIBUTES, mAudioAttrs);
        return bundle;
    }

    public static PlaybackInfo createPlaybackInfo(Context context, int playbackType,
            AudioAttributes attrs, int controlType, int max, int current) {
        return new PlaybackInfoImpl(context, playbackType, attrs, controlType, max, current)
                .getInstance();
    }

    public static PlaybackInfo fromBundle(Context context, Bundle bundle) {
        if (bundle == null) {
            return null;
        }
        final int volumeType = bundle.getInt(KEY_PLAYBACK_TYPE);
        final int volumeControl = bundle.getInt(KEY_CONTROL_TYPE);
        final int maxVolume = bundle.getInt(KEY_MAX_VOLUME);
        final int currentVolume = bundle.getInt(KEY_CURRENT_VOLUME);
        final AudioAttributes attrs = bundle.getParcelable(KEY_AUDIO_ATTRIBUTES);

        return createPlaybackInfo(
                context, volumeType, attrs, volumeControl, maxVolume, currentVolume);
    }
}