From d1dd4cdf2c32a6e49c43858150b51cebf07765ea Mon Sep 17 00:00:00 2001 From: ykhung Date: Wed, 6 Oct 2021 10:35:50 +0800 Subject: [PATCH] Replace the cached MotionEvent with getX() value to improve GC state avoid to cache the whole MotionEvent, since it will clone the whole instance fields again to cause GC triggering, since we only need the getX() value in the MotionEvent, rather than the whole data Bug: 195306545 Test: make SettingsRoboTests Change-Id: Ic3840b0a4eb6587ec229ba3de2c62ec868d1a535 --- .../android/settings/fuelgauge/BatteryChartView.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/com/android/settings/fuelgauge/BatteryChartView.java b/src/com/android/settings/fuelgauge/BatteryChartView.java index 7bc60b23c38..5336fd5c034 100644 --- a/src/com/android/settings/fuelgauge/BatteryChartView.java +++ b/src/com/android/settings/fuelgauge/BatteryChartView.java @@ -108,7 +108,7 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick Paint mTrapezoidCurvePaint = null; private TrapezoidSlot[] mTrapezoidSlots; // Records the location to calculate selected index. - private MotionEvent mTouchUpEvent; + private float mTouchUpEventX = Float.MIN_VALUE; private BatteryChartView.OnSelectListener mOnSelectListener; public BatteryChartView(Context context) { @@ -255,20 +255,20 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick // Caches the location to calculate selected trapezoid index. final int action = event.getAction(); if (action == MotionEvent.ACTION_UP) { - mTouchUpEvent = MotionEvent.obtain(event); + mTouchUpEventX = event.getX(); } else if (action == MotionEvent.ACTION_CANCEL) { - mTouchUpEvent = null; // reset + mTouchUpEventX = Float.MIN_VALUE; // reset } return super.onTouchEvent(event); } @Override public void onClick(View view) { - if (mTouchUpEvent == null) { + if (mTouchUpEventX == Float.MIN_VALUE) { Log.w(TAG, "invalid motion event for onClick() callback"); return; } - final int trapezoidIndex = getTrapezoidIndex(mTouchUpEvent.getX()); + final int trapezoidIndex = getTrapezoidIndex(mTouchUpEventX); // Ignores the click event if the level is zero. if (trapezoidIndex == SELECTED_INDEX_INVALID || !isValidToDraw(trapezoidIndex)) {