Merge "AudioManager: isFixedVolume() fix for dynamic volume behavior"

This commit is contained in:
TreeHugger Robot
2021-10-28 19:23:55 +00:00
committed by Android (Google) Code Review
3 changed files with 37 additions and 14 deletions

View File

@@ -96,8 +96,6 @@ public class AudioManager {
private Context mOriginalContext;
private Context mApplicationContext;
private long mVolumeKeyUpTime;
private boolean mUseFixedVolumeInitialized;
private boolean mUseFixedVolume;
private static final String TAG = "AudioManager";
private static final boolean DEBUG = false;
private static final AudioPortEventHandler sAudioPortEventHandler = new AudioPortEventHandler();
@@ -893,19 +891,13 @@ public class AudioManager {
* </ul>
*/
public boolean isVolumeFixed() {
synchronized (this) {
try {
if (!mUseFixedVolumeInitialized) {
mUseFixedVolume = getContext().getResources().getBoolean(
com.android.internal.R.bool.config_useFixedVolume);
}
} catch (Exception e) {
} finally {
// only ever try once, so always consider initialized even if query failed
mUseFixedVolumeInitialized = true;
}
boolean res = false;
try {
res = getService().isVolumeFixed();
} catch (RemoteException e) {
Log.e(TAG, "Error querying isVolumeFixed", e);
}
return mUseFixedVolume;
return res;
}
/**

View File

@@ -454,4 +454,6 @@ interface IAudioService {
void registerSpatializerOutputCallback(in ISpatializerOutputCallback cb);
void unregisterSpatializerOutputCallback(in ISpatializerOutputCallback cb);
boolean isVolumeFixed();
}

View File

@@ -6214,9 +6214,15 @@ public class AudioService extends IAudioService.Stub
*/
public @AudioManager.DeviceVolumeBehavior
int getDeviceVolumeBehavior(@NonNull AudioDeviceAttributes device) {
Objects.requireNonNull(device);
// verify permissions
enforceQueryStateOrModifyRoutingPermission();
return getDeviceVolumeBehaviorInt(device);
}
private @AudioManager.DeviceVolumeBehavior
int getDeviceVolumeBehaviorInt(@NonNull AudioDeviceAttributes device) {
// translate Java device type to native device type (for the devices masks for full / fixed)
final int audioSystemDeviceOut = AudioDeviceInfo.convertDeviceTypeToInternalDevice(
device.getType());
@@ -6244,6 +6250,29 @@ public class AudioService extends IAudioService.Stub
return AudioManager.DEVICE_VOLUME_BEHAVIOR_VARIABLE;
}
/**
* @see AudioManager#isVolumeFixed()
* Note there are no permission checks on this operation, as this is part of API 21
* @return true if the current device's volume behavior for media is
* DEVICE_VOLUME_BEHAVIOR_FIXED
*/
public boolean isVolumeFixed() {
if (mUseFixedVolume) {
return true;
}
final AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.build();
// calling getDevice*Int to bypass permission check
final List<AudioDeviceAttributes> devices = getDevicesForAttributesInt(attributes);
for (AudioDeviceAttributes device : devices) {
if (getDeviceVolumeBehaviorInt(device) == AudioManager.DEVICE_VOLUME_BEHAVIOR_FIXED) {
return true;
}
}
return false;
}
/*package*/ static final int CONNECTION_STATE_DISCONNECTED = 0;
/*package*/ static final int CONNECTION_STATE_CONNECTED = 1;
/**