Merge "Fix Cell broadcast sound in total silence" into oc-mr1-dev

This commit is contained in:
Eric Laurent
2017-10-04 14:45:01 +00:00
committed by Android (Google) Code Review
3 changed files with 58 additions and 13 deletions

View File

@@ -127,8 +127,9 @@ public abstract class PlayerBase {
Log.e(TAG, "Error talking to audio service, STARTED state will not be tracked", e); Log.e(TAG, "Error talking to audio service, STARTED state will not be tracked", e);
} }
synchronized (mLock) { synchronized (mLock) {
boolean attributesChanged = (mAttributes != attr);
mAttributes = attr; mAttributes = attr;
updateAppOpsPlayAudio_sync(); updateAppOpsPlayAudio_sync(attributesChanged);
} }
} }
@@ -200,16 +201,13 @@ public abstract class PlayerBase {
} }
void baseSetVolume(float leftVolume, float rightVolume) { void baseSetVolume(float leftVolume, float rightVolume) {
final boolean hasAppOpsPlayAudio; final boolean isRestricted;
synchronized (mLock) { synchronized (mLock) {
mLeftVolume = leftVolume; mLeftVolume = leftVolume;
mRightVolume = rightVolume; mRightVolume = rightVolume;
hasAppOpsPlayAudio = mHasAppOpsPlayAudio; isRestricted = isRestricted_sync();
if (isRestricted_sync()) {
return;
}
} }
playerSetVolume(!hasAppOpsPlayAudio/*muting*/, playerSetVolume(isRestricted/*muting*/,
leftVolume * mPanMultiplierL, rightVolume * mPanMultiplierR); leftVolume * mPanMultiplierL, rightVolume * mPanMultiplierR);
} }
@@ -250,7 +248,7 @@ public abstract class PlayerBase {
private void updateAppOpsPlayAudio() { private void updateAppOpsPlayAudio() {
synchronized (mLock) { synchronized (mLock) {
updateAppOpsPlayAudio_sync(); updateAppOpsPlayAudio_sync(false);
} }
} }
@@ -258,7 +256,7 @@ public abstract class PlayerBase {
* To be called whenever a condition that might affect audibility of this player is updated. * To be called whenever a condition that might affect audibility of this player is updated.
* Must be called synchronized on mLock. * Must be called synchronized on mLock.
*/ */
void updateAppOpsPlayAudio_sync() { void updateAppOpsPlayAudio_sync(boolean attributesChanged) {
boolean oldHasAppOpsPlayAudio = mHasAppOpsPlayAudio; boolean oldHasAppOpsPlayAudio = mHasAppOpsPlayAudio;
try { try {
int mode = AppOpsManager.MODE_IGNORED; int mode = AppOpsManager.MODE_IGNORED;
@@ -275,9 +273,10 @@ public abstract class PlayerBase {
// AppsOps alters a player's volume; when the restriction changes, reflect it on the actual // AppsOps alters a player's volume; when the restriction changes, reflect it on the actual
// volume used by the player // volume used by the player
try { try {
if (oldHasAppOpsPlayAudio != mHasAppOpsPlayAudio) { if (oldHasAppOpsPlayAudio != mHasAppOpsPlayAudio ||
attributesChanged) {
getService().playerHasOpPlayAudio(mPlayerIId, mHasAppOpsPlayAudio); getService().playerHasOpPlayAudio(mPlayerIId, mHasAppOpsPlayAudio);
if (mHasAppOpsPlayAudio) { if (!isRestricted_sync()) {
if (DEBUG_APP_OPS) { if (DEBUG_APP_OPS) {
Log.v(TAG, "updateAppOpsPlayAudio: unmuting player, vol=" + mLeftVolume Log.v(TAG, "updateAppOpsPlayAudio: unmuting player, vol=" + mLeftVolume
+ "/" + mRightVolume); + "/" + mRightVolume);

View File

@@ -751,6 +751,9 @@ public class AudioService extends IAudioService.Stub
// relies on audio policy having correct ranges for volume indexes. // relies on audio policy having correct ranges for volume indexes.
mSafeUsbMediaVolumeIndex = getSafeUsbMediaVolumeIndex(); mSafeUsbMediaVolumeIndex = getSafeUsbMediaVolumeIndex();
mPlaybackMonitor =
new PlaybackActivityMonitor(context, MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]);
mMediaFocusControl = new MediaFocusControl(mContext, mPlaybackMonitor); mMediaFocusControl = new MediaFocusControl(mContext, mPlaybackMonitor);
mRecordMonitor = new RecordingActivityMonitor(mContext); mRecordMonitor = new RecordingActivityMonitor(mContext);
@@ -6977,7 +6980,7 @@ public class AudioService extends IAudioService.Stub
//====================== //======================
// Audio playback notification // Audio playback notification
//====================== //======================
private final PlaybackActivityMonitor mPlaybackMonitor = new PlaybackActivityMonitor(); private final PlaybackActivityMonitor mPlaybackMonitor;
public void registerPlaybackCallback(IPlaybackConfigDispatcher pcdb) { public void registerPlaybackCallback(IPlaybackConfigDispatcher pcdb) {
final boolean isPrivileged = final boolean isPrivileged =

View File

@@ -17,6 +17,8 @@
package com.android.server.audio; package com.android.server.audio;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.AudioAttributes; import android.media.AudioAttributes;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.AudioPlaybackConfiguration; import android.media.AudioPlaybackConfiguration;
@@ -90,7 +92,14 @@ public final class PlaybackActivityMonitor
private final HashMap<Integer, AudioPlaybackConfiguration> mPlayers = private final HashMap<Integer, AudioPlaybackConfiguration> mPlayers =
new HashMap<Integer, AudioPlaybackConfiguration>(); new HashMap<Integer, AudioPlaybackConfiguration>();
PlaybackActivityMonitor() { private final Context mContext;
private int mSavedAlarmVolume = -1;
private final int mMaxAlarmVolume;
private int mPrivilegedAlarmActiveCount = 0;
PlaybackActivityMonitor(Context context, int maxAlarmVolume) {
mContext = context;
mMaxAlarmVolume = maxAlarmVolume;
PlayMonitorClient.sListenerDeathMonitor = this; PlayMonitorClient.sListenerDeathMonitor = this;
AudioPlaybackConfiguration.sPlayerDeathMonitor = this; AudioPlaybackConfiguration.sPlayerDeathMonitor = this;
} }
@@ -175,6 +184,38 @@ public final class PlaybackActivityMonitor
} }
} }
private void checkVolumeForPrivilegedAlarm(AudioPlaybackConfiguration apc, int event) {
if (event == AudioPlaybackConfiguration.PLAYER_STATE_STARTED ||
apc.getPlayerState() == AudioPlaybackConfiguration.PLAYER_STATE_STARTED) {
if ((apc.getAudioAttributes().getAllFlags() &
AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY) != 0 &&
apc.getAudioAttributes().getUsage() == AudioAttributes.USAGE_ALARM &&
mContext.checkPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
apc.getClientPid(), apc.getClientUid()) ==
PackageManager.PERMISSION_GRANTED) {
if (event == AudioPlaybackConfiguration.PLAYER_STATE_STARTED &&
apc.getPlayerState() != AudioPlaybackConfiguration.PLAYER_STATE_STARTED) {
if (mPrivilegedAlarmActiveCount++ == 0) {
mSavedAlarmVolume = AudioSystem.getStreamVolumeIndex(
AudioSystem.STREAM_ALARM, AudioSystem.DEVICE_OUT_SPEAKER);
AudioSystem.setStreamVolumeIndex(AudioSystem.STREAM_ALARM,
mMaxAlarmVolume, AudioSystem.DEVICE_OUT_SPEAKER);
}
} else if (event != AudioPlaybackConfiguration.PLAYER_STATE_STARTED &&
apc.getPlayerState() == AudioPlaybackConfiguration.PLAYER_STATE_STARTED) {
if (--mPrivilegedAlarmActiveCount == 0) {
if (AudioSystem.getStreamVolumeIndex(
AudioSystem.STREAM_ALARM, AudioSystem.DEVICE_OUT_SPEAKER) ==
mMaxAlarmVolume) {
AudioSystem.setStreamVolumeIndex(AudioSystem.STREAM_ALARM,
mSavedAlarmVolume, AudioSystem.DEVICE_OUT_SPEAKER);
}
}
}
}
}
}
public void playerEvent(int piid, int event, int binderUid) { public void playerEvent(int piid, int event, int binderUid) {
if (DEBUG) { Log.v(TAG, String.format("playerEvent(piid=%d, event=%d)", piid, event)); } if (DEBUG) { Log.v(TAG, String.format("playerEvent(piid=%d, event=%d)", piid, event)); }
final boolean change; final boolean change;
@@ -200,6 +241,7 @@ public final class PlaybackActivityMonitor
} }
if (checkConfigurationCaller(piid, apc, binderUid)) { if (checkConfigurationCaller(piid, apc, binderUid)) {
//TODO add generation counter to only update to the latest state //TODO add generation counter to only update to the latest state
checkVolumeForPrivilegedAlarm(apc, event);
change = apc.handleStateEvent(event); change = apc.handleStateEvent(event);
} else { } else {
Log.e(TAG, "Error handling event " + event); Log.e(TAG, "Error handling event " + event);
@@ -228,6 +270,7 @@ public final class PlaybackActivityMonitor
"releasing player piid:" + piid)); "releasing player piid:" + piid));
mPlayers.remove(new Integer(piid)); mPlayers.remove(new Integer(piid));
mDuckingManager.removeReleased(apc); mDuckingManager.removeReleased(apc);
checkVolumeForPrivilegedAlarm(apc, AudioPlaybackConfiguration.PLAYER_STATE_RELEASED);
apc.handleStateEvent(AudioPlaybackConfiguration.PLAYER_STATE_RELEASED); apc.handleStateEvent(AudioPlaybackConfiguration.PLAYER_STATE_RELEASED);
} }
} }