Remove unnecessary notify in BT preference

notifyHierarchyChanged() is used before when we have
connected/disconnect deivce in same list. So only use it in
DevicePickerFragment.java, not other normal fragments.

Also that call will rebuild whole preference list, which is heavy.

Bug: 119479725
Test: Manual
Change-Id: I06cf221588001b38634fec9f02dee8bc1e561ea8
This commit is contained in:
jackqdyulei
2019-03-29 15:00:07 -07:00
parent d94d72a16c
commit 36948cefe9
4 changed files with 27 additions and 12 deletions

View File

@@ -31,6 +31,7 @@ import android.util.Pair;
import android.util.TypedValue; import android.util.TypedValue;
import android.widget.ImageView; import android.widget.ImageView;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder; import androidx.preference.PreferenceViewHolder;
@@ -58,6 +59,8 @@ 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;
@VisibleForTesting
boolean mNeedNotifyHierarchyChanged = false;
/* Talk-back descriptions for various BT icons */ /* Talk-back descriptions for various BT icons */
Resources mResources; Resources mResources;
@@ -80,8 +83,8 @@ public final class BluetoothDevicePreference extends GearPreference implements
onDeviceAttributesChanged(); onDeviceAttributesChanged();
} }
void rebind() { public void setNeedNotifyHierarchyChanged(boolean needNotifyHierarchyChanged) {
notifyChanged(); mNeedNotifyHierarchyChanged = needNotifyHierarchyChanged;
} }
@Override @Override
@@ -144,8 +147,10 @@ public final class BluetoothDevicePreference extends GearPreference implements
setVisible(mShowDevicesWithoutNames || mCachedDevice.hasHumanReadableName()); setVisible(mShowDevicesWithoutNames || mCachedDevice.hasHumanReadableName());
// This could affect ordering, so notify that // This could affect ordering, so notify that
if (mNeedNotifyHierarchyChanged) {
notifyHierarchyChanged(); notifyHierarchyChanged();
} }
}
@Override @Override
public void onBindViewHolder(PreferenceViewHolder view) { public void onBindViewHolder(PreferenceViewHolder view) {

View File

@@ -36,6 +36,7 @@ import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.WeakHashMap; import java.util.WeakHashMap;
/** /**
@@ -69,8 +70,8 @@ public abstract class DeviceListPreferenceFragment extends
@VisibleForTesting @VisibleForTesting
PreferenceGroup mDeviceListGroup; PreferenceGroup mDeviceListGroup;
final WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap = final HashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
new WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference>(); new HashMap<>();
boolean mShowDevicesWithoutNames; boolean mShowDevicesWithoutNames;
@@ -195,17 +196,13 @@ public abstract class DeviceListPreferenceFragment extends
//Set hideSecondTarget is true if it's bonded device. //Set hideSecondTarget is true if it's bonded device.
preference.hideSecondTarget(true); preference.hideSecondTarget(true);
mDeviceListGroup.addPreference(preference); mDeviceListGroup.addPreference(preference);
} else {
// Tell the preference it is being re-used in case there is new info in the
// cached device.
preference.rebind();
} }
initDevicePreference(preference); initDevicePreference(preference);
mDevicePreferenceMap.put(cachedDevice, preference); mDevicePreferenceMap.put(cachedDevice, preference);
} }
void initDevicePreference(BluetoothDevicePreference preference) { protected void initDevicePreference(BluetoothDevicePreference preference) {
// Does nothing by default // Does nothing by default
} }

View File

@@ -151,6 +151,12 @@ public final class DevicePickerFragment extends DeviceListPreferenceFragment {
} }
} }
@Override
protected void initDevicePreference(BluetoothDevicePreference preference) {
super.initDevicePreference(preference);
preference.setNeedNotifyHierarchyChanged(true);
}
@Override @Override
public void onBluetoothStateChanged(int bluetoothState) { public void onBluetoothStateChanged(int bluetoothState) {
super.onBluetoothStateChanged(bluetoothState); super.onBluetoothStateChanged(bluetoothState);

View File

@@ -166,7 +166,7 @@ public class BluetoothDevicePreferenceTest {
} }
@Test @Test
public void testVisible_showDeviceWithoutNames_visible() { public void isVisible_showDeviceWithoutNames_visible() {
doReturn(false).when(mCachedBluetoothDevice).hasHumanReadableName(); doReturn(false).when(mCachedBluetoothDevice).hasHumanReadableName();
BluetoothDevicePreference preference = BluetoothDevicePreference preference =
new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
@@ -176,11 +176,18 @@ public class BluetoothDevicePreferenceTest {
} }
@Test @Test
public void testVisible_hideDeviceWithoutNames_invisible() { public void isVisible_hideDeviceWithoutNames_invisible() {
doReturn(false).when(mCachedBluetoothDevice).hasHumanReadableName(); doReturn(false).when(mCachedBluetoothDevice).hasHumanReadableName();
BluetoothDevicePreference preference = BluetoothDevicePreference preference =
new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, false); new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, false);
assertThat(preference.isVisible()).isFalse(); assertThat(preference.isVisible()).isFalse();
} }
@Test
public void setNeedNotifyHierarchyChanged_updateValue() {
mPreference.setNeedNotifyHierarchyChanged(true);
assertThat(mPreference.mNeedNotifyHierarchyChanged).isTrue();
}
} }