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

Commit 52fcf955 authored by Andy Hung's avatar Andy Hung Committed by Android (Google) Code Review
Browse files

Merge "Use device set for getDevicesForStream"

parents 07761ee5 1162c55e
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -828,13 +828,6 @@ android_media_AudioSystem_getMasterBalance(JNIEnv *env, jobject thiz)
    return balance;
}

static jint
android_media_AudioSystem_getDevicesForStream(JNIEnv *env, jobject thiz, jint stream)
{
    return (jint)deviceTypesToBitMask(
            AudioSystem::getDevicesForStream(static_cast<audio_stream_type_t>(stream)));
}

static jint
android_media_AudioSystem_getPrimaryOutputSamplingRate(JNIEnv *env, jobject clazz)
{
@@ -2959,7 +2952,6 @@ static const JNINativeMethod gMethods[] =
         {"getMasterMono", "()Z", (void *)android_media_AudioSystem_getMasterMono},
         {"setMasterBalance", "(F)I", (void *)android_media_AudioSystem_setMasterBalance},
         {"getMasterBalance", "()F", (void *)android_media_AudioSystem_getMasterBalance},
         {"getDevicesForStream", "(I)I", (void *)android_media_AudioSystem_getDevicesForStream},
         {"getPrimaryOutputSamplingRate", "()I",
          (void *)android_media_AudioSystem_getPrimaryOutputSamplingRate},
         {"getPrimaryOutputFrameCount", "()I",
+23 −17
Original line number Diff line number Diff line
@@ -5624,9 +5624,15 @@ public class AudioManager {
     * Note that the information may be imprecise when the implementation
     * cannot distinguish whether a particular device is enabled.
     *
     * {@hide}
     * @deprecated on {@link android.os.Build.VERSION_CODES#T} as new devices
     *             will have multi-bit device types.
     *             Prefer to use {@link #getDevicesForAttributes()} instead,
     *             noting that getDevicesForStream() has a few small discrepancies
     *             for better volume handling.
     * @hide
     */
    @UnsupportedAppUsage
    @Deprecated
    public int getDevicesForStream(int streamType) {
        switch (streamType) {
            case STREAM_VOICE_CALL:
@@ -5639,7 +5645,7 @@ public class AudioManager {
            case STREAM_ACCESSIBILITY:
                final IAudioService service = getService();
                try {
                return service.getDevicesForStream(streamType);
                    return service.getDeviceMaskForStream(streamType);
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
+67 −14
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.content.pm.PackageManager;
import android.media.audio.common.AidlConversion;
import android.media.audiofx.AudioEffect;
import android.media.audiopolicy.AudioMix;
import android.media.audiopolicy.AudioProductStrategy;
import android.os.Build;
import android.os.IBinder;
import android.os.Parcel;
@@ -47,6 +48,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

/* IF YOU CHANGE ANY OF THE CONSTANTS IN THIS FILE, DO NOT FORGET
 * TO UPDATE THE CORRESPONDING NATIVE GLUE AND AudioManager.java.
@@ -1655,9 +1657,63 @@ public class AudioSystem
    /** @hide */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    public static native boolean getMasterMute();
    /** @hide */
    /** @hide
     * Only used (unsupported) for legacy apps.
     * @deprecated on {@link android.os.Build.VERSION_CODES#T} as new devices
     *             will have multi-bit device types.
     *             Use {@link AudioManager#getDevicesForAttributes(AudioAttributes)} instead.
     */
    @UnsupportedAppUsage
    public static native int getDevicesForStream(int stream);
    @Deprecated
    public static int getDevicesForStream(int stream) {
        final AudioAttributes attr =
                AudioProductStrategy.getAudioAttributesForStrategyWithLegacyStreamType(stream);
        return getDeviceMaskFromSet(generateAudioDeviceTypesSet(
                getDevicesForAttributes(attr, true /* forVolume */)));
    }

    /** @hide
     * Conversion from a device set to a bit mask.
     *
     * Legacy compatibility method (use a device list instead of a bit mask).
     * Conversion to bit mask skips multi-bit (S and later) internal device types
     * (e.g. AudioSystem.DEVICE_OUT* or AudioManager.DEVICE_OUT*) for legacy
     * compatibility reasons.  Legacy apps will not understand these new device types
     * and it will raise false matches with old device types.
     */
    public static int getDeviceMaskFromSet(@NonNull Set<Integer> deviceSet) {
        int deviceMask = DEVICE_NONE; // zero.
        int deviceInChecksum = DEVICE_BIT_IN;
        for (Integer device : deviceSet) {
            if ((device & (device - 1) & ~DEVICE_BIT_IN) != 0) {
                Log.v(TAG, "getDeviceMaskFromSet skipping multi-bit device value " + device);
                continue;
            }
            deviceMask |= device;
            deviceInChecksum &= device;
        }
        // Validate that deviceSet is either ALL input devices or ALL output devices.
        // We check that the "OR" of all the DEVICE_BIT_INs == the "AND" of all DEVICE_BIT_INs.
        if (!deviceSet.isEmpty() && deviceInChecksum != (deviceMask & DEVICE_BIT_IN)) {
            Log.e(TAG, "getDeviceMaskFromSet: Invalid set: " + deviceSetToString(deviceSet));
        }
        return deviceMask;
    }

    /** @hide */
    @NonNull
    public static String deviceSetToString(@NonNull Set<Integer> devices) {
        int n = 0;
        StringBuilder sb = new StringBuilder();
        for (Integer device : devices) {
            if (n++ > 0) {
                sb.append(", ");
            }
            sb.append(AudioSystem.getDeviceName(device));
            sb.append("(" + Integer.toHexString(device) + ")");
        }
        return sb.toString();
    }

    /**
     * @hide
@@ -2252,18 +2308,15 @@ public class AudioSystem

    /**
     * @hide
     * Return a set of audio device types from a bit mask audio device type, which may
     * Return a set of audio device types from a list of audio device attributes, which may
     * represent multiple audio device types.
     * FIXME: Remove this when getting ride of bit mask usage of audio device types.
     */
    public static Set<Integer> generateAudioDeviceTypesSet(int types) {
        Set<Integer> deviceTypes = new HashSet<>();
        Set<Integer> allDeviceTypes =
                (types & DEVICE_BIT_IN) == 0 ? DEVICE_OUT_ALL_SET : DEVICE_IN_ALL_SET;
        for (int deviceType : allDeviceTypes) {
            if ((types & deviceType) == deviceType) {
                deviceTypes.add(deviceType);
            }
    @NonNull
    public static Set<Integer> generateAudioDeviceTypesSet(
            @NonNull List<AudioDeviceAttributes> deviceList) {
        Set<Integer> deviceTypes = new TreeSet<>();
        for (AudioDeviceAttributes device : deviceList) {
            deviceTypes.add(device.getInternalType());
        }
        return deviceTypes;
    }
@@ -2274,7 +2327,7 @@ public class AudioSystem
     */
    public static Set<Integer> intersectionAudioDeviceTypes(
            @NonNull Set<Integer> a, @NonNull Set<Integer> b) {
        Set<Integer> intersection = new HashSet<>(a);
        Set<Integer> intersection = new TreeSet<>(a);
        intersection.retainAll(b);
        return intersection;
    }
+1 −1
Original line number Diff line number Diff line
@@ -360,7 +360,7 @@ interface IAudioService {

    boolean isMusicActive(in boolean remotely);

    int getDevicesForStream(in int streamType);
    int getDeviceMaskForStream(in int streamType);

    int[] getAvailableCommunicationDeviceIds();

+9 −2
Original line number Diff line number Diff line
@@ -1565,6 +1565,13 @@ import java.util.concurrent.atomic.AtomicBoolean;

    private AtomicBoolean mMusicMuted = new AtomicBoolean(false);

    private static <T> boolean hasIntersection(Set<T> a, Set<T> b) {
        for (T e : a) {
            if (b.contains(e)) return true;
        }
        return false;
    }

    boolean messageMutesMusic(int message) {
        if (message == 0) {
            return false;
@@ -1574,8 +1581,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
                || message == MSG_L_A2DP_DEVICE_CONNECTION_CHANGE_EXT
                || message == MSG_L_A2DP_DEVICE_CONFIG_CHANGE)
                && AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0)
                && mDeviceInventory.DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG_SET.contains(
                        mAudioService.getDevicesForStream(AudioSystem.STREAM_MUSIC))) {
                && hasIntersection(mDeviceInventory.DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG_SET,
                        mAudioService.getDeviceSetForStream(AudioSystem.STREAM_MUSIC))) {
            return false;
        }
        return true;
Loading