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

Commit d83bec70 authored by Eric Laurent's avatar Eric Laurent Committed by Android (Google) Code Review
Browse files

Merge changes I8c01f31d,I0a2b90d1,I78bfa0d1,I803f9df1,I3ad42802, ... into tm-qpr-dev

* changes:
  [IMPR] AudioProductStrategy: add getName hidden API
  fixup '[IMPR] AudioManager: add adjustAttributesVolume API'
  [IMPR] AudioService: improve bijectivity between VSS / VGS
  [BUG] AudioService: fix mute/umute of aliased streams.
  [IMPR] AudioManager: add adjustAttributesVolume API
  [IMPR] AudioService: VolumeGroupState: improve implementation
  [IMPR] AudioProductStrategy: get volume group from AudioAttributes
parents 558219ec 362e4ef5
Loading
Loading
Loading
Loading
+167 −17
Original line number Diff line number Diff line
@@ -1337,12 +1337,8 @@ public class AudioManager {
    public void setVolumeIndexForAttributes(@NonNull AudioAttributes attr, int index, int flags) {
        Preconditions.checkNotNull(attr, "attr must not be null");
        final IAudioService service = getService();
        try {
            service.setVolumeIndexForAttributes(attr, index, flags,
                    getContext().getOpPackageName(), getContext().getAttributionTag());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
        int groupId = getVolumeGroupIdForAttributes(attr);
        setVolumeGroupVolumeIndex(groupId, index, flags);
    }

    /**
@@ -1361,11 +1357,8 @@ public class AudioManager {
    public int getVolumeIndexForAttributes(@NonNull AudioAttributes attr) {
        Preconditions.checkNotNull(attr, "attr must not be null");
        final IAudioService service = getService();
        try {
            return service.getVolumeIndexForAttributes(attr);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
        int groupId = getVolumeGroupIdForAttributes(attr);
        return getVolumeGroupVolumeIndex(groupId);
    }

    /**
@@ -1382,11 +1375,8 @@ public class AudioManager {
    public int getMaxVolumeIndexForAttributes(@NonNull AudioAttributes attr) {
        Preconditions.checkNotNull(attr, "attr must not be null");
        final IAudioService service = getService();
        try {
            return service.getMaxVolumeIndexForAttributes(attr);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
        int groupId = getVolumeGroupIdForAttributes(attr);
        return getVolumeGroupMaxVolumeIndex(groupId);
    }

    /**
@@ -1403,8 +1393,168 @@ public class AudioManager {
    public int getMinVolumeIndexForAttributes(@NonNull AudioAttributes attr) {
        Preconditions.checkNotNull(attr, "attr must not be null");
        final IAudioService service = getService();
        int groupId = getVolumeGroupIdForAttributes(attr);
        return getVolumeGroupMinVolumeIndex(groupId);
    }

    /**
     * Returns the volume group id associated to the given {@link AudioAttributes}.
     *
     * @param attributes The {@link AudioAttributes} to consider.
     * @return {@link android.media.audiopolicy.AudioVolumeGroup} id supporting the given
     * {@link AudioAttributes} if found,
     * {@code android.media.audiopolicy.AudioVolumeGroup.DEFAULT_VOLUME_GROUP} otherwise.
     * @hide
     */
    public int getVolumeGroupIdForAttributes(@NonNull AudioAttributes attributes) {
        Preconditions.checkNotNull(attributes, "Audio Attributes must not be null");
        return AudioProductStrategy.getVolumeGroupIdForAudioAttributes(attributes,
                /* fallbackOnDefault= */ false);
    }

    /**
     * Sets the volume index for a particular group associated to given id.
     * <p> Call first in prior {@link getVolumeGroupIdForAttributes} to retrieve the volume group
     * id supporting the given {@link AudioAttributes}.
     *
     * @param groupId of the {@link android.media.audiopolicy.AudioVolumeGroup} to consider.
     * @param index The volume index to set. See
     *          {@link #getVolumeGroupMaxVolumeIndex(id)} for the largest valid value
     *          {@link #getVolumeGroupMinVolumeIndex(id)} for the lowest valid value.
     * @param flags One or more flags.
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING)
    public void setVolumeGroupVolumeIndex(int groupId, int index, int flags) {
        final IAudioService service = getService();
        try {
            service.setVolumeGroupVolumeIndex(groupId, index, flags,
                    getContext().getOpPackageName(), getContext().getAttributionTag());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Returns the current volume index for a particular group associated to given id.
     * <p> Call first in prior {@link getVolumeGroupIdForAttributes} to retrieve the volume group
     * id supporting the given {@link AudioAttributes}.
     *
     * @param groupId of the {@link android.media.audiopolicy.AudioVolumeGroup} to consider.
     * @return The current volume index for the stream.
     * @hide
     */
    @IntRange(from = 0)
    @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING)
    public int getVolumeGroupVolumeIndex(int groupId) {
        final IAudioService service = getService();
        try {
            return service.getVolumeGroupVolumeIndex(groupId);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Returns the maximum volume index for a particular group associated to given id.
     * <p> Call first in prior {@link getVolumeGroupIdForAttributes} to retrieve the volume group
     * id supporting the given {@link AudioAttributes}.
     *
     * @param groupId of the {@link android.media.audiopolicy.AudioVolumeGroup} to consider.
     * @return The maximum valid volume index for the {@link AudioAttributes}.
     * @hide
     */
    @IntRange(from = 0)
    @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING)
    public int getVolumeGroupMaxVolumeIndex(int groupId) {
        final IAudioService service = getService();
        try {
            return service.getVolumeGroupMaxVolumeIndex(groupId);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Returns the minimum volume index for a particular group associated to given id.
     * <p> Call first in prior {@link getVolumeGroupIdForAttributes} to retrieve the volume group
     * id supporting the given {@link AudioAttributes}.
     *
     * @param groupId of the {@link android.media.audiopolicy.AudioVolumeGroup} to consider.
     * @return The minimum valid volume index for the {@link AudioAttributes}.
     * @hide
     */
    @IntRange(from = 0)
    @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING)
    public int getVolumeGroupMinVolumeIndex(int groupId) {
        final IAudioService service = getService();
        try {
            return service.getVolumeGroupMinVolumeIndex(groupId);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Adjusts the volume of a particular group associated to given id by one step in a direction.
     * <p> If the volume group is associated to a stream type, it fallbacks on
     * {@link AudioManager#adjustStreamVolume()} for compatibility reason.
     * <p> Call first in prior {@link getVolumeGroupIdForAttributes} to retrieve the volume group
     * id supporting the given {@link AudioAttributes}.
     *
     * @param groupId of the {@link android.media.audiopolicy.AudioVolumeGroup} to consider.
     * @param direction The direction to adjust the volume. One of
     *            {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or
     *            {@link #ADJUST_SAME}.
     * @param flags One or more flags.
     * @throws SecurityException if the adjustment triggers a Do Not Disturb change and the caller
     * is not granted notification policy access.
     * @hide
     */
    public void adjustVolumeGroupVolume(int groupId, int direction, int flags) {
        IAudioService service = getService();
        try {
            service.adjustVolumeGroupVolume(groupId, direction, flags,
                    getContext().getOpPackageName());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Get last audible volume of the group associated to given id before it was muted.
     * <p> Call first in prior {@link getVolumeGroupIdForAttributes} to retrieve the volume group
     * id supporting the given {@link AudioAttributes}.
     *
     * @param groupId of the {@link android.media.audiopolicy.AudioVolumeGroup} to consider.
     * @return current volume if not muted, volume before muted otherwise.
     * @hide
     */
    @RequiresPermission("android.permission.QUERY_AUDIO_STATE")
    @IntRange(from = 0)
    public int getLastAudibleVolumeGroupVolume(int groupId) {
        IAudioService service = getService();
        try {
            return service.getLastAudibleVolumeGroupVolume(groupId);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Returns the current mute state for a particular volume group associated to the given id.
     * <p> Call first in prior {@link getVolumeGroupIdForAttributes} to retrieve the volume group
     * id supporting the given {@link AudioAttributes}.
     *
     * @param groupId of the {@link android.media.audiopolicy.AudioVolumeGroup} to consider.
     * @return The mute state for the given {@link android.media.audiopolicy.AudioVolumeGroup} id.
     * @see #adjustAttributesVolume(AudioAttributes, int, int)
     * @hide
     */
    public boolean isVolumeGroupMuted(int groupId) {
        IAudioService service = getService();
        try {
            return service.getMinVolumeIndexForAttributes(attr);
            return service.isVolumeGroupMuted(groupId);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+11 −5
Original line number Diff line number Diff line
@@ -126,14 +126,20 @@ interface IAudioService {

    List<AudioVolumeGroup> getAudioVolumeGroups();

    void setVolumeIndexForAttributes(in AudioAttributes aa, int index, int flags,
            String callingPackage, in String attributionTag);
    void setVolumeGroupVolumeIndex(int groupId, int index, int flags, String callingPackage,
            in String attributionTag);

    int getVolumeGroupVolumeIndex(int groupId);

    int getVolumeGroupMaxVolumeIndex(int groupId);

    int getVolumeGroupMinVolumeIndex(int groupId);

    int getVolumeIndexForAttributes(in AudioAttributes aa);
    int getLastAudibleVolumeGroupVolume(int groupId);

    int getMaxVolumeIndexForAttributes(in AudioAttributes aa);
    boolean isVolumeGroupMuted(int groupId);

    int getMinVolumeIndexForAttributes(in AudioAttributes aa);
    void adjustVolumeGroupVolume(int groupId, int direction, int flags, String callingPackage);

    int getLastAudibleStreamVolume(int streamType);

+53 −9
Original line number Diff line number Diff line
@@ -29,10 +29,10 @@ import android.text.TextUtils;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.Preconditions;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @hide
@@ -142,7 +142,7 @@ public final class AudioProductStrategy implements Parcelable {
     */
    public static int getLegacyStreamTypeForStrategyWithAudioAttributes(
            @NonNull AudioAttributes audioAttributes) {
        Preconditions.checkNotNull(audioAttributes, "AudioAttributes must not be null");
        Objects.requireNonNull(audioAttributes, "AudioAttributes must not be null");
        for (final AudioProductStrategy productStrategy :
                AudioProductStrategy.getAudioProductStrategies()) {
            if (productStrategy.supportsAudioAttributes(audioAttributes)) {
@@ -162,6 +162,30 @@ public final class AudioProductStrategy implements Parcelable {
        return AudioSystem.STREAM_MUSIC;
    }

    /**
     * @hide
     * @param attributes the {@link AudioAttributes} to identify VolumeGroupId with
     * @param fallbackOnDefault if set, allows to fallback on the default group (e.g. the group
     *                          associated to {@link AudioManager#STREAM_MUSIC}).
     * @return volume group id associated with the given {@link AudioAttributes} if found,
     *     default volume group id if fallbackOnDefault is set
     * <p>By convention, the product strategy with default attributes will be associated to the
     * default volume group (e.g. associated to {@link AudioManager#STREAM_MUSIC})
     * or {@link AudioVolumeGroup#DEFAULT_VOLUME_GROUP} if not found.
     */
    public static int getVolumeGroupIdForAudioAttributes(
            @NonNull AudioAttributes attributes, boolean fallbackOnDefault) {
        Objects.requireNonNull(attributes, "attributes must not be null");
        int volumeGroupId = getVolumeGroupIdForAudioAttributesInt(attributes);
        if (volumeGroupId != AudioVolumeGroup.DEFAULT_VOLUME_GROUP) {
            return volumeGroupId;
        }
        if (fallbackOnDefault) {
            return getVolumeGroupIdForAudioAttributesInt(getDefaultAttributes());
        }
        return AudioVolumeGroup.DEFAULT_VOLUME_GROUP;
    }

    private static List<AudioProductStrategy> initializeAudioProductStrategies() {
        ArrayList<AudioProductStrategy> apsList = new ArrayList<AudioProductStrategy>();
        int status = native_list_audio_product_strategies(apsList);
@@ -192,8 +216,8 @@ public final class AudioProductStrategy implements Parcelable {
     */
    private AudioProductStrategy(@NonNull String name, int id,
            @NonNull AudioAttributesGroup[] aag) {
        Preconditions.checkNotNull(name, "name must not be null");
        Preconditions.checkNotNull(aag, "AudioAttributesGroups must not be null");
        Objects.requireNonNull(name, "name must not be null");
        Objects.requireNonNull(aag, "AudioAttributesGroups must not be null");
        mName = name;
        mId = id;
        mAudioAttributesGroups = aag;
@@ -209,6 +233,15 @@ public final class AudioProductStrategy implements Parcelable {
        return mId;
    }

    /**
     * @hide
     * @return the product strategy ID (which is the generalisation of Car Audio Usage / legacy
     *         routing_strategy linked to {@link AudioAttributes#getUsage()}).
     */
    @NonNull public String getName() {
        return mName;
    }

    /**
     * @hide
     * @return first {@link AudioAttributes} associated to this product strategy.
@@ -243,7 +276,7 @@ public final class AudioProductStrategy implements Parcelable {
     */
    @TestApi
    public int getLegacyStreamTypeForAudioAttributes(@NonNull AudioAttributes aa) {
        Preconditions.checkNotNull(aa, "AudioAttributes must not be null");
        Objects.requireNonNull(aa, "AudioAttributes must not be null");
        for (final AudioAttributesGroup aag : mAudioAttributesGroups) {
            if (aag.supportsAttributes(aa)) {
                return aag.getStreamType();
@@ -260,7 +293,7 @@ public final class AudioProductStrategy implements Parcelable {
     */
    @SystemApi
    public boolean supportsAudioAttributes(@NonNull AudioAttributes aa) {
        Preconditions.checkNotNull(aa, "AudioAttributes must not be null");
        Objects.requireNonNull(aa, "AudioAttributes must not be null");
        for (final AudioAttributesGroup aag : mAudioAttributesGroups) {
            if (aag.supportsAttributes(aa)) {
                return true;
@@ -293,7 +326,7 @@ public final class AudioProductStrategy implements Parcelable {
     */
    @TestApi
    public int getVolumeGroupIdForAudioAttributes(@NonNull AudioAttributes aa) {
        Preconditions.checkNotNull(aa, "AudioAttributes must not be null");
        Objects.requireNonNull(aa, "AudioAttributes must not be null");
        for (final AudioAttributesGroup aag : mAudioAttributesGroups) {
            if (aag.supportsAttributes(aa)) {
                return aag.getVolumeGroupId();
@@ -302,6 +335,17 @@ public final class AudioProductStrategy implements Parcelable {
        return AudioVolumeGroup.DEFAULT_VOLUME_GROUP;
    }

    private static int getVolumeGroupIdForAudioAttributesInt(@NonNull AudioAttributes attributes) {
        Objects.requireNonNull(attributes, "attributes must not be null");
        for (AudioProductStrategy productStrategy : getAudioProductStrategies()) {
            int volumeGroupId = productStrategy.getVolumeGroupIdForAudioAttributes(attributes);
            if (volumeGroupId != AudioVolumeGroup.DEFAULT_VOLUME_GROUP) {
                return volumeGroupId;
            }
        }
        return AudioVolumeGroup.DEFAULT_VOLUME_GROUP;
    }

    @Override
    public int describeContents() {
        return 0;
@@ -377,8 +421,8 @@ public final class AudioProductStrategy implements Parcelable {
     */
    private static boolean attributesMatches(@NonNull AudioAttributes refAttr,
            @NonNull AudioAttributes attr) {
        Preconditions.checkNotNull(refAttr, "refAttr must not be null");
        Preconditions.checkNotNull(attr, "attr must not be null");
        Objects.requireNonNull(refAttr, "reference AudioAttributes must not be null");
        Objects.requireNonNull(attr, "requester's AudioAttributes must not be null");
        String refFormattedTags = TextUtils.join(";", refAttr.getTags());
        String cliFormattedTags = TextUtils.join(";", attr.getTags());
        if (refAttr.equals(DEFAULT_ATTRIBUTES)) {
+524 −179

File changed.

Preview size limit exceeded, changes collapsed.

+21 −14
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.server.audio;

import android.annotation.NonNull;
import android.media.AudioAttributes;
import android.media.AudioDeviceAttributes;
import android.media.AudioManager;
import android.media.AudioSystem;
@@ -221,6 +220,7 @@ public class AudioServiceEvents {
        static final int VOL_SET_GROUP_VOL = 8;
        static final int VOL_MUTE_STREAM_INT = 9;
        static final int VOL_SET_LE_AUDIO_VOL = 10;
        static final int VOL_ADJUST_GROUP_VOL = 11;

        final int mOp;
        final int mStream;
@@ -228,7 +228,6 @@ public class AudioServiceEvents {
        final int mVal2;
        final String mCaller;
        final String mGroupName;
        final AudioAttributes mAudioAttributes;

        /** used for VOL_ADJUST_VOL_UID,
         *           VOL_ADJUST_SUGG_VOL,
@@ -241,7 +240,6 @@ public class AudioServiceEvents {
            mVal2 = val2;
            mCaller = caller;
            mGroupName = null;
            mAudioAttributes = null;
            logMetricEvent();
        }

@@ -254,7 +252,6 @@ public class AudioServiceEvents {
            mStream = -1;
            mCaller = null;
            mGroupName = null;
            mAudioAttributes = null;
            logMetricEvent();
        }

@@ -267,7 +264,6 @@ public class AudioServiceEvents {
            mStream = -1;
            mCaller = null;
            mGroupName = null;
            mAudioAttributes = null;
            logMetricEvent();
        }

@@ -280,7 +276,6 @@ public class AudioServiceEvents {
            // unused
            mCaller = null;
            mGroupName = null;
            mAudioAttributes = null;
            logMetricEvent();
        }

@@ -293,19 +288,18 @@ public class AudioServiceEvents {
            // unused
            mCaller = null;
            mGroupName = null;
            mAudioAttributes = null;
            logMetricEvent();
        }

        /** used for VOL_SET_GROUP_VOL */
        VolumeEvent(int op, AudioAttributes aa, String group, int index, int flags, String caller) {
        /** used for VOL_SET_GROUP_VOL,
         *           VOL_ADJUST_GROUP_VOL */
        VolumeEvent(int op, String group, int index, int flags, String caller) {
            mOp = op;
            mStream = -1;
            mVal1 = index;
            mVal2 = flags;
            mCaller = caller;
            mGroupName = group;
            mAudioAttributes = aa;
            logMetricEvent();
        }

@@ -317,7 +311,6 @@ public class AudioServiceEvents {
            mVal2 = 0;
            mCaller = null;
            mGroupName = null;
            mAudioAttributes = null;
            logMetricEvent();
        }

@@ -359,6 +352,15 @@ public class AudioServiceEvents {
                            .record();
                    return;
                }
                case VOL_ADJUST_GROUP_VOL:
                    new MediaMetrics.Item(mMetricsId)
                            .set(MediaMetrics.Property.CALLING_PACKAGE, mCaller)
                            .set(MediaMetrics.Property.DIRECTION, mVal1 > 0 ? "up" : "down")
                            .set(MediaMetrics.Property.EVENT, "adjustVolumeGroupVolume")
                            .set(MediaMetrics.Property.FLAGS, mVal2)
                            .set(MediaMetrics.Property.GROUP, mGroupName)
                            .record();
                    return;
                case VOL_SET_STREAM_VOL:
                    new MediaMetrics.Item(mMetricsId)
                            .set(MediaMetrics.Property.CALLING_PACKAGE, mCaller)
@@ -410,7 +412,6 @@ public class AudioServiceEvents {
                    return;
                case VOL_SET_GROUP_VOL:
                    new MediaMetrics.Item(mMetricsId)
                            .set(MediaMetrics.Property.ATTRIBUTES, mAudioAttributes.toString())
                            .set(MediaMetrics.Property.CALLING_PACKAGE, mCaller)
                            .set(MediaMetrics.Property.EVENT, "setVolumeIndexForAttributes")
                            .set(MediaMetrics.Property.FLAGS, mVal2)
@@ -436,6 +437,13 @@ public class AudioServiceEvents {
                            .append(" flags:0x").append(Integer.toHexString(mVal2))
                            .append(") from ").append(mCaller)
                            .toString();
                case VOL_ADJUST_GROUP_VOL:
                    return new StringBuilder("adjustVolumeGroupVolume(group:")
                            .append(mGroupName)
                            .append(" dir:").append(AudioManager.adjustToString(mVal1))
                            .append(" flags:0x").append(Integer.toHexString(mVal2))
                            .append(") from ").append(mCaller)
                            .toString();
                case VOL_ADJUST_STREAM_VOL:
                    return new StringBuilder("adjustStreamVolume(stream:")
                            .append(AudioSystem.streamToString(mStream))
@@ -484,8 +492,7 @@ public class AudioServiceEvents {
                            .append(" stream:").append(AudioSystem.streamToString(mStream))
                            .toString();
                case VOL_SET_GROUP_VOL:
                    return new StringBuilder("setVolumeIndexForAttributes(attr:")
                            .append(mAudioAttributes.toString())
                    return new StringBuilder("setVolumeIndexForAttributes(group:")
                            .append(" group: ").append(mGroupName)
                            .append(" index:").append(mVal1)
                            .append(" flags:0x").append(Integer.toHexString(mVal2))