Merge "Use flag to confirm whether callback should unregister"
diff --git a/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java b/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java
index 292b203..bee93fb 100644
--- a/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java
+++ b/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java
@@ -73,6 +73,8 @@
     @VisibleForTesting
     Handler mHandler = new Handler(Looper.getMainLooper());
     @VisibleForTesting
+    boolean mIsRegisterCallback = false;
+    @VisibleForTesting
     final BluetoothAdapter.OnMetadataChangedListener mMetadataListener =
             new BluetoothAdapter.OnMetadataChangedListener() {
                 @Override
@@ -96,6 +98,7 @@
         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 @@
         if (!isAvailable()) {
             return;
         }
+        mIsRegisterCallback = true;
         mCachedDevice.registerCallback(this);
         mBluetoothAdapter.addOnMetadataChangedListener(mCachedDevice.getDevice(),
                 mContext.getMainExecutor(), mMetadataListener);
@@ -120,19 +124,17 @@
 
     @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) {
diff --git a/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java
index 03e9b6f..80ab42c 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java
@@ -210,11 +210,8 @@
     }
 
     @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 @@
     }
 
     @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 @@
     }
 
     @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();