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

Commit 88a15298 authored by Ajay Panicker's avatar Ajay Panicker
Browse files

Remove dependency on delay by ignoring invalid playback states

Remove the dependency on delaying for a media update by instead
using the playback state and current metadata to determine current
song status.

Bug: 34471252
Test: Switch players on BMW carkit
Change-Id: I25532d6e47fbecc1357b78ce189cb9f411ecc256
parent 6c402ed8
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -260,7 +260,6 @@ public class AddressedMediaPlayer {
        long qid = getActiveQueueItemId(mediaController);
        byte[] track = ByteBuffer.allocate(AvrcpConstants.UID_SIZE).putLong(qid).array();
        // The nowPlayingList changed: the new list has the full data for the current item
        if (type == AvrcpConstants.NOTIFICATION_TYPE_CHANGED) sendNowPlayingListChanged();
        mMediaInterface.trackChangedRsp(type, track);
        mLastTrackIdSent = qid;
    }
@@ -527,7 +526,9 @@ public class AddressedMediaPlayer {
    private long getActiveQueueItemId(@Nullable MediaController controller) {
        if (controller == null) return MediaSession.QueueItem.UNKNOWN_ID;
        PlaybackState state = controller.getPlaybackState();
        if (state == null) return MediaSession.QueueItem.UNKNOWN_ID;
        if (state == null || state.getState() == PlaybackState.STATE_BUFFERING
                || state.getState() == PlaybackState.STATE_NONE)
            return MediaSession.QueueItem.UNKNOWN_ID;
        long qid = state.getActiveQueueItemId();
        if (qid != MediaSession.QueueItem.UNKNOWN_ID) return qid;
        // Check if we're presenting a "one item queue"
+42 −41
Original line number Diff line number Diff line
@@ -164,10 +164,8 @@ public final class Avrcp {
    private static final int MSG_ABS_VOL_TIMEOUT = 17;
    private static final int MSG_SET_A2DP_AUDIO_STATE = 18;
    private static final int MSG_NOW_PLAYING_CHANGED_RSP = 19;
    private static final int MSG_UPDATE_MEDIA = 20;

    private static final int CMD_TIMEOUT_DELAY = 2000;
    private static final int MEDIA_DWELL_TIME = 1000;
    private static final int MAX_ERROR_RETRY_TIMES = 6;
    private static final int AVRCP_MAX_VOL = 127;
    private static final int AVRCP_BASE_VOLUME_STEP = 1;
@@ -375,12 +373,13 @@ public final class Avrcp {
        @Override
        public void onMetadataChanged(MediaMetadata metadata) {
            if (DEBUG) Log.v(TAG, "onMetadataChanged");
            scheduleMediaUpdate();
            updateCurrentMediaState(false);
        }
        @Override
        public synchronized void onPlaybackStateChanged(PlaybackState state) {
            if (DEBUG) Log.v(TAG, "onPlaybackStateChanged: state " + state.toString());
            scheduleMediaUpdate();

            updateCurrentMediaState(false);
        }

        @Override
@@ -400,7 +399,7 @@ public final class Avrcp {
            }

            Log.v(TAG, "onQueueChanged: NowPlaying list changed, Queue Size = "+ queue.size());
            mHandler.sendEmptyMessageDelayed(MSG_NOW_PLAYING_CHANGED_RSP, MEDIA_DWELL_TIME);
            mHandler.sendEmptyMessage(MSG_NOW_PLAYING_CHANGED_RSP);
        }
    }

@@ -480,7 +479,7 @@ public final class Avrcp {
            case MSG_NOW_PLAYING_CHANGED_RSP:
                if (DEBUG) Log.v(TAG, "MSG_NOW_PLAYING_CHANGED_RSP");
                removeMessages(MSG_NOW_PLAYING_CHANGED_RSP);
                mAddressedMediaPlayer.updateNowPlayingList(mMediaController);
                updateCurrentMediaState(false);
                break;

            case MSG_PLAY_INTERVAL_TIMEOUT:
@@ -694,7 +693,7 @@ public final class Avrcp {
            case MSG_SET_A2DP_AUDIO_STATE:
                if (DEBUG) Log.v(TAG, "MSG_SET_A2DP_AUDIO_STATE:" + msg.arg1);
                mA2dpState = msg.arg1;
                scheduleMediaUpdate();
                updateCurrentMediaState(false);
                break;

            case MSG_NATIVE_REQ_GET_FOLDER_ITEMS: {
@@ -780,13 +779,6 @@ public final class Avrcp {
                handlePassthroughCmd(msg.arg1, msg.arg2);
                break;

            case MSG_UPDATE_MEDIA:
                if (DEBUG) Log.v(TAG, "MSG_UPDATE_MEDIA");
                // Throttle to once per MEDIA_DWELL_TIME
                removeMessages(MSG_UPDATE_MEDIA);
                updateCurrentMediaState(false);
                break;

            default:
                Log.e(TAG, "unknown message! msg.what=" + msg.what);
                break;
@@ -976,11 +968,6 @@ public final class Avrcp {
        }
    }

    private void scheduleMediaUpdate() {
        Message msg = mHandler.obtainMessage(MSG_UPDATE_MEDIA);
        mHandler.sendMessageDelayed(msg, MEDIA_DWELL_TIME);
    }

    private void updateCurrentMediaState(boolean registering) {
        // Only do player updates when we aren't registering for track changes.
        if (!registering) {
@@ -1014,25 +1001,40 @@ public final class Avrcp {
            if (mMediaController == null) {
                currentAttributes = new MediaAttributes(null);
            } else {
                newState = mMediaController.getPlaybackState();
                currentAttributes = new MediaAttributes(mMediaController.getMetadata());
            }
        }

        if (newState.getState() != PlaybackState.STATE_BUFFERING
                && newState.getState() != PlaybackState.STATE_NONE) {
            long newQueueId = MediaSession.QueueItem.UNKNOWN_ID;
            if (newState != null) newQueueId = newState.getActiveQueueItemId();
            Log.v(TAG, "Media update: id " + mLastQueueId + "➡" + newQueueId + "? "
                        + currentAttributes.toRedactedString());
                            + currentAttributes.toRedactedString() + " : "
                            + mMediaAttributes.toRedactedString());

            // Dont send now playing list changed if the player doesn't support browsing
            MediaPlayerInfo info = getAddressedPlayerInfo();
            if (info != null && info.isBrowseSupported()) {
                Log.v(TAG, "Check if NowPlayingList is updated");
                mAddressedMediaPlayer.updateNowPlayingList(mMediaController);
            }

            // Notify track changed if:
            //  - The CT is registering for the notification
            //  - Queue ID is UNKNOWN and MediaMetadata is different
            //  - Queue ID is valid and different and MediaMetadata is different
        if (registering || (((newQueueId == -1) || (newQueueId != mLastQueueId))
            if (registering || ((newQueueId == -1 || newQueueId != mLastQueueId)
                                       && !currentAttributes.equals(mMediaAttributes))) {
            sendTrackChangedRsp(registering);
                Log.v(TAG, "Send track changed");
                mMediaAttributes = currentAttributes;
                mLastQueueId = newQueueId;
                sendTrackChangedRsp(registering);
            }
        } else {
            Log.i(TAG, "Skipping update due to invalid playback state");
        }

        sendPlayPosNotificationRsp(false);
    }

@@ -1074,7 +1076,7 @@ public final class Avrcp {
            case EVT_TRACK_CHANGED:
                Log.v(TAG, "Track changed notification enabled");
                mTrackChangedNT = AvrcpConstants.NOTIFICATION_TYPE_INTERIM;
                updateCurrentMediaState(true);
                sendTrackChangedRsp(true);
                break;

            case EVT_PLAY_POS_CHANGED:
@@ -1164,8 +1166,7 @@ public final class Avrcp {

    private boolean isPlayingState(@Nullable PlaybackState state) {
        if (state == null) return false;
        return (state != null) && (state.getState() == PlaybackState.STATE_PLAYING)
                || (state.getState() == PlaybackState.STATE_BUFFERING);
        return (state != null) && (state.getState() == PlaybackState.STATE_PLAYING);
    }

    /**
@@ -1609,7 +1610,7 @@ public final class Avrcp {
                            Log.v(TAG, "No addressed player but active sessions, taking first.");
                        setAddressedMediaSessionPackage(newControllers.get(0).getPackageName());
                    }
                    scheduleMediaUpdate();
                    updateCurrentMediaState(false);
                }
            };

@@ -1625,7 +1626,7 @@ public final class Avrcp {
        // If the player doesn't exist, we need to add it.
        if (getMediaPlayerInfo(packageName) == null) {
            addMediaPlayerPackage(packageName);
            scheduleMediaUpdate();
            updateCurrentMediaState(false);
        }
        synchronized (mMediaPlayerInfoList) {
            for (Map.Entry<Integer, MediaPlayerInfo> entry : mMediaPlayerInfoList.entrySet()) {
@@ -1633,7 +1634,7 @@ public final class Avrcp {
                    int newAddrID = entry.getKey();
                    if (DEBUG) Log.v(TAG, "Set addressed #" + newAddrID + " " + entry.getValue());
                    updateCurrentController(newAddrID, mCurrBrowsePlayerID);
                    scheduleMediaUpdate();
                    updateCurrentMediaState(false);
                    return;
                }
            }
@@ -1772,7 +1773,7 @@ public final class Avrcp {
                addMediaPlayerController(controller);
            }

            scheduleMediaUpdate();
            updateCurrentMediaState(false);

            if (mMediaPlayerInfoList.size() > 0) {
                // Set the first one as the Addressed Player
@@ -1903,9 +1904,9 @@ public final class Avrcp {

        switch (pbState.getState()) {
            case PlaybackState.STATE_PLAYING:
            case PlaybackState.STATE_BUFFERING:
                return PLAYSTATUS_PLAYING;

            case PlaybackState.STATE_BUFFERING:
            case PlaybackState.STATE_STOPPED:
            case PlaybackState.STATE_NONE:
            case PlaybackState.STATE_CONNECTING: