Fix the device on "Media devices" wouldn't update

- This CL use new CachedBluetoothDevice callback instance
  in construct to avoid unregister wrong callback.
- This CL use flag to make sure preference will register
  callback on onAttached() when preference remove callback
  on onPrepareForRemoval() or onDetached().
- Update test case

Bug: 168682778
Bug: 157653997
Test: make -j42 RunSettingsRoboTests
Change-Id: I7a0d9f5332153ee80634e191847b84cd7c380b7d
This commit is contained in:
Hugh Chen
2020-09-16 20:43:40 +08:00
parent 195aa70291
commit cafeab2813
2 changed files with 45 additions and 7 deletions

View File

@@ -52,8 +52,7 @@ import java.lang.annotation.RetentionPolicy;
* BluetoothDevicePreference is the preference type used to display each remote * BluetoothDevicePreference is the preference type used to display each remote
* Bluetooth device in the Bluetooth Settings screen. * Bluetooth device in the Bluetooth Settings screen.
*/ */
public final class BluetoothDevicePreference extends GearPreference implements public final class BluetoothDevicePreference extends GearPreference {
CachedBluetoothDevice.Callback {
private static final String TAG = "BluetoothDevicePref"; private static final String TAG = "BluetoothDevicePref";
private static int sDimAlpha = Integer.MIN_VALUE; private static int sDimAlpha = Integer.MIN_VALUE;
@@ -77,10 +76,20 @@ public final class BluetoothDevicePreference extends GearPreference implements
private AlertDialog mDisconnectDialog; private AlertDialog mDisconnectDialog;
private String contentDescription = null; private String contentDescription = null;
private boolean mHideSecondTarget = false; private boolean mHideSecondTarget = false;
private boolean mIsCallbackRemoved = false;
@VisibleForTesting @VisibleForTesting
boolean mNeedNotifyHierarchyChanged = false; boolean mNeedNotifyHierarchyChanged = false;
/* Talk-back descriptions for various BT icons */ /* Talk-back descriptions for various BT icons */
Resources mResources; Resources mResources;
final BluetoothDevicePreferenceCallback mCallback;
private class BluetoothDevicePreferenceCallback implements CachedBluetoothDevice.Callback {
@Override
public void onDeviceAttributesChanged() {
onPreferenceAttributesChanged();
}
}
public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice, public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice,
boolean showDeviceWithoutNames, @SortType int type) { boolean showDeviceWithoutNames, @SortType int type) {
@@ -96,10 +105,12 @@ public final class BluetoothDevicePreference extends GearPreference implements
} }
mCachedDevice = cachedDevice; mCachedDevice = cachedDevice;
mCallback = new BluetoothDevicePreferenceCallback();
mCachedDevice.registerCallback(mCallback);
mCurrentTime = System.currentTimeMillis(); mCurrentTime = System.currentTimeMillis();
mType = type; mType = type;
onDeviceAttributesChanged(); onPreferenceAttributesChanged();
} }
public void setNeedNotifyHierarchyChanged(boolean needNotifyHierarchyChanged) { public void setNeedNotifyHierarchyChanged(boolean needNotifyHierarchyChanged) {
@@ -126,6 +137,10 @@ public final class BluetoothDevicePreference extends GearPreference implements
@Override @Override
protected void onPrepareForRemoval() { protected void onPrepareForRemoval() {
super.onPrepareForRemoval(); super.onPrepareForRemoval();
if (!mIsCallbackRemoved) {
mCachedDevice.unregisterCallback(mCallback);
mIsCallbackRemoved = true;
}
if (mDisconnectDialog != null) { if (mDisconnectDialog != null) {
mDisconnectDialog.dismiss(); mDisconnectDialog.dismiss();
mDisconnectDialog = null; mDisconnectDialog = null;
@@ -135,13 +150,20 @@ public final class BluetoothDevicePreference extends GearPreference implements
@Override @Override
public void onAttached() { public void onAttached() {
super.onAttached(); super.onAttached();
mCachedDevice.registerCallback(this); if (mIsCallbackRemoved) {
mCachedDevice.registerCallback(mCallback);
mIsCallbackRemoved = false;
}
onPreferenceAttributesChanged();
} }
@Override @Override
public void onDetached() { public void onDetached() {
super.onDetached(); super.onDetached();
mCachedDevice.unregisterCallback(this); if (!mIsCallbackRemoved) {
mCachedDevice.unregisterCallback(mCallback);
mIsCallbackRemoved = true;
}
} }
public CachedBluetoothDevice getBluetoothDevice() { public CachedBluetoothDevice getBluetoothDevice() {
@@ -152,7 +174,7 @@ public final class BluetoothDevicePreference extends GearPreference implements
mHideSecondTarget = hideSecondTarget; mHideSecondTarget = hideSecondTarget;
} }
public void onDeviceAttributesChanged() { private void onPreferenceAttributesChanged() {
/* /*
* The preference framework takes care of making sure the value has * The preference framework takes care of making sure the value has
* changed before proceeding. It will also call notifyChanged() if * changed before proceeding. It will also call notifyChanged() if

View File

@@ -17,10 +17,12 @@ package com.android.settings.bluetooth;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -35,7 +37,6 @@ import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.testutils.DrawableTestHelper;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -261,4 +262,19 @@ public class BluetoothDevicePreferenceTest {
assertThat(mPreferenceList.get(2).getCachedDevice().getAddress()) assertThat(mPreferenceList.get(2).getCachedDevice().getAddress())
.isEqualTo(preference3.getCachedDevice().getAddress()); .isEqualTo(preference3.getCachedDevice().getAddress());
} }
@Test
public void onAttached_callbackNotRemoved_doNotRegisterCallback() {
mPreference.onAttached();
verify(mCachedBluetoothDevice, never()).unregisterCallback(any());
}
@Test
public void onAttached_callbackRemoved_registerCallback() {
mPreference.onPrepareForRemoval();
mPreference.onAttached();
verify(mCachedBluetoothDevice, times(2)).registerCallback(any());
}
} }