diff options
Diffstat (limited to 'services/core/java/com/android/server/audio/AudioDeviceInventory.java')
-rw-r--r-- | services/core/java/com/android/server/audio/AudioDeviceInventory.java | 154 |
1 files changed, 12 insertions, 142 deletions
diff --git a/services/core/java/com/android/server/audio/AudioDeviceInventory.java b/services/core/java/com/android/server/audio/AudioDeviceInventory.java index 9524c54acc36..dbe4fb8c8795 100644 --- a/services/core/java/com/android/server/audio/AudioDeviceInventory.java +++ b/services/core/java/com/android/server/audio/AudioDeviceInventory.java @@ -15,8 +15,6 @@ */ package com.android.server.audio; -import static android.media.AudioSystem.isBluetoothDevice; - import android.annotation.NonNull; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; @@ -40,7 +38,6 @@ import android.text.TextUtils; import android.util.ArrayMap; import android.util.ArraySet; import android.util.Log; -import android.util.Pair; import android.util.Slog; import com.android.internal.annotations.GuardedBy; @@ -48,9 +45,7 @@ import com.android.internal.annotations.VisibleForTesting; import java.io.PrintWriter; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Objects; @@ -66,81 +61,12 @@ public class AudioDeviceInventory { private static final String TAG = "AS.AudioDeviceInventory"; - private static final String SETTING_DEVICE_SEPARATOR_CHAR = "|"; - private static final String SETTING_DEVICE_SEPARATOR = "\\|"; - // lock to synchronize all access to mConnectedDevices and mApmConnectedDevices private final Object mDevicesLock = new Object(); //Audio Analytics ids. private static final String mMetricsId = "audio.device."; - private final Object mDeviceInventoryLock = new Object(); - @GuardedBy("mDeviceInventoryLock") - private final HashMap<Pair<Integer, String>, AdiDeviceState> mDeviceInventory = new HashMap<>(); - - List<AdiDeviceState> getImmutableDeviceInventory() { - synchronized (mDeviceInventoryLock) { - return new ArrayList<AdiDeviceState>(mDeviceInventory.values()); - } - } - - void addDeviceStateToInventory(AdiDeviceState deviceState) { - synchronized (mDeviceInventoryLock) { - mDeviceInventory.put(deviceState.getDeviceId(), deviceState); - } - } - - /** - * Adds a new entry in mDeviceInventory if the AudioDeviceAttributes passed is an sink - * Bluetooth device and no corresponding entry already exists. - * @param ada the device to add if needed - */ - void addAudioDeviceInInventoryIfNeeded(AudioDeviceAttributes ada) { - if (!AudioSystem.isBluetoothOutDevice(ada.getInternalType())) { - return; - } - synchronized (mDeviceInventoryLock) { - if (findDeviceStateForAudioDeviceAttributes(ada, ada.getType()) != null) { - return; - } - AdiDeviceState ads = new AdiDeviceState( - ada.getType(), ada.getInternalType(), ada.getAddress()); - mDeviceInventory.put(ads.getDeviceId(), ads); - } - mDeviceBroker.persistAudioDeviceSettings(); - } - - /** - * Finds the device state that matches the passed {@link AudioDeviceAttributes} and device - * type. Note: currently this method only returns a valid device for A2DP and BLE devices. - * - * @param ada attributes of device to match - * @param canonicalDeviceType external device type to match - * @return the found {@link AdiDeviceState} matching a cached A2DP or BLE device or - * {@code null} otherwise. - */ - AdiDeviceState findDeviceStateForAudioDeviceAttributes(AudioDeviceAttributes ada, - int canonicalDeviceType) { - final boolean isWireless = isBluetoothDevice(ada.getInternalType()); - synchronized (mDeviceInventoryLock) { - for (AdiDeviceState deviceState : mDeviceInventory.values()) { - if (deviceState.getDeviceType() == canonicalDeviceType - && (!isWireless || ada.getAddress().equals( - deviceState.getDeviceAddress()))) { - return deviceState; - } - } - } - return null; - } - - void clearDeviceInventory() { - synchronized (mDeviceInventoryLock) { - mDeviceInventory.clear(); - } - } - // List of connected devices // Key for map created from DeviceInfo.makeDeviceListKey() @GuardedBy("mDevicesLock") @@ -336,12 +262,6 @@ public class AudioDeviceInventory { mPreferredDevicesForCapturePreset.forEach((capturePreset, devices) -> { pw.println(" " + prefix + "capturePreset:" + capturePreset + " devices:" + devices); }); - pw.println("\ndevices:\n"); - synchronized (mDeviceInventoryLock) { - for (AdiDeviceState device : mDeviceInventory.values()) { - pw.println("\t" + device + "\n"); - } - } } //------------------------------------------------------------ @@ -725,8 +645,8 @@ public class AudioDeviceInventory { } /*package*/ void registerStrategyPreferredDevicesDispatcher( - @NonNull IStrategyPreferredDevicesDispatcher dispatcher, boolean isPrivileged) { - mPrefDevDispatchers.register(dispatcher, isPrivileged); + @NonNull IStrategyPreferredDevicesDispatcher dispatcher) { + mPrefDevDispatchers.register(dispatcher); } /*package*/ void unregisterStrategyPreferredDevicesDispatcher( @@ -760,8 +680,8 @@ public class AudioDeviceInventory { } /*package*/ void registerCapturePresetDevicesRoleDispatcher( - @NonNull ICapturePresetDevicesRoleDispatcher dispatcher, boolean isPrivileged) { - mDevRoleCapturePresetDispatchers.register(dispatcher, isPrivileged); + @NonNull ICapturePresetDevicesRoleDispatcher dispatcher) { + mDevRoleCapturePresetDispatchers.register(dispatcher); } /*package*/ void unregisterCapturePresetDevicesRoleDispatcher( @@ -839,9 +759,6 @@ public class AudioDeviceInventory { mConnectedDevices.put(deviceKey, new DeviceInfo( device, deviceName, address, AudioSystem.AUDIO_FORMAT_DEFAULT)); mDeviceBroker.postAccessoryPlugMediaUnmute(device); - if (AudioSystem.isBluetoothScoDevice(device)) { - addAudioDeviceInInventoryIfNeeded(attributes); - } mmi.set(MediaMetrics.Property.STATE, MediaMetrics.Value.CONNECTED).record(); return true; } else if (!connect && isConnected) { @@ -1071,9 +988,8 @@ public class AudioDeviceInventory { mDeviceBroker.setBluetoothA2dpOnInt(true, true /*fromA2dp*/, eventSource); // at this point there could be another A2DP device already connected in APM, but it // doesn't matter as this new one will overwrite the previous one - AudioDeviceAttributes ada = new AudioDeviceAttributes( - AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address, name); - final int res = mAudioSystem.setDeviceConnectionState(ada, + final int res = mAudioSystem.setDeviceConnectionState(new AudioDeviceAttributes( + AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address, name), AudioSystem.DEVICE_STATE_AVAILABLE, a2dpCodec); // TODO: log in MediaMetrics once distinction between connection failure and @@ -1095,7 +1011,8 @@ public class AudioDeviceInventory { // The convention for head tracking sensors associated with A2DP devices is to // use a UUID derived from the MAC address as follows: // time_low = 0, time_mid = 0, time_hi = 0, clock_seq = 0, node = MAC Address - UUID sensorUuid = UuidUtils.uuidFromAudioDeviceAttributes(ada); + UUID sensorUuid = UuidUtils.uuidFromAudioDeviceAttributes( + new AudioDeviceAttributes(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); final DeviceInfo di = new DeviceInfo(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, name, address, a2dpCodec, sensorUuid); final String diKey = di.getKey(); @@ -1106,10 +1023,8 @@ public class AudioDeviceInventory { mDeviceBroker.postAccessoryPlugMediaUnmute(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP); setCurrentAudioRouteNameIfPossible(name, true /*fromA2dp*/); - addAudioDeviceInInventoryIfNeeded(ada); } - @GuardedBy("mDevicesLock") private void makeA2dpDeviceUnavailableNow(String address, int a2dpCodec) { MediaMetrics.Item mmi = new MediaMetrics.Item(mMetricsId + "a2dp." + address) @@ -1203,9 +1118,9 @@ public class AudioDeviceInventory { final int hearingAidVolIndex = mDeviceBroker.getVssVolumeForDevice(streamType, AudioSystem.DEVICE_OUT_HEARING_AID); mDeviceBroker.postSetHearingAidVolumeIndex(hearingAidVolIndex, streamType); - AudioDeviceAttributes ada = new AudioDeviceAttributes( - AudioSystem.DEVICE_OUT_HEARING_AID, address, name); - mAudioSystem.setDeviceConnectionState(ada, + + mAudioSystem.setDeviceConnectionState(new AudioDeviceAttributes( + AudioSystem.DEVICE_OUT_HEARING_AID, address, name), AudioSystem.DEVICE_STATE_AVAILABLE, AudioSystem.AUDIO_FORMAT_DEFAULT); mConnectedDevices.put( @@ -1216,7 +1131,6 @@ public class AudioDeviceInventory { mDeviceBroker.postApplyVolumeOnDevice(streamType, AudioSystem.DEVICE_OUT_HEARING_AID, "makeHearingAidDeviceAvailable"); setCurrentAudioRouteNameIfPossible(name, false /*fromA2dp*/); - addAudioDeviceInInventoryIfNeeded(ada); new MediaMetrics.Item(mMetricsId + "makeHearingAidDeviceAvailable") .set(MediaMetrics.Property.ADDRESS, address != null ? address : "") .set(MediaMetrics.Property.DEVICE, @@ -1253,15 +1167,13 @@ public class AudioDeviceInventory { */ mDeviceBroker.setBluetoothA2dpOnInt(true, false /*fromA2dp*/, eventSource); - AudioDeviceAttributes ada = new AudioDeviceAttributes(device, address, name); - AudioSystem.setDeviceConnectionState(ada, + AudioSystem.setDeviceConnectionState(new AudioDeviceAttributes(device, address, name), AudioSystem.DEVICE_STATE_AVAILABLE, AudioSystem.AUDIO_FORMAT_DEFAULT); mConnectedDevices.put(DeviceInfo.makeDeviceListKey(device, address), new DeviceInfo(device, name, address, AudioSystem.AUDIO_FORMAT_DEFAULT)); mDeviceBroker.postAccessoryPlugMediaUnmute(device); setCurrentAudioRouteNameIfPossible(name, /*fromA2dp=*/false); - addAudioDeviceInInventoryIfNeeded(ada); } if (streamType == AudioSystem.STREAM_DEFAULT) { @@ -1562,9 +1474,6 @@ public class AudioDeviceInventory { final int nbDispatchers = mPrefDevDispatchers.beginBroadcast(); for (int i = 0; i < nbDispatchers; i++) { try { - if (!((Boolean) mPrefDevDispatchers.getBroadcastCookie(i))) { - devices = mDeviceBroker.anonymizeAudioDeviceAttributesListUnchecked(devices); - } mPrefDevDispatchers.getBroadcastItem(i).dispatchPrefDevicesChanged( strategy, devices); } catch (RemoteException e) { @@ -1578,9 +1487,6 @@ public class AudioDeviceInventory { final int nbDispatchers = mDevRoleCapturePresetDispatchers.beginBroadcast(); for (int i = 0; i < nbDispatchers; ++i) { try { - if (!((Boolean) mDevRoleCapturePresetDispatchers.getBroadcastCookie(i))) { - devices = mDeviceBroker.anonymizeAudioDeviceAttributesListUnchecked(devices); - } mDevRoleCapturePresetDispatchers.getBroadcastItem(i).dispatchDevicesRoleChanged( capturePreset, role, devices); } catch (RemoteException e) { @@ -1600,42 +1506,6 @@ public class AudioDeviceInventory { return di.mSensorUuid; } } - - /*package*/ String getDeviceSettings() { - int deviceCatalogSize = 0; - synchronized (mDeviceInventoryLock) { - deviceCatalogSize = mDeviceInventory.size(); - - final StringBuilder settingsBuilder = new StringBuilder( - deviceCatalogSize * AdiDeviceState.getPeristedMaxSize()); - - Iterator<AdiDeviceState> iterator = mDeviceInventory.values().iterator(); - if (iterator.hasNext()) { - settingsBuilder.append(iterator.next().toPersistableString()); - } - while (iterator.hasNext()) { - settingsBuilder.append(SETTING_DEVICE_SEPARATOR_CHAR); - settingsBuilder.append(iterator.next().toPersistableString()); - } - return settingsBuilder.toString(); - } - } - - /*package*/ void setDeviceSettings(String settings) { - clearDeviceInventory(); - String[] devSettings = TextUtils.split(Objects.requireNonNull(settings), - SETTING_DEVICE_SEPARATOR); - // small list, not worth overhead of Arrays.stream(devSettings) - for (String setting : devSettings) { - AdiDeviceState devState = AdiDeviceState.fromPersistedString(setting); - // Note if the device is not compatible with spatialization mode or the device - // type is not canonical, it will be ignored in {@link SpatializerHelper}. - if (devState != null) { - addDeviceStateToInventory(devState); - } - } - } - //---------------------------------------------------------- // For tests only |