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

Commit 1d7bfbd0 authored by Ján Sebechlebský's avatar Ján Sebechlebský Committed by Android (Google) Code Review
Browse files

Merge "Bypass audio focus handling for VDM"

parents b8651661 84e9721f
Loading
Loading
Loading
Loading
+32 −11
Original line number Original line Diff line number Diff line
@@ -106,6 +106,7 @@ public class AudioManager {


    private Context mOriginalContext;
    private Context mOriginalContext;
    private Context mApplicationContext;
    private Context mApplicationContext;
    private int mOriginalContextDeviceId = DEVICE_ID_DEFAULT;
    private @Nullable VirtualDeviceManager mVirtualDeviceManager; // Lazy initialized.
    private @Nullable VirtualDeviceManager mVirtualDeviceManager; // Lazy initialized.
    private static final String TAG = "AudioManager";
    private static final String TAG = "AudioManager";
    private static final boolean DEBUG = false;
    private static final boolean DEBUG = false;
@@ -844,6 +845,7 @@ public class AudioManager {
    }
    }


    private void setContext(Context context) {
    private void setContext(Context context) {
        mOriginalContextDeviceId = context.getDeviceId();
        mApplicationContext = context.getApplicationContext();
        mApplicationContext = context.getApplicationContext();
        if (mApplicationContext != null) {
        if (mApplicationContext != null) {
            mOriginalContext = null;
            mOriginalContext = null;
@@ -3958,7 +3960,7 @@ public class AudioManager {
    }
    }


    /**
    /**
     * Checks whether this {@link AudioManager} instance is asociated with {@link VirtualDevice}
     * Checks whether this {@link AudioManager} instance is associated with {@link VirtualDevice}
     * configured with custom device policy for audio. If there is such device, request to play
     * configured with custom device policy for audio. If there is such device, request to play
     * sound effect is forwarded to {@link VirtualDeviceManager}.
     * sound effect is forwarded to {@link VirtualDeviceManager}.
     *
     *
@@ -3967,18 +3969,24 @@ public class AudioManager {
     * false otherwise.
     * false otherwise.
     */
     */
    private boolean delegateSoundEffectToVdm(@SystemSoundEffect int effectType) {
    private boolean delegateSoundEffectToVdm(@SystemSoundEffect int effectType) {
        int deviceId = getContext().getDeviceId();
        if (hasCustomPolicyVirtualDeviceContext()) {
        if (deviceId != DEVICE_ID_DEFAULT) {
            VirtualDeviceManager vdm = getVirtualDeviceManager();
            VirtualDeviceManager vdm = getVirtualDeviceManager();
            if (vdm != null && vdm.getDevicePolicy(deviceId, POLICY_TYPE_AUDIO)
            vdm.playSoundEffect(mOriginalContextDeviceId, effectType);
                    != DEVICE_POLICY_DEFAULT) {
                vdm.playSoundEffect(deviceId, effectType);
            return true;
            return true;
        }
        }
        return false;
    }
    }

    private boolean hasCustomPolicyVirtualDeviceContext() {
        if (mOriginalContextDeviceId == DEVICE_ID_DEFAULT) {
            return false;
            return false;
        }
        }


        VirtualDeviceManager vdm = getVirtualDeviceManager();
        return vdm != null && vdm.getDevicePolicy(mOriginalContextDeviceId, POLICY_TYPE_AUDIO)
                != DEVICE_POLICY_DEFAULT;
    }

    /**
    /**
     *  Load Sound effects.
     *  Load Sound effects.
     *  This method must be called when sound effects are enabled.
     *  This method must be called when sound effects are enabled.
@@ -4677,6 +4685,16 @@ public class AudioManager {
            throw new IllegalArgumentException(
            throw new IllegalArgumentException(
                    "Illegal null audio policy when locking audio focus");
                    "Illegal null audio policy when locking audio focus");
        }
        }

        if (hasCustomPolicyVirtualDeviceContext()) {
            // If the focus request was made within context associated with VirtualDevice
            // configured with custom device policy for audio, bypass audio service focus handling.
            // The custom device policy for audio means that audio associated with this device
            // is likely rerouted to VirtualAudioDevice and playback on the VirtualAudioDevice
            // shouldn't affect non-virtual audio tracks (and vice versa).
            return AUDIOFOCUS_REQUEST_GRANTED;
        }

        registerAudioFocusRequest(afr);
        registerAudioFocusRequest(afr);
        final IAudioService service = getService();
        final IAudioService service = getService();
        final int status;
        final int status;
@@ -4949,16 +4967,19 @@ public class AudioManager {
    @SuppressLint("RequiresPermission") // no permission enforcement, but only "undoes" what would
    @SuppressLint("RequiresPermission") // no permission enforcement, but only "undoes" what would
    // have been done by a matching requestAudioFocus
    // have been done by a matching requestAudioFocus
    public int abandonAudioFocus(OnAudioFocusChangeListener l, AudioAttributes aa) {
    public int abandonAudioFocus(OnAudioFocusChangeListener l, AudioAttributes aa) {
        int status = AUDIOFOCUS_REQUEST_FAILED;
        if (hasCustomPolicyVirtualDeviceContext()) {
            // If this AudioManager instance is running within VirtualDevice context configured
            // with custom device policy for audio, the audio focus handling is bypassed.
            return AUDIOFOCUS_REQUEST_GRANTED;
        }
        unregisterAudioFocusRequest(l);
        unregisterAudioFocusRequest(l);
        final IAudioService service = getService();
        final IAudioService service = getService();
        try {
        try {
            status = service.abandonAudioFocus(mAudioFocusDispatcher,
            return service.abandonAudioFocus(mAudioFocusDispatcher,
                    getIdForAudioFocusListener(l), aa, getContext().getOpPackageName());
                    getIdForAudioFocusListener(l), aa, getContext().getOpPackageName());
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
            throw e.rethrowFromSystemServer();
        }
        }
        return status;
    }
    }


    //====================================================================
    //====================================================================