[Dock Defend] Cache the density in the drawable so we don't re-fetch it
constantly.
Bug: 255625888
Test: verified density got updated each time display size changed
Change-Id: I8cbb9ffe0e3ef18dc43fe13749b7584eb83640e1
diff --git a/packages/SystemUI/src/com/android/systemui/battery/AccessorizedBatteryDrawable.kt b/packages/SystemUI/src/com/android/systemui/battery/AccessorizedBatteryDrawable.kt
index a91100a..b52ddc1 100644
--- a/packages/SystemUI/src/com/android/systemui/battery/AccessorizedBatteryDrawable.kt
+++ b/packages/SystemUI/src/com/android/systemui/battery/AccessorizedBatteryDrawable.kt
@@ -57,6 +57,8 @@
private var shieldLeftOffsetScaled = SHIELD_LEFT_OFFSET
private var shieldTopOffsetScaled = SHIELD_TOP_OFFSET
+ private var density = context.resources.displayMetrics.density
+
private val dualTone =
context.resources.getBoolean(com.android.internal.R.bool.config_batterymeterDualTone)
@@ -126,8 +128,7 @@
} else {
BATTERY_HEIGHT
}
- // TODO(b/255625888): Cache the density so we don't have to re-fetch.
- return (height * context.resources.displayMetrics.density).toInt()
+ return (height * density).toInt()
}
override fun getIntrinsicWidth(): Int {
@@ -137,8 +138,7 @@
} else {
BATTERY_WIDTH
}
- // TODO(b/255625888): Cache the density so we don't have to re-fetch.
- return (width * context.resources.displayMetrics.density).toInt()
+ return (width * density).toInt()
}
override fun draw(c: Canvas) {
@@ -195,6 +195,11 @@
mainBatteryDrawable.setColors(fgColor, bgColor, singleToneColor)
}
+ /** Notifies this drawable that the density might have changed. */
+ fun notifyDensityChanged() {
+ density = context.resources.displayMetrics.density
+ }
+
private fun loadPaths() {
val shieldPathString = context.resources.getString(R.string.config_batterymeterShieldPath)
shieldPath.set(PathParser.createPathFromPathData(shieldPathString))
diff --git a/packages/SystemUI/src/com/android/systemui/battery/BatteryMeterView.java b/packages/SystemUI/src/com/android/systemui/battery/BatteryMeterView.java
index 1ab8453..03d999f 100644
--- a/packages/SystemUI/src/com/android/systemui/battery/BatteryMeterView.java
+++ b/packages/SystemUI/src/com/android/systemui/battery/BatteryMeterView.java
@@ -179,6 +179,7 @@
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
updatePercentView();
+ mDrawable.notifyDensityChanged();
}
public void setColorsFromContext(Context context) {