Permalink
Browse files

Framework: safe headset volume option (1 of 2)

thanks DvTonder

Change-Id: I5355820a24d48c1d8f54ea17a7022d891ad07857
  • Loading branch information...
kufikugel authored and akhilnarang committed Nov 8, 2013
1 parent c25283d commit 93790c0203e70f7060cca859774aabd58b0d18c0
@@ -3811,6 +3811,12 @@ public boolean validate(String value) {
*/
public static final String DISABLE_SUGGESTIONS = "disable_suggestions";
/**
* Whether to prevent loud volume levels when headset is first plugged in.
* @hide
*/
public static final String SAFE_HEADSET_VOLUME = "safe_headset_volume";
/**
* Whether to show the battery info on the lockscreen while charging
* @hide
@@ -53,6 +53,7 @@
import android.hardware.hdmi.HdmiPlaybackClient;
import android.hardware.hdmi.HdmiTvClient;
import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.media.AudioAttributes;
import android.media.AudioDevicePort;
import android.media.AudioSystem;
@@ -1226,7 +1227,6 @@ private void readDockAudioSettings(ContentResolver cr)
0);
}
private void updateMasterMono(ContentResolver cr)
{
final boolean masterMono = System.getIntForUser(
@@ -1275,6 +1275,15 @@ private void sendEncodedSurroundMode(int encodedSurroundMode)
}
}
private boolean safeVolumeEnabled(ContentResolver cr) {
boolean safeMediaVolumeEnabled = mContext.getResources().getBoolean(
com.android.internal.R.bool.config_safe_media_volume_enabled);
boolean safeHeadsetVolumeEnabled = Settings.System.getIntForUser(cr,
Settings.System.SAFE_HEADSET_VOLUME, safeMediaVolumeEnabled ? 1 : 0,
UserHandle.USER_CURRENT_OR_SELF) != 0;
return safeHeadsetVolumeEnabled;
}
private void readPersistedSettings() {
final ContentResolver cr = mContentResolver;
@@ -1317,6 +1326,7 @@ private void readPersistedSettings() {
updateRingerModeAffectedStreams();
readDockAudioSettings(cr);
sendEncodedSurroundMode(cr);
mSafeVolumeEnabled = new Boolean(safeVolumeEnabled(cr));
}
mMuteAffectedStreams = System.getIntForUser(cr,
@@ -2873,7 +2883,8 @@ private void readAudioSettings(boolean userSwitch) {
mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver,
Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT),
0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX);
if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) {
if (mSafeVolumeEnabled &&
mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) {
enforceSafeMediaVolume(TAG);
}
}
@@ -3547,7 +3558,8 @@ void disconnectHeadset() {
private void onCheckMusicActive(String caller) {
synchronized (mSafeMediaVolumeState) {
if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) {
if (mSafeVolumeEnabled &&
mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) {
int device = getDeviceForStream(AudioSystem.STREAM_MUSIC);
if ((device & mSafeMediaVolumeDevices) != 0) {
@@ -5044,31 +5056,42 @@ public void handleMessage(Message msg) {
mContentResolver.registerContentObserver(Settings.Global.getUriFor(
Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this);
mContentResolver.registerContentObserver(Settings.System.getUriFor(
Settings.System.MASTER_MONO), false, this);
Settings.System.MASTER_MONO), false, this);
mEncodedSurroundMode = Settings.Global.getInt(
mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT,
Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO);
mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT,
Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO);
mContentResolver.registerContentObserver(Settings.Global.getUriFor(
Settings.Global.ENCODED_SURROUND_OUTPUT), false, this);
Settings.Global.ENCODED_SURROUND_OUTPUT), false, this);
mContentResolver.registerContentObserver(Settings.System.getUriFor(
Settings.System.VOLUME_LINK_NOTIFICATION), false, this);
Settings.System.VOLUME_LINK_NOTIFICATION), false, this);
mContentResolver.registerContentObserver(Settings.System.getUriFor(
Settings.System.SAFE_HEADSET_VOLUME), false, this,
UserHandle.USER_ALL);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
// FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode.
// However there appear to be some missing locks around mRingerModeMutedStreams
// and mRingerModeAffectedStreams, so will leave this synchronized for now.
// mRingerModeMutedStreams and mMuteAffectedStreams are safe (only accessed once).
synchronized (mSettingsLock) {
if (updateRingerModeAffectedStreams()) {
/*
* Ensure all stream types that should be affected by ringer mode
* are in the proper state.
*/
setRingerModeInt(getRingerModeInternal(), false);
if (uri.equals(Settings.System.getUriFor(
Settings.System.SAFE_HEADSET_VOLUME))) {
mSafeVolumeEnabled = safeVolumeEnabled(mContentResolver);
} else if (uri.equals(Settings.System.getUriFor(
Settings.System.MODE_RINGER_STREAMS_AFFECTED))) {
if (updateRingerModeAffectedStreams()) {
/*
* Ensure all stream types that should be affected by ringer mode
* are in the proper state.
*/
setRingerModeInt(getRingerModeInternal(), false);
}
} else if (uri.equals(Settings.Global.getUriFor(
Settings.Global.DOCK_AUDIO_MEDIA_ENABLED))) {
readDockAudioSettings(mContentResolver);
}
readDockAudioSettings(mContentResolver);
updateMasterMono(mContentResolver);
@@ -6030,6 +6053,8 @@ public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) {
private Integer mSafeMediaVolumeState;
private int mMcc = 0;
// mSafeVolumeEnabled indicates whether to check the volume of media play via headset
private boolean mSafeVolumeEnabled;
// mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property
private int mSafeMediaVolumeIndex;
// mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced,
@@ -6045,7 +6070,8 @@ public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) {
private void setSafeMediaVolumeEnabled(boolean on, String caller) {
synchronized (mSafeMediaVolumeState) {
if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) &&
if (mSafeVolumeEnabled &&
(mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) &&
(mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) {
if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) {
mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE;
@@ -6093,7 +6119,8 @@ private void enforceSafeMediaVolume(String caller) {
private boolean checkSafeMediaVolume(int streamType, int index, int device) {
synchronized (mSafeMediaVolumeState) {
if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) &&
if (mSafeVolumeEnabled &&
(mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) &&
(mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) &&
((device & mSafeMediaVolumeDevices) != 0) &&
(index > mSafeMediaVolumeIndex)) {

0 comments on commit 93790c0

Please sign in to comment.