audioservice: special mute behavior for BT SCO volume

Allow muting BT SCO volume only from explicit mute request and not
when stream volume index is set to 0. This is to match BT requirement
that the valid SCO volume range is 0 to 15 and let the headset deal with
actual attenuation for index 0.
If explicitly muted via adjustStreamVolume() from an app with permission
MODIFY_PHONE_STATE, then we set the audio policy stream volume to 0 and
effectively mute RX path.

Also change default value of Settings.System.MUTE_STREAMS_AFFECTED
to include AudioManager.STREAM_BLUETOOTH_SCO.

Bug: 111195322
Test: test volume changes in call over BT SCO
Change-Id: I3b6560bbd1bcbb01a29aa746f23bae74329a1fa7
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
index d07bc32..5cd29b7 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -3238,7 +3238,7 @@
         }
 
         private final class UpgradeController {
-            private static final int SETTINGS_VERSION = 181;
+            private static final int SETTINGS_VERSION = 182;
 
             private final int mUserId;
 
@@ -4421,6 +4421,37 @@
                     currentVersion = 181;
                 }
 
+                if (currentVersion == 181) {
+                    // Version cd : by default, add STREAM_BLUETOOTH_SCO to list of streams that can
+                    // be muted.
+                    final SettingsState systemSettings = getSystemSettingsLocked(userId);
+                    final Setting currentSetting = systemSettings.getSettingLocked(
+                              Settings.System.MUTE_STREAMS_AFFECTED);
+                    if (!currentSetting.isNull()) {
+                        try {
+                            int currentSettingIntegerValue = Integer.parseInt(
+                                    currentSetting.getValue());
+                            if ((currentSettingIntegerValue
+                                    & (1 << AudioManager.STREAM_BLUETOOTH_SCO)) == 0) {
+                                systemSettings.insertSettingLocked(
+                                        Settings.System.MUTE_STREAMS_AFFECTED,
+                                        Integer.toString(
+                                        currentSettingIntegerValue
+                                        | (1 << AudioManager.STREAM_BLUETOOTH_SCO)),
+                                        null, true, SettingsState.SYSTEM_PACKAGE_NAME);
+                            }
+                        } catch (NumberFormatException e) {
+                            // remove the setting in case it is not a valid integer
+                            Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED"
+                                    + "setting, removing setting", e);
+                            systemSettings.deleteSettingLocked(
+                                    Settings.System.MUTE_STREAMS_AFFECTED);
+                        }
+
+                    }
+                    currentVersion = 182;
+                }
+
                 // vXXX: Add new settings above this point.
 
                 if (currentVersion != newVersion) {