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

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

Merge "AudioService playback activity notif: check origin of player updates"

parents 2cba0e8b 46e310b3
Loading
Loading
Loading
Loading
+4 −4
Original line number Original line Diff line number Diff line
@@ -6491,7 +6491,7 @@ public class AudioService extends IAudioService.Stub
    }
    }


    //======================
    //======================
    // Audio policy callbacks from players for playback configuration updates
    // Audio playback notification
    //======================
    //======================
    private final PlaybackActivityMonitor mPlaybackMonitor = new PlaybackActivityMonitor();
    private final PlaybackActivityMonitor mPlaybackMonitor = new PlaybackActivityMonitor();


@@ -6518,15 +6518,15 @@ public class AudioService extends IAudioService.Stub
    }
    }


    public void playerAttributes(int piid, AudioAttributes attr) {
    public void playerAttributes(int piid, AudioAttributes attr) {
        mPlaybackMonitor.playerAttributes(piid, attr);
        mPlaybackMonitor.playerAttributes(piid, attr, Binder.getCallingUid());
    }
    }


    public void playerEvent(int piid, int event) {
    public void playerEvent(int piid, int event) {
        mPlaybackMonitor.playerEvent(piid, event);
        mPlaybackMonitor.playerEvent(piid, event, Binder.getCallingUid());
    }
    }


    public void releasePlayer(int piid) {
    public void releasePlayer(int piid) {
        mPlaybackMonitor.releasePlayer(piid);
        mPlaybackMonitor.releasePlayer(piid, Binder.getCallingUid());
    }
    }


    //======================
    //======================
+34 −14
Original line number Original line Diff line number Diff line
@@ -78,15 +78,15 @@ public final class PlaybackActivityMonitor {
        return newPiid;
        return newPiid;
    }
    }


    public void playerAttributes(int piid, @NonNull AudioAttributes attr) {
    public void playerAttributes(int piid, @NonNull AudioAttributes attr, int binderUid) {
        final boolean change;
        final boolean change;
        synchronized(mPlayerLock) {
        synchronized(mPlayerLock) {
            final AudioPlaybackConfiguration apc = mPlayers.get(new Integer(piid));
            final AudioPlaybackConfiguration apc = mPlayers.get(new Integer(piid));
            if (apc == null) {
            if (checkConfigurationCaller(piid, apc, binderUid)) {
                Log.e(TAG, "Unknown player " + piid + " for audio attributes change");
                change = false;
            } else {
                change = apc.handleAudioAttributesEvent(attr);
                change = apc.handleAudioAttributesEvent(attr);
            } else {
                Log.e(TAG, "Error updating audio attributes");
                change = false;
            }
            }
        }
        }
        if (change) {
        if (change) {
@@ -94,17 +94,17 @@ public final class PlaybackActivityMonitor {
        }
        }
    }
    }


    public void playerEvent(int piid, int event) {
    public void playerEvent(int piid, int event, int binderUid) {
        if (DEBUG) { Log.v(TAG, String.format("playerEvent(piid=%d, event=%d)", piid, event)); }
        if (DEBUG) { Log.v(TAG, String.format("playerEvent(piid=%d, event=%d)", piid, event)); }
        final boolean change;
        final boolean change;
        synchronized(mPlayerLock) {
        synchronized(mPlayerLock) {
            final AudioPlaybackConfiguration apc = mPlayers.get(new Integer(piid));
            final AudioPlaybackConfiguration apc = mPlayers.get(new Integer(piid));
            if (apc == null) {
            if (checkConfigurationCaller(piid, apc, binderUid)) {
                Log.e(TAG, "Unknown player " + piid + " for event " + event);
                change = false;
            } else {
                //TODO add generation counter to only update to the latest state
                //TODO add generation counter to only update to the latest state
                change = apc.handleStateEvent(event);
                change = apc.handleStateEvent(event);
            } else {
                Log.e(TAG, "Error handling event " + event);
                change = false;
            }
            }
        }
        }
        if (change) {
        if (change) {
@@ -112,13 +112,14 @@ public final class PlaybackActivityMonitor {
        }
        }
    }
    }


    public void releasePlayer(int piid) {
    public void releasePlayer(int piid, int binderUid) {
        if (DEBUG) { Log.v(TAG, "releasePlayer() for piid=" + piid); }
        if (DEBUG) { Log.v(TAG, "releasePlayer() for piid=" + piid); }
        synchronized(mPlayerLock) {
        synchronized(mPlayerLock) {
            if (!mPlayers.containsKey(new Integer(piid))) {
            final AudioPlaybackConfiguration apc = mPlayers.get(new Integer(piid));
                Log.e(TAG, "Unknown player " + piid + " for release");
            if (checkConfigurationCaller(piid, apc, binderUid)) {
            } else {
                mPlayers.remove(new Integer(piid));
                mPlayers.remove(new Integer(piid));
            } else {
                Log.e(TAG, "Error releasing player " + piid);
            }
            }
        }
        }
    }
    }
@@ -133,6 +134,25 @@ public final class PlaybackActivityMonitor {
        }
        }
    }
    }


    /**
     * Check that piid and uid are valid for the given configuration.
     * @param piid the piid of the player.
     * @param apc the configuration found for this piid.
     * @param binderUid actual uid of client trying to signal a player state/event/attributes.
     * @return true if the call is valid and the change should proceed, false otherwise.
     */
    private static boolean checkConfigurationCaller(int piid,
            final AudioPlaybackConfiguration apc, int binderUid) {
        if (apc == null) {
            Log.e(TAG, "Invalid operation: unknown player " + piid);
            return false;
        } else if ((binderUid != 0) && (apc.getClientUid() != binderUid)) {
            Log.e(TAG, "Forbidden operation from uid " + binderUid + " for player " + piid);
            return false;
        }
        return true;
    }

    private void dispatchPlaybackChange() {
    private void dispatchPlaybackChange() {
        synchronized (mClients) {
        synchronized (mClients) {
            // typical use case, nobody is listening, don't do any work
            // typical use case, nobody is listening, don't do any work