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

Commit 58a99c6d authored by hughchen's avatar hughchen
Browse files

Use flag to confirm whether callback should unregister

This CL before, this class use isAvailable() to check whether
should register/unregister callback.
UI will be updated when this callback called.
But in one case, the isAvailable() state will not consistent
on onStart() and onStop().
The isAvailable() state will not consistent when user disconnect
device then forgot the device.
It's will cause callback not unregister when meet this case.
Then callback will duplicate call many times cause UI update not smoth.

This CL use flag to confirm the callback will unreigser on onStop().

Bug: 146617530
Bug: 145647143
Test: make -j42 RunSettingsRoboTests
Change-Id: I2b503ca9a7040b94ccc64ae196dad5a228fcbc6a
parent 27be6b2e
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -73,6 +73,8 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
    @VisibleForTesting
    Handler mHandler = new Handler(Looper.getMainLooper());
    @VisibleForTesting
    boolean mIsRegisterCallback = false;
    @VisibleForTesting
    final BluetoothAdapter.OnMetadataChangedListener mMetadataListener =
            new BluetoothAdapter.OnMetadataChangedListener() {
                @Override
@@ -96,6 +98,7 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
        final boolean untetheredHeadset = mCachedDevice != null
                && BluetoothUtils.getBooleanMetaData(
                mCachedDevice.getDevice(), BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET);
        Log.d(TAG, "getAvailabilityStatus() is untethered : " + untetheredHeadset);
        return advancedEnabled && untetheredHeadset ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
    }

@@ -113,6 +116,7 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
        if (!isAvailable()) {
            return;
        }
        mIsRegisterCallback = true;
        mCachedDevice.registerCallback(this);
        mBluetoothAdapter.addOnMetadataChangedListener(mCachedDevice.getDevice(),
                mContext.getMainExecutor(), mMetadataListener);
@@ -120,19 +124,17 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont

    @Override
    public void onStop() {
        if (!isAvailable()) {
        if (!mIsRegisterCallback) {
            return;
        }
        mCachedDevice.unregisterCallback(this);
        mBluetoothAdapter.removeOnMetadataChangedListener(mCachedDevice.getDevice(),
                mMetadataListener);
        mIsRegisterCallback = false;
    }

    @Override
    public void onDestroy() {
        if (!isAvailable()) {
            return;
        }
        // Destroy icon bitmap associated with this header
        for (Bitmap bitmap : mIconCache.values()) {
            if (bitmap != null) {
+5 −13
Original line number Diff line number Diff line
@@ -210,11 +210,8 @@ public class AdvancedBluetoothDetailsHeaderControllerTest {
    }

    @Test
    public void onStop_isAvailable_unregisterCallback() {
        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_UI,
                SettingsUIDeviceConfig.BT_ADVANCED_HEADER_ENABLED, "true", true);
        when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET))
                .thenReturn("true".getBytes());
    public void onStop_isRegisterCallback_unregisterCallback() {
        mController.mIsRegisterCallback = true;

        mController.onStop();

@@ -234,9 +231,8 @@ public class AdvancedBluetoothDetailsHeaderControllerTest {
    }

    @Test
    public void onStop_notAvailable_unregisterCallback() {
        when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET))
                .thenReturn("false".getBytes());
    public void onStop_notRegisterCallback_unregisterCallback() {
        mController.mIsRegisterCallback = false;

        mController.onStop();

@@ -245,11 +241,7 @@ public class AdvancedBluetoothDetailsHeaderControllerTest {
    }

    @Test
    public void onDestroy_isAvailable_recycleBitmap() {
        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_UI,
                SettingsUIDeviceConfig.BT_ADVANCED_HEADER_ENABLED, "true", true);
        when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET))
                .thenReturn("true".getBytes());
    public void onDestroy_recycleBitmap() {
        mController.mIconCache.put(ICON_URI, mBitmap);

        mController.onDestroy();