Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 88ba6914 authored by William Escande's avatar William Escande
Browse files

AICS: cleanup and preliminary changes

Also:
* inject BluetoothDevice and Native Interface in InputDescriptor
* re-order volumecontrol binder method to match aidl order
* use concurrentHashMap to store InputDescriptors
* rename getSettingxxxSetting
* add the constant in framework bp and in jarjar
* clean some logs and typos

Bug: 372328699
Flag: com.android.bluetooth.flags.aics_api
Test: atest CtsBluetoothTestCases
Change-Id: I38d8867f7d8d95c6db64428c5b82edf4054da93f
parent 9e163431
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -243,6 +243,7 @@ android_app {
    jni_uses_platform_apis: true,
    libs: [
        "app-compat-annotations",
        "bluetooth_constants_java",
        "bluetooth_flags_java_lib",
        "error_prone_annotations",
        "framework-annotations-lib",
@@ -271,7 +272,6 @@ android_app {
        "bluetooth-protos-lite",
        "bluetooth.change-ids",
        "bluetooth.mapsapi",
        "bluetooth_constants_java",
        "com.android.obex",
        "com.android.vcard",
        "guava",
+18 −9
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.bluetooth.vc;

import static java.util.Objects.requireNonNull;

import android.bluetooth.BluetoothDevice;
import android.util.Log;

import com.android.bluetooth.btservice.ProfileService;
@@ -28,9 +31,16 @@ import bluetooth.constants.aics.Mute;
class VolumeControlInputDescriptor {
    private static final String TAG = VolumeControlInputDescriptor.class.getSimpleName();

    final VolumeControlNativeInterface mNativeInterface;
    final BluetoothDevice mDevice;
    final Descriptor[] mVolumeInputs;

    VolumeControlInputDescriptor(int numberOfExternalInputs) {
    VolumeControlInputDescriptor(
            VolumeControlNativeInterface nativeInterface,
            BluetoothDevice device,
            int numberOfExternalInputs) {
        mNativeInterface = requireNonNull(nativeInterface);
        mDevice = requireNonNull(device);
        mVolumeInputs = new Descriptor[numberOfExternalInputs];
        // Stack delivers us number of audio inputs. ids are countinous from [0;n[
        for (int i = 0; i < numberOfExternalInputs; i++) {
@@ -57,8 +67,8 @@ class VolumeControlInputDescriptor {
         */
        int mGainSettingsUnits = 0;

        int mGainSettingsMaxSetting = 0;
        int mGainSettingsMinSetting = 0;
        int mGainSettingsMax = 0;
        int mGainSettingsMin = 0;

        String mDescription = "";
    }
@@ -119,8 +129,8 @@ class VolumeControlInputDescriptor {
        if (!isValidId(id)) return;

        mVolumeInputs[id].mGainSettingsUnits = gainUnit;
        mVolumeInputs[id].mGainSettingsMinSetting = gainMin;
        mVolumeInputs[id].mGainSettingsMaxSetting = gainMax;
        mVolumeInputs[id].mGainSettingsMin = gainMin;
        mVolumeInputs[id].mGainSettingsMax = gainMax;
    }

    void setState(int id, int gainSetting, int mute, int gainMode) {
@@ -128,8 +138,7 @@ class VolumeControlInputDescriptor {

        Descriptor desc = mVolumeInputs[id];

        if (gainSetting > desc.mGainSettingsMaxSetting
                || gainSetting < desc.mGainSettingsMinSetting) {
        if (gainSetting > desc.mGainSettingsMax || gainSetting < desc.mGainSettingsMin) {
            Log.e(TAG, "Request fail. Illegal gainSetting argument: " + gainSetting);
            return;
        }
@@ -150,8 +159,8 @@ class VolumeControlInputDescriptor {
            ProfileService.println(sb, "        gainMode: " + desc.mGainMode);
            ProfileService.println(sb, "        mute: " + desc.mMute);
            ProfileService.println(sb, "        units:" + desc.mGainSettingsUnits);
            ProfileService.println(sb, "        minGain:" + desc.mGainSettingsMinSetting);
            ProfileService.println(sb, "        maxGain:" + desc.mGainSettingsMaxSetting);
            ProfileService.println(sb, "        minGain:" + desc.mGainSettingsMin);
            ProfileService.println(sb, "        maxGain:" + desc.mGainSettingsMax);
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -182,7 +182,7 @@ public class VolumeControlNativeInterface {
    private native boolean setExtAudioOutDescriptionNative(
            byte[] address, int externalOutputId, String descr);

    /* Native methods for external audio inputs */
    /* Native methods for audio inputs control service */
    private native boolean getExtAudioInStateNative(byte[] address, int externalInputId);

    private native boolean getExtAudioInStatusNative(byte[] address, int externalInputId);
+24 −18
Original line number Diff line number Diff line
@@ -74,6 +74,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
@@ -107,7 +108,8 @@ public class VolumeControlService extends ProfileService {
    private final Map<BluetoothDevice, VolumeControlStateMachine> mStateMachines = new HashMap<>();
    private final Map<BluetoothDevice, VolumeControlOffsetDescriptor> mAudioOffsets =
            new HashMap<>();
    private final Map<BluetoothDevice, VolumeControlInputDescriptor> mAudioInputs = new HashMap<>();
    private final Map<BluetoothDevice, VolumeControlInputDescriptor> mAudioInputs =
            new ConcurrentHashMap<>();
    private final Map<Integer, Integer> mGroupVolumeCache = new HashMap<>();
    private final Map<Integer, Boolean> mGroupMuteCache = new HashMap<>();
    private final Map<BluetoothDevice, Integer> mDeviceVolumeCache = new HashMap<>();
@@ -948,7 +950,9 @@ public class VolumeControlService extends ProfileService {
            return;
        }

        mAudioInputs.put(device, new VolumeControlInputDescriptor(numberOfExternalInputs));
        mAudioInputs.put(
                device,
                new VolumeControlInputDescriptor(mNativeInterface, device, numberOfExternalInputs));
    }

    void handleDeviceAvailable(
@@ -1054,9 +1058,9 @@ public class VolumeControlService extends ProfileService {
    void onExtAudioInStatusChanged(BluetoothDevice device, int id, int status) {
        String logInfo =
                "onExtAudioInStatusChanged("
                        + ("device:" + device)
                        + (", id" + id)
                        + (", status" + status)
                        + ("device=" + device)
                        + (", id=" + id)
                        + (", status=" + status)
                        + ")";

        VolumeControlInputDescriptor input = mAudioInputs.get(device);
@@ -1077,9 +1081,9 @@ public class VolumeControlService extends ProfileService {
    void onExtAudioInTypeChanged(BluetoothDevice device, int id, int type) {
        String logInfo =
                "onExtAudioInTypeChanged("
                        + ("device:" + device)
                        + (", id" + id)
                        + (", type" + type)
                        + ("device=" + device)
                        + (", id=" + id)
                        + (", type=" + type)
                        + ")";

        VolumeControlInputDescriptor input = mAudioInputs.get(device);
@@ -1100,9 +1104,9 @@ public class VolumeControlService extends ProfileService {
    void onExtAudioInDescriptionChanged(BluetoothDevice device, int id, String description) {
        String logInfo =
                "onExtAudioInDescriptionChanged("
                        + ("device:" + device)
                        + (", id" + id)
                        + (", description" + description)
                        + ("device=" + device)
                        + (", id=" + id)
                        + (", description=" + description)
                        + ")";

        VolumeControlInputDescriptor input = mAudioInputs.get(device);
@@ -1123,9 +1127,11 @@ public class VolumeControlService extends ProfileService {
    void onExtAudioInGainPropsChanged(BluetoothDevice device, int id, int unit, int min, int max) {
        String logInfo =
                "onExtAudioInGainPropsChanged("
                        + ("device:" + device)
                        + (", id" + id)
                        + (" unit: " + unit + " min" + min + " max:" + max)
                        + ("device=" + device)
                        + (", id=" + id)
                        + (", unit=" + unit)
                        + (", min=" + min)
                        + (", max=" + max)
                        + ")";

        VolumeControlInputDescriptor input = mAudioInputs.get(device);
@@ -1652,7 +1658,7 @@ public class VolumeControlService extends ProfileService {
        }

        @Override
        public void notifyNewRegisteredCallback(
        public void unregisterCallback(
                IBluetoothVolumeControlCallback callback, AttributionSource source) {
            requireNonNull(callback);

@@ -1662,11 +1668,11 @@ public class VolumeControlService extends ProfileService {
            }

            service.enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, null);
            postAndWait(service.mHandler, () -> service.notifyNewRegisteredCallback(callback));
            postAndWait(service.mHandler, () -> service.unregisterCallback(callback));
        }

        @Override
        public void unregisterCallback(
        public void notifyNewRegisteredCallback(
                IBluetoothVolumeControlCallback callback, AttributionSource source) {
            requireNonNull(callback);

@@ -1676,7 +1682,7 @@ public class VolumeControlService extends ProfileService {
            }

            service.enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, null);
            postAndWait(service.mHandler, () -> service.unregisterCallback(callback));
            postAndWait(service.mHandler, () -> service.notifyNewRegisteredCallback(callback));
        }
    }

+15 −2
Original line number Diff line number Diff line
@@ -19,34 +19,47 @@ import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.*;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.platform.test.flag.junit.SetFlagsRule;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.TestUtils;

import bluetooth.constants.aics.Mute;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class VolumeControlInputDescriptorTest {
    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    @Mock private VolumeControlNativeInterface mNativeInterface;

    private static final int NUMBER_OF_INPUT = 3;
    private static final int NUMBER_OF_FIELD_IN_STRUCT = 9;
    private static final int VALID_ID = 1;
    private static final int INVALID_ID = NUMBER_OF_INPUT;
    private static final int INVALID_ID2 = -1;

    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
    private final BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
    private final BluetoothDevice mDevice = TestUtils.getTestDevice(mAdapter, 0x42);

    private VolumeControlInputDescriptor mDescriptor;

    @Before
    public void setUp() {
        mDescriptor = new VolumeControlInputDescriptor(NUMBER_OF_INPUT);
        mDescriptor = new VolumeControlInputDescriptor(mNativeInterface, mDevice, NUMBER_OF_INPUT);
    }

    @Test
Loading