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
This commit is contained in:
hughchen
2019-12-26 14:43:56 +08:00
parent 27be6b2e55
commit 58a99c6dab
2 changed files with 11 additions and 17 deletions

View File

@@ -73,6 +73,8 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
@VisibleForTesting @VisibleForTesting
Handler mHandler = new Handler(Looper.getMainLooper()); Handler mHandler = new Handler(Looper.getMainLooper());
@VisibleForTesting @VisibleForTesting
boolean mIsRegisterCallback = false;
@VisibleForTesting
final BluetoothAdapter.OnMetadataChangedListener mMetadataListener = final BluetoothAdapter.OnMetadataChangedListener mMetadataListener =
new BluetoothAdapter.OnMetadataChangedListener() { new BluetoothAdapter.OnMetadataChangedListener() {
@Override @Override
@@ -96,6 +98,7 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
final boolean untetheredHeadset = mCachedDevice != null final boolean untetheredHeadset = mCachedDevice != null
&& BluetoothUtils.getBooleanMetaData( && BluetoothUtils.getBooleanMetaData(
mCachedDevice.getDevice(), BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET); mCachedDevice.getDevice(), BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET);
Log.d(TAG, "getAvailabilityStatus() is untethered : " + untetheredHeadset);
return advancedEnabled && untetheredHeadset ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; return advancedEnabled && untetheredHeadset ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
} }
@@ -113,6 +116,7 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
if (!isAvailable()) { if (!isAvailable()) {
return; return;
} }
mIsRegisterCallback = true;
mCachedDevice.registerCallback(this); mCachedDevice.registerCallback(this);
mBluetoothAdapter.addOnMetadataChangedListener(mCachedDevice.getDevice(), mBluetoothAdapter.addOnMetadataChangedListener(mCachedDevice.getDevice(),
mContext.getMainExecutor(), mMetadataListener); mContext.getMainExecutor(), mMetadataListener);
@@ -120,19 +124,17 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
@Override @Override
public void onStop() { public void onStop() {
if (!isAvailable()) { if (!mIsRegisterCallback) {
return; return;
} }
mCachedDevice.unregisterCallback(this); mCachedDevice.unregisterCallback(this);
mBluetoothAdapter.removeOnMetadataChangedListener(mCachedDevice.getDevice(), mBluetoothAdapter.removeOnMetadataChangedListener(mCachedDevice.getDevice(),
mMetadataListener); mMetadataListener);
mIsRegisterCallback = false;
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
if (!isAvailable()) {
return;
}
// Destroy icon bitmap associated with this header // Destroy icon bitmap associated with this header
for (Bitmap bitmap : mIconCache.values()) { for (Bitmap bitmap : mIconCache.values()) {
if (bitmap != null) { if (bitmap != null) {

View File

@@ -210,11 +210,8 @@ public class AdvancedBluetoothDetailsHeaderControllerTest {
} }
@Test @Test
public void onStop_isAvailable_unregisterCallback() { public void onStop_isRegisterCallback_unregisterCallback() {
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_UI, mController.mIsRegisterCallback = true;
SettingsUIDeviceConfig.BT_ADVANCED_HEADER_ENABLED, "true", true);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET))
.thenReturn("true".getBytes());
mController.onStop(); mController.onStop();
@@ -234,9 +231,8 @@ public class AdvancedBluetoothDetailsHeaderControllerTest {
} }
@Test @Test
public void onStop_notAvailable_unregisterCallback() { public void onStop_notRegisterCallback_unregisterCallback() {
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)) mController.mIsRegisterCallback = false;
.thenReturn("false".getBytes());
mController.onStop(); mController.onStop();
@@ -245,11 +241,7 @@ public class AdvancedBluetoothDetailsHeaderControllerTest {
} }
@Test @Test
public void onDestroy_isAvailable_recycleBitmap() { public void onDestroy_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());
mController.mIconCache.put(ICON_URI, mBitmap); mController.mIconCache.put(ICON_URI, mBitmap);
mController.onDestroy(); mController.onDestroy();