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

Skip to content
Snippets Groups Projects
Commit e0112b77 authored by Vlad Popa's avatar Vlad Popa Committed by Android (Google) Code Review
Browse files

Merge "CSD: Improve map from device type to volume index" into udc-dev

parents aede9255 3ca01c57
Branches
No related tags found
No related merge requests found
...@@ -41,6 +41,7 @@ import android.provider.Settings; ...@@ -41,6 +41,7 @@ import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.MathUtils; import android.util.MathUtils;
import android.util.SparseIntArray;
import com.android.internal.R; import com.android.internal.R;
import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.GuardedBy;
...@@ -52,7 +53,6 @@ import com.android.server.utils.EventLogger; ...@@ -52,7 +53,6 @@ import com.android.server.utils.EventLogger;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
...@@ -112,6 +112,8 @@ public class SoundDoseHelper { ...@@ -112,6 +112,8 @@ public class SoundDoseHelper {
private static final long GLOBAL_TIME_OFFSET_UNINITIALIZED = -1; private static final long GLOBAL_TIME_OFFSET_UNINITIALIZED = -1;
private static final int SAFE_MEDIA_VOLUME_UNINITIALIZED = -1;
private final EventLogger mLogger = new EventLogger(AudioService.LOG_NB_EVENTS_SOUND_DOSE, private final EventLogger mLogger = new EventLogger(AudioService.LOG_NB_EVENTS_SOUND_DOSE,
"CSD updates"); "CSD updates");
...@@ -132,15 +134,6 @@ public class SoundDoseHelper { ...@@ -132,15 +134,6 @@ public class SoundDoseHelper {
// For now using the same value for CSD supported devices // For now using the same value for CSD supported devices
private float mSafeMediaVolumeDbfs; private float mSafeMediaVolumeDbfs;
private static class SafeDeviceVolumeInfo {
int mDeviceType;
int mSafeVolumeIndex = -1;
SafeDeviceVolumeInfo(int deviceType) {
mDeviceType = deviceType;
}
}
/** /**
* mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced. * mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced.
* Contains a safe volume index for a given device type. * Contains a safe volume index for a given device type.
...@@ -152,25 +145,7 @@ public class SoundDoseHelper { ...@@ -152,25 +145,7 @@ public class SoundDoseHelper {
* This level corresponds to a loudness of 85 dB SPL for the warning to be displayed when * This level corresponds to a loudness of 85 dB SPL for the warning to be displayed when
* the headset is compliant to EN 60950 with a max loudness of 100dB SPL. * the headset is compliant to EN 60950 with a max loudness of 100dB SPL.
*/ */
private final HashMap<Integer, SafeDeviceVolumeInfo> mSafeMediaVolumeDevices = private final SparseIntArray mSafeMediaVolumeDevices = new SparseIntArray();
new HashMap<>() {{
put(AudioSystem.DEVICE_OUT_WIRED_HEADSET,
new SafeDeviceVolumeInfo(AudioSystem.DEVICE_OUT_WIRED_HEADSET));
put(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE,
new SafeDeviceVolumeInfo(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE));
put(AudioSystem.DEVICE_OUT_USB_HEADSET,
new SafeDeviceVolumeInfo(AudioSystem.DEVICE_OUT_USB_HEADSET));
put(AudioSystem.DEVICE_OUT_BLE_HEADSET,
new SafeDeviceVolumeInfo(AudioSystem.DEVICE_OUT_BLE_HEADSET));
put(AudioSystem.DEVICE_OUT_BLE_BROADCAST,
new SafeDeviceVolumeInfo(AudioSystem.DEVICE_OUT_BLE_BROADCAST));
put(AudioSystem.DEVICE_OUT_HEARING_AID,
new SafeDeviceVolumeInfo(AudioSystem.DEVICE_OUT_HEARING_AID));
put(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
new SafeDeviceVolumeInfo(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES));
put(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP,
new SafeDeviceVolumeInfo(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP));
}};
// mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled.
// When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled
...@@ -291,6 +266,7 @@ public class SoundDoseHelper { ...@@ -291,6 +266,7 @@ public class SoundDoseHelper {
mEnableCsd = mContext.getResources().getBoolean(R.bool.config_audio_csd_enabled_default); mEnableCsd = mContext.getResources().getBoolean(R.bool.config_audio_csd_enabled_default);
initCsd(); initCsd();
initSafeVolumes();
mSafeMediaVolumeState = mSettings.getGlobalInt(audioService.getContentResolver(), mSafeMediaVolumeState = mSettings.getGlobalInt(audioService.getContentResolver(),
Settings.Global.AUDIO_SAFE_VOLUME_STATE, SAFE_MEDIA_VOLUME_NOT_CONFIGURED); Settings.Global.AUDIO_SAFE_VOLUME_STATE, SAFE_MEDIA_VOLUME_NOT_CONFIGURED);
...@@ -305,6 +281,25 @@ public class SoundDoseHelper { ...@@ -305,6 +281,25 @@ public class SoundDoseHelper {
Context.ALARM_SERVICE); Context.ALARM_SERVICE);
} }
void initSafeVolumes() {
mSafeMediaVolumeDevices.append(AudioSystem.DEVICE_OUT_WIRED_HEADSET,
SAFE_MEDIA_VOLUME_UNINITIALIZED);
mSafeMediaVolumeDevices.append(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE,
SAFE_MEDIA_VOLUME_UNINITIALIZED);
mSafeMediaVolumeDevices.append(AudioSystem.DEVICE_OUT_USB_HEADSET,
SAFE_MEDIA_VOLUME_UNINITIALIZED);
mSafeMediaVolumeDevices.append(AudioSystem.DEVICE_OUT_BLE_HEADSET,
SAFE_MEDIA_VOLUME_UNINITIALIZED);
mSafeMediaVolumeDevices.append(AudioSystem.DEVICE_OUT_BLE_BROADCAST,
SAFE_MEDIA_VOLUME_UNINITIALIZED);
mSafeMediaVolumeDevices.append(AudioSystem.DEVICE_OUT_HEARING_AID,
SAFE_MEDIA_VOLUME_UNINITIALIZED);
mSafeMediaVolumeDevices.append(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
SAFE_MEDIA_VOLUME_UNINITIALIZED);
mSafeMediaVolumeDevices.append(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP,
SAFE_MEDIA_VOLUME_UNINITIALIZED);
}
float getRs2Value() { float getRs2Value() {
if (!mEnableCsd) { if (!mEnableCsd) {
return 0.f; return 0.f;
...@@ -435,12 +430,12 @@ public class SoundDoseHelper { ...@@ -435,12 +430,12 @@ public class SoundDoseHelper {
} }
/*package*/ int safeMediaVolumeIndex(int device) { /*package*/ int safeMediaVolumeIndex(int device) {
final SafeDeviceVolumeInfo vi = mSafeMediaVolumeDevices.get(device); final int vol = mSafeMediaVolumeDevices.get(device);
if (vi == null) { if (vol == SAFE_MEDIA_VOLUME_UNINITIALIZED) {
return MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; return MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC];
} }
return vi.mSafeVolumeIndex; return vol;
} }
/*package*/ void restoreMusicActiveMs() { /*package*/ void restoreMusicActiveMs() {
...@@ -465,14 +460,15 @@ public class SoundDoseHelper { ...@@ -465,14 +460,15 @@ public class SoundDoseHelper {
AudioService.VolumeStreamState streamState = mAudioService.getVssVolumeForStream( AudioService.VolumeStreamState streamState = mAudioService.getVssVolumeForStream(
AudioSystem.STREAM_MUSIC); AudioSystem.STREAM_MUSIC);
for (SafeDeviceVolumeInfo vi : mSafeMediaVolumeDevices.values()) { for (int i = 0; i < mSafeMediaVolumeDevices.size(); ++i) {
int index = streamState.getIndex(vi.mDeviceType); int deviceType = mSafeMediaVolumeDevices.keyAt(i);
int safeIndex = safeMediaVolumeIndex(vi.mDeviceType); int index = streamState.getIndex(deviceType);
int safeIndex = safeMediaVolumeIndex(deviceType);
if (index > safeIndex) { if (index > safeIndex) {
streamState.setIndex(safeIndex, vi.mDeviceType, caller, streamState.setIndex(safeIndex, deviceType, caller,
true /*hasModifyAudioSettings*/); true /*hasModifyAudioSettings*/);
mAudioHandler.sendMessageAtTime( mAudioHandler.sendMessageAtTime(
mAudioHandler.obtainMessage(MSG_SET_DEVICE_VOLUME, vi.mDeviceType, mAudioHandler.obtainMessage(MSG_SET_DEVICE_VOLUME, deviceType,
/*arg2=*/0, streamState), /*delay=*/0); /*arg2=*/0, streamState), /*delay=*/0);
} }
} }
...@@ -494,7 +490,7 @@ public class SoundDoseHelper { ...@@ -494,7 +490,7 @@ public class SoundDoseHelper {
private boolean checkSafeMediaVolume_l(int streamType, int index, int device) { private boolean checkSafeMediaVolume_l(int streamType, int index, int device) {
return (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) return (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)
&& (AudioService.mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && (AudioService.mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC)
&& (mSafeMediaVolumeDevices.containsKey(device)) && safeDevicesContains(device)
&& (index > safeMediaVolumeIndex(device)); && (index > safeMediaVolumeIndex(device));
} }
...@@ -546,7 +542,7 @@ public class SoundDoseHelper { ...@@ -546,7 +542,7 @@ public class SoundDoseHelper {
synchronized (mSafeMediaVolumeStateLock) { synchronized (mSafeMediaVolumeStateLock) {
if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) {
int device = mAudioService.getDeviceForStream(AudioSystem.STREAM_MUSIC); int device = mAudioService.getDeviceForStream(AudioSystem.STREAM_MUSIC);
if (mSafeMediaVolumeDevices.containsKey(device) && isStreamActive) { if (safeDevicesContains(device) && isStreamActive) {
scheduleMusicActiveCheck(); scheduleMusicActiveCheck();
int index = mAudioService.getVssVolumeForDevice(AudioSystem.STREAM_MUSIC, int index = mAudioService.getVssVolumeForDevice(AudioSystem.STREAM_MUSIC,
device); device);
...@@ -589,14 +585,15 @@ public class SoundDoseHelper { ...@@ -589,14 +585,15 @@ public class SoundDoseHelper {
} }
/*package*/ void initSafeMediaVolumeIndex() { /*package*/ void initSafeMediaVolumeIndex() {
for (SafeDeviceVolumeInfo vi : mSafeMediaVolumeDevices.values()) { for (int i = 0; i < mSafeMediaVolumeDevices.size(); ++i) {
vi.mSafeVolumeIndex = getSafeDeviceMediaVolumeIndex(vi.mDeviceType); int deviceType = mSafeMediaVolumeDevices.keyAt(i);
mSafeMediaVolumeDevices.put(deviceType, getSafeDeviceMediaVolumeIndex(deviceType));
} }
} }
/*package*/ int getSafeMediaVolumeIndex(int device) { /*package*/ int getSafeMediaVolumeIndex(int device) {
if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE
&& mSafeMediaVolumeDevices.containsKey(device)) { && safeDevicesContains(device)) {
return safeMediaVolumeIndex(device); return safeMediaVolumeIndex(device);
} else { } else {
return -1; return -1;
...@@ -614,7 +611,7 @@ public class SoundDoseHelper { ...@@ -614,7 +611,7 @@ public class SoundDoseHelper {
} }
/*package*/ boolean safeDevicesContains(int device) { /*package*/ boolean safeDevicesContains(int device) {
return mSafeMediaVolumeDevices.containsKey(device); return mSafeMediaVolumeDevices.indexOfKey(device) >= 0;
} }
/*package*/ void invalidatPendingVolumeCommand() { /*package*/ void invalidatPendingVolumeCommand() {
...@@ -665,9 +662,9 @@ public class SoundDoseHelper { ...@@ -665,9 +662,9 @@ public class SoundDoseHelper {
pw.print(" mSafeMediaVolumeState="); pw.print(" mSafeMediaVolumeState=");
pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState));
pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex);
for (SafeDeviceVolumeInfo vi : mSafeMediaVolumeDevices.values()) { for (int i = 0; i < mSafeMediaVolumeDevices.size(); ++i) {
pw.print(" mSafeMediaVolumeIndex["); pw.print(vi.mDeviceType); pw.print(" mSafeMediaVolumeIndex["); pw.print(mSafeMediaVolumeDevices.keyAt(i));
pw.print("]="); pw.println(vi.mSafeVolumeIndex); pw.print("]="); pw.println(mSafeMediaVolumeDevices.valueAt(i));
} }
pw.print(" mSafeMediaVolumeDbfs="); pw.println(mSafeMediaVolumeDbfs); pw.print(" mSafeMediaVolumeDbfs="); pw.println(mSafeMediaVolumeDbfs);
pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs);
...@@ -721,7 +718,7 @@ public class SoundDoseHelper { ...@@ -721,7 +718,7 @@ public class SoundDoseHelper {
} }
if (AudioService.mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC if (AudioService.mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC
&& mSafeMediaVolumeDevices.containsKey(device)) { && safeDevicesContains(device)) {
soundDose.updateAttenuation( soundDose.updateAttenuation(
AudioSystem.getStreamVolumeDB(AudioSystem.STREAM_MUSIC, AudioSystem.getStreamVolumeDB(AudioSystem.STREAM_MUSIC,
(newIndex + 5) / 10, (newIndex + 5) / 10,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment