Try to recycle prefs in battery summary

Bug: 27404159
Change-Id: I5a71413f22e14b8300b6821da661a613392a06e0
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index bc44eeb..1712734 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -28,6 +28,7 @@
 import android.os.UserHandle;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceGroup;
+import android.text.TextUtils;
 import android.util.SparseArray;
 import android.util.TypedValue;
 import android.view.Menu;
@@ -167,9 +168,14 @@
     }
 
     private void addNotAvailableMessage() {
-        Preference notAvailable = new Preference(getPrefContext());
-        notAvailable.setTitle(R.string.power_usage_not_available);
-        mAppListGroup.addPreference(notAvailable);
+        final String NOT_AVAILABLE = "not_available";
+        Preference notAvailable = getCachedPreference(NOT_AVAILABLE);
+        if (notAvailable == null) {
+            notAvailable = new Preference(getPrefContext());
+            notAvailable.setKey(NOT_AVAILABLE);
+            notAvailable.setTitle(R.string.power_usage_not_available);
+            mAppListGroup.addPreference(notAvailable);
+        }
     }
 
     private static boolean isSharedGid(int uid) {
@@ -274,7 +280,7 @@
         super.refreshStats();
         PowerWhitelistBackend powerWhiteist = PowerWhitelistBackend.getInstance();
         updatePreference(mHistPref);
-        mAppListGroup.removeAll();
+        cacheRemoveAllPrefs(mAppListGroup);
         mAppListGroup.setOrderingAsAdded(false);
         boolean addedSome = false;
 
@@ -336,8 +342,16 @@
                         userHandle);
                 final CharSequence contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(),
                         userHandle);
-                final PowerGaugePreference pref = new PowerGaugePreference(getPrefContext(),
-                        badgedIcon, contentDescription, entry);
+                final String key = sipper.drainType == DrainType.APP ? sipper.getPackages() != null
+                        ? TextUtils.concat(sipper.getPackages()).toString()
+                        : String.valueOf(sipper.getUid())
+                        : sipper.drainType.toString();
+                PowerGaugePreference pref = (PowerGaugePreference) getCachedPreference(key);
+                if (pref == null) {
+                    pref = new PowerGaugePreference(getPrefContext(), badgedIcon,
+                            contentDescription, entry);
+                    pref.setKey(key);
+                }
 
                 final double percentOfMax = (sipper.totalPowerMah * 100)
                         / mStatsHelper.getMaxPower();
@@ -368,6 +382,7 @@
         if (!addedSome) {
             addNotAvailableMessage();
         }
+        removeCachedPrefs(mAppListGroup);
 
         BatteryEntry.startRequestQueue();
     }