audioservice: fix camera sound control for dual SIMs

Make sure that all MCC/MNC corresponding to active SIMs are taken
into accound whne deciding if camera sound must be enforced according to
country regulations.

Test: make
Bug: 254636778
Change-Id: If24f064c8ea3470d82e1d2ae1411a0c0f5c6aea5
This commit is contained in:
Eric Laurent
2023-03-17 17:41:42 +01:00
parent 21f7408abe
commit 26292ae3d2

View File

@@ -170,6 +170,7 @@ import android.provider.Settings;
import android.provider.Settings.System;
import android.service.notification.ZenModeConfig;
import android.telecom.TelecomManager;
import android.telephony.SubscriptionManager;
import android.text.TextUtils;
import android.util.AndroidRuntimeException;
import android.util.ArrayMap;
@@ -184,6 +185,7 @@ import android.view.KeyEvent;
import android.view.accessibility.AccessibilityManager;
import android.widget.Toast;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.config.sysui.SystemUiDeviceConfigFlags;
@@ -393,6 +395,7 @@ public class AudioService extends IAudioService.Stub
private static final int MSG_NO_LOG_FOR_PLAYER_I = 51;
private static final int MSG_DISPATCH_PREFERRED_MIXER_ATTRIBUTES = 52;
private static final int MSG_LOWER_VOLUME_TO_RS1 = 53;
private static final int MSG_CONFIGURATION_CHANGED = 54;
/** Messages handled by the {@link SoundDoseHelper}. */
/*package*/ static final int SAFE_MEDIA_VOLUME_MSG_START = 1000;
@@ -1214,17 +1217,6 @@ public class AudioService extends IAudioService.Stub
updateAudioHalPids();
boolean cameraSoundForced = readCameraSoundForced();
mCameraSoundForced = new Boolean(cameraSoundForced);
sendMsg(mAudioHandler,
MSG_SET_FORCE_USE,
SENDMSG_QUEUE,
AudioSystem.FOR_SYSTEM,
cameraSoundForced ?
AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE,
new String("AudioService ctor"),
0);
mUseFixedVolume = mContext.getResources().getBoolean(
com.android.internal.R.bool.config_useFixedVolume);
@@ -1309,6 +1301,18 @@ public class AudioService extends IAudioService.Stub
* Called by handling of MSG_INIT_STREAMS_VOLUMES
*/
private void onInitStreamsAndVolumes() {
synchronized (mSettingsLock) {
mCameraSoundForced = readCameraSoundForced();
sendMsg(mAudioHandler,
MSG_SET_FORCE_USE,
SENDMSG_QUEUE,
AudioSystem.FOR_SYSTEM,
mCameraSoundForced
? AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE,
new String("AudioService ctor"),
0);
}
createStreamStates();
// must be called after createStreamStates() as it uses MUSIC volume as default if no
@@ -1344,8 +1348,19 @@ public class AudioService extends IAudioService.Stub
// check on volume initialization
checkVolumeRangeInitialization("AudioService()");
}
private SubscriptionManager.OnSubscriptionsChangedListener mSubscriptionChangedListener =
new SubscriptionManager.OnSubscriptionsChangedListener() {
@Override
public void onSubscriptionsChanged() {
Log.i(TAG, "onSubscriptionsChanged()");
sendMsg(mAudioHandler, MSG_CONFIGURATION_CHANGED, SENDMSG_REPLACE,
0, 0, null, 0);
}
};
/**
* Initialize intent receives and settings observers for this service.
* Must be called after createStreamStates() as the handling of some events
@@ -1383,6 +1398,13 @@ public class AudioService extends IAudioService.Stub
mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, intentFilter, null, null,
Context.RECEIVER_EXPORTED);
SubscriptionManager subscriptionManager = mContext.getSystemService(
SubscriptionManager.class);
if (subscriptionManager == null) {
Log.e(TAG, "initExternalEventReceivers cannot create SubscriptionManager!");
} else {
subscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionChangedListener);
}
}
public void systemReady() {
@@ -3660,7 +3682,7 @@ public class AudioService extends IAudioService.Stub
for (int stream = 0; stream < mStreamStates.length; stream++) {
VolumeStreamState vss = mStreamStates[stream];
if (streamAlias == mStreamVolumeAlias[stream] && vss.isMutable()) {
if (!(readCameraSoundForced()
if (!(mCameraSoundForced
&& (vss.getStreamType()
== AudioSystem.STREAM_SYSTEM_ENFORCED))) {
boolean changed = vss.mute(state, /* apply= */ false);
@@ -9232,6 +9254,10 @@ public class AudioService extends IAudioService.Stub
onLowerVolumeToRs1();
break;
case MSG_CONFIGURATION_CHANGED:
onConfigurationChanged();
break;
default:
if (msg.what >= SAFE_MEDIA_VOLUME_MSG_START) {
// msg could be for the SoundDoseHelper
@@ -9414,7 +9440,12 @@ public class AudioService extends IAudioService.Stub
}
AudioSystem.setParameters("screen_state=off");
} else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
handleConfigurationChanged(context);
sendMsg(mAudioHandler,
MSG_CONFIGURATION_CHANGED,
SENDMSG_REPLACE,
0,
0,
null, 0);
} else if (action.equals(Intent.ACTION_USER_SWITCHED)) {
if (mUserSwitchedReceived) {
// attempt to stop music playback for background user except on first user
@@ -10156,10 +10187,30 @@ public class AudioService extends IAudioService.Stub
}
//==========================================================================================
// camera sound is forced if any of the resources corresponding to one active SIM
// demands it.
private boolean readCameraSoundForced() {
return SystemProperties.getBoolean("audio.camerasound.force", false) ||
mContext.getResources().getBoolean(
com.android.internal.R.bool.config_camera_sound_forced);
if (SystemProperties.getBoolean("audio.camerasound.force", false)
|| mContext.getResources().getBoolean(
com.android.internal.R.bool.config_camera_sound_forced)) {
return true;
}
SubscriptionManager subscriptionManager = mContext.getSystemService(
SubscriptionManager.class);
if (subscriptionManager == null) {
Log.e(TAG, "readCameraSoundForced cannot create SubscriptionManager!");
return false;
}
int[] subscriptionIds = subscriptionManager.getActiveSubscriptionIdList(false);
for (int subId : subscriptionIds) {
if (SubscriptionManager.getResourcesForSubId(mContext, subId).getBoolean(
com.android.internal.R.bool.config_camera_sound_forced)) {
return true;
}
}
return false;
}
//==========================================================================================
@@ -10370,11 +10421,11 @@ public class AudioService extends IAudioService.Stub
* Monitoring rotation is optional, and is defined by the definition and value
* of the "ro.audio.monitorRotation" system property.
*/
private void handleConfigurationChanged(Context context) {
private void onConfigurationChanged() {
try {
// reading new configuration "safely" (i.e. under try catch) in case anything
// goes wrong.
Configuration config = context.getResources().getConfiguration();
Configuration config = mContext.getResources().getConfiguration();
mSoundDoseHelper.configureSafeMedia(/*forced*/false, TAG);
boolean cameraSoundForced = readCameraSoundForced();
@@ -10401,7 +10452,7 @@ public class AudioService extends IAudioService.Stub
mDeviceBroker.setForceUse_Async(AudioSystem.FOR_SYSTEM,
cameraSoundForced ?
AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE,
"handleConfigurationChanged");
"onConfigurationChanged");
sendMsg(mAudioHandler,
MSG_SET_ALL_VOLUMES,
SENDMSG_QUEUE,