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

Commit 40d6a46b authored by Ian Baker's avatar Ian Baker
Browse files

Remove static memoization from AudioPlayerStateMonitor.getInstance

This static memoization can result in the Context parameter being
ignored, and an instance associated with one Context being returned
to a caller passing a different Context.

The static memoization was already present before the Context parameter
was introduced in ag/6935293.

Bug: 438917725
Test: Presubmit
Flag: com.android.media.mediasession.flags.remove_audioplayerstatemonitor_static_memoization
Change-Id: Ic8ead19edb46ae0f65b6664a2b46b30b0aca29f4
parent 1245747b
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -44,3 +44,10 @@ flag {
               "from infinity to zero."
  bug: "432003816"
}

flag {
  name: "remove_audioplayerstatemonitor_static_memoization"
  namespace: "media_solutions"
  description: "Remove the static memoization from AudioPlayerStateMonitor.getInstance(Context)."
  bug: "438917725"
}
+14 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.util.ArraySet;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;
import com.android.media.mediasession.flags.Flags;
import com.android.server.SystemService;

import java.io.PrintWriter;
@@ -111,8 +112,20 @@ class AudioPlayerStateMonitor {
    @SuppressWarnings("WeakerAccess") /* synthetic access */
    final List<Integer> mSortedAudioPlaybackClientUids = new ArrayList<>();

    /**
     * Static factory method.
     *
     * <p>If {@link Flags#removeAudioplayerstatemonitorStaticMemoization()} is true then this method
     * creates a new instance (and registers a new listener on {@link AudioManager}) each time it's
     * called, so should only be called once per {@link SystemService} lifecycle.
     *
     * @param context A context.
     * @return The constructed instance.
     */
    static AudioPlayerStateMonitor getInstance(Context context) {
        // TODO: b/438917725 - Remove this static memoization.
        if (Flags.removeAudioplayerstatemonitorStaticMemoization()) {
            return getInstance(context, Set.of());
        }
        synchronized (AudioPlayerStateMonitor.class) {
            if (sInstance == null) {
                sInstance = new AudioPlayerStateMonitor(context, Set.of());