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

Commit d9f44b8f authored by Hansong Zhang's avatar Hansong Zhang
Browse files

Hearing Aid: change set/get active device(2/3)

* setActiveDevice() returns false in error case, e.g. when the device is
not connected
* add getActiveDevices() instead of isActiveDevice(), which returns a list
that must have two elements: left and right, and each can be null

Test: manual
Bug: 69623109
Change-Id: I1e8541ca5b2030a231bbbea86c71183768a6570f
parent d2b42735
Loading
Loading
Loading
Loading
+58 −24
Original line number Original line Diff line number Diff line
@@ -416,34 +416,68 @@ public class HearingAidService extends ProfileService {
    /**
    /**
     * Set the active device.
     * Set the active device.
     * @param device the new active device
     * @param device the new active device
     * @return true on success, otherwise false
     */
     */
    public void setActiveDevice(BluetoothDevice device) {
    public boolean setActiveDevice(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission");
        if (DBG) {
        if (DBG) {
            Log.d(TAG, "setActiveDevice:" + device);
            Log.d(TAG, "setActiveDevice:" + device);
        }
        }
        synchronized (mStateMachines) {
            if (device == null) {
            if (device == null) {
                if (mActiveDeviceHiSyncId != BluetoothHearingAid.HI_SYNC_ID_INVALID) {
                if (mActiveDeviceHiSyncId != BluetoothHearingAid.HI_SYNC_ID_INVALID) {
                    reportActiveDevice(null);
                    reportActiveDevice(null);
                    mActiveDeviceHiSyncId = BluetoothHearingAid.HI_SYNC_ID_INVALID;
                    mActiveDeviceHiSyncId = BluetoothHearingAid.HI_SYNC_ID_INVALID;
                }
                }
            return;
                return true;
            }
            if (getConnectionState(device) != BluetoothProfile.STATE_CONNECTED) {
                Log.e(TAG, "setActiveDevice(" + device + "): failed because device not connected");
                return false;
            }
            }
            Long deviceHiSyncId = mDeviceHiSyncIdMap.getOrDefault(device,
            Long deviceHiSyncId = mDeviceHiSyncIdMap.getOrDefault(device,
                    BluetoothHearingAid.HI_SYNC_ID_INVALID);
                    BluetoothHearingAid.HI_SYNC_ID_INVALID);
            if (deviceHiSyncId != mActiveDeviceHiSyncId) {
            if (deviceHiSyncId != mActiveDeviceHiSyncId) {
            reportActiveDevice(device);
                mActiveDeviceHiSyncId = deviceHiSyncId;
                mActiveDeviceHiSyncId = deviceHiSyncId;
                reportActiveDevice(device);
            }
            }
        }
        }
        return true;
    }


    boolean isActiveDevice(BluetoothDevice device) {
    /**
     * Get the connected physical Hearing Aid devices that are active
     *
     * @return the list of active devices. The first element is the left active
     * device; the second element is the right active device. If either or both side
     * is not active, it will be null on that position
     */
    List<BluetoothDevice> getActiveDevices() {
        if (DBG) {
        if (DBG) {
            Log.d(TAG, "isActiveDevice:" + device);
            Log.d(TAG, "getActiveDevices");
        }
        ArrayList<BluetoothDevice> activeDevices = new ArrayList<>();
        activeDevices.add(null);
        activeDevices.add(null);
        synchronized (mStateMachines) {
            if (mActiveDeviceHiSyncId == BluetoothHearingAid.HI_SYNC_ID_INVALID) {
                return activeDevices;
            }
            for (BluetoothDevice device : mDeviceHiSyncIdMap.keySet()) {
                if (getConnectionState(device) != BluetoothProfile.STATE_CONNECTED) {
                    continue;
                }
                if (mDeviceHiSyncIdMap.get(device) == mActiveDeviceHiSyncId) {
                    int deviceSide = getCapabilities(device) & 1;
                    if (deviceSide == BluetoothHearingAid.SIDE_RIGHT) {
                        activeDevices.set(1, device);
                    } else {
                        activeDevices.set(0, device);
                    }
                    }
        return getConnectionState(device) == BluetoothProfile.STATE_CONNECTED
                }
                && mActiveDeviceHiSyncId != BluetoothHearingAid.HI_SYNC_ID_INVALID
            }
                && mActiveDeviceHiSyncId == mDeviceHiSyncIdMap.getOrDefault(device,
        }
                BluetoothHearingAid.HI_SYNC_ID_INVALID);
        return activeDevices;
    }
    }


    void messageFromNative(HearingAidStackEvent stackEvent) {
    void messageFromNative(HearingAidStackEvent stackEvent) {
@@ -718,21 +752,21 @@ public class HearingAidService extends ProfileService {
        }
        }


        @Override
        @Override
        public void setActiveDevice(BluetoothDevice device) {
        public boolean setActiveDevice(BluetoothDevice device) {
            HearingAidService service = getService();
            HearingAidService service = getService();
            if (service == null) {
            if (service == null) {
                return;
                return false;
            }
            }
            service.setActiveDevice(device);
            return service.setActiveDevice(device);
        }
        }


        @Override
        @Override
        public boolean isActiveDevice(BluetoothDevice device) {
        public List<BluetoothDevice> getActiveDevices() {
            HearingAidService service = getService();
            HearingAidService service = getService();
            if (service == null) {
            if (service == null) {
                return false;
                return new ArrayList<>();
            }
            }
            return service.isActiveDevice(device);
            return service.getActiveDevices();
        }
        }


        @Override
        @Override
+15 −15
Original line number Original line Diff line number Diff line
@@ -614,23 +614,23 @@ public class HearingAidServiceTest {


        generateConnectionMessageFromNative(mRightDevice, BluetoothProfile.STATE_CONNECTED,
        generateConnectionMessageFromNative(mRightDevice, BluetoothProfile.STATE_CONNECTED,
                BluetoothProfile.STATE_DISCONNECTED);
                BluetoothProfile.STATE_DISCONNECTED);
        Assert.assertTrue(mService.isActiveDevice(mRightDevice));
        Assert.assertTrue(mService.getActiveDevices().contains(mRightDevice));
        Assert.assertFalse(mService.isActiveDevice(mLeftDevice));
        Assert.assertFalse(mService.getActiveDevices().contains(mLeftDevice));


        generateConnectionMessageFromNative(mLeftDevice, BluetoothProfile.STATE_CONNECTED,
        generateConnectionMessageFromNative(mLeftDevice, BluetoothProfile.STATE_CONNECTED,
                BluetoothProfile.STATE_DISCONNECTED);
                BluetoothProfile.STATE_DISCONNECTED);
        Assert.assertTrue(mService.isActiveDevice(mRightDevice));
        Assert.assertTrue(mService.getActiveDevices().contains(mRightDevice));
        Assert.assertTrue(mService.isActiveDevice(mLeftDevice));
        Assert.assertTrue(mService.getActiveDevices().contains(mLeftDevice));


        generateConnectionMessageFromNative(mRightDevice, BluetoothProfile.STATE_DISCONNECTED,
        generateConnectionMessageFromNative(mRightDevice, BluetoothProfile.STATE_DISCONNECTED,
                BluetoothProfile.STATE_CONNECTED);
                BluetoothProfile.STATE_CONNECTED);
        Assert.assertFalse(mService.isActiveDevice(mRightDevice));
        Assert.assertFalse(mService.getActiveDevices().contains(mRightDevice));
        Assert.assertTrue(mService.isActiveDevice(mLeftDevice));
        Assert.assertTrue(mService.getActiveDevices().contains(mLeftDevice));


        generateConnectionMessageFromNative(mLeftDevice, BluetoothProfile.STATE_DISCONNECTED,
        generateConnectionMessageFromNative(mLeftDevice, BluetoothProfile.STATE_DISCONNECTED,
                BluetoothProfile.STATE_CONNECTED);
                BluetoothProfile.STATE_CONNECTED);
        Assert.assertFalse(mService.isActiveDevice(mRightDevice));
        Assert.assertFalse(mService.getActiveDevices().contains(mRightDevice));
        Assert.assertFalse(mService.isActiveDevice(mLeftDevice));
        Assert.assertFalse(mService.getActiveDevices().contains(mLeftDevice));
    }
    }


    @Test
    @Test
@@ -642,19 +642,19 @@ public class HearingAidServiceTest {


        generateConnectionMessageFromNative(mRightDevice, BluetoothProfile.STATE_CONNECTED,
        generateConnectionMessageFromNative(mRightDevice, BluetoothProfile.STATE_CONNECTED,
                BluetoothProfile.STATE_DISCONNECTED);
                BluetoothProfile.STATE_DISCONNECTED);
        Assert.assertTrue(mService.isActiveDevice(mRightDevice));
        Assert.assertTrue(mService.getActiveDevices().contains(mRightDevice));
        Assert.assertFalse(mService.isActiveDevice(mLeftDevice));
        Assert.assertFalse(mService.getActiveDevices().contains(mLeftDevice));


        generateConnectionMessageFromNative(mLeftDevice, BluetoothProfile.STATE_CONNECTED,
        generateConnectionMessageFromNative(mLeftDevice, BluetoothProfile.STATE_CONNECTED,
                BluetoothProfile.STATE_DISCONNECTED);
                BluetoothProfile.STATE_DISCONNECTED);
        Assert.assertTrue(mService.isActiveDevice(mRightDevice));
        Assert.assertTrue(mService.getActiveDevices().contains(mRightDevice));
        Assert.assertTrue(mService.isActiveDevice(mLeftDevice));
        Assert.assertTrue(mService.getActiveDevices().contains(mLeftDevice));


        generateConnectionMessageFromNative(mSingleDevice, BluetoothProfile.STATE_CONNECTED,
        generateConnectionMessageFromNative(mSingleDevice, BluetoothProfile.STATE_CONNECTED,
                BluetoothProfile.STATE_DISCONNECTED);
                BluetoothProfile.STATE_DISCONNECTED);
        Assert.assertFalse(mService.isActiveDevice(mRightDevice));
        Assert.assertFalse(mService.getActiveDevices().contains(mRightDevice));
        Assert.assertFalse(mService.isActiveDevice(mLeftDevice));
        Assert.assertFalse(mService.getActiveDevices().contains(mLeftDevice));
        Assert.assertTrue(mService.isActiveDevice(mSingleDevice));
        Assert.assertTrue(mService.getActiveDevices().contains(mSingleDevice));
    }
    }


    private void connectDevice(BluetoothDevice device) {
    private void connectDevice(BluetoothDevice device) {